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 CONFIG_FILE "$HOME/.conkyrc"
70 #define MAIL_FILE "$MAIL"
71 #define MAX_IF_BLOCK_DEPTH 5
72 #define MAX_TAIL_LINES 100
74 /* #define SIGNAL_BLOCKING */
75 #undef SIGNAL_BLOCKING
77 static void print_version(void) __attribute__((noreturn));
79 static void print_version(void)
81 printf("Conky %s compiled %s for %s\n", VERSION, BUILD_DATE, BUILD_ARCH);
83 printf("\nCompiled in features:\n\n"
84 "System config file: %s\n\n"
88 " * Xdamage extension\n"
89 # endif /* HAVE_XDAMAGE */
91 " * Xdbe extension (double buffer)\n"
92 # endif /* HAVE_XDBE */
97 "\n Music detection:\n"
100 #endif /* AUDACIOUS */
110 "\n General features:\n"
114 #ifdef TCP_PORT_MONITOR
116 #endif /* TCP_PORT_MONITOR */
122 #endif /* HAVE_IWLIB */
129 "", SYSTEM_CONFIG_FILE
135 static const char *suffixes[] = { "B", "kiB", "MiB", "GiB", "TiB", "PiB", "" };
141 static int text_start_x, text_start_y; /* text start position in window */
142 static int text_width, text_height;
160 char name[DEFAULT_TEXT_BUFFER_SIZE];
170 static int selected_font = 0;
171 static int font_count = -1;
172 struct font_list *fonts = NULL;
176 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
177 fonts[selected_font].xftfont->descent) \
178 : (fonts[selected_font].font->max_bounds.ascent + \
179 fonts[selected_font].font->max_bounds.descent))
180 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
181 : fonts[selected_font].font->max_bounds.ascent)
182 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
183 : fonts[selected_font].font->max_bounds.descent)
187 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
188 fonts[selected_font].font->max_bounds.descent)
189 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
190 #define font_descent() fonts[selected_font].font->max_bounds.descent
194 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
196 static void set_font(void);
198 int addfont(const char *data_in)
200 if (font_count > MAX_FONTS) {
201 CRIT_ERR("you don't need that many fonts, sorry.");
204 if (font_count == 0) {
208 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
212 memset(fonts, 0, sizeof(struct font_list));
214 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
215 memset(&fonts[font_count], 0, sizeof(struct font_list));
217 CRIT_ERR("realloc in addfont");
219 // must account for null terminator
220 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
221 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
223 fonts[font_count].font_alpha = 0xffff;
226 CRIT_ERR("Oops...looks like something overflowed in addfont().");
231 void set_first_font(const char *data_in)
233 if (font_count < 0) {
234 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
238 memset(fonts, 0, sizeof(struct font_list));
241 if (strlen(data_in) > 1) {
242 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
244 fonts[0].font_alpha = 0xffff;
249 void free_fonts(void)
253 for (i = 0; i <= font_count; i++) {
256 XftFontClose(display, fonts[i].xftfont);
257 fonts[i].xftfont = 0;
261 XFreeFont(display, fonts[i].font);
271 static void load_fonts(void)
275 for (i = 0; i <= font_count; i++) {
278 if (use_xft && fonts[i].xftfont) {
280 } else if (use_xft) {
281 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
282 XftFontClose(display, fonts[i].xftfont);
284 fonts[i].xftfont = XftFontOpenName(display, screen,
286 if (fonts[i].xftfont != NULL) {
290 ERR("can't load Xft font '%s'", fonts[i].name);
291 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
292 "courier-12")) != NULL) {
296 ERR("can't load Xft font '%s'", "courier-12");
298 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
299 CRIT_ERR("can't load font '%s'", "fixed");
306 /* load normal font */
307 /* if (fonts[i].font != NULL) {
308 XFreeFont(display, fonts[i].font);
311 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
312 ERR("can't load font '%s'", fonts[i].name);
313 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
314 CRIT_ERR("can't load font '%s'", "fixed");
315 printf("loaded fixed?\n");
323 /* default config file */
324 static char *current_config;
326 /* set to 1 if you want all text to be in uppercase */
327 static unsigned int stuff_in_upper_case;
329 /* Run how many times? */
330 static unsigned long total_run_times;
333 static int fork_to_background;
335 static int cpu_avg_samples, net_avg_samples;
339 static int show_graph_scale;
341 /* Position on the screen */
342 static int text_alignment;
343 static int gap_x, gap_y;
346 static int draw_borders;
347 static int draw_graph_borders;
348 static int stippled_borders;
350 static int draw_shades, draw_outline;
352 static int border_margin, border_width;
354 static long default_fg_color, default_bg_color, default_out_color;
356 /* create own window or draw stuff to root? */
357 static int set_transparent = 0;
360 static int own_window = 0;
361 static int background_colour = 0;
363 /* fixed size/pos is set if wm/user changes them */
364 static int fixed_size = 0, fixed_pos = 0;
367 static int minimum_width, minimum_height;
368 static int maximum_width;
373 static int sensor_device;
376 static long color0, color1, color2, color3, color4, color5, color6, color7,
379 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
380 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
382 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
383 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
385 /* maximum size of individual text buffers, ie $exec buffer size */
386 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
389 #define CODEPAGE_LENGTH 20
391 long iconv_count = 0;
392 char iconv_converting;
393 static iconv_t **iconv_cd = 0;
395 int register_iconv(iconv_t *new_iconv)
397 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
399 CRIT_ERR("Out of memory");
401 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
402 if (!iconv_cd[iconv_count]) {
403 CRIT_ERR("Out of memory");
405 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
410 void free_iconv(void)
415 for (i = 0; i < iconv_count; i++) {
417 iconv_close(*iconv_cd[i]);
431 /* no buffers in used memory? */
434 /* pad percentages to decimals? */
435 static int pad_percents = 0;
437 #ifdef TCP_PORT_MONITOR
438 tcp_port_monitor_args_t tcp_port_monitor_args;
441 static char *text = 0;
444 static int total_updates;
447 static int blockdepth = 0;
448 static int if_jumped = 0;
449 static int blockstart[MAX_IF_BLOCK_DEPTH];
451 int check_contains(char *f, char *s)
454 FILE *where = fopen(f, "r");
457 char buf1[256], buf2[256];
459 while (fgets(buf1, 256, where)) {
460 sscanf(buf1, "%255s", buf2);
461 if (strstr(buf2, s)) {
468 ERR("Could not open the file");
474 static inline int calc_text_width(const char *s, int l)
481 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
482 (const FcChar8 *) s, l, &gi);
484 XftTextExtents8(display, fonts[selected_font].xftfont,
485 (const FcChar8 *) s, l, &gi);
491 return XTextWidth(fonts[selected_font].font, s, l);
496 /* formatted text to render on screen, generated in generate_text(),
497 * drawn in draw_stuff() */
499 static char *text_buffer;
501 /* special stuff in text_buffer */
503 #define SPECIAL_CHAR '\x01'
532 unsigned long first_colour; // for graph gradient
533 unsigned long last_colour;
537 /* create specials array on heap instead of stack with introduction of
539 static struct special_t *specials = NULL;
541 static unsigned int special_count;
544 static unsigned int special_index; /* used when drawing */
547 /* why 256? cause an array of more then 256 doubles seems excessive,
548 * and who needs that kind of precision anyway? */
549 #define MAX_GRAPH_DEPTH 256
551 static struct special_t *new_special(char *buf, enum special_types t)
553 if (special_count >= max_specials) {
554 CRIT_ERR("too many special things in text");
557 buf[0] = SPECIAL_CHAR;
559 specials[special_count].type = t;
560 return &specials[special_count++];
563 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
567 unsigned int count = 0;
568 static char buf[BUFSZ];
569 long orig_pos = ftell(fp);
571 long buf_size = BUFSZ;
572 char *cur_found = NULL;
574 while (count < step) {
575 if (cur_found == NULL) {
576 buf_size = fread(buf, 1, buf_size, fp);
579 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
580 if (cur_found != NULL) {
581 buf_pos = cur_found - buf + 1;
590 ret = ftell(fp) - buf_size + buf_pos - 1;
592 fseek(fp, orig_pos, SEEK_SET);
598 void *memrchr(const void *buffer, char c, size_t n)
600 const unsigned char *p = buffer;
602 for (p += n; n; n--) {
611 long rev_fcharfind(FILE *fp, char val, unsigned int step)
615 unsigned int count = 0;
616 static char buf[BUFSZ];
617 long orig_pos = ftell(fp);
619 long file_pos = orig_pos;
620 long buf_size = BUFSZ;
623 while (count < step) {
625 if (file_pos > BUFSZ) {
626 fseek(fp, file_pos - BUFSZ, SEEK_SET);
629 fseek(fp, 0, SEEK_SET);
631 file_pos = ftell(fp);
632 buf_pos = fread(buf, 1, buf_size, fp);
634 cur_found = memrchr(buf, val, (size_t) buf_pos);
635 if (cur_found != NULL) {
636 buf_pos = cur_found - buf;
645 fseek(fp, orig_pos, SEEK_SET);
647 ret = file_pos + buf_pos;
653 static void new_bar(char *buf, int w, int h, int usage)
655 struct special_t *s = new_special(buf, BAR);
657 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
662 static const char *scan_bar(const char *args, int *w, int *h)
664 /* zero width means all space that is available */
667 /* bar's argument is either height or height,width */
671 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
672 sscanf(args, "%d %n", h, &n);
680 static char *scan_font(const char *args)
683 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
690 static void new_font(char *buf, char *args)
693 struct special_t *s = new_special(buf, FONT);
695 if (s->font_added > font_count || !s->font_added || strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE)) {
696 int tmp = selected_font;
698 selected_font = s->font_added = addfont(args);
703 struct special_t *s = new_special(buf, FONT);
704 int tmp = selected_font;
706 selected_font = s->font_added = 0;
711 inline void graph_append(struct special_t *graph, double f)
715 if (!graph->scaled && f > graph->graph_scale) {
716 f = graph->graph_scale;
720 graph->graph_scale = 1;
722 graph->graph[0] = f; /* add new data */
723 /* shift all the data by 1 */
724 for (i = graph->graph_width - 1; i > 0; i--) {
725 graph->graph[i] = graph->graph[i - 1];
726 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
727 /* check if we need to update the scale */
728 graph->graph_scale = graph->graph[i];
733 short colour_depth = 0;
734 void set_up_gradient(void);
736 /* precalculated: 31/255, and 63/255 */
737 #define CONST_8_TO_5_BITS 0.12156862745098
738 #define CONST_8_TO_6_BITS 0.247058823529412
740 /* adjust color values depending on color depth */
741 static unsigned int adjust_colors(unsigned int color)
745 if (colour_depth == 0) {
748 if (colour_depth == 16) {
749 r = (color & 0xff0000) >> 16;
750 g = (color & 0xff00) >> 8;
752 color = (int) (r * CONST_8_TO_5_BITS) << 11;
753 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
754 color |= (int) (b * CONST_8_TO_5_BITS);
759 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
760 unsigned int second_colour, double i, int scale, int append)
762 struct special_t *s = new_special(buf, GRAPH);
765 if (s->graph == NULL) {
766 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
767 // subtract 2 for the box
768 s->graph_width = s->width /* - 2 */;
770 s->graph_width = MAX_GRAPH_DEPTH - 2;
772 s->graph = malloc(s->graph_width * sizeof(double));
773 memset(s->graph, 0, s->graph_width * sizeof(double));
774 s->graph_scale = 100;
777 s->first_colour = adjust_colors(first_colour);
778 s->last_colour = adjust_colors(second_colour);
785 s->graph_width = s->width - 2; // subtract 2 for rectangle around
791 s->graph_scale = scale;
799 static char *scan_graph(const char *args, int *w, int *h,
800 unsigned int *first_colour, unsigned int *last_colour,
806 /* zero width means all space that is available */
812 /* graph's argument is either height or height,width */
814 if (sscanf(args, "%d,%d %x %x %u", h, w, first_colour, last_colour,
819 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
822 if (sscanf(args, "%63s %d,%d %x %x %u", buf, h, w, first_colour,
823 last_colour, scale) == 6) {
824 return strndup(buf, text_buffer_size);
827 if (sscanf(args, "%63s %d,%d %x %x", buf, h, w, first_colour,
829 return strndup(buf, text_buffer_size);
834 if (sscanf(args, "%x %x %u", first_colour, last_colour, scale) == 3) {
838 if (sscanf(args, "%x %x", first_colour, last_colour) == 2) {
841 if (sscanf(args, "%63s %x %x %u", buf, first_colour, last_colour,
843 return strndup(buf, text_buffer_size);
846 if (sscanf(args, "%63s %x %x", buf, first_colour, last_colour) == 3) {
847 return strndup(buf, text_buffer_size);
852 if (sscanf(args, "%d,%d %u", h, w, scale) == 3) {
856 if (sscanf(args, "%d,%d", h, w) == 2) {
859 if (sscanf(args, "%63s %d,%d %u", buf, h, w, scale) < 4) {
861 //TODO: check the return value and throw an error?
862 sscanf(args, "%63s %d,%d", buf, h, w);
865 return strndup(buf, text_buffer_size);
868 if (buf[0] == '\0') {
871 return strndup(buf, text_buffer_size);
875 static inline void new_hr(char *buf, int a)
877 new_special(buf, HORIZONTAL_LINE)->height = a;
880 static inline void new_stippled_hr(char *buf, int a, int b)
882 struct special_t *s = new_special(buf, STIPPLED_HR);
888 static inline void new_fg(char *buf, long c)
890 new_special(buf, FG)->arg = c;
893 static inline void new_bg(char *buf, long c)
895 new_special(buf, BG)->arg = c;
898 static inline void new_outline(char *buf, long c)
900 new_special(buf, OUTLINE)->arg = c;
903 static inline void new_offset(char *buf, long c)
905 new_special(buf, OFFSET)->arg = c;
908 static inline void new_voffset(char *buf, long c)
910 new_special(buf, VOFFSET)->arg = c;
913 static inline void new_alignr(char *buf, long c)
915 new_special(buf, ALIGNR)->arg = c;
918 // A postive offset pushes the text further left
919 static inline void new_alignc(char *buf, long c)
921 new_special(buf, ALIGNC)->arg = c;
924 static inline void new_goto(char *buf, long c)
926 new_special(buf, GOTO)->arg = c;
929 static inline void new_tab(char *buf, int a, int b)
931 struct special_t *s = new_special(buf, TAB);
937 /* quite boring functions */
939 static inline void for_each_line(char *b, void f(char *))
943 for (ps = b, pe = b; *pe; pe++) {
957 static void convert_escapes(char *buf)
959 char *p = buf, *s = buf;
966 } else if (*s == '\\') {
977 /* Prints anything normally printed with snprintf according to the current value
978 * of use_spacer. Actually slightly more flexible than snprintf, as you can
979 * safely specify the destination buffer as one of your inputs. */
980 static int spaced_print(char *, int, const char *, int, const char *, ...)
981 __attribute__((format(printf, 3, 6)));
983 static int spaced_print(char *buf, int size, const char *format, int width,
984 const char *func_name, ...) {
992 tempbuf = malloc(size * sizeof(char));
994 // Passes the varargs along to vsnprintf
995 va_start(argp, func_name);
996 vsnprintf(tempbuf, size, format, argp);
999 switch (use_spacer) {
1001 len = snprintf(buf, size, "%s", tempbuf);
1004 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
1007 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
1010 CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
1019 /* converts from bytes to human readable format (k, M, G, T) */
1020 static void human_readable(long long num, char *buf, int size, const char *func_name)
1022 const char **suffix = suffixes;
1025 static const int WIDTH = 10, SHORT_WIDTH = 8;
1029 spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num,
1032 spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix);
1037 while (num / 1024 >= 1000LL && **(suffix + 2)) {
1043 fnum = num / 1024.0;
1048 if (precision < 0) {
1052 len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name,
1053 precision, fnum, **suffix);
1055 len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision,
1058 } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
1063 enum text_object_type {
1065 #if defined(__linux__)
1067 #endif /* __linux__ */
1077 OBJ_battery_percent,
1079 #endif /* !__OpenBSD__ */
1094 OBJ_conky_build_date,
1095 OBJ_conky_build_arch,
1105 OBJ_diskiograph_read,
1106 OBJ_diskiograph_write,
1144 #if defined(__linux__)
1151 OBJ_i8k_left_fan_status,
1152 OBJ_i8k_right_fan_status,
1153 OBJ_i8k_left_fan_rpm,
1154 OBJ_i8k_right_fan_rpm,
1156 OBJ_i8k_buttons_status,
1172 OBJ_wireless_bitrate,
1174 OBJ_wireless_link_qual,
1175 OBJ_wireless_link_qual_max,
1176 OBJ_wireless_link_qual_perc,
1177 OBJ_wireless_link_bar,
1178 #endif /* __linux__ */
1211 OBJ_running_processes,
1220 OBJ_temp1, /* i2c is used instead in these */
1244 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1245 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1247 OBJ_apm_battery_time,
1248 OBJ_apm_battery_life,
1249 #endif /* __FreeBSD__ __OpenBSD__ */
1251 OBJ_obsd_sensors_temp,
1252 OBJ_obsd_sensors_fan,
1253 OBJ_obsd_sensors_volt,
1256 #endif /* __OpenBSD__ */
1278 OBJ_music_player_interval,
1298 OBJ_xmms2_timesplayed,
1301 OBJ_audacious_status,
1302 OBJ_audacious_title,
1303 OBJ_audacious_length,
1304 OBJ_audacious_length_seconds,
1305 OBJ_audacious_position,
1306 OBJ_audacious_position_seconds,
1307 OBJ_audacious_bitrate,
1308 OBJ_audacious_frequency,
1309 OBJ_audacious_channels,
1310 OBJ_audacious_filename,
1311 OBJ_audacious_playlist_length,
1312 OBJ_audacious_playlist_position,
1326 #ifdef TCP_PORT_MONITOR
1340 OBJ_if_smapi_bat_installed,
1343 OBJ_entropy_poolsize,
1347 struct text_object {
1349 char *s; /* some string */
1350 int i; /* some integer */
1351 long l; /* some other integer */
1352 unsigned int sensor;
1353 struct net_stat *net;
1355 struct diskio_stat *diskio;
1356 unsigned char loadavg[3];
1357 unsigned int cpu_index;
1358 struct mail_s *mail;
1366 char *tz; /* timezone variable */
1367 char *fmt; /* time display formatting */
1405 /* If not -1, a file descriptor to read from when
1406 * logfile is a FIFO. */
1423 timed_thread *p_timed_thread;
1429 #ifdef TCP_PORT_MONITOR
1431 /* starting port to monitor */
1432 in_port_t port_range_begin;
1433 /* ending port to monitor */
1434 in_port_t port_range_end;
1435 /* enum from libtcp-portmon.h, e.g. COUNT, etc. */
1437 /* 0 to n-1 connections. */
1438 int connection_index;
1459 struct local_mail_s local_mail;
1461 struct nvidia_s nvidia;
1468 unsigned int c, d, e;
1473 struct text_object_list {
1474 unsigned int text_object_count;
1475 struct text_object *text_objects;
1478 static struct text_object_list *global_text_object_list;
1480 static void generate_text_internal(char *p, int p_max_size,
1481 struct text_object_list *text_object_list,
1482 struct information *cur);
1484 #define MAXDATASIZE 1000
1488 struct mail_s *parse_mail_args(char type, const char *arg)
1490 struct mail_s *mail;
1493 mail = malloc(sizeof(struct mail_s));
1494 memset(mail, 0, sizeof(struct mail_s));
1496 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass)
1499 ERR("Scanning IMAP args failed");
1500 } else if (type == IMAP) {
1501 ERR("Scanning POP3 args failed");
1504 // see if password needs prompting
1505 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1506 int fp = fileno(stdin);
1507 struct termios term;
1509 tcgetattr(fp, &term);
1510 term.c_lflag &= ~ECHO;
1511 tcsetattr(fp, TCSANOW, &term);
1512 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1513 scanf("%128s", mail->pass);
1515 term.c_lflag |= ECHO;
1516 tcsetattr(fp, TCSANOW, &term);
1518 // now we check for optional args
1519 tmp = strstr(arg, "-i ");
1522 sscanf(tmp, "%f", &mail->interval);
1524 mail->interval = 300; // 5 minutes
1526 tmp = strstr(arg, "-p ");
1529 sscanf(tmp, "%lu", &mail->port);
1532 mail->port = 110; // default pop3 port
1533 } else if (type == IMAP) {
1534 mail->port = 143; // default imap port
1538 tmp = strstr(arg, "-f ");
1541 sscanf(tmp, "%s", mail->folder);
1543 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1546 tmp = strstr(arg, "-e ");
1551 if (tmp[0] == '\'') {
1552 len = strstr(tmp + 1, "'") - tmp - 1;
1557 strncpy(mail->command, tmp + 1, len);
1559 mail->command[0] = '\0';
1561 mail->p_timed_thread = NULL;
1565 void *imap_thread(void *arg)
1567 int sockfd, numbytes;
1568 char recvbuf[MAXDATASIZE];
1569 char sendbuf[MAXDATASIZE];
1572 unsigned int old_unseen = UINT_MAX;
1573 unsigned int old_messages = UINT_MAX;
1574 struct stat stat_buf;
1575 struct hostent he, *he_res = 0;
1577 char hostbuff[2048];
1578 struct sockaddr_in their_addr; // connector's address information
1579 struct mail_s *mail = (struct mail_s *)arg;
1581 #ifdef HAVE_GETHOSTBYNAME_R
1582 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1583 ERR("IMAP gethostbyname_r: %s", hstrerror(h_errno));
1586 #else /* HAVE_GETHOSTBYNAME_R */
1587 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1588 herror("gethostbyname");
1591 #endif /* HAVE_GETHOSTBYNAME_R */
1593 struct timeval timeout;
1598 ERR("Trying IMAP connection again for %s@%s (try %i/5)",
1599 mail->user, mail->host, fail + 1);
1602 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1609 their_addr.sin_family = AF_INET;
1610 // short, network byte order
1611 their_addr.sin_port = htons(mail->port);
1612 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1613 // zero the rest of the struct
1614 memset(&(their_addr.sin_zero), '\0', 8);
1616 if (connect(sockfd, (struct sockaddr *) &their_addr,
1617 sizeof(struct sockaddr)) == -1) {
1623 timeout.tv_sec = 60; // 60 second timeout i guess
1624 timeout.tv_usec = 0;
1626 FD_SET(sockfd, &fdset);
1627 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1629 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1635 ERR("IMAP connection failed: timeout");
1639 recvbuf[numbytes] = '\0';
1640 if (strstr(recvbuf, "* OK") != recvbuf) {
1641 ERR("IMAP connection failed, probably not an IMAP server");
1645 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1646 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1647 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1648 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1649 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1650 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1655 timeout.tv_sec = 60; // 60 second timeout i guess
1656 timeout.tv_usec = 0;
1658 FD_SET(sockfd, &fdset);
1659 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1661 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1667 recvbuf[numbytes] = '\0';
1668 if (strstr(recvbuf, "a1 OK") == NULL) {
1669 ERR("IMAP server login failed: %s", recvbuf);
1673 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1674 strncat(sendbuf, mail->folder, MAXDATASIZE - strlen(sendbuf) - 1);
1675 strncat(sendbuf, " (MESSAGES UNSEEN)\r\n",
1676 MAXDATASIZE - strlen(sendbuf) - 1);
1677 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1682 timeout.tv_sec = 60; // 60 second timeout i guess
1683 timeout.tv_usec = 0;
1685 FD_SET(sockfd, &fdset);
1686 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1688 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1694 recvbuf[numbytes] = '\0';
1695 if (strstr(recvbuf, "a2 OK") == NULL) {
1696 ERR("IMAP status failed: %s", recvbuf);
1700 // now we get the data
1701 reply = strstr(recvbuf, " (MESSAGES ");
1703 *strchr(reply, ')') = '\0';
1704 if (reply == NULL) {
1705 ERR("Error parsing IMAP response: %s", recvbuf);
1709 timed_thread_lock(mail->p_timed_thread);
1710 sscanf(reply, "MESSAGES %lu UNSEEN %lu", &mail->messages,
1712 timed_thread_unlock(mail->p_timed_thread);
1714 strncpy(sendbuf, "a3 logout\r\n", MAXDATASIZE);
1715 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1720 timeout.tv_sec = 60; // 60 second timeout i guess
1721 timeout.tv_usec = 0;
1723 FD_SET(sockfd, &fdset);
1724 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1726 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1732 recvbuf[numbytes] = '\0';
1733 if (strstr(recvbuf, "a3 OK") == NULL) {
1734 ERR("IMAP logout failed: %s", recvbuf);
1738 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen
1739 || (mail->messages > old_messages && mail->unseen > 0))) {
1741 if (system(mail->command) == -1) {
1746 old_unseen = mail->unseen;
1747 old_messages = mail->messages;
1749 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1750 /* if a valid socket, close it */
1753 if (timed_thread_test(mail->p_timed_thread)) {
1754 timed_thread_exit(mail->p_timed_thread);
1762 void *pop3_thread(void *arg)
1764 int sockfd, numbytes;
1765 char recvbuf[MAXDATASIZE];
1766 char sendbuf[MAXDATASIZE];
1769 unsigned int old_unseen = UINT_MAX;
1770 struct stat stat_buf;
1771 struct hostent he, *he_res = 0;
1773 char hostbuff[2048];
1774 struct sockaddr_in their_addr; // connector's address information
1775 struct mail_s *mail = (struct mail_s *)arg;
1777 #ifdef HAVE_GETHOSTBYNAME_R
1778 if (gethostbyname_r(mail->host, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info
1779 ERR("POP3 gethostbyname_r: %s", hstrerror(h_errno));
1782 #else /* HAVE_GETHOSTBYNAME_R */
1783 if ((he_res = gethostbyname(mail->host)) == NULL) { // get the host info
1784 herror("gethostbyname");
1787 #endif /* HAVE_GETHOSTBYNAME_R */
1789 struct timeval timeout;
1794 ERR("Trying POP3 connection again for %s@%s (try %i/5)",
1795 mail->user, mail->host, fail + 1);
1798 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1805 their_addr.sin_family = AF_INET;
1806 // short, network byte order
1807 their_addr.sin_port = htons(mail->port);
1808 their_addr.sin_addr = *((struct in_addr *) he_res->h_addr);
1809 // zero the rest of the struct
1810 memset(&(their_addr.sin_zero), '\0', 8);
1812 if (connect(sockfd, (struct sockaddr *) &their_addr,
1813 sizeof(struct sockaddr)) == -1) {
1819 timeout.tv_sec = 60; // 60 second timeout i guess
1820 timeout.tv_usec = 0;
1822 FD_SET(sockfd, &fdset);
1823 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1825 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1831 ERR("POP3 connection failed: timeout\n");
1835 recvbuf[numbytes] = '\0';
1836 if (strstr(recvbuf, "+OK ") != recvbuf) {
1837 ERR("POP3 connection failed, probably not a POP3 server");
1841 strncpy(sendbuf, "USER ", MAXDATASIZE);
1842 strncat(sendbuf, mail->user, MAXDATASIZE - strlen(sendbuf) - 1);
1843 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1844 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1845 perror("send USER");
1849 timeout.tv_sec = 60; // 60 second timeout i guess
1850 timeout.tv_usec = 0;
1852 FD_SET(sockfd, &fdset);
1853 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1855 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1856 perror("recv USER");
1861 recvbuf[numbytes] = '\0';
1862 if (strstr(recvbuf, "+OK ") == NULL) {
1863 ERR("POP3 server login failed: %s", recvbuf);
1867 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1868 strncat(sendbuf, mail->pass, MAXDATASIZE - strlen(sendbuf) - 1);
1869 strncat(sendbuf, "\r\n", MAXDATASIZE - strlen(sendbuf) - 1);
1870 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1871 perror("send PASS");
1875 timeout.tv_sec = 60; // 60 second timeout i guess
1876 timeout.tv_usec = 0;
1878 FD_SET(sockfd, &fdset);
1879 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1881 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1882 perror("recv PASS");
1887 recvbuf[numbytes] = '\0';
1888 if (strstr(recvbuf, "+OK ") == NULL) {
1889 ERR("POP3 server login failed: %s", recvbuf);
1893 strncpy(sendbuf, "STAT\r\n", MAXDATASIZE);
1894 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1895 perror("send STAT");
1899 timeout.tv_sec = 60; // 60 second timeout i guess
1900 timeout.tv_usec = 0;
1902 FD_SET(sockfd, &fdset);
1903 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1905 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1906 perror("recv STAT");
1911 recvbuf[numbytes] = '\0';
1912 if (strstr(recvbuf, "+OK ") == NULL) {
1913 ERR("POP3 status failed: %s", recvbuf);
1917 // now we get the data
1918 reply = recvbuf + 4;
1919 if (reply == NULL) {
1920 ERR("Error parsing POP3 response: %s", recvbuf);
1924 timed_thread_lock(mail->p_timed_thread);
1925 sscanf(reply, "%lu %lu", &mail->unseen, &mail->used);
1926 timed_thread_unlock(mail->p_timed_thread);
1928 strncpy(sendbuf, "QUIT\r\n", MAXDATASIZE);
1929 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1930 perror("send QUIT");
1934 timeout.tv_sec = 60; // 60 second timeout i guess
1935 timeout.tv_usec = 0;
1937 FD_SET(sockfd, &fdset);
1938 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1940 if ((numbytes = recv(sockfd, recvbuf, MAXDATASIZE - 1, 0)) == -1) {
1941 perror("recv QUIT");
1946 recvbuf[numbytes] = '\0';
1947 if (strstr(recvbuf, "+OK") == NULL) {
1948 ERR("POP3 logout failed: %s", recvbuf);
1952 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {
1954 if (system(mail->command) == -1) {
1959 old_unseen = mail->unseen;
1961 if ((fstat(sockfd, &stat_buf) == 0) && S_ISSOCK(stat_buf.st_mode)) {
1962 /* if a valid socket, close it */
1965 if (timed_thread_test(mail->p_timed_thread)) {
1966 timed_thread_exit(mail->p_timed_thread);
1974 void *threaded_exec(void *) __attribute__((noreturn));
1976 void *threaded_exec(void *arg)
1981 struct text_object *obj = (struct text_object *)arg;
1983 p2 = obj->data.texeci.buffer;
1984 fp = popen(obj->data.texeci.cmd, "r");
1986 timed_thread_lock(obj->data.texeci.p_timed_thread);
1987 n2 = fread(p2, 1, text_buffer_size, fp);
1991 if (n2 && p2[n2 - 1] == '\n') {
1995 if (*p2 == '\001') {
2000 timed_thread_unlock(obj->data.texeci.p_timed_thread);
2001 if (timed_thread_test(obj->data.texeci.p_timed_thread)) {
2002 timed_thread_exit(obj->data.texeci.p_timed_thread);
2008 static struct text_object *new_text_object_internal(void)
2010 struct text_object *obj = malloc(sizeof(struct text_object));
2011 memset(obj, 0, sizeof(struct text_object));
2015 static void free_text_objects(struct text_object_list *text_object_list)
2018 struct text_object *obj;
2020 if (text_object_list == NULL) {
2024 for (i = 0; i < text_object_list->text_object_count; i++) {
2025 obj = &text_object_list->text_objects[i];
2026 switch (obj->type) {
2035 close(obj->data.sysfs.fd);
2037 #endif /* !__OpenBSD__ */
2043 free(obj->data.tztime.tz);
2044 free(obj->data.tztime.fmt);
2047 free(obj->data.mboxscan.args);
2048 free(obj->data.mboxscan.output);
2052 free(obj->data.local_mail.box);
2057 case OBJ_imap_unseen:
2058 if (!obj->global_mode) {
2059 free(obj->data.mail);
2062 case OBJ_imap_messages:
2063 if (!obj->global_mode) {
2064 free(obj->data.mail);
2070 case OBJ_pop3_unseen:
2071 if (!obj->global_mode) {
2072 free(obj->data.mail);
2076 if (!obj->global_mode) {
2077 free(obj->data.mail);
2081 case OBJ_if_existing:
2082 case OBJ_if_mounted:
2083 case OBJ_if_running:
2084 free(obj->data.ifblock.s);
2085 free(obj->data.ifblock.str);
2088 free(obj->data.tail.logfile);
2089 free(obj->data.tail.buffer);
2101 case OBJ_iconv_start:
2106 case OBJ_disk_protect:
2107 free(objs[i].data.s);
2110 free(objs[i].data.ifblock.s);
2111 free(objs[i].data.ifblock.str);
2114 free(objs[i].data.ifblock.s);
2115 free(objs[i].data.ifblock.str);
2118 if (info.gw_info.iface) {
2119 free(info.gw_info.iface);
2120 info.gw_info.iface = 0;
2122 if (info.gw_info.ip) {
2123 free(info.gw_info.ip);
2124 info.gw_info.ip = 0;
2127 case OBJ_ioscheduler:
2129 free(objs[i].data.s);
2133 case OBJ_xmms2_artist:
2134 if (info.xmms2.artist) {
2135 free(info.xmms2.artist);
2136 info.xmms2.artist = 0;
2139 case OBJ_xmms2_album:
2140 if (info.xmms2.album) {
2141 free(info.xmms2.album);
2142 info.xmms2.album = 0;
2145 case OBJ_xmms2_title:
2146 if (info.xmms2.title) {
2147 free(info.xmms2.title);
2148 info.xmms2.title = 0;
2151 case OBJ_xmms2_genre:
2152 if (info.xmms2.genre) {
2153 free(info.xmms2.genre);
2154 info.xmms2.genre = 0;
2157 case OBJ_xmms2_comment:
2158 if (info.xmms2.comment) {
2159 free(info.xmms2.comment);
2160 info.xmms2.comment = 0;
2164 if (info.xmms2.url) {
2165 free(info.xmms2.url);
2169 case OBJ_xmms2_date:
2170 if (info.xmms2.date) {
2171 free(info.xmms2.date);
2172 info.xmms2.date = 0;
2175 case OBJ_xmms2_status:
2176 if (info.xmms2.status) {
2177 free(info.xmms2.status);
2178 info.xmms2.status = 0;
2181 case OBJ_xmms2_playlist:
2182 if (info.xmms2.playlist) {
2183 free(info.xmms2.playlist);
2184 info.xmms2.playlist = 0;
2187 case OBJ_xmms2_smart:
2188 if (info.xmms2.artist) {
2189 free(info.xmms2.artist);
2190 info.xmms2.artist = 0;
2192 if (info.xmms2.title) {
2193 free(info.xmms2.title);
2194 info.xmms2.title = 0;
2196 if (info.xmms2.url) {
2197 free(info.xmms2.url);
2203 case OBJ_bmpx_title:
2204 case OBJ_bmpx_artist:
2205 case OBJ_bmpx_album:
2206 case OBJ_bmpx_track:
2208 case OBJ_bmpx_bitrate:
2213 free(obj->data.rss.uri);
2214 free(obj->data.rss.action);
2223 case OBJ_battery_time:
2226 #endif /* !__OpenBSD__ */
2230 case OBJ_execigraph:
2231 free(obj->data.execi.cmd);
2232 free(obj->data.execi.buffer);
2235 free(obj->data.texeci.cmd);
2236 free(obj->data.texeci.buffer);
2238 case OBJ_nameserver:
2243 if (info.first_process) {
2244 free_all_processes();
2245 info.first_process = NULL;
2250 free(obj->data.hddtemp.dev);
2251 free(obj->data.hddtemp.addr);
2252 free(obj->data.hddtemp.temp);
2255 case OBJ_entropy_avail:
2256 case OBJ_entropy_poolsize:
2257 case OBJ_entropy_bar:
2259 case OBJ_user_names:
2260 if (info.users.names) {
2261 free(info.users.names);
2262 info.users.names = 0;
2265 case OBJ_user_terms:
2266 if (info.users.terms) {
2267 free(info.users.terms);
2268 info.users.terms = 0;
2271 case OBJ_user_times:
2272 if (info.users.times) {
2273 free(info.users.times);
2274 info.users.times = 0;
2279 case OBJ_smapi_bat_perc:
2282 case OBJ_if_smapi_bat_installed:
2283 free(obj->data.ifblock.s);
2284 free(obj->data.ifblock.str);
2293 case OBJ_mpd_artist:
2295 case OBJ_mpd_random:
2296 case OBJ_mpd_repeat:
2298 case OBJ_mpd_bitrate:
2299 case OBJ_mpd_status:
2302 case OBJ_mpd_password:
2304 case OBJ_mpd_elapsed:
2305 case OBJ_mpd_length:
2309 case OBJ_mpd_percent:
2311 free_mpd_vars(&info.mpd);
2316 free(text_object_list->text_objects);
2317 text_object_list->text_objects = NULL;
2318 text_object_list->text_object_count = 0;
2321 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2326 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2327 *a = mixer_init(buf1);
2328 scan_bar(arg + n, w, h);
2330 *a = mixer_init(NULL);
2331 scan_bar(arg, w, h);
2335 /* strip a leading /dev/ if any */
2336 #define DEV_NAME(x) x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 \
2339 /* construct_text_object() creates a new text_object */
2340 static struct text_object *construct_text_object(const char *s,
2341 const char *arg, unsigned int object_count,
2342 struct text_object *text_objects, long line)
2344 // struct text_object *obj = new_text_object();
2345 struct text_object *obj = new_text_object_internal();
2349 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
2350 obj->type = OBJ_##a; need_mask |= (1 << n); {
2351 #define END } } else
2355 obj->type = OBJ_color;
2356 obj->data.l = get_x11_color(s);
2360 OBJ(freq, INFO_FREQ)
2363 obj->data.i = open_acpi_temperature(arg);
2364 END OBJ(acpitempf, 0)
2365 obj->data.i = open_acpi_temperature(arg);
2366 END OBJ(acpiacadapter, 0)
2367 END OBJ(freq, INFO_FREQ)
2368 #endif /* !__OpenBSD__ */
2370 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2371 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2372 obj->data.cpu_index = 1;
2373 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
2374 "Displaying the clock for CPU 1."); */
2376 obj->data.cpu_index = atoi(&arg[0]);
2379 END OBJ(freq_g, INFO_FREQ)
2381 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2382 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2383 obj->data.cpu_index = 1;
2384 /* ERR("freq_g: Invalid CPU number or you don't have that many "
2385 "CPUs! Displaying the clock for CPU 1."); */
2387 obj->data.cpu_index = atoi(&arg[0]);
2390 #if defined(__linux__)
2391 END OBJ(voltage_mv, 0)
2393 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2394 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2395 obj->data.cpu_index = 1;
2396 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
2397 "CPUs! Displaying voltage for CPU 1."); */
2399 obj->data.cpu_index = atoi(&arg[0]);
2402 END OBJ(voltage_v, 0)
2404 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
2405 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
2406 obj->data.cpu_index = 1;
2407 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
2408 "CPUs! Displaying voltage for CPU 1."); */
2410 obj->data.cpu_index = atoi(&arg[0]);
2415 END OBJ(wireless_essid, INFO_NET)
2417 obj->data.net = get_net_stat(arg);
2419 CRIT_ERR("wireless_essid: needs an argument");
2421 END OBJ(wireless_mode, INFO_NET)
2423 obj->data.net = get_net_stat(arg);
2425 CRIT_ERR("wireless_mode: needs an argument");
2427 END OBJ(wireless_bitrate, INFO_NET)
2429 obj->data.net = get_net_stat(arg);
2431 CRIT_ERR("wireless_bitrate: needs an argument");
2433 END OBJ(wireless_ap, INFO_NET)
2435 obj->data.net = get_net_stat(arg);
2437 CRIT_ERR("wireless_ap: needs an argument");
2439 END OBJ(wireless_link_qual, INFO_NET)
2441 obj->data.net = get_net_stat(arg);
2443 CRIT_ERR("wireless_link_qual: needs an argument");
2445 END OBJ(wireless_link_qual_max, INFO_NET)
2447 obj->data.net = get_net_stat(arg);
2449 CRIT_ERR("wireless_link_qual_max: needs an argument");
2451 END OBJ(wireless_link_qual_perc, INFO_NET)
2453 obj->data.net = get_net_stat(arg);
2455 CRIT_ERR("wireless_link_qual_perc: needs an argument");
2457 END OBJ(wireless_link_bar, INFO_NET)
2459 arg = scan_bar(arg, &obj->a, &obj->b);
2460 obj->data.net = get_net_stat(arg);
2462 CRIT_ERR("wireless_link_bar: needs an argument");
2464 #endif /* HAVE_IWLIB */
2466 #endif /* __linux__ */
2467 END OBJ(freq_dyn, 0)
2468 END OBJ(freq_dyn_g, 0)
2476 sscanf(arg, "%63s", bat);
2478 strcpy(bat, "BAT0");
2480 obj->data.s = strndup(bat, text_buffer_size);
2481 END OBJ(battery_time, 0)
2485 sscanf(arg, "%63s", bat);
2487 strcpy(bat, "BAT0");
2489 obj->data.s = strndup(bat, text_buffer_size);
2490 END OBJ(battery_percent, 0)
2494 sscanf(arg, "%63s", bat);
2496 strcpy(bat, "BAT0");
2498 obj->data.s = strndup(bat, text_buffer_size);
2499 END OBJ(battery_bar, 0)
2503 arg = scan_bar(arg, &obj->a, &obj->b);
2504 sscanf(arg, "%63s", bat);
2506 strcpy(bat, "BAT0");
2508 obj->data.s = strndup(bat, text_buffer_size);
2509 #endif /* !__OpenBSD__ */
2511 #if defined(__linux__)
2512 END OBJ(disk_protect, 0)
2514 obj->data.s = strndup(DEV_NAME(arg), text_buffer_size);
2516 CRIT_ERR("disk_protect needs an argument");
2517 END OBJ(i8k_version, INFO_I8K)
2518 END OBJ(i8k_bios, INFO_I8K)
2519 END OBJ(i8k_serial, INFO_I8K)
2520 END OBJ(i8k_cpu_temp, INFO_I8K)
2521 END OBJ(i8k_cpu_tempf, INFO_I8K)
2522 END OBJ(i8k_left_fan_status, INFO_I8K)
2523 END OBJ(i8k_right_fan_status, INFO_I8K)
2524 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2525 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2526 END OBJ(i8k_ac_status, INFO_I8K)
2527 END OBJ(i8k_buttons_status, INFO_I8K)
2529 END OBJ(ibm_temps, 0)
2531 CRIT_ERR("ibm_temps: needs an argument");
2533 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
2534 obj->data.sensor = 0;
2535 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
2536 "Using 0 (CPU temp sensor).");
2538 obj->data.sensor = atoi(&arg[0]);
2539 END OBJ(ibm_volume, 0)
2540 END OBJ(ibm_brightness, 0)
2542 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2543 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2546 ERR("if_up needs an argument");
2547 obj->data.ifblock.s = 0;
2549 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2550 blockstart[blockdepth] = object_count;
2551 obj->data.ifblock.pos = object_count + 2;
2554 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2555 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2557 blockstart[blockdepth] = object_count;
2558 obj->data.ifblock.pos = object_count + 2;
2560 END OBJ(ioscheduler, 0)
2562 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
2565 obj->data.s = strndup(DEV_NAME(arg), text_buffer_size);
2566 END OBJ(laptop_mode, 0)
2567 END OBJ(pb_battery, 0)
2568 if (arg && strcmp(arg, "status") == 0) {
2569 obj->data.i = PB_BATT_STATUS;
2570 } else if (arg && strcmp(arg, "percent") == 0) {
2571 obj->data.i = PB_BATT_PERCENT;
2572 } else if (arg && strcmp(arg, "time") == 0) {
2573 obj->data.i = PB_BATT_TIME;
2575 ERR("pb_battery: needs one argument: status, percent or time");
2580 #endif /* __linux__ */
2581 #if defined(__OpenBSD__)
2582 END OBJ(obsd_sensors_temp, 0)
2584 CRIT_ERR("obsd_sensors_temp: needs an argument");
2586 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2587 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2588 obj->data.sensor = 0;
2589 ERR("Invalid temperature sensor number!");
2591 obj->data.sensor = atoi(&arg[0]);
2592 END OBJ(obsd_sensors_fan, 0)
2594 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
2597 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2598 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2599 obj->data.sensor = 0;
2600 ERR("Invalid fan sensor number!");
2602 obj->data.sensor = atoi(&arg[0]);
2603 END OBJ(obsd_sensors_volt, 0)
2605 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
2608 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
2609 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
2610 obj->data.sensor = 0;
2611 ERR("Invalid voltage sensor number!");
2613 obj->data.sensor = atoi(&arg[0]);
2614 END OBJ(obsd_vendor, 0)
2615 END OBJ(obsd_product, 0)
2616 #endif /* __OpenBSD__ */
2617 END OBJ(buffers, INFO_BUFFERS)
2618 END OBJ(cached, INFO_BUFFERS)
2619 END OBJ(cpu, INFO_CPU)
2621 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2622 obj->data.cpu_index = atoi(&arg[3]);
2625 obj->data.cpu_index = 0;
2628 obj->data.cpu_index = 0;
2630 END OBJ(cpubar, INFO_CPU)
2632 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2633 obj->data.cpu_index = atoi(&arg[3]);
2636 obj->data.cpu_index = 0;
2638 scan_bar(arg, &obj->a, &obj->b);
2640 scan_bar(arg, &obj->a, &obj->b);
2641 obj->data.cpu_index = 0;
2643 END OBJ(cpugraph, INFO_CPU)
2644 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2648 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
2649 obj->data.cpu_index = atoi(&buf[3]);
2651 obj->data.cpu_index = 0;
2655 END OBJ(loadgraph, INFO_LOADAVG)
2656 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2661 r = sscanf(arg, "%d", &a);
2663 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2666 #if defined(__linux__)
2667 END OBJ(diskio, INFO_DISKIO)
2669 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2671 obj->data.diskio = NULL;
2673 END OBJ(diskio_read, INFO_DISKIO)
2675 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2677 obj->data.diskio = NULL;
2679 END OBJ(diskio_write, INFO_DISKIO)
2681 obj->data.diskio = prepare_diskio_stat(DEV_NAME(arg));
2683 obj->data.diskio = NULL;
2685 END OBJ(diskiograph, INFO_DISKIO)
2686 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2690 obj->data.diskio = prepare_diskio_stat(buf);
2693 obj->data.diskio = NULL;
2695 END OBJ(diskiograph_read, INFO_DISKIO)
2696 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2700 obj->data.diskio = prepare_diskio_stat(buf);
2703 obj->data.diskio = NULL;
2705 END OBJ(diskiograph_write, INFO_DISKIO)
2706 char *buf = scan_graph(DEV_NAME(arg), &obj->a, &obj->b, &obj->c, &obj->d,
2710 obj->data.diskio = prepare_diskio_stat(buf);
2713 obj->data.diskio = NULL;
2718 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2721 obj->data.l = color0;
2723 obj->data.l = color1;
2725 obj->data.l = color2;
2727 obj->data.l = color3;
2729 obj->data.l = color4;
2731 obj->data.l = color5;
2733 obj->data.l = color6;
2735 obj->data.l = color7;
2737 obj->data.l = color8;
2739 obj->data.l = color9;
2741 obj->data.s = scan_font(arg);
2742 END OBJ(conky_version, 0)
2743 END OBJ(conky_build_date, 0)
2744 END OBJ(conky_build_arch, 0)
2745 END OBJ(downspeed, INFO_NET)
2747 obj->data.net = get_net_stat(arg);
2749 CRIT_ERR("downspeed needs argument");
2751 END OBJ(downspeedf, INFO_NET)
2753 obj->data.net = get_net_stat(arg);
2755 CRIT_ERR("downspeedf needs argument");
2757 END OBJ(downspeedgraph, INFO_NET)
2758 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2762 obj->data.net = get_net_stat(buf);
2767 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos =
2769 blockstart[blockdepth - 1] = object_count;
2770 obj->data.ifblock.pos = object_count + 2;
2772 ERR("$else: no matching $if_*");
2777 text_objects[blockstart[blockdepth]].data.ifblock.pos =
2780 ERR("$endif: no matching $if_*");
2783 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2786 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2788 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2790 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2791 END OBJ(execgraph, 0)
2792 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2793 END OBJ(execibar, 0)
2796 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2799 ERR("${execibar <interval> command}");
2800 obj->type = OBJ_text;
2801 snprintf(buf, 256, "${%s}", s);
2802 obj->data.s = strndup(buf, text_buffer_size);
2804 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2806 END OBJ(execigraph, 0)
2809 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2812 ERR("${execigraph <interval> command}");
2813 obj->type = OBJ_text;
2814 snprintf(buf, 256, "${%s}", s);
2815 obj->data.s = strndup(buf, text_buffer_size);
2817 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2822 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2825 ERR("${execi <interval> command}");
2826 obj->type = OBJ_text;
2827 snprintf(buf, 256, "${%s}", s);
2828 obj->data.s = strndup(buf, text_buffer_size);
2830 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2831 obj->data.execi.buffer = malloc(text_buffer_size);
2836 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2839 ERR("${execi <interval> command}");
2840 obj->type = OBJ_text;
2841 snprintf(buf, 256, "${%s}", s);
2842 obj->data.s = strndup(buf, text_buffer_size);
2844 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2845 obj->data.execi.buffer = malloc(text_buffer_size);
2850 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2853 ERR("${texeci <interval> command}");
2854 obj->type = OBJ_text;
2855 snprintf(buf, 256, "${%s}", s);
2856 obj->data.s = strndup(buf, text_buffer_size);
2858 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
2859 obj->data.texeci.buffer = malloc(text_buffer_size);
2861 obj->data.texeci.p_timed_thread = NULL;
2862 END OBJ(pre_exec, 0)
2863 obj->type = OBJ_text;
2865 FILE *fp = popen(arg, "r");
2869 n = fread(buf, 1, 2048, fp);
2872 if (n && buf[n - 1] == '\n') {
2878 obj->data.s = strndup(buf, text_buffer_size);
2880 obj->data.s = strndup("", text_buffer_size);
2883 END OBJ(fs_bar, INFO_FS)
2884 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2886 while (isspace(*arg)) {
2895 obj->data.fsbar.fs = prepare_fs_stat(arg);
2896 END OBJ(fs_bar_free, INFO_FS)
2897 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2899 while (isspace(*arg)) {
2909 obj->data.fsbar.fs = prepare_fs_stat(arg);
2910 END OBJ(fs_free, INFO_FS)
2914 obj->data.fs = prepare_fs_stat(arg);
2915 END OBJ(fs_used_perc, INFO_FS)
2919 obj->data.fs = prepare_fs_stat(arg);
2920 END OBJ(fs_free_perc, INFO_FS)
2924 obj->data.fs = prepare_fs_stat(arg);
2925 END OBJ(fs_size, INFO_FS)
2929 obj->data.fs = prepare_fs_stat(arg);
2930 END OBJ(fs_type, INFO_FS)
2934 obj->data.fs = prepare_fs_stat(arg);
2935 END OBJ(fs_used, INFO_FS)
2939 obj->data.fs = prepare_fs_stat(arg);
2941 obj->data.i = arg ? atoi(arg) : 1;
2942 END OBJ(nameserver, INFO_DNS)
2943 obj->data.i = arg ? atoi(arg) : 0;
2945 obj->data.i = arg ? atoi(arg) : 1;
2947 obj->data.i = arg ? atoi(arg) : 1;
2951 ERR("goto needs arguments");
2952 obj->type = OBJ_text;
2953 obj->data.s = strndup("${goto}", text_buffer_size);
2957 obj->data.i = atoi(arg);
2963 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2964 sscanf(arg, "%d", &b);
2970 obj->data.pair.a = a;
2971 obj->data.pair.b = b;
2974 END OBJ(i2c, INFO_SYSFS)
2975 char buf1[64], buf2[64];
2979 ERR("i2c needs arguments");
2980 obj->type = OBJ_text;
2981 // obj->data.s = strndup("${i2c}", text_buffer_size);
2985 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2986 /* if scanf couldn't read three values, read type and num and use
2988 sscanf(arg, "%63s %d", buf2, &n);
2989 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
2990 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2991 strncpy(obj->data.sysfs.type, buf2, 63);
2993 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
2994 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2995 strncpy(obj->data.sysfs.type, buf2, 63);
2998 END OBJ(platform, INFO_SYSFS)
2999 char buf1[64], buf2[64];
3003 ERR("platform needs arguments");
3004 obj->type = OBJ_text;
3008 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3009 /* if scanf couldn't read three values, read type and num and use
3011 sscanf(arg, "%63s %d", buf2, &n);
3012 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
3013 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3014 strncpy(obj->data.sysfs.type, buf2, 63);
3016 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
3017 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3018 strncpy(obj->data.sysfs.type, buf2, 63);
3021 END OBJ(hwmon, INFO_SYSFS)
3022 char buf1[64], buf2[64];
3026 ERR("hwmon needs argumanets");
3027 obj->type = OBJ_text;
3031 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
3032 /* if scanf couldn't read three values, read type and num and use
3034 sscanf(arg, "%63s %d", buf2, &n);
3035 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
3036 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3037 strncpy(obj->data.sysfs.type, buf2, 63);
3039 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
3040 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3041 strncpy(obj->data.sysfs.type, buf2, 63);
3043 #endif /* !__OpenBSD__ */
3045 END OBJ(top, INFO_TOP)
3050 ERR("top needs arguments");
3051 obj->type = OBJ_text;
3052 // obj->data.s = strndup("${top}", text_buffer_size);
3055 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3056 if (strcmp(buf, "name") == 0) {
3057 obj->data.top.type = TOP_NAME;
3058 } else if (strcmp(buf, "cpu") == 0) {
3059 obj->data.top.type = TOP_CPU;
3060 } else if (strcmp(buf, "pid") == 0) {
3061 obj->data.top.type = TOP_PID;
3062 } else if (strcmp(buf, "mem") == 0) {
3063 obj->data.top.type = TOP_MEM;
3064 } else if (strcmp(buf, "time") == 0) {
3065 obj->data.top.type = TOP_TIME;
3066 } else if (strcmp(buf, "mem_res") == 0) {
3067 obj->data.top.type = TOP_MEM_RES;
3068 } else if (strcmp(buf, "mem_vsize") == 0) {
3069 obj->data.top.type = TOP_MEM_VSIZE;
3071 ERR("invalid arg for top");
3074 if (n < 1 || n > 10) {
3075 CRIT_ERR("invalid arg for top");
3078 obj->data.top.num = n - 1;
3082 ERR("invalid args given for top");
3085 END OBJ(top_mem, INFO_TOP)
3090 ERR("top_mem needs arguments");
3091 obj->type = OBJ_text;
3092 obj->data.s = strndup("${top_mem}", text_buffer_size);
3095 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
3096 if (strcmp(buf, "name") == 0) {
3097 obj->data.top.type = TOP_NAME;
3098 } else if (strcmp(buf, "cpu") == 0) {
3099 obj->data.top.type = TOP_CPU;
3100 } else if (strcmp(buf, "pid") == 0) {
3101 obj->data.top.type = TOP_PID;
3102 } else if (strcmp(buf, "mem") == 0) {
3103 obj->data.top.type = TOP_MEM;
3104 } else if (strcmp(buf, "time") == 0) {
3105 obj->data.top.type = TOP_TIME;
3106 } else if (strcmp(buf, "mem_res") == 0) {
3107 obj->data.top.type = TOP_MEM_RES;
3108 } else if (strcmp(buf, "mem_vsize") == 0) {
3109 obj->data.top.type = TOP_MEM_VSIZE;
3111 ERR("invalid arg for top");
3114 if (n < 1 || n > 10) {
3115 CRIT_ERR("invalid arg for top");
3118 obj->data.top.num = n - 1;
3122 ERR("invalid args given for top");
3125 END OBJ(addr, INFO_NET)
3127 obj->data.net = get_net_stat(arg);
3129 CRIT_ERR("addr needs argument");
3131 #if defined(__linux__)
3132 END OBJ(addrs, INFO_NET)
3134 obj->data.net = get_net_stat(arg);
3136 CRIT_ERR("addrs needs argument");
3138 #endif /* __linux__ */
3145 ERR("tail needs arguments");
3146 obj->type = OBJ_text;
3147 obj->data.s = strndup("${tail}", text_buffer_size);
3150 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3151 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3152 CRIT_ERR("invalid arg for tail, number of lines must be "
3153 "between 1 and %i", MAX_TAIL_LINES);
3159 obj->data.tail.fd = -1;
3161 if (stat(buf, &st) == 0) {
3162 if (S_ISFIFO(st.st_mode)) {
3163 fd = open(buf, O_RDONLY | O_NONBLOCK);
3166 CRIT_ERR("tail logfile does not exist, or you do "
3167 "not have correct permissions");
3170 obj->data.tail.fd = fd;
3172 fp = fopen(buf, "r");
3176 if (fp || obj->data.tail.fd != -1) {
3177 obj->data.tail.logfile = malloc(text_buffer_size);
3178 strcpy(obj->data.tail.logfile, buf);
3179 obj->data.tail.wantedlines = n1;
3180 obj->data.tail.interval = update_interval * 2;
3182 if (obj->data.tail.fd == -1) {
3187 CRIT_ERR("tail logfile does not exist, or you do not have "
3188 "correct permissions");
3191 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3192 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3193 CRIT_ERR("invalid arg for tail, number of lines must be "
3194 "between 1 and %i", MAX_TAIL_LINES);
3196 } else if (n2 < 1 || n2 < update_interval) {
3197 CRIT_ERR("invalid arg for tail, interval must be greater than "
3198 "0 and Conky's interval");
3204 obj->data.tail.fd = -1;
3206 if (stat(buf, &st) == 0) {
3207 if (S_ISFIFO(st.st_mode)) {
3208 fd = open(buf, O_RDONLY | O_NONBLOCK);
3211 CRIT_ERR("tail logfile does not exist, or you do "
3212 "not have correct permissions");
3215 obj->data.tail.fd = fd;
3217 fp = fopen(buf, "r");
3221 if (fp || obj->data.tail.fd != -1) {
3222 obj->data.tail.logfile = malloc(text_buffer_size);
3223 strcpy(obj->data.tail.logfile, buf);
3224 obj->data.tail.wantedlines = n1;
3225 obj->data.tail.interval = n2;
3227 if (obj->data.tail.fd == -1) {
3232 CRIT_ERR("tail logfile does not exist, or you do not have "
3233 "correct permissions");
3237 ERR("invalid args given for tail");
3240 /* asumming all else worked */
3241 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3247 ERR("head needs arguments");
3248 obj->type = OBJ_text;
3249 obj->data.s = strndup("${head}", text_buffer_size);
3252 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
3253 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3254 CRIT_ERR("invalid arg for head, number of lines must be "
3255 "between 1 and %i", MAX_TAIL_LINES);
3260 fp = fopen(buf, "r");
3262 obj->data.tail.logfile = malloc(text_buffer_size);
3263 strcpy(obj->data.tail.logfile, buf);
3264 obj->data.tail.wantedlines = n1;
3265 obj->data.tail.interval = update_interval * 2;
3269 CRIT_ERR("head logfile does not exist, or you do not have "
3270 "correct permissions");
3273 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
3274 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
3275 CRIT_ERR("invalid arg for head, number of lines must be "
3276 "between 1 and %i", MAX_TAIL_LINES);
3278 } else if (n2 < 1 || n2 < update_interval) {
3279 CRIT_ERR("invalid arg for head, interval must be greater than "
3280 "0 and Conky's interval");
3285 fp = fopen(buf, "r");
3287 obj->data.tail.logfile = malloc(text_buffer_size);
3288 strcpy(obj->data.tail.logfile, buf);
3289 obj->data.tail.wantedlines = n1;
3290 obj->data.tail.interval = n2;
3294 CRIT_ERR("head logfile does not exist, or you do not have "
3295 "correct permissions");
3299 ERR("invalid args given for head");
3302 /* asumming all else worked */
3303 obj->data.tail.buffer = malloc(text_buffer_size * 20);
3304 END OBJ(loadavg, INFO_LOADAVG)
3305 int a = 1, b = 2, c = 3, r = 3;
3308 r = sscanf(arg, "%d %d %d", &a, &b, &c);
3309 if (r >= 3 && (c < 1 || c > 3)) {
3312 if (r >= 2 && (b < 1 || b > 3)) {
3315 if (r >= 1 && (a < 1 || a > 3)) {
3319 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
3320 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
3321 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
3322 END OBJ(if_empty, 0)
3323 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3324 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3327 ERR("if_empty needs an argument");
3328 obj->data.ifblock.s = 0;
3330 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3332 blockstart[blockdepth] = object_count;
3333 obj->data.ifblock.pos = object_count + 2;
3335 END OBJ(if_existing, 0)
3336 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3337 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3340 ERR("if_existing needs an argument or two");
3341 obj->data.ifblock.s = NULL;
3342 obj->data.ifblock.str = NULL;
3344 char buf1[256], buf2[256];
3345 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3348 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
3349 obj->data.ifblock.str = NULL;
3351 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
3352 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
3355 blockstart[blockdepth] = object_count;
3356 obj->data.ifblock.pos = object_count + 2;
3358 END OBJ(if_mounted, 0)
3359 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3360 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3363 ERR("if_mounted needs an argument");
3364 obj->data.ifblock.s = 0;
3366 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3368 blockstart[blockdepth] = object_count;
3369 obj->data.ifblock.pos = object_count + 2;
3371 END OBJ(if_running, 0)
3372 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3373 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3378 snprintf(buf, 256, "pidof %s >/dev/null", arg);
3379 obj->data.ifblock.s = strndup(buf, text_buffer_size);
3381 ERR("if_running needs an argument");
3382 obj->data.ifblock.s = 0;
3384 blockstart[blockdepth] = object_count;
3385 obj->data.ifblock.pos = object_count + 2;
3391 char box[256], dst[256];
3395 /* Kapil: Changed from MAIL_FILE to
3396 current_mail_spool since the latter
3397 is a copy of the former if undefined
3398 but the latter should take precedence
3400 strncpy(box, current_mail_spool, sizeof(box));
3402 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3404 strncpy(box, arg, sizeof(box));
3408 variable_substitute(box, dst, sizeof(dst));
3409 obj->data.local_mail.box = strndup(dst, text_buffer_size);
3410 obj->data.local_mail.interval = n1;
3411 END OBJ(mboxscan, 0)
3412 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
3413 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
3414 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
3415 obj->data.mboxscan.output[0] = 1;
3416 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
3417 END OBJ(mem, INFO_MEM)
3418 END OBJ(memmax, INFO_MEM)
3419 END OBJ(memperc, INFO_MEM)
3420 END OBJ(membar, INFO_MEM)
3421 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3422 END OBJ(memgraph, INFO_MEM)
3423 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3429 END OBJ(mixer, INFO_MIXER)
3430 obj->data.l = mixer_init(arg);
3431 END OBJ(mixerl, INFO_MIXER)
3432 obj->data.l = mixer_init(arg);
3433 END OBJ(mixerr, INFO_MIXER)
3434 obj->data.l = mixer_init(arg);
3435 END OBJ(mixerbar, INFO_MIXER)
3436 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3437 &obj->data.mixerbar.h);
3438 END OBJ(mixerlbar, INFO_MIXER)
3439 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3440 &obj->data.mixerbar.h);
3441 END OBJ(mixerrbar, INFO_MIXER)
3442 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
3443 &obj->data.mixerbar.h);
3444 END OBJ(new_mails, 0)
3446 char box[256], dst[256];
3450 /* Kapil: Changed from MAIL_FILE to
3451 current_mail_spool since the latter
3452 is a copy of the former if undefined
3453 but the latter should take precedence
3455 strncpy(box, current_mail_spool, sizeof(box));
3457 if (sscanf(arg, "%s %f", box, &n1) != 2) {
3459 strncpy(box, arg, sizeof(box));
3463 variable_substitute(box, dst, sizeof(dst));
3464 obj->data.local_mail.box = strndup(dst, text_buffer_size);
3465 obj->data.local_mail.interval = n1;
3466 END OBJ(nodename, 0)
3467 END OBJ(processes, INFO_PROCS)
3468 END OBJ(running_processes, INFO_RUN_PROCS)
3469 END OBJ(shadecolor, 0)
3471 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
3473 END OBJ(outlinecolor, 0)
3475 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
3477 END OBJ(stippled_hr, 0)
3479 int a = stippled_borders, b = 1;
3482 if (sscanf(arg, "%d %d", &a, &b) != 2) {
3483 sscanf(arg, "%d", &b);
3489 obj->data.pair.a = a;
3490 obj->data.pair.b = b;
3492 END OBJ(swap, INFO_MEM)
3493 END OBJ(swapmax, INFO_MEM)
3494 END OBJ(swapperc, INFO_MEM)
3495 END OBJ(swapbar, INFO_MEM)
3496 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3499 END OBJ(temp1, INFO_SYSFS)
3500 obj->type = OBJ_i2c;
3501 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
3502 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3503 END OBJ(temp2, INFO_SYSFS)
3504 obj->type = OBJ_i2c;
3505 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
3506 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
3509 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
3511 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
3513 char buf1[256], buf2[256], *fmt, *tz;
3517 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
3527 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
3528 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
3530 END OBJ(iconv_start, 0)
3531 if (iconv_converting) {
3532 CRIT_ERR("You must stop your last iconv conversion before "
3533 "starting another");
3536 char iconv_from[CODEPAGE_LENGTH];
3537 char iconv_to[CODEPAGE_LENGTH];
3539 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
3540 CRIT_ERR("Invalid arguments for iconv_start");
3544 new_iconv = iconv_open(iconv_to, iconv_from);
3545 if (new_iconv == (iconv_t) (-1)) {
3546 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
3548 obj->a = register_iconv(&new_iconv);
3549 iconv_converting = 1;
3553 CRIT_ERR("Iconv requires arguments");
3555 END OBJ(iconv_stop, 0)
3556 iconv_converting = 0;
3559 END OBJ(totaldown, INFO_NET)
3561 obj->data.net = get_net_stat(arg);
3563 CRIT_ERR("totaldown needs argument");
3565 END OBJ(totalup, INFO_NET)
3566 obj->data.net = get_net_stat(arg);
3568 obj->data.net = get_net_stat(arg);
3570 CRIT_ERR("totalup needs argument");
3574 obj->data.i = arg ? atoi(arg) : 0;
3576 obj->data.i = arg ? atoi(arg) : 0;
3577 END OBJ(upspeed, INFO_NET)
3579 obj->data.net = get_net_stat(arg);
3581 CRIT_ERR("upspeed needs argument");
3583 END OBJ(upspeedf, INFO_NET)
3585 obj->data.net = get_net_stat(arg);
3587 CRIT_ERR("upspeedf needs argument");
3590 END OBJ(upspeedgraph, INFO_NET)
3591 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3595 obj->data.net = get_net_stat(buf);
3598 END OBJ(uptime_short, INFO_UPTIME)
3599 END OBJ(uptime, INFO_UPTIME)
3600 END OBJ(user_names, INFO_USERS)
3601 END OBJ(user_times, INFO_USERS)
3602 END OBJ(user_terms, INFO_USERS)
3603 END OBJ(user_number, INFO_USERS)
3604 #if defined(__linux__)
3605 END OBJ(gw_iface, INFO_GW)
3606 END OBJ(gw_ip, INFO_GW)
3607 END OBJ(if_gw, INFO_GW)
3608 #endif /* !__linux__ */
3610 END OBJ(adt746xcpu, 0)
3611 END OBJ(adt746xfan, 0)
3612 #endif /* !__OpenBSD__ */
3613 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
3614 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
3615 END OBJ(apm_adapter, 0)
3616 END OBJ(apm_battery_life, 0)
3617 END OBJ(apm_battery_time, 0)
3618 #endif /* __FreeBSD__ */
3619 END OBJ(imap_unseen, 0)
3622 obj->data.mail = parse_mail_args(IMAP, arg);
3623 obj->global_mode = 0;
3625 obj->global_mode = 1;
3627 END OBJ(imap_messages, 0)
3630 obj->data.mail = parse_mail_args(IMAP, arg);
3631 obj->global_mode = 0;
3633 obj->global_mode = 1;
3635 END OBJ(pop3_unseen, 0)
3638 obj->data.mail = parse_mail_args(POP3, arg);
3639 obj->global_mode = 0;
3641 obj->global_mode = 1;
3643 END OBJ(pop3_used, 0)
3646 obj->data.mail = parse_mail_args(POP3, arg);
3647 obj->global_mode = 0;
3649 obj->global_mode = 1;
3654 obj->data.s = strndup(arg, text_buffer_size);
3656 ERR("smapi needs an argument");
3657 END OBJ(if_smapi_bat_installed, 0)
3658 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
3659 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
3662 ERR("if_smapi_bat_installed needs an argument");
3663 obj->data.ifblock.s = 0;
3665 obj->data.ifblock.s = strndup(arg, text_buffer_size);
3666 blockstart[blockdepth] = object_count;
3667 obj->data.ifblock.pos = object_count + 2;
3669 END OBJ(smapi_bat_perc, 0)
3671 obj->data.s = strndup(arg, text_buffer_size);
3673 ERR("smapi_bat_perc needs an argument");
3674 END OBJ(smapi_bat_bar, 0)
3677 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
3678 ERR("first argument to smapi_bat_bar must be an integer value");
3682 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
3685 ERR("if_smapi_bat_bar needs an argument");
3688 END OBJ(mpd_artist, INFO_MPD)
3689 END OBJ(mpd_title, INFO_MPD)
3691 sscanf(arg, "%d", &info.mpd.max_title_len);
3692 if (info.mpd.max_title_len > 0) {
3693 info.mpd.max_title_len++;
3695 CRIT_ERR("mpd_title: invalid length argument");
3698 info.mpd.max_title_len = 0;
3700 END OBJ(mpd_random, INFO_MPD)
3701 END OBJ(mpd_repeat, INFO_MPD)
3702 END OBJ(mpd_elapsed, INFO_MPD)
3703 END OBJ(mpd_length, INFO_MPD)
3704 END OBJ(mpd_track, INFO_MPD)
3705 END OBJ(mpd_name, INFO_MPD)
3706 END OBJ(mpd_file, INFO_MPD)
3707 END OBJ(mpd_percent, INFO_MPD)
3708 END OBJ(mpd_album, INFO_MPD)
3709 END OBJ(mpd_vol, INFO_MPD)
3710 END OBJ(mpd_bitrate, INFO_MPD)
3711 END OBJ(mpd_status, INFO_MPD)
3712 END OBJ(mpd_bar, INFO_MPD)
3713 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3714 END OBJ(mpd_smart, INFO_MPD)
3717 END OBJ(xmms2_artist, INFO_XMMS2)
3718 END OBJ(xmms2_album, INFO_XMMS2)
3719 END OBJ(xmms2_title, INFO_XMMS2)
3720 END OBJ(xmms2_genre, INFO_XMMS2)
3721 END OBJ(xmms2_comment, INFO_XMMS2)
3722 END OBJ(xmms2_url, INFO_XMMS2)
3723 END OBJ(xmms2_tracknr, INFO_XMMS2)
3724 END OBJ(xmms2_bitrate, INFO_XMMS2)
3725 END OBJ(xmms2_date, INFO_XMMS2)
3726 END OBJ(xmms2_id, INFO_XMMS2)
3727 END OBJ(xmms2_duration, INFO_XMMS2)
3728 END OBJ(xmms2_elapsed, INFO_XMMS2)
3729 END OBJ(xmms2_size, INFO_XMMS2)
3730 END OBJ(xmms2_status, INFO_XMMS2)
3731 END OBJ(xmms2_percent, INFO_XMMS2)
3732 END OBJ(xmms2_bar, INFO_XMMS2)
3733 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3734 END OBJ(xmms2_smart, INFO_XMMS2)
3735 END OBJ(xmms2_playlist, INFO_XMMS2)
3736 END OBJ(xmms2_timesplayed, INFO_XMMS2)
3739 END OBJ(audacious_status, INFO_AUDACIOUS)
3740 END OBJ(audacious_title, INFO_AUDACIOUS)
3742 sscanf(arg, "%d", &info.audacious.max_title_len);
3743 if (info.audacious.max_title_len > 0) {
3744 info.audacious.max_title_len++;
3746 CRIT_ERR("audacious_title: invalid length argument");
3749 END OBJ(audacious_length, INFO_AUDACIOUS)
3750 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
3751 END OBJ(audacious_position, INFO_AUDACIOUS)
3752 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
3753 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
3754 END OBJ(audacious_frequency, INFO_AUDACIOUS)
3755 END OBJ(audacious_channels, INFO_AUDACIOUS)
3756 END OBJ(audacious_filename, INFO_AUDACIOUS)
3757 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
3758 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
3759 END OBJ(audacious_bar, INFO_AUDACIOUS)
3760 scan_bar(arg, &obj->a, &obj->b);
3763 END OBJ(bmpx_title, INFO_BMPX)
3764 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3765 END OBJ(bmpx_artist, INFO_BMPX)
3766 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3767 END OBJ(bmpx_album, INFO_BMPX)
3768 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3769 END OBJ(bmpx_track, INFO_BMPX)
3770 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3771 END OBJ(bmpx_uri, INFO_BMPX)
3772 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3773 END OBJ(bmpx_bitrate, INFO_BMPX)
3774 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3779 int argc, delay, act_par;
3780 char *uri = (char *) malloc(128 * sizeof(char));
3781 char *action = (char *) malloc(64 * sizeof(char));
3783 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
3785 obj->data.rss.uri = uri;
3786 obj->data.rss.delay = delay;
3787 obj->data.rss.action = action;
3788 obj->data.rss.act_par = act_par;
3792 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
3798 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
3799 &obj->data.hddtemp.addr, &obj->data.hddtemp.port, &obj->data.hddtemp.temp)) {
3800 ERR("hddtemp needs arguments");
3801 obj->type = OBJ_text;
3802 obj->data.s = strndup("${hddtemp}", text_buffer_size);
3803 obj->data.hddtemp.update_time = 0;
3807 #ifdef TCP_PORT_MONITOR
3808 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3809 int argc, port_begin, port_end, item, connection_index;
3812 memset(itembuf, 0, sizeof(itembuf));
3813 connection_index = 0;
3814 /* massive argument checking */
3816 CRIT_ERR("tcp_portmon: needs arguments");
3818 argc = sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf,
3820 if ((argc != 3) && (argc != 4)) {
3821 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
3823 if ((port_begin < 1) || (port_begin > 65535) || (port_end < 1)
3824 || (port_end > 65535)) {
3825 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
3827 if (port_begin > port_end) {
3828 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
3830 if (strncmp(itembuf, "count", 31) == 0) {
3832 } else if (strncmp(itembuf, "rip", 31) == 0) {
3834 } else if (strncmp(itembuf, "rhost", 31) == 0) {
3836 } else if (strncmp(itembuf, "rport", 31) == 0) {
3838 } else if (strncmp(itembuf, "rservice", 31) == 0) {
3839 item = REMOTESERVICE;
3840 } else if (strncmp(itembuf, "lip", 31) == 0) {
3842 } else if (strncmp(itembuf, "lhost", 31) == 0) {
3844 } else if (strncmp(itembuf, "lport", 31) == 0) {
3846 } else if (strncmp(itembuf, "lservice", 31) == 0) {
3847 item = LOCALSERVICE;
3849 CRIT_ERR("tcp_portmon: invalid item specified");
3851 if ((argc == 3) && (item != COUNT)) {
3852 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" "
3855 if ((argc == 4) && (connection_index < 0)) {
3856 CRIT_ERR("tcp_portmon: connection index must be non-negative");
3858 /* ok, args looks good. save the text object data */
3859 obj->data.tcp_port_monitor.port_range_begin = (in_port_t) port_begin;
3860 obj->data.tcp_port_monitor.port_range_end = (in_port_t) port_end;
3861 obj->data.tcp_port_monitor.item = item;
3862 obj->data.tcp_port_monitor.connection_index = connection_index;
3864 /* if the port monitor collection hasn't been created,
3865 * we must create it */
3866 if (!info.p_tcp_port_monitor_collection) {
3867 info.p_tcp_port_monitor_collection =
3868 create_tcp_port_monitor_collection();
3869 if (!info.p_tcp_port_monitor_collection) {
3870 CRIT_ERR("tcp_portmon: unable to create port monitor "
3875 /* if a port monitor for this port does not exist,
3876 * create one and add it to the collection */
3877 if (find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
3878 port_begin, port_end) == NULL) {
3879 tcp_port_monitor_t *p_monitor = create_tcp_port_monitor(port_begin,
3880 port_end, &tcp_port_monitor_args);
3883 CRIT_ERR("tcp_portmon: unable to create port monitor");
3885 /* add the newly created monitor to the collection */
3886 if (insert_tcp_port_monitor_into_collection(
3887 info.p_tcp_port_monitor_collection, p_monitor) != 0) {
3888 CRIT_ERR("tcp_portmon: unable to add port monitor to "
3893 END OBJ(entropy_avail, INFO_ENTROPY)
3894 END OBJ(entropy_poolsize, INFO_ENTROPY)
3895 END OBJ(entropy_bar, INFO_ENTROPY)
3896 scan_bar(arg, &obj->a, &obj->b);
3900 CRIT_ERR("nvidia needs one argument "
3901 "[temp,threshold,gpufreq,memfreq,imagequality]");
3903 if (strcmp(arg, "temp") == 0)
3904 obj->data.nvidia.type = NV_TEMP;
3905 else if (strcmp(arg, "threshold") == 0)
3906 obj->data.nvidia.type = NV_TEMP_THRESHOLD;
3907 else if (strcmp(arg, "gpufreq") == 0)
3908 obj->data.nvidia.type = NV_GPU_FREQ;
3909 else if (strcmp(arg, "memfreq") == 0)
3910 obj->data.nvidia.type = NV_MEM_FREQ;
3911 else if (strcmp(arg, "imagequality") == 0)
3912 obj->data.nvidia.type = NV_IMAGE_QUALITY;
3914 CRIT_ERR("you have to give one of these arguments "
3915 "[temp,threshold,gpufreq,memfreq,imagequality");
3916 strncpy((char*)&obj->data.nvidia.arg, arg, 20);
3922 ERR("unknown variable %s", s);
3923 obj->type = OBJ_text;
3924 snprintf(buf, 256, "${%s}", s);
3925 obj->data.s = strndup(buf, text_buffer_size);
3932 static struct text_object *create_plain_text(const char *s)
3934 struct text_object *obj;
3936 if (s == NULL || *s == '\0') {
3940 obj = new_text_object_internal();
3942 obj->type = OBJ_text;
3943 obj->data.s = strndup(s, text_buffer_size);
3947 static struct text_object_list *extract_variable_text_internal(const char *const_p)
3949 struct text_object_list *retval;
3950 struct text_object *obj;
3951 char *p, *s, *orig_p;
3954 p = strndup(const_p, max_user_text);
3957 retval = malloc(sizeof(struct text_object_list));
3958 memset(retval, 0, sizeof(struct text_object_list));
3959 retval->text_object_count = 0;
3969 obj = create_plain_text(s);
3971 // allocate memory for the object
3972 retval->text_objects = realloc(retval->text_objects,
3973 sizeof(struct text_object) *
3974 (retval->text_object_count + 1));
3975 // assign the new object to the end of the list.
3976 memcpy(&retval->text_objects[retval->text_object_count++], obj,
3977 sizeof(struct text_object));
3989 /* variable is either $foo or ${foo} */
3991 unsigned int brl = 1, brr = 0;
3995 while (*p && brl != brr) {
4010 while (*p && (isalnum((int) *p) || *p == '_')) {
4015 /* copy variable to buffer */
4016 len = (p - s > 255) ? 255 : (p - s);
4017 strncpy(buf, s, len);
4027 /* if variable wasn't found in environment, use some special */
4033 if (strchr(buf, ' ')) {
4034 arg = strchr(buf, ' ');
4037 while (isspace((int) *arg)) {
4045 /* lowercase variable name */
4048 *tmp_p = tolower(*tmp_p);
4052 // create new object
4053 obj = construct_text_object(buf, arg,
4054 retval->text_object_count, retval->text_objects, line);
4056 // allocate memory for the object
4057 retval->text_objects = realloc(retval->text_objects,
4058 sizeof(struct text_object) *
4059 (retval->text_object_count + 1));
4060 // assign the new object to the end of the list.
4062 &retval->text_objects[retval->text_object_count++],
4063 obj, sizeof(struct text_object));
4069 obj = create_plain_text("$");
4071 // allocate memory for the object
4072 retval->text_objects = realloc(retval->text_objects,
4073 sizeof(struct text_object) *
4074 (retval->text_object_count + 1));
4075 // assign the new object to the end of the list.
4076 memcpy(&retval->text_objects[retval->text_object_count++],
4077 obj, sizeof(struct text_object));
4084 obj = create_plain_text(s);
4086 // allocate memory for the object
4087 retval->text_objects = realloc(retval->text_objects,
4088 sizeof(struct text_object) * (retval->text_object_count + 1));
4089 // assign the new object to the end of the list.
4090 memcpy(&retval->text_objects[retval->text_object_count++], obj,
4091 sizeof(struct text_object));
4096 ERR("one or more $endif's are missing");
4103 static void extract_variable_text(const char *p)
4105 free_text_objects(global_text_object_list);
4106 free(global_text_object_list);
4120 global_text_object_list = extract_variable_text_internal(p);
4123 struct text_object_list *parse_conky_vars(char *txt, char *p, struct information *cur)
4125 struct text_object_list *object_list =
4126 extract_variable_text_internal(txt);
4128 generate_text_internal(p, max_user_text, object_list, cur);
4132 /* Allows reading from a FIFO (i.e., /dev/xconsole).
4133 * The file descriptor is set to non-blocking which makes this possible.
4135 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
4136 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
4138 #define TAIL_PIPE_BUFSIZE 4096
4142 int fd = obj->data.tail.fd;
4145 char buf[TAIL_PIPE_BUFSIZE];
4146 ssize_t len = read(fd, buf, sizeof(buf));
4150 if (errno != EAGAIN) {
4151 strcpy(obj->data.tail.buffer, "Logfile Read Error");
4152 snprintf(dst, dst_size, "Logfile Read Error");
4156 } else if (len == 0) {
4157 strcpy(obj->data.tail.buffer, "Logfile Empty");
4158 snprintf(dst, dst_size, "Logfile Empty");
4162 for (line_len = 0, i = 0; i < len; i++) {
4166 if (buf[i] == '\n') {
4169 if (obj->data.tail.readlines > 0) {
4174 for (n = 0; obj->data.tail.buffer[n]; n++) {
4175 if (obj->data.tail.buffer[n] == '\n') {
4180 if (++olines < obj->data.tail.wantedlines) {
4186 p = obj->data.tail.buffer + first_line;
4187 pos = n - first_line;
4188 memmove(obj->data.tail.buffer,
4189 obj->data.tail.buffer + first_line, strlen(p));
4190 obj->data.tail.buffer[pos] = 0;
4196 p = buf + last_line;
4198 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
4199 obj->data.tail.buffer[pos + line_len] = 0;
4202 obj->data.tail.readlines = lines;
4210 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
4213 char *format_time(unsigned long timeval, const int width)
4216 unsigned long nt; // narrow time, for speed on 32-bit
4217 unsigned cc; // centiseconds
4218 unsigned nn; // multi-purpose whatever
4221 cc = nt % 100; // centiseconds past second
4222 nt /= 100; // total seconds
4223 nn = nt % 60; // seconds past the minute
4224 nt /= 60; // total minutes
4225 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
4227 return strndup(buf, text_buffer_size);
4229 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
4230 return strndup(buf, text_buffer_size);
4232 nn = nt % 60; // minutes past the hour
4233 nt /= 60; // total hours
4234 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
4235 return strndup(buf, text_buffer_size);
4237 nn = nt; // now also hours
4238 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
4239 return strndup(buf, text_buffer_size);
4241 nn /= 24; // now days
4242 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
4243 return strndup(buf, text_buffer_size);
4245 nn /= 7; // now weeks
4246 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
4247 return strndup(buf, text_buffer_size);
4249 // well shoot, this outta' fit...
4250 return strndup("<inf>", text_buffer_size);
4253 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
4254 //string has to end with \0 and it's length should fit in a int
4256 void remove_deleted_chars(char *string){
4258 while(string[i] != 0){
4259 if(string[i] == BACKSPACE){
4261 strcpy( &(string[i-1]), &(string[i+1]) );
4263 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
4268 static inline void format_media_player_time(char *buf, const int size,
4271 int days, hours, minutes;
4273 days = seconds / (24 * 60 * 60);
4274 seconds %= (24 * 60 * 60);
4275 hours = seconds / (60 * 60);
4276 seconds %= (60 * 60);
4277 minutes = seconds / 60;
4281 snprintf(buf, size, "%i days %i:%02i:%02i", days,
4282 hours, minutes, seconds);
4283 } else if (hours > 0) {
4284 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
4287 snprintf(buf, size, "%i:%02i", minutes, seconds);
4291 static void generate_text_internal(char *p, int p_max_size,
4292 struct text_object_list *text_object_list,
4293 struct information *cur)
4298 char buff_in[p_max_size];
4300 iconv_converting = 0;
4304 for (i = 0; i < text_object_list->text_object_count; i++) {
4305 struct text_object *obj = &(text_object_list->text_objects[i]);
4307 if (p_max_size < 1) {
4311 #define OBJ(a) break; case OBJ_##a:
4313 switch (obj->type) {
4315 ERR("not implemented obj type %d", obj->type);
4318 /* does anyone have decimals in acpi temperature? */
4319 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4320 round_to_int(get_acpi_temperature(obj->data.i)));
4323 /* does anyone have decimals in acpi temperature? */
4324 spaced_print(p, p_max_size, "%d", 5, "acpitemp",
4325 round_to_int((get_acpi_temperature(obj->data.i) + 40) *
4328 #endif /* !__OpenBSD__ */
4331 obj->a = get_freq(p, p_max_size, "%.0f", 1,
4332 obj->data.cpu_index);
4338 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
4339 obj->data.cpu_index);
4341 /* OpenBSD has no such flag (SUSv2) */
4342 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
4343 obj->data.cpu_index);
4347 #if defined(__linux__)
4350 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
4351 obj->data.cpu_index);
4356 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
4357 obj->data.cpu_index);
4362 OBJ(wireless_essid) {
4363 snprintf(p, p_max_size, "%s", obj->data.net->essid);
4365 OBJ(wireless_mode) {
4366 snprintf(p, p_max_size, "%s", obj->data.net->mode);
4368 OBJ(wireless_bitrate) {
4369 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
4372 snprintf(p, p_max_size, "%s", obj->data.net->ap);
4374 OBJ(wireless_link_qual) {
4375 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
4376 obj->data.net->link_qual);
4378 OBJ(wireless_link_qual_max) {
4379 spaced_print(p, p_max_size, "%d", 4,
4380 "wireless_link_qual_max", obj->data.net->link_qual_max);
4382 OBJ(wireless_link_qual_perc) {
4383 if (obj->data.net->link_qual_max > 0) {
4384 spaced_print(p, p_max_size, "%.0f", 5,
4385 "wireless_link_qual_perc",
4386 (double) obj->data.net->link_qual /
4387 obj->data.net->link_qual_max * 100);
4389 spaced_print(p, p_max_size, "unk", 5,
4390 "wireless_link_qual_perc");
4393 OBJ(wireless_link_bar) {
4394 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4395 obj->data.net->link_qual_max) * 255.0);
4397 #endif /* HAVE_IWLIB */
4399 #endif /* __linux__ */
4402 get_freq_dynamic(p, p_max_size, "%.0f", 1);
4403 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4407 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
4409 get_freq_dynamic(p, p_max_size, "%.2f", 1000);
4411 spaced_print(p, p_max_size, "%s", 6, "freq_dyn", p);
4416 get_adt746x_cpu(p, p_max_size);
4419 get_adt746x_fan(p, p_max_size);
4422 get_acpi_fan(p, p_max_size);
4424 OBJ(acpiacadapter) {
4425 get_acpi_ac_adapter(p, p_max_size);
4428 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4431 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4433 OBJ(battery_percent) {
4434 spaced_print(p, p_max_size, "%*d", 4, "battery_percent",
4435 pad_percents, get_battery_perct(obj->data.s));
4438 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4440 #endif /* __OpenBSD__ */
4443 human_readable(cur->buffers * 1024, p, 255, "buffers");
4446 human_readable(cur->cached * 1024, p, 255, "buffers");
4449 if (obj->data.cpu_index > info.cpu_count) {
4450 printf("obj->data.cpu_index %i info.cpu_count %i",
4451 obj->data.cpu_index, info.cpu_count);
4452 CRIT_ERR("attempting to use more CPUs than you have!");
4454 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
4455 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4458 new_bar(p, obj->a, obj->b,
4459 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4462 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
4463 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4467 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
4471 new_fg(p, obj->data.l);
4503 OBJ(conky_version) {
4504 snprintf(p, p_max_size, "%s", VERSION);
4506 OBJ(conky_build_date) {
4507 snprintf(p, p_max_size, "%s", BUILD_DATE);
4509 OBJ(conky_build_arch) {
4510 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4512 #if defined(__linux__)
4514 snprintf(p, p_max_size, "%s",
4515 get_disk_protect_queue(obj->data.s));
4518 snprintf(p, p_max_size, "%s", i8k.version);
4521 snprintf(p, p_max_size, "%s", i8k.bios);
4524 snprintf(p, p_max_size, "%s", i8k.serial);
4527 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
4529 OBJ(i8k_cpu_tempf) {
4532 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4533 snprintf(p, p_max_size, "%.1f", cpu_temp * (9.0 / 5.0) + 32.0);
4535 OBJ(i8k_left_fan_status) {
4536 int left_fan_status;
4538 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4539 if (left_fan_status == 0) {
4540 snprintf(p, p_max_size, "off");
4542 if (left_fan_status == 1) {
4543 snprintf(p, p_max_size, "low");
4545 if (left_fan_status == 2) {
4546 snprintf(p, p_max_size, "high");
4549 OBJ(i8k_right_fan_status) {
4550 int right_fan_status;
4552 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4553 if (right_fan_status == 0) {
4554 snprintf(p, p_max_size, "off");
4556 if (right_fan_status == 1) {
4557 snprintf(p, p_max_size, "low");
4559 if (right_fan_status == 2) {
4560 snprintf(p, p_max_size, "high");
4563 OBJ(i8k_left_fan_rpm) {
4564 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4566 OBJ(i8k_right_fan_rpm) {
4567 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4569 OBJ(i8k_ac_status) {
4572 sscanf(i8k.ac_status, "%d", &ac_status);
4573 if (ac_status == -1) {
4574 snprintf(p, p_max_size, "disabled (read i8k docs)");
4576 if (ac_status == 0) {
4577 snprintf(p, p_max_size, "off");
4579 if (ac_status == 1) {
4580 snprintf(p, p_max_size, "on");
4583 OBJ(i8k_buttons_status) {
4584 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4587 get_ibm_acpi_fan(p, p_max_size);
4590 get_ibm_acpi_temps();
4591 snprintf(p, p_max_size, "%d", ibm_acpi.temps[obj->data.sensor]);
4594 get_ibm_acpi_volume(p, p_max_size);
4596 OBJ(ibm_brightness) {
4597 get_ibm_acpi_brightness(p, p_max_size);
4600 if ((obj->data.ifblock.s)
4601 && (!interface_up(obj->data.ifblock.s))) {
4602 i = obj->data.ifblock.pos;
4609 if (!cur->gw_info.count) {
4610 i = obj->data.ifblock.pos;
4617 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4620 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4623 snprintf(p, p_max_size, "%d", get_laptop_mode());
4626 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4628 #endif /* __linux__ */
4631 OBJ(obsd_sensors_temp) {
4632 obsd_sensors.device = sensor_device;
4633 update_obsd_sensors();
4634 snprintf(p, p_max_size, "%.1f",
4635 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor]);
4637 OBJ(obsd_sensors_fan) {
4638 obsd_sensors.device = sensor_device;
4639 update_obsd_sensors();
4640 snprintf(p, p_max_size, "%d",
4641 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4643 OBJ(obsd_sensors_volt) {
4644 obsd_sensors.device = sensor_device;
4645 update_obsd_sensors();
4646 snprintf(p, p_max_size, "%.2f",
4647 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4650 get_obsd_vendor(p, p_max_size);
4653 get_obsd_product(p, p_max_size);
4655 #endif /* __OpenBSD__ */
4659 new_font(p, obj->data.s);
4662 /* TODO: move this correction from kB to kB/s elsewhere
4663 * (or get rid of it??) */
4665 if (obj->data.diskio) {
4667 (obj->data.diskio->current / update_interval) * 1024LL,
4668 p, p_max_size, "diskio");
4670 human_readable(info.diskio_value * 1024LL, p, p_max_size,
4675 if (obj->data.diskio) {
4676 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
4679 human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
4684 if (obj->data.diskio) {
4685 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
4688 human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
4693 if (obj->data.diskio) {
4694 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4695 obj->data.diskio->current, obj->e, 1);
4697 new_graph(p, obj->a, obj->b, obj->c, obj->d, info.diskio_value,
4701 OBJ(diskiograph_read) {
4702 if (obj->data.diskio) {
4703 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4704 obj->data.diskio->current_read, obj->e, 1);
4706 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4707 info.diskio_read_value, obj->e, 1);
4710 OBJ(diskiograph_write) {
4711 if (obj->data.diskio) {
4712 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4713 obj->data.diskio->current_write, obj->e, 1);
4715 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4716 info.diskio_write_value, obj->e, 1);
4720 spaced_print(p, p_max_size, "%d", 6, "downspeed",
4721 round_to_int(obj->data.net->recv_speed / 1024));
4724 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
4725 obj->data.net->recv_speed / 1024.0);
4727 OBJ(downspeedgraph) {
4728 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4729 obj->data.net->recv_speed / 1024.0, obj->e, 1);
4733 i = obj->data.ifblock.pos - 1;
4743 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4744 && (obj->data.net->addr.sa_data[3] & 255) == 0
4745 && (obj->data.net->addr.sa_data[4] & 255) == 0
4746 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4747 snprintf(p, p_max_size, "No Address");
4749 snprintf(p, p_max_size, "%u.%u.%u.%u",
4750 obj->data.net->addr.sa_data[2] & 255,
4751 obj->data.net->addr.sa_data[3] & 255,
4752 obj->data.net->addr.sa_data[4] & 255,
4753 obj->data.net->addr.sa_data[5] & 255);
4756 #if defined(__linux__)
4758 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4760 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4761 strcpy(p, obj->data.net->addrs);
4764 strcpy(p, "0.0.0.0");
4766 #endif /* __linux__ */
4768 #if defined(IMLIB2) && defined(X11)
4773 Imlib_Image image, buffer;
4775 image = imlib_load_image(obj->data.s);
4776 imlib_context_set_image(image);
4780 w = imlib_image_get_width();
4781 h = imlib_image_get_height();
4782 buffer = imlib_create_image(w, h);
4783 imlib_context_set_display(display);
4784 imlib_context_set_drawable(window.drawable);
4785 imlib_context_set_colormap(DefaultColormap(display,
4787 imlib_context_set_visual(DefaultVisual(display,
4789 imlib_context_set_image(buffer);
4790 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
4791 text_start_x, text_start_y, w, h);
4792 imlib_render_image_on_drawable(text_start_x,
4795 imlib_context_set_image(image);
4803 FILE *fp = popen(obj->data.s, "r");
4804 int length = fread(p, 1, p_max_size, fp);
4809 remove_deleted_chars(p);
4810 if (length > 0 && p[length - 1] == '\n') {
4811 p[length - 1] = '\0';
4816 struct information *my_info;
4817 struct text_object_list *text_objects;
4820 fp = popen(obj->data.s, "r");
4821 fread(p, 1, p_max_size, fp);
4824 my_info = malloc(sizeof(struct information));
4825 memcpy(my_info, cur, sizeof(struct information));
4826 text_objects = parse_conky_vars(p, p, my_info);
4831 if (length > 0 && p[length - 1] == '\n') {
4832 p[length - 1] = '\0';
4835 free_text_objects(text_objects);
4841 FILE *fp = popen(obj->data.s, "r");
4842 int n2 = fread(p, 1, p_max_size, fp);
4847 if (n2 && p[n2 - 1] == '\n') {
4852 if (*p2 == '\001') {
4858 if (sscanf(p, "%lf", &barnum) == 0) {
4859 ERR("reading execbar value failed (perhaps it's not the "
4860 "correct format?)");
4862 if (barnum > 100 || barnum < 0) {
4863 ERR("your execbar value is not between 0 and 100, "
4864 "therefore it will be ignored");
4866 barnum = barnum / 100.0;
4867 new_bar(p, 0, 4, (int) (barnum * 255.0));
4872 FILE *fp = popen(obj->data.s, "r");
4873 int n2 = fread(p, 1, p_max_size, fp);
4878 if (n2 && p[n2 - 1] == '\n') {
4882 if (*p2 == '\001') {
4888 if (sscanf(p, "%lf", &barnum) == 0) {
4889 ERR("reading execgraph value failed (perhaps it's not the "
4890 "correct format?)");
4892 if (barnum > 100 || barnum < 0) {
4893 ERR("your execgraph value is not between 0 and 100, "
4894 "therefore it will be ignored");
4896 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), 100, 1);
4900 if (current_update_time - obj->data.execi.last_update
4901 < obj->data.execi.interval) {
4902 new_bar(p, 0, 4, (int) obj->f);
4905 FILE *fp = popen(obj->data.execi.cmd, "r");
4906 int n2 = fread(p, 1, p_max_size, fp);
4911 if (n2 && p[n2 - 1] == '\n') {
4916 if (*p2 == '\001') {
4922 if (sscanf(p, "%f", &barnum) == 0) {
4923 ERR("reading execibar value failed (perhaps it's not "
4924 "the correct format?)");
4926 if (barnum > 100 || barnum < 0) {
4927 ERR("your execibar value is not between 0 and 100, "
4928 "therefore it will be ignored");
4930 obj->f = 255 * barnum / 100.0;
4931 new_bar(p, 0, 4, (int) obj->f);
4933 obj->data.execi.last_update = current_update_time;
4937 if (current_update_time - obj->data.execi.last_update
4938 < obj->data.execi.interval) {
4939 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
4942 FILE *fp = popen(obj->data.execi.cmd, "r");
4943 int n2 = fread(p, 1, p_max_size, fp);
4948 if (n2 && p[n2 - 1] == '\n') {
4953 if (*p2 == '\001') {
4959 if (sscanf(p, "%f", &barnum) == 0) {
4960 ERR("reading execigraph value failed (perhaps it's not "
4961 "the correct format?)");
4963 if (barnum > 100 || barnum < 0) {
4964 ERR("your execigraph value is not between 0 and 100, "
4965 "therefore it will be ignored");
4968 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f),
4971 obj->data.execi.last_update = current_update_time;
4975 if (current_update_time - obj->data.execi.last_update
4976 < obj->data.execi.interval
4977 || obj->data.execi.interval == 0) {
4978 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
4980 char *output = obj->data.execi.buffer;
4981 FILE *fp = popen(obj->data.execi.cmd, "r");
4983 int length = fread(output, 1, text_buffer_size, fp);
4986 output[length] = '\0';
4987 if (length > 0 && output[length - 1] == '\n') {
4988 output[length - 1] = '\0';
4990 obj->data.execi.last_update = current_update_time;
4991 snprintf(p, p_max_size, "%s", output);
4993 // parse_conky_vars(output, p, cur);
4996 struct text_object_list *text_objects = 0;
4997 struct information *my_info =
4998 malloc(sizeof(struct information));
4999 memcpy(my_info, cur, sizeof(struct information));
5001 if (current_update_time - obj->data.execi.last_update
5002 < obj->data.execi.interval
5003 || obj->data.execi.interval == 0) {
5004 text_objects = parse_conky_vars(obj->data.execi.buffer, p, my_info);
5006 char *output = obj->data.execi.buffer;
5007 FILE *fp = popen(obj->data.execi.cmd, "r");
5008 int length = fread(output, 1, text_buffer_size, fp);
5012 output[length] = '\0';
5013 if (length > 0 && output[length - 1] == '\n') {
5014 output[length - 1] = '\0';
5017 text_objects = parse_conky_vars(obj->data.execi.buffer, p, my_info);
5018 obj->data.execi.last_update = current_update_time;
5020 free_text_objects(text_objects);
5025 if (!obj->data.texeci.p_timed_thread) {
5026 obj->data.texeci.p_timed_thread =
5027 timed_thread_create(&threaded_exec,
5028 (void *) obj, obj->data.texeci.interval * 1000000);
5029 if (!obj->data.texeci.p_timed_thread) {
5030 ERR("Error creating texeci timed thread");
5032 timed_thread_register(obj->data.texeci.p_timed_thread,
5033 &obj->data.texeci.p_timed_thread);
5034 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
5035 ERR("Error running texeci timed thread");
5038 timed_thread_lock(obj->data.texeci.p_timed_thread);
5039 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
5040 timed_thread_unlock(obj->data.texeci.p_timed_thread);
5042 #endif /* HAVE_POPEN */
5044 if (obj->global_mode && info.mail) {
5045 // this means we use info
5046 if (!info.mail->p_timed_thread) {
5047 info.mail->p_timed_thread =
5048 timed_thread_create(&imap_thread,
5049 (void *) info.mail, info.mail->interval * 1000000);
5050 if (!info.mail->p_timed_thread) {
5051 ERR("Error creating imap timed thread");
5053 timed_thread_register(info.mail->p_timed_thread,
5054 &info.mail->p_timed_thread);
5055 if (timed_thread_run(info.mail->p_timed_thread)) {
5056 ERR("Error running imap timed thread");
5059 timed_thread_lock(info.mail->p_timed_thread);
5060 snprintf(p, p_max_size, "%lu", info.mail->unseen);
5061 timed_thread_unlock(info.mail->p_timed_thread);
5062 } else if (obj->data.mail) {
5063 // this means we use obj
5064 if (!obj->data.mail->p_timed_thread) {
5065 obj->data.mail->p_timed_thread =
5066 timed_thread_create(&imap_thread,
5067 (void *) obj->data.mail,
5068 obj->data.mail->interval * 1000000);
5069 if (!obj->data.mail->p_timed_thread) {
5070 ERR("Error creating imap timed thread");
5072 timed_thread_register(obj->data.mail->p_timed_thread,
5073 &obj->data.mail->p_timed_thread);
5074 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
5075 ERR("Error running imap timed thread");
5078 timed_thread_lock(obj->data.mail->p_timed_thread);
5079 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
5080 timed_thread_unlock(obj->data.mail->p_timed_thread);
5081 } else if (!obj->a) {
5082 // something is wrong, warn once then stop
5083 ERR("Theres a problem with your imap_unseen settings. "
5084 "Check that the global IMAP settings are defined "
5085 "properly (line %li).", obj->line);
5089 OBJ(imap_messages) {
5090 if (obj->global_mode && info.mail) {
5091 // this means we use info
5092 if (!info.mail->p_timed_thread) {
5093 info.mail->p_timed_thread =
5094 timed_thread_create(&imap_thread,
5095 (void *) info.mail, info.mail->interval * 1000000);
5096 if (!info.mail->p_timed_thread) {
5097 ERR("Error creating imap timed thread");
5099 timed_thread_register(info.mail->p_timed_thread,
5100 &info.mail->p_timed_thread);
5101 if (timed_thread_run(info.mail->p_timed_thread)) {
5102 ERR("Error running imap timed thread");
5105 timed_thread_lock(info.mail->p_timed_thread);
5106 snprintf(p, p_max_size, "%lu", info.mail->messages);
5107 timed_thread_unlock(info.mail->p_timed_thread);
5108 } else if (obj->data.mail) {
5109 // this means we use obj
5110 if (!obj->data.mail->p_timed_thread) {
5111 obj->data.mail->p_timed_thread =
5112 timed_thread_create(&imap_thread,
5113 (void *) obj->data.mail,
5114 obj->data.mail->interval * 1000000);
5115 if (!obj->data.mail->p_timed_thread) {
5116 ERR("Error creating imap timed thread");
5118 timed_thread_register(obj->data.mail->p_timed_thread,
5119 &obj->data.mail->p_timed_thread);
5120 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
5121 ERR("Error runninging imap timed thread");
5124 timed_thread_lock(obj->data.mail->p_timed_thread);
5125 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
5126 timed_thread_lock(obj->data.mail->p_timed_thread);
5127 } else if (!obj->a) {
5128 // something is wrong, warn once then stop
5129 ERR("Theres a problem with your imap_messages settings. "
5130 "Check that the global IMAP settings are defined "
5131 "properly (line %li).", obj->line);
5136 if (obj->global_mode && info.mail) {
5137 // this means we use info
5138 if (!info.mail->p_timed_thread) {
5139 info.mail->p_timed_thread =
5140 timed_thread_create(&pop3_thread,
5141 (void *) info.mail, info.mail->interval * 1000000);
5142 if (!info.mail->p_timed_thread) {
5143 ERR("Error creating pop3 timed thread");
5145 timed_thread_register(info.mail->p_timed_thread,
5146 &info.mail->p_timed_thread);
5147 if (timed_thread_run(info.mail->p_timed_thread)) {
5148 ERR("Error running pop3 timed thread");
5151 timed_thread_lock(info.mail->p_timed_thread);
5152 snprintf(p, p_max_size, "%lu", info.mail->unseen);
5153 timed_thread_unlock(info.mail->p_timed_thread);
5154 } else if (obj->data.mail) {
5155 // this means we use obj
5156 if (!obj->data.mail->p_timed_thread) {
5157 obj->data.mail->p_timed_thread =
5158 timed_thread_create(&pop3_thread,
5159 (void *) obj->data.mail,
5160 obj->data.mail->interval * 1000000);
5161 if (!obj->data.mail->p_timed_thread) {
5162 ERR("Error creating pop3 timed thread");
5164 timed_thread_register(obj->data.mail->p_timed_thread,
5165 &obj->data.mail->p_timed_thread);
5166 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
5167 ERR("Error running pop3 timed thread");
5170 timed_thread_lock(obj->data.mail->p_timed_thread);
5171 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
5172 timed_thread_unlock(obj->data.mail->p_timed_thread);
5173 } else if (!obj->a) {
5174 // something is wrong, warn once then stop
5175 ERR("Theres a problem with your pop3_unseen settings. "
5176 "Check that the global POP3 settings are defined "
5177 "properly (line %li).", obj->line);
5182 if (obj->global_mode && info.mail) {
5183 // this means we use info
5184 if (!info.mail->p_timed_thread) {
5185 info.mail->p_timed_thread =
5186 timed_thread_create(&pop3_thread,
5187 (void *) info.mail, info.mail->interval * 1000000);
5188 if (!info.mail->p_timed_thread) {
5189 ERR("Error creating pop3 timed thread");
5191 timed_thread_register(info.mail->p_timed_thread,
5192 &info.mail->p_timed_thread);
5193 if (timed_thread_run(info.mail->p_timed_thread)) {
5194 ERR("Error running pop3 timed thread");
5197 timed_thread_lock(info.mail->p_timed_thread);
5198 snprintf(p, p_max_size, "%.1f",
5199 info.mail->used / 1024.0 / 1024.0);
5200 timed_thread_unlock(info.mail->p_timed_thread);
5201 } else if (obj->data.mail) {
5202 // this means we use obj
5203 if (!obj->data.mail->p_timed_thread) {
5204 obj->data.mail->p_timed_thread =
5205 timed_thread_create(&pop3_thread,
5206 (void *) obj->data.mail,
5207 obj->data.mail->interval * 1000000);
5208 if (!obj->data.mail->p_timed_thread) {
5209 ERR("Error creating pop3 timed thread");
5211 timed_thread_register(obj->data.mail->p_timed_thread,
5212 &obj->data.mail->p_timed_thread);
5213 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
5214 ERR("Error running pop3 timed thread");
5217 timed_thread_lock(obj->data.mail->p_timed_thread);
5218 snprintf(p, p_max_size, "%.1f",
5219 obj->data.mail->used / 1024.0 / 1024.0);
5220 timed_thread_unlock(obj->data.mail->p_timed_thread);
5221 } else if (!obj->a) {
5222 // something is wrong, warn once then stop
5223 ERR("Theres a problem with your pop3_used settings. "
5224 "Check that the global POP3 settings are defined "
5225 "properly (line %li).", obj->line);
5230 if (obj->data.fs != NULL) {
5231 if (obj->data.fs->size == 0) {
5232 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5234 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5235 (int) (255 - obj->data.fsbar.fs->avail * 255 /
5236 obj->data.fs->size));
5241 if (obj->data.fs != NULL) {
5242 human_readable(obj->data.fs->avail, p, 255, "fs_free");
5246 if (obj->data.fs != NULL) {
5247 if (obj->data.fs->size) {
5248 spaced_print(p, p_max_size, "%*d", 4, "fs_free_perc",
5249 pad_percents, (int) ((obj->data.fs->avail * 100) /
5250 obj->data.fs->size));
5252 snprintf(p, p_max_size, "0");
5257 if (obj->data.fs != NULL) {
5258 human_readable(obj->data.fs->size, p, 255, "fs_size");
5262 if (obj->data.fs != NULL)
5263 snprintf(p, p_max_size, "%s", obj->data.fs->type);
5266 if (obj->data.fs != NULL) {
5267 human_readable(obj->data.fs->size - (obj->data.fs->free
5268 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
5273 if (obj->data.fs != NULL) {
5274 if (obj->data.fs->size == 0) {
5275 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
5277 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
5278 (int) (obj->data.fsbar.fs->avail * 255 /
5279 obj->data.fs->size));
5284 if (obj->data.fs != NULL) {
5285 if (obj->data.fs->size) {
5286 spaced_print(p, 4, "%*d", 4, "fs_used_perc",
5287 pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
5288 obj->data.fs->size)));
5290 snprintf(p, p_max_size, "0");
5295 float *v = info.loadavg;
5297 if (obj->data.loadavg[2]) {
5298 snprintf(p, p_max_size, "%.2f %.2f %.2f",
5299 v[obj->data.loadavg[0] - 1],
5300 v[obj->data.loadavg[1] - 1],
5301 v[obj->data.loadavg[2] - 1]);
5302 } else if (obj->data.loadavg[1]) {
5303 snprintf(p, p_max_size, "%.2f %.2f",
5304 v[obj->data.loadavg[0] - 1],
5305 v[obj->data.loadavg[1] - 1]);
5306 } else if (obj->data.loadavg[0]) {
5307 snprintf(p, p_max_size, "%.2f",
5308 v[obj->data.loadavg[0] - 1]);
5312 new_goto(p, obj->data.i);
5315 new_tab(p, obj->data.pair.a, obj->data.pair.b);
5318 new_hr(p, obj->data.i);
5321 if (cur->nameserver_info.nscount > obj->data.i)
5322 snprintf(p, p_max_size, "%s",
5323 cur->nameserver_info.ns_list[obj->data.i]);
5327 PRSS *data = get_rss_info(obj->data.rss.uri,
5328 obj->data.rss.delay);
5332 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
5334 if (!strcmp(obj->data.rss.action, "feed_title")) {
5336 // remove trailing new line if one exists
5337 if (str[strlen(str) - 1] == '\n') {
5338 str[strlen(str) - 1] = 0;
5340 snprintf(p, p_max_size, "%s", str);
5341 } else if (!strcmp(obj->data.rss.action, "item_title")) {
5342 if (obj->data.rss.act_par < data->item_count) {
5343 str = data->items[obj->data.rss.act_par].title;
5344 // remove trailing new line if one exists
5345 if (str[strlen(str) - 1] == '\n') {
5346 str[strlen(str) - 1] = 0;
5348 snprintf(p, p_max_size, "%s", str);
5350 } else if (!strcmp(obj->data.rss.action, "item_desc")) {
5351 if (obj->data.rss.act_par < data->item_count) {
5353 data->items[obj->data.rss.act_par].description;
5354 // remove trailing new line if one exists
5355 if (str[strlen(str) - 1] == '\n') {
5356 str[strlen(str) - 1] = 0;
5358 snprintf(p, p_max_size, "%s", str);
5360 } else if (!strcmp(obj->data.rss.action, "item_titles")) {
5361 if (data->item_count > 0) {
5367 if (obj->data.rss.act_par > data->item_count) {
5368 show = data->item_count;
5370 show = obj->data.rss.act_par;
5372 for (itmp = 0; itmp < show; itmp++) {
5373 PRSS_Item *item = &data->items[itmp];
5377 // don't add new line before first item
5379 strncat(p, "\n", p_max_size);
5381 /* remove trailing new line if one exists,
5382 * we have our own */
5383 if (str[strlen(str) - 1] == '\n') {
5384 str[strlen(str) - 1] = 0;
5386 strncat(p, str, p_max_size);
5396 if (obj->data.hddtemp.update_time < current_update_time - 30) {
5397 char *str = get_hddtemp_info(obj->data.hddtemp.dev,
5398 obj->data.hddtemp.addr, obj->data.hddtemp.port, &obj->data.hddtemp.unit);
5400 strncpy(obj->data.hddtemp.temp, str, text_buffer_size);
5402 obj->data.hddtemp.temp[0] = 0;
5404 obj->data.hddtemp.update_time = current_update_time;
5406 if (!obj->data.hddtemp.temp) {
5407 snprintf(p, p_max_size, "N/A");
5408 } else if (obj->data.hddtemp.unit == '*') {
5409 snprintf(p, p_max_size, "%s", obj->data.hddtemp.temp);
5411 snprintf(p, p_max_size, "%s%c", obj->data.hddtemp.temp, obj->data.hddtemp.unit);
5416 new_offset(p, obj->data.i);
5419 new_voffset(p, obj->data.i);
5425 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5426 obj->data.sysfs.devtype, obj->data.sysfs.type);
5428 if (r >= 100.0 || r == 0) {
5429 snprintf(p, p_max_size, "%d", (int) r);
5431 snprintf(p, p_max_size, "%.1f", r);
5437 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5438 obj->data.sysfs.devtype, obj->data.sysfs.type);
5440 if (r >= 100.0 || r == 0) {
5441 snprintf(p, p_max_size, "%d", (int) r);
5443 snprintf(p, p_max_size, "%.1f", r);
5449 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5450 obj->data.sysfs.devtype, obj->data.sysfs.type);
5452 if (r >= 100.0 || r == 0) {
5453 snprintf(p, p_max_size, "%d", (int) r);
5455 snprintf(p, p_max_size, "%.1f", r);
5458 #endif /* !__OpenBSD__ */
5460 new_alignr(p, obj->data.i);
5463 new_alignc(p, obj->data.i);
5466 struct text_object_list *text_objects;
5467 struct information *my_info =
5468 malloc(sizeof(struct information));
5469 memcpy(my_info, cur, sizeof(struct information));
5470 text_objects = parse_conky_vars(obj->data.ifblock.s, p, my_info);
5472 if (strlen(p) != 0) {
5473 i = obj->data.ifblock.pos;
5479 free_text_objects(text_objects);
5486 if ((obj->data.ifblock.s)
5487 && (stat(obj->data.ifblock.s, &tmp) == -1)) {
5488 i = obj->data.ifblock.pos;
5491 if (obj->data.ifblock.str) {
5492 if (!check_contains(obj->data.ifblock.s,
5493 obj->data.ifblock.str)) {
5494 i = obj->data.ifblock.pos;
5505 if ((obj->data.ifblock.s)
5506 && (!check_mount(obj->data.ifblock.s))) {
5507 i = obj->data.ifblock.pos;
5514 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
5515 i = obj->data.ifblock.pos;
5521 #if defined(__linux__)
5523 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
5527 snprintf(p, p_max_size, "%s", cur->uname_s.release);
5530 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
5535 human_readable(cur->mem * 1024, p, 255, "mem");
5538 human_readable(cur->memmax * 1024, p, 255, "memmax");
5542 spaced_print(p, p_max_size, "%*llu", 4, "memperc",
5543 pad_percents, cur->mem * 100 / cur->memmax);
5547 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5548 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5551 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5552 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5558 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
5561 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
5564 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
5567 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5568 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
5571 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5572 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
5575 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5576 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
5581 update_mail_count(&obj->data.local_mail);
5582 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5585 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5587 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5590 update_mail_count(&obj->data.local_mail);
5591 snprintf(p, p_max_size, "%d",
5592 obj->data.local_mail.new_mail_count);
5595 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5598 new_outline(p, obj->data.l);
5601 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
5603 OBJ(running_processes) {
5604 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
5607 snprintf(p, p_max_size, "%s", obj->data.s);
5610 new_bg(p, obj->data.l);
5613 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5616 human_readable(cur->swap * 1024, p, 255, "swap");
5619 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
5622 if (cur->swapmax == 0) {
5623 strncpy(p, "No swap", p_max_size);
5625 spaced_print(p, p_max_size, "%*llu", 4, "swapperc",
5626 pad_percents, cur->swap * 100 / cur->swapmax);
5630 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5631 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5634 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5637 time_t t = time(NULL);
5638 struct tm *tm = localtime(&t);
5640 setlocale(LC_TIME, "");
5641 strftime(p, p_max_size, obj->data.s, tm);
5644 time_t t = time(NULL);
5645 struct tm *tm = gmtime(&t);
5647 strftime(p, p_max_size, obj->data.s, tm);
5654 if (obj->data.tztime.tz) {
5655 oldTZ = getenv("TZ");
5656 setenv("TZ", obj->data.tztime.tz, 1);
5662 setlocale(LC_TIME, "");
5663 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5665 setenv("TZ", oldTZ, 1);
5670 // Needless to free oldTZ since getenv gives ptr to static data
5673 human_readable(obj->data.net->recv, p, 255, "totaldown");
5676 human_readable(obj->data.net->trans, p, 255, "totalup");
5679 snprintf(p, p_max_size, "%d", total_updates);
5682 spaced_print(p, p_max_size, "%d", 6, "upspeed",
5683 round_to_int(obj->data.net->trans_speed / 1024));
5686 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
5687 obj->data.net->trans_speed / 1024.0);
5690 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5691 obj->data.net->trans_speed / 1024.0, obj->e, 1);
5694 format_seconds_short(p, p_max_size, (int) cur->uptime);
5697 format_seconds(p, p_max_size, (int) cur->uptime);
5700 snprintf(p, p_max_size, "%s", cur->users.names);
5703 snprintf(p, p_max_size, "%s", cur->users.terms);
5706 snprintf(p, p_max_size, "%s", cur->users.times);
5709 snprintf(p, p_max_size, "%d", cur->users.number);
5711 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5712 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5716 msg = get_apm_adapter();
5717 snprintf(p, p_max_size, "%s", msg);
5720 OBJ(apm_battery_life) {
5723 msg = get_apm_battery_life();
5724 snprintf(p, p_max_size, "%s", msg);
5727 OBJ(apm_battery_time) {
5730 msg = get_apm_battery_time();
5731 snprintf(p, p_max_size, "%s", msg);
5734 #endif /* __FreeBSD__ __OpenBSD__ */
5738 snprintf(p, cur->mpd.max_title_len > 0
5739 ? cur->mpd.max_title_len : p_max_size, "%s",
5743 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5746 snprintf(p, p_max_size, "%s", cur->mpd.album);
5749 snprintf(p, p_max_size, "%s", cur->mpd.random);
5752 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
5755 snprintf(p, p_max_size, "%s", cur->mpd.track);
5758 snprintf(p, p_max_size, "%s", cur->mpd.name);
5761 snprintf(p, p_max_size, "%s", cur->mpd.file);
5764 snprintf(p, p_max_size, "%i", cur->mpd.volume);
5767 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
5770 snprintf(p, p_max_size, "%s", cur->mpd.status);
5773 format_media_player_time(p, p_max_size, cur->mpd.elapsed);
5776 format_media_player_time(p, p_max_size, cur->mpd.length);
5779 spaced_print(p, p_max_size, "%*d", 4, "mpd_percent",
5780 pad_percents, (int) (cur->mpd.progress * 100));
5783 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5784 (int) (cur->mpd.progress * 255.0f));
5787 memset(p, 0, p_max_size);
5788 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
5789 && *cur->mpd.title) {
5790 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist,
5792 } else if (cur->mpd.title && *cur->mpd.title) {
5793 snprintf(p, p_max_size, "%s", cur->mpd.title);
5794 } else if (cur->mpd.artist && *cur->mpd.artist) {
5795 snprintf(p, p_max_size, "%s", cur->mpd.artist);
5796 } else if (cur->mpd.file && *cur->mpd.file) {
5797 snprintf(p, p_max_size, "%s", cur->mpd.file);
5806 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5809 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5812 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5815 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5817 OBJ(xmms2_comment) {
5818 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5821 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5824 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5827 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5829 OBJ(xmms2_tracknr) {
5830 if (cur->xmms2.tracknr != -1) {
5831 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5834 OBJ(xmms2_bitrate) {
5835 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5838 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5841 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5843 OBJ(xmms2_elapsed) {
5844 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5845 (cur->xmms2.elapsed / 1000) % 60);
5847 OBJ(xmms2_duration) {
5848 snprintf(p, p_max_size, "%02d:%02d",
5849 cur->xmms2.duration / 60000,
5850 (cur->xmms2.duration / 1000) % 60);
5852 OBJ(xmms2_percent) {
5853 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5856 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5857 (int) (cur->xmms2.progress * 255.0f));
5859 OBJ(xmms2_playlist) {
5860 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5862 OBJ(xmms2_timesplayed) {
5863 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5866 if (strlen(cur->xmms2.title) < 2
5867 && strlen(cur->xmms2.title) < 2) {
5868 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5870 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5876 OBJ(audacious_status) {
5877 snprintf(p, p_max_size, "%s",
5878 cur->audacious.items[AUDACIOUS_STATUS]);
5880 OBJ(audacious_title) {
5881 snprintf(p, cur->audacious.max_title_len > 0
5882 ? cur->audacious.max_title_len : p_max_size, "%s",
5883 cur->audacious.items[AUDACIOUS_TITLE]);
5885 OBJ(audacious_length) {
5886 snprintf(p, p_max_size, "%s",
5887 cur->audacious.items[AUDACIOUS_LENGTH]);
5889 OBJ(audacious_length_seconds) {
5890 snprintf(p, p_max_size, "%s",
5891 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5893 OBJ(audacious_position) {
5894 snprintf(p, p_max_size, "%s",
5895 cur->audacious.items[AUDACIOUS_POSITION]);
5897 OBJ(audacious_position_seconds) {
5898 snprintf(p, p_max_size, "%s",
5899 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5901 OBJ(audacious_bitrate) {
5902 snprintf(p, p_max_size, "%s",
5903 cur->audacious.items[AUDACIOUS_BITRATE]);
5905 OBJ(audacious_frequency) {
5906 snprintf(p, p_max_size, "%s",
5907 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5909 OBJ(audacious_channels) {
5910 snprintf(p, p_max_size, "%s",
5911 cur->audacious.items[AUDACIOUS_CHANNELS]);
5913 OBJ(audacious_filename) {
5914 snprintf(p, p_max_size, "%s",
5915 cur->audacious.items[AUDACIOUS_FILENAME]);
5917 OBJ(audacious_playlist_length) {
5918 snprintf(p, p_max_size, "%s",
5919 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5921 OBJ(audacious_playlist_position) {
5922 snprintf(p, p_max_size, "%s",
5923 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5925 OBJ(audacious_bar) {
5929 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5930 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5931 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5937 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5940 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5943 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5946 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5949 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5952 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5956 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5959 switch (obj->data.top.type) {
5961 snprintf(p, 16, "%-15s",
5962 cur->cpu[obj->data.top.num]->name);
5965 snprintf(p, 7, "%6.2f",
5966 cur->cpu[obj->data.top.num]->amount);
5969 snprintf(p, 6, "%5i",
5970 cur->cpu[obj->data.top.num]->pid);
5973 snprintf(p, 7, "%6.2f",
5974 cur->cpu[obj->data.top.num]->totalmem);
5977 timeval = format_time(
5978 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
5979 snprintf(p, 10, "%9s", timeval);
5983 human_readable(cur->cpu[obj->data.top.num]->rss,
5987 human_readable(cur->cpu[obj->data.top.num]->vsize,
5991 ERR("Unhandled top data type: %d\n",
5992 obj->data.top.type);
5995 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5999 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
6002 switch (obj->data.top.type) {
6004 snprintf(p, 16, "%-15s",
6005 cur->memu[obj->data.top.num]->name);
6008 snprintf(p, 7, "%6.2f",
6009 cur->memu[obj->data.top.num]->amount);
6012 snprintf(p, 6, "%5i",
6013 cur->memu[obj->data.top.num]->pid);
6016 snprintf(p, 7, "%6.2f",
6017 cur->memu[obj->data.top.num]->totalmem);
6020 timeval = format_time(
6021 cur->memu[obj->data.top.num]->total_cpu_time,
6023 snprintf(p, 10, "%9s", timeval);
6027 human_readable(cur->cpu[obj->data.top.num]->rss,
6031 human_readable(cur->cpu[obj->data.top.num]->vsize,
6035 ERR("Unhandled top data type: %d\n",
6036 obj->data.top.type);
6039 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
6043 if (current_update_time - obj->data.tail.last_update
6044 < obj->data.tail.interval) {
6045 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
6051 obj->data.tail.last_update = current_update_time;
6053 if (obj->data.tail.fd != -1) {
6054 tail_pipe(obj, p, p_max_size);
6058 fp = fopen(obj->data.tail.logfile, "rt");
6061 /* Send one message, but do not consistently spam
6062 * on missing logfiles. */
6063 if (obj->data.tail.readlines != 0) {
6064 ERR("tail logfile failed to open");
6065 strcpy(obj->data.tail.buffer, "Logfile Missing");
6067 obj->data.tail.readlines = 0;
6068 snprintf(p, p_max_size, "Logfile Missing");
6070 obj->data.tail.readlines = 0;
6071 /* -1 instead of 0 to avoid counting a trailing
6073 fseek(fp, -1, SEEK_END);
6074 bsize = ftell(fp) + 1;
6075 for (iter = obj->data.tail.wantedlines; iter > 0;
6077 nl = rev_fcharfind(fp, '\n', iter);
6082 obj->data.tail.readlines = iter;
6083 if (obj->data.tail.readlines
6084 < obj->data.tail.wantedlines) {
6085 fseek(fp, 0, SEEK_SET);
6087 fseek(fp, nl + 1, SEEK_SET);
6090 /* Make sure bsize is at least 1 byte smaller than the
6091 * buffer max size. */
6092 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
6093 fseek(fp, bsize - text_buffer_size * 20 - 1,
6095 bsize = text_buffer_size * 20 - 1;
6097 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
6100 /* Clean up trailing newline, make sure the
6101 * buffer is null terminated. */
6102 if (obj->data.tail.buffer[bsize - 1] == '\n') {
6103 obj->data.tail.buffer[bsize - 1] = '\0';
6105 obj->data.tail.buffer[bsize] = '\0';
6107 snprintf(p, p_max_size, "%s",
6108 obj->data.tail.buffer);
6110 strcpy(obj->data.tail.buffer, "Logfile Empty");
6111 snprintf(p, p_max_size, "Logfile Empty");
6114 } /* if cur_upd_time >= */
6115 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6119 if (current_update_time - obj->data.tail.last_update
6120 < obj->data.tail.interval) {
6121 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
6127 obj->data.tail.last_update = current_update_time;
6129 fp = fopen(obj->data.tail.logfile, "rt");
6131 /* Send one message, but do not consistently spam
6132 * on missing logfiles. */
6133 if (obj->data.tail.readlines != 0) {
6134 ERR("head logfile failed to open");
6135 strcpy(obj->data.tail.buffer, "Logfile Missing");
6137 obj->data.tail.readlines = 0;
6138 snprintf(p, p_max_size, "Logfile Missing");
6140 obj->data.tail.readlines = 0;
6141 for (iter = obj->data.tail.wantedlines; iter > 0;
6143 nl = fwd_fcharfind(fp, '\n', iter);
6148 obj->data.tail.readlines = iter;
6149 /* Make sure nl is at least 1 byte smaller than the
6150 * buffer max size. */
6151 if (nl > (long) ((text_buffer_size * 20) - 1)) {
6152 nl = text_buffer_size * 20 - 1;
6154 nl = fread(obj->data.tail.buffer, 1, nl, fp);
6157 /* Clean up trailing newline, make sure the buffer
6158 * is null terminated. */
6159 if (obj->data.tail.buffer[nl - 1] == '\n') {
6160 obj->data.tail.buffer[nl - 1] = '\0';
6162 obj->data.tail.buffer[nl] = '\0';
6164 snprintf(p, p_max_size, "%s",
6165 obj->data.tail.buffer);
6167 strcpy(obj->data.tail.buffer, "Logfile Empty");
6168 snprintf(p, p_max_size, "Logfile Empty");
6170 } /* if fp == null */
6171 } /* cur_upd_time >= */
6172 // parse_conky_vars(obj->data.tail.buffer, p, cur);
6175 #ifdef TCP_PORT_MONITOR
6177 /* grab a pointer to this port monitor */
6178 tcp_port_monitor_t *p_monitor =
6179 find_tcp_port_monitor(info.p_tcp_port_monitor_collection,
6180 obj->data.tcp_port_monitor.port_range_begin,
6181 obj->data.tcp_port_monitor.port_range_end);
6184 snprintf(p, p_max_size, "monitor not found");
6188 /* now grab the text of interest */
6189 if (peek_tcp_port_monitor(p_monitor,
6190 obj->data.tcp_port_monitor.item,
6191 obj->data.tcp_port_monitor.connection_index, p,
6193 snprintf(p, p_max_size, "monitor peek error");
6201 iconv_converting = 1;
6202 iconv_selected = obj->a;
6205 iconv_converting = 0;
6210 OBJ(entropy_avail) {
6211 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
6213 OBJ(entropy_poolsize) {
6214 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
6217 double entropy_perc;
6219 entropy_perc = (double) cur->entropy.entropy_avail /
6220 (double) cur->entropy.poolsize;
6221 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
6227 s = smapi_get_val(obj->data.s);
6228 snprintf(p, p_max_size, "%s", s);
6232 OBJ(if_smapi_bat_installed) {
6234 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
6235 if(!smapi_bat_installed(idx)) {
6236 i = obj->data.ifblock.pos;
6241 ERR("argument to if_smapi_bat_installed must be an integer");
6243 OBJ(smapi_bat_perc) {
6245 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
6246 val = smapi_bat_installed(idx) ?
6247 smapi_get_bat_int(idx, "remaining_percent") : 0;
6248 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
6250 ERR("argument to smapi_bat_perc must be an integer");
6252 OBJ(smapi_bat_bar) {
6253 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
6254 new_bar(p, obj->a, obj->b, (int)
6255 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
6257 new_bar(p, obj->a, obj->b, 0);
6262 int hol = (strcmp((char*)&obj->data.nvidia.arg, "gpufreq")) ? 1 : 0;
6263 if(!(obj->data.nvidia.value = get_nvidia_value(obj->data.nvidia.type, display, hol)))
6264 snprintf(p, p_max_size, "value unavailible");
6266 spaced_print(p, p_max_size, "%*d", 4, "nvidia",
6267 4, obj->data.nvidia.value);
6276 unsigned int a = strlen(p);
6279 if (a > 0 && iconv_converting && iconv_selected > 0
6280 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6282 size_t dummy1, dummy2;
6283 char *ptr = buff_in;
6286 dummy1 = dummy2 = a;
6288 strncpy(buff_in, p, p_max_size);
6290 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6291 while (dummy1 > 0) {
6292 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6295 ERR("Iconv codeset conversion failed");
6300 /* It is nessecary when we are converting from multibyte to
6301 * singlebyte codepage */
6304 #endif /* HAVE_ICONV */
6311 double current_update_time, last_update_time;
6313 static void generate_text(void)
6315 struct information *cur = &info;
6322 current_update_time = get_time();
6325 /* fix diskio rates to b/s (use update_interval */
6326 info.diskio_read_value /= update_interval;
6327 info.diskio_write_value /= update_interval;
6328 info.diskio_value /= update_interval;
6330 /* add things to the buffer */
6336 generate_text_internal(p, max_user_text, global_text_object_list, cur);
6338 if (stuff_in_upper_case) {
6341 tmp_p = text_buffer;
6343 *tmp_p = toupper(*tmp_p);
6348 last_update_time = current_update_time;
6354 static void set_font(void)
6358 if (window.xftdraw != NULL) {
6359 XftDrawDestroy(window.xftdraw);
6361 window.xftdraw = XftDrawCreate(display, window.drawable,
6362 DefaultVisual(display, screen), DefaultColormap(display, screen));
6366 XSetFont(display, window.gc, fonts[selected_font].font->fid);
6372 static inline int get_string_width(const char *s)
6375 return *s ? calc_text_width(s, strlen(s)) : 0;
6381 static inline int get_string_width_special(char *s)
6393 p = strndup(s, text_buffer_size);
6397 if (*p == SPECIAL_CHAR) {
6398 /* shift everything over by 1 so that the special char
6399 * doesn't mess up the size calculation */
6400 for (i = 0; i < strlen(p); i++) {
6401 *(p + i) = *(p + i + 1);
6403 if (specials[special_index + idx].type == GRAPH
6404 || specials[special_index + idx].type == BAR) {
6405 width += specials[special_index + idx].width;
6412 if (strlen(final) > 1) {
6413 width += calc_text_width(final, strlen(final));
6418 return (s) ? strlen(s) : 0;
6423 static void text_size_updater(char *s);
6425 int last_font_height;
6426 static void update_text_area(void)
6430 /* update text size if it isn't fixed */
6435 text_width = minimum_width;
6438 last_font_height = font_height();
6439 for_each_line(text_buffer, text_size_updater);
6441 if (text_height < minimum_height) {
6442 text_height = minimum_height;
6444 if (text_width > maximum_width && maximum_width > 0) {
6445 text_width = maximum_width;
6449 /* get text position on workarea */
6450 switch (text_alignment) {
6457 x = workarea[2] - text_width - gap_x;
6462 x = workarea[2] / 2 - text_width / 2 - gap_x;
6469 y = workarea[3] - text_height - gap_y;
6473 x = workarea[2] - text_width - gap_x;
6474 y = workarea[3] - text_height - gap_y;
6478 x = workarea[2] / 2 - text_width / 2 - gap_x;
6479 y = workarea[3] - text_height - gap_y;
6484 y = workarea[3] / 2 - text_height / 2 - gap_y;
6488 x = workarea[2] - text_width - gap_x;
6489 y = workarea[3] / 2 - text_height / 2 - gap_y;
6493 case NONE: // Let the WM manage the window
6504 if (own_window && !fixed_pos) {
6507 text_start_x = border_margin + 1;
6508 text_start_y = border_margin + 1;
6509 window.x = x - border_margin - 1;
6510 window.y = y - border_margin - 1;
6514 /* If window size doesn't match to workarea's size,
6515 * then window probably includes panels (gnome).
6516 * Blah, doesn't work on KDE. */
6517 if (workarea[2] != window.width || workarea[3] != window.height) {
6529 static int cur_x, cur_y; /* current x and y for drawing */
6531 //draw_mode also without X11 because we only need to print to stdout with FG
6532 static int draw_mode; /* FG, BG or OUTLINE */
6534 static long current_color;
6537 static void text_size_updater(char *s)
6542 /* get string widths and skip specials */
6545 if (*p == SPECIAL_CHAR) {
6547 w += get_string_width(s);
6550 if (specials[special_index].type == BAR
6551 || specials[special_index].type == GRAPH) {
6552 w += specials[special_index].width;
6553 if (specials[special_index].height > last_font_height) {
6554 last_font_height = specials[special_index].height;
6555 last_font_height += font_ascent();
6557 } else if (specials[special_index].type == OFFSET) {
6558 if (specials[special_index].arg > 0) {
6559 w += specials[special_index].arg;
6561 } else if (specials[special_index].type == VOFFSET) {
6562 last_font_height += specials[special_index].arg;
6563 } else if (specials[special_index].type == GOTO) {
6564 if (specials[special_index].arg > cur_x) {
6565 w = (int) specials[special_index].arg;
6567 } else if (specials[special_index].type == TAB) {
6568 int start = specials[special_index].arg;
6569 int step = specials[special_index].width;
6571 if (!step || step < 0) {
6574 w += step - (cur_x - text_start_x - start) % step;
6575 } else if (specials[special_index].type == FONT) {
6576 selected_font = specials[special_index].font_added;
6577 if (font_height() > last_font_height) {
6578 last_font_height = font_height();
6587 w += get_string_width(s);
6588 if (w > text_width) {
6591 if (text_width > maximum_width && maximum_width) {
6592 text_width = maximum_width;
6595 text_height += last_font_height;
6596 last_font_height = font_height();
6600 static inline void set_foreground_color(long c)
6603 XSetForeground(display, window.gc, c);
6607 static void draw_string(const char *s)
6609 int i, i2, pos, width_of_s;
6617 width_of_s = get_string_width(s);
6618 if (out_to_console && draw_mode == FG) {
6620 fflush(stdout); /* output immediately, don't buffer */
6622 memset(tmpstring1, 0, text_buffer_size);
6623 memset(tmpstring2, 0, text_buffer_size);
6624 strncpy(tmpstring1, s, text_buffer_size - 1);
6629 max = ((text_width - width_of_s) / get_string_width(" "));
6631 /* This code looks for tabs in the text and coverts them to spaces.
6632 * The trick is getting the correct number of spaces, and not going
6633 * over the window's size without forcing the window larger. */
6634 for (i = 0; i < (int) text_buffer_size; i++) {
6635 if (tmpstring1[i] == '\t') {
6637 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6638 /* guard against overrun */
6639 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6644 /* guard against overrun */
6645 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6650 if (text_width == maximum_width) {
6651 /* this means the text is probably pushing the limit,
6652 * so we'll chop it */
6653 while (cur_x + get_string_width(tmpstring2) - text_start_x
6654 > maximum_width && strlen(tmpstring2) > 0) {
6655 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6666 c.pixel = current_color;
6667 XQueryColor(display, DefaultColormap(display, screen), &c);
6670 c2.color.red = c.red;
6671 c2.color.green = c.green;
6672 c2.color.blue = c.blue;
6673 c2.color.alpha = fonts[selected_font].font_alpha;
6675 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6676 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6678 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6679 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6684 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6687 cur_x += width_of_s;
6689 memcpy(tmpstring1, s, text_buffer_size);
6692 long redmask, greenmask, bluemask;
6694 void set_up_gradient(void)
6698 colour_depth = DisplayPlanes(display, screen);
6702 if (colour_depth != 24 && colour_depth != 16) {
6703 ERR("using non-standard colour depth, gradients may look like a "
6710 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6712 greenmask |= 1 << i;
6715 if (colour_depth % 3 == 1) {
6716 greenmask |= 1 << (colour_depth / 3);
6718 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6719 greenmask = greenmask << (colour_depth / 3);
6722 /* this function returns the next colour between two colours for a gradient */
6723 inline unsigned long do_gradient(unsigned long first_colour,
6724 unsigned long last_colour)
6727 int red1, green1, blue1; // first colour
6728 int red2, green2, blue2; // second colour
6729 int red3 = 0, green3 = 0, blue3 = 0; // difference
6730 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6731 short greenshift = (colour_depth / 3);
6733 red1 = (first_colour & redmask) >> redshift;
6734 green1 = (first_colour & greenmask) >> greenshift;
6735 blue1 = first_colour & bluemask;
6736 red2 = (last_colour & redmask) >> redshift;
6737 green2 = (last_colour & greenmask) >> greenshift;
6738 blue2 = last_colour & bluemask;
6745 if (green1 > green2) {
6748 if (green1 < green2) {
6751 if (blue1 > blue2) {
6754 if (blue1 < blue2) {
6769 if (red1 > bluemask) {
6772 if (green1 > bluemask) {
6775 if (blue1 > bluemask) {
6778 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
6782 /* this function returns the max diff for a gradient */
6783 inline unsigned long gradient_max(unsigned long first_colour,
6784 unsigned long last_colour)
6786 int red1, green1, blue1; // first colour
6787 int red2, green2, blue2; // second colour
6788 int red3 = 0, green3 = 0, blue3 = 0; // difference
6789 long redshift, greenshift;
6792 if (colour_depth == 0) {
6795 redshift = (2 * colour_depth / 3 + colour_depth % 3);
6796 greenshift = (colour_depth / 3);
6798 red1 = (first_colour & redmask) >> redshift;
6799 green1 = (first_colour & greenmask) >> greenshift;
6800 blue1 = first_colour & bluemask;
6801 red2 = (last_colour & redmask) >> redshift;
6802 green2 = (last_colour & greenmask) >> greenshift;
6803 blue2 = last_colour & bluemask;
6804 red3 = abs(red1 - red2);
6805 green3 = abs(green1 - green2);
6806 blue3 = abs(blue1 - blue2);
6818 static void draw_line(char *s)
6826 cur_x = text_start_x;
6827 cur_y += font_ascent();
6828 font_h = font_height();
6830 /* find specials and draw stuff */
6833 if (*p == SPECIAL_CHAR) {
6836 /* draw string before special */
6843 switch (specials[special_index].type) {
6844 case HORIZONTAL_LINE:
6846 int h = specials[special_index].height;
6847 int mid = font_ascent() / 2;
6849 w = text_start_x + text_width - cur_x;
6851 XSetLineAttributes(display, window.gc, h, LineSolid,
6852 CapButt, JoinMiter);
6853 XDrawLine(display, window.drawable, window.gc, cur_x,
6854 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6860 int h = specials[special_index].height;
6861 int tmp_s = specials[special_index].arg;
6862 int mid = font_ascent() / 2;
6863 char ss[2] = { tmp_s, tmp_s };
6865 w = text_start_x + text_width - cur_x - 1;
6866 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6867 CapButt, JoinMiter);
6868 XSetDashes(display, window.gc, 0, ss, 2);
6869 XDrawLine(display, window.drawable, window.gc, cur_x,
6870 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6876 int h, bar_usage, by;
6877 if (cur_x - text_start_x > maximum_width
6878 && maximum_width > 0) {
6881 h = specials[special_index].height;
6882 bar_usage = specials[special_index].arg;
6883 by = cur_y - (font_ascent() / 2) - 1;
6885 if (h < font_height()) {
6888 w = specials[special_index].width;
6890 w = text_start_x + text_width - cur_x - 1;
6896 XSetLineAttributes(display, window.gc, 1, LineSolid,
6897 CapButt, JoinMiter);
6899 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6901 XFillRectangle(display, window.drawable, window.gc, cur_x,
6902 by, w * bar_usage / 255, h);
6903 if (specials[special_index].height > cur_y_add
6904 && specials[special_index].height > font_h) {
6905 cur_y_add = specials[special_index].height;
6912 int h, by, i, j = 0;
6913 int gradient_size = 0;
6914 float gradient_factor = 0;
6915 float gradient_update = 0;
6916 unsigned long last_colour = current_color;
6917 unsigned long tmpcolour = current_color;
6918 int show_scale_x = cur_x + font_ascent() / 2;
6919 int show_scale_y = cur_y + font_height() / 2;
6920 if (cur_x - text_start_x > maximum_width
6921 && maximum_width > 0) {
6924 h = specials[special_index].height;
6925 by = cur_y - (font_ascent() / 2) - 1;
6927 if (h < font_height()) {
6930 w = specials[special_index].width;
6932 w = text_start_x + text_width - cur_x - 1;
6937 if (draw_graph_borders) {
6938 XSetLineAttributes(display, window.gc, 1, LineSolid,
6939 CapButt, JoinMiter);
6940 XDrawRectangle(display, window.drawable, window.gc,
6943 XSetLineAttributes(display, window.gc, 1, LineSolid,
6944 CapButt, JoinMiter);
6946 if (specials[special_index].last_colour != 0
6947 || specials[special_index].first_colour != 0) {
6948 tmpcolour = specials[special_index].last_colour;
6950 gradient_max(specials[special_index].last_colour,
6951 specials[special_index].first_colour);
6952 gradient_factor = (float) gradient_size / (w - 2);
6954 for (i = w - 2; i > -1; i--) {
6955 if (specials[special_index].last_colour != 0
6956 || specials[special_index].first_colour != 0) {
6957 XSetForeground(display, window.gc, tmpcolour);
6958 gradient_update += gradient_factor;
6959 while (gradient_update > 0) {
6960 tmpcolour = do_gradient(tmpcolour,
6961 specials[special_index].first_colour);
6965 if ((w - i) / ((float) (w - 2) /
6966 (specials[special_index].graph_width)) > j
6967 && j < MAX_GRAPH_DEPTH - 3) {
6970 /* this is mugfugly, but it works */
6971 XDrawLine(display, window.drawable, window.gc,
6972 cur_x + i + 1, by + h, cur_x + i + 1,
6973 by + h - specials[special_index].graph[j] *
6974 (h - 1) / specials[special_index].graph_scale);
6976 if (specials[special_index].height > cur_y_add
6977 && specials[special_index].height > font_h) {
6978 cur_y_add = specials[special_index].height;
6980 /* if (draw_mode == BG) {
6981 set_foreground_color(default_bg_color);
6982 } else if (draw_mode == OUTLINE) {
6983 set_foreground_color(default_out_color);
6985 set_foreground_color(default_fg_color);
6987 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6990 cur_x = show_scale_x;
6991 cur_y = show_scale_y;
6993 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6995 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6996 draw_string(tmp_str);
7001 set_foreground_color(last_colour);
7007 int old = font_ascent();
7009 cur_y -= font_ascent();
7010 selected_font = specials[special_index].font_added;
7011 if (cur_y + font_ascent() < cur_y + old) {
7014 cur_y += font_ascent();
7020 if (draw_mode == FG) {
7021 set_foreground_color(specials[special_index].arg);
7026 if (draw_mode == BG) {
7027 set_foreground_color(specials[special_index].arg);
7032 if (draw_mode == OUTLINE) {
7033 set_foreground_color(specials[special_index].arg);
7038 w += specials[special_index].arg;
7042 cur_y += specials[special_index].arg;
7046 if (specials[special_index].arg >= 0) {
7047 cur_x = (int) specials[special_index].arg;
7053 int start = specials[special_index].arg;
7054 int step = specials[special_index].width;
7056 if (!step || step < 0) {
7059 w = step - (cur_x - text_start_x - start) % step;
7065 /* TODO: add back in "+ border_margin" to the end of
7067 int pos_x = text_start_x + text_width -
7068 get_string_width_special(s);
7070 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7071 "get_string_width(p) %i gap_x %i "
7072 "specials[special_index].arg %i border_margin %i "
7073 "border_width %i\n", pos_x, text_start_x, text_width,
7074 cur_x, get_string_width_special(s), gap_x,
7075 specials[special_index].arg, border_margin,
7077 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
7078 cur_x = pos_x - specials[special_index].arg;
7085 int pos_x = (text_width) / 2 - get_string_width_special(s) /
7086 2 - (cur_x - text_start_x);
7087 /* int pos_x = text_start_x + text_width / 2 -
7088 get_string_width_special(s) / 2; */
7090 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7091 "get_string_width(p) %i gap_x %i "
7092 "specials[special_index].arg %i\n", pos_x, text_start_x,
7093 text_width, cur_x, get_string_width(s), gap_x,
7094 specials[special_index].arg); */
7095 if (pos_x > specials[special_index].arg) {
7096 w = pos_x - specials[special_index].arg;
7113 if (cur_y_add > 0) {
7115 cur_y -= font_descent();
7120 cur_y += font_descent();
7124 static void draw_text(void)
7127 cur_y = text_start_y;
7130 if (draw_borders && border_width > 0) {
7131 unsigned int b = (border_width + 1) / 2;
7133 if (stippled_borders) {
7134 char ss[2] = { stippled_borders, stippled_borders };
7135 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
7136 CapButt, JoinMiter);
7137 XSetDashes(display, window.gc, 0, ss, 2);
7139 XSetLineAttributes(display, window.gc, border_width, LineSolid,
7140 CapButt, JoinMiter);
7143 XDrawRectangle(display, window.drawable, window.gc,
7144 text_start_x - border_margin + b, text_start_y - border_margin + b,
7145 text_width + border_margin * 2 - 1 - b * 2,
7146 text_height + border_margin * 2 - 1 - b * 2);
7152 for_each_line(text_buffer, draw_line);
7155 static void draw_stuff(void)
7159 if (draw_shades && !draw_outline) {
7162 set_foreground_color(default_bg_color);
7173 for (i = -1; i < 2; i++) {
7174 for (j = -1; j < 2; j++) {
7175 if (i == 0 && j == 0) {
7180 set_foreground_color(default_out_color);
7181 draw_mode = OUTLINE;
7189 set_foreground_color(default_fg_color);
7198 swap.swap_window = window.window;
7199 swap.swap_action = XdbeBackground;
7200 XdbeSwapBuffers(display, &swap, 1);
7207 static void clear_text(int exposures)
7211 /* The swap action is XdbeBackground, which clears */
7216 /* there is some extra space for borders and outlines */
7217 XClearArea(display, window.window, text_start_x - border_margin - 1,
7218 text_start_y - border_margin - 1,
7219 text_width + border_margin * 2 + 2,
7220 text_height + border_margin * 2 + 2, exposures ? True : 0);
7225 static int need_to_update;
7227 /* update_text() generates new text and clears old text area */
7228 static void update_text(void)
7237 static void main_loop(void)
7239 #ifdef SIGNAL_BLOCKING
7240 sigset_t newmask, oldmask;
7243 Region region = XCreateRegion();
7247 XserverRegion region2, part;
7248 int event_base, error_base;
7250 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
7251 ERR("Xdamage extension unavailable");
7253 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7254 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7255 part = XFixesCreateRegionFromWindow(display, window.window, 0);
7256 #endif /* HAVE_XDAMAGE */
7259 #ifdef SIGNAL_BLOCKING
7260 sigemptyset(&newmask);
7261 sigaddset(&newmask, SIGINT);
7262 sigaddset(&newmask, SIGTERM);
7263 sigaddset(&newmask, SIGUSR1);
7267 while (total_run_times == 0 || info.looped < total_run_times) {
7270 #ifdef SIGNAL_BLOCKING
7271 /* block signals. we will inspect for pending signals later */
7272 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7273 CRIT_ERR("unable to sigprocmask()");
7280 /* wait for X event or timeout */
7282 if (!XPending(display)) {
7286 double t = update_interval - (get_time() - last_update_time);
7292 tv.tv_sec = (long) t;
7293 tv.tv_usec = (long) (t * 1000000) % 1000000;
7295 FD_SET(ConnectionNumber(display), &fdsr);
7297 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7299 if (errno != EINTR) {
7300 ERR("can't select(): %s", strerror(errno));
7306 /* FIXME just sleep for the interval time if no X11 */
7307 usleep(update_interval * 1000000);
7315 if (need_to_update) {
7317 int wx = window.x, wy = window.y;
7325 /* resize window if it isn't right size */
7327 && (text_width + border_margin * 2 + 1 != window.width
7328 || text_height + border_margin * 2 + 1 != window.height)) {
7329 window.width = text_width + border_margin * 2 + 1;
7330 window.height = text_height + border_margin * 2 + 1;
7331 XResizeWindow(display, window.window, window.width,
7334 set_transparent_background(window.window);
7338 /* move window if it isn't in right position */
7339 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7340 XMoveWindow(display, window.window, window.x, window.y);
7351 r.x = text_start_x - border_margin;
7352 r.y = text_start_y - border_margin;
7353 r.width = text_width + border_margin * 2;
7354 r.height = text_height + border_margin * 2;
7355 XUnionRectWithRegion(&r, region, region);
7360 /* handle X events */
7361 while (XPending(display)) {
7364 XNextEvent(display, &ev);
7372 r.width = ev.xexpose.width;
7373 r.height = ev.xexpose.height;
7374 XUnionRectWithRegion(&r, region, region);
7379 case ReparentNotify:
7380 /* set background to ParentRelative for all parents */
7382 set_transparent_background(window.window);
7386 case ConfigureNotify:
7388 /* if window size isn't what expected, set fixed size */
7389 if (ev.xconfigure.width != window.width
7390 || ev.xconfigure.height != window.height) {
7391 if (window.width != 0 && window.height != 0) {
7395 /* clear old stuff before screwing up
7400 XWindowAttributes attrs;
7402 if (XGetWindowAttributes(display,
7403 window.window, &attrs)) {
7404 window.width = attrs.width;
7405 window.height = attrs.height;
7409 text_width = window.width - border_margin * 2 - 1;
7410 text_height = window.height - border_margin * 2 - 1;
7411 if (text_width > maximum_width
7412 && maximum_width > 0) {
7413 text_width = maximum_width;
7417 /* if position isn't what expected, set fixed pos
7418 * total_updates avoids setting fixed_pos when window
7419 * is set to weird locations when started */
7420 /* // this is broken
7421 if (total_updates >= 2 && !fixed_pos
7422 && (window.x != ev.xconfigure.x
7423 || window.y != ev.xconfigure.y)
7424 && (ev.xconfigure.x != 0
7425 || ev.xconfigure.y != 0)) {
7434 /* if an ordinary window with decorations */
7435 if ((window.type == TYPE_NORMAL)
7436 && (!TEST_HINT(window.hints,
7437 HINT_UNDECORATED))) {
7438 /* allow conky to hold input focus. */
7441 /* forward the click to the desktop window */
7442 XUngrabPointer(display, ev.xbutton.time);
7443 ev.xbutton.window = window.desktop;
7444 XSendEvent(display, ev.xbutton.window, False,
7445 ButtonPressMask, &ev);
7446 XSetInputFocus(display, ev.xbutton.window,
7447 RevertToParent, ev.xbutton.time);
7454 /* if an ordinary window with decorations */
7455 if ((window.type == TYPE_NORMAL)
7456 && (!TEST_HINT(window.hints,
7457 HINT_UNDECORATED))) {
7458 /* allow conky to hold input focus. */
7461 /* forward the release to the desktop window */
7462 ev.xbutton.window = window.desktop;
7463 XSendEvent(display, ev.xbutton.window, False,
7464 ButtonReleaseMask, &ev);
7473 if (ev.type == event_base + XDamageNotify) {
7474 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7476 XFixesSetRegion(display, part, &dev->area, 1);
7477 XFixesUnionRegion(display, region2, region2, part);
7479 #endif /* HAVE_XDAMAGE */
7485 XDamageSubtract(display, damage, region2, None);
7486 XFixesSetRegion(display, region2, 0, 0);
7487 #endif /* HAVE_XDAMAGE */
7489 /* XDBE doesn't seem to provide a way to clear the back buffer without
7490 * interfering with the front buffer, other than passing XdbeBackground
7491 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7492 * redraw the text even if it wasn't part of the exposed area. OTOH,
7493 * if we're not going to call draw_stuff at all, then no swap happens
7494 * and we can safely do nothing. */
7496 if (!XEmptyRegion(region)) {
7501 r.x = text_start_x - border_margin;
7502 r.y = text_start_y - border_margin;
7503 r.width = text_width + border_margin * 2;
7504 r.height = text_height + border_margin * 2;
7505 XUnionRectWithRegion(&r, region, region);
7508 XSetRegion(display, window.gc, region);
7511 XftDrawSetClip(window.xftdraw, region);
7517 XDestroyRegion(region);
7518 region = XCreateRegion();
7522 #ifdef SIGNAL_BLOCKING
7523 /* unblock signals of interest and let handler fly */
7524 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7525 CRIT_ERR("unable to sigprocmask()");
7529 switch (g_signal_pending) {
7532 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7537 ERR("received SIGINT or SIGTERM to terminate. bye!");
7540 XDestroyRegion(region);
7543 XDamageDestroy(display, damage);
7544 XFixesDestroyRegion(display, region2);
7545 XFixesDestroyRegion(display, part);
7546 #endif /* HAVE_XDAMAGE */
7548 return; /* return from main_loop */
7551 /* Reaching here means someone set a signal
7552 * (SIGXXXX, signal_handler), but didn't write any code
7554 * If you don't want to handle a signal, don't set a handler on
7555 * it in the first place. */
7556 if (g_signal_pending) {
7557 ERR("ignoring signal (%d)", g_signal_pending);
7561 g_signal_pending = 0;
7564 #if defined(X11) && defined(HAVE_XDAMAGE)
7565 XDamageDestroy(display, damage);
7566 XFixesDestroyRegion(display, region2);
7567 XFixesDestroyRegion(display, part);
7568 XDestroyRegion(region);
7570 #endif /* X11 && HAVE_XDAMAGE */
7573 static void load_config_file(const char *);
7575 /* reload the config file */
7576 void reload_config(void)
7578 timed_thread_destroy_registered_threads();
7580 if (info.cpu_usage) {
7581 free(info.cpu_usage);
7582 info.cpu_usage = NULL;
7590 if (info.mpd.title) {
7591 free(info.mpd.title);
7592 info.mpd.title = NULL;
7594 if (info.mpd.artist) {
7595 free(info.mpd.artist);
7596 info.mpd.artist = NULL;
7598 if (info.mpd.album) {
7599 free(info.mpd.album);
7600 info.mpd.album = NULL;
7602 if (info.mpd.random) {
7603 free(info.mpd.random);
7604 info.mpd.random = NULL;
7606 if (info.mpd.repeat) {
7607 free(info.mpd.repeat);
7608 info.mpd.repeat = NULL;
7610 if (info.mpd.track) {
7611 free(info.mpd.track);
7612 info.mpd.track = NULL;
7614 if (info.mpd.name) {
7615 free(info.mpd.name);
7616 info.mpd.name = NULL;
7618 if (info.mpd.file) {
7619 free(info.mpd.file);
7620 info.mpd.file = NULL;
7622 if (info.mpd.status) {
7623 free(info.mpd.status);
7624 info.mpd.status = NULL;
7632 #ifdef TCP_PORT_MONITOR
7633 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7636 if (current_config) {
7638 load_config_file(current_config);
7640 /* re-init specials array */
7641 if ((specials = realloc((void *) specials,
7642 sizeof(struct special_t) * max_specials)) == 0) {
7643 ERR("failed to realloc specials array");
7649 // clear the window first
7650 XClearWindow(display, RootWindow(display, screen));
7653 #ifdef TCP_PORT_MONITOR
7654 info.p_tcp_port_monitor_collection = NULL;
7656 extract_variable_text(text);
7662 tmpstring1 = malloc(text_buffer_size);
7663 memset(tmpstring1, 0, text_buffer_size);
7667 tmpstring2 = malloc(text_buffer_size);
7668 memset(tmpstring2, 0, text_buffer_size);
7672 text_buffer = malloc(max_user_text);
7673 memset(text_buffer, 0, max_user_text);
7680 timed_thread_destroy_registered_threads();
7682 if (info.cpu_usage) {
7683 free(info.cpu_usage);
7684 info.cpu_usage = NULL;
7689 XdbeDeallocateBackBufferName(display, window.back_buffer);
7694 XDestroyWindow(display, window.window);
7695 XClearWindow(display, RootWindow(display, screen));
7700 XClearWindow(display, RootWindow(display, screen));
7705 XFreeGC(display, window.gc);
7709 free_text_objects(global_text_object_list);
7710 free(global_text_object_list);
7711 global_text_object_list = NULL;
7730 free(current_config);
7732 #ifdef TCP_PORT_MONITOR
7733 destroy_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
7734 info.p_tcp_port_monitor_collection = NULL;
7743 for (i = 0; i < special_count; i++) {
7744 if (specials[i].type == GRAPH) {
7745 free(specials[i].graph);
7752 clear_diskio_stats();
7755 static int string_to_bool(const char *s)
7758 // Assumes an option without a true/false means true
7760 } else if (strcasecmp(s, "yes") == 0) {
7762 } else if (strcasecmp(s, "true") == 0) {
7764 } else if (strcasecmp(s, "1") == 0) {
7771 static enum alignment string_to_alignment(const char *s)
7773 if (strcasecmp(s, "top_left") == 0) {
7775 } else if (strcasecmp(s, "top_right") == 0) {
7777 } else if (strcasecmp(s, "top_middle") == 0) {
7779 } else if (strcasecmp(s, "bottom_left") == 0) {
7781 } else if (strcasecmp(s, "bottom_right") == 0) {
7782 return BOTTOM_RIGHT;
7783 } else if (strcasecmp(s, "bottom_middle") == 0) {
7784 return BOTTOM_MIDDLE;
7785 } else if (strcasecmp(s, "middle_left") == 0) {
7787 } else if (strcasecmp(s, "middle_right") == 0) {
7788 return MIDDLE_RIGHT;
7789 } else if (strcasecmp(s, "tl") == 0) {
7791 } else if (strcasecmp(s, "tr") == 0) {
7793 } else if (strcasecmp(s, "tm") == 0) {
7795 } else if (strcasecmp(s, "bl") == 0) {
7797 } else if (strcasecmp(s, "br") == 0) {
7798 return BOTTOM_RIGHT;
7799 } else if (strcasecmp(s, "bm") == 0) {
7800 return BOTTOM_MIDDLE;
7801 } else if (strcasecmp(s, "ml") == 0) {
7803 } else if (strcasecmp(s, "mr") == 0) {
7804 return MIDDLE_RIGHT;
7805 } else if (strcasecmp(s, "none") == 0) {
7812 static void set_default_configurations(void)
7814 fork_to_background = 0;
7815 total_run_times = 0;
7816 info.cpu_avg_samples = 2;
7817 info.net_avg_samples = 2;
7824 strcpy(info.mpd.host, "localhost");
7825 info.mpd.port = 6600;
7826 info.mpd.status = NULL;
7827 info.mpd.artist = NULL;
7828 info.mpd.album = NULL;
7829 info.mpd.title = NULL;
7830 info.mpd.random = NULL;
7831 info.mpd.track = NULL;
7832 info.mpd.name = NULL;
7833 info.mpd.file = NULL;
7836 info.xmms2.artist = NULL;
7837 info.xmms2.album = NULL;
7838 info.xmms2.title = NULL;
7839 info.xmms2.genre = NULL;
7840 info.xmms2.comment = NULL;
7841 info.xmms2.url = NULL;
7842 info.xmms2.status = NULL;
7843 info.xmms2.playlist = NULL;
7845 use_spacer = NO_SPACER;
7852 show_graph_scale = 0;
7853 default_fg_color = WhitePixel(display, screen);
7854 default_bg_color = BlackPixel(display, screen);
7855 default_out_color = BlackPixel(display, screen);
7856 color0 = default_fg_color;
7857 color1 = default_fg_color;
7858 color2 = default_fg_color;
7859 color3 = default_fg_color;
7860 color4 = default_fg_color;
7861 color5 = default_fg_color;
7862 color6 = default_fg_color;
7863 color7 = default_fg_color;
7864 color8 = default_fg_color;
7865 color9 = default_fg_color;
7868 draw_graph_borders = 1;
7870 set_first_font("6x10");
7878 window.type = TYPE_NORMAL;
7880 strcpy(window.class_name, "Conky");
7882 sprintf(window.title, "Conky (%s)", info.uname_s.nodename);
7884 stippled_borders = 0;
7887 text_alignment = BOTTOM_LEFT;
7890 free(current_mail_spool);
7894 variable_substitute(MAIL_FILE, buf, 256);
7895 if (buf[0] != '\0') {
7896 current_mail_spool = strndup(buf, text_buffer_size);
7901 update_interval = 3.0;
7902 info.music_player_interval = 1.0;
7903 stuff_in_upper_case = 0;
7904 info.users.number = 1;
7906 #ifdef TCP_PORT_MONITOR
7907 tcp_port_monitor_args.max_port_monitor_connections =
7908 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
7912 static void load_config_file(const char *f)
7914 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7918 set_default_configurations();
7925 char buf[256], *p, *p2, *name, *value;
7928 if (fgets(buf, 256, fp) == NULL) {
7934 /* break at comment */
7935 p2 = strchr(p, '#');
7941 while (*p && isspace((int) *p)) {
7945 continue; /* empty line */
7952 while (*p2 && !isspace((int) *p2)) {
7956 *p2 = '\0'; /* break at name's end */
7963 while (*p && isspace((int) *p)) {
7969 p2 = value + strlen(value);
7970 while (isspace((int) *(p2 - 1))) {
7977 #define CONF2(a) if (strcasecmp(name, a) == 0)
7978 #define CONF(a) else CONF2(a)
7979 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7980 || strcasecmp(name, b) == 0)
7983 CONF2("alignment") {
7985 int a = string_to_alignment(value);
7996 CONF("background") {
7997 fork_to_background = string_to_bool(value);
8000 CONF2("background") {
8001 fork_to_background = string_to_bool(value);
8005 CONF("show_graph_scale") {
8006 show_graph_scale = string_to_bool(value);
8008 CONF("border_margin") {
8010 border_margin = strtol(value, 0, 0);
8015 CONF("border_width") {
8017 border_width = strtol(value, 0, 0);
8024 color0 = get_x11_color(value);
8031 color1 = get_x11_color(value);
8038 color2 = get_x11_color(value);
8045 color3 = get_x11_color(value);
8052 color4 = get_x11_color(value);
8059 color5 = get_x11_color(value);
8066 color6 = get_x11_color(value);
8073 color7 = get_x11_color(value);
8080 color8 = get_x11_color(value);
8087 color9 = get_x11_color(value);
8092 CONF("default_color") {
8094 default_fg_color = get_x11_color(value);
8099 CONF3("default_shade_color", "default_shadecolor") {
8101 default_bg_color = get_x11_color(value);
8106 CONF3("default_outline_color", "default_outlinecolor") {
8108 default_out_color = get_x11_color(value);
8116 info.mail = parse_mail_args(IMAP, value);
8123 info.mail = parse_mail_args(POP3, value);
8131 strncpy(info.mpd.host, value, 127);
8138 info.mpd.port = strtol(value, 0, 0);
8139 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
8144 CONF("mpd_password") {
8146 strncpy(info.mpd.password, value, 127);
8152 CONF("music_player_interval") {
8154 info.music_player_interval = strtod(value, 0);
8160 CONF("sensor_device") {
8162 sensor_device = strtol(value, 0, 0);
8168 CONF("cpu_avg_samples") {
8170 cpu_avg_samples = strtol(value, 0, 0);
8171 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8174 info.cpu_avg_samples = cpu_avg_samples;
8180 CONF("net_avg_samples") {
8182 net_avg_samples = strtol(value, 0, 0);
8183 if (net_avg_samples < 1 || net_avg_samples > 14) {
8186 info.net_avg_samples = net_avg_samples;
8194 CONF("double_buffer") {
8195 use_xdbe = string_to_bool(value);
8199 CONF("override_utf8_locale") {
8200 utf8_mode = string_to_bool(value);
8202 CONF("draw_borders") {
8203 draw_borders = string_to_bool(value);
8205 CONF("draw_graph_borders") {
8206 draw_graph_borders = string_to_bool(value);
8208 CONF("draw_shades") {
8209 draw_shades = string_to_bool(value);
8211 CONF("draw_outline") {
8212 draw_outline = string_to_bool(value);
8215 CONF("out_to_console") {
8216 out_to_console = string_to_bool(value);
8218 CONF("use_spacer") {
8220 if (strcasecmp(value, "left") == 0) {
8221 use_spacer = LEFT_SPACER;
8222 } else if (strcasecmp(value, "right") == 0) {
8223 use_spacer = RIGHT_SPACER;
8224 } else if (strcasecmp(value, "none") == 0) {
8225 use_spacer = NO_SPACER;
8227 use_spacer = string_to_bool(value);
8228 ERR("use_spacer should have an argument of left, right, or"
8229 " none. '%s' seems to be some form of '%s', so"
8230 " defaulting to %s.", value,
8231 use_spacer ? "true" : "false",
8232 use_spacer ? "right" : "none");
8234 use_spacer = RIGHT_SPACER;
8236 use_spacer = NO_SPACER;
8240 ERR("use_spacer should have an argument. Defaulting to right.");
8241 use_spacer = RIGHT_SPACER;
8247 use_xft = string_to_bool(value);
8251 set_first_font(value);
8257 if (value && font_count >= 0) {
8258 fonts[0].font_alpha = atof(value) * 65535.0;
8267 if (string_to_bool(value)) {
8268 ERR("Xft not enabled");
8272 /* xftfont silently ignored when no Xft */
8275 /* xftalpha is silently ignored when no Xft */
8280 set_first_font(value);
8290 gap_x = atoi(value);
8297 gap_y = atoi(value);
8303 CONF("mail_spool") {
8307 variable_substitute(value, buffer, 256);
8309 if (buffer[0] != '\0') {
8310 if (current_mail_spool) {
8311 free(current_mail_spool);
8313 current_mail_spool = strndup(buffer, text_buffer_size);
8320 CONF("minimum_size") {
8322 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8324 if (sscanf(value, "%d", &minimum_width) != 1) {
8332 CONF("maximum_width") {
8334 if (sscanf(value, "%d", &maximum_width) != 1) {
8342 CONF("no_buffers") {
8343 no_buffers = string_to_bool(value);
8345 CONF("top_cpu_separate") {
8346 cpu_separate = string_to_bool(value);
8348 CONF("short_units") {
8349 short_units = string_to_bool(value);
8351 CONF("pad_percents") {
8352 pad_percents = atoi(value);
8356 CONF("own_window") {
8358 own_window = string_to_bool(value);
8363 CONF("own_window_class") {
8365 memset(window.class_name, 0, sizeof(window.class_name));
8366 strncpy(window.class_name, value,
8367 sizeof(window.class_name) - 1);
8372 CONF("own_window_title") {
8374 memset(window.title, 0, sizeof(window.title));
8375 strncpy(window.title, value, sizeof(window.title) - 1);
8380 CONF("own_window_transparent") {
8382 set_transparent = string_to_bool(value);
8387 CONF("own_window_colour") {
8389 background_colour = get_x11_color(value);
8391 ERR("Invalid colour for own_window_colour (try omitting the "
8392 "'#' for hex colours");
8395 CONF("own_window_hints") {
8397 char *p_hint, *p_save;
8398 char delim[] = ", ";
8400 /* tokenize the value into individual hints */
8401 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8403 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8404 if (strncmp(p_hint, "undecorate", 10) == 0) {
8405 SET_HINT(window.hints, HINT_UNDECORATED);
8406 } else if (strncmp(p_hint, "below", 5) == 0) {
8407 SET_HINT(window.hints, HINT_BELOW);
8408 } else if (strncmp(p_hint, "above", 5) == 0) {
8409 SET_HINT(window.hints, HINT_ABOVE);
8410 } else if (strncmp(p_hint, "sticky", 6) == 0) {
8411 SET_HINT(window.hints, HINT_STICKY);
8412 } else if (strncmp(p_hint, "skip_taskbar", 12) == 0) {
8413 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8414 } else if (strncmp(p_hint, "skip_pager", 10) == 0) {
8415 SET_HINT(window.hints, HINT_SKIP_PAGER);
8420 p_hint = strtok_r(NULL, delim, &p_save);
8421 } while (p_hint != NULL);
8427 CONF("own_window_type") {
8429 if (strncmp(value, "normal", 6) == 0) {
8430 window.type = TYPE_NORMAL;
8431 } else if (strncmp(value, "desktop", 7) == 0) {
8432 window.type = TYPE_DESKTOP;
8433 } else if (strncmp(value, "dock", 7) == 0) {
8434 window.type = TYPE_DOCK;
8435 } else if (strncmp(value, "override", 8) == 0) {
8436 window.type = TYPE_OVERRIDE;
8445 CONF("stippled_borders") {
8447 stippled_borders = strtol(value, 0, 0);
8449 stippled_borders = 4;
8454 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
8458 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
8461 CONF("update_interval") {
8463 update_interval = strtod(value, 0);
8467 if (info.music_player_interval == 0) {
8468 // default to update_interval
8469 info.music_player_interval = update_interval;
8472 CONF("total_run_times") {
8474 total_run_times = strtod(value, 0);
8480 stuff_in_upper_case = string_to_bool(value);
8482 CONF("max_specials") {
8484 max_specials = atoi(value);
8489 CONF("max_user_text") {
8491 max_user_text = atoi(value);
8496 CONF("text_buffer_size") {
8498 text_buffer_size = atoi(value);
8499 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8500 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8501 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8513 text = (char *) malloc(1);
8517 unsigned int l = strlen(text);
8519 if (fgets(buf, 256, fp) == NULL) {
8522 text = (char *) realloc(text, l + strlen(buf) + 1);
8525 if (strlen(text) > max_user_text) {
8530 if (strlen(text) < 1) {
8531 CRIT_ERR("no text supplied in configuration; exiting");
8533 text_lines = line + 1;
8536 #ifdef TCP_PORT_MONITOR
8537 CONF("max_port_monitor_connections") {
8538 if (!value || (sscanf(value, "%d",
8539 &tcp_port_monitor_args.max_port_monitor_connections) != 1)
8540 || tcp_port_monitor_args.max_port_monitor_connections < 0) {
8541 /* an error. use default, warn and continue. */
8542 tcp_port_monitor_args.max_port_monitor_connections =
8543 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8545 } else if (tcp_port_monitor_args.max_port_monitor_connections
8547 /* no error, just use default */
8548 tcp_port_monitor_args.max_port_monitor_connections =
8549 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8551 /* else tcp_port_monitor_args.max_port_monitor_connections > 0
8555 CONF("if_up_strictness") {
8557 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8558 ifup_strictness = IFUP_UP;
8559 } else if (!strcmp(value, "up")) {
8560 ifup_strictness = IFUP_UP;
8561 } else if (!strcmp(value, "link")) {
8562 ifup_strictness = IFUP_LINK;
8563 } else if (!strcmp(value, "address")) {
8564 ifup_strictness = IFUP_ADDR;
8566 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8567 ifup_strictness = IFUP_UP;
8571 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8582 if (info.music_player_interval == 0) {
8583 // default to update_interval
8584 info.music_player_interval = update_interval;
8586 if (!text) { // didn't supply any text
8587 CRIT_ERR("missing text block in configuration; exiting");
8591 static void print_help(const char *prog_name) {
8592 printf("Usage: %s [OPTION]...\n"
8593 "Conky is a system monitor that renders text on desktop or to own transparent\n"
8594 "window. Command line options will override configurations defined in config\n"
8596 " -v, --version version\n"
8597 " -q, --quiet quiet mode\n"
8598 " -c, --config=FILE config file to load\n"
8599 " -d, --daemonize daemonize, fork to background\n"
8600 " -h, --help help\n"
8602 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8603 " -f, --font=FONT font to use\n"
8605 " -o, --own-window create own window to draw\n"
8608 " -b, --double-buffer double buffer (prevents flickering)\n"
8610 " -w, --window-id=WIN_ID window id to draw\n"
8611 " -x X x position\n"
8612 " -y Y y position\n"
8614 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8615 " -u, --interval=SECS update interval\n"
8616 " -i COUNT number of times to update Conky (and quit)\n",
8621 /* : means that character before that takes an argument */
8622 static const char *getopt_string = "vVqdt:u:i:hc:"
8634 static const struct option longopts[] = {
8635 { "help", 0, NULL, 'h' },
8636 { "version", 0, NULL, 'V' },
8637 { "config", 1, NULL, 'c' },
8638 { "daemonize", 0, NULL, 'd' },
8640 { "alignment", 1, NULL, 'a' },
8641 { "font", 1, NULL, 'f' },
8643 { "own-window", 0, NULL, 'o' },
8646 { "double-buffer", 0, NULL, 'b' },
8648 { "window-id", 1, NULL, 'w' },
8650 { "text", 1, NULL, 't' },
8651 { "interval", 0, NULL, 'u' },
8655 int main(int argc, char **argv)
8661 struct sigaction act, oact;
8663 g_signal_pending = 0;
8664 memset(&info, 0, sizeof(info));
8666 #ifdef TCP_PORT_MONITOR
8667 tcp_port_monitor_args.max_port_monitor_connections =
8668 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
8671 /* handle command line parameters that don't change configs */
8673 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8674 || ((s = getenv("LANG")) && *s)) {
8675 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8677 ERR("malloc failed");
8679 for (x = 0; x < strlen(s); x++) {
8680 temp[x] = tolower(s[x]);
8683 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8689 if (!setlocale(LC_CTYPE, "")) {
8690 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8694 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8705 if (current_config) {
8706 free(current_config);
8708 current_config = strndup(optarg, max_user_text);
8711 freopen("/dev/null", "w", stderr);
8714 print_help(argv[0]);
8718 window.window = strtol(optarg, 0, 0);
8727 /* initalize X BEFORE we load config.
8728 * (we need to so that 'screen' is set) */
8732 /* check if specified config file is valid */
8733 if (current_config) {
8735 if (stat(current_config, &sb) ||
8736 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
8737 ERR("invalid configuration file '%s'\n", current_config);
8738 free(current_config);
8743 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8745 if (!current_config) {
8746 /* load default config file */
8750 /* Try to use personal config file first */
8751 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
8752 if (buf[0] && (fp = fopen(buf, "r"))) {
8753 current_config = strndup(buf, max_user_text);
8757 /* Try to use system config file if personal config not readable */
8758 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8759 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
8763 /* No readable config found */
8764 if (!current_config) {
8765 CRIT_ERR("no readable personal or system-wide config file found");
8769 load_config_file(current_config);
8771 /* init specials array */
8772 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8773 ERR("failed to create specials array");
8777 if (current_mail_spool == NULL) {
8780 variable_substitute(MAIL_FILE, buf, 256);
8782 if (buf[0] != '\0') {
8783 current_mail_spool = strndup(buf, text_buffer_size);
8788 /* handle other command line arguments */
8790 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8791 || defined(__NetBSD__)
8792 optind = optreset = 1;
8797 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8798 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8799 "kvm_open")) == NULL) {
8800 CRIT_ERR("cannot read kvm");
8805 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8813 fork_to_background = 1;
8818 set_first_font(optarg);
8821 text_alignment = string_to_alignment(optarg);
8840 text = strndup(optarg, max_user_text);
8841 convert_escapes(text);
8845 update_interval = strtod(optarg, 0);
8846 if (info.music_player_interval == 0) {
8847 // default to update_interval
8848 info.music_player_interval = update_interval;
8853 total_run_times = strtod(optarg, 0);
8857 gap_x = atoi(optarg);
8861 gap_y = atoi(optarg);
8875 /* generate text and get initial size */
8876 extract_variable_text(text);
8883 if (fork_to_background) {
8888 ERR("Conky: couldn't fork() to background: %s",
8895 fprintf(stderr, "\n");
8900 /* parent process */
8901 fprintf(stderr, "Conky: forked to background, pid is %d\n",
8908 text_buffer = malloc(max_user_text);
8909 memset(text_buffer, 0, max_user_text);
8910 tmpstring1 = malloc(text_buffer_size);
8911 memset(tmpstring1, 0, text_buffer_size);
8912 tmpstring2 = malloc(text_buffer_size);
8913 memset(tmpstring2, 0, text_buffer_size);
8917 update_text_area(); /* to get initial size of the window */
8920 init_window(own_window, text_width + border_margin * 2 + 1,
8921 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8923 #else /* OWN_WINDOW */
8924 init_window(0, text_width + border_margin * 2 + 1,
8925 text_height + border_margin * 2 + 1, set_transparent, 0,
8927 #endif /* OWN_WINDOW */
8930 update_text_area(); /* to position text/window on screen */
8935 if (own_window && !fixed_pos) {
8936 XMoveWindow(display, window.window, window.x, window.y);
8939 set_transparent_background(window.window);
8949 /* Set signal handlers */
8950 act.sa_handler = signal_handler;
8951 sigemptyset(&act.sa_mask);
8954 act.sa_flags |= SA_RESTART;
8957 if (sigaction(SIGINT, &act, &oact) < 0
8958 || sigaction(SIGUSR1, &act, &oact) < 0
8959 || sigaction(SIGHUP,&act,&oact) < 0
8960 || sigaction(SIGTERM, &act, &oact) < 0) {
8961 ERR("error setting signal handler: %s", strerror(errno));
8964 /* *************** *
8966 * *************** */
8969 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8976 void signal_handler(int sig)
8978 /* signal handler is light as a feather, as it should be.
8979 * we will poll g_signal_pending with each loop of conky
8980 * and do any signal processing there, NOT here. */
8981 g_signal_pending = sig;