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__ */
1193 OBJ_running_processes,
1202 OBJ_temp1, /* i2c is used instead in these */
1226 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1227 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1229 OBJ_apm_battery_time,
1230 OBJ_apm_battery_life,
1231 #endif /* __FreeBSD__ __OpenBSD__ */
1233 OBJ_obsd_sensors_temp,
1234 OBJ_obsd_sensors_fan,
1235 OBJ_obsd_sensors_volt,
1238 #endif /* __OpenBSD__ */
1260 OBJ_music_player_interval,
1280 OBJ_xmms2_timesplayed,
1283 OBJ_audacious_status,
1284 OBJ_audacious_title,
1285 OBJ_audacious_length,
1286 OBJ_audacious_length_seconds,
1287 OBJ_audacious_position,
1288 OBJ_audacious_position_seconds,
1289 OBJ_audacious_bitrate,
1290 OBJ_audacious_frequency,
1291 OBJ_audacious_channels,
1292 OBJ_audacious_filename,
1293 OBJ_audacious_playlist_length,
1294 OBJ_audacious_playlist_position,
1308 #ifdef TCP_PORT_MONITOR
1322 OBJ_if_smapi_bat_installed,
1325 OBJ_entropy_poolsize,
1329 struct text_object {
1331 char *s; /* some string */
1332 int i; /* some integer */
1333 long l; /* some other integer */
1334 unsigned int sensor;
1335 struct net_stat *net;
1337 struct diskio_stat *diskio;
1338 unsigned char loadavg[3];
1339 unsigned int cpu_index;
1340 struct mail_s *mail;
1348 char *tz; /* timezone variable */
1349 char *fmt; /* time display formatting */
1387 /* If not -1, a file descriptor to read from when
1388 * logfile is a FIFO. */
1405 timed_thread *p_timed_thread;
1411 #ifdef TCP_PORT_MONITOR
1413 /* starting port to monitor */
1414 in_port_t port_range_begin;
1415 /* ending port to monitor */
1416 in_port_t port_range_end;
1417 /* enum from libtcp-portmon.h, e.g. COUNT, etc. */
1419 /* 0 to n-1 connections. */
1420 int connection_index;
1438 struct local_mail_s local_mail;
1443 unsigned int c, d, e;
1448 struct text_object_list {
1449 unsigned int text_object_count;
1450 struct text_object *text_objects;
1453 static unsigned int text_object_count;
1454 static struct text_object *text_objects;
1455 static void generate_text_internal(char *p, int p_max_size,
1456 struct text_object *objs, unsigned int object_count,
1457 struct information *cur);
1459 #define MAXDATASIZE 1000
1463 struct mail_s *parse_mail_args(char type, const char *arg)
1465 struct mail_s *mail;
1466 mail = malloc(sizeof(struct mail_s));
1467 memset(mail, 0, sizeof(struct mail_s));
1470 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
1473 ERR("Scanning IMAP args failed");
1474 } else if (type == IMAP) {
1475 ERR("Scanning POP3 args failed");
1478 // see if password needs prompting
1479 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1480 int fp = fileno(stdin);
1481 struct termios term;
1483 tcgetattr(fp, &term);
1484 term.c_lflag &= ~ECHO;
1485 tcsetattr(fp, TCSANOW, &term);
1486 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1487 scanf("%128s", mail->pass);
1489 term.c_lflag |= ECHO;
1490 tcsetattr(fp, TCSANOW, &term);
1492 // now we check for optional args
1493 tmp = strstr(arg, "-i ");
1496 sscanf(tmp, "%f", &mail->interval);
1498 mail->interval = 300; // 5 minutes
1500 tmp = strstr(arg, "-p ");
1503 sscanf(tmp, "%lu", &mail->port);
1506 mail->port = 110; // default pop3 port
1507 } else if (type == IMAP) {
1508 mail->port = 143; // default imap port
1512 tmp = strstr(arg, "-f ");
1515 sscanf(tmp, "%s", mail->folder);
1517 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1520 tmp = strstr(arg, "-e ");
1525 if (tmp[0] == '\'') {
1526 len = strstr(tmp + 1, "'") - tmp - 1;
1531 strncpy(mail->command, tmp + 1, len);
1533 mail->command[0] = '\0';
1535 mail->p_timed_thread = NULL;
1539 void *imap_thread(struct mail_s *mail)
1541 int sockfd, numbytes;
1542 char recvbuf[MAXDATASIZE];
1543 char sendbuf[MAXDATASIZE];
1546 unsigned int old_unseen = UINT_MAX;
1547 unsigned int old_messages = UINT_MAX;
1548 struct stat stat_buf;
1550 struct sockaddr_in their_addr; // connector's address information
1552 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1553 herror("gethostbyname");
1558 ERR("Trying IMAP connection again for %s@%s (try %i/5)",
1559 mail->user, mail->host, fail + 1);
1561 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1564 goto next_iteration;
1568 their_addr.sin_family = AF_INET;
1569 // short, network byte order
1570 their_addr.sin_port = htons(mail->port);
1571 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1572 // zero the rest of the struct
1573 memset(&(their_addr.sin_zero), '\0', 8);
1575 if (connect(sockfd, (struct sockaddr *) &their_addr,
1576 sizeof(struct sockaddr)) == -1) {
1579 goto next_iteration;
1581 struct timeval timeout;
1585 timeout.tv_sec = 60; // 60 second timeout i guess
1586 timeout.tv_usec = 0;
1588 FD_SET(sockfd, &fdset);
1589 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1591 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1594 goto next_iteration;
1597 ERR("IMAP connection failed: timeout");
1599 goto next_iteration;
1601 recvbuf[numbytes] = '\0';
1602 if (strstr(recvbuf, "* OK") != recvbuf) {
1603 ERR("IMAP connection failed, probably not an IMAP server");
1605 goto next_iteration;
1607 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1608 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1609 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1610 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1611 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1612 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1615 goto next_iteration;
1617 timeout.tv_sec = 60; // 60 second timeout i guess
1618 timeout.tv_usec = 0;
1620 FD_SET(sockfd, &fdset);
1621 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1623 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1626 goto next_iteration;
1629 recvbuf[numbytes] = '\0';
1630 if (strstr(recvbuf, "a1 OK") == NULL) {
1631 ERR("IMAP server login failed: %s", recvbuf);
1633 goto next_iteration;
1635 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1636 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1637 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
1638 MAXDATASIZE - strlen(sendbuf) - 1);
1639 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1642 goto next_iteration;
1644 timeout.tv_sec = 60; // 60 second timeout i guess
1645 timeout.tv_usec = 0;
1647 FD_SET(sockfd, &fdset);
1648 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1650 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1653 goto next_iteration;
1656 recvbuf[numbytes] = '\0';
1657 if (strstr(recvbuf, "a2 OK") == NULL) {
1658 ERR("IMAP status failed: %s", recvbuf);
1660 goto next_iteration;
1662 // now we get the data
1663 reply = strstr(recvbuf, " (MESSAGES ");
1665 *strchr(reply, ')') = '\0';
1666 if (reply == NULL) {
1667 ERR("Error parsing IMAP response: %s", recvbuf);
1669 goto next_iteration;
1671 timed_thread_lock(mail->p_timed_thread);
1672 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
1674 timed_thread_unlock(mail->p_timed_thread);
1676 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
1677 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1680 goto next_iteration;
1682 timeout.tv_sec = 60; // 60 second timeout i guess
1683 timeout.tv_usec = 0;
1685 FD_SET(sockfd, &fdset);
1686 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1688 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1691 goto next_iteration;
1694 recvbuf[numbytes] = '\0';
1695 if (strstr(recvbuf, "a3 OK") == NULL) {
1696 ERR("IMAP logout failed: %s", recvbuf);
1698 goto next_iteration;
1700 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
1701 || (mail->messages > old_messages && mail->unseen > 0))) {
1703 if (system(mail->command) == -1) {
1708 old_unseen = mail->unseen;
1709 old_messages = mail->messages;
1711 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1712 /* if a valid socket, close it */
1715 if (timed_thread_test(mail->p_timed_thread)) {
1716 timed_thread_exit(mail->p_timed_thread);
1724 void *pop3_thread(struct mail_s *mail)
1726 int sockfd, numbytes;
1727 char recvbuf[MAXDATASIZE];
1728 char sendbuf[MAXDATASIZE];
1731 unsigned int old_unseen = UINT_MAX;
1732 struct stat stat_buf;
1734 struct sockaddr_in their_addr; // connector's address information
1736 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1737 herror("gethostbyname");
1742 ERR("Trying POP3 connection again for %s@%s (try %i/5)",
1743 mail->user, mail->host, fail + 1);
1745 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1748 goto next_iteration;
1752 their_addr.sin_family = AF_INET;
1753 // short, network byte order
1754 their_addr.sin_port = htons(mail->port);
1755 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1756 // zero the rest of the struct
1757 memset(&(their_addr.sin_zero), '\0', 8);
1759 if (connect(sockfd, (struct sockaddr *) &their_addr,
1760 sizeof(struct sockaddr)) == -1) {
1763 goto next_iteration;
1765 struct timeval timeout;
1769 timeout.tv_sec = 60; // 60 second timeout i guess
1770 timeout.tv_usec = 0;
1772 FD_SET(sockfd, &fdset);
1773 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1775 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1778 goto next_iteration;
1781 ERR("POP3 connection failed: timeout\n");
1783 goto next_iteration;
1785 recvbuf[numbytes] = '\0';
1786 if (strstr(recvbuf, "+OK ") != recvbuf) {
1787 ERR("POP3 connection failed, probably not a POP3 server");
1789 goto next_iteration;
1791 strncpy(sendbuf, "USER ", MAXDATASIZE);
1792 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1793 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1794 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1795 perror("send USER");
1797 goto next_iteration;
1799 timeout.tv_sec = 60; // 60 second timeout i guess
1800 timeout.tv_usec = 0;
1802 FD_SET(sockfd, &fdset);
1803 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1805 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1806 perror("recv USER");
1808 goto next_iteration;
1811 recvbuf[numbytes] = '\0';
1812 if (strstr(recvbuf, "+OK ") == NULL) {
1813 ERR("POP3 server login failed: %s", recvbuf);
1815 goto next_iteration;
1817 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1818 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1819 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1820 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1821 perror("send PASS");
1823 goto next_iteration;
1825 timeout.tv_sec = 60; // 60 second timeout i guess
1826 timeout.tv_usec = 0;
1828 FD_SET(sockfd, &fdset);
1829 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1831 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1832 perror("recv PASS");
1834 goto next_iteration;
1837 recvbuf[numbytes] = '\0';
1838 if (strstr(recvbuf, "+OK ") == NULL) {
1839 ERR("POP3 server login failed: %s", recvbuf);
1841 goto next_iteration;
1843 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
1844 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1845 perror("send STAT");
1847 goto next_iteration;
1849 timeout.tv_sec = 60; // 60 second timeout i guess
1850 timeout.tv_usec = 0;
1852 FD_SET(sockfd, &fdset);
1853 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1855 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1856 perror("recv STAT");
1858 goto next_iteration;
1861 recvbuf[numbytes] = '\0';
1862 if (strstr(recvbuf, "+OK ") == NULL) {
1863 ERR("POP3 status failed: %s", recvbuf);
1865 goto next_iteration;
1867 // now we get the data
1868 reply = recvbuf + 4;
1869 if (reply == NULL) {
1870 ERR("Error parsing POP3 response: %s", recvbuf);
1872 goto next_iteration;
1874 timed_thread_lock(mail->p_timed_thread);
1875 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
1876 timed_thread_unlock(mail->p_timed_thread);
1878 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
1879 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1880 perror("send QUIT");
1882 goto next_iteration;
1884 timeout.tv_sec = 60; // 60 second timeout i guess
1885 timeout.tv_usec = 0;
1887 FD_SET(sockfd, &fdset);
1888 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1890 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1891 perror("recv QUIT");
1893 goto next_iteration;
1896 recvbuf[numbytes] = '\0';
1897 if (strstr(recvbuf, "+OK") == NULL) {
1898 ERR("POP3 logout failed: %s", recvbuf);
1900 goto next_iteration;
1902 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
1904 if (system(mail->command) == -1) {
1909 old_unseen = mail->unseen;
1911 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1912 /* if a valid socket, close it */
1915 if (timed_thread_test(mail->p_timed_thread)) {
1916 timed_thread_exit(mail->p_timed_thread);
1924 void *threaded_exec(struct text_object *obj)
1927 char *p2 = obj->data.texeci.buffer;
1928 FILE *fp = popen(obj->data.texeci.cmd, "r");
1930 timed_thread_lock(obj->data.texeci.p_timed_thread);
1931 int n2 = fread(p2, 1, text_buffer_size, fp);
1935 if (n2 && p2[n2 - 1] == '\n') {
1939 if (*p2 == '\001') {
1944 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1945 if (timed_thread_test(obj->data.texeci.p_timed_thread)) {
1946 timed_thread_exit(obj->data.texeci.p_timed_thread);
1952 static struct text_object *new_text_object_internal()
1954 struct text_object *obj = malloc(sizeof(struct text_object));
1955 memset(obj, 0, sizeof(struct text_object));
1959 static void free_text_objects(unsigned int count, struct text_object *objs)
1963 for (i = 0; i < count; i++) {
1964 switch (objs[i].type) {
1967 close(objs[i].data.i);
1970 close(objs[i].data.i);
1973 close(objs[i].data.sysfs.fd);
1976 close(objs[i].data.sysfs.fd);
1979 close(objs[i].data.sysfs.fd);
1981 #endif /* !__OpenBSD__ */
1983 free(objs[i].data.s);
1986 free(objs[i].data.s);
1989 free(objs[i].data.tztime.tz);
1990 free(objs[i].data.tztime.fmt);
1993 free(objs[i].data.mboxscan.args);
1994 free(objs[i].data.mboxscan.output);
1998 free(objs[i].data.local_mail.box);
2003 case OBJ_imap_unseen:
2004 if (!objs[i].global_mode) {
2005 free(objs[i].data.mail);
2008 case OBJ_imap_messages:
2009 if (!objs[i].global_mode) {
2010 free(objs[i].data.mail);
2016 case OBJ_pop3_unseen:
2017 if (!objs[i].global_mode) {
2018 free(objs[i].data.mail);
2022 if (!objs[i].global_mode) {
2023 free(objs[i].data.mail);
2027 case OBJ_if_existing:
2028 case OBJ_if_mounted:
2029 case OBJ_if_running:
2030 free(objs[i].data.ifblock.s);
2031 free(objs[i].data.ifblock.str);
2034 free(objs[i].data.tail.logfile);
2035 free(objs[i].data.tail.buffer);
2044 free(objs[i].data.s);
2047 case OBJ_iconv_start:
2053 free(objs[i].data.ifblock.s);
2054 free(objs[i].data.ifblock.str);
2057 free(objs[i].data.ifblock.s);
2058 free(objs[i].data.ifblock.str);
2061 if (info.gw_info.iface) {
2062 free(info.gw_info.iface);
2063 info.gw_info.iface = 0;
2065 if (info.gw_info.ip) {
2066 free(info.gw_info.ip);
2067 info.gw_info.ip = 0;
2070 case OBJ_ioscheduler:
2072 free(objs[i].data.s);
2076 case OBJ_xmms2_artist:
2077 if (info.xmms2.artist) {
2078 free(info.xmms2.artist);
2079 info.xmms2.artist = 0;
2082 case OBJ_xmms2_album:
2083 if (info.xmms2.album) {
2084 free(info.xmms2.album);
2085 info.xmms2.album = 0;
2088 case OBJ_xmms2_title:
2089 if (info.xmms2.title) {
2090 free(info.xmms2.title);
2091 info.xmms2.title = 0;
2094 case OBJ_xmms2_genre:
2095 if (info.xmms2.genre) {
2096 free(info.xmms2.genre);
2097 info.xmms2.genre = 0;
2100 case OBJ_xmms2_comment:
2101 if (info.xmms2.comment) {
2102 free(info.xmms2.comment);
2103 info.xmms2.comment = 0;
2107 if (info.xmms2.url) {
2108 free(info.xmms2.url);
2112 case OBJ_xmms2_date:
2113 if (info.xmms2.date) {
2114 free(info.xmms2.date);
2115 info.xmms2.date = 0;
2118 case OBJ_xmms2_status:
2119 if (info.xmms2.status) {
2120 free(info.xmms2.status);
2121 info.xmms2.status = 0;
2124 case OBJ_xmms2_playlist:
2125 if (info.xmms2.playlist) {
2126 free(info.xmms2.playlist);
2127 info.xmms2.playlist = 0;
2130 case OBJ_xmms2_smart:
2131 if (info.xmms2.artist) {
2132 free(info.xmms2.artist);
2133 info.xmms2.artist = 0;
2135 if (info.xmms2.title) {
2136 free(info.xmms2.title);
2137 info.xmms2.title = 0;
2139 if (info.xmms2.url) {
2140 free(info.xmms2.url);
2146 case OBJ_bmpx_title:
2147 case OBJ_bmpx_artist:
2148 case OBJ_bmpx_album:
2149 case OBJ_bmpx_track:
2151 case OBJ_bmpx_bitrate:
2156 free(objs[i].data.rss.uri);
2157 free(objs[i].data.rss.action);
2164 free(objs[i].data.s);
2166 case OBJ_battery_time:
2167 free(objs[i].data.s);
2169 #endif /* !__OpenBSD__ */
2173 case OBJ_execigraph:
2174 free(objs[i].data.execi.cmd);
2175 free(objs[i].data.execi.buffer);
2178 free(objs[i].data.texeci.cmd);
2179 free(objs[i].data.texeci.buffer);
2181 case OBJ_nameserver:
2186 if (info.first_process) {
2187 free_all_processes();
2188 info.first_process = NULL;
2193 free(objs[i].data.hddtemp.dev);
2194 free(objs[i].data.hddtemp.addr);
2197 case OBJ_entropy_avail:
2198 case OBJ_entropy_poolsize:
2199 case OBJ_entropy_bar:
2201 case OBJ_user_names:
2202 if (info.users.names) {
2203 free(info.users.names);
2204 info.users.names = 0;
2207 case OBJ_user_terms:
2208 if (info.users.terms) {
2209 free(info.users.terms);
2210 info.users.terms = 0;
2213 case OBJ_user_times:
2214 if (info.users.times) {
2215 free(info.users.times);
2216 info.users.times = 0;
2221 case OBJ_smapi_bat_perc:
2222 free(objs[i].data.s);
2224 case OBJ_if_smapi_bat_installed:
2225 free(objs[i].data.ifblock.s);
2226 free(objs[i].data.ifblock.str);
2231 case OBJ_mpd_artist:
2233 case OBJ_mpd_random:
2234 case OBJ_mpd_repeat:
2236 case OBJ_mpd_bitrate:
2237 case OBJ_mpd_status:
2240 case OBJ_mpd_password:
2242 case OBJ_mpd_elapsed:
2243 case OBJ_mpd_length:
2247 case OBJ_mpd_percent:
2249 free_mpd_vars(&info);
2255 /* text_objects = NULL;
2256 text_object_count = 0; */
2259 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2264 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2265 *a = mixer_init(buf1);
2266 scan_bar(arg + n, w, h);
2268 *a = mixer_init(NULL);
2269 scan_bar(arg, w, h);
2273 /* construct_text_object() creates a new text_object */
2274 static struct text_object *construct_text_object(const char *s,
2275 const char *arg, unsigned int object_count,
2276 struct text_object *text_objects, long line)
2278 // struct text_object *obj = new_text_object();
2279 struct text_object *obj = new_text_object_internal();
2283 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
2284 obj->type = OBJ_##a; need_mask |= (1 << n); {
2285 #define END } } else
2289 obj->type = OBJ_color;
2290 obj->data.l = get_x11_color(s);
2294 OBJ(freq, INFO_FREQ)
2297 obj->data.i = open_acpi_temperature(arg);
2298 END OBJ(acpitempf, 0)
2299 obj->data.i = open_acpi_temperature(arg);
2300 END OBJ(acpiacadapter, 0)
2301 END OBJ(freq, INFO_FREQ)
2302 #endif /* !__OpenBSD__ */
2304 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2305 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2306 obj->data.cpu_index = 1;
2307 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
2308 "Displaying the clock for CPU 1."); */
2310 obj->data.cpu_index = atoi(&arg[0]);
2313 END OBJ(freq_g, INFO_FREQ)
2315 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2316 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2317 obj->data.cpu_index = 1;
2318 /* ERR("freq_g: Invalid CPU number or you don't have that many "
2319 "CPUs! Displaying the clock for CPU 1."); */
2321 obj->data.cpu_index = atoi(&arg[0]);
2324 #if defined(__linux__)
2325 END OBJ(voltage_mv, 0)
2327 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2328 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2329 obj->data.cpu_index = 1;
2330 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
2331 "CPUs! Displaying voltage for CPU 1."); */
2333 obj->data.cpu_index = atoi(&arg[0]);
2336 END OBJ(voltage_v, 0)
2338 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2339 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2340 obj->data.cpu_index = 1;
2341 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
2342 "CPUs! Displaying voltage for CPU 1."); */
2344 obj->data.cpu_index = atoi(&arg[0]);
2349 END OBJ(wireless_essid, INFO_NET)
2351 obj->data.net = get_net_stat(arg);
2353 CRIT_ERR("wireless_essid: needs an argument");
2355 END OBJ(wireless_mode, INFO_NET)
2357 obj->data.net = get_net_stat(arg);
2359 CRIT_ERR("wireless_mode: needs an argument");
2361 END OBJ(wireless_bitrate, INFO_NET)
2363 obj->data.net = get_net_stat(arg);
2365 CRIT_ERR("wireless_bitrate: needs an argument");
2367 END OBJ(wireless_ap, INFO_NET)
2369 obj->data.net = get_net_stat(arg);
2371 CRIT_ERR("wireless_ap: needs an argument");
2373 END OBJ(wireless_link_qual, INFO_NET)
2375 obj->data.net = get_net_stat(arg);
2377 CRIT_ERR("wireless_link_qual: needs an argument");
2379 END OBJ(wireless_link_qual_max, INFO_NET)
2381 obj->data.net = get_net_stat(arg);
2383 CRIT_ERR("wireless_link_qual_max: needs an argument");
2385 END OBJ(wireless_link_qual_perc, INFO_NET)
2387 obj->data.net = get_net_stat(arg);
2389 CRIT_ERR("wireless_link_qual_perc: needs an argument");
2391 END OBJ(wireless_link_bar, INFO_NET)
2393 arg = scan_bar(arg, &obj->a, &obj->b);
2394 obj->data.net = get_net_stat(arg);
2396 CRIT_ERR("wireless_link_bar: needs an argument");
2398 #endif /* HAVE_IWLIB */
2400 #endif /* __linux__ */
2401 END OBJ(freq_dyn, 0)
2402 END OBJ(freq_dyn_g, 0)
2410 sscanf(arg, "%63s", bat);
2412 strcpy(bat, "BAT0");
2414 obj->data.s = strdup(bat);
2415 END OBJ(battery_time, 0)
2419 sscanf(arg, "%63s", bat);
2421 strcpy(bat, "BAT0");
2423 obj->data.s = strdup(bat);
2424 END OBJ(battery_percent, 0)
2428 sscanf(arg, "%63s", bat);
2430 strcpy(bat, "BAT0");
2432 obj->data.s = strdup(bat);
2433 END OBJ(battery_bar, 0)
2437 arg = scan_bar(arg, &obj->a, &obj->b);
2438 sscanf(arg, "%63s", bat);
2440 strcpy(bat, "BAT0");
2442 obj->data.s = strdup(bat);
2443 #endif /* !__OpenBSD__ */
2445 #if defined(__linux__)
2446 END OBJ(i8k_version, INFO_I8K)
2447 END OBJ(i8k_bios, INFO_I8K)
2448 END OBJ(i8k_serial, INFO_I8K)
2449 END OBJ(i8k_cpu_temp, INFO_I8K)
2450 END OBJ(i8k_cpu_tempf, INFO_I8K)
2451 END OBJ(i8k_left_fan_status, INFO_I8K)
2452 END OBJ(i8k_right_fan_status, INFO_I8K)
2453 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2454 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2455 END OBJ(i8k_ac_status, INFO_I8K)
2456 END OBJ(i8k_buttons_status, INFO_I8K)
2458 END OBJ(ibm_temps, 0)
2460 CRIT_ERR("ibm_temps: needs an argument");
2462 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
2463 obj->data.sensor = 0;
2464 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
2465 "Using 0 (CPU temp sensor).");
2467 obj->data.sensor = atoi(&arg[0]);
2468 END OBJ(ibm_volume, 0)
2469 END OBJ(ibm_brightness, 0)
2471 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2472 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2475 ERR("if_up needs an argument");
2476 obj->data.ifblock.s = 0;
2478 obj->data.ifblock.s = strdup(arg);
2479 blockstart[blockdepth] = object_count;
2480 obj->data.ifblock.pos = object_count + 2;
2483 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2484 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2486 blockstart[blockdepth] = object_count;
2487 obj->data.ifblock.pos = object_count + 2;
2489 END OBJ(ioscheduler, 0)
2491 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
2494 obj->data.s = strdup(arg);
2495 END OBJ(laptop_mode, 0)
2496 END OBJ(pb_battery, 0)
2497 if (arg && strcmp(arg, "status") == 0) {
2498 obj->data.i = PB_BATT_STATUS;
2499 } else if (arg && strcmp(arg, "percent") == 0) {
2500 obj->data.i = PB_BATT_PERCENT;
2501 } else if (arg && strcmp(arg, "time") == 0) {
2502 obj->data.i = PB_BATT_TIME;
2504 ERR("pb_battery: needs one argument: status, percent or time");
2509 #endif /* __linux__ */
2510 #if defined(__OpenBSD__)
2511 END OBJ(obsd_sensors_temp, 0)
2513 CRIT_ERR("obsd_sensors_temp: needs an argument");
2515 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2516 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2517 obj->data.sensor = 0;
2518 ERR("Invalid temperature sensor number!");
2520 obj->data.sensor = atoi(&arg[0]);
2521 END OBJ(obsd_sensors_fan, 0)
2523 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
2526 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2527 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2528 obj->data.sensor = 0;
2529 ERR("Invalid fan sensor number!");
2531 obj->data.sensor = atoi(&arg[0]);
2532 END OBJ(obsd_sensors_volt, 0)
2534 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
2537 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2538 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2539 obj->data.sensor = 0;
2540 ERR("Invalid voltage sensor number!");
2542 obj->data.sensor = atoi(&arg[0]);
2543 END OBJ(obsd_vendor, 0)
2544 END OBJ(obsd_product, 0)
2545 #endif /* __OpenBSD__ */
2546 END OBJ(buffers, INFO_BUFFERS)
2547 END OBJ(cached, INFO_BUFFERS)
2548 END OBJ(cpu, INFO_CPU)
2550 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2551 obj->data.cpu_index = atoi(&arg[3]);
2554 obj->data.cpu_index = 0;
2557 obj->data.cpu_index = 0;
2559 END OBJ(cpubar, INFO_CPU)
2561 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2562 obj->data.cpu_index = atoi(&arg[3]);
2565 obj->data.cpu_index = 0;
2567 scan_bar(arg, &obj->a, &obj->b);
2569 scan_bar(arg, &obj->a, &obj->b);
2570 obj->data.cpu_index = 0;
2572 END OBJ(cpugraph, INFO_CPU)
2573 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2577 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
2578 obj->data.cpu_index = atoi(&buf[3]);
2580 obj->data.cpu_index = 0;
2584 END OBJ(diskio, INFO_DISKIO)
2586 obj->data.diskio = prepare_diskio_stat(arg);
2588 obj->data.diskio = NULL;
2590 END OBJ(diskio_read, INFO_DISKIO)
2592 obj->data.diskio = prepare_diskio_stat(arg);
2594 obj->data.diskio = NULL;
2596 END OBJ(diskio_write, INFO_DISKIO)
2598 obj->data.diskio = prepare_diskio_stat(arg);
2600 obj->data.diskio = NULL;
2602 END OBJ(diskiograph, INFO_DISKIO)
2603 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2607 obj->data.diskio = prepare_diskio_stat(buf);
2610 obj->data.diskio = NULL;
2612 END OBJ(diskiograph_read, INFO_DISKIO)
2613 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2617 obj->data.diskio = prepare_diskio_stat(buf);
2620 obj->data.diskio = NULL;
2622 END OBJ(diskiograph_write, INFO_DISKIO)
2623 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2627 obj->data.diskio = prepare_diskio_stat(buf);
2630 obj->data.diskio = NULL;
2634 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2637 obj->data.l = color0;
2639 obj->data.l = color1;
2641 obj->data.l = color2;
2643 obj->data.l = color3;
2645 obj->data.l = color4;
2647 obj->data.l = color5;
2649 obj->data.l = color6;
2651 obj->data.l = color7;
2653 obj->data.l = color8;
2655 obj->data.l = color9;
2657 obj->data.s = scan_font(arg);
2658 END OBJ(conky_version, 0)
2659 END OBJ(conky_build_date, 0)
2660 END OBJ(conky_build_arch, 0)
2661 END OBJ(downspeed, INFO_NET)
2663 obj->data.net = get_net_stat(arg);
2665 CRIT_ERR("downspeed needs argument");
2667 END OBJ(downspeedf, INFO_NET)
2669 obj->data.net = get_net_stat(arg);
2671 CRIT_ERR("downspeedf needs argument");
2673 END OBJ(downspeedgraph, INFO_NET)
2674 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2678 obj->data.net = get_net_stat(buf);
2683 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos =
2685 blockstart[blockdepth - 1] = object_count;
2686 obj->data.ifblock.pos = object_count + 2;
2688 ERR("$else: no matching $if_*");
2693 text_objects[blockstart[blockdepth]].data.ifblock.pos =
2696 ERR("$endif: no matching $if_*");
2699 obj->data.s = strdup(arg ? arg : "");
2702 obj->data.s = strdup(arg ? arg : "");
2704 obj->data.s = strdup(arg ? arg : "");
2706 obj->data.s = strdup(arg ? arg : "");
2707 END OBJ(execgraph, 0)
2708 obj->data.s = strdup(arg ? arg : "");
2709 END OBJ(execibar, 0)
2712 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2715 ERR("${execibar <interval> command}");
2716 obj->type = OBJ_text;
2717 snprintf(buf, 256, "${%s}", s);
2718 obj->data.s = strdup(buf);
2720 obj->data.execi.cmd = strdup(arg + n);
2722 END OBJ(execigraph, 0)
2725 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2728 ERR("${execigraph <interval> command}");
2729 obj->type = OBJ_text;
2730 snprintf(buf, 256, "${%s}", s);
2731 obj->data.s = strdup(buf);
2733 obj->data.execi.cmd = strdup(arg + n);
2738 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2741 ERR("${execi <interval> command}");
2742 obj->type = OBJ_text;
2743 snprintf(buf, 256, "${%s}", s);
2744 obj->data.s = strdup(buf);
2746 obj->data.execi.cmd = strdup(arg + n);
2747 obj->data.execi.buffer = malloc(text_buffer_size);
2752 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2755 ERR("${execi <interval> command}");
2756 obj->type = OBJ_text;
2757 snprintf(buf, 256, "${%s}", s);
2758 obj->data.s = strdup(buf);
2760 obj->data.execi.cmd = strdup(arg + n);
2761 obj->data.execi.buffer = malloc(text_buffer_size);
2766 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2769 ERR("${texeci <interval> command}");
2770 obj->type = OBJ_text;
2771 snprintf(buf, 256, "${%s}", s);
2772 obj->data.s = strdup(buf);
2774 obj->data.texeci.cmd = strdup(arg + n);
2775 obj->data.texeci.buffer = malloc(text_buffer_size);
2777 obj->data.texeci.p_timed_thread = NULL;
2778 END OBJ(pre_exec, 0)
2779 obj->type = OBJ_text;
2781 FILE *fp = popen(arg, "r");
2785 n = fread(buf, 1, 2048, fp);
2788 if (n && buf[n - 1] == '\n') {
2794 obj->data.s = strdup(buf);
2796 obj->data.s = strdup("");
2799 END OBJ(fs_bar, INFO_FS)
2800 obj->data.fsbar.h = 4;
2801 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2803 while (isspace(*arg)) {
2812 obj->data.fsbar.fs = prepare_fs_stat(arg);
2813 END OBJ(fs_bar_free, INFO_FS)
2814 obj->data.fsbar.h = 4;
2818 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1) {
2824 obj->data.fsbar.fs = prepare_fs_stat(arg);
2825 END OBJ(fs_free, INFO_FS)
2829 obj->data.fs = prepare_fs_stat(arg);
2830 END OBJ(fs_used_perc, INFO_FS)
2834 obj->data.fs = prepare_fs_stat(arg);
2835 END OBJ(fs_free_perc, INFO_FS)
2839 obj->data.fs = prepare_fs_stat(arg);
2840 END OBJ(fs_size, INFO_FS)
2844 obj->data.fs = prepare_fs_stat(arg);
2845 END OBJ(fs_type, INFO_FS)
2849 obj->data.fs = prepare_fs_stat(arg);
2850 END OBJ(fs_used, INFO_FS)
2854 obj->data.fs = prepare_fs_stat(arg);
2856 obj->data.i = arg ? atoi(arg) : 1;
2857 END OBJ(nameserver, INFO_DNS)
2858 obj->data.i = arg ? atoi(arg) : 0;
2860 obj->data.i = arg ? atoi(arg) : 1;
2862 obj->data.i = arg ? atoi(arg) : 1;
2866 ERR("goto needs arguments");
2867 obj->type = OBJ_text;
2868 obj->data.s = strdup("${goto}");
2872 obj->data.i = atoi(arg);
2878 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2879 sscanf(arg, "%d", &b);
2885 obj->data.pair.a = a;
2886 obj->data.pair.b = b;
2889 END OBJ(i2c, INFO_SYSFS)
2890 char buf1[64], buf2[64];
2894 ERR("i2c needs arguments");
2895 obj->type = OBJ_text;
2896 // obj->data.s = strdup("${i2c}");
2900 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2901 /* if scanf couldn't read three values, read type and num and use
2903 sscanf(arg, "%63s %d", buf2, &n);
2904 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
2905 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2906 strncpy(obj->data.sysfs.type, buf2, 63);
2908 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
2909 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2910 strncpy(obj->data.sysfs.type, buf2, 63);
2913 END OBJ(platform, INFO_SYSFS)
2914 char buf1[64], buf2[64];
2918 ERR("platform needs arguments");
2919 obj->type = OBJ_text;
2923 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2924 /* if scanf couldn't read three values, read type and num and use
2926 sscanf(arg, "%63s %d", buf2, &n);
2927 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
2928 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2929 strncpy(obj->data.sysfs.type, buf2, 63);
2931 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
2932 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2933 strncpy(obj->data.sysfs.type, buf2, 63);
2936 END OBJ(hwmon, INFO_SYSFS)
2937 char buf1[64], buf2[64];
2941 ERR("hwmon needs argumanets");
2942 obj->type = OBJ_text;
2946 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2947 /* if scanf couldn't read three values, read type and num and use
2949 sscanf(arg, "%63s %d", buf2, &n);
2950 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
2951 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2952 strncpy(obj->data.sysfs.type, buf2, 63);
2954 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
2955 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2956 strncpy(obj->data.sysfs.type, buf2, 63);
2958 #endif /* !__OpenBSD__ */
2960 END OBJ(top, INFO_TOP)
2965 ERR("top needs arguments");
2966 obj->type = OBJ_text;
2967 // obj->data.s = strdup("${top}");
2970 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2971 if (strcmp(buf, "name") == 0) {
2972 obj->data.top.type = TOP_NAME;
2973 } else if (strcmp(buf, "cpu") == 0) {
2974 obj->data.top.type = TOP_CPU;
2975 } else if (strcmp(buf, "pid") == 0) {
2976 obj->data.top.type = TOP_PID;
2977 } else if (strcmp(buf, "mem") == 0) {
2978 obj->data.top.type = TOP_MEM;
2979 } else if (strcmp(buf, "time") == 0) {
2980 obj->data.top.type = TOP_TIME;
2982 ERR("invalid arg for top");
2985 if (n < 1 || n > 10) {
2986 CRIT_ERR("invalid arg for top");
2989 obj->data.top.num = n - 1;
2993 ERR("invalid args given for top");
2996 END OBJ(top_mem, INFO_TOP)
3001 ERR("top_mem needs arguments");
3002 obj->type = OBJ_text;
3003 obj->data.s = strdup("${top_mem}");
3006 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3007 if (strcmp(buf, "name") == 0) {
3008 obj->data.top.type = TOP_NAME;
3009 } else if (strcmp(buf, "cpu") == 0) {
3010 obj->data.top.type = TOP_CPU;
3011 } else if (strcmp(buf, "pid") == 0) {
3012 obj->data.top.type = TOP_PID;
3013 } else if (strcmp(buf, "mem") == 0) {
3014 obj->data.top.type = TOP_MEM;
3015 } else if (strcmp(buf, "time") == 0) {
3016 obj->data.top.type = TOP_TIME;
3018 ERR("invalid arg for top");
3021 if (n < 1 || n > 10) {
3022 CRIT_ERR("invalid arg for top");
3025 obj->data.top.num = n - 1;
3029 ERR("invalid args given for top");
3032 END OBJ(addr, INFO_NET)
3034 obj->data.net = get_net_stat(arg);
3036 CRIT_ERR("addr needs argument");
3038 #if defined(__linux__)
3039 END OBJ(addrs, INFO_NET)
3041 obj->data.net = get_net_stat(arg);
3043 CRIT_ERR("addrs needs argument");
3045 #endif /* __linux__ */
3052 ERR("tail needs arguments");
3053 obj->type = OBJ_text;
3054 obj->data.s = strdup("${tail}");
3057 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3058 if (n1 < 1 || n1 > 30) {
3059 CRIT_ERR("invalid arg for tail, number of lines must be "
3060 "between 1 and 30");
3066 obj->data.tail.fd = -1;
3068 if (stat(buf, &st) == 0) {
3069 if (S_ISFIFO(st.st_mode)) {
3070 fd = open(buf, O_RDONLY | O_NONBLOCK);
3073 CRIT_ERR("tail logfile does not exist, or you do "
3074 "not have correct permissions");
3077 obj->data.tail.fd = fd;
3079 fp = fopen(buf, "r");
3083 if (fp || obj->data.tail.fd != -1) {
3084 obj->data.tail.logfile = malloc(text_buffer_size);
3085 strcpy(obj->data.tail.logfile, buf);
3086 obj->data.tail.wantedlines = n1;
3087 obj->data.tail.interval = update_interval * 2;
3089 if (obj->data.tail.fd == -1) {
3094 CRIT_ERR("tail logfile does not exist, or you do not have "
3095 "correct permissions");
3098 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3099 if (n1 < 1 || n1 > 30) {
3100 CRIT_ERR("invalid arg for tail, number of lines must be "
3101 "between 1 and 30");
3103 } else if (n2 < 1 || n2 < update_interval) {
3104 CRIT_ERR("invalid arg for tail, interval must be greater than "
3105 "0 and Conky's interval");
3111 obj->data.tail.fd = -1;
3113 if (stat(buf, &st) == 0) {
3114 if (S_ISFIFO(st.st_mode)) {
3115 fd = open(buf, O_RDONLY | O_NONBLOCK);
3118 CRIT_ERR("tail logfile does not exist, or you do "
3119 "not have correct permissions");
3122 obj->data.tail.fd = fd;
3124 fp = fopen(buf, "r");
3128 if (fp || obj->data.tail.fd != -1) {
3129 obj->data.tail.logfile = malloc(text_buffer_size);
3130 strcpy(obj->data.tail.logfile, buf);
3131 obj->data.tail.wantedlines = n1;
3132 obj->data.tail.interval = n2;
3134 if (obj->data.tail.fd == -1) {
3139 CRIT_ERR("tail logfile does not exist, or you do not have "
3140 "correct permissions");
3144 ERR("invalid args given for tail");
3147 /* asumming all else worked */
3148 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3154 ERR("head needs arguments");
3155 obj->type = OBJ_text;
3156 obj->data.s = strdup("${head}");
3159 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3160 if (n1 < 1 || n1 > 30) {
3161 CRIT_ERR("invalid arg for head, number of lines must be "
3162 "between 1 and 30");
3167 fp = fopen(buf, "r");
3169 obj->data.tail.logfile = malloc(text_buffer_size);
3170 strcpy(obj->data.tail.logfile, buf);
3171 obj->data.tail.wantedlines = n1;
3172 obj->data.tail.interval = update_interval * 2;
3176 CRIT_ERR("head logfile does not exist, or you do not have "
3177 "correct permissions");
3180 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3181 if (n1 < 1 || n1 > 30) {
3182 CRIT_ERR("invalid arg for head, number of lines must be "
3183 "between 1 and 30");
3185 } else if (n2 < 1 || n2 < update_interval) {
3186 CRIT_ERR("invalid arg for head, interval must be greater than "
3187 "0 and Conky's interval");
3192 fp = fopen(buf, "r");
3194 obj->data.tail.logfile = malloc(text_buffer_size);
3195 strcpy(obj->data.tail.logfile, buf);
3196 obj->data.tail.wantedlines = n1;
3197 obj->data.tail.interval = n2;
3201 CRIT_ERR("head logfile does not exist, or you do not have "
3202 "correct permissions");
3206 ERR("invalid args given for head");
3209 /* asumming all else worked */
3210 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3211 END OBJ(loadavg, INFO_LOADAVG)
3212 int a = 1, b = 2, c = 3, r = 3;
3215 r = sscanf(arg, "%d %d %d", &a, &b, &c);
3216 if (r >= 3 && (c < 1 || c > 3)) {
3219 if (r >= 2 && (b < 1 || b > 3)) {
3222 if (r >= 1 && (a < 1 || a > 3)) {
3226 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
3227 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
3228 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
3229 END OBJ(if_empty, 0)
3230 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3231 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3234 ERR("if_empty needs an argument");
3235 obj->data.ifblock.s = 0;
3237 obj->data.ifblock.s = strdup(arg);
3239 blockstart[blockdepth] = object_count;
3240 obj->data.ifblock.pos = object_count + 2;
3242 END OBJ(if_existing, 0)
3243 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3244 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3247 ERR("if_existing needs an argument or two");
3248 obj->data.ifblock.s = NULL;
3249 obj->data.ifblock.str = NULL;
3251 char buf1[256], buf2[256];
3252 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3255 obj->data.ifblock.s = strdup(buf1);
3256 obj->data.ifblock.str = NULL;
3258 obj->data.ifblock.s = strdup(buf1);
3259 obj->data.ifblock.str = strdup(buf2);
3262 blockstart[blockdepth] = object_count;
3263 obj->data.ifblock.pos = object_count + 2;
3265 END OBJ(if_mounted, 0)
3266 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3267 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3270 ERR("if_mounted needs an argument");
3271 obj->data.ifblock.s = 0;
3273 obj->data.ifblock.s = strdup(arg);
3275 blockstart[blockdepth] = object_count;
3276 obj->data.ifblock.pos = object_count + 2;
3278 END OBJ(if_running, 0)
3279 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3280 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3285 snprintf(buf, 256, "pidof %s >/dev/null", arg);
3286 obj->data.ifblock.s = strdup(buf);
3288 ERR("if_running needs an argument");
3289 obj->data.ifblock.s = 0;
3291 blockstart[blockdepth] = object_count;
3292 obj->data.ifblock.pos = object_count + 2;
3298 char box[256], dst[256];
3302 strncpy(box, MAIL_FILE, sizeof(box));
3304 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3306 strncpy(box, arg, sizeof(box));
3310 variable_substitute(box, dst, sizeof(dst));
3311 obj->data.local_mail.box = strdup(dst);
3312 obj->data.local_mail.interval = n1;
3313 END OBJ(mboxscan, 0)
3314 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
3315 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
3316 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
3317 obj->data.mboxscan.output[0] = 1;
3318 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
3319 END OBJ(mem, INFO_MEM)
3320 END OBJ(memmax, INFO_MEM)
3321 END OBJ(memperc, INFO_MEM)
3322 END OBJ(membar, INFO_MEM)
3323 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3324 END OBJ(memgraph, INFO_MEM)
3325 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3331 END OBJ(mixer, INFO_MIXER)
3332 obj->data.l = mixer_init(arg);
3333 END OBJ(mixerl, INFO_MIXER)
3334 obj->data.l = mixer_init(arg);
3335 END OBJ(mixerr, INFO_MIXER)
3336 obj->data.l = mixer_init(arg);
3337 END OBJ(mixerbar, INFO_MIXER)
3338 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3339 &obj->data.mixerbar.h);
3340 END OBJ(mixerlbar, INFO_MIXER)
3341 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3342 &obj->data.mixerbar.h);
3343 END OBJ(mixerrbar, INFO_MIXER)
3344 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3345 &obj->data.mixerbar.h);
3346 END OBJ(new_mails, 0)
3348 char box[256], dst[256];
3352 strncpy(box, MAIL_FILE, sizeof(box));
3354 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3356 strncpy(box, arg, sizeof(box));
3360 variable_substitute(box, dst, sizeof(dst));
3361 obj->data.local_mail.box = strdup(dst);
3362 obj->data.local_mail.interval = n1;
3363 END OBJ(nodename, 0)
3364 END OBJ(processes, INFO_PROCS)
3365 END OBJ(running_processes, INFO_RUN_PROCS)
3366 END OBJ(shadecolor, 0)
3368 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
3370 END OBJ(outlinecolor, 0)
3372 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
3374 END OBJ(stippled_hr, 0)
3376 int a = stippled_borders, b = 1;
3379 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3380 sscanf(arg, "%d", &b);
3386 obj->data.pair.a = a;
3387 obj->data.pair.b = b;
3389 END OBJ(swap, INFO_MEM)
3390 END OBJ(swapmax, INFO_MEM)
3391 END OBJ(swapperc, INFO_MEM)
3392 END OBJ(swapbar, INFO_MEM)
3393 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3396 END OBJ(temp1, INFO_SYSFS)
3397 obj->type = OBJ_i2c;
3398 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
3399 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3400 END OBJ(temp2, INFO_SYSFS)
3401 obj->type = OBJ_i2c;
3402 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
3403 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3406 obj->data.s = strdup(arg ? arg : "%F %T");
3408 obj->data.s = strdup(arg ? arg : "%F %T");
3410 char buf1[256], buf2[256], *fmt, *tz;
3414 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3424 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
3425 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
3427 END OBJ(iconv_start, 0)
3428 if (iconv_converting) {
3429 CRIT_ERR("You must stop your last iconv conversion before "
3430 "starting another");
3433 char iconv_from[CODEPAGE_LENGTH];
3434 char iconv_to[CODEPAGE_LENGTH];
3436 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
3437 CRIT_ERR("Invalid arguments for iconv_start");
3441 new_iconv = iconv_open(iconv_to, iconv_from);
3442 if (new_iconv == (iconv_t) (-1)) {
3443 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
3445 obj->a = register_iconv(&new_iconv);
3446 iconv_converting = 1;
3450 CRIT_ERR("Iconv requires arguments");
3452 END OBJ(iconv_stop, 0)
3453 iconv_converting = 0;
3456 END OBJ(totaldown, INFO_NET)
3458 obj->data.net = get_net_stat(arg);
3460 CRIT_ERR("totaldown needs argument");
3462 END OBJ(totalup, INFO_NET)
3463 obj->data.net = get_net_stat(arg);
3465 obj->data.net = get_net_stat(arg);
3467 CRIT_ERR("totalup needs argument");
3471 obj->data.i = arg ? atoi(arg) : 0;
3473 obj->data.i = arg ? atoi(arg) : 0;
3474 END OBJ(upspeed, INFO_NET)
3476 obj->data.net = get_net_stat(arg);
3478 CRIT_ERR("upspeed needs argument");
3480 END OBJ(upspeedf, INFO_NET)
3482 obj->data.net = get_net_stat(arg);
3484 CRIT_ERR("upspeedf needs argument");
3487 END OBJ(upspeedgraph, INFO_NET)
3488 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3492 obj->data.net = get_net_stat(buf);
3495 END OBJ(uptime_short, INFO_UPTIME)
3496 END OBJ(uptime, INFO_UPTIME)
3497 END OBJ(user_names, INFO_USERS)
3498 END OBJ(user_times, INFO_USERS)
3499 END OBJ(user_terms, INFO_USERS)
3500 END OBJ(user_number, INFO_USERS)
3501 END OBJ(gw_iface, INFO_GW)
3502 END OBJ(gw_ip, INFO_GW)
3503 END OBJ(if_gw, INFO_GW)
3505 END OBJ(adt746xcpu, 0)
3506 END OBJ(adt746xfan, 0)
3507 #endif /* !__OpenBSD__ */
3508 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
3509 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
3510 END OBJ(apm_adapter, 0)
3511 END OBJ(apm_battery_life, 0)
3512 END OBJ(apm_battery_time, 0)
3513 #endif /* __FreeBSD__ */
3514 END OBJ(imap_unseen, 0)
3517 obj->data.mail = parse_mail_args(IMAP, arg);
3518 obj->global_mode = 0;
3520 obj->global_mode = 1;
3522 END OBJ(imap_messages, 0)
3525 obj->data.mail = parse_mail_args(IMAP, arg);
3526 obj->global_mode = 0;
3528 obj->global_mode = 1;
3530 END OBJ(pop3_unseen, 0)
3533 obj->data.mail = parse_mail_args(POP3, arg);
3534 obj->global_mode = 0;
3536 obj->global_mode = 1;
3538 END OBJ(pop3_used, 0)
3541 obj->data.mail = parse_mail_args(POP3, arg);
3542 obj->global_mode = 0;
3544 obj->global_mode = 1;
3549 obj->data.s = strdup(arg);
3551 ERR("smapi needs an argument");
3552 END OBJ(if_smapi_bat_installed, 0)
3553 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3554 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3557 ERR("if_smapi_bat_installed needs an argument");
3558 obj->data.ifblock.s = 0;
3560 obj->data.ifblock.s = strdup(arg);
3561 blockstart[blockdepth] = object_count;
3562 obj->data.ifblock.pos = object_count + 2;
3564 END OBJ(smapi_bat_perc, 0)
3566 obj->data.s = strdup(arg);
3568 ERR("smapi_bat_perc needs an argument");
3569 END OBJ(smapi_bat_bar, 0)
3572 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
3573 ERR("first argument to smapi_bat_bar must be an integer value");
3577 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
3580 ERR("if_smapi_bat_bar needs an argument");
3583 END OBJ(mpd_artist, INFO_MPD)
3584 END OBJ(mpd_title, INFO_MPD)
3586 sscanf(arg, "%d", &info.mpd.max_title_len);
3587 if (info.mpd.max_title_len > 0) {
3588 info.mpd.max_title_len++;
3590 CRIT_ERR("mpd_title: invalid length argument");
3593 info.mpd.max_title_len = 0;
3595 END OBJ(mpd_random, INFO_MPD)
3596 END OBJ(mpd_repeat, INFO_MPD)
3597 END OBJ(mpd_elapsed, INFO_MPD)
3598 END OBJ(mpd_length, INFO_MPD)
3599 END OBJ(mpd_track, INFO_MPD)
3600 END OBJ(mpd_name, INFO_MPD)
3601 END OBJ(mpd_file, INFO_MPD)
3602 END OBJ(mpd_percent, INFO_MPD)
3603 END OBJ(mpd_album, INFO_MPD)
3604 END OBJ(mpd_vol, INFO_MPD)
3605 END OBJ(mpd_bitrate, INFO_MPD)
3606 END OBJ(mpd_status, INFO_MPD)
3607 END OBJ(mpd_bar, INFO_MPD)
3608 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3609 END OBJ(mpd_smart, INFO_MPD)
3612 END OBJ(xmms2_artist, INFO_XMMS2)
3613 END OBJ(xmms2_album, INFO_XMMS2)
3614 END OBJ(xmms2_title, INFO_XMMS2)
3615 END OBJ(xmms2_genre, INFO_XMMS2)
3616 END OBJ(xmms2_comment, INFO_XMMS2)
3617 END OBJ(xmms2_url, INFO_XMMS2)
3618 END OBJ(xmms2_tracknr, INFO_XMMS2)
3619 END OBJ(xmms2_bitrate, INFO_XMMS2)
3620 END OBJ(xmms2_date, INFO_XMMS2)
3621 END OBJ(xmms2_id, INFO_XMMS2)
3622 END OBJ(xmms2_duration, INFO_XMMS2)
3623 END OBJ(xmms2_elapsed, INFO_XMMS2)
3624 END OBJ(xmms2_size, INFO_XMMS2)
3625 END OBJ(xmms2_status, INFO_XMMS2)
3626 END OBJ(xmms2_percent, INFO_XMMS2)
3627 END OBJ(xmms2_bar, INFO_XMMS2)
3628 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3629 END OBJ(xmms2_smart, INFO_XMMS2)
3630 END OBJ(xmms2_playlist, INFO_XMMS2)
3631 END OBJ(xmms2_timesplayed, INFO_XMMS2)
3634 END OBJ(audacious_status, INFO_AUDACIOUS)
3635 END OBJ(audacious_title, INFO_AUDACIOUS)
3637 sscanf(arg, "%d", &info.audacious.max_title_len);
3638 if (info.audacious.max_title_len > 0) {
3639 info.audacious.max_title_len++;
3641 CRIT_ERR("audacious_title: invalid length argument");
3644 END OBJ(audacious_length, INFO_AUDACIOUS)
3645 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
3646 END OBJ(audacious_position, INFO_AUDACIOUS)
3647 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
3648 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
3649 END OBJ(audacious_frequency, INFO_AUDACIOUS)
3650 END OBJ(audacious_channels, INFO_AUDACIOUS)
3651 END OBJ(audacious_filename, INFO_AUDACIOUS)
3652 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
3653 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
3654 END OBJ(audacious_bar, INFO_AUDACIOUS)
3655 scan_bar(arg, &obj->a, &obj->b);
3658 END OBJ(bmpx_title, INFO_BMPX)
3659 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3660 END OBJ(bmpx_artist, INFO_BMPX)
3661 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3662 END OBJ(bmpx_album, INFO_BMPX)
3663 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3664 END OBJ(bmpx_track, INFO_BMPX)
3665 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3666 END OBJ(bmpx_uri, INFO_BMPX)
3667 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3668 END OBJ(bmpx_bitrate, INFO_BMPX)
3669 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3674 int argc, delay, act_par;
3675 char *uri = (char *) malloc(128 * sizeof(char));
3676 char *action = (char *) malloc(64 * sizeof(char));
3678 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
3680 obj->data.rss.uri = uri;
3681 obj->data.rss.delay = delay;
3682 obj->data.rss.action = action;
3683 obj->data.rss.act_par = act_par;
3687 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
3693 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
3694 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3695 ERR("hddtemp needs arguments");
3696 obj->type = OBJ_text;
3697 obj->data.s = strdup("${hddtemp}");
3701 #ifdef TCP_PORT_MONITOR
3702 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3703 int argc, port_begin, port_end, item, connection_index;
3706 memset(itembuf, 0, sizeof(itembuf));
3707 connection_index = 0;
3708 /* massive argument checking */
3710 CRIT_ERR("tcp_portmon: needs arguments");
3712 argc = sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf,
3714 if ((argc != 3) && (argc != 4)) {
3715 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
3717 if ((port_begin < 1) || (port_begin > 65535) || (port_end < 1)
3718 || (port_end > 65535)) {
3719 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
3721 if (port_begin > port_end) {
3722 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
3724 if (strncmp(itembuf, "count", 31) == 0) {
3726 } else if (strncmp(itembuf, "rip", 31) == 0) {
3728 } else if (strncmp(itembuf, "rhost", 31) == 0) {
3730 } else if (strncmp(itembuf, "rport", 31) == 0) {
3732 } else if (strncmp(itembuf, "rservice", 31) == 0) {
3733 item = REMOTESERVICE;
3734 } else if (strncmp(itembuf, "lip", 31) == 0) {
3736 } else if (strncmp(itembuf, "lhost", 31) == 0) {
3738 } else if (strncmp(itembuf, "lport", 31) == 0) {
3740 } else if (strncmp(itembuf, "lservice", 31) == 0) {
3741 item = LOCALSERVICE;
3743 CRIT_ERR("tcp_portmon: invalid item specified");
3745 if ((argc == 3) && (item != COUNT)) {
3746 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" "
3749 if ((argc == 4) && (connection_index < 0)) {
3750 CRIT_ERR("tcp_portmon: connection index must be non-negative");
3752 /* ok, args looks good. save the text object data */
3753 obj->data.tcp_port_monitor.port_range_begin = (in_port_t) port_begin;
3754 obj->data.tcp_port_monitor.port_range_end = (in_port_t) port_end;
3755 obj->data.tcp_port_monitor.item = item;
3756 obj->data.tcp_port_monitor.connection_index = connection_index;
3758 /* if the port monitor collection hasn't been created,
3759 * we must create it */
3760 if (!info.p_tcp_port_monitor_collection) {
3761 info.p_tcp_port_monitor_collection =
3762 create_tcp_port_monitor_collection();
3763 if (!info.p_tcp_port_monitor_collection) {
3764 CRIT_ERR("tcp_portmon: unable to create port monitor "
3769 /* if a port monitor for this port does not exist,
3770 * create one and add it to the collection */
3771 if (find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
3772 port_begin, port_end) == NULL) {
3773 tcp_port_monitor_t *p_monitor = create_tcp_port_monitor(port_begin,
3774 port_end, &tcp_port_monitor_args);
3777 CRIT_ERR("tcp_portmon: unable to create port monitor");
3779 /* add the newly created monitor to the collection */
3780 if (insert_tcp_port_monitor_into_collection(
3781 info.p_tcp_port_monitor_collection, p_monitor) != 0) {
3782 CRIT_ERR("tcp_portmon: unable to add port monitor to "
3787 END OBJ(entropy_avail, INFO_ENTROPY)
3788 END OBJ(entropy_poolsize, INFO_ENTROPY)
3789 END OBJ(entropy_bar, INFO_ENTROPY)
3790 scan_bar(arg, &obj->a, &obj->b);
3794 ERR("unknown variable %s", s);
3795 obj->type = OBJ_text;
3796 snprintf(buf, 256, "${%s}", s);
3797 obj->data.s = strdup(buf);
3804 static struct text_object *create_plain_text(const char *s)
3806 struct text_object *obj;
3808 if (s == NULL || *s == '\0') {
3812 obj = new_text_object_internal();
3814 obj->type = OBJ_text;
3815 obj->data.s = strdup(s);
3819 static struct text_object_list *extract_variable_text_internal(const char *p)
3821 struct text_object_list *retval;
3822 struct text_object *obj;
3825 retval = malloc(sizeof(struct text_object_list));
3826 memset(retval, 0, sizeof(struct text_object_list));
3827 retval->text_object_count = 0;
3829 long line = text_lines;
3837 obj = create_plain_text(s);
3839 // allocate memory for the object
3840 retval->text_objects = realloc(retval->text_objects,
3841 sizeof(struct text_object) *
3842 (retval->text_object_count + 1));
3843 // assign the new object to the end of the list.
3844 memcpy(&retval->text_objects[retval->text_object_count++], obj,
3845 sizeof(struct text_object));
3857 /* variable is either $foo or ${foo} */
3859 unsigned int brl = 1, brr = 0;
3863 while (*p && brl != brr) {
3878 while (*p && (isalnum((int) *p) || *p == '_')) {
3883 /* copy variable to buffer */
3884 len = (p - s > 255) ? 255 : (p - s);
3885 strncpy(buf, s, len);
3895 /* if variable wasn't found in environment, use some special */
3901 if (strchr(buf, ' ')) {
3902 arg = strchr(buf, ' ');
3905 while (isspace((int) *arg)) {
3913 /* lowercase variable name */
3920 // create new object
3921 obj = construct_text_object(buf, arg,
3922 retval->text_object_count, retval->text_objects, line);
3924 // allocate memory for the object
3925 retval->text_objects = realloc(retval->text_objects,
3926 sizeof(struct text_object) *
3927 (retval->text_object_count + 1));
3928 // assign the new object to the end of the list.
3930 &retval->text_objects[retval->text_object_count++],
3931 obj, sizeof(struct text_object));
3937 obj = create_plain_text("$");
3939 // allocate memory for the object
3940 retval->text_objects = realloc(retval->text_objects,
3941 sizeof(struct text_object) *
3942 (retval->text_object_count + 1));
3943 // assign the new object to the end of the list.
3944 memcpy(&retval->text_objects[retval->text_object_count++],
3945 obj, sizeof(struct text_object));
3952 obj = create_plain_text(s);
3954 // allocate memory for the object
3955 retval->text_objects = realloc(retval->text_objects,
3956 sizeof(struct text_object) * (retval->text_object_count + 1));
3957 // assign the new object to the end of the list.
3958 memcpy(&retval->text_objects[retval->text_object_count++], obj,
3959 sizeof(struct text_object));
3964 ERR("one or more $endif's are missing");
3970 static void extract_variable_text(const char *p)
3972 struct text_object_list *list;
3974 free_text_objects(text_object_count, text_objects);
3987 text_object_count = 0;
3988 text_objects = NULL;
3990 list = extract_variable_text_internal(p);
3991 text_objects = list->text_objects;
3992 text_object_count = list->text_object_count;
3997 struct text_object_list *parse_conky_vars(char *text, char *p, struct information *cur)
3999 struct text_object_list *object_list =
4000 extract_variable_text_internal(text);
4002 generate_text_internal(p, max_user_text, object_list->text_objects,
4003 object_list->text_object_count, cur);
4007 /* Allows reading from a FIFO (i.e., /dev/xconsole).
4008 * The file descriptor is set to non-blocking which makes this possible.
4010 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
4011 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
4013 #define TAIL_PIPE_BUFSIZE 4096
4017 int fd = obj->data.tail.fd;
4020 char buf[TAIL_PIPE_BUFSIZE];
4021 ssize_t len = read(fd, buf, sizeof(buf));
4025 if (errno != EAGAIN) {
4026 strcpy(obj->data.tail.buffer, "Logfile Read Error");
4027 snprintf(dst, dst_size, "Logfile Read Error");
4031 } else if (len == 0) {
4032 strcpy(obj->data.tail.buffer, "Logfile Empty");
4033 snprintf(dst, dst_size, "Logfile Empty");
4037 for (line_len = 0, i = 0; i < len; i++) {
4041 if (buf[i] == '\n') {
4044 if (obj->data.tail.readlines > 0) {
4049 for (n = 0; obj->data.tail.buffer[n]; n++) {
4050 if (obj->data.tail.buffer[n] == '\n') {
4055 if (++olines < obj->data.tail.wantedlines) {
4061 p = obj->data.tail.buffer + first_line;
4062 pos = n - first_line;
4063 memmove(obj->data.tail.buffer,
4064 obj->data.tail.buffer + first_line, strlen(p));
4065 obj->data.tail.buffer[pos] = 0;
4071 p = buf + last_line;
4073 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
4074 obj->data.tail.buffer[pos + line_len] = 0;
4077 obj->data.tail.readlines = lines;
4085 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
4088 char *format_time(unsigned long time, const int width)
4091 unsigned long nt; // narrow time, for speed on 32-bit
4092 unsigned cc; // centiseconds
4093 unsigned nn; // multi-purpose whatever
4096 cc = nt % 100; // centiseconds past second
4097 nt /= 100; // total seconds
4098 nn = nt % 60; // seconds past the minute
4099 nt /= 60; // total minutes
4100 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
4104 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
4107 nn = nt % 60; // minutes past the hour
4108 nt /= 60; // total hours
4109 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
4112 nn = nt; // now also hours
4113 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
4116 nn /= 24; // now days
4117 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
4120 nn /= 7; // now weeks
4121 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
4124 // well shoot, this outta' fit...
4125 return strdup("<inf>");
4128 static void generate_text_internal(char *p, int p_max_size,
4129 struct text_object *objs, unsigned int object_count,
4130 struct information *cur)
4135 char buff_in[p_max_size];
4137 iconv_converting = 0;
4140 for (i = 0; i < object_count; i++) {
4141 struct text_object *obj = &objs[i];
4143 if (p_max_size < 1) {
4147 #define OBJ(a) break; case OBJ_##a:
4149 switch (obj->type) {
4151 ERR("not implemented obj type %d", obj->type);
4154 /* does anyone have decimals in acpi temperature? */
4155 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4156 round_to_int(get_acpi_temperature(obj->data.i)));
4159 /* does anyone have decimals in acpi temperature? */
4160 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4161 round_to_int((get_acpi_temperature(obj->data.i) + 40) *
4164 #endif /* !__OpenBSD__ */
4167 obj->a = get_freq(p, p_max_size, "%.0f", 1,
4168 obj->data.cpu_index);
4174 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
4175 obj->data.cpu_index);
4177 /* OpenBSD has no such flag (SUSv2) */
4178 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
4179 obj->data.cpu_index);
4183 #if defined(__linux__)
4186 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
4187 obj->data.cpu_index);
4192 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
4193 obj->data.cpu_index);
4198 OBJ(wireless_essid) {
4199 snprintf(p, p_max_size, "%s", obj->data.net->essid);
4201 OBJ(wireless_mode) {
4202 snprintf(p, p_max_size, "%s", obj->data.net->mode);
4204 OBJ(wireless_bitrate) {
4205 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
4208 snprintf(p, p_max_size, "%s", obj->data.net->ap);
4210 OBJ(wireless_link_qual) {
4211 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
4212 obj->data.net->link_qual);
4214 OBJ(wireless_link_qual_max) {
4215 spaced_print(p, p_max_size, "%d", 4,
4216 "wireless_link_qual_max", obj->data.net->link_qual_max);
4218 OBJ(wireless_link_qual_perc) {
4219 if (obj->data.net->link_qual_max > 0) {
4220 spaced_print(p, p_max_size, "%.0f", 5,
4221 "wireless_link_qual_perc",
4222 (double) obj->data.net->link_qual /
4223 obj->data.net->link_qual_max * 100);
4225 spaced_print(p, p_max_size, "unk", 5,
4226 "wireless_link_qual_perc");
4229 OBJ(wireless_link_bar) {
4230 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4231 obj->data.net->link_qual_max) * 255.0);
4233 #endif /* HAVE_IWLIB */
4235 #endif /* __linux__ */
4238 get_freq_dynamic(p, p_max_size, "%.0f", 1);
4239 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4243 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
4245 get_freq_dynamic(p, p_max_size, "%.2f", 1000);
4247 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4252 get_adt746x_cpu(p, p_max_size);
4255 get_adt746x_fan(p, p_max_size);
4258 get_acpi_fan(p, p_max_size);
4260 OBJ(acpiacadapter) {
4261 get_acpi_ac_adapter(p, p_max_size);
4264 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4267 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4269 OBJ(battery_percent) {
4270 spaced_print(p, p_max_size, "%*d", pad_percents,
4271 "battery_percent", get_battery_perct(obj->data.s));
4274 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4276 #endif /* __OpenBSD__ */
4279 human_readable(cur->buffers * 1024, p, 255, "buffers");
4282 human_readable(cur->cached * 1024, p, 255, "buffers");
4285 if (obj->data.cpu_index > info.cpu_count) {
4286 printf("obj->data.cpu_index %i info.cpu_count %i",
4287 obj->data.cpu_index, info.cpu_count);
4288 CRIT_ERR("attempting to use more CPUs then you have!");
4290 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
4291 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4294 new_bar(p, obj->a, obj->b,
4295 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4298 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
4299 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4303 new_fg(p, obj->data.l);
4335 OBJ(conky_version) {
4336 snprintf(p, p_max_size, "%s", VERSION);
4338 OBJ(conky_build_date) {
4339 snprintf(p, p_max_size, "%s", BUILD_DATE);
4341 OBJ(conky_build_arch) {
4342 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4344 #if defined(__linux__)
4346 snprintf(p, p_max_size, "%s", i8k.version);
4349 snprintf(p, p_max_size, "%s", i8k.bios);
4352 snprintf(p, p_max_size, "%s", i8k.serial);
4355 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
4357 OBJ(i8k_cpu_tempf) {
4360 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4361 snprintf(p, p_max_size, "%.1f", cpu_temp * (9.0 / 5.0) + 32.0);
4363 OBJ(i8k_left_fan_status) {
4364 int left_fan_status;
4366 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4367 if (left_fan_status == 0) {
4368 snprintf(p, p_max_size, "off");
4370 if (left_fan_status == 1) {
4371 snprintf(p, p_max_size, "low");
4373 if (left_fan_status == 2) {
4374 snprintf(p, p_max_size, "high");
4377 OBJ(i8k_right_fan_status) {
4378 int right_fan_status;
4380 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4381 if (right_fan_status == 0) {
4382 snprintf(p, p_max_size, "off");
4384 if (right_fan_status == 1) {
4385 snprintf(p, p_max_size, "low");
4387 if (right_fan_status == 2) {
4388 snprintf(p, p_max_size, "high");
4391 OBJ(i8k_left_fan_rpm) {
4392 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4394 OBJ(i8k_right_fan_rpm) {
4395 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4397 OBJ(i8k_ac_status) {
4400 sscanf(i8k.ac_status, "%d", &ac_status);
4401 if (ac_status == -1) {
4402 snprintf(p, p_max_size, "disabled (read i8k docs)");
4404 if (ac_status == 0) {
4405 snprintf(p, p_max_size, "off");
4407 if (ac_status == 1) {
4408 snprintf(p, p_max_size, "on");
4411 OBJ(i8k_buttons_status) {
4412 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4415 get_ibm_acpi_fan(p, p_max_size);
4418 get_ibm_acpi_temps();
4419 snprintf(p, p_max_size, "%d", ibm_acpi.temps[obj->data.sensor]);
4422 get_ibm_acpi_volume(p, p_max_size);
4424 OBJ(ibm_brightness) {
4425 get_ibm_acpi_brightness(p, p_max_size);
4428 if ((obj->data.ifblock.s)
4429 && (!interface_up(obj->data.ifblock.s))) {
4430 i = obj->data.ifblock.pos;
4437 if (!cur->gw_info.count) {
4438 i = obj->data.ifblock.pos;
4445 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4448 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4451 snprintf(p, p_max_size, "%d", get_laptop_mode());
4454 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4456 #endif /* __linux__ */
4459 OBJ(obsd_sensors_temp) {
4460 obsd_sensors.device = sensor_device;
4461 update_obsd_sensors();
4462 snprintf(p, p_max_size, "%.1f",
4463 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor]);
4465 OBJ(obsd_sensors_fan) {
4466 obsd_sensors.device = sensor_device;
4467 update_obsd_sensors();
4468 snprintf(p, p_max_size, "%d",
4469 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4471 OBJ(obsd_sensors_volt) {
4472 obsd_sensors.device = sensor_device;
4473 update_obsd_sensors();
4474 snprintf(p, p_max_size, "%.2f",
4475 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4478 get_obsd_vendor(p, p_max_size);
4481 get_obsd_product(p, p_max_size);
4483 #endif /* __OpenBSD__ */
4487 new_font(p, obj->data.s);
4490 /* TODO: move this correction from kB to kB/s elsewhere
4491 * (or get rid of it??) */
4493 if (obj->data.diskio) {
4495 (obj->data.diskio->current / update_interval) * 1024LL,
4496 p, p_max_size, "diskio");
4498 human_readable(diskio_value * 1024LL, p, p_max_size,
4503 if (obj->data.diskio) {
4504 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
4507 human_readable(diskio_write_value * 1024LL, p, p_max_size,
4512 if (obj->data.diskio) {
4513 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
4516 human_readable(diskio_read_value * 1024LL, p, p_max_size,
4521 if (obj->data.diskio) {
4522 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4523 obj->data.diskio->current, obj->e, 1);
4525 new_graph(p, obj->a, obj->b, obj->c, obj->d, diskio_value,
4529 OBJ(diskiograph_read) {
4530 if (obj->data.diskio) {
4531 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4532 obj->data.diskio->current_read, obj->e, 1);
4534 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4535 diskio_read_value, obj->e, 1);
4538 OBJ(diskiograph_write) {
4539 if (obj->data.diskio) {
4540 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4541 obj->data.diskio->current_write, obj->e, 1);
4543 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4544 diskio_write_value, obj->e, 1);
4548 spaced_print(p, p_max_size, "%d", 6, "downspeed",
4549 round_to_int(obj->data.net->recv_speed / 1024));
4552 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
4553 obj->data.net->recv_speed / 1024.0);
4555 OBJ(downspeedgraph) {
4556 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4557 obj->data.net->recv_speed / 1024.0, obj->e, 1);
4561 i = obj->data.ifblock.pos - 1;
4571 snprintf(p, p_max_size, "%u.%u.%u.%u",
4572 obj->data.net->addr.sa_data[2] & 255,
4573 obj->data.net->addr.sa_data[3] & 255,
4574 obj->data.net->addr.sa_data[4] & 255,
4575 obj->data.net->addr.sa_data[5] & 255);
4578 #if defined(__linux__)
4580 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4582 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4583 strcpy(p, obj->data.net->addrs);
4586 strcpy(p, "0.0.0.0");
4588 #endif /* __linux__ */
4590 #if defined(IMLIB2) && defined(X11)
4595 Imlib_Image image, buffer;
4597 image = imlib_load_image(obj->data.s);
4598 imlib_context_set_image(image);
4602 w = imlib_image_get_width();
4603 h = imlib_image_get_height();
4604 buffer = imlib_create_image(w, h);
4605 imlib_context_set_display(display);
4606 imlib_context_set_drawable(window.drawable);
4607 imlib_context_set_colormap(DefaultColormap(display,
4609 imlib_context_set_visual(DefaultVisual(display,
4611 imlib_context_set_image(buffer);
4612 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
4613 text_start_x, text_start_y, w, h);
4614 imlib_render_image_on_drawable(text_start_x,
4617 imlib_context_set_image(image);
4625 FILE *fp = popen(obj->data.s, "r");
4626 int length = fread(p, 1, p_max_size, fp);
4631 if (length > 0 && p[length - 1] == '\n') {
4632 p[length - 1] = '\0';
4636 FILE *fp = popen(obj->data.s, "r");
4637 fread(p, 1, p_max_size, fp);
4641 struct information *my_info =
4642 malloc(sizeof(struct information));
4643 memcpy(my_info, cur, sizeof(struct information));
4644 struct text_object_list *text_objects = parse_conky_vars(p, p, my_info);
4646 int length = strlen(p);
4649 if (length > 0 && p[length - 1] == '\n') {
4650 p[length - 1] = '\0';
4653 free_text_objects(text_objects->text_object_count, text_objects->text_objects);
4659 FILE *fp = popen(obj->data.s, "r");
4660 int n2 = fread(p, 1, p_max_size, fp);
4664 if (n2 && p[n2 - 1] == '\n') {
4669 if (*p2 == '\001') {
4676 if (sscanf(p, "%lf", &barnum) == 0) {
4677 ERR("reading execbar value failed (perhaps it's not the "
4678 "correct format?)");
4680 if (barnum > 100 || barnum < 0) {
4681 ERR("your execbar value is not between 0 and 100, "
4682 "therefore it will be ignored");
4684 barnum = barnum / 100.0;
4685 new_bar(p, 0, 4, (int) (barnum * 255.0));
4690 FILE *fp = popen(obj->data.s, "r");
4691 int n2 = fread(p, 1, p_max_size, fp);
4695 if (n2 && p[n2 - 1] == '\n') {
4699 if (*p2 == '\001') {
4706 if (sscanf(p, "%lf", &barnum) == 0) {
4707 ERR("reading execgraph value failed (perhaps it's not the "
4708 "correct format?)");
4710 if (barnum > 100 || barnum < 0) {
4711 ERR("your execgraph value is not between 0 and 100, "
4712 "therefore it will be ignored");
4714 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum),
4719 if (current_update_time - obj->data.execi.last_update
4720 < obj->data.execi.interval) {
4721 new_bar(p, 0, 4, (int) obj->f);
4724 FILE *fp = popen(obj->data.execi.cmd, "r");
4725 int n2 = fread(p, 1, p_max_size, fp);
4729 if (n2 && p[n2 - 1] == '\n') {
4734 if (*p2 == '\001') {
4741 if (sscanf(p, "%f", &barnum) == 0) {
4742 ERR("reading execibar value failed (perhaps it's not "
4743 "the correct format?)");
4745 if (barnum > 100 || barnum < 0) {
4746 ERR("your execibar value is not between 0 and 100, "
4747 "therefore it will be ignored");
4749 obj->f = 255 * barnum / 100.0;
4750 new_bar(p, 0, 4, (int) obj->f);
4752 obj->data.execi.last_update = current_update_time;
4756 if (current_update_time - obj->data.execi.last_update
4757 < obj->data.execi.interval) {
4758 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
4761 FILE *fp = popen(obj->data.execi.cmd, "r");
4762 int n2 = fread(p, 1, p_max_size, fp);
4766 if (n2 && p[n2 - 1] == '\n') {
4771 if (*p2 == '\001') {
4778 if (sscanf(p, "%f", &barnum) == 0) {
4779 ERR("reading execigraph value failed (perhaps it's not "
4780 "the correct format?)");
4782 if (barnum > 100 || barnum < 0) {
4783 ERR("your execigraph value is not between 0 and 100, "
4784 "therefore it will be ignored");
4787 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f),
4790 obj->data.execi.last_update = current_update_time;
4794 if (current_update_time - obj->data.execi.last_update
4795 < obj->data.execi.interval
4796 || obj->data.execi.interval == 0) {
4797 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
4799 char *output = obj->data.execi.buffer;
4800 FILE *fp = popen(obj->data.execi.cmd, "r");
4802 int length = fread(output, 1, text_buffer_size, fp);
4805 output[length] = '\0';
4806 if (length > 0 && output[length - 1] == '\n') {
4807 output[length - 1] = '\0';
4809 obj->data.execi.last_update = current_update_time;
4810 snprintf(p, p_max_size, "%s", output);
4812 // parse_conky_vars(output, p, cur);
4815 struct information *my_info =
4816 malloc(sizeof(struct information));
4817 memcpy(my_info, cur, sizeof(struct information));
4818 struct text_object_list *text_objects = 0;
4820 if (current_update_time - obj->data.execi.last_update
4821 < obj->data.execi.interval
4822 || obj->data.execi.interval == 0) {
4823 text_objects = parse_conky_vars(obj->data.execi.buffer, p, my_info);
4825 char *output = obj->data.execi.buffer;
4826 FILE *fp = popen(obj->data.execi.cmd, "r");
4827 int length = fread(output, 1, text_buffer_size, fp);
4831 output[length] = '\0';
4832 if (length > 0 && output[length - 1] == '\n') {
4833 output[length - 1] = '\0';
4836 text_objects = parse_conky_vars(obj->data.execi.buffer, p, my_info);
4837 obj->data.execi.last_update = current_update_time;
4839 free_text_objects(text_objects->text_object_count, text_objects->text_objects);
4844 if (!obj->data.texeci.p_timed_thread) {
4845 obj->data.texeci.p_timed_thread =
4846 timed_thread_create((void *) threaded_exec,
4847 (void *) obj, obj->data.texeci.interval * 1000000);
4848 if (!obj->data.texeci.p_timed_thread) {
4849 ERR("Error creating texeci timed thread");
4851 timed_thread_register(obj->data.texeci.p_timed_thread,
4852 &obj->data.texeci.p_timed_thread);
4853 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4854 ERR("Error running texeci timed thread");
4857 timed_thread_lock(obj->data.texeci.p_timed_thread);
4858 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
4859 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4861 #endif /* HAVE_POPEN */
4863 if (obj->global_mode && info.mail) {
4864 // this means we use info
4865 if (!info.mail->p_timed_thread) {
4866 info.mail->p_timed_thread =
4867 timed_thread_create((void *) imap_thread,
4868 (void *) info.mail, info.mail->interval * 1000000);
4869 if (!info.mail->p_timed_thread) {
4870 ERR("Error creating imap timed thread");
4872 timed_thread_register(info.mail->p_timed_thread,
4873 &info.mail->p_timed_thread);
4874 if (timed_thread_run(info.mail->p_timed_thread)) {
4875 ERR("Error running imap timed thread");
4878 timed_thread_lock(info.mail->p_timed_thread);
4879 snprintf(p, p_max_size, "%lu", info.mail->unseen);
4880 timed_thread_unlock(info.mail->p_timed_thread);
4881 } else if (obj->data.mail) {
4882 // this means we use obj
4883 if (!obj->data.mail->p_timed_thread) {
4884 obj->data.mail->p_timed_thread =
4885 timed_thread_create((void *) imap_thread,
4886 (void *) obj->data.mail,
4887 obj->data.mail->interval * 1000000);
4888 if (!obj->data.mail->p_timed_thread) {
4889 ERR("Error creating imap timed thread");
4891 timed_thread_register(obj->data.mail->p_timed_thread,
4892 &obj->data.mail->p_timed_thread);
4893 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4894 ERR("Error running imap timed thread");
4897 timed_thread_lock(obj->data.mail->p_timed_thread);
4898 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
4899 timed_thread_unlock(obj->data.mail->p_timed_thread);
4900 } else if (!obj->a) {
4901 // something is wrong, warn once then stop
4902 ERR("Theres a problem with your imap_unseen settings. "
4903 "Check that the global IMAP settings are defined "
4904 "properly (line %li).", obj->line);
4908 OBJ(imap_messages) {
4909 if (obj->global_mode && info.mail) {
4910 // this means we use info
4911 if (!info.mail->p_timed_thread) {
4912 info.mail->p_timed_thread =
4913 timed_thread_create((void *) imap_thread,
4914 (void *) info.mail, info.mail->interval * 1000000);
4915 if (!info.mail->p_timed_thread) {
4916 ERR("Error creating imap timed thread");
4918 timed_thread_register(info.mail->p_timed_thread,
4919 &info.mail->p_timed_thread);
4920 if (timed_thread_run(info.mail->p_timed_thread)) {
4921 ERR("Error running imap timed thread");
4924 timed_thread_lock(info.mail->p_timed_thread);
4925 snprintf(p, p_max_size, "%lu", info.mail->messages);
4926 timed_thread_unlock(info.mail->p_timed_thread);
4927 } else if (obj->data.mail) {
4928 // this means we use obj
4929 if (!obj->data.mail->p_timed_thread) {
4930 obj->data.mail->p_timed_thread =
4931 timed_thread_create((void *) imap_thread,
4932 (void *) obj->data.mail,
4933 obj->data.mail->interval * 1000000);
4934 if (!obj->data.mail->p_timed_thread) {
4935 ERR("Error creating imap timed thread");
4937 timed_thread_register(obj->data.mail->p_timed_thread,
4938 &obj->data.mail->p_timed_thread);
4939 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4940 ERR("Error runninging imap timed thread");
4943 timed_thread_lock(obj->data.mail->p_timed_thread);
4944 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
4945 timed_thread_lock(obj->data.mail->p_timed_thread);
4946 } else if (!obj->a) {
4947 // something is wrong, warn once then stop
4948 ERR("Theres a problem with your imap_messages settings. "
4949 "Check that the global IMAP settings are defined "
4950 "properly (line %li).", obj->line);
4955 if (obj->global_mode && info.mail) {
4956 // this means we use info
4957 if (!info.mail->p_timed_thread) {
4958 info.mail->p_timed_thread =
4959 timed_thread_create((void *) pop3_thread,
4960 (void *) info.mail, info.mail->interval * 1000000);
4961 if (!info.mail->p_timed_thread) {
4962 ERR("Error creating pop3 timed thread");
4964 timed_thread_register(info.mail->p_timed_thread,
4965 &info.mail->p_timed_thread);
4966 if (timed_thread_run(info.mail->p_timed_thread)) {
4967 ERR("Error running pop3 timed thread");
4970 timed_thread_lock(info.mail->p_timed_thread);
4971 snprintf(p, p_max_size, "%lu", info.mail->unseen);
4972 timed_thread_unlock(info.mail->p_timed_thread);
4973 } else if (obj->data.mail) {
4974 // this means we use obj
4975 if (!obj->data.mail->p_timed_thread) {
4976 obj->data.mail->p_timed_thread =
4977 timed_thread_create((void *) pop3_thread,
4978 (void *) obj->data.mail,
4979 obj->data.mail->interval * 1000000);
4980 if (!obj->data.mail->p_timed_thread) {
4981 ERR("Error creating pop3 timed thread");
4983 timed_thread_register(obj->data.mail->p_timed_thread,
4984 &obj->data.mail->p_timed_thread);
4985 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4986 ERR("Error running pop3 timed thread");
4989 timed_thread_lock(obj->data.mail->p_timed_thread);
4990 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
4991 timed_thread_unlock(obj->data.mail->p_timed_thread);
4992 } else if (!obj->a) {
4993 // something is wrong, warn once then stop
4994 ERR("Theres a problem with your pop3_unseen settings. "
4995 "Check that the global POP3 settings are defined "
4996 "properly (line %li).", obj->line);
5001 if (obj->global_mode && info.mail) {
5002 // this means we use info
5003 if (!info.mail->p_timed_thread) {
5004 info.mail->p_timed_thread =
5005 timed_thread_create((void *) pop3_thread,
5006 (void *) info.mail, info.mail->interval * 1000000);
5007 if (!info.mail->p_timed_thread) {
5008 ERR("Error creating pop3 timed thread");
5010 timed_thread_register(info.mail->p_timed_thread,
5011 &info.mail->p_timed_thread);
5012 if (timed_thread_run(info.mail->p_timed_thread)) {
5013 ERR("Error running pop3 timed thread");
5016 timed_thread_lock(info.mail->p_timed_thread);
5017 snprintf(p, p_max_size, "%.1f",
5018 info.mail->used / 1024.0 / 1024.0);
5019 timed_thread_unlock(info.mail->p_timed_thread);
5020 } else if (obj->data.mail) {
5021 // this means we use obj
5022 if (!obj->data.mail->p_timed_thread) {
5023 obj->data.mail->p_timed_thread =
5024 timed_thread_create((void *) pop3_thread,
5025 (void *) obj->data.mail,
5026 obj->data.mail->interval * 1000000);
5027 if (!obj->data.mail->p_timed_thread) {
5028 ERR("Error creating pop3 timed thread");
5030 timed_thread_register(obj->data.mail->p_timed_thread,
5031 &obj->data.mail->p_timed_thread);
5032 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
5033 ERR("Error running pop3 timed thread");
5036 timed_thread_lock(obj->data.mail->p_timed_thread);
5037 snprintf(p, p_max_size, "%.1f",
5038 obj->data.mail->used / 1024.0 / 1024.0);
5039 timed_thread_unlock(obj->data.mail->p_timed_thread);
5040 } else if (!obj->a) {
5041 // something is wrong, warn once then stop
5042 ERR("Theres a problem with your pop3_used settings. "
5043 "Check that the global POP3 settings are defined "
5044 "properly (line %li).", obj->line);
5049 if (obj->data.fs != NULL) {
5050 if (obj->data.fs->size == 0) {
5051 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5053 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5054 (int) (255 - obj->data.fsbar.fs->avail * 255 /
5055 obj->data.fs->size));
5060 if (obj->data.fs != NULL) {
5061 human_readable(obj->data.fs->avail, p, 255, "fs_free");
5065 if (obj->data.fs != NULL) {
5066 if (obj->data.fs->size) {
5067 spaced_print(p, p_max_size, "%*d", pad_percents,
5068 "fs_free_perc", (int) ((obj->data.fs->avail * 100) /
5069 obj->data.fs->size));
5071 snprintf(p, p_max_size, "0");
5076 if (obj->data.fs != NULL) {
5077 human_readable(obj->data.fs->size, p, 255, "fs_size");
5081 if (obj->data.fs != NULL)
5082 snprintf(p, p_max_size, "%s", obj->data.fs->type);
5085 if (obj->data.fs != NULL) {
5086 human_readable(obj->data.fs->size - (obj->data.fs->free
5087 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
5092 if (obj->data.fs != NULL) {
5093 if (obj->data.fs->size == 0) {
5094 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5096 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5097 (int) (obj->data.fsbar.fs->avail * 255 /
5098 obj->data.fs->size));
5103 if (obj->data.fs != NULL) {
5104 if (obj->data.fs->size) {
5105 spaced_print(p, 4, "%*d", pad_percents,
5106 "fs_used_perc", 100 - ((int) ((obj->data.fs->avail * 100) /
5107 obj->data.fs->size)));
5109 snprintf(p, p_max_size, "0");
5114 float *v = info.loadavg;
5116 if (obj->data.loadavg[2]) {
5117 snprintf(p, p_max_size, "%.2f %.2f %.2f",
5118 v[obj->data.loadavg[0] - 1],
5119 v[obj->data.loadavg[1] - 1],
5120 v[obj->data.loadavg[2] - 1]);
5121 } else if (obj->data.loadavg[1]) {
5122 snprintf(p, p_max_size, "%.2f %.2f",
5123 v[obj->data.loadavg[0] - 1],
5124 v[obj->data.loadavg[1] - 1]);
5125 } else if (obj->data.loadavg[0]) {
5126 snprintf(p, p_max_size, "%.2f",
5127 v[obj->data.loadavg[0] - 1]);
5131 new_goto(p, obj->data.i);
5134 new_tab(p, obj->data.pair.a, obj->data.pair.b);
5137 new_hr(p, obj->data.i);
5140 if (cur->nameserver_info.nscount > obj->data.i)
5141 snprintf(p, p_max_size, "%s",
5142 cur->nameserver_info.ns_list[obj->data.i]);
5146 PRSS *data = get_rss_info(obj->data.rss.uri,
5147 obj->data.rss.delay);
5151 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
5153 if (!strcmp(obj->data.rss.action, "feed_title")) {
5155 // remove trailing new line if one exists
5156 if (str[strlen(str) - 1] == '\n') {
5157 str[strlen(str) - 1] = 0;
5159 snprintf(p, p_max_size, "%s", str);
5160 } else if (!strcmp(obj->data.rss.action, "item_title")) {
5161 if (obj->data.rss.act_par < data->item_count) {
5162 str = data->items[obj->data.rss.act_par].title;
5163 // remove trailing new line if one exists
5164 if (str[strlen(str) - 1] == '\n') {
5165 str[strlen(str) - 1] = 0;
5167 snprintf(p, p_max_size, "%s", str);
5169 } else if (!strcmp(obj->data.rss.action, "item_desc")) {
5170 if (obj->data.rss.act_par < data->item_count) {
5172 data->items[obj->data.rss.act_par].description;
5173 // remove trailing new line if one exists
5174 if (str[strlen(str) - 1] == '\n') {
5175 str[strlen(str) - 1] = 0;
5177 snprintf(p, p_max_size, "%s", str);
5179 } else if (!strcmp(obj->data.rss.action, "item_titles")) {
5180 if (data->item_count > 0) {
5186 if (obj->data.rss.act_par > data->item_count) {
5187 show = data->item_count;
5189 show = obj->data.rss.act_par;
5191 for (itmp = 0; itmp < show; itmp++) {
5192 PRSS_Item *item = &data->items[itmp];
5196 // don't add new line before first item
5198 strncat(p, "\n", p_max_size);
5200 /* remove trailing new line if one exists,
5201 * we have our own */
5202 if (str[strlen(str) - 1] == '\n') {
5203 str[strlen(str) - 1] = 0;
5205 strncat(p, str, p_max_size);
5218 temp = get_hddtemp_info(obj->data.hddtemp.dev,
5219 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
5221 snprintf(p, p_max_size, "N/A");
5222 } else if (unit == '*') {
5223 snprintf(p, p_max_size, "%s", temp);
5225 snprintf(p, p_max_size, "%s°%c", temp, unit);
5230 new_offset(p, obj->data.i);
5233 new_voffset(p, obj->data.i);
5239 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5240 obj->data.sysfs.devtype, obj->data.sysfs.type);
5242 if (r >= 100.0 || r == 0) {
5243 snprintf(p, p_max_size, "%d", (int) r);
5245 snprintf(p, p_max_size, "%.1f", r);
5251 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5252 obj->data.sysfs.devtype, obj->data.sysfs.type);
5254 if (r >= 100.0 || r == 0) {
5255 snprintf(p, p_max_size, "%d", (int) r);
5257 snprintf(p, p_max_size, "%.1f", r);
5263 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5264 obj->data.sysfs.devtype, obj->data.sysfs.type);
5266 if (r >= 100.0 || r == 0) {
5267 snprintf(p, p_max_size, "%d", (int) r);
5269 snprintf(p, p_max_size, "%.1f", r);
5272 #endif /* !__OpenBSD__ */
5274 new_alignr(p, obj->data.i);
5277 new_alignc(p, obj->data.i);
5280 struct information *my_info =
5281 malloc(sizeof(struct information));
5283 memcpy(my_info, cur, sizeof(struct information));
5284 struct text_object_list *text_objects = parse_conky_vars(obj->data.ifblock.s, p, my_info);
5285 if (strlen(p) != 0) {
5286 i = obj->data.ifblock.pos;
5292 free_text_objects(text_objects->text_object_count, text_objects->text_objects);
5299 if ((obj->data.ifblock.s)
5300 && (stat(obj->data.ifblock.s, &tmp) == -1)) {
5301 i = obj->data.ifblock.pos;
5304 if (obj->data.ifblock.str) {
5305 if (!check_contains(obj->data.ifblock.s,
5306 obj->data.ifblock.str)) {
5307 i = obj->data.ifblock.pos;
5318 if ((obj->data.ifblock.s)
5319 && (!check_mount(obj->data.ifblock.s))) {
5320 i = obj->data.ifblock.pos;
5327 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
5328 i = obj->data.ifblock.pos;
5335 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
5338 snprintf(p, p_max_size, "%s", cur->uname_s.release);
5341 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
5346 human_readable(cur->mem * 1024, p, 255, "mem");
5349 human_readable(cur->memmax * 1024, p, 255, "memmax");
5353 spaced_print(p, p_max_size, "%*Lu", 4, "memperc",
5354 pad_percents, cur->mem * 100 / cur->memmax);
5358 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5359 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5362 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5363 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5369 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
5372 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
5375 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
5378 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5379 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
5382 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5383 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
5386 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5387 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
5392 update_mail_count(&obj->data.local_mail);
5393 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5396 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5398 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5401 update_mail_count(&obj->data.local_mail);
5402 snprintf(p, p_max_size, "%d",
5403 obj->data.local_mail.new_mail_count);
5406 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5409 new_outline(p, obj->data.l);
5412 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
5414 OBJ(running_processes) {
5415 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
5418 snprintf(p, p_max_size, "%s", obj->data.s);
5421 new_bg(p, obj->data.l);
5424 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5427 human_readable(cur->swap * 1024, p, 255, "swap");
5430 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
5433 if (cur->swapmax == 0) {
5434 strncpy(p, "No swap", p_max_size);
5436 spaced_print(p, p_max_size, "%*Lu", 4, "swapperc",
5437 pad_percents, cur->swap * 100 / cur->swapmax);
5441 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5442 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5445 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5448 time_t t = time(NULL);
5449 struct tm *tm = localtime(&t);
5451 setlocale(LC_TIME, "");
5452 strftime(p, p_max_size, obj->data.s, tm);
5455 time_t t = time(NULL);
5456 struct tm *tm = gmtime(&t);
5458 strftime(p, p_max_size, obj->data.s, tm);
5463 if (obj->data.tztime.tz) {
5464 oldTZ = getenv("TZ");
5465 setenv("TZ", obj->data.tztime.tz, 1);
5468 time_t t = time(NULL);
5469 struct tm *tm = localtime(&t);
5471 setlocale(LC_TIME, "");
5472 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5474 setenv("TZ", oldTZ, 1);
5479 // Needless to free oldTZ since getenv gives ptr to static data
5482 human_readable(obj->data.net->recv, p, 255, "totaldown");
5485 human_readable(obj->data.net->trans, p, 255, "totalup");
5488 snprintf(p, p_max_size, "%d", total_updates);
5491 spaced_print(p, p_max_size, "%d", 6, "upspeed",
5492 round_to_int(obj->data.net->trans_speed / 1024));
5495 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
5496 obj->data.net->trans_speed / 1024.0);
5499 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5500 obj->data.net->trans_speed / 1024.0, obj->e, 1);
5503 format_seconds_short(p, p_max_size, (int) cur->uptime);
5506 format_seconds(p, p_max_size, (int) cur->uptime);
5509 snprintf(p, p_max_size, "%s", cur->users.names);
5512 snprintf(p, p_max_size, "%s", cur->users.terms);
5515 snprintf(p, p_max_size, "%s", cur->users.times);
5518 snprintf(p, p_max_size, "%d", cur->users.number);
5520 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5521 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5525 msg = get_apm_adapter();
5526 snprintf(p, p_max_size, "%s", msg);
5529 OBJ(apm_battery_life) {
5532 msg = get_apm_battery_life();
5533 snprintf(p, p_max_size, "%s", msg);
5536 OBJ(apm_battery_time) {
5539 msg = get_apm_battery_time();
5540 snprintf(p, p_max_size, "%s", msg);
5543 #endif /* __FreeBSD__ __OpenBSD__ */
5547 snprintf(p, cur->mpd.max_title_len > 0
5548 ? cur->mpd.max_title_len : p_max_size, "%s",
5552 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5555 snprintf(p, p_max_size, "%s", cur->mpd.album);
5558 snprintf(p, p_max_size, "%s", cur->mpd.random);
5561 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
5564 snprintf(p, p_max_size, "%s", cur->mpd.track);
5567 snprintf(p, p_max_size, "%s", cur->mpd.name);
5570 snprintf(p, p_max_size, "%s", cur->mpd.file);
5573 snprintf(p, p_max_size, "%i", cur->mpd.volume);
5576 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
5579 snprintf(p, p_max_size, "%s", cur->mpd.status);
5582 int days = 0, hours = 0, minutes = 0, seconds = 0;
5583 int tmp = cur->mpd.elapsed;
5585 while (tmp >= 86400) {
5589 while (tmp >= 3600) {
5599 snprintf(p, p_max_size, "%i days %i:%02i:%02i", days,
5600 hours, minutes, seconds);
5601 } else if (hours > 0) {
5602 snprintf(p, p_max_size, "%i:%02i:%02i", hours, minutes,
5605 snprintf(p, p_max_size, "%i:%02i", minutes, seconds);
5609 int days = 0, hours = 0, minutes = 0, seconds = 0;
5610 int tmp = cur->mpd.length;
5612 while (tmp >= 86400) {
5616 while (tmp >= 3600) {
5626 snprintf(p, p_max_size, "%i days %i:%02i:%02i", days,
5627 hours, minutes, seconds);
5628 } else if (hours > 0) {
5629 snprintf(p, p_max_size, "%i:%02i:%02i", hours, minutes,
5632 snprintf(p, p_max_size, "%i:%02i", minutes, seconds);
5636 spaced_print(p, p_max_size, "%*d", pad_percents,
5637 "mpd_percent", (int) (cur->mpd.progress * 100));
5640 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5641 (int) (cur->mpd.progress * 255.0f));
5644 memset(p, 0, p_max_size);
5645 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
5646 && *cur->mpd.title) {
5647 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist,
5649 } else if (cur->mpd.title && *cur->mpd.title) {
5650 snprintf(p, p_max_size, "%s", cur->mpd.title);
5651 } else if (cur->mpd.artist && *cur->mpd.artist) {
5652 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5653 } else if (cur->mpd.file && *cur->mpd.file) {
5654 snprintf(p, p_max_size, "%s", cur->mpd.file);
5663 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5666 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5669 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5672 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5674 OBJ(xmms2_comment) {
5675 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5678 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5681 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5684 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5686 OBJ(xmms2_tracknr) {
5687 if (cur->xmms2.tracknr != -1) {
5688 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5691 OBJ(xmms2_bitrate) {
5692 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5695 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5698 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5700 OBJ(xmms2_elapsed) {
5701 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5702 (cur->xmms2.elapsed / 1000) % 60);
5704 OBJ(xmms2_duration) {
5705 snprintf(p, p_max_size, "%02d:%02d",
5706 cur->xmms2.duration / 60000,
5707 (cur->xmms2.duration / 1000) % 60);
5709 OBJ(xmms2_percent) {
5710 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5713 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5714 (int) (cur->xmms2.progress * 255.0f));
5716 OBJ(xmms2_playlist) {
5717 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5719 OBJ(xmms2_timesplayed) {
5720 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5723 if (strlen(cur->xmms2.title) < 2
5724 && strlen(cur->xmms2.title) < 2) {
5725 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5727 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5733 OBJ(audacious_status) {
5734 snprintf(p, p_max_size, "%s",
5735 cur->audacious.items[AUDACIOUS_STATUS]);
5737 OBJ(audacious_title) {
5738 snprintf(p, cur->audacious.max_title_len > 0
5739 ? cur->audacious.max_title_len : p_max_size, "%s",
5740 cur->audacious.items[AUDACIOUS_TITLE]);
5742 OBJ(audacious_length) {
5743 snprintf(p, p_max_size, "%s",
5744 cur->audacious.items[AUDACIOUS_LENGTH]);
5746 OBJ(audacious_length_seconds) {
5747 snprintf(p, p_max_size, "%s",
5748 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5750 OBJ(audacious_position) {
5751 snprintf(p, p_max_size, "%s",
5752 cur->audacious.items[AUDACIOUS_POSITION]);
5754 OBJ(audacious_position_seconds) {
5755 snprintf(p, p_max_size, "%s",
5756 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5758 OBJ(audacious_bitrate) {
5759 snprintf(p, p_max_size, "%s",
5760 cur->audacious.items[AUDACIOUS_BITRATE]);
5762 OBJ(audacious_frequency) {
5763 snprintf(p, p_max_size, "%s",
5764 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5766 OBJ(audacious_channels) {
5767 snprintf(p, p_max_size, "%s",
5768 cur->audacious.items[AUDACIOUS_CHANNELS]);
5770 OBJ(audacious_filename) {
5771 snprintf(p, p_max_size, "%s",
5772 cur->audacious.items[AUDACIOUS_FILENAME]);
5774 OBJ(audacious_playlist_length) {
5775 snprintf(p, p_max_size, "%s",
5776 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5778 OBJ(audacious_playlist_position) {
5779 snprintf(p, p_max_size, "%s",
5780 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5782 OBJ(audacious_bar) {
5786 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5787 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5788 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5794 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5797 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5800 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5803 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5806 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5809 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5813 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5816 switch (obj->data.top.type) {
5818 snprintf(p, 16, "%-15s",
5819 cur->cpu[obj->data.top.num]->name);
5822 snprintf(p, 7, "%6.2f",
5823 cur->cpu[obj->data.top.num]->amount);
5826 snprintf(p, 6, "%5i",
5827 cur->cpu[obj->data.top.num]->pid);
5830 snprintf(p, 7, "%6.2f",
5831 cur->cpu[obj->data.top.num]->totalmem);
5835 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
5836 snprintf(p, 10, "%9s", time);
5840 ERR("Unhandled top data type: %d\n",
5841 obj->data.top.type);
5844 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5848 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5851 switch (obj->data.top.type) {
5853 snprintf(p, 16, "%-15s",
5854 cur->memu[obj->data.top.num]->name);
5857 snprintf(p, 7, "%6.2f",
5858 cur->memu[obj->data.top.num]->amount);
5861 snprintf(p, 6, "%5i",
5862 cur->memu[obj->data.top.num]->pid);
5865 snprintf(p, 7, "%6.2f",
5866 cur->memu[obj->data.top.num]->totalmem);
5870 cur->memu[obj->data.top.num]->total_cpu_time,
5872 snprintf(p, 10, "%9s", time);
5876 ERR("Unhandled top data type: %d\n",
5877 obj->data.top.type);
5880 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5884 if (current_update_time - obj->data.tail.last_update
5885 < obj->data.tail.interval) {
5886 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5888 obj->data.tail.last_update = current_update_time;
5893 if (obj->data.tail.fd != -1) {
5894 tail_pipe(obj, p, p_max_size);
5898 fp = fopen(obj->data.tail.logfile, "rt");
5901 /* Send one message, but do not consistently spam
5902 * on missing logfiles. */
5903 if (obj->data.tail.readlines != 0) {
5904 ERR("tail logfile failed to open");
5905 strcpy(obj->data.tail.buffer, "Logfile Missing");
5907 obj->data.tail.readlines = 0;
5908 snprintf(p, p_max_size, "Logfile Missing");
5910 obj->data.tail.readlines = 0;
5911 /* -1 instead of 0 to avoid counting a trailing
5913 fseek(fp, -1, SEEK_END);
5914 bsize = ftell(fp) + 1;
5915 for (iter = obj->data.tail.wantedlines; iter > 0;
5917 nl = rev_fcharfind(fp, '\n', iter);
5922 obj->data.tail.readlines = iter;
5923 if (obj->data.tail.readlines
5924 < obj->data.tail.wantedlines) {
5925 fseek(fp, 0, SEEK_SET);
5927 fseek(fp, nl + 1, SEEK_SET);
5930 /* Make sure bsize is at least 1 byte smaller than the
5931 * buffer max size. */
5932 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
5933 fseek(fp, bsize - text_buffer_size * 20 - 1,
5935 bsize = text_buffer_size * 20 - 1;
5937 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
5940 /* Clean up trailing newline, make sure the
5941 * buffer is null terminated. */
5942 if (obj->data.tail.buffer[bsize - 1] == '\n') {
5943 obj->data.tail.buffer[bsize - 1] = '\0';
5945 obj->data.tail.buffer[bsize] = '\0';
5947 snprintf(p, p_max_size, "%s",
5948 obj->data.tail.buffer);
5950 strcpy(obj->data.tail.buffer, "Logfile Empty");
5951 snprintf(p, p_max_size, "Logfile Empty");
5954 } /* if cur_upd_time >= */
5955 // parse_conky_vars(obj->data.tail.buffer, p, cur);
5960 if (current_update_time - obj->data.tail.last_update
5961 < obj->data.tail.interval) {
5962 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5964 obj->data.tail.last_update = current_update_time;
5969 fp = fopen(obj->data.tail.logfile, "rt");
5971 /* Send one message, but do not consistently spam
5972 * on missing logfiles. */
5973 if (obj->data.tail.readlines != 0) {
5974 ERR("head logfile failed to open");
5975 strcpy(obj->data.tail.buffer, "Logfile Missing");
5977 obj->data.tail.readlines = 0;
5978 snprintf(p, p_max_size, "Logfile Missing");
5980 obj->data.tail.readlines = 0;
5981 for (iter = obj->data.tail.wantedlines; iter > 0;
5983 nl = fwd_fcharfind(fp, '\n', iter);
5988 obj->data.tail.readlines = iter;
5989 /* Make sure nl is at least 1 byte smaller than the
5990 * buffer max size. */
5991 if (nl > (long) ((text_buffer_size * 20) - 1)) {
5992 nl = text_buffer_size * 20 - 1;
5994 nl = fread(obj->data.tail.buffer, 1, nl, fp);
5997 /* Clean up trailing newline, make sure the buffer
5998 * is null terminated. */
5999 if (obj->data.tail.buffer[nl - 1] == '\n') {
6000 obj->data.tail.buffer[nl - 1] = '\0';
6002 obj->data.tail.buffer[nl] = '\0';
6004 snprintf(p, p_max_size, "%s",
6005 obj->data.tail.buffer);
6007 strcpy(obj->data.tail.buffer, "Logfile Empty");
6008 snprintf(p, p_max_size, "Logfile Empty");
6010 } /* if fp == null */
6011 } /* cur_upd_time >= */
6012 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6015 #ifdef TCP_PORT_MONITOR
6017 /* grab a pointer to this port monitor */
6018 tcp_port_monitor_t *p_monitor =
6019 find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
6020 obj->data.tcp_port_monitor.port_range_begin,
6021 obj->data.tcp_port_monitor.port_range_end);
6024 snprintf(p, p_max_size, "monitor not found");
6028 /* now grab the text of interest */
6029 if (peek_tcp_port_monitor(p_monitor,
6030 obj->data.tcp_port_monitor.item,
6031 obj->data.tcp_port_monitor.connection_index, p,
6033 snprintf(p, p_max_size, "monitor peek error");
6041 iconv_converting = 1;
6042 iconv_selected = obj->a;
6045 iconv_converting = 0;
6050 OBJ(entropy_avail) {
6051 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
6053 OBJ(entropy_poolsize) {
6054 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
6057 double entropy_perc;
6059 entropy_perc = (double) cur->entropy.entropy_avail /
6060 (double) cur->entropy.poolsize;
6061 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
6067 s = smapi_get_val(obj->data.s);
6068 snprintf(p, p_max_size, "%s", s);
6072 OBJ(if_smapi_bat_installed) {
6074 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
6075 if(!smapi_bat_installed(idx)) {
6076 i = obj->data.ifblock.pos;
6081 ERR("argument to if_smapi_bat_installed must be an integer");
6083 OBJ(smapi_bat_perc) {
6085 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6086 val = smapi_bat_installed(idx) ?
6087 smapi_get_bat_int(idx, "remaining_percent") : 0;
6088 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
6090 ERR("argument to smapi_bat_perc must be an integer");
6092 OBJ(smapi_bat_bar) {
6093 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
6094 new_bar(p, obj->a, obj->b, (int)
6095 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
6097 new_bar(p, obj->a, obj->b, 0);
6105 unsigned int a = strlen(p);
6108 if (a > 0 && iconv_converting && iconv_selected > 0
6109 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6111 size_t dummy1, dummy2;
6112 char *ptr = buff_in;
6115 dummy1 = dummy2 = a;
6117 strncpy(buff_in, p, p_max_size);
6119 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6120 while (dummy1 > 0) {
6121 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6124 ERR("Iconv codeset conversion failed");
6129 /* It is nessecary when we are converting from multibyte to
6130 * singlebyte codepage */
6133 #endif /* HAVE_ICONV */
6140 double current_update_time, last_update_time;
6142 static void generate_text()
6144 struct information *cur = &info;
6151 current_update_time = get_time();
6154 /* fix diskio rates to b/s (use update_interval */
6155 diskio_read_value = diskio_read_value / update_interval;
6156 diskio_write_value = diskio_write_value / update_interval;
6157 diskio_value = diskio_value / update_interval;
6159 /* add things to the buffer */
6165 generate_text_internal(p, max_user_text, text_objects, text_object_count, cur);
6167 if (stuff_in_upper_case) {
6177 last_update_time = current_update_time;
6183 static void set_font()
6187 if (window.xftdraw != NULL) {
6188 XftDrawDestroy(window.xftdraw);
6190 window.xftdraw = XftDrawCreate(display, window.drawable,
6191 DefaultVisual(display, screen), DefaultColormap(display, screen));
6195 XSetFont(display, window.gc, fonts[selected_font].font->fid);
6201 static inline int get_string_width(const char *s)
6204 return *s ? calc_text_width(s, strlen(s)) : 0;
6210 static inline int get_string_width_special(char *s)
6225 if (*p == SPECIAL_CHAR) {
6226 /* shift everything over by 1 so that the special char
6227 * doesn't mess up the size calculation */
6228 for (i = 0; i < strlen(p); i++) {
6229 *(p + i) = *(p + i + 1);
6231 if (specials[special_index + index].type == GRAPH
6232 || specials[special_index + index].type == BAR) {
6233 width += specials[special_index + index].width;
6240 if (strlen(final) > 1) {
6241 width += calc_text_width(final, strlen(final));
6251 static void text_size_updater(char *s);
6253 int last_font_height;
6254 static void update_text_area()
6258 /* update text size if it isn't fixed */
6263 text_width = minimum_width;
6266 last_font_height = font_height();
6267 for_each_line(text_buffer, text_size_updater);
6269 if (text_height < minimum_height) {
6270 text_height = minimum_height;
6272 if (text_width > maximum_width && maximum_width > 0) {
6273 text_width = maximum_width;
6277 /* get text position on workarea */
6278 switch (text_alignment) {
6285 x = workarea[2] - text_width - gap_x;
6290 x = workarea[2] / 2 - text_width / 2 - gap_x;
6297 y = workarea[3] - text_height - gap_y;
6301 x = workarea[2] - text_width - gap_x;
6302 y = workarea[3] - text_height - gap_y;
6306 x = workarea[2] / 2 - text_width / 2 - gap_x;
6307 y = workarea[3] - text_height - gap_y;
6312 y = workarea[3] / 2 - text_height / 2 - gap_y;
6316 x = workarea[2] - text_width - gap_x;
6317 y = workarea[3] / 2 - text_height / 2 - gap_y;
6321 case NONE: // Let the WM manage the window
6332 if (own_window && !fixed_pos) {
6335 text_start_x = border_margin + 1;
6336 text_start_y = border_margin + 1;
6337 window.x = x - border_margin - 1;
6338 window.y = y - border_margin - 1;
6342 /* If window size doesn't match to workarea's size,
6343 * then window probably includes panels (gnome).
6344 * Blah, doesn't work on KDE. */
6345 if (workarea[2] != window.width || workarea[3] != window.height) {
6357 static int cur_x, cur_y; /* current x and y for drawing */
6358 static int draw_mode; /* FG, BG or OUTLINE */
6359 static long current_color;
6362 static void text_size_updater(char *s)
6367 /* get string widths and skip specials */
6370 if (*p == SPECIAL_CHAR) {
6372 w += get_string_width(s);
6375 if (specials[special_index].type == BAR
6376 || specials[special_index].type == GRAPH) {
6377 w += specials[special_index].width;
6378 if (specials[special_index].height > last_font_height) {
6379 last_font_height = specials[special_index].height;
6380 last_font_height += font_ascent();
6382 } else if (specials[special_index].type == OFFSET) {
6383 if (specials[special_index].arg > 0) {
6384 w += specials[special_index].arg;
6386 } else if (specials[special_index].type == VOFFSET) {
6387 last_font_height += specials[special_index].arg;
6388 } else if (specials[special_index].type == GOTO) {
6389 if (specials[special_index].arg > cur_x) {
6390 w = (int) specials[special_index].arg;
6392 } else if (specials[special_index].type == TAB) {
6393 int start = specials[special_index].arg;
6394 int step = specials[special_index].width;
6396 if (!step || step < 0) {
6399 w += step - (cur_x - text_start_x - start) % step;
6400 } else if (specials[special_index].type == FONT) {
6401 selected_font = specials[special_index].font_added;
6402 if (font_height() > last_font_height) {
6403 last_font_height = font_height();
6412 w += get_string_width(s);
6413 if (w > text_width) {
6416 if (text_width > maximum_width && maximum_width) {
6417 text_width = maximum_width;
6420 text_height += last_font_height;
6421 last_font_height = font_height();
6425 static inline void set_foreground_color(long c)
6428 XSetForeground(display, window.gc, c);
6432 static void draw_string(const char *s)
6437 int i, i2, pos, width_of_s;
6441 width_of_s = get_string_width(s);
6442 if (out_to_console) {
6444 fflush(stdout); /* output immediately, don't buffer */
6446 memset(tmpstring1, 0, text_buffer_size);
6447 memset(tmpstring2, 0, text_buffer_size);
6448 strncpy(tmpstring1, s, text_buffer_size - 1);
6453 snprintf(space, 2, " ");
6455 max = ((text_width - width_of_s) / get_string_width(space));
6457 /* This code looks for tabs in the text and coverts them to spaces.
6458 * The trick is getting the correct number of spaces, and not going
6459 * over the window's size without forcing the window larger. */
6460 for (i = 0; i < (int)text_buffer_size; i++) {
6461 if (tmpstring1[i] == '\t') { // 9 is ascii tab
6463 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6464 /* guard against overrun */
6465 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6470 if (tmpstring1[i] != 9) {
6471 /* guard against overrun */
6472 tmpstring2[MIN(pos, (int)text_buffer_size - 1)] = tmpstring1[i];
6478 if (text_width == maximum_width) {
6479 /* this means the text is probably pushing the limit,
6480 * so we'll chop it */
6481 while (cur_x + get_string_width(tmpstring2) - text_start_x
6482 > maximum_width && strlen(tmpstring2) > 0) {
6483 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6494 c.pixel = current_color;
6495 XQueryColor(display, DefaultColormap(display, screen), &c);
6498 c2.color.red = c.red;
6499 c2.color.green = c.green;
6500 c2.color.blue = c.blue;
6501 c2.color.alpha = fonts[selected_font].font_alpha;
6503 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6504 cur_x, cur_y, (XftChar8 *) s, strlen(s));
6506 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6507 cur_x, cur_y, (XftChar8 *) s, strlen(s));
6512 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6515 cur_x += width_of_s;
6517 memcpy(tmpstring1, s, text_buffer_size);
6520 long redmask, greenmask, bluemask;
6522 void set_up_gradient()
6525 colour_depth = DisplayPlanes(display, screen);
6529 if (colour_depth != 24 && colour_depth != 16) {
6530 ERR("using non-standard colour depth, gradients may look like a "
6538 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6540 greenmask |= 1 << i;
6543 if (colour_depth % 3 == 1) {
6544 greenmask |= 1 << (colour_depth / 3);
6546 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6547 greenmask = greenmask << (colour_depth / 3);
6550 /* this function returns the next colour between two colours for a gradient */
6551 inline unsigned long do_gradient(unsigned long first_colour,
6552 unsigned long last_colour)
6555 int red1, green1, blue1; // first colour
6556 int red2, green2, blue2; // second colour
6557 int red3 = 0, green3 = 0, blue3 = 0; // difference
6558 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6559 short greenshift = (colour_depth / 3);
6561 red1 = (first_colour & redmask) >> redshift;
6562 green1 = (first_colour & greenmask) >> greenshift;
6563 blue1 = first_colour & bluemask;
6564 red2 = (last_colour & redmask) >> redshift;
6565 green2 = (last_colour & greenmask) >> greenshift;
6566 blue2 = last_colour & bluemask;
6573 if (green1 > green2) {
6576 if (green1 < green2) {
6579 if (blue1 > blue2) {
6582 if (blue1 < blue2) {
6597 if (red1 > bluemask) {
6600 if (green1 > bluemask) {
6603 if (blue1 > bluemask) {
6606 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
6610 /* this function returns the max diff for a gradient */
6611 inline unsigned long gradient_max(unsigned long first_colour,
6612 unsigned long last_colour)
6614 if (colour_depth == 0) {
6617 int red1, green1, blue1; // first colour
6618 int red2, green2, blue2; // second colour
6619 long redshift = (2 * colour_depth / 3 + colour_depth % 3);
6620 long greenshift = (colour_depth / 3);
6621 int red3 = 0, green3 = 0, blue3 = 0; // difference
6623 red1 = (first_colour & redmask) >> redshift;
6624 green1 = (first_colour & greenmask) >> greenshift;
6625 blue1 = first_colour & bluemask;
6626 red2 = (last_colour & redmask) >> redshift;
6627 green2 = (last_colour & greenmask) >> greenshift;
6628 blue2 = last_colour & bluemask;
6629 red3 = abs(red1 - red2);
6630 green3 = abs(green1 - green2);
6631 blue3 = abs(blue1 - blue2);
6643 static void draw_line(char *s)
6648 cur_x = text_start_x;
6649 cur_y += font_ascent();
6651 short font_h = font_height();
6653 /* find specials and draw stuff */
6656 if (*p == SPECIAL_CHAR) {
6659 /* draw string before special */
6666 switch (specials[special_index].type) {
6667 case HORIZONTAL_LINE:
6669 int h = specials[special_index].height;
6670 int mid = font_ascent() / 2;
6672 w = text_start_x + text_width - cur_x;
6674 XSetLineAttributes(display, window.gc, h, LineSolid,
6675 CapButt, JoinMiter);
6676 XDrawLine(display, window.drawable, window.gc, cur_x,
6677 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6683 int h = specials[special_index].height;
6684 int s = specials[special_index].arg;
6685 int mid = font_ascent() / 2;
6686 char ss[2] = { s, s };
6688 w = text_start_x + text_width - cur_x - 1;
6689 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6690 CapButt, JoinMiter);
6691 XSetDashes(display, window.gc, 0, ss, 2);
6692 XDrawLine(display, window.drawable, window.gc, cur_x,
6693 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6699 if (cur_x - text_start_x > maximum_width
6700 && maximum_width > 0) {
6703 int h = specials[special_index].height;
6704 int bar_usage = specials[special_index].arg;
6705 int by = cur_y - (font_ascent() / 2) - 1;
6707 if (h < font_height()) {
6710 w = specials[special_index].width;
6712 w = text_start_x + text_width - cur_x - 1;
6718 XSetLineAttributes(display, window.gc, 1, LineSolid,
6719 CapButt, JoinMiter);
6721 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6723 XFillRectangle(display, window.drawable, window.gc, cur_x,
6724 by, w * bar_usage / 255, h);
6725 if (specials[special_index].height > cur_y_add
6726 && specials[special_index].height > font_h) {
6727 cur_y_add = specials[special_index].height;
6734 if (cur_x - text_start_x > maximum_width
6735 && maximum_width > 0) {
6738 int h = specials[special_index].height;
6739 unsigned long last_colour = current_color;
6740 int by = cur_y - (font_ascent() / 2) - 1;
6742 if (h < font_height()) {
6745 w = specials[special_index].width;
6747 w = text_start_x + text_width - cur_x - 1;
6752 if (draw_graph_borders) {
6753 XSetLineAttributes(display, window.gc, 1, LineSolid,
6754 CapButt, JoinMiter);
6755 XDrawRectangle(display, window.drawable, window.gc,
6758 XSetLineAttributes(display, window.gc, 1, LineSolid,
6759 CapButt, JoinMiter);
6762 int gradient_size = 0;
6763 float gradient_factor = 0;
6764 float gradient_update = 0;
6765 unsigned long tmpcolour = current_color;
6767 if (specials[special_index].last_colour != 0
6768 || specials[special_index].first_colour != 0) {
6769 tmpcolour = specials[special_index].last_colour;
6771 gradient_max(specials[special_index].last_colour,
6772 specials[special_index].first_colour);
6773 gradient_factor = (float) gradient_size / (w - 2);
6775 for (i = w - 2; i > -1; i--) {
6776 if (specials[special_index].last_colour != 0
6777 || specials[special_index].first_colour != 0) {
6778 XSetForeground(display, window.gc, tmpcolour);
6779 gradient_update += gradient_factor;
6780 while (gradient_update > 0) {
6781 tmpcolour = do_gradient(tmpcolour,
6782 specials[special_index].first_colour);
6786 if ((w - i) / ((float) (w - 2) /
6787 (specials[special_index].graph_width)) > j
6788 && j < MAX_GRAPH_DEPTH - 3) {
6791 /* this is mugfugly, but it works */
6792 XDrawLine(display, window.drawable, window.gc,
6793 cur_x + i + 1, by + h, cur_x + i + 1,
6794 by + h - specials[special_index].graph[j] *
6795 (h - 1) / specials[special_index].graph_scale);
6797 if (specials[special_index].height > cur_y_add
6798 && specials[special_index].height > font_h) {
6799 cur_y_add = specials[special_index].height;
6801 /* if (draw_mode == BG) {
6802 set_foreground_color(default_bg_color);
6803 } else if (draw_mode == OUTLINE) {
6804 set_foreground_color(default_out_color);
6806 set_foreground_color(default_fg_color);
6808 set_foreground_color(last_colour);
6814 int old = font_ascent();
6816 cur_y -= font_ascent();
6817 selected_font = specials[special_index].font_added;
6818 if (cur_y + font_ascent() < cur_y + old) {
6821 cur_y += font_ascent();
6827 if (draw_mode == FG) {
6828 set_foreground_color(specials[special_index].arg);
6833 if (draw_mode == BG) {
6834 set_foreground_color(specials[special_index].arg);
6839 if (draw_mode == OUTLINE) {
6840 set_foreground_color(specials[special_index].arg);
6845 w += specials[special_index].arg;
6849 cur_y += specials[special_index].arg;
6853 if (specials[special_index].arg >= 0) {
6854 cur_x = (int) specials[special_index].arg;
6860 int start = specials[special_index].arg;
6861 int step = specials[special_index].width;
6863 if (!step || step < 0) {
6866 w = step - (cur_x - text_start_x - start) % step;
6872 /* TODO: add back in "+ border_margin" to the end of
6874 int pos_x = text_start_x + text_width -
6875 get_string_width_special(s);
6877 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6878 "get_string_width(p) %i gap_x %i "
6879 "specials[special_index].arg %i border_margin %i "
6880 "border_width %i\n", pos_x, text_start_x, text_width,
6881 cur_x, get_string_width_special(s), gap_x,
6882 specials[special_index].arg, border_margin,
6884 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6885 cur_x = pos_x - specials[special_index].arg;
6892 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6893 2 - (cur_x - text_start_x);
6894 /* int pos_x = text_start_x + text_width / 2 -
6895 get_string_width_special(s) / 2; */
6897 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6898 "get_string_width(p) %i gap_x %i "
6899 "specials[special_index].arg %i\n", pos_x, text_start_x,
6900 text_width, cur_x, get_string_width(s), gap_x,
6901 specials[special_index].arg); */
6902 if (pos_x > specials[special_index].arg) {
6903 w = pos_x - specials[special_index].arg;
6920 if (cur_y_add > 0) {
6922 cur_y -= font_descent();
6927 cur_y += font_descent();
6931 static void draw_text()
6934 cur_y = text_start_y;
6937 if (draw_borders && border_width > 0) {
6938 unsigned int b = (border_width + 1) / 2;
6940 if (stippled_borders) {
6941 char ss[2] = { stippled_borders, stippled_borders };
6942 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6943 CapButt, JoinMiter);
6944 XSetDashes(display, window.gc, 0, ss, 2);
6946 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6947 CapButt, JoinMiter);
6950 XDrawRectangle(display, window.drawable, window.gc,
6951 text_start_x - border_margin + b, text_start_y - border_margin + b,
6952 text_width + border_margin * 2 - 1 - b * 2,
6953 text_height + border_margin * 2 - 1 - b * 2);
6959 for_each_line(text_buffer, draw_line);
6962 static void draw_stuff()
6966 if (draw_shades && !draw_outline) {
6969 set_foreground_color(default_bg_color);
6980 for (i = -1; i < 2; i++) {
6981 for (j = -1; j < 2; j++) {
6982 if (i == 0 && j == 0) {
6987 set_foreground_color(default_out_color);
6988 draw_mode = OUTLINE;
6996 set_foreground_color(default_fg_color);
7005 swap.swap_window = window.window;
7006 swap.swap_action = XdbeBackground;
7007 XdbeSwapBuffers(display, &swap, 1);
7014 static void clear_text(int exposures)
7018 /* The swap action is XdbeBackground, which clears */
7023 /* there is some extra space for borders and outlines */
7024 XClearArea(display, window.window, text_start_x - border_margin - 1,
7025 text_start_y - border_margin - 1,
7026 text_width + border_margin * 2 + 2,
7027 text_height + border_margin * 2 + 2, exposures ? True : 0);
7032 static int need_to_update;
7034 /* update_text() generates new text and clears old text area */
7035 static void update_text()
7044 static void main_loop()
7046 #ifdef SIGNAL_BLOCKING
7047 sigset_t newmask, oldmask;
7049 sigemptyset(&newmask);
7050 sigaddset(&newmask, SIGINT);
7051 sigaddset(&newmask, SIGTERM);
7052 sigaddset(&newmask, SIGUSR1);
7056 Region region = XCreateRegion();
7059 int event_base, error_base;
7061 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
7062 ERR("Xdamage extension unavailable");
7064 Damage damage = XDamageCreate(display, window.window,
7065 XDamageReportNonEmpty);
7066 XserverRegion region2 = XFixesCreateRegionFromWindow(display,
7068 XserverRegion part = XFixesCreateRegionFromWindow(display,
7070 #endif /* HAVE_XDAMAGE */
7074 while (total_run_times == 0 || info.looped < total_run_times) {
7077 #ifdef SIGNAL_BLOCKING
7078 /* block signals. we will inspect for pending signals later */
7079 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7080 CRIT_ERR("unable to sigprocmask()");
7087 /* wait for X event or timeout */
7089 if (!XPending(display)) {
7093 double t = update_interval - (get_time() - last_update_time);
7099 tv.tv_sec = (long) t;
7100 tv.tv_usec = (long) (t * 1000000) % 1000000;
7102 FD_SET(ConnectionNumber(display), &fdsr);
7104 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7106 if (errno != EINTR) {
7107 ERR("can't select(): %s", strerror(errno));
7113 /* FIXME just sleep for the interval time if no X11 */
7114 usleep(update_interval * 1000000);
7122 if (need_to_update) {
7124 int wx = window.x, wy = window.y;
7132 /* resize window if it isn't right size */
7134 && (text_width + border_margin * 2 + 1 != window.width
7135 || text_height + border_margin * 2 + 1 != window.height)) {
7136 window.width = text_width + border_margin * 2 + 1;
7137 window.height = text_height + border_margin * 2 + 1;
7138 XResizeWindow(display, window.window, window.width,
7141 set_transparent_background(window.window);
7145 /* move window if it isn't in right position */
7146 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7147 XMoveWindow(display, window.window, window.x, window.y);
7158 r.x = text_start_x - border_margin;
7159 r.y = text_start_y - border_margin;
7160 r.width = text_width + border_margin * 2;
7161 r.height = text_height + border_margin * 2;
7162 XUnionRectWithRegion(&r, region, region);
7167 /* handle X events */
7168 while (XPending(display)) {
7171 XNextEvent(display, &ev);
7179 r.width = ev.xexpose.width;
7180 r.height = ev.xexpose.height;
7181 XUnionRectWithRegion(&r, region, region);
7186 case ReparentNotify:
7187 /* set background to ParentRelative for all parents */
7189 set_transparent_background(window.window);
7193 case ConfigureNotify:
7195 /* if window size isn't what expected, set fixed size */
7196 if (ev.xconfigure.width != window.width
7197 || ev.xconfigure.height != window.height) {
7198 if (window.width != 0 && window.height != 0) {
7202 /* clear old stuff before screwing up
7207 XWindowAttributes attrs;
7209 if (XGetWindowAttributes(display,
7210 window.window, &attrs)) {
7211 window.width = attrs.width;
7212 window.height = attrs.height;
7216 text_width = window.width - border_margin * 2 - 1;
7217 text_height = window.height - border_margin * 2 - 1;
7218 if (text_width > maximum_width
7219 && maximum_width > 0) {
7220 text_width = maximum_width;
7224 /* if position isn't what expected, set fixed pos
7225 * total_updates avoids setting fixed_pos when window
7226 * is set to weird locations when started */
7227 /* // this is broken
7228 if (total_updates >= 2 && !fixed_pos
7229 && (window.x != ev.xconfigure.x
7230 || window.y != ev.xconfigure.y)
7231 && (ev.xconfigure.x != 0
7232 || ev.xconfigure.y != 0)) {
7241 /* if an ordinary window with decorations */
7242 if ((window.type == TYPE_NORMAL)
7243 && (!TEST_HINT(window.hints,
7244 HINT_UNDECORATED))) {
7245 /* allow conky to hold input focus. */
7248 /* forward the click to the desktop window */
7249 XUngrabPointer(display, ev.xbutton.time);
7250 ev.xbutton.window = window.desktop;
7251 XSendEvent(display, ev.xbutton.window, False,
7252 ButtonPressMask, &ev);
7253 XSetInputFocus(display, ev.xbutton.window,
7254 RevertToParent, ev.xbutton.time);
7261 /* if an ordinary window with decorations */
7262 if ((window.type == TYPE_NORMAL)
7263 && (!TEST_HINT(window.hints,
7264 HINT_UNDECORATED))) {
7265 /* allow conky to hold input focus. */
7268 /* forward the release to the desktop window */
7269 ev.xbutton.window = window.desktop;
7270 XSendEvent(display, ev.xbutton.window, False,
7271 ButtonReleaseMask, &ev);
7280 if (ev.type == event_base + XDamageNotify) {
7281 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7283 XFixesSetRegion(display, part, &dev->area, 1);
7284 XFixesUnionRegion(display, region2, region2, part);
7286 #endif /* HAVE_XDAMAGE */
7292 XDamageSubtract(display, damage, region2, None);
7293 XFixesSetRegion(display, region2, 0, 0);
7294 #endif /* HAVE_XDAMAGE */
7296 /* XDBE doesn't seem to provide a way to clear the back buffer without
7297 * interfering with the front buffer, other than passing XdbeBackground
7298 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7299 * redraw the text even if it wasn't part of the exposed area. OTOH,
7300 * if we're not going to call draw_stuff at all, then no swap happens
7301 * and we can safely do nothing. */
7303 if (!XEmptyRegion(region)) {
7308 r.x = text_start_x - border_margin;
7309 r.y = text_start_y - border_margin;
7310 r.width = text_width + border_margin * 2;
7311 r.height = text_height + border_margin * 2;
7312 XUnionRectWithRegion(&r, region, region);
7315 XSetRegion(display, window.gc, region);
7318 XftDrawSetClip(window.xftdraw, region);
7324 XDestroyRegion(region);
7325 region = XCreateRegion();
7329 #ifdef SIGNAL_BLOCKING
7330 /* unblock signals of interest and let handler fly */
7331 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7332 CRIT_ERR("unable to sigprocmask()");
7336 switch (g_signal_pending) {
7339 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7344 ERR("received SIGINT or SIGTERM to terminate. bye!");
7347 XDestroyRegion(region);
7350 XDamageDestroy(display, damage);
7351 XFixesDestroyRegion(display, region2);
7352 XFixesDestroyRegion(display, part);
7353 #endif /* HAVE_XDAMAGE */
7355 return; /* return from main_loop */
7358 /* Reaching here means someone set a signal
7359 * (SIGXXXX, signal_handler), but didn't write any code
7361 * If you don't want to handle a signal, don't set a handler on
7362 * it in the first place. */
7363 if (g_signal_pending) {
7364 ERR("ignoring signal (%d)", g_signal_pending);
7368 g_signal_pending = 0;
7371 #if defined(X11) && defined(HAVE_XDAMAGE)
7372 XDamageDestroy(display, damage);
7373 XFixesDestroyRegion(display, region2);
7374 XFixesDestroyRegion(display, part);
7375 XDestroyRegion(region);
7377 #endif /* X11 && HAVE_XDAMAGE */
7380 static void load_config_file(const char *);
7382 /* reload the config file */
7383 void reload_config(void)
7385 timed_thread_destroy_registered_threads();
7387 if (info.cpu_usage) {
7388 free(info.cpu_usage);
7389 info.cpu_usage = NULL;
7397 if (info.mpd.title) {
7398 free(info.mpd.title);
7399 info.mpd.title = NULL;
7401 if (info.mpd.artist) {
7402 free(info.mpd.artist);
7403 info.mpd.artist = NULL;
7405 if (info.mpd.album) {
7406 free(info.mpd.album);
7407 info.mpd.album = NULL;
7409 if (info.mpd.random) {
7410 free(info.mpd.random);
7411 info.mpd.random = NULL;
7413 if (info.mpd.repeat) {
7414 free(info.mpd.repeat);
7415 info.mpd.repeat = NULL;
7417 if (info.mpd.track) {
7418 free(info.mpd.track);
7419 info.mpd.track = NULL;
7421 if (info.mpd.name) {
7422 free(info.mpd.name);
7423 info.mpd.name = NULL;
7425 if (info.mpd.file) {
7426 free(info.mpd.file);
7427 info.mpd.file = NULL;
7429 if (info.mpd.status) {
7430 free(info.mpd.status);
7431 info.mpd.status = NULL;
7441 #ifdef TCP_PORT_MONITOR
7442 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7445 if (current_config) {
7447 load_config_file(current_config);
7449 /* re-init specials array */
7450 if ((specials = realloc((void *) specials,
7451 sizeof(struct special_t) * max_specials)) == 0) {
7452 ERR("failed to realloc specials array");
7456 // clear the window first
7457 XClearWindow(display, RootWindow(display, screen));
7460 #ifdef TCP_PORT_MONITOR
7461 info.p_tcp_port_monitor_collection = NULL;
7463 extract_variable_text(text);
7469 tmpstring1 = malloc(text_buffer_size);
7470 memset(tmpstring1, 0, text_buffer_size);
7474 tmpstring2 = malloc(text_buffer_size);
7475 memset(tmpstring2, 0, text_buffer_size);
7479 text_buffer = malloc(max_user_text);
7480 memset(text_buffer, 0, max_user_text);
7487 timed_thread_destroy_registered_threads();
7489 if (info.cpu_usage) {
7490 free(info.cpu_usage);
7491 info.cpu_usage = NULL;
7496 XdbeDeallocateBackBufferName(display, window.back_buffer);
7501 XDestroyWindow(display, window.window);
7502 XClearWindow(display, RootWindow(display, screen));
7507 XClearWindow(display, RootWindow(display, screen));
7512 XFreeGC(display, window.gc);
7516 free_text_objects(text_object_count, text_objects);
7529 text_object_count = 0;
7530 text_objects = NULL;
7537 free(current_config);
7539 #ifdef TCP_PORT_MONITOR
7540 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7541 info.p_tcp_port_monitor_collection = NULL;
7550 for (i = 0; i < special_count; i++) {
7551 if (specials[i].type == GRAPH) {
7552 free(specials[i].graph);
7559 clear_diskio_stats();
7562 static int string_to_bool(const char *s)
7566 } else if (strcasecmp(s, "yes") == 0) {
7568 } else if (strcasecmp(s, "true") == 0) {
7570 } else if (strcasecmp(s, "1") == 0) {
7577 static enum alignment string_to_alignment(const char *s)
7579 if (strcasecmp(s, "top_left") == 0) {
7581 } else if (strcasecmp(s, "top_right") == 0) {
7583 } else if (strcasecmp(s, "top_middle") == 0) {
7585 } else if (strcasecmp(s, "bottom_left") == 0) {
7587 } else if (strcasecmp(s, "bottom_right") == 0) {
7588 return BOTTOM_RIGHT;
7589 } else if (strcasecmp(s, "bottom_middle") == 0) {
7590 return BOTTOM_MIDDLE;
7591 } else if (strcasecmp(s, "middle_left") == 0) {
7593 } else if (strcasecmp(s, "middle_right") == 0) {
7594 return MIDDLE_RIGHT;
7595 } else if (strcasecmp(s, "tl") == 0) {
7597 } else if (strcasecmp(s, "tr") == 0) {
7599 } else if (strcasecmp(s, "tm") == 0) {
7601 } else if (strcasecmp(s, "bl") == 0) {
7603 } else if (strcasecmp(s, "br") == 0) {
7604 return BOTTOM_RIGHT;
7605 } else if (strcasecmp(s, "bm") == 0) {
7606 return BOTTOM_MIDDLE;
7607 } else if (strcasecmp(s, "ml") == 0) {
7609 } else if (strcasecmp(s, "mr") == 0) {
7610 return MIDDLE_RIGHT;
7611 } else if (strcasecmp(s, "none") == 0) {
7618 static void set_default_configurations(void)
7620 fork_to_background = 0;
7621 total_run_times = 0;
7622 info.cpu_avg_samples = 2;
7623 info.net_avg_samples = 2;
7630 strcpy(info.mpd.host, "localhost");
7631 info.mpd.port = 6600;
7632 info.mpd.status = NULL;
7633 info.mpd.artist = NULL;
7634 info.mpd.album = NULL;
7635 info.mpd.title = NULL;
7636 info.mpd.random = NULL;
7637 info.mpd.track = NULL;
7638 info.mpd.name = NULL;
7639 info.mpd.file = NULL;
7642 info.xmms2.artist = NULL;
7643 info.xmms2.album = NULL;
7644 info.xmms2.title = NULL;
7645 info.xmms2.genre = NULL;
7646 info.xmms2.comment = NULL;
7647 info.xmms2.url = NULL;
7648 info.xmms2.status = NULL;
7649 info.xmms2.playlist = NULL;
7651 use_spacer = NO_SPACER;
7658 default_fg_color = WhitePixel(display, screen);
7659 default_bg_color = BlackPixel(display, screen);
7660 default_out_color = BlackPixel(display, screen);
7661 color0 = default_fg_color;
7662 color1 = default_fg_color;
7663 color2 = default_fg_color;
7664 color3 = default_fg_color;
7665 color4 = default_fg_color;
7666 color5 = default_fg_color;
7667 color6 = default_fg_color;
7668 color7 = default_fg_color;
7669 color8 = default_fg_color;
7670 color9 = default_fg_color;
7673 draw_graph_borders = 1;
7675 set_first_font("6x10");
7683 window.type = TYPE_NORMAL;
7685 strcpy(window.class_name, "Conky");
7687 sprintf(window.title, "Conky (%s)", info.uname_s.nodename);
7689 stippled_borders = 0;
7692 text_alignment = BOTTOM_LEFT;
7695 free(current_mail_spool);
7699 variable_substitute(MAIL_FILE, buf, 256);
7700 if (buf[0] != '\0') {
7701 current_mail_spool = strdup(buf);
7706 update_interval = 3.0;
7707 info.music_player_interval = 1.0;
7708 stuff_in_upper_case = 0;
7709 info.users.number = 1;
7711 #ifdef TCP_PORT_MONITOR
7712 tcp_port_monitor_args.max_port_monitor_connections =
7713 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
7717 static void load_config_file(const char *f)
7719 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7723 set_default_configurations();
7730 char buf[256], *p, *p2, *name, *value;
7733 if (fgets(buf, 256, fp) == NULL) {
7739 /* break at comment */
7740 p2 = strchr(p, '#');
7746 while (*p && isspace((int) *p)) {
7750 continue; /* empty line */
7757 while (*p2 && !isspace((int) *p2)) {
7761 *p2 = '\0'; /* break at name's end */
7768 while (*p && isspace((int) *p)) {
7774 p2 = value + strlen(value);
7775 while (isspace((int) *(p2 - 1))) {
7782 #define CONF2(a) if (strcasecmp(name, a) == 0)
7783 #define CONF(a) else CONF2(a)
7784 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7785 || strcasecmp(name, b) == 0)
7788 CONF2("alignment") {
7790 int a = string_to_alignment(value);
7801 CONF("background") {
7802 fork_to_background = string_to_bool(value);
7805 CONF2("background") {
7806 fork_to_background = string_to_bool(value);
7810 CONF("border_margin") {
7812 border_margin = strtol(value, 0, 0);
7817 CONF("border_width") {
7819 border_width = strtol(value, 0, 0);
7826 color0 = get_x11_color(value);
7833 color1 = get_x11_color(value);
7840 color2 = get_x11_color(value);
7847 color3 = get_x11_color(value);
7854 color4 = get_x11_color(value);
7861 color5 = get_x11_color(value);
7868 color6 = get_x11_color(value);
7875 color7 = get_x11_color(value);
7882 color8 = get_x11_color(value);
7889 color9 = get_x11_color(value);
7894 CONF("default_color") {
7896 default_fg_color = get_x11_color(value);
7901 CONF3("default_shade_color", "default_shadecolor") {
7903 default_bg_color = get_x11_color(value);
7908 CONF3("default_outline_color", "default_outlinecolor") {
7910 default_out_color = get_x11_color(value);
7918 info.mail = parse_mail_args(IMAP, value);
7925 info.mail = parse_mail_args(POP3, value);
7933 strncpy(info.mpd.host, value, 127);
7940 info.mpd.port = strtol(value, 0, 0);
7941 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
7946 CONF("mpd_password") {
7948 strncpy(info.mpd.password, value, 127);
7954 CONF("music_player_interval") {
7956 info.music_player_interval = strtod(value, 0);
7962 CONF("sensor_device") {
7964 sensor_device = strtol(value, 0, 0);
7970 CONF("cpu_avg_samples") {
7972 cpu_avg_samples = strtol(value, 0, 0);
7973 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
7976 info.cpu_avg_samples = cpu_avg_samples;
7982 CONF("net_avg_samples") {
7984 net_avg_samples = strtol(value, 0, 0);
7985 if (net_avg_samples < 1 || net_avg_samples > 14) {
7988 info.net_avg_samples = net_avg_samples;
7996 CONF("double_buffer") {
7997 use_xdbe = string_to_bool(value);
8001 CONF("override_utf8_locale") {
8002 utf8_mode = string_to_bool(value);
8004 CONF("draw_borders") {
8005 draw_borders = string_to_bool(value);
8007 CONF("draw_graph_borders") {
8008 draw_graph_borders = string_to_bool(value);
8010 CONF("draw_shades") {
8011 draw_shades = string_to_bool(value);
8013 CONF("draw_outline") {
8014 draw_outline = string_to_bool(value);
8017 CONF("out_to_console") {
8018 out_to_console = string_to_bool(value);
8020 CONF("use_spacer") {
8022 if (strcasecmp(value, "left") == 0) {
8023 use_spacer = LEFT_SPACER;
8024 } else if (strcasecmp(value, "right") == 0) {
8025 use_spacer = RIGHT_SPACER;
8026 } else if (strcasecmp(value, "none") == 0) {
8027 use_spacer = NO_SPACER;
8029 use_spacer = string_to_bool(value);
8030 ERR("use_spacer should have an argument of left, right, or"
8031 " none. '%s' seems to be some form of '%s', so"
8032 " defaulting to %s.", value,
8033 use_spacer ? "true" : "false",
8034 use_spacer ? "right" : "none");
8036 use_spacer = RIGHT_SPACER;
8038 use_spacer = NO_SPACER;
8042 ERR("use_spacer should have an argument. Defaulting to right.");
8043 use_spacer = RIGHT_SPACER;
8049 use_xft = string_to_bool(value);
8053 set_first_font(value);
8059 if (value && font_count >= 0) {
8060 fonts[0].font_alpha = atof(value) * 65535.0;
8069 if (string_to_bool(value)) {
8070 ERR("Xft not enabled");
8074 /* xftfont silently ignored when no Xft */
8077 /* xftalpha is silently ignored when no Xft */
8082 set_first_font(value);
8092 gap_x = atoi(value);
8099 gap_y = atoi(value);
8105 CONF("mail_spool") {
8109 variable_substitute(value, buf, 256);
8111 if (buf[0] != '\0') {
8112 if (current_mail_spool) {
8113 free(current_mail_spool);
8115 current_mail_spool = strdup(buf);
8122 CONF("minimum_size") {
8124 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8126 if (sscanf(value, "%d", &minimum_width) != 1) {
8134 CONF("maximum_width") {
8136 if (sscanf(value, "%d", &maximum_width) != 1) {
8144 CONF("no_buffers") {
8145 no_buffers = string_to_bool(value);
8147 CONF("top_cpu_separate") {
8148 cpu_separate = string_to_bool(value);
8150 CONF("short_units") {
8151 short_units = string_to_bool(value);
8153 CONF("pad_percents") {
8154 pad_percents = atoi(value);
8158 CONF("own_window") {
8160 own_window = string_to_bool(value);
8165 CONF("own_window_class") {
8167 memset(window.class_name, 0, sizeof(window.class_name));
8168 strncpy(window.class_name, value,
8169 sizeof(window.class_name) - 1);
8174 CONF("own_window_title") {
8176 memset(window.title, 0, sizeof(window.title));
8177 strncpy(window.title, value, sizeof(window.title) - 1);
8182 CONF("own_window_transparent") {
8184 set_transparent = string_to_bool(value);
8189 CONF("own_window_colour") {
8191 background_colour = get_x11_color(value);
8193 ERR("Invalid colour for own_window_colour (try omitting the "
8194 "'#' for hex colours");
8197 CONF("own_window_hints") {
8199 char *p_hint, *p_save;
8200 char delim[] = ", ";
8202 /* tokenize the value into individual hints */
8203 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8205 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8206 if (strncmp(p_hint, "undecorate", 10) == 0) {
8207 SET_HINT(window.hints, HINT_UNDECORATED);
8208 } else if (strncmp(p_hint, "below", 5) == 0) {
8209 SET_HINT(window.hints, HINT_BELOW);
8210 } else if (strncmp(p_hint, "above", 5) == 0) {
8211 SET_HINT(window.hints, HINT_ABOVE);
8212 } else if (strncmp(p_hint, "sticky", 6) == 0) {
8213 SET_HINT(window.hints, HINT_STICKY);
8214 } else if (strncmp(p_hint, "skip_taskbar", 12) == 0) {
8215 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8216 } else if (strncmp(p_hint, "skip_pager", 10) == 0) {
8217 SET_HINT(window.hints, HINT_SKIP_PAGER);
8222 p_hint = strtok_r(NULL, delim, &p_save);
8223 } while (p_hint != NULL);
8229 CONF("own_window_type") {
8231 if (strncmp(value, "normal", 6) == 0) {
8232 window.type = TYPE_NORMAL;
8233 } else if (strncmp(value, "desktop", 7) == 0) {
8234 window.type = TYPE_DESKTOP;
8235 } else if (strncmp(value, "override", 8) == 0) {
8236 window.type = TYPE_OVERRIDE;
8245 CONF("stippled_borders") {
8247 stippled_borders = strtol(value, 0, 0);
8249 stippled_borders = 4;
8254 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
8258 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
8261 CONF("update_interval") {
8263 update_interval = strtod(value, 0);
8267 if (info.music_player_interval == 0) {
8268 // default to update_interval
8269 info.music_player_interval = update_interval;
8272 CONF("total_run_times") {
8274 total_run_times = strtod(value, 0);
8280 stuff_in_upper_case = string_to_bool(value);
8282 CONF("max_specials") {
8284 max_specials = atoi(value);
8289 CONF("max_user_text") {
8291 max_user_text = atoi(value);
8296 CONF("text_buffer_size") {
8298 text_buffer_size = atoi(value);
8299 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8300 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8301 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8313 text = (char *) malloc(1);
8317 unsigned int l = strlen(text);
8319 if (fgets(buf, 256, fp) == NULL) {
8322 text = (char *) realloc(text, l + strlen(buf) + 1);
8325 if (strlen(text) > max_user_text) {
8330 text_lines = line + 1;
8333 #ifdef TCP_PORT_MONITOR
8334 CONF("max_port_monitor_connections") {
8335 if (!value || (sscanf(value, "%d",
8336 &tcp_port_monitor_args.max_port_monitor_connections) != 1)
8337 || tcp_port_monitor_args.max_port_monitor_connections < 0) {
8338 /* an error. use default, warn and continue. */
8339 tcp_port_monitor_args.max_port_monitor_connections =
8340 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8342 } else if (tcp_port_monitor_args.max_port_monitor_connections
8344 /* no error, just use default */
8345 tcp_port_monitor_args.max_port_monitor_connections =
8346 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8348 /* else tcp_port_monitor_args.max_port_monitor_connections > 0
8353 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8364 if (info.music_player_interval == 0) {
8365 // default to update_interval
8366 info.music_player_interval = update_interval;
8370 /* : means that character before that takes an argument */
8371 static const char *getopt_string = "vVdt:u:i:hc:"
8383 static const struct option longopts[] = {
8384 { "help", 0, NULL, 'h' },
8385 { "version", 0, NULL, 'V' },
8386 { "config", 1, NULL, 'c' },
8387 { "daemonize", 0, NULL, 'd' },
8389 { "alignment", 1, NULL, 'a' },
8390 { "font", 1, NULL, 'f' },
8392 { "own-window", 0, NULL, 'o' },
8395 { "double-buffer", 0, NULL, 'b' },
8397 { "window-id", 1, NULL, 'w' },
8399 { "text", 1, NULL, 't' },
8400 { "interval", 0, NULL, 'u' },
8404 int main(int argc, char **argv)
8406 struct sigaction act, oact;
8408 g_signal_pending = 0;
8409 memset(&info, 0, sizeof(info));
8411 #ifdef TCP_PORT_MONITOR
8412 tcp_port_monitor_args.max_port_monitor_connections =
8413 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8416 /* handle command line parameters that don't change configs */
8421 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8422 || ((s = getenv("LANG")) && *s)) {
8423 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8425 ERR("malloc failed");
8427 for (x = 0; x < strlen(s); x++) {
8428 temp[x] = tolower(s[x]);
8431 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8437 if (!setlocale(LC_CTYPE, "")) {
8438 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8442 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8453 if (current_config) {
8454 free(current_config);
8456 current_config = strdup(optarg);
8460 printf("Usage: %s [OPTION]...\n"
8461 "Conky is a system monitor that renders text on desktop or to own transparent\n"
8462 "window. Command line options will override configurations defined in config\n"
8464 " -V, --version version\n"
8465 " -c, --config=FILE config file to load\n"
8466 " -d, --daemonize daemonize, fork to background\n"
8467 " -h, --help help\n"
8469 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
8470 " -f, --font=FONT font to use\n"
8472 " -o, --own-window create own window to draw\n"
8475 " -b, --double-buffer double buffer (prevents flickering)\n"
8477 " -w, --window-id=WIN_ID window id to draw\n"
8478 " -x X x position\n"
8479 " -y Y y position\n"
8481 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8482 " -u, --interval=SECS update interval\n"
8483 " -i NUM number of times to update Conky\n",
8489 window.window = strtol(optarg, 0, 0);
8498 /* initalize X BEFORE we load config.
8499 * (we need to so that 'screen' is set) */
8503 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8505 if (!current_config) {
8506 /* load default config file */
8510 /* Try to use personal config file first */
8511 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
8512 if (buf[0] && (fp = fopen(buf, "r"))) {
8513 current_config = strdup(buf);
8517 /* Try to use system config file if personal config not readable */
8518 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8519 current_config = strdup(SYSTEM_CONFIG_FILE);
8523 /* No readable config found */
8524 if (!current_config) {
8525 CRIT_ERR("no readable personal or system-wide config file found");
8529 load_config_file(current_config);
8531 /* init specials array */
8532 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8533 ERR("failed to create specials array");
8537 if (current_mail_spool == NULL) {
8540 variable_substitute(MAIL_FILE, buf, 256);
8542 if (buf[0] != '\0') {
8543 current_mail_spool = strdup(buf);
8548 /* handle other command line arguments */
8550 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8551 || defined(__NetBSD__)
8552 optind = optreset = 1;
8557 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8558 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8559 "kvm_open")) == NULL) {
8560 CRIT_ERR("cannot read kvm");
8565 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8573 fork_to_background = 1;
8578 set_first_font(optarg);
8581 text_alignment = string_to_alignment(optarg);
8600 text = strdup(optarg);
8601 convert_escapes(text);
8605 update_interval = strtod(optarg, 0);
8606 if (info.music_player_interval == 0) {
8607 // default to update_interval
8608 info.music_player_interval = update_interval;
8613 total_run_times = strtod(optarg, 0);
8617 gap_x = atoi(optarg);
8621 gap_y = atoi(optarg);
8635 /* generate text and get initial size */
8636 extract_variable_text(text);
8643 if (fork_to_background) {
8648 ERR("Conky: couldn't fork() to background: %s",
8655 fprintf(stderr, "\n");
8660 /* parent process */
8661 fprintf(stderr, "Conky: forked to background, pid is %d\n",
8668 text_buffer = malloc(max_user_text);
8669 memset(text_buffer, 0, max_user_text);
8670 tmpstring1 = malloc(text_buffer_size);
8671 memset(tmpstring1, 0, text_buffer_size);
8672 tmpstring2 = malloc(text_buffer_size);
8673 memset(tmpstring2, 0, text_buffer_size);
8677 update_text_area(); /* to get initial size of the window */
8680 init_window(own_window, text_width + border_margin * 2 + 1,
8681 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8683 #else /* OWN_WINDOW */
8684 init_window(0, text_width + border_margin * 2 + 1,
8685 text_height + border_margin * 2 + 1, set_transparent, 0,
8687 #endif /* OWN_WINDOW */
8690 update_text_area(); /* to position text/window on screen */
8695 if (own_window && !fixed_pos) {
8696 XMoveWindow(display, window.window, window.x, window.y);
8699 set_transparent_background(window.window);
8709 /* Set signal handlers */
8710 act.sa_handler = signal_handler;
8711 sigemptyset(&act.sa_mask);
8714 act.sa_flags |= SA_RESTART;
8717 if (sigaction(SIGINT, &act, &oact) < 0
8718 || sigaction(SIGUSR1, &act, &oact) < 0
8719 || sigaction(SIGHUP,&act,&oact) < 0
8720 || sigaction(SIGTERM, &act, &oact) < 0) {
8721 ERR("error setting signal handler: %s", strerror(errno));
8724 /* *************** *
8726 * *************** */
8729 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8736 void signal_handler(int sig)
8738 /* signal handler is light as a feather, as it should be.
8739 * we will poll g_signal_pending with each loop of conky
8740 * and do any signal processing there, NOT here. */
8741 g_signal_pending = sig;