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
76 static void print_version(void) __attribute__((noreturn));
78 static void print_version(void)
80 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
82 printf("\nCompiled in features:\n\n"
83 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
87 " * Xdamage extension\n"
88 # endif /* HAVE_XDAMAGE */
90 " * Xdbe extension (double buffer)\n"
91 # endif /* HAVE_XDBE */
96 "\n Music detection:\n"
99 #endif /* AUDACIOUS */
112 "\n General features:\n"
119 #ifdef TCP_PORT_MONITOR
121 #endif /* TCP_PORT_MONITOR */
130 #endif /* HAVE_IWLIB */
142 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
149 static int text_start_x, text_start_y; /* text start position in window */
150 static int text_width, text_height;
168 char name[DEFAULT_TEXT_BUFFER_SIZE];
178 static int selected_font = 0;
179 static int font_count = -1;
180 struct font_list *fonts = NULL;
184 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
185 fonts[selected_font].xftfont->descent) \
186 : (fonts[selected_font].font->max_bounds.ascent + \
187 fonts[selected_font].font->max_bounds.descent))
188 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
189 : fonts[selected_font].font->max_bounds.ascent)
190 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
191 : fonts[selected_font].font->max_bounds.descent)
195 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
196 fonts[selected_font].font->max_bounds.descent)
197 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
198 #define font_descent() fonts[selected_font].font->max_bounds.descent
202 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
204 static void set_font(void);
206 int addfont(const char *data_in)
208 if (font_count > MAX_FONTS) {
209 CRIT_ERR("you don't need that many fonts, sorry.");
212 if (font_count == 0) {
216 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
220 memset(fonts, 0, sizeof(struct font_list));
222 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
223 memset(&fonts[font_count], 0, sizeof(struct font_list));
225 CRIT_ERR("realloc in addfont");
227 // must account for null terminator
228 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
229 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
231 fonts[font_count].font_alpha = 0xffff;
234 CRIT_ERR("Oops...looks like something overflowed in addfont().");
239 void set_first_font(const char *data_in)
241 if (font_count < 0) {
242 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
246 memset(fonts, 0, sizeof(struct font_list));
249 if (strlen(data_in) > 1) {
250 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
252 fonts[0].font_alpha = 0xffff;
257 void free_fonts(void)
261 for (i = 0; i <= font_count; i++) {
264 XftFontClose(display, fonts[i].xftfont);
265 fonts[i].xftfont = 0;
269 XFreeFont(display, fonts[i].font);
279 static void load_fonts(void)
283 for (i = 0; i <= font_count; i++) {
286 if (use_xft && fonts[i].xftfont) {
288 } else if (use_xft) {
289 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
290 XftFontClose(display, fonts[i].xftfont);
292 fonts[i].xftfont = XftFontOpenName(display, screen,
294 if (fonts[i].xftfont != NULL) {
298 ERR("can't load Xft font '%s'", fonts[i].name);
299 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
300 "courier-12")) != NULL) {
304 ERR("can't load Xft font '%s'", "courier-12");
306 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
307 CRIT_ERR("can't load font '%s'", "fixed");
314 /* load normal font */
315 /* if (fonts[i].font != NULL) {
316 XFreeFont(display, fonts[i].font);
319 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
320 ERR("can't load font '%s'", fonts[i].name);
321 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
322 CRIT_ERR("can't load font '%s'", "fixed");
323 printf("loaded fixed?\n");
331 /* default config file */
332 static char *current_config;
334 /* set to 1 if you want all text to be in uppercase */
335 static unsigned int stuff_in_upper_case;
337 /* Run how many times? */
338 static unsigned long total_run_times;
341 static int fork_to_background;
343 static int cpu_avg_samples, net_avg_samples;
347 static int show_graph_scale;
348 static int show_graph_range;
350 /* Position on the screen */
351 static int text_alignment;
352 static int gap_x, gap_y;
355 static int draw_borders;
356 static int draw_graph_borders;
357 static int stippled_borders;
359 static int draw_shades, draw_outline;
361 static int border_margin, border_width;
363 static long default_fg_color, default_bg_color, default_out_color;
365 /* create own window or draw stuff to root? */
366 static int set_transparent = 0;
369 static int own_window = 0;
370 static int background_colour = 0;
372 /* fixed size/pos is set if wm/user changes them */
373 static int fixed_size = 0, fixed_pos = 0;
376 static int minimum_width, minimum_height;
377 static int maximum_width;
382 static int sensor_device;
385 static long color0, color1, color2, color3, color4, color5, color6, color7,
388 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
389 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
391 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
392 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
394 /* maximum size of individual text buffers, ie $exec buffer size */
395 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
398 #define CODEPAGE_LENGTH 20
400 long iconv_count = 0;
401 char iconv_converting;
402 static iconv_t **iconv_cd = 0;
404 int register_iconv(iconv_t *new_iconv)
406 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
408 CRIT_ERR("Out of memory");
410 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
411 if (!iconv_cd[iconv_count]) {
412 CRIT_ERR("Out of memory");
414 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
419 void free_iconv(void)
424 for (i = 0; i < iconv_count; i++) {
426 iconv_close(*iconv_cd[i]);
440 /* no buffers in used memory? */
443 /* pad percentages to decimals? */
444 static int pad_percents = 0;
446 #ifdef TCP_PORT_MONITOR
447 tcp_port_monitor_args_t tcp_port_monitor_args;
450 static char *global_text = 0;
451 long global_text_lines;
453 static int total_updates;
456 static int blockdepth = 0;
457 static int if_jumped = 0;
458 static int blockstart[MAX_IF_BLOCK_DEPTH];
460 int check_contains(char *f, char *s)
463 FILE *where = fopen(f, "r");
466 char buf1[256], buf2[256];
468 while (fgets(buf1, 256, where)) {
469 sscanf(buf1, "%255s", buf2);
470 if (strstr(buf2, s)) {
477 ERR("Could not open the file");
483 static inline int calc_text_width(const char *s, int l)
490 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
491 (const FcChar8 *) s, l, &gi);
493 XftTextExtents8(display, fonts[selected_font].xftfont,
494 (const FcChar8 *) s, l, &gi);
500 return XTextWidth(fonts[selected_font].font, s, l);
505 /* formatted text to render on screen, generated in generate_text(),
506 * drawn in draw_stuff() */
508 static char *text_buffer;
510 /* special stuff in text_buffer */
512 #define SPECIAL_CHAR '\x01'
541 unsigned long first_colour; // for graph gradient
542 unsigned long last_colour;
546 /* create specials array on heap instead of stack with introduction of
548 static struct special_t *specials = NULL;
550 static unsigned int special_count;
553 static unsigned int special_index; /* used when drawing */
556 /* why 256? cause an array of more then 256 doubles seems excessive,
557 * and who needs that kind of precision anyway? */
558 #define MAX_GRAPH_DEPTH 256
560 static struct special_t *new_special(char *buf, enum special_types t)
562 if (special_count >= max_specials) {
563 CRIT_ERR("too many special things in text");
566 buf[0] = SPECIAL_CHAR;
568 specials[special_count].type = t;
569 return &specials[special_count++];
572 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
576 unsigned int count = 0;
577 static char buf[BUFSZ];
578 long orig_pos = ftell(fp);
580 long buf_size = BUFSZ;
581 char *cur_found = NULL;
583 while (count < step) {
584 if (cur_found == NULL) {
585 buf_size = fread(buf, 1, buf_size, fp);
588 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
589 if (cur_found != NULL) {
590 buf_pos = cur_found - buf + 1;
599 ret = ftell(fp) - buf_size + buf_pos - 1;
601 fseek(fp, orig_pos, SEEK_SET);
606 void *memrchr(const void *buffer, char c, size_t n)
608 const unsigned char *p = buffer;
610 for (p += n; n; n--) {
619 long rev_fcharfind(FILE *fp, char val, unsigned int step)
623 unsigned int count = 0;
624 static char buf[BUFSZ];
625 long orig_pos = ftell(fp);
627 long file_pos = orig_pos;
628 long buf_size = BUFSZ;
631 while (count < step) {
633 if (file_pos > BUFSZ) {
634 fseek(fp, file_pos - BUFSZ, SEEK_SET);
637 fseek(fp, 0, SEEK_SET);
639 file_pos = ftell(fp);
640 buf_pos = fread(buf, 1, buf_size, fp);
642 cur_found = memrchr(buf, val, (size_t) buf_pos);
643 if (cur_found != NULL) {
644 buf_pos = cur_found - buf;
653 fseek(fp, orig_pos, SEEK_SET);
655 ret = file_pos + buf_pos;
660 static void new_bar(char *buf, int w, int h, int usage)
662 struct special_t *s = new_special(buf, BAR);
664 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
669 static const char *scan_bar(const char *args, int *w, int *h)
671 /* zero width means all space that is available */
674 /* bar's argument is either height or height,width */
678 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
679 sscanf(args, "%d %n", h, &n);
687 static char *scan_font(const char *args)
690 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
697 static void new_font(char *buf, char *args)
700 struct special_t *s = new_special(buf, FONT);
702 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
703 int tmp = selected_font;
705 selected_font = s->font_added = addfont(args);
710 struct special_t *s = new_special(buf, FONT);
711 int tmp = selected_font;
713 selected_font = s->font_added = 0;
718 void graph_append(struct special_t *graph, double f, char showaslog)
728 if (!graph->scaled && f > graph->graph_scale) {
729 f = graph->graph_scale;
732 /* Already happens in new_graph
734 graph->graph_scale = 1;
737 graph->graph[0] = f; /* add new data */
738 /* shift all the data by 1 */
739 for (i = graph->graph_width - 1; i > 0; i--) {
740 graph->graph[i] = graph->graph[i - 1];
741 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
742 /* check if we need to update the scale */
743 graph->graph_scale = graph->graph[i];
748 short colour_depth = 0;
749 void set_up_gradient(void);
751 /* precalculated: 31/255, and 63/255 */
752 #define CONST_8_TO_5_BITS 0.12156862745098
753 #define CONST_8_TO_6_BITS 0.247058823529412
755 /* adjust color values depending on color depth */
756 static unsigned int adjust_colors(unsigned int color)
760 if (colour_depth == 0) {
763 if (colour_depth == 16) {
764 r = (color & 0xff0000) >> 16;
765 g = (color & 0xff00) >> 8;
767 color = (int) (r * CONST_8_TO_5_BITS) << 11;
768 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
769 color |= (int) (b * CONST_8_TO_5_BITS);
774 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
775 unsigned int second_colour, double i, int scale, int append, char showaslog)
777 struct special_t *s = new_special(buf, GRAPH);
780 if (s->graph == NULL) {
781 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
782 // subtract 2 for the box
783 s->graph_width = s->width /* - 2 */;
785 s->graph_width = MAX_GRAPH_DEPTH - 2;
787 s->graph = malloc(s->graph_width * sizeof(double));
788 memset(s->graph, 0, s->graph_width * sizeof(double));
789 s->graph_scale = 100;
792 s->first_colour = adjust_colors(first_colour);
793 s->last_colour = adjust_colors(second_colour);
796 s->graph_scale = scale;
804 s->graph_width = s->width - 2; // subtract 2 for rectangle around
808 s->graph_scale = log10(s->graph_scale + 1);
812 graph_append(s, i, showaslog);
816 //don't use spaces in LOGGRAPH or NORMGRAPH if you change them
817 #define LOGGRAPH "log"
818 #define NORMGRAPH "normal"
820 static char *scan_graph(const char *args, int *w, int *h,
821 unsigned int *first_colour, unsigned int *last_colour,
822 unsigned int *scale, char *showaslog)
824 const char *nographtype;
828 /* zero width means all space that is available */
835 //set showaslog and place the rest of the args in nographtype
836 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
839 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
842 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
844 nographtype = &args[strlen(LOGGRAPH) + 1];
845 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
847 nographtype = &args[strlen(NORMGRAPH) + 1];
852 //check the rest of the args
853 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
857 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
860 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
861 return strndup(buf, text_buffer_size);
864 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
865 return strndup(buf, text_buffer_size);
870 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
874 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
877 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
878 return strndup(buf, text_buffer_size);
881 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
882 return strndup(buf, text_buffer_size);
887 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
891 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
894 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
896 //TODO: check the return value and throw an error?
897 sscanf(nographtype, "%63s %d,%d", buf, h, w);
900 return strndup(buf, text_buffer_size);
903 if (buf[0] == '\0') {
906 return strndup(buf, text_buffer_size);
910 static inline void new_hr(char *buf, int a)
912 new_special(buf, HORIZONTAL_LINE)->height = a;
915 static inline void new_stippled_hr(char *buf, int a, int b)
917 struct special_t *s = new_special(buf, STIPPLED_HR);
923 static inline void new_fg(char *buf, long c)
925 new_special(buf, FG)->arg = c;
928 static inline void new_bg(char *buf, long c)
930 new_special(buf, BG)->arg = c;
933 static inline void new_outline(char *buf, long c)
935 new_special(buf, OUTLINE)->arg = c;
938 static inline void new_offset(char *buf, long c)
940 new_special(buf, OFFSET)->arg = c;
943 static inline void new_voffset(char *buf, long c)
945 new_special(buf, VOFFSET)->arg = c;
948 static inline void new_alignr(char *buf, long c)
950 new_special(buf, ALIGNR)->arg = c;
953 // A postive offset pushes the text further left
954 static inline void new_alignc(char *buf, long c)
956 new_special(buf, ALIGNC)->arg = c;
959 static inline void new_goto(char *buf, long c)
961 new_special(buf, GOTO)->arg = c;
964 static inline void new_tab(char *buf, int a, int b)
966 struct special_t *s = new_special(buf, TAB);
972 /* quite boring functions */
974 static inline void for_each_line(char *b, void f(char *))
978 for (ps = b, pe = b; *pe; pe++) {
992 static void convert_escapes(char *buf)
994 char *p = buf, *s = buf;
1001 } else if (*s == '\\') {
1012 /* Prints anything normally printed with snprintf according to the current value
1013 * of use_spacer. Actually slightly more flexible than snprintf, as you can
1014 * safely specify the destination buffer as one of your inputs. */
1015 static int spaced_print(char *, int, const char *, int, const char *, ...)
1016 __attribute__((format(printf, 3, 6)));
1018 static int spaced_print(char *buf, int size, const char *format, int width,
1019 const char *func_name, ...) {
1027 tempbuf = malloc(size * sizeof(char));
1029 // Passes the varargs along to vsnprintf
1030 va_start(argp, func_name);
1031 vsnprintf(tempbuf, size, format, argp);
1034 switch (use_spacer) {
1036 len = snprintf(buf, size, "%s", tempbuf);
1039 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
1042 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
1045 CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
1054 /* converts from bytes to human readable format (k, M, G, T) */
1055 static void human_readable(long long num, char *buf, int size, const char *func_name)
1057 const char **suffix = suffixes;
1060 static const int WIDTH = 10, SHORT_WIDTH = 8;
1062 if (num < 1024LL && num > -1024LL) {
1064 spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num,
1067 spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix);
1072 while ((num / 1024 >= 1000LL || num / 1024 <= -1024LL) && **(suffix + 2)) {
1078 fnum = num / 1024.0;
1083 if (precision < 0) {
1087 len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name,
1088 precision, fnum, **suffix);
1090 len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision,
1093 } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
1098 enum text_object_type {
1100 #if defined(__linux__)
1102 #endif /* __linux__ */
1112 OBJ_battery_percent,
1114 #endif /* !__OpenBSD__ */
1129 OBJ_conky_build_date,
1130 OBJ_conky_build_arch,
1140 OBJ_diskiograph_read,
1141 OBJ_diskiograph_write,
1179 #if defined(__linux__)
1186 OBJ_i8k_left_fan_status,
1187 OBJ_i8k_right_fan_status,
1188 OBJ_i8k_left_fan_rpm,
1189 OBJ_i8k_right_fan_rpm,
1191 OBJ_i8k_buttons_status,
1207 OBJ_wireless_bitrate,
1209 OBJ_wireless_link_qual,
1210 OBJ_wireless_link_qual_max,
1211 OBJ_wireless_link_qual_perc,
1212 OBJ_wireless_link_bar,
1213 #endif /* __linux__ */
1254 OBJ_running_processes,
1263 OBJ_temp1, /* i2c is used instead in these */
1287 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1288 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1290 OBJ_apm_battery_time,
1291 OBJ_apm_battery_life,
1292 #endif /* __FreeBSD__ __OpenBSD__ */
1294 OBJ_obsd_sensors_temp,
1295 OBJ_obsd_sensors_fan,
1296 OBJ_obsd_sensors_volt,
1299 #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, const char *command, char *response, const char *verify)
1652 struct timeval timeout;
1654 int res, numbytes = 0;
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 int imap_check_status(char *recvbuf, struct mail_s *mail)
1680 reply = strstr(recvbuf, " (MESSAGES ");
1681 if (!reply || strlen(reply) < 2) {
1685 *strchr(reply, ')') = '\0';
1686 if (reply == NULL) {
1687 ERR("Error parsing IMAP response: %s", recvbuf);
1690 timed_thread_lock(mail->p_timed_thread);
1691 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
1693 timed_thread_unlock(mail->p_timed_thread);
1698 void imap_unseen_command(struct mail_s *mail, unsigned long old_unseen, unsigned long old_messages)
1700 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
1701 || (mail->messages > old_messages && mail->unseen > 0))) {
1703 if (system(mail->command) == -1) {
1709 void *imap_thread(void *arg)
1711 int sockfd, numbytes;
1712 char recvbuf[MAXDATASIZE];
1713 char sendbuf[MAXDATASIZE];
1714 unsigned int fail = 0;
1715 unsigned long old_unseen = ULONG_MAX;
1716 unsigned long old_messages = ULONG_MAX;
1717 struct stat stat_buf;
1718 struct hostent he, *he_res = 0;
1720 char hostbuff[2048];
1721 struct sockaddr_in their_addr; // connector's address information
1722 struct mail_s *mail = (struct mail_s *)arg;
1724 int threadfd = timed_thread_readfd(mail->p_timed_thread);
1726 #ifdef HAVE_GETHOSTBYNAME_R
1727 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1728 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
1731 #else /* HAVE_GETHOSTBYNAME_R */
1732 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1733 herror("gethostbyname");
1736 #endif /* HAVE_GETHOSTBYNAME_R */
1737 while (fail < mail->retries) {
1738 struct timeval timeout;
1743 ERR("Trying IMAP connection again for %s@%s (try %u/%u)",
1744 mail->user, mail->host, fail + 1, mail->retries);
1747 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1754 their_addr.sin_family = AF_INET;
1755 // short, network byte order
1756 their_addr.sin_port = htons(mail->port);
1757 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1758 // zero the rest of the struct
1759 memset(&(their_addr.sin_zero), '\0', 8);
1761 if (connect(sockfd, (struct sockaddr *) &their_addr,
1762 sizeof(struct sockaddr)) == -1) {
1768 timeout.tv_sec = 60; // 60 second timeout i guess
1769 timeout.tv_usec = 0;
1771 FD_SET(sockfd, &fdset);
1772 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1774 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1780 ERR("IMAP connection failed: timeout");
1784 recvbuf[numbytes] = '\0';
1785 if (strstr(recvbuf, "* OK") != recvbuf) {
1786 ERR("IMAP connection failed, probably not an IMAP server");
1790 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1791 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1792 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1793 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1794 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1795 if (imap_command(sockfd, sendbuf, recvbuf, "a1 OK")) {
1799 if (strstr(recvbuf, " IDLE ") != NULL) {
1803 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1804 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1805 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
1806 MAXDATASIZE - strlen(sendbuf) - 1);
1807 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
1812 if (imap_check_status(recvbuf, mail)) {
1816 imap_unseen_command(mail, old_unseen, old_messages);
1818 old_unseen = mail->unseen;
1819 old_messages = mail->messages;
1822 strncpy(sendbuf, "a4 SELECT ", MAXDATASIZE);
1823 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1824 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1825 if (imap_command(sockfd, sendbuf, recvbuf, "a4 OK")) {
1830 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
1831 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
1839 * RFC 2177 says we have to re-idle every 29 minutes.
1840 * We'll do it every 20 minutes to be safe.
1842 timeout.tv_sec = 1200;
1843 timeout.tv_usec = 0;
1845 FD_SET(sockfd, &fdset);
1846 FD_SET(threadfd, &fdset);
1847 res = select(MAX(sockfd + 1, threadfd + 1), &fdset, NULL, NULL, NULL);
1848 if (timed_thread_test(mail->p_timed_thread) || (res == -1 && errno == EINTR) || FD_ISSET(threadfd, &fdset)) {
1849 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1850 /* if a valid socket, close it */
1853 timed_thread_exit(mail->p_timed_thread);
1854 } else if (res > 0) {
1855 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1856 perror("recv idling");
1863 recvbuf[numbytes] = '\0';
1864 if (strlen(recvbuf) > 2) {
1865 unsigned long messages, recent;
1866 char *buf = recvbuf;
1867 buf = strstr(buf, "EXISTS");
1868 while (buf && strlen(buf) > 1 && strstr(buf + 1, "EXISTS")) {
1869 buf = strstr(buf + 1, "EXISTS");
1872 // back up until we reach '*'
1873 while (buf >= recvbuf && buf[0] != '*') {
1876 if (sscanf(buf, "* %lu EXISTS\r\n", &messages) == 1) {
1877 timed_thread_lock(mail->p_timed_thread);
1878 mail->messages = messages;
1879 timed_thread_unlock(mail->p_timed_thread);
1883 buf = strstr(buf, "RECENT");
1884 while (buf && strlen(buf) > 1 && strstr(buf + 1, "RECENT")) {
1885 buf = strstr(buf + 1, "RECENT");
1888 // back up until we reach '*'
1889 while (buf >= recvbuf && buf[0] != '*') {
1892 if (sscanf(buf, "* %lu RECENT\r\n", &recent) != 1) {
1897 * check if we got a FETCH from server, recent was
1898 * something other than 0, or we had a timeout
1901 if (recent > 0 || (buf && strstr(buf, " FETCH ")) || timeout.tv_sec == 0) {
1902 // re-check messages and unseen
1903 if (imap_command(sockfd, "DONE\r\n", recvbuf, "a5 OK")) {
1907 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1908 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1909 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
1910 MAXDATASIZE - strlen(sendbuf) - 1);
1911 if (imap_command(sockfd, sendbuf, recvbuf, "a2 OK")) {
1915 if (imap_check_status(recvbuf, mail)) {
1919 strncpy(sendbuf, "a5 IDLE\r\n", MAXDATASIZE);
1920 if (imap_command(sockfd, sendbuf, recvbuf, "+ idling")) {
1926 * check if we got a BYE from server
1929 if (buf && strstr(buf, "* BYE")) {
1930 // need to re-connect
1934 imap_unseen_command(mail, old_unseen, old_messages);
1936 old_unseen = mail->unseen;
1937 old_messages = mail->messages;
1940 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
1941 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1946 timeout.tv_sec = 60; // 60 second timeout i guess
1947 timeout.tv_usec = 0;
1949 FD_SET(sockfd, &fdset);
1950 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1952 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1958 recvbuf[numbytes] = '\0';
1959 if (strstr(recvbuf, "a3 OK") == NULL) {
1960 ERR("IMAP logout failed: %s", recvbuf);
1966 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1967 /* if a valid socket, close it */
1970 if (timed_thread_test(mail->p_timed_thread)) {
1971 timed_thread_exit(mail->p_timed_thread);
1979 void *pop3_thread(void *arg)
1981 int sockfd, numbytes;
1982 char recvbuf[MAXDATASIZE];
1983 char sendbuf[MAXDATASIZE];
1985 unsigned int fail = 0;
1986 unsigned long old_unseen = ULONG_MAX;
1987 struct stat stat_buf;
1988 struct hostent he, *he_res = 0;
1990 char hostbuff[2048];
1991 struct sockaddr_in their_addr; // connector's address information
1992 struct mail_s *mail = (struct mail_s *)arg;
1994 #ifdef HAVE_GETHOSTBYNAME_R
1995 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1996 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
1999 #else /* HAVE_GETHOSTBYNAME_R */
2000 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
2001 herror("gethostbyname");
2004 #endif /* HAVE_GETHOSTBYNAME_R */
2005 while (fail < mail->retries) {
2006 struct timeval timeout;
2011 ERR("Trying POP3 connection again for %s@%s (try %u/%u)",
2012 mail->user, mail->host, fail + 1, mail->retries);
2015 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
2022 their_addr.sin_family = AF_INET;
2023 // short, network byte order
2024 their_addr.sin_port = htons(mail->port);
2025 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
2026 // zero the rest of the struct
2027 memset(&(their_addr.sin_zero), '\0', 8);
2029 if (connect(sockfd, (struct sockaddr *) &their_addr,
2030 sizeof(struct sockaddr)) == -1) {
2036 timeout.tv_sec = 60; // 60 second timeout i guess
2037 timeout.tv_usec = 0;
2039 FD_SET(sockfd, &fdset);
2040 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2042 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2048 ERR("POP3 connection failed: timeout\n");
2052 recvbuf[numbytes] = '\0';
2053 if (strstr(recvbuf, "+OK ") != recvbuf) {
2054 ERR("POP3 connection failed, probably not a POP3 server");
2058 strncpy(sendbuf, "USER ", MAXDATASIZE);
2059 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
2060 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
2061 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2062 perror("send USER");
2066 timeout.tv_sec = 60; // 60 second timeout i guess
2067 timeout.tv_usec = 0;
2069 FD_SET(sockfd, &fdset);
2070 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2072 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2073 perror("recv USER");
2078 recvbuf[numbytes] = '\0';
2079 if (strstr(recvbuf, "+OK ") == NULL) {
2080 ERR("POP3 server login failed: %s", recvbuf);
2084 strncpy(sendbuf, "PASS ", MAXDATASIZE);
2085 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
2086 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
2087 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2088 perror("send PASS");
2092 timeout.tv_sec = 60; // 60 second timeout i guess
2093 timeout.tv_usec = 0;
2095 FD_SET(sockfd, &fdset);
2096 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2098 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2099 perror("recv PASS");
2104 recvbuf[numbytes] = '\0';
2105 if (strstr(recvbuf, "+OK ") == NULL) {
2106 ERR("POP3 server login failed: %s", recvbuf);
2110 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
2111 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2112 perror("send STAT");
2116 timeout.tv_sec = 60; // 60 second timeout i guess
2117 timeout.tv_usec = 0;
2119 FD_SET(sockfd, &fdset);
2120 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2122 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2123 perror("recv STAT");
2128 recvbuf[numbytes] = '\0';
2129 if (strstr(recvbuf, "+OK ") == NULL) {
2130 ERR("POP3 status failed: %s", recvbuf);
2134 // now we get the data
2135 reply = recvbuf + 4;
2136 if (reply == NULL) {
2137 ERR("Error parsing POP3 response: %s", recvbuf);
2141 timed_thread_lock(mail->p_timed_thread);
2142 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
2143 timed_thread_unlock(mail->p_timed_thread);
2145 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
2146 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
2147 perror("send QUIT");
2151 timeout.tv_sec = 60; // 60 second timeout i guess
2152 timeout.tv_usec = 0;
2154 FD_SET(sockfd, &fdset);
2155 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
2157 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
2158 perror("recv QUIT");
2163 recvbuf[numbytes] = '\0';
2164 if (strstr(recvbuf, "+OK") == NULL) {
2165 ERR("POP3 logout failed: %s", recvbuf);
2169 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
2171 if (system(mail->command) == -1) {
2176 old_unseen = mail->unseen;
2178 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
2179 /* if a valid socket, close it */
2182 if (timed_thread_test(mail->p_timed_thread)) {
2183 timed_thread_exit(mail->p_timed_thread);
2191 static inline void read_exec(const char *data, char *buf, const int size)
2193 FILE *fp = popen(data, "r");
2194 int length = fread(buf, 1, size, fp);
2198 if (length > 0 && buf[length - 1] == '\n') {
2199 buf[length - 1] = '\0';
2203 void *threaded_exec(void *) __attribute__((noreturn));
2205 void *threaded_exec(void *arg)
2208 struct text_object *obj = (struct text_object *)arg;
2211 p2 = obj->data.texeci.buffer;
2212 timed_thread_lock(obj->data.texeci.p_timed_thread);
2213 read_exec(obj->data.texeci.cmd, obj->data.texeci.buffer,
2216 if (*p2 == '\001') {
2221 timed_thread_unlock(obj->data.texeci.p_timed_thread);
2222 if (timed_thread_test(obj->data.texeci.p_timed_thread)) {
2223 timed_thread_exit(obj->data.texeci.p_timed_thread);
2229 static struct text_object *new_text_object_internal(void)
2231 struct text_object *obj = malloc(sizeof(struct text_object));
2232 memset(obj, 0, sizeof(struct text_object));
2237 * call with full == 0 when freeing after 'internal' evaluation of objects
2239 static void free_text_objects(struct text_object_list *text_object_list, char full)
2242 struct text_object *obj;
2244 if (text_object_list == NULL) {
2248 for (i = 0; i < text_object_list->text_object_count; i++) {
2249 obj = &text_object_list->text_objects[i];
2250 switch (obj->type) {
2259 close(obj->data.sysfs.fd);
2261 #endif /* !__OpenBSD__ */
2267 free(obj->data.tztime.tz);
2268 free(obj->data.tztime.fmt);
2271 free(obj->data.mboxscan.args);
2272 free(obj->data.mboxscan.output);
2276 free(obj->data.local_mail.box);
2281 case OBJ_imap_unseen:
2282 if (!obj->global_mode) {
2283 free(obj->data.mail);
2286 case OBJ_imap_messages:
2287 if (!obj->global_mode) {
2288 free(obj->data.mail);
2294 case OBJ_pop3_unseen:
2295 if (!obj->global_mode) {
2296 free(obj->data.mail);
2300 if (!obj->global_mode) {
2301 free(obj->data.mail);
2305 case OBJ_if_existing:
2306 case OBJ_if_mounted:
2307 case OBJ_if_running:
2308 free(obj->data.ifblock.s);
2309 free(obj->data.ifblock.str);
2312 free(obj->data.tail.logfile);
2313 free(obj->data.tail.buffer);
2325 case OBJ_iconv_start:
2330 case OBJ_disk_protect:
2331 free(objs[i].data.s);
2334 free(objs[i].data.ifblock.s);
2335 free(objs[i].data.ifblock.str);
2338 free(objs[i].data.ifblock.s);
2339 free(objs[i].data.ifblock.str);
2342 if (info.gw_info.iface) {
2343 free(info.gw_info.iface);
2344 info.gw_info.iface = 0;
2346 if (info.gw_info.ip) {
2347 free(info.gw_info.ip);
2348 info.gw_info.ip = 0;
2351 case OBJ_ioscheduler:
2353 free(objs[i].data.s);
2357 case OBJ_xmms2_artist:
2358 if (info.xmms2.artist) {
2359 free(info.xmms2.artist);
2360 info.xmms2.artist = 0;
2363 case OBJ_xmms2_album:
2364 if (info.xmms2.album) {
2365 free(info.xmms2.album);
2366 info.xmms2.album = 0;
2369 case OBJ_xmms2_title:
2370 if (info.xmms2.title) {
2371 free(info.xmms2.title);
2372 info.xmms2.title = 0;
2375 case OBJ_xmms2_genre:
2376 if (info.xmms2.genre) {
2377 free(info.xmms2.genre);
2378 info.xmms2.genre = 0;
2381 case OBJ_xmms2_comment:
2382 if (info.xmms2.comment) {
2383 free(info.xmms2.comment);
2384 info.xmms2.comment = 0;
2388 if (info.xmms2.url) {
2389 free(info.xmms2.url);
2393 case OBJ_xmms2_date:
2394 if (info.xmms2.date) {
2395 free(info.xmms2.date);
2396 info.xmms2.date = 0;
2399 case OBJ_xmms2_status:
2400 if (info.xmms2.status) {
2401 free(info.xmms2.status);
2402 info.xmms2.status = 0;
2405 case OBJ_xmms2_playlist:
2406 if (info.xmms2.playlist) {
2407 free(info.xmms2.playlist);
2408 info.xmms2.playlist = 0;
2411 case OBJ_xmms2_smart:
2412 if (info.xmms2.artist) {
2413 free(info.xmms2.artist);
2414 info.xmms2.artist = 0;
2416 if (info.xmms2.title) {
2417 free(info.xmms2.title);
2418 info.xmms2.title = 0;
2420 if (info.xmms2.url) {
2421 free(info.xmms2.url);
2427 case OBJ_bmpx_title:
2428 case OBJ_bmpx_artist:
2429 case OBJ_bmpx_album:
2430 case OBJ_bmpx_track:
2432 case OBJ_bmpx_bitrate:
2441 free(obj->data.rss.uri);
2442 free(obj->data.rss.action);
2451 case OBJ_battery_time:
2454 #endif /* !__OpenBSD__ */
2458 case OBJ_execigraph:
2459 free(obj->data.execi.cmd);
2460 free(obj->data.execi.buffer);
2463 free(obj->data.texeci.cmd);
2464 free(obj->data.texeci.buffer);
2466 case OBJ_nameserver:
2471 if (info.first_process) {
2472 free_all_processes();
2473 info.first_process = NULL;
2478 free(obj->data.hddtemp.dev);
2479 free(obj->data.hddtemp.addr);
2480 free(obj->data.hddtemp.temp);
2483 case OBJ_entropy_avail:
2484 case OBJ_entropy_poolsize:
2485 case OBJ_entropy_bar:
2487 case OBJ_user_names:
2488 if (info.users.names) {
2489 free(info.users.names);
2490 info.users.names = 0;
2493 case OBJ_user_terms:
2494 if (info.users.terms) {
2495 free(info.users.terms);
2496 info.users.terms = 0;
2499 case OBJ_user_times:
2500 if (info.users.times) {
2501 free(info.users.times);
2502 info.users.times = 0;
2507 case OBJ_smapi_bat_perc:
2508 case OBJ_smapi_bat_temp:
2509 case OBJ_smapi_bat_power:
2512 case OBJ_if_smapi_bat_installed:
2513 free(obj->data.ifblock.s);
2514 free(obj->data.ifblock.str);
2523 case OBJ_mpd_artist:
2525 case OBJ_mpd_random:
2526 case OBJ_mpd_repeat:
2528 case OBJ_mpd_bitrate:
2529 case OBJ_mpd_status:
2532 case OBJ_mpd_password:
2534 case OBJ_mpd_elapsed:
2535 case OBJ_mpd_length:
2539 case OBJ_mpd_percent:
2541 case OBJ_if_mpd_playing:
2543 free_mpd_vars(&info.mpd);
2551 case OBJ_moc_artist:
2554 case OBJ_moc_totaltime:
2555 case OBJ_moc_timeleft:
2556 case OBJ_moc_curtime:
2557 case OBJ_moc_bitrate:
2559 free_moc(&info.moc);
2563 free(obj->data.scroll.text);
2567 if (full) {} // disable warning when MPD !defined
2568 free(text_object_list->text_objects);
2569 text_object_list->text_objects = NULL;
2570 text_object_list->text_object_count = 0;
2573 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2578 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2579 *a = mixer_init(buf1);
2580 scan_bar(arg + n, w, h);
2582 *a = mixer_init(NULL);
2583 scan_bar(arg, w, h);
2587 /* strip a leading /dev/ if any */
2588 #define DEV_NAME(x) x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 \
2591 /* construct_text_object() creates a new text_object */
2592 static struct text_object *construct_text_object(const char *s,
2593 const char *arg, unsigned int object_count,
2594 struct text_object *text_objects, long line, char allow_threaded)
2596 // struct text_object *obj = new_text_object();
2597 struct text_object *obj = new_text_object_internal();
2601 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
2602 obj->type = OBJ_##a; need_mask |= (1 << n); {
2603 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
2604 obj->type = OBJ_##a; need_mask |= (1 << n); {
2605 #define END } } else
2609 obj->type = OBJ_color;
2610 obj->data.l = get_x11_color(s);
2614 OBJ(freq, INFO_FREQ)
2617 obj->data.i = open_acpi_temperature(arg);
2618 END OBJ(acpitempf, 0)
2619 obj->data.i = open_acpi_temperature(arg);
2620 END OBJ(acpiacadapter, 0)
2621 END OBJ(freq, INFO_FREQ)
2622 #endif /* !__OpenBSD__ */
2624 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2625 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2626 obj->data.cpu_index = 1;
2627 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
2628 "Displaying the clock for CPU 1."); */
2630 obj->data.cpu_index = atoi(&arg[0]);
2633 END OBJ(freq_g, INFO_FREQ)
2635 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2636 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2637 obj->data.cpu_index = 1;
2638 /* ERR("freq_g: Invalid CPU number or you don't have that many "
2639 "CPUs! Displaying the clock for CPU 1."); */
2641 obj->data.cpu_index = atoi(&arg[0]);
2644 #if defined(__linux__)
2645 END OBJ(voltage_mv, 0)
2647 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2648 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2649 obj->data.cpu_index = 1;
2650 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
2651 "CPUs! Displaying voltage for CPU 1."); */
2653 obj->data.cpu_index = atoi(&arg[0]);
2656 END OBJ(voltage_v, 0)
2658 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2659 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2660 obj->data.cpu_index = 1;
2661 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
2662 "CPUs! Displaying voltage for CPU 1."); */
2664 obj->data.cpu_index = atoi(&arg[0]);
2669 END OBJ(wireless_essid, INFO_NET)
2671 obj->data.net = get_net_stat(arg);
2673 CRIT_ERR("wireless_essid: needs an argument");
2675 END OBJ(wireless_mode, INFO_NET)
2677 obj->data.net = get_net_stat(arg);
2679 CRIT_ERR("wireless_mode: needs an argument");
2681 END OBJ(wireless_bitrate, INFO_NET)
2683 obj->data.net = get_net_stat(arg);
2685 CRIT_ERR("wireless_bitrate: needs an argument");
2687 END OBJ(wireless_ap, INFO_NET)
2689 obj->data.net = get_net_stat(arg);
2691 CRIT_ERR("wireless_ap: needs an argument");
2693 END OBJ(wireless_link_qual, INFO_NET)
2695 obj->data.net = get_net_stat(arg);
2697 CRIT_ERR("wireless_link_qual: needs an argument");
2699 END OBJ(wireless_link_qual_max, INFO_NET)
2701 obj->data.net = get_net_stat(arg);
2703 CRIT_ERR("wireless_link_qual_max: needs an argument");
2705 END OBJ(wireless_link_qual_perc, INFO_NET)
2707 obj->data.net = get_net_stat(arg);
2709 CRIT_ERR("wireless_link_qual_perc: needs an argument");
2711 END OBJ(wireless_link_bar, INFO_NET)
2713 arg = scan_bar(arg, &obj->a, &obj->b);
2714 obj->data.net = get_net_stat(arg);
2716 CRIT_ERR("wireless_link_bar: needs an argument");
2718 #endif /* HAVE_IWLIB */
2720 #endif /* __linux__ */
2721 END OBJ(freq_dyn, 0)
2722 END OBJ(freq_dyn_g, 0)
2730 sscanf(arg, "%63s", bat);
2732 strcpy(bat, "BAT0");
2734 obj->data.s = strndup(bat, text_buffer_size);
2735 END OBJ(battery_time, 0)
2739 sscanf(arg, "%63s", bat);
2741 strcpy(bat, "BAT0");
2743 obj->data.s = strndup(bat, text_buffer_size);
2744 END OBJ(battery_percent, 0)
2748 sscanf(arg, "%63s", bat);
2750 strcpy(bat, "BAT0");
2752 obj->data.s = strndup(bat, text_buffer_size);
2753 END OBJ(battery_bar, 0)
2757 arg = scan_bar(arg, &obj->a, &obj->b);
2758 sscanf(arg, "%63s", bat);
2760 strcpy(bat, "BAT0");
2762 obj->data.s = strndup(bat, text_buffer_size);
2763 #endif /* !__OpenBSD__ */
2765 #if defined(__linux__)
2766 END OBJ(disk_protect, 0)
2768 obj->data.s = strndup(DEV_NAME(arg), text_buffer_size);
2770 CRIT_ERR("disk_protect needs an argument");
2771 END OBJ(i8k_version, INFO_I8K)
2772 END OBJ(i8k_bios, INFO_I8K)
2773 END OBJ(i8k_serial, INFO_I8K)
2774 END OBJ(i8k_cpu_temp, INFO_I8K)
2775 END OBJ(i8k_cpu_tempf, INFO_I8K)
2776 END OBJ(i8k_left_fan_status, INFO_I8K)
2777 END OBJ(i8k_right_fan_status, INFO_I8K)
2778 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2779 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2780 END OBJ(i8k_ac_status, INFO_I8K)
2781 END OBJ(i8k_buttons_status, INFO_I8K)
2783 END OBJ(ibm_temps, 0)
2785 CRIT_ERR("ibm_temps: needs an argument");
2787 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
2788 obj->data.sensor = 0;
2789 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
2790 "Using 0 (CPU temp sensor).");
2792 obj->data.sensor = atoi(&arg[0]);
2793 END OBJ(ibm_volume, 0)
2794 END OBJ(ibm_brightness, 0)
2796 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2797 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2800 ERR("if_up needs an argument");
2801 obj->data.ifblock.s = 0;
2803 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2804 blockstart[blockdepth] = object_count;
2805 obj->data.ifblock.pos = object_count + 2;
2808 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2809 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2811 blockstart[blockdepth] = object_count;
2812 obj->data.ifblock.pos = object_count + 2;
2814 END OBJ(ioscheduler, 0)
2816 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
2819 obj->data.s = strndup(DEV_NAME(arg), text_buffer_size);
2820 END OBJ(laptop_mode, 0)
2821 END OBJ(pb_battery, 0)
2822 if (arg && strcmp(arg, "status") == EQUAL) {
2823 obj->data.i = PB_BATT_STATUS;
2824 } else if (arg && strcmp(arg, "percent") == EQUAL) {
2825 obj->data.i = PB_BATT_PERCENT;
2826 } else if (arg && strcmp(arg, "time") == EQUAL) {
2827 obj->data.i = PB_BATT_TIME;
2829 ERR("pb_battery: needs one argument: status, percent or time");
2834 #endif /* __linux__ */
2835 #if defined(__OpenBSD__)
2836 END OBJ(obsd_sensors_temp, 0)
2838 CRIT_ERR("obsd_sensors_temp: needs an argument");
2840 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2841 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2842 obj->data.sensor = 0;
2843 ERR("Invalid temperature sensor number!");
2845 obj->data.sensor = atoi(&arg[0]);
2846 END OBJ(obsd_sensors_fan, 0)
2848 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
2851 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2852 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2853 obj->data.sensor = 0;
2854 ERR("Invalid fan sensor number!");
2856 obj->data.sensor = atoi(&arg[0]);
2857 END OBJ(obsd_sensors_volt, 0)
2859 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
2862 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2863 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2864 obj->data.sensor = 0;
2865 ERR("Invalid voltage sensor number!");
2867 obj->data.sensor = atoi(&arg[0]);
2868 END OBJ(obsd_vendor, 0)
2869 END OBJ(obsd_product, 0)
2870 #endif /* __OpenBSD__ */
2871 END OBJ(buffers, INFO_BUFFERS)
2872 END OBJ(cached, INFO_BUFFERS)
2873 END OBJ(cpu, INFO_CPU)
2875 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
2876 obj->data.cpu_index = atoi(&arg[3]);
2879 obj->data.cpu_index = 0;
2882 obj->data.cpu_index = 0;
2884 END OBJ(cpubar, INFO_CPU)
2886 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
2887 obj->data.cpu_index = atoi(&arg[3]);
2890 obj->data.cpu_index = 0;
2892 scan_bar(arg, &obj->a, &obj->b);
2894 scan_bar(arg, &obj->a, &obj->b);
2895 obj->data.cpu_index = 0;
2897 END OBJ(cpugraph, INFO_CPU)
2898 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2899 &obj->e, &obj->showaslog);
2902 if (strncmp(buf, "cpu", 3) == EQUAL && isdigit(buf[3])) {
2903 obj->data.cpu_index = atoi(&buf[3]);
2905 obj->data.cpu_index = 0;
2909 END OBJ(loadgraph, INFO_LOADAVG)
2910 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2911 &obj->e, &obj->showaslog);
2915 r = sscanf(arg, "%d", &a);
2917 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2920 #if defined(__linux__)
2921 END OBJ(diskio, INFO_DISKIO)
2923 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2925 obj->data.diskio = NULL;
2927 END OBJ(diskio_read, INFO_DISKIO)
2929 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2931 obj->data.diskio = NULL;
2933 END OBJ(diskio_write, INFO_DISKIO)
2935 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2937 obj->data.diskio = NULL;
2939 END OBJ(diskiograph, INFO_DISKIO)
2940 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2941 &obj->e, &obj->showaslog);
2944 obj->data.diskio = prepare_diskio_stat(buf);
2947 obj->data.diskio = NULL;
2949 END OBJ(diskiograph_read, INFO_DISKIO)
2950 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2951 &obj->e, &obj->showaslog);
2954 obj->data.diskio = prepare_diskio_stat(buf);
2957 obj->data.diskio = NULL;
2959 END OBJ(diskiograph_write, INFO_DISKIO)
2960 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2961 &obj->e, &obj->showaslog);
2964 obj->data.diskio = prepare_diskio_stat(buf);
2967 obj->data.diskio = NULL;
2972 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2975 obj->data.l = color0;
2977 obj->data.l = color1;
2979 obj->data.l = color2;
2981 obj->data.l = color3;
2983 obj->data.l = color4;
2985 obj->data.l = color5;
2987 obj->data.l = color6;
2989 obj->data.l = color7;
2991 obj->data.l = color8;
2993 obj->data.l = color9;
2995 obj->data.s = scan_font(arg);
2996 END OBJ(conky_version, 0)
2997 END OBJ(conky_build_date, 0)
2998 END OBJ(conky_build_arch, 0)
2999 END OBJ(downspeed, INFO_NET)
3001 obj->data.net = get_net_stat(arg);
3003 CRIT_ERR("downspeed needs argument");
3005 END OBJ(downspeedf, INFO_NET)
3007 obj->data.net = get_net_stat(arg);
3009 CRIT_ERR("downspeedf needs argument");
3011 END OBJ(downspeedgraph, INFO_NET)
3012 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3013 &obj->e, &obj->showaslog);
3015 // default to DEFAULTNETDEV
3016 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
3017 obj->data.net = get_net_stat(buf);
3021 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos =
3023 blockstart[blockdepth - 1] = object_count;
3024 obj->data.ifblock.pos = object_count + 2;
3026 ERR("$else: no matching $if_*");
3031 text_objects[blockstart[blockdepth]].data.ifblock.pos =
3034 ERR("$endif: no matching $if_*");
3037 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
3040 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
3042 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
3044 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
3045 END OBJ(execgraph, 0)
3046 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
3047 END OBJ(execibar, 0)
3050 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3053 ERR("${execibar <interval> command}");
3054 obj->type = OBJ_text;
3055 snprintf(buf, 256, "${%s}", s);
3056 obj->data.s = strndup(buf, text_buffer_size);
3058 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3060 END OBJ(execigraph, 0)
3063 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3066 ERR("${execigraph <interval> command}");
3067 obj->type = OBJ_text;
3068 snprintf(buf, 256, "${%s}", s);
3069 obj->data.s = strndup(buf, text_buffer_size);
3071 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3076 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3079 ERR("${execi <interval> command}");
3080 obj->type = OBJ_text;
3081 snprintf(buf, 256, "${%s}", s);
3082 obj->data.s = strndup(buf, text_buffer_size);
3084 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3085 obj->data.execi.buffer = malloc(text_buffer_size);
3090 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
3093 ERR("${execi <interval> command}");
3094 obj->type = OBJ_text;
3095 snprintf(buf, 256, "${%s}", s);
3096 obj->data.s = strndup(buf, text_buffer_size);
3098 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
3099 obj->data.execi.buffer = malloc(text_buffer_size);
3101 END OBJ_THREAD(texeci, 0)
3104 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
3107 ERR("${texeci <interval> command}");
3108 obj->type = OBJ_text;
3109 snprintf(buf, 256, "${%s}", s);
3110 obj->data.s = strndup(buf, text_buffer_size);
3112 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
3113 obj->data.texeci.buffer = malloc(text_buffer_size);
3115 obj->data.texeci.p_timed_thread = NULL;
3116 END OBJ(pre_exec, 0)
3117 obj->type = OBJ_text;
3121 read_exec(arg, buf, sizeof(buf));
3122 obj->data.s = strndup(buf, text_buffer_size);
3124 obj->data.s = strndup("", text_buffer_size);
3127 END OBJ(fs_bar, INFO_FS)
3128 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
3130 while (isspace(*arg)) {
3139 obj->data.fsbar.fs = prepare_fs_stat(arg);
3140 END OBJ(fs_bar_free, INFO_FS)
3141 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
3143 while (isspace(*arg)) {
3153 obj->data.fsbar.fs = prepare_fs_stat(arg);
3154 END OBJ(fs_free, INFO_FS)
3158 obj->data.fs = prepare_fs_stat(arg);
3159 END OBJ(fs_used_perc, INFO_FS)
3163 obj->data.fs = prepare_fs_stat(arg);
3164 END OBJ(fs_free_perc, INFO_FS)
3168 obj->data.fs = prepare_fs_stat(arg);
3169 END OBJ(fs_size, INFO_FS)
3173 obj->data.fs = prepare_fs_stat(arg);
3174 END OBJ(fs_type, INFO_FS)
3178 obj->data.fs = prepare_fs_stat(arg);
3179 END OBJ(fs_used, INFO_FS)
3183 obj->data.fs = prepare_fs_stat(arg);
3185 obj->data.i = arg ? atoi(arg) : 1;
3186 END OBJ(nameserver, INFO_DNS)
3187 obj->data.i = arg ? atoi(arg) : 0;
3189 obj->data.i = arg ? atoi(arg) : 1;
3191 obj->data.i = arg ? atoi(arg) : 1;
3195 ERR("goto needs arguments");
3196 obj->type = OBJ_text;
3197 obj->data.s = strndup("${goto}", text_buffer_size);
3201 obj->data.i = atoi(arg);
3207 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3208 sscanf(arg, "%d", &b);
3214 obj->data.pair.a = a;
3215 obj->data.pair.b = b;
3218 END OBJ(i2c, INFO_SYSFS)
3219 char buf1[64], buf2[64];
3223 ERR("i2c needs arguments");
3224 obj->type = OBJ_text;
3225 // obj->data.s = strndup("${i2c}", text_buffer_size);
3229 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3230 /* if scanf couldn't read three values, read type and num and use
3232 sscanf(arg, "%63s %d", buf2, &n);
3233 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
3234 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3235 strncpy(obj->data.sysfs.type, buf2, 63);
3237 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
3238 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3239 strncpy(obj->data.sysfs.type, buf2, 63);
3242 END OBJ(platform, INFO_SYSFS)
3243 char buf1[64], buf2[64];
3247 ERR("platform needs arguments");
3248 obj->type = OBJ_text;
3252 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3253 /* if scanf couldn't read three values, read type and num and use
3255 sscanf(arg, "%63s %d", buf2, &n);
3256 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
3257 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3258 strncpy(obj->data.sysfs.type, buf2, 63);
3260 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
3261 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3262 strncpy(obj->data.sysfs.type, buf2, 63);
3265 END OBJ(hwmon, INFO_SYSFS)
3266 char buf1[64], buf2[64];
3270 ERR("hwmon needs argumanets");
3271 obj->type = OBJ_text;
3275 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3276 /* if scanf couldn't read three values, read type and num and use
3278 sscanf(arg, "%63s %d", buf2, &n);
3279 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
3280 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3281 strncpy(obj->data.sysfs.type, buf2, 63);
3283 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
3284 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3285 strncpy(obj->data.sysfs.type, buf2, 63);
3287 #endif /* !__OpenBSD__ */
3289 END OBJ(top, INFO_TOP)
3294 ERR("top needs arguments");
3295 obj->type = OBJ_text;
3296 // obj->data.s = strndup("${top}", text_buffer_size);
3299 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3300 if (strcmp(buf, "name") == EQUAL) {
3301 obj->data.top.type = TOP_NAME;
3302 } else if (strcmp(buf, "cpu") == EQUAL) {
3303 obj->data.top.type = TOP_CPU;
3304 } else if (strcmp(buf, "pid") == EQUAL) {
3305 obj->data.top.type = TOP_PID;
3306 } else if (strcmp(buf, "mem") == EQUAL) {
3307 obj->data.top.type = TOP_MEM;
3308 } else if (strcmp(buf, "time") == EQUAL) {
3309 obj->data.top.type = TOP_TIME;
3310 } else if (strcmp(buf, "mem_res") == EQUAL) {
3311 obj->data.top.type = TOP_MEM_RES;
3312 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
3313 obj->data.top.type = TOP_MEM_VSIZE;
3315 ERR("invalid arg for top");
3318 if (n < 1 || n > 10) {
3319 CRIT_ERR("invalid arg for top");
3322 obj->data.top.num = n - 1;
3326 ERR("invalid args given for top");
3329 END OBJ(top_mem, INFO_TOP)
3334 ERR("top_mem needs arguments");
3335 obj->type = OBJ_text;
3336 obj->data.s = strndup("${top_mem}", text_buffer_size);
3339 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3340 if (strcmp(buf, "name") == EQUAL) {
3341 obj->data.top.type = TOP_NAME;
3342 } else if (strcmp(buf, "cpu") == EQUAL) {
3343 obj->data.top.type = TOP_CPU;
3344 } else if (strcmp(buf, "pid") == EQUAL) {
3345 obj->data.top.type = TOP_PID;
3346 } else if (strcmp(buf, "mem") == EQUAL) {
3347 obj->data.top.type = TOP_MEM;
3348 } else if (strcmp(buf, "time") == EQUAL) {
3349 obj->data.top.type = TOP_TIME;
3350 } else if (strcmp(buf, "mem_res") == EQUAL) {
3351 obj->data.top.type = TOP_MEM_RES;
3352 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
3353 obj->data.top.type = TOP_MEM_VSIZE;
3355 ERR("invalid arg for top");
3358 if (n < 1 || n > 10) {
3359 CRIT_ERR("invalid arg for top");
3362 obj->data.top.num = n - 1;
3366 ERR("invalid args given for top");
3369 END OBJ(addr, INFO_NET)
3371 obj->data.net = get_net_stat(arg);
3373 CRIT_ERR("addr needs argument");
3375 #if defined(__linux__)
3376 END OBJ(addrs, INFO_NET)
3378 obj->data.net = get_net_stat(arg);
3380 CRIT_ERR("addrs needs argument");
3382 #endif /* __linux__ */
3389 ERR("tail needs arguments");
3390 obj->type = OBJ_text;
3391 obj->data.s = strndup("${tail}", text_buffer_size);
3394 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3395 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3396 CRIT_ERR("invalid arg for tail, number of lines must be "
3397 "between 1 and %i", MAX_TAIL_LINES);
3403 obj->data.tail.fd = -1;
3405 if (stat(buf, &st) == 0) {
3406 if (S_ISFIFO(st.st_mode)) {
3407 fd = open(buf, O_RDONLY | O_NONBLOCK);
3410 CRIT_ERR("tail logfile does not exist, or you do "
3411 "not have correct permissions");
3414 obj->data.tail.fd = fd;
3416 fp = fopen(buf, "r");
3420 if (fp || obj->data.tail.fd != -1) {
3421 obj->data.tail.logfile = malloc(text_buffer_size);
3422 strcpy(obj->data.tail.logfile, buf);
3423 obj->data.tail.wantedlines = n1;
3424 obj->data.tail.interval = update_interval * 2;
3426 if (obj->data.tail.fd == -1) {
3431 CRIT_ERR("tail logfile does not exist, or you do not have "
3432 "correct permissions");
3435 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3436 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3437 CRIT_ERR("invalid arg for tail, number of lines must be "
3438 "between 1 and %i", MAX_TAIL_LINES);
3440 } else if (n2 < 1 || n2 < update_interval) {
3441 CRIT_ERR("invalid arg for tail, interval must be greater than "
3442 "0 and "PACKAGE_NAME"'s interval");
3448 obj->data.tail.fd = -1;
3450 if (stat(buf, &st) == 0) {
3451 if (S_ISFIFO(st.st_mode)) {
3452 fd = open(buf, O_RDONLY | O_NONBLOCK);
3455 CRIT_ERR("tail logfile does not exist, or you do "
3456 "not have correct permissions");
3459 obj->data.tail.fd = fd;
3461 fp = fopen(buf, "r");
3465 if (fp || obj->data.tail.fd != -1) {
3466 obj->data.tail.logfile = malloc(text_buffer_size);
3467 strcpy(obj->data.tail.logfile, buf);
3468 obj->data.tail.wantedlines = n1;
3469 obj->data.tail.interval = n2;
3471 if (obj->data.tail.fd == -1) {
3476 CRIT_ERR("tail logfile does not exist, or you do not have "
3477 "correct permissions");
3481 ERR("invalid args given for tail");
3484 /* asumming all else worked */
3485 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3491 ERR("head needs arguments");
3492 obj->type = OBJ_text;
3493 obj->data.s = strndup("${head}", text_buffer_size);
3496 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3497 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3498 CRIT_ERR("invalid arg for head, number of lines must be "
3499 "between 1 and %i", MAX_TAIL_LINES);
3504 fp = fopen(buf, "r");
3506 obj->data.tail.logfile = malloc(text_buffer_size);
3507 strcpy(obj->data.tail.logfile, buf);
3508 obj->data.tail.wantedlines = n1;
3509 obj->data.tail.interval = update_interval * 2;
3513 CRIT_ERR("head logfile does not exist, or you do not have "
3514 "correct permissions");
3517 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3518 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3519 CRIT_ERR("invalid arg for head, number of lines must be "
3520 "between 1 and %i", MAX_TAIL_LINES);
3522 } else if (n2 < 1 || n2 < update_interval) {
3523 CRIT_ERR("invalid arg for head, interval must be greater than "
3524 "0 and "PACKAGE_NAME"'s interval");
3529 fp = fopen(buf, "r");
3531 obj->data.tail.logfile = malloc(text_buffer_size);
3532 strcpy(obj->data.tail.logfile, buf);
3533 obj->data.tail.wantedlines = n1;
3534 obj->data.tail.interval = n2;
3538 CRIT_ERR("head logfile does not exist, or you do not have "
3539 "correct permissions");
3543 ERR("invalid args given for head");
3546 /* asumming all else worked */
3547 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3550 obj->data.s = strdup(arg);
3552 CRIT_ERR("lines needs a argument");
3556 obj->data.s = strdup(arg);
3558 CRIT_ERR("words needs a argument");
3560 END OBJ(loadavg, INFO_LOADAVG)
3561 int a = 1, b = 2, c = 3, r = 3;
3564 r = sscanf(arg, "%d %d %d", &a, &b, &c);
3565 if (r >= 3 && (c < 1 || c > 3)) {
3568 if (r >= 2 && (b < 1 || b > 3)) {
3571 if (r >= 1 && (a < 1 || a > 3)) {
3575 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
3576 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
3577 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
3578 END OBJ(if_empty, 0)
3579 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3580 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3583 ERR("if_empty needs an argument");
3584 obj->data.ifblock.s = 0;
3586 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3588 blockstart[blockdepth] = object_count;
3589 obj->data.ifblock.pos = object_count + 2;
3591 END OBJ(if_existing, 0)
3592 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3593 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3596 ERR("if_existing needs an argument or two");
3597 obj->data.ifblock.s = NULL;
3598 obj->data.ifblock.str = NULL;
3600 char buf1[256], buf2[256];
3601 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3604 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
3605 obj->data.ifblock.str = NULL;
3607 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
3608 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
3611 blockstart[blockdepth] = object_count;
3612 obj->data.ifblock.pos = object_count + 2;
3614 END OBJ(if_mounted, 0)
3615 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3616 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3619 ERR("if_mounted needs an argument");
3620 obj->data.ifblock.s = 0;
3622 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3624 blockstart[blockdepth] = object_count;
3625 obj->data.ifblock.pos = object_count + 2;
3627 END OBJ(if_running, 0)
3628 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3629 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3634 snprintf(buf, 256, "pidof %s >/dev/null", arg);
3635 obj->data.ifblock.s = strndup(buf, text_buffer_size);
3637 ERR("if_running needs an argument");
3638 obj->data.ifblock.s = 0;
3640 blockstart[blockdepth] = object_count;
3641 obj->data.ifblock.pos = object_count + 2;
3647 char box[256], dst[256];
3651 /* Kapil: Changed from MAIL_FILE to
3652 current_mail_spool since the latter
3653 is a copy of the former if undefined
3654 but the latter should take precedence
3656 strncpy(box, current_mail_spool, sizeof(box));
3658 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3660 strncpy(box, arg, sizeof(box));
3664 variable_substitute(box, dst, sizeof(dst));
3665 obj->data.local_mail.box = strndup(dst, text_buffer_size);
3666 obj->data.local_mail.interval = n1;
3667 END OBJ(mboxscan, 0)
3668 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
3669 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
3670 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
3671 obj->data.mboxscan.output[0] = 1;
3672 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
3673 END OBJ(mem, INFO_MEM)
3674 END OBJ(memeasyfree, INFO_MEM)
3675 END OBJ(memfree, INFO_MEM)
3676 END OBJ(memmax, INFO_MEM)
3677 END OBJ(memperc, INFO_MEM)
3678 END OBJ(membar, INFO_MEM)
3679 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3680 END OBJ(memgraph, INFO_MEM)
3681 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3682 &obj->e, &obj->showaslog);
3687 END OBJ(mixer, INFO_MIXER)
3688 obj->data.l = mixer_init(arg);
3689 END OBJ(mixerl, INFO_MIXER)
3690 obj->data.l = mixer_init(arg);
3691 END OBJ(mixerr, INFO_MIXER)
3692 obj->data.l = mixer_init(arg);
3693 END OBJ(mixerbar, INFO_MIXER)
3694 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3695 &obj->data.mixerbar.h);
3696 END OBJ(mixerlbar, INFO_MIXER)
3697 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3698 &obj->data.mixerbar.h);
3699 END OBJ(mixerrbar, INFO_MIXER)
3700 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3701 &obj->data.mixerbar.h);
3703 END OBJ(monitor, INFO_X11)
3704 END OBJ(monitor_number, INFO_X11)
3706 END OBJ(new_mails, 0)
3708 char box[256], dst[256];
3712 /* Kapil: Changed from MAIL_FILE to
3713 current_mail_spool since the latter
3714 is a copy of the former if undefined
3715 but the latter should take precedence
3717 strncpy(box, current_mail_spool, sizeof(box));
3719 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3721 strncpy(box, arg, sizeof(box));
3725 variable_substitute(box, dst, sizeof(dst));
3726 obj->data.local_mail.box = strndup(dst, text_buffer_size);
3727 obj->data.local_mail.interval = n1;
3728 END OBJ(nodename, 0)
3729 END OBJ(processes, INFO_PROCS)
3730 END OBJ(running_processes, INFO_RUN_PROCS)
3731 END OBJ(shadecolor, 0)
3733 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
3735 END OBJ(outlinecolor, 0)
3737 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
3739 END OBJ(stippled_hr, 0)
3741 int a = stippled_borders, b = 1;
3744 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3745 sscanf(arg, "%d", &b);
3751 obj->data.pair.a = a;
3752 obj->data.pair.b = b;
3754 END OBJ(swap, INFO_MEM)
3755 END OBJ(swapmax, INFO_MEM)
3756 END OBJ(swapperc, INFO_MEM)
3757 END OBJ(swapbar, INFO_MEM)
3758 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3761 END OBJ(temp1, INFO_SYSFS)
3762 obj->type = OBJ_i2c;
3763 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
3764 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3765 END OBJ(temp2, INFO_SYSFS)
3766 obj->type = OBJ_i2c;
3767 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
3768 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3771 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
3773 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
3775 char buf1[256], buf2[256], *fmt, *tz;
3779 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3789 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
3790 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
3792 END OBJ(iconv_start, 0)
3793 if (iconv_converting) {
3794 CRIT_ERR("You must stop your last iconv conversion before "
3795 "starting another");
3798 char iconv_from[CODEPAGE_LENGTH];
3799 char iconv_to[CODEPAGE_LENGTH];
3801 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
3802 CRIT_ERR("Invalid arguments for iconv_start");
3806 new_iconv = iconv_open(iconv_to, iconv_from);
3807 if (new_iconv == (iconv_t) (-1)) {
3808 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
3810 obj->a = register_iconv(&new_iconv);
3811 iconv_converting = 1;
3815 CRIT_ERR("Iconv requires arguments");
3817 END OBJ(iconv_stop, 0)
3818 iconv_converting = 0;
3821 END OBJ(totaldown, INFO_NET)
3823 obj->data.net = get_net_stat(arg);
3825 CRIT_ERR("totaldown needs argument");
3827 END OBJ(totalup, INFO_NET)
3828 obj->data.net = get_net_stat(arg);
3830 obj->data.net = get_net_stat(arg);
3832 CRIT_ERR("totalup needs argument");
3836 obj->data.i = arg ? atoi(arg) : 0;
3838 obj->data.i = arg ? atoi(arg) : 0;
3839 END OBJ(upspeed, INFO_NET)
3841 obj->data.net = get_net_stat(arg);
3843 CRIT_ERR("upspeed needs argument");
3845 END OBJ(upspeedf, INFO_NET)
3847 obj->data.net = get_net_stat(arg);
3849 CRIT_ERR("upspeedf needs argument");
3852 END OBJ(upspeedgraph, INFO_NET)
3853 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3854 &obj->e, &obj->showaslog);
3856 // default to DEFAULTNETDEV
3857 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
3858 obj->data.net = get_net_stat(buf);
3860 END OBJ(uptime_short, INFO_UPTIME)
3861 END OBJ(uptime, INFO_UPTIME)
3862 END OBJ(user_names, INFO_USERS)
3863 END OBJ(user_times, INFO_USERS)
3864 END OBJ(user_terms, INFO_USERS)
3865 END OBJ(user_number, INFO_USERS)
3866 #if defined(__linux__)
3867 END OBJ(gw_iface, INFO_GW)
3868 END OBJ(gw_ip, INFO_GW)
3869 END OBJ(if_gw, INFO_GW)
3870 #endif /* !__linux__ */
3872 END OBJ(adt746xcpu, 0)
3873 END OBJ(adt746xfan, 0)
3874 #endif /* !__OpenBSD__ */
3875 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
3876 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
3877 END OBJ(apm_adapter, 0)
3878 END OBJ(apm_battery_life, 0)
3879 END OBJ(apm_battery_time, 0)
3880 #endif /* __FreeBSD__ */
3881 END OBJ_THREAD(imap_unseen, 0)
3884 obj->data.mail = parse_mail_args(IMAP, arg);
3885 obj->global_mode = 0;
3887 obj->global_mode = 1;
3889 END OBJ_THREAD(imap_messages, 0)
3892 obj->data.mail = parse_mail_args(IMAP, arg);
3893 obj->global_mode = 0;
3895 obj->global_mode = 1;
3897 END OBJ_THREAD(pop3_unseen, 0)
3900 obj->data.mail = parse_mail_args(POP3, arg);
3901 obj->global_mode = 0;
3903 obj->global_mode = 1;
3905 END OBJ_THREAD(pop3_used, 0)
3908 obj->data.mail = parse_mail_args(POP3, arg);
3909 obj->global_mode = 0;
3911 obj->global_mode = 1;
3916 obj->data.s = strndup(arg, text_buffer_size);
3918 ERR("smapi needs an argument");
3919 END OBJ(if_smapi_bat_installed, 0)
3920 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3921 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3924 ERR("if_smapi_bat_installed needs an argument");
3925 obj->data.ifblock.s = 0;
3927 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3928 blockstart[blockdepth] = object_count;
3929 obj->data.ifblock.pos = object_count + 2;
3931 END OBJ(smapi_bat_perc, 0)
3933 obj->data.s = strndup(arg, text_buffer_size);
3935 ERR("smapi_bat_perc needs an argument");
3936 END OBJ(smapi_bat_temp, 0)
3938 obj->data.s = strndup(arg, text_buffer_size);
3940 ERR("smapi_bat_temp needs an argument");
3941 END OBJ(smapi_bat_power, 0)
3943 obj->data.s = strndup(arg, text_buffer_size);
3945 ERR("smapi_bat_power needs an argument");
3946 END OBJ(smapi_bat_bar, 0)
3949 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
3950 ERR("first argument to smapi_bat_bar must be an integer value");
3954 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
3957 ERR("if_smapi_bat_bar needs an argument");
3960 END OBJ_THREAD(mpd_artist, INFO_MPD)
3961 END OBJ_THREAD(mpd_title, INFO_MPD)
3963 sscanf(arg, "%d", &obj->data.i);
3964 if (obj->data.i > 0) {
3967 CRIT_ERR("mpd_title: invalid length argument");
3972 END OBJ_THREAD(mpd_random, INFO_MPD)
3973 END OBJ_THREAD(mpd_repeat, INFO_MPD)
3974 END OBJ_THREAD(mpd_elapsed, INFO_MPD)
3975 END OBJ_THREAD(mpd_length, INFO_MPD)
3976 END OBJ_THREAD(mpd_track, INFO_MPD)
3977 END OBJ_THREAD(mpd_name, INFO_MPD)
3978 END OBJ_THREAD(mpd_file, INFO_MPD)
3979 END OBJ_THREAD(mpd_percent, INFO_MPD)
3980 END OBJ_THREAD(mpd_album, INFO_MPD)
3981 END OBJ_THREAD(mpd_vol, INFO_MPD)
3982 END OBJ_THREAD(mpd_bitrate, INFO_MPD)
3983 END OBJ_THREAD(mpd_status, INFO_MPD)
3984 END OBJ_THREAD(mpd_bar, INFO_MPD)
3985 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3986 END OBJ_THREAD(mpd_smart, INFO_MPD)
3988 sscanf(arg, "%d", &obj->data.i);
3989 if (obj->data.i > 0) {
3992 CRIT_ERR("mpd_smart: invalid length argument");
3997 END OBJ_THREAD(if_mpd_playing, INFO_MPD)
3998 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3999 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
4001 blockstart[blockdepth] = object_count;
4002 obj->data.ifblock.pos = object_count + 2;
4006 END OBJ_THREAD(moc_state, INFO_MOC)
4007 END OBJ_THREAD(moc_file, INFO_MOC)
4008 END OBJ_THREAD(moc_title, INFO_MOC)
4009 END OBJ_THREAD(moc_artist, INFO_MOC)
4010 END OBJ_THREAD(moc_song, INFO_MOC)
4011 END OBJ_THREAD(moc_album, INFO_MOC)
4012 END OBJ_THREAD(moc_totaltime, INFO_MOC)
4013 END OBJ_THREAD(moc_timeleft, INFO_MOC)
4014 END OBJ_THREAD(moc_curtime, INFO_MOC)
4015 END OBJ_THREAD(moc_bitrate, INFO_MOC)
4016 END OBJ_THREAD(moc_rate, INFO_MOC)
4019 END OBJ(xmms2_artist, INFO_XMMS2)
4020 END OBJ(xmms2_album, INFO_XMMS2)
4021 END OBJ(xmms2_title, INFO_XMMS2)
4022 END OBJ(xmms2_genre, INFO_XMMS2)
4023 END OBJ(xmms2_comment, INFO_XMMS2)
4024 END OBJ(xmms2_url, INFO_XMMS2)
4025 END OBJ(xmms2_tracknr, INFO_XMMS2)
4026 END OBJ(xmms2_bitrate, INFO_XMMS2)
4027 END OBJ(xmms2_date, INFO_XMMS2)
4028 END OBJ(xmms2_id, INFO_XMMS2)
4029 END OBJ(xmms2_duration, INFO_XMMS2)
4030 END OBJ(xmms2_elapsed, INFO_XMMS2)
4031 END OBJ(xmms2_size, INFO_XMMS2)
4032 END OBJ(xmms2_status, INFO_XMMS2)
4033 END OBJ(xmms2_percent, INFO_XMMS2)
4034 END OBJ(xmms2_bar, INFO_XMMS2)
4035 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
4036 END OBJ(xmms2_smart, INFO_XMMS2)
4037 END OBJ(xmms2_playlist, INFO_XMMS2)
4038 END OBJ(xmms2_timesplayed, INFO_XMMS2)
4041 END OBJ(audacious_status, INFO_AUDACIOUS)
4042 END OBJ(audacious_title, INFO_AUDACIOUS)
4044 sscanf(arg, "%d", &info.audacious.max_title_len);
4045 if (info.audacious.max_title_len > 0) {
4046 info.audacious.max_title_len++;
4048 CRIT_ERR("audacious_title: invalid length argument");
4051 END OBJ(audacious_length, INFO_AUDACIOUS)
4052 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
4053 END OBJ(audacious_position, INFO_AUDACIOUS)
4054 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
4055 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
4056 END OBJ(audacious_frequency, INFO_AUDACIOUS)
4057 END OBJ(audacious_channels, INFO_AUDACIOUS)
4058 END OBJ(audacious_filename, INFO_AUDACIOUS)
4059 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
4060 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
4061 END OBJ(audacious_bar, INFO_AUDACIOUS)
4062 scan_bar(arg, &obj->a, &obj->b);
4065 END OBJ(bmpx_title, INFO_BMPX)
4066 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4067 END OBJ(bmpx_artist, INFO_BMPX)
4068 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4069 END OBJ(bmpx_album, INFO_BMPX)
4070 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4071 END OBJ(bmpx_track, INFO_BMPX)
4072 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4073 END OBJ(bmpx_uri, INFO_BMPX)
4074 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4075 END OBJ(bmpx_bitrate, INFO_BMPX)
4076 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
4082 char *userid = (char *) malloc(20 * sizeof(char));
4083 char *apikey = (char *) malloc(64 * sizeof(char));
4084 char *charid = (char *) malloc(20 * sizeof(char));
4086 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
4087 obj->data.eve.charid = charid;
4088 obj->data.eve.userid = userid;
4089 obj->data.eve.apikey = apikey;
4093 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
4099 int argc, delay, act_par;
4100 char *uri = (char *) malloc(128 * sizeof(char));
4101 char *action = (char *) malloc(64 * sizeof(char));
4103 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
4105 obj->data.rss.uri = uri;
4106 obj->data.rss.delay = delay;
4107 obj->data.rss.action = action;
4108 obj->data.rss.act_par = act_par;
4112 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
4118 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
4119 &obj->data.hddtemp.addr, &obj->data.hddtemp.port, &obj->data.hddtemp.temp)) {
4120 ERR("hddtemp needs arguments");
4121 obj->type = OBJ_text;
4122 obj->data.s = strndup("${hddtemp}", text_buffer_size);
4123 obj->data.hddtemp.update_time = 0;
4127 #ifdef TCP_PORT_MONITOR
4128 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
4129 int argc, port_begin, port_end, item, connection_index;
4132 memset(itembuf, 0, sizeof(itembuf));
4133 connection_index = 0;
4134 /* massive argument checking */
4136 CRIT_ERR("tcp_portmon: needs arguments");
4138 argc = sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf,
4140 if ((argc != 3) && (argc != 4)) {
4141 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
4143 if ((port_begin < 1) || (port_begin > 65535) || (port_end < 1)
4144 || (port_end > 65535)) {
4145 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
4147 if (port_begin > port_end) {
4148 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
4150 if (strncmp(itembuf, "count", 31) == EQUAL) {
4152 } else if (strncmp(itembuf, "rip", 31) == EQUAL) {
4154 } else if (strncmp(itembuf, "rhost", 31) == EQUAL) {
4156 } else if (strncmp(itembuf, "rport", 31) == EQUAL) {
4158 } else if (strncmp(itembuf, "rservice", 31) == EQUAL) {
4159 item = REMOTESERVICE;
4160 } else if (strncmp(itembuf, "lip", 31) == EQUAL) {
4162 } else if (strncmp(itembuf, "lhost", 31) == EQUAL) {
4164 } else if (strncmp(itembuf, "lport", 31) == EQUAL) {
4166 } else if (strncmp(itembuf, "lservice", 31) == EQUAL) {
4167 item = LOCALSERVICE;
4169 CRIT_ERR("tcp_portmon: invalid item specified");
4171 if ((argc == 3) && (item != COUNT)) {
4172 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" "
4175 if ((argc == 4) && (connection_index < 0)) {
4176 CRIT_ERR("tcp_portmon: connection index must be non-negative");
4178 /* ok, args looks good. save the text object data */
4179 obj->data.tcp_port_monitor.port_range_begin = (in_port_t) port_begin;
4180 obj->data.tcp_port_monitor.port_range_end = (in_port_t) port_end;
4181 obj->data.tcp_port_monitor.item = item;
4182 obj->data.tcp_port_monitor.connection_index = connection_index;
4184 /* if the port monitor collection hasn't been created,
4185 * we must create it */
4186 if (!info.p_tcp_port_monitor_collection) {
4187 info.p_tcp_port_monitor_collection =
4188 create_tcp_port_monitor_collection();
4189 if (!info.p_tcp_port_monitor_collection) {
4190 CRIT_ERR("tcp_portmon: unable to create port monitor "
4195 /* if a port monitor for this port does not exist,
4196 * create one and add it to the collection */
4197 if (find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
4198 port_begin, port_end) == NULL) {
4199 tcp_port_monitor_t *p_monitor = create_tcp_port_monitor(port_begin,
4200 port_end, &tcp_port_monitor_args);
4203 CRIT_ERR("tcp_portmon: unable to create port monitor");
4205 /* add the newly created monitor to the collection */
4206 if (insert_tcp_port_monitor_into_collection(
4207 info.p_tcp_port_monitor_collection, p_monitor) != 0) {
4208 CRIT_ERR("tcp_portmon: unable to add port monitor to "
4213 END OBJ(entropy_avail, INFO_ENTROPY)
4214 END OBJ(entropy_poolsize, INFO_ENTROPY)
4215 END OBJ(entropy_bar, INFO_ENTROPY)
4216 scan_bar(arg, &obj->a, &obj->b);
4220 obj->data.scroll.step = 1;
4221 if (arg && sscanf(arg, "%u %u %n", &obj->data.scroll.show, &obj->data.scroll.step, &n) > 0) {
4222 obj->data.scroll.text = strndup(arg + n, text_buffer_size);
4223 obj->data.scroll.start = 0;
4225 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
4230 CRIT_ERR("nvidia needs an argument\n");
4231 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
4232 CRIT_ERR("nvidia: invalid argument"
4233 " specified: '%s'\n", arg);
4239 ERR("unknown variable %s", s);
4240 obj->type = OBJ_text;
4241 snprintf(buf, 256, "${%s}", s);
4242 obj->data.s = strndup(buf, text_buffer_size);
4249 static struct text_object *create_plain_text(const char *s)
4251 struct text_object *obj;
4253 if (s == NULL || *s == '\0') {
4257 obj = new_text_object_internal();
4259 obj->type = OBJ_text;
4260 obj->data.s = strndup(s, text_buffer_size);
4264 static struct text_object_list *extract_variable_text_internal(const char *const_p, char allow_threaded)
4266 struct text_object_list *retval;
4267 struct text_object *obj;
4268 char *p, *s, *orig_p;
4271 p = strndup(const_p, max_user_text);
4274 retval = malloc(sizeof(struct text_object_list));
4275 memset(retval, 0, sizeof(struct text_object_list));
4276 retval->text_object_count = 0;
4278 line = global_text_lines;
4286 obj = create_plain_text(s);
4288 // allocate memory for the object
4289 retval->text_objects = realloc(retval->text_objects,
4290 sizeof(struct text_object) *
4291 (retval->text_object_count + 1));
4292 // assign the new object to the end of the list.
4293 memcpy(&retval->text_objects[retval->text_object_count++], obj,
4294 sizeof(struct text_object));
4306 /* variable is either $foo or ${foo} */
4308 unsigned int brl = 1, brr = 0;
4312 while (*p && brl != brr) {
4327 while (*p && (isalnum((int) *p) || *p == '_')) {
4332 /* copy variable to buffer */
4333 len = (p - s > 255) ? 255 : (p - s);
4334 strncpy(buf, s, len);
4344 /* if variable wasn't found in environment, use some special */
4350 if (strchr(buf, ' ')) {
4351 arg = strchr(buf, ' ');
4354 while (isspace((int) *arg)) {
4362 /* lowercase variable name */
4365 *tmp_p = tolower(*tmp_p);
4369 // create new object
4370 obj = construct_text_object(buf, arg,
4371 retval->text_object_count, retval->text_objects, line, allow_threaded);
4373 // allocate memory for the object
4374 retval->text_objects = realloc(retval->text_objects,
4375 sizeof(struct text_object) *
4376 (retval->text_object_count + 1));
4377 // assign the new object to the end of the list.
4379 &retval->text_objects[retval->text_object_count++],
4380 obj, sizeof(struct text_object));
4386 obj = create_plain_text("$");
4388 // allocate memory for the object
4389 retval->text_objects = realloc(retval->text_objects,
4390 sizeof(struct text_object) *
4391 (retval->text_object_count + 1));
4392 // assign the new object to the end of the list.
4393 memcpy(&retval->text_objects[retval->text_object_count++],
4394 obj, sizeof(struct text_object));
4401 obj = create_plain_text(s);
4403 // allocate memory for the object
4404 retval->text_objects = realloc(retval->text_objects,
4405 sizeof(struct text_object) * (retval->text_object_count + 1));
4406 // assign the new object to the end of the list.
4407 memcpy(&retval->text_objects[retval->text_object_count++], obj,
4408 sizeof(struct text_object));
4413 ERR("one or more $endif's are missing");
4420 static void extract_variable_text(const char *p)
4422 free_text_objects(global_text_object_list, 1);
4423 free(global_text_object_list);
4437 global_text_object_list = extract_variable_text_internal(p, 1);
4440 struct text_object_list *parse_conky_vars(char *txt, char *p, struct information *cur)
4442 struct text_object_list *object_list =
4443 extract_variable_text_internal(txt, 0);
4445 generate_text_internal(p, max_user_text, object_list, cur);
4449 /* Allows reading from a FIFO (i.e., /dev/xconsole).
4450 * The file descriptor is set to non-blocking which makes this possible.
4452 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
4453 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
4455 #define TAIL_PIPE_BUFSIZE 4096
4459 int fd = obj->data.tail.fd;
4462 char buf[TAIL_PIPE_BUFSIZE];
4463 ssize_t len = read(fd, buf, sizeof(buf));
4467 if (errno != EAGAIN) {
4468 strcpy(obj->data.tail.buffer, "Logfile Read Error");
4469 snprintf(dst, dst_size, "Logfile Read Error");
4473 } else if (len == 0) {
4474 strcpy(obj->data.tail.buffer, "Logfile Empty");
4475 snprintf(dst, dst_size, "Logfile Empty");
4479 for (line_len = 0, i = 0; i < len; i++) {
4483 if (buf[i] == '\n') {
4486 if (obj->data.tail.readlines > 0) {
4491 for (n = 0; obj->data.tail.buffer[n]; n++) {
4492 if (obj->data.tail.buffer[n] == '\n') {
4497 if (++olines < obj->data.tail.wantedlines) {
4503 p = obj->data.tail.buffer + first_line;
4504 pos = n - first_line;
4505 memmove(obj->data.tail.buffer,
4506 obj->data.tail.buffer + first_line, strlen(p));
4507 obj->data.tail.buffer[pos] = 0;
4513 p = buf + last_line;
4515 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
4516 obj->data.tail.buffer[pos + line_len] = 0;
4519 obj->data.tail.readlines = lines;
4527 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
4530 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
4531 void *thread(void *), const char *text)
4533 if (obj->global_mode && info.mail) {
4534 // this means we use info
4535 if (!info.mail->p_timed_thread) {
4536 info.mail->p_timed_thread =
4537 timed_thread_create(thread,
4538 (void *) info.mail, info.mail->interval * 1000000);
4539 if (!info.mail->p_timed_thread) {
4540 ERR("Error creating %s timed thread", text);
4542 timed_thread_register(info.mail->p_timed_thread,
4543 &info.mail->p_timed_thread);
4544 if (timed_thread_run(info.mail->p_timed_thread)) {
4545 ERR("Error running %s timed thread", text);
4549 } else if (obj->data.mail) {
4550 // this means we use obj
4551 if (!obj->data.mail->p_timed_thread) {
4552 obj->data.mail->p_timed_thread =
4553 timed_thread_create(thread,
4554 (void *) obj->data.mail,
4555 obj->data.mail->interval * 1000000);
4556 if (!obj->data.mail->p_timed_thread) {
4557 ERR("Error creating %s timed thread", text);
4559 timed_thread_register(obj->data.mail->p_timed_thread,
4560 &obj->data.mail->p_timed_thread);
4561 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
4562 ERR("Error running %s timed thread", text);
4565 return obj->data.mail;
4566 } else if (!obj->a) {
4567 // something is wrong, warn once then stop
4568 ERR("Theres a problem with your %s_unseen settings. "
4569 "Check that the global %s settings are defined "
4570 "properly (line %li).", global_text, global_text, obj->line);
4576 char *format_time(unsigned long timeval, const int width)
4579 unsigned long nt; // narrow time, for speed on 32-bit
4580 unsigned cc; // centiseconds
4581 unsigned nn; // multi-purpose whatever
4584 cc = nt % 100; // centiseconds past second
4585 nt /= 100; // total seconds
4586 nn = nt % 60; // seconds past the minute
4587 nt /= 60; // total minutes
4588 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
4590 return strndup(buf, text_buffer_size);
4592 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
4593 return strndup(buf, text_buffer_size);
4595 nn = nt % 60; // minutes past the hour
4596 nt /= 60; // total hours
4597 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
4598 return strndup(buf, text_buffer_size);
4600 nn = nt; // now also hours
4601 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
4602 return strndup(buf, text_buffer_size);
4604 nn /= 24; // now days
4605 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
4606 return strndup(buf, text_buffer_size);
4608 nn /= 7; // now weeks
4609 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
4610 return strndup(buf, text_buffer_size);
4612 // well shoot, this outta' fit...
4613 return strndup("<inf>", text_buffer_size);
4616 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
4617 //string has to end with \0 and it's length should fit in a int
4619 void remove_deleted_chars(char *string){
4621 while(string[i] != 0){
4622 if(string[i] == BACKSPACE){
4624 strcpy( &(string[i-1]), &(string[i+1]) );
4626 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
4631 static inline void format_media_player_time(char *buf, const int size,
4634 int days, hours, minutes;
4636 days = seconds / (24 * 60 * 60);
4637 seconds %= (24 * 60 * 60);
4638 hours = seconds / (60 * 60);
4639 seconds %= (60 * 60);
4640 minutes = seconds / 60;
4644 snprintf(buf, size, "%i days %i:%02i:%02i", days,
4645 hours, minutes, seconds);
4646 } else if (hours > 0) {
4647 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
4650 snprintf(buf, size, "%i:%02i", minutes, seconds);
4654 static inline double get_barnum(char *buf)
4666 if (sscanf(buf, "%lf", &barnum) == 0) {
4667 ERR("reading execbar value failed (perhaps it's not the "
4668 "correct format?)");
4671 if (barnum > 100.0 || barnum < 0.0) {
4672 ERR("your execbar value is not between 0 and 100, "
4673 "therefore it will be ignored");
4679 static void generate_text_internal(char *p, int p_max_size,
4680 struct text_object_list *text_object_list,
4681 struct information *cur)
4686 char buff_in[p_max_size];
4688 iconv_converting = 0;
4692 for (i = 0; i < text_object_list->text_object_count; i++) {
4693 struct text_object *obj = &(text_object_list->text_objects[i]);
4695 if (p_max_size < 1) {
4699 #define OBJ(a) break; case OBJ_##a:
4701 switch (obj->type) {
4703 ERR("not implemented obj type %d", obj->type);
4706 /* does anyone have decimals in acpi temperature? */
4707 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4708 round_to_int(get_acpi_temperature(obj->data.i)));
4711 /* does anyone have decimals in acpi temperature? */
4712 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4713 round_to_int((get_acpi_temperature(obj->data.i) + 40) *
4716 #endif /* !__OpenBSD__ */
4719 obj->a = get_freq(p, p_max_size, "%.0f", 1,
4720 obj->data.cpu_index);
4726 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
4727 obj->data.cpu_index);
4729 /* OpenBSD has no such flag (SUSv2) */
4730 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
4731 obj->data.cpu_index);
4735 #if defined(__linux__)
4738 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
4739 obj->data.cpu_index);
4744 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
4745 obj->data.cpu_index);
4750 OBJ(wireless_essid) {
4751 snprintf(p, p_max_size, "%s", obj->data.net->essid);
4753 OBJ(wireless_mode) {
4754 snprintf(p, p_max_size, "%s", obj->data.net->mode);
4756 OBJ(wireless_bitrate) {
4757 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
4760 snprintf(p, p_max_size, "%s", obj->data.net->ap);
4762 OBJ(wireless_link_qual) {
4763 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
4764 obj->data.net->link_qual);
4766 OBJ(wireless_link_qual_max) {
4767 spaced_print(p, p_max_size, "%d", 4,
4768 "wireless_link_qual_max", obj->data.net->link_qual_max);
4770 OBJ(wireless_link_qual_perc) {
4771 if (obj->data.net->link_qual_max > 0) {
4772 spaced_print(p, p_max_size, "%.0f", 5,
4773 "wireless_link_qual_perc",
4774 (double) obj->data.net->link_qual /
4775 obj->data.net->link_qual_max * 100);
4777 spaced_print(p, p_max_size, "unk", 5,
4778 "wireless_link_qual_perc");
4781 OBJ(wireless_link_bar) {
4782 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4783 obj->data.net->link_qual_max) * 255.0);
4785 #endif /* HAVE_IWLIB */
4787 #endif /* __linux__ */
4790 get_freq_dynamic(p, p_max_size, "%.0f", 1);
4791 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4795 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
4797 get_freq_dynamic(p, p_max_size, "%.2f", 1000);
4799 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4804 get_adt746x_cpu(p, p_max_size);
4807 get_adt746x_fan(p, p_max_size);
4810 get_acpi_fan(p, p_max_size);
4812 OBJ(acpiacadapter) {
4813 get_acpi_ac_adapter(p, p_max_size);
4816 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4819 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4821 OBJ(battery_percent) {
4822 spaced_print(p, p_max_size, "%*d", 4, "battery_percent",
4823 pad_percents, get_battery_perct(obj->data.s));
4826 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4828 #endif /* __OpenBSD__ */
4831 human_readable(cur->buffers * 1024, p, 255, "buffers");
4834 human_readable(cur->cached * 1024, p, 255, "buffers");
4837 if (obj->data.cpu_index > info.cpu_count) {
4838 printf("obj->data.cpu_index %i info.cpu_count %i",
4839 obj->data.cpu_index, info.cpu_count);
4840 CRIT_ERR("attempting to use more CPUs than you have!");
4842 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
4843 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4846 new_bar(p, obj->a, obj->b,
4847 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4850 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
4851 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4852 100, 1, obj->showaslog);
4855 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
4856 obj->e, 1, obj->showaslog);
4859 new_fg(p, obj->data.l);
4891 OBJ(conky_version) {
4892 snprintf(p, p_max_size, "%s", VERSION);
4894 OBJ(conky_build_date) {
4895 snprintf(p, p_max_size, "%s", BUILD_DATE);
4897 OBJ(conky_build_arch) {
4898 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4900 #if defined(__linux__)
4902 snprintf(p, p_max_size, "%s",
4903 get_disk_protect_queue(obj->data.s));
4906 snprintf(p, p_max_size, "%s", i8k.version);
4909 snprintf(p, p_max_size, "%s", i8k.bios);
4912 snprintf(p, p_max_size, "%s", i8k.serial);
4915 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
4917 OBJ(i8k_cpu_tempf) {
4920 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4921 snprintf(p, p_max_size, "%.1f", cpu_temp * (9.0 / 5.0) + 32.0);
4923 OBJ(i8k_left_fan_status) {
4924 int left_fan_status;
4926 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4927 if (left_fan_status == 0) {
4928 snprintf(p, p_max_size, "off");
4930 if (left_fan_status == 1) {
4931 snprintf(p, p_max_size, "low");
4933 if (left_fan_status == 2) {
4934 snprintf(p, p_max_size, "high");
4937 OBJ(i8k_right_fan_status) {
4938 int right_fan_status;
4940 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4941 if (right_fan_status == 0) {
4942 snprintf(p, p_max_size, "off");
4944 if (right_fan_status == 1) {
4945 snprintf(p, p_max_size, "low");
4947 if (right_fan_status == 2) {
4948 snprintf(p, p_max_size, "high");
4951 OBJ(i8k_left_fan_rpm) {
4952 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4954 OBJ(i8k_right_fan_rpm) {
4955 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4957 OBJ(i8k_ac_status) {
4960 sscanf(i8k.ac_status, "%d", &ac_status);
4961 if (ac_status == -1) {
4962 snprintf(p, p_max_size, "disabled (read i8k docs)");
4964 if (ac_status == 0) {
4965 snprintf(p, p_max_size, "off");
4967 if (ac_status == 1) {
4968 snprintf(p, p_max_size, "on");
4971 OBJ(i8k_buttons_status) {
4972 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4975 get_ibm_acpi_fan(p, p_max_size);
4978 get_ibm_acpi_temps();
4979 snprintf(p, p_max_size, "%d", ibm_acpi.temps[obj->data.sensor]);
4982 get_ibm_acpi_volume(p, p_max_size);
4984 OBJ(ibm_brightness) {
4985 get_ibm_acpi_brightness(p, p_max_size);
4988 if ((obj->data.ifblock.s)
4989 && (!interface_up(obj->data.ifblock.s))) {
4990 i = obj->data.ifblock.pos;
4997 if (!cur->gw_info.count) {
4998 i = obj->data.ifblock.pos;
5005 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
5008 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
5011 snprintf(p, p_max_size, "%d", get_laptop_mode());
5014 get_powerbook_batt_info(p, p_max_size, obj->data.i);
5016 #endif /* __linux__ */
5019 OBJ(obsd_sensors_temp) {
5020 obsd_sensors.device = sensor_device;
5021 update_obsd_sensors();
5022 snprintf(p, p_max_size, "%.1f",
5023 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor]);
5025 OBJ(obsd_sensors_fan) {
5026 obsd_sensors.device = sensor_device;
5027 update_obsd_sensors();
5028 snprintf(p, p_max_size, "%d",
5029 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
5031 OBJ(obsd_sensors_volt) {
5032 obsd_sensors.device = sensor_device;
5033 update_obsd_sensors();
5034 snprintf(p, p_max_size, "%.2f",
5035 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
5038 get_obsd_vendor(p, p_max_size);
5041 get_obsd_product(p, p_max_size);
5043 #endif /* __OpenBSD__ */
5047 new_font(p, obj->data.s);
5050 /* TODO: move this correction from kB to kB/s elsewhere
5051 * (or get rid of it??) */
5053 if (obj->data.diskio) {
5055 (obj->data.diskio->current / update_interval) * 1024LL,
5056 p, p_max_size, "diskio");
5058 human_readable(info.diskio_value * 1024LL, p, p_max_size,
5063 if (obj->data.diskio) {
5064 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
5067 human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
5072 if (obj->data.diskio) {
5073 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
5076 human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
5081 if (obj->data.diskio) {
5082 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5083 obj->data.diskio->current, obj->e, 1, obj->showaslog);
5085 new_graph(p, obj->a, obj->b, obj->c, obj->d, info.diskio_value,
5086 obj->e, 1, obj->showaslog);
5089 OBJ(diskiograph_read) {
5090 if (obj->data.diskio) {
5091 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5092 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
5094 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5095 info.diskio_read_value, obj->e, 1, obj->showaslog);
5098 OBJ(diskiograph_write) {
5099 if (obj->data.diskio) {
5100 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5101 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
5103 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5104 info.diskio_write_value, obj->e, 1, obj->showaslog);
5108 spaced_print(p, p_max_size, "%d", 6, "downspeed",
5109 round_to_int(obj->data.net->recv_speed / 1024));
5112 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
5113 obj->data.net->recv_speed / 1024.0);
5115 OBJ(downspeedgraph) {
5116 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5117 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
5121 i = obj->data.ifblock.pos - 1;
5131 if ((obj->data.net->addr.sa_data[2] & 255) == 0
5132 && (obj->data.net->addr.sa_data[3] & 255) == 0
5133 && (obj->data.net->addr.sa_data[4] & 255) == 0
5134 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
5135 snprintf(p, p_max_size, "No Address");
5137 snprintf(p, p_max_size, "%u.%u.%u.%u",
5138 obj->data.net->addr.sa_data[2] & 255,
5139 obj->data.net->addr.sa_data[3] & 255,
5140 obj->data.net->addr.sa_data[4] & 255,
5141 obj->data.net->addr.sa_data[5] & 255);
5144 #if defined(__linux__)
5146 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
5148 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
5149 strcpy(p, obj->data.net->addrs);
5152 strcpy(p, "0.0.0.0");
5154 #endif /* __linux__ */
5156 #if defined(IMLIB2) && defined(X11)
5161 Imlib_Image image, buffer;
5163 image = imlib_load_image(obj->data.s);
5164 imlib_context_set_image(image);
5168 w = imlib_image_get_width();
5169 h = imlib_image_get_height();
5170 buffer = imlib_create_image(w, h);
5171 imlib_context_set_display(display);
5172 imlib_context_set_drawable(window.drawable);
5173 imlib_context_set_colormap(DefaultColormap(display,
5175 imlib_context_set_visual(DefaultVisual(display,
5177 imlib_context_set_image(buffer);
5178 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
5179 text_start_x, text_start_y, w, h);
5180 imlib_render_image_on_drawable(text_start_x,
5183 imlib_context_set_image(image);
5191 read_exec(obj->data.s, p, p_max_size);
5192 remove_deleted_chars(p);
5195 struct information *tmp_info;
5196 struct text_object_list *text_objects;
5198 read_exec(obj->data.s, p, p_max_size);
5200 tmp_info = malloc(sizeof(struct information));
5201 memcpy(tmp_info, cur, sizeof(struct information));
5202 text_objects = parse_conky_vars(p, p, tmp_info);
5204 free_text_objects(text_objects, 0);
5211 read_exec(obj->data.s, p, p_max_size);
5212 barnum = get_barnum(p);
5214 if (barnum >= 0.0) {
5215 new_bar(p, 0, 4, round_to_int(barnum * 255.0));
5219 char showaslog = FALSE;
5222 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
5224 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, p_max_size);
5225 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
5226 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, p_max_size);
5228 read_exec(obj->data.s, p, p_max_size);
5230 barnum = get_barnum(p);
5232 if (barnum >= 0.0) {
5233 new_graph(p, 0, 25, obj->c, obj->d, round_to_int(barnum),
5238 if (current_update_time - obj->data.execi.last_update
5239 >= obj->data.execi.interval) {
5242 read_exec(obj->data.execi.cmd, p, p_max_size);
5243 barnum = get_barnum(p);
5245 if (barnum >= 0.0) {
5246 obj->f = 255 * barnum / 100.0;
5248 obj->data.execi.last_update = current_update_time;
5250 new_bar(p, 0, 4, round_to_int(obj->f));
5253 if (current_update_time - obj->data.execi.last_update
5254 >= obj->data.execi.interval) {
5257 read_exec(obj->data.execi.cmd, p, p_max_size);
5258 barnum = get_barnum(p);
5260 if (barnum >= 0.0) {
5263 obj->data.execi.last_update = current_update_time;
5265 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
5268 if (current_update_time - obj->data.execi.last_update
5269 >= obj->data.execi.interval
5270 && obj->data.execi.interval != 0) {
5271 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
5273 obj->data.execi.last_update = current_update_time;
5275 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
5278 struct text_object_list *text_objects = 0;
5279 struct information *tmp_info =
5280 malloc(sizeof(struct information));
5281 memcpy(tmp_info, cur, sizeof(struct information));
5283 if (current_update_time - obj->data.execi.last_update
5284 < obj->data.execi.interval
5285 || obj->data.execi.interval == 0) {
5286 text_objects = parse_conky_vars(obj->data.execi.buffer, p, tmp_info);
5288 char *output = obj->data.execi.buffer;
5289 FILE *fp = popen(obj->data.execi.cmd, "r");
5290 int length = fread(output, 1, text_buffer_size, fp);
5294 output[length] = '\0';
5295 if (length > 0 && output[length - 1] == '\n') {
5296 output[length - 1] = '\0';
5299 text_objects = parse_conky_vars(obj->data.execi.buffer, p, tmp_info);
5300 obj->data.execi.last_update = current_update_time;
5302 free_text_objects(text_objects, 0);
5307 if (!obj->data.texeci.p_timed_thread) {
5308 obj->data.texeci.p_timed_thread =
5309 timed_thread_create(&threaded_exec,
5310 (void *) obj, obj->data.texeci.interval * 1000000);
5311 if (!obj->data.texeci.p_timed_thread) {
5312 ERR("Error creating texeci timed thread");
5314 timed_thread_register(obj->data.texeci.p_timed_thread,
5315 &obj->data.texeci.p_timed_thread);
5316 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
5317 ERR("Error running texeci timed thread");
5320 timed_thread_lock(obj->data.texeci.p_timed_thread);
5321 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
5322 timed_thread_unlock(obj->data.texeci.p_timed_thread);
5324 #endif /* HAVE_POPEN */
5326 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
5328 if (mail && mail->p_timed_thread) {
5329 timed_thread_lock(mail->p_timed_thread);
5330 snprintf(p, p_max_size, "%lu", mail->unseen);
5331 timed_thread_unlock(mail->p_timed_thread);
5334 OBJ(imap_messages) {
5335 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
5337 if (mail && mail->p_timed_thread) {
5338 timed_thread_lock(mail->p_timed_thread);
5339 snprintf(p, p_max_size, "%lu", mail->messages);
5340 timed_thread_unlock(mail->p_timed_thread);
5344 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
5346 if (mail && mail->p_timed_thread) {
5347 timed_thread_lock(mail->p_timed_thread);
5348 snprintf(p, p_max_size, "%lu", mail->unseen);
5349 timed_thread_unlock(mail->p_timed_thread);
5353 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
5355 if (mail && mail->p_timed_thread) {
5356 timed_thread_lock(mail->p_timed_thread);
5357 snprintf(p, p_max_size, "%.1f",
5358 mail->used / 1024.0 / 1024.0);
5359 timed_thread_unlock(mail->p_timed_thread);
5363 if (obj->data.fs != NULL) {
5364 if (obj->data.fs->size == 0) {
5365 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5367 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5368 (int) (255 - obj->data.fsbar.fs->avail * 255 /
5369 obj->data.fs->size));
5374 if (obj->data.fs != NULL) {
5375 human_readable(obj->data.fs->avail, p, 255, "fs_free");
5379 if (obj->data.fs != NULL) {
5380 if (obj->data.fs->size) {
5381 spaced_print(p, p_max_size, "%*d", 4, "fs_free_perc",
5382 pad_percents, (int) ((obj->data.fs->avail * 100) /
5383 obj->data.fs->size));
5385 snprintf(p, p_max_size, "0");
5390 if (obj->data.fs != NULL) {
5391 human_readable(obj->data.fs->size, p, 255, "fs_size");
5395 if (obj->data.fs != NULL)
5396 snprintf(p, p_max_size, "%s", obj->data.fs->type);
5399 if (obj->data.fs != NULL) {
5400 human_readable(obj->data.fs->size - (obj->data.fs->free
5401 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
5406 if (obj->data.fs != NULL) {
5407 if (obj->data.fs->size == 0) {
5408 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5410 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5411 (int) (obj->data.fsbar.fs->avail * 255 /
5412 obj->data.fs->size));
5417 if (obj->data.fs != NULL) {
5418 if (obj->data.fs->size) {
5419 spaced_print(p, 4, "%*d", 4, "fs_used_perc",
5420 pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
5421 obj->data.fs->size)));
5423 snprintf(p, p_max_size, "0");
5428 float *v = info.loadavg;
5430 if (obj->data.loadavg[2]) {
5431 snprintf(p, p_max_size, "%.2f %.2f %.2f",
5432 v[obj->data.loadavg[0] - 1],
5433 v[obj->data.loadavg[1] - 1],
5434 v[obj->data.loadavg[2] - 1]);
5435 } else if (obj->data.loadavg[1]) {
5436 snprintf(p, p_max_size, "%.2f %.2f",
5437 v[obj->data.loadavg[0] - 1],
5438 v[obj->data.loadavg[1] - 1]);
5439 } else if (obj->data.loadavg[0]) {
5440 snprintf(p, p_max_size, "%.2f",
5441 v[obj->data.loadavg[0] - 1]);
5445 new_goto(p, obj->data.i);
5448 new_tab(p, obj->data.pair.a, obj->data.pair.b);
5451 new_hr(p, obj->data.i);
5454 if (cur->nameserver_info.nscount > obj->data.i)
5455 snprintf(p, p_max_size, "%s",
5456 cur->nameserver_info.ns_list[obj->data.i]);
5460 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
5461 snprintf(p, p_max_size, "%s", skill);
5466 PRSS *data = get_rss_info(obj->data.rss.uri,
5467 obj->data.rss.delay);
5471 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
5473 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
5475 // remove trailing new line if one exists
5476 if (str[strlen(str) - 1] == '\n') {
5477 str[strlen(str) - 1] = 0;
5479 snprintf(p, p_max_size, "%s", str);
5480 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
5481 if (obj->data.rss.act_par < data->item_count) {
5482 str = data->items[obj->data.rss.act_par].title;
5483 // remove trailing new line if one exists
5484 if (str[strlen(str) - 1] == '\n') {
5485 str[strlen(str) - 1] = 0;
5487 snprintf(p, p_max_size, "%s", str);
5489 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
5490 if (obj->data.rss.act_par < data->item_count) {
5492 data->items[obj->data.rss.act_par].description;
5493 // remove trailing new line if one exists
5494 if (str[strlen(str) - 1] == '\n') {
5495 str[strlen(str) - 1] = 0;
5497 snprintf(p, p_max_size, "%s", str);
5499 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
5500 if (data->item_count > 0) {
5506 if (obj->data.rss.act_par > data->item_count) {
5507 show = data->item_count;
5509 show = obj->data.rss.act_par;
5511 for (itmp = 0; itmp < show; itmp++) {
5512 PRSS_Item *item = &data->items[itmp];
5516 // don't add new line before first item
5518 strncat(p, "\n", p_max_size);
5520 /* remove trailing new line if one exists,
5521 * we have our own */
5522 if (str[strlen(str) - 1] == '\n') {
5523 str[strlen(str) - 1] = 0;
5525 strncat(p, str, p_max_size);
5535 if (obj->data.hddtemp.update_time < current_update_time - 30) {
5536 char *str = get_hddtemp_info(obj->data.hddtemp.dev,
5537 obj->data.hddtemp.addr, obj->data.hddtemp.port, &obj->data.hddtemp.unit);
5539 strncpy(obj->data.hddtemp.temp, str, text_buffer_size);
5541 obj->data.hddtemp.temp[0] = 0;
5543 obj->data.hddtemp.update_time = current_update_time;
5545 if (!obj->data.hddtemp.temp) {
5546 snprintf(p, p_max_size, "N/A");
5547 } else if (obj->data.hddtemp.unit == '*') {
5548 snprintf(p, p_max_size, "%s", obj->data.hddtemp.temp);
5550 snprintf(p, p_max_size, "%s%c", obj->data.hddtemp.temp, obj->data.hddtemp.unit);
5555 new_offset(p, obj->data.i);
5558 new_voffset(p, obj->data.i);
5564 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5565 obj->data.sysfs.devtype, obj->data.sysfs.type);
5567 if (r >= 100.0 || r == 0) {
5568 snprintf(p, p_max_size, "%d", (int) r);
5570 snprintf(p, p_max_size, "%.1f", r);
5576 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5577 obj->data.sysfs.devtype, obj->data.sysfs.type);
5579 if (r >= 100.0 || r == 0) {
5580 snprintf(p, p_max_size, "%d", (int) r);
5582 snprintf(p, p_max_size, "%.1f", r);
5588 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5589 obj->data.sysfs.devtype, obj->data.sysfs.type);
5591 if (r >= 100.0 || r == 0) {
5592 snprintf(p, p_max_size, "%d", (int) r);
5594 snprintf(p, p_max_size, "%.1f", r);
5597 #endif /* !__OpenBSD__ */
5599 new_alignr(p, obj->data.i);
5602 new_alignc(p, obj->data.i);
5605 struct text_object_list *text_objects;
5606 struct information *tmp_info =
5607 malloc(sizeof(struct information));
5608 memcpy(tmp_info, cur, sizeof(struct information));
5609 text_objects = parse_conky_vars(obj->data.ifblock.s, p, tmp_info);
5611 if (strlen(p) != 0) {
5612 i = obj->data.ifblock.pos;
5618 free_text_objects(text_objects, 0);
5625 if ((obj->data.ifblock.s)
5626 && (stat(obj->data.ifblock.s, &tmp) == -1)) {
5627 i = obj->data.ifblock.pos;
5630 if (obj->data.ifblock.str) {
5631 if (!check_contains(obj->data.ifblock.s,
5632 obj->data.ifblock.str)) {
5633 i = obj->data.ifblock.pos;
5644 if ((obj->data.ifblock.s)
5645 && (!check_mount(obj->data.ifblock.s))) {
5646 i = obj->data.ifblock.pos;
5653 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
5654 i = obj->data.ifblock.pos;
5660 #if defined(__linux__)
5662 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
5666 snprintf(p, p_max_size, "%s", cur->uname_s.release);
5669 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
5674 human_readable(cur->mem * 1024, p, 255, "mem");
5677 human_readable(cur->memeasyfree * 1024, p, 255, "memeasyfree");
5680 human_readable(cur->memfree * 1024, p, 255, "memfree");
5683 human_readable(cur->memmax * 1024, p, 255, "memmax");
5687 spaced_print(p, p_max_size, "%*llu", 4, "memperc",
5688 pad_percents, cur->mem * 100 / cur->memmax);
5692 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5693 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5696 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5697 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5698 100, 1, obj->showaslog);
5703 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
5706 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
5709 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
5712 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5713 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
5716 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5717 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
5720 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5721 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
5725 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5727 OBJ(monitor_number) {
5728 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5734 update_mail_count(&obj->data.local_mail);
5735 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5738 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5740 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5743 update_mail_count(&obj->data.local_mail);
5744 snprintf(p, p_max_size, "%d",
5745 obj->data.local_mail.new_mail_count);
5748 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5751 new_outline(p, obj->data.l);
5754 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
5756 OBJ(running_processes) {
5757 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
5760 snprintf(p, p_max_size, "%s", obj->data.s);
5763 new_bg(p, obj->data.l);
5766 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5769 human_readable(cur->swap * 1024, p, 255, "swap");
5772 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
5775 if (cur->swapmax == 0) {
5776 strncpy(p, "No swap", p_max_size);
5778 spaced_print(p, p_max_size, "%*llu", 4, "swapperc",
5779 pad_percents, cur->swap * 100 / cur->swapmax);
5783 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5784 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5787 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5790 time_t t = time(NULL);
5791 struct tm *tm = localtime(&t);
5793 setlocale(LC_TIME, "");
5794 strftime(p, p_max_size, obj->data.s, tm);
5797 time_t t = time(NULL);
5798 struct tm *tm = gmtime(&t);
5800 strftime(p, p_max_size, obj->data.s, tm);
5807 if (obj->data.tztime.tz) {
5808 oldTZ = getenv("TZ");
5809 setenv("TZ", obj->data.tztime.tz, 1);
5815 setlocale(LC_TIME, "");
5816 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5818 setenv("TZ", oldTZ, 1);
5823 // Needless to free oldTZ since getenv gives ptr to static data
5826 human_readable(obj->data.net->recv, p, 255, "totaldown");
5829 human_readable(obj->data.net->trans, p, 255, "totalup");
5832 snprintf(p, p_max_size, "%d", total_updates);
5835 spaced_print(p, p_max_size, "%d", 6, "upspeed",
5836 round_to_int(obj->data.net->trans_speed / 1024));
5839 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
5840 obj->data.net->trans_speed / 1024.0);
5843 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5844 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
5847 format_seconds_short(p, p_max_size, (int) cur->uptime);
5850 format_seconds(p, p_max_size, (int) cur->uptime);
5853 snprintf(p, p_max_size, "%s", cur->users.names);
5856 snprintf(p, p_max_size, "%s", cur->users.terms);
5859 snprintf(p, p_max_size, "%s", cur->users.times);
5862 snprintf(p, p_max_size, "%d", cur->users.number);
5864 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5865 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5869 msg = get_apm_adapter();
5870 snprintf(p, p_max_size, "%s", msg);
5873 OBJ(apm_battery_life) {
5876 msg = get_apm_battery_life();
5877 snprintf(p, p_max_size, "%s", msg);
5880 OBJ(apm_battery_time) {
5883 msg = get_apm_battery_time();
5884 snprintf(p, p_max_size, "%s", msg);
5887 #endif /* __FreeBSD__ __OpenBSD__ */
5891 int len = obj->data.i;
5892 if (len == 0 || len > p_max_size)
5894 snprintf(p, len, "%s", cur->mpd.title);
5897 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5900 snprintf(p, p_max_size, "%s", cur->mpd.album);
5903 snprintf(p, p_max_size, "%s", cur->mpd.random);
5906 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
5909 snprintf(p, p_max_size, "%s", cur->mpd.track);
5912 snprintf(p, p_max_size, "%s", cur->mpd.name);
5915 snprintf(p, p_max_size, "%s", cur->mpd.file);
5918 snprintf(p, p_max_size, "%i", cur->mpd.volume);
5921 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
5924 snprintf(p, p_max_size, "%s", cur->mpd.status);
5927 format_media_player_time(p, p_max_size, cur->mpd.elapsed);
5930 format_media_player_time(p, p_max_size, cur->mpd.length);
5933 spaced_print(p, p_max_size, "%*d", 4, "mpd_percent",
5934 pad_percents, (int) (cur->mpd.progress * 100));
5937 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5938 (int) (cur->mpd.progress * 255.0f));
5941 int len = obj->data.i;
5942 if (len == 0 || len > p_max_size)
5945 memset(p, 0, p_max_size);
5946 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
5947 && *cur->mpd.title) {
5948 snprintf(p, len, "%s - %s", cur->mpd.artist,
5950 } else if (cur->mpd.title && *cur->mpd.title) {
5951 snprintf(p, len, "%s", cur->mpd.title);
5952 } else if (cur->mpd.artist && *cur->mpd.artist) {
5953 snprintf(p, len, "%s", cur->mpd.artist);
5954 } else if (cur->mpd.file && *cur->mpd.file) {
5955 snprintf(p, len, "%s", cur->mpd.file);
5960 OBJ(if_mpd_playing) {
5961 if (cur->mpd.is_playing) {
5964 i = obj->data.ifblock.pos;
5972 snprintf(p, p_max_size, "%s", (cur->moc.state ? cur->moc.state : "??"));
5975 snprintf(p, p_max_size, "%s", (cur->moc.file ? cur->moc.file : "no file"));
5978 snprintf(p, p_max_size, "%s", (cur->moc.title ? cur->moc.title : "no title"));
5981 snprintf(p, p_max_size, "%s", (cur->moc.artist ? cur->moc.artist : "no artist"));
5984 snprintf(p, p_max_size, "%s", (cur->moc.song ? cur->moc.song : "no song"));
5987 snprintf(p, p_max_size, "%s", (cur->moc.album ? cur->moc.album : "no album"));
5989 OBJ(moc_totaltime) {
5990 snprintf(p, p_max_size, "%s", (cur->moc.totaltime ? cur->moc.totaltime : "0:00"));
5993 snprintf(p, p_max_size, "%s", (cur->moc.timeleft ? cur->moc.timeleft : "0:00"));
5996 snprintf(p, p_max_size, "%s", (cur->moc.curtime ? cur->moc.curtime : "0:00"));
5999 snprintf(p, p_max_size, "%s", (cur->moc.bitrate ? cur->moc.bitrate : "0Kbps"));
6002 snprintf(p, p_max_size, "%s", (cur->moc.rate ? cur->moc.rate : "0KHz"));
6007 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
6010 snprintf(p, p_max_size, "%s", cur->xmms2.album);
6013 snprintf(p, p_max_size, "%s", cur->xmms2.title);
6016 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
6018 OBJ(xmms2_comment) {
6019 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
6022 snprintf(p, p_max_size, "%s", cur->xmms2.url);
6025 snprintf(p, p_max_size, "%s", cur->xmms2.status);
6028 snprintf(p, p_max_size, "%s", cur->xmms2.date);
6030 OBJ(xmms2_tracknr) {
6031 if (cur->xmms2.tracknr != -1) {
6032 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
6035 OBJ(xmms2_bitrate) {
6036 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
6039 snprintf(p, p_max_size, "%u", cur->xmms2.id);
6042 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
6044 OBJ(xmms2_elapsed) {
6045 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
6046 (cur->xmms2.elapsed / 1000) % 60);
6048 OBJ(xmms2_duration) {
6049 snprintf(p, p_max_size, "%02d:%02d",
6050 cur->xmms2.duration / 60000,
6051 (cur->xmms2.duration / 1000) % 60);
6053 OBJ(xmms2_percent) {
6054 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
6057 new_bar(p, obj->data.pair.a, obj->data.pair.b,
6058 (int) (cur->xmms2.progress * 255.0f));
6060 OBJ(xmms2_playlist) {
6061 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
6063 OBJ(xmms2_timesplayed) {
6064 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
6067 if (strlen(cur->xmms2.title) < 2
6068 && strlen(cur->xmms2.title) < 2) {
6069 snprintf(p, p_max_size, "%s", cur->xmms2.url);
6071 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
6077 OBJ(audacious_status) {
6078 snprintf(p, p_max_size, "%s",
6079 cur->audacious.items[AUDACIOUS_STATUS]);
6081 OBJ(audacious_title) {
6082 snprintf(p, cur->audacious.max_title_len > 0
6083 ? cur->audacious.max_title_len : p_max_size, "%s",
6084 cur->audacious.items[AUDACIOUS_TITLE]);
6086 OBJ(audacious_length) {
6087 snprintf(p, p_max_size, "%s",
6088 cur->audacious.items[AUDACIOUS_LENGTH]);
6090 OBJ(audacious_length_seconds) {
6091 snprintf(p, p_max_size, "%s",
6092 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
6094 OBJ(audacious_position) {
6095 snprintf(p, p_max_size, "%s",
6096 cur->audacious.items[AUDACIOUS_POSITION]);
6098 OBJ(audacious_position_seconds) {
6099 snprintf(p, p_max_size, "%s",
6100 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
6102 OBJ(audacious_bitrate) {
6103 snprintf(p, p_max_size, "%s",
6104 cur->audacious.items[AUDACIOUS_BITRATE]);
6106 OBJ(audacious_frequency) {
6107 snprintf(p, p_max_size, "%s",
6108 cur->audacious.items[AUDACIOUS_FREQUENCY]);
6110 OBJ(audacious_channels) {
6111 snprintf(p, p_max_size, "%s",
6112 cur->audacious.items[AUDACIOUS_CHANNELS]);
6114 OBJ(audacious_filename) {
6115 snprintf(p, p_max_size, "%s",
6116 cur->audacious.items[AUDACIOUS_FILENAME]);
6118 OBJ(audacious_playlist_length) {
6119 snprintf(p, p_max_size, "%s",
6120 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
6122 OBJ(audacious_playlist_position) {
6123 snprintf(p, p_max_size, "%s",
6124 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
6126 OBJ(audacious_bar) {
6130 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
6131 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
6132 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
6138 snprintf(p, p_max_size, "%s", cur->bmpx.title);
6141 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
6144 snprintf(p, p_max_size, "%s", cur->bmpx.album);
6147 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
6150 snprintf(p, p_max_size, "%i", cur->bmpx.track);
6153 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
6157 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
6160 switch (obj->data.top.type) {
6162 snprintf(p, 16, "%-15s",
6163 cur->cpu[obj->data.top.num]->name);
6166 snprintf(p, 7, "%6.2f",
6167 cur->cpu[obj->data.top.num]->amount);
6170 snprintf(p, 6, "%5i",
6171 cur->cpu[obj->data.top.num]->pid);
6174 snprintf(p, 7, "%6.2f",
6175 cur->cpu[obj->data.top.num]->totalmem);
6178 timeval = format_time(
6179 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
6180 snprintf(p, 10, "%9s", timeval);
6184 human_readable(cur->cpu[obj->data.top.num]->rss,
6188 human_readable(cur->cpu[obj->data.top.num]->vsize,
6192 ERR("Unhandled top data type: %d\n",
6193 obj->data.top.type);
6196 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
6200 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
6203 switch (obj->data.top.type) {
6205 snprintf(p, 16, "%-15s",
6206 cur->memu[obj->data.top.num]->name);
6209 snprintf(p, 7, "%6.2f",
6210 cur->memu[obj->data.top.num]->amount);
6213 snprintf(p, 6, "%5i",
6214 cur->memu[obj->data.top.num]->pid);
6217 snprintf(p, 7, "%6.2f",
6218 cur->memu[obj->data.top.num]->totalmem);
6221 timeval = format_time(
6222 cur->memu[obj->data.top.num]->total_cpu_time,
6224 snprintf(p, 10, "%9s", timeval);
6228 human_readable(cur->cpu[obj->data.top.num]->rss,
6232 human_readable(cur->cpu[obj->data.top.num]->vsize,
6236 ERR("Unhandled top data type: %d\n",
6237 obj->data.top.type);
6240 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
6244 if (current_update_time - obj->data.tail.last_update
6245 < obj->data.tail.interval) {
6246 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
6252 obj->data.tail.last_update = current_update_time;
6254 if (obj->data.tail.fd != -1) {
6255 tail_pipe(obj, p, p_max_size);
6259 fp = fopen(obj->data.tail.logfile, "rt");
6262 /* Send one message, but do not consistently spam
6263 * on missing logfiles. */
6264 if (obj->data.tail.readlines != 0) {
6265 ERR("tail logfile failed to open");
6266 strcpy(obj->data.tail.buffer, "Logfile Missing");
6268 obj->data.tail.readlines = 0;
6269 snprintf(p, p_max_size, "Logfile Missing");
6271 obj->data.tail.readlines = 0;
6272 /* -1 instead of 0 to avoid counting a trailing
6274 fseek(fp, -1, SEEK_END);
6275 bsize = ftell(fp) + 1;
6276 for (iter = obj->data.tail.wantedlines; iter > 0;
6278 nl = rev_fcharfind(fp, '\n', iter);
6283 obj->data.tail.readlines = iter;
6284 if (obj->data.tail.readlines
6285 < obj->data.tail.wantedlines) {
6286 fseek(fp, 0, SEEK_SET);
6288 fseek(fp, nl + 1, SEEK_SET);
6291 /* Make sure bsize is at least 1 byte smaller than the
6292 * buffer max size. */
6293 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
6294 fseek(fp, bsize - text_buffer_size * 20 - 1,
6296 bsize = text_buffer_size * 20 - 1;
6298 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
6301 /* Clean up trailing newline, make sure the
6302 * buffer is null terminated. */
6303 if (obj->data.tail.buffer[bsize - 1] == '\n') {
6304 obj->data.tail.buffer[bsize - 1] = '\0';
6306 obj->data.tail.buffer[bsize] = '\0';
6308 snprintf(p, p_max_size, "%s",
6309 obj->data.tail.buffer);
6311 strcpy(obj->data.tail.buffer, "Logfile Empty");
6312 snprintf(p, p_max_size, "Logfile Empty");
6315 } /* if cur_upd_time >= */
6316 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6320 if (current_update_time - obj->data.tail.last_update
6321 < obj->data.tail.interval) {
6322 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
6328 obj->data.tail.last_update = current_update_time;
6330 fp = fopen(obj->data.tail.logfile, "rt");
6332 /* Send one message, but do not consistently spam
6333 * on missing logfiles. */
6334 if (obj->data.tail.readlines != 0) {
6335 ERR("head logfile failed to open");
6336 strcpy(obj->data.tail.buffer, "Logfile Missing");
6338 obj->data.tail.readlines = 0;
6339 snprintf(p, p_max_size, "Logfile Missing");
6341 obj->data.tail.readlines = 0;
6342 for (iter = obj->data.tail.wantedlines; iter > 0;
6344 nl = fwd_fcharfind(fp, '\n', iter);
6349 obj->data.tail.readlines = iter;
6350 /* Make sure nl is at least 1 byte smaller than the
6351 * buffer max size. */
6352 if (nl > (long) ((text_buffer_size * 20) - 1)) {
6353 nl = text_buffer_size * 20 - 1;
6355 nl = fread(obj->data.tail.buffer, 1, nl, fp);
6358 /* Clean up trailing newline, make sure the buffer
6359 * is null terminated. */
6360 if (obj->data.tail.buffer[nl - 1] == '\n') {
6361 obj->data.tail.buffer[nl - 1] = '\0';
6363 obj->data.tail.buffer[nl] = '\0';
6365 snprintf(p, p_max_size, "%s",
6366 obj->data.tail.buffer);
6368 strcpy(obj->data.tail.buffer, "Logfile Empty");
6369 snprintf(p, p_max_size, "Logfile Empty");
6371 } /* if fp == null */
6372 } /* cur_upd_time >= */
6373 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6377 FILE *fp = fopen(obj->data.s,"r");
6384 while(fgets(buf, BUFSZ, fp) != NULL){
6385 for(j = 0; buf[j] != 0; j++) {
6386 if(buf[j] == '\n') {
6391 sprintf(p, "%d", lines);
6394 sprintf(p, "File Unreadable");
6399 FILE *fp = fopen(obj->data.s,"r");
6404 char inword = FALSE;
6407 while(fgets(buf, BUFSZ, fp) != NULL){
6408 for(j = 0; buf[j] != 0; j++) {
6409 if(!isspace(buf[j])) {
6410 if(inword == FALSE) {
6419 sprintf(p, "%d", words);
6422 sprintf(p, "File Unreadable");
6425 #ifdef TCP_PORT_MONITOR
6427 /* grab a pointer to this port monitor */
6428 tcp_port_monitor_t *p_monitor =
6429 find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
6430 obj->data.tcp_port_monitor.port_range_begin,
6431 obj->data.tcp_port_monitor.port_range_end);
6434 snprintf(p, p_max_size, "monitor not found");
6438 /* now grab the text of interest */
6439 if (peek_tcp_port_monitor(p_monitor,
6440 obj->data.tcp_port_monitor.item,
6441 obj->data.tcp_port_monitor.connection_index, p,
6443 snprintf(p, p_max_size, "monitor peek error");
6451 iconv_converting = 1;
6452 iconv_selected = obj->a;
6455 iconv_converting = 0;
6460 OBJ(entropy_avail) {
6461 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
6463 OBJ(entropy_poolsize) {
6464 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
6467 double entropy_perc;
6469 entropy_perc = (double) cur->entropy.entropy_avail /
6470 (double) cur->entropy.poolsize;
6471 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
6477 s = smapi_get_val(obj->data.s);
6478 snprintf(p, p_max_size, "%s", s);
6482 OBJ(if_smapi_bat_installed) {
6484 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
6485 if(!smapi_bat_installed(idx)) {
6486 i = obj->data.ifblock.pos;
6491 ERR("argument to if_smapi_bat_installed must be an integer");
6493 OBJ(smapi_bat_perc) {
6495 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6496 val = smapi_bat_installed(idx) ?
6497 smapi_get_bat_int(idx, "remaining_percent") : 0;
6498 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
6500 ERR("argument to smapi_bat_perc must be an integer");
6502 OBJ(smapi_bat_temp) {
6504 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6505 val = smapi_bat_installed(idx) ?
6506 smapi_get_bat_int(idx, "temperature") : 0;
6507 /* temperature is in milli degree celsius, round to degree celsius */
6508 snprintf(p, p_max_size, "%d", (int)((val / 1000) + 0.5));
6510 ERR("argument to smapi_bat_temp must be an integer");
6512 OBJ(smapi_bat_power) {
6514 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6515 val = smapi_bat_installed(idx) ?
6516 smapi_get_bat_int(idx, "power_now") : 0;
6517 /* power_now is in mW, set to W with one digit precision */
6518 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
6520 ERR("argument to smapi_bat_power must be an integer");
6522 OBJ(smapi_bat_bar) {
6523 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
6524 new_bar(p, obj->a, obj->b, (int)
6525 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
6527 new_bar(p, obj->a, obj->b, 0);
6533 parse_conky_vars(obj->data.scroll.text, p, cur);
6535 if (strlen(p) <= obj->data.scroll.show) {
6538 #define LINESEPARATOR '|'
6539 //place all the lines behind each other with LINESEPARATOR between them
6540 for(j = 0; p[j] != 0; j++) {
6545 //scroll the output obj->data.scroll.start places by copying that many chars from
6546 //the front of the string to tmp, scrolling the rest to the front and placing tmp
6547 //at the back of the string
6548 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
6549 strncpy(tmp, p, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
6550 for(j = obj->data.scroll.start; p[j] != 0; j++){
6551 p[j - obj->data.scroll.start] = p[j];
6553 strcpy(&p[j - obj->data.scroll.start], tmp);
6555 //only show the requested number of chars
6556 if(obj->data.scroll.show < j) {
6557 p[obj->data.scroll.show] = 0;
6559 //next time, scroll a place more or reset scrolling if we are at the end
6560 obj->data.scroll.start += obj->data.scroll.step;
6561 if(obj->data.scroll.start >= j){
6562 obj->data.scroll.start = 0;
6567 int value = get_nvidia_value(obj->data.nvidia.type, display);
6569 snprintf(p, p_max_size, "N/A");
6570 else if (obj->data.nvidia.print_as_float &&
6571 value > 0 && value < 100)
6572 snprintf(p, p_max_size, "%.1f", (float)value);
6574 snprintf(p, p_max_size, "%d", value);
6582 unsigned int a = strlen(p);
6585 if (a > 0 && iconv_converting && iconv_selected > 0
6586 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6588 size_t dummy1, dummy2;
6589 char *ptr = buff_in;
6592 dummy1 = dummy2 = a;
6594 strncpy(buff_in, p, p_max_size);
6596 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6597 while (dummy1 > 0) {
6598 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6601 ERR("Iconv codeset conversion failed");
6606 /* It is nessecary when we are converting from multibyte to
6607 * singlebyte codepage */
6610 #endif /* HAVE_ICONV */
6617 double current_update_time, next_update_time, last_update_time;
6619 static void generate_text(void)
6621 struct information *cur = &info;
6628 current_update_time = get_time();
6631 /* fix diskio rates to b/s (use update_interval */
6632 info.diskio_read_value /= update_interval;
6633 info.diskio_write_value /= update_interval;
6634 info.diskio_value /= update_interval;
6636 /* add things to the buffer */
6642 generate_text_internal(p, max_user_text, global_text_object_list, cur);
6644 if (stuff_in_upper_case) {
6647 tmp_p = text_buffer;
6649 *tmp_p = toupper(*tmp_p);
6654 next_update_time += update_interval;
6655 if (next_update_time < get_time()) {
6656 next_update_time = get_time() + update_interval;
6657 } else if (next_update_time > get_time() + update_interval) {
6658 next_update_time = get_time() + update_interval;
6660 last_update_time = current_update_time;
6666 static void set_font(void)
6670 if (window.xftdraw != NULL) {
6671 XftDrawDestroy(window.xftdraw);
6673 window.xftdraw = XftDrawCreate(display, window.drawable,
6674 DefaultVisual(display, screen), DefaultColormap(display, screen));
6678 XSetFont(display, window.gc, fonts[selected_font].font->fid);
6684 static inline int get_string_width(const char *s)
6687 return *s ? calc_text_width(s, strlen(s)) : 0;
6693 static inline int get_string_width_special(char *s)
6705 p = strndup(s, text_buffer_size);
6709 if (*p == SPECIAL_CHAR) {
6710 /* shift everything over by 1 so that the special char
6711 * doesn't mess up the size calculation */
6712 for (i = 0; i < strlen(p); i++) {
6713 *(p + i) = *(p + i + 1);
6715 if (specials[special_index + idx].type == GRAPH
6716 || specials[special_index + idx].type == BAR) {
6717 width += specials[special_index + idx].width;
6724 if (strlen(final) > 1) {
6725 width += calc_text_width(final, strlen(final));
6730 return (s) ? strlen(s) : 0;
6735 static void text_size_updater(char *s);
6737 int last_font_height;
6738 static void update_text_area(void)
6742 /* update text size if it isn't fixed */
6747 text_width = minimum_width;
6750 last_font_height = font_height();
6751 for_each_line(text_buffer, text_size_updater);
6753 if (text_height < minimum_height) {
6754 text_height = minimum_height;
6756 if (text_width > maximum_width && maximum_width > 0) {
6757 text_width = maximum_width;
6761 /* get text position on workarea */
6762 switch (text_alignment) {
6769 x = workarea[2] - text_width - gap_x;
6774 x = workarea[2] / 2 - text_width / 2 - gap_x;
6781 y = workarea[3] - text_height - gap_y;
6785 x = workarea[2] - text_width - gap_x;
6786 y = workarea[3] - text_height - gap_y;
6790 x = workarea[2] / 2 - text_width / 2 - gap_x;
6791 y = workarea[3] - text_height - gap_y;
6796 y = workarea[3] / 2 - text_height / 2 - gap_y;
6800 x = workarea[2] - text_width - gap_x;
6801 y = workarea[3] / 2 - text_height / 2 - gap_y;
6805 case NONE: // Let the WM manage the window
6816 if (own_window && !fixed_pos) {
6819 text_start_x = border_margin + 1;
6820 text_start_y = border_margin + 1;
6821 window.x = x - border_margin - 1;
6822 window.y = y - border_margin - 1;
6826 /* If window size doesn't match to workarea's size,
6827 * then window probably includes panels (gnome).
6828 * Blah, doesn't work on KDE. */
6829 if (workarea[2] != window.width || workarea[3] != window.height) {
6841 static int cur_x, cur_y; /* current x and y for drawing */
6843 //draw_mode also without X11 because we only need to print to stdout with FG
6844 static int draw_mode; /* FG, BG or OUTLINE */
6846 static long current_color;
6849 static void text_size_updater(char *s)
6854 /* get string widths and skip specials */
6857 if (*p == SPECIAL_CHAR) {
6859 w += get_string_width(s);
6862 if (specials[special_index].type == BAR
6863 || specials[special_index].type == GRAPH) {
6864 w += specials[special_index].width;
6865 if (specials[special_index].height > last_font_height) {
6866 last_font_height = specials[special_index].height;
6867 last_font_height += font_ascent();
6869 } else if (specials[special_index].type == OFFSET) {
6870 if (specials[special_index].arg > 0) {
6871 w += specials[special_index].arg;
6873 } else if (specials[special_index].type == VOFFSET) {
6874 last_font_height += specials[special_index].arg;
6875 } else if (specials[special_index].type == GOTO) {
6876 if (specials[special_index].arg > cur_x) {
6877 w = (int) specials[special_index].arg;
6879 } else if (specials[special_index].type == TAB) {
6880 int start = specials[special_index].arg;
6881 int step = specials[special_index].width;
6883 if (!step || step < 0) {
6886 w += step - (cur_x - text_start_x - start) % step;
6887 } else if (specials[special_index].type == FONT) {
6888 selected_font = specials[special_index].font_added;
6889 if (font_height() > last_font_height) {
6890 last_font_height = font_height();
6899 w += get_string_width(s);
6900 if (w > text_width) {
6903 if (text_width > maximum_width && maximum_width) {
6904 text_width = maximum_width;
6907 text_height += last_font_height;
6908 last_font_height = font_height();
6912 static inline void set_foreground_color(long c)
6915 XSetForeground(display, window.gc, c);
6919 static void draw_string(const char *s)
6921 int i, i2, pos, width_of_s;
6929 width_of_s = get_string_width(s);
6930 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6932 fflush(stdout); /* output immediately, don't buffer */
6934 memset(tmpstring1, 0, text_buffer_size);
6935 memset(tmpstring2, 0, text_buffer_size);
6936 strncpy(tmpstring1, s, text_buffer_size - 1);
6941 max = ((text_width - width_of_s) / get_string_width(" "));
6943 /* This code looks for tabs in the text and coverts them to spaces.
6944 * The trick is getting the correct number of spaces, and not going
6945 * over the window's size without forcing the window larger. */
6946 for (i = 0; i < (int) text_buffer_size; i++) {
6947 if (tmpstring1[i] == '\t') {
6949 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6950 /* guard against overrun */
6951 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6956 /* guard against overrun */
6957 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6962 if (text_width == maximum_width) {
6963 /* this means the text is probably pushing the limit,
6964 * so we'll chop it */
6965 while (cur_x + get_string_width(tmpstring2) - text_start_x
6966 > maximum_width && strlen(tmpstring2) > 0) {
6967 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6978 c.pixel = current_color;
6979 XQueryColor(display, DefaultColormap(display, screen), &c);
6982 c2.color.red = c.red;
6983 c2.color.green = c.green;
6984 c2.color.blue = c.blue;
6985 c2.color.alpha = fonts[selected_font].font_alpha;
6987 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6988 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6990 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6991 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6996 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6999 cur_x += width_of_s;
7001 memcpy(tmpstring1, s, text_buffer_size);
7004 long redmask, greenmask, bluemask;
7006 void set_up_gradient(void)
7010 colour_depth = DisplayPlanes(display, screen);
7014 if (colour_depth != 24 && colour_depth != 16) {
7015 ERR("using non-standard colour depth, gradients may look like a "
7022 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
7024 greenmask |= 1 << i;
7027 if (colour_depth % 3 == 1) {
7028 greenmask |= 1 << (colour_depth / 3);
7030 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
7031 greenmask = greenmask << (colour_depth / 3);
7034 /* this function returns the next colour between two colours for a gradient */
7035 unsigned long do_gradient(unsigned long first_colour,
7036 unsigned long last_colour)
7039 int red1, green1, blue1; // first colour
7040 int red2, green2, blue2; // second colour
7041 int red3 = 0, green3 = 0, blue3 = 0; // difference
7042 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
7043 short greenshift = (colour_depth / 3);
7045 red1 = (first_colour & redmask) >> redshift;
7046 green1 = (first_colour & greenmask) >> greenshift;
7047 blue1 = first_colour & bluemask;
7048 red2 = (last_colour & redmask) >> redshift;
7049 green2 = (last_colour & greenmask) >> greenshift;
7050 blue2 = last_colour & bluemask;
7057 if (green1 > green2) {
7060 if (green1 < green2) {
7063 if (blue1 > blue2) {
7066 if (blue1 < blue2) {
7081 if (red1 > bluemask) {
7084 if (green1 > bluemask) {
7087 if (blue1 > bluemask) {
7090 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
7094 /* this function returns the max diff for a gradient */
7095 unsigned long gradient_max(unsigned long first_colour,
7096 unsigned long last_colour)
7098 int red1, green1, blue1; // first colour
7099 int red2, green2, blue2; // second colour
7100 int red3 = 0, green3 = 0, blue3 = 0; // difference
7101 long redshift, greenshift;
7104 if (colour_depth == 0) {
7107 redshift = (2 * colour_depth / 3 + colour_depth % 3);
7108 greenshift = (colour_depth / 3);
7110 red1 = (first_colour & redmask) >> redshift;
7111 green1 = (first_colour & greenmask) >> greenshift;
7112 blue1 = first_colour & bluemask;
7113 red2 = (last_colour & redmask) >> redshift;
7114 green2 = (last_colour & greenmask) >> greenshift;
7115 blue2 = last_colour & bluemask;
7116 red3 = abs(red1 - red2);
7117 green3 = abs(green1 - green2);
7118 blue3 = abs(blue1 - blue2);
7130 static void draw_line(char *s)
7138 cur_x = text_start_x;
7139 cur_y += font_ascent();
7140 font_h = font_height();
7142 /* find specials and draw stuff */
7145 if (*p == SPECIAL_CHAR) {
7148 /* draw string before special */
7155 switch (specials[special_index].type) {
7156 case HORIZONTAL_LINE:
7158 int h = specials[special_index].height;
7159 int mid = font_ascent() / 2;
7161 w = text_start_x + text_width - cur_x;
7163 XSetLineAttributes(display, window.gc, h, LineSolid,
7164 CapButt, JoinMiter);
7165 XDrawLine(display, window.drawable, window.gc, cur_x,
7166 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
7172 int h = specials[special_index].height;
7173 int tmp_s = specials[special_index].arg;
7174 int mid = font_ascent() / 2;
7175 char ss[2] = { tmp_s, tmp_s };
7177 w = text_start_x + text_width - cur_x - 1;
7178 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
7179 CapButt, JoinMiter);
7180 XSetDashes(display, window.gc, 0, ss, 2);
7181 XDrawLine(display, window.drawable, window.gc, cur_x,
7182 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
7188 int h, bar_usage, by;
7189 if (cur_x - text_start_x > maximum_width
7190 && maximum_width > 0) {
7193 h = specials[special_index].height;
7194 bar_usage = specials[special_index].arg;
7195 by = cur_y - (font_ascent() / 2) - 1;
7197 if (h < font_height()) {
7200 w = specials[special_index].width;
7202 w = text_start_x + text_width - cur_x - 1;
7208 XSetLineAttributes(display, window.gc, 1, LineSolid,
7209 CapButt, JoinMiter);
7211 XDrawRectangle(display, window.drawable, window.gc, cur_x,
7213 XFillRectangle(display, window.drawable, window.gc, cur_x,
7214 by, w * bar_usage / 255, h);
7215 if (specials[special_index].height > cur_y_add
7216 && specials[special_index].height > font_h) {
7217 cur_y_add = specials[special_index].height;
7224 int h, by, i, j = 0;
7225 int gradient_size = 0;
7226 float gradient_factor = 0;
7227 float gradient_update = 0;
7228 unsigned long last_colour = current_color;
7229 unsigned long tmpcolour = current_color;
7230 if (cur_x - text_start_x > maximum_width
7231 && maximum_width > 0) {
7234 h = specials[special_index].height;
7235 by = cur_y - (font_ascent() / 2) - 1;
7237 if (h < font_height()) {
7240 w = specials[special_index].width;
7242 w = text_start_x + text_width - cur_x - 1;
7247 if (draw_graph_borders) {
7248 XSetLineAttributes(display, window.gc, 1, LineSolid,
7249 CapButt, JoinMiter);
7250 XDrawRectangle(display, window.drawable, window.gc,
7253 XSetLineAttributes(display, window.gc, 1, LineSolid,
7254 CapButt, JoinMiter);
7256 if (specials[special_index].last_colour != 0
7257 || specials[special_index].first_colour != 0) {
7258 tmpcolour = specials[special_index].last_colour;
7260 gradient_max(specials[special_index].last_colour,
7261 specials[special_index].first_colour);
7262 gradient_factor = (float) gradient_size / (w - 2);
7264 for (i = w - 2; i > -1; i--) {
7265 if (specials[special_index].last_colour != 0
7266 || specials[special_index].first_colour != 0) {
7267 XSetForeground(display, window.gc, tmpcolour);
7268 gradient_update += gradient_factor;
7269 while (gradient_update > 0) {
7270 tmpcolour = do_gradient(tmpcolour,
7271 specials[special_index].first_colour);
7275 if ((w - i) / ((float) (w - 2) /
7276 (specials[special_index].graph_width)) > j
7277 && j < MAX_GRAPH_DEPTH - 3) {
7280 /* this is mugfugly, but it works */
7281 XDrawLine(display, window.drawable, window.gc,
7282 cur_x + i + 1, by + h, cur_x + i + 1,
7283 by + h - specials[special_index].graph[j] *
7284 (h - 1) / specials[special_index].graph_scale);
7286 if (specials[special_index].height > cur_y_add
7287 && specials[special_index].height > font_h) {
7288 cur_y_add = specials[special_index].height;
7290 /* if (draw_mode == BG) {
7291 set_foreground_color(default_bg_color);
7292 } else if (draw_mode == OUTLINE) {
7293 set_foreground_color(default_out_color);
7295 set_foreground_color(default_fg_color);
7297 if (show_graph_range) {
7300 unsigned short int seconds = update_interval * w;
7305 unsigned short int timeunits;
7307 timeunits = seconds / 86400; seconds %= 86400;
7308 if( timeunits > 0 ) {
7309 asprintf(&tmp_day_str, "%dd", timeunits);
7311 tmp_day_str = strdup("");
7313 timeunits = seconds / 3600; seconds %= 3600;
7314 if( timeunits > 0 ) {
7315 asprintf(&tmp_hour_str, "%dh", timeunits);
7317 tmp_hour_str = strdup("");
7319 timeunits = seconds / 60; seconds %= 60;
7321 asprintf(&tmp_min_str, "%dm", timeunits);
7323 tmp_min_str = strdup("");
7326 asprintf(&tmp_sec_str, "%ds", seconds);
7328 tmp_sec_str = strdup("");
7330 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
7331 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
7333 asprintf(&tmp_str, "Range not possible"); //should never happen, but better safe then sorry
7335 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
7336 cur_y += font_height() / 2;
7337 draw_string(tmp_str);
7343 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
7346 cur_x += font_ascent() / 2;
7347 cur_y += font_height() / 2;
7349 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
7351 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
7352 draw_string(tmp_str);
7358 set_foreground_color(last_colour);
7364 int old = font_ascent();
7366 cur_y -= font_ascent();
7367 selected_font = specials[special_index].font_added;
7368 if (cur_y + font_ascent() < cur_y + old) {
7371 cur_y += font_ascent();
7377 if (draw_mode == FG) {
7378 set_foreground_color(specials[special_index].arg);
7383 if (draw_mode == BG) {
7384 set_foreground_color(specials[special_index].arg);
7389 if (draw_mode == OUTLINE) {
7390 set_foreground_color(specials[special_index].arg);
7395 w += specials[special_index].arg;
7399 cur_y += specials[special_index].arg;
7403 if (specials[special_index].arg >= 0) {
7404 cur_x = (int) specials[special_index].arg;
7410 int start = specials[special_index].arg;
7411 int step = specials[special_index].width;
7413 if (!step || step < 0) {
7416 w = step - (cur_x - text_start_x - start) % step;
7422 /* TODO: add back in "+ border_margin" to the end of
7424 int pos_x = text_start_x + text_width -
7425 get_string_width_special(s);
7427 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7428 "get_string_width(p) %i gap_x %i "
7429 "specials[special_index].arg %i border_margin %i "
7430 "border_width %i\n", pos_x, text_start_x, text_width,
7431 cur_x, get_string_width_special(s), gap_x,
7432 specials[special_index].arg, border_margin,
7434 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
7435 cur_x = pos_x - specials[special_index].arg;
7442 int pos_x = (text_width) / 2 - get_string_width_special(s) /
7443 2 - (cur_x - text_start_x);
7444 /* int pos_x = text_start_x + text_width / 2 -
7445 get_string_width_special(s) / 2; */
7447 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7448 "get_string_width(p) %i gap_x %i "
7449 "specials[special_index].arg %i\n", pos_x, text_start_x,
7450 text_width, cur_x, get_string_width(s), gap_x,
7451 specials[special_index].arg); */
7452 if (pos_x > specials[special_index].arg) {
7453 w = pos_x - specials[special_index].arg;
7470 if (cur_y_add > 0) {
7472 cur_y -= font_descent();
7477 cur_y += font_descent();
7481 static void draw_text(void)
7484 cur_y = text_start_y;
7487 if (draw_borders && border_width > 0) {
7488 unsigned int b = (border_width + 1) / 2;
7490 if (stippled_borders) {
7491 char ss[2] = { stippled_borders, stippled_borders };
7492 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
7493 CapButt, JoinMiter);
7494 XSetDashes(display, window.gc, 0, ss, 2);
7496 XSetLineAttributes(display, window.gc, border_width, LineSolid,
7497 CapButt, JoinMiter);
7500 XDrawRectangle(display, window.drawable, window.gc,
7501 text_start_x - border_margin + b, text_start_y - border_margin + b,
7502 text_width + border_margin * 2 - 1 - b * 2,
7503 text_height + border_margin * 2 - 1 - b * 2);
7509 for_each_line(text_buffer, draw_line);
7512 static void draw_stuff(void)
7516 if (draw_shades && !draw_outline) {
7519 set_foreground_color(default_bg_color);
7530 for (i = -1; i < 2; i++) {
7531 for (j = -1; j < 2; j++) {
7532 if (i == 0 && j == 0) {
7537 set_foreground_color(default_out_color);
7538 draw_mode = OUTLINE;
7546 set_foreground_color(default_fg_color);
7555 swap.swap_window = window.window;
7556 swap.swap_action = XdbeBackground;
7557 XdbeSwapBuffers(display, &swap, 1);
7564 static void clear_text(int exposures)
7568 /* The swap action is XdbeBackground, which clears */
7573 /* there is some extra space for borders and outlines */
7574 XClearArea(display, window.window, text_start_x - border_margin - 1,
7575 text_start_y - border_margin - 1,
7576 text_width + border_margin * 2 + 2,
7577 text_height + border_margin * 2 + 2, exposures ? True : 0);
7582 static int need_to_update;
7584 /* update_text() generates new text and clears old text area */
7585 static void update_text(void)
7594 static void main_loop(void)
7596 #ifdef SIGNAL_BLOCKING
7597 sigset_t newmask, oldmask;
7601 Region region = XCreateRegion();
7605 XserverRegion region2, part;
7606 int event_base, error_base;
7608 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
7609 ERR("Xdamage extension unavailable");
7611 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7612 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7613 part = XFixesCreateRegionFromWindow(display, window.window, 0);
7614 #endif /* HAVE_XDAMAGE */
7617 #ifdef SIGNAL_BLOCKING
7618 sigemptyset(&newmask);
7619 sigaddset(&newmask, SIGINT);
7620 sigaddset(&newmask, SIGTERM);
7621 sigaddset(&newmask, SIGUSR1);
7624 next_update_time = last_update_time = get_time();
7626 while (total_run_times == 0 || info.looped < total_run_times) {
7629 #ifdef SIGNAL_BLOCKING
7630 /* block signals. we will inspect for pending signals later */
7631 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7632 CRIT_ERR("unable to sigprocmask()");
7639 /* wait for X event or timeout */
7641 if (!XPending(display)) {
7645 t = next_update_time - get_time();
7649 } else if (t > update_interval) {
7650 t = update_interval;
7653 tv.tv_sec = (long) t;
7654 tv.tv_usec = (long) (t * 1000000) % 1000000;
7656 FD_SET(ConnectionNumber(display), &fdsr);
7658 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7660 if (errno != EINTR) {
7661 ERR("can't select(): %s", strerror(errno));
7667 t = (next_update_time - get_time()) * 1000000;
7668 usleep((useconds_t)t);
7676 if (need_to_update) {
7678 int wx = window.x, wy = window.y;
7686 /* resize window if it isn't right size */
7688 && (text_width + border_margin * 2 + 1 != window.width
7689 || text_height + border_margin * 2 + 1 != window.height)) {
7690 window.width = text_width + border_margin * 2 + 1;
7691 window.height = text_height + border_margin * 2 + 1;
7692 XResizeWindow(display, window.window, window.width,
7695 set_transparent_background(window.window);
7699 /* move window if it isn't in right position */
7700 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7701 XMoveWindow(display, window.window, window.x, window.y);
7712 r.x = text_start_x - border_margin;
7713 r.y = text_start_y - border_margin;
7714 r.width = text_width + border_margin * 2;
7715 r.height = text_height + border_margin * 2;
7716 XUnionRectWithRegion(&r, region, region);
7721 /* handle X events */
7722 while (XPending(display)) {
7725 XNextEvent(display, &ev);
7733 r.width = ev.xexpose.width;
7734 r.height = ev.xexpose.height;
7735 XUnionRectWithRegion(&r, region, region);
7740 case ReparentNotify:
7741 /* set background to ParentRelative for all parents */
7743 set_transparent_background(window.window);
7747 case ConfigureNotify:
7749 /* if window size isn't what expected, set fixed size */
7750 if (ev.xconfigure.width != window.width
7751 || ev.xconfigure.height != window.height) {
7752 if (window.width != 0 && window.height != 0) {
7756 /* clear old stuff before screwing up
7761 XWindowAttributes attrs;
7763 if (XGetWindowAttributes(display,
7764 window.window, &attrs)) {
7765 window.width = attrs.width;
7766 window.height = attrs.height;
7770 text_width = window.width - border_margin * 2 - 1;
7771 text_height = window.height - border_margin * 2 - 1;
7772 if (text_width > maximum_width
7773 && maximum_width > 0) {
7774 text_width = maximum_width;
7778 /* if position isn't what expected, set fixed pos
7779 * total_updates avoids setting fixed_pos when window
7780 * is set to weird locations when started */
7781 /* // this is broken
7782 if (total_updates >= 2 && !fixed_pos
7783 && (window.x != ev.xconfigure.x
7784 || window.y != ev.xconfigure.y)
7785 && (ev.xconfigure.x != 0
7786 || ev.xconfigure.y != 0)) {
7795 /* if an ordinary window with decorations */
7796 if ((window.type == TYPE_NORMAL)
7797 && (!TEST_HINT(window.hints,
7798 HINT_UNDECORATED))) {
7799 /* allow conky to hold input focus. */
7802 /* forward the click to the desktop window */
7803 XUngrabPointer(display, ev.xbutton.time);
7804 ev.xbutton.window = window.desktop;
7805 XSendEvent(display, ev.xbutton.window, False,
7806 ButtonPressMask, &ev);
7807 XSetInputFocus(display, ev.xbutton.window,
7808 RevertToParent, ev.xbutton.time);
7815 /* if an ordinary window with decorations */
7816 if ((window.type == TYPE_NORMAL)
7817 && (!TEST_HINT(window.hints,
7818 HINT_UNDECORATED))) {
7819 /* allow conky to hold input focus. */
7822 /* forward the release to the desktop window */
7823 ev.xbutton.window = window.desktop;
7824 XSendEvent(display, ev.xbutton.window, False,
7825 ButtonReleaseMask, &ev);
7834 if (ev.type == event_base + XDamageNotify) {
7835 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7837 XFixesSetRegion(display, part, &dev->area, 1);
7838 XFixesUnionRegion(display, region2, region2, part);
7840 #endif /* HAVE_XDAMAGE */
7846 XDamageSubtract(display, damage, region2, None);
7847 XFixesSetRegion(display, region2, 0, 0);
7848 #endif /* HAVE_XDAMAGE */
7850 /* XDBE doesn't seem to provide a way to clear the back buffer without
7851 * interfering with the front buffer, other than passing XdbeBackground
7852 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7853 * redraw the text even if it wasn't part of the exposed area. OTOH,
7854 * if we're not going to call draw_stuff at all, then no swap happens
7855 * and we can safely do nothing. */
7857 if (!XEmptyRegion(region)) {
7862 r.x = text_start_x - border_margin;
7863 r.y = text_start_y - border_margin;
7864 r.width = text_width + border_margin * 2;
7865 r.height = text_height + border_margin * 2;
7866 XUnionRectWithRegion(&r, region, region);
7869 XSetRegion(display, window.gc, region);
7872 XftDrawSetClip(window.xftdraw, region);
7878 XDestroyRegion(region);
7879 region = XCreateRegion();
7883 #ifdef SIGNAL_BLOCKING
7884 /* unblock signals of interest and let handler fly */
7885 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7886 CRIT_ERR("unable to sigprocmask()");
7890 switch (g_signal_pending) {
7893 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7898 ERR("received SIGINT or SIGTERM to terminate. bye!");
7901 XDestroyRegion(region);
7904 XDamageDestroy(display, damage);
7905 XFixesDestroyRegion(display, region2);
7906 XFixesDestroyRegion(display, part);
7907 #endif /* HAVE_XDAMAGE */
7909 return; /* return from main_loop */
7912 /* Reaching here means someone set a signal
7913 * (SIGXXXX, signal_handler), but didn't write any code
7915 * If you don't want to handle a signal, don't set a handler on
7916 * it in the first place. */
7917 if (g_signal_pending) {
7918 ERR("ignoring signal (%d)", g_signal_pending);
7922 g_signal_pending = 0;
7925 #if defined(X11) && defined(HAVE_XDAMAGE)
7926 XDamageDestroy(display, damage);
7927 XFixesDestroyRegion(display, region2);
7928 XFixesDestroyRegion(display, part);
7929 XDestroyRegion(region);
7931 #endif /* X11 && HAVE_XDAMAGE */
7934 static void load_config_file(const char *);
7936 /* reload the config file */
7937 void reload_config(void)
7939 timed_thread_destroy_registered_threads();
7941 if (info.cpu_usage) {
7942 free(info.cpu_usage);
7943 info.cpu_usage = NULL;
7951 if (info.mpd.title) {
7952 free(info.mpd.title);
7953 info.mpd.title = NULL;
7955 if (info.mpd.artist) {
7956 free(info.mpd.artist);
7957 info.mpd.artist = NULL;
7959 if (info.mpd.album) {
7960 free(info.mpd.album);
7961 info.mpd.album = NULL;
7963 if (info.mpd.random) {
7964 free(info.mpd.random);
7965 info.mpd.random = NULL;
7967 if (info.mpd.repeat) {
7968 free(info.mpd.repeat);
7969 info.mpd.repeat = NULL;
7971 if (info.mpd.track) {
7972 free(info.mpd.track);
7973 info.mpd.track = NULL;
7975 if (info.mpd.name) {
7976 free(info.mpd.name);
7977 info.mpd.name = NULL;
7979 if (info.mpd.file) {
7980 free(info.mpd.file);
7981 info.mpd.file = NULL;
7983 if (info.mpd.status) {
7984 free(info.mpd.status);
7985 info.mpd.status = NULL;
7990 free_moc(&info.moc);
7997 #ifdef TCP_PORT_MONITOR
7998 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
8001 if (current_config) {
8003 load_config_file(current_config);
8005 /* re-init specials array */
8006 if ((specials = realloc((void *) specials,
8007 sizeof(struct special_t) * max_specials)) == 0) {
8008 ERR("failed to realloc specials array");
8014 // clear the window first
8015 XClearWindow(display, RootWindow(display, screen));
8018 #ifdef TCP_PORT_MONITOR
8019 info.p_tcp_port_monitor_collection = NULL;
8021 extract_variable_text(global_text);
8027 tmpstring1 = malloc(text_buffer_size);
8028 memset(tmpstring1, 0, text_buffer_size);
8032 tmpstring2 = malloc(text_buffer_size);
8033 memset(tmpstring2, 0, text_buffer_size);
8037 text_buffer = malloc(max_user_text);
8038 memset(text_buffer, 0, max_user_text);
8045 timed_thread_destroy_registered_threads();
8047 if (info.cpu_usage) {
8048 free(info.cpu_usage);
8049 info.cpu_usage = NULL;
8054 XdbeDeallocateBackBufferName(display, window.back_buffer);
8059 XDestroyWindow(display, window.window);
8060 XClearWindow(display, RootWindow(display, screen));
8065 XClearWindow(display, RootWindow(display, screen));
8070 XFreeGC(display, window.gc);
8074 free_text_objects(global_text_object_list, 1);
8075 free(global_text_object_list);
8076 global_text_object_list = NULL;
8095 free(current_config);
8097 #ifdef TCP_PORT_MONITOR
8098 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
8099 info.p_tcp_port_monitor_collection = NULL;
8108 for (i = 0; i < special_count; i++) {
8109 if (specials[i].type == GRAPH) {
8110 free(specials[i].graph);
8117 clear_diskio_stats();
8120 static int string_to_bool(const char *s)
8123 // Assumes an option without a true/false means true
8125 } else if (strcasecmp(s, "yes") == EQUAL) {
8127 } else if (strcasecmp(s, "true") == EQUAL) {
8129 } else if (strcasecmp(s, "1") == EQUAL) {
8136 static enum alignment string_to_alignment(const char *s)
8138 if (strcasecmp(s, "top_left") == EQUAL) {
8140 } else if (strcasecmp(s, "top_right") == EQUAL) {
8142 } else if (strcasecmp(s, "top_middle") == EQUAL) {
8144 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
8146 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
8147 return BOTTOM_RIGHT;
8148 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
8149 return BOTTOM_MIDDLE;
8150 } else if (strcasecmp(s, "middle_left") == EQUAL) {
8152 } else if (strcasecmp(s, "middle_right") == EQUAL) {
8153 return MIDDLE_RIGHT;
8154 } else if (strcasecmp(s, "tl") == EQUAL) {
8156 } else if (strcasecmp(s, "tr") == EQUAL) {
8158 } else if (strcasecmp(s, "tm") == EQUAL) {
8160 } else if (strcasecmp(s, "bl") == EQUAL) {
8162 } else if (strcasecmp(s, "br") == EQUAL) {
8163 return BOTTOM_RIGHT;
8164 } else if (strcasecmp(s, "bm") == EQUAL) {
8165 return BOTTOM_MIDDLE;
8166 } else if (strcasecmp(s, "ml") == EQUAL) {
8168 } else if (strcasecmp(s, "mr") == EQUAL) {
8169 return MIDDLE_RIGHT;
8170 } else if (strcasecmp(s, "none") == EQUAL) {
8177 static void set_default_configurations(void)
8179 fork_to_background = 0;
8180 total_run_times = 0;
8181 info.cpu_avg_samples = 2;
8182 info.net_avg_samples = 2;
8189 strcpy(info.mpd.host, "localhost");
8190 info.mpd.port = 6600;
8191 info.mpd.status = NULL;
8192 info.mpd.artist = NULL;
8193 info.mpd.album = NULL;
8194 info.mpd.title = NULL;
8195 info.mpd.random = NULL;
8196 info.mpd.track = NULL;
8197 info.mpd.name = NULL;
8198 info.mpd.file = NULL;
8201 init_moc(&info.moc);
8204 info.xmms2.artist = NULL;
8205 info.xmms2.album = NULL;
8206 info.xmms2.title = NULL;
8207 info.xmms2.genre = NULL;
8208 info.xmms2.comment = NULL;
8209 info.xmms2.url = NULL;
8210 info.xmms2.status = NULL;
8211 info.xmms2.playlist = NULL;
8213 use_spacer = NO_SPACER;
8215 output_methods = TO_X;
8217 output_methods = TO_STDOUT;
8220 show_graph_scale = 0;
8221 show_graph_range = 0;
8222 default_fg_color = WhitePixel(display, screen);
8223 default_bg_color = BlackPixel(display, screen);
8224 default_out_color = BlackPixel(display, screen);
8225 color0 = default_fg_color;
8226 color1 = default_fg_color;
8227 color2 = default_fg_color;
8228 color3 = default_fg_color;
8229 color4 = default_fg_color;
8230 color5 = default_fg_color;
8231 color6 = default_fg_color;
8232 color7 = default_fg_color;
8233 color8 = default_fg_color;
8234 color9 = default_fg_color;
8237 draw_graph_borders = 1;
8239 set_first_font("6x10");
8247 window.type = TYPE_NORMAL;
8249 strcpy(window.class_name, PACKAGE_NAME);
8251 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
8253 stippled_borders = 0;
8256 text_alignment = BOTTOM_LEFT;
8257 info.x11.monitor.number = 1;
8258 info.x11.monitor.current = 0;
8261 free(current_mail_spool);
8265 variable_substitute(MAIL_FILE, buf, 256);
8266 if (buf[0] != '\0') {
8267 current_mail_spool = strndup(buf, text_buffer_size);
8272 update_interval = 3.0;
8273 info.music_player_interval = 1.0;
8274 stuff_in_upper_case = 0;
8275 info.users.number = 1;
8277 #ifdef TCP_PORT_MONITOR
8278 tcp_port_monitor_args.max_port_monitor_connections =
8279 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8283 static void load_config_file(const char *f)
8285 #define CONF_ERR ERR("%s: %d: config file error", f, line)
8289 set_default_configurations();
8296 char buf[256], *p, *p2, *name, *value;
8299 if (fgets(buf, 256, fp) == NULL) {
8305 /* break at comment */
8306 p2 = strchr(p, '#');
8312 while (*p && isspace((int) *p)) {
8316 continue; /* empty line */
8323 while (*p2 && !isspace((int) *p2)) {
8327 *p2 = '\0'; /* break at name's end */
8334 while (*p && isspace((int) *p)) {
8340 p2 = value + strlen(value);
8341 while (isspace((int) *(p2 - 1))) {
8348 #define CONF2(a) if (strcasecmp(name, a) == 0)
8349 #define CONF(a) else CONF2(a)
8350 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
8351 || strcasecmp(name, b) == 0)
8354 CONF2("alignment") {
8356 int a = string_to_alignment(value);
8367 CONF("background") {
8368 fork_to_background = string_to_bool(value);
8371 CONF2("background") {
8372 fork_to_background = string_to_bool(value);
8376 CONF("show_graph_scale") {
8377 show_graph_scale = string_to_bool(value);
8379 CONF("show_graph_range") {
8380 show_graph_range = string_to_bool(value);
8382 CONF("border_margin") {
8384 border_margin = strtol(value, 0, 0);
8389 CONF("border_width") {
8391 border_width = strtol(value, 0, 0);
8398 color0 = get_x11_color(value);
8405 color1 = get_x11_color(value);
8412 color2 = get_x11_color(value);
8419 color3 = get_x11_color(value);
8426 color4 = get_x11_color(value);
8433 color5 = get_x11_color(value);
8440 color6 = get_x11_color(value);
8447 color7 = get_x11_color(value);
8454 color8 = get_x11_color(value);
8461 color9 = get_x11_color(value);
8466 CONF("default_color") {
8468 default_fg_color = get_x11_color(value);
8473 CONF3("default_shade_color", "default_shadecolor") {
8475 default_bg_color = get_x11_color(value);
8480 CONF3("default_outline_color", "default_outlinecolor") {
8482 default_out_color = get_x11_color(value);
8490 info.mail = parse_mail_args(IMAP, value);
8497 info.mail = parse_mail_args(POP3, value);
8505 strncpy(info.mpd.host, value, 127);
8512 info.mpd.port = strtol(value, 0, 0);
8513 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
8518 CONF("mpd_password") {
8520 strncpy(info.mpd.password, value, 127);
8526 CONF("music_player_interval") {
8528 info.music_player_interval = strtod(value, 0);
8534 CONF("sensor_device") {
8536 sensor_device = strtol(value, 0, 0);
8542 CONF("cpu_avg_samples") {
8544 cpu_avg_samples = strtol(value, 0, 0);
8545 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8548 info.cpu_avg_samples = cpu_avg_samples;
8554 CONF("net_avg_samples") {
8556 net_avg_samples = strtol(value, 0, 0);
8557 if (net_avg_samples < 1 || net_avg_samples > 14) {
8560 info.net_avg_samples = net_avg_samples;
8568 CONF("double_buffer") {
8569 use_xdbe = string_to_bool(value);
8573 CONF("override_utf8_locale") {
8574 utf8_mode = string_to_bool(value);
8576 CONF("draw_borders") {
8577 draw_borders = string_to_bool(value);
8579 CONF("draw_graph_borders") {
8580 draw_graph_borders = string_to_bool(value);
8582 CONF("draw_shades") {
8583 draw_shades = string_to_bool(value);
8585 CONF("draw_outline") {
8586 draw_outline = string_to_bool(value);
8589 CONF("out_to_console") {
8590 if(string_to_bool(value)) output_methods |= TO_STDOUT;
8592 CONF("use_spacer") {
8594 if (strcasecmp(value, "left") == EQUAL) {
8595 use_spacer = LEFT_SPACER;
8596 } else if (strcasecmp(value, "right") == EQUAL) {
8597 use_spacer = RIGHT_SPACER;
8598 } else if (strcasecmp(value, "none") == EQUAL) {
8599 use_spacer = NO_SPACER;
8601 use_spacer = string_to_bool(value);
8602 ERR("use_spacer should have an argument of left, right, or"
8603 " none. '%s' seems to be some form of '%s', so"
8604 " defaulting to %s.", value,
8605 use_spacer ? "true" : "false",
8606 use_spacer ? "right" : "none");
8608 use_spacer = RIGHT_SPACER;
8610 use_spacer = NO_SPACER;
8614 ERR("use_spacer should have an argument. Defaulting to right.");
8615 use_spacer = RIGHT_SPACER;
8621 use_xft = string_to_bool(value);
8625 set_first_font(value);
8631 if (value && font_count >= 0) {
8632 fonts[0].font_alpha = atof(value) * 65535.0;
8641 if (string_to_bool(value)) {
8642 ERR("Xft not enabled");
8646 /* xftfont silently ignored when no Xft */
8649 /* xftalpha is silently ignored when no Xft */
8654 set_first_font(value);
8664 gap_x = atoi(value);
8671 gap_y = atoi(value);
8677 CONF("mail_spool") {
8681 variable_substitute(value, buffer, 256);
8683 if (buffer[0] != '\0') {
8684 if (current_mail_spool) {
8685 free(current_mail_spool);
8687 current_mail_spool = strndup(buffer, text_buffer_size);
8694 CONF("minimum_size") {
8696 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8698 if (sscanf(value, "%d", &minimum_width) != 1) {
8706 CONF("maximum_width") {
8708 if (sscanf(value, "%d", &maximum_width) != 1) {
8716 CONF("no_buffers") {
8717 no_buffers = string_to_bool(value);
8719 CONF("top_cpu_separate") {
8720 cpu_separate = string_to_bool(value);
8722 CONF("short_units") {
8723 short_units = string_to_bool(value);
8725 CONF("pad_percents") {
8726 pad_percents = atoi(value);
8730 CONF("own_window") {
8732 own_window = string_to_bool(value);
8737 CONF("own_window_class") {
8739 memset(window.class_name, 0, sizeof(window.class_name));
8740 strncpy(window.class_name, value,
8741 sizeof(window.class_name) - 1);
8746 CONF("own_window_title") {
8748 memset(window.title, 0, sizeof(window.title));
8749 strncpy(window.title, value, sizeof(window.title) - 1);
8754 CONF("own_window_transparent") {
8756 set_transparent = string_to_bool(value);
8761 CONF("own_window_colour") {
8763 background_colour = get_x11_color(value);
8765 ERR("Invalid colour for own_window_colour (try omitting the "
8766 "'#' for hex colours");
8769 CONF("own_window_hints") {
8771 char *p_hint, *p_save;
8772 char delim[] = ", ";
8774 /* tokenize the value into individual hints */
8775 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8777 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8778 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8779 SET_HINT(window.hints, HINT_UNDECORATED);
8780 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8781 SET_HINT(window.hints, HINT_BELOW);
8782 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8783 SET_HINT(window.hints, HINT_ABOVE);
8784 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8785 SET_HINT(window.hints, HINT_STICKY);
8786 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8787 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8788 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8789 SET_HINT(window.hints, HINT_SKIP_PAGER);
8794 p_hint = strtok_r(NULL, delim, &p_save);
8795 } while (p_hint != NULL);
8801 CONF("own_window_type") {
8803 if (strncmp(value, "normal", 6) == EQUAL) {
8804 window.type = TYPE_NORMAL;
8805 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8806 window.type = TYPE_DESKTOP;
8807 } else if (strncmp(value, "dock", 7) == EQUAL) {
8808 window.type = TYPE_DOCK;
8809 } else if (strncmp(value, "override", 8) == EQUAL) {
8810 window.type = TYPE_OVERRIDE;
8819 CONF("stippled_borders") {
8821 stippled_borders = strtol(value, 0, 0);
8823 stippled_borders = 4;
8828 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
8832 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
8835 CONF("update_interval") {
8837 update_interval = strtod(value, 0);
8841 if (info.music_player_interval == 0) {
8842 // default to update_interval
8843 info.music_player_interval = update_interval;
8846 CONF("total_run_times") {
8848 total_run_times = strtod(value, 0);
8854 stuff_in_upper_case = string_to_bool(value);
8856 CONF("max_specials") {
8858 max_specials = atoi(value);
8863 CONF("max_user_text") {
8865 max_user_text = atoi(value);
8870 CONF("text_buffer_size") {
8872 text_buffer_size = atoi(value);
8873 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8874 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8875 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8887 global_text = (char *) malloc(1);
8888 global_text[0] = '\0';
8891 unsigned int l = strlen(global_text);
8893 if (fgets(buf, 256, fp) == NULL) {
8896 global_text = (char *) realloc(global_text, l + strlen(buf) + 1);
8897 strcat(global_text, buf);
8899 if (strlen(global_text) > max_user_text) {
8904 if (strlen(global_text) < 1) {
8905 CRIT_ERR("no text supplied in configuration; exiting");
8907 global_text_lines = line + 1;
8910 #ifdef TCP_PORT_MONITOR
8911 CONF("max_port_monitor_connections") {
8912 if (!value || (sscanf(value, "%d",
8913 &tcp_port_monitor_args.max_port_monitor_connections) != 1)
8914 || tcp_port_monitor_args.max_port_monitor_connections < 0) {
8915 /* an error. use default, warn and continue. */
8916 tcp_port_monitor_args.max_port_monitor_connections =
8917 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8919 } else if (tcp_port_monitor_args.max_port_monitor_connections
8921 /* no error, just use default */
8922 tcp_port_monitor_args.max_port_monitor_connections =
8923 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8925 /* else tcp_port_monitor_args.max_port_monitor_connections > 0
8929 CONF("if_up_strictness") {
8931 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8932 ifup_strictness = IFUP_UP;
8933 } else if (strcasecmp(value, "up") == EQUAL) {
8934 ifup_strictness = IFUP_UP;
8935 } else if (strcasecmp(value, "link") == EQUAL) {
8936 ifup_strictness = IFUP_LINK;
8937 } else if (strcasecmp(value, "address") == EQUAL) {
8938 ifup_strictness = IFUP_ADDR;
8940 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8941 ifup_strictness = IFUP_UP;
8945 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8956 if (info.music_player_interval == 0) {
8957 // default to update_interval
8958 info.music_player_interval = update_interval;
8960 if (!global_text) { // didn't supply any text
8961 CRIT_ERR("missing text block in configuration; exiting");
8965 static void print_help(const char *prog_name) {
8966 printf("Usage: %s [OPTION]...\n"
8967 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8968 "window. Command line options will override configurations defined in config\n"
8970 " -v, --version version\n"
8971 " -q, --quiet quiet mode\n"
8972 " -c, --config=FILE config file to load\n"
8973 " -d, --daemonize daemonize, fork to background\n"
8974 " -h, --help help\n"
8976 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8977 " -f, --font=FONT font to use\n"
8979 " -o, --own-window create own window to draw\n"
8982 " -b, --double-buffer double buffer (prevents flickering)\n"
8984 " -w, --window-id=WIN_ID window id to draw\n"
8985 " -x X x position\n"
8986 " -y Y y position\n"
8988 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8989 " -u, --interval=SECS update interval\n"
8990 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8995 /* : means that character before that takes an argument */
8996 static const char *getopt_string = "vVqdt:u:i:hc:"
9008 static const struct option longopts[] = {
9009 { "help", 0, NULL, 'h' },
9010 { "version", 0, NULL, 'V' },
9011 { "config", 1, NULL, 'c' },
9012 { "daemonize", 0, NULL, 'd' },
9014 { "alignment", 1, NULL, 'a' },
9015 { "font", 1, NULL, 'f' },
9017 { "own-window", 0, NULL, 'o' },
9020 { "double-buffer", 0, NULL, 'b' },
9022 { "window-id", 1, NULL, 'w' },
9024 { "text", 1, NULL, 't' },
9025 { "interval", 0, NULL, 'u' },
9029 int main(int argc, char **argv)
9035 struct sigaction act, oact;
9037 g_signal_pending = 0;
9038 memset(&info, 0, sizeof(info));
9041 #ifdef TCP_PORT_MONITOR
9042 tcp_port_monitor_args.max_port_monitor_connections =
9043 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
9046 /* handle command line parameters that don't change configs */
9048 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9049 || ((s = getenv("LANG")) && *s)) {
9050 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9052 ERR("malloc failed");
9054 for (x = 0; x < strlen(s); x++) {
9055 temp[x] = tolower(s[x]);
9058 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9064 if (!setlocale(LC_CTYPE, "")) {
9065 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9069 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9080 if (current_config) {
9081 free(current_config);
9083 current_config = strndup(optarg, max_user_text);
9086 freopen("/dev/null", "w", stderr);
9089 print_help(argv[0]);
9093 window.window = strtol(optarg, 0, 0);
9102 /* initalize X BEFORE we load config.
9103 * (we need to so that 'screen' is set) */
9107 /* check if specified config file is valid */
9108 if (current_config) {
9110 if (stat(current_config, &sb) ||
9111 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9112 ERR("invalid configuration file '%s'\n", current_config);
9113 free(current_config);
9118 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9120 if (!current_config) {
9121 /* load default config file */
9125 /* Try to use personal config file first */
9126 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
9127 if (buf[0] && (fp = fopen(buf, "r"))) {
9128 current_config = strndup(buf, max_user_text);
9132 /* Try to use system config file if personal config not readable */
9133 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9134 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9138 /* No readable config found */
9139 if (!current_config) {
9140 CRIT_ERR("no readable personal or system-wide config file found");
9144 load_config_file(current_config);
9146 /* init specials array */
9147 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9148 ERR("failed to create specials array");
9152 if (current_mail_spool == NULL) {
9155 variable_substitute(MAIL_FILE, buf, 256);
9157 if (buf[0] != '\0') {
9158 current_mail_spool = strndup(buf, text_buffer_size);
9163 /* handle other command line arguments */
9165 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9166 || defined(__NetBSD__)
9167 optind = optreset = 1;
9172 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9173 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9174 "kvm_open")) == NULL) {
9175 CRIT_ERR("cannot read kvm");
9180 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9188 fork_to_background = 1;
9193 set_first_font(optarg);
9196 text_alignment = string_to_alignment(optarg);
9215 global_text = strndup(optarg, max_user_text);
9216 convert_escapes(global_text);
9220 update_interval = strtod(optarg, 0);
9221 if (info.music_player_interval == 0) {
9222 // default to update_interval
9223 info.music_player_interval = update_interval;
9228 total_run_times = strtod(optarg, 0);
9232 gap_x = atoi(optarg);
9236 gap_y = atoi(optarg);
9250 /* generate text and get initial size */
9251 extract_variable_text(global_text);
9258 if (fork_to_background) {
9263 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9270 fprintf(stderr, "\n");
9275 /* parent process */
9276 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9283 text_buffer = malloc(max_user_text);
9284 memset(text_buffer, 0, max_user_text);
9285 tmpstring1 = malloc(text_buffer_size);
9286 memset(tmpstring1, 0, text_buffer_size);
9287 tmpstring2 = malloc(text_buffer_size);
9288 memset(tmpstring2, 0, text_buffer_size);
9292 update_text_area(); /* to get initial size of the window */
9295 init_window(own_window, text_width + border_margin * 2 + 1,
9296 text_height + border_margin * 2 + 1, set_transparent, background_colour,
9298 #else /* OWN_WINDOW */
9299 init_window(0, text_width + border_margin * 2 + 1,
9300 text_height + border_margin * 2 + 1, set_transparent, 0,
9302 #endif /* OWN_WINDOW */
9305 update_text_area(); /* to position text/window on screen */
9308 if (own_window && !fixed_pos) {
9309 XMoveWindow(display, window.window, window.x, window.y);
9312 set_transparent_background(window.window);
9322 /* Set signal handlers */
9323 act.sa_handler = signal_handler;
9324 sigemptyset(&act.sa_mask);
9327 act.sa_flags |= SA_RESTART;
9330 if ( sigaction(SIGINT, &act, &oact) < 0
9331 || sigaction(SIGUSR1, &act, &oact) < 0
9332 || sigaction(SIGHUP, &act, &oact) < 0
9333 || sigaction(SIGTERM, &act, &oact) < 0) {
9334 ERR("error setting signal handler: %s", strerror(errno));
9339 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9346 void signal_handler(int sig)
9348 /* signal handler is light as a feather, as it should be.
9349 * we will poll g_signal_pending with each loop of conky
9350 * and do any signal processing there, NOT here. */
9351 g_signal_pending = sig;