1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 #include <X11/Xutil.h>
47 #include <X11/extensions/Xdamage.h>
53 #include <sys/types.h>
55 #include <netinet/in.h>
67 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
70 #define CONFIG_FILE "$HOME/.conkyrc"
71 #define MAIL_FILE "$MAIL"
72 #define MAX_IF_BLOCK_DEPTH 5
74 /* #define SIGNAL_BLOCKING */
75 #undef SIGNAL_BLOCKING
77 static void print_version()
79 printf("Conky %s compiled %s for %s\n", VERSION, BUILD_DATE, BUILD_ARCH);
81 printf("\nCompiled in features:\n\n"
82 "System config file: %s\n\n"
86 " * Xdamage extension\n"
87 # endif /* HAVE_XDAMAGE */
89 " * Xdbe extension (double buffer)\n"
90 # endif /* HAVE_XDBE */
95 "\n Music detection:\n"
98 #endif /* AUDACIOUS */
108 "\n General features:\n"
112 #ifdef TCP_PORT_MONITOR
114 #endif /* TCP_PORT_MONITOR */
120 #endif /* HAVE_IWLIB */
124 "", SYSTEM_CONFIG_FILE
134 static int text_start_x, text_start_y; /* text start position in window */
135 static int text_width, text_height;
153 char name[DEFAULT_TEXT_BUFFER_SIZE];
163 static int selected_font = 0;
164 static int font_count = -1;
165 struct font_list *fonts = NULL;
167 static char *suffixes[] = { "B", "kiB", "MiB", "GiB", "TiB", "PiB", "" };
171 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
172 fonts[selected_font].xftfont->descent) \
173 : (fonts[selected_font].font->max_bounds.ascent + \
174 fonts[selected_font].font->max_bounds.descent))
175 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
176 : fonts[selected_font].font->max_bounds.ascent)
177 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
178 : fonts[selected_font].font->max_bounds.descent)
182 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
183 fonts[selected_font].font->max_bounds.descent)
184 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
185 #define font_descent() fonts[selected_font].font->max_bounds.descent
189 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
191 static void set_font();
193 int addfont(const char *data_in)
195 if (font_count > MAX_FONTS) {
196 CRIT_ERR("you don't need that many fonts, sorry.");
199 if (font_count == 0) {
203 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
207 memset(fonts, 0, sizeof(struct font_list));
209 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
210 memset(&fonts[font_count], 0, sizeof(struct font_list));
212 CRIT_ERR("realloc in addfont");
214 // must account for null terminator
215 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
216 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
218 fonts[font_count].font_alpha = 0xffff;
221 CRIT_ERR("Oops...looks like something overflowed in addfont().");
226 void set_first_font(const char *data_in)
228 if (font_count < 0) {
229 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
233 memset(fonts, 0, sizeof(struct font_list));
236 if (strlen(data_in) > 1) {
237 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE - 1);
239 fonts[0].font_alpha = 0xffff;
248 for (i = 0; i <= font_count; i++) {
251 XftFontClose(display, fonts[i].xftfont);
252 fonts[i].xftfont = 0;
256 XFreeFont(display, fonts[i].font);
266 static void load_fonts()
270 for (i = 0; i <= font_count; i++) {
273 if (use_xft && fonts[i].xftfont) {
275 } else if (use_xft) {
276 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
277 XftFontClose(display, fonts[i].xftfont);
279 fonts[i].xftfont = XftFontOpenName(display, screen,
281 if (fonts[i].xftfont != NULL) {
285 ERR("can't load Xft font '%s'", fonts[i].name);
286 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
287 "courier-12")) != NULL) {
291 ERR("can't load Xft font '%s'", "courier-12");
293 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
294 CRIT_ERR("can't load font '%s'", "fixed");
301 /* load normal font */
302 /* if (fonts[i].font != NULL) {
303 XFreeFont(display, fonts[i].font);
306 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
307 ERR("can't load font '%s'", fonts[i].name);
308 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
309 CRIT_ERR("can't load font '%s'", "fixed");
310 printf("loaded fixed?\n");
318 /* default config file */
319 static char *current_config;
321 /* set to 1 if you want all text to be in uppercase */
322 static unsigned int stuff_in_upper_case;
324 /* Run how many times? */
325 static unsigned long total_run_times;
328 static int fork_to_background;
330 static int cpu_avg_samples, net_avg_samples;
334 /* Position on the screen */
335 static int text_alignment;
336 static int gap_x, gap_y;
339 static int draw_borders;
340 static int draw_graph_borders;
341 static int stippled_borders;
343 static int draw_shades, draw_outline;
345 static int border_margin, border_width;
347 static long default_fg_color, default_bg_color, default_out_color;
349 /* create own window or draw stuff to root? */
350 static int set_transparent = 0;
353 static int own_window = 0;
354 static int background_colour = 0;
356 /* fixed size/pos is set if wm/user changes them */
357 static int fixed_size = 0, fixed_pos = 0;
360 static int minimum_width, minimum_height;
361 static int maximum_width;
366 static int sensor_device;
369 static long color0, color1, color2, color3, color4, color5, color6, color7,
372 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
373 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
375 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
376 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
378 /* maximum size of individual text buffers, ie $exec buffer size */
379 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
382 #define CODEPAGE_LENGTH 20
384 long iconv_count = 0;
385 char iconv_converting;
386 static iconv_t **iconv_cd = 0;
388 int register_iconv(iconv_t *new_iconv)
391 iconv_cd = realloc(iconv, sizeof(iconv_t *) * (iconv_count + 1));
393 iconv_cd = malloc(sizeof(iconv_t *));
396 CRIT_ERR("Out of memory");
398 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
399 if (!iconv_cd[iconv_count]) {
400 CRIT_ERR("Out of memory");
402 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
407 void free_iconv(void)
412 for (i = 0; i < iconv_count; i++) {
414 iconv_close(*iconv_cd[i]);
428 /* no buffers in used memory? */
431 /* pad percentages to decimals? */
432 static int pad_percents = 0;
434 #ifdef TCP_PORT_MONITOR
435 tcp_port_monitor_args_t tcp_port_monitor_args;
438 static char *text = 0;
441 static int total_updates;
444 static int blockdepth = 0;
445 static int if_jumped = 0;
446 static int blockstart[MAX_IF_BLOCK_DEPTH];
448 int check_contains(char *f, char *s)
451 FILE *where = fopen(f, "r");
454 char buf1[256], buf2[256];
456 while (fgets(buf1, 256, where)) {
457 sscanf(buf1, "%255s", buf2);
458 if (strstr(buf2, s)) {
465 ERR("Could not open the file");
471 static inline int calc_text_width(const char *s, int l)
478 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
479 (FcChar8 *) s, l, &gi);
481 XftTextExtents8(display, fonts[selected_font].xftfont,
482 (FcChar8 *) s, l, &gi);
488 return XTextWidth(fonts[selected_font].font, s, l);
493 /* formatted text to render on screen, generated in generate_text(),
494 * drawn in draw_stuff() */
496 static char *text_buffer;
498 /* special stuff in text_buffer */
500 #define SPECIAL_CHAR '\x01'
528 unsigned long first_colour; // for graph gradient
529 unsigned long last_colour;
533 /* create specials array on heap instead of stack with introduction of
535 static struct special_t *specials = NULL;
537 static unsigned int special_count;
540 static unsigned int special_index; /* used when drawing */
543 /* why 256? cause an array of more then 256 doubles seems excessive,
544 * and who needs that kind of precision anyway? */
545 #define MAX_GRAPH_DEPTH 256
547 static struct special_t *new_special(char *buf, enum special_types t)
549 if (special_count >= max_specials) {
550 CRIT_ERR("too many special things in text");
553 buf[0] = SPECIAL_CHAR;
555 specials[special_count].type = t;
556 return &specials[special_count++];
559 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
563 unsigned int count = 0;
564 static char buf[BUFSZ];
565 long orig_pos = ftell(fp);
567 long buf_size = BUFSZ;
568 char *cur_found = NULL;
570 while (count < step) {
571 if (cur_found == NULL) {
572 buf_size = fread(buf, 1, buf_size, fp);
575 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
576 if (cur_found != NULL) {
577 buf_pos = cur_found - buf + 1;
586 ret = ftell(fp) - buf_size + buf_pos - 1;
588 fseek(fp, orig_pos, SEEK_SET);
594 void *memrchr(const void *buffer, char c, size_t n)
596 const unsigned char *p = buffer;
598 for (p += n; n; n--) {
607 long rev_fcharfind(FILE *fp, char val, unsigned int step)
611 unsigned int count = 0;
612 static char buf[BUFSZ];
613 long orig_pos = ftell(fp);
615 long file_pos = orig_pos;
616 long buf_size = BUFSZ;
619 while (count < step) {
621 if (file_pos > BUFSZ) {
622 fseek(fp, file_pos - BUFSZ, SEEK_SET);
625 fseek(fp, 0, SEEK_SET);
627 file_pos = ftell(fp);
628 buf_pos = fread(buf, 1, buf_size, fp);
630 cur_found = memrchr(buf, val, (size_t) buf_pos);
631 if (cur_found != NULL) {
632 buf_pos = cur_found - buf;
641 fseek(fp, orig_pos, SEEK_SET);
643 ret = file_pos + buf_pos;
649 static void new_bar(char *buf, int w, int h, int usage)
651 struct special_t *s = new_special(buf, BAR);
653 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
658 static const char *scan_bar(const char *args, int *w, int *h)
660 /* zero width means all space that is available */
663 /* bar's argument is either height or height,width */
667 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
668 sscanf(args, "%d %n", h, &n);
676 static char *scan_font(const char *args)
686 static void new_font(char *buf, char *args)
689 struct special_t *s = new_special(buf, FONT);
691 if (s->font_added > font_count || !s->font_added || strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE)) {
692 int tmp = selected_font;
694 selected_font = s->font_added = addfont(args);
699 struct special_t *s = new_special(buf, FONT);
700 int tmp = selected_font;
702 selected_font = s->font_added = 0;
708 inline void graph_append(struct special_t *graph, double f)
710 if (!graph->scaled && f > graph->graph_scale) {
711 f = graph->graph_scale;
716 graph->graph_scale = 1;
718 graph->graph[0] = f; /* add new data */
719 /* shift all the data by 1 */
720 for (i = graph->graph_width - 1; i > 0; i--) {
721 graph->graph[i] = graph->graph[i - 1];
722 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
723 /* check if we need to update the scale */
724 graph->graph_scale = graph->graph[i];
729 short colour_depth = 0;
730 void set_up_gradient();
732 /* precalculated: 31/255, and 63/255 */
733 #define CONST_8_TO_5_BITS 0.12156862745098
734 #define CONST_8_TO_6_BITS 0.247058823529412
736 /* adjust color values depending on color depth */
737 static unsigned int adjust_colors(unsigned int color)
741 if (colour_depth == 0) {
744 if (colour_depth == 16) {
745 r = (color & 0xff0000) >> 16;
746 g = (color & 0xff00) >> 8;
748 color = (int) (r * CONST_8_TO_5_BITS) << 11;
749 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
750 color |= (int) (b * CONST_8_TO_5_BITS);
755 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
756 unsigned int second_colour, double i, int scale, int append)
758 struct special_t *s = new_special(buf, GRAPH);
761 if (s->graph == NULL) {
762 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
763 // subtract 2 for the box
764 s->graph_width = s->width /* - 2 */;
766 s->graph_width = MAX_GRAPH_DEPTH - 2;
768 s->graph = malloc(s->graph_width * sizeof(double));
769 memset(s->graph, 0, s->graph_width * sizeof(double));
770 s->graph_scale = 100;
773 s->first_colour = adjust_colors(first_colour);
774 s->last_colour = adjust_colors(second_colour);
781 s->graph_width = s->width - 2; // subtract 2 for rectangle around
786 s->graph_scale = scale;
793 static char *scan_graph(const char *args, int *w, int *h,
794 unsigned int *first_colour, unsigned int *last_colour,
799 /* zero width means all space that is available */
805 /* graph's argument is either height or height,width */
807 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour,
812 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
815 if (sscanf(args, "%63s %d,%d %x %x %i", buf, h, w, first_colour,
816 last_colour, scale) == 6) {
820 if (sscanf(args, "%63s %d,%d %x %x", buf, h, w, first_colour,
827 if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) == 3) {
831 if (sscanf(args, "%x %x", first_colour, last_colour) == 2) {
834 if (sscanf(args, "%63s %x %x %i", buf, first_colour, last_colour,
839 if (sscanf(args, "%63s %x %x", buf, first_colour, last_colour) == 3) {
845 if (sscanf(args, "%d,%d %i", h, w, scale) == 3) {
849 if (sscanf(args, "%d,%d", h, w) == 2) {
852 if (sscanf(args, "%63s %d,%d %i", buf, h, w, scale) < 4) {
854 //TODO: check the return value and throw an error?
855 sscanf(args, "%63s %d,%d", buf, h, w);
861 if (buf[0] == '\0') {
868 static inline void new_hr(char *buf, int a)
870 new_special(buf, HORIZONTAL_LINE)->height = a;
873 static inline void new_stippled_hr(char *buf, int a, int b)
875 struct special_t *s = new_special(buf, STIPPLED_HR);
881 static inline void new_fg(char *buf, long c)
883 new_special(buf, FG)->arg = c;
886 static inline void new_bg(char *buf, long c)
888 new_special(buf, BG)->arg = c;
891 static inline void new_outline(char *buf, long c)
893 new_special(buf, OUTLINE)->arg = c;
896 static inline void new_offset(char *buf, long c)
898 new_special(buf, OFFSET)->arg = c;
901 static inline void new_voffset(char *buf, long c)
903 new_special(buf, VOFFSET)->arg = c;
906 static inline void new_alignr(char *buf, long c)
908 new_special(buf, ALIGNR)->arg = c;
911 static inline void new_alignc(char *buf, long c)
913 new_special(buf, ALIGNC)->arg = c;
916 static inline void new_goto(char *buf, long c)
918 new_special(buf, GOTO)->arg = c;
921 static inline void new_tab(char *buf, int a, int b)
923 struct special_t *s = new_special(buf, TAB);
929 /* quite boring functions */
931 static inline void for_each_line(char *b, void f(char *))
935 for (ps = b, pe = b; *pe; pe++) {
949 static void convert_escapes(char *buf)
951 char *p = buf, *s = buf;
958 } else if (*s == '\\') {
969 /* Prints anything normally printed with snprintf according to the current value
970 * of use_spacer. Actually slightly more flexible than snprintf, as you can
971 * safely specify the destination buffer as one of your inputs. */
972 static int spaced_print(char *buf, int size, char *format, int width,
973 char *func_name, ...) {
979 char *tempbuf = malloc(size * sizeof(char));
981 // Passes the varargs along to vsnprintf
982 va_start(argp, func_name);
983 vsnprintf(tempbuf, size, format, argp);
986 switch (use_spacer) {
988 len = snprintf(buf, size, "%s", tempbuf);
991 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
994 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
997 CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
1006 /* converts from bytes to human readable format (k, M, G, T) */
1007 static void human_readable(long long num, char *buf, int size, char *func_name)
1009 char **suffix = suffixes;
1012 static const int WIDTH = 10, SHORT_WIDTH = 8;
1016 spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num,
1019 spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix);
1024 while (num / 1024 >= 1000LL && **(suffix + 2)) {
1030 fnum = num / 1024.0;
1035 if (precision < 0) {
1039 len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name,
1040 precision, fnum, **suffix);
1042 len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision,
1045 } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
1050 enum text_object_type {
1052 #if defined(__linux__)
1054 #endif /* __linux__ */
1064 OBJ_battery_percent,
1066 #endif /* !__OpenBSD__ */
1081 OBJ_conky_build_date,
1082 OBJ_conky_build_arch,
1091 OBJ_diskiograph_read,
1092 OBJ_diskiograph_write,
1130 #if defined(__linux__)
1136 OBJ_i8k_left_fan_status,
1137 OBJ_i8k_right_fan_status,
1138 OBJ_i8k_left_fan_rpm,
1139 OBJ_i8k_right_fan_rpm,
1141 OBJ_i8k_buttons_status,
1157 OBJ_wireless_bitrate,
1159 OBJ_wireless_link_qual,
1160 OBJ_wireless_link_qual_max,
1161 OBJ_wireless_link_qual_perc,
1162 OBJ_wireless_link_bar,
1163 #endif /* __linux__ */
1192 OBJ_running_processes,
1201 OBJ_temp1, /* i2c is used instead in these */
1225 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1226 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1228 OBJ_apm_battery_time,
1229 OBJ_apm_battery_life,
1230 #endif /* __FreeBSD__ __OpenBSD__ */
1232 OBJ_obsd_sensors_temp,
1233 OBJ_obsd_sensors_fan,
1234 OBJ_obsd_sensors_volt,
1237 #endif /* __OpenBSD__ */
1259 OBJ_music_player_interval,
1279 OBJ_xmms2_timesplayed,
1282 OBJ_audacious_status,
1283 OBJ_audacious_title,
1284 OBJ_audacious_length,
1285 OBJ_audacious_length_seconds,
1286 OBJ_audacious_position,
1287 OBJ_audacious_position_seconds,
1288 OBJ_audacious_bitrate,
1289 OBJ_audacious_frequency,
1290 OBJ_audacious_channels,
1291 OBJ_audacious_filename,
1292 OBJ_audacious_playlist_length,
1293 OBJ_audacious_playlist_position,
1307 #ifdef TCP_PORT_MONITOR
1321 OBJ_if_smapi_bat_installed,
1324 OBJ_entropy_poolsize,
1328 struct text_object {
1330 char *s; /* some string */
1331 int i; /* some integer */
1332 long l; /* some other integer */
1333 unsigned int sensor;
1334 struct net_stat *net;
1336 struct diskio_stat *diskio;
1337 unsigned char loadavg[3];
1338 unsigned int cpu_index;
1339 struct mail_s *mail;
1347 char *tz; /* timezone variable */
1348 char *fmt; /* time display formatting */
1386 /* If not -1, a file descriptor to read from when
1387 * logfile is a FIFO. */
1404 timed_thread *p_timed_thread;
1410 #ifdef TCP_PORT_MONITOR
1412 /* starting port to monitor */
1413 in_port_t port_range_begin;
1414 /* ending port to monitor */
1415 in_port_t port_range_end;
1416 /* enum from libtcp-portmon.h, e.g. COUNT, etc. */
1418 /* 0 to n-1 connections. */
1419 int connection_index;
1437 struct local_mail_s local_mail;
1442 unsigned int c, d, e;
1447 struct text_object_list {
1448 unsigned int text_object_count;
1449 struct text_object *text_objects;
1452 static unsigned int text_object_count;
1453 static struct text_object *text_objects;
1454 static void generate_text_internal(char *p, int p_max_size,
1455 struct text_object *objs, unsigned int object_count,
1456 struct information *cur);
1458 #define MAXDATASIZE 1000
1462 struct mail_s *parse_mail_args(char type, const char *arg)
1464 struct mail_s *mail;
1465 mail = malloc(sizeof(struct mail_s));
1466 memset(mail, 0, sizeof(struct mail_s));
1469 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
1472 ERR("Scanning IMAP args failed");
1473 } else if (type == IMAP) {
1474 ERR("Scanning POP3 args failed");
1477 // see if password needs prompting
1478 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1479 int fp = fileno(stdin);
1480 struct termios term;
1482 tcgetattr(fp, &term);
1483 term.c_lflag &= ~ECHO;
1484 tcsetattr(fp, TCSANOW, &term);
1485 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1486 scanf("%128s", mail->pass);
1488 term.c_lflag |= ECHO;
1489 tcsetattr(fp, TCSANOW, &term);
1491 // now we check for optional args
1492 tmp = strstr(arg, "-i ");
1495 sscanf(tmp, "%f", &mail->interval);
1497 mail->interval = 300; // 5 minutes
1499 tmp = strstr(arg, "-p ");
1502 sscanf(tmp, "%lu", &mail->port);
1505 mail->port = 110; // default pop3 port
1506 } else if (type == IMAP) {
1507 mail->port = 143; // default imap port
1511 tmp = strstr(arg, "-f ");
1514 sscanf(tmp, "%s", mail->folder);
1516 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1519 tmp = strstr(arg, "-e ");
1524 if (tmp[0] == '\'') {
1525 len = strstr(tmp + 1, "'") - tmp - 1;
1530 strncpy(mail->command, tmp + 1, len);
1532 mail->command[0] = '\0';
1534 mail->p_timed_thread = NULL;
1538 void *imap_thread(struct mail_s *mail)
1540 int sockfd, numbytes;
1541 char recvbuf[MAXDATASIZE];
1542 char sendbuf[MAXDATASIZE];
1545 unsigned int old_unseen = UINT_MAX;
1546 unsigned int old_messages = UINT_MAX;
1547 struct stat stat_buf;
1549 struct sockaddr_in their_addr; // connector's address information
1551 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1552 herror("gethostbyname");
1557 ERR("Trying IMAP connection again for %s@%s (try %i/5)",
1558 mail->user, mail->host, fail + 1);
1560 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1563 goto next_iteration;
1567 their_addr.sin_family = AF_INET;
1568 // short, network byte order
1569 their_addr.sin_port = htons(mail->port);
1570 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1571 // zero the rest of the struct
1572 memset(&(their_addr.sin_zero), '\0', 8);
1574 if (connect(sockfd, (struct sockaddr *) &their_addr,
1575 sizeof(struct sockaddr)) == -1) {
1578 goto next_iteration;
1580 struct timeval timeout;
1584 timeout.tv_sec = 60; // 60 second timeout i guess
1585 timeout.tv_usec = 0;
1587 FD_SET(sockfd, &fdset);
1588 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1590 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1593 goto next_iteration;
1596 ERR("IMAP connection failed: timeout");
1598 goto next_iteration;
1600 recvbuf[numbytes] = '\0';
1601 if (strstr(recvbuf, "* OK") != recvbuf) {
1602 ERR("IMAP connection failed, probably not an IMAP server");
1604 goto next_iteration;
1606 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1607 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1608 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1609 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1610 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1611 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1614 goto next_iteration;
1616 timeout.tv_sec = 60; // 60 second timeout i guess
1617 timeout.tv_usec = 0;
1619 FD_SET(sockfd, &fdset);
1620 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1622 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1625 goto next_iteration;
1628 recvbuf[numbytes] = '\0';
1629 if (strstr(recvbuf, "a1 OK") == NULL) {
1630 ERR("IMAP server login failed: %s", recvbuf);
1632 goto next_iteration;
1634 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1635 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1636 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
1637 MAXDATASIZE - strlen(sendbuf) - 1);
1638 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1641 goto next_iteration;
1643 timeout.tv_sec = 60; // 60 second timeout i guess
1644 timeout.tv_usec = 0;
1646 FD_SET(sockfd, &fdset);
1647 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1649 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1652 goto next_iteration;
1655 recvbuf[numbytes] = '\0';
1656 if (strstr(recvbuf, "a2 OK") == NULL) {
1657 ERR("IMAP status failed: %s", recvbuf);
1659 goto next_iteration;
1661 // now we get the data
1662 reply = strstr(recvbuf, " (MESSAGES ");
1664 *strchr(reply, ')') = '\0';
1665 if (reply == NULL) {
1666 ERR("Error parsing IMAP response: %s", recvbuf);
1668 goto next_iteration;
1670 timed_thread_lock(mail->p_timed_thread);
1671 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
1673 timed_thread_unlock(mail->p_timed_thread);
1675 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
1676 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1679 goto next_iteration;
1681 timeout.tv_sec = 60; // 60 second timeout i guess
1682 timeout.tv_usec = 0;
1684 FD_SET(sockfd, &fdset);
1685 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1687 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1690 goto next_iteration;
1693 recvbuf[numbytes] = '\0';
1694 if (strstr(recvbuf, "a3 OK") == NULL) {
1695 ERR("IMAP logout failed: %s", recvbuf);
1697 goto next_iteration;
1699 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
1700 || (mail->messages > old_messages && mail->unseen > 0))) {
1702 if (system(mail->command) == -1) {
1707 old_unseen = mail->unseen;
1708 old_messages = mail->messages;
1710 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1711 /* if a valid socket, close it */
1714 if (timed_thread_test(mail->p_timed_thread)) {
1715 timed_thread_exit(mail->p_timed_thread);
1723 void *pop3_thread(struct mail_s *mail)
1725 int sockfd, numbytes;
1726 char recvbuf[MAXDATASIZE];
1727 char sendbuf[MAXDATASIZE];
1730 unsigned int old_unseen = UINT_MAX;
1731 struct stat stat_buf;
1733 struct sockaddr_in their_addr; // connector's address information
1735 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1736 herror("gethostbyname");
1741 ERR("Trying POP3 connection again for %s@%s (try %i/5)",
1742 mail->user, mail->host, fail + 1);
1744 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1747 goto next_iteration;
1751 their_addr.sin_family = AF_INET;
1752 // short, network byte order
1753 their_addr.sin_port = htons(mail->port);
1754 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1755 // zero the rest of the struct
1756 memset(&(their_addr.sin_zero), '\0', 8);
1758 if (connect(sockfd, (struct sockaddr *) &their_addr,
1759 sizeof(struct sockaddr)) == -1) {
1762 goto next_iteration;
1764 struct timeval timeout;
1768 timeout.tv_sec = 60; // 60 second timeout i guess
1769 timeout.tv_usec = 0;
1771 FD_SET(sockfd, &fdset);
1772 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1774 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1777 goto next_iteration;
1780 ERR("POP3 connection failed: timeout\n");
1782 goto next_iteration;
1784 recvbuf[numbytes] = '\0';
1785 if (strstr(recvbuf, "+OK ") != recvbuf) {
1786 ERR("POP3 connection failed, probably not a POP3 server");
1788 goto next_iteration;
1790 strncpy(sendbuf, "USER ", MAXDATASIZE);
1791 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1792 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1793 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1794 perror("send USER");
1796 goto next_iteration;
1798 timeout.tv_sec = 60; // 60 second timeout i guess
1799 timeout.tv_usec = 0;
1801 FD_SET(sockfd, &fdset);
1802 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1804 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1805 perror("recv USER");
1807 goto next_iteration;
1810 recvbuf[numbytes] = '\0';
1811 if (strstr(recvbuf, "+OK ") == NULL) {
1812 ERR("POP3 server login failed: %s", recvbuf);
1814 goto next_iteration;
1816 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1817 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1818 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1819 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1820 perror("send PASS");
1822 goto next_iteration;
1824 timeout.tv_sec = 60; // 60 second timeout i guess
1825 timeout.tv_usec = 0;
1827 FD_SET(sockfd, &fdset);
1828 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1830 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1831 perror("recv PASS");
1833 goto next_iteration;
1836 recvbuf[numbytes] = '\0';
1837 if (strstr(recvbuf, "+OK ") == NULL) {
1838 ERR("POP3 server login failed: %s", recvbuf);
1840 goto next_iteration;
1842 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
1843 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1844 perror("send STAT");
1846 goto next_iteration;
1848 timeout.tv_sec = 60; // 60 second timeout i guess
1849 timeout.tv_usec = 0;
1851 FD_SET(sockfd, &fdset);
1852 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1854 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1855 perror("recv STAT");
1857 goto next_iteration;
1860 recvbuf[numbytes] = '\0';
1861 if (strstr(recvbuf, "+OK ") == NULL) {
1862 ERR("POP3 status failed: %s", recvbuf);
1864 goto next_iteration;
1866 // now we get the data
1867 reply = recvbuf + 4;
1868 if (reply == NULL) {
1869 ERR("Error parsing POP3 response: %s", recvbuf);
1871 goto next_iteration;
1873 timed_thread_lock(mail->p_timed_thread);
1874 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
1875 timed_thread_unlock(mail->p_timed_thread);
1877 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
1878 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1879 perror("send QUIT");
1881 goto next_iteration;
1883 timeout.tv_sec = 60; // 60 second timeout i guess
1884 timeout.tv_usec = 0;
1886 FD_SET(sockfd, &fdset);
1887 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1889 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1890 perror("recv QUIT");
1892 goto next_iteration;
1895 recvbuf[numbytes] = '\0';
1896 if (strstr(recvbuf, "+OK") == NULL) {
1897 ERR("POP3 logout failed: %s", recvbuf);
1899 goto next_iteration;
1901 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
1903 if (system(mail->command) == -1) {
1908 old_unseen = mail->unseen;
1910 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1911 /* if a valid socket, close it */
1914 if (timed_thread_test(mail->p_timed_thread)) {
1915 timed_thread_exit(mail->p_timed_thread);
1923 void *threaded_exec(struct text_object *obj)
1926 char *p2 = obj->data.texeci.buffer;
1927 FILE *fp = popen(obj->data.texeci.cmd, "r");
1929 timed_thread_lock(obj->data.texeci.p_timed_thread);
1930 int n2 = fread(p2, 1, text_buffer_size, fp);
1934 if (n2 && p2[n2 - 1] == '\n') {
1938 if (*p2 == '\001') {
1943 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1944 if (timed_thread_test(obj->data.texeci.p_timed_thread)) {
1945 timed_thread_exit(obj->data.texeci.p_timed_thread);
1951 static struct text_object *new_text_object_internal()
1953 struct text_object *obj = malloc(sizeof(struct text_object));
1954 memset(obj, 0, sizeof(struct text_object));
1958 static void free_text_objects(unsigned int count, struct text_object *objs)
1962 for (i = 0; i < count; i++) {
1963 switch (objs[i].type) {
1966 close(objs[i].data.i);
1969 close(objs[i].data.i);
1972 close(objs[i].data.sysfs.fd);
1975 close(objs[i].data.sysfs.fd);
1978 close(objs[i].data.sysfs.fd);
1980 #endif /* !__OpenBSD__ */
1982 free(objs[i].data.s);
1985 free(objs[i].data.s);
1988 free(objs[i].data.tztime.tz);
1989 free(objs[i].data.tztime.fmt);
1992 free(objs[i].data.mboxscan.args);
1993 free(objs[i].data.mboxscan.output);
1997 free(objs[i].data.local_mail.box);
2002 case OBJ_imap_unseen:
2003 if (!objs[i].global_mode) {
2004 free(objs[i].data.mail);
2007 case OBJ_imap_messages:
2008 if (!objs[i].global_mode) {
2009 free(objs[i].data.mail);
2015 case OBJ_pop3_unseen:
2016 if (!objs[i].global_mode) {
2017 free(objs[i].data.mail);
2021 if (!objs[i].global_mode) {
2022 free(objs[i].data.mail);
2026 case OBJ_if_existing:
2027 case OBJ_if_mounted:
2028 case OBJ_if_running:
2029 free(objs[i].data.ifblock.s);
2030 free(objs[i].data.ifblock.str);
2033 free(objs[i].data.tail.logfile);
2034 free(objs[i].data.tail.buffer);
2043 free(objs[i].data.s);
2046 case OBJ_iconv_start:
2052 free(objs[i].data.ifblock.s);
2053 free(objs[i].data.ifblock.str);
2056 free(objs[i].data.ifblock.s);
2057 free(objs[i].data.ifblock.str);
2060 if (info.gw_info.iface) {
2061 free(info.gw_info.iface);
2062 info.gw_info.iface = 0;
2064 if (info.gw_info.ip) {
2065 free(info.gw_info.ip);
2066 info.gw_info.ip = 0;
2069 case OBJ_ioscheduler:
2071 free(objs[i].data.s);
2075 case OBJ_xmms2_artist:
2076 if (info.xmms2.artist) {
2077 free(info.xmms2.artist);
2078 info.xmms2.artist = 0;
2081 case OBJ_xmms2_album:
2082 if (info.xmms2.album) {
2083 free(info.xmms2.album);
2084 info.xmms2.album = 0;
2087 case OBJ_xmms2_title:
2088 if (info.xmms2.title) {
2089 free(info.xmms2.title);
2090 info.xmms2.title = 0;
2093 case OBJ_xmms2_genre:
2094 if (info.xmms2.genre) {
2095 free(info.xmms2.genre);
2096 info.xmms2.genre = 0;
2099 case OBJ_xmms2_comment:
2100 if (info.xmms2.comment) {
2101 free(info.xmms2.comment);
2102 info.xmms2.comment = 0;
2106 if (info.xmms2.url) {
2107 free(info.xmms2.url);
2111 case OBJ_xmms2_date:
2112 if (info.xmms2.date) {
2113 free(info.xmms2.date);
2114 info.xmms2.date = 0;
2117 case OBJ_xmms2_status:
2118 if (info.xmms2.status) {
2119 free(info.xmms2.status);
2120 info.xmms2.status = 0;
2123 case OBJ_xmms2_playlist:
2124 if (info.xmms2.playlist) {
2125 free(info.xmms2.playlist);
2126 info.xmms2.playlist = 0;
2129 case OBJ_xmms2_smart:
2130 if (info.xmms2.artist) {
2131 free(info.xmms2.artist);
2132 info.xmms2.artist = 0;
2134 if (info.xmms2.title) {
2135 free(info.xmms2.title);
2136 info.xmms2.title = 0;
2138 if (info.xmms2.url) {
2139 free(info.xmms2.url);
2145 case OBJ_bmpx_title:
2146 case OBJ_bmpx_artist:
2147 case OBJ_bmpx_album:
2148 case OBJ_bmpx_track:
2150 case OBJ_bmpx_bitrate:
2155 free(objs[i].data.rss.uri);
2156 free(objs[i].data.rss.action);
2163 free(objs[i].data.s);
2165 case OBJ_battery_time:
2166 free(objs[i].data.s);
2168 #endif /* !__OpenBSD__ */
2172 case OBJ_execigraph:
2173 free(objs[i].data.execi.cmd);
2174 free(objs[i].data.execi.buffer);
2177 free(objs[i].data.texeci.cmd);
2178 free(objs[i].data.texeci.buffer);
2182 if (info.first_process) {
2183 free_all_processes();
2184 info.first_process = NULL;
2189 free(objs[i].data.hddtemp.dev);
2190 free(objs[i].data.hddtemp.addr);
2193 case OBJ_entropy_avail:
2194 case OBJ_entropy_poolsize:
2195 case OBJ_entropy_bar:
2197 case OBJ_user_names:
2198 if (info.users.names) {
2199 free(info.users.names);
2200 info.users.names = 0;
2203 case OBJ_user_terms:
2204 if (info.users.terms) {
2205 free(info.users.terms);
2206 info.users.terms = 0;
2209 case OBJ_user_times:
2210 if (info.users.times) {
2211 free(info.users.times);
2212 info.users.times = 0;
2217 case OBJ_smapi_bat_perc:
2218 free(objs[i].data.s);
2220 case OBJ_if_smapi_bat_installed:
2221 free(objs[i].data.ifblock.s);
2222 free(objs[i].data.ifblock.str);
2227 case OBJ_mpd_artist:
2229 case OBJ_mpd_random:
2230 case OBJ_mpd_repeat:
2232 case OBJ_mpd_bitrate:
2233 case OBJ_mpd_status:
2236 case OBJ_mpd_password:
2238 case OBJ_mpd_elapsed:
2239 case OBJ_mpd_length:
2243 case OBJ_mpd_percent:
2245 free_mpd_vars(&info);
2251 /* text_objects = NULL;
2252 text_object_count = 0; */
2255 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2260 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2261 *a = mixer_init(buf1);
2262 scan_bar(arg + n, w, h);
2264 *a = mixer_init(NULL);
2265 scan_bar(arg, w, h);
2269 /* construct_text_object() creates a new text_object */
2270 static struct text_object *construct_text_object(const char *s,
2271 const char *arg, unsigned int object_count,
2272 struct text_object *text_objects, long line)
2274 // struct text_object *obj = new_text_object();
2275 struct text_object *obj = new_text_object_internal();
2279 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
2280 obj->type = OBJ_##a; need_mask |= (1 << n); {
2281 #define END } } else
2285 obj->type = OBJ_color;
2286 obj->data.l = get_x11_color(s);
2290 OBJ(freq, INFO_FREQ)
2293 obj->data.i = open_acpi_temperature(arg);
2294 END OBJ(acpitempf, 0)
2295 obj->data.i = open_acpi_temperature(arg);
2296 END OBJ(acpiacadapter, 0)
2297 END OBJ(freq, INFO_FREQ)
2298 #endif /* !__OpenBSD__ */
2300 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2301 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2302 obj->data.cpu_index = 1;
2303 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
2304 "Displaying the clock for CPU 1."); */
2306 obj->data.cpu_index = atoi(&arg[0]);
2309 END OBJ(freq_g, INFO_FREQ)
2311 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2312 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2313 obj->data.cpu_index = 1;
2314 /* ERR("freq_g: Invalid CPU number or you don't have that many "
2315 "CPUs! Displaying the clock for CPU 1."); */
2317 obj->data.cpu_index = atoi(&arg[0]);
2320 #if defined(__linux__)
2321 END OBJ(voltage_mv, 0)
2323 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2324 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2325 obj->data.cpu_index = 1;
2326 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
2327 "CPUs! Displaying voltage for CPU 1."); */
2329 obj->data.cpu_index = atoi(&arg[0]);
2332 END OBJ(voltage_v, 0)
2334 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2335 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2336 obj->data.cpu_index = 1;
2337 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
2338 "CPUs! Displaying voltage for CPU 1."); */
2340 obj->data.cpu_index = atoi(&arg[0]);
2345 END OBJ(wireless_essid, INFO_NET)
2347 obj->data.net = get_net_stat(arg);
2349 CRIT_ERR("wireless_essid: needs an argument");
2351 END OBJ(wireless_mode, INFO_NET)
2353 obj->data.net = get_net_stat(arg);
2355 CRIT_ERR("wireless_mode: needs an argument");
2357 END OBJ(wireless_bitrate, INFO_NET)
2359 obj->data.net = get_net_stat(arg);
2361 CRIT_ERR("wireless_bitrate: needs an argument");
2363 END OBJ(wireless_ap, INFO_NET)
2365 obj->data.net = get_net_stat(arg);
2367 CRIT_ERR("wireless_ap: needs an argument");
2369 END OBJ(wireless_link_qual, INFO_NET)
2371 obj->data.net = get_net_stat(arg);
2373 CRIT_ERR("wireless_link_qual: needs an argument");
2375 END OBJ(wireless_link_qual_max, INFO_NET)
2377 obj->data.net = get_net_stat(arg);
2379 CRIT_ERR("wireless_link_qual_max: needs an argument");
2381 END OBJ(wireless_link_qual_perc, INFO_NET)
2383 obj->data.net = get_net_stat(arg);
2385 CRIT_ERR("wireless_link_qual_perc: needs an argument");
2387 END OBJ(wireless_link_bar, INFO_NET)
2389 arg = scan_bar(arg, &obj->a, &obj->b);
2390 obj->data.net = get_net_stat(arg);
2392 CRIT_ERR("wireless_link_bar: needs an argument");
2394 #endif /* HAVE_IWLIB */
2396 #endif /* __linux__ */
2397 END OBJ(freq_dyn, 0)
2398 END OBJ(freq_dyn_g, 0)
2406 sscanf(arg, "%63s", bat);
2408 strcpy(bat, "BAT0");
2410 obj->data.s = strdup(bat);
2411 END OBJ(battery_time, 0)
2415 sscanf(arg, "%63s", bat);
2417 strcpy(bat, "BAT0");
2419 obj->data.s = strdup(bat);
2420 END OBJ(battery_percent, 0)
2424 sscanf(arg, "%63s", bat);
2426 strcpy(bat, "BAT0");
2428 obj->data.s = strdup(bat);
2429 END OBJ(battery_bar, 0)
2433 arg = scan_bar(arg, &obj->a, &obj->b);
2434 sscanf(arg, "%63s", bat);
2436 strcpy(bat, "BAT0");
2438 obj->data.s = strdup(bat);
2439 #endif /* !__OpenBSD__ */
2441 #if defined(__linux__)
2442 END OBJ(i8k_version, INFO_I8K)
2443 END OBJ(i8k_bios, INFO_I8K)
2444 END OBJ(i8k_serial, INFO_I8K)
2445 END OBJ(i8k_cpu_temp, INFO_I8K)
2446 END OBJ(i8k_cpu_tempf, INFO_I8K)
2447 END OBJ(i8k_left_fan_status, INFO_I8K)
2448 END OBJ(i8k_right_fan_status, INFO_I8K)
2449 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2450 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2451 END OBJ(i8k_ac_status, INFO_I8K)
2452 END OBJ(i8k_buttons_status, INFO_I8K)
2454 END OBJ(ibm_temps, 0)
2456 CRIT_ERR("ibm_temps: needs an argument");
2458 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
2459 obj->data.sensor = 0;
2460 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
2461 "Using 0 (CPU temp sensor).");
2463 obj->data.sensor = atoi(&arg[0]);
2464 END OBJ(ibm_volume, 0)
2465 END OBJ(ibm_brightness, 0)
2467 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2468 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2471 ERR("if_up needs an argument");
2472 obj->data.ifblock.s = 0;
2474 obj->data.ifblock.s = strdup(arg);
2475 blockstart[blockdepth] = object_count;
2476 obj->data.ifblock.pos = object_count + 2;
2479 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2480 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2482 blockstart[blockdepth] = object_count;
2483 obj->data.ifblock.pos = object_count + 2;
2485 END OBJ(ioscheduler, 0)
2487 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
2490 obj->data.s = strdup(arg);
2491 END OBJ(laptop_mode, 0)
2492 END OBJ(pb_battery, 0)
2493 if (arg && strcmp(arg, "status") == 0) {
2494 obj->data.i = PB_BATT_STATUS;
2495 } else if (arg && strcmp(arg, "percent") == 0) {
2496 obj->data.i = PB_BATT_PERCENT;
2497 } else if (arg && strcmp(arg, "time") == 0) {
2498 obj->data.i = PB_BATT_TIME;
2500 ERR("pb_battery: needs one argument: status, percent or time");
2505 #endif /* __linux__ */
2506 #if defined(__OpenBSD__)
2507 END OBJ(obsd_sensors_temp, 0)
2509 CRIT_ERR("obsd_sensors_temp: needs an argument");
2511 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2512 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2513 obj->data.sensor = 0;
2514 ERR("Invalid temperature sensor number!");
2516 obj->data.sensor = atoi(&arg[0]);
2517 END OBJ(obsd_sensors_fan, 0)
2519 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
2522 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2523 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2524 obj->data.sensor = 0;
2525 ERR("Invalid fan sensor number!");
2527 obj->data.sensor = atoi(&arg[0]);
2528 END OBJ(obsd_sensors_volt, 0)
2530 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
2533 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2534 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2535 obj->data.sensor = 0;
2536 ERR("Invalid voltage sensor number!");
2538 obj->data.sensor = atoi(&arg[0]);
2539 END OBJ(obsd_vendor, 0)
2540 END OBJ(obsd_product, 0)
2541 #endif /* __OpenBSD__ */
2542 END OBJ(buffers, INFO_BUFFERS)
2543 END OBJ(cached, INFO_BUFFERS)
2544 END OBJ(cpu, INFO_CPU)
2546 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2547 obj->data.cpu_index = atoi(&arg[3]);
2550 obj->data.cpu_index = 0;
2553 obj->data.cpu_index = 0;
2555 END OBJ(cpubar, INFO_CPU)
2557 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2558 obj->data.cpu_index = atoi(&arg[3]);
2561 obj->data.cpu_index = 0;
2563 scan_bar(arg, &obj->a, &obj->b);
2565 scan_bar(arg, &obj->a, &obj->b);
2566 obj->data.cpu_index = 0;
2568 END OBJ(cpugraph, INFO_CPU)
2569 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2573 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
2574 obj->data.cpu_index = atoi(&buf[3]);
2576 obj->data.cpu_index = 0;
2580 END OBJ(diskio, INFO_DISKIO)
2582 obj->data.diskio = prepare_diskio_stat(arg);
2584 obj->data.diskio = NULL;
2586 END OBJ(diskio_read, INFO_DISKIO)
2588 obj->data.diskio = prepare_diskio_stat(arg);
2590 obj->data.diskio = NULL;
2592 END OBJ(diskio_write, INFO_DISKIO)
2594 obj->data.diskio = prepare_diskio_stat(arg);
2596 obj->data.diskio = NULL;
2598 END OBJ(diskiograph, INFO_DISKIO)
2599 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2603 obj->data.diskio = prepare_diskio_stat(buf);
2606 obj->data.diskio = NULL;
2608 END OBJ(diskiograph_read, INFO_DISKIO)
2609 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2613 obj->data.diskio = prepare_diskio_stat(buf);
2616 obj->data.diskio = NULL;
2618 END OBJ(diskiograph_write, INFO_DISKIO)
2619 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2623 obj->data.diskio = prepare_diskio_stat(buf);
2626 obj->data.diskio = NULL;
2630 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2633 obj->data.l = color0;
2635 obj->data.l = color1;
2637 obj->data.l = color2;
2639 obj->data.l = color3;
2641 obj->data.l = color4;
2643 obj->data.l = color5;
2645 obj->data.l = color6;
2647 obj->data.l = color7;
2649 obj->data.l = color8;
2651 obj->data.l = color9;
2653 obj->data.s = scan_font(arg);
2654 END OBJ(conky_version, 0)
2655 END OBJ(conky_build_date, 0)
2656 END OBJ(conky_build_arch, 0)
2657 END OBJ(downspeed, INFO_NET)
2659 obj->data.net = get_net_stat(arg);
2661 CRIT_ERR("downspeed needs argument");
2663 END OBJ(downspeedf, INFO_NET)
2665 obj->data.net = get_net_stat(arg);
2667 CRIT_ERR("downspeedf needs argument");
2669 END OBJ(downspeedgraph, INFO_NET)
2670 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2674 obj->data.net = get_net_stat(buf);
2679 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos =
2681 blockstart[blockdepth - 1] = object_count;
2682 obj->data.ifblock.pos = object_count + 2;
2684 ERR("$else: no matching $if_*");
2689 text_objects[blockstart[blockdepth]].data.ifblock.pos =
2692 ERR("$endif: no matching $if_*");
2695 obj->data.s = strdup(arg ? arg : "");
2698 obj->data.s = strdup(arg ? arg : "");
2700 obj->data.s = strdup(arg ? arg : "");
2702 obj->data.s = strdup(arg ? arg : "");
2703 END OBJ(execgraph, 0)
2704 obj->data.s = strdup(arg ? arg : "");
2705 END OBJ(execibar, 0)
2708 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2711 ERR("${execibar <interval> command}");
2712 obj->type = OBJ_text;
2713 snprintf(buf, 256, "${%s}", s);
2714 obj->data.s = strdup(buf);
2716 obj->data.execi.cmd = strdup(arg + n);
2718 END OBJ(execigraph, 0)
2721 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2724 ERR("${execigraph <interval> command}");
2725 obj->type = OBJ_text;
2726 snprintf(buf, 256, "${%s}", s);
2727 obj->data.s = strdup(buf);
2729 obj->data.execi.cmd = strdup(arg + n);
2734 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2737 ERR("${execi <interval> command}");
2738 obj->type = OBJ_text;
2739 snprintf(buf, 256, "${%s}", s);
2740 obj->data.s = strdup(buf);
2742 obj->data.execi.cmd = strdup(arg + n);
2743 obj->data.execi.buffer = malloc(text_buffer_size);
2748 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2751 ERR("${execi <interval> command}");
2752 obj->type = OBJ_text;
2753 snprintf(buf, 256, "${%s}", s);
2754 obj->data.s = strdup(buf);
2756 obj->data.execi.cmd = strdup(arg + n);
2757 obj->data.execi.buffer = malloc(text_buffer_size);
2762 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2765 ERR("${texeci <interval> command}");
2766 obj->type = OBJ_text;
2767 snprintf(buf, 256, "${%s}", s);
2768 obj->data.s = strdup(buf);
2770 obj->data.texeci.cmd = strdup(arg + n);
2771 obj->data.texeci.buffer = malloc(text_buffer_size);
2773 obj->data.texeci.p_timed_thread = NULL;
2774 END OBJ(pre_exec, 0)
2775 obj->type = OBJ_text;
2777 FILE *fp = popen(arg, "r");
2781 n = fread(buf, 1, 2048, fp);
2784 if (n && buf[n - 1] == '\n') {
2790 obj->data.s = strdup(buf);
2792 obj->data.s = strdup("");
2795 END OBJ(fs_bar, INFO_FS)
2796 obj->data.fsbar.h = 4;
2797 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2799 while (isspace(*arg)) {
2808 obj->data.fsbar.fs = prepare_fs_stat(arg);
2809 END OBJ(fs_bar_free, INFO_FS)
2810 obj->data.fsbar.h = 4;
2814 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1) {
2820 obj->data.fsbar.fs = prepare_fs_stat(arg);
2821 END OBJ(fs_free, INFO_FS)
2825 obj->data.fs = prepare_fs_stat(arg);
2826 END OBJ(fs_used_perc, INFO_FS)
2830 obj->data.fs = prepare_fs_stat(arg);
2831 END OBJ(fs_free_perc, INFO_FS)
2835 obj->data.fs = prepare_fs_stat(arg);
2836 END OBJ(fs_size, INFO_FS)
2840 obj->data.fs = prepare_fs_stat(arg);
2841 END OBJ(fs_type, INFO_FS)
2845 obj->data.fs = prepare_fs_stat(arg);
2846 END OBJ(fs_used, INFO_FS)
2850 obj->data.fs = prepare_fs_stat(arg);
2852 obj->data.i = arg ? atoi(arg) : 1;
2854 obj->data.i = arg ? atoi(arg) : 1;
2856 obj->data.i = arg ? atoi(arg) : 1;
2860 ERR("goto needs arguments");
2861 obj->type = OBJ_text;
2862 obj->data.s = strdup("${goto}");
2866 obj->data.i = atoi(arg);
2872 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2873 sscanf(arg, "%d", &b);
2879 obj->data.pair.a = a;
2880 obj->data.pair.b = b;
2883 END OBJ(i2c, INFO_SYSFS)
2884 char buf1[64], buf2[64];
2888 ERR("i2c needs arguments");
2889 obj->type = OBJ_text;
2890 // obj->data.s = strdup("${i2c}");
2894 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2895 /* if scanf couldn't read three values, read type and num and use
2897 sscanf(arg, "%63s %d", buf2, &n);
2898 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
2899 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2900 strncpy(obj->data.sysfs.type, buf2, 63);
2902 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
2903 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2904 strncpy(obj->data.sysfs.type, buf2, 63);
2907 END OBJ(platform, INFO_SYSFS)
2908 char buf1[64], buf2[64];
2912 ERR("platform needs arguments");
2913 obj->type = OBJ_text;
2917 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2918 /* if scanf couldn't read three values, read type and num and use
2920 sscanf(arg, "%63s %d", buf2, &n);
2921 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
2922 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2923 strncpy(obj->data.sysfs.type, buf2, 63);
2925 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
2926 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2927 strncpy(obj->data.sysfs.type, buf2, 63);
2930 END OBJ(hwmon, INFO_SYSFS)
2931 char buf1[64], buf2[64];
2935 ERR("hwmon needs argumanets");
2936 obj->type = OBJ_text;
2940 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2941 /* if scanf couldn't read three values, read type and num and use
2943 sscanf(arg, "%63s %d", buf2, &n);
2944 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
2945 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2946 strncpy(obj->data.sysfs.type, buf2, 63);
2948 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
2949 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2950 strncpy(obj->data.sysfs.type, buf2, 63);
2952 #endif /* !__OpenBSD__ */
2954 END OBJ(top, INFO_TOP)
2959 ERR("top needs arguments");
2960 obj->type = OBJ_text;
2961 // obj->data.s = strdup("${top}");
2964 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2965 if (strcmp(buf, "name") == 0) {
2966 obj->data.top.type = TOP_NAME;
2967 } else if (strcmp(buf, "cpu") == 0) {
2968 obj->data.top.type = TOP_CPU;
2969 } else if (strcmp(buf, "pid") == 0) {
2970 obj->data.top.type = TOP_PID;
2971 } else if (strcmp(buf, "mem") == 0) {
2972 obj->data.top.type = TOP_MEM;
2973 } else if (strcmp(buf, "time") == 0) {
2974 obj->data.top.type = TOP_TIME;
2976 ERR("invalid arg for top");
2979 if (n < 1 || n > 10) {
2980 CRIT_ERR("invalid arg for top");
2983 obj->data.top.num = n - 1;
2987 ERR("invalid args given for top");
2990 END OBJ(top_mem, INFO_TOP)
2995 ERR("top_mem needs arguments");
2996 obj->type = OBJ_text;
2997 obj->data.s = strdup("${top_mem}");
3000 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3001 if (strcmp(buf, "name") == 0) {
3002 obj->data.top.type = TOP_NAME;
3003 } else if (strcmp(buf, "cpu") == 0) {
3004 obj->data.top.type = TOP_CPU;
3005 } else if (strcmp(buf, "pid") == 0) {
3006 obj->data.top.type = TOP_PID;
3007 } else if (strcmp(buf, "mem") == 0) {
3008 obj->data.top.type = TOP_MEM;
3009 } else if (strcmp(buf, "time") == 0) {
3010 obj->data.top.type = TOP_TIME;
3012 ERR("invalid arg for top");
3015 if (n < 1 || n > 10) {
3016 CRIT_ERR("invalid arg for top");
3019 obj->data.top.num = n - 1;
3023 ERR("invalid args given for top");
3026 END OBJ(addr, INFO_NET)
3028 obj->data.net = get_net_stat(arg);
3030 CRIT_ERR("addr needs argument");
3032 #if defined(__linux__)
3033 END OBJ(addrs, INFO_NET)
3035 obj->data.net = get_net_stat(arg);
3037 CRIT_ERR("addrs needs argument");
3039 #endif /* __linux__ */
3046 ERR("tail needs arguments");
3047 obj->type = OBJ_text;
3048 obj->data.s = strdup("${tail}");
3051 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3052 if (n1 < 1 || n1 > 30) {
3053 CRIT_ERR("invalid arg for tail, number of lines must be "
3054 "between 1 and 30");
3060 obj->data.tail.fd = -1;
3062 if (stat(buf, &st) == 0) {
3063 if (S_ISFIFO(st.st_mode)) {
3064 fd = open(buf, O_RDONLY | O_NONBLOCK);
3067 CRIT_ERR("tail logfile does not exist, or you do "
3068 "not have correct permissions");
3071 obj->data.tail.fd = fd;
3073 fp = fopen(buf, "r");
3077 if (fp || obj->data.tail.fd != -1) {
3078 obj->data.tail.logfile = malloc(text_buffer_size);
3079 strcpy(obj->data.tail.logfile, buf);
3080 obj->data.tail.wantedlines = n1;
3081 obj->data.tail.interval = update_interval * 2;
3083 if (obj->data.tail.fd == -1) {
3088 CRIT_ERR("tail logfile does not exist, or you do not have "
3089 "correct permissions");
3092 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3093 if (n1 < 1 || n1 > 30) {
3094 CRIT_ERR("invalid arg for tail, number of lines must be "
3095 "between 1 and 30");
3097 } else if (n2 < 1 || n2 < update_interval) {
3098 CRIT_ERR("invalid arg for tail, interval must be greater than "
3099 "0 and Conky's interval");
3105 obj->data.tail.fd = -1;
3107 if (stat(buf, &st) == 0) {
3108 if (S_ISFIFO(st.st_mode)) {
3109 fd = open(buf, O_RDONLY | O_NONBLOCK);
3112 CRIT_ERR("tail logfile does not exist, or you do "
3113 "not have correct permissions");
3116 obj->data.tail.fd = fd;
3118 fp = fopen(buf, "r");
3122 if (fp || obj->data.tail.fd != -1) {
3123 obj->data.tail.logfile = malloc(text_buffer_size);
3124 strcpy(obj->data.tail.logfile, buf);
3125 obj->data.tail.wantedlines = n1;
3126 obj->data.tail.interval = n2;
3128 if (obj->data.tail.fd == -1) {
3133 CRIT_ERR("tail logfile does not exist, or you do not have "
3134 "correct permissions");
3138 ERR("invalid args given for tail");
3141 /* asumming all else worked */
3142 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3148 ERR("head needs arguments");
3149 obj->type = OBJ_text;
3150 obj->data.s = strdup("${head}");
3153 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3154 if (n1 < 1 || n1 > 30) {
3155 CRIT_ERR("invalid arg for head, number of lines must be "
3156 "between 1 and 30");
3161 fp = fopen(buf, "r");
3163 obj->data.tail.logfile = malloc(text_buffer_size);
3164 strcpy(obj->data.tail.logfile, buf);
3165 obj->data.tail.wantedlines = n1;
3166 obj->data.tail.interval = update_interval * 2;
3170 CRIT_ERR("head logfile does not exist, or you do not have "
3171 "correct permissions");
3174 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3175 if (n1 < 1 || n1 > 30) {
3176 CRIT_ERR("invalid arg for head, number of lines must be "
3177 "between 1 and 30");
3179 } else if (n2 < 1 || n2 < update_interval) {
3180 CRIT_ERR("invalid arg for head, interval must be greater than "
3181 "0 and Conky's interval");
3186 fp = fopen(buf, "r");
3188 obj->data.tail.logfile = malloc(text_buffer_size);
3189 strcpy(obj->data.tail.logfile, buf);
3190 obj->data.tail.wantedlines = n1;
3191 obj->data.tail.interval = n2;
3195 CRIT_ERR("head logfile does not exist, or you do not have "
3196 "correct permissions");
3200 ERR("invalid args given for head");
3203 /* asumming all else worked */
3204 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3205 END OBJ(loadavg, INFO_LOADAVG)
3206 int a = 1, b = 2, c = 3, r = 3;
3209 r = sscanf(arg, "%d %d %d", &a, &b, &c);
3210 if (r >= 3 && (c < 1 || c > 3)) {
3213 if (r >= 2 && (b < 1 || b > 3)) {
3216 if (r >= 1 && (a < 1 || a > 3)) {
3220 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
3221 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
3222 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
3223 END OBJ(if_empty, 0)
3224 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3225 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3228 ERR("if_empty needs an argument");
3229 obj->data.ifblock.s = 0;
3231 obj->data.ifblock.s = strdup(arg);
3233 blockstart[blockdepth] = object_count;
3234 obj->data.ifblock.pos = object_count + 2;
3236 END OBJ(if_existing, 0)
3237 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3238 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3241 ERR("if_existing needs an argument or two");
3242 obj->data.ifblock.s = NULL;
3243 obj->data.ifblock.str = NULL;
3245 char buf1[256], buf2[256];
3246 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3249 obj->data.ifblock.s = strdup(buf1);
3250 obj->data.ifblock.str = NULL;
3252 obj->data.ifblock.s = strdup(buf1);
3253 obj->data.ifblock.str = strdup(buf2);
3256 blockstart[blockdepth] = object_count;
3257 obj->data.ifblock.pos = object_count + 2;
3259 END OBJ(if_mounted, 0)
3260 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3261 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3264 ERR("if_mounted needs an argument");
3265 obj->data.ifblock.s = 0;
3267 obj->data.ifblock.s = strdup(arg);
3269 blockstart[blockdepth] = object_count;
3270 obj->data.ifblock.pos = object_count + 2;
3272 END OBJ(if_running, 0)
3273 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3274 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3279 snprintf(buf, 256, "pidof %s >/dev/null", arg);
3280 obj->data.ifblock.s = strdup(buf);
3282 ERR("if_running needs an argument");
3283 obj->data.ifblock.s = 0;
3285 blockstart[blockdepth] = object_count;
3286 obj->data.ifblock.pos = object_count + 2;
3292 char box[256], dst[256];
3296 strncpy(box, MAIL_FILE, sizeof(box));
3298 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3300 strncpy(box, arg, sizeof(box));
3304 variable_substitute(box, dst, sizeof(dst));
3305 obj->data.local_mail.box = strdup(dst);
3306 obj->data.local_mail.interval = n1;
3307 END OBJ(mboxscan, 0)
3308 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
3309 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
3310 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
3311 obj->data.mboxscan.output[0] = 1;
3312 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
3313 END OBJ(mem, INFO_MEM)
3314 END OBJ(memmax, INFO_MEM)
3315 END OBJ(memperc, INFO_MEM)
3316 END OBJ(membar, INFO_MEM)
3317 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3318 END OBJ(memgraph, INFO_MEM)
3319 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3325 END OBJ(mixer, INFO_MIXER)
3326 obj->data.l = mixer_init(arg);
3327 END OBJ(mixerl, INFO_MIXER)
3328 obj->data.l = mixer_init(arg);
3329 END OBJ(mixerr, INFO_MIXER)
3330 obj->data.l = mixer_init(arg);
3331 END OBJ(mixerbar, INFO_MIXER)
3332 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3333 &obj->data.mixerbar.h);
3334 END OBJ(mixerlbar, INFO_MIXER)
3335 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3336 &obj->data.mixerbar.h);
3337 END OBJ(mixerrbar, INFO_MIXER)
3338 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3339 &obj->data.mixerbar.h);
3340 END OBJ(new_mails, 0)
3342 char box[256], dst[256];
3346 strncpy(box, MAIL_FILE, sizeof(box));
3348 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3350 strncpy(box, arg, sizeof(box));
3354 variable_substitute(box, dst, sizeof(dst));
3355 obj->data.local_mail.box = strdup(dst);
3356 obj->data.local_mail.interval = n1;
3357 END OBJ(nodename, 0)
3358 END OBJ(processes, INFO_PROCS)
3359 END OBJ(running_processes, INFO_RUN_PROCS)
3360 END OBJ(shadecolor, 0)
3362 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
3364 END OBJ(outlinecolor, 0)
3366 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
3368 END OBJ(stippled_hr, 0)
3370 int a = stippled_borders, b = 1;
3373 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3374 sscanf(arg, "%d", &b);
3380 obj->data.pair.a = a;
3381 obj->data.pair.b = b;
3383 END OBJ(swap, INFO_MEM)
3384 END OBJ(swapmax, INFO_MEM)
3385 END OBJ(swapperc, INFO_MEM)
3386 END OBJ(swapbar, INFO_MEM)
3387 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3390 END OBJ(temp1, INFO_SYSFS)
3391 obj->type = OBJ_i2c;
3392 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
3393 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3394 END OBJ(temp2, INFO_SYSFS)
3395 obj->type = OBJ_i2c;
3396 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
3397 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3400 obj->data.s = strdup(arg ? arg : "%F %T");
3402 obj->data.s = strdup(arg ? arg : "%F %T");
3404 char buf1[256], buf2[256], *fmt, *tz;
3408 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3418 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
3419 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
3421 END OBJ(iconv_start, 0)
3422 if (iconv_converting) {
3423 CRIT_ERR("You must stop your last iconv conversion before "
3424 "starting another");
3427 char iconv_from[CODEPAGE_LENGTH];
3428 char iconv_to[CODEPAGE_LENGTH];
3430 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
3431 CRIT_ERR("Invalid arguments for iconv_start");
3435 new_iconv = iconv_open(iconv_to, iconv_from);
3436 if (new_iconv == (iconv_t) (-1)) {
3437 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
3439 obj->a = register_iconv(&new_iconv);
3440 iconv_converting = 1;
3444 CRIT_ERR("Iconv requires arguments");
3446 END OBJ(iconv_stop, 0)
3447 iconv_converting = 0;
3450 END OBJ(totaldown, INFO_NET)
3452 obj->data.net = get_net_stat(arg);
3454 CRIT_ERR("totaldown needs argument");
3456 END OBJ(totalup, INFO_NET)
3457 obj->data.net = get_net_stat(arg);
3459 obj->data.net = get_net_stat(arg);
3461 CRIT_ERR("totalup needs argument");
3465 obj->data.i = arg ? atoi(arg) : 0;
3467 obj->data.i = arg ? atoi(arg) : 0;
3468 END OBJ(upspeed, INFO_NET)
3470 obj->data.net = get_net_stat(arg);
3472 CRIT_ERR("upspeed needs argument");
3474 END OBJ(upspeedf, INFO_NET)
3476 obj->data.net = get_net_stat(arg);
3478 CRIT_ERR("upspeedf needs argument");
3481 END OBJ(upspeedgraph, INFO_NET)
3482 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3486 obj->data.net = get_net_stat(buf);
3489 END OBJ(uptime_short, INFO_UPTIME)
3490 END OBJ(uptime, INFO_UPTIME)
3491 END OBJ(user_names, INFO_USERS)
3492 END OBJ(user_times, INFO_USERS)
3493 END OBJ(user_terms, INFO_USERS)
3494 END OBJ(user_number, INFO_USERS)
3495 END OBJ(gw_iface, INFO_GW)
3496 END OBJ(gw_ip, INFO_GW)
3497 END OBJ(if_gw, INFO_GW)
3499 END OBJ(adt746xcpu, 0)
3500 END OBJ(adt746xfan, 0)
3501 #endif /* !__OpenBSD__ */
3502 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
3503 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
3504 END OBJ(apm_adapter, 0)
3505 END OBJ(apm_battery_life, 0)
3506 END OBJ(apm_battery_time, 0)
3507 #endif /* __FreeBSD__ */
3508 END OBJ(imap_unseen, 0)
3511 obj->data.mail = parse_mail_args(IMAP, arg);
3512 obj->global_mode = 0;
3514 obj->global_mode = 1;
3516 END OBJ(imap_messages, 0)
3519 obj->data.mail = parse_mail_args(IMAP, arg);
3520 obj->global_mode = 0;
3522 obj->global_mode = 1;
3524 END OBJ(pop3_unseen, 0)
3527 obj->data.mail = parse_mail_args(POP3, arg);
3528 obj->global_mode = 0;
3530 obj->global_mode = 1;
3532 END OBJ(pop3_used, 0)
3535 obj->data.mail = parse_mail_args(POP3, arg);
3536 obj->global_mode = 0;
3538 obj->global_mode = 1;
3543 obj->data.s = strdup(arg);
3545 ERR("smapi needs an argument");
3546 END OBJ(if_smapi_bat_installed, 0)
3547 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3548 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3551 ERR("if_smapi_bat_installed needs an argument");
3552 obj->data.ifblock.s = 0;
3554 obj->data.ifblock.s = strdup(arg);
3555 blockstart[blockdepth] = object_count;
3556 obj->data.ifblock.pos = object_count + 2;
3558 END OBJ(smapi_bat_perc, 0)
3560 obj->data.s = strdup(arg);
3562 ERR("smapi_bat_perc needs an argument");
3563 END OBJ(smapi_bat_bar, 0)
3566 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
3567 ERR("first argument to smapi_bat_bar must be an integer value");
3571 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
3574 ERR("if_smapi_bat_bar needs an argument");
3577 END OBJ(mpd_artist, INFO_MPD)
3578 END OBJ(mpd_title, INFO_MPD)
3580 sscanf(arg, "%d", &info.mpd.max_title_len);
3581 if (info.mpd.max_title_len > 0) {
3582 info.mpd.max_title_len++;
3584 CRIT_ERR("mpd_title: invalid length argument");
3587 info.mpd.max_title_len = 0;
3589 END OBJ(mpd_random, INFO_MPD)
3590 END OBJ(mpd_repeat, INFO_MPD)
3591 END OBJ(mpd_elapsed, INFO_MPD)
3592 END OBJ(mpd_length, INFO_MPD)
3593 END OBJ(mpd_track, INFO_MPD)
3594 END OBJ(mpd_name, INFO_MPD)
3595 END OBJ(mpd_file, INFO_MPD)
3596 END OBJ(mpd_percent, INFO_MPD)
3597 END OBJ(mpd_album, INFO_MPD)
3598 END OBJ(mpd_vol, INFO_MPD)
3599 END OBJ(mpd_bitrate, INFO_MPD)
3600 END OBJ(mpd_status, INFO_MPD)
3601 END OBJ(mpd_bar, INFO_MPD)
3602 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3603 END OBJ(mpd_smart, INFO_MPD)
3606 END OBJ(xmms2_artist, INFO_XMMS2)
3607 END OBJ(xmms2_album, INFO_XMMS2)
3608 END OBJ(xmms2_title, INFO_XMMS2)
3609 END OBJ(xmms2_genre, INFO_XMMS2)
3610 END OBJ(xmms2_comment, INFO_XMMS2)
3611 END OBJ(xmms2_url, INFO_XMMS2)
3612 END OBJ(xmms2_tracknr, INFO_XMMS2)
3613 END OBJ(xmms2_bitrate, INFO_XMMS2)
3614 END OBJ(xmms2_date, INFO_XMMS2)
3615 END OBJ(xmms2_id, INFO_XMMS2)
3616 END OBJ(xmms2_duration, INFO_XMMS2)
3617 END OBJ(xmms2_elapsed, INFO_XMMS2)
3618 END OBJ(xmms2_size, INFO_XMMS2)
3619 END OBJ(xmms2_status, INFO_XMMS2)
3620 END OBJ(xmms2_percent, INFO_XMMS2)
3621 END OBJ(xmms2_bar, INFO_XMMS2)
3622 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3623 END OBJ(xmms2_smart, INFO_XMMS2)
3624 END OBJ(xmms2_playlist, INFO_XMMS2)
3625 END OBJ(xmms2_timesplayed, INFO_XMMS2)
3628 END OBJ(audacious_status, INFO_AUDACIOUS)
3629 END OBJ(audacious_title, INFO_AUDACIOUS)
3631 sscanf(arg, "%d", &info.audacious.max_title_len);
3632 if (info.audacious.max_title_len > 0) {
3633 info.audacious.max_title_len++;
3635 CRIT_ERR("audacious_title: invalid length argument");
3638 END OBJ(audacious_length, INFO_AUDACIOUS)
3639 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
3640 END OBJ(audacious_position, INFO_AUDACIOUS)
3641 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
3642 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
3643 END OBJ(audacious_frequency, INFO_AUDACIOUS)
3644 END OBJ(audacious_channels, INFO_AUDACIOUS)
3645 END OBJ(audacious_filename, INFO_AUDACIOUS)
3646 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
3647 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
3648 END OBJ(audacious_bar, INFO_AUDACIOUS)
3649 scan_bar(arg, &obj->a, &obj->b);
3652 END OBJ(bmpx_title, INFO_BMPX)
3653 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3654 END OBJ(bmpx_artist, INFO_BMPX)
3655 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3656 END OBJ(bmpx_album, INFO_BMPX)
3657 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3658 END OBJ(bmpx_track, INFO_BMPX)
3659 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3660 END OBJ(bmpx_uri, INFO_BMPX)
3661 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3662 END OBJ(bmpx_bitrate, INFO_BMPX)
3663 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3668 int argc, delay, act_par;
3669 char *uri = (char *) malloc(128 * sizeof(char));
3670 char *action = (char *) malloc(64 * sizeof(char));
3672 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
3674 obj->data.rss.uri = uri;
3675 obj->data.rss.delay = delay;
3676 obj->data.rss.action = action;
3677 obj->data.rss.act_par = act_par;
3681 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
3687 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
3688 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3689 ERR("hddtemp needs arguments");
3690 obj->type = OBJ_text;
3691 obj->data.s = strdup("${hddtemp}");
3695 #ifdef TCP_PORT_MONITOR
3696 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3697 int argc, port_begin, port_end, item, connection_index;
3700 memset(itembuf, 0, sizeof(itembuf));
3701 connection_index = 0;
3702 /* massive argument checking */
3704 CRIT_ERR("tcp_portmon: needs arguments");
3706 argc = sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf,
3708 if ((argc != 3) && (argc != 4)) {
3709 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
3711 if ((port_begin < 1) || (port_begin > 65535) || (port_end < 1)
3712 || (port_end > 65535)) {
3713 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
3715 if (port_begin > port_end) {
3716 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
3718 if (strncmp(itembuf, "count", 31) == 0) {
3720 } else if (strncmp(itembuf, "rip", 31) == 0) {
3722 } else if (strncmp(itembuf, "rhost", 31) == 0) {
3724 } else if (strncmp(itembuf, "rport", 31) == 0) {
3726 } else if (strncmp(itembuf, "rservice", 31) == 0) {
3727 item = REMOTESERVICE;
3728 } else if (strncmp(itembuf, "lip", 31) == 0) {
3730 } else if (strncmp(itembuf, "lhost", 31) == 0) {
3732 } else if (strncmp(itembuf, "lport", 31) == 0) {
3734 } else if (strncmp(itembuf, "lservice", 31) == 0) {
3735 item = LOCALSERVICE;
3737 CRIT_ERR("tcp_portmon: invalid item specified");
3739 if ((argc == 3) && (item != COUNT)) {
3740 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" "
3743 if ((argc == 4) && (connection_index < 0)) {
3744 CRIT_ERR("tcp_portmon: connection index must be non-negative");
3746 /* ok, args looks good. save the text object data */
3747 obj->data.tcp_port_monitor.port_range_begin = (in_port_t) port_begin;
3748 obj->data.tcp_port_monitor.port_range_end = (in_port_t) port_end;
3749 obj->data.tcp_port_monitor.item = item;
3750 obj->data.tcp_port_monitor.connection_index = connection_index;
3752 /* if the port monitor collection hasn't been created,
3753 * we must create it */
3754 if (!info.p_tcp_port_monitor_collection) {
3755 info.p_tcp_port_monitor_collection =
3756 create_tcp_port_monitor_collection();
3757 if (!info.p_tcp_port_monitor_collection) {
3758 CRIT_ERR("tcp_portmon: unable to create port monitor "
3763 /* if a port monitor for this port does not exist,
3764 * create one and add it to the collection */
3765 if (find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
3766 port_begin, port_end) == NULL) {
3767 tcp_port_monitor_t *p_monitor = create_tcp_port_monitor(port_begin,
3768 port_end, &tcp_port_monitor_args);
3771 CRIT_ERR("tcp_portmon: unable to create port monitor");
3773 /* add the newly created monitor to the collection */
3774 if (insert_tcp_port_monitor_into_collection(
3775 info.p_tcp_port_monitor_collection, p_monitor) != 0) {
3776 CRIT_ERR("tcp_portmon: unable to add port monitor to "
3781 END OBJ(entropy_avail, INFO_ENTROPY)
3782 END OBJ(entropy_poolsize, INFO_ENTROPY)
3783 END OBJ(entropy_bar, INFO_ENTROPY)
3784 scan_bar(arg, &obj->a, &obj->b);
3788 ERR("unknown variable %s", s);
3789 obj->type = OBJ_text;
3790 snprintf(buf, 256, "${%s}", s);
3791 obj->data.s = strdup(buf);
3798 static struct text_object *create_plain_text(const char *s)
3800 struct text_object *obj;
3802 if (s == NULL || *s == '\0') {
3806 obj = new_text_object_internal();
3808 obj->type = OBJ_text;
3809 obj->data.s = strdup(s);
3813 static struct text_object_list *extract_variable_text_internal(const char *p)
3815 struct text_object_list *retval;
3816 struct text_object *obj;
3819 retval = malloc(sizeof(struct text_object_list));
3820 memset(retval, 0, sizeof(struct text_object_list));
3821 retval->text_object_count = 0;
3823 long line = text_lines;
3831 obj = create_plain_text(s);
3833 // allocate memory for the object
3834 retval->text_objects = realloc(retval->text_objects,
3835 sizeof(struct text_object) *
3836 (retval->text_object_count + 1));
3837 // assign the new object to the end of the list.
3838 memcpy(&retval->text_objects[retval->text_object_count++], obj,
3839 sizeof(struct text_object));
3851 /* variable is either $foo or ${foo} */
3853 unsigned int brl = 1, brr = 0;
3857 while (*p && brl != brr) {
3872 while (*p && (isalnum((int) *p) || *p == '_')) {
3877 /* copy variable to buffer */
3878 len = (p - s > 255) ? 255 : (p - s);
3879 strncpy(buf, s, len);
3889 /* if variable wasn't found in environment, use some special */
3895 if (strchr(buf, ' ')) {
3896 arg = strchr(buf, ' ');
3899 while (isspace((int) *arg)) {
3907 /* lowercase variable name */
3914 // create new object
3915 obj = construct_text_object(buf, arg,
3916 retval->text_object_count, retval->text_objects, line);
3918 // allocate memory for the object
3919 retval->text_objects = realloc(retval->text_objects,
3920 sizeof(struct text_object) *
3921 (retval->text_object_count + 1));
3922 // assign the new object to the end of the list.
3924 &retval->text_objects[retval->text_object_count++],
3925 obj, sizeof(struct text_object));
3931 obj = create_plain_text("$");
3933 // allocate memory for the object
3934 retval->text_objects = realloc(retval->text_objects,
3935 sizeof(struct text_object) *
3936 (retval->text_object_count + 1));
3937 // assign the new object to the end of the list.
3938 memcpy(&retval->text_objects[retval->text_object_count++],
3939 obj, sizeof(struct text_object));
3946 obj = create_plain_text(s);
3948 // allocate memory for the object
3949 retval->text_objects = realloc(retval->text_objects,
3950 sizeof(struct text_object) * (retval->text_object_count + 1));
3951 // assign the new object to the end of the list.
3952 memcpy(&retval->text_objects[retval->text_object_count++], obj,
3953 sizeof(struct text_object));
3958 ERR("one or more $endif's are missing");
3964 static void extract_variable_text(const char *p)
3966 struct text_object_list *list;
3968 free_text_objects(text_object_count, text_objects);
3981 text_object_count = 0;
3982 text_objects = NULL;
3984 list = extract_variable_text_internal(p);
3985 text_objects = list->text_objects;
3986 text_object_count = list->text_object_count;
3991 struct text_object_list *parse_conky_vars(char *text, char *p, struct information *cur)
3993 struct text_object_list *object_list =
3994 extract_variable_text_internal(text);
3996 generate_text_internal(p, max_user_text, object_list->text_objects,
3997 object_list->text_object_count, cur);
4001 /* Allows reading from a FIFO (i.e., /dev/xconsole).
4002 * The file descriptor is set to non-blocking which makes this possible.
4004 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
4005 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
4007 #define TAIL_PIPE_BUFSIZE 4096
4011 int fd = obj->data.tail.fd;
4014 char buf[TAIL_PIPE_BUFSIZE];
4015 ssize_t len = read(fd, buf, sizeof(buf));
4019 if (errno != EAGAIN) {
4020 strcpy(obj->data.tail.buffer, "Logfile Read Error");
4021 snprintf(dst, dst_size, "Logfile Read Error");
4025 } else if (len == 0) {
4026 strcpy(obj->data.tail.buffer, "Logfile Empty");
4027 snprintf(dst, dst_size, "Logfile Empty");
4031 for (line_len = 0, i = 0; i < len; i++) {
4035 if (buf[i] == '\n') {
4038 if (obj->data.tail.readlines > 0) {
4043 for (n = 0; obj->data.tail.buffer[n]; n++) {
4044 if (obj->data.tail.buffer[n] == '\n') {
4049 if (++olines < obj->data.tail.wantedlines) {
4055 p = obj->data.tail.buffer + first_line;
4056 pos = n - first_line;
4057 memmove(obj->data.tail.buffer,
4058 obj->data.tail.buffer + first_line, strlen(p));
4059 obj->data.tail.buffer[pos] = 0;
4065 p = buf + last_line;
4067 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
4068 obj->data.tail.buffer[pos + line_len] = 0;
4071 obj->data.tail.readlines = lines;
4079 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
4082 char *format_time(unsigned long time, const int width)
4085 unsigned long nt; // narrow time, for speed on 32-bit
4086 unsigned cc; // centiseconds
4087 unsigned nn; // multi-purpose whatever
4090 cc = nt % 100; // centiseconds past second
4091 nt /= 100; // total seconds
4092 nn = nt % 60; // seconds past the minute
4093 nt /= 60; // total minutes
4094 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
4098 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
4101 nn = nt % 60; // minutes past the hour
4102 nt /= 60; // total hours
4103 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
4106 nn = nt; // now also hours
4107 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
4110 nn /= 24; // now days
4111 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
4114 nn /= 7; // now weeks
4115 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
4118 // well shoot, this outta' fit...
4119 return strdup("<inf>");
4122 static void generate_text_internal(char *p, int p_max_size,
4123 struct text_object *objs, unsigned int object_count,
4124 struct information *cur)
4129 char buff_in[p_max_size];
4131 iconv_converting = 0;
4134 for (i = 0; i < object_count; i++) {
4135 struct text_object *obj = &objs[i];
4137 if (p_max_size < 1) {
4141 #define OBJ(a) break; case OBJ_##a:
4143 switch (obj->type) {
4145 ERR("not implemented obj type %d", obj->type);
4148 /* does anyone have decimals in acpi temperature? */
4149 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4150 round_to_int(get_acpi_temperature(obj->data.i)));
4153 /* does anyone have decimals in acpi temperature? */
4154 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4155 round_to_int((get_acpi_temperature(obj->data.i) + 40) *
4158 #endif /* !__OpenBSD__ */
4161 obj->a = get_freq(p, p_max_size, "%.0f", 1,
4162 obj->data.cpu_index);
4168 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
4169 obj->data.cpu_index);
4171 /* OpenBSD has no such flag (SUSv2) */
4172 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
4173 obj->data.cpu_index);
4177 #if defined(__linux__)
4180 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
4181 obj->data.cpu_index);
4186 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
4187 obj->data.cpu_index);
4192 OBJ(wireless_essid) {
4193 snprintf(p, p_max_size, "%s", obj->data.net->essid);
4195 OBJ(wireless_mode) {
4196 snprintf(p, p_max_size, "%s", obj->data.net->mode);
4198 OBJ(wireless_bitrate) {
4199 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
4202 snprintf(p, p_max_size, "%s", obj->data.net->ap);
4204 OBJ(wireless_link_qual) {
4205 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
4206 obj->data.net->link_qual);
4208 OBJ(wireless_link_qual_max) {
4209 spaced_print(p, p_max_size, "%d", 4,
4210 "wireless_link_qual_max", obj->data.net->link_qual_max);
4212 OBJ(wireless_link_qual_perc) {
4213 if (obj->data.net->link_qual_max > 0) {
4214 spaced_print(p, p_max_size, "%.0f", 5,
4215 "wireless_link_qual_perc",
4216 (double) obj->data.net->link_qual /
4217 obj->data.net->link_qual_max * 100);
4219 spaced_print(p, p_max_size, "unk", 5,
4220 "wireless_link_qual_perc");
4223 OBJ(wireless_link_bar) {
4224 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4225 obj->data.net->link_qual_max) * 255.0);
4227 #endif /* HAVE_IWLIB */
4229 #endif /* __linux__ */
4232 get_freq_dynamic(p, p_max_size, "%.0f", 1);
4233 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4237 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
4239 get_freq_dynamic(p, p_max_size, "%.2f", 1000);
4241 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4246 get_adt746x_cpu(p, p_max_size);
4249 get_adt746x_fan(p, p_max_size);
4252 get_acpi_fan(p, p_max_size);
4254 OBJ(acpiacadapter) {
4255 get_acpi_ac_adapter(p, p_max_size);
4258 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4261 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4263 OBJ(battery_percent) {
4264 spaced_print(p, p_max_size, "%*d", pad_percents,
4265 "battery_percent", get_battery_perct(obj->data.s));
4268 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4270 #endif /* __OpenBSD__ */
4273 human_readable(cur->buffers * 1024, p, 255, "buffers");
4276 human_readable(cur->cached * 1024, p, 255, "buffers");
4279 if (obj->data.cpu_index > info.cpu_count) {
4280 printf("obj->data.cpu_index %i info.cpu_count %i",
4281 obj->data.cpu_index, info.cpu_count);
4282 CRIT_ERR("attempting to use more CPUs then you have!");
4284 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
4285 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4288 new_bar(p, obj->a, obj->b,
4289 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4292 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
4293 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4297 new_fg(p, obj->data.l);
4329 OBJ(conky_version) {
4330 snprintf(p, p_max_size, "%s", VERSION);
4332 OBJ(conky_build_date) {
4333 snprintf(p, p_max_size, "%s", BUILD_DATE);
4335 OBJ(conky_build_arch) {
4336 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4338 #if defined(__linux__)
4340 snprintf(p, p_max_size, "%s", i8k.version);
4343 snprintf(p, p_max_size, "%s", i8k.bios);
4346 snprintf(p, p_max_size, "%s", i8k.serial);
4349 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
4351 OBJ(i8k_cpu_tempf) {
4354 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4355 snprintf(p, p_max_size, "%.1f", cpu_temp * (9.0 / 5.0) + 32.0);
4357 OBJ(i8k_left_fan_status) {
4358 int left_fan_status;
4360 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4361 if (left_fan_status == 0) {
4362 snprintf(p, p_max_size, "off");
4364 if (left_fan_status == 1) {
4365 snprintf(p, p_max_size, "low");
4367 if (left_fan_status == 2) {
4368 snprintf(p, p_max_size, "high");
4371 OBJ(i8k_right_fan_status) {
4372 int right_fan_status;
4374 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4375 if (right_fan_status == 0) {
4376 snprintf(p, p_max_size, "off");
4378 if (right_fan_status == 1) {
4379 snprintf(p, p_max_size, "low");
4381 if (right_fan_status == 2) {
4382 snprintf(p, p_max_size, "high");
4385 OBJ(i8k_left_fan_rpm) {
4386 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4388 OBJ(i8k_right_fan_rpm) {
4389 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4391 OBJ(i8k_ac_status) {
4394 sscanf(i8k.ac_status, "%d", &ac_status);
4395 if (ac_status == -1) {
4396 snprintf(p, p_max_size, "disabled (read i8k docs)");
4398 if (ac_status == 0) {
4399 snprintf(p, p_max_size, "off");
4401 if (ac_status == 1) {
4402 snprintf(p, p_max_size, "on");
4405 OBJ(i8k_buttons_status) {
4406 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4409 get_ibm_acpi_fan(p, p_max_size);
4412 get_ibm_acpi_temps();
4413 snprintf(p, p_max_size, "%d", ibm_acpi.temps[obj->data.sensor]);
4416 get_ibm_acpi_volume(p, p_max_size);
4418 OBJ(ibm_brightness) {
4419 get_ibm_acpi_brightness(p, p_max_size);
4422 if ((obj->data.ifblock.s)
4423 && (!interface_up(obj->data.ifblock.s))) {
4424 i = obj->data.ifblock.pos;
4431 if (!cur->gw_info.count) {
4432 i = obj->data.ifblock.pos;
4439 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4442 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4445 snprintf(p, p_max_size, "%d", get_laptop_mode());
4448 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4450 #endif /* __linux__ */
4453 OBJ(obsd_sensors_temp) {
4454 obsd_sensors.device = sensor_device;
4455 update_obsd_sensors();
4456 snprintf(p, p_max_size, "%.1f",
4457 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor]);
4459 OBJ(obsd_sensors_fan) {
4460 obsd_sensors.device = sensor_device;
4461 update_obsd_sensors();
4462 snprintf(p, p_max_size, "%d",
4463 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4465 OBJ(obsd_sensors_volt) {
4466 obsd_sensors.device = sensor_device;
4467 update_obsd_sensors();
4468 snprintf(p, p_max_size, "%.2f",
4469 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4472 get_obsd_vendor(p, p_max_size);
4475 get_obsd_product(p, p_max_size);
4477 #endif /* __OpenBSD__ */
4481 new_font(p, obj->data.s);
4484 /* TODO: move this correction from kB to kB/s elsewhere
4485 * (or get rid of it??) */
4487 if (obj->data.diskio) {
4489 (obj->data.diskio->current / update_interval) * 1024LL,
4490 p, p_max_size, "diskio");
4492 human_readable(diskio_value * 1024LL, p, p_max_size,
4497 if (obj->data.diskio) {
4498 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
4501 human_readable(diskio_write_value * 1024LL, p, p_max_size,
4506 if (obj->data.diskio) {
4507 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
4510 human_readable(diskio_read_value * 1024LL, p, p_max_size,
4515 if (obj->data.diskio) {
4516 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4517 obj->data.diskio->current, obj->e, 1);
4519 new_graph(p, obj->a, obj->b, obj->c, obj->d, diskio_value,
4523 OBJ(diskiograph_read) {
4524 if (obj->data.diskio) {
4525 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4526 obj->data.diskio->current_read, obj->e, 1);
4528 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4529 diskio_read_value, obj->e, 1);
4532 OBJ(diskiograph_write) {
4533 if (obj->data.diskio) {
4534 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4535 obj->data.diskio->current_write, obj->e, 1);
4537 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4538 diskio_write_value, obj->e, 1);
4542 spaced_print(p, p_max_size, "%d", 6, "downspeed",
4543 round_to_int(obj->data.net->recv_speed / 1024));
4546 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
4547 obj->data.net->recv_speed / 1024.0);
4549 OBJ(downspeedgraph) {
4550 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4551 obj->data.net->recv_speed / 1024.0, obj->e, 1);
4555 i = obj->data.ifblock.pos - 1;
4565 snprintf(p, p_max_size, "%u.%u.%u.%u",
4566 obj->data.net->addr.sa_data[2] & 255,
4567 obj->data.net->addr.sa_data[3] & 255,
4568 obj->data.net->addr.sa_data[4] & 255,
4569 obj->data.net->addr.sa_data[5] & 255);
4572 #if defined(__linux__)
4574 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4576 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4577 strcpy(p, obj->data.net->addrs);
4580 strcpy(p, "0.0.0.0");
4582 #endif /* __linux__ */
4584 #if defined(IMLIB2) && defined(X11)
4589 Imlib_Image image, buffer;
4591 image = imlib_load_image(obj->data.s);
4592 imlib_context_set_image(image);
4596 w = imlib_image_get_width();
4597 h = imlib_image_get_height();
4598 buffer = imlib_create_image(w, h);
4599 imlib_context_set_display(display);
4600 imlib_context_set_drawable(window.drawable);
4601 imlib_context_set_colormap(DefaultColormap(display,
4603 imlib_context_set_visual(DefaultVisual(display,
4605 imlib_context_set_image(buffer);
4606 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
4607 text_start_x, text_start_y, w, h);
4608 imlib_render_image_on_drawable(text_start_x,
4611 imlib_context_set_image(image);
4619 FILE *fp = popen(obj->data.s, "r");
4620 int length = fread(p, 1, p_max_size, fp);
4625 if (length > 0 && p[length - 1] == '\n') {
4626 p[length - 1] = '\0';
4630 FILE *fp = popen(obj->data.s, "r");
4631 fread(p, 1, p_max_size, fp);
4635 struct information *my_info =
4636 malloc(sizeof(struct information));
4637 memcpy(my_info, cur, sizeof(struct information));
4638 struct text_object_list *text_objects = parse_conky_vars(p, p, my_info);
4640 int length = strlen(p);
4643 if (length > 0 && p[length - 1] == '\n') {
4644 p[length - 1] = '\0';
4647 free_text_objects(text_objects->text_object_count, text_objects->text_objects);
4653 FILE *fp = popen(obj->data.s, "r");
4654 int n2 = fread(p, 1, p_max_size, fp);
4658 if (n2 && p[n2 - 1] == '\n') {
4663 if (*p2 == '\001') {
4670 if (sscanf(p, "%lf", &barnum) == 0) {
4671 ERR("reading execbar value failed (perhaps it's not the "
4672 "correct format?)");
4674 if (barnum > 100 || barnum < 0) {
4675 ERR("your execbar value is not between 0 and 100, "
4676 "therefore it will be ignored");
4678 barnum = barnum / 100.0;
4679 new_bar(p, 0, 4, (int) (barnum * 255.0));
4684 FILE *fp = popen(obj->data.s, "r");
4685 int n2 = fread(p, 1, p_max_size, fp);
4689 if (n2 && p[n2 - 1] == '\n') {
4693 if (*p2 == '\001') {
4700 if (sscanf(p, "%lf", &barnum) == 0) {
4701 ERR("reading execgraph value failed (perhaps it's not the "
4702 "correct format?)");
4704 if (barnum > 100 || barnum < 0) {
4705 ERR("your execgraph value is not between 0 and 100, "
4706 "therefore it will be ignored");
4708 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum),
4713 if (current_update_time - obj->data.execi.last_update
4714 < obj->data.execi.interval) {
4715 new_bar(p, 0, 4, (int) obj->f);
4718 FILE *fp = popen(obj->data.execi.cmd, "r");
4719 int n2 = fread(p, 1, p_max_size, fp);
4723 if (n2 && p[n2 - 1] == '\n') {
4728 if (*p2 == '\001') {
4735 if (sscanf(p, "%f", &barnum) == 0) {
4736 ERR("reading execibar value failed (perhaps it's not "
4737 "the correct format?)");
4739 if (barnum > 100 || barnum < 0) {
4740 ERR("your execibar value is not between 0 and 100, "
4741 "therefore it will be ignored");
4743 obj->f = 255 * barnum / 100.0;
4744 new_bar(p, 0, 4, (int) obj->f);
4746 obj->data.execi.last_update = current_update_time;
4750 if (current_update_time - obj->data.execi.last_update
4751 < obj->data.execi.interval) {
4752 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
4755 FILE *fp = popen(obj->data.execi.cmd, "r");
4756 int n2 = fread(p, 1, p_max_size, fp);
4760 if (n2 && p[n2 - 1] == '\n') {
4765 if (*p2 == '\001') {
4772 if (sscanf(p, "%f", &barnum) == 0) {
4773 ERR("reading execigraph value failed (perhaps it's not "
4774 "the correct format?)");
4776 if (barnum > 100 || barnum < 0) {
4777 ERR("your execigraph value is not between 0 and 100, "
4778 "therefore it will be ignored");
4781 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f),
4784 obj->data.execi.last_update = current_update_time;
4788 if (current_update_time - obj->data.execi.last_update
4789 < obj->data.execi.interval
4790 || obj->data.execi.interval == 0) {
4791 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
4793 char *output = obj->data.execi.buffer;
4794 FILE *fp = popen(obj->data.execi.cmd, "r");
4796 int length = fread(output, 1, text_buffer_size, fp);
4799 output[length] = '\0';
4800 if (length > 0 && output[length - 1] == '\n') {
4801 output[length - 1] = '\0';
4803 obj->data.execi.last_update = current_update_time;
4804 snprintf(p, p_max_size, "%s", output);
4806 // parse_conky_vars(output, p, cur);
4809 struct information *my_info =
4810 malloc(sizeof(struct information));
4811 memcpy(my_info, cur, sizeof(struct information));
4812 struct text_object_list *text_objects = 0;
4814 if (current_update_time - obj->data.execi.last_update
4815 < obj->data.execi.interval
4816 || obj->data.execi.interval == 0) {
4817 text_objects = parse_conky_vars(obj->data.execi.buffer, p, my_info);
4819 char *output = obj->data.execi.buffer;
4820 FILE *fp = popen(obj->data.execi.cmd, "r");
4821 int length = fread(output, 1, text_buffer_size, fp);
4825 output[length] = '\0';
4826 if (length > 0 && output[length - 1] == '\n') {
4827 output[length - 1] = '\0';
4830 text_objects = parse_conky_vars(obj->data.execi.buffer, p, my_info);
4831 obj->data.execi.last_update = current_update_time;
4833 free_text_objects(text_objects->text_object_count, text_objects->text_objects);
4838 if (!obj->data.texeci.p_timed_thread) {
4839 obj->data.texeci.p_timed_thread =
4840 timed_thread_create((void *) threaded_exec,
4841 (void *) obj, obj->data.texeci.interval * 1000000);
4842 if (!obj->data.texeci.p_timed_thread) {
4843 ERR("Error creating texeci timed thread");
4845 timed_thread_register(obj->data.texeci.p_timed_thread,
4846 &obj->data.texeci.p_timed_thread);
4847 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4848 ERR("Error running texeci timed thread");
4851 timed_thread_lock(obj->data.texeci.p_timed_thread);
4852 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
4853 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4855 #endif /* HAVE_POPEN */
4857 if (obj->global_mode && info.mail) {
4858 // this means we use info
4859 if (!info.mail->p_timed_thread) {
4860 info.mail->p_timed_thread =
4861 timed_thread_create((void *) imap_thread,
4862 (void *) info.mail, info.mail->interval * 1000000);
4863 if (!info.mail->p_timed_thread) {
4864 ERR("Error creating imap timed thread");
4866 timed_thread_register(info.mail->p_timed_thread,
4867 &info.mail->p_timed_thread);
4868 if (timed_thread_run(info.mail->p_timed_thread)) {
4869 ERR("Error running imap timed thread");
4872 timed_thread_lock(info.mail->p_timed_thread);
4873 snprintf(p, p_max_size, "%lu", info.mail->unseen);
4874 timed_thread_unlock(info.mail->p_timed_thread);
4875 } else if (obj->data.mail) {
4876 // this means we use obj
4877 if (!obj->data.mail->p_timed_thread) {
4878 obj->data.mail->p_timed_thread =
4879 timed_thread_create((void *) imap_thread,
4880 (void *) obj->data.mail,
4881 obj->data.mail->interval * 1000000);
4882 if (!obj->data.mail->p_timed_thread) {
4883 ERR("Error creating imap timed thread");
4885 timed_thread_register(obj->data.mail->p_timed_thread,
4886 &obj->data.mail->p_timed_thread);
4887 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4888 ERR("Error running imap timed thread");
4891 timed_thread_lock(obj->data.mail->p_timed_thread);
4892 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
4893 timed_thread_unlock(obj->data.mail->p_timed_thread);
4894 } else if (!obj->a) {
4895 // something is wrong, warn once then stop
4896 ERR("Theres a problem with your imap_unseen settings. "
4897 "Check that the global IMAP settings are defined "
4898 "properly (line %li).", obj->line);
4902 OBJ(imap_messages) {
4903 if (obj->global_mode && info.mail) {
4904 // this means we use info
4905 if (!info.mail->p_timed_thread) {
4906 info.mail->p_timed_thread =
4907 timed_thread_create((void *) imap_thread,
4908 (void *) info.mail, info.mail->interval * 1000000);
4909 if (!info.mail->p_timed_thread) {
4910 ERR("Error creating imap timed thread");
4912 timed_thread_register(info.mail->p_timed_thread,
4913 &info.mail->p_timed_thread);
4914 if (timed_thread_run(info.mail->p_timed_thread)) {
4915 ERR("Error running imap timed thread");
4918 timed_thread_lock(info.mail->p_timed_thread);
4919 snprintf(p, p_max_size, "%lu", info.mail->messages);
4920 timed_thread_unlock(info.mail->p_timed_thread);
4921 } else if (obj->data.mail) {
4922 // this means we use obj
4923 if (!obj->data.mail->p_timed_thread) {
4924 obj->data.mail->p_timed_thread =
4925 timed_thread_create((void *) imap_thread,
4926 (void *) obj->data.mail,
4927 obj->data.mail->interval * 1000000);
4928 if (!obj->data.mail->p_timed_thread) {
4929 ERR("Error creating imap timed thread");
4931 timed_thread_register(obj->data.mail->p_timed_thread,
4932 &obj->data.mail->p_timed_thread);
4933 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4934 ERR("Error runninging imap timed thread");
4937 timed_thread_lock(obj->data.mail->p_timed_thread);
4938 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
4939 timed_thread_lock(obj->data.mail->p_timed_thread);
4940 } else if (!obj->a) {
4941 // something is wrong, warn once then stop
4942 ERR("Theres a problem with your imap_messages settings. "
4943 "Check that the global IMAP settings are defined "
4944 "properly (line %li).", obj->line);
4949 if (obj->global_mode && info.mail) {
4950 // this means we use info
4951 if (!info.mail->p_timed_thread) {
4952 info.mail->p_timed_thread =
4953 timed_thread_create((void *) pop3_thread,
4954 (void *) info.mail, info.mail->interval * 1000000);
4955 if (!info.mail->p_timed_thread) {
4956 ERR("Error creating pop3 timed thread");
4958 timed_thread_register(info.mail->p_timed_thread,
4959 &info.mail->p_timed_thread);
4960 if (timed_thread_run(info.mail->p_timed_thread)) {
4961 ERR("Error running pop3 timed thread");
4964 timed_thread_lock(info.mail->p_timed_thread);
4965 snprintf(p, p_max_size, "%lu", info.mail->unseen);
4966 timed_thread_unlock(info.mail->p_timed_thread);
4967 } else if (obj->data.mail) {
4968 // this means we use obj
4969 if (!obj->data.mail->p_timed_thread) {
4970 obj->data.mail->p_timed_thread =
4971 timed_thread_create((void *) pop3_thread,
4972 (void *) obj->data.mail,
4973 obj->data.mail->interval * 1000000);
4974 if (!obj->data.mail->p_timed_thread) {
4975 ERR("Error creating pop3 timed thread");
4977 timed_thread_register(obj->data.mail->p_timed_thread,
4978 &obj->data.mail->p_timed_thread);
4979 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4980 ERR("Error running pop3 timed thread");
4983 timed_thread_lock(obj->data.mail->p_timed_thread);
4984 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
4985 timed_thread_unlock(obj->data.mail->p_timed_thread);
4986 } else if (!obj->a) {
4987 // something is wrong, warn once then stop
4988 ERR("Theres a problem with your pop3_unseen settings. "
4989 "Check that the global POP3 settings are defined "
4990 "properly (line %li).", obj->line);
4995 if (obj->global_mode && info.mail) {
4996 // this means we use info
4997 if (!info.mail->p_timed_thread) {
4998 info.mail->p_timed_thread =
4999 timed_thread_create((void *) pop3_thread,
5000 (void *) info.mail, info.mail->interval * 1000000);
5001 if (!info.mail->p_timed_thread) {
5002 ERR("Error creating pop3 timed thread");
5004 timed_thread_register(info.mail->p_timed_thread,
5005 &info.mail->p_timed_thread);
5006 if (timed_thread_run(info.mail->p_timed_thread)) {
5007 ERR("Error running pop3 timed thread");
5010 timed_thread_lock(info.mail->p_timed_thread);
5011 snprintf(p, p_max_size, "%.1f",
5012 info.mail->used / 1024.0 / 1024.0);
5013 timed_thread_unlock(info.mail->p_timed_thread);
5014 } else if (obj->data.mail) {
5015 // this means we use obj
5016 if (!obj->data.mail->p_timed_thread) {
5017 obj->data.mail->p_timed_thread =
5018 timed_thread_create((void *) pop3_thread,
5019 (void *) obj->data.mail,
5020 obj->data.mail->interval * 1000000);
5021 if (!obj->data.mail->p_timed_thread) {
5022 ERR("Error creating pop3 timed thread");
5024 timed_thread_register(obj->data.mail->p_timed_thread,
5025 &obj->data.mail->p_timed_thread);
5026 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
5027 ERR("Error running pop3 timed thread");
5030 timed_thread_lock(obj->data.mail->p_timed_thread);
5031 snprintf(p, p_max_size, "%.1f",
5032 obj->data.mail->used / 1024.0 / 1024.0);
5033 timed_thread_unlock(obj->data.mail->p_timed_thread);
5034 } else if (!obj->a) {
5035 // something is wrong, warn once then stop
5036 ERR("Theres a problem with your pop3_used settings. "
5037 "Check that the global POP3 settings are defined "
5038 "properly (line %li).", obj->line);
5043 if (obj->data.fs != NULL) {
5044 if (obj->data.fs->size == 0) {
5045 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5047 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5048 (int) (255 - obj->data.fsbar.fs->avail * 255 /
5049 obj->data.fs->size));
5054 if (obj->data.fs != NULL) {
5055 human_readable(obj->data.fs->avail, p, 255, "fs_free");
5059 if (obj->data.fs != NULL) {
5060 if (obj->data.fs->size) {
5061 spaced_print(p, p_max_size, "%*d", pad_percents,
5062 "fs_free_perc", (int) ((obj->data.fs->avail * 100) /
5063 obj->data.fs->size));
5065 snprintf(p, p_max_size, "0");
5070 if (obj->data.fs != NULL) {
5071 human_readable(obj->data.fs->size, p, 255, "fs_size");
5075 if (obj->data.fs != NULL)
5076 snprintf(p, p_max_size, "%s", obj->data.fs->type);
5079 if (obj->data.fs != NULL) {
5080 human_readable(obj->data.fs->size - (obj->data.fs->free
5081 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
5086 if (obj->data.fs != NULL) {
5087 if (obj->data.fs->size == 0) {
5088 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5090 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5091 (int) (obj->data.fsbar.fs->avail * 255 /
5092 obj->data.fs->size));
5097 if (obj->data.fs != NULL) {
5098 if (obj->data.fs->size) {
5099 spaced_print(p, 4, "%*d", pad_percents,
5100 "fs_used_perc", 100 - ((int) ((obj->data.fs->avail * 100) /
5101 obj->data.fs->size)));
5103 snprintf(p, p_max_size, "0");
5108 float *v = info.loadavg;
5110 if (obj->data.loadavg[2]) {
5111 snprintf(p, p_max_size, "%.2f %.2f %.2f",
5112 v[obj->data.loadavg[0] - 1],
5113 v[obj->data.loadavg[1] - 1],
5114 v[obj->data.loadavg[2] - 1]);
5115 } else if (obj->data.loadavg[1]) {
5116 snprintf(p, p_max_size, "%.2f %.2f",
5117 v[obj->data.loadavg[0] - 1],
5118 v[obj->data.loadavg[1] - 1]);
5119 } else if (obj->data.loadavg[0]) {
5120 snprintf(p, p_max_size, "%.2f",
5121 v[obj->data.loadavg[0] - 1]);
5125 new_goto(p, obj->data.i);
5128 new_tab(p, obj->data.pair.a, obj->data.pair.b);
5131 new_hr(p, obj->data.i);
5135 PRSS *data = get_rss_info(obj->data.rss.uri,
5136 obj->data.rss.delay);
5140 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
5142 if (!strcmp(obj->data.rss.action, "feed_title")) {
5144 // remove trailing new line if one exists
5145 if (str[strlen(str) - 1] == '\n') {
5146 str[strlen(str) - 1] = 0;
5148 snprintf(p, p_max_size, "%s", str);
5149 } else if (!strcmp(obj->data.rss.action, "item_title")) {
5150 if (obj->data.rss.act_par < data->item_count) {
5151 str = data->items[obj->data.rss.act_par].title;
5152 // remove trailing new line if one exists
5153 if (str[strlen(str) - 1] == '\n') {
5154 str[strlen(str) - 1] = 0;
5156 snprintf(p, p_max_size, "%s", str);
5158 } else if (!strcmp(obj->data.rss.action, "item_desc")) {
5159 if (obj->data.rss.act_par < data->item_count) {
5161 data->items[obj->data.rss.act_par].description;
5162 // remove trailing new line if one exists
5163 if (str[strlen(str) - 1] == '\n') {
5164 str[strlen(str) - 1] = 0;
5166 snprintf(p, p_max_size, "%s", str);
5168 } else if (!strcmp(obj->data.rss.action, "item_titles")) {
5169 if (data->item_count > 0) {
5175 if (obj->data.rss.act_par > data->item_count) {
5176 show = data->item_count;
5178 show = obj->data.rss.act_par;
5180 for (itmp = 0; itmp < show; itmp++) {
5181 PRSS_Item *item = &data->items[itmp];
5185 // don't add new line before first item
5187 strncat(p, "\n", p_max_size);
5189 /* remove trailing new line if one exists,
5190 * we have our own */
5191 if (str[strlen(str) - 1] == '\n') {
5192 str[strlen(str) - 1] = 0;
5194 strncat(p, str, p_max_size);
5207 temp = get_hddtemp_info(obj->data.hddtemp.dev,
5208 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
5210 snprintf(p, p_max_size, "N/A");
5211 } else if (unit == '*') {
5212 snprintf(p, p_max_size, "%s", temp);
5214 snprintf(p, p_max_size, "%s°%c", temp, unit);
5219 new_offset(p, obj->data.i);
5222 new_voffset(p, obj->data.i);
5228 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5229 obj->data.sysfs.devtype, obj->data.sysfs.type);
5231 if (r >= 100.0 || r == 0) {
5232 snprintf(p, p_max_size, "%d", (int) r);
5234 snprintf(p, p_max_size, "%.1f", r);
5240 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5241 obj->data.sysfs.devtype, obj->data.sysfs.type);
5243 if (r >= 100.0 || r == 0) {
5244 snprintf(p, p_max_size, "%d", (int) r);
5246 snprintf(p, p_max_size, "%.1f", r);
5252 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5253 obj->data.sysfs.devtype, obj->data.sysfs.type);
5255 if (r >= 100.0 || r == 0) {
5256 snprintf(p, p_max_size, "%d", (int) r);
5258 snprintf(p, p_max_size, "%.1f", r);
5261 #endif /* !__OpenBSD__ */
5263 new_alignr(p, obj->data.i);
5266 new_alignc(p, obj->data.i);
5269 struct information *my_info =
5270 malloc(sizeof(struct information));
5272 memcpy(my_info, cur, sizeof(struct information));
5273 struct text_object_list *text_objects = parse_conky_vars(obj->data.ifblock.s, p, my_info);
5274 if (strlen(p) != 0) {
5275 i = obj->data.ifblock.pos;
5281 free_text_objects(text_objects->text_object_count, text_objects->text_objects);
5288 if ((obj->data.ifblock.s)
5289 && (stat(obj->data.ifblock.s, &tmp) == -1)) {
5290 i = obj->data.ifblock.pos;
5293 if (obj->data.ifblock.str) {
5294 if (!check_contains(obj->data.ifblock.s,
5295 obj->data.ifblock.str)) {
5296 i = obj->data.ifblock.pos;
5307 if ((obj->data.ifblock.s)
5308 && (!check_mount(obj->data.ifblock.s))) {
5309 i = obj->data.ifblock.pos;
5316 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
5317 i = obj->data.ifblock.pos;
5324 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
5327 snprintf(p, p_max_size, "%s", cur->uname_s.release);
5330 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
5335 human_readable(cur->mem * 1024, p, 255, "mem");
5338 human_readable(cur->memmax * 1024, p, 255, "memmax");
5342 spaced_print(p, p_max_size, "%*Lu", 4, "memperc",
5343 pad_percents, cur->mem * 100 / cur->memmax);
5347 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5348 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5351 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5352 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5358 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
5361 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
5364 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
5367 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5368 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
5371 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5372 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
5375 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5376 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
5381 update_mail_count(&obj->data.local_mail);
5382 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5385 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5387 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5390 update_mail_count(&obj->data.local_mail);
5391 snprintf(p, p_max_size, "%d",
5392 obj->data.local_mail.new_mail_count);
5395 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5398 new_outline(p, obj->data.l);
5401 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
5403 OBJ(running_processes) {
5404 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
5407 snprintf(p, p_max_size, "%s", obj->data.s);
5410 new_bg(p, obj->data.l);
5413 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5416 human_readable(cur->swap * 1024, p, 255, "swap");
5419 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
5422 if (cur->swapmax == 0) {
5423 strncpy(p, "No swap", p_max_size);
5425 spaced_print(p, p_max_size, "%*Lu", 4, "swapperc",
5426 pad_percents, cur->swap * 100 / cur->swapmax);
5430 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5431 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5434 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5437 time_t t = time(NULL);
5438 struct tm *tm = localtime(&t);
5440 setlocale(LC_TIME, "");
5441 strftime(p, p_max_size, obj->data.s, tm);
5444 time_t t = time(NULL);
5445 struct tm *tm = gmtime(&t);
5447 strftime(p, p_max_size, obj->data.s, tm);
5452 if (obj->data.tztime.tz) {
5453 oldTZ = getenv("TZ");
5454 setenv("TZ", obj->data.tztime.tz, 1);
5457 time_t t = time(NULL);
5458 struct tm *tm = localtime(&t);
5460 setlocale(LC_TIME, "");
5461 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5463 setenv("TZ", oldTZ, 1);
5468 // Needless to free oldTZ since getenv gives ptr to static data
5471 human_readable(obj->data.net->recv, p, 255, "totaldown");
5474 human_readable(obj->data.net->trans, p, 255, "totalup");
5477 snprintf(p, p_max_size, "%d", total_updates);
5480 spaced_print(p, p_max_size, "%d", 6, "upspeed",
5481 round_to_int(obj->data.net->trans_speed / 1024));
5484 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
5485 obj->data.net->trans_speed / 1024.0);
5488 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5489 obj->data.net->trans_speed / 1024.0, obj->e, 1);
5492 format_seconds_short(p, p_max_size, (int) cur->uptime);
5495 format_seconds(p, p_max_size, (int) cur->uptime);
5498 snprintf(p, p_max_size, "%s", cur->users.names);
5501 snprintf(p, p_max_size, "%s", cur->users.terms);
5504 snprintf(p, p_max_size, "%s", cur->users.times);
5507 snprintf(p, p_max_size, "%d", cur->users.number);
5509 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5510 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5514 msg = get_apm_adapter();
5515 snprintf(p, p_max_size, "%s", msg);
5518 OBJ(apm_battery_life) {
5521 msg = get_apm_battery_life();
5522 snprintf(p, p_max_size, "%s", msg);
5525 OBJ(apm_battery_time) {
5528 msg = get_apm_battery_time();
5529 snprintf(p, p_max_size, "%s", msg);
5532 #endif /* __FreeBSD__ __OpenBSD__ */
5536 snprintf(p, cur->mpd.max_title_len > 0
5537 ? cur->mpd.max_title_len : p_max_size, "%s",
5541 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5544 snprintf(p, p_max_size, "%s", cur->mpd.album);
5547 snprintf(p, p_max_size, "%s", cur->mpd.random);
5550 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
5553 snprintf(p, p_max_size, "%s", cur->mpd.track);
5556 snprintf(p, p_max_size, "%s", cur->mpd.name);
5559 snprintf(p, p_max_size, "%s", cur->mpd.file);
5562 snprintf(p, p_max_size, "%i", cur->mpd.volume);
5565 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
5568 snprintf(p, p_max_size, "%s", cur->mpd.status);
5571 int days = 0, hours = 0, minutes = 0, seconds = 0;
5572 int tmp = cur->mpd.elapsed;
5574 while (tmp >= 86400) {
5578 while (tmp >= 3600) {
5588 snprintf(p, p_max_size, "%i days %i:%02i:%02i", days,
5589 hours, minutes, seconds);
5590 } else if (hours > 0) {
5591 snprintf(p, p_max_size, "%i:%02i:%02i", hours, minutes,
5594 snprintf(p, p_max_size, "%i:%02i", minutes, seconds);
5598 int days = 0, hours = 0, minutes = 0, seconds = 0;
5599 int tmp = cur->mpd.length;
5601 while (tmp >= 86400) {
5605 while (tmp >= 3600) {
5615 snprintf(p, p_max_size, "%i days %i:%02i:%02i", days,
5616 hours, minutes, seconds);
5617 } else if (hours > 0) {
5618 snprintf(p, p_max_size, "%i:%02i:%02i", hours, minutes,
5621 snprintf(p, p_max_size, "%i:%02i", minutes, seconds);
5625 spaced_print(p, p_max_size, "%*d", pad_percents,
5626 "mpd_percent", (int) (cur->mpd.progress * 100));
5629 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5630 (int) (cur->mpd.progress * 255.0f));
5633 memset(p, 0, p_max_size);
5634 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
5635 && *cur->mpd.title) {
5636 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist,
5638 } else if (cur->mpd.title && *cur->mpd.title) {
5639 snprintf(p, p_max_size, "%s", cur->mpd.title);
5640 } else if (cur->mpd.artist && *cur->mpd.artist) {
5641 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5642 } else if (cur->mpd.file && *cur->mpd.file) {
5643 snprintf(p, p_max_size, "%s", cur->mpd.file);
5652 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5655 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5658 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5661 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5663 OBJ(xmms2_comment) {
5664 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5667 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5670 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5673 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5675 OBJ(xmms2_tracknr) {
5676 if (cur->xmms2.tracknr != -1) {
5677 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5680 OBJ(xmms2_bitrate) {
5681 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5684 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5687 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5689 OBJ(xmms2_elapsed) {
5690 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5691 (cur->xmms2.elapsed / 1000) % 60);
5693 OBJ(xmms2_duration) {
5694 snprintf(p, p_max_size, "%02d:%02d",
5695 cur->xmms2.duration / 60000,
5696 (cur->xmms2.duration / 1000) % 60);
5698 OBJ(xmms2_percent) {
5699 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5702 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5703 (int) (cur->xmms2.progress * 255.0f));
5705 OBJ(xmms2_playlist) {
5706 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5708 OBJ(xmms2_timesplayed) {
5709 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5712 if (strlen(cur->xmms2.title) < 2
5713 && strlen(cur->xmms2.title) < 2) {
5714 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5716 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5722 OBJ(audacious_status) {
5723 snprintf(p, p_max_size, "%s",
5724 cur->audacious.items[AUDACIOUS_STATUS]);
5726 OBJ(audacious_title) {
5727 snprintf(p, cur->audacious.max_title_len > 0
5728 ? cur->audacious.max_title_len : p_max_size, "%s",
5729 cur->audacious.items[AUDACIOUS_TITLE]);
5731 OBJ(audacious_length) {
5732 snprintf(p, p_max_size, "%s",
5733 cur->audacious.items[AUDACIOUS_LENGTH]);
5735 OBJ(audacious_length_seconds) {
5736 snprintf(p, p_max_size, "%s",
5737 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5739 OBJ(audacious_position) {
5740 snprintf(p, p_max_size, "%s",
5741 cur->audacious.items[AUDACIOUS_POSITION]);
5743 OBJ(audacious_position_seconds) {
5744 snprintf(p, p_max_size, "%s",
5745 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5747 OBJ(audacious_bitrate) {
5748 snprintf(p, p_max_size, "%s",
5749 cur->audacious.items[AUDACIOUS_BITRATE]);
5751 OBJ(audacious_frequency) {
5752 snprintf(p, p_max_size, "%s",
5753 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5755 OBJ(audacious_channels) {
5756 snprintf(p, p_max_size, "%s",
5757 cur->audacious.items[AUDACIOUS_CHANNELS]);
5759 OBJ(audacious_filename) {
5760 snprintf(p, p_max_size, "%s",
5761 cur->audacious.items[AUDACIOUS_FILENAME]);
5763 OBJ(audacious_playlist_length) {
5764 snprintf(p, p_max_size, "%s",
5765 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5767 OBJ(audacious_playlist_position) {
5768 snprintf(p, p_max_size, "%s",
5769 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5771 OBJ(audacious_bar) {
5775 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5776 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5777 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5783 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5786 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5789 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5792 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5795 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5798 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5802 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5805 switch (obj->data.top.type) {
5807 snprintf(p, 16, "%-15s",
5808 cur->cpu[obj->data.top.num]->name);
5811 snprintf(p, 7, "%6.2f",
5812 cur->cpu[obj->data.top.num]->amount);
5815 snprintf(p, 6, "%5i",
5816 cur->cpu[obj->data.top.num]->pid);
5819 snprintf(p, 7, "%6.2f",
5820 cur->cpu[obj->data.top.num]->totalmem);
5824 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
5825 snprintf(p, 10, "%9s", time);
5829 ERR("Unhandled top data type: %d\n",
5830 obj->data.top.type);
5833 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5837 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5840 switch (obj->data.top.type) {
5842 snprintf(p, 16, "%-15s",
5843 cur->memu[obj->data.top.num]->name);
5846 snprintf(p, 7, "%6.2f",
5847 cur->memu[obj->data.top.num]->amount);
5850 snprintf(p, 6, "%5i",
5851 cur->memu[obj->data.top.num]->pid);
5854 snprintf(p, 7, "%6.2f",
5855 cur->memu[obj->data.top.num]->totalmem);
5859 cur->memu[obj->data.top.num]->total_cpu_time,
5861 snprintf(p, 10, "%9s", time);
5865 ERR("Unhandled top data type: %d\n",
5866 obj->data.top.type);
5869 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5873 if (current_update_time - obj->data.tail.last_update
5874 < obj->data.tail.interval) {
5875 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5877 obj->data.tail.last_update = current_update_time;
5882 if (obj->data.tail.fd != -1) {
5883 tail_pipe(obj, p, p_max_size);
5887 fp = fopen(obj->data.tail.logfile, "rt");
5890 /* Send one message, but do not consistently spam
5891 * on missing logfiles. */
5892 if (obj->data.tail.readlines != 0) {
5893 ERR("tail logfile failed to open");
5894 strcpy(obj->data.tail.buffer, "Logfile Missing");
5896 obj->data.tail.readlines = 0;
5897 snprintf(p, p_max_size, "Logfile Missing");
5899 obj->data.tail.readlines = 0;
5900 /* -1 instead of 0 to avoid counting a trailing
5902 fseek(fp, -1, SEEK_END);
5903 bsize = ftell(fp) + 1;
5904 for (iter = obj->data.tail.wantedlines; iter > 0;
5906 nl = rev_fcharfind(fp, '\n', iter);
5911 obj->data.tail.readlines = iter;
5912 if (obj->data.tail.readlines
5913 < obj->data.tail.wantedlines) {
5914 fseek(fp, 0, SEEK_SET);
5916 fseek(fp, nl + 1, SEEK_SET);
5919 /* Make sure bsize is at least 1 byte smaller than the
5920 * buffer max size. */
5921 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
5922 fseek(fp, bsize - text_buffer_size * 20 - 1,
5924 bsize = text_buffer_size * 20 - 1;
5926 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
5929 /* Clean up trailing newline, make sure the
5930 * buffer is null terminated. */
5931 if (obj->data.tail.buffer[bsize - 1] == '\n') {
5932 obj->data.tail.buffer[bsize - 1] = '\0';
5934 obj->data.tail.buffer[bsize] = '\0';
5936 snprintf(p, p_max_size, "%s",
5937 obj->data.tail.buffer);
5939 strcpy(obj->data.tail.buffer, "Logfile Empty");
5940 snprintf(p, p_max_size, "Logfile Empty");
5943 } /* if cur_upd_time >= */
5944 // parse_conky_vars(obj->data.tail.buffer, p, cur);
5949 if (current_update_time - obj->data.tail.last_update
5950 < obj->data.tail.interval) {
5951 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5953 obj->data.tail.last_update = current_update_time;
5958 fp = fopen(obj->data.tail.logfile, "rt");
5960 /* Send one message, but do not consistently spam
5961 * on missing logfiles. */
5962 if (obj->data.tail.readlines != 0) {
5963 ERR("head logfile failed to open");
5964 strcpy(obj->data.tail.buffer, "Logfile Missing");
5966 obj->data.tail.readlines = 0;
5967 snprintf(p, p_max_size, "Logfile Missing");
5969 obj->data.tail.readlines = 0;
5970 for (iter = obj->data.tail.wantedlines; iter > 0;
5972 nl = fwd_fcharfind(fp, '\n', iter);
5977 obj->data.tail.readlines = iter;
5978 /* Make sure nl is at least 1 byte smaller than the
5979 * buffer max size. */
5980 if (nl > (long) ((text_buffer_size * 20) - 1)) {
5981 nl = text_buffer_size * 20 - 1;
5983 nl = fread(obj->data.tail.buffer, 1, nl, fp);
5986 /* Clean up trailing newline, make sure the buffer
5987 * is null terminated. */
5988 if (obj->data.tail.buffer[nl - 1] == '\n') {
5989 obj->data.tail.buffer[nl - 1] = '\0';
5991 obj->data.tail.buffer[nl] = '\0';
5993 snprintf(p, p_max_size, "%s",
5994 obj->data.tail.buffer);
5996 strcpy(obj->data.tail.buffer, "Logfile Empty");
5997 snprintf(p, p_max_size, "Logfile Empty");
5999 } /* if fp == null */
6000 } /* cur_upd_time >= */
6001 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6004 #ifdef TCP_PORT_MONITOR
6006 /* grab a pointer to this port monitor */
6007 tcp_port_monitor_t *p_monitor =
6008 find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
6009 obj->data.tcp_port_monitor.port_range_begin,
6010 obj->data.tcp_port_monitor.port_range_end);
6013 snprintf(p, p_max_size, "monitor not found");
6017 /* now grab the text of interest */
6018 if (peek_tcp_port_monitor(p_monitor,
6019 obj->data.tcp_port_monitor.item,
6020 obj->data.tcp_port_monitor.connection_index, p,
6022 snprintf(p, p_max_size, "monitor peek error");
6030 iconv_converting = 1;
6031 iconv_selected = obj->a;
6034 iconv_converting = 0;
6039 OBJ(entropy_avail) {
6040 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
6042 OBJ(entropy_poolsize) {
6043 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
6046 double entropy_perc;
6048 entropy_perc = (double) cur->entropy.entropy_avail /
6049 (double) cur->entropy.poolsize;
6050 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
6056 s = smapi_get_val(obj->data.s);
6057 snprintf(p, p_max_size, "%s", s);
6061 OBJ(if_smapi_bat_installed) {
6063 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
6064 if(!smapi_bat_installed(idx)) {
6065 i = obj->data.ifblock.pos;
6070 ERR("argument to if_smapi_bat_installed must be an integer");
6072 OBJ(smapi_bat_perc) {
6074 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6075 val = smapi_bat_installed(idx) ?
6076 smapi_get_bat_int(idx, "remaining_percent") : 0;
6077 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
6079 ERR("argument to smapi_bat_perc must be an integer");
6081 OBJ(smapi_bat_bar) {
6082 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
6083 new_bar(p, obj->a, obj->b, (int)
6084 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
6086 new_bar(p, obj->a, obj->b, 0);
6094 unsigned int a = strlen(p);
6097 if (a > 0 && iconv_converting && iconv_selected > 0
6098 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6100 size_t dummy1, dummy2;
6101 char *ptr = buff_in;
6104 dummy1 = dummy2 = a;
6106 strncpy(buff_in, p, p_max_size);
6108 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6109 while (dummy1 > 0) {
6110 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6113 ERR("Iconv codeset conversion failed");
6118 /* It is nessecary when we are converting from multibyte to
6119 * singlebyte codepage */
6122 #endif /* HAVE_ICONV */
6129 double current_update_time, last_update_time;
6131 static void generate_text()
6133 struct information *cur = &info;
6140 current_update_time = get_time();
6143 /* fix diskio rates to b/s (use update_interval */
6144 diskio_read_value = diskio_read_value / update_interval;
6145 diskio_write_value = diskio_write_value / update_interval;
6146 diskio_value = diskio_value / update_interval;
6148 /* add things to the buffer */
6154 generate_text_internal(p, max_user_text, text_objects, text_object_count, cur);
6156 if (stuff_in_upper_case) {
6166 last_update_time = current_update_time;
6172 static void set_font()
6176 if (window.xftdraw != NULL) {
6177 XftDrawDestroy(window.xftdraw);
6179 window.xftdraw = XftDrawCreate(display, window.drawable,
6180 DefaultVisual(display, screen), DefaultColormap(display, screen));
6184 XSetFont(display, window.gc, fonts[selected_font].font->fid);
6190 static inline int get_string_width(const char *s)
6193 return *s ? calc_text_width(s, strlen(s)) : 0;
6199 static inline int get_string_width_special(char *s)
6214 if (*p == SPECIAL_CHAR) {
6215 /* shift everything over by 1 so that the special char
6216 * doesn't mess up the size calculation */
6217 for (i = 0; i < strlen(p); i++) {
6218 *(p + i) = *(p + i + 1);
6220 if (specials[special_index + index].type == GRAPH
6221 || specials[special_index + index].type == BAR) {
6222 width += specials[special_index + index].width;
6229 if (strlen(final) > 1) {
6230 width += calc_text_width(final, strlen(final));
6240 static void text_size_updater(char *s);
6242 int last_font_height;
6243 static void update_text_area()
6247 /* update text size if it isn't fixed */
6252 text_width = minimum_width;
6255 last_font_height = font_height();
6256 for_each_line(text_buffer, text_size_updater);
6258 if (text_height < minimum_height) {
6259 text_height = minimum_height;
6261 if (text_width > maximum_width && maximum_width > 0) {
6262 text_width = maximum_width;
6266 /* get text position on workarea */
6267 switch (text_alignment) {
6274 x = workarea[2] - text_width - gap_x;
6279 x = workarea[2] / 2 - text_width / 2 - gap_x;
6286 y = workarea[3] - text_height - gap_y;
6290 x = workarea[2] - text_width - gap_x;
6291 y = workarea[3] - text_height - gap_y;
6295 x = workarea[2] / 2 - text_width / 2 - gap_x;
6296 y = workarea[3] - text_height - gap_y;
6301 y = workarea[3] / 2 - text_height / 2 - gap_y;
6305 x = workarea[2] - text_width - gap_x;
6306 y = workarea[3] / 2 - text_height / 2 - gap_y;
6310 case NONE: // Let the WM manage the window
6321 if (own_window && !fixed_pos) {
6324 text_start_x = border_margin + 1;
6325 text_start_y = border_margin + 1;
6326 window.x = x - border_margin - 1;
6327 window.y = y - border_margin - 1;
6331 /* If window size doesn't match to workarea's size,
6332 * then window probably includes panels (gnome).
6333 * Blah, doesn't work on KDE. */
6334 if (workarea[2] != window.width || workarea[3] != window.height) {
6346 static int cur_x, cur_y; /* current x and y for drawing */
6347 static int draw_mode; /* FG, BG or OUTLINE */
6348 static long current_color;
6351 static void text_size_updater(char *s)
6356 /* get string widths and skip specials */
6359 if (*p == SPECIAL_CHAR) {
6361 w += get_string_width(s);
6364 if (specials[special_index].type == BAR
6365 || specials[special_index].type == GRAPH) {
6366 w += specials[special_index].width;
6367 if (specials[special_index].height > last_font_height) {
6368 last_font_height = specials[special_index].height;
6369 last_font_height += font_ascent();
6371 } else if (specials[special_index].type == OFFSET) {
6372 if (specials[special_index].arg > 0) {
6373 w += specials[special_index].arg;
6375 } else if (specials[special_index].type == VOFFSET) {
6376 last_font_height += specials[special_index].arg;
6377 } else if (specials[special_index].type == GOTO) {
6378 if (specials[special_index].arg > cur_x) {
6379 w = (int) specials[special_index].arg;
6381 } else if (specials[special_index].type == TAB) {
6382 int start = specials[special_index].arg;
6383 int step = specials[special_index].width;
6385 if (!step || step < 0) {
6388 w += step - (cur_x - text_start_x - start) % step;
6389 } else if (specials[special_index].type == FONT) {
6390 selected_font = specials[special_index].font_added;
6391 if (font_height() > last_font_height) {
6392 last_font_height = font_height();
6401 w += get_string_width(s);
6402 if (w > text_width) {
6405 if (text_width > maximum_width && maximum_width) {
6406 text_width = maximum_width;
6409 text_height += last_font_height;
6410 last_font_height = font_height();
6414 static inline void set_foreground_color(long c)
6417 XSetForeground(display, window.gc, c);
6421 static void draw_string(const char *s)
6426 int i, i2, pos, width_of_s;
6430 width_of_s = get_string_width(s);
6431 if (out_to_console) {
6433 fflush(stdout); /* output immediately, don't buffer */
6435 memset(tmpstring1, 0, text_buffer_size);
6436 memset(tmpstring2, 0, text_buffer_size);
6437 strncpy(tmpstring1, s, text_buffer_size - 1);
6442 snprintf(space, 2, " ");
6444 max = ((text_width - width_of_s) / get_string_width(space));
6446 /* This code looks for tabs in the text and coverts them to spaces.
6447 * The trick is getting the correct number of spaces, and not going
6448 * over the window's size without forcing the window larger. */
6449 for (i = 0; i < (int)text_buffer_size; i++) {
6450 if (tmpstring1[i] == '\t') { // 9 is ascii tab
6452 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6453 /* guard against overrun */
6454 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6459 if (tmpstring1[i] != 9) {
6460 /* guard against overrun */
6461 tmpstring2[MIN(pos, (int)text_buffer_size - 1)] = tmpstring1[i];
6467 if (text_width == maximum_width) {
6468 /* this means the text is probably pushing the limit,
6469 * so we'll chop it */
6470 while (cur_x + get_string_width(tmpstring2) - text_start_x
6471 > maximum_width && strlen(tmpstring2) > 0) {
6472 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6483 c.pixel = current_color;
6484 XQueryColor(display, DefaultColormap(display, screen), &c);
6487 c2.color.red = c.red;
6488 c2.color.green = c.green;
6489 c2.color.blue = c.blue;
6490 c2.color.alpha = fonts[selected_font].font_alpha;
6492 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6493 cur_x, cur_y, (XftChar8 *) s, strlen(s));
6495 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6496 cur_x, cur_y, (XftChar8 *) s, strlen(s));
6501 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6504 cur_x += width_of_s;
6506 memcpy(tmpstring1, s, text_buffer_size);
6509 long redmask, greenmask, bluemask;
6511 void set_up_gradient()
6514 colour_depth = DisplayPlanes(display, screen);
6518 if (colour_depth != 24 && colour_depth != 16) {
6519 ERR("using non-standard colour depth, gradients may look like a "
6527 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6529 greenmask |= 1 << i;
6532 if (colour_depth % 3 == 1) {
6533 greenmask |= 1 << (colour_depth / 3);
6535 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6536 greenmask = greenmask << (colour_depth / 3);
6539 /* this function returns the next colour between two colours for a gradient */
6540 inline unsigned long do_gradient(unsigned long first_colour,
6541 unsigned long last_colour)
6544 int red1, green1, blue1; // first colour
6545 int red2, green2, blue2; // second colour
6546 int red3 = 0, green3 = 0, blue3 = 0; // difference
6547 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6548 short greenshift = (colour_depth / 3);
6550 red1 = (first_colour & redmask) >> redshift;
6551 green1 = (first_colour & greenmask) >> greenshift;
6552 blue1 = first_colour & bluemask;
6553 red2 = (last_colour & redmask) >> redshift;
6554 green2 = (last_colour & greenmask) >> greenshift;
6555 blue2 = last_colour & bluemask;
6562 if (green1 > green2) {
6565 if (green1 < green2) {
6568 if (blue1 > blue2) {
6571 if (blue1 < blue2) {
6586 if (red1 > bluemask) {
6589 if (green1 > bluemask) {
6592 if (blue1 > bluemask) {
6595 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
6599 /* this function returns the max diff for a gradient */
6600 inline unsigned long gradient_max(unsigned long first_colour,
6601 unsigned long last_colour)
6603 if (colour_depth == 0) {
6606 int red1, green1, blue1; // first colour
6607 int red2, green2, blue2; // second colour
6608 long redshift = (2 * colour_depth / 3 + colour_depth % 3);
6609 long greenshift = (colour_depth / 3);
6610 int red3 = 0, green3 = 0, blue3 = 0; // difference
6612 red1 = (first_colour & redmask) >> redshift;
6613 green1 = (first_colour & greenmask) >> greenshift;
6614 blue1 = first_colour & bluemask;
6615 red2 = (last_colour & redmask) >> redshift;
6616 green2 = (last_colour & greenmask) >> greenshift;
6617 blue2 = last_colour & bluemask;
6618 red3 = abs(red1 - red2);
6619 green3 = abs(green1 - green2);
6620 blue3 = abs(blue1 - blue2);
6632 static void draw_line(char *s)
6637 cur_x = text_start_x;
6638 cur_y += font_ascent();
6640 short font_h = font_height();
6642 /* find specials and draw stuff */
6645 if (*p == SPECIAL_CHAR) {
6648 /* draw string before special */
6655 switch (specials[special_index].type) {
6656 case HORIZONTAL_LINE:
6658 int h = specials[special_index].height;
6659 int mid = font_ascent() / 2;
6661 w = text_start_x + text_width - cur_x;
6663 XSetLineAttributes(display, window.gc, h, LineSolid,
6664 CapButt, JoinMiter);
6665 XDrawLine(display, window.drawable, window.gc, cur_x,
6666 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6672 int h = specials[special_index].height;
6673 int s = specials[special_index].arg;
6674 int mid = font_ascent() / 2;
6675 char ss[2] = { s, s };
6677 w = text_start_x + text_width - cur_x - 1;
6678 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6679 CapButt, JoinMiter);
6680 XSetDashes(display, window.gc, 0, ss, 2);
6681 XDrawLine(display, window.drawable, window.gc, cur_x,
6682 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6688 if (cur_x - text_start_x > maximum_width
6689 && maximum_width > 0) {
6692 int h = specials[special_index].height;
6693 int bar_usage = specials[special_index].arg;
6694 int by = cur_y - (font_ascent() / 2) - 1;
6696 if (h < font_height()) {
6699 w = specials[special_index].width;
6701 w = text_start_x + text_width - cur_x - 1;
6707 XSetLineAttributes(display, window.gc, 1, LineSolid,
6708 CapButt, JoinMiter);
6710 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6712 XFillRectangle(display, window.drawable, window.gc, cur_x,
6713 by, w * bar_usage / 255, h);
6714 if (specials[special_index].height > cur_y_add
6715 && specials[special_index].height > font_h) {
6716 cur_y_add = specials[special_index].height;
6723 if (cur_x - text_start_x > maximum_width
6724 && maximum_width > 0) {
6727 int h = specials[special_index].height;
6728 unsigned long last_colour = current_color;
6729 int by = cur_y - (font_ascent() / 2) - 1;
6731 if (h < font_height()) {
6734 w = specials[special_index].width;
6736 w = text_start_x + text_width - cur_x - 1;
6741 if (draw_graph_borders) {
6742 XSetLineAttributes(display, window.gc, 1, LineSolid,
6743 CapButt, JoinMiter);
6744 XDrawRectangle(display, window.drawable, window.gc,
6747 XSetLineAttributes(display, window.gc, 1, LineSolid,
6748 CapButt, JoinMiter);
6751 int gradient_size = 0;
6752 float gradient_factor = 0;
6753 float gradient_update = 0;
6754 unsigned long tmpcolour = current_color;
6756 if (specials[special_index].last_colour != 0
6757 || specials[special_index].first_colour != 0) {
6758 tmpcolour = specials[special_index].last_colour;
6760 gradient_max(specials[special_index].last_colour,
6761 specials[special_index].first_colour);
6762 gradient_factor = (float) gradient_size / (w - 2);
6764 for (i = w - 2; i > -1; i--) {
6765 if (specials[special_index].last_colour != 0
6766 || specials[special_index].first_colour != 0) {
6767 XSetForeground(display, window.gc, tmpcolour);
6768 gradient_update += gradient_factor;
6769 while (gradient_update > 0) {
6770 tmpcolour = do_gradient(tmpcolour,
6771 specials[special_index].first_colour);
6775 if ((w - i) / ((float) (w - 2) /
6776 (specials[special_index].graph_width)) > j
6777 && j < MAX_GRAPH_DEPTH - 3) {
6780 /* this is mugfugly, but it works */
6781 XDrawLine(display, window.drawable, window.gc,
6782 cur_x + i + 1, by + h, cur_x + i + 1,
6783 by + h - specials[special_index].graph[j] *
6784 (h - 1) / specials[special_index].graph_scale);
6786 if (specials[special_index].height > cur_y_add
6787 && specials[special_index].height > font_h) {
6788 cur_y_add = specials[special_index].height;
6790 /* if (draw_mode == BG) {
6791 set_foreground_color(default_bg_color);
6792 } else if (draw_mode == OUTLINE) {
6793 set_foreground_color(default_out_color);
6795 set_foreground_color(default_fg_color);
6797 set_foreground_color(last_colour);
6803 int old = font_ascent();
6805 cur_y -= font_ascent();
6806 selected_font = specials[special_index].font_added;
6807 if (cur_y + font_ascent() < cur_y + old) {
6810 cur_y += font_ascent();
6816 if (draw_mode == FG) {
6817 set_foreground_color(specials[special_index].arg);
6822 if (draw_mode == BG) {
6823 set_foreground_color(specials[special_index].arg);
6828 if (draw_mode == OUTLINE) {
6829 set_foreground_color(specials[special_index].arg);
6834 w += specials[special_index].arg;
6838 cur_y += specials[special_index].arg;
6842 if (specials[special_index].arg >= 0) {
6843 cur_x = (int) specials[special_index].arg;
6849 int start = specials[special_index].arg;
6850 int step = specials[special_index].width;
6852 if (!step || step < 0) {
6855 w = step - (cur_x - text_start_x - start) % step;
6861 /* TODO: add back in "+ border_margin" to the end of
6863 int pos_x = text_start_x + text_width -
6864 get_string_width_special(s);
6866 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6867 "get_string_width(p) %i gap_x %i "
6868 "specials[special_index].arg %i border_margin %i "
6869 "border_width %i\n", pos_x, text_start_x, text_width,
6870 cur_x, get_string_width_special(s), gap_x,
6871 specials[special_index].arg, border_margin,
6873 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6874 cur_x = pos_x - specials[special_index].arg;
6881 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6882 2 - (cur_x - text_start_x);
6883 /* int pos_x = text_start_x + text_width / 2 -
6884 get_string_width_special(s) / 2; */
6886 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6887 "get_string_width(p) %i gap_x %i "
6888 "specials[special_index].arg %i\n", pos_x, text_start_x,
6889 text_width, cur_x, get_string_width(s), gap_x,
6890 specials[special_index].arg); */
6891 if (pos_x > specials[special_index].arg) {
6892 w = pos_x - specials[special_index].arg;
6909 if (cur_y_add > 0) {
6911 cur_y -= font_descent();
6916 cur_y += font_descent();
6920 static void draw_text()
6923 cur_y = text_start_y;
6926 if (draw_borders && border_width > 0) {
6927 unsigned int b = (border_width + 1) / 2;
6929 if (stippled_borders) {
6930 char ss[2] = { stippled_borders, stippled_borders };
6931 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6932 CapButt, JoinMiter);
6933 XSetDashes(display, window.gc, 0, ss, 2);
6935 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6936 CapButt, JoinMiter);
6939 XDrawRectangle(display, window.drawable, window.gc,
6940 text_start_x - border_margin + b, text_start_y - border_margin + b,
6941 text_width + border_margin * 2 - 1 - b * 2,
6942 text_height + border_margin * 2 - 1 - b * 2);
6948 for_each_line(text_buffer, draw_line);
6951 static void draw_stuff()
6955 if (draw_shades && !draw_outline) {
6958 set_foreground_color(default_bg_color);
6969 for (i = -1; i < 2; i++) {
6970 for (j = -1; j < 2; j++) {
6971 if (i == 0 && j == 0) {
6976 set_foreground_color(default_out_color);
6977 draw_mode = OUTLINE;
6985 set_foreground_color(default_fg_color);
6994 swap.swap_window = window.window;
6995 swap.swap_action = XdbeBackground;
6996 XdbeSwapBuffers(display, &swap, 1);
7003 static void clear_text(int exposures)
7007 /* The swap action is XdbeBackground, which clears */
7012 /* there is some extra space for borders and outlines */
7013 XClearArea(display, window.window, text_start_x - border_margin - 1,
7014 text_start_y - border_margin - 1,
7015 text_width + border_margin * 2 + 2,
7016 text_height + border_margin * 2 + 2, exposures ? True : 0);
7021 static int need_to_update;
7023 /* update_text() generates new text and clears old text area */
7024 static void update_text()
7033 static void main_loop()
7035 #ifdef SIGNAL_BLOCKING
7036 sigset_t newmask, oldmask;
7038 sigemptyset(&newmask);
7039 sigaddset(&newmask, SIGINT);
7040 sigaddset(&newmask, SIGTERM);
7041 sigaddset(&newmask, SIGUSR1);
7045 Region region = XCreateRegion();
7048 int event_base, error_base;
7050 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
7051 ERR("Xdamage extension unavailable");
7053 Damage damage = XDamageCreate(display, window.window,
7054 XDamageReportNonEmpty);
7055 XserverRegion region2 = XFixesCreateRegionFromWindow(display,
7057 XserverRegion part = XFixesCreateRegionFromWindow(display,
7059 #endif /* HAVE_XDAMAGE */
7063 while (total_run_times == 0 || info.looped < total_run_times) {
7066 #ifdef SIGNAL_BLOCKING
7067 /* block signals. we will inspect for pending signals later */
7068 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7069 CRIT_ERR("unable to sigprocmask()");
7076 /* wait for X event or timeout */
7078 if (!XPending(display)) {
7082 double t = update_interval - (get_time() - last_update_time);
7088 tv.tv_sec = (long) t;
7089 tv.tv_usec = (long) (t * 1000000) % 1000000;
7091 FD_SET(ConnectionNumber(display), &fdsr);
7093 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7095 if (errno != EINTR) {
7096 ERR("can't select(): %s", strerror(errno));
7102 /* FIXME just sleep for the interval time if no X11 */
7103 usleep(update_interval * 1000000);
7111 if (need_to_update) {
7113 int wx = window.x, wy = window.y;
7121 /* resize window if it isn't right size */
7123 && (text_width + border_margin * 2 + 1 != window.width
7124 || text_height + border_margin * 2 + 1 != window.height)) {
7125 window.width = text_width + border_margin * 2 + 1;
7126 window.height = text_height + border_margin * 2 + 1;
7127 XResizeWindow(display, window.window, window.width,
7130 set_transparent_background(window.window);
7134 /* move window if it isn't in right position */
7135 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7136 XMoveWindow(display, window.window, window.x, window.y);
7147 r.x = text_start_x - border_margin;
7148 r.y = text_start_y - border_margin;
7149 r.width = text_width + border_margin * 2;
7150 r.height = text_height + border_margin * 2;
7151 XUnionRectWithRegion(&r, region, region);
7156 /* handle X events */
7157 while (XPending(display)) {
7160 XNextEvent(display, &ev);
7168 r.width = ev.xexpose.width;
7169 r.height = ev.xexpose.height;
7170 XUnionRectWithRegion(&r, region, region);
7175 case ReparentNotify:
7176 /* set background to ParentRelative for all parents */
7178 set_transparent_background(window.window);
7182 case ConfigureNotify:
7184 /* if window size isn't what expected, set fixed size */
7185 if (ev.xconfigure.width != window.width
7186 || ev.xconfigure.height != window.height) {
7187 if (window.width != 0 && window.height != 0) {
7191 /* clear old stuff before screwing up
7196 XWindowAttributes attrs;
7198 if (XGetWindowAttributes(display,
7199 window.window, &attrs)) {
7200 window.width = attrs.width;
7201 window.height = attrs.height;
7205 text_width = window.width - border_margin * 2 - 1;
7206 text_height = window.height - border_margin * 2 - 1;
7207 if (text_width > maximum_width
7208 && maximum_width > 0) {
7209 text_width = maximum_width;
7213 /* if position isn't what expected, set fixed pos
7214 * total_updates avoids setting fixed_pos when window
7215 * is set to weird locations when started */
7216 /* // this is broken
7217 if (total_updates >= 2 && !fixed_pos
7218 && (window.x != ev.xconfigure.x
7219 || window.y != ev.xconfigure.y)
7220 && (ev.xconfigure.x != 0
7221 || ev.xconfigure.y != 0)) {
7230 /* if an ordinary window with decorations */
7231 if ((window.type == TYPE_NORMAL)
7232 && (!TEST_HINT(window.hints,
7233 HINT_UNDECORATED))) {
7234 /* allow conky to hold input focus. */
7237 /* forward the click to the desktop window */
7238 XUngrabPointer(display, ev.xbutton.time);
7239 ev.xbutton.window = window.desktop;
7240 XSendEvent(display, ev.xbutton.window, False,
7241 ButtonPressMask, &ev);
7242 XSetInputFocus(display, ev.xbutton.window,
7243 RevertToParent, ev.xbutton.time);
7250 /* if an ordinary window with decorations */
7251 if ((window.type == TYPE_NORMAL)
7252 && (!TEST_HINT(window.hints,
7253 HINT_UNDECORATED))) {
7254 /* allow conky to hold input focus. */
7257 /* forward the release to the desktop window */
7258 ev.xbutton.window = window.desktop;
7259 XSendEvent(display, ev.xbutton.window, False,
7260 ButtonReleaseMask, &ev);
7269 if (ev.type == event_base + XDamageNotify) {
7270 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7272 XFixesSetRegion(display, part, &dev->area, 1);
7273 XFixesUnionRegion(display, region2, region2, part);
7275 #endif /* HAVE_XDAMAGE */
7281 XDamageSubtract(display, damage, region2, None);
7282 XFixesSetRegion(display, region2, 0, 0);
7283 #endif /* HAVE_XDAMAGE */
7285 /* XDBE doesn't seem to provide a way to clear the back buffer without
7286 * interfering with the front buffer, other than passing XdbeBackground
7287 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7288 * redraw the text even if it wasn't part of the exposed area. OTOH,
7289 * if we're not going to call draw_stuff at all, then no swap happens
7290 * and we can safely do nothing. */
7292 if (!XEmptyRegion(region)) {
7297 r.x = text_start_x - border_margin;
7298 r.y = text_start_y - border_margin;
7299 r.width = text_width + border_margin * 2;
7300 r.height = text_height + border_margin * 2;
7301 XUnionRectWithRegion(&r, region, region);
7304 XSetRegion(display, window.gc, region);
7307 XftDrawSetClip(window.xftdraw, region);
7313 XDestroyRegion(region);
7314 region = XCreateRegion();
7318 #ifdef SIGNAL_BLOCKING
7319 /* unblock signals of interest and let handler fly */
7320 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7321 CRIT_ERR("unable to sigprocmask()");
7325 switch (g_signal_pending) {
7328 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7333 ERR("received SIGINT or SIGTERM to terminate. bye!");
7336 XDestroyRegion(region);
7339 XDamageDestroy(display, damage);
7340 XFixesDestroyRegion(display, region2);
7341 XFixesDestroyRegion(display, part);
7342 #endif /* HAVE_XDAMAGE */
7344 return; /* return from main_loop */
7347 /* Reaching here means someone set a signal
7348 * (SIGXXXX, signal_handler), but didn't write any code
7350 * If you don't want to handle a signal, don't set a handler on
7351 * it in the first place. */
7352 if (g_signal_pending) {
7353 ERR("ignoring signal (%d)", g_signal_pending);
7357 g_signal_pending = 0;
7360 #if defined(X11) && defined(HAVE_XDAMAGE)
7361 XDamageDestroy(display, damage);
7362 XFixesDestroyRegion(display, region2);
7363 XFixesDestroyRegion(display, part);
7364 XDestroyRegion(region);
7366 #endif /* X11 && HAVE_XDAMAGE */
7369 static void load_config_file(const char *);
7371 /* reload the config file */
7372 void reload_config(void)
7374 timed_thread_destroy_registered_threads();
7376 if (info.cpu_usage) {
7377 free(info.cpu_usage);
7378 info.cpu_usage = NULL;
7386 if (info.mpd.title) {
7387 free(info.mpd.title);
7388 info.mpd.title = NULL;
7390 if (info.mpd.artist) {
7391 free(info.mpd.artist);
7392 info.mpd.artist = NULL;
7394 if (info.mpd.album) {
7395 free(info.mpd.album);
7396 info.mpd.album = NULL;
7398 if (info.mpd.random) {
7399 free(info.mpd.random);
7400 info.mpd.random = NULL;
7402 if (info.mpd.repeat) {
7403 free(info.mpd.repeat);
7404 info.mpd.repeat = NULL;
7406 if (info.mpd.track) {
7407 free(info.mpd.track);
7408 info.mpd.track = NULL;
7410 if (info.mpd.name) {
7411 free(info.mpd.name);
7412 info.mpd.name = NULL;
7414 if (info.mpd.file) {
7415 free(info.mpd.file);
7416 info.mpd.file = NULL;
7418 if (info.mpd.status) {
7419 free(info.mpd.status);
7420 info.mpd.status = NULL;
7430 #ifdef TCP_PORT_MONITOR
7431 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7434 if (current_config) {
7436 load_config_file(current_config);
7438 /* re-init specials array */
7439 if ((specials = realloc((void *) specials,
7440 sizeof(struct special_t) * max_specials)) == 0) {
7441 ERR("failed to realloc specials array");
7445 // clear the window first
7446 XClearWindow(display, RootWindow(display, screen));
7449 #ifdef TCP_PORT_MONITOR
7450 info.p_tcp_port_monitor_collection = NULL;
7452 extract_variable_text(text);
7458 tmpstring1 = malloc(text_buffer_size);
7459 memset(tmpstring1, 0, text_buffer_size);
7463 tmpstring2 = malloc(text_buffer_size);
7464 memset(tmpstring2, 0, text_buffer_size);
7468 text_buffer = malloc(max_user_text);
7469 memset(text_buffer, 0, max_user_text);
7476 timed_thread_destroy_registered_threads();
7478 if (info.cpu_usage) {
7479 free(info.cpu_usage);
7480 info.cpu_usage = NULL;
7485 XdbeDeallocateBackBufferName(display, window.back_buffer);
7490 XDestroyWindow(display, window.window);
7491 XClearWindow(display, RootWindow(display, screen));
7496 XClearWindow(display, RootWindow(display, screen));
7501 XFreeGC(display, window.gc);
7505 free_text_objects(text_object_count, text_objects);
7518 text_object_count = 0;
7519 text_objects = NULL;
7526 free(current_config);
7528 #ifdef TCP_PORT_MONITOR
7529 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7530 info.p_tcp_port_monitor_collection = NULL;
7539 for (i = 0; i < special_count; i++) {
7540 if (specials[i].type == GRAPH) {
7541 free(specials[i].graph);
7548 clear_diskio_stats();
7551 static int string_to_bool(const char *s)
7555 } else if (strcasecmp(s, "yes") == 0) {
7557 } else if (strcasecmp(s, "true") == 0) {
7559 } else if (strcasecmp(s, "1") == 0) {
7566 static enum alignment string_to_alignment(const char *s)
7568 if (strcasecmp(s, "top_left") == 0) {
7570 } else if (strcasecmp(s, "top_right") == 0) {
7572 } else if (strcasecmp(s, "top_middle") == 0) {
7574 } else if (strcasecmp(s, "bottom_left") == 0) {
7576 } else if (strcasecmp(s, "bottom_right") == 0) {
7577 return BOTTOM_RIGHT;
7578 } else if (strcasecmp(s, "bottom_middle") == 0) {
7579 return BOTTOM_MIDDLE;
7580 } else if (strcasecmp(s, "middle_left") == 0) {
7582 } else if (strcasecmp(s, "middle_right") == 0) {
7583 return MIDDLE_RIGHT;
7584 } else if (strcasecmp(s, "tl") == 0) {
7586 } else if (strcasecmp(s, "tr") == 0) {
7588 } else if (strcasecmp(s, "tm") == 0) {
7590 } else if (strcasecmp(s, "bl") == 0) {
7592 } else if (strcasecmp(s, "br") == 0) {
7593 return BOTTOM_RIGHT;
7594 } else if (strcasecmp(s, "bm") == 0) {
7595 return BOTTOM_MIDDLE;
7596 } else if (strcasecmp(s, "ml") == 0) {
7598 } else if (strcasecmp(s, "mr") == 0) {
7599 return MIDDLE_RIGHT;
7600 } else if (strcasecmp(s, "none") == 0) {
7607 static void set_default_configurations(void)
7609 fork_to_background = 0;
7610 total_run_times = 0;
7611 info.cpu_avg_samples = 2;
7612 info.net_avg_samples = 2;
7619 strcpy(info.mpd.host, "localhost");
7620 info.mpd.port = 6600;
7621 info.mpd.status = NULL;
7622 info.mpd.artist = NULL;
7623 info.mpd.album = NULL;
7624 info.mpd.title = NULL;
7625 info.mpd.random = NULL;
7626 info.mpd.track = NULL;
7627 info.mpd.name = NULL;
7628 info.mpd.file = NULL;
7631 info.xmms2.artist = NULL;
7632 info.xmms2.album = NULL;
7633 info.xmms2.title = NULL;
7634 info.xmms2.genre = NULL;
7635 info.xmms2.comment = NULL;
7636 info.xmms2.url = NULL;
7637 info.xmms2.status = NULL;
7638 info.xmms2.playlist = NULL;
7640 use_spacer = NO_SPACER;
7647 default_fg_color = WhitePixel(display, screen);
7648 default_bg_color = BlackPixel(display, screen);
7649 default_out_color = BlackPixel(display, screen);
7650 color0 = default_fg_color;
7651 color1 = default_fg_color;
7652 color2 = default_fg_color;
7653 color3 = default_fg_color;
7654 color4 = default_fg_color;
7655 color5 = default_fg_color;
7656 color6 = default_fg_color;
7657 color7 = default_fg_color;
7658 color8 = default_fg_color;
7659 color9 = default_fg_color;
7662 draw_graph_borders = 1;
7664 set_first_font("6x10");
7672 window.type = TYPE_NORMAL;
7674 strcpy(window.class_name, "Conky");
7676 sprintf(window.title, "Conky (%s)", info.uname_s.nodename);
7678 stippled_borders = 0;
7681 text_alignment = BOTTOM_LEFT;
7684 free(current_mail_spool);
7688 variable_substitute(MAIL_FILE, buf, 256);
7689 if (buf[0] != '\0') {
7690 current_mail_spool = strdup(buf);
7695 update_interval = 3.0;
7696 info.music_player_interval = 1.0;
7697 stuff_in_upper_case = 0;
7698 info.users.number = 1;
7700 #ifdef TCP_PORT_MONITOR
7701 tcp_port_monitor_args.max_port_monitor_connections =
7702 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
7706 static void load_config_file(const char *f)
7708 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7712 set_default_configurations();
7719 char buf[256], *p, *p2, *name, *value;
7722 if (fgets(buf, 256, fp) == NULL) {
7728 /* break at comment */
7729 p2 = strchr(p, '#');
7735 while (*p && isspace((int) *p)) {
7739 continue; /* empty line */
7746 while (*p2 && !isspace((int) *p2)) {
7750 *p2 = '\0'; /* break at name's end */
7757 while (*p && isspace((int) *p)) {
7763 p2 = value + strlen(value);
7764 while (isspace((int) *(p2 - 1))) {
7771 #define CONF2(a) if (strcasecmp(name, a) == 0)
7772 #define CONF(a) else CONF2(a)
7773 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7774 || strcasecmp(name, b) == 0)
7777 CONF2("alignment") {
7779 int a = string_to_alignment(value);
7790 CONF("background") {
7791 fork_to_background = string_to_bool(value);
7794 CONF2("background") {
7795 fork_to_background = string_to_bool(value);
7799 CONF("border_margin") {
7801 border_margin = strtol(value, 0, 0);
7806 CONF("border_width") {
7808 border_width = strtol(value, 0, 0);
7815 color0 = get_x11_color(value);
7822 color1 = get_x11_color(value);
7829 color2 = get_x11_color(value);
7836 color3 = get_x11_color(value);
7843 color4 = get_x11_color(value);
7850 color5 = get_x11_color(value);
7857 color6 = get_x11_color(value);
7864 color7 = get_x11_color(value);
7871 color8 = get_x11_color(value);
7878 color9 = get_x11_color(value);
7883 CONF("default_color") {
7885 default_fg_color = get_x11_color(value);
7890 CONF3("default_shade_color", "default_shadecolor") {
7892 default_bg_color = get_x11_color(value);
7897 CONF3("default_outline_color", "default_outlinecolor") {
7899 default_out_color = get_x11_color(value);
7907 info.mail = parse_mail_args(IMAP, value);
7914 info.mail = parse_mail_args(POP3, value);
7922 strncpy(info.mpd.host, value, 127);
7929 info.mpd.port = strtol(value, 0, 0);
7930 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
7935 CONF("mpd_password") {
7937 strncpy(info.mpd.password, value, 127);
7943 CONF("music_player_interval") {
7945 info.music_player_interval = strtod(value, 0);
7951 CONF("sensor_device") {
7953 sensor_device = strtol(value, 0, 0);
7959 CONF("cpu_avg_samples") {
7961 cpu_avg_samples = strtol(value, 0, 0);
7962 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
7965 info.cpu_avg_samples = cpu_avg_samples;
7971 CONF("net_avg_samples") {
7973 net_avg_samples = strtol(value, 0, 0);
7974 if (net_avg_samples < 1 || net_avg_samples > 14) {
7977 info.net_avg_samples = net_avg_samples;
7985 CONF("double_buffer") {
7986 use_xdbe = string_to_bool(value);
7990 CONF("override_utf8_locale") {
7991 utf8_mode = string_to_bool(value);
7993 CONF("draw_borders") {
7994 draw_borders = string_to_bool(value);
7996 CONF("draw_graph_borders") {
7997 draw_graph_borders = string_to_bool(value);
7999 CONF("draw_shades") {
8000 draw_shades = string_to_bool(value);
8002 CONF("draw_outline") {
8003 draw_outline = string_to_bool(value);
8006 CONF("out_to_console") {
8007 out_to_console = string_to_bool(value);
8009 CONF("use_spacer") {
8011 if (strcasecmp(value, "left") == 0) {
8012 use_spacer = LEFT_SPACER;
8013 } else if (strcasecmp(value, "right") == 0) {
8014 use_spacer = RIGHT_SPACER;
8015 } else if (strcasecmp(value, "none") == 0) {
8016 use_spacer = NO_SPACER;
8018 use_spacer = string_to_bool(value);
8019 ERR("use_spacer should have an argument of left, right, or"
8020 " none. '%s' seems to be some form of '%s', so"
8021 " defaulting to %s.", value,
8022 use_spacer ? "true" : "false",
8023 use_spacer ? "right" : "none");
8025 use_spacer = RIGHT_SPACER;
8027 use_spacer = NO_SPACER;
8031 ERR("use_spacer should have an argument. Defaulting to right.");
8032 use_spacer = RIGHT_SPACER;
8038 use_xft = string_to_bool(value);
8042 set_first_font(value);
8048 if (value && font_count >= 0) {
8049 fonts[0].font_alpha = atof(value) * 65535.0;
8058 if (string_to_bool(value)) {
8059 ERR("Xft not enabled");
8063 /* xftfont silently ignored when no Xft */
8066 /* xftalpha is silently ignored when no Xft */
8071 set_first_font(value);
8081 gap_x = atoi(value);
8088 gap_y = atoi(value);
8094 CONF("mail_spool") {
8098 variable_substitute(value, buf, 256);
8100 if (buf[0] != '\0') {
8101 if (current_mail_spool) {
8102 free(current_mail_spool);
8104 current_mail_spool = strdup(buf);
8111 CONF("minimum_size") {
8113 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8115 if (sscanf(value, "%d", &minimum_width) != 1) {
8123 CONF("maximum_width") {
8125 if (sscanf(value, "%d", &maximum_width) != 1) {
8133 CONF("no_buffers") {
8134 no_buffers = string_to_bool(value);
8136 CONF("top_cpu_separate") {
8137 cpu_separate = string_to_bool(value);
8139 CONF("short_units") {
8140 short_units = string_to_bool(value);
8142 CONF("pad_percents") {
8143 pad_percents = atoi(value);
8147 CONF("own_window") {
8149 own_window = string_to_bool(value);
8154 CONF("own_window_class") {
8156 memset(window.class_name, 0, sizeof(window.class_name));
8157 strncpy(window.class_name, value,
8158 sizeof(window.class_name) - 1);
8163 CONF("own_window_title") {
8165 memset(window.title, 0, sizeof(window.title));
8166 strncpy(window.title, value, sizeof(window.title) - 1);
8171 CONF("own_window_transparent") {
8173 set_transparent = string_to_bool(value);
8178 CONF("own_window_colour") {
8180 background_colour = get_x11_color(value);
8182 ERR("Invalid colour for own_window_colour (try omitting the "
8183 "'#' for hex colours");
8186 CONF("own_window_hints") {
8188 char *p_hint, *p_save;
8189 char delim[] = ", ";
8191 /* tokenize the value into individual hints */
8192 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8194 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8195 if (strncmp(p_hint, "undecorate", 10) == 0) {
8196 SET_HINT(window.hints, HINT_UNDECORATED);
8197 } else if (strncmp(p_hint, "below", 5) == 0) {
8198 SET_HINT(window.hints, HINT_BELOW);
8199 } else if (strncmp(p_hint, "above", 5) == 0) {
8200 SET_HINT(window.hints, HINT_ABOVE);
8201 } else if (strncmp(p_hint, "sticky", 6) == 0) {
8202 SET_HINT(window.hints, HINT_STICKY);
8203 } else if (strncmp(p_hint, "skip_taskbar", 12) == 0) {
8204 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8205 } else if (strncmp(p_hint, "skip_pager", 10) == 0) {
8206 SET_HINT(window.hints, HINT_SKIP_PAGER);
8211 p_hint = strtok_r(NULL, delim, &p_save);
8212 } while (p_hint != NULL);
8218 CONF("own_window_type") {
8220 if (strncmp(value, "normal", 6) == 0) {
8221 window.type = TYPE_NORMAL;
8222 } else if (strncmp(value, "desktop", 7) == 0) {
8223 window.type = TYPE_DESKTOP;
8224 } else if (strncmp(value, "override", 8) == 0) {
8225 window.type = TYPE_OVERRIDE;
8234 CONF("stippled_borders") {
8236 stippled_borders = strtol(value, 0, 0);
8238 stippled_borders = 4;
8243 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
8247 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
8250 CONF("update_interval") {
8252 update_interval = strtod(value, 0);
8256 if (info.music_player_interval == 0) {
8257 // default to update_interval
8258 info.music_player_interval = update_interval;
8261 CONF("total_run_times") {
8263 total_run_times = strtod(value, 0);
8269 stuff_in_upper_case = string_to_bool(value);
8271 CONF("max_specials") {
8273 max_specials = atoi(value);
8278 CONF("max_user_text") {
8280 max_user_text = atoi(value);
8285 CONF("text_buffer_size") {
8287 text_buffer_size = atoi(value);
8288 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8289 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8290 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8302 text = (char *) malloc(1);
8306 unsigned int l = strlen(text);
8308 if (fgets(buf, 256, fp) == NULL) {
8311 text = (char *) realloc(text, l + strlen(buf) + 1);
8314 if (strlen(text) > max_user_text) {
8319 text_lines = line + 1;
8322 #ifdef TCP_PORT_MONITOR
8323 CONF("max_port_monitor_connections") {
8324 if (!value || (sscanf(value, "%d",
8325 &tcp_port_monitor_args.max_port_monitor_connections) != 1)
8326 || tcp_port_monitor_args.max_port_monitor_connections < 0) {
8327 /* an error. use default, warn and continue. */
8328 tcp_port_monitor_args.max_port_monitor_connections =
8329 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8331 } else if (tcp_port_monitor_args.max_port_monitor_connections
8333 /* no error, just use default */
8334 tcp_port_monitor_args.max_port_monitor_connections =
8335 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8337 /* else tcp_port_monitor_args.max_port_monitor_connections > 0
8342 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8353 if (info.music_player_interval == 0) {
8354 // default to update_interval
8355 info.music_player_interval = update_interval;
8359 /* : means that character before that takes an argument */
8360 static const char *getopt_string = "vVdt:u:i:hc:"
8372 static const struct option longopts[] = {
8373 { "help", 0, NULL, 'h' },
8374 { "version", 0, NULL, 'V' },
8375 { "config", 1, NULL, 'c' },
8376 { "daemonize", 0, NULL, 'd' },
8378 { "alignment", 1, NULL, 'a' },
8379 { "font", 1, NULL, 'f' },
8381 { "own-window", 0, NULL, 'o' },
8384 { "double-buffer", 0, NULL, 'b' },
8386 { "window-id", 1, NULL, 'w' },
8388 { "text", 1, NULL, 't' },
8389 { "interval", 0, NULL, 'u' },
8393 int main(int argc, char **argv)
8395 struct sigaction act, oact;
8397 g_signal_pending = 0;
8398 memset(&info, 0, sizeof(info));
8400 #ifdef TCP_PORT_MONITOR
8401 tcp_port_monitor_args.max_port_monitor_connections =
8402 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8405 /* handle command line parameters that don't change configs */
8410 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8411 || ((s = getenv("LANG")) && *s)) {
8412 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8414 ERR("malloc failed");
8416 for (x = 0; x < strlen(s); x++) {
8417 temp[x] = tolower(s[x]);
8420 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8426 if (!setlocale(LC_CTYPE, "")) {
8427 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8431 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8442 if (current_config) {
8443 free(current_config);
8445 current_config = strdup(optarg);
8449 printf("Usage: %s [OPTION]...\n"
8450 "Conky is a system monitor that renders text on desktop or to own transparent\n"
8451 "window. Command line options will override configurations defined in config\n"
8453 " -V, --version version\n"
8454 " -c, --config=FILE config file to load\n"
8455 " -d, --daemonize daemonize, fork to background\n"
8456 " -h, --help help\n"
8458 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
8459 " -f, --font=FONT font to use\n"
8461 " -o, --own-window create own window to draw\n"
8464 " -b, --double-buffer double buffer (prevents flickering)\n"
8466 " -w, --window-id=WIN_ID window id to draw\n"
8467 " -x X x position\n"
8468 " -y Y y position\n"
8470 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8471 " -u, --interval=SECS update interval\n"
8472 " -i NUM number of times to update Conky\n",
8478 window.window = strtol(optarg, 0, 0);
8487 /* initalize X BEFORE we load config.
8488 * (we need to so that 'screen' is set) */
8492 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8494 if (!current_config) {
8495 /* load default config file */
8499 /* Try to use personal config file first */
8500 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
8501 if (buf[0] && (fp = fopen(buf, "r"))) {
8502 current_config = strdup(buf);
8506 /* Try to use system config file if personal config not readable */
8507 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8508 current_config = strdup(SYSTEM_CONFIG_FILE);
8512 /* No readable config found */
8513 if (!current_config) {
8514 CRIT_ERR("no readable personal or system-wide config file found");
8518 load_config_file(current_config);
8520 /* init specials array */
8521 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8522 ERR("failed to create specials array");
8526 if (current_mail_spool == NULL) {
8529 variable_substitute(MAIL_FILE, buf, 256);
8531 if (buf[0] != '\0') {
8532 current_mail_spool = strdup(buf);
8537 /* handle other command line arguments */
8539 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8540 || defined(__NetBSD__)
8541 optind = optreset = 1;
8546 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8547 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8548 "kvm_open")) == NULL) {
8549 CRIT_ERR("cannot read kvm");
8554 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8562 fork_to_background = 1;
8567 set_first_font(optarg);
8570 text_alignment = string_to_alignment(optarg);
8589 text = strdup(optarg);
8590 convert_escapes(text);
8594 update_interval = strtod(optarg, 0);
8595 if (info.music_player_interval == 0) {
8596 // default to update_interval
8597 info.music_player_interval = update_interval;
8602 total_run_times = strtod(optarg, 0);
8606 gap_x = atoi(optarg);
8610 gap_y = atoi(optarg);
8624 /* generate text and get initial size */
8625 extract_variable_text(text);
8632 if (fork_to_background) {
8637 ERR("Conky: couldn't fork() to background: %s",
8644 fprintf(stderr, "\n");
8649 /* parent process */
8650 fprintf(stderr, "Conky: forked to background, pid is %d\n",
8657 text_buffer = malloc(max_user_text);
8658 memset(text_buffer, 0, max_user_text);
8659 tmpstring1 = malloc(text_buffer_size);
8660 memset(tmpstring1, 0, text_buffer_size);
8661 tmpstring2 = malloc(text_buffer_size);
8662 memset(tmpstring2, 0, text_buffer_size);
8666 update_text_area(); /* to get initial size of the window */
8669 init_window(own_window, text_width + border_margin * 2 + 1,
8670 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8672 #else /* OWN_WINDOW */
8673 init_window(0, text_width + border_margin * 2 + 1,
8674 text_height + border_margin * 2 + 1, set_transparent, 0,
8676 #endif /* OWN_WINDOW */
8679 update_text_area(); /* to position text/window on screen */
8684 if (own_window && !fixed_pos) {
8685 XMoveWindow(display, window.window, window.x, window.y);
8688 set_transparent_background(window.window);
8698 /* Set signal handlers */
8699 act.sa_handler = signal_handler;
8700 sigemptyset(&act.sa_mask);
8703 act.sa_flags |= SA_RESTART;
8706 if (sigaction(SIGINT, &act, &oact) < 0
8707 || sigaction(SIGUSR1, &act, &oact) < 0
8708 || sigaction(SIGHUP,&act,&oact) < 0
8709 || sigaction(SIGTERM, &act, &oact) < 0) {
8710 ERR("error setting signal handler: %s", strerror(errno));
8713 /* *************** *
8715 * *************** */
8718 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8725 void signal_handler(int sig)
8727 /* signal handler is light as a feather, as it should be.
8728 * we will poll g_signal_pending with each loop of conky
8729 * and do any signal processing there, NOT here. */
8730 g_signal_pending = sig;