2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
28 #include <X11/Xutil.h>
30 #include <sys/types.h>
33 #define CONFIG_FILE "$HOME/.conkyrc"
34 #define MAIL_FILE "$MAIL"
35 #define MAX_IF_BLOCK_DEPTH 5
37 /* #define SIGNAL_BLOCKING */
38 #undef SIGNAL_BLOCKING
55 char name[TEXT_BUFFER_SIZE];
65 static int selected_font = 0;
66 static int font_count = -1;
67 struct font_list *fonts = NULL;
71 #define font_height() use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
72 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
73 #define font_ascent() use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent
74 #define font_descent() use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent
78 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
79 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
80 #define font_descent() fonts[selected_font].font->max_bounds.descent
84 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
87 static void set_font();
90 int addfont(const char *data_in)
92 if (font_count > MAX_FONTS) {
93 CRIT_ERR("you don't need that many fonts, sorry.");
96 if (font_count == 0) {
100 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
104 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
106 CRIT_ERR("realloc in addfont");
108 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
109 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
111 fonts[font_count].font_alpha = 0xffff;
114 CRIT_ERR("Oops...looks like something overflowed in addfont().");
119 void set_first_font(const char *data_in)
121 if (font_count < 0) {
122 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
127 if (strlen(data_in) > 1) {
128 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
130 fonts[0].font_alpha = 0xffff;
138 for (i=0;i<=font_count;i++) {
141 XftFontClose(display, fonts[i].xftfont);
145 XFreeFont(display, fonts[i].font);
152 set_first_font("6x10");
156 static void load_fonts()
159 for (i=0;i<=font_count;i++) {
163 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
164 XftFontClose(display, fonts[i].xftfont);
166 if ((fonts[i].xftfont =
167 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
170 ERR("can't load Xft font '%s'", fonts[i].name);
171 if ((fonts[i].xftfont =
172 XftFontOpenName(display, screen,
173 "courier-12")) != NULL)
176 ERR("can't load Xft font '%s'", "courier-12");
178 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
179 CRIT_ERR("can't load font '%s'", "fixed");
186 /* load normal font */
187 /* if (fonts[i].font != NULL)
188 XFreeFont(display, fonts[i].font);*/
190 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
191 ERR("can't load font '%s'", fonts[i].name);
192 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
193 CRIT_ERR("can't load font '%s'", "fixed");
194 printf("loaded fixed?\n");
202 /* default config file */
203 static char *current_config;
205 /* set to 1 if you want all text to be in uppercase */
206 static unsigned int stuff_in_upper_case;
208 /* Update interval */
209 static double update_interval;
211 /* Run how many times? */
212 static unsigned long total_run_times;
215 static int fork_to_background;
217 static int cpu_avg_samples, net_avg_samples;
221 /* Always on bottom */
222 static int on_bottom;
224 /* Position on the screen */
225 static int text_alignment;
226 static int gap_x, gap_y;
229 static int draw_borders;
230 static int stippled_borders;
232 static int draw_shades, draw_outline;
234 static int border_margin, border_width;
236 static long default_fg_color, default_bg_color, default_out_color;
238 /* create own window or draw stuff to root? */
239 static int set_transparent = 0;
243 static int own_window = 0;
244 static int background_colour = 0;
245 static char wm_class_name[256];
246 /* fixed size/pos is set if wm/user changes them */
247 static int fixed_size = 0, fixed_pos = 0;
250 static int minimum_width, minimum_height;
251 static int maximum_width;
258 /* no buffers in used memory? */
261 /* pad percentages to decimals? */
262 static int pad_percents = 0;
264 #ifdef TCP_PORT_MONITOR
265 tcp_port_monitor_collection_args_t tcp_port_monitor_collection_args;
266 tcp_port_monitor_args_t tcp_port_monitor_args;
269 /* Text that is shown */
270 static char original_text[] =
271 "$nodename - $sysname $kernel on $machine\n"
273 "${color grey}Uptime:$color $uptime\n"
274 "${color grey}Frequency (in MHz):$color $freq\n"
275 "${color grey}Frequency (in GHz):$color $freq_g\n"
276 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
277 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
278 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
279 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
281 "${color grey}File systems:\n"
282 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
283 "${color grey}Networking:\n"
284 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
285 "${color grey}Temperatures:\n"
286 " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
289 "${color grey}SETI@Home Statistics:\n"
290 "${color grey}Seti Unit Number:$color $seti_credit\n"
291 "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
294 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
295 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
297 "${color grey}Name PID CPU% MEM%\n"
298 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
299 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
300 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
301 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
302 "${tail /var/log/Xorg.0.log 3}";
304 static char *text = original_text;
306 static int total_updates;
309 static int blockdepth = 0;
310 static int if_jumped = 0;
311 static int blockstart[MAX_IF_BLOCK_DEPTH];
313 int check_mount(char *s)
315 #if defined(__linux__)
317 FILE *mtab = fopen("/etc/mtab", "r");
319 char buf1[256], buf2[128];
320 while (fgets(buf1, 256, mtab)) {
321 sscanf(buf1, "%*s %128s", buf2);
322 if (!strcmp(s, buf2)) {
329 ERR("Could not open mtab");
332 #elif defined(__FreeBSD__)
333 struct statfs *mntbuf;
336 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
337 for (i = mntsize - 1; i >= 0; i--)
338 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
347 static inline int calc_text_width(const char *s, int l)
353 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
355 XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
361 return XTextWidth(fonts[selected_font].font, s, l);
366 /* formatted text to render on screen, generated in generate_text(),
367 * drawn in draw_stuff() */
369 static char text_buffer[TEXT_BUFFER_SIZE * 4];
371 /* special stuff in text_buffer */
373 #define SPECIAL_CHAR '\x01'
390 static struct special_t {
400 unsigned long first_colour; // for graph gradient
401 unsigned long last_colour;
404 static int special_count;
406 static int special_index; /* used when drawing */
409 #define MAX_GRAPH_DEPTH 256 /* why 256? cause an array of more then 256 doubles seems excessive, and who needs that kind of precision anyway? */
411 static struct special_t *new_special(char *buf, int t)
413 if (special_count >= 128)
414 CRIT_ERR("too many special things in text");
416 buf[0] = SPECIAL_CHAR;
418 specials[special_count].type = t;
419 return &specials[special_count++];
422 typedef struct tailstring_list {
423 char data[TEXT_BUFFER_SIZE];
424 struct tailstring_list *next;
425 struct tailstring_list *first;
428 void addtail(tailstring ** head, char *data_in)
431 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
437 tmp->first = (*head)->first;
439 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
444 void freetail(tailstring * head)
447 while (head != NULL) {
454 void freelasttail(tailstring * head)
456 tailstring * tmp = head;
458 if (tmp->next == head->first) {
465 while(head != NULL && tmp != NULL) {
471 static void new_bar(char *buf, int w, int h, int usage)
473 struct special_t *s = new_special(buf, BAR);
474 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
479 static const char *scan_bar(const char *args, int *w, int *h)
481 *w = 0; /* zero width means all space that is available */
483 /* bar's argument is either height or height,width */
486 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
487 sscanf(args, "%d %n", h, &n);
494 static char *scan_font(const char *args)
496 if (args && sizeof(args) < 127) {
500 ERR("font scan failed, lets hope it doesn't mess stuff up");
506 static void new_font(char *buf, char * args) {
507 struct special_t *s = new_special(buf, FONT);
508 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
509 int tmp = selected_font;
510 selected_font = s->font_added = addfont(args);
518 inline void graph_append(struct special_t *graph, double f)
520 if (!graph->scaled && f > graph->graph_scale) {
521 f = graph->graph_scale;
525 graph->graph_scale = 1;
527 graph->graph[graph->graph_width - 1] = f; /* add new data */
528 for (i = 0; i < graph->graph_width - 1; i++) { /* shift all the data by 1 */
529 graph->graph[i] = graph->graph[i + 1];
530 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
531 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
536 short colour_depth = 0;
537 void set_up_gradient();
539 /* precalculated: 31/255, and 63/255 */
540 #define CONST_8_TO_5_BITS 0.12156862745098
541 #define CONST_8_TO_6_BITS 0.247058823529412
543 /* adjust color values depending on color depth*/
544 static unsigned int adjust_colors(unsigned int color)
547 if (colour_depth == 0) {
550 if (colour_depth == 16) {
551 r = (color & 0xff0000) >> 16;
552 g = (color & 0xff00) >> 8;
554 color = (int)(r * CONST_8_TO_5_BITS) << 11;
555 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
556 color |= (int)(b * CONST_8_TO_5_BITS);
561 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
563 struct special_t *s = new_special(buf, GRAPH);
565 if (s->graph == NULL) {
566 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
567 s->graph_width = s->width - 3; // subtract 3 for the box
569 s->graph_width = MAX_GRAPH_DEPTH - 3;
571 s->graph = malloc(s->graph_width * sizeof(double));
572 memset(s->graph, 0, s->graph_width * sizeof(double));
573 s->graph_scale = 100;
576 s->first_colour = adjust_colors(first_colour);
577 s->last_colour = adjust_colors(second_colour);
584 s->graph_width = s->width - 3; // subtract 3 for rectangle around
589 s->graph_scale = scale;
596 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
598 *w = 0; /* zero width means all space that is available */
602 /* graph's argument is either height or height,width */
604 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
605 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
607 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
609 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
612 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
616 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
619 if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
623 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
626 if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
630 if (sscanf(args, "%d,%d", h, w) < 2) {
633 sscanf(args, "%*s %d,%d", h, w);
639 static inline void new_hr(char *buf, int a)
641 new_special(buf, HORIZONTAL_LINE)->height = a;
644 static inline void new_stippled_hr(char *buf, int a, int b)
646 struct special_t *s = new_special(buf, STIPPLED_HR);
651 static inline void new_fg(char *buf, long c)
653 new_special(buf, FG)->arg = c;
656 static inline void new_bg(char *buf, long c)
658 new_special(buf, BG)->arg = c;
661 static inline void new_outline(char *buf, long c)
663 new_special(buf, OUTLINE)->arg = c;
666 static inline void new_offset(char *buf, long c)
668 new_special(buf, OFFSET)->arg = c;
671 static inline void new_voffset(char *buf, long c)
673 new_special(buf, VOFFSET)->arg = c;
676 static inline void new_alignr(char *buf, long c)
678 new_special(buf, ALIGNR)->arg = c;
681 static inline void new_alignc(char *buf, long c)
683 new_special(buf, ALIGNC)->arg = c;
686 /* quite boring functions */
688 static inline void for_each_line(char *b, void (*f) (char *))
692 for (ps = b, pe = b; *pe; pe++) {
705 static void convert_escapes(char *buf)
707 char *p = buf, *s = buf;
723 /* converts from bytes to human readable format (k, M, G, T) */
724 static void human_readable(long long a, char *buf, int size)
726 // Strange conditional due to possible overflows
727 if(a / 1024 / 1024 / 1024.0 > 1024.0){
728 snprintf(buf, size, "%.2fT", (a / 1024 / 1024 / 1024) / 1024.0);
730 else if (a >= 1024 * 1024 * 1024) {
731 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
733 else if (a >= 1024 * 1024) {
734 double m = (a / 1024) / 1024.0;
736 snprintf(buf, size, "%.0fM", m);
738 snprintf(buf, size, "%.1fM", m);
739 } else if (a >= 1024)
740 snprintf(buf, size, "%Ldk", a / (long long) 1024);
742 snprintf(buf, size, "%Ld", a);
747 enum text_object_type {
795 #if defined(__linux__)
801 OBJ_i8k_left_fan_status,
802 OBJ_i8k_right_fan_status,
803 OBJ_i8k_left_fan_rpm,
804 OBJ_i8k_right_fan_rpm,
806 OBJ_i8k_buttons_status,
807 #endif /* __linux__ */
834 OBJ_ml_upload_counter,
835 OBJ_ml_download_counter,
836 OBJ_ml_nshared_files,
837 OBJ_ml_shared_counter,
838 OBJ_ml_tcp_upload_rate,
839 OBJ_ml_tcp_download_rate,
840 OBJ_ml_udp_upload_rate,
841 OBJ_ml_udp_download_rate,
842 OBJ_ml_ndownloaded_files,
843 OBJ_ml_ndownloading_files,
846 OBJ_running_processes,
855 OBJ_temp1, /* i2c is used instead in these */
868 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
870 OBJ_apm_battery_time,
871 OBJ_apm_battery_life,
872 #endif /* __FreeBSD__ */
895 #ifdef TCP_PORT_MONITOR
903 unsigned int c, d, e;
906 char *s; /* some string */
907 int i; /* some integer */
908 long l; /* some other integer */
909 struct net_stat *net;
911 unsigned char loadavg[3];
912 //unsigned int diskio;
913 unsigned int cpu_index;
959 #ifdef TCP_PORT_MONITOR
961 in_port_t port_range_begin; /* starting port to monitor */
962 in_port_t port_range_end; /* ending port to monitor */
963 int item; /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
964 int connection_index; /* 0 to n-1 connections. */
970 static unsigned int text_object_count;
971 static struct text_object *text_objects;
973 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
975 void *threaded_exec( struct text_object *obj ) {
976 char *p2 = obj->data.execi.buffer;
977 FILE *fp = popen(obj->data.execi.cmd,"r");
978 pthread_mutex_lock( &mutex1 );
979 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
982 if (n2 && p2[n2 - 1] == '\n')
990 pthread_mutex_unlock( &mutex1 );
994 /* new_text_object() allocates a new zeroed text_object */
995 static struct text_object *new_text_object()
998 text_objects = (struct text_object *) realloc(text_objects,
1002 memset(&text_objects[text_object_count - 1], 0,
1003 sizeof(struct text_object));
1005 return &text_objects[text_object_count - 1];
1011 if (mlconfig.mldonkey_hostname) {
1012 free(mlconfig.mldonkey_hostname);
1013 mlconfig.mldonkey_hostname = NULL;
1018 static void free_text_objects()
1021 for (i = 0; i < text_object_count; i++) {
1022 switch (text_objects[i].type) {
1024 close(text_objects[i].data.i);
1027 close(text_objects[i].data.i);
1030 close(text_objects[i].data.i2c.fd);
1033 free(text_objects[i].data.s);
1036 case OBJ_if_existing:
1037 case OBJ_if_mounted:
1038 case OBJ_if_running:
1039 free(text_objects[i].data.ifblock.s);
1042 free(text_objects[i].data.tail.logfile);
1043 free(text_objects[i].data.tail.buffer);
1047 free(text_objects[i].data.s);
1050 free(text_objects[i].data.s);
1053 free(text_objects[i].data.s);
1056 free(text_objects[i].data.s);
1058 /* case OBJ_execibar:
1059 free(text_objects[i].data.s);
1061 case OBJ_execigraph:
1062 free(text_objects[i].data.s);
1066 case OBJ_mpd_artist:
1068 case OBJ_mpd_random:
1069 case OBJ_mpd_repeat:
1071 case OBJ_mpd_status:
1076 free(text_objects[i].data.s);
1080 free(text_objects[i].data.execi.cmd);
1081 free(text_objects[i].data.execi.buffer);
1084 free(text_objects[i].data.execi.cmd);
1085 free(text_objects[i].data.execi.buffer);
1088 if (info.first_process) {
1089 free_all_processes();
1090 info.first_process = NULL;
1094 if (info.first_process) {
1095 free_all_processes();
1096 info.first_process = NULL;
1103 #endif /* MLDONKEY */
1105 text_objects = NULL;
1106 text_object_count = 0;
1109 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1114 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1115 *a = mixer_init(buf1);
1116 (void) scan_bar(arg + n, w, h);
1119 (void) scan_bar(arg, w, h);
1123 /* construct_text_object() creates a new text_object */
1124 static void construct_text_object(const char *s, const char *arg)
1126 struct text_object *obj = new_text_object();
1128 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
1129 #define END ; } } else
1133 obj->type = OBJ_color;
1134 obj->data.l = get_x11_color(s);
1137 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
1138 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
1139 END OBJ(acpiacadapter, 0)
1142 END OBJ(freq_dyn, 0);
1143 END OBJ(freq_dyn_g, 0);
1144 END OBJ(acpifan, 0);
1145 END OBJ(battery, 0);
1148 sscanf(arg, "%63s", bat);
1150 strcpy(bat, "BAT0");
1151 obj->data.s = strdup(bat);
1152 #if defined(__linux__)
1153 END OBJ(i8k_version, INFO_I8K)
1154 END OBJ(i8k_bios, INFO_I8K)
1155 END OBJ(i8k_serial, INFO_I8K)
1156 END OBJ(i8k_cpu_temp, INFO_I8K)
1157 END OBJ(i8k_cpu_tempf, INFO_I8K)
1158 END OBJ(i8k_left_fan_status, INFO_I8K)
1159 END OBJ(i8k_right_fan_status, INFO_I8K)
1160 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1161 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1162 END OBJ(i8k_ac_status, INFO_I8K)
1163 END OBJ(i8k_buttons_status, INFO_I8K)
1164 #endif /* __linux__ */
1165 END OBJ(buffers, INFO_BUFFERS)
1166 END OBJ(cached, INFO_BUFFERS)
1167 END OBJ(cpu, INFO_CPU)
1169 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1170 obj->data.cpu_index = atoi(&arg[3]);
1172 } else {obj->data.cpu_index = 0; }
1174 obj->data.cpu_index = 0;
1176 END OBJ(cpubar, INFO_CPU)
1178 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1179 obj->data.cpu_index = atoi(&arg[3]);
1182 else {obj->data.cpu_index = 0;}
1183 (void) scan_bar(arg, &obj->a, &obj->b);
1185 (void) scan_bar(arg, &obj->a, &obj->b);
1186 obj->data.cpu_index = 0;
1188 END OBJ(cpugraph, INFO_CPU)
1190 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1191 obj->data.cpu_index = atoi(&arg[3]);
1194 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1196 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1197 obj->data.cpu_index = 0;
1199 END OBJ(diskio, INFO_DISKIO)
1200 END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1203 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1207 obj->data.s = scan_font(arg);
1209 OBJ(downspeed, INFO_NET)
1211 obj->data.net = get_net_stat(arg);
1214 CRIT_ERR("downspeed needs argument");
1216 END OBJ(downspeedf, INFO_NET)
1218 obj->data.net = get_net_stat(arg);
1221 CRIT_ERR("downspeedf needs argument");
1223 END OBJ(downspeedgraph, INFO_NET)
1224 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1226 sscanf(arg, "%63s %*i,%*i %*i", buf);
1227 obj->data.net = get_net_stat(buf);
1228 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1229 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
1238 text_objects[blockstart[blockdepth - 1] -
1239 1].data.ifblock.pos = text_object_count;
1240 blockstart[blockdepth - 1] = text_object_count;
1241 obj->data.ifblock.pos = text_object_count + 2;
1243 ERR("$else: no matching $if_*");
1248 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
1251 ERR("$endif: no matching $if_*");
1255 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
1256 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
1257 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
1258 END OBJ(execibar, 0) unsigned int n;
1259 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1261 ERR("${execibar <interval> command}");
1262 obj->type = OBJ_text;
1263 snprintf(buf, 256, "${%s}", s);
1264 obj->data.s = strdup(buf);
1266 obj->data.execi.cmd = strdup(arg + n);
1268 END OBJ(execigraph, 0) unsigned int n;
1269 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1271 ERR("${execigraph <interval> command}");
1272 obj->type = OBJ_text;
1273 snprintf(buf, 256, "${%s}", s);
1274 obj->data.s = strdup(buf);
1276 obj->data.execi.cmd = strdup(arg + n);
1278 END OBJ(execi, 0) unsigned int n;
1281 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1283 ERR("${execi <interval> command}");
1284 obj->type = OBJ_text;
1285 snprintf(buf, 256, "${%s}", s);
1286 obj->data.s = strdup(buf);
1288 obj->data.execi.cmd = strdup(arg + n);
1289 obj->data.execi.buffer =
1290 (char *) calloc(1, TEXT_BUFFER_SIZE);
1292 END OBJ(texeci, 0) unsigned int n;
1295 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1297 ERR("${texeci <interval> command}");
1298 obj->type = OBJ_text;
1299 snprintf(buf, 256, "${%s}", s);
1300 obj->data.s = strdup(buf);
1302 obj->data.execi.cmd = strdup(arg + n);
1303 obj->data.execi.buffer =
1304 (char *) calloc(1, TEXT_BUFFER_SIZE);
1306 END OBJ(pre_exec, 0) obj->type = OBJ_text;
1308 FILE *fp = popen(arg, "r");
1312 n = fread(buf, 1, 2048, fp);
1315 if (n && buf[n - 1] == '\n')
1320 obj->data.s = strdup(buf);
1322 obj->data.s = strdup("");
1325 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1326 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1328 while (isspace(*arg))
1334 obj->data.fsbar.fs = prepare_fs_stat(arg);
1335 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1338 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1342 obj->data.fsbar.fs = prepare_fs_stat(arg);
1343 END OBJ(fs_free, INFO_FS) if (!arg)
1345 obj->data.fs = prepare_fs_stat(arg);
1346 END OBJ(fs_used_perc, INFO_FS) if (!arg)
1348 obj->data.fs = prepare_fs_stat(arg);
1349 END OBJ(fs_free_perc, INFO_FS) if (!arg)
1351 obj->data.fs = prepare_fs_stat(arg);
1352 END OBJ(fs_size, INFO_FS) if (!arg)
1354 obj->data.fs = prepare_fs_stat(arg);
1355 END OBJ(fs_used, INFO_FS) if (!arg)
1357 obj->data.fs = prepare_fs_stat(arg);
1358 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
1359 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
1360 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
1361 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
1365 ERR("i2c needs arguments");
1366 obj->type = OBJ_text;
1367 //obj->data.s = strdup("${i2c}");
1371 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1372 /* if scanf couldn't read three values, read type and num and use
1374 sscanf(arg, "%63s %d", buf2, &n);
1376 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1377 obj->data.i2c.devtype);
1378 strncpy(obj->data.i2c.type, buf2, 63);
1381 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1382 obj->data.i2c.devtype);
1383 strncpy(obj->data.i2c.type, buf2, 63);
1386 END OBJ(top, INFO_TOP)
1390 ERR("top needs arguments");
1391 obj->type = OBJ_text;
1392 //obj->data.s = strdup("${top}");
1395 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1396 if (strcmp(buf, "name") == 0) {
1397 obj->data.top.type = TOP_NAME;
1398 } else if (strcmp(buf, "cpu") == 0) {
1399 obj->data.top.type = TOP_CPU;
1400 } else if (strcmp(buf, "pid") == 0) {
1401 obj->data.top.type = TOP_PID;
1402 } else if (strcmp(buf, "mem") == 0) {
1403 obj->data.top.type = TOP_MEM;
1405 ERR("invalid arg for top");
1408 if (n < 1 || n > 10) {
1409 CRIT_ERR("invalid arg for top");
1412 obj->data.top.num = n - 1;
1416 ERR("invalid args given for top");
1419 END OBJ(top_mem, INFO_TOP)
1423 ERR("top_mem needs arguments");
1424 obj->type = OBJ_text;
1425 obj->data.s = strdup("${top_mem}");
1428 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1429 if (strcmp(buf, "name") == 0) {
1430 obj->data.top.type = TOP_NAME;
1431 } else if (strcmp(buf, "cpu") == 0) {
1432 obj->data.top.type = TOP_CPU;
1433 } else if (strcmp(buf, "pid") == 0) {
1434 obj->data.top.type = TOP_PID;
1435 } else if (strcmp(buf, "mem") == 0) {
1436 obj->data.top.type = TOP_MEM;
1438 ERR("invalid arg for top");
1441 if (n < 1 || n > 10) {
1442 CRIT_ERR("invalid arg for top");
1445 obj->data.top.num = n - 1;
1449 ERR("invalid args given for top");
1452 END OBJ(addr, INFO_NET)
1454 obj->data.net = get_net_stat(arg);
1457 CRIT_ERR("addr needs argument");
1459 END OBJ(linkstatus, INFO_WIFI)
1461 obj->data.net = get_net_stat(arg);
1464 CRIT_ERR("linkstatus needs argument");
1470 ERR("tail needs arguments");
1471 obj->type = OBJ_text;
1472 obj->data.s = strdup("${tail}");
1475 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1476 if (n1 < 1 || n1 > 30) {
1477 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
1481 fp = fopen(buf, "r");
1483 obj->data.tail.logfile =
1484 malloc(TEXT_BUFFER_SIZE);
1485 strcpy(obj->data.tail.logfile, buf);
1486 obj->data.tail.wantedlines = n1 - 1;
1487 obj->data.tail.interval =
1488 update_interval * 2;
1492 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1495 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1496 if (n1 < 1 || n1 > 30) {
1498 ("invalid arg for tail, number of lines must be between 1 and 30");
1500 } else if (n2 < 1 || n2 < update_interval) {
1502 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1506 fp = fopen(buf, "r");
1508 obj->data.tail.logfile =
1509 malloc(TEXT_BUFFER_SIZE);
1510 strcpy(obj->data.tail.logfile, buf);
1511 obj->data.tail.wantedlines = n1 - 1;
1512 obj->data.tail.interval = n2;
1516 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1522 ERR("invalid args given for tail");
1525 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1530 ERR("head needs arguments");
1531 obj->type = OBJ_text;
1532 obj->data.s = strdup("${head}");
1535 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1536 if (n1 < 1 || n1 > 30) {
1537 CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
1541 fp = fopen(buf, "r");
1543 obj->data.tail.logfile =
1544 malloc(TEXT_BUFFER_SIZE);
1545 strcpy(obj->data.tail.logfile, buf);
1546 obj->data.tail.wantedlines = n1 - 1;
1547 obj->data.tail.interval =
1548 update_interval * 2;
1552 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1555 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1556 if (n1 < 1 || n1 > 30) {
1558 ("invalid arg for head, number of lines must be between 1 and 30");
1560 } else if (n2 < 1 || n2 < update_interval) {
1562 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
1566 fp = fopen(buf, "r");
1568 obj->data.tail.logfile =
1569 malloc(TEXT_BUFFER_SIZE);
1570 strcpy(obj->data.tail.logfile, buf);
1571 obj->data.tail.wantedlines = n1 - 1;
1572 obj->data.tail.interval = n2;
1576 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1582 ERR("invalid args given for head");
1585 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1586 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
1588 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1589 if (r >= 3 && (c < 1 || c > 3))
1591 if (r >= 2 && (b < 1 || b > 3))
1593 if (r >= 1 && (a < 1 || a > 3))
1596 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1597 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1598 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1599 END OBJ(if_existing, 0)
1600 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1601 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1604 ERR("if_existing needs an argument");
1605 obj->data.ifblock.s = 0;
1607 obj->data.ifblock.s = strdup(arg);
1608 blockstart[blockdepth] = text_object_count;
1609 obj->data.ifblock.pos = text_object_count + 2;
1611 END OBJ(if_mounted, 0)
1612 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1613 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1616 ERR("if_mounted needs an argument");
1617 obj->data.ifblock.s = 0;
1619 obj->data.ifblock.s = strdup(arg);
1620 blockstart[blockdepth] = text_object_count;
1621 obj->data.ifblock.pos = text_object_count + 2;
1623 END OBJ(if_running, 0)
1624 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1625 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1629 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1630 obj->data.ifblock.s = strdup(buf);
1632 ERR("if_running needs an argument");
1633 obj->data.ifblock.s = 0;
1635 blockstart[blockdepth] = text_object_count;
1636 obj->data.ifblock.pos = text_object_count + 2;
1640 END OBJ(mails, INFO_MAIL)
1641 END OBJ(mem, INFO_MEM)
1642 END OBJ(memmax, INFO_MEM)
1643 END OBJ(memperc, INFO_MEM)
1644 END OBJ(membar, INFO_MEM)
1645 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1646 END OBJ(memgraph, INFO_MEM)
1647 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1648 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
1649 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
1650 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
1651 END OBJ(mixerbar, INFO_MIXER)
1652 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1653 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1654 END OBJ(mixerlbar, INFO_MIXER)
1655 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1656 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1657 END OBJ(mixerrbar, INFO_MIXER)
1658 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1659 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1662 OBJ(ml_upload_counter, INFO_MLDONKEY)
1663 END OBJ(ml_download_counter, INFO_MLDONKEY)
1664 END OBJ(ml_nshared_files, INFO_MLDONKEY)
1665 END OBJ(ml_shared_counter, INFO_MLDONKEY)
1666 END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1667 END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1668 END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1669 END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1670 END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1671 END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
1673 OBJ(new_mails, INFO_MAIL)
1674 END OBJ(nodename, 0)
1675 END OBJ(processes, INFO_PROCS)
1676 END OBJ(running_processes, INFO_RUN_PROCS)
1677 END OBJ(shadecolor, 0)
1679 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1681 END OBJ(outlinecolor, 0)
1683 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1685 END OBJ(stippled_hr, 0)
1687 int a = stippled_borders, b = 1;
1689 if (sscanf(arg, "%d %d", &a, &b) != 2)
1690 sscanf(arg, "%d", &b);
1694 obj->data.pair.a = a;
1695 obj->data.pair.b = b;
1697 END OBJ(swap, INFO_MEM)
1698 END OBJ(swapmax, INFO_MEM)
1699 END OBJ(swapperc, INFO_MEM)
1700 END OBJ(swapbar, INFO_MEM)
1701 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1702 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1704 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1705 obj->data.i2c.devtype);
1706 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1708 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1709 obj->data.i2c.devtype);
1710 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1711 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1712 END OBJ(totaldown, INFO_NET)
1714 obj->data.net = get_net_stat(arg);
1717 CRIT_ERR("totaldown needs argument");
1719 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1721 obj->data.net = get_net_stat(arg);
1724 CRIT_ERR("totalup needs argument");
1727 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
1728 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
1729 END OBJ(upspeed, INFO_NET)
1731 obj->data.net = get_net_stat(arg);
1734 CRIT_ERR("upspeed needs argument");
1736 END OBJ(upspeedf, INFO_NET)
1738 obj->data.net = get_net_stat(arg);
1741 CRIT_ERR("upspeedf needs argument");
1744 END OBJ(upspeedgraph, INFO_NET)
1745 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1747 sscanf(arg, "%63s %*i,%*i %*i", buf);
1748 obj->data.net = get_net_stat(buf);
1749 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1750 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
1755 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1756 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
1757 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
1758 OBJ(apm_adapter, 0) END
1759 OBJ(apm_battery_life, 0) END
1760 OBJ(apm_battery_time, 0) END
1761 #endif /* __FreeBSD__ */
1763 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1764 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1765 END OBJ(seti_credit, INFO_SETI) END
1768 OBJ(mpd_artist, INFO_MPD)
1769 END OBJ(mpd_title, INFO_MPD)
1770 END OBJ(mpd_random, INFO_MPD)
1771 END OBJ(mpd_repeat, INFO_MPD)
1772 END OBJ(mpd_elapsed, INFO_MPD)
1773 END OBJ(mpd_length, INFO_MPD)
1774 END OBJ(mpd_track, INFO_MPD)
1775 END OBJ(mpd_percent, INFO_MPD)
1776 END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol,
1777 INFO_MPD) END OBJ(mpd_bitrate,
1779 END OBJ(mpd_status, INFO_MPD)
1780 END OBJ(mpd_bar, INFO_MPD)
1781 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1784 #ifdef TCP_PORT_MONITOR
1785 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
1786 int argc, port_begin, port_end, item, connection_index;
1788 memset(itembuf,0,sizeof(itembuf));
1790 /* massive argument checking */
1792 CRIT_ERR("tcp_portmon: needs arguments");
1794 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
1795 if ( (argc != 3) && (argc != 4) )
1797 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
1799 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
1801 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
1803 if ( port_begin > port_end )
1805 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
1807 if ( strncmp(itembuf,"count",31) == 0 )
1809 else if ( strncmp(itembuf,"rip",31) == 0 )
1811 else if ( strncmp(itembuf,"rhost",31) == 0 )
1813 else if ( strncmp(itembuf,"rport",31) == 0 )
1815 else if ( strncmp(itembuf,"lip",31) == 0 )
1817 else if ( strncmp(itembuf,"lhost",31) == 0 )
1819 else if ( strncmp(itembuf,"lport",31) == 0 )
1821 else if ( strncmp(itembuf,"lservice",31) == 0 )
1825 CRIT_ERR("tcp_portmon: invalid item specified");
1827 if ( (argc==3) && (item!=COUNT) )
1829 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
1831 if ( (argc==4) && (connection_index<0) )
1833 CRIT_ERR("tcp_portmon: connection index must be non-negative");
1835 /* ok, args looks good. save the text object data */
1836 obj->data.tcp_port_monitor.port_range_begin = (in_addr_t)port_begin;
1837 obj->data.tcp_port_monitor.port_range_end = (in_addr_t)port_end;
1838 obj->data.tcp_port_monitor.item = item;
1839 obj->data.tcp_port_monitor.connection_index = connection_index;
1841 /* if the port monitor collection hasn't been created, we must create it */
1842 if ( !info.p_tcp_port_monitor_collection )
1844 info.p_tcp_port_monitor_collection =
1845 create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
1846 if ( !info.p_tcp_port_monitor_collection )
1848 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
1852 /* if a port monitor for this port does not exist, create one and add it to the collection */
1853 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
1855 tcp_port_monitor_t * p_monitor =
1856 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
1859 CRIT_ERR("tcp_portmon: unable to create port monitor");
1861 /* add the newly created monitor to the collection */
1862 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
1865 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
1872 ERR("unknown variable %s", s);
1873 obj->type = OBJ_text;
1874 snprintf(buf, 256, "${%s}", s);
1875 obj->data.s = strdup(buf);
1880 /* append_text() appends text to last text_object if it's text, if it isn't
1881 * it creates a new text_object */
1882 static void append_text(const char *s)
1884 struct text_object *obj;
1886 if (s == NULL || *s == '\0')
1889 obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1891 /* create a new text object? */
1892 if (!obj || obj->type != OBJ_text) {
1893 obj = new_text_object();
1894 obj->type = OBJ_text;
1895 obj->data.s = strdup(s);
1898 obj->data.s = (char *) realloc(obj->data.s,
1899 strlen(obj->data.s) +
1901 strcat(obj->data.s, s);
1905 static void extract_variable_text(const char *p)
1909 free_text_objects();
1924 /* variable is either $foo or ${foo} */
1928 while (*p && *p != '}')
1934 while (*p && (isalnum((int) *p)
1939 /* copy variable to buffer */
1940 len = (p - s > 255) ? 255 : (p - s);
1941 strncpy(buf, s, len);
1950 /* if variable wasn't found from environment, use some special */
1956 if (strchr(buf, ' ')) {
1957 arg = strchr(buf, ' ');
1960 while (isspace((int) *arg))
1966 /* lowercase variable name */
1973 construct_text_object(buf, arg);
1984 ERR("one or more $endif's are missing");
1988 double current_update_time, last_update_time;
1990 static void generate_text()
1993 struct information *cur = &info;
2000 current_update_time = get_time();
2006 n = TEXT_BUFFER_SIZE * 4 - 2;
2009 for (i = 0; i < text_object_count; i++) {
2010 struct text_object *obj = &text_objects[i];
2012 #define OBJ(a) break; case OBJ_##a:
2014 switch (obj->type) {
2017 ERR("not implemented obj type %d",
2021 /* does anyone have decimals in acpi temperature? */
2023 snprintf(p, n, "%d", (int)
2024 get_acpi_temperature(obj->
2028 snprintf(p, 5, "%d ", (int)
2029 get_acpi_temperature(obj->
2034 /* does anyone have decimals in acpi temperature? */
2036 snprintf(p, n, "%d", (int)
2037 ((get_acpi_temperature(obj->
2039 i)+ 40) * 9.0 / 5 - 40));
2041 snprintf(p, 5, "%d ", (int)
2042 ((get_acpi_temperature(obj->
2044 i)+ 40) * 9.0 / 5 - 40));
2047 get_freq(p, n, "%.0f", 1); /* pk */
2050 get_freq(p, n, "%'.2f", 1000); /* pk */
2053 get_freq_dynamic(p, n, "%.0f", 1 ); /* pk */
2056 get_freq_dynamic(p, n, "%'.2f", 1000); /* pk */
2059 get_adt746x_cpu(p, n); /* pk */
2062 get_adt746x_fan(p, n); /* pk */
2065 get_acpi_fan(p, n); /* pk */
2067 OBJ(acpiacadapter) {
2068 get_acpi_ac_adapter(p, n); /* pk */
2071 get_battery_stuff(p, n, obj->data.s);
2074 human_readable(cur->buffers * 1024, p,
2078 human_readable(cur->cached * 1024, p, 255);
2081 if (obj->data.cpu_index > info.cpu_count) {
2082 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
2083 CRIT_ERR("attempting to use more CPUs then you have!");
2086 snprintf(p, n, "%*d", pad_percents,
2087 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2090 snprintf(p, 4, "%*d ",
2092 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2098 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
2101 new_graph(p, obj->a,
2102 obj->b, obj->c, obj->d,
2103 (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2107 new_fg(p, obj->data.l);
2109 #if defined(__linux__)
2111 snprintf(p, n, "%s", i8k.version);
2114 snprintf(p, n, "%s", i8k.bios);
2117 snprintf(p, n, "%s", i8k.serial);
2120 snprintf(p, n, "%s", i8k.cpu_temp);
2122 OBJ(i8k_cpu_tempf) {
2124 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
2125 snprintf(p, n, "%.1f", cpu_temp*(9.0/5.0)+32.0);
2127 OBJ(i8k_left_fan_status) {
2128 int left_fan_status;
2129 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
2130 if(left_fan_status == 0) {
2131 snprintf(p, n,"off");
2132 } if(left_fan_status == 1) {
2133 snprintf(p, n, "low");
2134 } if(left_fan_status == 2) {
2135 snprintf(p, n, "high");
2139 OBJ(i8k_right_fan_status) {
2140 int right_fan_status;
2141 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
2142 if(right_fan_status == 0) {
2143 snprintf(p, n,"off");
2144 } if(right_fan_status == 1) {
2145 snprintf(p, n, "low");
2146 } if(right_fan_status == 2) {
2147 snprintf(p, n, "high");
2150 OBJ(i8k_left_fan_rpm) {
2151 snprintf(p, n, "%s", i8k.left_fan_rpm);
2153 OBJ(i8k_right_fan_rpm) {
2154 snprintf(p, n, "%s", i8k.right_fan_rpm);
2156 OBJ(i8k_ac_status) {
2158 sscanf(i8k.ac_status, "%d", &ac_status);
2159 if(ac_status == -1) {
2160 snprintf(p, n,"disabled (read i8k docs)");
2161 } if(ac_status == 0) {
2162 snprintf(p, n, "off");
2163 } if(ac_status == 1) {
2164 snprintf(p, n, "on");
2167 OBJ(i8k_buttons_status) {
2168 snprintf(p, n, "%s", i8k.buttons_status);
2171 #endif /* __linux__ */
2175 new_font(p, obj->data.s);
2180 if (diskio_value > 1024*1024) {
2181 snprintf(p, n, "%.1fG",
2182 (double)diskio_value/1024/1024);
2183 } else if (diskio_value > 1024) {
2184 snprintf(p, n, "%.1fM",
2185 (double)diskio_value/1024);
2186 } else if (diskio_value > 0) {
2187 snprintf(p, n, "%dK", diskio_value);
2189 snprintf(p, n, "%d", diskio_value);
2192 if (diskio_value > 1024*1024) {
2193 snprintf(p, 6, "%.1fG ",
2194 (double)diskio_value/1024/1024);
2195 } else if (diskio_value > 1024) {
2196 snprintf(p, 6, "%.1fM ",
2197 (double)diskio_value/1024);
2198 } else if (diskio_value > 0) {
2199 snprintf(p, 6, "%dK ", diskio_value);
2201 snprintf(p, 6, "%d ", diskio_value);
2206 new_graph(p, obj->a,
2207 obj->b, obj->c, obj->d,
2208 diskio_value, obj->e, 1);
2213 snprintf(p, n, "%d",
2214 (int) (obj->data.net->
2218 snprintf(p, 6, "%d ",
2219 (int) (obj->data.net->
2225 snprintf(p, n, "%.1f",
2227 recv_speed / 1024.0);
2229 snprintf(p, 8, "%.1f ",
2231 recv_speed / 1024.0);
2233 OBJ(downspeedgraph) {
2234 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
2235 obj->data.net->recv_speed = 0.01;
2236 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2237 (obj->data.net->recv_speed /
2238 1024.0), obj->e, 1);
2244 i = obj->data.ifblock.pos - 2;
2254 snprintf(p, n, "%u.%u.%u.%u",
2255 obj->data.net->addr.
2257 obj->data.net->addr.
2259 obj->data.net->addr.
2261 obj->data.net->addr.
2266 snprintf(p, n, "%d",
2267 obj->data.net->linkstatus);
2272 FILE *fp = popen(obj->data.s, "r");
2273 int n2 = fread(p, 1, n, fp);
2277 if (n2 && p[n2 - 1] == '\n')
2288 FILE *fp = popen(obj->data.s, "r");
2289 int n2 = fread(p, 1, n, fp);
2293 if (n2 && p[n2 - 1] == '\n')
2302 if (sscanf(p, "%lf", &barnum) == 0) {
2303 ERR("reading execbar value failed (perhaps it's not the correct format?)");
2305 if (barnum > 100 || barnum < 0) {
2306 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
2308 barnum = barnum / 100.0;
2309 new_bar(p, 0, 4, (int) (barnum * 255.0));
2315 FILE *fp = popen(obj->data.s, "r");
2316 int n2 = fread(p, 1, n, fp);
2320 if (n2 && p[n2 - 1] == '\n')
2329 if (sscanf(p, "%lf", &barnum) == 0) {
2330 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
2332 if (barnum > 100 || barnum < 0) {
2333 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
2336 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
2341 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2342 new_bar(p, 0, 4, (int) obj->f);
2345 FILE *fp = popen(obj->data.execi.cmd, "r");
2346 int n2 = fread(p, 1, n, fp);
2349 if (n2 && p[n2 - 1] == '\n')
2358 if (sscanf(p, "%f", &barnum) == 0) {
2359 ERR("reading execibar value failed (perhaps it's not the correct format?)");
2361 if (barnum > 100 || barnum < 0) {
2362 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
2364 obj->f = 255 * barnum / 100.0;
2365 new_bar(p, 0, 4, (int) obj->f);
2367 obj->data.execi.last_update =
2368 current_update_time;
2372 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2373 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
2376 FILE *fp = popen(obj->data.execi.cmd, "r");
2377 int n2 = fread(p, 1, n, fp);
2380 if (n2 && p[n2 - 1] == '\n')
2389 if (sscanf(p, "%f", &barnum) == 0) {
2390 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
2392 if (barnum > 100 || barnum < 0) {
2393 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
2396 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
2398 obj->data.execi.last_update = current_update_time;
2404 if (current_update_time -
2405 obj->data.execi.last_update <
2406 obj->data.execi.interval) {
2407 snprintf(p, n, "%s",
2408 obj->data.execi.buffer);
2410 char *p2 = obj->data.execi.buffer;
2412 popen(obj->data.execi.cmd,
2415 fread(p2, 1, TEXT_BUFFER_SIZE,
2420 if (n2 && p2[n2 - 1] == '\n')
2429 snprintf(p, n, "%s",
2430 obj->data.execi.buffer);
2432 obj->data.execi.last_update =
2433 current_update_time;
2437 static int running = 0;
2438 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2439 snprintf(p, n, "%s", obj->data.execi.buffer);
2441 static pthread_t execthread;
2444 pthread_create( &execthread, NULL, (void*)threaded_exec, (void*) obj);
2445 pthread_mutex_lock( &mutex1 );
2446 obj->data.execi.last_update = current_update_time;
2447 pthread_mutex_unlock( &mutex1 );
2449 pthread_join( execthread, NULL);
2452 snprintf(p, n, "%s", obj->data.execi.buffer);
2457 if (obj->data.fs != NULL) {
2458 if (obj->data.fs->size == 0)
2477 if (obj->data.fs != NULL)
2478 human_readable(obj->data.fs->avail,
2482 if (obj->data.fs != NULL) {
2483 if (obj->data.fs->size)
2484 snprintf(p, n, "%*d",
2493 snprintf(p, n, "0");
2497 if (obj->data.fs != NULL)
2498 human_readable(obj->data.fs->size,
2502 if (obj->data.fs != NULL)
2503 human_readable(obj->data.fs->size -
2504 (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
2508 if (obj->data.fs != NULL) {
2509 if (obj->data.fs->size == 0)
2527 if (obj->data.fs != NULL) {
2528 if (obj->data.fs->size)
2529 snprintf(p, 4, "%d",
2539 snprintf(p, n, "0");
2543 float *v = info.loadavg;
2545 if (obj->data.loadavg[2])
2546 snprintf(p, n, "%.2f %.2f %.2f",
2547 v[obj->data.loadavg[0] -
2549 v[obj->data.loadavg[1] -
2551 v[obj->data.loadavg[2] -
2553 else if (obj->data.loadavg[1])
2554 snprintf(p, n, "%.2f %.2f",
2555 v[obj->data.loadavg[0] -
2557 v[obj->data.loadavg[1] -
2559 else if (obj->data.loadavg[0])
2560 snprintf(p, n, "%.2f",
2561 v[obj->data.loadavg[0] -
2565 new_hr(p, obj->data.i);
2568 new_offset(p, obj->data.i);
2571 new_voffset(p, obj->data.i);
2576 r = get_i2c_info(&obj->data.i2c.fd,
2578 obj->data.i2c.devtype,
2579 obj->data.i2c.type);
2581 if (r >= 100.0 || r == 0)
2582 snprintf(p, n, "%d", (int) r);
2584 snprintf(p, n, "%.1f", r);
2587 new_alignr(p, obj->data.i);
2590 new_alignc(p, obj->data.i);
2594 if ((obj->data.ifblock.s)
2595 && (stat(obj->data.ifblock.s, &tmp) ==
2597 i = obj->data.ifblock.pos - 2;
2603 if ((obj->data.ifblock.s)
2604 && (!check_mount(obj->data.ifblock.s))) {
2605 i = obj->data.ifblock.pos - 2;
2611 if ((obj->data.ifblock.s)
2612 && system(obj->data.ifblock.s)) {
2613 i = obj->data.ifblock.pos - 2;
2619 snprintf(p, n, "%s", cur->uname_s.release);
2622 snprintf(p, n, "%s", cur->uname_s.machine);
2627 human_readable(cur->mem * 1024, p, 6);
2630 human_readable(cur->memmax * 1024, p, 255);
2635 snprintf(p, n, "%*lu",
2640 snprintf(p, 4, "%*lu ",
2647 new_bar(p, obj->data.pair.a,
2649 cur->memmax ? (cur->mem * 255) /
2654 new_graph(p, obj->a,
2655 obj->b, obj->c, obj->d,
2656 cur->memmax ? (cur->mem * 100.0) /
2657 (cur->memmax) : 0.0, 100, 1);
2661 snprintf(p, n, "%d",
2662 mixer_get_avg(obj->data.l));
2665 snprintf(p, n, "%d",
2666 mixer_get_left(obj->data.l));
2669 snprintf(p, n, "%d",
2670 mixer_get_right(obj->data.l));
2673 new_bar(p, obj->data.mixerbar.w,
2674 obj->data.mixerbar.h,
2675 mixer_get_avg(obj->data.mixerbar.
2679 new_bar(p, obj->data.mixerbar.w,
2680 obj->data.mixerbar.h,
2681 mixer_get_left(obj->data.mixerbar.
2685 new_bar(p, obj->data.mixerbar.w,
2686 obj->data.mixerbar.h,
2687 mixer_get_right(obj->data.mixerbar.
2693 snprintf(p, n, "%d", cur->mail_count);
2696 snprintf(p, n, "%d", cur->new_mail_count);
2699 OBJ(ml_upload_counter) {
2700 snprintf(p, n, "%lld",
2701 mlinfo.upload_counter / 1048576);
2703 OBJ(ml_download_counter) {
2704 snprintf(p, n, "%lld",
2705 mlinfo.download_counter /
2708 OBJ(ml_nshared_files) {
2709 snprintf(p, n, "%i", mlinfo.nshared_files);
2711 OBJ(ml_shared_counter) {
2712 snprintf(p, n, "%lld",
2713 mlinfo.shared_counter / 1048576);
2715 OBJ(ml_tcp_upload_rate) {
2716 snprintf(p, n, "%.2f",
2717 (float) mlinfo.tcp_upload_rate /
2720 OBJ(ml_tcp_download_rate) {
2721 snprintf(p, n, "%.2f",
2722 (float) mlinfo.tcp_download_rate /
2725 OBJ(ml_udp_upload_rate) {
2726 snprintf(p, n, "%.2f",
2727 (float) mlinfo.udp_upload_rate /
2730 OBJ(ml_udp_download_rate) {
2731 snprintf(p, n, "%.2f",
2732 (float) mlinfo.udp_download_rate /
2735 OBJ(ml_ndownloaded_files) {
2736 snprintf(p, n, "%i",
2737 mlinfo.ndownloaded_files);
2739 OBJ(ml_ndownloading_files) {
2740 snprintf(p, n, "%i",
2741 mlinfo.ndownloading_files);
2746 snprintf(p, n, "%s",
2747 cur->uname_s.nodename);
2750 new_outline(p, obj->data.l);
2754 snprintf(p, n, "%hu", cur->procs);
2756 snprintf(p, 5, "%hu ",
2759 OBJ(running_processes) {
2761 snprintf(p, n, "%hu",
2764 snprintf(p, 3, "%hu ",
2768 snprintf(p, n, "%s", obj->data.s);
2771 new_bg(p, obj->data.l);
2774 new_stippled_hr(p, obj->data.pair.a,
2778 human_readable(cur->swap * 1024, p, 255);
2781 human_readable(cur->swapmax * 1024, p,
2785 if (cur->swapmax == 0) {
2786 strncpy(p, "No swap", 255);
2789 snprintf(p, 255, "%*lu",
2795 snprintf(p, 4, "%*lu ",
2803 new_bar(p, obj->data.pair.a,
2805 cur->swapmax ? (cur->swap * 255) /
2806 (cur->swapmax) : 0);
2809 snprintf(p, n, "%s", cur->uname_s.sysname);
2812 time_t t = time(NULL);
2813 struct tm *tm = localtime(&t);
2814 setlocale(LC_TIME, "");
2815 strftime(p, n, obj->data.s, tm);
2818 time_t t = time(NULL);
2819 struct tm *tm = gmtime(&t);
2820 strftime(p, n, obj->data.s, tm);
2823 human_readable(obj->data.net->recv, p,
2827 human_readable(obj->data.net->trans, p,
2831 snprintf(p, n, "%d", total_updates);
2835 snprintf(p, n, "%d",
2836 (int) (obj->data.net->
2840 snprintf(p, 5, "%d ",
2841 (int) (obj->data.net->
2847 snprintf(p, n, "%.1f",
2849 trans_speed / 1024.0);
2851 snprintf(p, 8, "%.1f ",
2853 trans_speed / 1024.0);
2856 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
2857 obj->data.net->trans_speed = 0.01;
2858 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2859 (obj->data.net->trans_speed /
2860 1024.0), obj->e, 1);
2863 format_seconds_short(p, n,
2867 format_seconds(p, n, (int) cur->uptime);
2870 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2872 snprintf(p, n, "%s", get_apm_adapter());
2874 OBJ(apm_battery_life) {
2876 msg = get_apm_battery_life();
2877 snprintf(p, n, "%s", msg);
2880 OBJ(apm_battery_time) {
2882 msg = get_apm_battery_time();
2883 snprintf(p, n, "%s", msg);
2886 #endif /* __FreeBSD__ */
2889 snprintf(p, n, "%.2f",
2890 cur->seti_prog * 100.0f);
2893 new_bar(p, obj->data.pair.a,
2895 (int) (cur->seti_prog * 255.0f));
2898 snprintf(p, n, "%.0f", cur->seti_credit);
2904 snprintf(p, n, "%s", cur->mpd.title);
2907 snprintf(p, n, "%s", cur->mpd.artist);
2910 snprintf(p, n, "%s", cur->mpd.album);
2913 snprintf(p, n, "%s", cur->mpd.random);
2916 snprintf(p, n, "%s", cur->mpd.repeat);
2919 snprintf(p, n, "%s", cur->mpd.track);
2922 snprintf(p, n, "%i", cur->mpd.volume);
2925 snprintf(p, n, "%i", cur->mpd.bitrate);
2928 snprintf(p, n, "%s", cur->mpd.status);
2931 int days = 0, hours = 0, minutes =
2933 int tmp = cur->mpd.elapsed;
2934 while (tmp >= 86400) {
2938 while (tmp >= 3600) {
2948 snprintf(p, n, "%i days %i:%02i:%02i",
2949 days, hours, minutes,
2952 snprintf(p, n, "%i:%02i:%02i", hours,
2955 snprintf(p, n, "%i:%02i", minutes,
2959 int days = 0, hours = 0, minutes =
2961 int tmp = cur->mpd.length;
2962 while (tmp >= 86400) {
2966 while (tmp >= 3600) {
2977 "%i days %i:%02i:%02i",
2978 days, hours, minutes,
2981 snprintf(p, n, "%i:%02i:%02i", hours,
2984 snprintf(p, n, "%i:%02i", minutes,
2988 snprintf(p, n, "%2.0f",
2989 cur->mpd.progress * 100);
2992 new_bar(p, obj->data.pair.a,
2994 (int) (cur->mpd.progress *
2999 if (obj->data.top.type == TOP_NAME
3000 && obj->data.top.num >= 0
3001 && obj->data.top.num < 10) {
3002 // if we limit the buffer and add a bunch of space after, it stops the thing from
3003 // moving other shit around, which is really fucking annoying
3004 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
3005 } else if (obj->data.top.type == TOP_CPU
3006 && obj->data.top.num >= 0
3007 && obj->data.top.num < 10) {
3008 snprintf(p, 7, "%3.2f ",
3009 cur->cpu[obj->data.top.
3011 } else if (obj->data.top.type == TOP_PID
3012 && obj->data.top.num >= 0
3013 && obj->data.top.num < 10) {
3014 snprintf(p, 8, "%i ",
3015 cur->cpu[obj->data.top.
3017 } else if (obj->data.top.type == TOP_MEM
3018 && obj->data.top.num >= 0
3019 && obj->data.top.num < 10) {
3020 snprintf(p, 7, "%3.2f ",
3021 cur->cpu[obj->data.top.
3026 if (obj->data.top.type == TOP_NAME
3027 && obj->data.top.num >= 0
3028 && obj->data.top.num < 10) {
3029 // if we limit the buffer and add a bunch of space after, it stops the thing from
3030 // moving other shit around, which is really fucking annoying
3033 cur->memu[obj->data.top.
3035 } else if (obj->data.top.type == TOP_CPU
3036 && obj->data.top.num >= 0
3037 && obj->data.top.num < 10) {
3038 snprintf(p, 7, "%3.2f ",
3039 cur->memu[obj->data.top.
3041 } else if (obj->data.top.type == TOP_PID
3042 && obj->data.top.num >= 0
3043 && obj->data.top.num < 10) {
3044 snprintf(p, 8, "%i ",
3045 cur->memu[obj->data.top.
3047 } else if (obj->data.top.type == TOP_MEM
3048 && obj->data.top.num >= 0
3049 && obj->data.top.num < 10) {
3050 snprintf(p, 7, "%3.2f ",
3051 cur->memu[obj->data.top.
3059 * I'm tired of everything being packed in
3066 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
3067 snprintf(p, n, "%s", obj->data.tail.buffer);
3069 obj->data.tail.last_update = current_update_time;
3073 tailstring *head = NULL;
3074 tailstring *headtmp = NULL;
3075 tailstring *freetmp = NULL;
3076 fp = fopen(obj->data.tail.logfile, "rt");
3078 ERR("tail logfile failed to open");
3081 obj->data.tail.readlines = 0;
3083 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
3090 addtail(&head, obj->data.tail.buffer);
3091 obj->data.tail.readlines++;
3097 if (obj->data.tail.readlines > 0) {
3098 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
3099 addtail(&headtmp, head->data);
3104 strcpy(obj->data.tail.buffer, headtmp->data);
3105 headtmp = headtmp->next;
3106 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
3108 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
3109 headtmp = headtmp->next;
3113 /* get rid of any ugly newlines at the end */
3114 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
3115 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
3117 snprintf(p, n, "%s", obj->data.tail.buffer);
3122 strcpy(obj->data.tail.buffer, "Logfile Empty");
3123 snprintf(p, n, "Logfile Empty");
3129 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
3130 snprintf(p, n, "%s", obj->data.tail.buffer);
3132 obj->data.tail.last_update = current_update_time;
3134 tailstring *head = NULL;
3135 tailstring *headtmp = NULL;
3136 tailstring *freetmp = NULL;
3137 fp = fopen(obj->data.tail.logfile, "rt");
3139 ERR("head logfile failed to open");
3142 obj->data.tail.readlines = 0;
3143 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
3144 addtail(&head, obj->data.tail.buffer);
3145 obj->data.tail.readlines++;
3149 if (obj->data.tail.readlines > 0) {
3151 addtail(&headtmp, head->data);
3156 strcpy(obj->data.tail.buffer, headtmp->data);
3157 headtmp = headtmp->next;
3159 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
3160 headtmp = headtmp->next;
3163 /* get rid of any ugly newlines at the end */
3164 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
3165 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
3167 snprintf(p, n, "%s", obj->data.tail.buffer);
3170 strcpy(obj->data.tail.buffer, "Logfile Empty");
3171 snprintf(p, n, "Logfile Empty");
3176 #ifdef TCP_PORT_MONITOR
3179 /* grab a pointer to this port monitor */
3180 tcp_port_monitor_t * p_monitor =
3181 find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
3182 obj->data.tcp_port_monitor.port_range_begin,
3183 obj->data.tcp_port_monitor.port_range_end );
3185 snprintf(p, n, "monitor not found");
3189 /* now grab the text of interest */
3190 if ( peek_tcp_port_monitor( p_monitor,
3191 obj->data.tcp_port_monitor.item,
3192 obj->data.tcp_port_monitor.connection_index,
3195 snprintf(p, n, "monitor peek error");
3205 unsigned int a = strlen(p);
3211 if (stuff_in_upper_case) {
3221 last_update_time = current_update_time;
3227 static void set_font()
3231 if (window.xftdraw != NULL) {
3232 XftDrawDestroy(window.xftdraw);
3234 window.xftdraw = XftDrawCreate(display, window.drawable,
3235 DefaultVisual(display,
3237 DefaultColormap(display,
3242 XSetFont(display, window.gc, fonts[selected_font].font->fid);
3251 static int text_start_x, text_start_y; /* text start position in window */
3252 static int text_width, text_height;
3256 static inline int get_string_width(const char *s)
3259 return *s ? calc_text_width(s, strlen(s)) : 0;
3265 static inline int get_string_width_special(char *s)
3278 if (*p == SPECIAL_CHAR) {
3279 /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
3280 for (i = 0; i < strlen(p); i++) {
3281 *(p + i) = *(p + i + 1);
3283 if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
3284 width += specials[special_index+index].width;
3291 if (strlen(final) > 1) {
3292 width += calc_text_width(final, strlen(final));
3304 static void text_size_updater(char *s)
3308 int h = font_height();
3309 /* get string widths and skip specials */
3312 if (*p == SPECIAL_CHAR) {
3314 w += get_string_width(s);
3317 if (specials[special_index].type == BAR
3318 || specials[special_index].type == GRAPH) {
3319 w += specials[special_index].width;
3320 if (specials[special_index].height > h) {
3321 h = specials[special_index].height;
3326 else if (specials[special_index].type == OFFSET) {
3327 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
3329 else if (specials[special_index].type == VOFFSET) {
3330 h += specials[special_index].arg;
3332 else if (specials[special_index].type == FONT) {
3333 fontchange = specials[special_index].font_added;
3334 selected_font = specials[special_index].font_added;
3344 w += get_string_width(s);
3347 if (text_width > maximum_width && maximum_width)
3348 text_width = maximum_width;
3359 static void update_text_area()
3363 /* update text size if it isn't fixed */
3368 text_width = minimum_width;
3371 for_each_line(text_buffer, text_size_updater);
3373 if (text_height < minimum_height)
3374 text_height = minimum_height;
3375 if (text_width > maximum_width && maximum_width > 0)
3376 text_width = maximum_width;
3379 /* get text position on workarea */
3380 switch (text_alignment) {
3387 x = workarea[2] - text_width - gap_x;
3394 y = workarea[3] - text_height - gap_y;
3398 x = workarea[2] - text_width - gap_x;
3399 y = workarea[3] - text_height - gap_y;
3403 case NONE: // Let the WM manage the window
3414 if (own_window && !fixed_pos) {
3417 text_start_x = border_margin + 1;
3418 text_start_y = border_margin + 1;
3419 window.x = x - border_margin - 1;
3420 window.y = y - border_margin - 1;
3424 /* If window size doesn't match to workarea's size, then window
3425 * probably includes panels (gnome).
3426 * Blah, doesn't work on KDE. */
3427 if (workarea[2] != window.width
3428 || workarea[3] != window.height) {
3442 static int cur_x, cur_y; /* current x and y for drawing */
3443 static int draw_mode; /* FG, BG or OUTLINE */
3444 static long current_color;
3446 static inline void set_foreground_color(long c)
3449 XSetForeground(display, window.gc, c);
3453 static void draw_string(const char *s)
3457 int i, i2, pos, width_of_s;
3460 width_of_s = get_string_width(s);
3461 if (out_to_console) {
3464 /* daemon_run(s); the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
3465 memset(tmpstring1,0,TEXT_BUFFER_SIZE);
3466 memset(tmpstring2,0,TEXT_BUFFER_SIZE);
3467 strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
3471 snprintf(space, 2, " ");
3473 max = ((text_width - width_of_s) / get_string_width(space));
3476 * This code looks for tabs in the text and coverts them to spaces.
3477 * The trick is getting the correct number of spaces,
3478 * and not going over the window's size without forcing
3479 * the window larger.
3481 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
3482 if (tmpstring1[i] == '\t') // 9 is ascii tab
3486 i2 < (8 - (1 + pos) % 8) && added <= max;
3489 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
3490 fprintf(stderr,"buffer overrun detected\n");
3492 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
3497 if (tmpstring1[i] != 9) {
3499 if ( pos > TEXT_BUFFER_SIZE-1 )
3500 fprintf(stderr,"buffer overrun detected\n");
3502 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
3508 if (text_width == maximum_width) {
3509 /* this means the text is probably pushing the limit, so we'll chop it */
3510 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
3511 tmpstring2[strlen(tmpstring2)-1] = '\0';
3521 c.pixel = current_color;
3522 XQueryColor(display, DefaultColormap(display, screen), &c);
3525 c2.color.red = c.red;
3526 c2.color.green = c.green;
3527 c2.color.blue = c.blue;
3528 c2.color.alpha = fonts[selected_font].font_alpha;
3530 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3531 cur_x, cur_y, (XftChar8 *) s,
3534 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3535 cur_x, cur_y, (XftChar8 *) s,
3541 XDrawString(display, window.drawable, window.gc,
3542 cur_x, cur_y, s, strlen(s));
3544 cur_x += width_of_s;
3546 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
3549 long redmask, greenmask, bluemask;
3551 void set_up_gradient()
3554 colour_depth = DisplayPlanes(display, screen);
3558 if (colour_depth != 24 && colour_depth != 16) {
3559 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
3565 for(i = (colour_depth / 3)-1; i>=0; i--) {
3567 greenmask |= 1 << i;
3570 if (colour_depth%3 == 1) {
3571 greenmask |= 1 << (colour_depth / 3);
3573 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
3574 greenmask = greenmask << (colour_depth / 3);
3577 inline unsigned long do_gradient(unsigned long first_colour, unsigned long last_colour) { /* this function returns the next colour between two colours for a gradient */
3579 int red1, green1, blue1; // first colour
3580 int red2, green2, blue2; // second colour
3581 int red3 = 0, green3 = 0, blue3 = 0; // difference
3582 short redshift = (2*colour_depth / 3 + colour_depth%3);
3583 short greenshift = (colour_depth / 3);
3584 red1 = (first_colour & redmask) >> redshift;
3585 green1 = (first_colour & greenmask) >> greenshift;
3586 blue1 = first_colour & bluemask;
3587 red2 = (last_colour & redmask) >> redshift;
3588 green2 = (last_colour & greenmask) >> greenshift;
3589 blue2 = last_colour & bluemask;
3596 if (green1 > green2) {
3599 if (green1 < green2) {
3602 if (blue1 > blue2) {
3605 if (blue1 < blue2) {
3620 if (red1 > bluemask) {
3623 if (green1 > bluemask) {
3626 if (blue1 > bluemask) {
3629 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
3633 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
3634 if (colour_depth == 0) {
3637 int red1, green1, blue1; // first colour
3638 int red2, green2, blue2; // second colour
3639 long redshift = (2*colour_depth / 3 + colour_depth%3);
3640 long greenshift = (colour_depth / 3);
3641 int red3 = 0, green3 = 0, blue3 = 0; // difference
3642 red1 = (first_colour & redmask) >> redshift;
3643 green1 = (first_colour & greenmask) >> greenshift;
3644 blue1 = first_colour & bluemask;
3645 red2 = (last_colour & redmask) >> redshift;
3646 green2 = (last_colour & greenmask) >> greenshift;
3647 blue2 = last_colour & bluemask;
3648 red3 = abs(red1 - red2);
3649 green3 = abs(green1 - green2);
3650 blue3 = abs(blue1 - blue2);
3659 static void draw_line(char *s)
3663 cur_x = text_start_x;
3664 cur_y += font_ascent();
3666 short font_h = font_height();
3668 /* find specials and draw stuff */
3671 if (*p == SPECIAL_CHAR) {
3674 /* draw string before special */
3681 switch (specials[special_index].type) {
3682 case HORIZONTAL_LINE:
3685 specials[special_index].height;
3686 int mid = font_ascent() / 2;
3687 w = text_start_x + text_width -
3690 XSetLineAttributes(display,
3695 XDrawLine(display, window.drawable,
3706 specials[special_index].height;
3708 specials[special_index].arg;
3709 int mid = font_ascent() / 2;
3710 char ss[2] = { s, s };
3711 w = text_start_x + text_width -
3714 XSetLineAttributes(display,
3719 XSetDashes(display, window.gc, 0,
3721 XDrawLine(display, window.drawable,
3731 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3735 specials[special_index].height;
3737 specials[special_index].arg;
3742 by = cur_y - (font_ascent() + h) / 2 - 1;
3746 by = cur_y - (font_ascent()/2) - 1;
3748 if (h < (font_height())) {
3751 w = specials[special_index].width;
3754 text_width - cur_x - 1;
3758 XSetLineAttributes(display,
3764 XDrawRectangle(display,
3768 XFillRectangle(display,
3772 w * bar_usage / 255,
3774 if (specials[special_index].
3776 && specials[special_index].
3780 [special_index].height;
3787 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3791 specials[special_index].height;
3795 by = cur_y - (font_ascent() + h) / 2 - 1;
3799 by = cur_y - (font_ascent()/2) - 1;
3801 if (h < (font_height())) {
3804 w = specials[special_index].width;
3806 w = text_start_x + text_width - cur_x - 1;
3809 XSetLineAttributes(display,
3814 XDrawRectangle(display,
3818 XSetLineAttributes(display,
3825 int gradient_size = 0;
3826 float gradient_factor = 0;
3827 float gradient_update = 0;
3828 unsigned long tmpcolour = current_color;
3829 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3830 tmpcolour = specials[special_index].first_colour;
3831 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3832 gradient_factor = (float)gradient_size / (w - 3);
3834 for (i = 0; i < w - 3; i++) {
3835 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3836 XSetForeground(display, window.gc, tmpcolour);
3837 gradient_update += gradient_factor;
3838 while (gradient_update > 0) {
3839 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3843 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3846 XDrawLine(display, window.drawable, window.gc, cur_x + i + 2, by + h, cur_x + i + 2, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale); /* this is mugfugly, but it works */
3848 if (specials[special_index].
3850 && specials[special_index].
3854 [special_index].height;
3857 if (draw_mode == BG) {
3858 set_foreground_color(default_bg_color);
3860 else if (draw_mode == OUTLINE) {
3861 set_foreground_color(default_out_color);
3863 set_foreground_color(default_fg_color);
3869 cur_y -= font_ascent();
3870 selected_font = specials[special_index].font_added;
3871 cur_y += font_ascent();
3873 if (!use_xft || use_xdbe)
3881 if (draw_mode == FG)
3882 set_foreground_color(specials
3888 if (draw_mode == BG)
3889 set_foreground_color(specials
3895 if (draw_mode == OUTLINE)
3896 set_foreground_color(specials
3903 w += specials[special_index].arg;
3908 cur_y += specials[special_index].arg;
3914 int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
3915 /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i border_margin %i border_width %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width_special(s), gap_x, specials[special_index].arg, border_margin, border_width);*/
3916 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3917 cur_x = pos_x - specials[special_index].arg;
3924 int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
3925 /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
3926 /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width(s), gap_x, specials[special_index].arg);*/
3928 specials[special_index].arg)
3931 [special_index].arg;
3948 if (cur_y_add > 0) {
3950 cur_y -= font_descent();
3955 cur_y += font_descent();
3962 static void draw_text()
3965 cur_y = text_start_y;
3968 if (draw_borders && border_width > 0) {
3969 unsigned int b = (border_width + 1) / 2;
3971 if (stippled_borders) {
3973 { stippled_borders, stippled_borders };
3974 XSetLineAttributes(display, window.gc,
3975 border_width, LineOnOffDash,
3976 CapButt, JoinMiter);
3977 XSetDashes(display, window.gc, 0, ss, 2);
3979 XSetLineAttributes(display, window.gc,
3980 border_width, LineSolid,
3981 CapButt, JoinMiter);
3984 XDrawRectangle(display, window.drawable, window.gc,
3985 text_start_x - border_margin + b,
3986 text_start_y - border_margin + b,
3987 text_width + border_margin * 2 - 1 - b * 2,
3988 text_height + border_margin * 2 - 1 -
3995 for_each_line(text_buffer, draw_line);
3998 static void draw_stuff()
4001 if (draw_shades && !draw_outline) {
4004 set_foreground_color(default_bg_color);
4013 for (i = -1; i < 2; i++)
4014 for (j = -1; j < 2; j++) {
4015 if (i == 0 && j == 0)
4019 set_foreground_color(default_out_color);
4020 draw_mode = OUTLINE;
4027 set_foreground_color(default_fg_color);
4035 swap.swap_window = window.window;
4036 swap.swap_action = XdbeBackground;
4037 XdbeSwapBuffers(display, &swap, 1);
4043 static void clear_text(int exposures)
4047 return; /* The swap action is XdbeBackground, which clears */
4051 /* there is some extra space for borders and outlines */
4052 XClearArea(display, window.drawable,
4053 text_start_x - border_margin - 1,
4054 text_start_y - border_margin - 1,
4055 text_width + border_margin * 2 + 2,
4056 text_height + border_margin * 2 + 2,
4057 exposures ? True : 0);
4062 static int need_to_update;
4064 /* update_text() generates new text and clears old text area */
4065 static void update_text()
4074 static void main_loop()
4076 #ifdef SIGNAL_BLOCKING
4077 sigset_t newmask, oldmask;
4079 sigemptyset(&newmask);
4080 sigaddset(&newmask,SIGINT);
4081 sigaddset(&newmask,SIGTERM);
4082 sigaddset(&newmask,SIGUSR1);
4086 Region region = XCreateRegion();
4090 while (total_run_times == 0 || info.looped < total_run_times - 1) {
4093 #ifdef SIGNAL_BLOCKING
4094 /* block signals. we will inspect for pending signals later */
4095 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
4096 CRIT_ERR("unable to sigprocmask()");
4102 /* wait for X event or timeout */
4104 if (!XPending(display)) {
4109 update_interval - (get_time() -
4115 tv.tv_sec = (long) t;
4116 tv.tv_usec = (long) (t * 1000000) % 1000000;
4118 FD_SET(ConnectionNumber(display), &fdsr);
4121 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
4124 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
4129 ERR("can't select(): %s",
4140 set_transparent_background(window.window);
4145 if (need_to_update) {
4147 int wx = window.x, wy = window.y;
4155 /* resize window if it isn't right size */
4157 (text_width + border_margin * 2 !=
4159 || text_height + border_margin * 2 !=
4163 border_margin * 2 + 1;
4166 border_margin * 2 + 1;
4167 XResizeWindow(display,
4173 /* move window if it isn't in right position */
4176 || window.y != wy)) {
4177 XMoveWindow(display, window.window,
4178 window.x, window.y);
4188 r.x = text_start_x - border_margin;
4189 r.y = text_start_y - border_margin;
4190 r.width = text_width + border_margin * 2;
4191 r.height = text_height + border_margin * 2;
4192 XUnionRectWithRegion(&r, region, region);
4197 /* handle X events */
4199 while (XPending(display)) {
4201 XNextEvent(display, &ev);
4208 r.width = ev.xexpose.width;
4209 r.height = ev.xexpose.height;
4210 XUnionRectWithRegion(&r, region,
4216 case ReparentNotify:
4217 /* set background to ParentRelative for all parents */
4219 set_transparent_background(window.
4224 case ConfigureNotify:
4226 /* if window size isn't what expected, set fixed size */
4227 if (ev.xconfigure.width !=
4229 || ev.xconfigure.height !=
4231 if (window.width != 0
4232 && window.height != 0)
4235 /* clear old stuff before screwing up size and pos */
4241 if (XGetWindowAttributes(display, window.window, &attrs)) {
4256 border_margin * 2 - 1;
4259 border_margin * 2 - 1;
4260 if (text_width > maximum_width && maximum_width > 0)
4261 text_width = maximum_width;
4264 /* if position isn't what expected, set fixed pos, total_updates
4265 * avoids setting fixed_pos when window is set to weird locations
4267 /*if (total_updates >= 2 this is broken
4269 && (window.x != ev.xconfigure.x
4272 && (ev.xconfigure.x != 0
4273 || ev.xconfigure.y != 0)) {
4286 /* XDBE doesn't seem to provide a way to clear the back buffer without
4287 * interfering with the front buffer, other than passing XdbeBackground
4288 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
4289 * redraw the text even if it wasn't part of the exposed area. OTOH,
4290 * if we're not going to call draw_stuff at all, then no swap happens
4291 * and we can safely do nothing.
4294 if (!XEmptyRegion(region)) {
4298 r.x = text_start_x - border_margin;
4299 r.y = text_start_y - border_margin;
4300 r.width = text_width + border_margin * 2;
4301 r.height = text_height + border_margin * 2;
4302 XUnionRectWithRegion(&r, region, region);
4305 XSetRegion(display, window.gc, region);
4308 XftDrawSetClip(window.xftdraw, region);
4314 XDestroyRegion(region);
4315 region = XCreateRegion();
4319 #ifdef SIGNAL_BLOCKING
4320 /* unblock signals of interest and let handler fly */
4321 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
4322 CRIT_ERR("unable to sigprocmask()");
4325 switch(g_signal_pending) {
4328 ERR("received SIGUSR1. reloading the config file.");
4335 ERR("received SIGINT or SIGTERM to terminate. bye!");
4338 XDestroyRegion(region);
4341 return; /* return from main_loop */
4346 /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
4347 * but didn't write any code to deal with it. if you don't want to
4348 * handle a signal, don't set a handler on it in the first place. */
4349 if (g_signal_pending)
4350 ERR("ignoring signal (%d)", g_signal_pending);
4358 XDestroyRegion(region);
4363 static void load_config_file(const char *);
4365 /* reload the config file */
4366 void reload_config(void)
4368 if (current_config) {
4370 load_config_file(current_config);
4374 XClearWindow(display, RootWindow(display, screen)); // clear the window first
4377 #ifdef TCP_PORT_MONITOR
4378 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
4379 info.p_tcp_port_monitor_collection = NULL;
4381 extract_variable_text(text);
4393 XdbeDeallocateBackBufferName(display, window.back_buffer);
4398 XDestroyWindow(display, window.window);
4402 XClearWindow(display, RootWindow(display, screen));
4407 XFreeGC(display, window.gc);
4411 /* it is really pointless to free() memory at the end of program but ak|ra
4412 * wants me to do this */
4414 free_text_objects();
4416 if (text != original_text)
4419 free(current_config);
4420 free(current_mail_spool);
4424 #ifdef TCP_PORT_MONITOR
4425 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
4426 info.p_tcp_port_monitor_collection = NULL;
4430 static int string_to_bool(const char *s)
4434 if (strcasecmp(s, "yes") == 0)
4436 if (strcasecmp(s, "true") == 0)
4438 if (strcasecmp(s, "1") == 0)
4443 static enum alignment string_to_alignment(const char *s)
4445 if (strcasecmp(s, "top_left") == 0)
4447 else if (strcasecmp(s, "top_right") == 0)
4449 else if (strcasecmp(s, "bottom_left") == 0)
4451 else if (strcasecmp(s, "bottom_right") == 0)
4452 return BOTTOM_RIGHT;
4453 else if (strcasecmp(s, "tl") == 0)
4455 else if (strcasecmp(s, "tr") == 0)
4457 else if (strcasecmp(s, "bl") == 0)
4459 else if (strcasecmp(s, "br") == 0)
4460 return BOTTOM_RIGHT;
4461 else if (strcasecmp(s, "none") == 0)
4468 static void set_default_configurations(void)
4470 fork_to_background = 0;
4471 total_run_times = 0;
4472 info.cpu_avg_samples = 2;
4473 info.net_avg_samples = 2;
4478 strcpy(info.mpd.host, "localhost");
4479 info.mpd.port = 6600;
4480 info.mpd.status = "Checking status...";
4489 default_fg_color = WhitePixel(display, screen);
4490 default_bg_color = BlackPixel(display, screen);
4491 default_out_color = BlackPixel(display, screen);
4495 set_first_font("6x10");
4503 strcpy(wm_class_name, "conky");
4505 stippled_borders = 0;
4508 text_alignment = BOTTOM_LEFT;
4512 free(current_mail_spool);
4515 variable_substitute(MAIL_FILE, buf, 256);
4517 current_mail_spool = strdup(buf);
4521 update_interval = 10.0;
4522 stuff_in_upper_case = 0;
4524 mlconfig.mldonkey_hostname = strdup("127.0.0.1");
4525 mlconfig.mldonkey_port = 4001;
4526 mlconfig.mldonkey_login = NULL;
4527 mlconfig.mldonkey_password = NULL;
4530 #ifdef TCP_PORT_MONITOR
4531 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4532 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4536 static void load_config_file(const char *f)
4538 #define CONF_ERR ERR("%s: %d: config file error", f, line)
4542 set_default_configurations();
4548 char buf[256], *p, *p2, *name, *value;
4550 if (fgets(buf, 256, fp) == NULL)
4555 /* break at comment */
4556 p2 = strchr(p, '#');
4561 while (*p && isspace((int) *p))
4564 continue; /* empty line */
4570 while (*p2 && !isspace((int) *p2))
4573 *p2 = '\0'; /* break at name's end */
4580 while (*p && isspace((int) *p))
4585 p2 = value + strlen(value);
4586 while (isspace((int) *(p2 - 1)))
4592 #define CONF2(a) if (strcasecmp(name, a) == 0)
4593 #define CONF(a) else CONF2(a)
4594 #define CONF3(a,b) \
4595 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
4599 CONF2("alignment") {
4601 int a = string_to_alignment(value);
4611 on_bottom = string_to_bool(value);
4615 CONF("background") {
4616 fork_to_background = string_to_bool(value);
4620 CONF2("background") {
4621 fork_to_background = string_to_bool(value);
4625 CONF("border_margin") {
4627 border_margin = strtol(value, 0, 0);
4631 CONF("border_width") {
4633 border_width = strtol(value, 0, 0);
4637 CONF("default_color") {
4639 default_fg_color = get_x11_color(value);
4643 CONF3("default_shade_color", "default_shadecolor") {
4645 default_bg_color = get_x11_color(value);
4649 CONF3("default_outline_color", "default_outlinecolor") {
4651 default_out_color = get_x11_color(value);
4659 strcpy(info.mpd.host, value);
4665 info.mpd.port = strtol(value, 0, 0);
4666 if (info.mpd.port < 1
4667 || info.mpd.port > 0xffff)
4672 CONF("cpu_avg_samples") {
4674 cpu_avg_samples = strtol(value, 0, 0);
4675 if (cpu_avg_samples < 1
4676 || cpu_avg_samples > 14)
4685 CONF("net_avg_samples") {
4687 net_avg_samples = strtol(value, 0, 0);
4688 if (net_avg_samples < 1
4689 || net_avg_samples > 14)
4705 CONF("double_buffer") {
4706 use_xdbe = string_to_bool(value);
4710 CONF("override_utf8_locale") {
4711 utf8_mode = string_to_bool(value);
4714 CONF("draw_borders") {
4715 draw_borders = string_to_bool(value);
4717 CONF("draw_shades") {
4718 draw_shades = string_to_bool(value);
4720 CONF("draw_outline") {
4721 draw_outline = string_to_bool(value);
4724 CONF("out_to_console") {
4725 out_to_console = string_to_bool(value);
4727 CONF("use_spacer") {
4728 use_spacer = string_to_bool(value);
4733 use_xft = string_to_bool(value);
4738 set_first_font(value);
4744 if (value && font_count >= 0)
4745 fonts[0].font_alpha = atof(value)
4753 if (string_to_bool(value))
4754 ERR("Xft not enabled");
4757 /* xftfont silently ignored when no Xft */
4760 /* xftalpha is silently ignored when no Xft */
4765 set_first_font(value);
4771 gap_x = atoi(value);
4777 gap_y = atoi(value);
4782 CONF("mail_spool") {
4785 variable_substitute(value, buf, 256);
4789 if (current_mail_spool)
4790 free(current_mail_spool);
4791 current_mail_spool = strdup(buf);
4797 CONF("minimum_size") {
4800 (value, "%d %d", &minimum_width,
4801 &minimum_height) != 2)
4804 &minimum_width) != 1)
4809 CONF("maximum_width") {
4811 if (sscanf(value, "%d", &maximum_width) != 1)
4817 CONF("no_buffers") {
4818 no_buffers = string_to_bool(value);
4821 CONF("mldonkey_hostname") {
4823 if (mlconfig.mldonkey_hostname != NULL) {
4824 free(mlconfig.mldonkey_hostname);
4826 mlconfig.mldonkey_hostname = strdup(value);
4831 CONF("mldonkey_port") {
4833 mlconfig.mldonkey_port = atoi(value);
4837 CONF("mldonkey_login") {
4839 if (mlconfig.mldonkey_login != NULL) {
4840 free(mlconfig.mldonkey_login);
4842 mlconfig.mldonkey_login = strdup(value);
4847 CONF("mldonkey_password") {
4849 if (mlconfig.mldonkey_password != NULL) {
4850 free(mlconfig.mldonkey_password);
4852 mlconfig.mldonkey_password = strdup(value);
4858 CONF("pad_percents") {
4859 pad_percents = atoi(value);
4863 CONF("own_window") {
4864 own_window = string_to_bool(value);
4866 CONF("wm_class_name") {
4867 strncpy(wm_class_name, value, sizeof(wm_class_name)-1);
4868 wm_class_name[sizeof(wm_class_name)-1] = 0;
4870 CONF("own_window_transparent") {
4871 set_transparent = string_to_bool(value);
4873 CONF("own_window_colour") {
4874 background_colour = get_x11_color(value);
4877 CONF("stippled_borders") {
4879 stippled_borders = strtol(value, 0, 0);
4881 stippled_borders = 4;
4885 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
4888 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
4890 CONF("update_interval") {
4892 update_interval = strtod(value, 0);
4896 CONF("total_run_times") {
4898 total_run_times = strtod(value, 0);
4903 stuff_in_upper_case = string_to_bool(value);
4908 malloc(strlen(value)
4910 strcpy(seti_dir, value);
4914 if (text != original_text)
4924 int l = strlen(text);
4925 if (fgets(buf, 256, fp) == NULL)
4928 realloc(text, l + strlen(buf)
4932 if (strlen(text) > 1024 * 8)
4938 #ifdef TCP_PORT_MONITOR
4939 CONF("min_port_monitors")
4942 (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) ||
4943 tcp_port_monitor_collection_args.min_port_monitors < 0 )
4945 /* an error. use default, warn and continue. */
4946 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4949 else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
4951 /* no error, just use default */
4952 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4954 /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
4956 CONF("min_port_monitor_connections")
4959 (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1)
4960 || tcp_port_monitor_args.min_port_monitor_connections < 0 )
4962 /* an error. use default, warni and continue. */
4963 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4966 else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
4968 /* no error, just use default */
4969 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4971 /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
4975 ERR("%s: %d: no such configuration: '%s'", f, line, name);
4985 /* : means that character before that takes an argument */
4986 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
4999 int main(int argc, char **argv)
5001 struct sigaction act, oact;
5004 memset(&info, 0, sizeof(info) );
5006 #ifdef TCP_PORT_MONITOR
5007 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5008 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5011 /* handle command line parameters that don't change configs */
5017 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
5018 *s) || ((s = getenv("LANG")) && *s)) {
5020 for(x = 0; x < strlen(s) ; x++) {
5021 temp[x] = tolower(s[x]);
5023 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5027 if (!setlocale(LC_CTYPE, "")) {
5028 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5032 int c = getopt(argc,
5042 ("Conky " VERSION " compiled " __DATE__ "\n");
5046 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
5047 * though free() does the NULL check itself;), then load optarg value */
5049 free(current_config);
5050 current_config = strdup(optarg);
5055 ("Usage: %s [OPTION]...\n"
5056 "Conky is a system monitor that renders text on desktop or to own transparent\n"
5057 "window. Command line options will override configurations defined in config\n"
5060 " -c FILE config file to load instead of "
5063 " -d daemonize, fork to background\n"
5066 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
5067 " -f FONT font to use\n"
5069 " -o create own window to draw\n"
5072 " -b double buffer (prevents flickering)\n"
5074 " -w WIN_ID window id to draw\n"
5075 " -x X x position\n"
5076 " -y Y y position\n"
5078 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
5079 " -u SECS update interval\n"
5080 " -i NUM number of times to update Conky\n", argv[0]);
5084 window.window = strtol(optarg, 0, 0);
5093 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
5097 /* load current_config or CONFIG_FILE */
5100 if (current_config == NULL) {
5101 /* load default config file */
5104 variable_substitute(CONFIG_FILE, buf, 256);
5107 current_config = strdup(buf);
5111 if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
5112 load_config_file(current_config);
5114 set_default_configurations();
5118 if (current_mail_spool == NULL) {
5120 variable_substitute(MAIL_FILE, buf, 256);
5123 current_mail_spool = strdup(buf);
5127 /* handle other command line arguments */
5129 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
5130 optind = optreset = 1;
5136 int c = getopt(argc,
5144 fork_to_background = 1;
5149 set_first_font(optarg);
5152 text_alignment = string_to_alignment(optarg);
5167 if (text != original_text)
5169 text = strdup(optarg);
5170 convert_escapes(text);
5174 update_interval = strtod(optarg, 0);
5178 total_run_times = strtod(optarg, 0);
5182 gap_x = atoi(optarg);
5186 gap_y = atoi(optarg);
5200 /* generate text and get initial size */
5201 extract_variable_text(text);
5202 if (text != original_text) {
5211 update_text_area(); /* to get initial size of the window */
5213 #if defined OWN_WINDOW
5217 text_width + border_margin * 2 + 1,
5218 text_height + border_margin * 2 + 1,
5219 on_bottom, fixed_pos, set_transparent, background_colour, info.uname_s.nodename);
5223 text_width + border_margin * 2 + 1,
5224 text_height + border_margin * 2 + 1,
5225 on_bottom, set_transparent, background_colour, info.uname_s.nodename);
5229 update_text_area(); /* to position text/window on screen */
5233 // why the fuck not?
5239 if (own_window && !fixed_pos) {
5240 XMoveWindow(display, window.window, window.x, window.y);
5243 set_transparent_background(window.window);
5254 if (fork_to_background) {
5258 ERR("can't fork() to background: %s",
5268 "Conky: forked to background, pid is %d\n",
5274 /* Set signal handlers */
5275 act.sa_handler = signal_handler;
5276 sigemptyset(&act.sa_mask);
5279 act.sa_flags |= SA_RESTART;
5282 if ( sigaction(SIGINT,&act,&oact) < 0 ||
5283 sigaction(SIGUSR1,&act,&oact) < 0 ||
5284 sigaction(SIGTERM,&act,&oact) < 0 )
5286 ERR("error setting signal handler: %s", strerror(errno) );
5294 void signal_handler(int sig)
5296 /* signal handler is light as a feather, as it should be.
5297 * we will poll g_signal_pending with each loop of conky
5298 * and do any signal processing there, NOT here. pkovacs. */
5299 g_signal_pending=sig;