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-2008 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/>.
44 #include <X11/Xutil.h>
46 #include <X11/extensions/Xdamage.h>
52 #include <sys/types.h>
54 #include <netinet/in.h>
66 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
69 #define MAIL_FILE "$MAIL"
70 #define MAX_IF_BLOCK_DEPTH 5
71 #define MAX_TAIL_LINES 100
73 #define SIGNAL_BLOCKING
74 //#undef SIGNAL_BLOCKING
77 static void print_version(void) __attribute__((noreturn));
79 static void print_version(void)
81 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
83 printf("\nCompiled in features:\n\n"
84 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
88 " * Xdamage extension\n"
89 # endif /* HAVE_XDAMAGE */
91 " * Xdbe extension (double buffer)\n"
92 # endif /* HAVE_XDBE */
97 "\n Music detection:\n"
100 #endif /* AUDACIOUS */
113 "\n General features:\n"
120 #ifdef TCP_PORT_MONITOR
122 #endif /* TCP_PORT_MONITOR */
131 #endif /* HAVE_IWLIB */
143 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
150 static int text_start_x, text_start_y; /* text start position in window */
151 static int text_width, text_height;
169 char name[DEFAULT_TEXT_BUFFER_SIZE];
179 static int selected_font = 0;
180 static int font_count = -1;
181 struct font_list *fonts = NULL;
185 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
186 fonts[selected_font].xftfont->descent) \
187 : (fonts[selected_font].font->max_bounds.ascent + \
188 fonts[selected_font].font->max_bounds.descent))
189 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
190 : fonts[selected_font].font->max_bounds.ascent)
191 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
192 : fonts[selected_font].font->max_bounds.descent)
196 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
197 fonts[selected_font].font->max_bounds.descent)
198 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
199 #define font_descent() fonts[selected_font].font->max_bounds.descent
203 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
205 static void set_font(void);
207 int addfont(const char *data_in)
209 if (font_count > MAX_FONTS) {
210 CRIT_ERR("you don't need that many fonts, sorry.");
213 if (font_count == 0) {
217 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
221 memset(fonts, 0, sizeof(struct font_list));
223 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
224 memset(&fonts[font_count], 0, sizeof(struct font_list));
226 CRIT_ERR("realloc in addfont");
228 // must account for null terminator
229 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
230 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
232 fonts[font_count].font_alpha = 0xffff;
235 CRIT_ERR("Oops...looks like something overflowed in addfont().");
240 void set_first_font(const char *data_in)
242 if (font_count < 0) {
243 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
247 memset(fonts, 0, sizeof(struct font_list));
250 if (strlen(data_in) > 1) {
251 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
253 fonts[0].font_alpha = 0xffff;
258 void free_fonts(void)
262 for (i = 0; i <= font_count; i++) {
265 XftFontClose(display, fonts[i].xftfont);
266 fonts[i].xftfont = 0;
270 XFreeFont(display, fonts[i].font);
280 static void load_fonts(void)
284 for (i = 0; i <= font_count; i++) {
287 if (use_xft && fonts[i].xftfont) {
289 } else if (use_xft) {
290 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
291 XftFontClose(display, fonts[i].xftfont);
293 fonts[i].xftfont = XftFontOpenName(display, screen,
295 if (fonts[i].xftfont != NULL) {
299 ERR("can't load Xft font '%s'", fonts[i].name);
300 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
301 "courier-12")) != NULL) {
305 ERR("can't load Xft font '%s'", "courier-12");
307 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
308 CRIT_ERR("can't load font '%s'", "fixed");
315 /* load normal font */
316 /* if (fonts[i].font != NULL) {
317 XFreeFont(display, fonts[i].font);
320 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
321 ERR("can't load font '%s'", fonts[i].name);
322 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
323 CRIT_ERR("can't load font '%s'", "fixed");
324 printf("loaded fixed?\n");
332 /* default config file */
333 static char *current_config;
335 /* set to 1 if you want all text to be in uppercase */
336 static unsigned int stuff_in_upper_case;
338 /* Run how many times? */
339 static unsigned long total_run_times;
342 static int fork_to_background;
344 static int cpu_avg_samples, net_avg_samples;
348 static int show_graph_scale;
349 static int show_graph_range;
351 /* Position on the screen */
352 static int text_alignment;
353 static int gap_x, gap_y;
356 static int draw_borders;
357 static int draw_graph_borders;
358 static int stippled_borders;
360 static int draw_shades, draw_outline;
362 static int border_margin, border_width;
364 static long default_fg_color, default_bg_color, default_out_color;
366 /* create own window or draw stuff to root? */
367 static int set_transparent = 0;
370 static int own_window = 0;
371 static int background_colour = 0;
373 /* fixed size/pos is set if wm/user changes them */
374 static int fixed_size = 0, fixed_pos = 0;
377 static int minimum_width, minimum_height;
378 static int maximum_width;
383 static int sensor_device;
386 static long color0, color1, color2, color3, color4, color5, color6, color7,
389 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
390 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
392 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
393 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
395 /* maximum size of individual text buffers, ie $exec buffer size */
396 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
399 #define CODEPAGE_LENGTH 20
401 long iconv_count = 0;
402 char iconv_converting;
403 static iconv_t **iconv_cd = 0;
405 int register_iconv(iconv_t *new_iconv)
407 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
409 CRIT_ERR("Out of memory");
411 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
412 if (!iconv_cd[iconv_count]) {
413 CRIT_ERR("Out of memory");
415 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
420 void free_iconv(void)
425 for (i = 0; i < iconv_count; i++) {
427 iconv_close(*iconv_cd[i]);
441 /* no buffers in used memory? */
444 /* pad percentages to decimals? */
445 static int pad_percents = 0;
447 #ifdef TCP_PORT_MONITOR
448 tcp_port_monitor_args_t tcp_port_monitor_args;
451 static char *global_text = 0;
452 long global_text_lines;
454 static int total_updates;
457 static int blockdepth = 0;
458 static int if_jumped = 0;
459 static int blockstart[MAX_IF_BLOCK_DEPTH];
461 int check_contains(char *f, char *s)
464 FILE *where = fopen(f, "r");
467 char buf1[256], buf2[256];
469 while (fgets(buf1, 256, where)) {
470 sscanf(buf1, "%255s", buf2);
471 if (strstr(buf2, s)) {
478 ERR("Could not open the file");
484 static inline int calc_text_width(const char *s, int l)
491 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
492 (const FcChar8 *) s, l, &gi);
494 XftTextExtents8(display, fonts[selected_font].xftfont,
495 (const FcChar8 *) s, l, &gi);
501 return XTextWidth(fonts[selected_font].font, s, l);
506 /* formatted text to render on screen, generated in generate_text(),
507 * drawn in draw_stuff() */
509 static char *text_buffer;
511 /* special stuff in text_buffer */
513 #define SPECIAL_CHAR '\x01'
542 unsigned long first_colour; // for graph gradient
543 unsigned long last_colour;
547 /* create specials array on heap instead of stack with introduction of
549 static struct special_t *specials = NULL;
551 static unsigned int special_count;
554 static unsigned int special_index; /* used when drawing */
557 /* why 256? cause an array of more then 256 doubles seems excessive,
558 * and who needs that kind of precision anyway? */
559 #define MAX_GRAPH_DEPTH 256
561 static struct special_t *new_special(char *buf, enum special_types t)
563 if (special_count >= max_specials) {
564 CRIT_ERR("too many special things in text");
567 buf[0] = SPECIAL_CHAR;
569 specials[special_count].type = t;
570 return &specials[special_count++];
573 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
577 unsigned int count = 0;
578 static char buf[BUFSZ];
579 long orig_pos = ftell(fp);
581 long buf_size = BUFSZ;
582 char *cur_found = NULL;
584 while (count < step) {
585 if (cur_found == NULL) {
586 buf_size = fread(buf, 1, buf_size, fp);
589 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
590 if (cur_found != NULL) {
591 buf_pos = cur_found - buf + 1;
600 ret = ftell(fp) - buf_size + buf_pos - 1;
602 fseek(fp, orig_pos, SEEK_SET);
607 void *memrchr(const void *buffer, char c, size_t n)
609 const unsigned char *p = buffer;
611 for (p += n; n; n--) {
620 long rev_fcharfind(FILE *fp, char val, unsigned int step)
624 unsigned int count = 0;
625 static char buf[BUFSZ];
626 long orig_pos = ftell(fp);
628 long file_pos = orig_pos;
629 long buf_size = BUFSZ;
632 while (count < step) {
634 if (file_pos > BUFSZ) {
635 fseek(fp, file_pos - BUFSZ, SEEK_SET);
638 fseek(fp, 0, SEEK_SET);
640 file_pos = ftell(fp);
641 buf_pos = fread(buf, 1, buf_size, fp);
643 cur_found = memrchr(buf, val, (size_t) buf_pos);
644 if (cur_found != NULL) {
645 buf_pos = cur_found - buf;
654 fseek(fp, orig_pos, SEEK_SET);
656 ret = file_pos + buf_pos;
661 static void new_bar(char *buf, int w, int h, int usage)
663 struct special_t *s = new_special(buf, BAR);
665 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
670 static const char *scan_bar(const char *args, int *w, int *h)
672 /* zero width means all space that is available */
675 /* bar's argument is either height or height,width */
679 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
680 sscanf(args, "%d %n", h, &n);
688 static char *scan_font(const char *args)
691 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
698 static void new_font(char *buf, char *args)
701 struct special_t *s = new_special(buf, FONT);
703 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
704 int tmp = selected_font;
706 selected_font = s->font_added = addfont(args);
711 struct special_t *s = new_special(buf, FONT);
712 int tmp = selected_font;
714 selected_font = s->font_added = 0;
719 void graph_append(struct special_t *graph, double f, char showaslog)
729 if (!graph->scaled && f > graph->graph_scale) {
730 f = graph->graph_scale;
733 /* Already happens in new_graph
735 graph->graph_scale = 1;
738 graph->graph[0] = f; /* add new data */
739 /* shift all the data by 1 */
740 for (i = graph->graph_width - 1; i > 0; i--) {
741 graph->graph[i] = graph->graph[i - 1];
742 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
743 /* check if we need to update the scale */
744 graph->graph_scale = graph->graph[i];
749 short colour_depth = 0;
750 void set_up_gradient(void);
752 /* precalculated: 31/255, and 63/255 */
753 #define CONST_8_TO_5_BITS 0.12156862745098
754 #define CONST_8_TO_6_BITS 0.247058823529412
756 /* adjust color values depending on color depth */
757 static unsigned int adjust_colors(unsigned int color)
761 if (colour_depth == 0) {
764 if (colour_depth == 16) {
765 r = (color & 0xff0000) >> 16;
766 g = (color & 0xff00) >> 8;
768 color = (int) (r * CONST_8_TO_5_BITS) << 11;
769 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
770 color |= (int) (b * CONST_8_TO_5_BITS);
775 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
776 unsigned int second_colour, double i, int scale, int append, char showaslog)
778 struct special_t *s = new_special(buf, GRAPH);
781 if (s->graph == NULL) {
782 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
783 // subtract 2 for the box
784 s->graph_width = s->width /* - 2 */;
786 s->graph_width = MAX_GRAPH_DEPTH - 2;
788 s->graph = malloc(s->graph_width * sizeof(double));
789 memset(s->graph, 0, s->graph_width * sizeof(double));
790 s->graph_scale = 100;
793 s->first_colour = adjust_colors(first_colour);
794 s->last_colour = adjust_colors(second_colour);
797 s->graph_scale = scale;
805 s->graph_width = s->width - 2; // subtract 2 for rectangle around
809 s->graph_scale = log10(s->graph_scale + 1);
813 graph_append(s, i, showaslog);
817 //don't use spaces in LOGGRAPH or NORMGRAPH if you change them
818 #define LOGGRAPH "log"
819 #define NORMGRAPH "normal"
821 static char *scan_graph(const char *args, int *w, int *h,
822 unsigned int *first_colour, unsigned int *last_colour,
823 unsigned int *scale, char *showaslog)
825 const char *nographtype;
829 /* zero width means all space that is available */
836 //set showaslog and place the rest of the args in nographtype
837 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
840 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
843 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
845 nographtype = &args[strlen(LOGGRAPH) + 1];
846 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
848 nographtype = &args[strlen(NORMGRAPH) + 1];
853 //check the rest of the args
854 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
858 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
861 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
862 return strndup(buf, text_buffer_size);
865 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
866 return strndup(buf, text_buffer_size);
871 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
875 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
878 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
879 return strndup(buf, text_buffer_size);
882 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
883 return strndup(buf, text_buffer_size);
888 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
892 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
895 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
897 //TODO: check the return value and throw an error?
898 sscanf(nographtype, "%63s %d,%d", buf, h, w);
901 return strndup(buf, text_buffer_size);
904 if (buf[0] == '\0') {
907 return strndup(buf, text_buffer_size);
911 static inline void new_hr(char *buf, int a)
913 new_special(buf, HORIZONTAL_LINE)->height = a;
916 static inline void new_stippled_hr(char *buf, int a, int b)
918 struct special_t *s = new_special(buf, STIPPLED_HR);
924 static inline void new_fg(char *buf, long c)
926 new_special(buf, FG)->arg = c;
929 static inline void new_bg(char *buf, long c)
931 new_special(buf, BG)->arg = c;
934 static inline void new_outline(char *buf, long c)
936 new_special(buf, OUTLINE)->arg = c;
939 static inline void new_offset(char *buf, long c)
941 new_special(buf, OFFSET)->arg = c;
944 static inline void new_voffset(char *buf, long c)
946 new_special(buf, VOFFSET)->arg = c;
949 static inline void new_alignr(char *buf, long c)
951 new_special(buf, ALIGNR)->arg = c;
954 // A postive offset pushes the text further left
955 static inline void new_alignc(char *buf, long c)
957 new_special(buf, ALIGNC)->arg = c;
960 static inline void new_goto(char *buf, long c)
962 new_special(buf, GOTO)->arg = c;
965 static inline void new_tab(char *buf, int a, int b)
967 struct special_t *s = new_special(buf, TAB);
973 /* quite boring functions */
975 static inline void for_each_line(char *b, void f(char *))
979 for (ps = b, pe = b; *pe; pe++) {
993 static void convert_escapes(char *buf)
995 char *p = buf, *s = buf;
1002 } else if (*s == '\\') {
1013 /* Prints anything normally printed with snprintf according to the current value
1014 * of use_spacer. Actually slightly more flexible than snprintf, as you can
1015 * safely specify the destination buffer as one of your inputs. */
1016 static int spaced_print(char *, int, const char *, int, const char *, ...)
1017 __attribute__((format(printf, 3, 6)));
1019 static int spaced_print(char *buf, int size, const char *format, int width,
1020 const char *func_name, ...) {
1028 tempbuf = malloc(size * sizeof(char));
1030 // Passes the varargs along to vsnprintf
1031 va_start(argp, func_name);
1032 vsnprintf(tempbuf, size, format, argp);
1035 switch (use_spacer) {
1037 len = snprintf(buf, size, "%s", tempbuf);
1040 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
1043 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
1046 CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
1055 /* converts from bytes to human readable format (k, M, G, T) */
1056 static void human_readable(long long num, char *buf, int size, const char *func_name)
1058 const char **suffix = suffixes;
1061 static const int WIDTH = 10, SHORT_WIDTH = 8;
1063 if (num < 1024LL && num > -1024LL) {
1065 spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num,
1068 spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix);
1073 while ((num / 1024 >= 1000LL || num / 1024 <= -1024LL) && **(suffix + 2)) {
1079 fnum = num / 1024.0;
1084 if (precision < 0) {
1088 len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name,
1089 precision, fnum, **suffix);
1091 len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision,
1094 } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
1099 enum text_object_type {
1101 #if defined(__linux__)
1103 #endif /* __linux__ */
1113 OBJ_battery_percent,
1115 #endif /* !__OpenBSD__ */
1130 OBJ_conky_build_date,
1131 OBJ_conky_build_arch,
1141 OBJ_diskiograph_read,
1142 OBJ_diskiograph_write,
1180 #if defined(__linux__)
1187 OBJ_i8k_left_fan_status,
1188 OBJ_i8k_right_fan_status,
1189 OBJ_i8k_left_fan_rpm,
1190 OBJ_i8k_right_fan_rpm,
1192 OBJ_i8k_buttons_status,
1208 OBJ_wireless_bitrate,
1210 OBJ_wireless_link_qual,
1211 OBJ_wireless_link_qual_max,
1212 OBJ_wireless_link_qual_perc,
1213 OBJ_wireless_link_bar,
1214 #endif /* __linux__ */
1255 OBJ_running_processes,
1264 OBJ_temp1, /* i2c is used instead in these */
1288 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1289 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1291 OBJ_apm_battery_time,
1292 OBJ_apm_battery_life,
1293 #endif /* __FreeBSD__ __OpenBSD__ */
1295 OBJ_obsd_sensors_temp,
1296 OBJ_obsd_sensors_fan,
1297 OBJ_obsd_sensors_volt,
1300 #endif /* __OpenBSD__ */
1335 OBJ_music_player_interval,
1355 OBJ_xmms2_timesplayed,
1358 OBJ_audacious_status,
1359 OBJ_audacious_title,
1360 OBJ_audacious_length,
1361 OBJ_audacious_length_seconds,
1362 OBJ_audacious_position,
1363 OBJ_audacious_position_seconds,
1364 OBJ_audacious_bitrate,
1365 OBJ_audacious_frequency,
1366 OBJ_audacious_channels,
1367 OBJ_audacious_filename,
1368 OBJ_audacious_playlist_length,
1369 OBJ_audacious_playlist_position,
1386 #ifdef TCP_PORT_MONITOR
1401 OBJ_smapi_bat_power,
1402 OBJ_if_smapi_bat_installed,
1406 OBJ_entropy_poolsize,
1410 struct text_object {
1412 char *s; /* some string */
1413 int i; /* some integer */
1414 long l; /* some other integer */
1415 unsigned int sensor;
1416 struct net_stat *net;
1418 struct diskio_stat *diskio;
1419 unsigned char loadavg[3];
1420 unsigned int cpu_index;
1421 struct mail_s *mail;
1429 char *tz; /* timezone variable */
1430 char *fmt; /* time display formatting */
1468 /* If not -1, a file descriptor to read from when
1469 * logfile is a FIFO. */
1486 timed_thread *p_timed_thread;
1492 #ifdef TCP_PORT_MONITOR
1494 /* starting port to monitor */
1495 in_port_t port_range_begin;
1496 /* ending port to monitor */
1497 in_port_t port_range_end;
1498 /* enum from libtcp-portmon.h, e.g. COUNT, etc. */
1500 /* 0 to n-1 connections. */
1501 int connection_index;
1536 struct local_mail_s local_mail;
1538 struct nvidia_s nvidia;
1545 unsigned int c, d, e;
1551 struct text_object_list {
1552 unsigned int text_object_count;
1553 struct text_object *text_objects;
1556 static struct text_object_list *global_text_object_list;
1558 static void generate_text_internal(char *p, int p_max_size,
1559 struct text_object_list *text_object_list,
1560 struct information *cur);
1562 #define MAXDATASIZE 1000
1566 struct mail_s *parse_mail_args(char type, const char *arg)
1568 struct mail_s *mail;
1571 mail = malloc(sizeof(struct mail_s));
1572 memset(mail, 0, sizeof(struct mail_s));
1574 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
1577 ERR("Scanning IMAP args failed");
1578 } else if (type == IMAP) {
1579 ERR("Scanning POP3 args failed");
1582 // see if password needs prompting
1583 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1584 int fp = fileno(stdin);
1585 struct termios term;
1587 tcgetattr(fp, &term);
1588 term.c_lflag &= ~ECHO;
1589 tcsetattr(fp, TCSANOW, &term);
1590 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1591 scanf("%128s", mail->pass);
1593 term.c_lflag |= ECHO;
1594 tcsetattr(fp, TCSANOW, &term);
1596 // now we check for optional args
1597 tmp = strstr(arg, "-r ");
1600 sscanf(tmp, "%u", &mail->retries);
1602 mail->retries = 5; // 5 retries after failure
1604 tmp = strstr(arg, "-i ");
1607 sscanf(tmp, "%f", &mail->interval);
1609 mail->interval = 300; // 5 minutes
1611 tmp = strstr(arg, "-p ");
1614 sscanf(tmp, "%lu", &mail->port);
1617 mail->port = 110; // default pop3 port
1618 } else if (type == IMAP) {
1619 mail->port = 143; // default imap port
1623 tmp = strstr(arg, "-f ");
1626 sscanf(tmp, "%s", mail->folder);
1628 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1631 tmp = strstr(arg, "-e ");
1636 if (tmp[0] == '\'') {
1637 len = strstr(tmp + 1, "'") - tmp - 1;
1642 strncpy(mail->command, tmp + 1, len);
1644 mail->command[0] = '\0';
1646 mail->p_timed_thread = NULL;
1650 int imap_command(int sockfd, char *command, char *response, const char *verify)
1652 struct timeval timeout;
1655 if (send(sockfd, command, strlen(command), 0) == -1) {
1659 timeout.tv_sec = 60; // 60 second timeout i guess
1660 timeout.tv_usec = 0;
1662 FD_SET(sockfd, &fdset);
1663 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1665 if ((numbytes = recv(sockfd, response, MAXDATASIZE - 1, 0)) == -1) {
1670 response[numbytes] = '\0';
1671 if (strstr(response, verify) == NULL) {
1677 void *imap_thread(void *arg)
1679 int sockfd, numbytes;
1680 char recvbuf[MAXDATASIZE];
1681 char sendbuf[MAXDATASIZE];
1683 unsigned int fail = 0;
1684 unsigned long old_unseen = ULONG_MAX;
1685 unsigned long old_messages = ULONG_MAX;
1686 struct stat stat_buf;
1687 struct hostent he, *he_res = 0;
1689 char hostbuff[2048];
1690 struct sockaddr_in their_addr; // connector's address information
1691 struct mail_s *mail = (struct mail_s *)arg;
1693 int threadfd = timed_thread_readfd(mail->p_timed_thread);
1695 #ifdef HAVE_GETHOSTBYNAME_R
1696 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1697 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
1700 #else /* HAVE_GETHOSTBYNAME_R */
1701 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1702 herror("gethostbyname");
1705 #endif /* HAVE_GETHOSTBYNAME_R */
1706 while (fail < mail->retries) {
1707 struct timeval timeout;
1712 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
1713 mail->user, mail->host, fail + 1, mail->retries);
1716 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1723 their_addr.sin_family = AF_INET;
1724 // short, network byte order
1725 their_addr.sin_port = htons(mail->port);
1726 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1727 // zero the rest of the struct
1728 memset(&(their_addr.sin_zero), '\0', 8);
1730 if (connect(sockfd, (struct sockaddr *) &their_addr,
1731 sizeof(struct sockaddr)) == -1) {
1737 timeout.tv_sec = 60; // 60 second timeout i guess
1738 timeout.tv_usec = 0;
1740 FD_SET(sockfd, &fdset);
1741 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1743 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1749 ERR("IMAP connection failed: timeout");
1753 recvbuf[numbytes] = '\0';
1754 if (strstr(recvbuf, "* OK") != recvbuf) {
1755 ERR("IMAP connection failed, probably not an IMAP server");
1759 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1760 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1761 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1762 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1763 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1764 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
1768 if (strstr(recvbuf, " IDLE ") != NULL) {
1772 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1773 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1774 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
1775 MAXDATASIZE - strlen(sendbuf) - 1);
1776 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
1781 // now we get the data
1782 reply = strstr(recvbuf, " (MESSAGES ");
1784 *strchr(reply, ')') = '\0';
1785 if (reply == NULL) {
1786 ERR("Error parsing IMAP response: %s", recvbuf);
1790 timed_thread_lock(mail->p_timed_thread);
1791 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
1793 timed_thread_unlock(mail->p_timed_thread);
1795 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
1796 || (mail->messages > old_messages && mail->unseen > 0))) {
1798 if (system(mail->command) == -1) {
1803 old_unseen = mail->unseen;
1804 old_messages = mail->messages;
1807 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
1808 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1809 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1810 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
1815 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
1816 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
1824 FD_SET(sockfd, &fdset);
1825 FD_SET(threadfd, &fdset);
1826 res = pselect(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL, &oldmask);
1827 if (timed_thread_test(mail->p_timed_thread)) {
1828 timed_thread_exit(mail->p_timed_thread);
1830 if ((res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
1831 timed_thread_exit(mail->p_timed_thread);
1832 } else if (res > 0) {
1833 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1834 perror("recv idling");
1841 recvbuf[numbytes] = '\0';
1842 if (strlen(recvbuf) > 2) {
1843 unsigned long messages, recent;
1844 char *buf = recvbuf;
1845 buf = strstr(buf, "EXISTS");
1846 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
1847 buf = strstr(buf + 1, "EXISTS");
1850 // back up until we reach '*'
1851 while (buf >= recvbuf && buf[0] != '*') {
1854 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
1855 timed_thread_lock(mail->p_timed_thread);
1856 mail->messages = messages;
1857 timed_thread_unlock(mail->p_timed_thread);
1861 buf = strstr(buf, "RECENT");
1862 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
1863 buf = strstr(buf + 1, "RECENT");
1866 // back up until we reach '*'
1867 while (buf >= recvbuf && buf[0] != '*') {
1870 if (sscanf(buf, "* %lu RECENT\r\n", &recent) == 1) {
1872 * if we have > 0 recent, re-check the unseen count
1874 timed_thread_lock(mail->p_timed_thread);
1875 mail->unseen = recent;
1876 timed_thread_unlock(mail->p_timed_thread);
1880 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
1881 || (mail->messages > old_messages && mail->unseen > 0))) {
1883 if (system(mail->command) == -1) {
1888 old_unseen = mail->unseen;
1889 old_messages = mail->messages;
1893 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
1894 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1899 timeout.tv_sec = 60; // 60 second timeout i guess
1900 timeout.tv_usec = 0;
1902 FD_SET(sockfd, &fdset);
1903 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1905 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1911 recvbuf[numbytes] = '\0';
1912 if (strstr(recvbuf, "a3 OK") == NULL) {
1913 ERR("IMAP logout failed: %s", recvbuf);
1919 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1920 /* if a valid socket, close it */
1923 if (timed_thread_test(mail->p_timed_thread)) {
1924 timed_thread_exit(mail->p_timed_thread);
1932 void *pop3_thread(void *arg)
1934 int sockfd, numbytes;
1935 char recvbuf[MAXDATASIZE];
1936 char sendbuf[MAXDATASIZE];
1938 unsigned int fail = 0;
1939 unsigned long old_unseen = ULONG_MAX;
1940 struct stat stat_buf;
1941 struct hostent he, *he_res = 0;
1943 char hostbuff[2048];
1944 struct sockaddr_in their_addr; // connector's address information
1945 struct mail_s *mail = (struct mail_s *)arg;
1947 #ifdef HAVE_GETHOSTBYNAME_R
1948 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1949 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
1952 #else /* HAVE_GETHOSTBYNAME_R */
1953 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1954 herror("gethostbyname");
1957 #endif /* HAVE_GETHOSTBYNAME_R */
1958 while (fail < mail->retries) {
1959 struct timeval timeout;
1964 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
1965 mail->user, mail->host, fail + 1, mail->retries);
1968 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1975 their_addr.sin_family = AF_INET;
1976 // short, network byte order
1977 their_addr.sin_port = htons(mail->port);
1978 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1979 // zero the rest of the struct
1980 memset(&(their_addr.sin_zero), '\0', 8);
1982 if (connect(sockfd, (struct sockaddr *) &their_addr,
1983 sizeof(struct sockaddr)) == -1) {
1989 timeout.tv_sec = 60; // 60 second timeout i guess
1990 timeout.tv_usec = 0;
1992 FD_SET(sockfd, &fdset);
1993 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1995 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2001 ERR("POP3 connection failed: timeout\n");
2005 recvbuf[numbytes] = '\0';
2006 if (strstr(recvbuf, "+OK ") != recvbuf) {
2007 ERR("POP3 connection failed, probably not a POP3 server");
2011 strncpy(sendbuf, "USER ", MAXDATASIZE);
2012 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
2013 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
2014 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2015 perror("send USER");
2019 timeout.tv_sec = 60; // 60 second timeout i guess
2020 timeout.tv_usec = 0;
2022 FD_SET(sockfd, &fdset);
2023 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2025 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2026 perror("recv USER");
2031 recvbuf[numbytes] = '\0';
2032 if (strstr(recvbuf, "+OK ") == NULL) {
2033 ERR("POP3 server login failed: %s", recvbuf);
2037 strncpy(sendbuf, "PASS ", MAXDATASIZE);
2038 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
2039 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
2040 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2041 perror("send PASS");
2045 timeout.tv_sec = 60; // 60 second timeout i guess
2046 timeout.tv_usec = 0;
2048 FD_SET(sockfd, &fdset);
2049 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2051 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2052 perror("recv PASS");
2057 recvbuf[numbytes] = '\0';
2058 if (strstr(recvbuf, "+OK ") == NULL) {
2059 ERR("POP3 server login failed: %s", recvbuf);
2063 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
2064 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2065 perror("send STAT");
2069 timeout.tv_sec = 60; // 60 second timeout i guess
2070 timeout.tv_usec = 0;
2072 FD_SET(sockfd, &fdset);
2073 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2075 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2076 perror("recv STAT");
2081 recvbuf[numbytes] = '\0';
2082 if (strstr(recvbuf, "+OK ") == NULL) {
2083 ERR("POP3 status failed: %s", recvbuf);
2087 // now we get the data
2088 reply = recvbuf + 4;
2089 if (reply == NULL) {
2090 ERR("Error parsing POP3 response: %s", recvbuf);
2094 timed_thread_lock(mail->p_timed_thread);
2095 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
2096 timed_thread_unlock(mail->p_timed_thread);
2098 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
2099 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2100 perror("send QUIT");
2104 timeout.tv_sec = 60; // 60 second timeout i guess
2105 timeout.tv_usec = 0;
2107 FD_SET(sockfd, &fdset);
2108 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2110 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2111 perror("recv QUIT");
2116 recvbuf[numbytes] = '\0';
2117 if (strstr(recvbuf, "+OK") == NULL) {
2118 ERR("POP3 logout failed: %s", recvbuf);
2122 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
2124 if (system(mail->command) == -1) {
2129 old_unseen = mail->unseen;
2131 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
2132 /* if a valid socket, close it */
2135 if (timed_thread_test(mail->p_timed_thread)) {
2136 timed_thread_exit(mail->p_timed_thread);
2144 static inline void read_exec(const char *data, char *buf, const int size)
2146 FILE *fp = popen(data, "r");
2147 int length = fread(buf, 1, size, fp);
2151 if (length > 0 && buf[length - 1] == '\n') {
2152 buf[length - 1] = '\0';
2156 void *threaded_exec(void *) __attribute__((noreturn));
2158 void *threaded_exec(void *arg)
2161 struct text_object *obj = (struct text_object *)arg;
2164 p2 = obj->data.texeci.buffer;
2165 timed_thread_lock(obj->data.texeci.p_timed_thread);
2166 read_exec(obj->data.texeci.cmd, obj->data.texeci.buffer,
2169 if (*p2 == '\001') {
2174 timed_thread_unlock(obj->data.texeci.p_timed_thread);
2175 if (timed_thread_test(obj->data.texeci.p_timed_thread)) {
2176 timed_thread_exit(obj->data.texeci.p_timed_thread);
2182 static struct text_object *new_text_object_internal(void)
2184 struct text_object *obj = malloc(sizeof(struct text_object));
2185 memset(obj, 0, sizeof(struct text_object));
2190 * call with full == 0 when freeing after 'internal' evaluation of objects
2192 static void free_text_objects(struct text_object_list *text_object_list, char full)
2195 struct text_object *obj;
2197 if (text_object_list == NULL) {
2201 for (i = 0; i < text_object_list->text_object_count; i++) {
2202 obj = &text_object_list->text_objects[i];
2203 switch (obj->type) {
2212 close(obj->data.sysfs.fd);
2214 #endif /* !__OpenBSD__ */
2220 free(obj->data.tztime.tz);
2221 free(obj->data.tztime.fmt);
2224 free(obj->data.mboxscan.args);
2225 free(obj->data.mboxscan.output);
2229 free(obj->data.local_mail.box);
2234 case OBJ_imap_unseen:
2235 if (!obj->global_mode) {
2236 free(obj->data.mail);
2239 case OBJ_imap_messages:
2240 if (!obj->global_mode) {
2241 free(obj->data.mail);
2247 case OBJ_pop3_unseen:
2248 if (!obj->global_mode) {
2249 free(obj->data.mail);
2253 if (!obj->global_mode) {
2254 free(obj->data.mail);
2258 case OBJ_if_existing:
2259 case OBJ_if_mounted:
2260 case OBJ_if_running:
2261 free(obj->data.ifblock.s);
2262 free(obj->data.ifblock.str);
2265 free(obj->data.tail.logfile);
2266 free(obj->data.tail.buffer);
2278 case OBJ_iconv_start:
2283 case OBJ_disk_protect:
2284 free(objs[i].data.s);
2287 free(objs[i].data.ifblock.s);
2288 free(objs[i].data.ifblock.str);
2291 free(objs[i].data.ifblock.s);
2292 free(objs[i].data.ifblock.str);
2295 if (info.gw_info.iface) {
2296 free(info.gw_info.iface);
2297 info.gw_info.iface = 0;
2299 if (info.gw_info.ip) {
2300 free(info.gw_info.ip);
2301 info.gw_info.ip = 0;
2304 case OBJ_ioscheduler:
2306 free(objs[i].data.s);
2310 case OBJ_xmms2_artist:
2311 if (info.xmms2.artist) {
2312 free(info.xmms2.artist);
2313 info.xmms2.artist = 0;
2316 case OBJ_xmms2_album:
2317 if (info.xmms2.album) {
2318 free(info.xmms2.album);
2319 info.xmms2.album = 0;
2322 case OBJ_xmms2_title:
2323 if (info.xmms2.title) {
2324 free(info.xmms2.title);
2325 info.xmms2.title = 0;
2328 case OBJ_xmms2_genre:
2329 if (info.xmms2.genre) {
2330 free(info.xmms2.genre);
2331 info.xmms2.genre = 0;
2334 case OBJ_xmms2_comment:
2335 if (info.xmms2.comment) {
2336 free(info.xmms2.comment);
2337 info.xmms2.comment = 0;
2341 if (info.xmms2.url) {
2342 free(info.xmms2.url);
2346 case OBJ_xmms2_date:
2347 if (info.xmms2.date) {
2348 free(info.xmms2.date);
2349 info.xmms2.date = 0;
2352 case OBJ_xmms2_status:
2353 if (info.xmms2.status) {
2354 free(info.xmms2.status);
2355 info.xmms2.status = 0;
2358 case OBJ_xmms2_playlist:
2359 if (info.xmms2.playlist) {
2360 free(info.xmms2.playlist);
2361 info.xmms2.playlist = 0;
2364 case OBJ_xmms2_smart:
2365 if (info.xmms2.artist) {
2366 free(info.xmms2.artist);
2367 info.xmms2.artist = 0;
2369 if (info.xmms2.title) {
2370 free(info.xmms2.title);
2371 info.xmms2.title = 0;
2373 if (info.xmms2.url) {
2374 free(info.xmms2.url);
2380 case OBJ_bmpx_title:
2381 case OBJ_bmpx_artist:
2382 case OBJ_bmpx_album:
2383 case OBJ_bmpx_track:
2385 case OBJ_bmpx_bitrate:
2394 free(obj->data.rss.uri);
2395 free(obj->data.rss.action);
2404 case OBJ_battery_time:
2407 #endif /* !__OpenBSD__ */
2411 case OBJ_execigraph:
2412 free(obj->data.execi.cmd);
2413 free(obj->data.execi.buffer);
2416 free(obj->data.texeci.cmd);
2417 free(obj->data.texeci.buffer);
2419 case OBJ_nameserver:
2424 if (info.first_process) {
2425 free_all_processes();
2426 info.first_process = NULL;
2431 free(obj->data.hddtemp.dev);
2432 free(obj->data.hddtemp.addr);
2433 free(obj->data.hddtemp.temp);
2436 case OBJ_entropy_avail:
2437 case OBJ_entropy_poolsize:
2438 case OBJ_entropy_bar:
2440 case OBJ_user_names:
2441 if (info.users.names) {
2442 free(info.users.names);
2443 info.users.names = 0;
2446 case OBJ_user_terms:
2447 if (info.users.terms) {
2448 free(info.users.terms);
2449 info.users.terms = 0;
2452 case OBJ_user_times:
2453 if (info.users.times) {
2454 free(info.users.times);
2455 info.users.times = 0;
2460 case OBJ_smapi_bat_perc:
2461 case OBJ_smapi_bat_temp:
2462 case OBJ_smapi_bat_power:
2465 case OBJ_if_smapi_bat_installed:
2466 free(obj->data.ifblock.s);
2467 free(obj->data.ifblock.str);
2476 case OBJ_mpd_artist:
2478 case OBJ_mpd_random:
2479 case OBJ_mpd_repeat:
2481 case OBJ_mpd_bitrate:
2482 case OBJ_mpd_status:
2485 case OBJ_mpd_password:
2487 case OBJ_mpd_elapsed:
2488 case OBJ_mpd_length:
2492 case OBJ_mpd_percent:
2495 free_mpd_vars(&info.mpd);
2503 case OBJ_moc_artist:
2506 case OBJ_moc_totaltime:
2507 case OBJ_moc_timeleft:
2508 case OBJ_moc_curtime:
2509 case OBJ_moc_bitrate:
2511 free_moc(&info.moc);
2515 free(obj->data.scroll.text);
2519 if (full) {} // disable warning when MPD !defined
2520 free(text_object_list->text_objects);
2521 text_object_list->text_objects = NULL;
2522 text_object_list->text_object_count = 0;
2525 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2530 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2531 *a = mixer_init(buf1);
2532 scan_bar(arg + n, w, h);
2534 *a = mixer_init(NULL);
2535 scan_bar(arg, w, h);
2539 /* strip a leading /dev/ if any */
2540 #define DEV_NAME(x) x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 \
2543 /* construct_text_object() creates a new text_object */
2544 static struct text_object *construct_text_object(const char *s,
2545 const char *arg, unsigned int object_count,
2546 struct text_object *text_objects, long line, char allow_threaded)
2548 // struct text_object *obj = new_text_object();
2549 struct text_object *obj = new_text_object_internal();
2553 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
2554 obj->type = OBJ_##a; need_mask |= (1 << n); {
2555 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
2556 obj->type = OBJ_##a; need_mask |= (1 << n); {
2557 #define END } } else
2561 obj->type = OBJ_color;
2562 obj->data.l = get_x11_color(s);
2566 OBJ(freq, INFO_FREQ)
2569 obj->data.i = open_acpi_temperature(arg);
2570 END OBJ(acpitempf, 0)
2571 obj->data.i = open_acpi_temperature(arg);
2572 END OBJ(acpiacadapter, 0)
2573 END OBJ(freq, INFO_FREQ)
2574 #endif /* !__OpenBSD__ */
2576 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2577 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2578 obj->data.cpu_index = 1;
2579 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
2580 "Displaying the clock for CPU 1."); */
2582 obj->data.cpu_index = atoi(&arg[0]);
2585 END OBJ(freq_g, INFO_FREQ)
2587 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2588 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2589 obj->data.cpu_index = 1;
2590 /* ERR("freq_g: Invalid CPU number or you don't have that many "
2591 "CPUs! Displaying the clock for CPU 1."); */
2593 obj->data.cpu_index = atoi(&arg[0]);
2596 #if defined(__linux__)
2597 END OBJ(voltage_mv, 0)
2599 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2600 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2601 obj->data.cpu_index = 1;
2602 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
2603 "CPUs! Displaying voltage for CPU 1."); */
2605 obj->data.cpu_index = atoi(&arg[0]);
2608 END OBJ(voltage_v, 0)
2610 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2611 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2612 obj->data.cpu_index = 1;
2613 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
2614 "CPUs! Displaying voltage for CPU 1."); */
2616 obj->data.cpu_index = atoi(&arg[0]);
2621 END OBJ(wireless_essid, INFO_NET)
2623 obj->data.net = get_net_stat(arg);
2625 CRIT_ERR("wireless_essid: needs an argument");
2627 END OBJ(wireless_mode, INFO_NET)
2629 obj->data.net = get_net_stat(arg);
2631 CRIT_ERR("wireless_mode: needs an argument");
2633 END OBJ(wireless_bitrate, INFO_NET)
2635 obj->data.net = get_net_stat(arg);
2637 CRIT_ERR("wireless_bitrate: needs an argument");
2639 END OBJ(wireless_ap, INFO_NET)
2641 obj->data.net = get_net_stat(arg);
2643 CRIT_ERR("wireless_ap: needs an argument");
2645 END OBJ(wireless_link_qual, INFO_NET)
2647 obj->data.net = get_net_stat(arg);
2649 CRIT_ERR("wireless_link_qual: needs an argument");
2651 END OBJ(wireless_link_qual_max, INFO_NET)
2653 obj->data.net = get_net_stat(arg);
2655 CRIT_ERR("wireless_link_qual_max: needs an argument");
2657 END OBJ(wireless_link_qual_perc, INFO_NET)
2659 obj->data.net = get_net_stat(arg);
2661 CRIT_ERR("wireless_link_qual_perc: needs an argument");
2663 END OBJ(wireless_link_bar, INFO_NET)
2665 arg = scan_bar(arg, &obj->a, &obj->b);
2666 obj->data.net = get_net_stat(arg);
2668 CRIT_ERR("wireless_link_bar: needs an argument");
2670 #endif /* HAVE_IWLIB */
2672 #endif /* __linux__ */
2673 END OBJ(freq_dyn, 0)
2674 END OBJ(freq_dyn_g, 0)
2682 sscanf(arg, "%63s", bat);
2684 strcpy(bat, "BAT0");
2686 obj->data.s = strndup(bat, text_buffer_size);
2687 END OBJ(battery_time, 0)
2691 sscanf(arg, "%63s", bat);
2693 strcpy(bat, "BAT0");
2695 obj->data.s = strndup(bat, text_buffer_size);
2696 END OBJ(battery_percent, 0)
2700 sscanf(arg, "%63s", bat);
2702 strcpy(bat, "BAT0");
2704 obj->data.s = strndup(bat, text_buffer_size);
2705 END OBJ(battery_bar, 0)
2709 arg = scan_bar(arg, &obj->a, &obj->b);
2710 sscanf(arg, "%63s", bat);
2712 strcpy(bat, "BAT0");
2714 obj->data.s = strndup(bat, text_buffer_size);
2715 #endif /* !__OpenBSD__ */
2717 #if defined(__linux__)
2718 END OBJ(disk_protect, 0)
2720 obj->data.s = strndup(DEV_NAME(arg), text_buffer_size);
2722 CRIT_ERR("disk_protect needs an argument");
2723 END OBJ(i8k_version, INFO_I8K)
2724 END OBJ(i8k_bios, INFO_I8K)
2725 END OBJ(i8k_serial, INFO_I8K)
2726 END OBJ(i8k_cpu_temp, INFO_I8K)
2727 END OBJ(i8k_cpu_tempf, INFO_I8K)
2728 END OBJ(i8k_left_fan_status, INFO_I8K)
2729 END OBJ(i8k_right_fan_status, INFO_I8K)
2730 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2731 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2732 END OBJ(i8k_ac_status, INFO_I8K)
2733 END OBJ(i8k_buttons_status, INFO_I8K)
2735 END OBJ(ibm_temps, 0)
2737 CRIT_ERR("ibm_temps: needs an argument");
2739 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
2740 obj->data.sensor = 0;
2741 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
2742 "Using 0 (CPU temp sensor).");
2744 obj->data.sensor = atoi(&arg[0]);
2745 END OBJ(ibm_volume, 0)
2746 END OBJ(ibm_brightness, 0)
2748 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2749 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2752 ERR("if_up needs an argument");
2753 obj->data.ifblock.s = 0;
2755 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2756 blockstart[blockdepth] = object_count;
2757 obj->data.ifblock.pos = object_count + 2;
2760 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2761 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2763 blockstart[blockdepth] = object_count;
2764 obj->data.ifblock.pos = object_count + 2;
2766 END OBJ(ioscheduler, 0)
2768 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
2771 obj->data.s = strndup(DEV_NAME(arg), text_buffer_size);
2772 END OBJ(laptop_mode, 0)
2773 END OBJ(pb_battery, 0)
2774 if (arg && strcmp(arg, "status") == EQUAL) {
2775 obj->data.i = PB_BATT_STATUS;
2776 } else if (arg && strcmp(arg, "percent") == EQUAL) {
2777 obj->data.i = PB_BATT_PERCENT;
2778 } else if (arg && strcmp(arg, "time") == EQUAL) {
2779 obj->data.i = PB_BATT_TIME;
2781 ERR("pb_battery: needs one argument: status, percent or time");
2786 #endif /* __linux__ */
2787 #if defined(__OpenBSD__)
2788 END OBJ(obsd_sensors_temp, 0)
2790 CRIT_ERR("obsd_sensors_temp: needs an argument");
2792 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2793 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2794 obj->data.sensor = 0;
2795 ERR("Invalid temperature sensor number!");
2797 obj->data.sensor = atoi(&arg[0]);
2798 END OBJ(obsd_sensors_fan, 0)
2800 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
2803 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2804 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2805 obj->data.sensor = 0;
2806 ERR("Invalid fan sensor number!");
2808 obj->data.sensor = atoi(&arg[0]);
2809 END OBJ(obsd_sensors_volt, 0)
2811 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
2814 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2815 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2816 obj->data.sensor = 0;
2817 ERR("Invalid voltage sensor number!");
2819 obj->data.sensor = atoi(&arg[0]);
2820 END OBJ(obsd_vendor, 0)
2821 END OBJ(obsd_product, 0)
2822 #endif /* __OpenBSD__ */
2823 END OBJ(buffers, INFO_BUFFERS)
2824 END OBJ(cached, INFO_BUFFERS)
2825 END OBJ(cpu, INFO_CPU)
2827 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
2828 obj->data.cpu_index = atoi(&arg[3]);
2831 obj->data.cpu_index = 0;
2834 obj->data.cpu_index = 0;
2836 END OBJ(cpubar, INFO_CPU)
2838 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
2839 obj->data.cpu_index = atoi(&arg[3]);
2842 obj->data.cpu_index = 0;
2844 scan_bar(arg, &obj->a, &obj->b);
2846 scan_bar(arg, &obj->a, &obj->b);
2847 obj->data.cpu_index = 0;
2849 END OBJ(cpugraph, INFO_CPU)
2850 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2851 &obj->e, &obj->showaslog);
2854 if (strncmp(buf, "cpu", 3) == EQUAL && isdigit(buf[3])) {
2855 obj->data.cpu_index = atoi(&buf[3]);
2857 obj->data.cpu_index = 0;
2861 END OBJ(loadgraph, INFO_LOADAVG)
2862 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2863 &obj->e, &obj->showaslog);
2867 r = sscanf(arg, "%d", &a);
2869 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2872 #if defined(__linux__)
2873 END OBJ(diskio, INFO_DISKIO)
2875 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2877 obj->data.diskio = NULL;
2879 END OBJ(diskio_read, INFO_DISKIO)
2881 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2883 obj->data.diskio = NULL;
2885 END OBJ(diskio_write, INFO_DISKIO)
2887 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2889 obj->data.diskio = NULL;
2891 END OBJ(diskiograph, INFO_DISKIO)
2892 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2893 &obj->e, &obj->showaslog);
2896 obj->data.diskio = prepare_diskio_stat(buf);
2899 obj->data.diskio = NULL;
2901 END OBJ(diskiograph_read, INFO_DISKIO)
2902 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2903 &obj->e, &obj->showaslog);
2906 obj->data.diskio = prepare_diskio_stat(buf);
2909 obj->data.diskio = NULL;
2911 END OBJ(diskiograph_write, INFO_DISKIO)
2912 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2913 &obj->e, &obj->showaslog);
2916 obj->data.diskio = prepare_diskio_stat(buf);
2919 obj->data.diskio = NULL;
2924 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2927 obj->data.l = color0;
2929 obj->data.l = color1;
2931 obj->data.l = color2;
2933 obj->data.l = color3;
2935 obj->data.l = color4;
2937 obj->data.l = color5;
2939 obj->data.l = color6;
2941 obj->data.l = color7;
2943 obj->data.l = color8;
2945 obj->data.l = color9;
2947 obj->data.s = scan_font(arg);
2948 END OBJ(conky_version, 0)
2949 END OBJ(conky_build_date, 0)
2950 END OBJ(conky_build_arch, 0)
2951 END OBJ(downspeed, INFO_NET)
2953 obj->data.net = get_net_stat(arg);
2955 CRIT_ERR("downspeed needs argument");
2957 END OBJ(downspeedf, INFO_NET)
2959 obj->data.net = get_net_stat(arg);
2961 CRIT_ERR("downspeedf needs argument");
2963 END OBJ(downspeedgraph, INFO_NET)
2964 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2965 &obj->e, &obj->showaslog);
2967 // default to DEFAULTNETDEV
2968 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
2969 obj->data.net = get_net_stat(buf);
2973 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos =
2975 blockstart[blockdepth - 1] = object_count;
2976 obj->data.ifblock.pos = object_count + 2;
2978 ERR("$else: no matching $if_*");
2983 text_objects[blockstart[blockdepth]].data.ifblock.pos =
2986 ERR("$endif: no matching $if_*");
2989 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2992 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2994 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2996 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2997 END OBJ(execgraph, 0)
2998 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2999 END OBJ(execibar, 0)
3002 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3005 ERR("${execibar <interval> command}");
3006 obj->type = OBJ_text;
3007 snprintf(buf, 256, "${%s}", s);
3008 obj->data.s = strndup(buf, text_buffer_size);
3010 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3012 END OBJ(execigraph, 0)
3015 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3018 ERR("${execigraph <interval> command}");
3019 obj->type = OBJ_text;
3020 snprintf(buf, 256, "${%s}", s);
3021 obj->data.s = strndup(buf, text_buffer_size);
3023 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3028 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3031 ERR("${execi <interval> command}");
3032 obj->type = OBJ_text;
3033 snprintf(buf, 256, "${%s}", s);
3034 obj->data.s = strndup(buf, text_buffer_size);
3036 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3037 obj->data.execi.buffer = malloc(text_buffer_size);
3042 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3045 ERR("${execi <interval> command}");
3046 obj->type = OBJ_text;
3047 snprintf(buf, 256, "${%s}", s);
3048 obj->data.s = strndup(buf, text_buffer_size);
3050 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3051 obj->data.execi.buffer = malloc(text_buffer_size);
3053 END OBJ_THREAD(texeci, 0)
3056 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
3059 ERR("${texeci <interval> command}");
3060 obj->type = OBJ_text;
3061 snprintf(buf, 256, "${%s}", s);
3062 obj->data.s = strndup(buf, text_buffer_size);
3064 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
3065 obj->data.texeci.buffer = malloc(text_buffer_size);
3067 obj->data.texeci.p_timed_thread = NULL;
3068 END OBJ(pre_exec, 0)
3069 obj->type = OBJ_text;
3073 read_exec(arg, buf, sizeof(buf));
3074 obj->data.s = strndup(buf, text_buffer_size);
3076 obj->data.s = strndup("", text_buffer_size);
3079 END OBJ(fs_bar, INFO_FS)
3080 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
3082 while (isspace(*arg)) {
3091 obj->data.fsbar.fs = prepare_fs_stat(arg);
3092 END OBJ(fs_bar_free, INFO_FS)
3093 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
3095 while (isspace(*arg)) {
3105 obj->data.fsbar.fs = prepare_fs_stat(arg);
3106 END OBJ(fs_free, INFO_FS)
3110 obj->data.fs = prepare_fs_stat(arg);
3111 END OBJ(fs_used_perc, INFO_FS)
3115 obj->data.fs = prepare_fs_stat(arg);
3116 END OBJ(fs_free_perc, INFO_FS)
3120 obj->data.fs = prepare_fs_stat(arg);
3121 END OBJ(fs_size, INFO_FS)
3125 obj->data.fs = prepare_fs_stat(arg);
3126 END OBJ(fs_type, INFO_FS)
3130 obj->data.fs = prepare_fs_stat(arg);
3131 END OBJ(fs_used, INFO_FS)
3135 obj->data.fs = prepare_fs_stat(arg);
3137 obj->data.i = arg ? atoi(arg) : 1;
3138 END OBJ(nameserver, INFO_DNS)
3139 obj->data.i = arg ? atoi(arg) : 0;
3141 obj->data.i = arg ? atoi(arg) : 1;
3143 obj->data.i = arg ? atoi(arg) : 1;
3147 ERR("goto needs arguments");
3148 obj->type = OBJ_text;
3149 obj->data.s = strndup("${goto}", text_buffer_size);
3153 obj->data.i = atoi(arg);
3159 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3160 sscanf(arg, "%d", &b);
3166 obj->data.pair.a = a;
3167 obj->data.pair.b = b;
3170 END OBJ(i2c, INFO_SYSFS)
3171 char buf1[64], buf2[64];
3175 ERR("i2c needs arguments");
3176 obj->type = OBJ_text;
3177 // obj->data.s = strndup("${i2c}", text_buffer_size);
3181 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3182 /* if scanf couldn't read three values, read type and num and use
3184 sscanf(arg, "%63s %d", buf2, &n);
3185 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
3186 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3187 strncpy(obj->data.sysfs.type, buf2, 63);
3189 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
3190 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3191 strncpy(obj->data.sysfs.type, buf2, 63);
3194 END OBJ(platform, INFO_SYSFS)
3195 char buf1[64], buf2[64];
3199 ERR("platform needs arguments");
3200 obj->type = OBJ_text;
3204 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3205 /* if scanf couldn't read three values, read type and num and use
3207 sscanf(arg, "%63s %d", buf2, &n);
3208 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
3209 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3210 strncpy(obj->data.sysfs.type, buf2, 63);
3212 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
3213 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3214 strncpy(obj->data.sysfs.type, buf2, 63);
3217 END OBJ(hwmon, INFO_SYSFS)
3218 char buf1[64], buf2[64];
3222 ERR("hwmon needs argumanets");
3223 obj->type = OBJ_text;
3227 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3228 /* if scanf couldn't read three values, read type and num and use
3230 sscanf(arg, "%63s %d", buf2, &n);
3231 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
3232 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3233 strncpy(obj->data.sysfs.type, buf2, 63);
3235 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
3236 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3237 strncpy(obj->data.sysfs.type, buf2, 63);
3239 #endif /* !__OpenBSD__ */
3241 END OBJ(top, INFO_TOP)
3246 ERR("top needs arguments");
3247 obj->type = OBJ_text;
3248 // obj->data.s = strndup("${top}", text_buffer_size);
3251 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3252 if (strcmp(buf, "name") == EQUAL) {
3253 obj->data.top.type = TOP_NAME;
3254 } else if (strcmp(buf, "cpu") == EQUAL) {
3255 obj->data.top.type = TOP_CPU;
3256 } else if (strcmp(buf, "pid") == EQUAL) {
3257 obj->data.top.type = TOP_PID;
3258 } else if (strcmp(buf, "mem") == EQUAL) {
3259 obj->data.top.type = TOP_MEM;
3260 } else if (strcmp(buf, "time") == EQUAL) {
3261 obj->data.top.type = TOP_TIME;
3262 } else if (strcmp(buf, "mem_res") == EQUAL) {
3263 obj->data.top.type = TOP_MEM_RES;
3264 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
3265 obj->data.top.type = TOP_MEM_VSIZE;
3267 ERR("invalid arg for top");
3270 if (n < 1 || n > 10) {
3271 CRIT_ERR("invalid arg for top");
3274 obj->data.top.num = n - 1;
3278 ERR("invalid args given for top");
3281 END OBJ(top_mem, INFO_TOP)
3286 ERR("top_mem needs arguments");
3287 obj->type = OBJ_text;
3288 obj->data.s = strndup("${top_mem}", text_buffer_size);
3291 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3292 if (strcmp(buf, "name") == EQUAL) {
3293 obj->data.top.type = TOP_NAME;
3294 } else if (strcmp(buf, "cpu") == EQUAL) {
3295 obj->data.top.type = TOP_CPU;
3296 } else if (strcmp(buf, "pid") == EQUAL) {
3297 obj->data.top.type = TOP_PID;
3298 } else if (strcmp(buf, "mem") == EQUAL) {
3299 obj->data.top.type = TOP_MEM;
3300 } else if (strcmp(buf, "time") == EQUAL) {
3301 obj->data.top.type = TOP_TIME;
3302 } else if (strcmp(buf, "mem_res") == EQUAL) {
3303 obj->data.top.type = TOP_MEM_RES;
3304 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
3305 obj->data.top.type = TOP_MEM_VSIZE;
3307 ERR("invalid arg for top");
3310 if (n < 1 || n > 10) {
3311 CRIT_ERR("invalid arg for top");
3314 obj->data.top.num = n - 1;
3318 ERR("invalid args given for top");
3321 END OBJ(addr, INFO_NET)
3323 obj->data.net = get_net_stat(arg);
3325 CRIT_ERR("addr needs argument");
3327 #if defined(__linux__)
3328 END OBJ(addrs, INFO_NET)
3330 obj->data.net = get_net_stat(arg);
3332 CRIT_ERR("addrs needs argument");
3334 #endif /* __linux__ */
3341 ERR("tail needs arguments");
3342 obj->type = OBJ_text;
3343 obj->data.s = strndup("${tail}", text_buffer_size);
3346 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3347 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3348 CRIT_ERR("invalid arg for tail, number of lines must be "
3349 "between 1 and %i", MAX_TAIL_LINES);
3355 obj->data.tail.fd = -1;
3357 if (stat(buf, &st) == 0) {
3358 if (S_ISFIFO(st.st_mode)) {
3359 fd = open(buf, O_RDONLY | O_NONBLOCK);
3362 CRIT_ERR("tail logfile does not exist, or you do "
3363 "not have correct permissions");
3366 obj->data.tail.fd = fd;
3368 fp = fopen(buf, "r");
3372 if (fp || obj->data.tail.fd != -1) {
3373 obj->data.tail.logfile = malloc(text_buffer_size);
3374 strcpy(obj->data.tail.logfile, buf);
3375 obj->data.tail.wantedlines = n1;
3376 obj->data.tail.interval = update_interval * 2;
3378 if (obj->data.tail.fd == -1) {
3383 CRIT_ERR("tail logfile does not exist, or you do not have "
3384 "correct permissions");
3387 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3388 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3389 CRIT_ERR("invalid arg for tail, number of lines must be "
3390 "between 1 and %i", MAX_TAIL_LINES);
3392 } else if (n2 < 1 || n2 < update_interval) {
3393 CRIT_ERR("invalid arg for tail, interval must be greater than "
3394 "0 and "PACKAGE_NAME"'s interval");
3400 obj->data.tail.fd = -1;
3402 if (stat(buf, &st) == 0) {
3403 if (S_ISFIFO(st.st_mode)) {
3404 fd = open(buf, O_RDONLY | O_NONBLOCK);
3407 CRIT_ERR("tail logfile does not exist, or you do "
3408 "not have correct permissions");
3411 obj->data.tail.fd = fd;
3413 fp = fopen(buf, "r");
3417 if (fp || obj->data.tail.fd != -1) {
3418 obj->data.tail.logfile = malloc(text_buffer_size);
3419 strcpy(obj->data.tail.logfile, buf);
3420 obj->data.tail.wantedlines = n1;
3421 obj->data.tail.interval = n2;
3423 if (obj->data.tail.fd == -1) {
3428 CRIT_ERR("tail logfile does not exist, or you do not have "
3429 "correct permissions");
3433 ERR("invalid args given for tail");
3436 /* asumming all else worked */
3437 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3443 ERR("head needs arguments");
3444 obj->type = OBJ_text;
3445 obj->data.s = strndup("${head}", text_buffer_size);
3448 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3449 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3450 CRIT_ERR("invalid arg for head, number of lines must be "
3451 "between 1 and %i", MAX_TAIL_LINES);
3456 fp = fopen(buf, "r");
3458 obj->data.tail.logfile = malloc(text_buffer_size);
3459 strcpy(obj->data.tail.logfile, buf);
3460 obj->data.tail.wantedlines = n1;
3461 obj->data.tail.interval = update_interval * 2;
3465 CRIT_ERR("head logfile does not exist, or you do not have "
3466 "correct permissions");
3469 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3470 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3471 CRIT_ERR("invalid arg for head, number of lines must be "
3472 "between 1 and %i", MAX_TAIL_LINES);
3474 } else if (n2 < 1 || n2 < update_interval) {
3475 CRIT_ERR("invalid arg for head, interval must be greater than "
3476 "0 and "PACKAGE_NAME"'s interval");
3481 fp = fopen(buf, "r");
3483 obj->data.tail.logfile = malloc(text_buffer_size);
3484 strcpy(obj->data.tail.logfile, buf);
3485 obj->data.tail.wantedlines = n1;
3486 obj->data.tail.interval = n2;
3490 CRIT_ERR("head logfile does not exist, or you do not have "
3491 "correct permissions");
3495 ERR("invalid args given for head");
3498 /* asumming all else worked */
3499 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3502 obj->data.s = strdup(arg);
3504 CRIT_ERR("lines needs a argument");
3508 obj->data.s = strdup(arg);
3510 CRIT_ERR("words needs a argument");
3512 END OBJ(loadavg, INFO_LOADAVG)
3513 int a = 1, b = 2, c = 3, r = 3;
3516 r = sscanf(arg, "%d %d %d", &a, &b, &c);
3517 if (r >= 3 && (c < 1 || c > 3)) {
3520 if (r >= 2 && (b < 1 || b > 3)) {
3523 if (r >= 1 && (a < 1 || a > 3)) {
3527 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
3528 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
3529 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
3530 END OBJ(if_empty, 0)
3531 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3532 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3535 ERR("if_empty needs an argument");
3536 obj->data.ifblock.s = 0;
3538 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3540 blockstart[blockdepth] = object_count;
3541 obj->data.ifblock.pos = object_count + 2;
3543 END OBJ(if_existing, 0)
3544 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3545 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3548 ERR("if_existing needs an argument or two");
3549 obj->data.ifblock.s = NULL;
3550 obj->data.ifblock.str = NULL;
3552 char buf1[256], buf2[256];
3553 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3556 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
3557 obj->data.ifblock.str = NULL;
3559 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
3560 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
3563 blockstart[blockdepth] = object_count;
3564 obj->data.ifblock.pos = object_count + 2;
3566 END OBJ(if_mounted, 0)
3567 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3568 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3571 ERR("if_mounted needs an argument");
3572 obj->data.ifblock.s = 0;
3574 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3576 blockstart[blockdepth] = object_count;
3577 obj->data.ifblock.pos = object_count + 2;
3579 END OBJ(if_running, 0)
3580 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3581 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3586 snprintf(buf, 256, "pidof %s >/dev/null", arg);
3587 obj->data.ifblock.s = strndup(buf, text_buffer_size);
3589 ERR("if_running needs an argument");
3590 obj->data.ifblock.s = 0;
3592 blockstart[blockdepth] = object_count;
3593 obj->data.ifblock.pos = object_count + 2;
3599 char box[256], dst[256];
3603 /* Kapil: Changed from MAIL_FILE to
3604 current_mail_spool since the latter
3605 is a copy of the former if undefined
3606 but the latter should take precedence
3608 strncpy(box, current_mail_spool, sizeof(box));
3610 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3612 strncpy(box, arg, sizeof(box));
3616 variable_substitute(box, dst, sizeof(dst));
3617 obj->data.local_mail.box = strndup(dst, text_buffer_size);
3618 obj->data.local_mail.interval = n1;
3619 END OBJ(mboxscan, 0)
3620 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
3621 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
3622 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
3623 obj->data.mboxscan.output[0] = 1;
3624 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
3625 END OBJ(mem, INFO_MEM)
3626 END OBJ(memeasyfree, INFO_MEM)
3627 END OBJ(memfree, INFO_MEM)
3628 END OBJ(memmax, INFO_MEM)
3629 END OBJ(memperc, INFO_MEM)
3630 END OBJ(membar, INFO_MEM)
3631 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3632 END OBJ(memgraph, INFO_MEM)
3633 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3634 &obj->e, &obj->showaslog);
3639 END OBJ(mixer, INFO_MIXER)
3640 obj->data.l = mixer_init(arg);
3641 END OBJ(mixerl, INFO_MIXER)
3642 obj->data.l = mixer_init(arg);
3643 END OBJ(mixerr, INFO_MIXER)
3644 obj->data.l = mixer_init(arg);
3645 END OBJ(mixerbar, INFO_MIXER)
3646 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3647 &obj->data.mixerbar.h);
3648 END OBJ(mixerlbar, INFO_MIXER)
3649 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3650 &obj->data.mixerbar.h);
3651 END OBJ(mixerrbar, INFO_MIXER)
3652 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3653 &obj->data.mixerbar.h);
3655 END OBJ(monitor, INFO_X11)
3656 END OBJ(monitor_number, INFO_X11)
3658 END OBJ(new_mails, 0)
3660 char box[256], dst[256];
3664 /* Kapil: Changed from MAIL_FILE to
3665 current_mail_spool since the latter
3666 is a copy of the former if undefined
3667 but the latter should take precedence
3669 strncpy(box, current_mail_spool, sizeof(box));
3671 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3673 strncpy(box, arg, sizeof(box));
3677 variable_substitute(box, dst, sizeof(dst));
3678 obj->data.local_mail.box = strndup(dst, text_buffer_size);
3679 obj->data.local_mail.interval = n1;
3680 END OBJ(nodename, 0)
3681 END OBJ(processes, INFO_PROCS)
3682 END OBJ(running_processes, INFO_RUN_PROCS)
3683 END OBJ(shadecolor, 0)
3685 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
3687 END OBJ(outlinecolor, 0)
3689 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
3691 END OBJ(stippled_hr, 0)
3693 int a = stippled_borders, b = 1;
3696 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3697 sscanf(arg, "%d", &b);
3703 obj->data.pair.a = a;
3704 obj->data.pair.b = b;
3706 END OBJ(swap, INFO_MEM)
3707 END OBJ(swapmax, INFO_MEM)
3708 END OBJ(swapperc, INFO_MEM)
3709 END OBJ(swapbar, INFO_MEM)
3710 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3713 END OBJ(temp1, INFO_SYSFS)
3714 obj->type = OBJ_i2c;
3715 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
3716 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3717 END OBJ(temp2, INFO_SYSFS)
3718 obj->type = OBJ_i2c;
3719 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
3720 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3723 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
3725 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
3727 char buf1[256], buf2[256], *fmt, *tz;
3731 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3741 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
3742 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
3744 END OBJ(iconv_start, 0)
3745 if (iconv_converting) {
3746 CRIT_ERR("You must stop your last iconv conversion before "
3747 "starting another");
3750 char iconv_from[CODEPAGE_LENGTH];
3751 char iconv_to[CODEPAGE_LENGTH];
3753 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
3754 CRIT_ERR("Invalid arguments for iconv_start");
3758 new_iconv = iconv_open(iconv_to, iconv_from);
3759 if (new_iconv == (iconv_t) (-1)) {
3760 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
3762 obj->a = register_iconv(&new_iconv);
3763 iconv_converting = 1;
3767 CRIT_ERR("Iconv requires arguments");
3769 END OBJ(iconv_stop, 0)
3770 iconv_converting = 0;
3773 END OBJ(totaldown, INFO_NET)
3775 obj->data.net = get_net_stat(arg);
3777 CRIT_ERR("totaldown needs argument");
3779 END OBJ(totalup, INFO_NET)
3780 obj->data.net = get_net_stat(arg);
3782 obj->data.net = get_net_stat(arg);
3784 CRIT_ERR("totalup needs argument");
3788 obj->data.i = arg ? atoi(arg) : 0;
3790 obj->data.i = arg ? atoi(arg) : 0;
3791 END OBJ(upspeed, INFO_NET)
3793 obj->data.net = get_net_stat(arg);
3795 CRIT_ERR("upspeed needs argument");
3797 END OBJ(upspeedf, INFO_NET)
3799 obj->data.net = get_net_stat(arg);
3801 CRIT_ERR("upspeedf needs argument");
3804 END OBJ(upspeedgraph, INFO_NET)
3805 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3806 &obj->e, &obj->showaslog);
3808 // default to DEFAULTNETDEV
3809 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
3810 obj->data.net = get_net_stat(buf);
3812 END OBJ(uptime_short, INFO_UPTIME)
3813 END OBJ(uptime, INFO_UPTIME)
3814 END OBJ(user_names, INFO_USERS)
3815 END OBJ(user_times, INFO_USERS)
3816 END OBJ(user_terms, INFO_USERS)
3817 END OBJ(user_number, INFO_USERS)
3818 #if defined(__linux__)
3819 END OBJ(gw_iface, INFO_GW)
3820 END OBJ(gw_ip, INFO_GW)
3821 END OBJ(if_gw, INFO_GW)
3822 #endif /* !__linux__ */
3824 END OBJ(adt746xcpu, 0)
3825 END OBJ(adt746xfan, 0)
3826 #endif /* !__OpenBSD__ */
3827 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
3828 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
3829 END OBJ(apm_adapter, 0)
3830 END OBJ(apm_battery_life, 0)
3831 END OBJ(apm_battery_time, 0)
3832 #endif /* __FreeBSD__ */
3833 END OBJ_THREAD(imap_unseen, 0)
3836 obj->data.mail = parse_mail_args(IMAP, arg);
3837 obj->global_mode = 0;
3839 obj->global_mode = 1;
3841 END OBJ_THREAD(imap_messages, 0)
3844 obj->data.mail = parse_mail_args(IMAP, arg);
3845 obj->global_mode = 0;
3847 obj->global_mode = 1;
3849 END OBJ_THREAD(pop3_unseen, 0)
3852 obj->data.mail = parse_mail_args(POP3, arg);
3853 obj->global_mode = 0;
3855 obj->global_mode = 1;
3857 END OBJ_THREAD(pop3_used, 0)
3860 obj->data.mail = parse_mail_args(POP3, arg);
3861 obj->global_mode = 0;
3863 obj->global_mode = 1;
3868 obj->data.s = strndup(arg, text_buffer_size);
3870 ERR("smapi needs an argument");
3871 END OBJ(if_smapi_bat_installed, 0)
3872 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3873 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3876 ERR("if_smapi_bat_installed needs an argument");
3877 obj->data.ifblock.s = 0;
3879 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3880 blockstart[blockdepth] = object_count;
3881 obj->data.ifblock.pos = object_count + 2;
3883 END OBJ(smapi_bat_perc, 0)
3885 obj->data.s = strndup(arg, text_buffer_size);
3887 ERR("smapi_bat_perc needs an argument");
3888 END OBJ(smapi_bat_temp, 0)
3890 obj->data.s = strndup(arg, text_buffer_size);
3892 ERR("smapi_bat_temp needs an argument");
3893 END OBJ(smapi_bat_power, 0)
3895 obj->data.s = strndup(arg, text_buffer_size);
3897 ERR("smapi_bat_power needs an argument");
3898 END OBJ(smapi_bat_bar, 0)
3901 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
3902 ERR("first argument to smapi_bat_bar must be an integer value");
3906 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
3909 ERR("if_smapi_bat_bar needs an argument");
3912 END OBJ_THREAD(mpd_artist, INFO_MPD)
3913 END OBJ_THREAD(mpd_title, INFO_MPD)
3915 sscanf(arg, "%d", &obj->data.i);
3916 if (obj->data.i > 0) {
3919 CRIT_ERR("mpd_title: invalid length argument");
3924 END OBJ_THREAD(mpd_random, INFO_MPD)
3925 END OBJ_THREAD(mpd_repeat, INFO_MPD)
3926 END OBJ_THREAD(mpd_elapsed, INFO_MPD)
3927 END OBJ_THREAD(mpd_length, INFO_MPD)
3928 END OBJ_THREAD(mpd_track, INFO_MPD)
3929 END OBJ_THREAD(mpd_name, INFO_MPD)
3930 END OBJ_THREAD(mpd_file, INFO_MPD)
3931 END OBJ_THREAD(mpd_percent, INFO_MPD)
3932 END OBJ_THREAD(mpd_album, INFO_MPD)
3933 END OBJ_THREAD(mpd_vol, INFO_MPD)
3934 END OBJ_THREAD(mpd_bitrate, INFO_MPD)
3935 END OBJ_THREAD(mpd_status, INFO_MPD)
3936 END OBJ_THREAD(mpd_bar, INFO_MPD)
3937 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3938 END OBJ_THREAD(mpd_smart, INFO_MPD)
3940 sscanf(arg, "%d", &obj->data.i);
3941 if (obj->data.i > 0) {
3944 CRIT_ERR("mpd_smart: invalid length argument");
3951 END OBJ_THREAD(moc_state, INFO_MOC)
3952 END OBJ_THREAD(moc_file, INFO_MOC)
3953 END OBJ_THREAD(moc_title, INFO_MOC)
3954 END OBJ_THREAD(moc_artist, INFO_MOC)
3955 END OBJ_THREAD(moc_song, INFO_MOC)
3956 END OBJ_THREAD(moc_album, INFO_MOC)
3957 END OBJ_THREAD(moc_totaltime, INFO_MOC)
3958 END OBJ_THREAD(moc_timeleft, INFO_MOC)
3959 END OBJ_THREAD(moc_curtime, INFO_MOC)
3960 END OBJ_THREAD(moc_bitrate, INFO_MOC)
3961 END OBJ_THREAD(moc_rate, INFO_MOC)
3964 END OBJ(xmms2_artist, INFO_XMMS2)
3965 END OBJ(xmms2_album, INFO_XMMS2)
3966 END OBJ(xmms2_title, INFO_XMMS2)
3967 END OBJ(xmms2_genre, INFO_XMMS2)
3968 END OBJ(xmms2_comment, INFO_XMMS2)
3969 END OBJ(xmms2_url, INFO_XMMS2)
3970 END OBJ(xmms2_tracknr, INFO_XMMS2)
3971 END OBJ(xmms2_bitrate, INFO_XMMS2)
3972 END OBJ(xmms2_date, INFO_XMMS2)
3973 END OBJ(xmms2_id, INFO_XMMS2)
3974 END OBJ(xmms2_duration, INFO_XMMS2)
3975 END OBJ(xmms2_elapsed, INFO_XMMS2)
3976 END OBJ(xmms2_size, INFO_XMMS2)
3977 END OBJ(xmms2_status, INFO_XMMS2)
3978 END OBJ(xmms2_percent, INFO_XMMS2)
3979 END OBJ(xmms2_bar, INFO_XMMS2)
3980 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3981 END OBJ(xmms2_smart, INFO_XMMS2)
3982 END OBJ(xmms2_playlist, INFO_XMMS2)
3983 END OBJ(xmms2_timesplayed, INFO_XMMS2)
3986 END OBJ(audacious_status, INFO_AUDACIOUS)
3987 END OBJ(audacious_title, INFO_AUDACIOUS)
3989 sscanf(arg, "%d", &info.audacious.max_title_len);
3990 if (info.audacious.max_title_len > 0) {
3991 info.audacious.max_title_len++;
3993 CRIT_ERR("audacious_title: invalid length argument");
3996 END OBJ(audacious_length, INFO_AUDACIOUS)
3997 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
3998 END OBJ(audacious_position, INFO_AUDACIOUS)
3999 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
4000 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
4001 END OBJ(audacious_frequency, INFO_AUDACIOUS)
4002 END OBJ(audacious_channels, INFO_AUDACIOUS)
4003 END OBJ(audacious_filename, INFO_AUDACIOUS)
4004 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
4005 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
4006 END OBJ(audacious_bar, INFO_AUDACIOUS)
4007 scan_bar(arg, &obj->a, &obj->b);
4010 END OBJ(bmpx_title, INFO_BMPX)
4011 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4012 END OBJ(bmpx_artist, INFO_BMPX)
4013 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4014 END OBJ(bmpx_album, INFO_BMPX)
4015 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4016 END OBJ(bmpx_track, INFO_BMPX)
4017 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4018 END OBJ(bmpx_uri, INFO_BMPX)
4019 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4020 END OBJ(bmpx_bitrate, INFO_BMPX)
4021 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4027 char *userid = (char *) malloc(20 * sizeof(char));
4028 char *apikey = (char *) malloc(64 * sizeof(char));
4029 char *charid = (char *) malloc(20 * sizeof(char));
4031 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
4032 obj->data.eve.charid = charid;
4033 obj->data.eve.userid = userid;
4034 obj->data.eve.apikey = apikey;
4038 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
4044 int argc, delay, act_par;
4045 char *uri = (char *) malloc(128 * sizeof(char));
4046 char *action = (char *) malloc(64 * sizeof(char));
4048 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
4050 obj->data.rss.uri = uri;
4051 obj->data.rss.delay = delay;
4052 obj->data.rss.action = action;
4053 obj->data.rss.act_par = act_par;
4057 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
4063 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
4064 &obj->data.hddtemp.addr, &obj->data.hddtemp.port, &obj->data.hddtemp.temp)) {
4065 ERR("hddtemp needs arguments");
4066 obj->type = OBJ_text;
4067 obj->data.s = strndup("${hddtemp}", text_buffer_size);
4068 obj->data.hddtemp.update_time = 0;
4072 #ifdef TCP_PORT_MONITOR
4073 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
4074 int argc, port_begin, port_end, item, connection_index;
4077 memset(itembuf, 0, sizeof(itembuf));
4078 connection_index = 0;
4079 /* massive argument checking */
4081 CRIT_ERR("tcp_portmon: needs arguments");
4083 argc = sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf,
4085 if ((argc != 3) && (argc != 4)) {
4086 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
4088 if ((port_begin < 1) || (port_begin > 65535) || (port_end < 1)
4089 || (port_end > 65535)) {
4090 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
4092 if (port_begin > port_end) {
4093 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
4095 if (strncmp(itembuf, "count", 31) == EQUAL) {
4097 } else if (strncmp(itembuf, "rip", 31) == EQUAL) {
4099 } else if (strncmp(itembuf, "rhost", 31) == EQUAL) {
4101 } else if (strncmp(itembuf, "rport", 31) == EQUAL) {
4103 } else if (strncmp(itembuf, "rservice", 31) == EQUAL) {
4104 item = REMOTESERVICE;
4105 } else if (strncmp(itembuf, "lip", 31) == EQUAL) {
4107 } else if (strncmp(itembuf, "lhost", 31) == EQUAL) {
4109 } else if (strncmp(itembuf, "lport", 31) == EQUAL) {
4111 } else if (strncmp(itembuf, "lservice", 31) == EQUAL) {
4112 item = LOCALSERVICE;
4114 CRIT_ERR("tcp_portmon: invalid item specified");
4116 if ((argc == 3) && (item != COUNT)) {
4117 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" "
4120 if ((argc == 4) && (connection_index < 0)) {
4121 CRIT_ERR("tcp_portmon: connection index must be non-negative");
4123 /* ok, args looks good. save the text object data */
4124 obj->data.tcp_port_monitor.port_range_begin = (in_port_t) port_begin;
4125 obj->data.tcp_port_monitor.port_range_end = (in_port_t) port_end;
4126 obj->data.tcp_port_monitor.item = item;
4127 obj->data.tcp_port_monitor.connection_index = connection_index;
4129 /* if the port monitor collection hasn't been created,
4130 * we must create it */
4131 if (!info.p_tcp_port_monitor_collection) {
4132 info.p_tcp_port_monitor_collection =
4133 create_tcp_port_monitor_collection();
4134 if (!info.p_tcp_port_monitor_collection) {
4135 CRIT_ERR("tcp_portmon: unable to create port monitor "
4140 /* if a port monitor for this port does not exist,
4141 * create one and add it to the collection */
4142 if (find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
4143 port_begin, port_end) == NULL) {
4144 tcp_port_monitor_t *p_monitor = create_tcp_port_monitor(port_begin,
4145 port_end, &tcp_port_monitor_args);
4148 CRIT_ERR("tcp_portmon: unable to create port monitor");
4150 /* add the newly created monitor to the collection */
4151 if (insert_tcp_port_monitor_into_collection(
4152 info.p_tcp_port_monitor_collection, p_monitor) != 0) {
4153 CRIT_ERR("tcp_portmon: unable to add port monitor to "
4158 END OBJ(entropy_avail, INFO_ENTROPY)
4159 END OBJ(entropy_poolsize, INFO_ENTROPY)
4160 END OBJ(entropy_bar, INFO_ENTROPY)
4161 scan_bar(arg, &obj->a, &obj->b);
4165 obj->data.scroll.step = 1;
4166 if (arg && sscanf(arg, "%u %u %n", &obj->data.scroll.show, &obj->data.scroll.step, &n) > 0) {
4167 obj->data.scroll.text = strndup(arg + n, text_buffer_size);
4168 obj->data.scroll.start = 0;
4170 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
4175 CRIT_ERR("nvidia needs an argument\n");
4176 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
4177 CRIT_ERR("nvidia: invalid argument"
4178 " specified: '%s'\n", arg);
4184 ERR("unknown variable %s", s);
4185 obj->type = OBJ_text;
4186 snprintf(buf, 256, "${%s}", s);
4187 obj->data.s = strndup(buf, text_buffer_size);
4194 static struct text_object *create_plain_text(const char *s)
4196 struct text_object *obj;
4198 if (s == NULL || *s == '\0') {
4202 obj = new_text_object_internal();
4204 obj->type = OBJ_text;
4205 obj->data.s = strndup(s, text_buffer_size);
4209 static struct text_object_list *extract_variable_text_internal(const char *const_p, char allow_threaded)
4211 struct text_object_list *retval;
4212 struct text_object *obj;
4213 char *p, *s, *orig_p;
4216 p = strndup(const_p, max_user_text);
4219 retval = malloc(sizeof(struct text_object_list));
4220 memset(retval, 0, sizeof(struct text_object_list));
4221 retval->text_object_count = 0;
4223 line = global_text_lines;
4231 obj = create_plain_text(s);
4233 // allocate memory for the object
4234 retval->text_objects = realloc(retval->text_objects,
4235 sizeof(struct text_object) *
4236 (retval->text_object_count + 1));
4237 // assign the new object to the end of the list.
4238 memcpy(&retval->text_objects[retval->text_object_count++], obj,
4239 sizeof(struct text_object));
4251 /* variable is either $foo or ${foo} */
4253 unsigned int brl = 1, brr = 0;
4257 while (*p && brl != brr) {
4272 while (*p && (isalnum((int) *p) || *p == '_')) {
4277 /* copy variable to buffer */
4278 len = (p - s > 255) ? 255 : (p - s);
4279 strncpy(buf, s, len);
4289 /* if variable wasn't found in environment, use some special */
4295 if (strchr(buf, ' ')) {
4296 arg = strchr(buf, ' ');
4299 while (isspace((int) *arg)) {
4307 /* lowercase variable name */
4310 *tmp_p = tolower(*tmp_p);
4314 // create new object
4315 obj = construct_text_object(buf, arg,
4316 retval->text_object_count, retval->text_objects, line, allow_threaded);
4318 // allocate memory for the object
4319 retval->text_objects = realloc(retval->text_objects,
4320 sizeof(struct text_object) *
4321 (retval->text_object_count + 1));
4322 // assign the new object to the end of the list.
4324 &retval->text_objects[retval->text_object_count++],
4325 obj, sizeof(struct text_object));
4331 obj = create_plain_text("$");
4333 // allocate memory for the object
4334 retval->text_objects = realloc(retval->text_objects,
4335 sizeof(struct text_object) *
4336 (retval->text_object_count + 1));
4337 // assign the new object to the end of the list.
4338 memcpy(&retval->text_objects[retval->text_object_count++],
4339 obj, sizeof(struct text_object));
4346 obj = create_plain_text(s);
4348 // allocate memory for the object
4349 retval->text_objects = realloc(retval->text_objects,
4350 sizeof(struct text_object) * (retval->text_object_count + 1));
4351 // assign the new object to the end of the list.
4352 memcpy(&retval->text_objects[retval->text_object_count++], obj,
4353 sizeof(struct text_object));
4358 ERR("one or more $endif's are missing");
4365 static void extract_variable_text(const char *p)
4367 free_text_objects(global_text_object_list, 1);
4368 free(global_text_object_list);
4382 global_text_object_list = extract_variable_text_internal(p, 1);
4385 struct text_object_list *parse_conky_vars(char *txt, char *p, struct information *cur)
4387 struct text_object_list *object_list =
4388 extract_variable_text_internal(txt, 0);
4390 generate_text_internal(p, max_user_text, object_list, cur);
4394 /* Allows reading from a FIFO (i.e., /dev/xconsole).
4395 * The file descriptor is set to non-blocking which makes this possible.
4397 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
4398 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
4400 #define TAIL_PIPE_BUFSIZE 4096
4404 int fd = obj->data.tail.fd;
4407 char buf[TAIL_PIPE_BUFSIZE];
4408 ssize_t len = read(fd, buf, sizeof(buf));
4412 if (errno != EAGAIN) {
4413 strcpy(obj->data.tail.buffer, "Logfile Read Error");
4414 snprintf(dst, dst_size, "Logfile Read Error");
4418 } else if (len == 0) {
4419 strcpy(obj->data.tail.buffer, "Logfile Empty");
4420 snprintf(dst, dst_size, "Logfile Empty");
4424 for (line_len = 0, i = 0; i < len; i++) {
4428 if (buf[i] == '\n') {
4431 if (obj->data.tail.readlines > 0) {
4436 for (n = 0; obj->data.tail.buffer[n]; n++) {
4437 if (obj->data.tail.buffer[n] == '\n') {
4442 if (++olines < obj->data.tail.wantedlines) {
4448 p = obj->data.tail.buffer + first_line;
4449 pos = n - first_line;
4450 memmove(obj->data.tail.buffer,
4451 obj->data.tail.buffer + first_line, strlen(p));
4452 obj->data.tail.buffer[pos] = 0;
4458 p = buf + last_line;
4460 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
4461 obj->data.tail.buffer[pos + line_len] = 0;
4464 obj->data.tail.readlines = lines;
4472 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
4475 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
4476 void *thread(void *), const char *text)
4478 if (obj->global_mode && info.mail) {
4479 // this means we use info
4480 if (!info.mail->p_timed_thread) {
4481 info.mail->p_timed_thread =
4482 timed_thread_create(thread,
4483 (void *) info.mail, info.mail->interval * 1000000);
4484 if (!info.mail->p_timed_thread) {
4485 ERR("Error creating %s timed thread", text);
4487 timed_thread_register(info.mail->p_timed_thread,
4488 &info.mail->p_timed_thread);
4489 if (timed_thread_run(info.mail->p_timed_thread)) {
4490 ERR("Error running %s timed thread", text);
4494 } else if (obj->data.mail) {
4495 // this means we use obj
4496 if (!obj->data.mail->p_timed_thread) {
4497 obj->data.mail->p_timed_thread =
4498 timed_thread_create(thread,
4499 (void *) obj->data.mail,
4500 obj->data.mail->interval * 1000000);
4501 if (!obj->data.mail->p_timed_thread) {
4502 ERR("Error creating %s timed thread", text);
4504 timed_thread_register(obj->data.mail->p_timed_thread,
4505 &obj->data.mail->p_timed_thread);
4506 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4507 ERR("Error running %s timed thread", text);
4510 return obj->data.mail;
4511 } else if (!obj->a) {
4512 // something is wrong, warn once then stop
4513 ERR("Theres a problem with your %s_unseen settings. "
4514 "Check that the global %s settings are defined "
4515 "properly (line %li).", global_text, global_text, obj->line);
4521 char *format_time(unsigned long timeval, const int width)
4524 unsigned long nt; // narrow time, for speed on 32-bit
4525 unsigned cc; // centiseconds
4526 unsigned nn; // multi-purpose whatever
4529 cc = nt % 100; // centiseconds past second
4530 nt /= 100; // total seconds
4531 nn = nt % 60; // seconds past the minute
4532 nt /= 60; // total minutes
4533 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
4535 return strndup(buf, text_buffer_size);
4537 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
4538 return strndup(buf, text_buffer_size);
4540 nn = nt % 60; // minutes past the hour
4541 nt /= 60; // total hours
4542 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
4543 return strndup(buf, text_buffer_size);
4545 nn = nt; // now also hours
4546 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
4547 return strndup(buf, text_buffer_size);
4549 nn /= 24; // now days
4550 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
4551 return strndup(buf, text_buffer_size);
4553 nn /= 7; // now weeks
4554 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
4555 return strndup(buf, text_buffer_size);
4557 // well shoot, this outta' fit...
4558 return strndup("<inf>", text_buffer_size);
4561 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
4562 //string has to end with \0 and it's length should fit in a int
4564 void remove_deleted_chars(char *string){
4566 while(string[i] != 0){
4567 if(string[i] == BACKSPACE){
4569 strcpy( &(string[i-1]), &(string[i+1]) );
4571 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
4576 static inline void format_media_player_time(char *buf, const int size,
4579 int days, hours, minutes;
4581 days = seconds / (24 * 60 * 60);
4582 seconds %= (24 * 60 * 60);
4583 hours = seconds / (60 * 60);
4584 seconds %= (60 * 60);
4585 minutes = seconds / 60;
4589 snprintf(buf, size, "%i days %i:%02i:%02i", days,
4590 hours, minutes, seconds);
4591 } else if (hours > 0) {
4592 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
4595 snprintf(buf, size, "%i:%02i", minutes, seconds);
4599 static inline double get_barnum(char *buf)
4611 if (sscanf(buf, "%lf", &barnum) == 0) {
4612 ERR("reading execbar value failed (perhaps it's not the "
4613 "correct format?)");
4616 if (barnum > 100.0 || barnum < 0.0) {
4617 ERR("your execbar value is not between 0 and 100, "
4618 "therefore it will be ignored");
4624 static void generate_text_internal(char *p, int p_max_size,
4625 struct text_object_list *text_object_list,
4626 struct information *cur)
4631 char buff_in[p_max_size];
4633 iconv_converting = 0;
4637 for (i = 0; i < text_object_list->text_object_count; i++) {
4638 struct text_object *obj = &(text_object_list->text_objects[i]);
4640 if (p_max_size < 1) {
4644 #define OBJ(a) break; case OBJ_##a:
4646 switch (obj->type) {
4648 ERR("not implemented obj type %d", obj->type);
4651 /* does anyone have decimals in acpi temperature? */
4652 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4653 round_to_int(get_acpi_temperature(obj->data.i)));
4656 /* does anyone have decimals in acpi temperature? */
4657 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4658 round_to_int((get_acpi_temperature(obj->data.i) + 40) *
4661 #endif /* !__OpenBSD__ */
4664 obj->a = get_freq(p, p_max_size, "%.0f", 1,
4665 obj->data.cpu_index);
4671 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
4672 obj->data.cpu_index);
4674 /* OpenBSD has no such flag (SUSv2) */
4675 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
4676 obj->data.cpu_index);
4680 #if defined(__linux__)
4683 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
4684 obj->data.cpu_index);
4689 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
4690 obj->data.cpu_index);
4695 OBJ(wireless_essid) {
4696 snprintf(p, p_max_size, "%s", obj->data.net->essid);
4698 OBJ(wireless_mode) {
4699 snprintf(p, p_max_size, "%s", obj->data.net->mode);
4701 OBJ(wireless_bitrate) {
4702 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
4705 snprintf(p, p_max_size, "%s", obj->data.net->ap);
4707 OBJ(wireless_link_qual) {
4708 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
4709 obj->data.net->link_qual);
4711 OBJ(wireless_link_qual_max) {
4712 spaced_print(p, p_max_size, "%d", 4,
4713 "wireless_link_qual_max", obj->data.net->link_qual_max);
4715 OBJ(wireless_link_qual_perc) {
4716 if (obj->data.net->link_qual_max > 0) {
4717 spaced_print(p, p_max_size, "%.0f", 5,
4718 "wireless_link_qual_perc",
4719 (double) obj->data.net->link_qual /
4720 obj->data.net->link_qual_max * 100);
4722 spaced_print(p, p_max_size, "unk", 5,
4723 "wireless_link_qual_perc");
4726 OBJ(wireless_link_bar) {
4727 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4728 obj->data.net->link_qual_max) * 255.0);
4730 #endif /* HAVE_IWLIB */
4732 #endif /* __linux__ */
4735 get_freq_dynamic(p, p_max_size, "%.0f", 1);
4736 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4740 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
4742 get_freq_dynamic(p, p_max_size, "%.2f", 1000);
4744 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4749 get_adt746x_cpu(p, p_max_size);
4752 get_adt746x_fan(p, p_max_size);
4755 get_acpi_fan(p, p_max_size);
4757 OBJ(acpiacadapter) {
4758 get_acpi_ac_adapter(p, p_max_size);
4761 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4764 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4766 OBJ(battery_percent) {
4767 spaced_print(p, p_max_size, "%*d", 4, "battery_percent",
4768 pad_percents, get_battery_perct(obj->data.s));
4771 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4773 #endif /* __OpenBSD__ */
4776 human_readable(cur->buffers * 1024, p, 255, "buffers");
4779 human_readable(cur->cached * 1024, p, 255, "buffers");
4782 if (obj->data.cpu_index > info.cpu_count) {
4783 printf("obj->data.cpu_index %i info.cpu_count %i",
4784 obj->data.cpu_index, info.cpu_count);
4785 CRIT_ERR("attempting to use more CPUs than you have!");
4787 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
4788 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4791 new_bar(p, obj->a, obj->b,
4792 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4795 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
4796 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4797 100, 1, obj->showaslog);
4800 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
4801 obj->e, 1, obj->showaslog);
4804 new_fg(p, obj->data.l);
4836 OBJ(conky_version) {
4837 snprintf(p, p_max_size, "%s", VERSION);
4839 OBJ(conky_build_date) {
4840 snprintf(p, p_max_size, "%s", BUILD_DATE);
4842 OBJ(conky_build_arch) {
4843 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4845 #if defined(__linux__)
4847 snprintf(p, p_max_size, "%s",
4848 get_disk_protect_queue(obj->data.s));
4851 snprintf(p, p_max_size, "%s", i8k.version);
4854 snprintf(p, p_max_size, "%s", i8k.bios);
4857 snprintf(p, p_max_size, "%s", i8k.serial);
4860 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
4862 OBJ(i8k_cpu_tempf) {
4865 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4866 snprintf(p, p_max_size, "%.1f", cpu_temp * (9.0 / 5.0) + 32.0);
4868 OBJ(i8k_left_fan_status) {
4869 int left_fan_status;
4871 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4872 if (left_fan_status == 0) {
4873 snprintf(p, p_max_size, "off");
4875 if (left_fan_status == 1) {
4876 snprintf(p, p_max_size, "low");
4878 if (left_fan_status == 2) {
4879 snprintf(p, p_max_size, "high");
4882 OBJ(i8k_right_fan_status) {
4883 int right_fan_status;
4885 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4886 if (right_fan_status == 0) {
4887 snprintf(p, p_max_size, "off");
4889 if (right_fan_status == 1) {
4890 snprintf(p, p_max_size, "low");
4892 if (right_fan_status == 2) {
4893 snprintf(p, p_max_size, "high");
4896 OBJ(i8k_left_fan_rpm) {
4897 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4899 OBJ(i8k_right_fan_rpm) {
4900 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4902 OBJ(i8k_ac_status) {
4905 sscanf(i8k.ac_status, "%d", &ac_status);
4906 if (ac_status == -1) {
4907 snprintf(p, p_max_size, "disabled (read i8k docs)");
4909 if (ac_status == 0) {
4910 snprintf(p, p_max_size, "off");
4912 if (ac_status == 1) {
4913 snprintf(p, p_max_size, "on");
4916 OBJ(i8k_buttons_status) {
4917 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4920 get_ibm_acpi_fan(p, p_max_size);
4923 get_ibm_acpi_temps();
4924 snprintf(p, p_max_size, "%d", ibm_acpi.temps[obj->data.sensor]);
4927 get_ibm_acpi_volume(p, p_max_size);
4929 OBJ(ibm_brightness) {
4930 get_ibm_acpi_brightness(p, p_max_size);
4933 if ((obj->data.ifblock.s)
4934 && (!interface_up(obj->data.ifblock.s))) {
4935 i = obj->data.ifblock.pos;
4942 if (!cur->gw_info.count) {
4943 i = obj->data.ifblock.pos;
4950 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4953 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4956 snprintf(p, p_max_size, "%d", get_laptop_mode());
4959 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4961 #endif /* __linux__ */
4964 OBJ(obsd_sensors_temp) {
4965 obsd_sensors.device = sensor_device;
4966 update_obsd_sensors();
4967 snprintf(p, p_max_size, "%.1f",
4968 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor]);
4970 OBJ(obsd_sensors_fan) {
4971 obsd_sensors.device = sensor_device;
4972 update_obsd_sensors();
4973 snprintf(p, p_max_size, "%d",
4974 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4976 OBJ(obsd_sensors_volt) {
4977 obsd_sensors.device = sensor_device;
4978 update_obsd_sensors();
4979 snprintf(p, p_max_size, "%.2f",
4980 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4983 get_obsd_vendor(p, p_max_size);
4986 get_obsd_product(p, p_max_size);
4988 #endif /* __OpenBSD__ */
4992 new_font(p, obj->data.s);
4995 /* TODO: move this correction from kB to kB/s elsewhere
4996 * (or get rid of it??) */
4998 if (obj->data.diskio) {
5000 (obj->data.diskio->current / update_interval) * 1024LL,
5001 p, p_max_size, "diskio");
5003 human_readable(info.diskio_value * 1024LL, p, p_max_size,
5008 if (obj->data.diskio) {
5009 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
5012 human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
5017 if (obj->data.diskio) {
5018 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
5021 human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
5026 if (obj->data.diskio) {
5027 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5028 obj->data.diskio->current, obj->e, 1, obj->showaslog);
5030 new_graph(p, obj->a, obj->b, obj->c, obj->d, info.diskio_value,
5031 obj->e, 1, obj->showaslog);
5034 OBJ(diskiograph_read) {
5035 if (obj->data.diskio) {
5036 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5037 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
5039 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5040 info.diskio_read_value, obj->e, 1, obj->showaslog);
5043 OBJ(diskiograph_write) {
5044 if (obj->data.diskio) {
5045 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5046 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
5048 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5049 info.diskio_write_value, obj->e, 1, obj->showaslog);
5053 spaced_print(p, p_max_size, "%d", 6, "downspeed",
5054 round_to_int(obj->data.net->recv_speed / 1024));
5057 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
5058 obj->data.net->recv_speed / 1024.0);
5060 OBJ(downspeedgraph) {
5061 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5062 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
5066 i = obj->data.ifblock.pos - 1;
5076 if ((obj->data.net->addr.sa_data[2] & 255) == 0
5077 && (obj->data.net->addr.sa_data[3] & 255) == 0
5078 && (obj->data.net->addr.sa_data[4] & 255) == 0
5079 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
5080 snprintf(p, p_max_size, "No Address");
5082 snprintf(p, p_max_size, "%u.%u.%u.%u",
5083 obj->data.net->addr.sa_data[2] & 255,
5084 obj->data.net->addr.sa_data[3] & 255,
5085 obj->data.net->addr.sa_data[4] & 255,
5086 obj->data.net->addr.sa_data[5] & 255);
5089 #if defined(__linux__)
5091 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
5093 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
5094 strcpy(p, obj->data.net->addrs);
5097 strcpy(p, "0.0.0.0");
5099 #endif /* __linux__ */
5101 #if defined(IMLIB2) && defined(X11)
5106 Imlib_Image image, buffer;
5108 image = imlib_load_image(obj->data.s);
5109 imlib_context_set_image(image);
5113 w = imlib_image_get_width();
5114 h = imlib_image_get_height();
5115 buffer = imlib_create_image(w, h);
5116 imlib_context_set_display(display);
5117 imlib_context_set_drawable(window.drawable);
5118 imlib_context_set_colormap(DefaultColormap(display,
5120 imlib_context_set_visual(DefaultVisual(display,
5122 imlib_context_set_image(buffer);
5123 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
5124 text_start_x, text_start_y, w, h);
5125 imlib_render_image_on_drawable(text_start_x,
5128 imlib_context_set_image(image);
5136 read_exec(obj->data.s, p, p_max_size);
5137 remove_deleted_chars(p);
5140 struct information *tmp_info;
5141 struct text_object_list *text_objects;
5143 read_exec(obj->data.s, p, p_max_size);
5145 tmp_info = malloc(sizeof(struct information));
5146 memcpy(tmp_info, cur, sizeof(struct information));
5147 text_objects = parse_conky_vars(p, p, tmp_info);
5149 free_text_objects(text_objects, 0);
5156 read_exec(obj->data.s, p, p_max_size);
5157 barnum = get_barnum(p);
5159 if (barnum >= 0.0) {
5160 new_bar(p, 0, 4, round_to_int(barnum * 255.0));
5164 char showaslog = FALSE;
5167 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
5169 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, p_max_size);
5170 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
5171 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, p_max_size);
5173 read_exec(obj->data.s, p, p_max_size);
5175 barnum = get_barnum(p);
5177 if (barnum >= 0.0) {
5178 new_graph(p, 0, 25, obj->c, obj->d, round_to_int(barnum),
5183 if (current_update_time - obj->data.execi.last_update
5184 >= obj->data.execi.interval) {
5187 read_exec(obj->data.execi.cmd, p, p_max_size);
5188 barnum = get_barnum(p);
5190 if (barnum >= 0.0) {
5191 obj->f = 255 * barnum / 100.0;
5193 obj->data.execi.last_update = current_update_time;
5195 new_bar(p, 0, 4, round_to_int(obj->f));
5198 if (current_update_time - obj->data.execi.last_update
5199 >= obj->data.execi.interval) {
5202 read_exec(obj->data.execi.cmd, p, p_max_size);
5203 barnum = get_barnum(p);
5205 if (barnum >= 0.0) {
5208 obj->data.execi.last_update = current_update_time;
5210 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
5213 if (current_update_time - obj->data.execi.last_update
5214 >= obj->data.execi.interval
5215 && obj->data.execi.interval != 0) {
5216 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
5218 obj->data.execi.last_update = current_update_time;
5220 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
5223 struct text_object_list *text_objects = 0;
5224 struct information *tmp_info =
5225 malloc(sizeof(struct information));
5226 memcpy(tmp_info, cur, sizeof(struct information));
5228 if (current_update_time - obj->data.execi.last_update
5229 < obj->data.execi.interval
5230 || obj->data.execi.interval == 0) {
5231 text_objects = parse_conky_vars(obj->data.execi.buffer, p, tmp_info);
5233 char *output = obj->data.execi.buffer;
5234 FILE *fp = popen(obj->data.execi.cmd, "r");
5235 int length = fread(output, 1, text_buffer_size, fp);
5239 output[length] = '\0';
5240 if (length > 0 && output[length - 1] == '\n') {
5241 output[length - 1] = '\0';
5244 text_objects = parse_conky_vars(obj->data.execi.buffer, p, tmp_info);
5245 obj->data.execi.last_update = current_update_time;
5247 free_text_objects(text_objects, 0);
5252 if (!obj->data.texeci.p_timed_thread) {
5253 obj->data.texeci.p_timed_thread =
5254 timed_thread_create(&threaded_exec,
5255 (void *) obj, obj->data.texeci.interval * 1000000);
5256 if (!obj->data.texeci.p_timed_thread) {
5257 ERR("Error creating texeci timed thread");
5259 timed_thread_register(obj->data.texeci.p_timed_thread,
5260 &obj->data.texeci.p_timed_thread);
5261 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
5262 ERR("Error running texeci timed thread");
5265 timed_thread_lock(obj->data.texeci.p_timed_thread);
5266 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
5267 timed_thread_unlock(obj->data.texeci.p_timed_thread);
5269 #endif /* HAVE_POPEN */
5271 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
5273 if (mail && mail->p_timed_thread) {
5274 timed_thread_lock(mail->p_timed_thread);
5275 snprintf(p, p_max_size, "%lu", mail->unseen);
5276 timed_thread_unlock(mail->p_timed_thread);
5279 OBJ(imap_messages) {
5280 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
5282 if (mail && mail->p_timed_thread) {
5283 timed_thread_lock(mail->p_timed_thread);
5284 snprintf(p, p_max_size, "%lu", mail->messages);
5285 timed_thread_unlock(mail->p_timed_thread);
5289 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
5291 if (mail && mail->p_timed_thread) {
5292 timed_thread_lock(mail->p_timed_thread);
5293 snprintf(p, p_max_size, "%lu", mail->unseen);
5294 timed_thread_unlock(mail->p_timed_thread);
5298 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
5300 if (mail && mail->p_timed_thread) {
5301 timed_thread_lock(mail->p_timed_thread);
5302 snprintf(p, p_max_size, "%.1f",
5303 mail->used / 1024.0 / 1024.0);
5304 timed_thread_unlock(mail->p_timed_thread);
5308 if (obj->data.fs != NULL) {
5309 if (obj->data.fs->size == 0) {
5310 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5312 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5313 (int) (255 - obj->data.fsbar.fs->avail * 255 /
5314 obj->data.fs->size));
5319 if (obj->data.fs != NULL) {
5320 human_readable(obj->data.fs->avail, p, 255, "fs_free");
5324 if (obj->data.fs != NULL) {
5325 if (obj->data.fs->size) {
5326 spaced_print(p, p_max_size, "%*d", 4, "fs_free_perc",
5327 pad_percents, (int) ((obj->data.fs->avail * 100) /
5328 obj->data.fs->size));
5330 snprintf(p, p_max_size, "0");
5335 if (obj->data.fs != NULL) {
5336 human_readable(obj->data.fs->size, p, 255, "fs_size");
5340 if (obj->data.fs != NULL)
5341 snprintf(p, p_max_size, "%s", obj->data.fs->type);
5344 if (obj->data.fs != NULL) {
5345 human_readable(obj->data.fs->size - (obj->data.fs->free
5346 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
5351 if (obj->data.fs != NULL) {
5352 if (obj->data.fs->size == 0) {
5353 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5355 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5356 (int) (obj->data.fsbar.fs->avail * 255 /
5357 obj->data.fs->size));
5362 if (obj->data.fs != NULL) {
5363 if (obj->data.fs->size) {
5364 spaced_print(p, 4, "%*d", 4, "fs_used_perc",
5365 pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
5366 obj->data.fs->size)));
5368 snprintf(p, p_max_size, "0");
5373 float *v = info.loadavg;
5375 if (obj->data.loadavg[2]) {
5376 snprintf(p, p_max_size, "%.2f %.2f %.2f",
5377 v[obj->data.loadavg[0] - 1],
5378 v[obj->data.loadavg[1] - 1],
5379 v[obj->data.loadavg[2] - 1]);
5380 } else if (obj->data.loadavg[1]) {
5381 snprintf(p, p_max_size, "%.2f %.2f",
5382 v[obj->data.loadavg[0] - 1],
5383 v[obj->data.loadavg[1] - 1]);
5384 } else if (obj->data.loadavg[0]) {
5385 snprintf(p, p_max_size, "%.2f",
5386 v[obj->data.loadavg[0] - 1]);
5390 new_goto(p, obj->data.i);
5393 new_tab(p, obj->data.pair.a, obj->data.pair.b);
5396 new_hr(p, obj->data.i);
5399 if (cur->nameserver_info.nscount > obj->data.i)
5400 snprintf(p, p_max_size, "%s",
5401 cur->nameserver_info.ns_list[obj->data.i]);
5405 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
5406 snprintf(p, p_max_size, "%s", skill);
5411 PRSS *data = get_rss_info(obj->data.rss.uri,
5412 obj->data.rss.delay);
5416 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
5418 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
5420 // remove trailing new line if one exists
5421 if (str[strlen(str) - 1] == '\n') {
5422 str[strlen(str) - 1] = 0;
5424 snprintf(p, p_max_size, "%s", str);
5425 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
5426 if (obj->data.rss.act_par < data->item_count) {
5427 str = data->items[obj->data.rss.act_par].title;
5428 // remove trailing new line if one exists
5429 if (str[strlen(str) - 1] == '\n') {
5430 str[strlen(str) - 1] = 0;
5432 snprintf(p, p_max_size, "%s", str);
5434 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
5435 if (obj->data.rss.act_par < data->item_count) {
5437 data->items[obj->data.rss.act_par].description;
5438 // remove trailing new line if one exists
5439 if (str[strlen(str) - 1] == '\n') {
5440 str[strlen(str) - 1] = 0;
5442 snprintf(p, p_max_size, "%s", str);
5444 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
5445 if (data->item_count > 0) {
5451 if (obj->data.rss.act_par > data->item_count) {
5452 show = data->item_count;
5454 show = obj->data.rss.act_par;
5456 for (itmp = 0; itmp < show; itmp++) {
5457 PRSS_Item *item = &data->items[itmp];
5461 // don't add new line before first item
5463 strncat(p, "\n", p_max_size);
5465 /* remove trailing new line if one exists,
5466 * we have our own */
5467 if (str[strlen(str) - 1] == '\n') {
5468 str[strlen(str) - 1] = 0;
5470 strncat(p, str, p_max_size);
5480 if (obj->data.hddtemp.update_time < current_update_time - 30) {
5481 char *str = get_hddtemp_info(obj->data.hddtemp.dev,
5482 obj->data.hddtemp.addr, obj->data.hddtemp.port, &obj->data.hddtemp.unit);
5484 strncpy(obj->data.hddtemp.temp, str, text_buffer_size);
5486 obj->data.hddtemp.temp[0] = 0;
5488 obj->data.hddtemp.update_time = current_update_time;
5490 if (!obj->data.hddtemp.temp) {
5491 snprintf(p, p_max_size, "N/A");
5492 } else if (obj->data.hddtemp.unit == '*') {
5493 snprintf(p, p_max_size, "%s", obj->data.hddtemp.temp);
5495 snprintf(p, p_max_size, "%s%c", obj->data.hddtemp.temp, obj->data.hddtemp.unit);
5500 new_offset(p, obj->data.i);
5503 new_voffset(p, obj->data.i);
5509 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5510 obj->data.sysfs.devtype, obj->data.sysfs.type);
5512 if (r >= 100.0 || r == 0) {
5513 snprintf(p, p_max_size, "%d", (int) r);
5515 snprintf(p, p_max_size, "%.1f", r);
5521 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5522 obj->data.sysfs.devtype, obj->data.sysfs.type);
5524 if (r >= 100.0 || r == 0) {
5525 snprintf(p, p_max_size, "%d", (int) r);
5527 snprintf(p, p_max_size, "%.1f", r);
5533 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5534 obj->data.sysfs.devtype, obj->data.sysfs.type);
5536 if (r >= 100.0 || r == 0) {
5537 snprintf(p, p_max_size, "%d", (int) r);
5539 snprintf(p, p_max_size, "%.1f", r);
5542 #endif /* !__OpenBSD__ */
5544 new_alignr(p, obj->data.i);
5547 new_alignc(p, obj->data.i);
5550 struct text_object_list *text_objects;
5551 struct information *tmp_info =
5552 malloc(sizeof(struct information));
5553 memcpy(tmp_info, cur, sizeof(struct information));
5554 text_objects = parse_conky_vars(obj->data.ifblock.s, p, tmp_info);
5556 if (strlen(p) != 0) {
5557 i = obj->data.ifblock.pos;
5563 free_text_objects(text_objects, 0);
5570 if ((obj->data.ifblock.s)
5571 && (stat(obj->data.ifblock.s, &tmp) == -1)) {
5572 i = obj->data.ifblock.pos;
5575 if (obj->data.ifblock.str) {
5576 if (!check_contains(obj->data.ifblock.s,
5577 obj->data.ifblock.str)) {
5578 i = obj->data.ifblock.pos;
5589 if ((obj->data.ifblock.s)
5590 && (!check_mount(obj->data.ifblock.s))) {
5591 i = obj->data.ifblock.pos;
5598 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
5599 i = obj->data.ifblock.pos;
5605 #if defined(__linux__)
5607 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
5611 snprintf(p, p_max_size, "%s", cur->uname_s.release);
5614 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
5619 human_readable(cur->mem * 1024, p, 255, "mem");
5622 human_readable(cur->memeasyfree * 1024, p, 255, "memeasyfree");
5625 human_readable(cur->memfree * 1024, p, 255, "memfree");
5628 human_readable(cur->memmax * 1024, p, 255, "memmax");
5632 spaced_print(p, p_max_size, "%*llu", 4, "memperc",
5633 pad_percents, cur->mem * 100 / cur->memmax);
5637 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5638 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5641 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5642 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5643 100, 1, obj->showaslog);
5648 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
5651 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
5654 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
5657 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5658 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
5661 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5662 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
5665 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5666 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
5670 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5672 OBJ(monitor_number) {
5673 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5679 update_mail_count(&obj->data.local_mail);
5680 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5683 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5685 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5688 update_mail_count(&obj->data.local_mail);
5689 snprintf(p, p_max_size, "%d",
5690 obj->data.local_mail.new_mail_count);
5693 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5696 new_outline(p, obj->data.l);
5699 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
5701 OBJ(running_processes) {
5702 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
5705 snprintf(p, p_max_size, "%s", obj->data.s);
5708 new_bg(p, obj->data.l);
5711 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5714 human_readable(cur->swap * 1024, p, 255, "swap");
5717 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
5720 if (cur->swapmax == 0) {
5721 strncpy(p, "No swap", p_max_size);
5723 spaced_print(p, p_max_size, "%*llu", 4, "swapperc",
5724 pad_percents, cur->swap * 100 / cur->swapmax);
5728 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5729 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5732 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5735 time_t t = time(NULL);
5736 struct tm *tm = localtime(&t);
5738 setlocale(LC_TIME, "");
5739 strftime(p, p_max_size, obj->data.s, tm);
5742 time_t t = time(NULL);
5743 struct tm *tm = gmtime(&t);
5745 strftime(p, p_max_size, obj->data.s, tm);
5752 if (obj->data.tztime.tz) {
5753 oldTZ = getenv("TZ");
5754 setenv("TZ", obj->data.tztime.tz, 1);
5760 setlocale(LC_TIME, "");
5761 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5763 setenv("TZ", oldTZ, 1);
5768 // Needless to free oldTZ since getenv gives ptr to static data
5771 human_readable(obj->data.net->recv, p, 255, "totaldown");
5774 human_readable(obj->data.net->trans, p, 255, "totalup");
5777 snprintf(p, p_max_size, "%d", total_updates);
5780 spaced_print(p, p_max_size, "%d", 6, "upspeed",
5781 round_to_int(obj->data.net->trans_speed / 1024));
5784 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
5785 obj->data.net->trans_speed / 1024.0);
5788 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5789 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
5792 format_seconds_short(p, p_max_size, (int) cur->uptime);
5795 format_seconds(p, p_max_size, (int) cur->uptime);
5798 snprintf(p, p_max_size, "%s", cur->users.names);
5801 snprintf(p, p_max_size, "%s", cur->users.terms);
5804 snprintf(p, p_max_size, "%s", cur->users.times);
5807 snprintf(p, p_max_size, "%d", cur->users.number);
5809 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5810 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5814 msg = get_apm_adapter();
5815 snprintf(p, p_max_size, "%s", msg);
5818 OBJ(apm_battery_life) {
5821 msg = get_apm_battery_life();
5822 snprintf(p, p_max_size, "%s", msg);
5825 OBJ(apm_battery_time) {
5828 msg = get_apm_battery_time();
5829 snprintf(p, p_max_size, "%s", msg);
5832 #endif /* __FreeBSD__ __OpenBSD__ */
5836 int len = obj->data.i;
5837 if (len == 0 || len > p_max_size)
5839 snprintf(p, len, "%s", cur->mpd.title);
5842 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5845 snprintf(p, p_max_size, "%s", cur->mpd.album);
5848 snprintf(p, p_max_size, "%s", cur->mpd.random);
5851 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
5854 snprintf(p, p_max_size, "%s", cur->mpd.track);
5857 snprintf(p, p_max_size, "%s", cur->mpd.name);
5860 snprintf(p, p_max_size, "%s", cur->mpd.file);
5863 snprintf(p, p_max_size, "%i", cur->mpd.volume);
5866 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
5869 snprintf(p, p_max_size, "%s", cur->mpd.status);
5872 format_media_player_time(p, p_max_size, cur->mpd.elapsed);
5875 format_media_player_time(p, p_max_size, cur->mpd.length);
5878 spaced_print(p, p_max_size, "%*d", 4, "mpd_percent",
5879 pad_percents, (int) (cur->mpd.progress * 100));
5882 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5883 (int) (cur->mpd.progress * 255.0f));
5886 int len = obj->data.i;
5887 if (len == 0 || len > p_max_size)
5890 memset(p, 0, p_max_size);
5891 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
5892 && *cur->mpd.title) {
5893 snprintf(p, len, "%s - %s", cur->mpd.artist,
5895 } else if (cur->mpd.title && *cur->mpd.title) {
5896 snprintf(p, len, "%s", cur->mpd.title);
5897 } else if (cur->mpd.artist && *cur->mpd.artist) {
5898 snprintf(p, len, "%s", cur->mpd.artist);
5899 } else if (cur->mpd.file && *cur->mpd.file) {
5900 snprintf(p, len, "%s", cur->mpd.file);
5909 snprintf(p, p_max_size, "%s", (cur->moc.state ? cur->moc.state : "??"));
5912 snprintf(p, p_max_size, "%s", (cur->moc.file ? cur->moc.file : "no file"));
5915 snprintf(p, p_max_size, "%s", (cur->moc.title ? cur->moc.title : "no title"));
5918 snprintf(p, p_max_size, "%s", (cur->moc.artist ? cur->moc.artist : "no artist"));
5921 snprintf(p, p_max_size, "%s", (cur->moc.song ? cur->moc.song : "no song"));
5924 snprintf(p, p_max_size, "%s", (cur->moc.album ? cur->moc.album : "no album"));
5926 OBJ(moc_totaltime) {
5927 snprintf(p, p_max_size, "%s", (cur->moc.totaltime ? cur->moc.totaltime : "0:00"));
5930 snprintf(p, p_max_size, "%s", (cur->moc.timeleft ? cur->moc.timeleft : "0:00"));
5933 snprintf(p, p_max_size, "%s", (cur->moc.curtime ? cur->moc.curtime : "0:00"));
5936 snprintf(p, p_max_size, "%s", (cur->moc.bitrate ? cur->moc.bitrate : "0Kbps"));
5939 snprintf(p, p_max_size, "%s", (cur->moc.rate ? cur->moc.rate : "0KHz"));
5944 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5947 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5950 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5953 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5955 OBJ(xmms2_comment) {
5956 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5959 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5962 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5965 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5967 OBJ(xmms2_tracknr) {
5968 if (cur->xmms2.tracknr != -1) {
5969 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5972 OBJ(xmms2_bitrate) {
5973 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5976 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5979 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5981 OBJ(xmms2_elapsed) {
5982 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5983 (cur->xmms2.elapsed / 1000) % 60);
5985 OBJ(xmms2_duration) {
5986 snprintf(p, p_max_size, "%02d:%02d",
5987 cur->xmms2.duration / 60000,
5988 (cur->xmms2.duration / 1000) % 60);
5990 OBJ(xmms2_percent) {
5991 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5994 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5995 (int) (cur->xmms2.progress * 255.0f));
5997 OBJ(xmms2_playlist) {
5998 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
6000 OBJ(xmms2_timesplayed) {
6001 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
6004 if (strlen(cur->xmms2.title) < 2
6005 && strlen(cur->xmms2.title) < 2) {
6006 snprintf(p, p_max_size, "%s", cur->xmms2.url);
6008 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
6014 OBJ(audacious_status) {
6015 snprintf(p, p_max_size, "%s",
6016 cur->audacious.items[AUDACIOUS_STATUS]);
6018 OBJ(audacious_title) {
6019 snprintf(p, cur->audacious.max_title_len > 0
6020 ? cur->audacious.max_title_len : p_max_size, "%s",
6021 cur->audacious.items[AUDACIOUS_TITLE]);
6023 OBJ(audacious_length) {
6024 snprintf(p, p_max_size, "%s",
6025 cur->audacious.items[AUDACIOUS_LENGTH]);
6027 OBJ(audacious_length_seconds) {
6028 snprintf(p, p_max_size, "%s",
6029 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
6031 OBJ(audacious_position) {
6032 snprintf(p, p_max_size, "%s",
6033 cur->audacious.items[AUDACIOUS_POSITION]);
6035 OBJ(audacious_position_seconds) {
6036 snprintf(p, p_max_size, "%s",
6037 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
6039 OBJ(audacious_bitrate) {
6040 snprintf(p, p_max_size, "%s",
6041 cur->audacious.items[AUDACIOUS_BITRATE]);
6043 OBJ(audacious_frequency) {
6044 snprintf(p, p_max_size, "%s",
6045 cur->audacious.items[AUDACIOUS_FREQUENCY]);
6047 OBJ(audacious_channels) {
6048 snprintf(p, p_max_size, "%s",
6049 cur->audacious.items[AUDACIOUS_CHANNELS]);
6051 OBJ(audacious_filename) {
6052 snprintf(p, p_max_size, "%s",
6053 cur->audacious.items[AUDACIOUS_FILENAME]);
6055 OBJ(audacious_playlist_length) {
6056 snprintf(p, p_max_size, "%s",
6057 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
6059 OBJ(audacious_playlist_position) {
6060 snprintf(p, p_max_size, "%s",
6061 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
6063 OBJ(audacious_bar) {
6067 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
6068 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
6069 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
6075 snprintf(p, p_max_size, "%s", cur->bmpx.title);
6078 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
6081 snprintf(p, p_max_size, "%s", cur->bmpx.album);
6084 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
6087 snprintf(p, p_max_size, "%i", cur->bmpx.track);
6090 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
6094 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
6097 switch (obj->data.top.type) {
6099 snprintf(p, 16, "%-15s",
6100 cur->cpu[obj->data.top.num]->name);
6103 snprintf(p, 7, "%6.2f",
6104 cur->cpu[obj->data.top.num]->amount);
6107 snprintf(p, 6, "%5i",
6108 cur->cpu[obj->data.top.num]->pid);
6111 snprintf(p, 7, "%6.2f",
6112 cur->cpu[obj->data.top.num]->totalmem);
6115 timeval = format_time(
6116 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
6117 snprintf(p, 10, "%9s", timeval);
6121 human_readable(cur->cpu[obj->data.top.num]->rss,
6125 human_readable(cur->cpu[obj->data.top.num]->vsize,
6129 ERR("Unhandled top data type: %d\n",
6130 obj->data.top.type);
6133 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
6137 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
6140 switch (obj->data.top.type) {
6142 snprintf(p, 16, "%-15s",
6143 cur->memu[obj->data.top.num]->name);
6146 snprintf(p, 7, "%6.2f",
6147 cur->memu[obj->data.top.num]->amount);
6150 snprintf(p, 6, "%5i",
6151 cur->memu[obj->data.top.num]->pid);
6154 snprintf(p, 7, "%6.2f",
6155 cur->memu[obj->data.top.num]->totalmem);
6158 timeval = format_time(
6159 cur->memu[obj->data.top.num]->total_cpu_time,
6161 snprintf(p, 10, "%9s", timeval);
6165 human_readable(cur->cpu[obj->data.top.num]->rss,
6169 human_readable(cur->cpu[obj->data.top.num]->vsize,
6173 ERR("Unhandled top data type: %d\n",
6174 obj->data.top.type);
6177 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
6181 if (current_update_time - obj->data.tail.last_update
6182 < obj->data.tail.interval) {
6183 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
6189 obj->data.tail.last_update = current_update_time;
6191 if (obj->data.tail.fd != -1) {
6192 tail_pipe(obj, p, p_max_size);
6196 fp = fopen(obj->data.tail.logfile, "rt");
6199 /* Send one message, but do not consistently spam
6200 * on missing logfiles. */
6201 if (obj->data.tail.readlines != 0) {
6202 ERR("tail logfile failed to open");
6203 strcpy(obj->data.tail.buffer, "Logfile Missing");
6205 obj->data.tail.readlines = 0;
6206 snprintf(p, p_max_size, "Logfile Missing");
6208 obj->data.tail.readlines = 0;
6209 /* -1 instead of 0 to avoid counting a trailing
6211 fseek(fp, -1, SEEK_END);
6212 bsize = ftell(fp) + 1;
6213 for (iter = obj->data.tail.wantedlines; iter > 0;
6215 nl = rev_fcharfind(fp, '\n', iter);
6220 obj->data.tail.readlines = iter;
6221 if (obj->data.tail.readlines
6222 < obj->data.tail.wantedlines) {
6223 fseek(fp, 0, SEEK_SET);
6225 fseek(fp, nl + 1, SEEK_SET);
6228 /* Make sure bsize is at least 1 byte smaller than the
6229 * buffer max size. */
6230 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
6231 fseek(fp, bsize - text_buffer_size * 20 - 1,
6233 bsize = text_buffer_size * 20 - 1;
6235 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
6238 /* Clean up trailing newline, make sure the
6239 * buffer is null terminated. */
6240 if (obj->data.tail.buffer[bsize - 1] == '\n') {
6241 obj->data.tail.buffer[bsize - 1] = '\0';
6243 obj->data.tail.buffer[bsize] = '\0';
6245 snprintf(p, p_max_size, "%s",
6246 obj->data.tail.buffer);
6248 strcpy(obj->data.tail.buffer, "Logfile Empty");
6249 snprintf(p, p_max_size, "Logfile Empty");
6252 } /* if cur_upd_time >= */
6253 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6257 if (current_update_time - obj->data.tail.last_update
6258 < obj->data.tail.interval) {
6259 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
6265 obj->data.tail.last_update = current_update_time;
6267 fp = fopen(obj->data.tail.logfile, "rt");
6269 /* Send one message, but do not consistently spam
6270 * on missing logfiles. */
6271 if (obj->data.tail.readlines != 0) {
6272 ERR("head logfile failed to open");
6273 strcpy(obj->data.tail.buffer, "Logfile Missing");
6275 obj->data.tail.readlines = 0;
6276 snprintf(p, p_max_size, "Logfile Missing");
6278 obj->data.tail.readlines = 0;
6279 for (iter = obj->data.tail.wantedlines; iter > 0;
6281 nl = fwd_fcharfind(fp, '\n', iter);
6286 obj->data.tail.readlines = iter;
6287 /* Make sure nl is at least 1 byte smaller than the
6288 * buffer max size. */
6289 if (nl > (long) ((text_buffer_size * 20) - 1)) {
6290 nl = text_buffer_size * 20 - 1;
6292 nl = fread(obj->data.tail.buffer, 1, nl, fp);
6295 /* Clean up trailing newline, make sure the buffer
6296 * is null terminated. */
6297 if (obj->data.tail.buffer[nl - 1] == '\n') {
6298 obj->data.tail.buffer[nl - 1] = '\0';
6300 obj->data.tail.buffer[nl] = '\0';
6302 snprintf(p, p_max_size, "%s",
6303 obj->data.tail.buffer);
6305 strcpy(obj->data.tail.buffer, "Logfile Empty");
6306 snprintf(p, p_max_size, "Logfile Empty");
6308 } /* if fp == null */
6309 } /* cur_upd_time >= */
6310 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6314 FILE *fp = fopen(obj->data.s,"r");
6321 while(fgets(buf, BUFSZ, fp) != NULL){
6322 for(j = 0; buf[j] != 0; j++) {
6323 if(buf[j] == '\n') {
6328 sprintf(p, "%d", lines);
6331 sprintf(p, "File Unreadable");
6336 FILE *fp = fopen(obj->data.s,"r");
6341 char inword = FALSE;
6344 while(fgets(buf, BUFSZ, fp) != NULL){
6345 for(j = 0; buf[j] != 0; j++) {
6346 if(!isspace(buf[j])) {
6347 if(inword == FALSE) {
6356 sprintf(p, "%d", words);
6359 sprintf(p, "File Unreadable");
6362 #ifdef TCP_PORT_MONITOR
6364 /* grab a pointer to this port monitor */
6365 tcp_port_monitor_t *p_monitor =
6366 find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
6367 obj->data.tcp_port_monitor.port_range_begin,
6368 obj->data.tcp_port_monitor.port_range_end);
6371 snprintf(p, p_max_size, "monitor not found");
6375 /* now grab the text of interest */
6376 if (peek_tcp_port_monitor(p_monitor,
6377 obj->data.tcp_port_monitor.item,
6378 obj->data.tcp_port_monitor.connection_index, p,
6380 snprintf(p, p_max_size, "monitor peek error");
6388 iconv_converting = 1;
6389 iconv_selected = obj->a;
6392 iconv_converting = 0;
6397 OBJ(entropy_avail) {
6398 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
6400 OBJ(entropy_poolsize) {
6401 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
6404 double entropy_perc;
6406 entropy_perc = (double) cur->entropy.entropy_avail /
6407 (double) cur->entropy.poolsize;
6408 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
6414 s = smapi_get_val(obj->data.s);
6415 snprintf(p, p_max_size, "%s", s);
6419 OBJ(if_smapi_bat_installed) {
6421 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
6422 if(!smapi_bat_installed(idx)) {
6423 i = obj->data.ifblock.pos;
6428 ERR("argument to if_smapi_bat_installed must be an integer");
6430 OBJ(smapi_bat_perc) {
6432 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6433 val = smapi_bat_installed(idx) ?
6434 smapi_get_bat_int(idx, "remaining_percent") : 0;
6435 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
6437 ERR("argument to smapi_bat_perc must be an integer");
6439 OBJ(smapi_bat_temp) {
6441 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6442 val = smapi_bat_installed(idx) ?
6443 smapi_get_bat_int(idx, "temperature") : 0;
6444 /* temperature is in milli degree celsius, round to degree celsius */
6445 snprintf(p, p_max_size, "%d", (int)((val / 1000) + 0.5));
6447 ERR("argument to smapi_bat_temp must be an integer");
6449 OBJ(smapi_bat_power) {
6451 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6452 val = smapi_bat_installed(idx) ?
6453 smapi_get_bat_int(idx, "power_now") : 0;
6454 /* power_now is in mW, set to W with one digit precision */
6455 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
6457 ERR("argument to smapi_bat_power must be an integer");
6459 OBJ(smapi_bat_bar) {
6460 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
6461 new_bar(p, obj->a, obj->b, (int)
6462 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
6464 new_bar(p, obj->a, obj->b, 0);
6470 parse_conky_vars(obj->data.scroll.text, p, cur);
6472 if (strlen(p) <= obj->data.scroll.show) {
6475 #define LINESEPARATOR '|'
6476 //place all the lines behind each other with LINESEPARATOR between them
6477 for(j = 0; p[j] != 0; j++) {
6482 //scroll the output obj->data.scroll.start places by copying that many chars from
6483 //the front of the string to tmp, scrolling the rest to the front and placing tmp
6484 //at the back of the string
6485 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
6486 strncpy(tmp, p, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
6487 for(j = obj->data.scroll.start; p[j] != 0; j++){
6488 p[j - obj->data.scroll.start] = p[j];
6490 strcpy(&p[j - obj->data.scroll.start], tmp);
6492 //only show the requested number of chars
6493 if(obj->data.scroll.show < j) {
6494 p[obj->data.scroll.show] = 0;
6496 //next time, scroll a place more or reset scrolling if we are at the end
6497 obj->data.scroll.start += obj->data.scroll.step;
6498 if(obj->data.scroll.start >= j){
6499 obj->data.scroll.start = 0;
6504 int value = get_nvidia_value(obj->data.nvidia.type, display);
6506 snprintf(p, p_max_size, "N/A");
6507 else if (obj->data.nvidia.print_as_float &&
6508 value > 0 && value < 100)
6509 snprintf(p, p_max_size, "%.1f", (float)value);
6511 snprintf(p, p_max_size, "%d", value);
6519 unsigned int a = strlen(p);
6522 if (a > 0 && iconv_converting && iconv_selected > 0
6523 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6525 size_t dummy1, dummy2;
6526 char *ptr = buff_in;
6529 dummy1 = dummy2 = a;
6531 strncpy(buff_in, p, p_max_size);
6533 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6534 while (dummy1 > 0) {
6535 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6538 ERR("Iconv codeset conversion failed");
6543 /* It is nessecary when we are converting from multibyte to
6544 * singlebyte codepage */
6547 #endif /* HAVE_ICONV */
6554 double current_update_time, next_update_time, last_update_time;
6556 static void generate_text(void)
6558 struct information *cur = &info;
6565 current_update_time = get_time();
6568 /* fix diskio rates to b/s (use update_interval */
6569 info.diskio_read_value /= update_interval;
6570 info.diskio_write_value /= update_interval;
6571 info.diskio_value /= update_interval;
6573 /* add things to the buffer */
6579 generate_text_internal(p, max_user_text, global_text_object_list, cur);
6581 if (stuff_in_upper_case) {
6584 tmp_p = text_buffer;
6586 *tmp_p = toupper(*tmp_p);
6591 next_update_time += update_interval;
6592 if (next_update_time < get_time()) {
6593 next_update_time = get_time() + update_interval;
6594 } else if (next_update_time > get_time() + update_interval) {
6595 next_update_time = get_time() + update_interval;
6597 last_update_time = current_update_time;
6603 static void set_font(void)
6607 if (window.xftdraw != NULL) {
6608 XftDrawDestroy(window.xftdraw);
6610 window.xftdraw = XftDrawCreate(display, window.drawable,
6611 DefaultVisual(display, screen), DefaultColormap(display, screen));
6615 XSetFont(display, window.gc, fonts[selected_font].font->fid);
6621 static inline int get_string_width(const char *s)
6624 return *s ? calc_text_width(s, strlen(s)) : 0;
6630 static inline int get_string_width_special(char *s)
6642 p = strndup(s, text_buffer_size);
6646 if (*p == SPECIAL_CHAR) {
6647 /* shift everything over by 1 so that the special char
6648 * doesn't mess up the size calculation */
6649 for (i = 0; i < strlen(p); i++) {
6650 *(p + i) = *(p + i + 1);
6652 if (specials[special_index + idx].type == GRAPH
6653 || specials[special_index + idx].type == BAR) {
6654 width += specials[special_index + idx].width;
6661 if (strlen(final) > 1) {
6662 width += calc_text_width(final, strlen(final));
6667 return (s) ? strlen(s) : 0;
6672 static void text_size_updater(char *s);
6674 int last_font_height;
6675 static void update_text_area(void)
6679 /* update text size if it isn't fixed */
6684 text_width = minimum_width;
6687 last_font_height = font_height();
6688 for_each_line(text_buffer, text_size_updater);
6690 if (text_height < minimum_height) {
6691 text_height = minimum_height;
6693 if (text_width > maximum_width && maximum_width > 0) {
6694 text_width = maximum_width;
6698 /* get text position on workarea */
6699 switch (text_alignment) {
6706 x = workarea[2] - text_width - gap_x;
6711 x = workarea[2] / 2 - text_width / 2 - gap_x;
6718 y = workarea[3] - text_height - gap_y;
6722 x = workarea[2] - text_width - gap_x;
6723 y = workarea[3] - text_height - gap_y;
6727 x = workarea[2] / 2 - text_width / 2 - gap_x;
6728 y = workarea[3] - text_height - gap_y;
6733 y = workarea[3] / 2 - text_height / 2 - gap_y;
6737 x = workarea[2] - text_width - gap_x;
6738 y = workarea[3] / 2 - text_height / 2 - gap_y;
6742 case NONE: // Let the WM manage the window
6753 if (own_window && !fixed_pos) {
6756 text_start_x = border_margin + 1;
6757 text_start_y = border_margin + 1;
6758 window.x = x - border_margin - 1;
6759 window.y = y - border_margin - 1;
6763 /* If window size doesn't match to workarea's size,
6764 * then window probably includes panels (gnome).
6765 * Blah, doesn't work on KDE. */
6766 if (workarea[2] != window.width || workarea[3] != window.height) {
6778 static int cur_x, cur_y; /* current x and y for drawing */
6780 //draw_mode also without X11 because we only need to print to stdout with FG
6781 static int draw_mode; /* FG, BG or OUTLINE */
6783 static long current_color;
6786 static void text_size_updater(char *s)
6791 /* get string widths and skip specials */
6794 if (*p == SPECIAL_CHAR) {
6796 w += get_string_width(s);
6799 if (specials[special_index].type == BAR
6800 || specials[special_index].type == GRAPH) {
6801 w += specials[special_index].width;
6802 if (specials[special_index].height > last_font_height) {
6803 last_font_height = specials[special_index].height;
6804 last_font_height += font_ascent();
6806 } else if (specials[special_index].type == OFFSET) {
6807 if (specials[special_index].arg > 0) {
6808 w += specials[special_index].arg;
6810 } else if (specials[special_index].type == VOFFSET) {
6811 last_font_height += specials[special_index].arg;
6812 } else if (specials[special_index].type == GOTO) {
6813 if (specials[special_index].arg > cur_x) {
6814 w = (int) specials[special_index].arg;
6816 } else if (specials[special_index].type == TAB) {
6817 int start = specials[special_index].arg;
6818 int step = specials[special_index].width;
6820 if (!step || step < 0) {
6823 w += step - (cur_x - text_start_x - start) % step;
6824 } else if (specials[special_index].type == FONT) {
6825 selected_font = specials[special_index].font_added;
6826 if (font_height() > last_font_height) {
6827 last_font_height = font_height();
6836 w += get_string_width(s);
6837 if (w > text_width) {
6840 if (text_width > maximum_width && maximum_width) {
6841 text_width = maximum_width;
6844 text_height += last_font_height;
6845 last_font_height = font_height();
6849 static inline void set_foreground_color(long c)
6852 XSetForeground(display, window.gc, c);
6856 static void draw_string(const char *s)
6858 int i, i2, pos, width_of_s;
6866 width_of_s = get_string_width(s);
6867 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6869 fflush(stdout); /* output immediately, don't buffer */
6871 memset(tmpstring1, 0, text_buffer_size);
6872 memset(tmpstring2, 0, text_buffer_size);
6873 strncpy(tmpstring1, s, text_buffer_size - 1);
6878 max = ((text_width - width_of_s) / get_string_width(" "));
6880 /* This code looks for tabs in the text and coverts them to spaces.
6881 * The trick is getting the correct number of spaces, and not going
6882 * over the window's size without forcing the window larger. */
6883 for (i = 0; i < (int) text_buffer_size; i++) {
6884 if (tmpstring1[i] == '\t') {
6886 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6887 /* guard against overrun */
6888 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6893 /* guard against overrun */
6894 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6899 if (text_width == maximum_width) {
6900 /* this means the text is probably pushing the limit,
6901 * so we'll chop it */
6902 while (cur_x + get_string_width(tmpstring2) - text_start_x
6903 > maximum_width && strlen(tmpstring2) > 0) {
6904 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6915 c.pixel = current_color;
6916 XQueryColor(display, DefaultColormap(display, screen), &c);
6919 c2.color.red = c.red;
6920 c2.color.green = c.green;
6921 c2.color.blue = c.blue;
6922 c2.color.alpha = fonts[selected_font].font_alpha;
6924 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6925 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6927 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6928 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6933 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6936 cur_x += width_of_s;
6938 memcpy(tmpstring1, s, text_buffer_size);
6941 long redmask, greenmask, bluemask;
6943 void set_up_gradient(void)
6947 colour_depth = DisplayPlanes(display, screen);
6951 if (colour_depth != 24 && colour_depth != 16) {
6952 ERR("using non-standard colour depth, gradients may look like a "
6959 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6961 greenmask |= 1 << i;
6964 if (colour_depth % 3 == 1) {
6965 greenmask |= 1 << (colour_depth / 3);
6967 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6968 greenmask = greenmask << (colour_depth / 3);
6971 /* this function returns the next colour between two colours for a gradient */
6972 unsigned long do_gradient(unsigned long first_colour,
6973 unsigned long last_colour)
6976 int red1, green1, blue1; // first colour
6977 int red2, green2, blue2; // second colour
6978 int red3 = 0, green3 = 0, blue3 = 0; // difference
6979 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6980 short greenshift = (colour_depth / 3);
6982 red1 = (first_colour & redmask) >> redshift;
6983 green1 = (first_colour & greenmask) >> greenshift;
6984 blue1 = first_colour & bluemask;
6985 red2 = (last_colour & redmask) >> redshift;
6986 green2 = (last_colour & greenmask) >> greenshift;
6987 blue2 = last_colour & bluemask;
6994 if (green1 > green2) {
6997 if (green1 < green2) {
7000 if (blue1 > blue2) {
7003 if (blue1 < blue2) {
7018 if (red1 > bluemask) {
7021 if (green1 > bluemask) {
7024 if (blue1 > bluemask) {
7027 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
7031 /* this function returns the max diff for a gradient */
7032 unsigned long gradient_max(unsigned long first_colour,
7033 unsigned long last_colour)
7035 int red1, green1, blue1; // first colour
7036 int red2, green2, blue2; // second colour
7037 int red3 = 0, green3 = 0, blue3 = 0; // difference
7038 long redshift, greenshift;
7041 if (colour_depth == 0) {
7044 redshift = (2 * colour_depth / 3 + colour_depth % 3);
7045 greenshift = (colour_depth / 3);
7047 red1 = (first_colour & redmask) >> redshift;
7048 green1 = (first_colour & greenmask) >> greenshift;
7049 blue1 = first_colour & bluemask;
7050 red2 = (last_colour & redmask) >> redshift;
7051 green2 = (last_colour & greenmask) >> greenshift;
7052 blue2 = last_colour & bluemask;
7053 red3 = abs(red1 - red2);
7054 green3 = abs(green1 - green2);
7055 blue3 = abs(blue1 - blue2);
7067 static void draw_line(char *s)
7075 cur_x = text_start_x;
7076 cur_y += font_ascent();
7077 font_h = font_height();
7079 /* find specials and draw stuff */
7082 if (*p == SPECIAL_CHAR) {
7085 /* draw string before special */
7092 switch (specials[special_index].type) {
7093 case HORIZONTAL_LINE:
7095 int h = specials[special_index].height;
7096 int mid = font_ascent() / 2;
7098 w = text_start_x + text_width - cur_x;
7100 XSetLineAttributes(display, window.gc, h, LineSolid,
7101 CapButt, JoinMiter);
7102 XDrawLine(display, window.drawable, window.gc, cur_x,
7103 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
7109 int h = specials[special_index].height;
7110 int tmp_s = specials[special_index].arg;
7111 int mid = font_ascent() / 2;
7112 char ss[2] = { tmp_s, tmp_s };
7114 w = text_start_x + text_width - cur_x - 1;
7115 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
7116 CapButt, JoinMiter);
7117 XSetDashes(display, window.gc, 0, ss, 2);
7118 XDrawLine(display, window.drawable, window.gc, cur_x,
7119 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
7125 int h, bar_usage, by;
7126 if (cur_x - text_start_x > maximum_width
7127 && maximum_width > 0) {
7130 h = specials[special_index].height;
7131 bar_usage = specials[special_index].arg;
7132 by = cur_y - (font_ascent() / 2) - 1;
7134 if (h < font_height()) {
7137 w = specials[special_index].width;
7139 w = text_start_x + text_width - cur_x - 1;
7145 XSetLineAttributes(display, window.gc, 1, LineSolid,
7146 CapButt, JoinMiter);
7148 XDrawRectangle(display, window.drawable, window.gc, cur_x,
7150 XFillRectangle(display, window.drawable, window.gc, cur_x,
7151 by, w * bar_usage / 255, h);
7152 if (specials[special_index].height > cur_y_add
7153 && specials[special_index].height > font_h) {
7154 cur_y_add = specials[special_index].height;
7161 int h, by, i, j = 0;
7162 int gradient_size = 0;
7163 float gradient_factor = 0;
7164 float gradient_update = 0;
7165 unsigned long last_colour = current_color;
7166 unsigned long tmpcolour = current_color;
7167 if (cur_x - text_start_x > maximum_width
7168 && maximum_width > 0) {
7171 h = specials[special_index].height;
7172 by = cur_y - (font_ascent() / 2) - 1;
7174 if (h < font_height()) {
7177 w = specials[special_index].width;
7179 w = text_start_x + text_width - cur_x - 1;
7184 if (draw_graph_borders) {
7185 XSetLineAttributes(display, window.gc, 1, LineSolid,
7186 CapButt, JoinMiter);
7187 XDrawRectangle(display, window.drawable, window.gc,
7190 XSetLineAttributes(display, window.gc, 1, LineSolid,
7191 CapButt, JoinMiter);
7193 if (specials[special_index].last_colour != 0
7194 || specials[special_index].first_colour != 0) {
7195 tmpcolour = specials[special_index].last_colour;
7197 gradient_max(specials[special_index].last_colour,
7198 specials[special_index].first_colour);
7199 gradient_factor = (float) gradient_size / (w - 2);
7201 for (i = w - 2; i > -1; i--) {
7202 if (specials[special_index].last_colour != 0
7203 || specials[special_index].first_colour != 0) {
7204 XSetForeground(display, window.gc, tmpcolour);
7205 gradient_update += gradient_factor;
7206 while (gradient_update > 0) {
7207 tmpcolour = do_gradient(tmpcolour,
7208 specials[special_index].first_colour);
7212 if ((w - i) / ((float) (w - 2) /
7213 (specials[special_index].graph_width)) > j
7214 && j < MAX_GRAPH_DEPTH - 3) {
7217 /* this is mugfugly, but it works */
7218 XDrawLine(display, window.drawable, window.gc,
7219 cur_x + i + 1, by + h, cur_x + i + 1,
7220 by + h - specials[special_index].graph[j] *
7221 (h - 1) / specials[special_index].graph_scale);
7223 if (specials[special_index].height > cur_y_add
7224 && specials[special_index].height > font_h) {
7225 cur_y_add = specials[special_index].height;
7227 /* if (draw_mode == BG) {
7228 set_foreground_color(default_bg_color);
7229 } else if (draw_mode == OUTLINE) {
7230 set_foreground_color(default_out_color);
7232 set_foreground_color(default_fg_color);
7234 if (show_graph_range) {
7237 unsigned short int seconds = update_interval * w;
7242 unsigned short int timeunits;
7244 timeunits = seconds / 86400; seconds %= 86400;
7245 if( timeunits > 0 ) {
7246 asprintf(&tmp_day_str, "%dd", timeunits);
7248 tmp_day_str = strdup("");
7250 timeunits = seconds / 3600; seconds %= 3600;
7251 if( timeunits > 0 ) {
7252 asprintf(&tmp_hour_str, "%dh", timeunits);
7254 tmp_hour_str = strdup("");
7256 timeunits = seconds / 60; seconds %= 60;
7258 asprintf(&tmp_min_str, "%dm", timeunits);
7260 tmp_min_str = strdup("");
7263 asprintf(&tmp_sec_str, "%ds", seconds);
7265 tmp_sec_str = strdup("");
7267 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
7268 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
7270 asprintf(&tmp_str, "Range not possible"); //should never happen, but better safe then sorry
7272 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
7273 cur_y += font_height() / 2;
7274 draw_string(tmp_str);
7280 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
7283 cur_x += font_ascent() / 2;
7284 cur_y += font_height() / 2;
7286 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
7288 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
7289 draw_string(tmp_str);
7295 set_foreground_color(last_colour);
7301 int old = font_ascent();
7303 cur_y -= font_ascent();
7304 selected_font = specials[special_index].font_added;
7305 if (cur_y + font_ascent() < cur_y + old) {
7308 cur_y += font_ascent();
7314 if (draw_mode == FG) {
7315 set_foreground_color(specials[special_index].arg);
7320 if (draw_mode == BG) {
7321 set_foreground_color(specials[special_index].arg);
7326 if (draw_mode == OUTLINE) {
7327 set_foreground_color(specials[special_index].arg);
7332 w += specials[special_index].arg;
7336 cur_y += specials[special_index].arg;
7340 if (specials[special_index].arg >= 0) {
7341 cur_x = (int) specials[special_index].arg;
7347 int start = specials[special_index].arg;
7348 int step = specials[special_index].width;
7350 if (!step || step < 0) {
7353 w = step - (cur_x - text_start_x - start) % step;
7359 /* TODO: add back in "+ border_margin" to the end of
7361 int pos_x = text_start_x + text_width -
7362 get_string_width_special(s);
7364 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7365 "get_string_width(p) %i gap_x %i "
7366 "specials[special_index].arg %i border_margin %i "
7367 "border_width %i\n", pos_x, text_start_x, text_width,
7368 cur_x, get_string_width_special(s), gap_x,
7369 specials[special_index].arg, border_margin,
7371 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
7372 cur_x = pos_x - specials[special_index].arg;
7379 int pos_x = (text_width) / 2 - get_string_width_special(s) /
7380 2 - (cur_x - text_start_x);
7381 /* int pos_x = text_start_x + text_width / 2 -
7382 get_string_width_special(s) / 2; */
7384 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7385 "get_string_width(p) %i gap_x %i "
7386 "specials[special_index].arg %i\n", pos_x, text_start_x,
7387 text_width, cur_x, get_string_width(s), gap_x,
7388 specials[special_index].arg); */
7389 if (pos_x > specials[special_index].arg) {
7390 w = pos_x - specials[special_index].arg;
7407 if (cur_y_add > 0) {
7409 cur_y -= font_descent();
7414 cur_y += font_descent();
7418 static void draw_text(void)
7421 cur_y = text_start_y;
7424 if (draw_borders && border_width > 0) {
7425 unsigned int b = (border_width + 1) / 2;
7427 if (stippled_borders) {
7428 char ss[2] = { stippled_borders, stippled_borders };
7429 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
7430 CapButt, JoinMiter);
7431 XSetDashes(display, window.gc, 0, ss, 2);
7433 XSetLineAttributes(display, window.gc, border_width, LineSolid,
7434 CapButt, JoinMiter);
7437 XDrawRectangle(display, window.drawable, window.gc,
7438 text_start_x - border_margin + b, text_start_y - border_margin + b,
7439 text_width + border_margin * 2 - 1 - b * 2,
7440 text_height + border_margin * 2 - 1 - b * 2);
7446 for_each_line(text_buffer, draw_line);
7449 static void draw_stuff(void)
7453 if (draw_shades && !draw_outline) {
7456 set_foreground_color(default_bg_color);
7467 for (i = -1; i < 2; i++) {
7468 for (j = -1; j < 2; j++) {
7469 if (i == 0 && j == 0) {
7474 set_foreground_color(default_out_color);
7475 draw_mode = OUTLINE;
7483 set_foreground_color(default_fg_color);
7492 swap.swap_window = window.window;
7493 swap.swap_action = XdbeBackground;
7494 XdbeSwapBuffers(display, &swap, 1);
7501 static void clear_text(int exposures)
7505 /* The swap action is XdbeBackground, which clears */
7510 /* there is some extra space for borders and outlines */
7511 XClearArea(display, window.window, text_start_x - border_margin - 1,
7512 text_start_y - border_margin - 1,
7513 text_width + border_margin * 2 + 2,
7514 text_height + border_margin * 2 + 2, exposures ? True : 0);
7519 static int need_to_update;
7521 /* update_text() generates new text and clears old text area */
7522 static void update_text(void)
7531 static void main_loop(void)
7533 #ifdef SIGNAL_BLOCKING
7538 Region region = XCreateRegion();
7542 XserverRegion region2, part;
7543 int event_base, error_base;
7545 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
7546 ERR("Xdamage extension unavailable");
7548 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7549 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7550 part = XFixesCreateRegionFromWindow(display, window.window, 0);
7551 #endif /* HAVE_XDAMAGE */
7554 #ifdef SIGNAL_BLOCKING
7555 sigemptyset(&newmask);
7556 sigaddset(&newmask, SIGINT);
7557 sigaddset(&newmask, SIGTERM);
7558 sigaddset(&newmask, SIGUSR1);
7561 next_update_time = last_update_time = get_time();
7563 while (total_run_times == 0 || info.looped < total_run_times) {
7566 #ifdef SIGNAL_BLOCKING
7567 /* block signals. we will inspect for pending signals later */
7568 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7569 CRIT_ERR("unable to sigprocmask()");
7576 /* wait for X event or timeout */
7578 if (!XPending(display)) {
7582 t = next_update_time - get_time();
7586 } else if (t > update_interval) {
7587 t = update_interval;
7590 tv.tv_sec = (long) t;
7591 tv.tv_usec = (long) (t * 1000000) % 1000000;
7593 FD_SET(ConnectionNumber(display), &fdsr);
7595 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7597 if (errno != EINTR) {
7598 ERR("can't select(): %s", strerror(errno));
7604 t = (next_update_time - get_time()) * 1000000;
7605 usleep((useconds_t)t);
7613 if (need_to_update) {
7615 int wx = window.x, wy = window.y;
7623 /* resize window if it isn't right size */
7625 && (text_width + border_margin * 2 + 1 != window.width
7626 || text_height + border_margin * 2 + 1 != window.height)) {
7627 window.width = text_width + border_margin * 2 + 1;
7628 window.height = text_height + border_margin * 2 + 1;
7629 XResizeWindow(display, window.window, window.width,
7632 set_transparent_background(window.window);
7636 /* move window if it isn't in right position */
7637 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7638 XMoveWindow(display, window.window, window.x, window.y);
7649 r.x = text_start_x - border_margin;
7650 r.y = text_start_y - border_margin;
7651 r.width = text_width + border_margin * 2;
7652 r.height = text_height + border_margin * 2;
7653 XUnionRectWithRegion(&r, region, region);
7658 /* handle X events */
7659 while (XPending(display)) {
7662 XNextEvent(display, &ev);
7670 r.width = ev.xexpose.width;
7671 r.height = ev.xexpose.height;
7672 XUnionRectWithRegion(&r, region, region);
7677 case ReparentNotify:
7678 /* set background to ParentRelative for all parents */
7680 set_transparent_background(window.window);
7684 case ConfigureNotify:
7686 /* if window size isn't what expected, set fixed size */
7687 if (ev.xconfigure.width != window.width
7688 || ev.xconfigure.height != window.height) {
7689 if (window.width != 0 && window.height != 0) {
7693 /* clear old stuff before screwing up
7698 XWindowAttributes attrs;
7700 if (XGetWindowAttributes(display,
7701 window.window, &attrs)) {
7702 window.width = attrs.width;
7703 window.height = attrs.height;
7707 text_width = window.width - border_margin * 2 - 1;
7708 text_height = window.height - border_margin * 2 - 1;
7709 if (text_width > maximum_width
7710 && maximum_width > 0) {
7711 text_width = maximum_width;
7715 /* if position isn't what expected, set fixed pos
7716 * total_updates avoids setting fixed_pos when window
7717 * is set to weird locations when started */
7718 /* // this is broken
7719 if (total_updates >= 2 && !fixed_pos
7720 && (window.x != ev.xconfigure.x
7721 || window.y != ev.xconfigure.y)
7722 && (ev.xconfigure.x != 0
7723 || ev.xconfigure.y != 0)) {
7732 /* if an ordinary window with decorations */
7733 if ((window.type == TYPE_NORMAL)
7734 && (!TEST_HINT(window.hints,
7735 HINT_UNDECORATED))) {
7736 /* allow conky to hold input focus. */
7739 /* forward the click to the desktop window */
7740 XUngrabPointer(display, ev.xbutton.time);
7741 ev.xbutton.window = window.desktop;
7742 XSendEvent(display, ev.xbutton.window, False,
7743 ButtonPressMask, &ev);
7744 XSetInputFocus(display, ev.xbutton.window,
7745 RevertToParent, ev.xbutton.time);
7752 /* if an ordinary window with decorations */
7753 if ((window.type == TYPE_NORMAL)
7754 && (!TEST_HINT(window.hints,
7755 HINT_UNDECORATED))) {
7756 /* allow conky to hold input focus. */
7759 /* forward the release to the desktop window */
7760 ev.xbutton.window = window.desktop;
7761 XSendEvent(display, ev.xbutton.window, False,
7762 ButtonReleaseMask, &ev);
7771 if (ev.type == event_base + XDamageNotify) {
7772 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7774 XFixesSetRegion(display, part, &dev->area, 1);
7775 XFixesUnionRegion(display, region2, region2, part);
7777 #endif /* HAVE_XDAMAGE */
7783 XDamageSubtract(display, damage, region2, None);
7784 XFixesSetRegion(display, region2, 0, 0);
7785 #endif /* HAVE_XDAMAGE */
7787 /* XDBE doesn't seem to provide a way to clear the back buffer without
7788 * interfering with the front buffer, other than passing XdbeBackground
7789 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7790 * redraw the text even if it wasn't part of the exposed area. OTOH,
7791 * if we're not going to call draw_stuff at all, then no swap happens
7792 * and we can safely do nothing. */
7794 if (!XEmptyRegion(region)) {
7799 r.x = text_start_x - border_margin;
7800 r.y = text_start_y - border_margin;
7801 r.width = text_width + border_margin * 2;
7802 r.height = text_height + border_margin * 2;
7803 XUnionRectWithRegion(&r, region, region);
7806 XSetRegion(display, window.gc, region);
7809 XftDrawSetClip(window.xftdraw, region);
7815 XDestroyRegion(region);
7816 region = XCreateRegion();
7820 #ifdef SIGNAL_BLOCKING
7821 /* unblock signals of interest and let handler fly */
7822 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7823 CRIT_ERR("unable to sigprocmask()");
7827 switch (g_signal_pending) {
7830 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7835 ERR("received SIGINT or SIGTERM to terminate. bye!");
7838 XDestroyRegion(region);
7841 XDamageDestroy(display, damage);
7842 XFixesDestroyRegion(display, region2);
7843 XFixesDestroyRegion(display, part);
7844 #endif /* HAVE_XDAMAGE */
7846 return; /* return from main_loop */
7849 /* Reaching here means someone set a signal
7850 * (SIGXXXX, signal_handler), but didn't write any code
7852 * If you don't want to handle a signal, don't set a handler on
7853 * it in the first place. */
7854 if (g_signal_pending) {
7855 ERR("ignoring signal (%d)", g_signal_pending);
7859 g_signal_pending = 0;
7862 #if defined(X11) && defined(HAVE_XDAMAGE)
7863 XDamageDestroy(display, damage);
7864 XFixesDestroyRegion(display, region2);
7865 XFixesDestroyRegion(display, part);
7866 XDestroyRegion(region);
7868 #endif /* X11 && HAVE_XDAMAGE */
7871 static void load_config_file(const char *);
7873 /* reload the config file */
7874 void reload_config(void)
7876 timed_thread_destroy_registered_threads();
7878 if (info.cpu_usage) {
7879 free(info.cpu_usage);
7880 info.cpu_usage = NULL;
7888 if (info.mpd.title) {
7889 free(info.mpd.title);
7890 info.mpd.title = NULL;
7892 if (info.mpd.artist) {
7893 free(info.mpd.artist);
7894 info.mpd.artist = NULL;
7896 if (info.mpd.album) {
7897 free(info.mpd.album);
7898 info.mpd.album = NULL;
7900 if (info.mpd.random) {
7901 free(info.mpd.random);
7902 info.mpd.random = NULL;
7904 if (info.mpd.repeat) {
7905 free(info.mpd.repeat);
7906 info.mpd.repeat = NULL;
7908 if (info.mpd.track) {
7909 free(info.mpd.track);
7910 info.mpd.track = NULL;
7912 if (info.mpd.name) {
7913 free(info.mpd.name);
7914 info.mpd.name = NULL;
7916 if (info.mpd.file) {
7917 free(info.mpd.file);
7918 info.mpd.file = NULL;
7920 if (info.mpd.status) {
7921 free(info.mpd.status);
7922 info.mpd.status = NULL;
7927 free_moc(&info.moc);
7934 #ifdef TCP_PORT_MONITOR
7935 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7938 if (current_config) {
7940 load_config_file(current_config);
7942 /* re-init specials array */
7943 if ((specials = realloc((void *) specials,
7944 sizeof(struct special_t) * max_specials)) == 0) {
7945 ERR("failed to realloc specials array");
7951 // clear the window first
7952 XClearWindow(display, RootWindow(display, screen));
7955 #ifdef TCP_PORT_MONITOR
7956 info.p_tcp_port_monitor_collection = NULL;
7958 extract_variable_text(global_text);
7964 tmpstring1 = malloc(text_buffer_size);
7965 memset(tmpstring1, 0, text_buffer_size);
7969 tmpstring2 = malloc(text_buffer_size);
7970 memset(tmpstring2, 0, text_buffer_size);
7974 text_buffer = malloc(max_user_text);
7975 memset(text_buffer, 0, max_user_text);
7978 sigemptyset(&oldmask);
7983 timed_thread_destroy_registered_threads();
7985 if (info.cpu_usage) {
7986 free(info.cpu_usage);
7987 info.cpu_usage = NULL;
7992 XdbeDeallocateBackBufferName(display, window.back_buffer);
7997 XDestroyWindow(display, window.window);
7998 XClearWindow(display, RootWindow(display, screen));
8003 XClearWindow(display, RootWindow(display, screen));
8008 XFreeGC(display, window.gc);
8012 free_text_objects(global_text_object_list, 1);
8013 free(global_text_object_list);
8014 global_text_object_list = NULL;
8033 free(current_config);
8035 #ifdef TCP_PORT_MONITOR
8036 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
8037 info.p_tcp_port_monitor_collection = NULL;
8046 for (i = 0; i < special_count; i++) {
8047 if (specials[i].type == GRAPH) {
8048 free(specials[i].graph);
8055 clear_diskio_stats();
8058 static int string_to_bool(const char *s)
8061 // Assumes an option without a true/false means true
8063 } else if (strcasecmp(s, "yes") == EQUAL) {
8065 } else if (strcasecmp(s, "true") == EQUAL) {
8067 } else if (strcasecmp(s, "1") == EQUAL) {
8074 static enum alignment string_to_alignment(const char *s)
8076 if (strcasecmp(s, "top_left") == EQUAL) {
8078 } else if (strcasecmp(s, "top_right") == EQUAL) {
8080 } else if (strcasecmp(s, "top_middle") == EQUAL) {
8082 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
8084 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
8085 return BOTTOM_RIGHT;
8086 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
8087 return BOTTOM_MIDDLE;
8088 } else if (strcasecmp(s, "middle_left") == EQUAL) {
8090 } else if (strcasecmp(s, "middle_right") == EQUAL) {
8091 return MIDDLE_RIGHT;
8092 } else if (strcasecmp(s, "tl") == EQUAL) {
8094 } else if (strcasecmp(s, "tr") == EQUAL) {
8096 } else if (strcasecmp(s, "tm") == EQUAL) {
8098 } else if (strcasecmp(s, "bl") == EQUAL) {
8100 } else if (strcasecmp(s, "br") == EQUAL) {
8101 return BOTTOM_RIGHT;
8102 } else if (strcasecmp(s, "bm") == EQUAL) {
8103 return BOTTOM_MIDDLE;
8104 } else if (strcasecmp(s, "ml") == EQUAL) {
8106 } else if (strcasecmp(s, "mr") == EQUAL) {
8107 return MIDDLE_RIGHT;
8108 } else if (strcasecmp(s, "none") == EQUAL) {
8115 static void set_default_configurations(void)
8117 fork_to_background = 0;
8118 total_run_times = 0;
8119 info.cpu_avg_samples = 2;
8120 info.net_avg_samples = 2;
8127 strcpy(info.mpd.host, "localhost");
8128 info.mpd.port = 6600;
8129 info.mpd.status = NULL;
8130 info.mpd.artist = NULL;
8131 info.mpd.album = NULL;
8132 info.mpd.title = NULL;
8133 info.mpd.random = NULL;
8134 info.mpd.track = NULL;
8135 info.mpd.name = NULL;
8136 info.mpd.file = NULL;
8139 init_moc(&info.moc);
8142 info.xmms2.artist = NULL;
8143 info.xmms2.album = NULL;
8144 info.xmms2.title = NULL;
8145 info.xmms2.genre = NULL;
8146 info.xmms2.comment = NULL;
8147 info.xmms2.url = NULL;
8148 info.xmms2.status = NULL;
8149 info.xmms2.playlist = NULL;
8151 use_spacer = NO_SPACER;
8153 output_methods = TO_X;
8155 output_methods = TO_STDOUT;
8158 show_graph_scale = 0;
8159 show_graph_range = 0;
8160 default_fg_color = WhitePixel(display, screen);
8161 default_bg_color = BlackPixel(display, screen);
8162 default_out_color = BlackPixel(display, screen);
8163 color0 = default_fg_color;
8164 color1 = default_fg_color;
8165 color2 = default_fg_color;
8166 color3 = default_fg_color;
8167 color4 = default_fg_color;
8168 color5 = default_fg_color;
8169 color6 = default_fg_color;
8170 color7 = default_fg_color;
8171 color8 = default_fg_color;
8172 color9 = default_fg_color;
8175 draw_graph_borders = 1;
8177 set_first_font("6x10");
8185 window.type = TYPE_NORMAL;
8187 strcpy(window.class_name, PACKAGE_NAME);
8189 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
8191 stippled_borders = 0;
8194 text_alignment = BOTTOM_LEFT;
8195 info.x11.monitor.number = 1;
8196 info.x11.monitor.current = 0;
8199 free(current_mail_spool);
8203 variable_substitute(MAIL_FILE, buf, 256);
8204 if (buf[0] != '\0') {
8205 current_mail_spool = strndup(buf, text_buffer_size);
8210 update_interval = 3.0;
8211 info.music_player_interval = 1.0;
8212 stuff_in_upper_case = 0;
8213 info.users.number = 1;
8215 #ifdef TCP_PORT_MONITOR
8216 tcp_port_monitor_args.max_port_monitor_connections =
8217 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8221 static void load_config_file(const char *f)
8223 #define CONF_ERR ERR("%s: %d: config file error", f, line)
8227 set_default_configurations();
8234 char buf[256], *p, *p2, *name, *value;
8237 if (fgets(buf, 256, fp) == NULL) {
8243 /* break at comment */
8244 p2 = strchr(p, '#');
8250 while (*p && isspace((int) *p)) {
8254 continue; /* empty line */
8261 while (*p2 && !isspace((int) *p2)) {
8265 *p2 = '\0'; /* break at name's end */
8272 while (*p && isspace((int) *p)) {
8278 p2 = value + strlen(value);
8279 while (isspace((int) *(p2 - 1))) {
8286 #define CONF2(a) if (strcasecmp(name, a) == 0)
8287 #define CONF(a) else CONF2(a)
8288 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
8289 || strcasecmp(name, b) == 0)
8292 CONF2("alignment") {
8294 int a = string_to_alignment(value);
8305 CONF("background") {
8306 fork_to_background = string_to_bool(value);
8309 CONF2("background") {
8310 fork_to_background = string_to_bool(value);
8314 CONF("show_graph_scale") {
8315 show_graph_scale = string_to_bool(value);
8317 CONF("show_graph_range") {
8318 show_graph_range = string_to_bool(value);
8320 CONF("border_margin") {
8322 border_margin = strtol(value, 0, 0);
8327 CONF("border_width") {
8329 border_width = strtol(value, 0, 0);
8336 color0 = get_x11_color(value);
8343 color1 = get_x11_color(value);
8350 color2 = get_x11_color(value);
8357 color3 = get_x11_color(value);
8364 color4 = get_x11_color(value);
8371 color5 = get_x11_color(value);
8378 color6 = get_x11_color(value);
8385 color7 = get_x11_color(value);
8392 color8 = get_x11_color(value);
8399 color9 = get_x11_color(value);
8404 CONF("default_color") {
8406 default_fg_color = get_x11_color(value);
8411 CONF3("default_shade_color", "default_shadecolor") {
8413 default_bg_color = get_x11_color(value);
8418 CONF3("default_outline_color", "default_outlinecolor") {
8420 default_out_color = get_x11_color(value);
8428 info.mail = parse_mail_args(IMAP, value);
8435 info.mail = parse_mail_args(POP3, value);
8443 strncpy(info.mpd.host, value, 127);
8450 info.mpd.port = strtol(value, 0, 0);
8451 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
8456 CONF("mpd_password") {
8458 strncpy(info.mpd.password, value, 127);
8464 CONF("music_player_interval") {
8466 info.music_player_interval = strtod(value, 0);
8472 CONF("sensor_device") {
8474 sensor_device = strtol(value, 0, 0);
8480 CONF("cpu_avg_samples") {
8482 cpu_avg_samples = strtol(value, 0, 0);
8483 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8486 info.cpu_avg_samples = cpu_avg_samples;
8492 CONF("net_avg_samples") {
8494 net_avg_samples = strtol(value, 0, 0);
8495 if (net_avg_samples < 1 || net_avg_samples > 14) {
8498 info.net_avg_samples = net_avg_samples;
8506 CONF("double_buffer") {
8507 use_xdbe = string_to_bool(value);
8511 CONF("override_utf8_locale") {
8512 utf8_mode = string_to_bool(value);
8514 CONF("draw_borders") {
8515 draw_borders = string_to_bool(value);
8517 CONF("draw_graph_borders") {
8518 draw_graph_borders = string_to_bool(value);
8520 CONF("draw_shades") {
8521 draw_shades = string_to_bool(value);
8523 CONF("draw_outline") {
8524 draw_outline = string_to_bool(value);
8527 CONF("out_to_console") {
8528 if(string_to_bool(value)) output_methods |= TO_STDOUT;
8530 CONF("use_spacer") {
8532 if (strcasecmp(value, "left") == EQUAL) {
8533 use_spacer = LEFT_SPACER;
8534 } else if (strcasecmp(value, "right") == EQUAL) {
8535 use_spacer = RIGHT_SPACER;
8536 } else if (strcasecmp(value, "none") == EQUAL) {
8537 use_spacer = NO_SPACER;
8539 use_spacer = string_to_bool(value);
8540 ERR("use_spacer should have an argument of left, right, or"
8541 " none. '%s' seems to be some form of '%s', so"
8542 " defaulting to %s.", value,
8543 use_spacer ? "true" : "false",
8544 use_spacer ? "right" : "none");
8546 use_spacer = RIGHT_SPACER;
8548 use_spacer = NO_SPACER;
8552 ERR("use_spacer should have an argument. Defaulting to right.");
8553 use_spacer = RIGHT_SPACER;
8559 use_xft = string_to_bool(value);
8563 set_first_font(value);
8569 if (value && font_count >= 0) {
8570 fonts[0].font_alpha = atof(value) * 65535.0;
8579 if (string_to_bool(value)) {
8580 ERR("Xft not enabled");
8584 /* xftfont silently ignored when no Xft */
8587 /* xftalpha is silently ignored when no Xft */
8592 set_first_font(value);
8602 gap_x = atoi(value);
8609 gap_y = atoi(value);
8615 CONF("mail_spool") {
8619 variable_substitute(value, buffer, 256);
8621 if (buffer[0] != '\0') {
8622 if (current_mail_spool) {
8623 free(current_mail_spool);
8625 current_mail_spool = strndup(buffer, text_buffer_size);
8632 CONF("minimum_size") {
8634 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8636 if (sscanf(value, "%d", &minimum_width) != 1) {
8644 CONF("maximum_width") {
8646 if (sscanf(value, "%d", &maximum_width) != 1) {
8654 CONF("no_buffers") {
8655 no_buffers = string_to_bool(value);
8657 CONF("top_cpu_separate") {
8658 cpu_separate = string_to_bool(value);
8660 CONF("short_units") {
8661 short_units = string_to_bool(value);
8663 CONF("pad_percents") {
8664 pad_percents = atoi(value);
8668 CONF("own_window") {
8670 own_window = string_to_bool(value);
8675 CONF("own_window_class") {
8677 memset(window.class_name, 0, sizeof(window.class_name));
8678 strncpy(window.class_name, value,
8679 sizeof(window.class_name) - 1);
8684 CONF("own_window_title") {
8686 memset(window.title, 0, sizeof(window.title));
8687 strncpy(window.title, value, sizeof(window.title) - 1);
8692 CONF("own_window_transparent") {
8694 set_transparent = string_to_bool(value);
8699 CONF("own_window_colour") {
8701 background_colour = get_x11_color(value);
8703 ERR("Invalid colour for own_window_colour (try omitting the "
8704 "'#' for hex colours");
8707 CONF("own_window_hints") {
8709 char *p_hint, *p_save;
8710 char delim[] = ", ";
8712 /* tokenize the value into individual hints */
8713 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8715 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8716 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8717 SET_HINT(window.hints, HINT_UNDECORATED);
8718 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8719 SET_HINT(window.hints, HINT_BELOW);
8720 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8721 SET_HINT(window.hints, HINT_ABOVE);
8722 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8723 SET_HINT(window.hints, HINT_STICKY);
8724 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8725 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8726 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8727 SET_HINT(window.hints, HINT_SKIP_PAGER);
8732 p_hint = strtok_r(NULL, delim, &p_save);
8733 } while (p_hint != NULL);
8739 CONF("own_window_type") {
8741 if (strncmp(value, "normal", 6) == EQUAL) {
8742 window.type = TYPE_NORMAL;
8743 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8744 window.type = TYPE_DESKTOP;
8745 } else if (strncmp(value, "dock", 7) == EQUAL) {
8746 window.type = TYPE_DOCK;
8747 } else if (strncmp(value, "override", 8) == EQUAL) {
8748 window.type = TYPE_OVERRIDE;
8757 CONF("stippled_borders") {
8759 stippled_borders = strtol(value, 0, 0);
8761 stippled_borders = 4;
8766 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
8770 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
8773 CONF("update_interval") {
8775 update_interval = strtod(value, 0);
8779 if (info.music_player_interval == 0) {
8780 // default to update_interval
8781 info.music_player_interval = update_interval;
8784 CONF("total_run_times") {
8786 total_run_times = strtod(value, 0);
8792 stuff_in_upper_case = string_to_bool(value);
8794 CONF("max_specials") {
8796 max_specials = atoi(value);
8801 CONF("max_user_text") {
8803 max_user_text = atoi(value);
8808 CONF("text_buffer_size") {
8810 text_buffer_size = atoi(value);
8811 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8812 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8813 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8825 global_text = (char *) malloc(1);
8826 global_text[0] = '\0';
8829 unsigned int l = strlen(global_text);
8831 if (fgets(buf, 256, fp) == NULL) {
8834 global_text = (char *) realloc(global_text, l + strlen(buf) + 1);
8835 strcat(global_text, buf);
8837 if (strlen(global_text) > max_user_text) {
8842 if (strlen(global_text) < 1) {
8843 CRIT_ERR("no text supplied in configuration; exiting");
8845 global_text_lines = line + 1;
8848 #ifdef TCP_PORT_MONITOR
8849 CONF("max_port_monitor_connections") {
8850 if (!value || (sscanf(value, "%d",
8851 &tcp_port_monitor_args.max_port_monitor_connections) != 1)
8852 || tcp_port_monitor_args.max_port_monitor_connections < 0) {
8853 /* an error. use default, warn and continue. */
8854 tcp_port_monitor_args.max_port_monitor_connections =
8855 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8857 } else if (tcp_port_monitor_args.max_port_monitor_connections
8859 /* no error, just use default */
8860 tcp_port_monitor_args.max_port_monitor_connections =
8861 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8863 /* else tcp_port_monitor_args.max_port_monitor_connections > 0
8867 CONF("if_up_strictness") {
8869 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8870 ifup_strictness = IFUP_UP;
8871 } else if (strcasecmp(value, "up") == EQUAL) {
8872 ifup_strictness = IFUP_UP;
8873 } else if (strcasecmp(value, "link") == EQUAL) {
8874 ifup_strictness = IFUP_LINK;
8875 } else if (strcasecmp(value, "address") == EQUAL) {
8876 ifup_strictness = IFUP_ADDR;
8878 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8879 ifup_strictness = IFUP_UP;
8883 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8894 if (info.music_player_interval == 0) {
8895 // default to update_interval
8896 info.music_player_interval = update_interval;
8898 if (!global_text) { // didn't supply any text
8899 CRIT_ERR("missing text block in configuration; exiting");
8903 static void print_help(const char *prog_name) {
8904 printf("Usage: %s [OPTION]...\n"
8905 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8906 "window. Command line options will override configurations defined in config\n"
8908 " -v, --version version\n"
8909 " -q, --quiet quiet mode\n"
8910 " -c, --config=FILE config file to load\n"
8911 " -d, --daemonize daemonize, fork to background\n"
8912 " -h, --help help\n"
8914 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8915 " -f, --font=FONT font to use\n"
8917 " -o, --own-window create own window to draw\n"
8920 " -b, --double-buffer double buffer (prevents flickering)\n"
8922 " -w, --window-id=WIN_ID window id to draw\n"
8923 " -x X x position\n"
8924 " -y Y y position\n"
8926 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8927 " -u, --interval=SECS update interval\n"
8928 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8933 /* : means that character before that takes an argument */
8934 static const char *getopt_string = "vVqdt:u:i:hc:"
8946 static const struct option longopts[] = {
8947 { "help", 0, NULL, 'h' },
8948 { "version", 0, NULL, 'V' },
8949 { "config", 1, NULL, 'c' },
8950 { "daemonize", 0, NULL, 'd' },
8952 { "alignment", 1, NULL, 'a' },
8953 { "font", 1, NULL, 'f' },
8955 { "own-window", 0, NULL, 'o' },
8958 { "double-buffer", 0, NULL, 'b' },
8960 { "window-id", 1, NULL, 'w' },
8962 { "text", 1, NULL, 't' },
8963 { "interval", 0, NULL, 'u' },
8967 int main(int argc, char **argv)
8973 struct sigaction act, oact;
8975 g_signal_pending = 0;
8976 memset(&info, 0, sizeof(info));
8979 #ifdef TCP_PORT_MONITOR
8980 tcp_port_monitor_args.max_port_monitor_connections =
8981 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8984 /* handle command line parameters that don't change configs */
8986 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8987 || ((s = getenv("LANG")) && *s)) {
8988 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8990 ERR("malloc failed");
8992 for (x = 0; x < strlen(s); x++) {
8993 temp[x] = tolower(s[x]);
8996 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9002 if (!setlocale(LC_CTYPE, "")) {
9003 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9007 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9018 if (current_config) {
9019 free(current_config);
9021 current_config = strndup(optarg, max_user_text);
9024 freopen("/dev/null", "w", stderr);
9027 print_help(argv[0]);
9031 window.window = strtol(optarg, 0, 0);
9040 /* initalize X BEFORE we load config.
9041 * (we need to so that 'screen' is set) */
9045 /* check if specified config file is valid */
9046 if (current_config) {
9048 if (stat(current_config, &sb) ||
9049 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9050 ERR("invalid configuration file '%s'\n", current_config);
9051 free(current_config);
9056 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9058 if (!current_config) {
9059 /* load default config file */
9063 /* Try to use personal config file first */
9064 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
9065 if (buf[0] && (fp = fopen(buf, "r"))) {
9066 current_config = strndup(buf, max_user_text);
9070 /* Try to use system config file if personal config not readable */
9071 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9072 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9076 /* No readable config found */
9077 if (!current_config) {
9078 CRIT_ERR("no readable personal or system-wide config file found");
9082 load_config_file(current_config);
9084 /* init specials array */
9085 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9086 ERR("failed to create specials array");
9090 if (current_mail_spool == NULL) {
9093 variable_substitute(MAIL_FILE, buf, 256);
9095 if (buf[0] != '\0') {
9096 current_mail_spool = strndup(buf, text_buffer_size);
9101 /* handle other command line arguments */
9103 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9104 || defined(__NetBSD__)
9105 optind = optreset = 1;
9110 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9111 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9112 "kvm_open")) == NULL) {
9113 CRIT_ERR("cannot read kvm");
9118 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9126 fork_to_background = 1;
9131 set_first_font(optarg);
9134 text_alignment = string_to_alignment(optarg);
9153 global_text = strndup(optarg, max_user_text);
9154 convert_escapes(global_text);
9158 update_interval = strtod(optarg, 0);
9159 if (info.music_player_interval == 0) {
9160 // default to update_interval
9161 info.music_player_interval = update_interval;
9166 total_run_times = strtod(optarg, 0);
9170 gap_x = atoi(optarg);
9174 gap_y = atoi(optarg);
9188 /* generate text and get initial size */
9189 extract_variable_text(global_text);
9196 if (fork_to_background) {
9201 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9208 fprintf(stderr, "\n");
9213 /* parent process */
9214 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9221 text_buffer = malloc(max_user_text);
9222 memset(text_buffer, 0, max_user_text);
9223 tmpstring1 = malloc(text_buffer_size);
9224 memset(tmpstring1, 0, text_buffer_size);
9225 tmpstring2 = malloc(text_buffer_size);
9226 memset(tmpstring2, 0, text_buffer_size);
9230 update_text_area(); /* to get initial size of the window */
9233 init_window(own_window, text_width + border_margin * 2 + 1,
9234 text_height + border_margin * 2 + 1, set_transparent, background_colour,
9236 #else /* OWN_WINDOW */
9237 init_window(0, text_width + border_margin * 2 + 1,
9238 text_height + border_margin * 2 + 1, set_transparent, 0,
9240 #endif /* OWN_WINDOW */
9243 update_text_area(); /* to position text/window on screen */
9246 if (own_window && !fixed_pos) {
9247 XMoveWindow(display, window.window, window.x, window.y);
9250 set_transparent_background(window.window);
9260 /* Set signal handlers */
9261 act.sa_handler = signal_handler;
9262 sigemptyset(&act.sa_mask);
9265 act.sa_flags |= SA_RESTART;
9268 if ( sigaction(SIGINT, &act, &oact) < 0
9269 || sigaction(SIGUSR1, &act, &oact) < 0
9270 || sigaction(SIGHUP, &act, &oact) < 0
9271 || sigaction(SIGTERM, &act, &oact) < 0) {
9272 ERR("error setting signal handler: %s", strerror(errno));
9277 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9284 void signal_handler(int sig)
9286 /* signal handler is light as a feather, as it should be.
9287 * we will poll g_signal_pending with each loop of conky
9288 * and do any signal processing there, NOT here. */
9289 g_signal_pending = sig;