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/>.
29 #include "text_object.h"
44 #include <sys/param.h>
47 #include <X11/Xutil.h>
49 #include <X11/extensions/Xdamage.h>
55 #include <sys/types.h>
57 #include <netinet/in.h>
71 #include "temphelper.h"
74 /* check for OS and include appropriate headers */
75 #if defined(__linux__)
77 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
79 #elif defined(__OpenBSD__)
83 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
84 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
85 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
86 int apm_getinfo(int fd, apm_info_t aip);
87 char *get_apm_adapter(void);
88 char *get_apm_battery_life(void);
89 char *get_apm_battery_time(void);
97 #include "defconfig.h"
98 #ifdef HAVE_FOPENCOOKIE
99 #include "conf_cookie.h"
104 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
107 #define MAIL_FILE "$MAIL"
108 #define MAX_IF_BLOCK_DEPTH 5
109 #define MAX_TAIL_LINES 100
111 //#define SIGNAL_BLOCKING
112 #undef SIGNAL_BLOCKING
114 /* debugging level, used by logging.h */
115 int global_debug_level = 0;
117 /* two strings for internal use */
118 static char *tmpstring1, *tmpstring2;
120 /* variables holding various config settings */
128 int top_cpu, top_mem, top_time;
131 static int output_methods;
132 static volatile int g_signal_pending;
133 /* Update interval */
134 static double update_interval;
137 /* prototypes for internally used functions */
138 static void signal_handler(int);
139 static void print_version(void) __attribute__((noreturn));
140 static void reload_config(void);
141 static void clean_up(void);
142 static void generate_text_internal(char *, int, struct text_object,
143 struct information *);
144 static int extract_variable_text_internal(struct text_object *,
147 static void print_version(void)
149 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
151 printf("\nCompiled in features:\n\n"
152 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
156 " * Xdamage extension\n"
157 # endif /* HAVE_XDAMAGE */
159 " * Xdbe extension (double buffer)\n"
160 # endif /* HAVE_XDBE */
165 "\n Music detection:\n"
168 #endif /* AUDACIOUS */
181 "\n General features:\n"
188 #ifdef TCP_PORT_MONITOR
190 #endif /* TCP_PORT_MONITOR */
199 #endif /* HAVE_IWLIB */
211 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
218 static int text_start_x, text_start_y; /* text start position in window */
219 static int text_width, text_height;
237 char name[DEFAULT_TEXT_BUFFER_SIZE];
247 static int selected_font = 0;
248 static int font_count = -1;
249 struct font_list *fonts = NULL;
253 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
254 fonts[selected_font].xftfont->descent) \
255 : (fonts[selected_font].font->max_bounds.ascent + \
256 fonts[selected_font].font->max_bounds.descent))
257 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
258 : fonts[selected_font].font->max_bounds.ascent)
259 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
260 : fonts[selected_font].font->max_bounds.descent)
264 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
265 fonts[selected_font].font->max_bounds.descent)
266 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
267 #define font_descent() fonts[selected_font].font->max_bounds.descent
271 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
273 static void set_font(void);
275 int addfont(const char *data_in)
277 if (font_count > MAX_FONTS) {
278 CRIT_ERR("you don't need that many fonts, sorry.");
281 if (font_count == 0) {
285 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
289 memset(fonts, 0, sizeof(struct font_list));
291 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
292 memset(&fonts[font_count], 0, sizeof(struct font_list));
294 CRIT_ERR("realloc in addfont");
296 // must account for null terminator
297 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
298 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
300 fonts[font_count].font_alpha = 0xffff;
303 CRIT_ERR("Oops...looks like something overflowed in addfont().");
308 void set_first_font(const char *data_in)
310 if (font_count < 0) {
311 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
315 memset(fonts, 0, sizeof(struct font_list));
318 if (strlen(data_in) > 1) {
319 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
321 fonts[0].font_alpha = 0xffff;
326 void free_fonts(void)
330 for (i = 0; i <= font_count; i++) {
333 XftFontClose(display, fonts[i].xftfont);
334 fonts[i].xftfont = 0;
338 XFreeFont(display, fonts[i].font);
348 static void load_fonts(void)
352 for (i = 0; i <= font_count; i++) {
355 if (use_xft && fonts[i].xftfont) {
357 } else if (use_xft) {
358 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
359 XftFontClose(display, fonts[i].xftfont);
361 fonts[i].xftfont = XftFontOpenName(display, screen,
363 if (fonts[i].xftfont != NULL) {
367 ERR("can't load Xft font '%s'", fonts[i].name);
368 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
369 "courier-12")) != NULL) {
373 ERR("can't load Xft font '%s'", "courier-12");
375 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
376 CRIT_ERR("can't load font '%s'", "fixed");
383 /* load normal font */
384 /* if (fonts[i].font != NULL) {
385 XFreeFont(display, fonts[i].font);
388 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
389 ERR("can't load font '%s'", fonts[i].name);
390 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
391 CRIT_ERR("can't load font '%s'", "fixed");
392 printf("loaded fixed?\n");
400 /* struct that has all info to be shared between
401 * instances of the same text object */
402 struct information info;
404 /* default config file */
405 static char *current_config;
407 /* set to 1 if you want all text to be in uppercase */
408 static unsigned int stuff_in_upper_case;
410 /* Run how many times? */
411 static unsigned long total_run_times;
414 static int fork_to_background;
416 static int cpu_avg_samples, net_avg_samples;
420 static int show_graph_scale;
421 static int show_graph_range;
423 /* Position on the screen */
424 static int text_alignment;
425 static int gap_x, gap_y;
428 static int draw_borders;
429 static int draw_graph_borders;
430 static int stippled_borders;
432 static int draw_shades, draw_outline;
434 static int border_margin, border_width;
436 static long default_fg_color, default_bg_color, default_out_color;
438 /* create own window or draw stuff to root? */
439 static int set_transparent = 0;
442 static int own_window = 0;
443 static int background_colour = 0;
445 /* fixed size/pos is set if wm/user changes them */
446 static int fixed_size = 0, fixed_pos = 0;
449 static int minimum_width, minimum_height;
450 static int maximum_width;
455 static int sensor_device;
458 static long color0, color1, color2, color3, color4, color5, color6, color7,
461 static char *template[10];
463 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
464 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
466 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
467 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
469 /* maximum size of individual text buffers, ie $exec buffer size */
470 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
473 #define CODEPAGE_LENGTH 20
475 long iconv_count = 0;
476 char iconv_converting;
477 static iconv_t **iconv_cd = 0;
479 int register_iconv(iconv_t *new_iconv)
481 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
483 CRIT_ERR("Out of memory");
485 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
486 if (!iconv_cd[iconv_count]) {
487 CRIT_ERR("Out of memory");
489 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
494 void free_iconv(void)
499 for (i = 0; i < iconv_count; i++) {
501 iconv_close(*iconv_cd[i]);
515 /* no buffers in used memory? */
518 /* pad percentages to decimals? */
519 static int pad_percents = 0;
521 static char *global_text = 0;
522 long global_text_lines;
524 static int total_updates;
526 int check_contains(char *f, char *s)
529 FILE *where = fopen(f, "r");
532 char buf1[256], buf2[256];
534 while (fgets(buf1, 256, where)) {
535 sscanf(buf1, "%255s", buf2);
536 if (strstr(buf2, s)) {
543 ERR("Could not open the file");
549 static inline int calc_text_width(const char *s, int l)
556 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
557 (const FcChar8 *) s, l, &gi);
559 XftTextExtents8(display, fonts[selected_font].xftfont,
560 (const FcChar8 *) s, l, &gi);
566 return XTextWidth(fonts[selected_font].font, s, l);
571 /* formatted text to render on screen, generated in generate_text(),
572 * drawn in draw_stuff() */
574 static char *text_buffer;
576 /* special stuff in text_buffer */
578 #define SPECIAL_CHAR '\x01'
607 unsigned long first_colour; // for graph gradient
608 unsigned long last_colour;
612 /* create specials array on heap instead of stack with introduction of
614 static struct special_t *specials = NULL;
616 static unsigned int special_count;
619 static unsigned int special_index; /* used when drawing */
622 /* why 256? cause an array of more then 256 doubles seems excessive,
623 * and who needs that kind of precision anyway? */
624 #define MAX_GRAPH_DEPTH 256
626 static struct special_t *new_special(char *buf, enum special_types t)
628 if (special_count >= max_specials) {
629 CRIT_ERR("too many special things in text");
632 buf[0] = SPECIAL_CHAR;
634 specials[special_count].type = t;
635 return &specials[special_count++];
638 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
642 unsigned int count = 0;
643 static char buf[BUFSZ];
644 long orig_pos = ftell(fp);
646 long buf_size = BUFSZ;
647 char *cur_found = NULL;
649 while (count < step) {
650 if (cur_found == NULL) {
651 buf_size = fread(buf, 1, buf_size, fp);
654 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
655 if (cur_found != NULL) {
656 buf_pos = cur_found - buf + 1;
665 ret = ftell(fp) - buf_size + buf_pos - 1;
667 fseek(fp, orig_pos, SEEK_SET);
672 void *memrchr(const void *buffer, char c, size_t n)
674 const unsigned char *p = buffer;
676 for (p += n; n; n--) {
685 long rev_fcharfind(FILE *fp, char val, unsigned int step)
689 unsigned int count = 0;
690 static char buf[BUFSZ];
691 long orig_pos = ftell(fp);
693 long file_pos = orig_pos;
694 long buf_size = BUFSZ;
697 while (count < step) {
699 if (file_pos > BUFSZ) {
700 fseek(fp, file_pos - BUFSZ, SEEK_SET);
703 fseek(fp, 0, SEEK_SET);
705 file_pos = ftell(fp);
706 buf_pos = fread(buf, 1, buf_size, fp);
708 cur_found = memrchr(buf, val, (size_t) buf_pos);
709 if (cur_found != NULL) {
710 buf_pos = cur_found - buf;
719 fseek(fp, orig_pos, SEEK_SET);
721 ret = file_pos + buf_pos;
726 static void new_bar(char *buf, int w, int h, int usage)
728 struct special_t *s = new_special(buf, BAR);
730 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
735 static const char *scan_bar(const char *args, int *w, int *h)
737 /* zero width means all space that is available */
740 /* bar's argument is either height or height,width */
744 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
745 sscanf(args, "%d %n", h, &n);
753 static char *scan_font(const char *args)
756 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
763 static void new_font(char *buf, char *args)
766 struct special_t *s = new_special(buf, FONT);
768 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
769 int tmp = selected_font;
771 selected_font = s->font_added = addfont(args);
776 struct special_t *s = new_special(buf, FONT);
777 int tmp = selected_font;
779 selected_font = s->font_added = 0;
784 void graph_append(struct special_t *graph, double f, char showaslog)
794 if (!graph->scaled && f > graph->graph_scale) {
795 f = graph->graph_scale;
798 /* Already happens in new_graph
800 graph->graph_scale = 1;
803 graph->graph[0] = f; /* add new data */
804 /* shift all the data by 1 */
805 for (i = graph->graph_width - 1; i > 0; i--) {
806 graph->graph[i] = graph->graph[i - 1];
807 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
808 /* check if we need to update the scale */
809 graph->graph_scale = graph->graph[i];
814 short colour_depth = 0;
815 void set_up_gradient(void);
817 /* precalculated: 31/255, and 63/255 */
818 #define CONST_8_TO_5_BITS 0.12156862745098
819 #define CONST_8_TO_6_BITS 0.247058823529412
821 /* adjust color values depending on color depth */
822 static unsigned int adjust_colors(unsigned int color)
826 if (colour_depth == 0) {
829 if (colour_depth == 16) {
830 r = (color & 0xff0000) >> 16;
831 g = (color & 0xff00) >> 8;
833 color = (int) (r * CONST_8_TO_5_BITS) << 11;
834 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
835 color |= (int) (b * CONST_8_TO_5_BITS);
840 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
841 unsigned int second_colour, double i, int scale, int append, char showaslog)
843 struct special_t *s = new_special(buf, GRAPH);
846 if (s->graph == NULL) {
847 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
848 // subtract 2 for the box
849 s->graph_width = s->width /* - 2 */;
851 s->graph_width = MAX_GRAPH_DEPTH - 2;
853 s->graph = malloc(s->graph_width * sizeof(double));
854 memset(s->graph, 0, s->graph_width * sizeof(double));
855 s->graph_scale = 100;
858 s->first_colour = adjust_colors(first_colour);
859 s->last_colour = adjust_colors(second_colour);
862 s->graph_scale = scale;
870 s->graph_width = s->width - 2; // subtract 2 for rectangle around
874 s->graph_scale = log10(s->graph_scale + 1);
878 graph_append(s, i, showaslog);
882 //don't use spaces in LOGGRAPH or NORMGRAPH if you change them
883 #define LOGGRAPH "log"
884 #define NORMGRAPH "normal"
886 static char *scan_graph(const char *args, int *w, int *h,
887 unsigned int *first_colour, unsigned int *last_colour,
888 unsigned int *scale, char *showaslog)
890 const char *nographtype;
894 /* zero width means all space that is available */
901 //set showaslog and place the rest of the args in nographtype
902 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
905 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
908 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
910 nographtype = &args[strlen(LOGGRAPH) + 1];
911 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
913 nographtype = &args[strlen(NORMGRAPH) + 1];
918 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
919 //check the rest of the args
920 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
924 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
927 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
928 return strndup(buf, text_buffer_size);
931 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
932 return strndup(buf, text_buffer_size);
937 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
941 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
944 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
945 return strndup(buf, text_buffer_size);
948 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
949 return strndup(buf, text_buffer_size);
954 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
958 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
961 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
963 //TODO: check the return value and throw an error?
964 sscanf(nographtype, "%63s %d,%d", buf, h, w);
967 return strndup(buf, text_buffer_size);
970 if (buf[0] == '\0') {
973 return strndup(buf, text_buffer_size);
977 static inline void new_hr(char *buf, int a)
979 new_special(buf, HORIZONTAL_LINE)->height = a;
982 static inline void new_stippled_hr(char *buf, int a, int b)
984 struct special_t *s = new_special(buf, STIPPLED_HR);
990 static inline void new_fg(char *buf, long c)
992 new_special(buf, FG)->arg = c;
995 static inline void new_bg(char *buf, long c)
997 new_special(buf, BG)->arg = c;
1000 static inline void new_outline(char *buf, long c)
1002 new_special(buf, OUTLINE)->arg = c;
1005 static inline void new_offset(char *buf, long c)
1007 new_special(buf, OFFSET)->arg = c;
1010 static inline void new_voffset(char *buf, long c)
1012 new_special(buf, VOFFSET)->arg = c;
1015 static inline void new_alignr(char *buf, long c)
1017 new_special(buf, ALIGNR)->arg = c;
1020 // A postive offset pushes the text further left
1021 static inline void new_alignc(char *buf, long c)
1023 new_special(buf, ALIGNC)->arg = c;
1026 static inline void new_goto(char *buf, long c)
1028 new_special(buf, GOTO)->arg = c;
1031 static inline void new_tab(char *buf, int a, int b)
1033 struct special_t *s = new_special(buf, TAB);
1039 /* quite boring functions */
1041 static inline void for_each_line(char *b, void f(char *))
1045 for (ps = b, pe = b; *pe; pe++) {
1059 static void convert_escapes(char *buf)
1061 char *p = buf, *s = buf;
1068 } else if (*s == '\\') {
1079 /* Prints anything normally printed with snprintf according to the current value
1080 * of use_spacer. Actually slightly more flexible than snprintf, as you can
1081 * safely specify the destination buffer as one of your inputs. */
1082 static int spaced_print(char *, int, const char *, int, ...)
1083 __attribute__((format(printf, 3, 5)));
1085 static int spaced_print(char *buf, int size, const char *format, int width, ...) {
1093 tempbuf = malloc(size * sizeof(char));
1095 // Passes the varargs along to vsnprintf
1096 va_start(argp, width);
1097 vsnprintf(tempbuf, size, format, argp);
1100 switch (use_spacer) {
1102 len = snprintf(buf, size, "%s", tempbuf);
1105 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
1108 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
1115 /* converts from bytes to human readable format (k, M, G, T) */
1116 static void human_readable(long long num, char *buf, int size)
1118 const char **suffix = suffixes;
1121 static const int WIDTH = 10, SHORT_WIDTH = 8;
1123 const char *format, *format2;
1126 width = SHORT_WIDTH;
1128 format2 = "%.*f%1s";
1135 if (llabs(num) < 1024LL) {
1136 spaced_print(buf, size, format, width, num, *suffix);
1140 while (llabs(num / 1024) >= 1024LL && **(suffix + 2)) {
1146 fnum = num / 1024.0;
1151 if (precision < 0) {
1154 len = spaced_print(buf, size, format2, width,
1155 precision, fnum, *suffix);
1156 } while (len >= MAX(width, size));
1159 /* global object list root element */
1160 static struct text_object global_root_object;
1162 static inline void read_exec(const char *data, char *buf, const int size)
1164 FILE *fp = popen(data, "r");
1165 int length = fread(buf, 1, size, fp);
1169 if (length > 0 && buf[length - 1] == '\n') {
1170 buf[length - 1] = '\0';
1174 void *threaded_exec(void *) __attribute__((noreturn));
1176 void *threaded_exec(void *arg)
1179 struct text_object *obj = (struct text_object *)arg;
1182 p2 = obj->data.texeci.buffer;
1183 timed_thread_lock(obj->data.texeci.p_timed_thread);
1184 read_exec(obj->data.texeci.cmd, obj->data.texeci.buffer,
1187 if (*p2 == '\001') {
1192 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1193 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
1194 timed_thread_exit(obj->data.texeci.p_timed_thread);
1200 static struct text_object *new_text_object_internal(void)
1202 struct text_object *obj = malloc(sizeof(struct text_object));
1203 memset(obj, 0, sizeof(struct text_object));
1207 /* free the list of text objects root points to */
1208 static void free_text_objects(struct text_object *root)
1210 struct text_object *obj;
1216 #define data obj->data
1217 for (obj = root->prev; obj; obj = root->prev) {
1218 root->prev = obj->prev;
1219 switch (obj->type) {
1227 close(data.sysfs.fd);
1229 #endif /* !__OpenBSD__ */
1235 free(data.tztime.tz);
1236 free(data.tztime.fmt);
1239 free(data.mboxscan.args);
1240 free(data.mboxscan.output);
1244 free(data.local_mail.box);
1249 case OBJ_imap_unseen:
1250 if (!obj->global_mode) {
1254 case OBJ_imap_messages:
1255 if (!obj->global_mode) {
1262 case OBJ_pop3_unseen:
1263 if (!obj->global_mode) {
1268 if (!obj->global_mode) {
1274 free_text_objects(obj->sub);
1277 case OBJ_if_existing:
1278 case OBJ_if_mounted:
1279 case OBJ_if_running:
1280 free(data.ifblock.s);
1281 free(data.ifblock.str);
1284 free(data.tail.logfile);
1285 free(data.tail.buffer);
1297 case OBJ_iconv_start:
1302 case OBJ_disk_protect:
1306 free(obj->data.ifblock.s);
1307 free(obj->data.ifblock.str);
1310 free(obj->data.ifblock.s);
1311 free(obj->data.ifblock.str);
1314 if (info.gw_info.iface) {
1315 free(info.gw_info.iface);
1316 info.gw_info.iface = 0;
1318 if (info.gw_info.ip) {
1319 free(info.gw_info.ip);
1320 info.gw_info.ip = 0;
1323 case OBJ_ioscheduler:
1329 case OBJ_xmms2_artist:
1330 if (info.xmms2.artist) {
1331 free(info.xmms2.artist);
1332 info.xmms2.artist = 0;
1335 case OBJ_xmms2_album:
1336 if (info.xmms2.album) {
1337 free(info.xmms2.album);
1338 info.xmms2.album = 0;
1341 case OBJ_xmms2_title:
1342 if (info.xmms2.title) {
1343 free(info.xmms2.title);
1344 info.xmms2.title = 0;
1347 case OBJ_xmms2_genre:
1348 if (info.xmms2.genre) {
1349 free(info.xmms2.genre);
1350 info.xmms2.genre = 0;
1353 case OBJ_xmms2_comment:
1354 if (info.xmms2.comment) {
1355 free(info.xmms2.comment);
1356 info.xmms2.comment = 0;
1360 if (info.xmms2.url) {
1361 free(info.xmms2.url);
1365 case OBJ_xmms2_date:
1366 if (info.xmms2.date) {
1367 free(info.xmms2.date);
1368 info.xmms2.date = 0;
1371 case OBJ_xmms2_status:
1372 if (info.xmms2.status) {
1373 free(info.xmms2.status);
1374 info.xmms2.status = 0;
1377 case OBJ_xmms2_playlist:
1378 if (info.xmms2.playlist) {
1379 free(info.xmms2.playlist);
1380 info.xmms2.playlist = 0;
1383 case OBJ_xmms2_smart:
1384 if (info.xmms2.artist) {
1385 free(info.xmms2.artist);
1386 info.xmms2.artist = 0;
1388 if (info.xmms2.title) {
1389 free(info.xmms2.title);
1390 info.xmms2.title = 0;
1392 if (info.xmms2.url) {
1393 free(info.xmms2.url);
1399 case OBJ_bmpx_title:
1400 case OBJ_bmpx_artist:
1401 case OBJ_bmpx_album:
1402 case OBJ_bmpx_track:
1404 case OBJ_bmpx_bitrate:
1414 free(data.rss.action);
1423 case OBJ_battery_time:
1426 #endif /* !__OpenBSD__ */
1430 case OBJ_execigraph:
1431 free(data.execi.cmd);
1432 free(data.execi.buffer);
1435 free(data.texeci.cmd);
1436 free(data.texeci.buffer);
1438 case OBJ_nameserver:
1444 if (info.first_process) {
1445 free_all_processes();
1446 info.first_process = NULL;
1451 free(data.hddtemp.dev);
1452 free(data.hddtemp.addr);
1453 if (data.hddtemp.temp)
1454 free(data.hddtemp.temp);
1457 case OBJ_entropy_avail:
1458 case OBJ_entropy_poolsize:
1459 case OBJ_entropy_bar:
1461 case OBJ_user_names:
1462 if (info.users.names) {
1463 free(info.users.names);
1464 info.users.names = 0;
1467 case OBJ_user_terms:
1468 if (info.users.terms) {
1469 free(info.users.terms);
1470 info.users.terms = 0;
1473 case OBJ_user_times:
1474 if (info.users.times) {
1475 free(info.users.times);
1476 info.users.times = 0;
1481 case OBJ_smapi_bat_perc:
1482 case OBJ_smapi_bat_temp:
1483 case OBJ_smapi_bat_power:
1486 case OBJ_if_smapi_bat_installed:
1487 free(data.ifblock.s);
1488 free(data.ifblock.str);
1497 case OBJ_mpd_artist:
1499 case OBJ_mpd_random:
1500 case OBJ_mpd_repeat:
1502 case OBJ_mpd_bitrate:
1503 case OBJ_mpd_status:
1506 case OBJ_mpd_password:
1508 case OBJ_mpd_elapsed:
1509 case OBJ_mpd_length:
1513 case OBJ_mpd_percent:
1515 case OBJ_if_mpd_playing:
1523 case OBJ_moc_artist:
1526 case OBJ_moc_totaltime:
1527 case OBJ_moc_timeleft:
1528 case OBJ_moc_curtime:
1529 case OBJ_moc_bitrate:
1535 free(data.scroll.text);
1536 free_text_objects(obj->sub);
1545 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1550 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1551 *a = mixer_init(buf1);
1552 scan_bar(arg + n, w, h);
1554 *a = mixer_init(NULL);
1555 scan_bar(arg, w, h);
1559 /* strip a leading /dev/ if any, following symlinks first
1561 * BEWARE: this function returns a pointer to static content
1562 * which gets overwritten in consecutive calls. I.e.:
1563 * this function is NOT reentrant.
1565 static const char *dev_name(const char *path)
1567 static char buf[255]; /* should be enough for pathnames */
1573 #define DEV_NAME(x) \
1574 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1575 if ((buflen = readlink(path, buf, 254)) == -1)
1576 return DEV_NAME(path);
1578 return DEV_NAME(buf);
1582 /* construct_text_object() creates a new text_object */
1583 static struct text_object *construct_text_object(const char *s,
1584 const char *arg, long line, char allow_threaded, void **ifblock_opaque)
1586 // struct text_object *obj = new_text_object();
1587 struct text_object *obj = new_text_object_internal();
1591 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1592 obj->type = OBJ_##a; need_mask |= (1 << n); {
1593 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1594 obj->type = OBJ_##a; need_mask |= (1 << n); \
1595 obj_be_ifblock_if(ifblock_opaque, obj); {
1596 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1597 obj->type = OBJ_##a; need_mask |= (1 << n); {
1598 #define END } } else
1602 obj->type = OBJ_color;
1603 obj->data.l = get_x11_color(s);
1607 OBJ(freq, INFO_FREQ)
1610 obj->data.i = open_acpi_temperature(arg);
1611 END OBJ(acpiacadapter, 0)
1612 END OBJ(freq, INFO_FREQ)
1613 #endif /* !__OpenBSD__ */
1615 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1616 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1617 obj->data.cpu_index = 1;
1618 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1619 "Displaying the clock for CPU 1."); */
1621 obj->data.cpu_index = atoi(&arg[0]);
1624 END OBJ(freq_g, INFO_FREQ)
1626 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1627 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1628 obj->data.cpu_index = 1;
1629 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1630 "CPUs! Displaying the clock for CPU 1."); */
1632 obj->data.cpu_index = atoi(&arg[0]);
1635 #if defined(__linux__)
1636 END OBJ(voltage_mv, 0)
1638 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1639 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1640 obj->data.cpu_index = 1;
1641 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1642 "CPUs! Displaying voltage for CPU 1."); */
1644 obj->data.cpu_index = atoi(&arg[0]);
1647 END OBJ(voltage_v, 0)
1649 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1650 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1651 obj->data.cpu_index = 1;
1652 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1653 "CPUs! Displaying voltage for CPU 1."); */
1655 obj->data.cpu_index = atoi(&arg[0]);
1660 END OBJ(wireless_essid, INFO_NET)
1662 obj->data.net = get_net_stat(arg);
1664 CRIT_ERR("wireless_essid: needs an argument");
1666 END OBJ(wireless_mode, INFO_NET)
1668 obj->data.net = get_net_stat(arg);
1670 CRIT_ERR("wireless_mode: needs an argument");
1672 END OBJ(wireless_bitrate, INFO_NET)
1674 obj->data.net = get_net_stat(arg);
1676 CRIT_ERR("wireless_bitrate: needs an argument");
1678 END OBJ(wireless_ap, INFO_NET)
1680 obj->data.net = get_net_stat(arg);
1682 CRIT_ERR("wireless_ap: needs an argument");
1684 END OBJ(wireless_link_qual, INFO_NET)
1686 obj->data.net = get_net_stat(arg);
1688 CRIT_ERR("wireless_link_qual: needs an argument");
1690 END OBJ(wireless_link_qual_max, INFO_NET)
1692 obj->data.net = get_net_stat(arg);
1694 CRIT_ERR("wireless_link_qual_max: needs an argument");
1696 END OBJ(wireless_link_qual_perc, INFO_NET)
1698 obj->data.net = get_net_stat(arg);
1700 CRIT_ERR("wireless_link_qual_perc: needs an argument");
1702 END OBJ(wireless_link_bar, INFO_NET)
1704 arg = scan_bar(arg, &obj->a, &obj->b);
1705 obj->data.net = get_net_stat(arg);
1707 CRIT_ERR("wireless_link_bar: needs an argument");
1709 #endif /* HAVE_IWLIB */
1711 #endif /* __linux__ */
1719 sscanf(arg, "%63s", bat);
1721 strcpy(bat, "BAT0");
1723 obj->data.s = strndup(bat, text_buffer_size);
1724 END OBJ(battery_time, 0)
1728 sscanf(arg, "%63s", bat);
1730 strcpy(bat, "BAT0");
1732 obj->data.s = strndup(bat, text_buffer_size);
1733 END OBJ(battery_percent, 0)
1737 sscanf(arg, "%63s", bat);
1739 strcpy(bat, "BAT0");
1741 obj->data.s = strndup(bat, text_buffer_size);
1742 END OBJ(battery_bar, 0)
1746 arg = scan_bar(arg, &obj->a, &obj->b);
1747 sscanf(arg, "%63s", bat);
1749 strcpy(bat, "BAT0");
1751 obj->data.s = strndup(bat, text_buffer_size);
1752 #endif /* !__OpenBSD__ */
1754 #if defined(__linux__)
1755 END OBJ(disk_protect, 0)
1757 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1759 CRIT_ERR("disk_protect needs an argument");
1760 END OBJ(i8k_version, INFO_I8K)
1761 END OBJ(i8k_bios, INFO_I8K)
1762 END OBJ(i8k_serial, INFO_I8K)
1763 END OBJ(i8k_cpu_temp, INFO_I8K)
1764 END OBJ(i8k_left_fan_status, INFO_I8K)
1765 END OBJ(i8k_right_fan_status, INFO_I8K)
1766 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1767 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1768 END OBJ(i8k_ac_status, INFO_I8K)
1769 END OBJ(i8k_buttons_status, INFO_I8K)
1771 END OBJ(ibm_temps, 0)
1773 CRIT_ERR("ibm_temps: needs an argument");
1775 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1776 obj->data.sensor = 0;
1777 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1778 "Using 0 (CPU temp sensor).");
1780 obj->data.sensor = atoi(&arg[0]);
1781 END OBJ(ibm_volume, 0)
1782 END OBJ(ibm_brightness, 0)
1783 END OBJ_IF(if_up, 0)
1785 ERR("if_up needs an argument");
1786 obj->data.ifblock.s = 0;
1788 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1789 END OBJ_IF(if_gw, 0)
1790 END OBJ(ioscheduler, 0)
1792 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1795 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1796 END OBJ(laptop_mode, 0)
1797 END OBJ(pb_battery, 0)
1798 if (arg && strcmp(arg, "status") == EQUAL) {
1799 obj->data.i = PB_BATT_STATUS;
1800 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1801 obj->data.i = PB_BATT_PERCENT;
1802 } else if (arg && strcmp(arg, "time") == EQUAL) {
1803 obj->data.i = PB_BATT_TIME;
1805 ERR("pb_battery: needs one argument: status, percent or time");
1810 #endif /* __linux__ */
1811 #if defined(__OpenBSD__)
1812 END OBJ(obsd_sensors_temp, 0)
1814 CRIT_ERR("obsd_sensors_temp: needs an argument");
1816 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1817 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1818 obj->data.sensor = 0;
1819 ERR("Invalid temperature sensor number!");
1821 obj->data.sensor = atoi(&arg[0]);
1822 END OBJ(obsd_sensors_fan, 0)
1824 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1827 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1828 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1829 obj->data.sensor = 0;
1830 ERR("Invalid fan sensor number!");
1832 obj->data.sensor = atoi(&arg[0]);
1833 END OBJ(obsd_sensors_volt, 0)
1835 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1838 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1839 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1840 obj->data.sensor = 0;
1841 ERR("Invalid voltage sensor number!");
1843 obj->data.sensor = atoi(&arg[0]);
1844 END OBJ(obsd_vendor, 0)
1845 END OBJ(obsd_product, 0)
1846 #endif /* __OpenBSD__ */
1847 END OBJ(buffers, INFO_BUFFERS)
1848 END OBJ(cached, INFO_BUFFERS)
1849 END OBJ(cpu, INFO_CPU)
1851 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
1852 obj->data.cpu_index = atoi(&arg[3]);
1855 obj->data.cpu_index = 0;
1857 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1859 obj->data.cpu_index = 0;
1861 END OBJ(cpubar, INFO_CPU)
1863 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
1864 obj->data.cpu_index = atoi(&arg[3]);
1867 obj->data.cpu_index = 0;
1869 scan_bar(arg, &obj->a, &obj->b);
1871 scan_bar(arg, &obj->a, &obj->b);
1872 obj->data.cpu_index = 0;
1874 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1875 END OBJ(cpugraph, INFO_CPU)
1876 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1877 &obj->e, &obj->showaslog);
1880 if (strncmp(buf, "cpu", 3) == EQUAL && isdigit(buf[3])) {
1881 obj->data.cpu_index = atoi(&buf[3]);
1883 obj->data.cpu_index = 0;
1887 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1888 END OBJ(loadgraph, INFO_LOADAVG)
1889 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1890 &obj->e, &obj->showaslog);
1894 r = sscanf(arg, "%d", &a);
1896 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1899 #if defined(__linux__)
1900 END OBJ(diskio, INFO_DISKIO)
1901 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1902 END OBJ(diskio_read, INFO_DISKIO)
1903 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1904 END OBJ(diskio_write, INFO_DISKIO)
1905 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1906 END OBJ(diskiograph, INFO_DISKIO)
1907 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1908 &obj->e, &obj->showaslog);
1910 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1913 END OBJ(diskiograph_read, INFO_DISKIO)
1914 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1915 &obj->e, &obj->showaslog);
1917 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1920 END OBJ(diskiograph_write, INFO_DISKIO)
1921 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1922 &obj->e, &obj->showaslog);
1924 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1930 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1933 obj->data.l = color0;
1935 obj->data.l = color1;
1937 obj->data.l = color2;
1939 obj->data.l = color3;
1941 obj->data.l = color4;
1943 obj->data.l = color5;
1945 obj->data.l = color6;
1947 obj->data.l = color7;
1949 obj->data.l = color8;
1951 obj->data.l = color9;
1953 obj->data.s = scan_font(arg);
1954 END OBJ(conky_version, 0)
1955 END OBJ(conky_build_date, 0)
1956 END OBJ(conky_build_arch, 0)
1957 END OBJ(downspeed, INFO_NET)
1959 obj->data.net = get_net_stat(arg);
1961 CRIT_ERR("downspeed needs argument");
1963 END OBJ(downspeedf, INFO_NET)
1965 obj->data.net = get_net_stat(arg);
1967 CRIT_ERR("downspeedf needs argument");
1969 END OBJ(downspeedgraph, INFO_NET)
1970 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1971 &obj->e, &obj->showaslog);
1973 // default to DEFAULTNETDEV
1974 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
1975 obj->data.net = get_net_stat(buf);
1978 obj_be_ifblock_else(ifblock_opaque, obj);
1980 obj_be_ifblock_endif(ifblock_opaque, obj);
1982 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1985 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1987 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1989 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1990 END OBJ(execgraph, 0)
1991 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1992 END OBJ(execibar, 0)
1995 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1998 ERR("${execibar <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);
2005 END OBJ(execigraph, 0)
2008 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2011 ERR("${execigraph <interval> command}");
2012 obj->type = OBJ_text;
2013 snprintf(buf, 256, "${%s}", s);
2014 obj->data.s = strndup(buf, text_buffer_size);
2016 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2021 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2024 ERR("${execi <interval> command}");
2025 obj->type = OBJ_text;
2026 snprintf(buf, 256, "${%s}", s);
2027 obj->data.s = strndup(buf, text_buffer_size);
2029 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2030 obj->data.execi.buffer = malloc(text_buffer_size);
2035 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2038 ERR("${execi <interval> command}");
2039 obj->type = OBJ_text;
2040 snprintf(buf, 256, "${%s}", s);
2041 obj->data.s = strndup(buf, text_buffer_size);
2043 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2044 obj->data.execi.buffer = malloc(text_buffer_size);
2046 END OBJ_THREAD(texeci, 0)
2049 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2052 ERR("${texeci <interval> command}");
2053 obj->type = OBJ_text;
2054 snprintf(buf, 256, "${%s}", s);
2055 obj->data.s = strndup(buf, text_buffer_size);
2057 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
2058 obj->data.texeci.buffer = malloc(text_buffer_size);
2060 obj->data.texeci.p_timed_thread = NULL;
2061 END OBJ(pre_exec, 0)
2062 obj->type = OBJ_text;
2066 read_exec(arg, buf, sizeof(buf));
2067 obj->data.s = strndup(buf, text_buffer_size);
2069 obj->data.s = strndup("", text_buffer_size);
2072 END OBJ(fs_bar, INFO_FS)
2073 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2075 while (isspace(*arg)) {
2084 obj->data.fsbar.fs = prepare_fs_stat(arg);
2085 END OBJ(fs_bar_free, INFO_FS)
2086 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2088 while (isspace(*arg)) {
2098 obj->data.fsbar.fs = prepare_fs_stat(arg);
2099 END OBJ(fs_free, INFO_FS)
2103 obj->data.fs = prepare_fs_stat(arg);
2104 END OBJ(fs_used_perc, INFO_FS)
2108 obj->data.fs = prepare_fs_stat(arg);
2109 END OBJ(fs_free_perc, INFO_FS)
2113 obj->data.fs = prepare_fs_stat(arg);
2114 END OBJ(fs_size, INFO_FS)
2118 obj->data.fs = prepare_fs_stat(arg);
2119 END OBJ(fs_type, INFO_FS)
2123 obj->data.fs = prepare_fs_stat(arg);
2124 END OBJ(fs_used, INFO_FS)
2128 obj->data.fs = prepare_fs_stat(arg);
2130 obj->data.i = arg ? atoi(arg) : 1;
2131 END OBJ(nameserver, INFO_DNS)
2132 obj->data.i = arg ? atoi(arg) : 0;
2134 obj->data.i = arg ? atoi(arg) : 1;
2136 obj->data.i = arg ? atoi(arg) : 1;
2140 ERR("goto needs arguments");
2141 obj->type = OBJ_text;
2142 obj->data.s = strndup("${goto}", text_buffer_size);
2146 obj->data.i = atoi(arg);
2152 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2153 sscanf(arg, "%d", &b);
2159 obj->data.pair.a = a;
2160 obj->data.pair.b = b;
2163 END OBJ(i2c, INFO_SYSFS)
2164 char buf1[64], buf2[64];
2168 ERR("i2c needs arguments");
2169 obj->type = OBJ_text;
2170 // obj->data.s = strndup("${i2c}", text_buffer_size);
2174 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2175 /* if scanf couldn't read three values, read type and num and use
2177 sscanf(arg, "%63s %d", buf2, &n);
2178 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
2179 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2180 strncpy(obj->data.sysfs.type, buf2, 63);
2182 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
2183 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2184 strncpy(obj->data.sysfs.type, buf2, 63);
2187 END OBJ(platform, INFO_SYSFS)
2188 char buf1[64], buf2[64];
2192 ERR("platform needs arguments");
2193 obj->type = OBJ_text;
2197 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2198 /* if scanf couldn't read three values, read type and num and use
2200 sscanf(arg, "%63s %d", buf2, &n);
2201 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
2202 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2203 strncpy(obj->data.sysfs.type, buf2, 63);
2205 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
2206 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2207 strncpy(obj->data.sysfs.type, buf2, 63);
2210 END OBJ(hwmon, INFO_SYSFS)
2211 char buf1[64], buf2[64];
2215 ERR("hwmon needs argumanets");
2216 obj->type = OBJ_text;
2220 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2221 /* if scanf couldn't read three values, read type and num and use
2223 sscanf(arg, "%63s %d", buf2, &n);
2224 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
2225 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2226 strncpy(obj->data.sysfs.type, buf2, 63);
2228 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
2229 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2230 strncpy(obj->data.sysfs.type, buf2, 63);
2232 #endif /* !__OpenBSD__ */
2234 END OBJ(top, INFO_TOP)
2239 ERR("top needs arguments");
2240 obj->type = OBJ_text;
2241 // obj->data.s = strndup("${top}", 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(top_mem, INFO_TOP)
2279 ERR("top_mem needs arguments");
2280 obj->type = OBJ_text;
2281 obj->data.s = strndup("${top_mem}", text_buffer_size);
2284 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2285 if (strcmp(buf, "name") == EQUAL) {
2286 obj->data.top.type = TOP_NAME;
2287 } else if (strcmp(buf, "cpu") == EQUAL) {
2288 obj->data.top.type = TOP_CPU;
2289 } else if (strcmp(buf, "pid") == EQUAL) {
2290 obj->data.top.type = TOP_PID;
2291 } else if (strcmp(buf, "mem") == EQUAL) {
2292 obj->data.top.type = TOP_MEM;
2293 } else if (strcmp(buf, "time") == EQUAL) {
2294 obj->data.top.type = TOP_TIME;
2295 } else if (strcmp(buf, "mem_res") == EQUAL) {
2296 obj->data.top.type = TOP_MEM_RES;
2297 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
2298 obj->data.top.type = TOP_MEM_VSIZE;
2300 ERR("invalid arg for top");
2303 if (n < 1 || n > 10) {
2304 CRIT_ERR("invalid arg for top");
2307 obj->data.top.num = n - 1;
2311 ERR("invalid args given for top");
2314 END OBJ(top_time, INFO_TOP)
2319 ERR("top_time needs arguments");
2320 obj->type = OBJ_text;
2321 obj->data.s = strndup("${top_time}", text_buffer_size);
2324 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2325 if (strcmp(buf, "name") == EQUAL) {
2326 obj->data.top.type = TOP_NAME;
2327 } else if (strcmp(buf, "cpu") == EQUAL) {
2328 obj->data.top.type = TOP_CPU;
2329 } else if (strcmp(buf, "pid") == EQUAL) {
2330 obj->data.top.type = TOP_PID;
2331 } else if (strcmp(buf, "mem") == EQUAL) {
2332 obj->data.top.type = TOP_MEM;
2333 } else if (strcmp(buf, "time") == EQUAL) {
2334 obj->data.top.type = TOP_TIME;
2335 } else if (strcmp(buf, "mem_res") == EQUAL) {
2336 obj->data.top.type = TOP_MEM_RES;
2337 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
2338 obj->data.top.type = TOP_MEM_VSIZE;
2340 ERR("invalid arg for top");
2343 if (n < 1 || n > 10) {
2344 CRIT_ERR("invalid arg for top");
2347 obj->data.top.num = n - 1;
2351 ERR("invalid args given for top");
2354 END OBJ(addr, INFO_NET)
2356 obj->data.net = get_net_stat(arg);
2358 CRIT_ERR("addr needs argument");
2360 #if defined(__linux__)
2361 END OBJ(addrs, INFO_NET)
2363 obj->data.net = get_net_stat(arg);
2365 CRIT_ERR("addrs needs argument");
2367 #endif /* __linux__ */
2374 ERR("tail needs arguments");
2375 obj->type = OBJ_text;
2376 obj->data.s = strndup("${tail}", text_buffer_size);
2379 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2380 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2381 CRIT_ERR("invalid arg for tail, number of lines must be "
2382 "between 1 and %i", MAX_TAIL_LINES);
2388 obj->data.tail.fd = -1;
2390 if (stat(buf, &st) == 0) {
2391 if (S_ISFIFO(st.st_mode)) {
2392 fd = open(buf, O_RDONLY | O_NONBLOCK);
2395 CRIT_ERR("tail logfile does not exist, or you do "
2396 "not have correct permissions");
2399 obj->data.tail.fd = fd;
2401 fp = fopen(buf, "r");
2405 if (fp || obj->data.tail.fd != -1) {
2406 obj->data.tail.logfile = malloc(text_buffer_size);
2407 strcpy(obj->data.tail.logfile, buf);
2408 obj->data.tail.wantedlines = n1;
2409 obj->data.tail.interval = update_interval * 2;
2411 if (obj->data.tail.fd == -1) {
2416 CRIT_ERR("tail logfile does not exist, or you do not have "
2417 "correct permissions");
2420 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2421 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2422 CRIT_ERR("invalid arg for tail, number of lines must be "
2423 "between 1 and %i", MAX_TAIL_LINES);
2425 } else if (n2 < 1 || n2 < update_interval) {
2426 CRIT_ERR("invalid arg for tail, interval must be greater than "
2427 "0 and "PACKAGE_NAME"'s interval");
2433 obj->data.tail.fd = -1;
2435 if (stat(buf, &st) == 0) {
2436 if (S_ISFIFO(st.st_mode)) {
2437 fd = open(buf, O_RDONLY | O_NONBLOCK);
2440 CRIT_ERR("tail logfile does not exist, or you do "
2441 "not have correct permissions");
2444 obj->data.tail.fd = fd;
2446 fp = fopen(buf, "r");
2450 if (fp || obj->data.tail.fd != -1) {
2451 obj->data.tail.logfile = malloc(text_buffer_size);
2452 strcpy(obj->data.tail.logfile, buf);
2453 obj->data.tail.wantedlines = n1;
2454 obj->data.tail.interval = n2;
2456 if (obj->data.tail.fd == -1) {
2461 CRIT_ERR("tail logfile does not exist, or you do not have "
2462 "correct permissions");
2466 ERR("invalid args given for tail");
2469 /* asumming all else worked */
2470 obj->data.tail.buffer = malloc(text_buffer_size * 20);
2476 ERR("head needs arguments");
2477 obj->type = OBJ_text;
2478 obj->data.s = strndup("${head}", text_buffer_size);
2481 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2482 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2483 CRIT_ERR("invalid arg for head, number of lines must be "
2484 "between 1 and %i", MAX_TAIL_LINES);
2489 fp = fopen(buf, "r");
2491 obj->data.tail.logfile = malloc(text_buffer_size);
2492 strcpy(obj->data.tail.logfile, buf);
2493 obj->data.tail.wantedlines = n1;
2494 obj->data.tail.interval = update_interval * 2;
2498 CRIT_ERR("head logfile does not exist, or you do not have "
2499 "correct permissions");
2502 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2503 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2504 CRIT_ERR("invalid arg for head, number of lines must be "
2505 "between 1 and %i", MAX_TAIL_LINES);
2507 } else if (n2 < 1 || n2 < update_interval) {
2508 CRIT_ERR("invalid arg for head, interval must be greater than "
2509 "0 and "PACKAGE_NAME"'s interval");
2514 fp = fopen(buf, "r");
2516 obj->data.tail.logfile = malloc(text_buffer_size);
2517 strcpy(obj->data.tail.logfile, buf);
2518 obj->data.tail.wantedlines = n1;
2519 obj->data.tail.interval = n2;
2523 CRIT_ERR("head logfile does not exist, or you do not have "
2524 "correct permissions");
2528 ERR("invalid args given for head");
2531 /* asumming all else worked */
2532 obj->data.tail.buffer = malloc(text_buffer_size * 20);
2535 obj->data.s = strdup(arg);
2537 CRIT_ERR("lines needs a argument");
2541 obj->data.s = strdup(arg);
2543 CRIT_ERR("words needs a argument");
2545 END OBJ(loadavg, INFO_LOADAVG)
2546 int a = 1, b = 2, c = 3, r = 3;
2549 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2550 if (r >= 3 && (c < 1 || c > 3)) {
2553 if (r >= 2 && (b < 1 || b > 3)) {
2556 if (r >= 1 && (a < 1 || a > 3)) {
2560 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2561 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2562 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2563 END OBJ_IF(if_empty, 0)
2565 ERR("if_empty needs an argument");
2566 obj->data.ifblock.s = 0;
2568 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2569 obj->sub = malloc(sizeof(struct text_object));
2570 extract_variable_text_internal(obj->sub,
2571 obj->data.ifblock.s, 0);
2573 END OBJ_IF(if_match, 0)
2575 ERR("if_match needs arguments");
2576 obj->data.ifblock.s = 0;
2578 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2579 obj->sub = malloc(sizeof(struct text_object));
2580 extract_variable_text_internal(obj->sub,
2581 obj->data.ifblock.s, 0);
2583 END OBJ_IF(if_existing, 0)
2585 ERR("if_existing needs an argument or two");
2586 obj->data.ifblock.s = NULL;
2587 obj->data.ifblock.str = NULL;
2589 char buf1[256], buf2[256];
2590 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2593 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2594 obj->data.ifblock.str = NULL;
2596 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2597 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2600 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2601 END OBJ_IF(if_mounted, 0)
2603 ERR("if_mounted needs an argument");
2604 obj->data.ifblock.s = 0;
2606 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2608 END OBJ_IF(if_running, 0)
2612 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2613 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2615 ERR("if_running needs an argument");
2616 obj->data.ifblock.s = 0;
2622 char box[256], dst[256];
2626 /* Kapil: Changed from MAIL_FILE to
2627 current_mail_spool since the latter
2628 is a copy of the former if undefined
2629 but the latter should take precedence
2631 strncpy(box, current_mail_spool, sizeof(box));
2633 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2635 strncpy(box, arg, sizeof(box));
2639 variable_substitute(box, dst, sizeof(dst));
2640 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2641 obj->data.local_mail.interval = n1;
2642 END OBJ(mboxscan, 0)
2643 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2644 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2645 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2646 obj->data.mboxscan.output[0] = 1;
2647 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2648 END OBJ(mem, INFO_MEM)
2649 END OBJ(memeasyfree, INFO_MEM)
2650 END OBJ(memfree, INFO_MEM)
2651 END OBJ(memmax, INFO_MEM)
2652 END OBJ(memperc, INFO_MEM)
2653 END OBJ(membar, INFO_MEM)
2654 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2655 END OBJ(memgraph, INFO_MEM)
2656 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2657 &obj->e, &obj->showaslog);
2662 END OBJ(mixer, INFO_MIXER)
2663 obj->data.l = mixer_init(arg);
2664 END OBJ(mixerl, INFO_MIXER)
2665 obj->data.l = mixer_init(arg);
2666 END OBJ(mixerr, INFO_MIXER)
2667 obj->data.l = mixer_init(arg);
2668 END OBJ(mixerbar, INFO_MIXER)
2669 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2670 &obj->data.mixerbar.h);
2671 END OBJ(mixerlbar, INFO_MIXER)
2672 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2673 &obj->data.mixerbar.h);
2674 END OBJ(mixerrbar, INFO_MIXER)
2675 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2676 &obj->data.mixerbar.h);
2678 END OBJ(monitor, INFO_X11)
2679 END OBJ(monitor_number, INFO_X11)
2681 END OBJ(new_mails, 0)
2683 char box[256], dst[256];
2687 /* Kapil: Changed from MAIL_FILE to
2688 current_mail_spool since the latter
2689 is a copy of the former if undefined
2690 but the latter should take precedence
2692 strncpy(box, current_mail_spool, sizeof(box));
2694 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2696 strncpy(box, arg, sizeof(box));
2700 variable_substitute(box, dst, sizeof(dst));
2701 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2702 obj->data.local_mail.interval = n1;
2703 END OBJ(nodename, 0)
2704 END OBJ(processes, INFO_PROCS)
2705 END OBJ(running_processes, INFO_RUN_PROCS)
2706 END OBJ(shadecolor, 0)
2708 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2710 END OBJ(outlinecolor, 0)
2712 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2714 END OBJ(stippled_hr, 0)
2716 int a = stippled_borders, b = 1;
2719 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2720 sscanf(arg, "%d", &b);
2726 obj->data.pair.a = a;
2727 obj->data.pair.b = b;
2729 END OBJ(swap, INFO_MEM)
2730 END OBJ(swapmax, INFO_MEM)
2731 END OBJ(swapperc, INFO_MEM)
2732 END OBJ(swapbar, INFO_MEM)
2733 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2736 END OBJ(temp1, INFO_SYSFS)
2737 obj->type = OBJ_i2c;
2738 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
2739 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2740 END OBJ(temp2, INFO_SYSFS)
2741 obj->type = OBJ_i2c;
2742 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
2743 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2746 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2748 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2750 char buf1[256], buf2[256], *fmt, *tz;
2754 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2764 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2765 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2767 END OBJ(iconv_start, 0)
2768 if (iconv_converting) {
2769 CRIT_ERR("You must stop your last iconv conversion before "
2770 "starting another");
2773 char iconv_from[CODEPAGE_LENGTH];
2774 char iconv_to[CODEPAGE_LENGTH];
2776 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2777 CRIT_ERR("Invalid arguments for iconv_start");
2781 new_iconv = iconv_open(iconv_to, iconv_from);
2782 if (new_iconv == (iconv_t) (-1)) {
2783 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2785 obj->a = register_iconv(&new_iconv);
2786 iconv_converting = 1;
2790 CRIT_ERR("Iconv requires arguments");
2792 END OBJ(iconv_stop, 0)
2793 iconv_converting = 0;
2796 END OBJ(totaldown, INFO_NET)
2798 obj->data.net = get_net_stat(arg);
2800 CRIT_ERR("totaldown needs argument");
2802 END OBJ(totalup, INFO_NET)
2803 obj->data.net = get_net_stat(arg);
2805 obj->data.net = get_net_stat(arg);
2807 CRIT_ERR("totalup needs argument");
2811 obj->data.i = arg ? atoi(arg) : 0;
2813 obj->data.i = arg ? atoi(arg) : 0;
2814 END OBJ(upspeed, INFO_NET)
2816 obj->data.net = get_net_stat(arg);
2818 CRIT_ERR("upspeed needs argument");
2820 END OBJ(upspeedf, INFO_NET)
2822 obj->data.net = get_net_stat(arg);
2824 CRIT_ERR("upspeedf needs argument");
2827 END OBJ(upspeedgraph, INFO_NET)
2828 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2829 &obj->e, &obj->showaslog);
2831 // default to DEFAULTNETDEV
2832 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
2833 obj->data.net = get_net_stat(buf);
2835 END OBJ(uptime_short, INFO_UPTIME)
2836 END OBJ(uptime, INFO_UPTIME)
2837 END OBJ(user_names, INFO_USERS)
2838 END OBJ(user_times, INFO_USERS)
2839 END OBJ(user_terms, INFO_USERS)
2840 END OBJ(user_number, INFO_USERS)
2841 #if defined(__linux__)
2842 END OBJ(gw_iface, INFO_GW)
2843 END OBJ(gw_ip, INFO_GW)
2844 END OBJ(if_gw, INFO_GW)
2845 #endif /* !__linux__ */
2847 END OBJ(adt746xcpu, 0)
2848 END OBJ(adt746xfan, 0)
2849 #endif /* !__OpenBSD__ */
2850 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2851 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2852 END OBJ(apm_adapter, 0)
2853 END OBJ(apm_battery_life, 0)
2854 END OBJ(apm_battery_time, 0)
2855 #endif /* __FreeBSD__ */
2856 END OBJ_THREAD(imap_unseen, 0)
2859 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2860 obj->global_mode = 0;
2862 obj->global_mode = 1;
2864 END OBJ_THREAD(imap_messages, 0)
2867 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2868 obj->global_mode = 0;
2870 obj->global_mode = 1;
2872 END OBJ_THREAD(pop3_unseen, 0)
2875 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2876 obj->global_mode = 0;
2878 obj->global_mode = 1;
2880 END OBJ_THREAD(pop3_used, 0)
2883 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2884 obj->global_mode = 0;
2886 obj->global_mode = 1;
2891 obj->data.s = strndup(arg, text_buffer_size);
2893 ERR("smapi needs an argument");
2894 END OBJ_IF(if_smapi_bat_installed, 0)
2896 ERR("if_smapi_bat_installed needs an argument");
2897 obj->data.ifblock.s = 0;
2899 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2900 END OBJ(smapi_bat_perc, 0)
2902 obj->data.s = strndup(arg, text_buffer_size);
2904 ERR("smapi_bat_perc needs an argument");
2905 END OBJ(smapi_bat_temp, 0)
2907 obj->data.s = strndup(arg, text_buffer_size);
2909 ERR("smapi_bat_temp needs an argument");
2910 END OBJ(smapi_bat_power, 0)
2912 obj->data.s = strndup(arg, text_buffer_size);
2914 ERR("smapi_bat_power needs an argument");
2915 END OBJ(smapi_bat_bar, 0)
2918 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2919 ERR("first argument to smapi_bat_bar must be an integer value");
2923 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2926 ERR("smapi_bat_bar needs an argument");
2929 #define mpd_set_maxlen(name) \
2932 sscanf(arg, "%d", &i); \
2934 obj->data.i = i + 1; \
2936 ERR(#name ": invalid length argument"); \
2938 END OBJ(mpd_artist, INFO_MPD)
2939 mpd_set_maxlen(mpd_artist);
2941 END OBJ(mpd_title, INFO_MPD)
2942 mpd_set_maxlen(mpd_title);
2944 END OBJ(mpd_random, INFO_MPD) init_mpd();
2945 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2946 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2947 END OBJ(mpd_length, INFO_MPD) init_mpd();
2948 END OBJ(mpd_track, INFO_MPD)
2949 mpd_set_maxlen(mpd_track);
2951 END OBJ(mpd_name, INFO_MPD)
2952 mpd_set_maxlen(mpd_name);
2954 END OBJ(mpd_file, INFO_MPD)
2955 mpd_set_maxlen(mpd_file);
2957 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2958 END OBJ(mpd_album, INFO_MPD)
2959 mpd_set_maxlen(mpd_album);
2961 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2962 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2963 END OBJ(mpd_status, INFO_MPD) init_mpd();
2964 END OBJ(mpd_bar, INFO_MPD)
2965 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2967 END OBJ(mpd_smart, INFO_MPD)
2968 mpd_set_maxlen(mpd_smart);
2970 END OBJ_IF(if_mpd_playing, INFO_MPD)
2972 #undef mpd_set_maxlen
2975 END OBJ(moc_state, INFO_MOC)
2976 END OBJ(moc_file, INFO_MOC)
2977 END OBJ(moc_title, INFO_MOC)
2978 END OBJ(moc_artist, INFO_MOC)
2979 END OBJ(moc_song, INFO_MOC)
2980 END OBJ(moc_album, INFO_MOC)
2981 END OBJ(moc_totaltime, INFO_MOC)
2982 END OBJ(moc_timeleft, INFO_MOC)
2983 END OBJ(moc_curtime, INFO_MOC)
2984 END OBJ(moc_bitrate, INFO_MOC)
2985 END OBJ(moc_rate, INFO_MOC)
2988 END OBJ(xmms2_artist, INFO_XMMS2)
2989 END OBJ(xmms2_album, INFO_XMMS2)
2990 END OBJ(xmms2_title, INFO_XMMS2)
2991 END OBJ(xmms2_genre, INFO_XMMS2)
2992 END OBJ(xmms2_comment, INFO_XMMS2)
2993 END OBJ(xmms2_url, INFO_XMMS2)
2994 END OBJ(xmms2_tracknr, INFO_XMMS2)
2995 END OBJ(xmms2_bitrate, INFO_XMMS2)
2996 END OBJ(xmms2_date, INFO_XMMS2)
2997 END OBJ(xmms2_id, INFO_XMMS2)
2998 END OBJ(xmms2_duration, INFO_XMMS2)
2999 END OBJ(xmms2_elapsed, INFO_XMMS2)
3000 END OBJ(xmms2_size, INFO_XMMS2)
3001 END OBJ(xmms2_status, INFO_XMMS2)
3002 END OBJ(xmms2_percent, INFO_XMMS2)
3003 END OBJ(xmms2_bar, INFO_XMMS2)
3004 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3005 END OBJ(xmms2_smart, INFO_XMMS2)
3006 END OBJ(xmms2_playlist, INFO_XMMS2)
3007 END OBJ(xmms2_timesplayed, INFO_XMMS2)
3008 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
3011 END OBJ(audacious_status, INFO_AUDACIOUS)
3012 END OBJ(audacious_title, INFO_AUDACIOUS)
3014 sscanf(arg, "%d", &info.audacious.max_title_len);
3015 if (info.audacious.max_title_len > 0) {
3016 info.audacious.max_title_len++;
3018 CRIT_ERR("audacious_title: invalid length argument");
3021 END OBJ(audacious_length, INFO_AUDACIOUS)
3022 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
3023 END OBJ(audacious_position, INFO_AUDACIOUS)
3024 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
3025 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
3026 END OBJ(audacious_frequency, INFO_AUDACIOUS)
3027 END OBJ(audacious_channels, INFO_AUDACIOUS)
3028 END OBJ(audacious_filename, INFO_AUDACIOUS)
3029 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
3030 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
3031 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
3032 END OBJ(audacious_bar, INFO_AUDACIOUS)
3033 scan_bar(arg, &obj->a, &obj->b);
3036 END OBJ(bmpx_title, INFO_BMPX)
3037 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3038 END OBJ(bmpx_artist, INFO_BMPX)
3039 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3040 END OBJ(bmpx_album, INFO_BMPX)
3041 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3042 END OBJ(bmpx_track, INFO_BMPX)
3043 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3044 END OBJ(bmpx_uri, INFO_BMPX)
3045 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3046 END OBJ(bmpx_bitrate, INFO_BMPX)
3047 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3053 char *userid = (char *) malloc(20 * sizeof(char));
3054 char *apikey = (char *) malloc(64 * sizeof(char));
3055 char *charid = (char *) malloc(20 * sizeof(char));
3057 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
3058 obj->data.eve.charid = charid;
3059 obj->data.eve.userid = userid;
3060 obj->data.eve.apikey = apikey;
3064 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
3070 int argc, delay, act_par;
3071 char *uri = (char *) malloc(128 * sizeof(char));
3072 char *action = (char *) malloc(64 * sizeof(char));
3074 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
3076 obj->data.rss.uri = uri;
3077 obj->data.rss.delay = delay;
3078 obj->data.rss.action = action;
3079 obj->data.rss.act_par = act_par;
3083 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
3089 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
3090 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3091 ERR("hddtemp needs arguments");
3092 obj->type = OBJ_text;
3093 obj->data.s = strndup("${hddtemp}", text_buffer_size);
3094 obj->data.hddtemp.update_time = 0;
3096 obj->data.hddtemp.temp = NULL;
3098 #ifdef TCP_PORT_MONITOR
3099 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3100 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
3102 END OBJ(entropy_avail, INFO_ENTROPY)
3103 END OBJ(entropy_poolsize, INFO_ENTROPY)
3104 END OBJ(entropy_bar, INFO_ENTROPY)
3105 scan_bar(arg, &obj->a, &obj->b);
3109 obj->data.scroll.step = 1;
3110 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
3111 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
3113 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
3114 obj->data.scroll.start = 0;
3115 obj->sub = malloc(sizeof(struct text_object));
3116 extract_variable_text_internal(obj->sub,
3117 obj->data.scroll.text, 0);
3119 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
3124 CRIT_ERR("nvidia needs an argument\n");
3125 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3126 CRIT_ERR("nvidia: invalid argument"
3127 " specified: '%s'\n", arg);
3133 ERR("unknown variable %s", s);
3134 obj->type = OBJ_text;
3135 snprintf(buf, 256, "${%s}", s);
3136 obj->data.s = strndup(buf, text_buffer_size);
3143 static struct text_object *create_plain_text(const char *s)
3145 struct text_object *obj;
3147 if (s == NULL || *s == '\0') {
3151 obj = new_text_object_internal();
3153 obj->type = OBJ_text;
3154 obj->data.s = strndup(s, text_buffer_size);
3158 /* backslash_escape - do the actual substitution task for template objects
3160 * The field templates is used for substituting the \N occurences. Set it to
3161 * NULL to leave them as they are.
3163 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3167 unsigned int dup_idx = 0, dup_len;
3169 dup_len = strlen(src);
3170 src_dup = malloc(dup_len * sizeof(char));
3178 if (*(p + 1) == '\\') {
3179 src_dup[dup_idx++] = '\\';
3181 } else if (*(p + 1) == ' ') {
3182 src_dup[dup_idx++] = ' ';
3184 } else if (*(p + 1) == 'n') {
3185 src_dup[dup_idx++] = '\n';
3187 } else if (templates) {
3188 unsigned int tmpl_num;
3190 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3191 (tmpl_num > template_count))
3193 dup_len += strlen(templates[tmpl_num - 1]);
3194 src_dup = realloc(src_dup, dup_len * sizeof(char));
3195 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3196 dup_idx += strlen(templates[tmpl_num - 1]);
3201 src_dup[dup_idx++] = *p;
3206 src_dup[dup_idx] = '\0';
3207 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3211 /* handle_template_object - core logic of the template object
3213 * use config variables like this:
3214 * template1 = "$\1\2"
3215 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3217 * and use them like this:
3218 * ${template1 node name}
3219 * ${template2 root /}
3220 * ${template2 cdrom /mnt/cdrom}
3222 static char *handle_template(const char *tmpl, const char *args)
3224 char *args_dup, *p, *p_old;
3225 char **argsp = NULL;
3226 unsigned int argcnt = 0, template_idx, i;
3229 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3233 args_dup = strdup(args);
3236 while (*p && (*p == ' ' && *(p - 1) != '\\'))
3238 if (*(p - 1) == '\\')
3241 while (*p && (*p != ' ' || *(p - 1) == '\\'))
3247 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3248 argsp[argcnt - 1] = p_old;
3250 for (i = 0; i < argcnt; i++) {
3252 tmp = backslash_escape(argsp[i], NULL, 0);
3253 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3257 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3258 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3260 for (i = 0; i < argcnt; i++)
3266 static char *find_and_replace_templates(const char *inbuf)
3268 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3271 outlen = strlen(inbuf) + 1;
3272 o = outbuf = calloc(outlen, sizeof(char));
3273 memset(outbuf, 0, outlen * sizeof(char));
3275 p = indup = strdup(inbuf);
3277 while (*p && *p != '$')
3283 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3288 if (*(p + 1) == '{') {
3310 tmpl_out = handle_template(templ, args);
3312 outlen += strlen(tmpl_out);
3313 outbuf = realloc(outbuf, outlen * sizeof(char));
3314 strcat (outbuf, tmpl_out);
3316 o = outbuf + strlen(outbuf);
3318 ERR("failed to handle template '%s' with args '%s'", templ, args);
3322 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3327 static int text_contains_templates(const char *text)
3329 if (strcasestr(text, "${template") != NULL)
3331 if (strcasestr(text, "$template") != NULL)
3336 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3338 struct text_object *obj;
3339 char *p, *s, *orig_p;
3341 void *ifblock_opaque = NULL;
3343 p = strndup(const_p, max_user_text - 1);
3344 while (text_contains_templates(p)) {
3346 tmp = find_and_replace_templates(p);
3352 if (strcmp(p, const_p)) {
3353 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3355 DBGP("no templates to replace");
3358 memset(retval, 0, sizeof(struct text_object));
3360 line = global_text_lines;
3368 obj = create_plain_text(s);
3370 append_object(retval, obj);
3381 /* variable is either $foo or ${foo} */
3383 unsigned int brl = 1, brr = 0;
3387 while (*p && brl != brr) {
3402 while (*p && (isalnum((int) *p) || *p == '_')) {
3407 /* copy variable to buffer */
3408 len = (p - s > 255) ? 255 : (p - s);
3409 strncpy(buf, s, len);
3419 /* if variable wasn't found in environment, use some special */
3425 if (strchr(buf, ' ')) {
3426 arg = strchr(buf, ' ');
3429 while (isspace((int) *arg)) {
3437 /* lowercase variable name */
3440 *tmp_p = tolower(*tmp_p);
3444 obj = construct_text_object(buf, arg,
3445 line, allow_threaded,
3448 append_object(retval, obj);
3453 obj = create_plain_text("$");
3455 append_object(retval, obj);
3461 obj = create_plain_text(s);
3463 append_object(retval, obj);
3466 if (!ifblock_stack_empty(&ifblock_opaque)) {
3467 ERR("one or more $endif's are missing");
3474 static void extract_variable_text(const char *p)
3476 free_text_objects(&global_root_object);
3490 extract_variable_text_internal(&global_root_object, p, 1);
3493 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3495 extract_variable_text_internal(root, txt, 0);
3496 generate_text_internal(p, max_user_text, *root, cur);
3500 /* Allows reading from a FIFO (i.e., /dev/xconsole).
3501 * The file descriptor is set to non-blocking which makes this possible.
3503 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
3504 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
3506 #define TAIL_PIPE_BUFSIZE 4096
3510 int fd = obj->data.tail.fd;
3513 char buf[TAIL_PIPE_BUFSIZE];
3514 ssize_t len = read(fd, buf, sizeof(buf));
3518 if (errno != EAGAIN) {
3519 strcpy(obj->data.tail.buffer, "Logfile Read Error");
3520 snprintf(dst, dst_size, "Logfile Read Error");
3524 } else if (len == 0) {
3525 strcpy(obj->data.tail.buffer, "Logfile Empty");
3526 snprintf(dst, dst_size, "Logfile Empty");
3530 for (line_len = 0, i = 0; i < len; i++) {
3534 if (buf[i] == '\n') {
3537 if (obj->data.tail.readlines > 0) {
3542 for (n = 0; obj->data.tail.buffer[n]; n++) {
3543 if (obj->data.tail.buffer[n] == '\n') {
3548 if (++olines < obj->data.tail.wantedlines) {
3554 p = obj->data.tail.buffer + first_line;
3555 pos = n - first_line;
3556 memmove(obj->data.tail.buffer,
3557 obj->data.tail.buffer + first_line, strlen(p));
3558 obj->data.tail.buffer[pos] = 0;
3564 p = buf + last_line;
3566 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
3567 obj->data.tail.buffer[pos + line_len] = 0;
3570 obj->data.tail.readlines = lines;
3578 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
3581 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3582 void *thread(void *), const char *text)
3584 if (obj->global_mode && info.mail) {
3585 // this means we use info
3586 if (!info.mail->p_timed_thread) {
3587 info.mail->p_timed_thread =
3588 timed_thread_create(thread,
3589 (void *) info.mail, info.mail->interval * 1000000);
3590 if (!info.mail->p_timed_thread) {
3591 ERR("Error creating %s timed thread", text);
3593 timed_thread_register(info.mail->p_timed_thread,
3594 &info.mail->p_timed_thread);
3595 if (timed_thread_run(info.mail->p_timed_thread)) {
3596 ERR("Error running %s timed thread", text);
3600 } else if (obj->data.mail) {
3601 // this means we use obj
3602 if (!obj->data.mail->p_timed_thread) {
3603 obj->data.mail->p_timed_thread =
3604 timed_thread_create(thread,
3605 (void *) obj->data.mail,
3606 obj->data.mail->interval * 1000000);
3607 if (!obj->data.mail->p_timed_thread) {
3608 ERR("Error creating %s timed thread", text);
3610 timed_thread_register(obj->data.mail->p_timed_thread,
3611 &obj->data.mail->p_timed_thread);
3612 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3613 ERR("Error running %s timed thread", text);
3616 return obj->data.mail;
3617 } else if (!obj->a) {
3618 // something is wrong, warn once then stop
3619 ERR("Theres a problem with your %s_unseen settings. "
3620 "Check that the global %s settings are defined "
3621 "properly (line %li).", global_text, global_text, obj->line);
3627 char *format_time(unsigned long timeval, const int width)
3630 unsigned long nt; // narrow time, for speed on 32-bit
3631 unsigned cc; // centiseconds
3632 unsigned nn; // multi-purpose whatever
3635 cc = nt % 100; // centiseconds past second
3636 nt /= 100; // total seconds
3637 nn = nt % 60; // seconds past the minute
3638 nt /= 60; // total minutes
3639 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3641 return strndup(buf, text_buffer_size);
3643 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3644 return strndup(buf, text_buffer_size);
3646 nn = nt % 60; // minutes past the hour
3647 nt /= 60; // total hours
3648 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3649 return strndup(buf, text_buffer_size);
3651 nn = nt; // now also hours
3652 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3653 return strndup(buf, text_buffer_size);
3655 nn /= 24; // now days
3656 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3657 return strndup(buf, text_buffer_size);
3659 nn /= 7; // now weeks
3660 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3661 return strndup(buf, text_buffer_size);
3663 // well shoot, this outta' fit...
3664 return strndup("<inf>", text_buffer_size);
3667 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3668 //string has to end with \0 and it's length should fit in a int
3670 void remove_deleted_chars(char *string){
3672 while(string[i] != 0){
3673 if(string[i] == BACKSPACE){
3675 strcpy( &(string[i-1]), &(string[i+1]) );
3677 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3682 static inline void format_media_player_time(char *buf, const int size,
3685 int days, hours, minutes;
3687 days = seconds / (24 * 60 * 60);
3688 seconds %= (24 * 60 * 60);
3689 hours = seconds / (60 * 60);
3690 seconds %= (60 * 60);
3691 minutes = seconds / 60;
3695 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3696 hours, minutes, seconds);
3697 } else if (hours > 0) {
3698 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3701 snprintf(buf, size, "%i:%02i", minutes, seconds);
3705 static inline double get_barnum(char *buf)
3717 if (sscanf(buf, "%lf", &barnum) == 0) {
3718 ERR("reading execbar value failed (perhaps it's not the "
3719 "correct format?)");
3722 if (barnum > 100.0 || barnum < 0.0) {
3723 ERR("your execbar value is not between 0 and 100, "
3724 "therefore it will be ignored");
3730 static void generate_text_internal(char *p, int p_max_size,
3731 struct text_object root, struct information *cur)
3733 struct text_object *obj;
3736 char buff_in[p_max_size];
3738 iconv_converting = 0;
3742 for (obj = root.next; obj && p_max_size > 0; obj = obj->next) {
3744 /* IFBLOCK jumping algorithm
3746 * This is easier as it looks like:
3747 * - each IF checks it's condition
3748 * - on FALSE: call DO_JUMP
3749 * - on TRUE: don't care
3750 * - each ELSE calls DO_JUMP unconditionally
3751 * - each ENDIF is silently being ignored
3754 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3755 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3756 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3757 * regularly, "obj" is being updated to point to obj->next, so object parsing
3758 * continues right after the corresponding ELSE or ENDIF. This means that if we
3759 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3760 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3761 * jumped, and there is nothing to do.
3765 obj = obj->data.ifblock.next; \
3768 #define OBJ(a) break; case OBJ_##a:
3770 switch (obj->type) {
3772 ERR("not implemented obj type %d", obj->type);
3775 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3777 #endif /* !__OpenBSD__ */
3780 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3781 obj->data.cpu_index);
3787 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3788 obj->data.cpu_index);
3790 /* OpenBSD has no such flag (SUSv2) */
3791 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3792 obj->data.cpu_index);
3796 #if defined(__linux__)
3799 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3800 obj->data.cpu_index);
3805 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3806 obj->data.cpu_index);
3811 OBJ(wireless_essid) {
3812 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3814 OBJ(wireless_mode) {
3815 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3817 OBJ(wireless_bitrate) {
3818 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3821 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3823 OBJ(wireless_link_qual) {
3824 spaced_print(p, p_max_size, "%d", 4,
3825 obj->data.net->link_qual);
3827 OBJ(wireless_link_qual_max) {
3828 spaced_print(p, p_max_size, "%d", 4,
3829 obj->data.net->link_qual_max);
3831 OBJ(wireless_link_qual_perc) {
3832 if (obj->data.net->link_qual_max > 0) {
3833 spaced_print(p, p_max_size, "%.0f", 5,
3834 (double) obj->data.net->link_qual /
3835 obj->data.net->link_qual_max * 100);
3837 spaced_print(p, p_max_size, "unk", 5);
3840 OBJ(wireless_link_bar) {
3841 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3842 obj->data.net->link_qual_max) * 255.0);
3844 #endif /* HAVE_IWLIB */
3846 #endif /* __linux__ */
3850 get_adt746x_cpu(p, p_max_size);
3853 get_adt746x_fan(p, p_max_size);
3856 get_acpi_fan(p, p_max_size);
3858 OBJ(acpiacadapter) {
3859 get_acpi_ac_adapter(p, p_max_size);
3862 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3865 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3867 OBJ(battery_percent) {
3868 spaced_print(p, p_max_size, "%*d", 4,
3869 pad_percents, get_battery_perct(obj->data.s));
3872 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3874 #endif /* __OpenBSD__ */
3877 human_readable(cur->buffers * 1024, p, 255);
3880 human_readable(cur->cached * 1024, p, 255);
3883 if (obj->data.cpu_index > info.cpu_count) {
3884 printf("obj->data.cpu_index %i info.cpu_count %i",
3885 obj->data.cpu_index, info.cpu_count);
3886 CRIT_ERR("attempting to use more CPUs than you have!");
3888 spaced_print(p, p_max_size, "%*d", 4, pad_percents,
3889 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3892 new_bar(p, obj->a, obj->b,
3893 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3896 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
3897 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3898 100, 1, obj->showaslog);
3901 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3902 obj->e, 1, obj->showaslog);
3905 new_fg(p, obj->data.l);
3937 OBJ(conky_version) {
3938 snprintf(p, p_max_size, "%s", VERSION);
3940 OBJ(conky_build_date) {
3941 snprintf(p, p_max_size, "%s", BUILD_DATE);
3943 OBJ(conky_build_arch) {
3944 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3946 #if defined(__linux__)
3948 snprintf(p, p_max_size, "%s",
3949 get_disk_protect_queue(obj->data.s));
3952 snprintf(p, p_max_size, "%s", i8k.version);
3955 snprintf(p, p_max_size, "%s", i8k.bios);
3958 snprintf(p, p_max_size, "%s", i8k.serial);
3963 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3964 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3966 OBJ(i8k_left_fan_status) {
3967 int left_fan_status;
3969 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3970 if (left_fan_status == 0) {
3971 snprintf(p, p_max_size, "off");
3973 if (left_fan_status == 1) {
3974 snprintf(p, p_max_size, "low");
3976 if (left_fan_status == 2) {
3977 snprintf(p, p_max_size, "high");
3980 OBJ(i8k_right_fan_status) {
3981 int right_fan_status;
3983 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3984 if (right_fan_status == 0) {
3985 snprintf(p, p_max_size, "off");
3987 if (right_fan_status == 1) {
3988 snprintf(p, p_max_size, "low");
3990 if (right_fan_status == 2) {
3991 snprintf(p, p_max_size, "high");
3994 OBJ(i8k_left_fan_rpm) {
3995 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3997 OBJ(i8k_right_fan_rpm) {
3998 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4000 OBJ(i8k_ac_status) {
4003 sscanf(i8k.ac_status, "%d", &ac_status);
4004 if (ac_status == -1) {
4005 snprintf(p, p_max_size, "disabled (read i8k docs)");
4007 if (ac_status == 0) {
4008 snprintf(p, p_max_size, "off");
4010 if (ac_status == 1) {
4011 snprintf(p, p_max_size, "on");
4014 OBJ(i8k_buttons_status) {
4015 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4018 get_ibm_acpi_fan(p, p_max_size);
4021 get_ibm_acpi_temps();
4022 temp_print(p, p_max_size,
4023 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4026 get_ibm_acpi_volume(p, p_max_size);
4028 OBJ(ibm_brightness) {
4029 get_ibm_acpi_brightness(p, p_max_size);
4032 if ((obj->data.ifblock.s)
4033 && (!interface_up(obj->data.ifblock.s))) {
4038 if (!cur->gw_info.count) {
4043 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4046 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4049 snprintf(p, p_max_size, "%d", get_laptop_mode());
4052 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4054 #endif /* __linux__ */
4057 OBJ(obsd_sensors_temp) {
4058 obsd_sensors.device = sensor_device;
4059 update_obsd_sensors();
4060 temp_print(p, p_max_size,
4061 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4064 OBJ(obsd_sensors_fan) {
4065 obsd_sensors.device = sensor_device;
4066 update_obsd_sensors();
4067 snprintf(p, p_max_size, "%d",
4068 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4070 OBJ(obsd_sensors_volt) {
4071 obsd_sensors.device = sensor_device;
4072 update_obsd_sensors();
4073 snprintf(p, p_max_size, "%.2f",
4074 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4077 get_obsd_vendor(p, p_max_size);
4080 get_obsd_product(p, p_max_size);
4082 #endif /* __OpenBSD__ */
4086 new_font(p, obj->data.s);
4089 /* TODO: move this correction from kB to kB/s elsewhere
4090 * (or get rid of it??) */
4092 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4096 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4100 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4104 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4105 obj->data.diskio->current, obj->e, 1, obj->showaslog);
4107 OBJ(diskiograph_read) {
4108 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4109 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
4111 OBJ(diskiograph_write) {
4112 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4113 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
4116 spaced_print(p, p_max_size, "%d", 6,
4117 round_to_int(obj->data.net->recv_speed / 1024));
4120 spaced_print(p, p_max_size, "%.1f", 8,
4121 obj->data.net->recv_speed / 1024.0);
4123 OBJ(downspeedgraph) {
4124 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4125 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
4128 /* Since we see you, you're if has not jumped.
4129 * Do Ninja jump here: without leaving traces.
4130 * This is to prevent us from stale jumped flags.
4132 obj = obj->data.ifblock.next;
4136 /* harmless object, just ignore */
4140 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4141 && (obj->data.net->addr.sa_data[3] & 255) == 0
4142 && (obj->data.net->addr.sa_data[4] & 255) == 0
4143 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4144 snprintf(p, p_max_size, "No Address");
4146 snprintf(p, p_max_size, "%u.%u.%u.%u",
4147 obj->data.net->addr.sa_data[2] & 255,
4148 obj->data.net->addr.sa_data[3] & 255,
4149 obj->data.net->addr.sa_data[4] & 255,
4150 obj->data.net->addr.sa_data[5] & 255);
4153 #if defined(__linux__)
4155 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4157 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4158 strcpy(p, obj->data.net->addrs);
4161 strcpy(p, "0.0.0.0");
4163 #endif /* __linux__ */
4165 #if defined(IMLIB2) && defined(X11)
4170 Imlib_Image image, buffer;
4172 image = imlib_load_image(obj->data.s);
4173 imlib_context_set_image(image);
4177 w = imlib_image_get_width();
4178 h = imlib_image_get_height();
4179 buffer = imlib_create_image(w, h);
4180 imlib_context_set_display(display);
4181 imlib_context_set_drawable(window.drawable);
4182 imlib_context_set_colormap(DefaultColormap(display,
4184 imlib_context_set_visual(DefaultVisual(display,
4186 imlib_context_set_image(buffer);
4187 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
4188 text_start_x, text_start_y, w, h);
4189 imlib_render_image_on_drawable(text_start_x,
4192 imlib_context_set_image(image);
4200 read_exec(obj->data.s, p, text_buffer_size);
4201 remove_deleted_chars(p);
4204 struct information *tmp_info;
4205 struct text_object subroot;
4207 read_exec(obj->data.s, p, text_buffer_size);
4209 tmp_info = malloc(sizeof(struct information));
4210 memcpy(tmp_info, cur, sizeof(struct information));
4211 parse_conky_vars(&subroot, p, p, tmp_info);
4213 free_text_objects(&subroot);
4219 read_exec(obj->data.s, p, text_buffer_size);
4220 barnum = get_barnum(p);
4222 if (barnum >= 0.0) {
4224 new_bar(p, 0, 4, round_to_int(barnum * 255.0));
4228 char showaslog = FALSE;
4231 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
4233 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, text_buffer_size);
4234 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
4235 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, text_buffer_size);
4237 read_exec(obj->data.s, p, text_buffer_size);
4239 barnum = get_barnum(p);
4241 if (barnum >= 0.0) {
4242 new_graph(p, 0, 25, obj->c, obj->d, round_to_int(barnum),
4247 if (current_update_time - obj->data.execi.last_update
4248 >= obj->data.execi.interval) {
4251 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4252 barnum = get_barnum(p);
4254 if (barnum >= 0.0) {
4255 obj->f = 255 * barnum / 100.0;
4257 obj->data.execi.last_update = current_update_time;
4259 new_bar(p, 0, 4, round_to_int(obj->f));
4262 if (current_update_time - obj->data.execi.last_update
4263 >= obj->data.execi.interval) {
4266 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4267 barnum = get_barnum(p);
4269 if (barnum >= 0.0) {
4272 obj->data.execi.last_update = current_update_time;
4274 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
4277 if (current_update_time - obj->data.execi.last_update
4278 >= obj->data.execi.interval
4279 && obj->data.execi.interval != 0) {
4280 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4282 obj->data.execi.last_update = current_update_time;
4284 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4287 struct text_object subroot;
4288 struct information *tmp_info =
4289 malloc(sizeof(struct information));
4290 memcpy(tmp_info, cur, sizeof(struct information));
4292 if (current_update_time - obj->data.execi.last_update
4293 < obj->data.execi.interval
4294 || obj->data.execi.interval == 0) {
4295 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4297 char *output = obj->data.execi.buffer;
4298 FILE *fp = popen(obj->data.execi.cmd, "r");
4299 int length = fread(output, 1, text_buffer_size, fp);
4303 output[length] = '\0';
4304 if (length > 0 && output[length - 1] == '\n') {
4305 output[length - 1] = '\0';
4308 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4309 obj->data.execi.last_update = current_update_time;
4311 free_text_objects(&subroot);
4315 if (!obj->data.texeci.p_timed_thread) {
4316 obj->data.texeci.p_timed_thread =
4317 timed_thread_create(&threaded_exec,
4318 (void *) obj, obj->data.texeci.interval * 1000000);
4319 if (!obj->data.texeci.p_timed_thread) {
4320 ERR("Error creating texeci timed thread");
4322 timed_thread_register(obj->data.texeci.p_timed_thread,
4323 &obj->data.texeci.p_timed_thread);
4324 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4325 ERR("Error running texeci timed thread");
4328 timed_thread_lock(obj->data.texeci.p_timed_thread);
4329 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4330 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4332 #endif /* HAVE_POPEN */
4334 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4336 if (mail && mail->p_timed_thread) {
4337 timed_thread_lock(mail->p_timed_thread);
4338 snprintf(p, p_max_size, "%lu", mail->unseen);
4339 timed_thread_unlock(mail->p_timed_thread);
4342 OBJ(imap_messages) {
4343 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4345 if (mail && mail->p_timed_thread) {
4346 timed_thread_lock(mail->p_timed_thread);
4347 snprintf(p, p_max_size, "%lu", mail->messages);
4348 timed_thread_unlock(mail->p_timed_thread);
4352 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4354 if (mail && mail->p_timed_thread) {
4355 timed_thread_lock(mail->p_timed_thread);
4356 snprintf(p, p_max_size, "%lu", mail->unseen);
4357 timed_thread_unlock(mail->p_timed_thread);
4361 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4363 if (mail && mail->p_timed_thread) {
4364 timed_thread_lock(mail->p_timed_thread);
4365 snprintf(p, p_max_size, "%.1f",
4366 mail->used / 1024.0 / 1024.0);
4367 timed_thread_unlock(mail->p_timed_thread);
4371 if (obj->data.fs != NULL) {
4372 if (obj->data.fs->size == 0) {
4373 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4375 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4376 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4377 obj->data.fs->size));
4382 if (obj->data.fs != NULL) {
4383 human_readable(obj->data.fs->avail, p, 255);
4387 if (obj->data.fs != NULL) {
4388 if (obj->data.fs->size) {
4389 spaced_print(p, p_max_size, "%*d", 4,
4390 pad_percents, (int) ((obj->data.fs->avail * 100) /
4391 obj->data.fs->size));
4393 snprintf(p, p_max_size, "0");
4398 if (obj->data.fs != NULL) {
4399 human_readable(obj->data.fs->size, p, 255);
4403 if (obj->data.fs != NULL)
4404 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4407 if (obj->data.fs != NULL) {
4408 human_readable(obj->data.fs->size - (obj->data.fs->free
4409 ? obj->data.fs->free : obj->data.fs->avail), p, 255);
4413 if (obj->data.fs != NULL) {
4414 if (obj->data.fs->size == 0) {
4415 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4417 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4418 (int) (obj->data.fsbar.fs->avail * 255 /
4419 obj->data.fs->size));
4424 if (obj->data.fs != NULL) {
4425 if (obj->data.fs->size) {
4426 spaced_print(p, 4, "%*d", 4,
4427 pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
4428 obj->data.fs->size)));
4430 snprintf(p, p_max_size, "0");
4435 float *v = info.loadavg;
4437 if (obj->data.loadavg[2]) {
4438 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4439 v[obj->data.loadavg[0] - 1],
4440 v[obj->data.loadavg[1] - 1],
4441 v[obj->data.loadavg[2] - 1]);
4442 } else if (obj->data.loadavg[1]) {
4443 snprintf(p, p_max_size, "%.2f %.2f",
4444 v[obj->data.loadavg[0] - 1],
4445 v[obj->data.loadavg[1] - 1]);
4446 } else if (obj->data.loadavg[0]) {
4447 snprintf(p, p_max_size, "%.2f",
4448 v[obj->data.loadavg[0] - 1]);
4452 new_goto(p, obj->data.i);
4455 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4458 new_hr(p, obj->data.i);
4461 if (cur->nameserver_info.nscount > obj->data.i)
4462 snprintf(p, p_max_size, "%s",
4463 cur->nameserver_info.ns_list[obj->data.i]);
4467 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4468 snprintf(p, p_max_size, "%s", skill);
4473 PRSS *data = get_rss_info(obj->data.rss.uri,
4474 obj->data.rss.delay);
4478 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4480 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4482 // remove trailing new line if one exists
4483 if (str[strlen(str) - 1] == '\n') {
4484 str[strlen(str) - 1] = 0;
4486 snprintf(p, p_max_size, "%s", str);
4487 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4488 if (obj->data.rss.act_par < data->item_count) {
4489 str = data->items[obj->data.rss.act_par].title;
4490 // remove trailing new line if one exists
4491 if (str[strlen(str) - 1] == '\n') {
4492 str[strlen(str) - 1] = 0;
4494 snprintf(p, p_max_size, "%s", str);
4496 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4497 if (obj->data.rss.act_par < data->item_count) {
4499 data->items[obj->data.rss.act_par].description;
4500 // remove trailing new line if one exists
4501 if (str[strlen(str) - 1] == '\n') {
4502 str[strlen(str) - 1] = 0;
4504 snprintf(p, p_max_size, "%s", str);
4506 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4507 if (data->item_count > 0) {
4513 if (obj->data.rss.act_par > data->item_count) {
4514 show = data->item_count;
4516 show = obj->data.rss.act_par;
4518 for (itmp = 0; itmp < show; itmp++) {
4519 PRSS_Item *item = &data->items[itmp];
4523 // don't add new line before first item
4525 strncat(p, "\n", p_max_size);
4527 /* remove trailing new line if one exists,
4528 * we have our own */
4529 if (str[strlen(str) - 1] == '\n') {
4530 str[strlen(str) - 1] = 0;
4532 strncat(p, str, p_max_size);
4544 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4545 if (obj->data.hddtemp.temp)
4546 free(obj->data.hddtemp.temp);
4547 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4548 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4549 obj->data.hddtemp.update_time = current_update_time;
4551 if (!obj->data.hddtemp.temp) {
4552 snprintf(p, p_max_size, "N/A");
4554 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4555 unit = obj->data.hddtemp.temp[0];
4557 if (*endptr != '\0')
4558 snprintf(p, p_max_size, "N/A");
4559 else if (unit == 'C')
4560 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4561 else if (unit == 'F')
4562 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4564 snprintf(p, p_max_size, "N/A");
4569 new_offset(p, obj->data.i);
4572 new_voffset(p, obj->data.i);
4578 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4579 obj->data.sysfs.devtype, obj->data.sysfs.type);
4581 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4582 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4583 } else if (r >= 100.0 || r == 0) {
4584 snprintf(p, p_max_size, "%d", (int) r);
4586 snprintf(p, p_max_size, "%.1f", r);
4592 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4593 obj->data.sysfs.devtype, obj->data.sysfs.type);
4595 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4596 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4597 } else if (r >= 100.0 || r == 0) {
4598 snprintf(p, p_max_size, "%d", (int) r);
4600 snprintf(p, p_max_size, "%.1f", r);
4606 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4607 obj->data.sysfs.devtype, obj->data.sysfs.type);
4609 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4610 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4611 } else if (r >= 100.0 || r == 0) {
4612 snprintf(p, p_max_size, "%d", (int) r);
4614 snprintf(p, p_max_size, "%.1f", r);
4617 #endif /* !__OpenBSD__ */
4619 new_alignr(p, obj->data.i);
4622 new_alignc(p, obj->data.i);
4625 char buf[max_user_text];
4626 struct information *tmp_info =
4627 malloc(sizeof(struct information));
4628 memcpy(tmp_info, cur, sizeof(struct information));
4629 generate_text_internal(buf, max_user_text,
4630 *obj->sub, tmp_info);
4632 if (strlen(buf) != 0) {
4638 char expression[max_user_text];
4640 struct information *tmp_info;
4642 tmp_info = malloc(sizeof(struct information));
4643 memcpy(tmp_info, cur, sizeof(struct information));
4644 generate_text_internal(expression, max_user_text,
4645 *obj->sub, tmp_info);
4646 DBGP("parsed arg into '%s'", expression);
4648 val = compare(expression);
4650 ERR("compare failed for expression '%s'",
4658 if (obj->data.ifblock.str
4659 && !check_contains(obj->data.ifblock.s,
4660 obj->data.ifblock.str)) {
4662 } else if (obj->data.ifblock.s
4663 && access(obj->data.ifblock.s, F_OK)) {
4668 if ((obj->data.ifblock.s)
4669 && (!check_mount(obj->data.ifblock.s))) {
4674 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4678 #if defined(__linux__)
4680 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4684 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4687 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4692 human_readable(cur->mem * 1024, p, 255);
4695 human_readable(cur->memeasyfree * 1024, p, 255);
4698 human_readable(cur->memfree * 1024, p, 255);
4701 human_readable(cur->memmax * 1024, p, 255);
4705 spaced_print(p, p_max_size, "%*llu", 4,
4706 pad_percents, cur->mem * 100 / cur->memmax);
4710 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4711 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4714 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4715 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4716 100, 1, obj->showaslog);
4721 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
4724 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
4727 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
4730 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4731 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
4734 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4735 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
4738 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4739 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
4743 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4745 OBJ(monitor_number) {
4746 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4752 update_mail_count(&obj->data.local_mail);
4753 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4756 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
4758 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4761 update_mail_count(&obj->data.local_mail);
4762 snprintf(p, p_max_size, "%d",
4763 obj->data.local_mail.new_mail_count);
4766 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
4769 new_outline(p, obj->data.l);
4772 spaced_print(p, p_max_size, "%hu", 5, cur->procs);
4774 OBJ(running_processes) {
4775 spaced_print(p, p_max_size, "%hu", 3, cur->run_procs);
4778 snprintf(p, p_max_size, "%s", obj->data.s);
4781 new_bg(p, obj->data.l);
4784 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
4787 human_readable(cur->swap * 1024, p, 255);
4790 human_readable(cur->swapmax * 1024, p, 255);
4793 if (cur->swapmax == 0) {
4794 strncpy(p, "No swap", p_max_size);
4796 spaced_print(p, p_max_size, "%*llu", 4,
4797 pad_percents, cur->swap * 100 / cur->swapmax);
4801 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4802 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
4805 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4808 time_t t = time(NULL);
4809 struct tm *tm = localtime(&t);
4811 setlocale(LC_TIME, "");
4812 strftime(p, p_max_size, obj->data.s, tm);
4815 time_t t = time(NULL);
4816 struct tm *tm = gmtime(&t);
4818 strftime(p, p_max_size, obj->data.s, tm);
4825 if (obj->data.tztime.tz) {
4826 oldTZ = getenv("TZ");
4827 setenv("TZ", obj->data.tztime.tz, 1);
4833 setlocale(LC_TIME, "");
4834 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4836 setenv("TZ", oldTZ, 1);
4841 // Needless to free oldTZ since getenv gives ptr to static data
4844 human_readable(obj->data.net->recv, p, 255);
4847 human_readable(obj->data.net->trans, p, 255);
4850 snprintf(p, p_max_size, "%d", total_updates);
4853 spaced_print(p, p_max_size, "%d", 6,
4854 round_to_int(obj->data.net->trans_speed / 1024));
4857 spaced_print(p, p_max_size, "%.1f", 8,
4858 obj->data.net->trans_speed / 1024.0);
4861 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4862 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
4865 format_seconds_short(p, p_max_size, (int) cur->uptime);
4868 format_seconds(p, p_max_size, (int) cur->uptime);
4871 snprintf(p, p_max_size, "%s", cur->users.names);
4874 snprintf(p, p_max_size, "%s", cur->users.terms);
4877 snprintf(p, p_max_size, "%s", cur->users.times);
4880 snprintf(p, p_max_size, "%d", cur->users.number);
4882 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
4883 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4887 msg = get_apm_adapter();
4888 snprintf(p, p_max_size, "%s", msg);
4891 OBJ(apm_battery_life) {
4894 msg = get_apm_battery_life();
4895 snprintf(p, p_max_size, "%s", msg);
4898 OBJ(apm_battery_time) {
4901 msg = get_apm_battery_time();
4902 snprintf(p, p_max_size, "%s", msg);
4905 #endif /* __FreeBSD__ __OpenBSD__ */
4908 #define mpd_printf(fmt, val) \
4909 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
4910 #define mpd_sprintf(val) { \
4911 if (!obj->data.i || obj->data.i > p_max_size) \
4912 mpd_printf("%s", val); \
4914 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
4919 mpd_sprintf(artist);
4923 mpd_printf("%s", random);
4925 mpd_printf("%s", repeat);
4933 mpd_printf("%d", volume);
4935 mpd_printf("%d", bitrate);
4937 mpd_printf("%s", status);
4939 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
4942 format_media_player_time(p, p_max_size, mpd_get_info()->length);
4945 spaced_print(p, p_max_size, "%*d", 4,
4946 pad_percents, (int) (mpd_get_info()->progress * 100));
4949 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4950 (int) (mpd_get_info()->progress * 255.0f));
4953 struct mpd_s *mpd = mpd_get_info();
4954 int len = obj->data.i;
4955 if (len == 0 || len > p_max_size)
4958 memset(p, 0, p_max_size);
4959 if (mpd->artist && *mpd->artist &&
4960 mpd->title && *mpd->title) {
4961 snprintf(p, len, "%s - %s", mpd->artist,
4963 } else if (mpd->title && *mpd->title) {
4964 snprintf(p, len, "%s", mpd->title);
4965 } else if (mpd->artist && *mpd->artist) {
4966 snprintf(p, len, "%s", mpd->artist);
4967 } else if (mpd->file && *mpd->file) {
4968 snprintf(p, len, "%s", mpd->file);
4973 OBJ(if_mpd_playing) {
4974 if (!mpd_get_info()->is_playing) {
4983 #define MOC_PRINT(t, a) \
4984 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
4986 MOC_PRINT(state, "??");
4989 MOC_PRINT(file, "no file");
4992 MOC_PRINT(title, "no title");
4995 MOC_PRINT(artist, "no artist");
4998 MOC_PRINT(song, "no song");
5001 MOC_PRINT(album, "no album");
5003 OBJ(moc_totaltime) {
5004 MOC_PRINT(totaltime, "0:00");
5007 MOC_PRINT(timeleft, "0:00");
5010 MOC_PRINT(curtime, "0:00");
5013 MOC_PRINT(bitrate, "0Kbps");
5016 MOC_PRINT(rate, "0KHz");
5022 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5025 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5028 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5031 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5033 OBJ(xmms2_comment) {
5034 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5037 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5040 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5043 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5045 OBJ(xmms2_tracknr) {
5046 if (cur->xmms2.tracknr != -1) {
5047 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5050 OBJ(xmms2_bitrate) {
5051 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5054 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5057 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5059 OBJ(xmms2_elapsed) {
5060 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5061 (cur->xmms2.elapsed / 1000) % 60);
5063 OBJ(xmms2_duration) {
5064 snprintf(p, p_max_size, "%02d:%02d",
5065 cur->xmms2.duration / 60000,
5066 (cur->xmms2.duration / 1000) % 60);
5068 OBJ(xmms2_percent) {
5069 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5072 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5073 (int) (cur->xmms2.progress * 255.0f));
5075 OBJ(xmms2_playlist) {
5076 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5078 OBJ(xmms2_timesplayed) {
5079 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5082 if (strlen(cur->xmms2.title) < 2
5083 && strlen(cur->xmms2.title) < 2) {
5084 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5086 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5090 OBJ(if_xmms2_connected) {
5091 if (cur->xmms2_conn_state != 1) {
5097 OBJ(audacious_status) {
5098 snprintf(p, p_max_size, "%s",
5099 cur->audacious.items[AUDACIOUS_STATUS]);
5101 OBJ(audacious_title) {
5102 snprintf(p, cur->audacious.max_title_len > 0
5103 ? cur->audacious.max_title_len : p_max_size, "%s",
5104 cur->audacious.items[AUDACIOUS_TITLE]);
5106 OBJ(audacious_length) {
5107 snprintf(p, p_max_size, "%s",
5108 cur->audacious.items[AUDACIOUS_LENGTH]);
5110 OBJ(audacious_length_seconds) {
5111 snprintf(p, p_max_size, "%s",
5112 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5114 OBJ(audacious_position) {
5115 snprintf(p, p_max_size, "%s",
5116 cur->audacious.items[AUDACIOUS_POSITION]);
5118 OBJ(audacious_position_seconds) {
5119 snprintf(p, p_max_size, "%s",
5120 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5122 OBJ(audacious_bitrate) {
5123 snprintf(p, p_max_size, "%s",
5124 cur->audacious.items[AUDACIOUS_BITRATE]);
5126 OBJ(audacious_frequency) {
5127 snprintf(p, p_max_size, "%s",
5128 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5130 OBJ(audacious_channels) {
5131 snprintf(p, p_max_size, "%s",
5132 cur->audacious.items[AUDACIOUS_CHANNELS]);
5134 OBJ(audacious_filename) {
5135 snprintf(p, p_max_size, "%s",
5136 cur->audacious.items[AUDACIOUS_FILENAME]);
5138 OBJ(audacious_playlist_length) {
5139 snprintf(p, p_max_size, "%s",
5140 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5142 OBJ(audacious_playlist_position) {
5143 snprintf(p, p_max_size, "%s",
5144 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5146 OBJ(audacious_main_volume) {
5147 snprintf(p, p_max_size, "%s",
5148 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5150 OBJ(audacious_bar) {
5154 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5155 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5156 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5162 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5165 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5168 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5171 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5174 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5177 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5181 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5184 switch (obj->data.top.type) {
5186 snprintf(p, 16, "%-15s",
5187 cur->cpu[obj->data.top.num]->name);
5190 snprintf(p, 7, "%6.2f",
5191 cur->cpu[obj->data.top.num]->amount);
5194 snprintf(p, 6, "%5i",
5195 cur->cpu[obj->data.top.num]->pid);
5198 snprintf(p, 7, "%6.2f",
5199 cur->cpu[obj->data.top.num]->totalmem);
5202 timeval = format_time(
5203 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
5204 snprintf(p, 10, "%9s", timeval);
5208 human_readable(cur->cpu[obj->data.top.num]->rss,
5212 human_readable(cur->cpu[obj->data.top.num]->vsize,
5216 ERR("Unhandled top data type: %d\n",
5217 obj->data.top.type);
5220 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5224 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5227 switch (obj->data.top.type) {
5229 snprintf(p, 16, "%-15s",
5230 cur->memu[obj->data.top.num]->name);
5233 snprintf(p, 7, "%6.2f",
5234 cur->memu[obj->data.top.num]->amount);
5237 snprintf(p, 6, "%5i",
5238 cur->memu[obj->data.top.num]->pid);
5241 snprintf(p, 7, "%6.2f",
5242 cur->memu[obj->data.top.num]->totalmem);
5245 timeval = format_time(
5246 cur->memu[obj->data.top.num]->total_cpu_time,
5248 snprintf(p, 10, "%9s", timeval);
5252 human_readable(cur->cpu[obj->data.top.num]->rss,
5256 human_readable(cur->cpu[obj->data.top.num]->vsize,
5260 ERR("Unhandled top data type: %d\n",
5261 obj->data.top.type);
5264 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5268 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5271 switch (obj->data.top.type) {
5273 snprintf(p, 16, "%-15s",
5274 cur->time[obj->data.top.num]->name);
5277 snprintf(p, 7, "%6.2f",
5278 cur->time[obj->data.top.num]->amount);
5281 snprintf(p, 6, "%5i",
5282 cur->time[obj->data.top.num]->pid);
5285 snprintf(p, 7, "%6.2f",
5286 cur->time[obj->data.top.num]->totalmem);
5289 timeval = format_time(
5290 cur->time[obj->data.top.num]->total_cpu_time,
5292 snprintf(p, 10, "%9s", timeval);
5296 human_readable(cur->cpu[obj->data.top.num]->rss,
5300 human_readable(cur->cpu[obj->data.top.num]->vsize,
5304 ERR("Unhandled top data type: %d\n",
5305 obj->data.top.type);
5308 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5312 if (current_update_time - obj->data.tail.last_update
5313 < obj->data.tail.interval) {
5314 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5320 obj->data.tail.last_update = current_update_time;
5322 if (obj->data.tail.fd != -1) {
5323 tail_pipe(obj, p, p_max_size);
5327 fp = fopen(obj->data.tail.logfile, "rt");
5330 /* Send one message, but do not consistently spam
5331 * on missing logfiles. */
5332 if (obj->data.tail.readlines != 0) {
5333 ERR("tail logfile failed to open");
5334 strcpy(obj->data.tail.buffer, "Logfile Missing");
5336 obj->data.tail.readlines = 0;
5337 snprintf(p, p_max_size, "Logfile Missing");
5339 obj->data.tail.readlines = 0;
5340 /* -1 instead of 0 to avoid counting a trailing
5342 fseek(fp, -1, SEEK_END);
5343 bsize = ftell(fp) + 1;
5344 for (iter = obj->data.tail.wantedlines; iter > 0;
5346 nl = rev_fcharfind(fp, '\n', iter);
5351 obj->data.tail.readlines = iter;
5352 if (obj->data.tail.readlines
5353 < obj->data.tail.wantedlines) {
5354 fseek(fp, 0, SEEK_SET);
5356 fseek(fp, nl + 1, SEEK_SET);
5359 /* Make sure bsize is at least 1 byte smaller than the
5360 * buffer max size. */
5361 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
5362 fseek(fp, bsize - text_buffer_size * 20 - 1,
5364 bsize = text_buffer_size * 20 - 1;
5366 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
5369 /* Clean up trailing newline, make sure the
5370 * buffer is null terminated. */
5371 if (obj->data.tail.buffer[bsize - 1] == '\n') {
5372 obj->data.tail.buffer[bsize - 1] = '\0';
5374 obj->data.tail.buffer[bsize] = '\0';
5376 snprintf(p, p_max_size, "%s",
5377 obj->data.tail.buffer);
5379 strcpy(obj->data.tail.buffer, "Logfile Empty");
5380 snprintf(p, p_max_size, "Logfile Empty");
5383 } /* if cur_upd_time >= */
5387 if (current_update_time - obj->data.tail.last_update
5388 < obj->data.tail.interval) {
5389 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5395 obj->data.tail.last_update = current_update_time;
5397 fp = fopen(obj->data.tail.logfile, "rt");
5399 /* Send one message, but do not consistently spam
5400 * on missing logfiles. */
5401 if (obj->data.tail.readlines != 0) {
5402 ERR("head logfile failed to open");
5403 strcpy(obj->data.tail.buffer, "Logfile Missing");
5405 obj->data.tail.readlines = 0;
5406 snprintf(p, p_max_size, "Logfile Missing");
5408 obj->data.tail.readlines = 0;
5409 for (iter = obj->data.tail.wantedlines; iter > 0;
5411 nl = fwd_fcharfind(fp, '\n', iter);
5416 obj->data.tail.readlines = iter;
5417 /* Make sure nl is at least 1 byte smaller than the
5418 * buffer max size. */
5419 if (nl > (long) ((text_buffer_size * 20) - 1)) {
5420 nl = text_buffer_size * 20 - 1;
5422 nl = fread(obj->data.tail.buffer, 1, nl, fp);
5425 /* Clean up trailing newline, make sure the buffer
5426 * is null terminated. */
5427 if (obj->data.tail.buffer[nl - 1] == '\n') {
5428 obj->data.tail.buffer[nl - 1] = '\0';
5430 obj->data.tail.buffer[nl] = '\0';
5432 snprintf(p, p_max_size, "%s",
5433 obj->data.tail.buffer);
5435 strcpy(obj->data.tail.buffer, "Logfile Empty");
5436 snprintf(p, p_max_size, "Logfile Empty");
5438 } /* if fp == null */
5439 } /* cur_upd_time >= */
5443 FILE *fp = fopen(obj->data.s,"r");
5450 while(fgets(buf, BUFSZ, fp) != NULL){
5451 for(j = 0; buf[j] != 0; j++) {
5452 if(buf[j] == '\n') {
5457 sprintf(p, "%d", lines);
5460 sprintf(p, "File Unreadable");
5465 FILE *fp = fopen(obj->data.s,"r");
5470 char inword = FALSE;
5473 while(fgets(buf, BUFSZ, fp) != NULL){
5474 for(j = 0; buf[j] != 0; j++) {
5475 if(!isspace(buf[j])) {
5476 if(inword == FALSE) {
5485 sprintf(p, "%d", words);
5488 sprintf(p, "File Unreadable");
5491 #ifdef TCP_PORT_MONITOR
5493 tcp_portmon_action(p, p_max_size,
5494 &obj->data.tcp_port_monitor);
5500 iconv_converting = 1;
5501 iconv_selected = obj->a;
5504 iconv_converting = 0;
5509 OBJ(entropy_avail) {
5510 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5512 OBJ(entropy_poolsize) {
5513 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5516 double entropy_perc;
5518 entropy_perc = (double) cur->entropy.entropy_avail /
5519 (double) cur->entropy.poolsize;
5520 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5526 s = smapi_get_val(obj->data.s);
5527 snprintf(p, p_max_size, "%s", s);
5531 OBJ(if_smapi_bat_installed) {
5533 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5534 if(!smapi_bat_installed(idx)) {
5538 ERR("argument to if_smapi_bat_installed must be an integer");
5540 OBJ(smapi_bat_perc) {
5542 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5543 val = smapi_bat_installed(idx) ?
5544 smapi_get_bat_int(idx, "remaining_percent") : 0;
5545 spaced_print(p, p_max_size, "%*d", 4, pad_percents, val);
5547 ERR("argument to smapi_bat_perc must be an integer");
5549 OBJ(smapi_bat_temp) {
5551 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5552 val = smapi_bat_installed(idx) ?
5553 smapi_get_bat_int(idx, "temperature") : 0;
5554 /* temperature is in milli degree celsius */
5555 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5557 ERR("argument to smapi_bat_temp must be an integer");
5559 OBJ(smapi_bat_power) {
5561 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5562 val = smapi_bat_installed(idx) ?
5563 smapi_get_bat_int(idx, "power_now") : 0;
5564 /* power_now is in mW, set to W with one digit precision */
5565 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5567 ERR("argument to smapi_bat_power must be an integer");
5569 OBJ(smapi_bat_bar) {
5570 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5571 new_bar(p, obj->a, obj->b, (int)
5572 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5574 new_bar(p, obj->a, obj->b, 0);
5579 char *tmp, buf[max_user_text];
5580 generate_text_internal(buf, max_user_text,
5583 if (strlen(buf) <= obj->data.scroll.show) {
5584 snprintf(p, p_max_size, "%s", buf);
5587 #define LINESEPARATOR '|'
5588 //place all the lines behind each other with LINESEPARATOR between them
5589 for(j = 0; buf[j] != 0; j++) {
5591 buf[j]=LINESEPARATOR;
5594 //scroll the output obj->data.scroll.start places by copying that many chars from
5595 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5596 //at the back of the string
5597 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5598 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5599 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5600 buf[j - obj->data.scroll.start] = buf[j];
5602 strcpy(&buf[j - obj->data.scroll.start], tmp);
5604 //only show the requested number of chars
5605 if(obj->data.scroll.show < j) {
5606 buf[obj->data.scroll.show] = 0;
5608 //next time, scroll a place more or reset scrolling if we are at the end
5609 obj->data.scroll.start += obj->data.scroll.step;
5610 if(obj->data.scroll.start >= j){
5611 obj->data.scroll.start = 0;
5613 snprintf(p, p_max_size, "%s", buf);
5617 int value = get_nvidia_value(obj->data.nvidia.type, display);
5619 snprintf(p, p_max_size, "N/A");
5620 else if (obj->data.nvidia.type == NV_TEMP)
5621 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5622 else if (obj->data.nvidia.print_as_float &&
5623 value > 0 && value < 100)
5624 snprintf(p, p_max_size, "%.1f", (float)value);
5626 snprintf(p, p_max_size, "%d", value);
5636 unsigned int a = strlen(p);
5639 if (a > 0 && iconv_converting && iconv_selected > 0
5640 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5642 size_t dummy1, dummy2;
5643 char *ptr = buff_in;
5646 dummy1 = dummy2 = a;
5648 strncpy(buff_in, p, p_max_size);
5650 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5651 while (dummy1 > 0) {
5652 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5655 ERR("Iconv codeset conversion failed");
5660 /* It is nessecary when we are converting from multibyte to
5661 * singlebyte codepage */
5664 #endif /* HAVE_ICONV */
5671 double current_update_time, next_update_time, last_update_time;
5673 static void generate_text(void)
5675 struct information *cur = &info;
5682 current_update_time = get_time();
5686 /* add things to the buffer */
5692 generate_text_internal(p, max_user_text, global_root_object, cur);
5694 if (stuff_in_upper_case) {
5697 tmp_p = text_buffer;
5699 *tmp_p = toupper(*tmp_p);
5704 next_update_time += update_interval;
5705 if (next_update_time < get_time()) {
5706 next_update_time = get_time() + update_interval;
5707 } else if (next_update_time > get_time() + update_interval) {
5708 next_update_time = get_time() + update_interval;
5710 last_update_time = current_update_time;
5716 static void set_font(void)
5720 if (window.xftdraw != NULL) {
5721 XftDrawDestroy(window.xftdraw);
5723 window.xftdraw = XftDrawCreate(display, window.drawable,
5724 DefaultVisual(display, screen), DefaultColormap(display, screen));
5728 XSetFont(display, window.gc, fonts[selected_font].font->fid);
5734 static inline int get_string_width(const char *s)
5737 return *s ? calc_text_width(s, strlen(s)) : 0;
5743 static inline int get_string_width_special(char *s)
5755 p = strndup(s, text_buffer_size);
5759 if (*p == SPECIAL_CHAR) {
5760 /* shift everything over by 1 so that the special char
5761 * doesn't mess up the size calculation */
5762 for (i = 0; i < strlen(p); i++) {
5763 *(p + i) = *(p + i + 1);
5765 if (specials[special_index + idx].type == GRAPH
5766 || specials[special_index + idx].type == BAR) {
5767 width += specials[special_index + idx].width;
5774 if (strlen(final) > 1) {
5775 width += calc_text_width(final, strlen(final));
5780 return (s) ? strlen(s) : 0;
5785 static void text_size_updater(char *s);
5787 int last_font_height;
5788 static void update_text_area(void)
5792 /* update text size if it isn't fixed */
5797 text_width = minimum_width;
5800 last_font_height = font_height();
5801 for_each_line(text_buffer, text_size_updater);
5803 if (text_height < minimum_height) {
5804 text_height = minimum_height;
5806 if (text_width > maximum_width && maximum_width > 0) {
5807 text_width = maximum_width;
5811 /* get text position on workarea */
5812 switch (text_alignment) {
5819 x = workarea[2] - text_width - gap_x;
5824 x = workarea[2] / 2 - text_width / 2 - gap_x;
5831 y = workarea[3] - text_height - gap_y;
5835 x = workarea[2] - text_width - gap_x;
5836 y = workarea[3] - text_height - gap_y;
5840 x = workarea[2] / 2 - text_width / 2 - gap_x;
5841 y = workarea[3] - text_height - gap_y;
5846 y = workarea[3] / 2 - text_height / 2 - gap_y;
5850 x = workarea[2] - text_width - gap_x;
5851 y = workarea[3] / 2 - text_height / 2 - gap_y;
5855 case NONE: // Let the WM manage the window
5866 if (own_window && !fixed_pos) {
5869 text_start_x = border_margin + 1;
5870 text_start_y = border_margin + 1;
5871 window.x = x - border_margin - 1;
5872 window.y = y - border_margin - 1;
5876 /* If window size doesn't match to workarea's size,
5877 * then window probably includes panels (gnome).
5878 * Blah, doesn't work on KDE. */
5879 if (workarea[2] != window.width || workarea[3] != window.height) {
5891 static int cur_x, cur_y; /* current x and y for drawing */
5893 //draw_mode also without X11 because we only need to print to stdout with FG
5894 static int draw_mode; /* FG, BG or OUTLINE */
5896 static long current_color;
5899 static void text_size_updater(char *s)
5904 /* get string widths and skip specials */
5907 if (*p == SPECIAL_CHAR) {
5909 w += get_string_width(s);
5912 if (specials[special_index].type == BAR
5913 || specials[special_index].type == GRAPH) {
5914 w += specials[special_index].width;
5915 if (specials[special_index].height > last_font_height) {
5916 last_font_height = specials[special_index].height;
5917 last_font_height += font_ascent();
5919 } else if (specials[special_index].type == OFFSET) {
5920 if (specials[special_index].arg > 0) {
5921 w += specials[special_index].arg;
5923 } else if (specials[special_index].type == VOFFSET) {
5924 last_font_height += specials[special_index].arg;
5925 } else if (specials[special_index].type == GOTO) {
5926 if (specials[special_index].arg > cur_x) {
5927 w = (int) specials[special_index].arg;
5929 } else if (specials[special_index].type == TAB) {
5930 int start = specials[special_index].arg;
5931 int step = specials[special_index].width;
5933 if (!step || step < 0) {
5936 w += step - (cur_x - text_start_x - start) % step;
5937 } else if (specials[special_index].type == FONT) {
5938 selected_font = specials[special_index].font_added;
5939 if (font_height() > last_font_height) {
5940 last_font_height = font_height();
5949 w += get_string_width(s);
5950 if (w > text_width) {
5953 if (text_width > maximum_width && maximum_width) {
5954 text_width = maximum_width;
5957 text_height += last_font_height;
5958 last_font_height = font_height();
5962 static inline void set_foreground_color(long c)
5965 XSetForeground(display, window.gc, c);
5969 static void draw_string(const char *s)
5971 int i, i2, pos, width_of_s;
5979 width_of_s = get_string_width(s);
5980 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
5982 fflush(stdout); /* output immediately, don't buffer */
5984 memset(tmpstring1, 0, text_buffer_size);
5985 memset(tmpstring2, 0, text_buffer_size);
5986 strncpy(tmpstring1, s, text_buffer_size - 1);
5991 max = ((text_width - width_of_s) / get_string_width(" "));
5993 /* This code looks for tabs in the text and coverts them to spaces.
5994 * The trick is getting the correct number of spaces, and not going
5995 * over the window's size without forcing the window larger. */
5996 for (i = 0; i < (int) text_buffer_size; i++) {
5997 if (tmpstring1[i] == '\t') {
5999 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6000 /* guard against overrun */
6001 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6006 /* guard against overrun */
6007 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6012 if (text_width == maximum_width) {
6013 /* this means the text is probably pushing the limit,
6014 * so we'll chop it */
6015 while (cur_x + get_string_width(tmpstring2) - text_start_x
6016 > maximum_width && strlen(tmpstring2) > 0) {
6017 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6028 c.pixel = current_color;
6029 XQueryColor(display, DefaultColormap(display, screen), &c);
6032 c2.color.red = c.red;
6033 c2.color.green = c.green;
6034 c2.color.blue = c.blue;
6035 c2.color.alpha = fonts[selected_font].font_alpha;
6037 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6038 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6040 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6041 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6046 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6049 cur_x += width_of_s;
6051 memcpy(tmpstring1, s, text_buffer_size);
6054 long redmask, greenmask, bluemask;
6056 void set_up_gradient(void)
6060 colour_depth = DisplayPlanes(display, screen);
6064 if (colour_depth != 24 && colour_depth != 16) {
6065 ERR("using non-standard colour depth, gradients may look like a "
6072 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6074 greenmask |= 1 << i;
6077 if (colour_depth % 3 == 1) {
6078 greenmask |= 1 << (colour_depth / 3);
6080 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6081 greenmask = greenmask << (colour_depth / 3);
6084 /* this function returns the next colour between two colours for a gradient */
6085 unsigned long do_gradient(unsigned long first_colour,
6086 unsigned long last_colour)
6089 int red1, green1, blue1; // first colour
6090 int red2, green2, blue2; // second colour
6091 int red3 = 0, green3 = 0, blue3 = 0; // difference
6092 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6093 short greenshift = (colour_depth / 3);
6095 red1 = (first_colour & redmask) >> redshift;
6096 green1 = (first_colour & greenmask) >> greenshift;
6097 blue1 = first_colour & bluemask;
6098 red2 = (last_colour & redmask) >> redshift;
6099 green2 = (last_colour & greenmask) >> greenshift;
6100 blue2 = last_colour & bluemask;
6107 if (green1 > green2) {
6110 if (green1 < green2) {
6113 if (blue1 > blue2) {
6116 if (blue1 < blue2) {
6131 if (red1 > bluemask) {
6134 if (green1 > bluemask) {
6137 if (blue1 > bluemask) {
6140 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
6144 /* this function returns the max diff for a gradient */
6145 unsigned long gradient_max(unsigned long first_colour,
6146 unsigned long last_colour)
6148 int red1, green1, blue1; // first colour
6149 int red2, green2, blue2; // second colour
6150 int red3 = 0, green3 = 0, blue3 = 0; // difference
6151 long redshift, greenshift;
6154 if (colour_depth == 0) {
6157 redshift = (2 * colour_depth / 3 + colour_depth % 3);
6158 greenshift = (colour_depth / 3);
6160 red1 = (first_colour & redmask) >> redshift;
6161 green1 = (first_colour & greenmask) >> greenshift;
6162 blue1 = first_colour & bluemask;
6163 red2 = (last_colour & redmask) >> redshift;
6164 green2 = (last_colour & greenmask) >> greenshift;
6165 blue2 = last_colour & bluemask;
6166 red3 = abs(red1 - red2);
6167 green3 = abs(green1 - green2);
6168 blue3 = abs(blue1 - blue2);
6180 static void draw_line(char *s)
6188 cur_x = text_start_x;
6189 cur_y += font_ascent();
6190 font_h = font_height();
6192 /* find specials and draw stuff */
6195 if (*p == SPECIAL_CHAR) {
6198 /* draw string before special */
6205 switch (specials[special_index].type) {
6206 case HORIZONTAL_LINE:
6208 int h = specials[special_index].height;
6209 int mid = font_ascent() / 2;
6211 w = text_start_x + text_width - cur_x;
6213 XSetLineAttributes(display, window.gc, h, LineSolid,
6214 CapButt, JoinMiter);
6215 XDrawLine(display, window.drawable, window.gc, cur_x,
6216 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6222 int h = specials[special_index].height;
6223 int tmp_s = specials[special_index].arg;
6224 int mid = font_ascent() / 2;
6225 char ss[2] = { tmp_s, tmp_s };
6227 w = text_start_x + text_width - cur_x - 1;
6228 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6229 CapButt, JoinMiter);
6230 XSetDashes(display, window.gc, 0, ss, 2);
6231 XDrawLine(display, window.drawable, window.gc, cur_x,
6232 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6238 int h, bar_usage, by;
6239 if (cur_x - text_start_x > maximum_width
6240 && maximum_width > 0) {
6243 h = specials[special_index].height;
6244 bar_usage = specials[special_index].arg;
6245 by = cur_y - (font_ascent() / 2) - 1;
6247 if (h < font_height()) {
6250 w = specials[special_index].width;
6252 w = text_start_x + text_width - cur_x - 1;
6258 XSetLineAttributes(display, window.gc, 1, LineSolid,
6259 CapButt, JoinMiter);
6261 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6263 XFillRectangle(display, window.drawable, window.gc, cur_x,
6264 by, w * bar_usage / 255, h);
6265 if (specials[special_index].height > cur_y_add
6266 && specials[special_index].height > font_h) {
6267 cur_y_add = specials[special_index].height;
6274 int h, by, i, j = 0;
6275 int gradient_size = 0;
6276 float gradient_factor = 0;
6277 float gradient_update = 0;
6278 unsigned long last_colour = current_color;
6279 unsigned long tmpcolour = current_color;
6280 if (cur_x - text_start_x > maximum_width
6281 && maximum_width > 0) {
6284 h = specials[special_index].height;
6285 by = cur_y - (font_ascent() / 2) - 1;
6287 if (h < font_height()) {
6290 w = specials[special_index].width;
6292 w = text_start_x + text_width - cur_x - 1;
6297 if (draw_graph_borders) {
6298 XSetLineAttributes(display, window.gc, 1, LineSolid,
6299 CapButt, JoinMiter);
6300 XDrawRectangle(display, window.drawable, window.gc,
6303 XSetLineAttributes(display, window.gc, 1, LineSolid,
6304 CapButt, JoinMiter);
6306 if (specials[special_index].last_colour != 0
6307 || specials[special_index].first_colour != 0) {
6308 tmpcolour = specials[special_index].last_colour;
6310 gradient_max(specials[special_index].last_colour,
6311 specials[special_index].first_colour);
6312 gradient_factor = (float) gradient_size / (w - 2);
6314 for (i = w - 2; i > -1; i--) {
6315 if (specials[special_index].last_colour != 0
6316 || specials[special_index].first_colour != 0) {
6317 XSetForeground(display, window.gc, tmpcolour);
6318 gradient_update += gradient_factor;
6319 while (gradient_update > 0) {
6320 tmpcolour = do_gradient(tmpcolour,
6321 specials[special_index].first_colour);
6325 if ((w - i) / ((float) (w - 2) /
6326 (specials[special_index].graph_width)) > j
6327 && j < MAX_GRAPH_DEPTH - 3) {
6330 /* this is mugfugly, but it works */
6331 XDrawLine(display, window.drawable, window.gc,
6332 cur_x + i + 1, by + h, cur_x + i + 1,
6333 by + h - specials[special_index].graph[j] *
6334 (h - 1) / specials[special_index].graph_scale);
6336 if (specials[special_index].height > cur_y_add
6337 && specials[special_index].height > font_h) {
6338 cur_y_add = specials[special_index].height;
6340 /* if (draw_mode == BG) {
6341 set_foreground_color(default_bg_color);
6342 } else if (draw_mode == OUTLINE) {
6343 set_foreground_color(default_out_color);
6345 set_foreground_color(default_fg_color);
6347 if (show_graph_range) {
6350 unsigned short int seconds = update_interval * w;
6355 unsigned short int timeunits;
6357 timeunits = seconds / 86400; seconds %= 86400;
6358 if( timeunits > 0 ) {
6359 asprintf(&tmp_day_str, "%dd", timeunits);
6361 tmp_day_str = strdup("");
6363 timeunits = seconds / 3600; seconds %= 3600;
6364 if( timeunits > 0 ) {
6365 asprintf(&tmp_hour_str, "%dh", timeunits);
6367 tmp_hour_str = strdup("");
6369 timeunits = seconds / 60; seconds %= 60;
6371 asprintf(&tmp_min_str, "%dm", timeunits);
6373 tmp_min_str = strdup("");
6376 asprintf(&tmp_sec_str, "%ds", seconds);
6378 tmp_sec_str = strdup("");
6380 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6381 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6383 asprintf(&tmp_str, "Range not possible"); //should never happen, but better safe then sorry
6385 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6386 cur_y += font_height() / 2;
6387 draw_string(tmp_str);
6393 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6396 cur_x += font_ascent() / 2;
6397 cur_y += font_height() / 2;
6399 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6401 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6402 draw_string(tmp_str);
6408 set_foreground_color(last_colour);
6414 int old = font_ascent();
6416 cur_y -= font_ascent();
6417 selected_font = specials[special_index].font_added;
6418 if (cur_y + font_ascent() < cur_y + old) {
6421 cur_y += font_ascent();
6427 if (draw_mode == FG) {
6428 set_foreground_color(specials[special_index].arg);
6433 if (draw_mode == BG) {
6434 set_foreground_color(specials[special_index].arg);
6439 if (draw_mode == OUTLINE) {
6440 set_foreground_color(specials[special_index].arg);
6445 w += specials[special_index].arg;
6449 cur_y += specials[special_index].arg;
6453 if (specials[special_index].arg >= 0) {
6454 cur_x = (int) specials[special_index].arg;
6460 int start = specials[special_index].arg;
6461 int step = specials[special_index].width;
6463 if (!step || step < 0) {
6466 w = step - (cur_x - text_start_x - start) % step;
6472 /* TODO: add back in "+ border_margin" to the end of
6474 int pos_x = text_start_x + text_width -
6475 get_string_width_special(s);
6477 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6478 "get_string_width(p) %i gap_x %i "
6479 "specials[special_index].arg %i border_margin %i "
6480 "border_width %i\n", pos_x, text_start_x, text_width,
6481 cur_x, get_string_width_special(s), gap_x,
6482 specials[special_index].arg, border_margin,
6484 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6485 cur_x = pos_x - specials[special_index].arg;
6492 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6493 2 - (cur_x - text_start_x);
6494 /* int pos_x = text_start_x + text_width / 2 -
6495 get_string_width_special(s) / 2; */
6497 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6498 "get_string_width(p) %i gap_x %i "
6499 "specials[special_index].arg %i\n", pos_x, text_start_x,
6500 text_width, cur_x, get_string_width(s), gap_x,
6501 specials[special_index].arg); */
6502 if (pos_x > specials[special_index].arg) {
6503 w = pos_x - specials[special_index].arg;
6520 if (cur_y_add > 0) {
6522 cur_y -= font_descent();
6527 cur_y += font_descent();
6531 static void draw_text(void)
6534 cur_y = text_start_y;
6537 if (draw_borders && border_width > 0) {
6538 unsigned int b = (border_width + 1) / 2;
6540 if (stippled_borders) {
6541 char ss[2] = { stippled_borders, stippled_borders };
6542 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6543 CapButt, JoinMiter);
6544 XSetDashes(display, window.gc, 0, ss, 2);
6546 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6547 CapButt, JoinMiter);
6550 XDrawRectangle(display, window.drawable, window.gc,
6551 text_start_x - border_margin + b, text_start_y - border_margin + b,
6552 text_width + border_margin * 2 - 1 - b * 2,
6553 text_height + border_margin * 2 - 1 - b * 2);
6559 for_each_line(text_buffer, draw_line);
6562 static void draw_stuff(void)
6566 if (draw_shades && !draw_outline) {
6569 set_foreground_color(default_bg_color);
6580 for (i = -1; i < 2; i++) {
6581 for (j = -1; j < 2; j++) {
6582 if (i == 0 && j == 0) {
6587 set_foreground_color(default_out_color);
6588 draw_mode = OUTLINE;
6596 set_foreground_color(default_fg_color);
6605 swap.swap_window = window.window;
6606 swap.swap_action = XdbeBackground;
6607 XdbeSwapBuffers(display, &swap, 1);
6614 static void clear_text(int exposures)
6618 /* The swap action is XdbeBackground, which clears */
6623 /* there is some extra space for borders and outlines */
6624 XClearArea(display, window.window, text_start_x - border_margin - 1,
6625 text_start_y - border_margin - 1,
6626 text_width + border_margin * 2 + 2,
6627 text_height + border_margin * 2 + 2, exposures ? True : 0);
6632 static int need_to_update;
6634 /* update_text() generates new text and clears old text area */
6635 static void update_text(void)
6644 static void main_loop(void)
6646 #ifdef SIGNAL_BLOCKING
6647 sigset_t newmask, oldmask;
6651 Region region = XCreateRegion();
6655 XserverRegion region2, part;
6656 int event_base, error_base;
6658 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
6659 ERR("Xdamage extension unavailable");
6661 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
6662 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
6663 part = XFixesCreateRegionFromWindow(display, window.window, 0);
6664 #endif /* HAVE_XDAMAGE */
6667 #ifdef SIGNAL_BLOCKING
6668 sigemptyset(&newmask);
6669 sigaddset(&newmask, SIGINT);
6670 sigaddset(&newmask, SIGTERM);
6671 sigaddset(&newmask, SIGUSR1);
6674 next_update_time = last_update_time = get_time();
6676 while (total_run_times == 0 || info.looped < total_run_times) {
6679 #ifdef SIGNAL_BLOCKING
6680 /* block signals. we will inspect for pending signals later */
6681 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6682 CRIT_ERR("unable to sigprocmask()");
6689 /* wait for X event or timeout */
6691 if (!XPending(display)) {
6695 t = next_update_time - get_time();
6699 } else if (t > update_interval) {
6700 t = update_interval;
6703 tv.tv_sec = (long) t;
6704 tv.tv_usec = (long) (t * 1000000) % 1000000;
6706 FD_SET(ConnectionNumber(display), &fdsr);
6708 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
6710 if (errno != EINTR) {
6711 ERR("can't select(): %s", strerror(errno));
6717 t = (next_update_time - get_time()) * 1000000;
6718 usleep((useconds_t)t);
6726 if (need_to_update) {
6728 int wx = window.x, wy = window.y;
6736 /* resize window if it isn't right size */
6738 && (text_width + border_margin * 2 + 1 != window.width
6739 || text_height + border_margin * 2 + 1 != window.height)) {
6740 window.width = text_width + border_margin * 2 + 1;
6741 window.height = text_height + border_margin * 2 + 1;
6742 XResizeWindow(display, window.window, window.width,
6745 set_transparent_background(window.window);
6749 /* move window if it isn't in right position */
6750 if (!fixed_pos && (window.x != wx || window.y != wy)) {
6751 XMoveWindow(display, window.window, window.x, window.y);
6762 r.x = text_start_x - border_margin;
6763 r.y = text_start_y - border_margin;
6764 r.width = text_width + border_margin * 2;
6765 r.height = text_height + border_margin * 2;
6766 XUnionRectWithRegion(&r, region, region);
6771 /* handle X events */
6772 while (XPending(display)) {
6775 XNextEvent(display, &ev);
6783 r.width = ev.xexpose.width;
6784 r.height = ev.xexpose.height;
6785 XUnionRectWithRegion(&r, region, region);
6790 case ReparentNotify:
6791 /* set background to ParentRelative for all parents */
6793 set_transparent_background(window.window);
6797 case ConfigureNotify:
6799 /* if window size isn't what expected, set fixed size */
6800 if (ev.xconfigure.width != window.width
6801 || ev.xconfigure.height != window.height) {
6802 if (window.width != 0 && window.height != 0) {
6806 /* clear old stuff before screwing up
6811 XWindowAttributes attrs;
6813 if (XGetWindowAttributes(display,
6814 window.window, &attrs)) {
6815 window.width = attrs.width;
6816 window.height = attrs.height;
6820 text_width = window.width - border_margin * 2 - 1;
6821 text_height = window.height - border_margin * 2 - 1;
6822 if (text_width > maximum_width
6823 && maximum_width > 0) {
6824 text_width = maximum_width;
6828 /* if position isn't what expected, set fixed pos
6829 * total_updates avoids setting fixed_pos when window
6830 * is set to weird locations when started */
6831 /* // this is broken
6832 if (total_updates >= 2 && !fixed_pos
6833 && (window.x != ev.xconfigure.x
6834 || window.y != ev.xconfigure.y)
6835 && (ev.xconfigure.x != 0
6836 || ev.xconfigure.y != 0)) {
6845 /* if an ordinary window with decorations */
6846 if ((window.type == TYPE_NORMAL)
6847 && (!TEST_HINT(window.hints,
6848 HINT_UNDECORATED))) {
6849 /* allow conky to hold input focus. */
6852 /* forward the click to the desktop window */
6853 XUngrabPointer(display, ev.xbutton.time);
6854 ev.xbutton.window = window.desktop;
6855 XSendEvent(display, ev.xbutton.window, False,
6856 ButtonPressMask, &ev);
6857 XSetInputFocus(display, ev.xbutton.window,
6858 RevertToParent, ev.xbutton.time);
6865 /* if an ordinary window with decorations */
6866 if ((window.type == TYPE_NORMAL)
6867 && (!TEST_HINT(window.hints,
6868 HINT_UNDECORATED))) {
6869 /* allow conky to hold input focus. */
6872 /* forward the release to the desktop window */
6873 ev.xbutton.window = window.desktop;
6874 XSendEvent(display, ev.xbutton.window, False,
6875 ButtonReleaseMask, &ev);
6884 if (ev.type == event_base + XDamageNotify) {
6885 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
6887 XFixesSetRegion(display, part, &dev->area, 1);
6888 XFixesUnionRegion(display, region2, region2, part);
6890 #endif /* HAVE_XDAMAGE */
6896 XDamageSubtract(display, damage, region2, None);
6897 XFixesSetRegion(display, region2, 0, 0);
6898 #endif /* HAVE_XDAMAGE */
6900 /* XDBE doesn't seem to provide a way to clear the back buffer without
6901 * interfering with the front buffer, other than passing XdbeBackground
6902 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6903 * redraw the text even if it wasn't part of the exposed area. OTOH,
6904 * if we're not going to call draw_stuff at all, then no swap happens
6905 * and we can safely do nothing. */
6907 if (!XEmptyRegion(region)) {
6912 r.x = text_start_x - border_margin;
6913 r.y = text_start_y - border_margin;
6914 r.width = text_width + border_margin * 2;
6915 r.height = text_height + border_margin * 2;
6916 XUnionRectWithRegion(&r, region, region);
6919 XSetRegion(display, window.gc, region);
6922 XftDrawSetClip(window.xftdraw, region);
6928 XDestroyRegion(region);
6929 region = XCreateRegion();
6933 #ifdef SIGNAL_BLOCKING
6934 /* unblock signals of interest and let handler fly */
6935 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
6936 CRIT_ERR("unable to sigprocmask()");
6940 switch (g_signal_pending) {
6943 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
6948 ERR("received SIGINT or SIGTERM to terminate. bye!");
6951 XDestroyRegion(region);
6954 XDamageDestroy(display, damage);
6955 XFixesDestroyRegion(display, region2);
6956 XFixesDestroyRegion(display, part);
6957 #endif /* HAVE_XDAMAGE */
6959 return; /* return from main_loop */
6962 /* Reaching here means someone set a signal
6963 * (SIGXXXX, signal_handler), but didn't write any code
6965 * If you don't want to handle a signal, don't set a handler on
6966 * it in the first place. */
6967 if (g_signal_pending) {
6968 ERR("ignoring signal (%d)", g_signal_pending);
6972 g_signal_pending = 0;
6975 #if defined(X11) && defined(HAVE_XDAMAGE)
6976 XDamageDestroy(display, damage);
6977 XFixesDestroyRegion(display, region2);
6978 XFixesDestroyRegion(display, part);
6979 XDestroyRegion(region);
6981 #endif /* X11 && HAVE_XDAMAGE */
6984 static void load_config_file(const char *);
6986 /* reload the config file */
6987 static void reload_config(void)
6989 timed_thread_destroy_registered_threads();
6991 if (info.cpu_usage) {
6992 free(info.cpu_usage);
6993 info.cpu_usage = NULL;
7004 #ifdef TCP_PORT_MONITOR
7005 tcp_portmon_clear();
7008 if (current_config) {
7010 load_config_file(current_config);
7012 /* re-init specials array */
7013 if ((specials = realloc((void *) specials,
7014 sizeof(struct special_t) * max_specials)) == 0) {
7015 ERR("failed to realloc specials array");
7021 // clear the window first
7022 XClearWindow(display, RootWindow(display, screen));
7025 extract_variable_text(global_text);
7031 tmpstring1 = malloc(text_buffer_size);
7032 memset(tmpstring1, 0, text_buffer_size);
7036 tmpstring2 = malloc(text_buffer_size);
7037 memset(tmpstring2, 0, text_buffer_size);
7041 text_buffer = malloc(max_user_text);
7042 memset(text_buffer, 0, max_user_text);
7047 static void clean_up(void)
7049 timed_thread_destroy_registered_threads();
7051 if (info.cpu_usage) {
7052 free(info.cpu_usage);
7053 info.cpu_usage = NULL;
7058 XdbeDeallocateBackBufferName(display, window.back_buffer);
7063 XDestroyWindow(display, window.window);
7064 XClearWindow(display, RootWindow(display, screen));
7069 XClearWindow(display, RootWindow(display, screen));
7074 XFreeGC(display, window.gc);
7078 free_text_objects(&global_root_object);
7097 free(current_config);
7099 #ifdef TCP_PORT_MONITOR
7100 tcp_portmon_clear();
7109 for (i = 0; i < special_count; i++) {
7110 if (specials[i].type == GRAPH) {
7111 free(specials[i].graph);
7118 clear_diskio_stats();
7121 static int string_to_bool(const char *s)
7124 // Assumes an option without a true/false means true
7126 } else if (strcasecmp(s, "yes") == EQUAL) {
7128 } else if (strcasecmp(s, "true") == EQUAL) {
7130 } else if (strcasecmp(s, "1") == EQUAL) {
7137 static enum alignment string_to_alignment(const char *s)
7139 if (strcasecmp(s, "top_left") == EQUAL) {
7141 } else if (strcasecmp(s, "top_right") == EQUAL) {
7143 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7145 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7147 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7148 return BOTTOM_RIGHT;
7149 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7150 return BOTTOM_MIDDLE;
7151 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7153 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7154 return MIDDLE_RIGHT;
7155 } else if (strcasecmp(s, "tl") == EQUAL) {
7157 } else if (strcasecmp(s, "tr") == EQUAL) {
7159 } else if (strcasecmp(s, "tm") == EQUAL) {
7161 } else if (strcasecmp(s, "bl") == EQUAL) {
7163 } else if (strcasecmp(s, "br") == EQUAL) {
7164 return BOTTOM_RIGHT;
7165 } else if (strcasecmp(s, "bm") == EQUAL) {
7166 return BOTTOM_MIDDLE;
7167 } else if (strcasecmp(s, "ml") == EQUAL) {
7169 } else if (strcasecmp(s, "mr") == EQUAL) {
7170 return MIDDLE_RIGHT;
7171 } else if (strcasecmp(s, "none") == EQUAL) {
7178 static void set_default_configurations(void)
7180 fork_to_background = 0;
7181 total_run_times = 0;
7182 info.cpu_avg_samples = 2;
7183 info.net_avg_samples = 2;
7191 mpd_set_host("localhost");
7192 mpd_set_port("6600");
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 mpd_set_host(value);
7542 if (value && mpd_set_port(value)) {
7546 CONF("mpd_password") {
7548 mpd_set_password(value);
7554 CONF("music_player_interval") {
7556 info.music_player_interval = strtod(value, 0);
7562 CONF("sensor_device") {
7564 sensor_device = strtol(value, 0, 0);
7570 CONF("cpu_avg_samples") {
7572 cpu_avg_samples = strtol(value, 0, 0);
7573 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
7576 info.cpu_avg_samples = cpu_avg_samples;
7582 CONF("net_avg_samples") {
7584 net_avg_samples = strtol(value, 0, 0);
7585 if (net_avg_samples < 1 || net_avg_samples > 14) {
7588 info.net_avg_samples = net_avg_samples;
7596 CONF("double_buffer") {
7597 use_xdbe = string_to_bool(value);
7601 CONF("override_utf8_locale") {
7602 utf8_mode = string_to_bool(value);
7604 CONF("draw_borders") {
7605 draw_borders = string_to_bool(value);
7607 CONF("draw_graph_borders") {
7608 draw_graph_borders = string_to_bool(value);
7610 CONF("draw_shades") {
7611 draw_shades = string_to_bool(value);
7613 CONF("draw_outline") {
7614 draw_outline = string_to_bool(value);
7617 CONF("out_to_console") {
7618 if(string_to_bool(value)) output_methods |= TO_STDOUT;
7620 CONF("use_spacer") {
7622 if (strcasecmp(value, "left") == EQUAL) {
7623 use_spacer = LEFT_SPACER;
7624 } else if (strcasecmp(value, "right") == EQUAL) {
7625 use_spacer = RIGHT_SPACER;
7626 } else if (strcasecmp(value, "none") == EQUAL) {
7627 use_spacer = NO_SPACER;
7629 use_spacer = string_to_bool(value);
7630 ERR("use_spacer should have an argument of left, right, or"
7631 " none. '%s' seems to be some form of '%s', so"
7632 " defaulting to %s.", value,
7633 use_spacer ? "true" : "false",
7634 use_spacer ? "right" : "none");
7636 use_spacer = RIGHT_SPACER;
7638 use_spacer = NO_SPACER;
7642 ERR("use_spacer should have an argument. Defaulting to right.");
7643 use_spacer = RIGHT_SPACER;
7649 use_xft = string_to_bool(value);
7653 set_first_font(value);
7659 if (value && font_count >= 0) {
7660 fonts[0].font_alpha = atof(value) * 65535.0;
7669 if (string_to_bool(value)) {
7670 ERR("Xft not enabled");
7674 /* xftfont silently ignored when no Xft */
7677 /* xftalpha is silently ignored when no Xft */
7682 set_first_font(value);
7692 gap_x = atoi(value);
7699 gap_y = atoi(value);
7705 CONF("mail_spool") {
7709 variable_substitute(value, buffer, 256);
7711 if (buffer[0] != '\0') {
7712 if (current_mail_spool) {
7713 free(current_mail_spool);
7715 current_mail_spool = strndup(buffer, text_buffer_size);
7722 CONF("minimum_size") {
7724 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
7726 if (sscanf(value, "%d", &minimum_width) != 1) {
7734 CONF("maximum_width") {
7736 if (sscanf(value, "%d", &maximum_width) != 1) {
7744 CONF("no_buffers") {
7745 no_buffers = string_to_bool(value);
7747 CONF("top_cpu_separate") {
7748 cpu_separate = string_to_bool(value);
7750 CONF("short_units") {
7751 short_units = string_to_bool(value);
7753 CONF("pad_percents") {
7754 pad_percents = atoi(value);
7758 CONF("own_window") {
7760 own_window = string_to_bool(value);
7765 CONF("own_window_class") {
7767 memset(window.class_name, 0, sizeof(window.class_name));
7768 strncpy(window.class_name, value,
7769 sizeof(window.class_name) - 1);
7774 CONF("own_window_title") {
7776 memset(window.title, 0, sizeof(window.title));
7777 strncpy(window.title, value, sizeof(window.title) - 1);
7782 CONF("own_window_transparent") {
7784 set_transparent = string_to_bool(value);
7789 CONF("own_window_colour") {
7791 background_colour = get_x11_color(value);
7793 ERR("Invalid colour for own_window_colour (try omitting the "
7794 "'#' for hex colours");
7797 CONF("own_window_hints") {
7799 char *p_hint, *p_save;
7800 char delim[] = ", ";
7802 /* tokenize the value into individual hints */
7803 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
7805 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
7806 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
7807 SET_HINT(window.hints, HINT_UNDECORATED);
7808 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
7809 SET_HINT(window.hints, HINT_BELOW);
7810 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
7811 SET_HINT(window.hints, HINT_ABOVE);
7812 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
7813 SET_HINT(window.hints, HINT_STICKY);
7814 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
7815 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
7816 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
7817 SET_HINT(window.hints, HINT_SKIP_PAGER);
7822 p_hint = strtok_r(NULL, delim, &p_save);
7823 } while (p_hint != NULL);
7829 CONF("own_window_type") {
7831 if (strncmp(value, "normal", 6) == EQUAL) {
7832 window.type = TYPE_NORMAL;
7833 } else if (strncmp(value, "desktop", 7) == EQUAL) {
7834 window.type = TYPE_DESKTOP;
7835 } else if (strncmp(value, "dock", 7) == EQUAL) {
7836 window.type = TYPE_DOCK;
7837 text_alignment = TOP_LEFT;
7838 } else if (strncmp(value, "override", 8) == EQUAL) {
7839 window.type = TYPE_OVERRIDE;
7848 CONF("stippled_borders") {
7850 stippled_borders = strtol(value, 0, 0);
7852 stippled_borders = 4;
7857 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
7861 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
7864 CONF("update_interval") {
7866 update_interval = strtod(value, 0);
7870 if (info.music_player_interval == 0) {
7871 // default to update_interval
7872 info.music_player_interval = update_interval;
7875 CONF("total_run_times") {
7877 total_run_times = strtod(value, 0);
7883 stuff_in_upper_case = string_to_bool(value);
7885 CONF("max_specials") {
7887 max_specials = atoi(value);
7892 CONF("max_user_text") {
7894 max_user_text = atoi(value);
7899 CONF("text_buffer_size") {
7901 text_buffer_size = atoi(value);
7902 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
7903 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
7904 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
7916 global_text = (char *) malloc(1);
7917 global_text[0] = '\0';
7920 unsigned int l = strlen(global_text);
7923 if (fgets(buf, 256, fp) == NULL) {
7929 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
7937 /* Check for continuation of \\-\n. */
7938 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
7939 global_text[l-1] = '\0';
7943 global_text = (char *) realloc(global_text, l + bl + 1);
7944 strcat(global_text, buf);
7946 if (strlen(global_text) > max_user_text) {
7951 if (strlen(global_text) < 1) {
7952 CRIT_ERR("no text supplied in configuration; exiting");
7954 global_text_lines = line + 1;
7957 #ifdef TCP_PORT_MONITOR
7958 CONF("max_port_monitor_connections") {
7960 if (!value || (sscanf(value, "%d", &max) != 1)) {
7961 /* an error. use default, warn and continue. */
7962 tcp_portmon_set_max_connections(0);
7964 } else if (tcp_portmon_set_max_connections(max)) {
7965 /* max is < 0, default has been set*/
7970 CONF("if_up_strictness") {
7972 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7973 ifup_strictness = IFUP_UP;
7974 } else if (strcasecmp(value, "up") == EQUAL) {
7975 ifup_strictness = IFUP_UP;
7976 } else if (strcasecmp(value, "link") == EQUAL) {
7977 ifup_strictness = IFUP_LINK;
7978 } else if (strcasecmp(value, "address") == EQUAL) {
7979 ifup_strictness = IFUP_ADDR;
7981 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7982 ifup_strictness = IFUP_UP;
7986 CONF("temperature_unit") {
7988 ERR("config option 'temperature_unit' needs an argument");
7989 } else if (set_temp_output_unit(value)) {
7990 ERR("temperature_unit: incorrect argument");
7995 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8006 if (info.music_player_interval == 0) {
8007 // default to update_interval
8008 info.music_player_interval = update_interval;
8010 if (!global_text) { // didn't supply any text
8011 CRIT_ERR("missing text block in configuration; exiting");
8015 static void print_help(const char *prog_name) {
8016 printf("Usage: %s [OPTION]...\n"
8017 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8018 "window. Command line options will override configurations defined in config\n"
8020 " -v, --version version\n"
8021 " -q, --quiet quiet mode\n"
8022 " -D, --debug increase debugging output\n"
8023 " -c, --config=FILE config file to load\n"
8024 " -d, --daemonize daemonize, fork to background\n"
8025 " -h, --help help\n"
8027 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8028 " -f, --font=FONT font to use\n"
8030 " -o, --own-window create own window to draw\n"
8033 " -b, --double-buffer double buffer (prevents flickering)\n"
8035 " -w, --window-id=WIN_ID window id to draw\n"
8036 " -x X x position\n"
8037 " -y Y y position\n"
8039 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8040 " -u, --interval=SECS update interval\n"
8041 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8046 /* : means that character before that takes an argument */
8047 static const char *getopt_string = "vVqdDt:u:i:hc:"
8056 #ifdef CONFIG_OUTPUT
8062 static const struct option longopts[] = {
8063 { "help", 0, NULL, 'h' },
8064 { "version", 0, NULL, 'V' },
8065 { "debug", 0, NULL, 'D' },
8066 { "config", 1, NULL, 'c' },
8067 #ifdef CONFIG_OUTPUT
8068 { "print-config", 0, NULL, 'C' },
8070 { "daemonize", 0, NULL, 'd' },
8072 { "alignment", 1, NULL, 'a' },
8073 { "font", 1, NULL, 'f' },
8075 { "own-window", 0, NULL, 'o' },
8078 { "double-buffer", 0, NULL, 'b' },
8080 { "window-id", 1, NULL, 'w' },
8082 { "text", 1, NULL, 't' },
8083 { "interval", 0, NULL, 'u' },
8087 int main(int argc, char **argv)
8093 struct sigaction act, oact;
8095 g_signal_pending = 0;
8096 memset(&info, 0, sizeof(info));
8099 #ifdef TCP_PORT_MONITOR
8100 /* set default connection limit */
8101 tcp_portmon_set_max_connections(0);
8104 /* handle command line parameters that don't change configs */
8106 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8107 || ((s = getenv("LANG")) && *s)) {
8108 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8110 ERR("malloc failed");
8112 for (x = 0; x < strlen(s); x++) {
8113 temp[x] = tolower(s[x]);
8116 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8122 if (!setlocale(LC_CTYPE, "")) {
8123 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8127 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8138 if (current_config) {
8139 free(current_config);
8141 current_config = strndup(optarg, max_user_text);
8144 freopen("/dev/null", "w", stderr);
8147 print_help(argv[0]);
8149 #ifdef CONFIG_OUTPUT
8156 window.window = strtol(optarg, 0, 0);
8165 /* initalize X BEFORE we load config.
8166 * (we need to so that 'screen' is set) */
8170 /* check if specified config file is valid */
8171 if (current_config) {
8173 if (stat(current_config, &sb) ||
8174 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
8175 ERR("invalid configuration file '%s'\n", current_config);
8176 free(current_config);
8181 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8183 if (!current_config) {
8184 /* load default config file */
8188 /* Try to use personal config file first */
8189 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
8190 if (buf[0] && (fp = fopen(buf, "r"))) {
8191 current_config = strndup(buf, max_user_text);
8195 /* Try to use system config file if personal config not readable */
8196 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8197 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
8201 /* No readable config found */
8202 if (!current_config) {
8203 #ifdef CONFIG_OUTPUT
8204 current_config = strdup("==builtin==");
8205 ERR("no readable personal or system-wide config file found,"
8206 " using builtin default");
8208 CRIT_ERR("no readable personal or system-wide config file found");
8209 #endif /* ! CONF_OUTPUT */
8213 load_config_file(current_config);
8215 /* init specials array */
8216 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8217 ERR("failed to create specials array");
8221 if (current_mail_spool == NULL) {
8224 variable_substitute(MAIL_FILE, buf, 256);
8226 if (buf[0] != '\0') {
8227 current_mail_spool = strndup(buf, text_buffer_size);
8232 /* handle other command line arguments */
8234 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8235 || defined(__NetBSD__)
8236 optind = optreset = 1;
8241 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8242 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8243 "kvm_open")) == NULL) {
8244 CRIT_ERR("cannot read kvm");
8249 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8257 fork_to_background = 1;
8260 global_debug_level++;
8264 set_first_font(optarg);
8267 text_alignment = string_to_alignment(optarg);
8286 global_text = strndup(optarg, max_user_text);
8287 convert_escapes(global_text);
8291 update_interval = strtod(optarg, 0);
8292 if (info.music_player_interval == 0) {
8293 // default to update_interval
8294 info.music_player_interval = update_interval;
8299 total_run_times = strtod(optarg, 0);
8303 gap_x = atoi(optarg);
8307 gap_y = atoi(optarg);
8321 /* generate text and get initial size */
8322 extract_variable_text(global_text);
8329 if (fork_to_background) {
8334 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
8341 fprintf(stderr, "\n");
8346 /* parent process */
8347 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
8354 text_buffer = malloc(max_user_text);
8355 memset(text_buffer, 0, max_user_text);
8356 tmpstring1 = malloc(text_buffer_size);
8357 memset(tmpstring1, 0, text_buffer_size);
8358 tmpstring2 = malloc(text_buffer_size);
8359 memset(tmpstring2, 0, text_buffer_size);
8363 update_text_area(); /* to get initial size of the window */
8366 init_window(own_window, text_width + border_margin * 2 + 1,
8367 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8369 #else /* OWN_WINDOW */
8370 init_window(0, text_width + border_margin * 2 + 1,
8371 text_height + border_margin * 2 + 1, set_transparent, 0,
8373 #endif /* OWN_WINDOW */
8376 update_text_area(); /* to position text/window on screen */
8379 if (own_window && !fixed_pos) {
8380 XMoveWindow(display, window.window, window.x, window.y);
8383 set_transparent_background(window.window);
8393 /* Set signal handlers */
8394 act.sa_handler = signal_handler;
8395 sigemptyset(&act.sa_mask);
8398 act.sa_flags |= SA_RESTART;
8401 if ( sigaction(SIGINT, &act, &oact) < 0
8402 || sigaction(SIGUSR1, &act, &oact) < 0
8403 || sigaction(SIGHUP, &act, &oact) < 0
8404 || sigaction(SIGTERM, &act, &oact) < 0) {
8405 ERR("error setting signal handler: %s", strerror(errno));
8410 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8417 static void signal_handler(int sig)
8419 /* signal handler is light as a feather, as it should be.
8420 * we will poll g_signal_pending with each loop of conky
8421 * and do any signal processing there, NOT here. */
8422 g_signal_pending = sig;