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"
46 #include <X11/Xutil.h>
48 #include <X11/extensions/Xdamage.h>
54 #include <sys/types.h>
56 #include <netinet/in.h>
69 #include "temphelper.h"
72 /* check for OS and include appropriate headers */
73 #if defined(__linux__)
75 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
77 #elif defined(__OpenBSD__)
81 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
82 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
83 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
84 int apm_getinfo(int fd, apm_info_t aip);
85 char *get_apm_adapter(void);
86 char *get_apm_battery_life(void);
87 char *get_apm_battery_time(void);
95 #include "defconfig.h"
96 #ifdef HAVE_FOPENCOOKIE
97 #include "conf_cookie.h"
102 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
105 #define MAIL_FILE "$MAIL"
106 #define MAX_IF_BLOCK_DEPTH 5
107 #define MAX_TAIL_LINES 100
109 //#define SIGNAL_BLOCKING
110 #undef SIGNAL_BLOCKING
112 /* debugging level, used by logging.h */
113 int global_debug_level = 0;
115 /* two strings for internal use */
116 static char *tmpstring1, *tmpstring2;
118 /* variables holding various config settings */
121 static int use_spacer;
122 int top_cpu, top_mem;
125 static int output_methods;
126 static volatile int g_signal_pending;
127 /* Update interval */
128 static double update_interval;
131 /* prototypes for internally used functions */
132 static void signal_handler(int);
133 static void print_version(void) __attribute__((noreturn));
134 static void reload_config(void);
135 static void clean_up(void);
137 static void print_version(void)
139 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
141 printf("\nCompiled in features:\n\n"
142 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
146 " * Xdamage extension\n"
147 # endif /* HAVE_XDAMAGE */
149 " * Xdbe extension (double buffer)\n"
150 # endif /* HAVE_XDBE */
155 "\n Music detection:\n"
158 #endif /* AUDACIOUS */
171 "\n General features:\n"
178 #ifdef TCP_PORT_MONITOR
180 #endif /* TCP_PORT_MONITOR */
189 #endif /* HAVE_IWLIB */
201 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
208 static int text_start_x, text_start_y; /* text start position in window */
209 static int text_width, text_height;
227 char name[DEFAULT_TEXT_BUFFER_SIZE];
237 static int selected_font = 0;
238 static int font_count = -1;
239 struct font_list *fonts = NULL;
243 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + \
244 fonts[selected_font].xftfont->descent) \
245 : (fonts[selected_font].font->max_bounds.ascent + \
246 fonts[selected_font].font->max_bounds.descent))
247 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent \
248 : fonts[selected_font].font->max_bounds.ascent)
249 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent \
250 : fonts[selected_font].font->max_bounds.descent)
254 #define font_height() (fonts[selected_font].font->max_bounds.ascent + \
255 fonts[selected_font].font->max_bounds.descent)
256 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
257 #define font_descent() fonts[selected_font].font->max_bounds.descent
261 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
263 static void set_font(void);
265 int addfont(const char *data_in)
267 if (font_count > MAX_FONTS) {
268 CRIT_ERR("you don't need that many fonts, sorry.");
271 if (font_count == 0) {
275 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
279 memset(fonts, 0, sizeof(struct font_list));
281 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
282 memset(&fonts[font_count], 0, sizeof(struct font_list));
284 CRIT_ERR("realloc in addfont");
286 // must account for null terminator
287 if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
288 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
290 fonts[font_count].font_alpha = 0xffff;
293 CRIT_ERR("Oops...looks like something overflowed in addfont().");
298 void set_first_font(const char *data_in)
300 if (font_count < 0) {
301 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
305 memset(fonts, 0, sizeof(struct font_list));
308 if (strlen(data_in) > 1) {
309 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
311 fonts[0].font_alpha = 0xffff;
316 void free_fonts(void)
320 for (i = 0; i <= font_count; i++) {
323 XftFontClose(display, fonts[i].xftfont);
324 fonts[i].xftfont = 0;
328 XFreeFont(display, fonts[i].font);
338 static void load_fonts(void)
342 for (i = 0; i <= font_count; i++) {
345 if (use_xft && fonts[i].xftfont) {
347 } else if (use_xft) {
348 /* if (fonts[i].xftfont != NULL && selected_font == 0) {
349 XftFontClose(display, fonts[i].xftfont);
351 fonts[i].xftfont = XftFontOpenName(display, screen,
353 if (fonts[i].xftfont != NULL) {
357 ERR("can't load Xft font '%s'", fonts[i].name);
358 if ((fonts[i].xftfont = XftFontOpenName(display, screen,
359 "courier-12")) != NULL) {
363 ERR("can't load Xft font '%s'", "courier-12");
365 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
366 CRIT_ERR("can't load font '%s'", "fixed");
373 /* load normal font */
374 /* if (fonts[i].font != NULL) {
375 XFreeFont(display, fonts[i].font);
378 if (fonts[i].font || (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
379 ERR("can't load font '%s'", fonts[i].name);
380 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
381 CRIT_ERR("can't load font '%s'", "fixed");
382 printf("loaded fixed?\n");
390 /* struct that has all info to be shared between
391 * instances of the same text object */
392 struct information info;
394 /* default config file */
395 static char *current_config;
397 /* set to 1 if you want all text to be in uppercase */
398 static unsigned int stuff_in_upper_case;
400 /* Run how many times? */
401 static unsigned long total_run_times;
404 static int fork_to_background;
406 static int cpu_avg_samples, net_avg_samples;
410 static int show_graph_scale;
411 static int show_graph_range;
413 /* Position on the screen */
414 static int text_alignment;
415 static int gap_x, gap_y;
418 static int draw_borders;
419 static int draw_graph_borders;
420 static int stippled_borders;
422 static int draw_shades, draw_outline;
424 static int border_margin, border_width;
426 static long default_fg_color, default_bg_color, default_out_color;
428 /* create own window or draw stuff to root? */
429 static int set_transparent = 0;
432 static int own_window = 0;
433 static int background_colour = 0;
435 /* fixed size/pos is set if wm/user changes them */
436 static int fixed_size = 0, fixed_pos = 0;
439 static int minimum_width, minimum_height;
440 static int maximum_width;
445 static int sensor_device;
448 static long color0, color1, color2, color3, color4, color5, color6, color7,
451 static char *template[10];
453 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
454 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
456 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
457 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
459 /* maximum size of individual text buffers, ie $exec buffer size */
460 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
463 #define CODEPAGE_LENGTH 20
465 long iconv_count = 0;
466 char iconv_converting;
467 static iconv_t **iconv_cd = 0;
469 int register_iconv(iconv_t *new_iconv)
471 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
473 CRIT_ERR("Out of memory");
475 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
476 if (!iconv_cd[iconv_count]) {
477 CRIT_ERR("Out of memory");
479 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
484 void free_iconv(void)
489 for (i = 0; i < iconv_count; i++) {
491 iconv_close(*iconv_cd[i]);
505 /* no buffers in used memory? */
508 /* pad percentages to decimals? */
509 static int pad_percents = 0;
511 static char *global_text = 0;
512 long global_text_lines;
514 static int total_updates;
516 int check_contains(char *f, char *s)
519 FILE *where = fopen(f, "r");
522 char buf1[256], buf2[256];
524 while (fgets(buf1, 256, where)) {
525 sscanf(buf1, "%255s", buf2);
526 if (strstr(buf2, s)) {
533 ERR("Could not open the file");
539 static inline int calc_text_width(const char *s, int l)
546 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
547 (const FcChar8 *) s, l, &gi);
549 XftTextExtents8(display, fonts[selected_font].xftfont,
550 (const FcChar8 *) s, l, &gi);
556 return XTextWidth(fonts[selected_font].font, s, l);
561 /* formatted text to render on screen, generated in generate_text(),
562 * drawn in draw_stuff() */
564 static char *text_buffer;
566 /* special stuff in text_buffer */
568 #define SPECIAL_CHAR '\x01'
597 unsigned long first_colour; // for graph gradient
598 unsigned long last_colour;
602 /* create specials array on heap instead of stack with introduction of
604 static struct special_t *specials = NULL;
606 static unsigned int special_count;
609 static unsigned int special_index; /* used when drawing */
612 /* why 256? cause an array of more then 256 doubles seems excessive,
613 * and who needs that kind of precision anyway? */
614 #define MAX_GRAPH_DEPTH 256
616 static struct special_t *new_special(char *buf, enum special_types t)
618 if (special_count >= max_specials) {
619 CRIT_ERR("too many special things in text");
622 buf[0] = SPECIAL_CHAR;
624 specials[special_count].type = t;
625 return &specials[special_count++];
628 long fwd_fcharfind(FILE *fp, char val, unsigned int step)
632 unsigned int count = 0;
633 static char buf[BUFSZ];
634 long orig_pos = ftell(fp);
636 long buf_size = BUFSZ;
637 char *cur_found = NULL;
639 while (count < step) {
640 if (cur_found == NULL) {
641 buf_size = fread(buf, 1, buf_size, fp);
644 cur_found = memchr(buf + buf_pos, val, buf_size - buf_pos);
645 if (cur_found != NULL) {
646 buf_pos = cur_found - buf + 1;
655 ret = ftell(fp) - buf_size + buf_pos - 1;
657 fseek(fp, orig_pos, SEEK_SET);
662 void *memrchr(const void *buffer, char c, size_t n)
664 const unsigned char *p = buffer;
666 for (p += n; n; n--) {
675 long rev_fcharfind(FILE *fp, char val, unsigned int step)
679 unsigned int count = 0;
680 static char buf[BUFSZ];
681 long orig_pos = ftell(fp);
683 long file_pos = orig_pos;
684 long buf_size = BUFSZ;
687 while (count < step) {
689 if (file_pos > BUFSZ) {
690 fseek(fp, file_pos - BUFSZ, SEEK_SET);
693 fseek(fp, 0, SEEK_SET);
695 file_pos = ftell(fp);
696 buf_pos = fread(buf, 1, buf_size, fp);
698 cur_found = memrchr(buf, val, (size_t) buf_pos);
699 if (cur_found != NULL) {
700 buf_pos = cur_found - buf;
709 fseek(fp, orig_pos, SEEK_SET);
711 ret = file_pos + buf_pos;
716 static void new_bar(char *buf, int w, int h, int usage)
718 struct special_t *s = new_special(buf, BAR);
720 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
725 static const char *scan_bar(const char *args, int *w, int *h)
727 /* zero width means all space that is available */
730 /* bar's argument is either height or height,width */
734 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
735 sscanf(args, "%d %n", h, &n);
743 static char *scan_font(const char *args)
746 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
753 static void new_font(char *buf, char *args)
756 struct special_t *s = new_special(buf, FONT);
758 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
759 int tmp = selected_font;
761 selected_font = s->font_added = addfont(args);
766 struct special_t *s = new_special(buf, FONT);
767 int tmp = selected_font;
769 selected_font = s->font_added = 0;
774 void graph_append(struct special_t *graph, double f, char showaslog)
784 if (!graph->scaled && f > graph->graph_scale) {
785 f = graph->graph_scale;
788 /* Already happens in new_graph
790 graph->graph_scale = 1;
793 graph->graph[0] = f; /* add new data */
794 /* shift all the data by 1 */
795 for (i = graph->graph_width - 1; i > 0; i--) {
796 graph->graph[i] = graph->graph[i - 1];
797 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
798 /* check if we need to update the scale */
799 graph->graph_scale = graph->graph[i];
804 short colour_depth = 0;
805 void set_up_gradient(void);
807 /* precalculated: 31/255, and 63/255 */
808 #define CONST_8_TO_5_BITS 0.12156862745098
809 #define CONST_8_TO_6_BITS 0.247058823529412
811 /* adjust color values depending on color depth */
812 static unsigned int adjust_colors(unsigned int color)
816 if (colour_depth == 0) {
819 if (colour_depth == 16) {
820 r = (color & 0xff0000) >> 16;
821 g = (color & 0xff00) >> 8;
823 color = (int) (r * CONST_8_TO_5_BITS) << 11;
824 color |= (int) (g * CONST_8_TO_6_BITS) << 5;
825 color |= (int) (b * CONST_8_TO_5_BITS);
830 static void new_graph(char *buf, int w, int h, unsigned int first_colour,
831 unsigned int second_colour, double i, int scale, int append, char showaslog)
833 struct special_t *s = new_special(buf, GRAPH);
836 if (s->graph == NULL) {
837 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
838 // subtract 2 for the box
839 s->graph_width = s->width /* - 2 */;
841 s->graph_width = MAX_GRAPH_DEPTH - 2;
843 s->graph = malloc(s->graph_width * sizeof(double));
844 memset(s->graph, 0, s->graph_width * sizeof(double));
845 s->graph_scale = 100;
848 s->first_colour = adjust_colors(first_colour);
849 s->last_colour = adjust_colors(second_colour);
852 s->graph_scale = scale;
860 s->graph_width = s->width - 2; // subtract 2 for rectangle around
864 s->graph_scale = log10(s->graph_scale + 1);
868 graph_append(s, i, showaslog);
872 //don't use spaces in LOGGRAPH or NORMGRAPH if you change them
873 #define LOGGRAPH "log"
874 #define NORMGRAPH "normal"
876 static char *scan_graph(const char *args, int *w, int *h,
877 unsigned int *first_colour, unsigned int *last_colour,
878 unsigned int *scale, char *showaslog)
880 const char *nographtype;
884 /* zero width means all space that is available */
891 //set showaslog and place the rest of the args in nographtype
892 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
895 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
898 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
900 nographtype = &args[strlen(LOGGRAPH) + 1];
901 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
903 nographtype = &args[strlen(NORMGRAPH) + 1];
908 //check the rest of the args
909 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
913 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
916 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
917 return strndup(buf, text_buffer_size);
920 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
921 return strndup(buf, text_buffer_size);
926 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
930 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
933 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
934 return strndup(buf, text_buffer_size);
937 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
938 return strndup(buf, text_buffer_size);
943 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
947 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
950 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
952 //TODO: check the return value and throw an error?
953 sscanf(nographtype, "%63s %d,%d", buf, h, w);
956 return strndup(buf, text_buffer_size);
959 if (buf[0] == '\0') {
962 return strndup(buf, text_buffer_size);
966 static inline void new_hr(char *buf, int a)
968 new_special(buf, HORIZONTAL_LINE)->height = a;
971 static inline void new_stippled_hr(char *buf, int a, int b)
973 struct special_t *s = new_special(buf, STIPPLED_HR);
979 static inline void new_fg(char *buf, long c)
981 new_special(buf, FG)->arg = c;
984 static inline void new_bg(char *buf, long c)
986 new_special(buf, BG)->arg = c;
989 static inline void new_outline(char *buf, long c)
991 new_special(buf, OUTLINE)->arg = c;
994 static inline void new_offset(char *buf, long c)
996 new_special(buf, OFFSET)->arg = c;
999 static inline void new_voffset(char *buf, long c)
1001 new_special(buf, VOFFSET)->arg = c;
1004 static inline void new_alignr(char *buf, long c)
1006 new_special(buf, ALIGNR)->arg = c;
1009 // A postive offset pushes the text further left
1010 static inline void new_alignc(char *buf, long c)
1012 new_special(buf, ALIGNC)->arg = c;
1015 static inline void new_goto(char *buf, long c)
1017 new_special(buf, GOTO)->arg = c;
1020 static inline void new_tab(char *buf, int a, int b)
1022 struct special_t *s = new_special(buf, TAB);
1028 /* quite boring functions */
1030 static inline void for_each_line(char *b, void f(char *))
1034 for (ps = b, pe = b; *pe; pe++) {
1048 static void convert_escapes(char *buf)
1050 char *p = buf, *s = buf;
1057 } else if (*s == '\\') {
1068 /* Prints anything normally printed with snprintf according to the current value
1069 * of use_spacer. Actually slightly more flexible than snprintf, as you can
1070 * safely specify the destination buffer as one of your inputs. */
1071 static int spaced_print(char *, int, const char *, int, const char *, ...)
1072 __attribute__((format(printf, 3, 6)));
1074 static int spaced_print(char *buf, int size, const char *format, int width,
1075 const char *func_name, ...) {
1083 tempbuf = malloc(size * sizeof(char));
1085 // Passes the varargs along to vsnprintf
1086 va_start(argp, func_name);
1087 vsnprintf(tempbuf, size, format, argp);
1090 switch (use_spacer) {
1092 len = snprintf(buf, size, "%s", tempbuf);
1095 len = snprintf(buf, size, "%*s", width - 1, tempbuf);
1098 len = snprintf(buf, size, "%-*s", width - 1, tempbuf);
1101 CRIT_ERR("%s encountered invalid use_spacer value (%d)", func_name,
1110 /* converts from bytes to human readable format (k, M, G, T) */
1111 static void human_readable(long long num, char *buf, int size, const char *func_name)
1113 const char **suffix = suffixes;
1116 static const int WIDTH = 10, SHORT_WIDTH = 8;
1118 if (num < 1024LL && num > -1024LL) {
1120 spaced_print(buf, size, "%lld%c", SHORT_WIDTH, func_name, num,
1123 spaced_print(buf, size, "%lld%s", WIDTH, func_name, num, *suffix);
1128 while ((num / 1024 >= 1000LL || num / 1024 <= -1024LL) && **(suffix + 2)) {
1134 fnum = num / 1024.0;
1139 if (precision < 0) {
1143 len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH, func_name,
1144 precision, fnum, **suffix);
1146 len = spaced_print(buf, size, "%.*f%s", WIDTH, func_name, precision,
1149 } while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
1152 /* global object list root element */
1153 static struct text_object global_root_object;
1155 static void generate_text_internal(char *p, int p_max_size,
1156 struct text_object text_object, struct information *cur);
1158 static inline void read_exec(const char *data, char *buf, const int size)
1160 FILE *fp = popen(data, "r");
1161 int length = fread(buf, 1, size, fp);
1165 if (length > 0 && buf[length - 1] == '\n') {
1166 buf[length - 1] = '\0';
1170 void *threaded_exec(void *) __attribute__((noreturn));
1172 void *threaded_exec(void *arg)
1175 struct text_object *obj = (struct text_object *)arg;
1178 p2 = obj->data.texeci.buffer;
1179 timed_thread_lock(obj->data.texeci.p_timed_thread);
1180 read_exec(obj->data.texeci.cmd, obj->data.texeci.buffer,
1183 if (*p2 == '\001') {
1188 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1189 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
1190 timed_thread_exit(obj->data.texeci.p_timed_thread);
1196 static struct text_object *new_text_object_internal(void)
1198 struct text_object *obj = malloc(sizeof(struct text_object));
1199 memset(obj, 0, sizeof(struct text_object));
1204 * call with full == 0 when freeing after 'internal' evaluation of objects
1206 static void free_text_objects(struct text_object *root, char full)
1208 struct text_object *obj;
1214 #define data obj->data
1215 for (obj = root->prev; obj; obj = root->prev) {
1216 root->prev = obj->prev;
1217 switch (obj->type) {
1225 close(data.sysfs.fd);
1227 #endif /* !__OpenBSD__ */
1233 free(data.tztime.tz);
1234 free(data.tztime.fmt);
1237 free(data.mboxscan.args);
1238 free(data.mboxscan.output);
1242 free(data.local_mail.box);
1247 case OBJ_imap_unseen:
1248 if (!obj->global_mode) {
1252 case OBJ_imap_messages:
1253 if (!obj->global_mode) {
1260 case OBJ_pop3_unseen:
1261 if (!obj->global_mode) {
1266 if (!obj->global_mode) {
1271 case OBJ_if_existing:
1272 case OBJ_if_mounted:
1273 case OBJ_if_running:
1274 free(data.ifblock.s);
1275 free(data.ifblock.str);
1278 free(data.tail.logfile);
1279 free(data.tail.buffer);
1291 case OBJ_iconv_start:
1296 case OBJ_disk_protect:
1300 free(obj->data.ifblock.s);
1301 free(obj->data.ifblock.str);
1304 free(obj->data.ifblock.s);
1305 free(obj->data.ifblock.str);
1308 if (info.gw_info.iface) {
1309 free(info.gw_info.iface);
1310 info.gw_info.iface = 0;
1312 if (info.gw_info.ip) {
1313 free(info.gw_info.ip);
1314 info.gw_info.ip = 0;
1317 case OBJ_ioscheduler:
1323 case OBJ_xmms2_artist:
1324 if (info.xmms2.artist) {
1325 free(info.xmms2.artist);
1326 info.xmms2.artist = 0;
1329 case OBJ_xmms2_album:
1330 if (info.xmms2.album) {
1331 free(info.xmms2.album);
1332 info.xmms2.album = 0;
1335 case OBJ_xmms2_title:
1336 if (info.xmms2.title) {
1337 free(info.xmms2.title);
1338 info.xmms2.title = 0;
1341 case OBJ_xmms2_genre:
1342 if (info.xmms2.genre) {
1343 free(info.xmms2.genre);
1344 info.xmms2.genre = 0;
1347 case OBJ_xmms2_comment:
1348 if (info.xmms2.comment) {
1349 free(info.xmms2.comment);
1350 info.xmms2.comment = 0;
1354 if (info.xmms2.url) {
1355 free(info.xmms2.url);
1359 case OBJ_xmms2_date:
1360 if (info.xmms2.date) {
1361 free(info.xmms2.date);
1362 info.xmms2.date = 0;
1365 case OBJ_xmms2_status:
1366 if (info.xmms2.status) {
1367 free(info.xmms2.status);
1368 info.xmms2.status = 0;
1371 case OBJ_xmms2_playlist:
1372 if (info.xmms2.playlist) {
1373 free(info.xmms2.playlist);
1374 info.xmms2.playlist = 0;
1377 case OBJ_xmms2_smart:
1378 if (info.xmms2.artist) {
1379 free(info.xmms2.artist);
1380 info.xmms2.artist = 0;
1382 if (info.xmms2.title) {
1383 free(info.xmms2.title);
1384 info.xmms2.title = 0;
1386 if (info.xmms2.url) {
1387 free(info.xmms2.url);
1393 case OBJ_bmpx_title:
1394 case OBJ_bmpx_artist:
1395 case OBJ_bmpx_album:
1396 case OBJ_bmpx_track:
1398 case OBJ_bmpx_bitrate:
1408 free(data.rss.action);
1417 case OBJ_battery_time:
1420 #endif /* !__OpenBSD__ */
1424 case OBJ_execigraph:
1425 free(data.execi.cmd);
1426 free(data.execi.buffer);
1429 free(data.texeci.cmd);
1430 free(data.texeci.buffer);
1432 case OBJ_nameserver:
1437 if (info.first_process) {
1438 free_all_processes();
1439 info.first_process = NULL;
1444 free(data.hddtemp.dev);
1445 free(data.hddtemp.addr);
1446 if (data.hddtemp.temp)
1447 free(data.hddtemp.temp);
1450 case OBJ_entropy_avail:
1451 case OBJ_entropy_poolsize:
1452 case OBJ_entropy_bar:
1454 case OBJ_user_names:
1455 if (info.users.names) {
1456 free(info.users.names);
1457 info.users.names = 0;
1460 case OBJ_user_terms:
1461 if (info.users.terms) {
1462 free(info.users.terms);
1463 info.users.terms = 0;
1466 case OBJ_user_times:
1467 if (info.users.times) {
1468 free(info.users.times);
1469 info.users.times = 0;
1474 case OBJ_smapi_bat_perc:
1475 case OBJ_smapi_bat_temp:
1476 case OBJ_smapi_bat_power:
1479 case OBJ_if_smapi_bat_installed:
1480 free(data.ifblock.s);
1481 free(data.ifblock.str);
1490 case OBJ_mpd_artist:
1492 case OBJ_mpd_random:
1493 case OBJ_mpd_repeat:
1495 case OBJ_mpd_bitrate:
1496 case OBJ_mpd_status:
1499 case OBJ_mpd_password:
1501 case OBJ_mpd_elapsed:
1502 case OBJ_mpd_length:
1506 case OBJ_mpd_percent:
1508 case OBJ_if_mpd_playing:
1510 free_mpd_vars(&info.mpd);
1518 case OBJ_moc_artist:
1521 case OBJ_moc_totaltime:
1522 case OBJ_moc_timeleft:
1523 case OBJ_moc_curtime:
1524 case OBJ_moc_bitrate:
1526 free_moc(&info.moc);
1530 free(data.scroll.text);
1536 /* FIXME: below is surely useless */
1537 if (full) {} // disable warning when MPD !defined
1540 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1545 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1546 *a = mixer_init(buf1);
1547 scan_bar(arg + n, w, h);
1549 *a = mixer_init(NULL);
1550 scan_bar(arg, w, h);
1554 /* strip a leading /dev/ if any, following symlinks first
1556 * BEWARE: this function returns a pointer to static content
1557 * which gets overwritten in consecutive calls. I.e.:
1558 * this function is NOT reentrant.
1560 const char *dev_name(const char *path)
1562 static char buf[255]; /* should be enough for pathnames */
1565 #define DEV_NAME(x) \
1566 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1567 if ((buflen = readlink(path, buf, 254)) == -1)
1568 return DEV_NAME(path);
1570 return DEV_NAME(buf);
1574 /* construct_text_object() creates a new text_object */
1575 static struct text_object *construct_text_object(const char *s,
1576 const char *arg, long line, char allow_threaded)
1578 // struct text_object *obj = new_text_object();
1579 struct text_object *obj = new_text_object_internal();
1583 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1584 obj->type = OBJ_##a; need_mask |= (1 << n); {
1585 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1586 obj->type = OBJ_##a; need_mask |= (1 << n); {
1587 #define END } } else
1591 obj->type = OBJ_color;
1592 obj->data.l = get_x11_color(s);
1596 OBJ(freq, INFO_FREQ)
1599 obj->data.i = open_acpi_temperature(arg);
1600 END OBJ(acpiacadapter, 0)
1601 END OBJ(freq, INFO_FREQ)
1602 #endif /* !__OpenBSD__ */
1604 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1605 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1606 obj->data.cpu_index = 1;
1607 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1608 "Displaying the clock for CPU 1."); */
1610 obj->data.cpu_index = atoi(&arg[0]);
1613 END OBJ(freq_g, INFO_FREQ)
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_g: Invalid CPU number or you don't have that many "
1619 "CPUs! Displaying the clock for CPU 1."); */
1621 obj->data.cpu_index = atoi(&arg[0]);
1624 #if defined(__linux__)
1625 END OBJ(voltage_mv, 0)
1627 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1628 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1629 obj->data.cpu_index = 1;
1630 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1631 "CPUs! Displaying voltage for CPU 1."); */
1633 obj->data.cpu_index = atoi(&arg[0]);
1636 END OBJ(voltage_v, 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_v: 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]);
1649 END OBJ(wireless_essid, INFO_NET)
1651 obj->data.net = get_net_stat(arg);
1653 CRIT_ERR("wireless_essid: needs an argument");
1655 END OBJ(wireless_mode, INFO_NET)
1657 obj->data.net = get_net_stat(arg);
1659 CRIT_ERR("wireless_mode: needs an argument");
1661 END OBJ(wireless_bitrate, INFO_NET)
1663 obj->data.net = get_net_stat(arg);
1665 CRIT_ERR("wireless_bitrate: needs an argument");
1667 END OBJ(wireless_ap, INFO_NET)
1669 obj->data.net = get_net_stat(arg);
1671 CRIT_ERR("wireless_ap: needs an argument");
1673 END OBJ(wireless_link_qual, INFO_NET)
1675 obj->data.net = get_net_stat(arg);
1677 CRIT_ERR("wireless_link_qual: needs an argument");
1679 END OBJ(wireless_link_qual_max, INFO_NET)
1681 obj->data.net = get_net_stat(arg);
1683 CRIT_ERR("wireless_link_qual_max: needs an argument");
1685 END OBJ(wireless_link_qual_perc, INFO_NET)
1687 obj->data.net = get_net_stat(arg);
1689 CRIT_ERR("wireless_link_qual_perc: needs an argument");
1691 END OBJ(wireless_link_bar, INFO_NET)
1693 arg = scan_bar(arg, &obj->a, &obj->b);
1694 obj->data.net = get_net_stat(arg);
1696 CRIT_ERR("wireless_link_bar: needs an argument");
1698 #endif /* HAVE_IWLIB */
1700 #endif /* __linux__ */
1708 sscanf(arg, "%63s", bat);
1710 strcpy(bat, "BAT0");
1712 obj->data.s = strndup(bat, text_buffer_size);
1713 END OBJ(battery_time, 0)
1717 sscanf(arg, "%63s", bat);
1719 strcpy(bat, "BAT0");
1721 obj->data.s = strndup(bat, text_buffer_size);
1722 END OBJ(battery_percent, 0)
1726 sscanf(arg, "%63s", bat);
1728 strcpy(bat, "BAT0");
1730 obj->data.s = strndup(bat, text_buffer_size);
1731 END OBJ(battery_bar, 0)
1735 arg = scan_bar(arg, &obj->a, &obj->b);
1736 sscanf(arg, "%63s", bat);
1738 strcpy(bat, "BAT0");
1740 obj->data.s = strndup(bat, text_buffer_size);
1741 #endif /* !__OpenBSD__ */
1743 #if defined(__linux__)
1744 END OBJ(disk_protect, 0)
1746 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1748 CRIT_ERR("disk_protect needs an argument");
1749 END OBJ(i8k_version, INFO_I8K)
1750 END OBJ(i8k_bios, INFO_I8K)
1751 END OBJ(i8k_serial, INFO_I8K)
1752 END OBJ(i8k_cpu_temp, INFO_I8K)
1753 END OBJ(i8k_left_fan_status, INFO_I8K)
1754 END OBJ(i8k_right_fan_status, INFO_I8K)
1755 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1756 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1757 END OBJ(i8k_ac_status, INFO_I8K)
1758 END OBJ(i8k_buttons_status, INFO_I8K)
1760 END OBJ(ibm_temps, 0)
1762 CRIT_ERR("ibm_temps: needs an argument");
1764 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1765 obj->data.sensor = 0;
1766 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1767 "Using 0 (CPU temp sensor).");
1769 obj->data.sensor = atoi(&arg[0]);
1770 END OBJ(ibm_volume, 0)
1771 END OBJ(ibm_brightness, 0)
1774 ERR("if_up needs an argument");
1775 obj->data.ifblock.s = 0;
1777 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1778 obj_be_ifblock_if(obj);
1780 obj_be_ifblock_if(obj);
1781 END OBJ(ioscheduler, 0)
1783 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1786 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1787 END OBJ(laptop_mode, 0)
1788 END OBJ(pb_battery, 0)
1789 if (arg && strcmp(arg, "status") == EQUAL) {
1790 obj->data.i = PB_BATT_STATUS;
1791 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1792 obj->data.i = PB_BATT_PERCENT;
1793 } else if (arg && strcmp(arg, "time") == EQUAL) {
1794 obj->data.i = PB_BATT_TIME;
1796 ERR("pb_battery: needs one argument: status, percent or time");
1801 #endif /* __linux__ */
1802 #if defined(__OpenBSD__)
1803 END OBJ(obsd_sensors_temp, 0)
1805 CRIT_ERR("obsd_sensors_temp: needs an argument");
1807 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1808 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1809 obj->data.sensor = 0;
1810 ERR("Invalid temperature sensor number!");
1812 obj->data.sensor = atoi(&arg[0]);
1813 END OBJ(obsd_sensors_fan, 0)
1815 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1818 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1819 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1820 obj->data.sensor = 0;
1821 ERR("Invalid fan sensor number!");
1823 obj->data.sensor = atoi(&arg[0]);
1824 END OBJ(obsd_sensors_volt, 0)
1826 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1829 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1830 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1831 obj->data.sensor = 0;
1832 ERR("Invalid voltage sensor number!");
1834 obj->data.sensor = atoi(&arg[0]);
1835 END OBJ(obsd_vendor, 0)
1836 END OBJ(obsd_product, 0)
1837 #endif /* __OpenBSD__ */
1838 END OBJ(buffers, INFO_BUFFERS)
1839 END OBJ(cached, INFO_BUFFERS)
1840 END OBJ(cpu, INFO_CPU)
1842 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
1843 obj->data.cpu_index = atoi(&arg[3]);
1846 obj->data.cpu_index = 0;
1848 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1850 obj->data.cpu_index = 0;
1852 END OBJ(cpubar, INFO_CPU)
1854 if (strncmp(arg, "cpu", 3) == EQUAL && isdigit(arg[3])) {
1855 obj->data.cpu_index = atoi(&arg[3]);
1858 obj->data.cpu_index = 0;
1860 scan_bar(arg, &obj->a, &obj->b);
1862 scan_bar(arg, &obj->a, &obj->b);
1863 obj->data.cpu_index = 0;
1865 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1866 END OBJ(cpugraph, INFO_CPU)
1867 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1868 &obj->e, &obj->showaslog);
1871 if (strncmp(buf, "cpu", 3) == EQUAL && isdigit(buf[3])) {
1872 obj->data.cpu_index = atoi(&buf[3]);
1874 obj->data.cpu_index = 0;
1878 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1879 END OBJ(loadgraph, INFO_LOADAVG)
1880 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1881 &obj->e, &obj->showaslog);
1885 r = sscanf(arg, "%d", &a);
1887 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1890 #if defined(__linux__)
1891 END OBJ(diskio, INFO_DISKIO)
1893 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1895 obj->data.diskio = NULL;
1897 END OBJ(diskio_read, INFO_DISKIO)
1899 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1901 obj->data.diskio = NULL;
1903 END OBJ(diskio_write, INFO_DISKIO)
1905 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1907 obj->data.diskio = NULL;
1909 END OBJ(diskiograph, INFO_DISKIO)
1910 char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
1911 &obj->e, &obj->showaslog);
1914 obj->data.diskio = prepare_diskio_stat(buf);
1917 obj->data.diskio = NULL;
1919 END OBJ(diskiograph_read, INFO_DISKIO)
1920 char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
1921 &obj->e, &obj->showaslog);
1924 obj->data.diskio = prepare_diskio_stat(buf);
1927 obj->data.diskio = NULL;
1929 END OBJ(diskiograph_write, INFO_DISKIO)
1930 char *buf = scan_graph(dev_name(arg), &obj->a, &obj->b, &obj->c, &obj->d,
1931 &obj->e, &obj->showaslog);
1934 obj->data.diskio = prepare_diskio_stat(buf);
1937 obj->data.diskio = NULL;
1942 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1945 obj->data.l = color0;
1947 obj->data.l = color1;
1949 obj->data.l = color2;
1951 obj->data.l = color3;
1953 obj->data.l = color4;
1955 obj->data.l = color5;
1957 obj->data.l = color6;
1959 obj->data.l = color7;
1961 obj->data.l = color8;
1963 obj->data.l = color9;
1965 obj->data.s = scan_font(arg);
1966 END OBJ(conky_version, 0)
1967 END OBJ(conky_build_date, 0)
1968 END OBJ(conky_build_arch, 0)
1969 END OBJ(downspeed, INFO_NET)
1971 obj->data.net = get_net_stat(arg);
1973 CRIT_ERR("downspeed needs argument");
1975 END OBJ(downspeedf, INFO_NET)
1977 obj->data.net = get_net_stat(arg);
1979 CRIT_ERR("downspeedf needs argument");
1981 END OBJ(downspeedgraph, INFO_NET)
1982 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1983 &obj->e, &obj->showaslog);
1985 // default to DEFAULTNETDEV
1986 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
1987 obj->data.net = get_net_stat(buf);
1990 obj_be_ifblock_else(obj);
1992 obj_be_ifblock_endif(obj);
1994 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1997 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1999 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2001 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2002 END OBJ(execgraph, 0)
2003 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
2004 END OBJ(execibar, 0)
2007 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2010 ERR("${execibar <interval> command}");
2011 obj->type = OBJ_text;
2012 snprintf(buf, 256, "${%s}", s);
2013 obj->data.s = strndup(buf, text_buffer_size);
2015 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2017 END OBJ(execigraph, 0)
2020 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2023 ERR("${execigraph <interval> command}");
2024 obj->type = OBJ_text;
2025 snprintf(buf, 256, "${%s}", s);
2026 obj->data.s = strndup(buf, text_buffer_size);
2028 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2033 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2036 ERR("${execi <interval> command}");
2037 obj->type = OBJ_text;
2038 snprintf(buf, 256, "${%s}", s);
2039 obj->data.s = strndup(buf, text_buffer_size);
2041 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2042 obj->data.execi.buffer = malloc(text_buffer_size);
2047 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2050 ERR("${execi <interval> command}");
2051 obj->type = OBJ_text;
2052 snprintf(buf, 256, "${%s}", s);
2053 obj->data.s = strndup(buf, text_buffer_size);
2055 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
2056 obj->data.execi.buffer = malloc(text_buffer_size);
2058 END OBJ_THREAD(texeci, 0)
2061 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2064 ERR("${texeci <interval> command}");
2065 obj->type = OBJ_text;
2066 snprintf(buf, 256, "${%s}", s);
2067 obj->data.s = strndup(buf, text_buffer_size);
2069 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
2070 obj->data.texeci.buffer = malloc(text_buffer_size);
2072 obj->data.texeci.p_timed_thread = NULL;
2073 END OBJ(pre_exec, 0)
2074 obj->type = OBJ_text;
2078 read_exec(arg, buf, sizeof(buf));
2079 obj->data.s = strndup(buf, text_buffer_size);
2081 obj->data.s = strndup("", text_buffer_size);
2084 END OBJ(fs_bar, INFO_FS)
2085 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2087 while (isspace(*arg)) {
2096 obj->data.fsbar.fs = prepare_fs_stat(arg);
2097 END OBJ(fs_bar_free, INFO_FS)
2098 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2100 while (isspace(*arg)) {
2110 obj->data.fsbar.fs = prepare_fs_stat(arg);
2111 END OBJ(fs_free, INFO_FS)
2115 obj->data.fs = prepare_fs_stat(arg);
2116 END OBJ(fs_used_perc, INFO_FS)
2120 obj->data.fs = prepare_fs_stat(arg);
2121 END OBJ(fs_free_perc, INFO_FS)
2125 obj->data.fs = prepare_fs_stat(arg);
2126 END OBJ(fs_size, INFO_FS)
2130 obj->data.fs = prepare_fs_stat(arg);
2131 END OBJ(fs_type, INFO_FS)
2135 obj->data.fs = prepare_fs_stat(arg);
2136 END OBJ(fs_used, INFO_FS)
2140 obj->data.fs = prepare_fs_stat(arg);
2142 obj->data.i = arg ? atoi(arg) : 1;
2143 END OBJ(nameserver, INFO_DNS)
2144 obj->data.i = arg ? atoi(arg) : 0;
2146 obj->data.i = arg ? atoi(arg) : 1;
2148 obj->data.i = arg ? atoi(arg) : 1;
2152 ERR("goto needs arguments");
2153 obj->type = OBJ_text;
2154 obj->data.s = strndup("${goto}", text_buffer_size);
2158 obj->data.i = atoi(arg);
2164 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2165 sscanf(arg, "%d", &b);
2171 obj->data.pair.a = a;
2172 obj->data.pair.b = b;
2175 END OBJ(i2c, INFO_SYSFS)
2176 char buf1[64], buf2[64];
2180 ERR("i2c needs arguments");
2181 obj->type = OBJ_text;
2182 // obj->data.s = strndup("${i2c}", text_buffer_size);
2186 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2187 /* if scanf couldn't read three values, read type and num and use
2189 sscanf(arg, "%63s %d", buf2, &n);
2190 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
2191 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2192 strncpy(obj->data.sysfs.type, buf2, 63);
2194 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
2195 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2196 strncpy(obj->data.sysfs.type, buf2, 63);
2199 END OBJ(platform, INFO_SYSFS)
2200 char buf1[64], buf2[64];
2204 ERR("platform needs arguments");
2205 obj->type = OBJ_text;
2209 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2210 /* if scanf couldn't read three values, read type and num and use
2212 sscanf(arg, "%63s %d", buf2, &n);
2213 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
2214 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2215 strncpy(obj->data.sysfs.type, buf2, 63);
2217 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
2218 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2219 strncpy(obj->data.sysfs.type, buf2, 63);
2222 END OBJ(hwmon, INFO_SYSFS)
2223 char buf1[64], buf2[64];
2227 ERR("hwmon needs argumanets");
2228 obj->type = OBJ_text;
2232 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2233 /* if scanf couldn't read three values, read type and num and use
2235 sscanf(arg, "%63s %d", buf2, &n);
2236 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
2237 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2238 strncpy(obj->data.sysfs.type, buf2, 63);
2240 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
2241 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2242 strncpy(obj->data.sysfs.type, buf2, 63);
2244 #endif /* !__OpenBSD__ */
2246 END OBJ(top, INFO_TOP)
2251 ERR("top needs arguments");
2252 obj->type = OBJ_text;
2253 // obj->data.s = strndup("${top}", text_buffer_size);
2256 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2257 if (strcmp(buf, "name") == EQUAL) {
2258 obj->data.top.type = TOP_NAME;
2259 } else if (strcmp(buf, "cpu") == EQUAL) {
2260 obj->data.top.type = TOP_CPU;
2261 } else if (strcmp(buf, "pid") == EQUAL) {
2262 obj->data.top.type = TOP_PID;
2263 } else if (strcmp(buf, "mem") == EQUAL) {
2264 obj->data.top.type = TOP_MEM;
2265 } else if (strcmp(buf, "time") == EQUAL) {
2266 obj->data.top.type = TOP_TIME;
2267 } else if (strcmp(buf, "mem_res") == EQUAL) {
2268 obj->data.top.type = TOP_MEM_RES;
2269 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
2270 obj->data.top.type = TOP_MEM_VSIZE;
2272 ERR("invalid arg for top");
2275 if (n < 1 || n > 10) {
2276 CRIT_ERR("invalid arg for top");
2279 obj->data.top.num = n - 1;
2283 ERR("invalid args given for top");
2286 END OBJ(top_mem, INFO_TOP)
2291 ERR("top_mem needs arguments");
2292 obj->type = OBJ_text;
2293 obj->data.s = strndup("${top_mem}", text_buffer_size);
2296 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2297 if (strcmp(buf, "name") == EQUAL) {
2298 obj->data.top.type = TOP_NAME;
2299 } else if (strcmp(buf, "cpu") == EQUAL) {
2300 obj->data.top.type = TOP_CPU;
2301 } else if (strcmp(buf, "pid") == EQUAL) {
2302 obj->data.top.type = TOP_PID;
2303 } else if (strcmp(buf, "mem") == EQUAL) {
2304 obj->data.top.type = TOP_MEM;
2305 } else if (strcmp(buf, "time") == EQUAL) {
2306 obj->data.top.type = TOP_TIME;
2307 } else if (strcmp(buf, "mem_res") == EQUAL) {
2308 obj->data.top.type = TOP_MEM_RES;
2309 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
2310 obj->data.top.type = TOP_MEM_VSIZE;
2312 ERR("invalid arg for top");
2315 if (n < 1 || n > 10) {
2316 CRIT_ERR("invalid arg for top");
2319 obj->data.top.num = n - 1;
2323 ERR("invalid args given for top");
2326 END OBJ(addr, INFO_NET)
2328 obj->data.net = get_net_stat(arg);
2330 CRIT_ERR("addr needs argument");
2332 #if defined(__linux__)
2333 END OBJ(addrs, INFO_NET)
2335 obj->data.net = get_net_stat(arg);
2337 CRIT_ERR("addrs needs argument");
2339 #endif /* __linux__ */
2346 ERR("tail needs arguments");
2347 obj->type = OBJ_text;
2348 obj->data.s = strndup("${tail}", text_buffer_size);
2351 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2352 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2353 CRIT_ERR("invalid arg for tail, number of lines must be "
2354 "between 1 and %i", MAX_TAIL_LINES);
2360 obj->data.tail.fd = -1;
2362 if (stat(buf, &st) == 0) {
2363 if (S_ISFIFO(st.st_mode)) {
2364 fd = open(buf, O_RDONLY | O_NONBLOCK);
2367 CRIT_ERR("tail logfile does not exist, or you do "
2368 "not have correct permissions");
2371 obj->data.tail.fd = fd;
2373 fp = fopen(buf, "r");
2377 if (fp || obj->data.tail.fd != -1) {
2378 obj->data.tail.logfile = malloc(text_buffer_size);
2379 strcpy(obj->data.tail.logfile, buf);
2380 obj->data.tail.wantedlines = n1;
2381 obj->data.tail.interval = update_interval * 2;
2383 if (obj->data.tail.fd == -1) {
2388 CRIT_ERR("tail logfile does not exist, or you do not have "
2389 "correct permissions");
2392 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2393 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2394 CRIT_ERR("invalid arg for tail, number of lines must be "
2395 "between 1 and %i", MAX_TAIL_LINES);
2397 } else if (n2 < 1 || n2 < update_interval) {
2398 CRIT_ERR("invalid arg for tail, interval must be greater than "
2399 "0 and "PACKAGE_NAME"'s interval");
2405 obj->data.tail.fd = -1;
2407 if (stat(buf, &st) == 0) {
2408 if (S_ISFIFO(st.st_mode)) {
2409 fd = open(buf, O_RDONLY | O_NONBLOCK);
2412 CRIT_ERR("tail logfile does not exist, or you do "
2413 "not have correct permissions");
2416 obj->data.tail.fd = fd;
2418 fp = fopen(buf, "r");
2422 if (fp || obj->data.tail.fd != -1) {
2423 obj->data.tail.logfile = malloc(text_buffer_size);
2424 strcpy(obj->data.tail.logfile, buf);
2425 obj->data.tail.wantedlines = n1;
2426 obj->data.tail.interval = n2;
2428 if (obj->data.tail.fd == -1) {
2433 CRIT_ERR("tail logfile does not exist, or you do not have "
2434 "correct permissions");
2438 ERR("invalid args given for tail");
2441 /* asumming all else worked */
2442 obj->data.tail.buffer = malloc(text_buffer_size * 20);
2448 ERR("head needs arguments");
2449 obj->type = OBJ_text;
2450 obj->data.s = strndup("${head}", text_buffer_size);
2453 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2454 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2455 CRIT_ERR("invalid arg for head, number of lines must be "
2456 "between 1 and %i", MAX_TAIL_LINES);
2461 fp = fopen(buf, "r");
2463 obj->data.tail.logfile = malloc(text_buffer_size);
2464 strcpy(obj->data.tail.logfile, buf);
2465 obj->data.tail.wantedlines = n1;
2466 obj->data.tail.interval = update_interval * 2;
2470 CRIT_ERR("head logfile does not exist, or you do not have "
2471 "correct permissions");
2474 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2475 if (n1 < 1 || n1 > MAX_TAIL_LINES) {
2476 CRIT_ERR("invalid arg for head, number of lines must be "
2477 "between 1 and %i", MAX_TAIL_LINES);
2479 } else if (n2 < 1 || n2 < update_interval) {
2480 CRIT_ERR("invalid arg for head, interval must be greater than "
2481 "0 and "PACKAGE_NAME"'s interval");
2486 fp = fopen(buf, "r");
2488 obj->data.tail.logfile = malloc(text_buffer_size);
2489 strcpy(obj->data.tail.logfile, buf);
2490 obj->data.tail.wantedlines = n1;
2491 obj->data.tail.interval = n2;
2495 CRIT_ERR("head logfile does not exist, or you do not have "
2496 "correct permissions");
2500 ERR("invalid args given for head");
2503 /* asumming all else worked */
2504 obj->data.tail.buffer = malloc(text_buffer_size * 20);
2507 obj->data.s = strdup(arg);
2509 CRIT_ERR("lines needs a argument");
2513 obj->data.s = strdup(arg);
2515 CRIT_ERR("words needs a argument");
2517 END OBJ(loadavg, INFO_LOADAVG)
2518 int a = 1, b = 2, c = 3, r = 3;
2521 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2522 if (r >= 3 && (c < 1 || c > 3)) {
2525 if (r >= 2 && (b < 1 || b > 3)) {
2528 if (r >= 1 && (a < 1 || a > 3)) {
2532 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2533 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2534 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2535 END OBJ(if_empty, 0)
2537 ERR("if_empty needs an argument");
2538 obj->data.ifblock.s = 0;
2540 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2542 obj_be_ifblock_if(obj);
2543 END OBJ(if_existing, 0)
2545 ERR("if_existing needs an argument or two");
2546 obj->data.ifblock.s = NULL;
2547 obj->data.ifblock.str = NULL;
2549 char buf1[256], buf2[256];
2550 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2553 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2554 obj->data.ifblock.str = NULL;
2556 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2557 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2560 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2561 obj_be_ifblock_if(obj);
2562 END OBJ(if_mounted, 0)
2564 ERR("if_mounted needs an argument");
2565 obj->data.ifblock.s = 0;
2567 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2569 obj_be_ifblock_if(obj);
2570 END OBJ(if_running, 0)
2574 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2575 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2577 ERR("if_running needs an argument");
2578 obj->data.ifblock.s = 0;
2580 obj_be_ifblock_if(obj);
2585 char box[256], dst[256];
2589 /* Kapil: Changed from MAIL_FILE to
2590 current_mail_spool since the latter
2591 is a copy of the former if undefined
2592 but the latter should take precedence
2594 strncpy(box, current_mail_spool, sizeof(box));
2596 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2598 strncpy(box, arg, sizeof(box));
2602 variable_substitute(box, dst, sizeof(dst));
2603 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2604 obj->data.local_mail.interval = n1;
2605 END OBJ(mboxscan, 0)
2606 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2607 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2608 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2609 obj->data.mboxscan.output[0] = 1;
2610 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2611 END OBJ(mem, INFO_MEM)
2612 END OBJ(memeasyfree, INFO_MEM)
2613 END OBJ(memfree, INFO_MEM)
2614 END OBJ(memmax, INFO_MEM)
2615 END OBJ(memperc, INFO_MEM)
2616 END OBJ(membar, INFO_MEM)
2617 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2618 END OBJ(memgraph, INFO_MEM)
2619 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2620 &obj->e, &obj->showaslog);
2625 END OBJ(mixer, INFO_MIXER)
2626 obj->data.l = mixer_init(arg);
2627 END OBJ(mixerl, INFO_MIXER)
2628 obj->data.l = mixer_init(arg);
2629 END OBJ(mixerr, INFO_MIXER)
2630 obj->data.l = mixer_init(arg);
2631 END OBJ(mixerbar, INFO_MIXER)
2632 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2633 &obj->data.mixerbar.h);
2634 END OBJ(mixerlbar, INFO_MIXER)
2635 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2636 &obj->data.mixerbar.h);
2637 END OBJ(mixerrbar, INFO_MIXER)
2638 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2639 &obj->data.mixerbar.h);
2641 END OBJ(monitor, INFO_X11)
2642 END OBJ(monitor_number, INFO_X11)
2644 END OBJ(new_mails, 0)
2646 char box[256], dst[256];
2650 /* Kapil: Changed from MAIL_FILE to
2651 current_mail_spool since the latter
2652 is a copy of the former if undefined
2653 but the latter should take precedence
2655 strncpy(box, current_mail_spool, sizeof(box));
2657 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2659 strncpy(box, arg, sizeof(box));
2663 variable_substitute(box, dst, sizeof(dst));
2664 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2665 obj->data.local_mail.interval = n1;
2666 END OBJ(nodename, 0)
2667 END OBJ(processes, INFO_PROCS)
2668 END OBJ(running_processes, INFO_RUN_PROCS)
2669 END OBJ(shadecolor, 0)
2671 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2673 END OBJ(outlinecolor, 0)
2675 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2677 END OBJ(stippled_hr, 0)
2679 int a = stippled_borders, b = 1;
2682 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2683 sscanf(arg, "%d", &b);
2689 obj->data.pair.a = a;
2690 obj->data.pair.b = b;
2692 END OBJ(swap, INFO_MEM)
2693 END OBJ(swapmax, INFO_MEM)
2694 END OBJ(swapperc, INFO_MEM)
2695 END OBJ(swapbar, INFO_MEM)
2696 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2699 END OBJ(temp1, INFO_SYSFS)
2700 obj->type = OBJ_i2c;
2701 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
2702 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2703 END OBJ(temp2, INFO_SYSFS)
2704 obj->type = OBJ_i2c;
2705 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
2706 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2709 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2711 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2713 char buf1[256], buf2[256], *fmt, *tz;
2717 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2727 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2728 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2730 END OBJ(iconv_start, 0)
2731 if (iconv_converting) {
2732 CRIT_ERR("You must stop your last iconv conversion before "
2733 "starting another");
2736 char iconv_from[CODEPAGE_LENGTH];
2737 char iconv_to[CODEPAGE_LENGTH];
2739 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2740 CRIT_ERR("Invalid arguments for iconv_start");
2744 new_iconv = iconv_open(iconv_to, iconv_from);
2745 if (new_iconv == (iconv_t) (-1)) {
2746 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2748 obj->a = register_iconv(&new_iconv);
2749 iconv_converting = 1;
2753 CRIT_ERR("Iconv requires arguments");
2755 END OBJ(iconv_stop, 0)
2756 iconv_converting = 0;
2759 END OBJ(totaldown, INFO_NET)
2761 obj->data.net = get_net_stat(arg);
2763 CRIT_ERR("totaldown needs argument");
2765 END OBJ(totalup, INFO_NET)
2766 obj->data.net = get_net_stat(arg);
2768 obj->data.net = get_net_stat(arg);
2770 CRIT_ERR("totalup needs argument");
2774 obj->data.i = arg ? atoi(arg) : 0;
2776 obj->data.i = arg ? atoi(arg) : 0;
2777 END OBJ(upspeed, INFO_NET)
2779 obj->data.net = get_net_stat(arg);
2781 CRIT_ERR("upspeed needs argument");
2783 END OBJ(upspeedf, INFO_NET)
2785 obj->data.net = get_net_stat(arg);
2787 CRIT_ERR("upspeedf needs argument");
2790 END OBJ(upspeedgraph, INFO_NET)
2791 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2792 &obj->e, &obj->showaslog);
2794 // default to DEFAULTNETDEV
2795 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
2796 obj->data.net = get_net_stat(buf);
2798 END OBJ(uptime_short, INFO_UPTIME)
2799 END OBJ(uptime, INFO_UPTIME)
2800 END OBJ(user_names, INFO_USERS)
2801 END OBJ(user_times, INFO_USERS)
2802 END OBJ(user_terms, INFO_USERS)
2803 END OBJ(user_number, INFO_USERS)
2804 #if defined(__linux__)
2805 END OBJ(gw_iface, INFO_GW)
2806 END OBJ(gw_ip, INFO_GW)
2807 END OBJ(if_gw, INFO_GW)
2808 #endif /* !__linux__ */
2810 END OBJ(adt746xcpu, 0)
2811 END OBJ(adt746xfan, 0)
2812 #endif /* !__OpenBSD__ */
2813 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2814 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2815 END OBJ(apm_adapter, 0)
2816 END OBJ(apm_battery_life, 0)
2817 END OBJ(apm_battery_time, 0)
2818 #endif /* __FreeBSD__ */
2819 END OBJ_THREAD(imap_unseen, 0)
2822 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2823 obj->global_mode = 0;
2825 obj->global_mode = 1;
2827 END OBJ_THREAD(imap_messages, 0)
2830 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2831 obj->global_mode = 0;
2833 obj->global_mode = 1;
2835 END OBJ_THREAD(pop3_unseen, 0)
2838 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2839 obj->global_mode = 0;
2841 obj->global_mode = 1;
2843 END OBJ_THREAD(pop3_used, 0)
2846 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2847 obj->global_mode = 0;
2849 obj->global_mode = 1;
2854 obj->data.s = strndup(arg, text_buffer_size);
2856 ERR("smapi needs an argument");
2857 END OBJ(if_smapi_bat_installed, 0)
2859 ERR("if_smapi_bat_installed needs an argument");
2860 obj->data.ifblock.s = 0;
2862 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2863 obj_be_ifblock_if(obj);
2864 END OBJ(smapi_bat_perc, 0)
2866 obj->data.s = strndup(arg, text_buffer_size);
2868 ERR("smapi_bat_perc needs an argument");
2869 END OBJ(smapi_bat_temp, 0)
2871 obj->data.s = strndup(arg, text_buffer_size);
2873 ERR("smapi_bat_temp needs an argument");
2874 END OBJ(smapi_bat_power, 0)
2876 obj->data.s = strndup(arg, text_buffer_size);
2878 ERR("smapi_bat_power needs an argument");
2879 END OBJ(smapi_bat_bar, 0)
2882 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2883 ERR("first argument to smapi_bat_bar must be an integer value");
2887 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2890 ERR("smapi_bat_bar needs an argument");
2893 END OBJ_THREAD(mpd_artist, INFO_MPD)
2895 sscanf(arg, "%d", &obj->data.i);
2896 if (obj->data.i > 0) {
2899 ERR("mpd_artist: invalid length argument");
2905 END OBJ_THREAD(mpd_title, INFO_MPD)
2907 sscanf(arg, "%d", &obj->data.i);
2908 if (obj->data.i > 0) {
2911 ERR("mpd_title: invalid length argument");
2917 END OBJ_THREAD(mpd_random, INFO_MPD)
2918 END OBJ_THREAD(mpd_repeat, INFO_MPD)
2919 END OBJ_THREAD(mpd_elapsed, INFO_MPD)
2920 END OBJ_THREAD(mpd_length, INFO_MPD)
2921 END OBJ_THREAD(mpd_track, INFO_MPD)
2923 sscanf(arg, "%d", &obj->data.i);
2924 if (obj->data.i > 0) {
2927 ERR("mpd_track: invalid length argument");
2933 END OBJ_THREAD(mpd_name, INFO_MPD)
2935 sscanf(arg, "%d", &obj->data.i);
2936 if (obj->data.i > 0) {
2939 ERR("mpd_name: invalid length argument");
2945 END OBJ_THREAD(mpd_file, INFO_MPD)
2947 sscanf(arg, "%d", &obj->data.i);
2948 if (obj->data.i > 0) {
2951 ERR("mpd_file: invalid length argument");
2957 END OBJ_THREAD(mpd_percent, INFO_MPD)
2958 END OBJ_THREAD(mpd_album, INFO_MPD)
2960 sscanf(arg, "%d", &obj->data.i);
2961 if (obj->data.i > 0) {
2964 ERR("mpd_album: invalid length argument");
2970 END OBJ_THREAD(mpd_vol, INFO_MPD)
2971 END OBJ_THREAD(mpd_bitrate, INFO_MPD)
2972 END OBJ_THREAD(mpd_status, INFO_MPD)
2973 END OBJ_THREAD(mpd_bar, INFO_MPD)
2974 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2975 END OBJ_THREAD(mpd_smart, INFO_MPD)
2977 sscanf(arg, "%d", &obj->data.i);
2978 if (obj->data.i > 0) {
2981 ERR("mpd_smart: invalid length argument");
2987 END OBJ_THREAD(if_mpd_playing, INFO_MPD)
2988 obj_be_ifblock_if(obj);
2991 END OBJ_THREAD(moc_state, INFO_MOC)
2992 END OBJ_THREAD(moc_file, INFO_MOC)
2993 END OBJ_THREAD(moc_title, INFO_MOC)
2994 END OBJ_THREAD(moc_artist, INFO_MOC)
2995 END OBJ_THREAD(moc_song, INFO_MOC)
2996 END OBJ_THREAD(moc_album, INFO_MOC)
2997 END OBJ_THREAD(moc_totaltime, INFO_MOC)
2998 END OBJ_THREAD(moc_timeleft, INFO_MOC)
2999 END OBJ_THREAD(moc_curtime, INFO_MOC)
3000 END OBJ_THREAD(moc_bitrate, INFO_MOC)
3001 END OBJ_THREAD(moc_rate, INFO_MOC)
3004 END OBJ(xmms2_artist, INFO_XMMS2)
3005 END OBJ(xmms2_album, INFO_XMMS2)
3006 END OBJ(xmms2_title, INFO_XMMS2)
3007 END OBJ(xmms2_genre, INFO_XMMS2)
3008 END OBJ(xmms2_comment, INFO_XMMS2)
3009 END OBJ(xmms2_url, INFO_XMMS2)
3010 END OBJ(xmms2_tracknr, INFO_XMMS2)
3011 END OBJ(xmms2_bitrate, INFO_XMMS2)
3012 END OBJ(xmms2_date, INFO_XMMS2)
3013 END OBJ(xmms2_id, INFO_XMMS2)
3014 END OBJ(xmms2_duration, INFO_XMMS2)
3015 END OBJ(xmms2_elapsed, INFO_XMMS2)
3016 END OBJ(xmms2_size, INFO_XMMS2)
3017 END OBJ(xmms2_status, INFO_XMMS2)
3018 END OBJ(xmms2_percent, INFO_XMMS2)
3019 END OBJ(xmms2_bar, INFO_XMMS2)
3020 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3021 END OBJ(xmms2_smart, INFO_XMMS2)
3022 END OBJ(xmms2_playlist, INFO_XMMS2)
3023 END OBJ(xmms2_timesplayed, INFO_XMMS2)
3024 END OBJ(if_xmms2_connected, INFO_XMMS2)
3025 obj_be_ifblock_if(obj);
3028 END OBJ(audacious_status, INFO_AUDACIOUS)
3029 END OBJ(audacious_title, INFO_AUDACIOUS)
3031 sscanf(arg, "%d", &info.audacious.max_title_len);
3032 if (info.audacious.max_title_len > 0) {
3033 info.audacious.max_title_len++;
3035 CRIT_ERR("audacious_title: invalid length argument");
3038 END OBJ(audacious_length, INFO_AUDACIOUS)
3039 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
3040 END OBJ(audacious_position, INFO_AUDACIOUS)
3041 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
3042 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
3043 END OBJ(audacious_frequency, INFO_AUDACIOUS)
3044 END OBJ(audacious_channels, INFO_AUDACIOUS)
3045 END OBJ(audacious_filename, INFO_AUDACIOUS)
3046 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
3047 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
3048 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
3049 END OBJ(audacious_bar, INFO_AUDACIOUS)
3050 scan_bar(arg, &obj->a, &obj->b);
3053 END OBJ(bmpx_title, INFO_BMPX)
3054 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3055 END OBJ(bmpx_artist, INFO_BMPX)
3056 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3057 END OBJ(bmpx_album, INFO_BMPX)
3058 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3059 END OBJ(bmpx_track, INFO_BMPX)
3060 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3061 END OBJ(bmpx_uri, INFO_BMPX)
3062 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3063 END OBJ(bmpx_bitrate, INFO_BMPX)
3064 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3070 char *userid = (char *) malloc(20 * sizeof(char));
3071 char *apikey = (char *) malloc(64 * sizeof(char));
3072 char *charid = (char *) malloc(20 * sizeof(char));
3074 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
3075 obj->data.eve.charid = charid;
3076 obj->data.eve.userid = userid;
3077 obj->data.eve.apikey = apikey;
3081 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
3087 int argc, delay, act_par;
3088 char *uri = (char *) malloc(128 * sizeof(char));
3089 char *action = (char *) malloc(64 * sizeof(char));
3091 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
3093 obj->data.rss.uri = uri;
3094 obj->data.rss.delay = delay;
3095 obj->data.rss.action = action;
3096 obj->data.rss.act_par = act_par;
3100 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
3106 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
3107 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3108 ERR("hddtemp needs arguments");
3109 obj->type = OBJ_text;
3110 obj->data.s = strndup("${hddtemp}", text_buffer_size);
3111 obj->data.hddtemp.update_time = 0;
3113 obj->data.hddtemp.temp = NULL;
3115 #ifdef TCP_PORT_MONITOR
3116 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3117 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
3119 END OBJ(entropy_avail, INFO_ENTROPY)
3120 END OBJ(entropy_poolsize, INFO_ENTROPY)
3121 END OBJ(entropy_bar, INFO_ENTROPY)
3122 scan_bar(arg, &obj->a, &obj->b);
3126 obj->data.scroll.step = 1;
3127 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
3128 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
3130 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
3131 obj->data.scroll.start = 0;
3133 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
3138 CRIT_ERR("nvidia needs an argument\n");
3139 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3140 CRIT_ERR("nvidia: invalid argument"
3141 " specified: '%s'\n", arg);
3147 ERR("unknown variable %s", s);
3148 obj->type = OBJ_text;
3149 snprintf(buf, 256, "${%s}", s);
3150 obj->data.s = strndup(buf, text_buffer_size);
3157 static struct text_object *create_plain_text(const char *s)
3159 struct text_object *obj;
3161 if (s == NULL || *s == '\0') {
3165 obj = new_text_object_internal();
3167 obj->type = OBJ_text;
3168 obj->data.s = strndup(s, text_buffer_size);
3172 /* backslash_escape - do the actual substitution task for template objects
3174 * The field templates is used for substituting the \N occurences. Set it to
3175 * NULL to leave them as they are.
3177 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3181 unsigned int dup_idx = 0, dup_len;
3183 dup_len = strlen(src);
3184 src_dup = malloc(dup_len * sizeof(char));
3192 if (*(p + 1) == '\\') {
3193 src_dup[dup_idx++] = '\\';
3195 } else if (*(p + 1) == ' ') {
3196 src_dup[dup_idx++] = ' ';
3198 } else if (*(p + 1) == 'n') {
3199 src_dup[dup_idx++] = '\n';
3201 } else if (templates) {
3202 unsigned int tmpl_num;
3204 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3205 (tmpl_num > template_count))
3207 dup_len += strlen(templates[tmpl_num - 1]);
3208 src_dup = realloc(src_dup, dup_len * sizeof(char));
3209 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3210 dup_idx += strlen(templates[tmpl_num - 1]);
3215 src_dup[dup_idx++] = *p;
3220 src_dup[dup_idx] = '\0';
3221 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3225 /* handle_template_object - core logic of the template object
3227 * use config variables like this:
3228 * template1 = "$\1\2"
3229 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3231 * and use them like this:
3232 * ${template1 node name}
3233 * ${template2 root /}
3234 * ${template2 cdrom /mnt/cdrom}
3236 static char *handle_template(const char *tmpl, const char *args)
3238 char *args_dup, *p, *p_old;
3239 char **argsp = NULL;
3240 unsigned int argcnt = 0, template_idx, i;
3243 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3247 args_dup = strdup(args);
3250 while (*p && (*p == ' ' && *(p - 1) != '\\'))
3252 if (*(p - 1) == '\\')
3255 while (*p && (*p != ' ' || *(p - 1) == '\\'))
3261 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3262 argsp[argcnt - 1] = p_old;
3264 for (i = 0; i < argcnt; i++) {
3266 tmp = backslash_escape(argsp[i], NULL, 0);
3267 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3271 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3272 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3274 for (i = 0; i < argcnt; i++)
3280 static char *find_and_replace_templates(const char *inbuf)
3282 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3285 outlen = strlen(inbuf) + 1;
3286 o = outbuf = calloc(outlen, sizeof(char));
3287 memset(outbuf, 0, outlen * sizeof(char));
3289 p = indup = strdup(inbuf);
3291 while (*p && *p != '$')
3297 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3302 if (*(p + 1) == '{') {
3324 tmpl_out = handle_template(templ, args);
3326 outlen += strlen(tmpl_out);
3327 outbuf = realloc(outbuf, outlen * sizeof(char));
3328 strcat (outbuf, tmpl_out);
3330 o = outbuf + strlen(outbuf);
3332 ERR("failed to handle template '%s' with args '%s'", templ, args);
3336 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3341 static int text_contains_templates(const char *text)
3343 if (strcasestr(text, "${template") != NULL)
3345 if (strcasestr(text, "$template") != NULL)
3350 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3352 struct text_object *obj;
3353 char *p, *s, *orig_p;
3356 p = strndup(const_p, max_user_text - 1);
3357 while (text_contains_templates(p)) {
3359 tmp = find_and_replace_templates(p);
3365 if (strcmp(p, const_p)) {
3366 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3368 DBGP("no templates to replace");
3371 memset(retval, 0, sizeof(struct text_object));
3373 line = global_text_lines;
3381 obj = create_plain_text(s);
3383 append_object(retval, obj);
3394 /* variable is either $foo or ${foo} */
3396 unsigned int brl = 1, brr = 0;
3400 while (*p && brl != brr) {
3415 while (*p && (isalnum((int) *p) || *p == '_')) {
3420 /* copy variable to buffer */
3421 len = (p - s > 255) ? 255 : (p - s);
3422 strncpy(buf, s, len);
3432 /* if variable wasn't found in environment, use some special */
3438 if (strchr(buf, ' ')) {
3439 arg = strchr(buf, ' ');
3442 while (isspace((int) *arg)) {
3450 /* lowercase variable name */
3453 *tmp_p = tolower(*tmp_p);
3457 obj = construct_text_object(buf, arg, line, allow_threaded);
3459 append_object(retval, obj);
3464 obj = create_plain_text("$");
3466 append_object(retval, obj);
3472 obj = create_plain_text(s);
3474 append_object(retval, obj);
3477 if (!ifblock_stack_empty()) {
3478 ERR("one or more $endif's are missing");
3485 static void extract_variable_text(const char *p)
3487 free_text_objects(&global_root_object, 1);
3501 extract_variable_text_internal(&global_root_object, p, 1);
3504 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3506 extract_variable_text_internal(root, txt, 0);
3507 generate_text_internal(p, max_user_text, *root, cur);
3511 /* Allows reading from a FIFO (i.e., /dev/xconsole).
3512 * The file descriptor is set to non-blocking which makes this possible.
3514 * FIXME: Since lseek cannot seek a file descriptor long lines will break. */
3515 static void tail_pipe(struct text_object *obj, char *dst, size_t dst_size)
3517 #define TAIL_PIPE_BUFSIZE 4096
3521 int fd = obj->data.tail.fd;
3524 char buf[TAIL_PIPE_BUFSIZE];
3525 ssize_t len = read(fd, buf, sizeof(buf));
3529 if (errno != EAGAIN) {
3530 strcpy(obj->data.tail.buffer, "Logfile Read Error");
3531 snprintf(dst, dst_size, "Logfile Read Error");
3535 } else if (len == 0) {
3536 strcpy(obj->data.tail.buffer, "Logfile Empty");
3537 snprintf(dst, dst_size, "Logfile Empty");
3541 for (line_len = 0, i = 0; i < len; i++) {
3545 if (buf[i] == '\n') {
3548 if (obj->data.tail.readlines > 0) {
3553 for (n = 0; obj->data.tail.buffer[n]; n++) {
3554 if (obj->data.tail.buffer[n] == '\n') {
3559 if (++olines < obj->data.tail.wantedlines) {
3565 p = obj->data.tail.buffer + first_line;
3566 pos = n - first_line;
3567 memmove(obj->data.tail.buffer,
3568 obj->data.tail.buffer + first_line, strlen(p));
3569 obj->data.tail.buffer[pos] = 0;
3575 p = buf + last_line;
3577 memcpy(&(obj->data.tail.buffer[pos]), p, line_len);
3578 obj->data.tail.buffer[pos + line_len] = 0;
3581 obj->data.tail.readlines = lines;
3589 snprintf(dst, dst_size, "%s", obj->data.tail.buffer);
3592 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3593 void *thread(void *), const char *text)
3595 if (obj->global_mode && info.mail) {
3596 // this means we use info
3597 if (!info.mail->p_timed_thread) {
3598 info.mail->p_timed_thread =
3599 timed_thread_create(thread,
3600 (void *) info.mail, info.mail->interval * 1000000);
3601 if (!info.mail->p_timed_thread) {
3602 ERR("Error creating %s timed thread", text);
3604 timed_thread_register(info.mail->p_timed_thread,
3605 &info.mail->p_timed_thread);
3606 if (timed_thread_run(info.mail->p_timed_thread)) {
3607 ERR("Error running %s timed thread", text);
3611 } else if (obj->data.mail) {
3612 // this means we use obj
3613 if (!obj->data.mail->p_timed_thread) {
3614 obj->data.mail->p_timed_thread =
3615 timed_thread_create(thread,
3616 (void *) obj->data.mail,
3617 obj->data.mail->interval * 1000000);
3618 if (!obj->data.mail->p_timed_thread) {
3619 ERR("Error creating %s timed thread", text);
3621 timed_thread_register(obj->data.mail->p_timed_thread,
3622 &obj->data.mail->p_timed_thread);
3623 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3624 ERR("Error running %s timed thread", text);
3627 return obj->data.mail;
3628 } else if (!obj->a) {
3629 // something is wrong, warn once then stop
3630 ERR("Theres a problem with your %s_unseen settings. "
3631 "Check that the global %s settings are defined "
3632 "properly (line %li).", global_text, global_text, obj->line);
3638 char *format_time(unsigned long timeval, const int width)
3641 unsigned long nt; // narrow time, for speed on 32-bit
3642 unsigned cc; // centiseconds
3643 unsigned nn; // multi-purpose whatever
3646 cc = nt % 100; // centiseconds past second
3647 nt /= 100; // total seconds
3648 nn = nt % 60; // seconds past the minute
3649 nt /= 60; // total minutes
3650 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3652 return strndup(buf, text_buffer_size);
3654 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3655 return strndup(buf, text_buffer_size);
3657 nn = nt % 60; // minutes past the hour
3658 nt /= 60; // total hours
3659 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3660 return strndup(buf, text_buffer_size);
3662 nn = nt; // now also hours
3663 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3664 return strndup(buf, text_buffer_size);
3666 nn /= 24; // now days
3667 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3668 return strndup(buf, text_buffer_size);
3670 nn /= 7; // now weeks
3671 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3672 return strndup(buf, text_buffer_size);
3674 // well shoot, this outta' fit...
3675 return strndup("<inf>", text_buffer_size);
3678 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3679 //string has to end with \0 and it's length should fit in a int
3681 void remove_deleted_chars(char *string){
3683 while(string[i] != 0){
3684 if(string[i] == BACKSPACE){
3686 strcpy( &(string[i-1]), &(string[i+1]) );
3688 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3693 static inline void format_media_player_time(char *buf, const int size,
3696 int days, hours, minutes;
3698 days = seconds / (24 * 60 * 60);
3699 seconds %= (24 * 60 * 60);
3700 hours = seconds / (60 * 60);
3701 seconds %= (60 * 60);
3702 minutes = seconds / 60;
3706 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3707 hours, minutes, seconds);
3708 } else if (hours > 0) {
3709 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3712 snprintf(buf, size, "%i:%02i", minutes, seconds);
3716 static inline double get_barnum(char *buf)
3728 if (sscanf(buf, "%lf", &barnum) == 0) {
3729 ERR("reading execbar value failed (perhaps it's not the "
3730 "correct format?)");
3733 if (barnum > 100.0 || barnum < 0.0) {
3734 ERR("your execbar value is not between 0 and 100, "
3735 "therefore it will be ignored");
3741 static void generate_text_internal(char *p, int p_max_size,
3742 struct text_object root, struct information *cur)
3744 struct text_object *obj;
3747 char buff_in[p_max_size];
3749 iconv_converting = 0;
3753 for (obj = root.next; obj && p_max_size > 0; obj = obj->next) {
3755 /* IFBLOCK jumping algorithm
3757 * This is easier as it looks like:
3758 * - each IF checks it's condition
3759 * - on FALSE: call DO_JUMP
3760 * - on TRUE: don't care
3761 * - each ELSE calls DO_JUMP unconditionally
3762 * - each ENDIF is silently being ignored
3765 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3766 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3767 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3768 * regularly, "obj" is being updated to point to obj->next, so object parsing
3769 * continues right after the corresponding ELSE or ENDIF. This means that if we
3770 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3771 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3772 * jumped, and there is nothing to do.
3776 obj = obj->data.ifblock.next; \
3779 #define OBJ(a) break; case OBJ_##a:
3781 switch (obj->type) {
3783 ERR("not implemented obj type %d", obj->type);
3786 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3788 #endif /* !__OpenBSD__ */
3791 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3792 obj->data.cpu_index);
3798 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3799 obj->data.cpu_index);
3801 /* OpenBSD has no such flag (SUSv2) */
3802 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3803 obj->data.cpu_index);
3807 #if defined(__linux__)
3810 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3811 obj->data.cpu_index);
3816 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3817 obj->data.cpu_index);
3822 OBJ(wireless_essid) {
3823 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3825 OBJ(wireless_mode) {
3826 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3828 OBJ(wireless_bitrate) {
3829 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3832 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3834 OBJ(wireless_link_qual) {
3835 spaced_print(p, p_max_size, "%d", 4, "wireless_link_qual",
3836 obj->data.net->link_qual);
3838 OBJ(wireless_link_qual_max) {
3839 spaced_print(p, p_max_size, "%d", 4,
3840 "wireless_link_qual_max", obj->data.net->link_qual_max);
3842 OBJ(wireless_link_qual_perc) {
3843 if (obj->data.net->link_qual_max > 0) {
3844 spaced_print(p, p_max_size, "%.0f", 5,
3845 "wireless_link_qual_perc",
3846 (double) obj->data.net->link_qual /
3847 obj->data.net->link_qual_max * 100);
3849 spaced_print(p, p_max_size, "unk", 5,
3850 "wireless_link_qual_perc");
3853 OBJ(wireless_link_bar) {
3854 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3855 obj->data.net->link_qual_max) * 255.0);
3857 #endif /* HAVE_IWLIB */
3859 #endif /* __linux__ */
3863 get_adt746x_cpu(p, p_max_size);
3866 get_adt746x_fan(p, p_max_size);
3869 get_acpi_fan(p, p_max_size);
3871 OBJ(acpiacadapter) {
3872 get_acpi_ac_adapter(p, p_max_size);
3875 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3878 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3880 OBJ(battery_percent) {
3881 spaced_print(p, p_max_size, "%*d", 4, "battery_percent",
3882 pad_percents, get_battery_perct(obj->data.s));
3885 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3887 #endif /* __OpenBSD__ */
3890 human_readable(cur->buffers * 1024, p, 255, "buffers");
3893 human_readable(cur->cached * 1024, p, 255, "buffers");
3896 if (obj->data.cpu_index > info.cpu_count) {
3897 printf("obj->data.cpu_index %i info.cpu_count %i",
3898 obj->data.cpu_index, info.cpu_count);
3899 CRIT_ERR("attempting to use more CPUs than you have!");
3901 spaced_print(p, p_max_size, "%*d", 4, "cpu", pad_percents,
3902 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3905 new_bar(p, obj->a, obj->b,
3906 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3909 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
3910 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3911 100, 1, obj->showaslog);
3914 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3915 obj->e, 1, obj->showaslog);
3918 new_fg(p, obj->data.l);
3950 OBJ(conky_version) {
3951 snprintf(p, p_max_size, "%s", VERSION);
3953 OBJ(conky_build_date) {
3954 snprintf(p, p_max_size, "%s", BUILD_DATE);
3956 OBJ(conky_build_arch) {
3957 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3959 #if defined(__linux__)
3961 snprintf(p, p_max_size, "%s",
3962 get_disk_protect_queue(obj->data.s));
3965 snprintf(p, p_max_size, "%s", i8k.version);
3968 snprintf(p, p_max_size, "%s", i8k.bios);
3971 snprintf(p, p_max_size, "%s", i8k.serial);
3976 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3977 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3979 OBJ(i8k_left_fan_status) {
3980 int left_fan_status;
3982 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3983 if (left_fan_status == 0) {
3984 snprintf(p, p_max_size, "off");
3986 if (left_fan_status == 1) {
3987 snprintf(p, p_max_size, "low");
3989 if (left_fan_status == 2) {
3990 snprintf(p, p_max_size, "high");
3993 OBJ(i8k_right_fan_status) {
3994 int right_fan_status;
3996 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3997 if (right_fan_status == 0) {
3998 snprintf(p, p_max_size, "off");
4000 if (right_fan_status == 1) {
4001 snprintf(p, p_max_size, "low");
4003 if (right_fan_status == 2) {
4004 snprintf(p, p_max_size, "high");
4007 OBJ(i8k_left_fan_rpm) {
4008 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4010 OBJ(i8k_right_fan_rpm) {
4011 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4013 OBJ(i8k_ac_status) {
4016 sscanf(i8k.ac_status, "%d", &ac_status);
4017 if (ac_status == -1) {
4018 snprintf(p, p_max_size, "disabled (read i8k docs)");
4020 if (ac_status == 0) {
4021 snprintf(p, p_max_size, "off");
4023 if (ac_status == 1) {
4024 snprintf(p, p_max_size, "on");
4027 OBJ(i8k_buttons_status) {
4028 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4031 get_ibm_acpi_fan(p, p_max_size);
4034 get_ibm_acpi_temps();
4035 temp_print(p, p_max_size,
4036 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4039 get_ibm_acpi_volume(p, p_max_size);
4041 OBJ(ibm_brightness) {
4042 get_ibm_acpi_brightness(p, p_max_size);
4045 if ((obj->data.ifblock.s)
4046 && (!interface_up(obj->data.ifblock.s))) {
4051 if (!cur->gw_info.count) {
4056 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4059 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4062 snprintf(p, p_max_size, "%d", get_laptop_mode());
4065 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4067 #endif /* __linux__ */
4070 OBJ(obsd_sensors_temp) {
4071 obsd_sensors.device = sensor_device;
4072 update_obsd_sensors();
4073 temp_print(p, p_max_size,
4074 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4077 OBJ(obsd_sensors_fan) {
4078 obsd_sensors.device = sensor_device;
4079 update_obsd_sensors();
4080 snprintf(p, p_max_size, "%d",
4081 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4083 OBJ(obsd_sensors_volt) {
4084 obsd_sensors.device = sensor_device;
4085 update_obsd_sensors();
4086 snprintf(p, p_max_size, "%.2f",
4087 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4090 get_obsd_vendor(p, p_max_size);
4093 get_obsd_product(p, p_max_size);
4095 #endif /* __OpenBSD__ */
4099 new_font(p, obj->data.s);
4102 /* TODO: move this correction from kB to kB/s elsewhere
4103 * (or get rid of it??) */
4105 if (obj->data.diskio) {
4107 (obj->data.diskio->current / update_interval) * 1024LL,
4108 p, p_max_size, "diskio");
4110 human_readable(info.diskio_value * 1024LL, p, p_max_size,
4115 if (obj->data.diskio) {
4116 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL, p, p_max_size,
4119 human_readable(info.diskio_write_value * 1024LL, p, p_max_size,
4124 if (obj->data.diskio) {
4125 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL, p, p_max_size,
4128 human_readable(info.diskio_read_value * 1024LL, p, p_max_size,
4133 if (obj->data.diskio) {
4134 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4135 obj->data.diskio->current, obj->e, 1, obj->showaslog);
4137 new_graph(p, obj->a, obj->b, obj->c, obj->d, info.diskio_value,
4138 obj->e, 1, obj->showaslog);
4141 OBJ(diskiograph_read) {
4142 if (obj->data.diskio) {
4143 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4144 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
4146 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4147 info.diskio_read_value, obj->e, 1, obj->showaslog);
4150 OBJ(diskiograph_write) {
4151 if (obj->data.diskio) {
4152 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4153 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
4155 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4156 info.diskio_write_value, obj->e, 1, obj->showaslog);
4160 spaced_print(p, p_max_size, "%d", 6, "downspeed",
4161 round_to_int(obj->data.net->recv_speed / 1024));
4164 spaced_print(p, p_max_size, "%.1f", 8, "downspeedf",
4165 obj->data.net->recv_speed / 1024.0);
4167 OBJ(downspeedgraph) {
4168 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4169 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
4172 /* Since we see you, you're if has not jumped.
4173 * Do Ninja jump here: without leaving traces.
4174 * This is to prevent us from stale jumped flags.
4176 obj = obj->data.ifblock.next;
4180 /* harmless object, just ignore */
4184 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4185 && (obj->data.net->addr.sa_data[3] & 255) == 0
4186 && (obj->data.net->addr.sa_data[4] & 255) == 0
4187 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4188 snprintf(p, p_max_size, "No Address");
4190 snprintf(p, p_max_size, "%u.%u.%u.%u",
4191 obj->data.net->addr.sa_data[2] & 255,
4192 obj->data.net->addr.sa_data[3] & 255,
4193 obj->data.net->addr.sa_data[4] & 255,
4194 obj->data.net->addr.sa_data[5] & 255);
4197 #if defined(__linux__)
4199 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4201 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4202 strcpy(p, obj->data.net->addrs);
4205 strcpy(p, "0.0.0.0");
4207 #endif /* __linux__ */
4209 #if defined(IMLIB2) && defined(X11)
4214 Imlib_Image image, buffer;
4216 image = imlib_load_image(obj->data.s);
4217 imlib_context_set_image(image);
4221 w = imlib_image_get_width();
4222 h = imlib_image_get_height();
4223 buffer = imlib_create_image(w, h);
4224 imlib_context_set_display(display);
4225 imlib_context_set_drawable(window.drawable);
4226 imlib_context_set_colormap(DefaultColormap(display,
4228 imlib_context_set_visual(DefaultVisual(display,
4230 imlib_context_set_image(buffer);
4231 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
4232 text_start_x, text_start_y, w, h);
4233 imlib_render_image_on_drawable(text_start_x,
4236 imlib_context_set_image(image);
4244 read_exec(obj->data.s, p, text_buffer_size);
4245 remove_deleted_chars(p);
4248 struct information *tmp_info;
4249 struct text_object subroot;
4251 read_exec(obj->data.s, p, text_buffer_size);
4253 tmp_info = malloc(sizeof(struct information));
4254 memcpy(tmp_info, cur, sizeof(struct information));
4255 parse_conky_vars(&subroot, p, p, tmp_info);
4257 free_text_objects(&subroot, 0);
4263 read_exec(obj->data.s, p, text_buffer_size);
4264 barnum = get_barnum(p);
4266 if (barnum >= 0.0) {
4268 new_bar(p, 0, 4, round_to_int(barnum * 255.0));
4272 char showaslog = FALSE;
4275 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
4277 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, text_buffer_size);
4278 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
4279 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, text_buffer_size);
4281 read_exec(obj->data.s, p, text_buffer_size);
4283 barnum = get_barnum(p);
4285 if (barnum >= 0.0) {
4286 new_graph(p, 0, 25, obj->c, obj->d, round_to_int(barnum),
4291 if (current_update_time - obj->data.execi.last_update
4292 >= obj->data.execi.interval) {
4295 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4296 barnum = get_barnum(p);
4298 if (barnum >= 0.0) {
4299 obj->f = 255 * barnum / 100.0;
4301 obj->data.execi.last_update = current_update_time;
4303 new_bar(p, 0, 4, round_to_int(obj->f));
4306 if (current_update_time - obj->data.execi.last_update
4307 >= obj->data.execi.interval) {
4310 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4311 barnum = get_barnum(p);
4313 if (barnum >= 0.0) {
4316 obj->data.execi.last_update = current_update_time;
4318 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
4321 if (current_update_time - obj->data.execi.last_update
4322 >= obj->data.execi.interval
4323 && obj->data.execi.interval != 0) {
4324 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4326 obj->data.execi.last_update = current_update_time;
4328 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4331 struct text_object subroot;
4332 struct information *tmp_info =
4333 malloc(sizeof(struct information));
4334 memcpy(tmp_info, cur, sizeof(struct information));
4336 if (current_update_time - obj->data.execi.last_update
4337 < obj->data.execi.interval
4338 || obj->data.execi.interval == 0) {
4339 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4341 char *output = obj->data.execi.buffer;
4342 FILE *fp = popen(obj->data.execi.cmd, "r");
4343 int length = fread(output, 1, text_buffer_size, fp);
4347 output[length] = '\0';
4348 if (length > 0 && output[length - 1] == '\n') {
4349 output[length - 1] = '\0';
4352 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4353 obj->data.execi.last_update = current_update_time;
4355 free_text_objects(&subroot, 0);
4359 if (!obj->data.texeci.p_timed_thread) {
4360 obj->data.texeci.p_timed_thread =
4361 timed_thread_create(&threaded_exec,
4362 (void *) obj, obj->data.texeci.interval * 1000000);
4363 if (!obj->data.texeci.p_timed_thread) {
4364 ERR("Error creating texeci timed thread");
4366 timed_thread_register(obj->data.texeci.p_timed_thread,
4367 &obj->data.texeci.p_timed_thread);
4368 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4369 ERR("Error running texeci timed thread");
4372 timed_thread_lock(obj->data.texeci.p_timed_thread);
4373 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4374 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4376 #endif /* HAVE_POPEN */
4378 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4380 if (mail && mail->p_timed_thread) {
4381 timed_thread_lock(mail->p_timed_thread);
4382 snprintf(p, p_max_size, "%lu", mail->unseen);
4383 timed_thread_unlock(mail->p_timed_thread);
4386 OBJ(imap_messages) {
4387 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4389 if (mail && mail->p_timed_thread) {
4390 timed_thread_lock(mail->p_timed_thread);
4391 snprintf(p, p_max_size, "%lu", mail->messages);
4392 timed_thread_unlock(mail->p_timed_thread);
4396 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4398 if (mail && mail->p_timed_thread) {
4399 timed_thread_lock(mail->p_timed_thread);
4400 snprintf(p, p_max_size, "%lu", mail->unseen);
4401 timed_thread_unlock(mail->p_timed_thread);
4405 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4407 if (mail && mail->p_timed_thread) {
4408 timed_thread_lock(mail->p_timed_thread);
4409 snprintf(p, p_max_size, "%.1f",
4410 mail->used / 1024.0 / 1024.0);
4411 timed_thread_unlock(mail->p_timed_thread);
4415 if (obj->data.fs != NULL) {
4416 if (obj->data.fs->size == 0) {
4417 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4419 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4420 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4421 obj->data.fs->size));
4426 if (obj->data.fs != NULL) {
4427 human_readable(obj->data.fs->avail, p, 255, "fs_free");
4431 if (obj->data.fs != NULL) {
4432 if (obj->data.fs->size) {
4433 spaced_print(p, p_max_size, "%*d", 4, "fs_free_perc",
4434 pad_percents, (int) ((obj->data.fs->avail * 100) /
4435 obj->data.fs->size));
4437 snprintf(p, p_max_size, "0");
4442 if (obj->data.fs != NULL) {
4443 human_readable(obj->data.fs->size, p, 255, "fs_size");
4447 if (obj->data.fs != NULL)
4448 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4451 if (obj->data.fs != NULL) {
4452 human_readable(obj->data.fs->size - (obj->data.fs->free
4453 ? obj->data.fs->free : obj->data.fs->avail), p, 255,
4458 if (obj->data.fs != NULL) {
4459 if (obj->data.fs->size == 0) {
4460 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4462 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4463 (int) (obj->data.fsbar.fs->avail * 255 /
4464 obj->data.fs->size));
4469 if (obj->data.fs != NULL) {
4470 if (obj->data.fs->size) {
4471 spaced_print(p, 4, "%*d", 4, "fs_used_perc",
4472 pad_percents, 100 - ((int) ((obj->data.fs->avail * 100) /
4473 obj->data.fs->size)));
4475 snprintf(p, p_max_size, "0");
4480 float *v = info.loadavg;
4482 if (obj->data.loadavg[2]) {
4483 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4484 v[obj->data.loadavg[0] - 1],
4485 v[obj->data.loadavg[1] - 1],
4486 v[obj->data.loadavg[2] - 1]);
4487 } else if (obj->data.loadavg[1]) {
4488 snprintf(p, p_max_size, "%.2f %.2f",
4489 v[obj->data.loadavg[0] - 1],
4490 v[obj->data.loadavg[1] - 1]);
4491 } else if (obj->data.loadavg[0]) {
4492 snprintf(p, p_max_size, "%.2f",
4493 v[obj->data.loadavg[0] - 1]);
4497 new_goto(p, obj->data.i);
4500 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4503 new_hr(p, obj->data.i);
4506 if (cur->nameserver_info.nscount > obj->data.i)
4507 snprintf(p, p_max_size, "%s",
4508 cur->nameserver_info.ns_list[obj->data.i]);
4512 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4513 snprintf(p, p_max_size, "%s", skill);
4518 PRSS *data = get_rss_info(obj->data.rss.uri,
4519 obj->data.rss.delay);
4523 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4525 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4527 // remove trailing new line if one exists
4528 if (str[strlen(str) - 1] == '\n') {
4529 str[strlen(str) - 1] = 0;
4531 snprintf(p, p_max_size, "%s", str);
4532 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4533 if (obj->data.rss.act_par < data->item_count) {
4534 str = data->items[obj->data.rss.act_par].title;
4535 // remove trailing new line if one exists
4536 if (str[strlen(str) - 1] == '\n') {
4537 str[strlen(str) - 1] = 0;
4539 snprintf(p, p_max_size, "%s", str);
4541 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4542 if (obj->data.rss.act_par < data->item_count) {
4544 data->items[obj->data.rss.act_par].description;
4545 // remove trailing new line if one exists
4546 if (str[strlen(str) - 1] == '\n') {
4547 str[strlen(str) - 1] = 0;
4549 snprintf(p, p_max_size, "%s", str);
4551 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4552 if (data->item_count > 0) {
4558 if (obj->data.rss.act_par > data->item_count) {
4559 show = data->item_count;
4561 show = obj->data.rss.act_par;
4563 for (itmp = 0; itmp < show; itmp++) {
4564 PRSS_Item *item = &data->items[itmp];
4568 // don't add new line before first item
4570 strncat(p, "\n", p_max_size);
4572 /* remove trailing new line if one exists,
4573 * we have our own */
4574 if (str[strlen(str) - 1] == '\n') {
4575 str[strlen(str) - 1] = 0;
4577 strncat(p, str, p_max_size);
4589 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4590 if (obj->data.hddtemp.temp)
4591 free(obj->data.hddtemp.temp);
4592 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4593 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4594 obj->data.hddtemp.update_time = current_update_time;
4596 if (!obj->data.hddtemp.temp) {
4597 snprintf(p, p_max_size, "N/A");
4599 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4600 unit = obj->data.hddtemp.temp[0];
4602 if (*endptr != '\0')
4603 snprintf(p, p_max_size, "N/A");
4604 else if (unit == 'C')
4605 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4606 else if (unit == 'F')
4607 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4609 snprintf(p, p_max_size, "N/A");
4614 new_offset(p, obj->data.i);
4617 new_voffset(p, obj->data.i);
4623 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4624 obj->data.sysfs.devtype, obj->data.sysfs.type);
4626 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4627 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4628 } else if (r >= 100.0 || r == 0) {
4629 snprintf(p, p_max_size, "%d", (int) r);
4631 snprintf(p, p_max_size, "%.1f", r);
4637 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4638 obj->data.sysfs.devtype, obj->data.sysfs.type);
4640 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4641 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4642 } else if (r >= 100.0 || r == 0) {
4643 snprintf(p, p_max_size, "%d", (int) r);
4645 snprintf(p, p_max_size, "%.1f", r);
4651 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4652 obj->data.sysfs.devtype, obj->data.sysfs.type);
4654 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4655 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4656 } else if (r >= 100.0 || r == 0) {
4657 snprintf(p, p_max_size, "%d", (int) r);
4659 snprintf(p, p_max_size, "%.1f", r);
4662 #endif /* !__OpenBSD__ */
4664 new_alignr(p, obj->data.i);
4667 new_alignc(p, obj->data.i);
4670 struct text_object subroot;
4671 struct information *tmp_info =
4672 malloc(sizeof(struct information));
4673 memcpy(tmp_info, cur, sizeof(struct information));
4674 parse_conky_vars(&subroot, obj->data.ifblock.s, p, tmp_info);
4676 if (strlen(p) != 0) {
4680 free_text_objects(&subroot, 0);
4684 if (obj->data.ifblock.str
4685 && !check_contains(obj->data.ifblock.s,
4686 obj->data.ifblock.str)) {
4688 } else if (obj->data.ifblock.s
4689 && access(obj->data.ifblock.s, F_OK)) {
4694 if ((obj->data.ifblock.s)
4695 && (!check_mount(obj->data.ifblock.s))) {
4700 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4704 #if defined(__linux__)
4706 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4710 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4713 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4718 human_readable(cur->mem * 1024, p, 255, "mem");
4721 human_readable(cur->memeasyfree * 1024, p, 255, "memeasyfree");
4724 human_readable(cur->memfree * 1024, p, 255, "memfree");
4727 human_readable(cur->memmax * 1024, p, 255, "memmax");
4731 spaced_print(p, p_max_size, "%*llu", 4, "memperc",
4732 pad_percents, cur->mem * 100 / cur->memmax);
4736 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4737 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4740 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4741 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4742 100, 1, obj->showaslog);
4747 snprintf(p, p_max_size, "%d", mixer_get_avg(obj->data.l));
4750 snprintf(p, p_max_size, "%d", mixer_get_left(obj->data.l));
4753 snprintf(p, p_max_size, "%d", mixer_get_right(obj->data.l));
4756 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4757 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
4760 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4761 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
4764 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4765 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
4769 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4771 OBJ(monitor_number) {
4772 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4778 update_mail_count(&obj->data.local_mail);
4779 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4782 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
4784 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4787 update_mail_count(&obj->data.local_mail);
4788 snprintf(p, p_max_size, "%d",
4789 obj->data.local_mail.new_mail_count);
4792 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
4795 new_outline(p, obj->data.l);
4798 spaced_print(p, p_max_size, "%hu", 5, "processes", cur->procs);
4800 OBJ(running_processes) {
4801 spaced_print(p, p_max_size, "%hu", 3, "running_processes", cur->run_procs);
4804 snprintf(p, p_max_size, "%s", obj->data.s);
4807 new_bg(p, obj->data.l);
4810 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
4813 human_readable(cur->swap * 1024, p, 255, "swap");
4816 human_readable(cur->swapmax * 1024, p, 255, "swapmax");
4819 if (cur->swapmax == 0) {
4820 strncpy(p, "No swap", p_max_size);
4822 spaced_print(p, p_max_size, "%*llu", 4, "swapperc",
4823 pad_percents, cur->swap * 100 / cur->swapmax);
4827 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4828 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
4831 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4834 time_t t = time(NULL);
4835 struct tm *tm = localtime(&t);
4837 setlocale(LC_TIME, "");
4838 strftime(p, p_max_size, obj->data.s, tm);
4841 time_t t = time(NULL);
4842 struct tm *tm = gmtime(&t);
4844 strftime(p, p_max_size, obj->data.s, tm);
4851 if (obj->data.tztime.tz) {
4852 oldTZ = getenv("TZ");
4853 setenv("TZ", obj->data.tztime.tz, 1);
4859 setlocale(LC_TIME, "");
4860 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4862 setenv("TZ", oldTZ, 1);
4867 // Needless to free oldTZ since getenv gives ptr to static data
4870 human_readable(obj->data.net->recv, p, 255, "totaldown");
4873 human_readable(obj->data.net->trans, p, 255, "totalup");
4876 snprintf(p, p_max_size, "%d", total_updates);
4879 spaced_print(p, p_max_size, "%d", 6, "upspeed",
4880 round_to_int(obj->data.net->trans_speed / 1024));
4883 spaced_print(p, p_max_size, "%.1f", 8, "upspeedf",
4884 obj->data.net->trans_speed / 1024.0);
4887 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4888 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
4891 format_seconds_short(p, p_max_size, (int) cur->uptime);
4894 format_seconds(p, p_max_size, (int) cur->uptime);
4897 snprintf(p, p_max_size, "%s", cur->users.names);
4900 snprintf(p, p_max_size, "%s", cur->users.terms);
4903 snprintf(p, p_max_size, "%s", cur->users.times);
4906 snprintf(p, p_max_size, "%d", cur->users.number);
4908 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
4909 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4913 msg = get_apm_adapter();
4914 snprintf(p, p_max_size, "%s", msg);
4917 OBJ(apm_battery_life) {
4920 msg = get_apm_battery_life();
4921 snprintf(p, p_max_size, "%s", msg);
4924 OBJ(apm_battery_time) {
4927 msg = get_apm_battery_time();
4928 snprintf(p, p_max_size, "%s", msg);
4931 #endif /* __FreeBSD__ __OpenBSD__ */
4935 int len = obj->data.i;
4936 if (len == 0 || len > p_max_size)
4938 snprintf(p, len, "%s", cur->mpd.title);
4941 int len = obj->data.i;
4942 if (len == 0 || len > p_max_size)
4944 snprintf(p, len, "%s", cur->mpd.artist);
4947 int len = obj->data.i;
4948 if (len == 0 || len > p_max_size)
4950 snprintf(p, len, "%s", cur->mpd.album);
4953 snprintf(p, p_max_size, "%s", cur->mpd.random);
4956 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4959 int len = obj->data.i;
4960 if (len == 0 || len > p_max_size)
4962 snprintf(p, len, "%s", cur->mpd.track);
4965 int len = obj->data.i;
4966 if (len == 0 || len > p_max_size)
4968 snprintf(p, len, "%s", cur->mpd.name);
4971 int len = obj->data.i;
4972 if (len == 0 || len > p_max_size)
4974 snprintf(p, len, "%s", cur->mpd.file);
4977 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4980 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4983 snprintf(p, p_max_size, "%s", cur->mpd.status);
4986 format_media_player_time(p, p_max_size, cur->mpd.elapsed);
4989 format_media_player_time(p, p_max_size, cur->mpd.length);
4992 spaced_print(p, p_max_size, "%*d", 4, "mpd_percent",
4993 pad_percents, (int) (cur->mpd.progress * 100));
4996 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4997 (int) (cur->mpd.progress * 255.0f));
5000 int len = obj->data.i;
5001 if (len == 0 || len > p_max_size)
5004 memset(p, 0, p_max_size);
5005 if (cur->mpd.artist && *cur->mpd.artist && cur->mpd.title
5006 && *cur->mpd.title) {
5007 snprintf(p, len, "%s - %s", cur->mpd.artist,
5009 } else if (cur->mpd.title && *cur->mpd.title) {
5010 snprintf(p, len, "%s", cur->mpd.title);
5011 } else if (cur->mpd.artist && *cur->mpd.artist) {
5012 snprintf(p, len, "%s", cur->mpd.artist);
5013 } else if (cur->mpd.file && *cur->mpd.file) {
5014 snprintf(p, len, "%s", cur->mpd.file);
5019 OBJ(if_mpd_playing) {
5020 if (!cur->mpd.is_playing) {
5028 snprintf(p, p_max_size, "%s", (cur->moc.state ? cur->moc.state : "??"));
5031 snprintf(p, p_max_size, "%s", (cur->moc.file ? cur->moc.file : "no file"));
5034 snprintf(p, p_max_size, "%s", (cur->moc.title ? cur->moc.title : "no title"));
5037 snprintf(p, p_max_size, "%s", (cur->moc.artist ? cur->moc.artist : "no artist"));
5040 snprintf(p, p_max_size, "%s", (cur->moc.song ? cur->moc.song : "no song"));
5043 snprintf(p, p_max_size, "%s", (cur->moc.album ? cur->moc.album : "no album"));
5045 OBJ(moc_totaltime) {
5046 snprintf(p, p_max_size, "%s", (cur->moc.totaltime ? cur->moc.totaltime : "0:00"));
5049 snprintf(p, p_max_size, "%s", (cur->moc.timeleft ? cur->moc.timeleft : "0:00"));
5052 snprintf(p, p_max_size, "%s", (cur->moc.curtime ? cur->moc.curtime : "0:00"));
5055 snprintf(p, p_max_size, "%s", (cur->moc.bitrate ? cur->moc.bitrate : "0Kbps"));
5058 snprintf(p, p_max_size, "%s", (cur->moc.rate ? cur->moc.rate : "0KHz"));
5063 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5066 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5069 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5072 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5074 OBJ(xmms2_comment) {
5075 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5078 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5081 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5084 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5086 OBJ(xmms2_tracknr) {
5087 if (cur->xmms2.tracknr != -1) {
5088 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5091 OBJ(xmms2_bitrate) {
5092 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5095 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5098 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5100 OBJ(xmms2_elapsed) {
5101 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5102 (cur->xmms2.elapsed / 1000) % 60);
5104 OBJ(xmms2_duration) {
5105 snprintf(p, p_max_size, "%02d:%02d",
5106 cur->xmms2.duration / 60000,
5107 (cur->xmms2.duration / 1000) % 60);
5109 OBJ(xmms2_percent) {
5110 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5113 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5114 (int) (cur->xmms2.progress * 255.0f));
5116 OBJ(xmms2_playlist) {
5117 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5119 OBJ(xmms2_timesplayed) {
5120 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5123 if (strlen(cur->xmms2.title) < 2
5124 && strlen(cur->xmms2.title) < 2) {
5125 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5127 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5131 OBJ(if_xmms2_connected) {
5132 if (cur->xmms2_conn_state != 1) {
5138 OBJ(audacious_status) {
5139 snprintf(p, p_max_size, "%s",
5140 cur->audacious.items[AUDACIOUS_STATUS]);
5142 OBJ(audacious_title) {
5143 snprintf(p, cur->audacious.max_title_len > 0
5144 ? cur->audacious.max_title_len : p_max_size, "%s",
5145 cur->audacious.items[AUDACIOUS_TITLE]);
5147 OBJ(audacious_length) {
5148 snprintf(p, p_max_size, "%s",
5149 cur->audacious.items[AUDACIOUS_LENGTH]);
5151 OBJ(audacious_length_seconds) {
5152 snprintf(p, p_max_size, "%s",
5153 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5155 OBJ(audacious_position) {
5156 snprintf(p, p_max_size, "%s",
5157 cur->audacious.items[AUDACIOUS_POSITION]);
5159 OBJ(audacious_position_seconds) {
5160 snprintf(p, p_max_size, "%s",
5161 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5163 OBJ(audacious_bitrate) {
5164 snprintf(p, p_max_size, "%s",
5165 cur->audacious.items[AUDACIOUS_BITRATE]);
5167 OBJ(audacious_frequency) {
5168 snprintf(p, p_max_size, "%s",
5169 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5171 OBJ(audacious_channels) {
5172 snprintf(p, p_max_size, "%s",
5173 cur->audacious.items[AUDACIOUS_CHANNELS]);
5175 OBJ(audacious_filename) {
5176 snprintf(p, p_max_size, "%s",
5177 cur->audacious.items[AUDACIOUS_FILENAME]);
5179 OBJ(audacious_playlist_length) {
5180 snprintf(p, p_max_size, "%s",
5181 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5183 OBJ(audacious_playlist_position) {
5184 snprintf(p, p_max_size, "%s",
5185 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5187 OBJ(audacious_main_volume) {
5188 snprintf(p, p_max_size, "%s",
5189 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5191 OBJ(audacious_bar) {
5195 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5196 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5197 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5203 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5206 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5209 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5212 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5215 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5218 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5222 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5225 switch (obj->data.top.type) {
5227 snprintf(p, 16, "%-15s",
5228 cur->cpu[obj->data.top.num]->name);
5231 snprintf(p, 7, "%6.2f",
5232 cur->cpu[obj->data.top.num]->amount);
5235 snprintf(p, 6, "%5i",
5236 cur->cpu[obj->data.top.num]->pid);
5239 snprintf(p, 7, "%6.2f",
5240 cur->cpu[obj->data.top.num]->totalmem);
5243 timeval = format_time(
5244 cur->cpu[obj->data.top.num]->total_cpu_time, 9);
5245 snprintf(p, 10, "%9s", timeval);
5249 human_readable(cur->cpu[obj->data.top.num]->rss,
5253 human_readable(cur->cpu[obj->data.top.num]->vsize,
5257 ERR("Unhandled top data type: %d\n",
5258 obj->data.top.type);
5261 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5265 if (obj->data.top.num >= 0 && obj->data.top.num < 10) {
5268 switch (obj->data.top.type) {
5270 snprintf(p, 16, "%-15s",
5271 cur->memu[obj->data.top.num]->name);
5274 snprintf(p, 7, "%6.2f",
5275 cur->memu[obj->data.top.num]->amount);
5278 snprintf(p, 6, "%5i",
5279 cur->memu[obj->data.top.num]->pid);
5282 snprintf(p, 7, "%6.2f",
5283 cur->memu[obj->data.top.num]->totalmem);
5286 timeval = format_time(
5287 cur->memu[obj->data.top.num]->total_cpu_time,
5289 snprintf(p, 10, "%9s", timeval);
5293 human_readable(cur->cpu[obj->data.top.num]->rss,
5297 human_readable(cur->cpu[obj->data.top.num]->vsize,
5301 ERR("Unhandled top data type: %d\n",
5302 obj->data.top.type);
5305 ERR("Top index < 0 or > 10: %d\n", obj->data.top.num);
5309 if (current_update_time - obj->data.tail.last_update
5310 < obj->data.tail.interval) {
5311 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5317 obj->data.tail.last_update = current_update_time;
5319 if (obj->data.tail.fd != -1) {
5320 tail_pipe(obj, p, p_max_size);
5324 fp = fopen(obj->data.tail.logfile, "rt");
5327 /* Send one message, but do not consistently spam
5328 * on missing logfiles. */
5329 if (obj->data.tail.readlines != 0) {
5330 ERR("tail logfile failed to open");
5331 strcpy(obj->data.tail.buffer, "Logfile Missing");
5333 obj->data.tail.readlines = 0;
5334 snprintf(p, p_max_size, "Logfile Missing");
5336 obj->data.tail.readlines = 0;
5337 /* -1 instead of 0 to avoid counting a trailing
5339 fseek(fp, -1, SEEK_END);
5340 bsize = ftell(fp) + 1;
5341 for (iter = obj->data.tail.wantedlines; iter > 0;
5343 nl = rev_fcharfind(fp, '\n', iter);
5348 obj->data.tail.readlines = iter;
5349 if (obj->data.tail.readlines
5350 < obj->data.tail.wantedlines) {
5351 fseek(fp, 0, SEEK_SET);
5353 fseek(fp, nl + 1, SEEK_SET);
5356 /* Make sure bsize is at least 1 byte smaller than the
5357 * buffer max size. */
5358 if (bsize > (long) ((text_buffer_size * 20) - 1)) {
5359 fseek(fp, bsize - text_buffer_size * 20 - 1,
5361 bsize = text_buffer_size * 20 - 1;
5363 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
5366 /* Clean up trailing newline, make sure the
5367 * buffer is null terminated. */
5368 if (obj->data.tail.buffer[bsize - 1] == '\n') {
5369 obj->data.tail.buffer[bsize - 1] = '\0';
5371 obj->data.tail.buffer[bsize] = '\0';
5373 snprintf(p, p_max_size, "%s",
5374 obj->data.tail.buffer);
5376 strcpy(obj->data.tail.buffer, "Logfile Empty");
5377 snprintf(p, p_max_size, "Logfile Empty");
5380 } /* if cur_upd_time >= */
5384 if (current_update_time - obj->data.tail.last_update
5385 < obj->data.tail.interval) {
5386 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5392 obj->data.tail.last_update = current_update_time;
5394 fp = fopen(obj->data.tail.logfile, "rt");
5396 /* Send one message, but do not consistently spam
5397 * on missing logfiles. */
5398 if (obj->data.tail.readlines != 0) {
5399 ERR("head logfile failed to open");
5400 strcpy(obj->data.tail.buffer, "Logfile Missing");
5402 obj->data.tail.readlines = 0;
5403 snprintf(p, p_max_size, "Logfile Missing");
5405 obj->data.tail.readlines = 0;
5406 for (iter = obj->data.tail.wantedlines; iter > 0;
5408 nl = fwd_fcharfind(fp, '\n', iter);
5413 obj->data.tail.readlines = iter;
5414 /* Make sure nl is at least 1 byte smaller than the
5415 * buffer max size. */
5416 if (nl > (long) ((text_buffer_size * 20) - 1)) {
5417 nl = text_buffer_size * 20 - 1;
5419 nl = fread(obj->data.tail.buffer, 1, nl, fp);
5422 /* Clean up trailing newline, make sure the buffer
5423 * is null terminated. */
5424 if (obj->data.tail.buffer[nl - 1] == '\n') {
5425 obj->data.tail.buffer[nl - 1] = '\0';
5427 obj->data.tail.buffer[nl] = '\0';
5429 snprintf(p, p_max_size, "%s",
5430 obj->data.tail.buffer);
5432 strcpy(obj->data.tail.buffer, "Logfile Empty");
5433 snprintf(p, p_max_size, "Logfile Empty");
5435 } /* if fp == null */
5436 } /* cur_upd_time >= */
5440 FILE *fp = fopen(obj->data.s,"r");
5447 while(fgets(buf, BUFSZ, fp) != NULL){
5448 for(j = 0; buf[j] != 0; j++) {
5449 if(buf[j] == '\n') {
5454 sprintf(p, "%d", lines);
5457 sprintf(p, "File Unreadable");
5462 FILE *fp = fopen(obj->data.s,"r");
5467 char inword = FALSE;
5470 while(fgets(buf, BUFSZ, fp) != NULL){
5471 for(j = 0; buf[j] != 0; j++) {
5472 if(!isspace(buf[j])) {
5473 if(inword == FALSE) {
5482 sprintf(p, "%d", words);
5485 sprintf(p, "File Unreadable");
5488 #ifdef TCP_PORT_MONITOR
5490 tcp_portmon_action(p, p_max_size,
5491 &obj->data.tcp_port_monitor);
5497 iconv_converting = 1;
5498 iconv_selected = obj->a;
5501 iconv_converting = 0;
5506 OBJ(entropy_avail) {
5507 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5509 OBJ(entropy_poolsize) {
5510 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5513 double entropy_perc;
5515 entropy_perc = (double) cur->entropy.entropy_avail /
5516 (double) cur->entropy.poolsize;
5517 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5523 s = smapi_get_val(obj->data.s);
5524 snprintf(p, p_max_size, "%s", s);
5528 OBJ(if_smapi_bat_installed) {
5530 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5531 if(!smapi_bat_installed(idx)) {
5535 ERR("argument to if_smapi_bat_installed must be an integer");
5537 OBJ(smapi_bat_perc) {
5539 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5540 val = smapi_bat_installed(idx) ?
5541 smapi_get_bat_int(idx, "remaining_percent") : 0;
5542 spaced_print(p, p_max_size, "%*d", 4, "smapi_bat_perc", pad_percents, val);
5544 ERR("argument to smapi_bat_perc must be an integer");
5546 OBJ(smapi_bat_temp) {
5548 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5549 val = smapi_bat_installed(idx) ?
5550 smapi_get_bat_int(idx, "temperature") : 0;
5551 /* temperature is in milli degree celsius */
5552 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5554 ERR("argument to smapi_bat_temp must be an integer");
5556 OBJ(smapi_bat_power) {
5558 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5559 val = smapi_bat_installed(idx) ?
5560 smapi_get_bat_int(idx, "power_now") : 0;
5561 /* power_now is in mW, set to W with one digit precision */
5562 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5564 ERR("argument to smapi_bat_power must be an integer");
5566 OBJ(smapi_bat_bar) {
5567 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5568 new_bar(p, obj->a, obj->b, (int)
5569 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5571 new_bar(p, obj->a, obj->b, 0);
5577 struct text_object subroot;
5578 parse_conky_vars(&subroot, obj->data.scroll.text, p, cur);
5580 if (strlen(p) <= obj->data.scroll.show) {
5583 #define LINESEPARATOR '|'
5584 //place all the lines behind each other with LINESEPARATOR between them
5585 for(j = 0; p[j] != 0; j++) {
5590 //scroll the output obj->data.scroll.start places by copying that many chars from
5591 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5592 //at the back of the string
5593 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5594 strncpy(tmp, p, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5595 for(j = obj->data.scroll.start; p[j] != 0; j++){
5596 p[j - obj->data.scroll.start] = p[j];
5598 strcpy(&p[j - obj->data.scroll.start], tmp);
5600 //only show the requested number of chars
5601 if(obj->data.scroll.show < j) {
5602 p[obj->data.scroll.show] = 0;
5604 //next time, scroll a place more or reset scrolling if we are at the end
5605 obj->data.scroll.start += obj->data.scroll.step;
5606 if(obj->data.scroll.start >= j){
5607 obj->data.scroll.start = 0;
5612 int value = get_nvidia_value(obj->data.nvidia.type, display);
5614 snprintf(p, p_max_size, "N/A");
5615 else if (obj->data.nvidia.type == NV_TEMP)
5616 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5617 else if (obj->data.nvidia.print_as_float &&
5618 value > 0 && value < 100)
5619 snprintf(p, p_max_size, "%.1f", (float)value);
5621 snprintf(p, p_max_size, "%d", value);
5631 unsigned int a = strlen(p);
5634 if (a > 0 && iconv_converting && iconv_selected > 0
5635 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5637 size_t dummy1, dummy2;
5638 char *ptr = buff_in;
5641 dummy1 = dummy2 = a;
5643 strncpy(buff_in, p, p_max_size);
5645 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5646 while (dummy1 > 0) {
5647 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5650 ERR("Iconv codeset conversion failed");
5655 /* It is nessecary when we are converting from multibyte to
5656 * singlebyte codepage */
5659 #endif /* HAVE_ICONV */
5666 double current_update_time, next_update_time, last_update_time;
5668 static void generate_text(void)
5670 struct information *cur = &info;
5677 current_update_time = get_time();
5680 /* fix diskio rates to b/s (use update_interval */
5681 info.diskio_read_value /= update_interval;
5682 info.diskio_write_value /= update_interval;
5683 info.diskio_value /= update_interval;
5685 /* add things to the buffer */
5691 generate_text_internal(p, max_user_text, global_root_object, cur);
5693 if (stuff_in_upper_case) {
5696 tmp_p = text_buffer;
5698 *tmp_p = toupper(*tmp_p);
5703 next_update_time += update_interval;
5704 if (next_update_time < get_time()) {
5705 next_update_time = get_time() + update_interval;
5706 } else if (next_update_time > get_time() + update_interval) {
5707 next_update_time = get_time() + update_interval;
5709 last_update_time = current_update_time;
5715 static void set_font(void)
5719 if (window.xftdraw != NULL) {
5720 XftDrawDestroy(window.xftdraw);
5722 window.xftdraw = XftDrawCreate(display, window.drawable,
5723 DefaultVisual(display, screen), DefaultColormap(display, screen));
5727 XSetFont(display, window.gc, fonts[selected_font].font->fid);
5733 static inline int get_string_width(const char *s)
5736 return *s ? calc_text_width(s, strlen(s)) : 0;
5742 static inline int get_string_width_special(char *s)
5754 p = strndup(s, text_buffer_size);
5758 if (*p == SPECIAL_CHAR) {
5759 /* shift everything over by 1 so that the special char
5760 * doesn't mess up the size calculation */
5761 for (i = 0; i < strlen(p); i++) {
5762 *(p + i) = *(p + i + 1);
5764 if (specials[special_index + idx].type == GRAPH
5765 || specials[special_index + idx].type == BAR) {
5766 width += specials[special_index + idx].width;
5773 if (strlen(final) > 1) {
5774 width += calc_text_width(final, strlen(final));
5779 return (s) ? strlen(s) : 0;
5784 static void text_size_updater(char *s);
5786 int last_font_height;
5787 static void update_text_area(void)
5791 /* update text size if it isn't fixed */
5796 text_width = minimum_width;
5799 last_font_height = font_height();
5800 for_each_line(text_buffer, text_size_updater);
5802 if (text_height < minimum_height) {
5803 text_height = minimum_height;
5805 if (text_width > maximum_width && maximum_width > 0) {
5806 text_width = maximum_width;
5810 /* get text position on workarea */
5811 switch (text_alignment) {
5818 x = workarea[2] - text_width - gap_x;
5823 x = workarea[2] / 2 - text_width / 2 - gap_x;
5830 y = workarea[3] - text_height - gap_y;
5834 x = workarea[2] - text_width - gap_x;
5835 y = workarea[3] - text_height - gap_y;
5839 x = workarea[2] / 2 - text_width / 2 - gap_x;
5840 y = workarea[3] - text_height - gap_y;
5845 y = workarea[3] / 2 - text_height / 2 - gap_y;
5849 x = workarea[2] - text_width - gap_x;
5850 y = workarea[3] / 2 - text_height / 2 - gap_y;
5854 case NONE: // Let the WM manage the window
5865 if (own_window && !fixed_pos) {
5868 text_start_x = border_margin + 1;
5869 text_start_y = border_margin + 1;
5870 window.x = x - border_margin - 1;
5871 window.y = y - border_margin - 1;
5875 /* If window size doesn't match to workarea's size,
5876 * then window probably includes panels (gnome).
5877 * Blah, doesn't work on KDE. */
5878 if (workarea[2] != window.width || workarea[3] != window.height) {
5890 static int cur_x, cur_y; /* current x and y for drawing */
5892 //draw_mode also without X11 because we only need to print to stdout with FG
5893 static int draw_mode; /* FG, BG or OUTLINE */
5895 static long current_color;
5898 static void text_size_updater(char *s)
5903 /* get string widths and skip specials */
5906 if (*p == SPECIAL_CHAR) {
5908 w += get_string_width(s);
5911 if (specials[special_index].type == BAR
5912 || specials[special_index].type == GRAPH) {
5913 w += specials[special_index].width;
5914 if (specials[special_index].height > last_font_height) {
5915 last_font_height = specials[special_index].height;
5916 last_font_height += font_ascent();
5918 } else if (specials[special_index].type == OFFSET) {
5919 if (specials[special_index].arg > 0) {
5920 w += specials[special_index].arg;
5922 } else if (specials[special_index].type == VOFFSET) {
5923 last_font_height += specials[special_index].arg;
5924 } else if (specials[special_index].type == GOTO) {
5925 if (specials[special_index].arg > cur_x) {
5926 w = (int) specials[special_index].arg;
5928 } else if (specials[special_index].type == TAB) {
5929 int start = specials[special_index].arg;
5930 int step = specials[special_index].width;
5932 if (!step || step < 0) {
5935 w += step - (cur_x - text_start_x - start) % step;
5936 } else if (specials[special_index].type == FONT) {
5937 selected_font = specials[special_index].font_added;
5938 if (font_height() > last_font_height) {
5939 last_font_height = font_height();
5948 w += get_string_width(s);
5949 if (w > text_width) {
5952 if (text_width > maximum_width && maximum_width) {
5953 text_width = maximum_width;
5956 text_height += last_font_height;
5957 last_font_height = font_height();
5961 static inline void set_foreground_color(long c)
5964 XSetForeground(display, window.gc, c);
5968 static void draw_string(const char *s)
5970 int i, i2, pos, width_of_s;
5978 width_of_s = get_string_width(s);
5979 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
5981 fflush(stdout); /* output immediately, don't buffer */
5983 memset(tmpstring1, 0, text_buffer_size);
5984 memset(tmpstring2, 0, text_buffer_size);
5985 strncpy(tmpstring1, s, text_buffer_size - 1);
5990 max = ((text_width - width_of_s) / get_string_width(" "));
5992 /* This code looks for tabs in the text and coverts them to spaces.
5993 * The trick is getting the correct number of spaces, and not going
5994 * over the window's size without forcing the window larger. */
5995 for (i = 0; i < (int) text_buffer_size; i++) {
5996 if (tmpstring1[i] == '\t') {
5998 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
5999 /* guard against overrun */
6000 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6005 /* guard against overrun */
6006 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6011 if (text_width == maximum_width) {
6012 /* this means the text is probably pushing the limit,
6013 * so we'll chop it */
6014 while (cur_x + get_string_width(tmpstring2) - text_start_x
6015 > maximum_width && strlen(tmpstring2) > 0) {
6016 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6027 c.pixel = current_color;
6028 XQueryColor(display, DefaultColormap(display, screen), &c);
6031 c2.color.red = c.red;
6032 c2.color.green = c.green;
6033 c2.color.blue = c.blue;
6034 c2.color.alpha = fonts[selected_font].font_alpha;
6036 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6037 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6039 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6040 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6045 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6048 cur_x += width_of_s;
6050 memcpy(tmpstring1, s, text_buffer_size);
6053 long redmask, greenmask, bluemask;
6055 void set_up_gradient(void)
6059 colour_depth = DisplayPlanes(display, screen);
6063 if (colour_depth != 24 && colour_depth != 16) {
6064 ERR("using non-standard colour depth, gradients may look like a "
6071 for (i = (colour_depth / 3) - 1; i >= 0; i--) {
6073 greenmask |= 1 << i;
6076 if (colour_depth % 3 == 1) {
6077 greenmask |= 1 << (colour_depth / 3);
6079 redmask = redmask << (2 * colour_depth / 3 + colour_depth % 3);
6080 greenmask = greenmask << (colour_depth / 3);
6083 /* this function returns the next colour between two colours for a gradient */
6084 unsigned long do_gradient(unsigned long first_colour,
6085 unsigned long last_colour)
6088 int red1, green1, blue1; // first colour
6089 int red2, green2, blue2; // second colour
6090 int red3 = 0, green3 = 0, blue3 = 0; // difference
6091 short redshift = (2 * colour_depth / 3 + colour_depth % 3);
6092 short greenshift = (colour_depth / 3);
6094 red1 = (first_colour & redmask) >> redshift;
6095 green1 = (first_colour & greenmask) >> greenshift;
6096 blue1 = first_colour & bluemask;
6097 red2 = (last_colour & redmask) >> redshift;
6098 green2 = (last_colour & greenmask) >> greenshift;
6099 blue2 = last_colour & bluemask;
6106 if (green1 > green2) {
6109 if (green1 < green2) {
6112 if (blue1 > blue2) {
6115 if (blue1 < blue2) {
6130 if (red1 > bluemask) {
6133 if (green1 > bluemask) {
6136 if (blue1 > bluemask) {
6139 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
6143 /* this function returns the max diff for a gradient */
6144 unsigned long gradient_max(unsigned long first_colour,
6145 unsigned long last_colour)
6147 int red1, green1, blue1; // first colour
6148 int red2, green2, blue2; // second colour
6149 int red3 = 0, green3 = 0, blue3 = 0; // difference
6150 long redshift, greenshift;
6153 if (colour_depth == 0) {
6156 redshift = (2 * colour_depth / 3 + colour_depth % 3);
6157 greenshift = (colour_depth / 3);
6159 red1 = (first_colour & redmask) >> redshift;
6160 green1 = (first_colour & greenmask) >> greenshift;
6161 blue1 = first_colour & bluemask;
6162 red2 = (last_colour & redmask) >> redshift;
6163 green2 = (last_colour & greenmask) >> greenshift;
6164 blue2 = last_colour & bluemask;
6165 red3 = abs(red1 - red2);
6166 green3 = abs(green1 - green2);
6167 blue3 = abs(blue1 - blue2);
6179 static void draw_line(char *s)
6187 cur_x = text_start_x;
6188 cur_y += font_ascent();
6189 font_h = font_height();
6191 /* find specials and draw stuff */
6194 if (*p == SPECIAL_CHAR) {
6197 /* draw string before special */
6204 switch (specials[special_index].type) {
6205 case HORIZONTAL_LINE:
6207 int h = specials[special_index].height;
6208 int mid = font_ascent() / 2;
6210 w = text_start_x + text_width - cur_x;
6212 XSetLineAttributes(display, window.gc, h, LineSolid,
6213 CapButt, JoinMiter);
6214 XDrawLine(display, window.drawable, window.gc, cur_x,
6215 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6221 int h = specials[special_index].height;
6222 int tmp_s = specials[special_index].arg;
6223 int mid = font_ascent() / 2;
6224 char ss[2] = { tmp_s, tmp_s };
6226 w = text_start_x + text_width - cur_x - 1;
6227 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6228 CapButt, JoinMiter);
6229 XSetDashes(display, window.gc, 0, ss, 2);
6230 XDrawLine(display, window.drawable, window.gc, cur_x,
6231 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6237 int h, bar_usage, by;
6238 if (cur_x - text_start_x > maximum_width
6239 && maximum_width > 0) {
6242 h = specials[special_index].height;
6243 bar_usage = specials[special_index].arg;
6244 by = cur_y - (font_ascent() / 2) - 1;
6246 if (h < font_height()) {
6249 w = specials[special_index].width;
6251 w = text_start_x + text_width - cur_x - 1;
6257 XSetLineAttributes(display, window.gc, 1, LineSolid,
6258 CapButt, JoinMiter);
6260 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6262 XFillRectangle(display, window.drawable, window.gc, cur_x,
6263 by, w * bar_usage / 255, h);
6264 if (specials[special_index].height > cur_y_add
6265 && specials[special_index].height > font_h) {
6266 cur_y_add = specials[special_index].height;
6273 int h, by, i, j = 0;
6274 int gradient_size = 0;
6275 float gradient_factor = 0;
6276 float gradient_update = 0;
6277 unsigned long last_colour = current_color;
6278 unsigned long tmpcolour = current_color;
6279 if (cur_x - text_start_x > maximum_width
6280 && maximum_width > 0) {
6283 h = specials[special_index].height;
6284 by = cur_y - (font_ascent() / 2) - 1;
6286 if (h < font_height()) {
6289 w = specials[special_index].width;
6291 w = text_start_x + text_width - cur_x - 1;
6296 if (draw_graph_borders) {
6297 XSetLineAttributes(display, window.gc, 1, LineSolid,
6298 CapButt, JoinMiter);
6299 XDrawRectangle(display, window.drawable, window.gc,
6302 XSetLineAttributes(display, window.gc, 1, LineSolid,
6303 CapButt, JoinMiter);
6305 if (specials[special_index].last_colour != 0
6306 || specials[special_index].first_colour != 0) {
6307 tmpcolour = specials[special_index].last_colour;
6309 gradient_max(specials[special_index].last_colour,
6310 specials[special_index].first_colour);
6311 gradient_factor = (float) gradient_size / (w - 2);
6313 for (i = w - 2; i > -1; i--) {
6314 if (specials[special_index].last_colour != 0
6315 || specials[special_index].first_colour != 0) {
6316 XSetForeground(display, window.gc, tmpcolour);
6317 gradient_update += gradient_factor;
6318 while (gradient_update > 0) {
6319 tmpcolour = do_gradient(tmpcolour,
6320 specials[special_index].first_colour);
6324 if ((w - i) / ((float) (w - 2) /
6325 (specials[special_index].graph_width)) > j
6326 && j < MAX_GRAPH_DEPTH - 3) {
6329 /* this is mugfugly, but it works */
6330 XDrawLine(display, window.drawable, window.gc,
6331 cur_x + i + 1, by + h, cur_x + i + 1,
6332 by + h - specials[special_index].graph[j] *
6333 (h - 1) / specials[special_index].graph_scale);
6335 if (specials[special_index].height > cur_y_add
6336 && specials[special_index].height > font_h) {
6337 cur_y_add = specials[special_index].height;
6339 /* if (draw_mode == BG) {
6340 set_foreground_color(default_bg_color);
6341 } else if (draw_mode == OUTLINE) {
6342 set_foreground_color(default_out_color);
6344 set_foreground_color(default_fg_color);
6346 if (show_graph_range) {
6349 unsigned short int seconds = update_interval * w;
6354 unsigned short int timeunits;
6356 timeunits = seconds / 86400; seconds %= 86400;
6357 if( timeunits > 0 ) {
6358 asprintf(&tmp_day_str, "%dd", timeunits);
6360 tmp_day_str = strdup("");
6362 timeunits = seconds / 3600; seconds %= 3600;
6363 if( timeunits > 0 ) {
6364 asprintf(&tmp_hour_str, "%dh", timeunits);
6366 tmp_hour_str = strdup("");
6368 timeunits = seconds / 60; seconds %= 60;
6370 asprintf(&tmp_min_str, "%dm", timeunits);
6372 tmp_min_str = strdup("");
6375 asprintf(&tmp_sec_str, "%ds", seconds);
6377 tmp_sec_str = strdup("");
6379 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6380 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6382 asprintf(&tmp_str, "Range not possible"); //should never happen, but better safe then sorry
6384 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6385 cur_y += font_height() / 2;
6386 draw_string(tmp_str);
6392 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6395 cur_x += font_ascent() / 2;
6396 cur_y += font_height() / 2;
6398 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6400 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6401 draw_string(tmp_str);
6407 set_foreground_color(last_colour);
6413 int old = font_ascent();
6415 cur_y -= font_ascent();
6416 selected_font = specials[special_index].font_added;
6417 if (cur_y + font_ascent() < cur_y + old) {
6420 cur_y += font_ascent();
6426 if (draw_mode == FG) {
6427 set_foreground_color(specials[special_index].arg);
6432 if (draw_mode == BG) {
6433 set_foreground_color(specials[special_index].arg);
6438 if (draw_mode == OUTLINE) {
6439 set_foreground_color(specials[special_index].arg);
6444 w += specials[special_index].arg;
6448 cur_y += specials[special_index].arg;
6452 if (specials[special_index].arg >= 0) {
6453 cur_x = (int) specials[special_index].arg;
6459 int start = specials[special_index].arg;
6460 int step = specials[special_index].width;
6462 if (!step || step < 0) {
6465 w = step - (cur_x - text_start_x - start) % step;
6471 /* TODO: add back in "+ border_margin" to the end of
6473 int pos_x = text_start_x + text_width -
6474 get_string_width_special(s);
6476 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6477 "get_string_width(p) %i gap_x %i "
6478 "specials[special_index].arg %i border_margin %i "
6479 "border_width %i\n", pos_x, text_start_x, text_width,
6480 cur_x, get_string_width_special(s), gap_x,
6481 specials[special_index].arg, border_margin,
6483 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6484 cur_x = pos_x - specials[special_index].arg;
6491 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6492 2 - (cur_x - text_start_x);
6493 /* int pos_x = text_start_x + text_width / 2 -
6494 get_string_width_special(s) / 2; */
6496 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6497 "get_string_width(p) %i gap_x %i "
6498 "specials[special_index].arg %i\n", pos_x, text_start_x,
6499 text_width, cur_x, get_string_width(s), gap_x,
6500 specials[special_index].arg); */
6501 if (pos_x > specials[special_index].arg) {
6502 w = pos_x - specials[special_index].arg;
6519 if (cur_y_add > 0) {
6521 cur_y -= font_descent();
6526 cur_y += font_descent();
6530 static void draw_text(void)
6533 cur_y = text_start_y;
6536 if (draw_borders && border_width > 0) {
6537 unsigned int b = (border_width + 1) / 2;
6539 if (stippled_borders) {
6540 char ss[2] = { stippled_borders, stippled_borders };
6541 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6542 CapButt, JoinMiter);
6543 XSetDashes(display, window.gc, 0, ss, 2);
6545 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6546 CapButt, JoinMiter);
6549 XDrawRectangle(display, window.drawable, window.gc,
6550 text_start_x - border_margin + b, text_start_y - border_margin + b,
6551 text_width + border_margin * 2 - 1 - b * 2,
6552 text_height + border_margin * 2 - 1 - b * 2);
6558 for_each_line(text_buffer, draw_line);
6561 static void draw_stuff(void)
6565 if (draw_shades && !draw_outline) {
6568 set_foreground_color(default_bg_color);
6579 for (i = -1; i < 2; i++) {
6580 for (j = -1; j < 2; j++) {
6581 if (i == 0 && j == 0) {
6586 set_foreground_color(default_out_color);
6587 draw_mode = OUTLINE;
6595 set_foreground_color(default_fg_color);
6604 swap.swap_window = window.window;
6605 swap.swap_action = XdbeBackground;
6606 XdbeSwapBuffers(display, &swap, 1);
6613 static void clear_text(int exposures)
6617 /* The swap action is XdbeBackground, which clears */
6622 /* there is some extra space for borders and outlines */
6623 XClearArea(display, window.window, text_start_x - border_margin - 1,
6624 text_start_y - border_margin - 1,
6625 text_width + border_margin * 2 + 2,
6626 text_height + border_margin * 2 + 2, exposures ? True : 0);
6631 static int need_to_update;
6633 /* update_text() generates new text and clears old text area */
6634 static void update_text(void)
6643 static void main_loop(void)
6645 #ifdef SIGNAL_BLOCKING
6646 sigset_t newmask, oldmask;
6650 Region region = XCreateRegion();
6654 XserverRegion region2, part;
6655 int event_base, error_base;
6657 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
6658 ERR("Xdamage extension unavailable");
6660 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
6661 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
6662 part = XFixesCreateRegionFromWindow(display, window.window, 0);
6663 #endif /* HAVE_XDAMAGE */
6666 #ifdef SIGNAL_BLOCKING
6667 sigemptyset(&newmask);
6668 sigaddset(&newmask, SIGINT);
6669 sigaddset(&newmask, SIGTERM);
6670 sigaddset(&newmask, SIGUSR1);
6673 next_update_time = last_update_time = get_time();
6675 while (total_run_times == 0 || info.looped < total_run_times) {
6678 #ifdef SIGNAL_BLOCKING
6679 /* block signals. we will inspect for pending signals later */
6680 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6681 CRIT_ERR("unable to sigprocmask()");
6688 /* wait for X event or timeout */
6690 if (!XPending(display)) {
6694 t = next_update_time - get_time();
6698 } else if (t > update_interval) {
6699 t = update_interval;
6702 tv.tv_sec = (long) t;
6703 tv.tv_usec = (long) (t * 1000000) % 1000000;
6705 FD_SET(ConnectionNumber(display), &fdsr);
6707 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
6709 if (errno != EINTR) {
6710 ERR("can't select(): %s", strerror(errno));
6716 t = (next_update_time - get_time()) * 1000000;
6717 usleep((useconds_t)t);
6725 if (need_to_update) {
6727 int wx = window.x, wy = window.y;
6735 /* resize window if it isn't right size */
6737 && (text_width + border_margin * 2 + 1 != window.width
6738 || text_height + border_margin * 2 + 1 != window.height)) {
6739 window.width = text_width + border_margin * 2 + 1;
6740 window.height = text_height + border_margin * 2 + 1;
6741 XResizeWindow(display, window.window, window.width,
6744 set_transparent_background(window.window);
6748 /* move window if it isn't in right position */
6749 if (!fixed_pos && (window.x != wx || window.y != wy)) {
6750 XMoveWindow(display, window.window, window.x, window.y);
6761 r.x = text_start_x - border_margin;
6762 r.y = text_start_y - border_margin;
6763 r.width = text_width + border_margin * 2;
6764 r.height = text_height + border_margin * 2;
6765 XUnionRectWithRegion(&r, region, region);
6770 /* handle X events */
6771 while (XPending(display)) {
6774 XNextEvent(display, &ev);
6782 r.width = ev.xexpose.width;
6783 r.height = ev.xexpose.height;
6784 XUnionRectWithRegion(&r, region, region);
6789 case ReparentNotify:
6790 /* set background to ParentRelative for all parents */
6792 set_transparent_background(window.window);
6796 case ConfigureNotify:
6798 /* if window size isn't what expected, set fixed size */
6799 if (ev.xconfigure.width != window.width
6800 || ev.xconfigure.height != window.height) {
6801 if (window.width != 0 && window.height != 0) {
6805 /* clear old stuff before screwing up
6810 XWindowAttributes attrs;
6812 if (XGetWindowAttributes(display,
6813 window.window, &attrs)) {
6814 window.width = attrs.width;
6815 window.height = attrs.height;
6819 text_width = window.width - border_margin * 2 - 1;
6820 text_height = window.height - border_margin * 2 - 1;
6821 if (text_width > maximum_width
6822 && maximum_width > 0) {
6823 text_width = maximum_width;
6827 /* if position isn't what expected, set fixed pos
6828 * total_updates avoids setting fixed_pos when window
6829 * is set to weird locations when started */
6830 /* // this is broken
6831 if (total_updates >= 2 && !fixed_pos
6832 && (window.x != ev.xconfigure.x
6833 || window.y != ev.xconfigure.y)
6834 && (ev.xconfigure.x != 0
6835 || ev.xconfigure.y != 0)) {
6844 /* if an ordinary window with decorations */
6845 if ((window.type == TYPE_NORMAL)
6846 && (!TEST_HINT(window.hints,
6847 HINT_UNDECORATED))) {
6848 /* allow conky to hold input focus. */
6851 /* forward the click to the desktop window */
6852 XUngrabPointer(display, ev.xbutton.time);
6853 ev.xbutton.window = window.desktop;
6854 XSendEvent(display, ev.xbutton.window, False,
6855 ButtonPressMask, &ev);
6856 XSetInputFocus(display, ev.xbutton.window,
6857 RevertToParent, ev.xbutton.time);
6864 /* if an ordinary window with decorations */
6865 if ((window.type == TYPE_NORMAL)
6866 && (!TEST_HINT(window.hints,
6867 HINT_UNDECORATED))) {
6868 /* allow conky to hold input focus. */
6871 /* forward the release to the desktop window */
6872 ev.xbutton.window = window.desktop;
6873 XSendEvent(display, ev.xbutton.window, False,
6874 ButtonReleaseMask, &ev);
6883 if (ev.type == event_base + XDamageNotify) {
6884 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
6886 XFixesSetRegion(display, part, &dev->area, 1);
6887 XFixesUnionRegion(display, region2, region2, part);
6889 #endif /* HAVE_XDAMAGE */
6895 XDamageSubtract(display, damage, region2, None);
6896 XFixesSetRegion(display, region2, 0, 0);
6897 #endif /* HAVE_XDAMAGE */
6899 /* XDBE doesn't seem to provide a way to clear the back buffer without
6900 * interfering with the front buffer, other than passing XdbeBackground
6901 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6902 * redraw the text even if it wasn't part of the exposed area. OTOH,
6903 * if we're not going to call draw_stuff at all, then no swap happens
6904 * and we can safely do nothing. */
6906 if (!XEmptyRegion(region)) {
6911 r.x = text_start_x - border_margin;
6912 r.y = text_start_y - border_margin;
6913 r.width = text_width + border_margin * 2;
6914 r.height = text_height + border_margin * 2;
6915 XUnionRectWithRegion(&r, region, region);
6918 XSetRegion(display, window.gc, region);
6921 XftDrawSetClip(window.xftdraw, region);
6927 XDestroyRegion(region);
6928 region = XCreateRegion();
6932 #ifdef SIGNAL_BLOCKING
6933 /* unblock signals of interest and let handler fly */
6934 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
6935 CRIT_ERR("unable to sigprocmask()");
6939 switch (g_signal_pending) {
6942 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
6947 ERR("received SIGINT or SIGTERM to terminate. bye!");
6950 XDestroyRegion(region);
6953 XDamageDestroy(display, damage);
6954 XFixesDestroyRegion(display, region2);
6955 XFixesDestroyRegion(display, part);
6956 #endif /* HAVE_XDAMAGE */
6958 return; /* return from main_loop */
6961 /* Reaching here means someone set a signal
6962 * (SIGXXXX, signal_handler), but didn't write any code
6964 * If you don't want to handle a signal, don't set a handler on
6965 * it in the first place. */
6966 if (g_signal_pending) {
6967 ERR("ignoring signal (%d)", g_signal_pending);
6971 g_signal_pending = 0;
6974 #if defined(X11) && defined(HAVE_XDAMAGE)
6975 XDamageDestroy(display, damage);
6976 XFixesDestroyRegion(display, region2);
6977 XFixesDestroyRegion(display, part);
6978 XDestroyRegion(region);
6980 #endif /* X11 && HAVE_XDAMAGE */
6983 static void load_config_file(const char *);
6985 /* reload the config file */
6986 static void reload_config(void)
6988 timed_thread_destroy_registered_threads();
6990 if (info.cpu_usage) {
6991 free(info.cpu_usage);
6992 info.cpu_usage = NULL;
7000 if (info.mpd.title) {
7001 free(info.mpd.title);
7002 info.mpd.title = NULL;
7004 if (info.mpd.artist) {
7005 free(info.mpd.artist);
7006 info.mpd.artist = NULL;
7008 if (info.mpd.album) {
7009 free(info.mpd.album);
7010 info.mpd.album = NULL;
7012 if (info.mpd.random) {
7013 free(info.mpd.random);
7014 info.mpd.random = NULL;
7016 if (info.mpd.repeat) {
7017 free(info.mpd.repeat);
7018 info.mpd.repeat = NULL;
7020 if (info.mpd.track) {
7021 free(info.mpd.track);
7022 info.mpd.track = NULL;
7024 if (info.mpd.name) {
7025 free(info.mpd.name);
7026 info.mpd.name = NULL;
7028 if (info.mpd.file) {
7029 free(info.mpd.file);
7030 info.mpd.file = NULL;
7032 if (info.mpd.status) {
7033 free(info.mpd.status);
7034 info.mpd.status = NULL;
7039 free_moc(&info.moc);
7046 #ifdef TCP_PORT_MONITOR
7047 tcp_portmon_clear();
7050 if (current_config) {
7052 load_config_file(current_config);
7054 /* re-init specials array */
7055 if ((specials = realloc((void *) specials,
7056 sizeof(struct special_t) * max_specials)) == 0) {
7057 ERR("failed to realloc specials array");
7063 // clear the window first
7064 XClearWindow(display, RootWindow(display, screen));
7067 extract_variable_text(global_text);
7073 tmpstring1 = malloc(text_buffer_size);
7074 memset(tmpstring1, 0, text_buffer_size);
7078 tmpstring2 = malloc(text_buffer_size);
7079 memset(tmpstring2, 0, text_buffer_size);
7083 text_buffer = malloc(max_user_text);
7084 memset(text_buffer, 0, max_user_text);
7089 static void clean_up(void)
7091 timed_thread_destroy_registered_threads();
7093 if (info.cpu_usage) {
7094 free(info.cpu_usage);
7095 info.cpu_usage = NULL;
7100 XdbeDeallocateBackBufferName(display, window.back_buffer);
7105 XDestroyWindow(display, window.window);
7106 XClearWindow(display, RootWindow(display, screen));
7111 XClearWindow(display, RootWindow(display, screen));
7116 XFreeGC(display, window.gc);
7120 free_text_objects(&global_root_object, 1);
7139 free(current_config);
7141 #ifdef TCP_PORT_MONITOR
7142 tcp_portmon_clear();
7151 for (i = 0; i < special_count; i++) {
7152 if (specials[i].type == GRAPH) {
7153 free(specials[i].graph);
7160 clear_diskio_stats();
7163 static int string_to_bool(const char *s)
7166 // Assumes an option without a true/false means true
7168 } else if (strcasecmp(s, "yes") == EQUAL) {
7170 } else if (strcasecmp(s, "true") == EQUAL) {
7172 } else if (strcasecmp(s, "1") == EQUAL) {
7179 static enum alignment string_to_alignment(const char *s)
7181 if (strcasecmp(s, "top_left") == EQUAL) {
7183 } else if (strcasecmp(s, "top_right") == EQUAL) {
7185 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7187 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7189 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7190 return BOTTOM_RIGHT;
7191 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7192 return BOTTOM_MIDDLE;
7193 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7195 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7196 return MIDDLE_RIGHT;
7197 } else if (strcasecmp(s, "tl") == EQUAL) {
7199 } else if (strcasecmp(s, "tr") == EQUAL) {
7201 } else if (strcasecmp(s, "tm") == EQUAL) {
7203 } else if (strcasecmp(s, "bl") == EQUAL) {
7205 } else if (strcasecmp(s, "br") == EQUAL) {
7206 return BOTTOM_RIGHT;
7207 } else if (strcasecmp(s, "bm") == EQUAL) {
7208 return BOTTOM_MIDDLE;
7209 } else if (strcasecmp(s, "ml") == EQUAL) {
7211 } else if (strcasecmp(s, "mr") == EQUAL) {
7212 return MIDDLE_RIGHT;
7213 } else if (strcasecmp(s, "none") == EQUAL) {
7220 static void set_default_configurations(void)
7222 fork_to_background = 0;
7223 total_run_times = 0;
7224 info.cpu_avg_samples = 2;
7225 info.net_avg_samples = 2;
7232 strcpy(info.mpd.host, "localhost");
7233 info.mpd.port = 6600;
7234 info.mpd.status = NULL;
7235 info.mpd.artist = NULL;
7236 info.mpd.album = NULL;
7237 info.mpd.title = NULL;
7238 info.mpd.random = NULL;
7239 info.mpd.track = NULL;
7240 info.mpd.name = NULL;
7241 info.mpd.file = NULL;
7244 init_moc(&info.moc);
7247 info.xmms2.artist = NULL;
7248 info.xmms2.album = NULL;
7249 info.xmms2.title = NULL;
7250 info.xmms2.genre = NULL;
7251 info.xmms2.comment = NULL;
7252 info.xmms2.url = NULL;
7253 info.xmms2.status = NULL;
7254 info.xmms2.playlist = NULL;
7256 use_spacer = NO_SPACER;
7258 output_methods = TO_X;
7260 output_methods = TO_STDOUT;
7263 show_graph_scale = 0;
7264 show_graph_range = 0;
7265 default_fg_color = WhitePixel(display, screen);
7266 default_bg_color = BlackPixel(display, screen);
7267 default_out_color = BlackPixel(display, screen);
7268 color0 = default_fg_color;
7269 color1 = default_fg_color;
7270 color2 = default_fg_color;
7271 color3 = default_fg_color;
7272 color4 = default_fg_color;
7273 color5 = default_fg_color;
7274 color6 = default_fg_color;
7275 color7 = default_fg_color;
7276 color8 = default_fg_color;
7277 color9 = default_fg_color;
7278 template[0] = strdup("");
7279 template[1] = strdup("");
7280 template[2] = strdup("");
7281 template[3] = strdup("");
7282 template[4] = strdup("");
7283 template[5] = strdup("");
7284 template[6] = strdup("");
7285 template[7] = strdup("");
7286 template[8] = strdup("");
7287 template[9] = strdup("");
7290 draw_graph_borders = 1;
7292 set_first_font("6x10");
7300 window.type = TYPE_NORMAL;
7302 strcpy(window.class_name, PACKAGE_NAME);
7304 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7306 stippled_borders = 0;
7309 text_alignment = BOTTOM_LEFT;
7310 info.x11.monitor.number = 1;
7311 info.x11.monitor.current = 0;
7314 free(current_mail_spool);
7318 variable_substitute(MAIL_FILE, buf, 256);
7319 if (buf[0] != '\0') {
7320 current_mail_spool = strndup(buf, text_buffer_size);
7325 update_interval = 3.0;
7326 info.music_player_interval = 1.0;
7327 stuff_in_upper_case = 0;
7328 info.users.number = 1;
7330 #ifdef TCP_PORT_MONITOR
7331 /* set default connection limit */
7332 tcp_portmon_set_max_connections(0);
7336 static void load_config_file(const char *f)
7338 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7342 set_default_configurations();
7343 #ifdef CONFIG_OUTPUT
7344 if (!strcmp(f, "==builtin==")) {
7345 #ifdef HAVE_FOPENCOOKIE
7346 fp = fopencookie(NULL, "r", conf_cookie);
7349 #endif /* CONFIG_OUTPUT */
7355 DBGP("reading contents from config file '%s'", f);
7358 char buf[256], *p, *p2, *name, *value;
7361 if (fgets(buf, 256, fp) == NULL) {
7367 /* break at comment */
7368 p2 = strchr(p, '#');
7374 while (*p && isspace((int) *p)) {
7378 continue; /* empty line */
7385 while (*p2 && !isspace((int) *p2)) {
7389 *p2 = '\0'; /* break at name's end */
7396 while (*p && isspace((int) *p)) {
7402 p2 = value + strlen(value);
7403 while (isspace((int) *(p2 - 1))) {
7410 #define CONF2(a) if (strcasecmp(name, a) == 0)
7411 #define CONF(a) else CONF2(a)
7412 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7413 || strcasecmp(name, b) == 0)
7416 CONF2("alignment") {
7417 if (window.type == TYPE_DOCK)
7420 int a = string_to_alignment(value);
7431 CONF("background") {
7432 fork_to_background = string_to_bool(value);
7435 CONF2("background") {
7436 fork_to_background = string_to_bool(value);
7440 CONF("show_graph_scale") {
7441 show_graph_scale = string_to_bool(value);
7443 CONF("show_graph_range") {
7444 show_graph_range = string_to_bool(value);
7446 CONF("border_margin") {
7448 border_margin = strtol(value, 0, 0);
7453 CONF("border_width") {
7455 border_width = strtol(value, 0, 0);
7462 color0 = get_x11_color(value);
7469 color1 = get_x11_color(value);
7476 color2 = get_x11_color(value);
7483 color3 = get_x11_color(value);
7490 color4 = get_x11_color(value);
7497 color5 = get_x11_color(value);
7504 color6 = get_x11_color(value);
7511 color7 = get_x11_color(value);
7518 color8 = get_x11_color(value);
7525 color9 = get_x11_color(value);
7530 #define TEMPLATE_CONF(n) \
7531 CONF("template"#n) { \
7533 free(template[n]); \
7534 template[n] = strdup(value); \
7549 CONF("default_color") {
7551 default_fg_color = get_x11_color(value);
7556 CONF3("default_shade_color", "default_shadecolor") {
7558 default_bg_color = get_x11_color(value);
7563 CONF3("default_outline_color", "default_outlinecolor") {
7565 default_out_color = get_x11_color(value);
7573 info.mail = parse_mail_args(IMAP_TYPE, value);
7580 info.mail = parse_mail_args(POP3_TYPE, value);
7588 strncpy(info.mpd.host, value, 127);
7595 info.mpd.port = strtol(value, 0, 0);
7596 if (info.mpd.port < 1 || info.mpd.port > 0xffff) {
7601 CONF("mpd_password") {
7603 strncpy(info.mpd.password, value, 127);
7609 CONF("music_player_interval") {
7611 info.music_player_interval = strtod(value, 0);
7617 CONF("sensor_device") {
7619 sensor_device = strtol(value, 0, 0);
7625 CONF("cpu_avg_samples") {
7627 cpu_avg_samples = strtol(value, 0, 0);
7628 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
7631 info.cpu_avg_samples = cpu_avg_samples;
7637 CONF("net_avg_samples") {
7639 net_avg_samples = strtol(value, 0, 0);
7640 if (net_avg_samples < 1 || net_avg_samples > 14) {
7643 info.net_avg_samples = net_avg_samples;
7651 CONF("double_buffer") {
7652 use_xdbe = string_to_bool(value);
7656 CONF("override_utf8_locale") {
7657 utf8_mode = string_to_bool(value);
7659 CONF("draw_borders") {
7660 draw_borders = string_to_bool(value);
7662 CONF("draw_graph_borders") {
7663 draw_graph_borders = string_to_bool(value);
7665 CONF("draw_shades") {
7666 draw_shades = string_to_bool(value);
7668 CONF("draw_outline") {
7669 draw_outline = string_to_bool(value);
7672 CONF("out_to_console") {
7673 if(string_to_bool(value)) output_methods |= TO_STDOUT;
7675 CONF("use_spacer") {
7677 if (strcasecmp(value, "left") == EQUAL) {
7678 use_spacer = LEFT_SPACER;
7679 } else if (strcasecmp(value, "right") == EQUAL) {
7680 use_spacer = RIGHT_SPACER;
7681 } else if (strcasecmp(value, "none") == EQUAL) {
7682 use_spacer = NO_SPACER;
7684 use_spacer = string_to_bool(value);
7685 ERR("use_spacer should have an argument of left, right, or"
7686 " none. '%s' seems to be some form of '%s', so"
7687 " defaulting to %s.", value,
7688 use_spacer ? "true" : "false",
7689 use_spacer ? "right" : "none");
7691 use_spacer = RIGHT_SPACER;
7693 use_spacer = NO_SPACER;
7697 ERR("use_spacer should have an argument. Defaulting to right.");
7698 use_spacer = RIGHT_SPACER;
7704 use_xft = string_to_bool(value);
7708 set_first_font(value);
7714 if (value && font_count >= 0) {
7715 fonts[0].font_alpha = atof(value) * 65535.0;
7724 if (string_to_bool(value)) {
7725 ERR("Xft not enabled");
7729 /* xftfont silently ignored when no Xft */
7732 /* xftalpha is silently ignored when no Xft */
7737 set_first_font(value);
7747 gap_x = atoi(value);
7754 gap_y = atoi(value);
7760 CONF("mail_spool") {
7764 variable_substitute(value, buffer, 256);
7766 if (buffer[0] != '\0') {
7767 if (current_mail_spool) {
7768 free(current_mail_spool);
7770 current_mail_spool = strndup(buffer, text_buffer_size);
7777 CONF("minimum_size") {
7779 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
7781 if (sscanf(value, "%d", &minimum_width) != 1) {
7789 CONF("maximum_width") {
7791 if (sscanf(value, "%d", &maximum_width) != 1) {
7799 CONF("no_buffers") {
7800 no_buffers = string_to_bool(value);
7802 CONF("top_cpu_separate") {
7803 cpu_separate = string_to_bool(value);
7805 CONF("short_units") {
7806 short_units = string_to_bool(value);
7808 CONF("pad_percents") {
7809 pad_percents = atoi(value);
7813 CONF("own_window") {
7815 own_window = string_to_bool(value);
7820 CONF("own_window_class") {
7822 memset(window.class_name, 0, sizeof(window.class_name));
7823 strncpy(window.class_name, value,
7824 sizeof(window.class_name) - 1);
7829 CONF("own_window_title") {
7831 memset(window.title, 0, sizeof(window.title));
7832 strncpy(window.title, value, sizeof(window.title) - 1);
7837 CONF("own_window_transparent") {
7839 set_transparent = string_to_bool(value);
7844 CONF("own_window_colour") {
7846 background_colour = get_x11_color(value);
7848 ERR("Invalid colour for own_window_colour (try omitting the "
7849 "'#' for hex colours");
7852 CONF("own_window_hints") {
7854 char *p_hint, *p_save;
7855 char delim[] = ", ";
7857 /* tokenize the value into individual hints */
7858 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
7860 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
7861 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
7862 SET_HINT(window.hints, HINT_UNDECORATED);
7863 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
7864 SET_HINT(window.hints, HINT_BELOW);
7865 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
7866 SET_HINT(window.hints, HINT_ABOVE);
7867 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
7868 SET_HINT(window.hints, HINT_STICKY);
7869 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
7870 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
7871 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
7872 SET_HINT(window.hints, HINT_SKIP_PAGER);
7877 p_hint = strtok_r(NULL, delim, &p_save);
7878 } while (p_hint != NULL);
7884 CONF("own_window_type") {
7886 if (strncmp(value, "normal", 6) == EQUAL) {
7887 window.type = TYPE_NORMAL;
7888 } else if (strncmp(value, "desktop", 7) == EQUAL) {
7889 window.type = TYPE_DESKTOP;
7890 } else if (strncmp(value, "dock", 7) == EQUAL) {
7891 window.type = TYPE_DOCK;
7892 text_alignment = TOP_LEFT;
7893 } else if (strncmp(value, "override", 8) == EQUAL) {
7894 window.type = TYPE_OVERRIDE;
7903 CONF("stippled_borders") {
7905 stippled_borders = strtol(value, 0, 0);
7907 stippled_borders = 4;
7912 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
7916 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
7919 CONF("update_interval") {
7921 update_interval = strtod(value, 0);
7925 if (info.music_player_interval == 0) {
7926 // default to update_interval
7927 info.music_player_interval = update_interval;
7930 CONF("total_run_times") {
7932 total_run_times = strtod(value, 0);
7938 stuff_in_upper_case = string_to_bool(value);
7940 CONF("max_specials") {
7942 max_specials = atoi(value);
7947 CONF("max_user_text") {
7949 max_user_text = atoi(value);
7954 CONF("text_buffer_size") {
7956 text_buffer_size = atoi(value);
7957 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
7958 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
7959 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
7971 global_text = (char *) malloc(1);
7972 global_text[0] = '\0';
7975 unsigned int l = strlen(global_text);
7977 if (fgets(buf, 256, fp) == NULL) {
7980 global_text = (char *) realloc(global_text, l + strlen(buf) + 1);
7981 strcat(global_text, buf);
7983 if (strlen(global_text) > max_user_text) {
7988 if (strlen(global_text) < 1) {
7989 CRIT_ERR("no text supplied in configuration; exiting");
7991 global_text_lines = line + 1;
7994 #ifdef TCP_PORT_MONITOR
7995 CONF("max_port_monitor_connections") {
7997 if (!value || (sscanf(value, "%d", &max) != 1)) {
7998 /* an error. use default, warn and continue. */
7999 tcp_portmon_set_max_connections(0);
8001 } else if (tcp_portmon_set_max_connections(max)) {
8002 /* max is < 0, default has been set*/
8007 CONF("if_up_strictness") {
8009 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8010 ifup_strictness = IFUP_UP;
8011 } else if (strcasecmp(value, "up") == EQUAL) {
8012 ifup_strictness = IFUP_UP;
8013 } else if (strcasecmp(value, "link") == EQUAL) {
8014 ifup_strictness = IFUP_LINK;
8015 } else if (strcasecmp(value, "address") == EQUAL) {
8016 ifup_strictness = IFUP_ADDR;
8018 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8019 ifup_strictness = IFUP_UP;
8023 CONF("temperature_unit") {
8025 ERR("config option 'temperature_unit' needs an argument");
8026 } else if (set_temp_output_unit(value)) {
8027 ERR("temperature_unit: incorrect argument");
8032 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8043 if (info.music_player_interval == 0) {
8044 // default to update_interval
8045 info.music_player_interval = update_interval;
8047 if (!global_text) { // didn't supply any text
8048 CRIT_ERR("missing text block in configuration; exiting");
8052 static void print_help(const char *prog_name) {
8053 printf("Usage: %s [OPTION]...\n"
8054 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8055 "window. Command line options will override configurations defined in config\n"
8057 " -v, --version version\n"
8058 " -q, --quiet quiet mode\n"
8059 " -D, --debug increase debugging output\n"
8060 " -c, --config=FILE config file to load\n"
8061 " -d, --daemonize daemonize, fork to background\n"
8062 " -h, --help help\n"
8064 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8065 " -f, --font=FONT font to use\n"
8067 " -o, --own-window create own window to draw\n"
8070 " -b, --double-buffer double buffer (prevents flickering)\n"
8072 " -w, --window-id=WIN_ID window id to draw\n"
8073 " -x X x position\n"
8074 " -y Y y position\n"
8076 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8077 " -u, --interval=SECS update interval\n"
8078 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8083 /* : means that character before that takes an argument */
8084 static const char *getopt_string = "vVqdDt:u:i:hc:"
8093 #ifdef CONFIG_OUTPUT
8099 static const struct option longopts[] = {
8100 { "help", 0, NULL, 'h' },
8101 { "version", 0, NULL, 'V' },
8102 { "debug", 0, NULL, 'D' },
8103 { "config", 1, NULL, 'c' },
8104 #ifdef CONFIG_OUTPUT
8105 { "print-config", 0, NULL, 'C' },
8107 { "daemonize", 0, NULL, 'd' },
8109 { "alignment", 1, NULL, 'a' },
8110 { "font", 1, NULL, 'f' },
8112 { "own-window", 0, NULL, 'o' },
8115 { "double-buffer", 0, NULL, 'b' },
8117 { "window-id", 1, NULL, 'w' },
8119 { "text", 1, NULL, 't' },
8120 { "interval", 0, NULL, 'u' },
8124 int main(int argc, char **argv)
8130 struct sigaction act, oact;
8132 g_signal_pending = 0;
8133 memset(&info, 0, sizeof(info));
8136 #ifdef TCP_PORT_MONITOR
8137 /* set default connection limit */
8138 tcp_portmon_set_max_connections(0);
8141 /* handle command line parameters that don't change configs */
8143 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8144 || ((s = getenv("LANG")) && *s)) {
8145 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8147 ERR("malloc failed");
8149 for (x = 0; x < strlen(s); x++) {
8150 temp[x] = tolower(s[x]);
8153 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8159 if (!setlocale(LC_CTYPE, "")) {
8160 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8164 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8175 if (current_config) {
8176 free(current_config);
8178 current_config = strndup(optarg, max_user_text);
8181 freopen("/dev/null", "w", stderr);
8184 print_help(argv[0]);
8186 #ifdef CONFIG_OUTPUT
8193 window.window = strtol(optarg, 0, 0);
8202 /* initalize X BEFORE we load config.
8203 * (we need to so that 'screen' is set) */
8207 /* check if specified config file is valid */
8208 if (current_config) {
8210 if (stat(current_config, &sb) ||
8211 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
8212 ERR("invalid configuration file '%s'\n", current_config);
8213 free(current_config);
8218 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8220 if (!current_config) {
8221 /* load default config file */
8225 /* Try to use personal config file first */
8226 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
8227 if (buf[0] && (fp = fopen(buf, "r"))) {
8228 current_config = strndup(buf, max_user_text);
8232 /* Try to use system config file if personal config not readable */
8233 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8234 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
8238 /* No readable config found */
8239 if (!current_config) {
8240 #ifdef CONFIG_OUTPUT
8241 current_config = strdup("==builtin==");
8242 ERR("no readable personal or system-wide config file found,"
8243 " using builtin default");
8245 CRIT_ERR("no readable personal or system-wide config file found");
8246 #endif /* ! CONF_OUTPUT */
8250 load_config_file(current_config);
8252 /* init specials array */
8253 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8254 ERR("failed to create specials array");
8258 if (current_mail_spool == NULL) {
8261 variable_substitute(MAIL_FILE, buf, 256);
8263 if (buf[0] != '\0') {
8264 current_mail_spool = strndup(buf, text_buffer_size);
8269 /* handle other command line arguments */
8271 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8272 || defined(__NetBSD__)
8273 optind = optreset = 1;
8278 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8279 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8280 "kvm_open")) == NULL) {
8281 CRIT_ERR("cannot read kvm");
8286 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8294 fork_to_background = 1;
8297 global_debug_level++;
8301 set_first_font(optarg);
8304 text_alignment = string_to_alignment(optarg);
8323 global_text = strndup(optarg, max_user_text);
8324 convert_escapes(global_text);
8328 update_interval = strtod(optarg, 0);
8329 if (info.music_player_interval == 0) {
8330 // default to update_interval
8331 info.music_player_interval = update_interval;
8336 total_run_times = strtod(optarg, 0);
8340 gap_x = atoi(optarg);
8344 gap_y = atoi(optarg);
8358 /* generate text and get initial size */
8359 extract_variable_text(global_text);
8366 if (fork_to_background) {
8371 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
8378 fprintf(stderr, "\n");
8383 /* parent process */
8384 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
8391 text_buffer = malloc(max_user_text);
8392 memset(text_buffer, 0, max_user_text);
8393 tmpstring1 = malloc(text_buffer_size);
8394 memset(tmpstring1, 0, text_buffer_size);
8395 tmpstring2 = malloc(text_buffer_size);
8396 memset(tmpstring2, 0, text_buffer_size);
8400 update_text_area(); /* to get initial size of the window */
8403 init_window(own_window, text_width + border_margin * 2 + 1,
8404 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8406 #else /* OWN_WINDOW */
8407 init_window(0, text_width + border_margin * 2 + 1,
8408 text_height + border_margin * 2 + 1, set_transparent, 0,
8410 #endif /* OWN_WINDOW */
8413 update_text_area(); /* to position text/window on screen */
8416 if (own_window && !fixed_pos) {
8417 XMoveWindow(display, window.window, window.x, window.y);
8420 set_transparent_background(window.window);
8430 /* Set signal handlers */
8431 act.sa_handler = signal_handler;
8432 sigemptyset(&act.sa_mask);
8435 act.sa_flags |= SA_RESTART;
8438 if ( sigaction(SIGINT, &act, &oact) < 0
8439 || sigaction(SIGUSR1, &act, &oact) < 0
8440 || sigaction(SIGHUP, &act, &oact) < 0
8441 || sigaction(SIGTERM, &act, &oact) < 0) {
8442 ERR("error setting signal handler: %s", strerror(errno));
8447 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8454 static void signal_handler(int sig)
8456 /* signal handler is light as a feather, as it should be.
8457 * we will poll g_signal_pending with each loop of conky
8458 * and do any signal processing there, NOT here. */
8459 g_signal_pending = sig;