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/>.
28 #include "text_object.h"
43 #include <X11/Xutil.h>
45 #include <X11/extensions/Xdamage.h>
51 #include <sys/types.h>
53 #include <netinet/in.h>
63 #include "defconfig.h"
64 #ifdef HAVE_FOPENCOOKIE
65 #include "conf_cookie.h"
71 #include "temphelper.h"
74 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
77 #define MAIL_FILE "$MAIL"
78 #define MAX_IF_BLOCK_DEPTH 5
79 #define MAX_TAIL_LINES 100
81 //#define SIGNAL_BLOCKING
82 #undef SIGNAL_BLOCKING
85 int global_debug_level = 0;
87 static void print_version(void) __attribute__((noreturn));
89 static void print_version(void)
91 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
93 printf("\nCompiled in features:\n\n"
94 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
98 " * Xdamage extension\n"
99 # endif /* HAVE_XDAMAGE */
101 " * Xdbe extension (double buffer)\n"
102 # endif /* HAVE_XDBE */
107 "\n Music detection:\n"
110 #endif /* AUDACIOUS */
123 "\n General features:\n"
130 #ifdef TCP_PORT_MONITOR
132 #endif /* TCP_PORT_MONITOR */
141 #endif /* HAVE_IWLIB */
153 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
160 static int text_start_x, text_start_y; /* text start position in window */
161 static int text_width, text_height;
179 char name[DEFAULT_TEXT_BUFFER_SIZE];
189 static int selected_font = 0;
190 static int font_count = -1;
191 struct font_list *fonts = NULL;
195 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
196 fonts[selected_font].xftfont->descent) \
197 : (fonts[selected_font].font->max_bounds.ascent + \
198 fonts[selected_font].font->max_bounds.descent))
199 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
200 : fonts[selected_font].font->max_bounds.ascent)
201 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
202 : fonts[selected_font].font->max_bounds.descent)
206 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
207 fonts[selected_font].font->max_bounds.descent)
208 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
209 #define font_descent() fonts[selected_font].font->max_bounds.descent
213 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
215 static void set_font(void);
217 int addfont(const char *data_in)
219 if (font_count > MAX_FONTS) {
220 CRIT_ERR("you don't need that many fonts, sorry.");
223 if (font_count == 0) {
227 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
231 memset(fonts, 0, sizeof(struct font_list));
233 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
234 memset(&fonts[font_count], 0, sizeof(struct font_list));
236 CRIT_ERR("realloc in addfont");
238 // must account for null terminator
239 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
240 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
242 fonts[font_count].font_alpha = 0xffff;
245 CRIT_ERR("Oops...looks like something overflowed in addfont().");
250 void set_first_font(const char *data_in)
252 if (font_count < 0) {
253 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
257 memset(fonts, 0, sizeof(struct font_list));
260 if (strlen(data_in) > 1) {
261 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
263 fonts[0].font_alpha = 0xffff;
268 void free_fonts(void)
272 for (i = 0; i <= font_count; i++) {
275 XftFontClose(display, fonts[i].xftfont);
276 fonts[i].xftfont = 0;
280 XFreeFont(display, fonts[i].font);
290 static void load_fonts(void)
294 for (i = 0; i <= font_count; i++) {
297 if (use_xft && fonts[i].xftfont) {
299 } else if (use_xft) {
300 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
301 XftFontClose(display, fonts[i].xftfont);
303 fonts[i].xftfont = XftFontOpenName(display, screen,
305 if (fonts[i].xftfont != NULL) {
309 ERR("can't load Xft font '%s'", fonts[i].name);
310 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
311 "courier-12")) != NULL) {
315 ERR("can't load Xft font '%s'", "courier-12");
317 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
318 CRIT_ERR("can't load font '%s'", "fixed");
325 /* load normal font */
326 /* if (fonts[i].font != NULL) {
327 XFreeFont(display, fonts[i].font);
330 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
331 ERR("can't load font '%s'", fonts[i].name);
332 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
333 CRIT_ERR("can't load font '%s'", "fixed");
334 printf("loaded fixed?\n");
342 /* default config file */
343 static char *current_config;
345 /* set to 1 if you want all text to be in uppercase */
346 static unsigned int stuff_in_upper_case;
348 /* Run how many times? */
349 static unsigned long total_run_times;
352 static int fork_to_background;
354 static int cpu_avg_samples, net_avg_samples;
358 static int show_graph_scale;
359 static int show_graph_range;
361 /* Position on the screen */
362 static int text_alignment;
363 static int gap_x, gap_y;
366 static int draw_borders;
367 static int draw_graph_borders;
368 static int stippled_borders;
370 static int draw_shades, draw_outline;
372 static int border_margin, border_width;
374 static long default_fg_color, default_bg_color, default_out_color;
376 /* create own window or draw stuff to root? */
377 static int set_transparent = 0;
380 static int own_window = 0;
381 static int background_colour = 0;
383 /* fixed size/pos is set if wm/user changes them */
384 static int fixed_size = 0, fixed_pos = 0;
387 static int minimum_width, minimum_height;
388 static int maximum_width;
393 static int sensor_device;
396 static long color0, color1, color2, color3, color4, color5, color6, color7,
399 static char *template[10];
401 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
402 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
404 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
405 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
407 /* maximum size of individual text buffers, ie $exec buffer size */
408 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
411 #define CODEPAGE_LENGTH 20
413 long iconv_count = 0;
414 char iconv_converting;
415 static iconv_t **iconv_cd = 0;
417 int register_iconv(iconv_t *new_iconv)
419 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
421 CRIT_ERR("Out of memory");
423 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
424 if (!iconv_cd[iconv_count]) {
425 CRIT_ERR("Out of memory");
427 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
432 void free_iconv(void)
437 for (i = 0; i < iconv_count; i++) {
439 iconv_close(*iconv_cd[i]);
453 /* no buffers in used memory? */
456 /* pad percentages to decimals? */
457 static int pad_percents = 0;
459 static char *global_text = 0;
460 long global_text_lines;
462 static int total_updates;
464 int check_contains(char *f, char *s)
467 FILE *where = fopen(f, "r");
470 char buf1[256], buf2[256];
472 while (fgets(buf1, 256, where)) {
473 sscanf(buf1, "%255s", buf2);
474 if (strstr(buf2, s)) {
481 ERR("Could not open the file");
487 static inline int calc_text_width(const char *s, int l)
494 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
495 (const FcChar8 *) s, l, &gi);
497 XftTextExtents8(display, fonts[selected_font].xftfont,
498 (const FcChar8 *) s, l, &gi);
504 return XTextWidth(fonts[selected_font].font, s, l);
509 /* formatted text to render on screen, generated in generate_text(),
510 * drawn in draw_stuff() */
512 static char *text_buffer;
514 /* special stuff in text_buffer */
516 #define SPECIAL_CHAR '\x01'
545 unsigned long first_colour; // for graph gradient
546 unsigned long last_colour;
550 /* create specials array on heap instead of stack with introduction of
552 static struct special_t *specials = NULL;
554 static unsigned int special_count;
557 static unsigned int special_index; /* used when drawing */
560 /* why 256? cause an array of more then 256 doubles seems excessive,
561 * and who needs that kind of precision anyway? */
562 #define MAX_GRAPH_DEPTH 256
564 static struct special_t *new_special(char *buf, enum special_types t)
566 if (special_count >= max_specials) {
567 CRIT_ERR("too many special things in text");
570 buf[0] = SPECIAL_CHAR;
572 specials[special_count].type = t;
573 return &specials[special_count++];
576 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
580 unsigned int count = 0;
581 static char buf[BUFSZ];
582 long orig_pos = ftell(fp);
584 long buf_size = BUFSZ;
585 char *cur_found = NULL;
587 while (count < step) {
588 if (cur_found == NULL) {
589 buf_size = fread(buf, 1, buf_size, fp);
592 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
593 if (cur_found != NULL) {
594 buf_pos = cur_found - buf + 1;
603 ret = ftell(fp) - buf_size + buf_pos - 1;
605 fseek(fp, orig_pos, SEEK_SET);
610 void *memrchr(const void *buffer, char c, size_t n)
612 const unsigned char *p = buffer;
614 for (p += n; n; n--) {
623 long rev_fcharfind(FILE *fp, char val, unsigned int step)
627 unsigned int count = 0;
628 static char buf[BUFSZ];
629 long orig_pos = ftell(fp);
631 long file_pos = orig_pos;
632 long buf_size = BUFSZ;
635 while (count < step) {
637 if (file_pos > BUFSZ) {
638 fseek(fp, file_pos - BUFSZ, SEEK_SET);
641 fseek(fp, 0, SEEK_SET);
643 file_pos = ftell(fp);
644 buf_pos = fread(buf, 1, buf_size, fp);
646 cur_found = memrchr(buf, val, (size_t) buf_pos);
647 if (cur_found != NULL) {
648 buf_pos = cur_found - buf;
657 fseek(fp, orig_pos, SEEK_SET);
659 ret = file_pos + buf_pos;
664 static void new_bar(char *buf, int w, int h, int usage)
666 struct special_t *s = new_special(buf, BAR);
668 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
673 static const char *scan_bar(const char *args, int *w, int *h)
675 /* zero width means all space that is available */
678 /* bar's argument is either height or height,width */
682 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
683 sscanf(args, "%d %n", h, &n);
691 static char *scan_font(const char *args)
694 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
701 static void new_font(char *buf, char *args)
704 struct special_t *s = new_special(buf, FONT);
706 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
707 int tmp = selected_font;
709 selected_font = s->font_added = addfont(args);
714 struct special_t *s = new_special(buf, FONT);
715 int tmp = selected_font;
717 selected_font = s->font_added = 0;
722 void graph_append(struct special_t *graph, double f, char showaslog)
732 if (!graph->scaled && f > graph->graph_scale) {
733 f = graph->graph_scale;
736 /* Already happens in new_graph
738 graph->graph_scale = 1;
741 graph->graph[0] = f; /* add new data */
742 /* shift all the data by 1 */
743 for (i = graph->graph_width - 1; i > 0; i--) {
744 graph->graph[i] = graph->graph[i - 1];
745 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
746 /* check if we need to update the scale */
747 graph->graph_scale = graph->graph[i];
752 short colour_depth = 0;
753 void set_up_gradient(void);
755 /* precalculated: 31/255, and 63/255 */
756 #define CONST_8_TO_5_BITS 0.12156862745098
757 #define CONST_8_TO_6_BITS 0.247058823529412
759 /* adjust color values depending on color depth */
760 static unsigned int adjust_colors(unsigned int color)
764 if (colour_depth == 0) {
767 if (colour_depth == 16) {
768 r = (color & 0xff0000) >> 16;
769 g = (color & 0xff00) >> 8;
771 color = (int) (r * CONST_8_TO_5_BITS) << 11;
772 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
773 color |= (int) (b * CONST_8_TO_5_BITS);
778 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
779 unsigned int second_colour, double i, int scale, int append, char showaslog)
781 struct special_t *s = new_special(buf, GRAPH);
784 if (s->graph == NULL) {
785 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
786 // subtract 2 for the box
787 s->graph_width = s->width /* - 2 */;
789 s->graph_width = MAX_GRAPH_DEPTH - 2;
791 s->graph = malloc(s->graph_width * sizeof(double));
792 memset(s->graph, 0, s->graph_width * sizeof(double));
793 s->graph_scale = 100;
796 s->first_colour = adjust_colors(first_colour);
797 s->last_colour = adjust_colors(second_colour);
800 s->graph_scale = scale;
808 s->graph_width = s->width - 2; // subtract 2 for rectangle around
812 s->graph_scale = log10(s->graph_scale + 1);
816 graph_append(s, i, showaslog);
820 //don't use spaces in LOGGRAPH or NORMGRAPH if you change them
821 #define LOGGRAPH "log"
822 #define NORMGRAPH "normal"
824 static char *scan_graph(const char *args, int *w, int *h,
825 unsigned int *first_colour, unsigned int *last_colour,
826 unsigned int *scale, char *showaslog)
828 const char *nographtype;
832 /* zero width means all space that is available */
839 //set showaslog and place the rest of the args in nographtype
840 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
843 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
846 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
848 nographtype = &args[strlen(LOGGRAPH) + 1];
849 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
851 nographtype = &args[strlen(NORMGRAPH) + 1];
856 //check the rest of the args
857 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
861 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
864 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
865 return strndup(buf, text_buffer_size);
868 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
869 return strndup(buf, text_buffer_size);
874 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
878 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
881 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
882 return strndup(buf, text_buffer_size);
885 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
886 return strndup(buf, text_buffer_size);
891 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
895 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
898 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
900 //TODO: check the return value and throw an error?
901 sscanf(nographtype, "%63s %d,%d", buf, h, w);
904 return strndup(buf, text_buffer_size);
907 if (buf[0] == '\0') {
910 return strndup(buf, text_buffer_size);
914 static inline void new_hr(char *buf, int a)
916 new_special(buf, HORIZONTAL_LINE)->height = a;
919 static inline void new_stippled_hr(char *buf, int a, int b)
921 struct special_t *s = new_special(buf, STIPPLED_HR);
927 static inline void new_fg(char *buf, long c)
929 new_special(buf, FG)->arg = c;
932 static inline void new_bg(char *buf, long c)
934 new_special(buf, BG)->arg = c;
937 static inline void new_outline(char *buf, long c)
939 new_special(buf, OUTLINE)->arg = c;
942 static inline void new_offset(char *buf, long c)
944 new_special(buf, OFFSET)->arg = c;
947 static inline void new_voffset(char *buf, long c)
949 new_special(buf, VOFFSET)->arg = c;
952 static inline void new_alignr(char *buf, long c)
954 new_special(buf, ALIGNR)->arg = c;
957 // A postive offset pushes the text further left
958 static inline void new_alignc(char *buf, long c)
960 new_special(buf, ALIGNC)->arg = c;
963 static inline void new_goto(char *buf, long c)
965 new_special(buf, GOTO)->arg = c;
968 static inline void new_tab(char *buf, int a, int b)
970 struct special_t *s = new_special(buf, TAB);
976 /* quite boring functions */
978 static inline void for_each_line(char *b, void f(char *))
982 for (ps = b, pe = b; *pe; pe++) {
996 static void convert_escapes(char *buf)
998 char *p = buf, *s = buf;
1005 } else if (*s == '\\') {
1016 /* Prints anything normally printed with snprintf according to the current value
1017 * of use_spacer. Actually slightly more flexible than snprintf, as you can
1018 * safely specify the destination buffer as one of your inputs. */
1019 static int spaced_print(char *, int, const char *, int, const char *, ...)
1020 __attribute__((format(printf, 3, 6)));
1022 static int spaced_print(char *buf, int size, const char *format, int width,
1023 const char *func_name, ...) {
1031 tempbuf = malloc(size * sizeof(char));
1033 // Passes the varargs along to vsnprintf
1034 va_start(argp, func_name);
1035 vsnprintf(tempbuf, size, format, argp);
1038 switch (use_spacer) {
1040 len = snprintf(buf, size, "%s", tempbuf);
1043 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
1046 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
1049 CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
1058 /* converts from bytes to human readable format (k, M, G, T) */
1059 static void human_readable(long long num, char *buf, int size, const char *func_name)
1061 const char **suffix = suffixes;
1064 static const int WIDTH = 10, SHORT_WIDTH = 8;
1066 if (num < 1024LL && num > -1024LL) {
1068 spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num,
1071 spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix);
1076 while ((num / 1024 >= 1000LL || num / 1024 <= -1024LL) && **(suffix + 2)) {
1082 fnum = num / 1024.0;
1087 if (precision < 0) {
1091 len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name,
1092 precision, fnum, **suffix);
1094 len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision,
1097 } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
1100 /* global object list root element */
1101 static struct text_object global_root_object;
1103 static void generate_text_internal(char *p, int p_max_size,
1104 struct text_object text_object, struct information *cur);
1106 static inline void read_exec(const char *data, char *buf, const int size)
1108 FILE *fp = popen(data, "r");
1109 int length = fread(buf, 1, size, fp);
1113 if (length > 0 && buf[length - 1] == '\n') {
1114 buf[length - 1] = '\0';
1118 void *threaded_exec(void *) __attribute__((noreturn));
1120 void *threaded_exec(void *arg)
1123 struct text_object *obj = (struct text_object *)arg;
1126 p2 = obj->data.texeci.buffer;
1127 timed_thread_lock(obj->data.texeci.p_timed_thread);
1128 read_exec(obj->data.texeci.cmd, obj->data.texeci.buffer,
1131 if (*p2 == '\001') {
1136 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1137 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
1138 timed_thread_exit(obj->data.texeci.p_timed_thread);
1144 static struct text_object *new_text_object_internal(void)
1146 struct text_object *obj = malloc(sizeof(struct text_object));
1147 memset(obj, 0, sizeof(struct text_object));
1152 * call with full == 0 when freeing after 'internal' evaluation of objects
1154 static void free_text_objects(struct text_object *root, char full)
1156 struct text_object *obj;
1162 #define data obj->data
1163 for (obj = root->prev; obj; obj = root->prev) {
1164 root->prev = obj->prev;
1165 switch (obj->type) {
1173 close(data.sysfs.fd);
1175 #endif /* !__OpenBSD__ */
1181 free(data.tztime.tz);
1182 free(data.tztime.fmt);
1185 free(data.mboxscan.args);
1186 free(data.mboxscan.output);
1190 free(data.local_mail.box);
1195 case OBJ_imap_unseen:
1196 if (!obj->global_mode) {
1200 case OBJ_imap_messages:
1201 if (!obj->global_mode) {
1208 case OBJ_pop3_unseen:
1209 if (!obj->global_mode) {
1214 if (!obj->global_mode) {
1219 case OBJ_if_existing:
1220 case OBJ_if_mounted:
1221 case OBJ_if_running:
1222 free(data.ifblock.s);
1223 free(data.ifblock.str);
1226 free(data.tail.logfile);
1227 free(data.tail.buffer);
1239 case OBJ_iconv_start:
1244 case OBJ_disk_protect:
1248 free(obj->data.ifblock.s);
1249 free(obj->data.ifblock.str);
1252 free(obj->data.ifblock.s);
1253 free(obj->data.ifblock.str);
1256 if (info.gw_info.iface) {
1257 free(info.gw_info.iface);
1258 info.gw_info.iface = 0;
1260 if (info.gw_info.ip) {
1261 free(info.gw_info.ip);
1262 info.gw_info.ip = 0;
1265 case OBJ_ioscheduler:
1271 case OBJ_xmms2_artist:
1272 if (info.xmms2.artist) {
1273 free(info.xmms2.artist);
1274 info.xmms2.artist = 0;
1277 case OBJ_xmms2_album:
1278 if (info.xmms2.album) {
1279 free(info.xmms2.album);
1280 info.xmms2.album = 0;
1283 case OBJ_xmms2_title:
1284 if (info.xmms2.title) {
1285 free(info.xmms2.title);
1286 info.xmms2.title = 0;
1289 case OBJ_xmms2_genre:
1290 if (info.xmms2.genre) {
1291 free(info.xmms2.genre);
1292 info.xmms2.genre = 0;
1295 case OBJ_xmms2_comment:
1296 if (info.xmms2.comment) {
1297 free(info.xmms2.comment);
1298 info.xmms2.comment = 0;
1302 if (info.xmms2.url) {
1303 free(info.xmms2.url);
1307 case OBJ_xmms2_date:
1308 if (info.xmms2.date) {
1309 free(info.xmms2.date);
1310 info.xmms2.date = 0;
1313 case OBJ_xmms2_status:
1314 if (info.xmms2.status) {
1315 free(info.xmms2.status);
1316 info.xmms2.status = 0;
1319 case OBJ_xmms2_playlist:
1320 if (info.xmms2.playlist) {
1321 free(info.xmms2.playlist);
1322 info.xmms2.playlist = 0;
1325 case OBJ_xmms2_smart:
1326 if (info.xmms2.artist) {
1327 free(info.xmms2.artist);
1328 info.xmms2.artist = 0;
1330 if (info.xmms2.title) {
1331 free(info.xmms2.title);
1332 info.xmms2.title = 0;
1334 if (info.xmms2.url) {
1335 free(info.xmms2.url);
1341 case OBJ_bmpx_title:
1342 case OBJ_bmpx_artist:
1343 case OBJ_bmpx_album:
1344 case OBJ_bmpx_track:
1346 case OBJ_bmpx_bitrate:
1356 free(data.rss.action);
1365 case OBJ_battery_time:
1368 #endif /* !__OpenBSD__ */
1372 case OBJ_execigraph:
1373 free(data.execi.cmd);
1374 free(data.execi.buffer);
1377 free(data.texeci.cmd);
1378 free(data.texeci.buffer);
1380 case OBJ_nameserver:
1385 if (info.first_process) {
1386 free_all_processes();
1387 info.first_process = NULL;
1392 free(data.hddtemp.dev);
1393 free(data.hddtemp.addr);
1394 if (data.hddtemp.temp)
1395 free(data.hddtemp.temp);
1398 case OBJ_entropy_avail:
1399 case OBJ_entropy_poolsize:
1400 case OBJ_entropy_bar:
1402 case OBJ_user_names:
1403 if (info.users.names) {
1404 free(info.users.names);
1405 info.users.names = 0;
1408 case OBJ_user_terms:
1409 if (info.users.terms) {
1410 free(info.users.terms);
1411 info.users.terms = 0;
1414 case OBJ_user_times:
1415 if (info.users.times) {
1416 free(info.users.times);
1417 info.users.times = 0;
1422 case OBJ_smapi_bat_perc:
1423 case OBJ_smapi_bat_temp:
1424 case OBJ_smapi_bat_power:
1427 case OBJ_if_smapi_bat_installed:
1428 free(data.ifblock.s);
1429 free(data.ifblock.str);
1438 case OBJ_mpd_artist:
1440 case OBJ_mpd_random:
1441 case OBJ_mpd_repeat:
1443 case OBJ_mpd_bitrate:
1444 case OBJ_mpd_status:
1447 case OBJ_mpd_password:
1449 case OBJ_mpd_elapsed:
1450 case OBJ_mpd_length:
1454 case OBJ_mpd_percent:
1456 case OBJ_if_mpd_playing:
1458 free_mpd_vars(&info.mpd);
1466 case OBJ_moc_artist:
1469 case OBJ_moc_totaltime:
1470 case OBJ_moc_timeleft:
1471 case OBJ_moc_curtime:
1472 case OBJ_moc_bitrate:
1474 free_moc(&info.moc);
1478 free(data.scroll.text);
1484 /* FIXME: below is surely useless */
1485 if (full) {} // disable warning when MPD !defined
1488 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1493 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1494 *a = mixer_init(buf1);
1495 scan_bar(arg + n, w, h);
1497 *a = mixer_init(NULL);
1498 scan_bar(arg, w, h);
1502 /* strip a leading /dev/ if any, following symlinks first
1504 * BEWARE: this function returns a pointer to static content
1505 * which gets overwritten in consecutive calls. I.e.:
1506 * this function is NOT reentrant.
1508 const char *dev_name(const char *path)
1510 static char buf[255]; /* should be enough for pathnames */
1513 #define DEV_NAME(x) \
1514 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1515 if ((buflen = readlink(path, buf, 254)) == -1)
1516 return DEV_NAME(path);
1518 return DEV_NAME(buf);
1522 /* construct_text_object() creates a new text_object */
1523 static struct text_object *construct_text_object(const char *s,
1524 const char *arg, long line, char allow_threaded)
1526 // struct text_object *obj = new_text_object();
1527 struct text_object *obj = new_text_object_internal();
1531 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1532 obj->type = OBJ_##a; need_mask |= (1 << n); {
1533 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1534 obj->type = OBJ_##a; need_mask |= (1 << n); {
1535 #define END } } else
1539 obj->type = OBJ_color;
1540 obj->data.l = get_x11_color(s);
1544 OBJ(freq, INFO_FREQ)
1547 obj->data.i = open_acpi_temperature(arg);
1548 END OBJ(acpiacadapter, 0)
1549 END OBJ(freq, INFO_FREQ)
1550 #endif /* !__OpenBSD__ */
1552 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1553 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1554 obj->data.cpu_index = 1;
1555 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1556 "Displaying the clock for CPU 1."); */
1558 obj->data.cpu_index = atoi(&arg[0]);
1561 END OBJ(freq_g, INFO_FREQ)
1563 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1564 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1565 obj->data.cpu_index = 1;
1566 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1567 "CPUs! Displaying the clock for CPU 1."); */
1569 obj->data.cpu_index = atoi(&arg[0]);
1572 #if defined(__linux__)
1573 END OBJ(voltage_mv, 0)
1575 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1576 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1577 obj->data.cpu_index = 1;
1578 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1579 "CPUs! Displaying voltage for CPU 1."); */
1581 obj->data.cpu_index = atoi(&arg[0]);
1584 END OBJ(voltage_v, 0)
1586 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1587 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1588 obj->data.cpu_index = 1;
1589 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1590 "CPUs! Displaying voltage for CPU 1."); */
1592 obj->data.cpu_index = atoi(&arg[0]);
1597 END OBJ(wireless_essid, INFO_NET)
1599 obj->data.net = get_net_stat(arg);
1601 CRIT_ERR("wireless_essid: needs an argument");
1603 END OBJ(wireless_mode, INFO_NET)
1605 obj->data.net = get_net_stat(arg);
1607 CRIT_ERR("wireless_mode: needs an argument");
1609 END OBJ(wireless_bitrate, INFO_NET)
1611 obj->data.net = get_net_stat(arg);
1613 CRIT_ERR("wireless_bitrate: needs an argument");
1615 END OBJ(wireless_ap, INFO_NET)
1617 obj->data.net = get_net_stat(arg);
1619 CRIT_ERR("wireless_ap: needs an argument");
1621 END OBJ(wireless_link_qual, INFO_NET)
1623 obj->data.net = get_net_stat(arg);
1625 CRIT_ERR("wireless_link_qual: needs an argument");
1627 END OBJ(wireless_link_qual_max, INFO_NET)
1629 obj->data.net = get_net_stat(arg);
1631 CRIT_ERR("wireless_link_qual_max: needs an argument");
1633 END OBJ(wireless_link_qual_perc, INFO_NET)
1635 obj->data.net = get_net_stat(arg);
1637 CRIT_ERR("wireless_link_qual_perc: needs an argument");
1639 END OBJ(wireless_link_bar, INFO_NET)
1641 arg = scan_bar(arg, &obj->a, &obj->b);
1642 obj->data.net = get_net_stat(arg);
1644 CRIT_ERR("wireless_link_bar: needs an argument");
1646 #endif /* HAVE_IWLIB */
1648 #endif /* __linux__ */
1656 sscanf(arg, "%63s", bat);
1658 strcpy(bat, "BAT0");
1660 obj->data.s = strndup(bat, text_buffer_size);
1661 END OBJ(battery_time, 0)
1665 sscanf(arg, "%63s", bat);
1667 strcpy(bat, "BAT0");
1669 obj->data.s = strndup(bat, text_buffer_size);
1670 END OBJ(battery_percent, 0)
1674 sscanf(arg, "%63s", bat);
1676 strcpy(bat, "BAT0");
1678 obj->data.s = strndup(bat, text_buffer_size);
1679 END OBJ(battery_bar, 0)
1683 arg = scan_bar(arg, &obj->a, &obj->b);
1684 sscanf(arg, "%63s", bat);
1686 strcpy(bat, "BAT0");
1688 obj->data.s = strndup(bat, text_buffer_size);
1689 #endif /* !__OpenBSD__ */
1691 #if defined(__linux__)
1692 END OBJ(disk_protect, 0)
1694 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1696 CRIT_ERR("disk_protect needs an argument");
1697 END OBJ(i8k_version, INFO_I8K)
1698 END OBJ(i8k_bios, INFO_I8K)
1699 END OBJ(i8k_serial, INFO_I8K)
1700 END OBJ(i8k_cpu_temp, INFO_I8K)
1701 END OBJ(i8k_left_fan_status, INFO_I8K)
1702 END OBJ(i8k_right_fan_status, INFO_I8K)
1703 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1704 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1705 END OBJ(i8k_ac_status, INFO_I8K)
1706 END OBJ(i8k_buttons_status, INFO_I8K)
1708 END OBJ(ibm_temps, 0)
1710 CRIT_ERR("ibm_temps: needs an argument");
1712 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1713 obj->data.sensor = 0;
1714 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1715 "Using 0 (CPU temp sensor).");
1717 obj->data.sensor = atoi(&arg[0]);
1718 END OBJ(ibm_volume, 0)
1719 END OBJ(ibm_brightness, 0)
1722 ERR("if_up needs an argument");
1723 obj->data.ifblock.s = 0;
1725 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1726 obj_be_ifblock_if(obj);
1728 obj_be_ifblock_if(obj);
1729 END OBJ(ioscheduler, 0)
1731 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1734 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1735 END OBJ(laptop_mode, 0)
1736 END OBJ(pb_battery, 0)
1737 if (arg && strcmp(arg, "status") == EQUAL) {
1738 obj->data.i = PB_BATT_STATUS;
1739 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1740 obj->data.i = PB_BATT_PERCENT;
1741 } else if (arg && strcmp(arg, "time") == EQUAL) {
1742 obj->data.i = PB_BATT_TIME;
1744 ERR("pb_battery: needs one argument: status, percent or time");
1749 #endif /* __linux__ */
1750 #if defined(__OpenBSD__)
1751 END OBJ(obsd_sensors_temp, 0)
1753 CRIT_ERR("obsd_sensors_temp: needs an argument");
1755 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1756 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1757 obj->data.sensor = 0;
1758 ERR("Invalid temperature sensor number!");
1760 obj->data.sensor = atoi(&arg[0]);
1761 END OBJ(obsd_sensors_fan, 0)
1763 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1766 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1767 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1768 obj->data.sensor = 0;
1769 ERR("Invalid fan sensor number!");
1771 obj->data.sensor = atoi(&arg[0]);
1772 END OBJ(obsd_sensors_volt, 0)
1774 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1777 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1778 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1779 obj->data.sensor = 0;
1780 ERR("Invalid voltage sensor number!");
1782 obj->data.sensor = atoi(&arg[0]);
1783 END OBJ(obsd_vendor, 0)
1784 END OBJ(obsd_product, 0)
1785 #endif /* __OpenBSD__ */
1786 END OBJ(buffers, INFO_BUFFERS)
1787 END OBJ(cached, INFO_BUFFERS)
1788 END OBJ(cpu, INFO_CPU)
1790 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
1791 obj->data.cpu_index = atoi(&arg[3]);
1794 obj->data.cpu_index = 0;
1796 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1798 obj->data.cpu_index = 0;
1800 END OBJ(cpubar, INFO_CPU)
1802 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
1803 obj->data.cpu_index = atoi(&arg[3]);
1806 obj->data.cpu_index = 0;
1808 scan_bar(arg, &obj->a, &obj->b);
1810 scan_bar(arg, &obj->a, &obj->b);
1811 obj->data.cpu_index = 0;
1813 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1814 END OBJ(cpugraph, INFO_CPU)
1815 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1816 &obj->e, &obj->showaslog);
1819 if (strncmp(buf, "cpu", 3) == EQUAL && isdigit(buf[3])) {
1820 obj->data.cpu_index = atoi(&buf[3]);
1822 obj->data.cpu_index = 0;
1826 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1827 END OBJ(loadgraph, INFO_LOADAVG)
1828 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1829 &obj->e, &obj->showaslog);
1833 r = sscanf(arg, "%d", &a);
1835 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1838 #if defined(__linux__)
1839 END OBJ(diskio, INFO_DISKIO)
1841 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1843 obj->data.diskio = NULL;
1845 END OBJ(diskio_read, INFO_DISKIO)
1847 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1849 obj->data.diskio = NULL;
1851 END OBJ(diskio_write, INFO_DISKIO)
1853 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1855 obj->data.diskio = NULL;
1857 END OBJ(diskiograph, INFO_DISKIO)
1858 char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
1859 &obj->e, &obj->showaslog);
1862 obj->data.diskio = prepare_diskio_stat(buf);
1865 obj->data.diskio = NULL;
1867 END OBJ(diskiograph_read, INFO_DISKIO)
1868 char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
1869 &obj->e, &obj->showaslog);
1872 obj->data.diskio = prepare_diskio_stat(buf);
1875 obj->data.diskio = NULL;
1877 END OBJ(diskiograph_write, INFO_DISKIO)
1878 char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
1879 &obj->e, &obj->showaslog);
1882 obj->data.diskio = prepare_diskio_stat(buf);
1885 obj->data.diskio = NULL;
1890 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1893 obj->data.l = color0;
1895 obj->data.l = color1;
1897 obj->data.l = color2;
1899 obj->data.l = color3;
1901 obj->data.l = color4;
1903 obj->data.l = color5;
1905 obj->data.l = color6;
1907 obj->data.l = color7;
1909 obj->data.l = color8;
1911 obj->data.l = color9;
1913 obj->data.s = scan_font(arg);
1914 END OBJ(conky_version, 0)
1915 END OBJ(conky_build_date, 0)
1916 END OBJ(conky_build_arch, 0)
1917 END OBJ(downspeed, INFO_NET)
1919 obj->data.net = get_net_stat(arg);
1921 CRIT_ERR("downspeed needs argument");
1923 END OBJ(downspeedf, INFO_NET)
1925 obj->data.net = get_net_stat(arg);
1927 CRIT_ERR("downspeedf needs argument");
1929 END OBJ(downspeedgraph, INFO_NET)
1930 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1931 &obj->e, &obj->showaslog);
1933 // default to DEFAULTNETDEV
1934 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
1935 obj->data.net = get_net_stat(buf);
1938 obj_be_ifblock_else(obj);
1940 obj_be_ifblock_endif(obj);
1942 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1945 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1947 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1949 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1950 END OBJ(execgraph, 0)
1951 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1952 END OBJ(execibar, 0)
1955 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1958 ERR("${execibar <interval> command}");
1959 obj->type = OBJ_text;
1960 snprintf(buf, 256, "${%s}", s);
1961 obj->data.s = strndup(buf, text_buffer_size);
1963 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1965 END OBJ(execigraph, 0)
1968 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1971 ERR("${execigraph <interval> command}");
1972 obj->type = OBJ_text;
1973 snprintf(buf, 256, "${%s}", s);
1974 obj->data.s = strndup(buf, text_buffer_size);
1976 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1981 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1984 ERR("${execi <interval> command}");
1985 obj->type = OBJ_text;
1986 snprintf(buf, 256, "${%s}", s);
1987 obj->data.s = strndup(buf, text_buffer_size);
1989 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1990 obj->data.execi.buffer = malloc(text_buffer_size);
1995 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1998 ERR("${execi <interval> command}");
1999 obj->type = OBJ_text;
2000 snprintf(buf, 256, "${%s}", s);
2001 obj->data.s = strndup(buf, text_buffer_size);
2003 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2004 obj->data.execi.buffer = malloc(text_buffer_size);
2006 END OBJ_THREAD(texeci, 0)
2009 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2012 ERR("${texeci <interval> command}");
2013 obj->type = OBJ_text;
2014 snprintf(buf, 256, "${%s}", s);
2015 obj->data.s = strndup(buf, text_buffer_size);
2017 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
2018 obj->data.texeci.buffer = malloc(text_buffer_size);
2020 obj->data.texeci.p_timed_thread = NULL;
2021 END OBJ(pre_exec, 0)
2022 obj->type = OBJ_text;
2026 read_exec(arg, buf, sizeof(buf));
2027 obj->data.s = strndup(buf, text_buffer_size);
2029 obj->data.s = strndup("", text_buffer_size);
2032 END OBJ(fs_bar, INFO_FS)
2033 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2035 while (isspace(*arg)) {
2044 obj->data.fsbar.fs = prepare_fs_stat(arg);
2045 END OBJ(fs_bar_free, INFO_FS)
2046 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2048 while (isspace(*arg)) {
2058 obj->data.fsbar.fs = prepare_fs_stat(arg);
2059 END OBJ(fs_free, INFO_FS)
2063 obj->data.fs = prepare_fs_stat(arg);
2064 END OBJ(fs_used_perc, INFO_FS)
2068 obj->data.fs = prepare_fs_stat(arg);
2069 END OBJ(fs_free_perc, INFO_FS)
2073 obj->data.fs = prepare_fs_stat(arg);
2074 END OBJ(fs_size, INFO_FS)
2078 obj->data.fs = prepare_fs_stat(arg);
2079 END OBJ(fs_type, INFO_FS)
2083 obj->data.fs = prepare_fs_stat(arg);
2084 END OBJ(fs_used, INFO_FS)
2088 obj->data.fs = prepare_fs_stat(arg);
2090 obj->data.i = arg ? atoi(arg) : 1;
2091 END OBJ(nameserver, INFO_DNS)
2092 obj->data.i = arg ? atoi(arg) : 0;
2094 obj->data.i = arg ? atoi(arg) : 1;
2096 obj->data.i = arg ? atoi(arg) : 1;
2100 ERR("goto needs arguments");
2101 obj->type = OBJ_text;
2102 obj->data.s = strndup("${goto}", text_buffer_size);
2106 obj->data.i = atoi(arg);
2112 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2113 sscanf(arg, "%d", &b);
2119 obj->data.pair.a = a;
2120 obj->data.pair.b = b;
2123 END OBJ(i2c, INFO_SYSFS)
2124 char buf1[64], buf2[64];
2128 ERR("i2c needs arguments");
2129 obj->type = OBJ_text;
2130 // obj->data.s = strndup("${i2c}", text_buffer_size);
2134 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2135 /* if scanf couldn't read three values, read type and num and use
2137 sscanf(arg, "%63s %d", buf2, &n);
2138 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
2139 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2140 strncpy(obj->data.sysfs.type, buf2, 63);
2142 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
2143 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2144 strncpy(obj->data.sysfs.type, buf2, 63);
2147 END OBJ(platform, INFO_SYSFS)
2148 char buf1[64], buf2[64];
2152 ERR("platform needs arguments");
2153 obj->type = OBJ_text;
2157 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2158 /* if scanf couldn't read three values, read type and num and use
2160 sscanf(arg, "%63s %d", buf2, &n);
2161 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
2162 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2163 strncpy(obj->data.sysfs.type, buf2, 63);
2165 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
2166 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2167 strncpy(obj->data.sysfs.type, buf2, 63);
2170 END OBJ(hwmon, INFO_SYSFS)
2171 char buf1[64], buf2[64];
2175 ERR("hwmon needs argumanets");
2176 obj->type = OBJ_text;
2180 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2181 /* if scanf couldn't read three values, read type and num and use
2183 sscanf(arg, "%63s %d", buf2, &n);
2184 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
2185 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2186 strncpy(obj->data.sysfs.type, buf2, 63);
2188 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
2189 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2190 strncpy(obj->data.sysfs.type, buf2, 63);
2192 #endif /* !__OpenBSD__ */
2194 END OBJ(top, INFO_TOP)
2199 ERR("top needs arguments");
2200 obj->type = OBJ_text;
2201 // obj->data.s = strndup("${top}", text_buffer_size);
2204 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2205 if (strcmp(buf, "name") == EQUAL) {
2206 obj->data.top.type = TOP_NAME;
2207 } else if (strcmp(buf, "cpu") == EQUAL) {
2208 obj->data.top.type = TOP_CPU;
2209 } else if (strcmp(buf, "pid") == EQUAL) {
2210 obj->data.top.type = TOP_PID;
2211 } else if (strcmp(buf, "mem") == EQUAL) {
2212 obj->data.top.type = TOP_MEM;
2213 } else if (strcmp(buf, "time") == EQUAL) {
2214 obj->data.top.type = TOP_TIME;
2215 } else if (strcmp(buf, "mem_res") == EQUAL) {
2216 obj->data.top.type = TOP_MEM_RES;
2217 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
2218 obj->data.top.type = TOP_MEM_VSIZE;
2220 ERR("invalid arg for top");
2223 if (n < 1 || n > 10) {
2224 CRIT_ERR("invalid arg for top");
2227 obj->data.top.num = n - 1;
2231 ERR("invalid args given for top");
2234 END OBJ(top_mem, INFO_TOP)
2239 ERR("top_mem needs arguments");
2240 obj->type = OBJ_text;
2241 obj->data.s = strndup("${top_mem}", text_buffer_size);
2244 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2245 if (strcmp(buf, "name") == EQUAL) {
2246 obj->data.top.type = TOP_NAME;
2247 } else if (strcmp(buf, "cpu") == EQUAL) {
2248 obj->data.top.type = TOP_CPU;
2249 } else if (strcmp(buf, "pid") == EQUAL) {
2250 obj->data.top.type = TOP_PID;
2251 } else if (strcmp(buf, "mem") == EQUAL) {
2252 obj->data.top.type = TOP_MEM;
2253 } else if (strcmp(buf, "time") == EQUAL) {
2254 obj->data.top.type = TOP_TIME;
2255 } else if (strcmp(buf, "mem_res") == EQUAL) {
2256 obj->data.top.type = TOP_MEM_RES;
2257 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
2258 obj->data.top.type = TOP_MEM_VSIZE;
2260 ERR("invalid arg for top");
2263 if (n < 1 || n > 10) {
2264 CRIT_ERR("invalid arg for top");
2267 obj->data.top.num = n - 1;
2271 ERR("invalid args given for top");
2274 END OBJ(addr, INFO_NET)
2276 obj->data.net = get_net_stat(arg);
2278 CRIT_ERR("addr needs argument");
2280 #if defined(__linux__)
2281 END OBJ(addrs, INFO_NET)
2283 obj->data.net = get_net_stat(arg);
2285 CRIT_ERR("addrs needs argument");
2287 #endif /* __linux__ */
2294 ERR("tail needs arguments");
2295 obj->type = OBJ_text;
2296 obj->data.s = strndup("${tail}", text_buffer_size);
2299 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2300 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2301 CRIT_ERR("invalid arg for tail, number of lines must be "
2302 "between 1 and %i", MAX_TAIL_LINES);
2308 obj->data.tail.fd = -1;
2310 if (stat(buf, &st) == 0) {
2311 if (S_ISFIFO(st.st_mode)) {
2312 fd = open(buf, O_RDONLY | O_NONBLOCK);
2315 CRIT_ERR("tail logfile does not exist, or you do "
2316 "not have correct permissions");
2319 obj->data.tail.fd = fd;
2321 fp = fopen(buf, "r");
2325 if (fp || obj->data.tail.fd != -1) {
2326 obj->data.tail.logfile = malloc(text_buffer_size);
2327 strcpy(obj->data.tail.logfile, buf);
2328 obj->data.tail.wantedlines = n1;
2329 obj->data.tail.interval = update_interval * 2;
2331 if (obj->data.tail.fd == -1) {
2336 CRIT_ERR("tail logfile does not exist, or you do not have "
2337 "correct permissions");
2340 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2341 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2342 CRIT_ERR("invalid arg for tail, number of lines must be "
2343 "between 1 and %i", MAX_TAIL_LINES);
2345 } else if (n2 < 1 || n2 < update_interval) {
2346 CRIT_ERR("invalid arg for tail, interval must be greater than "
2347 "0 and "PACKAGE_NAME"'s interval");
2353 obj->data.tail.fd = -1;
2355 if (stat(buf, &st) == 0) {
2356 if (S_ISFIFO(st.st_mode)) {
2357 fd = open(buf, O_RDONLY | O_NONBLOCK);
2360 CRIT_ERR("tail logfile does not exist, or you do "
2361 "not have correct permissions");
2364 obj->data.tail.fd = fd;
2366 fp = fopen(buf, "r");
2370 if (fp || obj->data.tail.fd != -1) {
2371 obj->data.tail.logfile = malloc(text_buffer_size);
2372 strcpy(obj->data.tail.logfile, buf);
2373 obj->data.tail.wantedlines = n1;
2374 obj->data.tail.interval = n2;
2376 if (obj->data.tail.fd == -1) {
2381 CRIT_ERR("tail logfile does not exist, or you do not have "
2382 "correct permissions");
2386 ERR("invalid args given for tail");
2389 /* asumming all else worked */
2390 obj->data.tail.buffer = malloc(text_buffer_size * 20);
2396 ERR("head needs arguments");
2397 obj->type = OBJ_text;
2398 obj->data.s = strndup("${head}", text_buffer_size);
2401 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2402 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2403 CRIT_ERR("invalid arg for head, number of lines must be "
2404 "between 1 and %i", MAX_TAIL_LINES);
2409 fp = fopen(buf, "r");
2411 obj->data.tail.logfile = malloc(text_buffer_size);
2412 strcpy(obj->data.tail.logfile, buf);
2413 obj->data.tail.wantedlines = n1;
2414 obj->data.tail.interval = update_interval * 2;
2418 CRIT_ERR("head logfile does not exist, or you do not have "
2419 "correct permissions");
2422 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2423 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2424 CRIT_ERR("invalid arg for head, number of lines must be "
2425 "between 1 and %i", MAX_TAIL_LINES);
2427 } else if (n2 < 1 || n2 < update_interval) {
2428 CRIT_ERR("invalid arg for head, interval must be greater than "
2429 "0 and "PACKAGE_NAME"'s interval");
2434 fp = fopen(buf, "r");
2436 obj->data.tail.logfile = malloc(text_buffer_size);
2437 strcpy(obj->data.tail.logfile, buf);
2438 obj->data.tail.wantedlines = n1;
2439 obj->data.tail.interval = n2;
2443 CRIT_ERR("head logfile does not exist, or you do not have "
2444 "correct permissions");
2448 ERR("invalid args given for head");
2451 /* asumming all else worked */
2452 obj->data.tail.buffer = malloc(text_buffer_size * 20);
2455 obj->data.s = strdup(arg);
2457 CRIT_ERR("lines needs a argument");
2461 obj->data.s = strdup(arg);
2463 CRIT_ERR("words needs a argument");
2465 END OBJ(loadavg, INFO_LOADAVG)
2466 int a = 1, b = 2, c = 3, r = 3;
2469 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2470 if (r >= 3 && (c < 1 || c > 3)) {
2473 if (r >= 2 && (b < 1 || b > 3)) {
2476 if (r >= 1 && (a < 1 || a > 3)) {
2480 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2481 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2482 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2483 END OBJ(if_empty, 0)
2485 ERR("if_empty needs an argument");
2486 obj->data.ifblock.s = 0;
2488 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2490 obj_be_ifblock_if(obj);
2491 END OBJ(if_existing, 0)
2493 ERR("if_existing needs an argument or two");
2494 obj->data.ifblock.s = NULL;
2495 obj->data.ifblock.str = NULL;
2497 char buf1[256], buf2[256];
2498 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2501 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2502 obj->data.ifblock.str = NULL;
2504 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2505 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2508 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2509 obj_be_ifblock_if(obj);
2510 END OBJ(if_mounted, 0)
2512 ERR("if_mounted needs an argument");
2513 obj->data.ifblock.s = 0;
2515 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2517 obj_be_ifblock_if(obj);
2518 END OBJ(if_running, 0)
2522 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2523 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2525 ERR("if_running needs an argument");
2526 obj->data.ifblock.s = 0;
2528 obj_be_ifblock_if(obj);
2533 char box[256], dst[256];
2537 /* Kapil: Changed from MAIL_FILE to
2538 current_mail_spool since the latter
2539 is a copy of the former if undefined
2540 but the latter should take precedence
2542 strncpy(box, current_mail_spool, sizeof(box));
2544 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2546 strncpy(box, arg, sizeof(box));
2550 variable_substitute(box, dst, sizeof(dst));
2551 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2552 obj->data.local_mail.interval = n1;
2553 END OBJ(mboxscan, 0)
2554 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2555 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2556 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2557 obj->data.mboxscan.output[0] = 1;
2558 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2559 END OBJ(mem, INFO_MEM)
2560 END OBJ(memeasyfree, INFO_MEM)
2561 END OBJ(memfree, INFO_MEM)
2562 END OBJ(memmax, INFO_MEM)
2563 END OBJ(memperc, INFO_MEM)
2564 END OBJ(membar, INFO_MEM)
2565 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2566 END OBJ(memgraph, INFO_MEM)
2567 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2568 &obj->e, &obj->showaslog);
2573 END OBJ(mixer, INFO_MIXER)
2574 obj->data.l = mixer_init(arg);
2575 END OBJ(mixerl, INFO_MIXER)
2576 obj->data.l = mixer_init(arg);
2577 END OBJ(mixerr, INFO_MIXER)
2578 obj->data.l = mixer_init(arg);
2579 END OBJ(mixerbar, INFO_MIXER)
2580 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2581 &obj->data.mixerbar.h);
2582 END OBJ(mixerlbar, INFO_MIXER)
2583 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2584 &obj->data.mixerbar.h);
2585 END OBJ(mixerrbar, INFO_MIXER)
2586 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2587 &obj->data.mixerbar.h);
2589 END OBJ(monitor, INFO_X11)
2590 END OBJ(monitor_number, INFO_X11)
2592 END OBJ(new_mails, 0)
2594 char box[256], dst[256];
2598 /* Kapil: Changed from MAIL_FILE to
2599 current_mail_spool since the latter
2600 is a copy of the former if undefined
2601 but the latter should take precedence
2603 strncpy(box, current_mail_spool, sizeof(box));
2605 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2607 strncpy(box, arg, sizeof(box));
2611 variable_substitute(box, dst, sizeof(dst));
2612 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2613 obj->data.local_mail.interval = n1;
2614 END OBJ(nodename, 0)
2615 END OBJ(processes, INFO_PROCS)
2616 END OBJ(running_processes, INFO_RUN_PROCS)
2617 END OBJ(shadecolor, 0)
2619 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2621 END OBJ(outlinecolor, 0)
2623 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2625 END OBJ(stippled_hr, 0)
2627 int a = stippled_borders, b = 1;
2630 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2631 sscanf(arg, "%d", &b);
2637 obj->data.pair.a = a;
2638 obj->data.pair.b = b;
2640 END OBJ(swap, INFO_MEM)
2641 END OBJ(swapmax, INFO_MEM)
2642 END OBJ(swapperc, INFO_MEM)
2643 END OBJ(swapbar, INFO_MEM)
2644 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2647 END OBJ(temp1, INFO_SYSFS)
2648 obj->type = OBJ_i2c;
2649 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
2650 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2651 END OBJ(temp2, INFO_SYSFS)
2652 obj->type = OBJ_i2c;
2653 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
2654 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2657 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2659 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2661 char buf1[256], buf2[256], *fmt, *tz;
2665 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2675 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2676 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2678 END OBJ(iconv_start, 0)
2679 if (iconv_converting) {
2680 CRIT_ERR("You must stop your last iconv conversion before "
2681 "starting another");
2684 char iconv_from[CODEPAGE_LENGTH];
2685 char iconv_to[CODEPAGE_LENGTH];
2687 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2688 CRIT_ERR("Invalid arguments for iconv_start");
2692 new_iconv = iconv_open(iconv_to, iconv_from);
2693 if (new_iconv == (iconv_t) (-1)) {
2694 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2696 obj->a = register_iconv(&new_iconv);
2697 iconv_converting = 1;
2701 CRIT_ERR("Iconv requires arguments");
2703 END OBJ(iconv_stop, 0)
2704 iconv_converting = 0;
2707 END OBJ(totaldown, INFO_NET)
2709 obj->data.net = get_net_stat(arg);
2711 CRIT_ERR("totaldown needs argument");
2713 END OBJ(totalup, INFO_NET)
2714 obj->data.net = get_net_stat(arg);
2716 obj->data.net = get_net_stat(arg);
2718 CRIT_ERR("totalup needs argument");
2722 obj->data.i = arg ? atoi(arg) : 0;
2724 obj->data.i = arg ? atoi(arg) : 0;
2725 END OBJ(upspeed, INFO_NET)
2727 obj->data.net = get_net_stat(arg);
2729 CRIT_ERR("upspeed needs argument");
2731 END OBJ(upspeedf, INFO_NET)
2733 obj->data.net = get_net_stat(arg);
2735 CRIT_ERR("upspeedf needs argument");
2738 END OBJ(upspeedgraph, INFO_NET)
2739 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2740 &obj->e, &obj->showaslog);
2742 // default to DEFAULTNETDEV
2743 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
2744 obj->data.net = get_net_stat(buf);
2746 END OBJ(uptime_short, INFO_UPTIME)
2747 END OBJ(uptime, INFO_UPTIME)
2748 END OBJ(user_names, INFO_USERS)
2749 END OBJ(user_times, INFO_USERS)
2750 END OBJ(user_terms, INFO_USERS)
2751 END OBJ(user_number, INFO_USERS)
2752 #if defined(__linux__)
2753 END OBJ(gw_iface, INFO_GW)
2754 END OBJ(gw_ip, INFO_GW)
2755 END OBJ(if_gw, INFO_GW)
2756 #endif /* !__linux__ */
2758 END OBJ(adt746xcpu, 0)
2759 END OBJ(adt746xfan, 0)
2760 #endif /* !__OpenBSD__ */
2761 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2762 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2763 END OBJ(apm_adapter, 0)
2764 END OBJ(apm_battery_life, 0)
2765 END OBJ(apm_battery_time, 0)
2766 #endif /* __FreeBSD__ */
2767 END OBJ_THREAD(imap_unseen, 0)
2770 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2771 obj->global_mode = 0;
2773 obj->global_mode = 1;
2775 END OBJ_THREAD(imap_messages, 0)
2778 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2779 obj->global_mode = 0;
2781 obj->global_mode = 1;
2783 END OBJ_THREAD(pop3_unseen, 0)
2786 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2787 obj->global_mode = 0;
2789 obj->global_mode = 1;
2791 END OBJ_THREAD(pop3_used, 0)
2794 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2795 obj->global_mode = 0;
2797 obj->global_mode = 1;
2802 obj->data.s = strndup(arg, text_buffer_size);
2804 ERR("smapi needs an argument");
2805 END OBJ(if_smapi_bat_installed, 0)
2807 ERR("if_smapi_bat_installed needs an argument");
2808 obj->data.ifblock.s = 0;
2810 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2811 obj_be_ifblock_if(obj);
2812 END OBJ(smapi_bat_perc, 0)
2814 obj->data.s = strndup(arg, text_buffer_size);
2816 ERR("smapi_bat_perc needs an argument");
2817 END OBJ(smapi_bat_temp, 0)
2819 obj->data.s = strndup(arg, text_buffer_size);
2821 ERR("smapi_bat_temp needs an argument");
2822 END OBJ(smapi_bat_power, 0)
2824 obj->data.s = strndup(arg, text_buffer_size);
2826 ERR("smapi_bat_power needs an argument");
2827 END OBJ(smapi_bat_bar, 0)
2830 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2831 ERR("first argument to smapi_bat_bar must be an integer value");
2835 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2838 ERR("smapi_bat_bar needs an argument");
2841 END OBJ_THREAD(mpd_artist, INFO_MPD)
2843 sscanf(arg, "%d", &obj->data.i);
2844 if (obj->data.i > 0) {
2847 ERR("mpd_artist: invalid length argument");
2853 END OBJ_THREAD(mpd_title, INFO_MPD)
2855 sscanf(arg, "%d", &obj->data.i);
2856 if (obj->data.i > 0) {
2859 ERR("mpd_title: invalid length argument");
2865 END OBJ_THREAD(mpd_random, INFO_MPD)
2866 END OBJ_THREAD(mpd_repeat, INFO_MPD)
2867 END OBJ_THREAD(mpd_elapsed, INFO_MPD)
2868 END OBJ_THREAD(mpd_length, INFO_MPD)
2869 END OBJ_THREAD(mpd_track, INFO_MPD)
2871 sscanf(arg, "%d", &obj->data.i);
2872 if (obj->data.i > 0) {
2875 ERR("mpd_track: invalid length argument");
2881 END OBJ_THREAD(mpd_name, INFO_MPD)
2883 sscanf(arg, "%d", &obj->data.i);
2884 if (obj->data.i > 0) {
2887 ERR("mpd_name: invalid length argument");
2893 END OBJ_THREAD(mpd_file, INFO_MPD)
2895 sscanf(arg, "%d", &obj->data.i);
2896 if (obj->data.i > 0) {
2899 ERR("mpd_file: invalid length argument");
2905 END OBJ_THREAD(mpd_percent, INFO_MPD)
2906 END OBJ_THREAD(mpd_album, INFO_MPD)
2908 sscanf(arg, "%d", &obj->data.i);
2909 if (obj->data.i > 0) {
2912 ERR("mpd_album: invalid length argument");
2918 END OBJ_THREAD(mpd_vol, INFO_MPD)
2919 END OBJ_THREAD(mpd_bitrate, INFO_MPD)
2920 END OBJ_THREAD(mpd_status, INFO_MPD)
2921 END OBJ_THREAD(mpd_bar, INFO_MPD)
2922 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2923 END OBJ_THREAD(mpd_smart, INFO_MPD)
2925 sscanf(arg, "%d", &obj->data.i);
2926 if (obj->data.i > 0) {
2929 ERR("mpd_smart: invalid length argument");
2935 END OBJ_THREAD(if_mpd_playing, INFO_MPD)
2936 obj_be_ifblock_if(obj);
2939 END OBJ_THREAD(moc_state, INFO_MOC)
2940 END OBJ_THREAD(moc_file, INFO_MOC)
2941 END OBJ_THREAD(moc_title, INFO_MOC)
2942 END OBJ_THREAD(moc_artist, INFO_MOC)
2943 END OBJ_THREAD(moc_song, INFO_MOC)
2944 END OBJ_THREAD(moc_album, INFO_MOC)
2945 END OBJ_THREAD(moc_totaltime, INFO_MOC)
2946 END OBJ_THREAD(moc_timeleft, INFO_MOC)
2947 END OBJ_THREAD(moc_curtime, INFO_MOC)
2948 END OBJ_THREAD(moc_bitrate, INFO_MOC)
2949 END OBJ_THREAD(moc_rate, INFO_MOC)
2952 END OBJ(xmms2_artist, INFO_XMMS2)
2953 END OBJ(xmms2_album, INFO_XMMS2)
2954 END OBJ(xmms2_title, INFO_XMMS2)
2955 END OBJ(xmms2_genre, INFO_XMMS2)
2956 END OBJ(xmms2_comment, INFO_XMMS2)
2957 END OBJ(xmms2_url, INFO_XMMS2)
2958 END OBJ(xmms2_tracknr, INFO_XMMS2)
2959 END OBJ(xmms2_bitrate, INFO_XMMS2)
2960 END OBJ(xmms2_date, INFO_XMMS2)
2961 END OBJ(xmms2_id, INFO_XMMS2)
2962 END OBJ(xmms2_duration, INFO_XMMS2)
2963 END OBJ(xmms2_elapsed, INFO_XMMS2)
2964 END OBJ(xmms2_size, INFO_XMMS2)
2965 END OBJ(xmms2_status, INFO_XMMS2)
2966 END OBJ(xmms2_percent, INFO_XMMS2)
2967 END OBJ(xmms2_bar, INFO_XMMS2)
2968 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2969 END OBJ(xmms2_smart, INFO_XMMS2)
2970 END OBJ(xmms2_playlist, INFO_XMMS2)
2971 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2972 END OBJ(if_xmms2_connected, INFO_XMMS2)
2973 obj_be_ifblock_if(obj);
2976 END OBJ(audacious_status, INFO_AUDACIOUS)
2977 END OBJ(audacious_title, INFO_AUDACIOUS)
2979 sscanf(arg, "%d", &info.audacious.max_title_len);
2980 if (info.audacious.max_title_len > 0) {
2981 info.audacious.max_title_len++;
2983 CRIT_ERR("audacious_title: invalid length argument");
2986 END OBJ(audacious_length, INFO_AUDACIOUS)
2987 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2988 END OBJ(audacious_position, INFO_AUDACIOUS)
2989 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2990 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2991 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2992 END OBJ(audacious_channels, INFO_AUDACIOUS)
2993 END OBJ(audacious_filename, INFO_AUDACIOUS)
2994 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2995 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2996 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2997 END OBJ(audacious_bar, INFO_AUDACIOUS)
2998 scan_bar(arg, &obj->a, &obj->b);
3001 END OBJ(bmpx_title, INFO_BMPX)
3002 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3003 END OBJ(bmpx_artist, INFO_BMPX)
3004 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3005 END OBJ(bmpx_album, INFO_BMPX)
3006 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3007 END OBJ(bmpx_track, INFO_BMPX)
3008 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3009 END OBJ(bmpx_uri, INFO_BMPX)
3010 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3011 END OBJ(bmpx_bitrate, INFO_BMPX)
3012 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3018 char *userid = (char *) malloc(20 * sizeof(char));
3019 char *apikey = (char *) malloc(64 * sizeof(char));
3020 char *charid = (char *) malloc(20 * sizeof(char));
3022 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
3023 obj->data.eve.charid = charid;
3024 obj->data.eve.userid = userid;
3025 obj->data.eve.apikey = apikey;
3029 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
3035 int argc, delay, act_par;
3036 char *uri = (char *) malloc(128 * sizeof(char));
3037 char *action = (char *) malloc(64 * sizeof(char));
3039 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
3041 obj->data.rss.uri = uri;
3042 obj->data.rss.delay = delay;
3043 obj->data.rss.action = action;
3044 obj->data.rss.act_par = act_par;
3048 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
3054 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
3055 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3056 ERR("hddtemp needs arguments");
3057 obj->type = OBJ_text;
3058 obj->data.s = strndup("${hddtemp}", text_buffer_size);
3059 obj->data.hddtemp.update_time = 0;
3061 obj->data.hddtemp.temp = NULL;
3063 #ifdef TCP_PORT_MONITOR
3064 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3065 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
3067 END OBJ(entropy_avail, INFO_ENTROPY)
3068 END OBJ(entropy_poolsize, INFO_ENTROPY)
3069 END OBJ(entropy_bar, INFO_ENTROPY)
3070 scan_bar(arg, &obj->a, &obj->b);
3074 obj->data.scroll.step = 1;
3075 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
3076 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
3078 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
3079 obj->data.scroll.start = 0;
3081 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
3086 CRIT_ERR("nvidia needs an argument\n");
3087 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3088 CRIT_ERR("nvidia: invalid argument"
3089 " specified: '%s'\n", arg);
3095 ERR("unknown variable %s", s);
3096 obj->type = OBJ_text;
3097 snprintf(buf, 256, "${%s}", s);
3098 obj->data.s = strndup(buf, text_buffer_size);
3105 static struct text_object *create_plain_text(const char *s)
3107 struct text_object *obj;
3109 if (s == NULL || *s == '\0') {
3113 obj = new_text_object_internal();
3115 obj->type = OBJ_text;
3116 obj->data.s = strndup(s, text_buffer_size);
3120 /* backslash_escape - do the actual substitution task for template objects
3122 * The field templates is used for substituting the \N occurences. Set it to
3123 * NULL to leave them as they are.
3125 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3129 unsigned int dup_idx = 0, dup_len;
3131 dup_len = strlen(src);
3132 src_dup = malloc(dup_len * sizeof(char));
3140 if (*(p + 1) == '\\') {
3141 src_dup[dup_idx++] = '\\';
3143 } else if (*(p + 1) == ' ') {
3144 src_dup[dup_idx++] = ' ';
3146 } else if (*(p + 1) == 'n') {
3147 src_dup[dup_idx++] = '\n';
3149 } else if (templates) {
3150 unsigned int tmpl_num;
3152 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3153 (tmpl_num > template_count))
3155 dup_len += strlen(templates[tmpl_num - 1]);
3156 src_dup = realloc(src_dup, dup_len * sizeof(char));
3157 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3158 dup_idx += strlen(templates[tmpl_num - 1]);
3163 src_dup[dup_idx++] = *p;
3168 src_dup[dup_idx] = '\0';
3169 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3173 /* handle_template_object - core logic of the template object
3175 * use config variables like this:
3176 * template1 = "$\1\2"
3177 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3179 * and use them like this:
3180 * ${template1 node name}
3181 * ${template2 root /}
3182 * ${template2 cdrom /mnt/cdrom}
3184 static char *handle_template(const char *tmpl, const char *args)
3186 char *args_dup, *p, *p_old;
3187 char **argsp = NULL;
3188 unsigned int argcnt = 0, template_idx, i;
3191 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3195 args_dup = strdup(args);
3198 while (*p && (*p == ' ' && *(p - 1) != '\\'))
3200 if (*(p - 1) == '\\')
3203 while (*p && (*p != ' ' || *(p - 1) == '\\'))
3209 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3210 argsp[argcnt - 1] = p_old;
3212 for (i = 0; i < argcnt; i++) {
3214 tmp = backslash_escape(argsp[i], NULL, 0);
3215 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3219 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3220 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3222 for (i = 0; i < argcnt; i++)
3228 static char *find_and_replace_templates(const char *inbuf)
3230 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3233 outlen = strlen(inbuf) + 1;
3234 o = outbuf = calloc(outlen, sizeof(char));
3235 memset(outbuf, 0, outlen * sizeof(char));
3237 p = indup = strdup(inbuf);
3239 while (*p && *p != '$')
3245 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3250 if (*(p + 1) == '{') {
3272 tmpl_out = handle_template(templ, args);
3274 outlen += strlen(tmpl_out);
3275 outbuf = realloc(outbuf, outlen * sizeof(char));
3276 strcat (outbuf, tmpl_out);
3278 o = outbuf + strlen(outbuf);
3280 ERR("failed to handle template '%s' with args '%s'", templ, args);
3284 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3289 static int text_contains_templates(const char *text)
3291 if (strcasestr(text, "${template") != NULL)
3293 if (strcasestr(text, "$template") != NULL)
3298 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3300 struct text_object *obj;
3301 char *p, *s, *orig_p;
3304 p = strndup(const_p, max_user_text - 1);
3305 while (text_contains_templates(p)) {
3307 tmp = find_and_replace_templates(p);
3313 if (strcmp(p, const_p)) {
3314 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3316 DBGP("no templates to replace");
3319 memset(retval, 0, sizeof(struct text_object));
3321 line = global_text_lines;
3329 obj = create_plain_text(s);
3331 append_object(retval, obj);
3342 /* variable is either $foo or ${foo} */
3344 unsigned int brl = 1, brr = 0;
3348 while (*p && brl != brr) {
3363 while (*p && (isalnum((int) *p) || *p == '_')) {
3368 /* copy variable to buffer */
3369 len = (p - s > 255) ? 255 : (p - s);
3370 strncpy(buf, s, len);
3380 /* if variable wasn't found in environment, use some special */
3386 if (strchr(buf, ' ')) {
3387 arg = strchr(buf, ' ');
3390 while (isspace((int) *arg)) {
3398 /* lowercase variable name */
3401 *tmp_p = tolower(*tmp_p);
3405 obj = construct_text_object(buf, arg, line, allow_threaded);
3407 append_object(retval, obj);
3412 obj = create_plain_text("$");
3414 append_object(retval, obj);
3420 obj = create_plain_text(s);
3422 append_object(retval, obj);
3425 if (!ifblock_stack_empty()) {
3426 ERR("one or more $endif's are missing");
3433 static void extract_variable_text(const char *p)
3435 free_text_objects(&global_root_object, 1);
3449 extract_variable_text_internal(&global_root_object, p, 1);
3452 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3454 extract_variable_text_internal(root, txt, 0);
3455 generate_text_internal(p, max_user_text, *root, cur);
3459 /* Allows reading from a FIFO (i.e., /dev/xconsole).
3460 * The file descriptor is set to non-blocking which makes this possible.
3462 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
3463 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
3465 #define TAIL_PIPE_BUFSIZE 4096
3469 int fd = obj->data.tail.fd;
3472 char buf[TAIL_PIPE_BUFSIZE];
3473 ssize_t len = read(fd, buf, sizeof(buf));
3477 if (errno != EAGAIN) {
3478 strcpy(obj->data.tail.buffer, "Logfile Read Error");
3479 snprintf(dst, dst_size, "Logfile Read Error");
3483 } else if (len == 0) {
3484 strcpy(obj->data.tail.buffer, "Logfile Empty");
3485 snprintf(dst, dst_size, "Logfile Empty");
3489 for (line_len = 0, i = 0; i < len; i++) {
3493 if (buf[i] == '\n') {
3496 if (obj->data.tail.readlines > 0) {
3501 for (n = 0; obj->data.tail.buffer[n]; n++) {
3502 if (obj->data.tail.buffer[n] == '\n') {
3507 if (++olines < obj->data.tail.wantedlines) {
3513 p = obj->data.tail.buffer + first_line;
3514 pos = n - first_line;
3515 memmove(obj->data.tail.buffer,
3516 obj->data.tail.buffer + first_line, strlen(p));
3517 obj->data.tail.buffer[pos] = 0;
3523 p = buf + last_line;
3525 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
3526 obj->data.tail.buffer[pos + line_len] = 0;
3529 obj->data.tail.readlines = lines;
3537 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
3540 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3541 void *thread(void *), const char *text)
3543 if (obj->global_mode && info.mail) {
3544 // this means we use info
3545 if (!info.mail->p_timed_thread) {
3546 info.mail->p_timed_thread =
3547 timed_thread_create(thread,
3548 (void *) info.mail, info.mail->interval * 1000000);
3549 if (!info.mail->p_timed_thread) {
3550 ERR("Error creating %s timed thread", text);
3552 timed_thread_register(info.mail->p_timed_thread,
3553 &info.mail->p_timed_thread);
3554 if (timed_thread_run(info.mail->p_timed_thread)) {
3555 ERR("Error running %s timed thread", text);
3559 } else if (obj->data.mail) {
3560 // this means we use obj
3561 if (!obj->data.mail->p_timed_thread) {
3562 obj->data.mail->p_timed_thread =
3563 timed_thread_create(thread,
3564 (void *) obj->data.mail,
3565 obj->data.mail->interval * 1000000);
3566 if (!obj->data.mail->p_timed_thread) {
3567 ERR("Error creating %s timed thread", text);
3569 timed_thread_register(obj->data.mail->p_timed_thread,
3570 &obj->data.mail->p_timed_thread);
3571 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3572 ERR("Error running %s timed thread", text);
3575 return obj->data.mail;
3576 } else if (!obj->a) {
3577 // something is wrong, warn once then stop
3578 ERR("Theres a problem with your %s_unseen settings. "
3579 "Check that the global %s settings are defined "
3580 "properly (line %li).", global_text, global_text, obj->line);
3586 char *format_time(unsigned long timeval, const int width)
3589 unsigned long nt; // narrow time, for speed on 32-bit
3590 unsigned cc; // centiseconds
3591 unsigned nn; // multi-purpose whatever
3594 cc = nt % 100; // centiseconds past second
3595 nt /= 100; // total seconds
3596 nn = nt % 60; // seconds past the minute
3597 nt /= 60; // total minutes
3598 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3600 return strndup(buf, text_buffer_size);
3602 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3603 return strndup(buf, text_buffer_size);
3605 nn = nt % 60; // minutes past the hour
3606 nt /= 60; // total hours
3607 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3608 return strndup(buf, text_buffer_size);
3610 nn = nt; // now also hours
3611 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3612 return strndup(buf, text_buffer_size);
3614 nn /= 24; // now days
3615 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3616 return strndup(buf, text_buffer_size);
3618 nn /= 7; // now weeks
3619 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3620 return strndup(buf, text_buffer_size);
3622 // well shoot, this outta' fit...
3623 return strndup("<inf>", text_buffer_size);
3626 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3627 //string has to end with \0 and it's length should fit in a int
3629 void remove_deleted_chars(char *string){
3631 while(string[i] != 0){
3632 if(string[i] == BACKSPACE){
3634 strcpy( &(string[i-1]), &(string[i+1]) );
3636 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3641 static inline void format_media_player_time(char *buf, const int size,
3644 int days, hours, minutes;
3646 days = seconds / (24 * 60 * 60);
3647 seconds %= (24 * 60 * 60);
3648 hours = seconds / (60 * 60);
3649 seconds %= (60 * 60);
3650 minutes = seconds / 60;
3654 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3655 hours, minutes, seconds);
3656 } else if (hours > 0) {
3657 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3660 snprintf(buf, size, "%i:%02i", minutes, seconds);
3664 static inline double get_barnum(char *buf)
3676 if (sscanf(buf, "%lf", &barnum) == 0) {
3677 ERR("reading execbar value failed (perhaps it's not the "
3678 "correct format?)");
3681 if (barnum > 100.0 || barnum < 0.0) {
3682 ERR("your execbar value is not between 0 and 100, "
3683 "therefore it will be ignored");
3689 static void generate_text_internal(char *p, int p_max_size,
3690 struct text_object root, struct information *cur)
3692 struct text_object *obj;
3695 char buff_in[p_max_size];
3697 iconv_converting = 0;
3701 for (obj = root.next; obj && p_max_size > 0; obj = obj->next) {
3703 /* IFBLOCK jumping algorithm
3705 * This is easier as it looks like:
3706 * - each IF checks it's condition
3707 * - on FALSE: call DO_JUMP
3708 * - on TRUE: don't care
3709 * - each ELSE calls DO_JUMP unconditionally
3710 * - each ENDIF is silently being ignored
3713 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3714 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3715 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3716 * regularly, "obj" is being updated to point to obj->next, so object parsing
3717 * continues right after the corresponding ELSE or ENDIF. This means that if we
3718 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3719 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3720 * jumped, and there is nothing to do.
3724 obj = obj->data.ifblock.next; \
3727 #define OBJ(a) break; case OBJ_##a:
3729 switch (obj->type) {
3731 ERR("not implemented obj type %d", obj->type);
3734 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3736 #endif /* !__OpenBSD__ */
3739 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3740 obj->data.cpu_index);
3746 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3747 obj->data.cpu_index);
3749 /* OpenBSD has no such flag (SUSv2) */
3750 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3751 obj->data.cpu_index);
3755 #if defined(__linux__)
3758 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3759 obj->data.cpu_index);
3764 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3765 obj->data.cpu_index);
3770 OBJ(wireless_essid) {
3771 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3773 OBJ(wireless_mode) {
3774 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3776 OBJ(wireless_bitrate) {
3777 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3780 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3782 OBJ(wireless_link_qual) {
3783 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
3784 obj->data.net->link_qual);
3786 OBJ(wireless_link_qual_max) {
3787 spaced_print(p, p_max_size, "%d", 4,
3788 "wireless_link_qual_max", obj->data.net->link_qual_max);
3790 OBJ(wireless_link_qual_perc) {
3791 if (obj->data.net->link_qual_max > 0) {
3792 spaced_print(p, p_max_size, "%.0f", 5,
3793 "wireless_link_qual_perc",
3794 (double) obj->data.net->link_qual /
3795 obj->data.net->link_qual_max * 100);
3797 spaced_print(p, p_max_size, "unk", 5,
3798 "wireless_link_qual_perc");
3801 OBJ(wireless_link_bar) {
3802 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3803 obj->data.net->link_qual_max) * 255.0);
3805 #endif /* HAVE_IWLIB */
3807 #endif /* __linux__ */
3811 get_adt746x_cpu(p, p_max_size);
3814 get_adt746x_fan(p, p_max_size);
3817 get_acpi_fan(p, p_max_size);
3819 OBJ(acpiacadapter) {
3820 get_acpi_ac_adapter(p, p_max_size);
3823 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3826 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3828 OBJ(battery_percent) {
3829 spaced_print(p, p_max_size, "%*d", 4, "battery_percent",
3830 pad_percents, get_battery_perct(obj->data.s));
3833 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3835 #endif /* __OpenBSD__ */
3838 human_readable(cur->buffers * 1024, p, 255, "buffers");
3841 human_readable(cur->cached * 1024, p, 255, "buffers");
3844 if (obj->data.cpu_index > info.cpu_count) {
3845 printf("obj->data.cpu_index %i info.cpu_count %i",
3846 obj->data.cpu_index, info.cpu_count);
3847 CRIT_ERR("attempting to use more CPUs than you have!");
3849 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
3850 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3853 new_bar(p, obj->a, obj->b,
3854 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3857 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
3858 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3859 100, 1, obj->showaslog);
3862 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3863 obj->e, 1, obj->showaslog);
3866 new_fg(p, obj->data.l);
3898 OBJ(conky_version) {
3899 snprintf(p, p_max_size, "%s", VERSION);
3901 OBJ(conky_build_date) {
3902 snprintf(p, p_max_size, "%s", BUILD_DATE);
3904 OBJ(conky_build_arch) {
3905 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3907 #if defined(__linux__)
3909 snprintf(p, p_max_size, "%s",
3910 get_disk_protect_queue(obj->data.s));
3913 snprintf(p, p_max_size, "%s", i8k.version);
3916 snprintf(p, p_max_size, "%s", i8k.bios);
3919 snprintf(p, p_max_size, "%s", i8k.serial);
3924 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3925 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3927 OBJ(i8k_left_fan_status) {
3928 int left_fan_status;
3930 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3931 if (left_fan_status == 0) {
3932 snprintf(p, p_max_size, "off");
3934 if (left_fan_status == 1) {
3935 snprintf(p, p_max_size, "low");
3937 if (left_fan_status == 2) {
3938 snprintf(p, p_max_size, "high");
3941 OBJ(i8k_right_fan_status) {
3942 int right_fan_status;
3944 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3945 if (right_fan_status == 0) {
3946 snprintf(p, p_max_size, "off");
3948 if (right_fan_status == 1) {
3949 snprintf(p, p_max_size, "low");
3951 if (right_fan_status == 2) {
3952 snprintf(p, p_max_size, "high");
3955 OBJ(i8k_left_fan_rpm) {
3956 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3958 OBJ(i8k_right_fan_rpm) {
3959 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3961 OBJ(i8k_ac_status) {
3964 sscanf(i8k.ac_status, "%d", &ac_status);
3965 if (ac_status == -1) {
3966 snprintf(p, p_max_size, "disabled (read i8k docs)");
3968 if (ac_status == 0) {
3969 snprintf(p, p_max_size, "off");
3971 if (ac_status == 1) {
3972 snprintf(p, p_max_size, "on");
3975 OBJ(i8k_buttons_status) {
3976 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3979 get_ibm_acpi_fan(p, p_max_size);
3982 get_ibm_acpi_temps();
3983 temp_print(p, p_max_size,
3984 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
3987 get_ibm_acpi_volume(p, p_max_size);
3989 OBJ(ibm_brightness) {
3990 get_ibm_acpi_brightness(p, p_max_size);
3993 if ((obj->data.ifblock.s)
3994 && (!interface_up(obj->data.ifblock.s))) {
3999 if (!cur->gw_info.count) {
4004 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4007 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4010 snprintf(p, p_max_size, "%d", get_laptop_mode());
4013 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4015 #endif /* __linux__ */
4018 OBJ(obsd_sensors_temp) {
4019 obsd_sensors.device = sensor_device;
4020 update_obsd_sensors();
4021 temp_print(p, p_max_size,
4022 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4025 OBJ(obsd_sensors_fan) {
4026 obsd_sensors.device = sensor_device;
4027 update_obsd_sensors();
4028 snprintf(p, p_max_size, "%d",
4029 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4031 OBJ(obsd_sensors_volt) {
4032 obsd_sensors.device = sensor_device;
4033 update_obsd_sensors();
4034 snprintf(p, p_max_size, "%.2f",
4035 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4038 get_obsd_vendor(p, p_max_size);
4041 get_obsd_product(p, p_max_size);
4043 #endif /* __OpenBSD__ */
4047 new_font(p, obj->data.s);
4050 /* TODO: move this correction from kB to kB/s elsewhere
4051 * (or get rid of it??) */
4053 if (obj->data.diskio) {
4055 (obj->data.diskio->current / update_interval) * 1024LL,
4056 p, p_max_size, "diskio");
4058 human_readable(info.diskio_value * 1024LL, p, p_max_size,
4063 if (obj->data.diskio) {
4064 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
4067 human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
4072 if (obj->data.diskio) {
4073 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
4076 human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
4081 if (obj->data.diskio) {
4082 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4083 obj->data.diskio->current, obj->e, 1, obj->showaslog);
4085 new_graph(p, obj->a, obj->b, obj->c, obj->d, info.diskio_value,
4086 obj->e, 1, obj->showaslog);
4089 OBJ(diskiograph_read) {
4090 if (obj->data.diskio) {
4091 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4092 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
4094 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4095 info.diskio_read_value, obj->e, 1, obj->showaslog);
4098 OBJ(diskiograph_write) {
4099 if (obj->data.diskio) {
4100 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4101 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
4103 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4104 info.diskio_write_value, obj->e, 1, obj->showaslog);
4108 spaced_print(p, p_max_size, "%d", 6, "downspeed",
4109 round_to_int(obj->data.net->recv_speed / 1024));
4112 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
4113 obj->data.net->recv_speed / 1024.0);
4115 OBJ(downspeedgraph) {
4116 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4117 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
4120 /* Since we see you, you're if has not jumped.
4121 * Do Ninja jump here: without leaving traces.
4122 * This is to prevent us from stale jumped flags.
4124 obj = obj->data.ifblock.next;
4128 /* harmless object, just ignore */
4132 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4133 && (obj->data.net->addr.sa_data[3] & 255) == 0
4134 && (obj->data.net->addr.sa_data[4] & 255) == 0
4135 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4136 snprintf(p, p_max_size, "No Address");
4138 snprintf(p, p_max_size, "%u.%u.%u.%u",
4139 obj->data.net->addr.sa_data[2] & 255,
4140 obj->data.net->addr.sa_data[3] & 255,
4141 obj->data.net->addr.sa_data[4] & 255,
4142 obj->data.net->addr.sa_data[5] & 255);
4145 #if defined(__linux__)
4147 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4149 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4150 strcpy(p, obj->data.net->addrs);
4153 strcpy(p, "0.0.0.0");
4155 #endif /* __linux__ */
4157 #if defined(IMLIB2) && defined(X11)
4162 Imlib_Image image, buffer;
4164 image = imlib_load_image(obj->data.s);
4165 imlib_context_set_image(image);
4169 w = imlib_image_get_width();
4170 h = imlib_image_get_height();
4171 buffer = imlib_create_image(w, h);
4172 imlib_context_set_display(display);
4173 imlib_context_set_drawable(window.drawable);
4174 imlib_context_set_colormap(DefaultColormap(display,
4176 imlib_context_set_visual(DefaultVisual(display,
4178 imlib_context_set_image(buffer);
4179 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
4180 text_start_x, text_start_y, w, h);
4181 imlib_render_image_on_drawable(text_start_x,
4184 imlib_context_set_image(image);
4192 read_exec(obj->data.s, p, text_buffer_size);
4193 remove_deleted_chars(p);
4196 struct information *tmp_info;
4197 struct text_object subroot;
4199 read_exec(obj->data.s, p, text_buffer_size);
4201 tmp_info = malloc(sizeof(struct information));
4202 memcpy(tmp_info, cur, sizeof(struct information));
4203 parse_conky_vars(&subroot, p, p, tmp_info);
4205 free_text_objects(&subroot, 0);
4211 read_exec(obj->data.s, p, text_buffer_size);
4212 barnum = get_barnum(p);
4214 if (barnum >= 0.0) {
4216 new_bar(p, 0, 4, round_to_int(barnum * 255.0));
4220 char showaslog = FALSE;
4223 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
4225 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, text_buffer_size);
4226 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
4227 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, text_buffer_size);
4229 read_exec(obj->data.s, p, text_buffer_size);
4231 barnum = get_barnum(p);
4233 if (barnum >= 0.0) {
4234 new_graph(p, 0, 25, obj->c, obj->d, round_to_int(barnum),
4239 if (current_update_time - obj->data.execi.last_update
4240 >= obj->data.execi.interval) {
4243 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4244 barnum = get_barnum(p);
4246 if (barnum >= 0.0) {
4247 obj->f = 255 * barnum / 100.0;
4249 obj->data.execi.last_update = current_update_time;
4251 new_bar(p, 0, 4, round_to_int(obj->f));
4254 if (current_update_time - obj->data.execi.last_update
4255 >= obj->data.execi.interval) {
4258 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4259 barnum = get_barnum(p);
4261 if (barnum >= 0.0) {
4264 obj->data.execi.last_update = current_update_time;
4266 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
4269 if (current_update_time - obj->data.execi.last_update
4270 >= obj->data.execi.interval
4271 && obj->data.execi.interval != 0) {
4272 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4274 obj->data.execi.last_update = current_update_time;
4276 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4279 struct text_object subroot;
4280 struct information *tmp_info =
4281 malloc(sizeof(struct information));
4282 memcpy(tmp_info, cur, sizeof(struct information));
4284 if (current_update_time - obj->data.execi.last_update
4285 < obj->data.execi.interval
4286 || obj->data.execi.interval == 0) {
4287 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4289 char *output = obj->data.execi.buffer;
4290 FILE *fp = popen(obj->data.execi.cmd, "r");
4291 int length = fread(output, 1, text_buffer_size, fp);
4295 output[length] = '\0';
4296 if (length > 0 && output[length - 1] == '\n') {
4297 output[length - 1] = '\0';
4300 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4301 obj->data.execi.last_update = current_update_time;
4303 free_text_objects(&subroot, 0);
4307 if (!obj->data.texeci.p_timed_thread) {
4308 obj->data.texeci.p_timed_thread =
4309 timed_thread_create(&threaded_exec,
4310 (void *) obj, obj->data.texeci.interval * 1000000);
4311 if (!obj->data.texeci.p_timed_thread) {
4312 ERR("Error creating texeci timed thread");
4314 timed_thread_register(obj->data.texeci.p_timed_thread,
4315 &obj->data.texeci.p_timed_thread);
4316 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4317 ERR("Error running texeci timed thread");
4320 timed_thread_lock(obj->data.texeci.p_timed_thread);
4321 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4322 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4324 #endif /* HAVE_POPEN */
4326 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4328 if (mail && mail->p_timed_thread) {
4329 timed_thread_lock(mail->p_timed_thread);
4330 snprintf(p, p_max_size, "%lu", mail->unseen);
4331 timed_thread_unlock(mail->p_timed_thread);
4334 OBJ(imap_messages) {
4335 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4337 if (mail && mail->p_timed_thread) {
4338 timed_thread_lock(mail->p_timed_thread);
4339 snprintf(p, p_max_size, "%lu", mail->messages);
4340 timed_thread_unlock(mail->p_timed_thread);
4344 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4346 if (mail && mail->p_timed_thread) {
4347 timed_thread_lock(mail->p_timed_thread);
4348 snprintf(p, p_max_size, "%lu", mail->unseen);
4349 timed_thread_unlock(mail->p_timed_thread);
4353 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4355 if (mail && mail->p_timed_thread) {
4356 timed_thread_lock(mail->p_timed_thread);
4357 snprintf(p, p_max_size, "%.1f",
4358 mail->used / 1024.0 / 1024.0);
4359 timed_thread_unlock(mail->p_timed_thread);
4363 if (obj->data.fs != NULL) {
4364 if (obj->data.fs->size == 0) {
4365 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4367 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4368 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4369 obj->data.fs->size));
4374 if (obj->data.fs != NULL) {
4375 human_readable(obj->data.fs->avail, p, 255, "fs_free");
4379 if (obj->data.fs != NULL) {
4380 if (obj->data.fs->size) {
4381 spaced_print(p, p_max_size, "%*d", 4, "fs_free_perc",
4382 pad_percents, (int) ((obj->data.fs->avail * 100) /
4383 obj->data.fs->size));
4385 snprintf(p, p_max_size, "0");
4390 if (obj->data.fs != NULL) {
4391 human_readable(obj->data.fs->size, p, 255, "fs_size");
4395 if (obj->data.fs != NULL)
4396 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4399 if (obj->data.fs != NULL) {
4400 human_readable(obj->data.fs->size - (obj->data.fs->free
4401 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
4406 if (obj->data.fs != NULL) {
4407 if (obj->data.fs->size == 0) {
4408 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4410 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4411 (int) (obj->data.fsbar.fs->avail * 255 /
4412 obj->data.fs->size));
4417 if (obj->data.fs != NULL) {
4418 if (obj->data.fs->size) {
4419 spaced_print(p, 4, "%*d", 4, "fs_used_perc",
4420 pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
4421 obj->data.fs->size)));
4423 snprintf(p, p_max_size, "0");
4428 float *v = info.loadavg;
4430 if (obj->data.loadavg[2]) {
4431 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4432 v[obj->data.loadavg[0] - 1],
4433 v[obj->data.loadavg[1] - 1],
4434 v[obj->data.loadavg[2] - 1]);
4435 } else if (obj->data.loadavg[1]) {
4436 snprintf(p, p_max_size, "%.2f %.2f",
4437 v[obj->data.loadavg[0] - 1],
4438 v[obj->data.loadavg[1] - 1]);
4439 } else if (obj->data.loadavg[0]) {
4440 snprintf(p, p_max_size, "%.2f",
4441 v[obj->data.loadavg[0] - 1]);
4445 new_goto(p, obj->data.i);
4448 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4451 new_hr(p, obj->data.i);
4454 if (cur->nameserver_info.nscount > obj->data.i)
4455 snprintf(p, p_max_size, "%s",
4456 cur->nameserver_info.ns_list[obj->data.i]);
4460 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4461 snprintf(p, p_max_size, "%s", skill);
4466 PRSS *data = get_rss_info(obj->data.rss.uri,
4467 obj->data.rss.delay);
4471 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4473 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4475 // remove trailing new line if one exists
4476 if (str[strlen(str) - 1] == '\n') {
4477 str[strlen(str) - 1] = 0;
4479 snprintf(p, p_max_size, "%s", str);
4480 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4481 if (obj->data.rss.act_par < data->item_count) {
4482 str = data->items[obj->data.rss.act_par].title;
4483 // remove trailing new line if one exists
4484 if (str[strlen(str) - 1] == '\n') {
4485 str[strlen(str) - 1] = 0;
4487 snprintf(p, p_max_size, "%s", str);
4489 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4490 if (obj->data.rss.act_par < data->item_count) {
4492 data->items[obj->data.rss.act_par].description;
4493 // remove trailing new line if one exists
4494 if (str[strlen(str) - 1] == '\n') {
4495 str[strlen(str) - 1] = 0;
4497 snprintf(p, p_max_size, "%s", str);
4499 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4500 if (data->item_count > 0) {
4506 if (obj->data.rss.act_par > data->item_count) {
4507 show = data->item_count;
4509 show = obj->data.rss.act_par;
4511 for (itmp = 0; itmp < show; itmp++) {
4512 PRSS_Item *item = &data->items[itmp];
4516 // don't add new line before first item
4518 strncat(p, "\n", p_max_size);
4520 /* remove trailing new line if one exists,
4521 * we have our own */
4522 if (str[strlen(str) - 1] == '\n') {
4523 str[strlen(str) - 1] = 0;
4525 strncat(p, str, p_max_size);
4537 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4538 if (obj->data.hddtemp.temp)
4539 free(obj->data.hddtemp.temp);
4540 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4541 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4542 obj->data.hddtemp.update_time = current_update_time;
4544 if (!obj->data.hddtemp.temp) {
4545 snprintf(p, p_max_size, "N/A");
4547 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4548 unit = obj->data.hddtemp.temp[0];
4550 if (*endptr != '\0')
4551 snprintf(p, p_max_size, "N/A");
4552 else if (unit == 'C')
4553 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4554 else if (unit == 'F')
4555 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4557 snprintf(p, p_max_size, "N/A");
4562 new_offset(p, obj->data.i);
4565 new_voffset(p, obj->data.i);
4571 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4572 obj->data.sysfs.devtype, obj->data.sysfs.type);
4574 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4575 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4576 } else if (r >= 100.0 || r == 0) {
4577 snprintf(p, p_max_size, "%d", (int) r);
4579 snprintf(p, p_max_size, "%.1f", r);
4585 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4586 obj->data.sysfs.devtype, obj->data.sysfs.type);
4588 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4589 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4590 } else if (r >= 100.0 || r == 0) {
4591 snprintf(p, p_max_size, "%d", (int) r);
4593 snprintf(p, p_max_size, "%.1f", r);
4599 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4600 obj->data.sysfs.devtype, obj->data.sysfs.type);
4602 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4603 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4604 } else if (r >= 100.0 || r == 0) {
4605 snprintf(p, p_max_size, "%d", (int) r);
4607 snprintf(p, p_max_size, "%.1f", r);
4610 #endif /* !__OpenBSD__ */
4612 new_alignr(p, obj->data.i);
4615 new_alignc(p, obj->data.i);
4618 struct text_object subroot;
4619 struct information *tmp_info =
4620 malloc(sizeof(struct information));
4621 memcpy(tmp_info, cur, sizeof(struct information));
4622 parse_conky_vars(&subroot, obj->data.ifblock.s, p, tmp_info);
4624 if (strlen(p) != 0) {
4628 free_text_objects(&subroot, 0);
4632 if (obj->data.ifblock.str
4633 && !check_contains(obj->data.ifblock.s,
4634 obj->data.ifblock.str)) {
4636 } else if (obj->data.ifblock.s
4637 && access(obj->data.ifblock.s, F_OK)) {
4642 if ((obj->data.ifblock.s)
4643 && (!check_mount(obj->data.ifblock.s))) {
4648 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4652 #if defined(__linux__)
4654 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4658 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4661 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4666 human_readable(cur->mem * 1024, p, 255, "mem");
4669 human_readable(cur->memeasyfree * 1024, p, 255, "memeasyfree");
4672 human_readable(cur->memfree * 1024, p, 255, "memfree");
4675 human_readable(cur->memmax * 1024, p, 255, "memmax");
4679 spaced_print(p, p_max_size, "%*llu", 4, "memperc",
4680 pad_percents, cur->mem * 100 / cur->memmax);
4684 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4685 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4688 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4689 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4690 100, 1, obj->showaslog);
4695 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
4698 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
4701 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
4704 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4705 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
4708 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4709 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
4712 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4713 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
4717 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4719 OBJ(monitor_number) {
4720 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4726 update_mail_count(&obj->data.local_mail);
4727 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4730 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
4732 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4735 update_mail_count(&obj->data.local_mail);
4736 snprintf(p, p_max_size, "%d",
4737 obj->data.local_mail.new_mail_count);
4740 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
4743 new_outline(p, obj->data.l);
4746 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
4748 OBJ(running_processes) {
4749 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
4752 snprintf(p, p_max_size, "%s", obj->data.s);
4755 new_bg(p, obj->data.l);
4758 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
4761 human_readable(cur->swap * 1024, p, 255, "swap");
4764 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
4767 if (cur->swapmax == 0) {
4768 strncpy(p, "No swap", p_max_size);
4770 spaced_print(p, p_max_size, "%*llu", 4, "swapperc",
4771 pad_percents, cur->swap * 100 / cur->swapmax);
4775 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4776 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
4779 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4782 time_t t = time(NULL);
4783 struct tm *tm = localtime(&t);
4785 setlocale(LC_TIME, "");
4786 strftime(p, p_max_size, obj->data.s, tm);
4789 time_t t = time(NULL);
4790 struct tm *tm = gmtime(&t);
4792 strftime(p, p_max_size, obj->data.s, tm);
4799 if (obj->data.tztime.tz) {
4800 oldTZ = getenv("TZ");
4801 setenv("TZ", obj->data.tztime.tz, 1);
4807 setlocale(LC_TIME, "");
4808 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4810 setenv("TZ", oldTZ, 1);
4815 // Needless to free oldTZ since getenv gives ptr to static data
4818 human_readable(obj->data.net->recv, p, 255, "totaldown");
4821 human_readable(obj->data.net->trans, p, 255, "totalup");
4824 snprintf(p, p_max_size, "%d", total_updates);
4827 spaced_print(p, p_max_size, "%d", 6, "upspeed",
4828 round_to_int(obj->data.net->trans_speed / 1024));
4831 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
4832 obj->data.net->trans_speed / 1024.0);
4835 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4836 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
4839 format_seconds_short(p, p_max_size, (int) cur->uptime);
4842 format_seconds(p, p_max_size, (int) cur->uptime);
4845 snprintf(p, p_max_size, "%s", cur->users.names);
4848 snprintf(p, p_max_size, "%s", cur->users.terms);
4851 snprintf(p, p_max_size, "%s", cur->users.times);
4854 snprintf(p, p_max_size, "%d", cur->users.number);
4856 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
4857 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4861 msg = get_apm_adapter();
4862 snprintf(p, p_max_size, "%s", msg);
4865 OBJ(apm_battery_life) {
4868 msg = get_apm_battery_life();
4869 snprintf(p, p_max_size, "%s", msg);
4872 OBJ(apm_battery_time) {
4875 msg = get_apm_battery_time();
4876 snprintf(p, p_max_size, "%s", msg);
4879 #endif /* __FreeBSD__ __OpenBSD__ */
4883 int len = obj->data.i;
4884 if (len == 0 || len > p_max_size)
4886 snprintf(p, len, "%s", cur->mpd.title);
4889 int len = obj->data.i;
4890 if (len == 0 || len > p_max_size)
4892 snprintf(p, len, "%s", cur->mpd.artist);
4895 int len = obj->data.i;
4896 if (len == 0 || len > p_max_size)
4898 snprintf(p, len, "%s", cur->mpd.album);
4901 snprintf(p, p_max_size, "%s", cur->mpd.random);
4904 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4907 int len = obj->data.i;
4908 if (len == 0 || len > p_max_size)
4910 snprintf(p, len, "%s", cur->mpd.track);
4913 int len = obj->data.i;
4914 if (len == 0 || len > p_max_size)
4916 snprintf(p, len, "%s", cur->mpd.name);
4919 int len = obj->data.i;
4920 if (len == 0 || len > p_max_size)
4922 snprintf(p, len, "%s", cur->mpd.file);
4925 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4928 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4931 snprintf(p, p_max_size, "%s", cur->mpd.status);
4934 format_media_player_time(p, p_max_size, cur->mpd.elapsed);
4937 format_media_player_time(p, p_max_size, cur->mpd.length);
4940 spaced_print(p, p_max_size, "%*d", 4, "mpd_percent",
4941 pad_percents, (int) (cur->mpd.progress * 100));
4944 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4945 (int) (cur->mpd.progress * 255.0f));
4948 int len = obj->data.i;
4949 if (len == 0 || len > p_max_size)
4952 memset(p, 0, p_max_size);
4953 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
4954 && *cur->mpd.title) {
4955 snprintf(p, len, "%s - %s", cur->mpd.artist,
4957 } else if (cur->mpd.title && *cur->mpd.title) {
4958 snprintf(p, len, "%s", cur->mpd.title);
4959 } else if (cur->mpd.artist && *cur->mpd.artist) {
4960 snprintf(p, len, "%s", cur->mpd.artist);
4961 } else if (cur->mpd.file && *cur->mpd.file) {
4962 snprintf(p, len, "%s", cur->mpd.file);
4967 OBJ(if_mpd_playing) {
4968 if (!cur->mpd.is_playing) {
4976 snprintf(p, p_max_size, "%s", (cur->moc.state ? cur->moc.state : "??"));
4979 snprintf(p, p_max_size, "%s", (cur->moc.file ? cur->moc.file : "no file"));
4982 snprintf(p, p_max_size, "%s", (cur->moc.title ? cur->moc.title : "no title"));
4985 snprintf(p, p_max_size, "%s", (cur->moc.artist ? cur->moc.artist : "no artist"));
4988 snprintf(p, p_max_size, "%s", (cur->moc.song ? cur->moc.song : "no song"));
4991 snprintf(p, p_max_size, "%s", (cur->moc.album ? cur->moc.album : "no album"));
4993 OBJ(moc_totaltime) {
4994 snprintf(p, p_max_size, "%s", (cur->moc.totaltime ? cur->moc.totaltime : "0:00"));
4997 snprintf(p, p_max_size, "%s", (cur->moc.timeleft ? cur->moc.timeleft : "0:00"));
5000 snprintf(p, p_max_size, "%s", (cur->moc.curtime ? cur->moc.curtime : "0:00"));
5003 snprintf(p, p_max_size, "%s", (cur->moc.bitrate ? cur->moc.bitrate : "0Kbps"));
5006 snprintf(p, p_max_size, "%s", (cur->moc.rate ? cur->moc.rate : "0KHz"));
5011 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5014 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5017 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5020 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5022 OBJ(xmms2_comment) {
5023 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5026 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5029 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5032 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5034 OBJ(xmms2_tracknr) {
5035 if (cur->xmms2.tracknr != -1) {
5036 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5039 OBJ(xmms2_bitrate) {
5040 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5043 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5046 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5048 OBJ(xmms2_elapsed) {
5049 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5050 (cur->xmms2.elapsed / 1000) % 60);
5052 OBJ(xmms2_duration) {
5053 snprintf(p, p_max_size, "%02d:%02d",
5054 cur->xmms2.duration / 60000,
5055 (cur->xmms2.duration / 1000) % 60);
5057 OBJ(xmms2_percent) {
5058 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5061 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5062 (int) (cur->xmms2.progress * 255.0f));
5064 OBJ(xmms2_playlist) {
5065 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5067 OBJ(xmms2_timesplayed) {
5068 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5071 if (strlen(cur->xmms2.title) < 2
5072 && strlen(cur->xmms2.title) < 2) {
5073 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5075 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5079 OBJ(if_xmms2_connected) {
5080 if (cur->xmms2_conn_state != 1) {
5086 OBJ(audacious_status) {
5087 snprintf(p, p_max_size, "%s",
5088 cur->audacious.items[AUDACIOUS_STATUS]);
5090 OBJ(audacious_title) {
5091 snprintf(p, cur->audacious.max_title_len > 0
5092 ? cur->audacious.max_title_len : p_max_size, "%s",
5093 cur->audacious.items[AUDACIOUS_TITLE]);
5095 OBJ(audacious_length) {
5096 snprintf(p, p_max_size, "%s",
5097 cur->audacious.items[AUDACIOUS_LENGTH]);
5099 OBJ(audacious_length_seconds) {
5100 snprintf(p, p_max_size, "%s",
5101 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5103 OBJ(audacious_position) {
5104 snprintf(p, p_max_size, "%s",
5105 cur->audacious.items[AUDACIOUS_POSITION]);
5107 OBJ(audacious_position_seconds) {
5108 snprintf(p, p_max_size, "%s",
5109 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5111 OBJ(audacious_bitrate) {
5112 snprintf(p, p_max_size, "%s",
5113 cur->audacious.items[AUDACIOUS_BITRATE]);
5115 OBJ(audacious_frequency) {
5116 snprintf(p, p_max_size, "%s",
5117 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5119 OBJ(audacious_channels) {
5120 snprintf(p, p_max_size, "%s",
5121 cur->audacious.items[AUDACIOUS_CHANNELS]);
5123 OBJ(audacious_filename) {
5124 snprintf(p, p_max_size, "%s",
5125 cur->audacious.items[AUDACIOUS_FILENAME]);
5127 OBJ(audacious_playlist_length) {
5128 snprintf(p, p_max_size, "%s",
5129 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5131 OBJ(audacious_playlist_position) {
5132 snprintf(p, p_max_size, "%s",
5133 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5135 OBJ(audacious_main_volume) {
5136 snprintf(p, p_max_size, "%s",
5137 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5139 OBJ(audacious_bar) {
5143 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5144 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5145 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5151 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5154 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5157 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5160 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5163 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5166 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5170 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5173 switch (obj->data.top.type) {
5175 snprintf(p, 16, "%-15s",
5176 cur->cpu[obj->data.top.num]->name);
5179 snprintf(p, 7, "%6.2f",
5180 cur->cpu[obj->data.top.num]->amount);
5183 snprintf(p, 6, "%5i",
5184 cur->cpu[obj->data.top.num]->pid);
5187 snprintf(p, 7, "%6.2f",
5188 cur->cpu[obj->data.top.num]->totalmem);
5191 timeval = format_time(
5192 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
5193 snprintf(p, 10, "%9s", timeval);
5197 human_readable(cur->cpu[obj->data.top.num]->rss,
5201 human_readable(cur->cpu[obj->data.top.num]->vsize,
5205 ERR("Unhandled top data type: %d\n",
5206 obj->data.top.type);
5209 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5213 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5216 switch (obj->data.top.type) {
5218 snprintf(p, 16, "%-15s",
5219 cur->memu[obj->data.top.num]->name);
5222 snprintf(p, 7, "%6.2f",
5223 cur->memu[obj->data.top.num]->amount);
5226 snprintf(p, 6, "%5i",
5227 cur->memu[obj->data.top.num]->pid);
5230 snprintf(p, 7, "%6.2f",
5231 cur->memu[obj->data.top.num]->totalmem);
5234 timeval = format_time(
5235 cur->memu[obj->data.top.num]->total_cpu_time,
5237 snprintf(p, 10, "%9s", timeval);
5241 human_readable(cur->cpu[obj->data.top.num]->rss,
5245 human_readable(cur->cpu[obj->data.top.num]->vsize,
5249 ERR("Unhandled top data type: %d\n",
5250 obj->data.top.type);
5253 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5257 if (current_update_time - obj->data.tail.last_update
5258 < obj->data.tail.interval) {
5259 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5265 obj->data.tail.last_update = current_update_time;
5267 if (obj->data.tail.fd != -1) {
5268 tail_pipe(obj, p, p_max_size);
5272 fp = fopen(obj->data.tail.logfile, "rt");
5275 /* Send one message, but do not consistently spam
5276 * on missing logfiles. */
5277 if (obj->data.tail.readlines != 0) {
5278 ERR("tail logfile failed to open");
5279 strcpy(obj->data.tail.buffer, "Logfile Missing");
5281 obj->data.tail.readlines = 0;
5282 snprintf(p, p_max_size, "Logfile Missing");
5284 obj->data.tail.readlines = 0;
5285 /* -1 instead of 0 to avoid counting a trailing
5287 fseek(fp, -1, SEEK_END);
5288 bsize = ftell(fp) + 1;
5289 for (iter = obj->data.tail.wantedlines; iter > 0;
5291 nl = rev_fcharfind(fp, '\n', iter);
5296 obj->data.tail.readlines = iter;
5297 if (obj->data.tail.readlines
5298 < obj->data.tail.wantedlines) {
5299 fseek(fp, 0, SEEK_SET);
5301 fseek(fp, nl + 1, SEEK_SET);
5304 /* Make sure bsize is at least 1 byte smaller than the
5305 * buffer max size. */
5306 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
5307 fseek(fp, bsize - text_buffer_size * 20 - 1,
5309 bsize = text_buffer_size * 20 - 1;
5311 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
5314 /* Clean up trailing newline, make sure the
5315 * buffer is null terminated. */
5316 if (obj->data.tail.buffer[bsize - 1] == '\n') {
5317 obj->data.tail.buffer[bsize - 1] = '\0';
5319 obj->data.tail.buffer[bsize] = '\0';
5321 snprintf(p, p_max_size, "%s",
5322 obj->data.tail.buffer);
5324 strcpy(obj->data.tail.buffer, "Logfile Empty");
5325 snprintf(p, p_max_size, "Logfile Empty");
5328 } /* if cur_upd_time >= */
5332 if (current_update_time - obj->data.tail.last_update
5333 < obj->data.tail.interval) {
5334 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5340 obj->data.tail.last_update = current_update_time;
5342 fp = fopen(obj->data.tail.logfile, "rt");
5344 /* Send one message, but do not consistently spam
5345 * on missing logfiles. */
5346 if (obj->data.tail.readlines != 0) {
5347 ERR("head logfile failed to open");
5348 strcpy(obj->data.tail.buffer, "Logfile Missing");
5350 obj->data.tail.readlines = 0;
5351 snprintf(p, p_max_size, "Logfile Missing");
5353 obj->data.tail.readlines = 0;
5354 for (iter = obj->data.tail.wantedlines; iter > 0;
5356 nl = fwd_fcharfind(fp, '\n', iter);
5361 obj->data.tail.readlines = iter;
5362 /* Make sure nl is at least 1 byte smaller than the
5363 * buffer max size. */
5364 if (nl > (long) ((text_buffer_size * 20) - 1)) {
5365 nl = text_buffer_size * 20 - 1;
5367 nl = fread(obj->data.tail.buffer, 1, nl, fp);
5370 /* Clean up trailing newline, make sure the buffer
5371 * is null terminated. */
5372 if (obj->data.tail.buffer[nl - 1] == '\n') {
5373 obj->data.tail.buffer[nl - 1] = '\0';
5375 obj->data.tail.buffer[nl] = '\0';
5377 snprintf(p, p_max_size, "%s",
5378 obj->data.tail.buffer);
5380 strcpy(obj->data.tail.buffer, "Logfile Empty");
5381 snprintf(p, p_max_size, "Logfile Empty");
5383 } /* if fp == null */
5384 } /* cur_upd_time >= */
5388 FILE *fp = fopen(obj->data.s,"r");
5395 while(fgets(buf, BUFSZ, fp) != NULL){
5396 for(j = 0; buf[j] != 0; j++) {
5397 if(buf[j] == '\n') {
5402 sprintf(p, "%d", lines);
5405 sprintf(p, "File Unreadable");
5410 FILE *fp = fopen(obj->data.s,"r");
5415 char inword = FALSE;
5418 while(fgets(buf, BUFSZ, fp) != NULL){
5419 for(j = 0; buf[j] != 0; j++) {
5420 if(!isspace(buf[j])) {
5421 if(inword == FALSE) {
5430 sprintf(p, "%d", words);
5433 sprintf(p, "File Unreadable");
5436 #ifdef TCP_PORT_MONITOR
5438 tcp_portmon_action(p, p_max_size,
5439 &obj->data.tcp_port_monitor);
5445 iconv_converting = 1;
5446 iconv_selected = obj->a;
5449 iconv_converting = 0;
5454 OBJ(entropy_avail) {
5455 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5457 OBJ(entropy_poolsize) {
5458 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5461 double entropy_perc;
5463 entropy_perc = (double) cur->entropy.entropy_avail /
5464 (double) cur->entropy.poolsize;
5465 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5471 s = smapi_get_val(obj->data.s);
5472 snprintf(p, p_max_size, "%s", s);
5476 OBJ(if_smapi_bat_installed) {
5478 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5479 if(!smapi_bat_installed(idx)) {
5483 ERR("argument to if_smapi_bat_installed must be an integer");
5485 OBJ(smapi_bat_perc) {
5487 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5488 val = smapi_bat_installed(idx) ?
5489 smapi_get_bat_int(idx, "remaining_percent") : 0;
5490 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
5492 ERR("argument to smapi_bat_perc must be an integer");
5494 OBJ(smapi_bat_temp) {
5496 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5497 val = smapi_bat_installed(idx) ?
5498 smapi_get_bat_int(idx, "temperature") : 0;
5499 /* temperature is in milli degree celsius */
5500 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5502 ERR("argument to smapi_bat_temp must be an integer");
5504 OBJ(smapi_bat_power) {
5506 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5507 val = smapi_bat_installed(idx) ?
5508 smapi_get_bat_int(idx, "power_now") : 0;
5509 /* power_now is in mW, set to W with one digit precision */
5510 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5512 ERR("argument to smapi_bat_power must be an integer");
5514 OBJ(smapi_bat_bar) {
5515 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5516 new_bar(p, obj->a, obj->b, (int)
5517 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5519 new_bar(p, obj->a, obj->b, 0);
5525 struct text_object subroot;
5526 parse_conky_vars(&subroot, obj->data.scroll.text, p, cur);
5528 if (strlen(p) <= obj->data.scroll.show) {
5531 #define LINESEPARATOR '|'
5532 //place all the lines behind each other with LINESEPARATOR between them
5533 for(j = 0; p[j] != 0; j++) {
5538 //scroll the output obj->data.scroll.start places by copying that many chars from
5539 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5540 //at the back of the string
5541 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5542 strncpy(tmp, p, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5543 for(j = obj->data.scroll.start; p[j] != 0; j++){
5544 p[j - obj->data.scroll.start] = p[j];
5546 strcpy(&p[j - obj->data.scroll.start], tmp);
5548 //only show the requested number of chars
5549 if(obj->data.scroll.show < j) {
5550 p[obj->data.scroll.show] = 0;
5552 //next time, scroll a place more or reset scrolling if we are at the end
5553 obj->data.scroll.start += obj->data.scroll.step;
5554 if(obj->data.scroll.start >= j){
5555 obj->data.scroll.start = 0;
5560 int value = get_nvidia_value(obj->data.nvidia.type, display);
5562 snprintf(p, p_max_size, "N/A");
5563 else if (obj->data.nvidia.type == NV_TEMP)
5564 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5565 else if (obj->data.nvidia.print_as_float &&
5566 value > 0 && value < 100)
5567 snprintf(p, p_max_size, "%.1f", (float)value);
5569 snprintf(p, p_max_size, "%d", value);
5579 unsigned int a = strlen(p);
5582 if (a > 0 && iconv_converting && iconv_selected > 0
5583 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5585 size_t dummy1, dummy2;
5586 char *ptr = buff_in;
5589 dummy1 = dummy2 = a;
5591 strncpy(buff_in, p, p_max_size);
5593 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5594 while (dummy1 > 0) {
5595 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5598 ERR("Iconv codeset conversion failed");
5603 /* It is nessecary when we are converting from multibyte to
5604 * singlebyte codepage */
5607 #endif /* HAVE_ICONV */
5614 double current_update_time, next_update_time, last_update_time;
5616 static void generate_text(void)
5618 struct information *cur = &info;
5625 current_update_time = get_time();
5628 /* fix diskio rates to b/s (use update_interval */
5629 info.diskio_read_value /= update_interval;
5630 info.diskio_write_value /= update_interval;
5631 info.diskio_value /= update_interval;
5633 /* add things to the buffer */
5639 generate_text_internal(p, max_user_text, global_root_object, cur);
5641 if (stuff_in_upper_case) {
5644 tmp_p = text_buffer;
5646 *tmp_p = toupper(*tmp_p);
5651 next_update_time += update_interval;
5652 if (next_update_time < get_time()) {
5653 next_update_time = get_time() + update_interval;
5654 } else if (next_update_time > get_time() + update_interval) {
5655 next_update_time = get_time() + update_interval;
5657 last_update_time = current_update_time;
5663 static void set_font(void)
5667 if (window.xftdraw != NULL) {
5668 XftDrawDestroy(window.xftdraw);
5670 window.xftdraw = XftDrawCreate(display, window.drawable,
5671 DefaultVisual(display, screen), DefaultColormap(display, screen));
5675 XSetFont(display, window.gc, fonts[selected_font].font->fid);
5681 static inline int get_string_width(const char *s)
5684 return *s ? calc_text_width(s, strlen(s)) : 0;
5690 static inline int get_string_width_special(char *s)
5702 p = strndup(s, text_buffer_size);
5706 if (*p == SPECIAL_CHAR) {
5707 /* shift everything over by 1 so that the special char
5708 * doesn't mess up the size calculation */
5709 for (i = 0; i < strlen(p); i++) {
5710 *(p + i) = *(p + i + 1);
5712 if (specials[special_index + idx].type == GRAPH
5713 || specials[special_index + idx].type == BAR) {
5714 width += specials[special_index + idx].width;
5721 if (strlen(final) > 1) {
5722 width += calc_text_width(final, strlen(final));
5727 return (s) ? strlen(s) : 0;
5732 static void text_size_updater(char *s);
5734 int last_font_height;
5735 static void update_text_area(void)
5739 /* update text size if it isn't fixed */
5744 text_width = minimum_width;
5747 last_font_height = font_height();
5748 for_each_line(text_buffer, text_size_updater);
5750 if (text_height < minimum_height) {
5751 text_height = minimum_height;
5753 if (text_width > maximum_width && maximum_width > 0) {
5754 text_width = maximum_width;
5758 /* get text position on workarea */
5759 switch (text_alignment) {
5766 x = workarea[2] - text_width - gap_x;
5771 x = workarea[2] / 2 - text_width / 2 - gap_x;
5778 y = workarea[3] - text_height - gap_y;
5782 x = workarea[2] - text_width - gap_x;
5783 y = workarea[3] - text_height - gap_y;
5787 x = workarea[2] / 2 - text_width / 2 - gap_x;
5788 y = workarea[3] - text_height - gap_y;
5793 y = workarea[3] / 2 - text_height / 2 - gap_y;
5797 x = workarea[2] - text_width - gap_x;
5798 y = workarea[3] / 2 - text_height / 2 - gap_y;
5802 case NONE: // Let the WM manage the window
5813 if (own_window && !fixed_pos) {
5816 text_start_x = border_margin + 1;
5817 text_start_y = border_margin + 1;
5818 window.x = x - border_margin - 1;
5819 window.y = y - border_margin - 1;
5823 /* If window size doesn't match to workarea's size,
5824 * then window probably includes panels (gnome).
5825 * Blah, doesn't work on KDE. */
5826 if (workarea[2] != window.width || workarea[3] != window.height) {
5838 static int cur_x, cur_y; /* current x and y for drawing */
5840 //draw_mode also without X11 because we only need to print to stdout with FG
5841 static int draw_mode; /* FG, BG or OUTLINE */
5843 static long current_color;
5846 static void text_size_updater(char *s)
5851 /* get string widths and skip specials */
5854 if (*p == SPECIAL_CHAR) {
5856 w += get_string_width(s);
5859 if (specials[special_index].type == BAR
5860 || specials[special_index].type == GRAPH) {
5861 w += specials[special_index].width;
5862 if (specials[special_index].height > last_font_height) {
5863 last_font_height = specials[special_index].height;
5864 last_font_height += font_ascent();
5866 } else if (specials[special_index].type == OFFSET) {
5867 if (specials[special_index].arg > 0) {
5868 w += specials[special_index].arg;
5870 } else if (specials[special_index].type == VOFFSET) {
5871 last_font_height += specials[special_index].arg;
5872 } else if (specials[special_index].type == GOTO) {
5873 if (specials[special_index].arg > cur_x) {
5874 w = (int) specials[special_index].arg;
5876 } else if (specials[special_index].type == TAB) {
5877 int start = specials[special_index].arg;
5878 int step = specials[special_index].width;
5880 if (!step || step < 0) {
5883 w += step - (cur_x - text_start_x - start) % step;
5884 } else if (specials[special_index].type == FONT) {
5885 selected_font = specials[special_index].font_added;
5886 if (font_height() > last_font_height) {
5887 last_font_height = font_height();
5896 w += get_string_width(s);
5897 if (w > text_width) {
5900 if (text_width > maximum_width && maximum_width) {
5901 text_width = maximum_width;
5904 text_height += last_font_height;
5905 last_font_height = font_height();
5909 static inline void set_foreground_color(long c)
5912 XSetForeground(display, window.gc, c);
5916 static void draw_string(const char *s)
5918 int i, i2, pos, width_of_s;
5926 width_of_s = get_string_width(s);
5927 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
5929 fflush(stdout); /* output immediately, don't buffer */
5931 memset(tmpstring1, 0, text_buffer_size);
5932 memset(tmpstring2, 0, text_buffer_size);
5933 strncpy(tmpstring1, s, text_buffer_size - 1);
5938 max = ((text_width - width_of_s) / get_string_width(" "));
5940 /* This code looks for tabs in the text and coverts them to spaces.
5941 * The trick is getting the correct number of spaces, and not going
5942 * over the window's size without forcing the window larger. */
5943 for (i = 0; i < (int) text_buffer_size; i++) {
5944 if (tmpstring1[i] == '\t') {
5946 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
5947 /* guard against overrun */
5948 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
5953 /* guard against overrun */
5954 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
5959 if (text_width == maximum_width) {
5960 /* this means the text is probably pushing the limit,
5961 * so we'll chop it */
5962 while (cur_x + get_string_width(tmpstring2) - text_start_x
5963 > maximum_width && strlen(tmpstring2) > 0) {
5964 tmpstring2[strlen(tmpstring2) - 1] = '\0';
5975 c.pixel = current_color;
5976 XQueryColor(display, DefaultColormap(display, screen), &c);
5979 c2.color.red = c.red;
5980 c2.color.green = c.green;
5981 c2.color.blue = c.blue;
5982 c2.color.alpha = fonts[selected_font].font_alpha;
5984 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5985 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5987 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5988 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5993 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
5996 cur_x += width_of_s;
5998 memcpy(tmpstring1, s, text_buffer_size);
6001 long redmask, greenmask, bluemask;
6003 void set_up_gradient(void)
6007 colour_depth = DisplayPlanes(display, screen);
6011 if (colour_depth != 24 && colour_depth != 16) {
6012 ERR("using non-standard colour depth, gradients may look like a "
6019 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6021 greenmask |= 1 << i;
6024 if (colour_depth % 3 == 1) {
6025 greenmask |= 1 << (colour_depth / 3);
6027 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6028 greenmask = greenmask << (colour_depth / 3);
6031 /* this function returns the next colour between two colours for a gradient */
6032 unsigned long do_gradient(unsigned long first_colour,
6033 unsigned long last_colour)
6036 int red1, green1, blue1; // first colour
6037 int red2, green2, blue2; // second colour
6038 int red3 = 0, green3 = 0, blue3 = 0; // difference
6039 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6040 short greenshift = (colour_depth / 3);
6042 red1 = (first_colour & redmask) >> redshift;
6043 green1 = (first_colour & greenmask) >> greenshift;
6044 blue1 = first_colour & bluemask;
6045 red2 = (last_colour & redmask) >> redshift;
6046 green2 = (last_colour & greenmask) >> greenshift;
6047 blue2 = last_colour & bluemask;
6054 if (green1 > green2) {
6057 if (green1 < green2) {
6060 if (blue1 > blue2) {
6063 if (blue1 < blue2) {
6078 if (red1 > bluemask) {
6081 if (green1 > bluemask) {
6084 if (blue1 > bluemask) {
6087 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
6091 /* this function returns the max diff for a gradient */
6092 unsigned long gradient_max(unsigned long first_colour,
6093 unsigned long last_colour)
6095 int red1, green1, blue1; // first colour
6096 int red2, green2, blue2; // second colour
6097 int red3 = 0, green3 = 0, blue3 = 0; // difference
6098 long redshift, greenshift;
6101 if (colour_depth == 0) {
6104 redshift = (2 * colour_depth / 3 + colour_depth % 3);
6105 greenshift = (colour_depth / 3);
6107 red1 = (first_colour & redmask) >> redshift;
6108 green1 = (first_colour & greenmask) >> greenshift;
6109 blue1 = first_colour & bluemask;
6110 red2 = (last_colour & redmask) >> redshift;
6111 green2 = (last_colour & greenmask) >> greenshift;
6112 blue2 = last_colour & bluemask;
6113 red3 = abs(red1 - red2);
6114 green3 = abs(green1 - green2);
6115 blue3 = abs(blue1 - blue2);
6127 static void draw_line(char *s)
6135 cur_x = text_start_x;
6136 cur_y += font_ascent();
6137 font_h = font_height();
6139 /* find specials and draw stuff */
6142 if (*p == SPECIAL_CHAR) {
6145 /* draw string before special */
6152 switch (specials[special_index].type) {
6153 case HORIZONTAL_LINE:
6155 int h = specials[special_index].height;
6156 int mid = font_ascent() / 2;
6158 w = text_start_x + text_width - cur_x;
6160 XSetLineAttributes(display, window.gc, h, LineSolid,
6161 CapButt, JoinMiter);
6162 XDrawLine(display, window.drawable, window.gc, cur_x,
6163 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6169 int h = specials[special_index].height;
6170 int tmp_s = specials[special_index].arg;
6171 int mid = font_ascent() / 2;
6172 char ss[2] = { tmp_s, tmp_s };
6174 w = text_start_x + text_width - cur_x - 1;
6175 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6176 CapButt, JoinMiter);
6177 XSetDashes(display, window.gc, 0, ss, 2);
6178 XDrawLine(display, window.drawable, window.gc, cur_x,
6179 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6185 int h, bar_usage, by;
6186 if (cur_x - text_start_x > maximum_width
6187 && maximum_width > 0) {
6190 h = specials[special_index].height;
6191 bar_usage = specials[special_index].arg;
6192 by = cur_y - (font_ascent() / 2) - 1;
6194 if (h < font_height()) {
6197 w = specials[special_index].width;
6199 w = text_start_x + text_width - cur_x - 1;
6205 XSetLineAttributes(display, window.gc, 1, LineSolid,
6206 CapButt, JoinMiter);
6208 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6210 XFillRectangle(display, window.drawable, window.gc, cur_x,
6211 by, w * bar_usage / 255, h);
6212 if (specials[special_index].height > cur_y_add
6213 && specials[special_index].height > font_h) {
6214 cur_y_add = specials[special_index].height;
6221 int h, by, i, j = 0;
6222 int gradient_size = 0;
6223 float gradient_factor = 0;
6224 float gradient_update = 0;
6225 unsigned long last_colour = current_color;
6226 unsigned long tmpcolour = current_color;
6227 if (cur_x - text_start_x > maximum_width
6228 && maximum_width > 0) {
6231 h = specials[special_index].height;
6232 by = cur_y - (font_ascent() / 2) - 1;
6234 if (h < font_height()) {
6237 w = specials[special_index].width;
6239 w = text_start_x + text_width - cur_x - 1;
6244 if (draw_graph_borders) {
6245 XSetLineAttributes(display, window.gc, 1, LineSolid,
6246 CapButt, JoinMiter);
6247 XDrawRectangle(display, window.drawable, window.gc,
6250 XSetLineAttributes(display, window.gc, 1, LineSolid,
6251 CapButt, JoinMiter);
6253 if (specials[special_index].last_colour != 0
6254 || specials[special_index].first_colour != 0) {
6255 tmpcolour = specials[special_index].last_colour;
6257 gradient_max(specials[special_index].last_colour,
6258 specials[special_index].first_colour);
6259 gradient_factor = (float) gradient_size / (w - 2);
6261 for (i = w - 2; i > -1; i--) {
6262 if (specials[special_index].last_colour != 0
6263 || specials[special_index].first_colour != 0) {
6264 XSetForeground(display, window.gc, tmpcolour);
6265 gradient_update += gradient_factor;
6266 while (gradient_update > 0) {
6267 tmpcolour = do_gradient(tmpcolour,
6268 specials[special_index].first_colour);
6272 if ((w - i) / ((float) (w - 2) /
6273 (specials[special_index].graph_width)) > j
6274 && j < MAX_GRAPH_DEPTH - 3) {
6277 /* this is mugfugly, but it works */
6278 XDrawLine(display, window.drawable, window.gc,
6279 cur_x + i + 1, by + h, cur_x + i + 1,
6280 by + h - specials[special_index].graph[j] *
6281 (h - 1) / specials[special_index].graph_scale);
6283 if (specials[special_index].height > cur_y_add
6284 && specials[special_index].height > font_h) {
6285 cur_y_add = specials[special_index].height;
6287 /* if (draw_mode == BG) {
6288 set_foreground_color(default_bg_color);
6289 } else if (draw_mode == OUTLINE) {
6290 set_foreground_color(default_out_color);
6292 set_foreground_color(default_fg_color);
6294 if (show_graph_range) {
6297 unsigned short int seconds = update_interval * w;
6302 unsigned short int timeunits;
6304 timeunits = seconds / 86400; seconds %= 86400;
6305 if( timeunits > 0 ) {
6306 asprintf(&tmp_day_str, "%dd", timeunits);
6308 tmp_day_str = strdup("");
6310 timeunits = seconds / 3600; seconds %= 3600;
6311 if( timeunits > 0 ) {
6312 asprintf(&tmp_hour_str, "%dh", timeunits);
6314 tmp_hour_str = strdup("");
6316 timeunits = seconds / 60; seconds %= 60;
6318 asprintf(&tmp_min_str, "%dm", timeunits);
6320 tmp_min_str = strdup("");
6323 asprintf(&tmp_sec_str, "%ds", seconds);
6325 tmp_sec_str = strdup("");
6327 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6328 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6330 asprintf(&tmp_str, "Range not possible"); //should never happen, but better safe then sorry
6332 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6333 cur_y += font_height() / 2;
6334 draw_string(tmp_str);
6340 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6343 cur_x += font_ascent() / 2;
6344 cur_y += font_height() / 2;
6346 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6348 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6349 draw_string(tmp_str);
6355 set_foreground_color(last_colour);
6361 int old = font_ascent();
6363 cur_y -= font_ascent();
6364 selected_font = specials[special_index].font_added;
6365 if (cur_y + font_ascent() < cur_y + old) {
6368 cur_y += font_ascent();
6374 if (draw_mode == FG) {
6375 set_foreground_color(specials[special_index].arg);
6380 if (draw_mode == BG) {
6381 set_foreground_color(specials[special_index].arg);
6386 if (draw_mode == OUTLINE) {
6387 set_foreground_color(specials[special_index].arg);
6392 w += specials[special_index].arg;
6396 cur_y += specials[special_index].arg;
6400 if (specials[special_index].arg >= 0) {
6401 cur_x = (int) specials[special_index].arg;
6407 int start = specials[special_index].arg;
6408 int step = specials[special_index].width;
6410 if (!step || step < 0) {
6413 w = step - (cur_x - text_start_x - start) % step;
6419 /* TODO: add back in "+ border_margin" to the end of
6421 int pos_x = text_start_x + text_width -
6422 get_string_width_special(s);
6424 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6425 "get_string_width(p) %i gap_x %i "
6426 "specials[special_index].arg %i border_margin %i "
6427 "border_width %i\n", pos_x, text_start_x, text_width,
6428 cur_x, get_string_width_special(s), gap_x,
6429 specials[special_index].arg, border_margin,
6431 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6432 cur_x = pos_x - specials[special_index].arg;
6439 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6440 2 - (cur_x - text_start_x);
6441 /* int pos_x = text_start_x + text_width / 2 -
6442 get_string_width_special(s) / 2; */
6444 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6445 "get_string_width(p) %i gap_x %i "
6446 "specials[special_index].arg %i\n", pos_x, text_start_x,
6447 text_width, cur_x, get_string_width(s), gap_x,
6448 specials[special_index].arg); */
6449 if (pos_x > specials[special_index].arg) {
6450 w = pos_x - specials[special_index].arg;
6467 if (cur_y_add > 0) {
6469 cur_y -= font_descent();
6474 cur_y += font_descent();
6478 static void draw_text(void)
6481 cur_y = text_start_y;
6484 if (draw_borders && border_width > 0) {
6485 unsigned int b = (border_width + 1) / 2;
6487 if (stippled_borders) {
6488 char ss[2] = { stippled_borders, stippled_borders };
6489 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6490 CapButt, JoinMiter);
6491 XSetDashes(display, window.gc, 0, ss, 2);
6493 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6494 CapButt, JoinMiter);
6497 XDrawRectangle(display, window.drawable, window.gc,
6498 text_start_x - border_margin + b, text_start_y - border_margin + b,
6499 text_width + border_margin * 2 - 1 - b * 2,
6500 text_height + border_margin * 2 - 1 - b * 2);
6506 for_each_line(text_buffer, draw_line);
6509 static void draw_stuff(void)
6513 if (draw_shades && !draw_outline) {
6516 set_foreground_color(default_bg_color);
6527 for (i = -1; i < 2; i++) {
6528 for (j = -1; j < 2; j++) {
6529 if (i == 0 && j == 0) {
6534 set_foreground_color(default_out_color);
6535 draw_mode = OUTLINE;
6543 set_foreground_color(default_fg_color);
6552 swap.swap_window = window.window;
6553 swap.swap_action = XdbeBackground;
6554 XdbeSwapBuffers(display, &swap, 1);
6561 static void clear_text(int exposures)
6565 /* The swap action is XdbeBackground, which clears */
6570 /* there is some extra space for borders and outlines */
6571 XClearArea(display, window.window, text_start_x - border_margin - 1,
6572 text_start_y - border_margin - 1,
6573 text_width + border_margin * 2 + 2,
6574 text_height + border_margin * 2 + 2, exposures ? True : 0);
6579 static int need_to_update;
6581 /* update_text() generates new text and clears old text area */
6582 static void update_text(void)
6591 static void main_loop(void)
6593 #ifdef SIGNAL_BLOCKING
6594 sigset_t newmask, oldmask;
6598 Region region = XCreateRegion();
6602 XserverRegion region2, part;
6603 int event_base, error_base;
6605 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
6606 ERR("Xdamage extension unavailable");
6608 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
6609 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
6610 part = XFixesCreateRegionFromWindow(display, window.window, 0);
6611 #endif /* HAVE_XDAMAGE */
6614 #ifdef SIGNAL_BLOCKING
6615 sigemptyset(&newmask);
6616 sigaddset(&newmask, SIGINT);
6617 sigaddset(&newmask, SIGTERM);
6618 sigaddset(&newmask, SIGUSR1);
6621 next_update_time = last_update_time = get_time();
6623 while (total_run_times == 0 || info.looped < total_run_times) {
6626 #ifdef SIGNAL_BLOCKING
6627 /* block signals. we will inspect for pending signals later */
6628 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6629 CRIT_ERR("unable to sigprocmask()");
6636 /* wait for X event or timeout */
6638 if (!XPending(display)) {
6642 t = next_update_time - get_time();
6646 } else if (t > update_interval) {
6647 t = update_interval;
6650 tv.tv_sec = (long) t;
6651 tv.tv_usec = (long) (t * 1000000) % 1000000;
6653 FD_SET(ConnectionNumber(display), &fdsr);
6655 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
6657 if (errno != EINTR) {
6658 ERR("can't select(): %s", strerror(errno));
6664 t = (next_update_time - get_time()) * 1000000;
6665 usleep((useconds_t)t);
6673 if (need_to_update) {
6675 int wx = window.x, wy = window.y;
6683 /* resize window if it isn't right size */
6685 && (text_width + border_margin * 2 + 1 != window.width
6686 || text_height + border_margin * 2 + 1 != window.height)) {
6687 window.width = text_width + border_margin * 2 + 1;
6688 window.height = text_height + border_margin * 2 + 1;
6689 XResizeWindow(display, window.window, window.width,
6692 set_transparent_background(window.window);
6696 /* move window if it isn't in right position */
6697 if (!fixed_pos && (window.x != wx || window.y != wy)) {
6698 XMoveWindow(display, window.window, window.x, window.y);
6709 r.x = text_start_x - border_margin;
6710 r.y = text_start_y - border_margin;
6711 r.width = text_width + border_margin * 2;
6712 r.height = text_height + border_margin * 2;
6713 XUnionRectWithRegion(&r, region, region);
6718 /* handle X events */
6719 while (XPending(display)) {
6722 XNextEvent(display, &ev);
6730 r.width = ev.xexpose.width;
6731 r.height = ev.xexpose.height;
6732 XUnionRectWithRegion(&r, region, region);
6737 case ReparentNotify:
6738 /* set background to ParentRelative for all parents */
6740 set_transparent_background(window.window);
6744 case ConfigureNotify:
6746 /* if window size isn't what expected, set fixed size */
6747 if (ev.xconfigure.width != window.width
6748 || ev.xconfigure.height != window.height) {
6749 if (window.width != 0 && window.height != 0) {
6753 /* clear old stuff before screwing up
6758 XWindowAttributes attrs;
6760 if (XGetWindowAttributes(display,
6761 window.window, &attrs)) {
6762 window.width = attrs.width;
6763 window.height = attrs.height;
6767 text_width = window.width - border_margin * 2 - 1;
6768 text_height = window.height - border_margin * 2 - 1;
6769 if (text_width > maximum_width
6770 && maximum_width > 0) {
6771 text_width = maximum_width;
6775 /* if position isn't what expected, set fixed pos
6776 * total_updates avoids setting fixed_pos when window
6777 * is set to weird locations when started */
6778 /* // this is broken
6779 if (total_updates >= 2 && !fixed_pos
6780 && (window.x != ev.xconfigure.x
6781 || window.y != ev.xconfigure.y)
6782 && (ev.xconfigure.x != 0
6783 || ev.xconfigure.y != 0)) {
6792 /* if an ordinary window with decorations */
6793 if ((window.type == TYPE_NORMAL)
6794 && (!TEST_HINT(window.hints,
6795 HINT_UNDECORATED))) {
6796 /* allow conky to hold input focus. */
6799 /* forward the click to the desktop window */
6800 XUngrabPointer(display, ev.xbutton.time);
6801 ev.xbutton.window = window.desktop;
6802 XSendEvent(display, ev.xbutton.window, False,
6803 ButtonPressMask, &ev);
6804 XSetInputFocus(display, ev.xbutton.window,
6805 RevertToParent, ev.xbutton.time);
6812 /* if an ordinary window with decorations */
6813 if ((window.type == TYPE_NORMAL)
6814 && (!TEST_HINT(window.hints,
6815 HINT_UNDECORATED))) {
6816 /* allow conky to hold input focus. */
6819 /* forward the release to the desktop window */
6820 ev.xbutton.window = window.desktop;
6821 XSendEvent(display, ev.xbutton.window, False,
6822 ButtonReleaseMask, &ev);
6831 if (ev.type == event_base + XDamageNotify) {
6832 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
6834 XFixesSetRegion(display, part, &dev->area, 1);
6835 XFixesUnionRegion(display, region2, region2, part);
6837 #endif /* HAVE_XDAMAGE */
6843 XDamageSubtract(display, damage, region2, None);
6844 XFixesSetRegion(display, region2, 0, 0);
6845 #endif /* HAVE_XDAMAGE */
6847 /* XDBE doesn't seem to provide a way to clear the back buffer without
6848 * interfering with the front buffer, other than passing XdbeBackground
6849 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6850 * redraw the text even if it wasn't part of the exposed area. OTOH,
6851 * if we're not going to call draw_stuff at all, then no swap happens
6852 * and we can safely do nothing. */
6854 if (!XEmptyRegion(region)) {
6859 r.x = text_start_x - border_margin;
6860 r.y = text_start_y - border_margin;
6861 r.width = text_width + border_margin * 2;
6862 r.height = text_height + border_margin * 2;
6863 XUnionRectWithRegion(&r, region, region);
6866 XSetRegion(display, window.gc, region);
6869 XftDrawSetClip(window.xftdraw, region);
6875 XDestroyRegion(region);
6876 region = XCreateRegion();
6880 #ifdef SIGNAL_BLOCKING
6881 /* unblock signals of interest and let handler fly */
6882 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
6883 CRIT_ERR("unable to sigprocmask()");
6887 switch (g_signal_pending) {
6890 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
6895 ERR("received SIGINT or SIGTERM to terminate. bye!");
6898 XDestroyRegion(region);
6901 XDamageDestroy(display, damage);
6902 XFixesDestroyRegion(display, region2);
6903 XFixesDestroyRegion(display, part);
6904 #endif /* HAVE_XDAMAGE */
6906 return; /* return from main_loop */
6909 /* Reaching here means someone set a signal
6910 * (SIGXXXX, signal_handler), but didn't write any code
6912 * If you don't want to handle a signal, don't set a handler on
6913 * it in the first place. */
6914 if (g_signal_pending) {
6915 ERR("ignoring signal (%d)", g_signal_pending);
6919 g_signal_pending = 0;
6922 #if defined(X11) && defined(HAVE_XDAMAGE)
6923 XDamageDestroy(display, damage);
6924 XFixesDestroyRegion(display, region2);
6925 XFixesDestroyRegion(display, part);
6926 XDestroyRegion(region);
6928 #endif /* X11 && HAVE_XDAMAGE */
6931 static void load_config_file(const char *);
6933 /* reload the config file */
6934 void reload_config(void)
6936 timed_thread_destroy_registered_threads();
6938 if (info.cpu_usage) {
6939 free(info.cpu_usage);
6940 info.cpu_usage = NULL;
6948 if (info.mpd.title) {
6949 free(info.mpd.title);
6950 info.mpd.title = NULL;
6952 if (info.mpd.artist) {
6953 free(info.mpd.artist);
6954 info.mpd.artist = NULL;
6956 if (info.mpd.album) {
6957 free(info.mpd.album);
6958 info.mpd.album = NULL;
6960 if (info.mpd.random) {
6961 free(info.mpd.random);
6962 info.mpd.random = NULL;
6964 if (info.mpd.repeat) {
6965 free(info.mpd.repeat);
6966 info.mpd.repeat = NULL;
6968 if (info.mpd.track) {
6969 free(info.mpd.track);
6970 info.mpd.track = NULL;
6972 if (info.mpd.name) {
6973 free(info.mpd.name);
6974 info.mpd.name = NULL;
6976 if (info.mpd.file) {
6977 free(info.mpd.file);
6978 info.mpd.file = NULL;
6980 if (info.mpd.status) {
6981 free(info.mpd.status);
6982 info.mpd.status = NULL;
6987 free_moc(&info.moc);
6994 #ifdef TCP_PORT_MONITOR
6995 tcp_portmon_clear();
6998 if (current_config) {
7000 load_config_file(current_config);
7002 /* re-init specials array */
7003 if ((specials = realloc((void *) specials,
7004 sizeof(struct special_t) * max_specials)) == 0) {
7005 ERR("failed to realloc specials array");
7011 // clear the window first
7012 XClearWindow(display, RootWindow(display, screen));
7015 extract_variable_text(global_text);
7021 tmpstring1 = malloc(text_buffer_size);
7022 memset(tmpstring1, 0, text_buffer_size);
7026 tmpstring2 = malloc(text_buffer_size);
7027 memset(tmpstring2, 0, text_buffer_size);
7031 text_buffer = malloc(max_user_text);
7032 memset(text_buffer, 0, max_user_text);
7039 timed_thread_destroy_registered_threads();
7041 if (info.cpu_usage) {
7042 free(info.cpu_usage);
7043 info.cpu_usage = NULL;
7048 XdbeDeallocateBackBufferName(display, window.back_buffer);
7053 XDestroyWindow(display, window.window);
7054 XClearWindow(display, RootWindow(display, screen));
7059 XClearWindow(display, RootWindow(display, screen));
7064 XFreeGC(display, window.gc);
7068 free_text_objects(&global_root_object, 1);
7087 free(current_config);
7089 #ifdef TCP_PORT_MONITOR
7090 tcp_portmon_clear();
7099 for (i = 0; i < special_count; i++) {
7100 if (specials[i].type == GRAPH) {
7101 free(specials[i].graph);
7108 clear_diskio_stats();
7111 static int string_to_bool(const char *s)
7114 // Assumes an option without a true/false means true
7116 } else if (strcasecmp(s, "yes") == EQUAL) {
7118 } else if (strcasecmp(s, "true") == EQUAL) {
7120 } else if (strcasecmp(s, "1") == EQUAL) {
7127 static enum alignment string_to_alignment(const char *s)
7129 if (strcasecmp(s, "top_left") == EQUAL) {
7131 } else if (strcasecmp(s, "top_right") == EQUAL) {
7133 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7135 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7137 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7138 return BOTTOM_RIGHT;
7139 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7140 return BOTTOM_MIDDLE;
7141 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7143 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7144 return MIDDLE_RIGHT;
7145 } else if (strcasecmp(s, "tl") == EQUAL) {
7147 } else if (strcasecmp(s, "tr") == EQUAL) {
7149 } else if (strcasecmp(s, "tm") == EQUAL) {
7151 } else if (strcasecmp(s, "bl") == EQUAL) {
7153 } else if (strcasecmp(s, "br") == EQUAL) {
7154 return BOTTOM_RIGHT;
7155 } else if (strcasecmp(s, "bm") == EQUAL) {
7156 return BOTTOM_MIDDLE;
7157 } else if (strcasecmp(s, "ml") == EQUAL) {
7159 } else if (strcasecmp(s, "mr") == EQUAL) {
7160 return MIDDLE_RIGHT;
7161 } else if (strcasecmp(s, "none") == EQUAL) {
7168 static void set_default_configurations(void)
7170 fork_to_background = 0;
7171 total_run_times = 0;
7172 info.cpu_avg_samples = 2;
7173 info.net_avg_samples = 2;
7180 strcpy(info.mpd.host, "localhost");
7181 info.mpd.port = 6600;
7182 info.mpd.status = NULL;
7183 info.mpd.artist = NULL;
7184 info.mpd.album = NULL;
7185 info.mpd.title = NULL;
7186 info.mpd.random = NULL;
7187 info.mpd.track = NULL;
7188 info.mpd.name = NULL;
7189 info.mpd.file = NULL;
7192 init_moc(&info.moc);
7195 info.xmms2.artist = NULL;
7196 info.xmms2.album = NULL;
7197 info.xmms2.title = NULL;
7198 info.xmms2.genre = NULL;
7199 info.xmms2.comment = NULL;
7200 info.xmms2.url = NULL;
7201 info.xmms2.status = NULL;
7202 info.xmms2.playlist = NULL;
7204 use_spacer = NO_SPACER;
7206 output_methods = TO_X;
7208 output_methods = TO_STDOUT;
7211 show_graph_scale = 0;
7212 show_graph_range = 0;
7213 default_fg_color = WhitePixel(display, screen);
7214 default_bg_color = BlackPixel(display, screen);
7215 default_out_color = BlackPixel(display, screen);
7216 color0 = default_fg_color;
7217 color1 = default_fg_color;
7218 color2 = default_fg_color;
7219 color3 = default_fg_color;
7220 color4 = default_fg_color;
7221 color5 = default_fg_color;
7222 color6 = default_fg_color;
7223 color7 = default_fg_color;
7224 color8 = default_fg_color;
7225 color9 = default_fg_color;
7226 template[0] = strdup("");
7227 template[1] = strdup("");
7228 template[2] = strdup("");
7229 template[3] = strdup("");
7230 template[4] = strdup("");
7231 template[5] = strdup("");
7232 template[6] = strdup("");
7233 template[7] = strdup("");
7234 template[8] = strdup("");
7235 template[9] = strdup("");
7238 draw_graph_borders = 1;
7240 set_first_font("6x10");
7248 window.type = TYPE_NORMAL;
7250 strcpy(window.class_name, PACKAGE_NAME);
7252 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7254 stippled_borders = 0;
7257 text_alignment = BOTTOM_LEFT;
7258 info.x11.monitor.number = 1;
7259 info.x11.monitor.current = 0;
7262 free(current_mail_spool);
7266 variable_substitute(MAIL_FILE, buf, 256);
7267 if (buf[0] != '\0') {
7268 current_mail_spool = strndup(buf, text_buffer_size);
7273 update_interval = 3.0;
7274 info.music_player_interval = 1.0;
7275 stuff_in_upper_case = 0;
7276 info.users.number = 1;
7278 #ifdef TCP_PORT_MONITOR
7279 /* set default connection limit */
7280 tcp_portmon_set_max_connections(0);
7284 static void load_config_file(const char *f)
7286 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7290 set_default_configurations();
7291 #ifdef CONFIG_OUTPUT
7292 if (!strcmp(f, "==builtin==")) {
7293 #ifdef HAVE_FOPENCOOKIE
7294 fp = fopencookie(NULL, "r", conf_cookie);
7297 #endif /* CONFIG_OUTPUT */
7303 DBGP("reading contents from config file '%s'", f);
7306 char buf[256], *p, *p2, *name, *value;
7309 if (fgets(buf, 256, fp) == NULL) {
7315 /* break at comment */
7316 p2 = strchr(p, '#');
7322 while (*p && isspace((int) *p)) {
7326 continue; /* empty line */
7333 while (*p2 && !isspace((int) *p2)) {
7337 *p2 = '\0'; /* break at name's end */
7344 while (*p && isspace((int) *p)) {
7350 p2 = value + strlen(value);
7351 while (isspace((int) *(p2 - 1))) {
7358 #define CONF2(a) if (strcasecmp(name, a) == 0)
7359 #define CONF(a) else CONF2(a)
7360 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7361 || strcasecmp(name, b) == 0)
7364 CONF2("alignment") {
7365 if (window.type == TYPE_DOCK)
7368 int a = string_to_alignment(value);
7379 CONF("background") {
7380 fork_to_background = string_to_bool(value);
7383 CONF2("background") {
7384 fork_to_background = string_to_bool(value);
7388 CONF("show_graph_scale") {
7389 show_graph_scale = string_to_bool(value);
7391 CONF("show_graph_range") {
7392 show_graph_range = string_to_bool(value);
7394 CONF("border_margin") {
7396 border_margin = strtol(value, 0, 0);
7401 CONF("border_width") {
7403 border_width = strtol(value, 0, 0);
7410 color0 = get_x11_color(value);
7417 color1 = get_x11_color(value);
7424 color2 = get_x11_color(value);
7431 color3 = get_x11_color(value);
7438 color4 = get_x11_color(value);
7445 color5 = get_x11_color(value);
7452 color6 = get_x11_color(value);
7459 color7 = get_x11_color(value);
7466 color8 = get_x11_color(value);
7473 color9 = get_x11_color(value);
7478 #define TEMPLATE_CONF(n) \
7479 CONF("template"#n) { \
7481 free(template[n]); \
7482 template[n] = strdup(value); \
7497 CONF("default_color") {
7499 default_fg_color = get_x11_color(value);
7504 CONF3("default_shade_color", "default_shadecolor") {
7506 default_bg_color = get_x11_color(value);
7511 CONF3("default_outline_color", "default_outlinecolor") {
7513 default_out_color = get_x11_color(value);
7521 info.mail = parse_mail_args(IMAP_TYPE, value);
7528 info.mail = parse_mail_args(POP3_TYPE, value);
7536 strncpy(info.mpd.host, value, 127);
7543 info.mpd.port = strtol(value, 0, 0);
7544 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
7549 CONF("mpd_password") {
7551 strncpy(info.mpd.password, value, 127);
7557 CONF("music_player_interval") {
7559 info.music_player_interval = strtod(value, 0);
7565 CONF("sensor_device") {
7567 sensor_device = strtol(value, 0, 0);
7573 CONF("cpu_avg_samples") {
7575 cpu_avg_samples = strtol(value, 0, 0);
7576 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
7579 info.cpu_avg_samples = cpu_avg_samples;
7585 CONF("net_avg_samples") {
7587 net_avg_samples = strtol(value, 0, 0);
7588 if (net_avg_samples < 1 || net_avg_samples > 14) {
7591 info.net_avg_samples = net_avg_samples;
7599 CONF("double_buffer") {
7600 use_xdbe = string_to_bool(value);
7604 CONF("override_utf8_locale") {
7605 utf8_mode = string_to_bool(value);
7607 CONF("draw_borders") {
7608 draw_borders = string_to_bool(value);
7610 CONF("draw_graph_borders") {
7611 draw_graph_borders = string_to_bool(value);
7613 CONF("draw_shades") {
7614 draw_shades = string_to_bool(value);
7616 CONF("draw_outline") {
7617 draw_outline = string_to_bool(value);
7620 CONF("out_to_console") {
7621 if(string_to_bool(value)) output_methods |= TO_STDOUT;
7623 CONF("use_spacer") {
7625 if (strcasecmp(value, "left") == EQUAL) {
7626 use_spacer = LEFT_SPACER;
7627 } else if (strcasecmp(value, "right") == EQUAL) {
7628 use_spacer = RIGHT_SPACER;
7629 } else if (strcasecmp(value, "none") == EQUAL) {
7630 use_spacer = NO_SPACER;
7632 use_spacer = string_to_bool(value);
7633 ERR("use_spacer should have an argument of left, right, or"
7634 " none. '%s' seems to be some form of '%s', so"
7635 " defaulting to %s.", value,
7636 use_spacer ? "true" : "false",
7637 use_spacer ? "right" : "none");
7639 use_spacer = RIGHT_SPACER;
7641 use_spacer = NO_SPACER;
7645 ERR("use_spacer should have an argument. Defaulting to right.");
7646 use_spacer = RIGHT_SPACER;
7652 use_xft = string_to_bool(value);
7656 set_first_font(value);
7662 if (value && font_count >= 0) {
7663 fonts[0].font_alpha = atof(value) * 65535.0;
7672 if (string_to_bool(value)) {
7673 ERR("Xft not enabled");
7677 /* xftfont silently ignored when no Xft */
7680 /* xftalpha is silently ignored when no Xft */
7685 set_first_font(value);
7695 gap_x = atoi(value);
7702 gap_y = atoi(value);
7708 CONF("mail_spool") {
7712 variable_substitute(value, buffer, 256);
7714 if (buffer[0] != '\0') {
7715 if (current_mail_spool) {
7716 free(current_mail_spool);
7718 current_mail_spool = strndup(buffer, text_buffer_size);
7725 CONF("minimum_size") {
7727 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
7729 if (sscanf(value, "%d", &minimum_width) != 1) {
7737 CONF("maximum_width") {
7739 if (sscanf(value, "%d", &maximum_width) != 1) {
7747 CONF("no_buffers") {
7748 no_buffers = string_to_bool(value);
7750 CONF("top_cpu_separate") {
7751 cpu_separate = string_to_bool(value);
7753 CONF("short_units") {
7754 short_units = string_to_bool(value);
7756 CONF("pad_percents") {
7757 pad_percents = atoi(value);
7761 CONF("own_window") {
7763 own_window = string_to_bool(value);
7768 CONF("own_window_class") {
7770 memset(window.class_name, 0, sizeof(window.class_name));
7771 strncpy(window.class_name, value,
7772 sizeof(window.class_name) - 1);
7777 CONF("own_window_title") {
7779 memset(window.title, 0, sizeof(window.title));
7780 strncpy(window.title, value, sizeof(window.title) - 1);
7785 CONF("own_window_transparent") {
7787 set_transparent = string_to_bool(value);
7792 CONF("own_window_colour") {
7794 background_colour = get_x11_color(value);
7796 ERR("Invalid colour for own_window_colour (try omitting the "
7797 "'#' for hex colours");
7800 CONF("own_window_hints") {
7802 char *p_hint, *p_save;
7803 char delim[] = ", ";
7805 /* tokenize the value into individual hints */
7806 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
7808 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
7809 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
7810 SET_HINT(window.hints, HINT_UNDECORATED);
7811 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
7812 SET_HINT(window.hints, HINT_BELOW);
7813 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
7814 SET_HINT(window.hints, HINT_ABOVE);
7815 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
7816 SET_HINT(window.hints, HINT_STICKY);
7817 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
7818 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
7819 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
7820 SET_HINT(window.hints, HINT_SKIP_PAGER);
7825 p_hint = strtok_r(NULL, delim, &p_save);
7826 } while (p_hint != NULL);
7832 CONF("own_window_type") {
7834 if (strncmp(value, "normal", 6) == EQUAL) {
7835 window.type = TYPE_NORMAL;
7836 } else if (strncmp(value, "desktop", 7) == EQUAL) {
7837 window.type = TYPE_DESKTOP;
7838 } else if (strncmp(value, "dock", 7) == EQUAL) {
7839 window.type = TYPE_DOCK;
7840 text_alignment = TOP_LEFT;
7841 } else if (strncmp(value, "override", 8) == EQUAL) {
7842 window.type = TYPE_OVERRIDE;
7851 CONF("stippled_borders") {
7853 stippled_borders = strtol(value, 0, 0);
7855 stippled_borders = 4;
7860 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
7864 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
7867 CONF("update_interval") {
7869 update_interval = strtod(value, 0);
7873 if (info.music_player_interval == 0) {
7874 // default to update_interval
7875 info.music_player_interval = update_interval;
7878 CONF("total_run_times") {
7880 total_run_times = strtod(value, 0);
7886 stuff_in_upper_case = string_to_bool(value);
7888 CONF("max_specials") {
7890 max_specials = atoi(value);
7895 CONF("max_user_text") {
7897 max_user_text = atoi(value);
7902 CONF("text_buffer_size") {
7904 text_buffer_size = atoi(value);
7905 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
7906 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
7907 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
7919 global_text = (char *) malloc(1);
7920 global_text[0] = '\0';
7923 unsigned int l = strlen(global_text);
7925 if (fgets(buf, 256, fp) == NULL) {
7928 global_text = (char *) realloc(global_text, l + strlen(buf) + 1);
7929 strcat(global_text, buf);
7931 if (strlen(global_text) > max_user_text) {
7936 if (strlen(global_text) < 1) {
7937 CRIT_ERR("no text supplied in configuration; exiting");
7939 global_text_lines = line + 1;
7942 #ifdef TCP_PORT_MONITOR
7943 CONF("max_port_monitor_connections") {
7945 if (!value || (sscanf(value, "%d", &max) != 1)) {
7946 /* an error. use default, warn and continue. */
7947 tcp_portmon_set_max_connections(0);
7949 } else if (tcp_portmon_set_max_connections(max)) {
7950 /* max is < 0, default has been set*/
7955 CONF("if_up_strictness") {
7957 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7958 ifup_strictness = IFUP_UP;
7959 } else if (strcasecmp(value, "up") == EQUAL) {
7960 ifup_strictness = IFUP_UP;
7961 } else if (strcasecmp(value, "link") == EQUAL) {
7962 ifup_strictness = IFUP_LINK;
7963 } else if (strcasecmp(value, "address") == EQUAL) {
7964 ifup_strictness = IFUP_ADDR;
7966 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7967 ifup_strictness = IFUP_UP;
7971 CONF("temperature_unit") {
7973 ERR("config option 'temperature_unit' needs an argument");
7974 } else if (set_temp_output_unit(value)) {
7975 ERR("temperature_unit: incorrect argument");
7980 ERR("%s: %d: no such configuration: '%s'", f, line, name);
7991 if (info.music_player_interval == 0) {
7992 // default to update_interval
7993 info.music_player_interval = update_interval;
7995 if (!global_text) { // didn't supply any text
7996 CRIT_ERR("missing text block in configuration; exiting");
8000 static void print_help(const char *prog_name) {
8001 printf("Usage: %s [OPTION]...\n"
8002 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8003 "window. Command line options will override configurations defined in config\n"
8005 " -v, --version version\n"
8006 " -q, --quiet quiet mode\n"
8007 " -D, --debug increase debugging output\n"
8008 " -c, --config=FILE config file to load\n"
8009 " -d, --daemonize daemonize, fork to background\n"
8010 " -h, --help help\n"
8012 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8013 " -f, --font=FONT font to use\n"
8015 " -o, --own-window create own window to draw\n"
8018 " -b, --double-buffer double buffer (prevents flickering)\n"
8020 " -w, --window-id=WIN_ID window id to draw\n"
8021 " -x X x position\n"
8022 " -y Y y position\n"
8024 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8025 " -u, --interval=SECS update interval\n"
8026 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8031 /* : means that character before that takes an argument */
8032 static const char *getopt_string = "vVqdDt:u:i:hc:"
8041 #ifdef CONFIG_OUTPUT
8047 static const struct option longopts[] = {
8048 { "help", 0, NULL, 'h' },
8049 { "version", 0, NULL, 'V' },
8050 { "debug", 0, NULL, 'D' },
8051 { "config", 1, NULL, 'c' },
8052 #ifdef CONFIG_OUTPUT
8053 { "print-config", 0, NULL, 'C' },
8055 { "daemonize", 0, NULL, 'd' },
8057 { "alignment", 1, NULL, 'a' },
8058 { "font", 1, NULL, 'f' },
8060 { "own-window", 0, NULL, 'o' },
8063 { "double-buffer", 0, NULL, 'b' },
8065 { "window-id", 1, NULL, 'w' },
8067 { "text", 1, NULL, 't' },
8068 { "interval", 0, NULL, 'u' },
8072 int main(int argc, char **argv)
8078 struct sigaction act, oact;
8080 g_signal_pending = 0;
8081 memset(&info, 0, sizeof(info));
8084 #ifdef TCP_PORT_MONITOR
8085 /* set default connection limit */
8086 tcp_portmon_set_max_connections(0);
8089 /* handle command line parameters that don't change configs */
8091 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8092 || ((s = getenv("LANG")) && *s)) {
8093 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8095 ERR("malloc failed");
8097 for (x = 0; x < strlen(s); x++) {
8098 temp[x] = tolower(s[x]);
8101 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8107 if (!setlocale(LC_CTYPE, "")) {
8108 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8112 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8123 if (current_config) {
8124 free(current_config);
8126 current_config = strndup(optarg, max_user_text);
8129 freopen("/dev/null", "w", stderr);
8132 print_help(argv[0]);
8134 #ifdef CONFIG_OUTPUT
8141 window.window = strtol(optarg, 0, 0);
8150 /* initalize X BEFORE we load config.
8151 * (we need to so that 'screen' is set) */
8155 /* check if specified config file is valid */
8156 if (current_config) {
8158 if (stat(current_config, &sb) ||
8159 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
8160 ERR("invalid configuration file '%s'\n", current_config);
8161 free(current_config);
8166 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8168 if (!current_config) {
8169 /* load default config file */
8173 /* Try to use personal config file first */
8174 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
8175 if (buf[0] && (fp = fopen(buf, "r"))) {
8176 current_config = strndup(buf, max_user_text);
8180 /* Try to use system config file if personal config not readable */
8181 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8182 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
8186 /* No readable config found */
8187 if (!current_config) {
8188 #ifdef CONFIG_OUTPUT
8189 current_config = strdup("==builtin==");
8190 ERR("no readable personal or system-wide config file found,"
8191 " using builtin default");
8193 CRIT_ERR("no readable personal or system-wide config file found");
8194 #endif /* ! CONF_OUTPUT */
8198 load_config_file(current_config);
8200 /* init specials array */
8201 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8202 ERR("failed to create specials array");
8206 if (current_mail_spool == NULL) {
8209 variable_substitute(MAIL_FILE, buf, 256);
8211 if (buf[0] != '\0') {
8212 current_mail_spool = strndup(buf, text_buffer_size);
8217 /* handle other command line arguments */
8219 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8220 || defined(__NetBSD__)
8221 optind = optreset = 1;
8226 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8227 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8228 "kvm_open")) == NULL) {
8229 CRIT_ERR("cannot read kvm");
8234 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8242 fork_to_background = 1;
8245 global_debug_level++;
8249 set_first_font(optarg);
8252 text_alignment = string_to_alignment(optarg);
8271 global_text = strndup(optarg, max_user_text);
8272 convert_escapes(global_text);
8276 update_interval = strtod(optarg, 0);
8277 if (info.music_player_interval == 0) {
8278 // default to update_interval
8279 info.music_player_interval = update_interval;
8284 total_run_times = strtod(optarg, 0);
8288 gap_x = atoi(optarg);
8292 gap_y = atoi(optarg);
8306 /* generate text and get initial size */
8307 extract_variable_text(global_text);
8314 if (fork_to_background) {
8319 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
8326 fprintf(stderr, "\n");
8331 /* parent process */
8332 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
8339 text_buffer = malloc(max_user_text);
8340 memset(text_buffer, 0, max_user_text);
8341 tmpstring1 = malloc(text_buffer_size);
8342 memset(tmpstring1, 0, text_buffer_size);
8343 tmpstring2 = malloc(text_buffer_size);
8344 memset(tmpstring2, 0, text_buffer_size);
8348 update_text_area(); /* to get initial size of the window */
8351 init_window(own_window, text_width + border_margin * 2 + 1,
8352 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8354 #else /* OWN_WINDOW */
8355 init_window(0, text_width + border_margin * 2 + 1,
8356 text_height + border_margin * 2 + 1, set_transparent, 0,
8358 #endif /* OWN_WINDOW */
8361 update_text_area(); /* to position text/window on screen */
8364 if (own_window && !fixed_pos) {
8365 XMoveWindow(display, window.window, window.x, window.y);
8368 set_transparent_background(window.window);
8378 /* Set signal handlers */
8379 act.sa_handler = signal_handler;
8380 sigemptyset(&act.sa_mask);
8383 act.sa_flags |= SA_RESTART;
8386 if ( sigaction(SIGINT, &act, &oact) < 0
8387 || sigaction(SIGUSR1, &act, &oact) < 0
8388 || sigaction(SIGHUP, &act, &oact) < 0
8389 || sigaction(SIGTERM, &act, &oact) < 0) {
8390 ERR("error setting signal handler: %s", strerror(errno));
8395 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8402 void signal_handler(int sig)
8404 /* signal handler is light as a feather, as it should be.
8405 * we will poll g_signal_pending with each loop of conky
8406 * and do any signal processing there, NOT here. */
8407 g_signal_pending = sig;