make conky fs stuff report same as df for ext3 volumes
[monky] / src / conky.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #include "conky.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <locale.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <pthread.h>
21 #include <string.h>
22 #include <limits.h>
23 #if HAVE_DIRENT_H
24 #include <dirent.h>
25 #endif
26 #include <sys/time.h>
27 #ifdef X11
28 #include <X11/Xutil.h>
29 #endif /* X11 */
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #define CONFIG_FILE "$HOME/.conkyrc"
34 #define MAIL_FILE "$MAIL"
35 #define MAX_IF_BLOCK_DEPTH 5
36
37 /* #define SIGNAL_BLOCKING */
38 #undef SIGNAL_BLOCKING
39
40 #ifdef X11
41
42 /* alignments */
43 enum alignment {
44         TOP_LEFT = 1,
45         TOP_RIGHT,
46         BOTTOM_LEFT,
47         BOTTOM_RIGHT,
48         NONE
49 };
50
51
52 /* for fonts */
53 struct font_list {
54
55         char name[TEXT_BUFFER_SIZE];
56         int num;
57         XFontStruct *font;
58
59 #ifdef XFT
60         XftFont *xftfont;
61         int font_alpha;
62 #endif  
63
64 };
65 static int selected_font = 0;
66 static int font_count = -1;
67 struct font_list *fonts = NULL;
68
69 #ifdef XFT
70
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
75
76 #else
77
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
81
82 #endif
83
84 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
85
86
87 static void set_font();
88
89
90 int addfont(const char *data_in)
91 {
92         if (font_count > MAX_FONTS) {
93                 CRIT_ERR("you don't need that many fonts, sorry.");
94         }
95         font_count++;
96         if (font_count == 0) {
97                 if (fonts != NULL) {
98                         free(fonts);
99                 }
100                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
101                         CRIT_ERR("malloc");
102                 }
103         }
104         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
105         if (fonts == NULL) {
106                 CRIT_ERR("realloc in addfont");
107         }
108         if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
109                 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
110 #ifdef XFT
111                 fonts[font_count].font_alpha = 0xffff;
112 #endif
113         } else {
114                 CRIT_ERR("Oops...looks like something overflowed in addfont().");
115         }
116         return font_count;
117 }
118
119 void set_first_font(const char *data_in)
120 {
121         if (font_count < 0) {
122                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
123                         CRIT_ERR("malloc");
124                 }
125                 font_count++;
126         }
127         if (strlen(data_in) > 1) {
128                 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
129 #ifdef XFT
130                 fonts[0].font_alpha = 0xffff;
131 #endif
132         }
133 }
134
135 void free_fonts()
136 {
137         int i;
138         for (i=0;i<=font_count;i++) {
139 #ifdef XFT
140                 if (use_xft) {
141                         XftFontClose(display, fonts[i].xftfont);
142                 } else
143 #endif
144                 {
145                         XFreeFont(display, fonts[i].font);
146                 }
147 }
148         free(fonts);
149         fonts = NULL;
150         font_count = -1;
151         selected_font = 0;
152         set_first_font("6x10");
153 }
154
155
156 static void load_fonts()
157 {
158         int i;
159         for (i=0;i<=font_count;i++) {
160 #ifdef XFT
161         /* load Xft font */
162         if (use_xft) {
163         /*if (fonts[i].xftfont != NULL && selected_font == 0) {
164                         XftFontClose(display, fonts[i].xftfont);
165         }*/
166                 if ((fonts[i].xftfont =
167                         XftFontOpenName(display, screen, fonts[i].name)) != NULL)
168                         continue;
169                 
170                 ERR("can't load Xft font '%s'", fonts[i].name);
171                 if ((fonts[i].xftfont =
172                         XftFontOpenName(display, screen,
173                                         "courier-12")) != NULL)
174                         continue;
175                 
176                 ERR("can't load Xft font '%s'", "courier-12");
177                 
178                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
179                         CRIT_ERR("can't load font '%s'", "fixed");
180                 }
181                 use_xft = 0;
182                 
183                 continue;
184         }
185 #endif
186         /* load normal font */
187 /*      if (fonts[i].font != NULL)
188                 XFreeFont(display, fonts[i].font);*/
189         
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");
195                 }
196         }
197         }
198 }
199
200 #endif /* X11 */
201
202 /* default config file */
203 static char *current_config;
204
205 /* set to 1 if you want all text to be in uppercase */
206 static unsigned int stuff_in_upper_case;
207
208 /* Update interval */
209 static double update_interval;
210
211 /* Run how many times? */
212 static unsigned long total_run_times;
213
214 /* fork? */
215 static int fork_to_background;
216
217 static int cpu_avg_samples, net_avg_samples;
218
219 #ifdef X11
220
221 /* Always on bottom */
222 static int on_bottom;
223
224 /* Position on the screen */
225 static int text_alignment;
226 static int gap_x, gap_y;
227
228 /* border */
229 static int draw_borders;
230 static int stippled_borders;
231
232 static int draw_shades, draw_outline;
233
234 static int border_margin, border_width;
235
236 static long default_fg_color, default_bg_color, default_out_color;
237
238 /* create own window or draw stuff to root? */
239 static int set_transparent = 0;
240
241
242 #ifdef OWN_WINDOW
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;
248 #endif
249
250 static int minimum_width, minimum_height;
251 static int maximum_width;
252
253 /* UTF-8 */
254 int utf8_mode = 0;
255
256 #endif /* X11 */
257
258 /* no buffers in used memory? */
259 int no_buffers;
260
261 /* pad percentages to decimals? */
262 static int pad_percents = 0;
263
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;
267 #endif
268
269 /* Text that is shown */
270 static char original_text[] =
271     "$nodename - $sysname $kernel on $machine\n"
272     "$hr\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"
280     "$hr\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"
287     "$hr\n"
288 #ifdef SETI
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"
292 #endif
293 #ifdef MPD
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"
296 #endif
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}";
303
304 static char *text = original_text;
305
306 static int total_updates;
307
308 /* if-blocks */
309 static int blockdepth = 0;
310 static int if_jumped = 0;
311 static int blockstart[MAX_IF_BLOCK_DEPTH];
312
313 int check_mount(char *s)
314 {
315 #if defined(__linux__)
316         int ret = 0;
317         FILE *mtab = fopen("/etc/mtab", "r");
318         if (mtab) {
319                 char buf1[256], buf2[128];
320                 while (fgets(buf1, 256, mtab)) {
321                         sscanf(buf1, "%*s %128s", buf2);
322                         if (!strcmp(s, buf2)) {
323                                 ret = 1;
324                                 break;
325                         }
326                 }
327                 fclose(mtab);
328         } else {
329                 ERR("Could not open mtab");
330         }
331         return ret;
332 #elif defined(__FreeBSD__)
333         struct statfs *mntbuf;
334         int i, mntsize;
335
336         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
337         for (i = mntsize - 1; i >= 0; i--)
338                 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
339                         return 1;
340
341         return 0;
342 #endif
343 }
344
345
346 #ifdef X11
347 static inline int calc_text_width(const char *s, int l)
348 {
349 #ifdef XFT
350         if (use_xft) {
351                 XGlyphInfo gi;
352                 if (utf8_mode) {
353                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
354                 } else {
355                         XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
356                 }
357                 return gi.xOff;
358         } else
359 #endif
360         {
361                 return XTextWidth(fonts[selected_font].font, s, l);
362         }
363 }
364 #endif /* X11 */
365
366 /* formatted text to render on screen, generated in generate_text(),
367  * drawn in draw_stuff() */
368
369 static char text_buffer[TEXT_BUFFER_SIZE * 4];
370
371 /* special stuff in text_buffer */
372
373 #define SPECIAL_CHAR '\x01'
374
375 enum {
376         HORIZONTAL_LINE,
377         STIPPLED_HR,
378         BAR,
379         FG,
380         BG,
381         OUTLINE,
382         ALIGNR,
383         ALIGNC,
384         GRAPH,
385         OFFSET,
386         VOFFSET,
387         FONT,
388 };
389
390 static struct special_t {
391         int type;
392         short height;
393         short width;
394         long arg;
395         double *graph;
396         double graph_scale;
397         int graph_width;
398         int scaled;
399         short font_added;
400         unsigned long first_colour; // for graph gradient
401         unsigned long last_colour;
402 } specials[128];
403
404 static int special_count;
405 #ifdef X11
406 static int special_index;       /* used when drawing */
407 #endif /* X11 */
408
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? */
410
411 static struct special_t *new_special(char *buf, int t)
412 {
413         if (special_count >= 128)
414                 CRIT_ERR("too many special things in text");
415
416         buf[0] = SPECIAL_CHAR;
417         buf[1] = '\0';
418         specials[special_count].type = t;
419         return &specials[special_count++];
420 }
421
422 typedef struct tailstring_list {
423         char data[TEXT_BUFFER_SIZE];
424         struct tailstring_list *next;
425         struct tailstring_list *first;
426 } tailstring;
427
428 void addtail(tailstring ** head, char *data_in)
429 {
430         tailstring *tmp;
431         if ((tmp = malloc(sizeof(*tmp))) == NULL) {
432                 CRIT_ERR("malloc");
433         }
434         if (*head == NULL) {
435                 tmp->first = tmp;
436         } else {
437                 tmp->first = (*head)->first;
438         }
439         strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
440         tmp->next = *head;
441         *head = tmp;
442 }
443
444 void freetail(tailstring * head)
445 {
446         tailstring *tmp;
447         while (head != NULL) {
448                 tmp = head->next;
449                 free(head);
450                 head = tmp;
451         }
452 }
453
454 void freelasttail(tailstring * head)
455 {
456         tailstring * tmp = head;
457         while(tmp != NULL) {
458                 if (tmp->next == head->first) {
459                         tmp->next = NULL;
460                         break;
461                 }
462                 tmp = tmp->next;
463         }
464         free(head->first);
465         while(head != NULL && tmp != NULL) {
466                 head->first = tmp;
467                 head = head->next;
468         }
469 }
470
471 static void new_bar(char *buf, int w, int h, int usage)
472 {
473         struct special_t *s = new_special(buf, BAR);
474         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
475         s->width = w;
476         s->height = h;
477 }
478
479 static const char *scan_bar(const char *args, int *w, int *h)
480 {
481         *w = 0;                 /* zero width means all space that is available */
482         *h = 6;
483         /* bar's argument is either height or height,width */
484         if (args) {
485                 int n = 0;
486                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
487                         sscanf(args, "%d %n", h, &n);
488                 args += n;
489         }
490
491         return args;
492 }
493
494 static char *scan_font(const char *args)
495 {
496         if (args && sizeof(args) < 127) {
497                 return strdup(args);
498         }
499         else {
500                 ERR("font scan failed, lets hope it doesn't mess stuff up");
501         }
502         return NULL;
503 }
504
505 #ifdef X11
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);
511                 load_fonts();
512 //              set_font();
513                 selected_font = tmp;
514         }
515 }
516 #endif
517
518 inline void graph_append(struct special_t *graph, double f)
519 {
520         if (!graph->scaled && f > graph->graph_scale) {
521                 f = graph->graph_scale;
522         }
523         int i;
524         if (graph->scaled) {
525                 graph->graph_scale = 1;
526         }
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 */
532                 }
533         }
534 }
535
536 short colour_depth = 0;
537 void set_up_gradient();
538
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
542
543 /* adjust color values depending on color depth*/
544 static unsigned int adjust_colors(unsigned int color)
545 {
546         double r, g, b;
547         if (colour_depth == 0) {
548                 set_up_gradient();
549         }
550         if (colour_depth == 16) {
551                 r = (color & 0xff0000) >> 16;
552                 g = (color & 0xff00) >> 8;
553                 b =  color & 0xff;
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);
557         }
558         return color;
559 }
560
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)
562 {
563         struct special_t *s = new_special(buf, GRAPH);
564         s->width = w;
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
568                 } else {
569                         s->graph_width = MAX_GRAPH_DEPTH - 3;
570                 }
571                 s->graph = malloc(s->graph_width * sizeof(double));
572                 memset(s->graph, 0, s->graph_width * sizeof(double));
573                 s->graph_scale = 100;
574         }
575         s->height = h;
576         s->first_colour = adjust_colors(first_colour);
577         s->last_colour = adjust_colors(second_colour);
578         if (scale != 0) {
579                 s->scaled = 0;
580         } else {
581                 s->scaled = 1;
582         }
583         /*if (s->width) {
584                 s->graph_width = s->width - 3;  // subtract 3 for rectangle around
585         }*/
586         if (s->scaled) {
587                 s->graph_scale = 1;
588         } else {
589                 s->graph_scale = scale;
590         }
591         if (append) {
592                 graph_append(s, i);
593         }
594 }
595
596 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
597 {
598         *w = 0;                 /* zero width means all space that is available */
599         *h = 25;
600         *first_colour = 0;
601         *last_colour = 0;
602         /* graph's argument is either height or height,width */
603         if (args) {
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) {
606                                 *scale = 0;
607                                 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
608                                         *scale = 0;
609                                         if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
610                                                 *w = 0;
611                                 *h = 25;                        
612                                 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
613                                 *w = 0;
614                                 *h = 25;
615                                 *scale = 0;
616                                 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
617                                         *w = 0;
618                                         *h = 25;
619                                         if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
620                                                 *first_colour = 0;
621                                                 *last_colour = 0;
622                                                 *scale = 0;
623                                                 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
624                                         *first_colour = 0;
625                                         *last_colour = 0;
626                                         if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
627                                                 *first_colour = 0;
628                                                 *last_colour = 0;
629                                                 *scale = 0;
630                                                 if (sscanf(args, "%d,%d", h, w) < 2) {
631                                                         *first_colour = 0;
632                                                         *last_colour = 0;
633                                                         sscanf(args, "%*s %d,%d", h, w);
634         }}}}}}}}}}} // haha
635         return args;
636 }
637
638
639 static inline void new_hr(char *buf, int a)
640 {
641         new_special(buf, HORIZONTAL_LINE)->height = a;
642 }
643
644 static inline void new_stippled_hr(char *buf, int a, int b)
645 {
646         struct special_t *s = new_special(buf, STIPPLED_HR);
647         s->height = b;
648         s->arg = a;
649 }
650
651 static inline void new_fg(char *buf, long c)
652 {
653         new_special(buf, FG)->arg = c;
654 }
655
656 static inline void new_bg(char *buf, long c)
657 {
658         new_special(buf, BG)->arg = c;
659 }
660
661 static inline void new_outline(char *buf, long c)
662 {
663         new_special(buf, OUTLINE)->arg = c;
664 }
665
666 static inline void new_offset(char *buf, long c)
667 {
668         new_special(buf, OFFSET)->arg = c;
669 }
670
671 static inline void new_voffset(char *buf, long c)
672 {
673         new_special(buf, VOFFSET)->arg = c;
674 }
675
676 static inline void new_alignr(char *buf, long c)
677 {
678         new_special(buf, ALIGNR)->arg = c;
679 }
680
681 static inline void new_alignc(char *buf, long c)
682 {
683         new_special(buf, ALIGNC)->arg = c;
684 }
685
686 /* quite boring functions */
687
688 static inline void for_each_line(char *b, void (*f) (char *))
689 {
690         char *ps, *pe;
691
692         for (ps = b, pe = b; *pe; pe++) {
693                 if (*pe == '\n') {
694                         *pe = '\0';
695                         f(ps);
696                         *pe = '\n';
697                         ps = pe + 1;
698                 }
699         }
700
701         if (ps < pe)
702                 f(ps);
703 }
704
705 static void convert_escapes(char *buf)
706 {
707         char *p = buf, *s = buf;
708
709         while (*s) {
710                 if (*s == '\\') {
711                         s++;
712                         if (*s == 'n')
713                                 *p++ = '\n';
714                         else if (*s == '\\')
715                                 *p++ = '\\';
716                         s++;
717                 } else
718                         *p++ = *s++;
719         }
720         *p = '\0';
721 }
722
723 /* converts from bytes to human readable format (k, M, G, T) */
724 static void human_readable(long long a, char *buf, int size)
725 {
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);
729         }
730         else if (a >= 1024 * 1024 * 1024) {
731                 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
732         }
733         else if (a >= 1024 * 1024) {
734                 double m = (a / 1024) / 1024.0;
735                 if (m >= 100.0)
736                         snprintf(buf, size, "%.0fM", m);
737                 else
738                         snprintf(buf, size, "%.1fM", m);
739         } else if (a >= 1024)
740                 snprintf(buf, size, "%Ldk", a / (long long) 1024);
741         else
742                 snprintf(buf, size, "%Ld", a);
743 }
744
745 /* text handling */
746
747 enum text_object_type {
748         OBJ_acpiacadapter,
749         OBJ_adt746xcpu,
750         OBJ_adt746xfan,
751         OBJ_acpifan,
752         OBJ_addr,
753         OBJ_linkstatus,
754         OBJ_acpitemp,
755         OBJ_acpitempf,
756         OBJ_battery,
757         OBJ_buffers,
758         OBJ_cached,
759         OBJ_color,
760         OBJ_font,
761         OBJ_cpu,
762         OBJ_cpubar,
763         OBJ_cpugraph,
764         OBJ_diskio,
765         OBJ_diskiograph,
766         OBJ_downspeed,
767         OBJ_downspeedf,
768         OBJ_downspeedgraph,
769         OBJ_else,
770         OBJ_endif,
771         OBJ_exec,
772         OBJ_execi,
773         OBJ_texeci,
774         OBJ_execbar,
775         OBJ_execgraph,
776         OBJ_execibar,
777         OBJ_execigraph,
778         OBJ_freq,
779         OBJ_freq_g,
780         OBJ_freq_dyn,
781         OBJ_freq_dyn_g,
782         OBJ_fs_bar,
783         OBJ_fs_bar_free,
784         OBJ_fs_free,
785         OBJ_fs_free_perc,
786         OBJ_fs_size,
787         OBJ_fs_used,
788         OBJ_fs_used_perc,
789         OBJ_hr,
790         OBJ_offset,
791         OBJ_voffset,
792         OBJ_alignr,
793         OBJ_alignc,
794         OBJ_i2c,
795 #if defined(__linux__)
796         OBJ_i8k_version,
797         OBJ_i8k_bios,
798         OBJ_i8k_serial,
799         OBJ_i8k_cpu_temp,
800         OBJ_i8k_cpu_tempf,
801         OBJ_i8k_left_fan_status,
802         OBJ_i8k_right_fan_status,
803         OBJ_i8k_left_fan_rpm,
804         OBJ_i8k_right_fan_rpm,
805         OBJ_i8k_ac_status,      
806         OBJ_i8k_buttons_status,
807 #endif /* __linux__ */
808         OBJ_if_existing,
809         OBJ_if_mounted,
810         OBJ_if_running,
811         OBJ_top,
812         OBJ_top_mem,
813         OBJ_tail,
814         OBJ_head,
815         OBJ_kernel,
816         OBJ_loadavg,
817         OBJ_machine,
818         OBJ_mails,
819         OBJ_mem,
820         OBJ_membar,
821         OBJ_memgraph,
822         OBJ_memmax,
823         OBJ_memperc,
824         OBJ_mixer,
825         OBJ_mixerl,
826         OBJ_mixerr,
827         OBJ_mixerbar,
828         OBJ_mixerlbar,
829         OBJ_mixerrbar,
830         OBJ_new_mails,
831         OBJ_nodename,
832         OBJ_pre_exec,
833 #ifdef MLDONKEY
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,
844 #endif
845         OBJ_processes,
846         OBJ_running_processes,
847         OBJ_shadecolor,
848         OBJ_outlinecolor,
849         OBJ_stippled_hr,
850         OBJ_swap,
851         OBJ_swapbar,
852         OBJ_swapmax,
853         OBJ_swapperc,
854         OBJ_sysname,
855         OBJ_temp1,              /* i2c is used instead in these */
856         OBJ_temp2,
857         OBJ_text,
858         OBJ_time,
859         OBJ_utime,
860         OBJ_totaldown,
861         OBJ_totalup,
862         OBJ_updates,
863         OBJ_upspeed,
864         OBJ_upspeedf,
865         OBJ_upspeedgraph,
866         OBJ_uptime,
867         OBJ_uptime_short,
868 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
869         OBJ_apm_adapter,
870         OBJ_apm_battery_time,
871         OBJ_apm_battery_life,
872 #endif /* __FreeBSD__ */
873 #ifdef SETI
874         OBJ_seti_prog,
875         OBJ_seti_progbar,
876         OBJ_seti_credit,
877 #endif
878 #ifdef MPD
879         OBJ_mpd_title,
880         OBJ_mpd_artist,
881         OBJ_mpd_album,
882         OBJ_mpd_random,
883         OBJ_mpd_repeat,
884         OBJ_mpd_vol,
885         OBJ_mpd_bitrate,
886         OBJ_mpd_status,
887         OBJ_mpd_host,
888         OBJ_mpd_port,
889         OBJ_mpd_bar,
890         OBJ_mpd_elapsed,
891         OBJ_mpd_length,
892         OBJ_mpd_track,
893         OBJ_mpd_percent,
894 #endif
895 #ifdef TCP_PORT_MONITOR
896         OBJ_tcp_portmon,
897 #endif
898 };
899
900 struct text_object {
901         int type;
902         int a, b;
903         unsigned int c, d, e;
904         float f;
905         union {
906                 char *s;        /* some string */
907                 int i;          /* some integer */
908                 long l;         /* some other integer */
909                 struct net_stat *net;
910                 struct fs_stat *fs;
911                 unsigned char loadavg[3];
912                 //unsigned int diskio;
913                 unsigned int cpu_index;
914                 struct {
915                         struct fs_stat *fs;
916                         int w, h;
917                 } fsbar;        /* 3 */
918
919                 struct {
920                         int l;
921                         int w, h;
922                 } mixerbar;     /* 3 */
923
924                 struct {
925                         int fd;
926                         int arg;
927                         char devtype[256];
928                         char type[64];
929                 } i2c;          /* 2 */
930                 struct {
931                         int pos;
932                         char *s;
933                 } ifblock;
934                 struct {
935                         int num;
936                         int type;
937                 } top;
938
939                 struct {
940                         int wantedlines;
941                         int readlines;
942                         char *logfile;
943                         double last_update;
944                         float interval;
945                         char *buffer;
946                 } tail;
947
948                 struct {
949                         double last_update;
950                         float interval;
951                         char *cmd;
952                         char *buffer;
953                         double data;
954                 } execi;        /* 5 */
955
956                 struct {
957                         int a, b;
958                 } pair;         /* 2 */
959 #ifdef TCP_PORT_MONITOR
960                 struct {
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. */
965                } tcp_port_monitor;
966 #endif
967         } data;
968 };
969
970 static unsigned int text_object_count;
971 static struct text_object *text_objects;
972
973 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
974
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);
980         (void) pclose(fp);      
981         p2[n2] = '\0';
982         if (n2 && p2[n2 - 1] == '\n')
983                 p2[n2 - 1] = '\0';
984         
985         while (*p2) {
986                 if (*p2 == '\001')
987                         *p2 = ' ';
988                 p2++;
989         }
990         pthread_mutex_unlock( &mutex1 );
991         return NULL;
992 }
993
994 /* new_text_object() allocates a new zeroed text_object */
995 static struct text_object *new_text_object()
996 {
997         text_object_count++;
998         text_objects = (struct text_object *) realloc(text_objects,
999                                                       sizeof(struct
1000                                                              text_object) *
1001                                                       text_object_count);
1002         memset(&text_objects[text_object_count - 1], 0,
1003                sizeof(struct text_object));
1004
1005         return &text_objects[text_object_count - 1];
1006 }
1007
1008 #ifdef MLDONKEY
1009 void ml_cleanup()
1010 {
1011         if (mlconfig.mldonkey_hostname) {
1012                 free(mlconfig.mldonkey_hostname);
1013                 mlconfig.mldonkey_hostname = NULL;
1014         }
1015 }
1016 #endif
1017
1018 static void free_text_objects()
1019 {
1020         unsigned int i;
1021         for (i = 0; i < text_object_count; i++) {
1022                 switch (text_objects[i].type) {
1023                 case OBJ_acpitemp:
1024                         close(text_objects[i].data.i);
1025                         break;
1026                 case OBJ_acpitempf:
1027                         close(text_objects[i].data.i);
1028                         break;
1029                 case OBJ_i2c:
1030                         close(text_objects[i].data.i2c.fd);
1031                         break;
1032                 case OBJ_time:
1033                         free(text_objects[i].data.s);
1034                         break;
1035                 case OBJ_utime:
1036                 case OBJ_if_existing:
1037                 case OBJ_if_mounted:
1038                 case OBJ_if_running:
1039                         free(text_objects[i].data.ifblock.s);
1040                         break;
1041                 case OBJ_tail:
1042                         free(text_objects[i].data.tail.logfile);
1043                         free(text_objects[i].data.tail.buffer);
1044                         break;
1045                 case OBJ_text:
1046                 case OBJ_font:
1047                         free(text_objects[i].data.s);
1048                         break;
1049                 case OBJ_exec:
1050                         free(text_objects[i].data.s);
1051                         break;
1052                 case OBJ_execbar:
1053                         free(text_objects[i].data.s);
1054                         break;
1055                 case OBJ_execgraph:
1056                         free(text_objects[i].data.s);
1057                         break;
1058 /*              case OBJ_execibar:
1059                         free(text_objects[i].data.s);
1060                         break;
1061                 case OBJ_execigraph:
1062                         free(text_objects[i].data.s);
1063                         break;*/
1064 #ifdef MPD
1065                 case OBJ_mpd_title:
1066                 case OBJ_mpd_artist:
1067                 case OBJ_mpd_album:
1068                 case OBJ_mpd_random:
1069                 case OBJ_mpd_repeat:
1070                 case OBJ_mpd_track:
1071                 case OBJ_mpd_status:
1072                 case OBJ_mpd_host:
1073 #endif
1074                 case OBJ_pre_exec:
1075                 case OBJ_battery:
1076                         free(text_objects[i].data.s);
1077                         break;
1078
1079                 case OBJ_execi:
1080                         free(text_objects[i].data.execi.cmd);
1081                         free(text_objects[i].data.execi.buffer);
1082                         break;
1083                 case OBJ_texeci:
1084                         free(text_objects[i].data.execi.cmd);
1085                         free(text_objects[i].data.execi.buffer);
1086                         break;
1087                 case OBJ_top:
1088                         if (info.first_process) {
1089                                 free_all_processes();
1090                                 info.first_process = NULL;
1091                         }
1092                         break;
1093                 case OBJ_top_mem:
1094                         if (info.first_process) {
1095                                 free_all_processes();
1096                                 info.first_process = NULL;
1097                         }
1098                         break;
1099                 }
1100         }
1101 #ifdef MLDONKEY
1102         ml_cleanup();
1103 #endif /* MLDONKEY */
1104         free(text_objects);
1105         text_objects = NULL;
1106         text_object_count = 0;
1107 }
1108
1109 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1110 {
1111         char buf1[64];
1112         int n;
1113
1114         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1115                 *a = mixer_init(buf1);
1116                 (void) scan_bar(arg + n, w, h);
1117         } else {
1118                 *a = mixer_init(0);
1119                 (void) scan_bar(arg, w, h);
1120         }
1121 }
1122
1123 /* construct_text_object() creates a new text_object */
1124 static void construct_text_object(const char *s, const char *arg)
1125 {
1126         struct text_object *obj = new_text_object();
1127
1128 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
1129 #define END ; } } else
1130
1131 #ifdef X11      
1132 if (s[0] == '#') {
1133                 obj->type = OBJ_color;
1134                 obj->data.l = get_x11_color(s);
1135         } else
1136 #endif /* X11 */
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)
1140         END OBJ(freq, 0);
1141         END OBJ(freq_g, 0);
1142         END OBJ(freq_dyn, 0);
1143         END OBJ(freq_dyn_g, 0);
1144         END OBJ(acpifan, 0);
1145         END OBJ(battery, 0);
1146         char bat[64];
1147         if (arg)
1148                 sscanf(arg, "%63s", bat);
1149         else
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)
1168                 if (arg) {
1169                 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1170                         obj->data.cpu_index = atoi(&arg[3]);
1171                         arg += 4;
1172                 } else {obj->data.cpu_index = 0; }
1173                 } else {
1174                                 obj->data.cpu_index = 0;
1175                         }
1176         END OBJ(cpubar, INFO_CPU)
1177                 if (arg) {
1178                 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1179                         obj->data.cpu_index = atoi(&arg[3]);
1180                         arg += 4;
1181                 }
1182                 else {obj->data.cpu_index = 0;}
1183                 (void) scan_bar(arg, &obj->a, &obj->b);
1184                 } else {
1185                                 (void) scan_bar(arg, &obj->a, &obj->b);
1186                                 obj->data.cpu_index = 0;
1187                         }
1188         END OBJ(cpugraph, INFO_CPU)
1189                         if (arg) {
1190                 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1191                         obj->data.cpu_index = atoi(&arg[3]);
1192                         arg += 4;
1193                 }
1194                                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1195                         } else {
1196         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1197         obj->data.cpu_index = 0;
1198                         }
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);
1201         END OBJ(color, 0) 
1202 #ifdef X11
1203                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1204 #endif /* X11 */
1205         END
1206         OBJ(font, 0)
1207                         obj->data.s = scan_font(arg);
1208         END
1209                 OBJ(downspeed, INFO_NET) 
1210                 if(arg) {
1211                         obj->data.net = get_net_stat(arg);
1212                 }
1213                 else {
1214                         CRIT_ERR("downspeed needs argument");
1215                 }
1216         END OBJ(downspeedf, INFO_NET)
1217                 if(arg) {
1218                         obj->data.net = get_net_stat(arg);
1219                 }
1220                 else {
1221                         CRIT_ERR("downspeedf needs argument");
1222                 }
1223         END OBJ(downspeedgraph, INFO_NET)
1224                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1225         char buf[64];
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) {
1230                         obj->a = 0;
1231                         obj->b = 25;
1232                 }
1233         }
1234         END OBJ(
1235                        else
1236                        , 0)
1237         if (blockdepth) {
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;
1242         } else {
1243                 ERR("$else: no matching $if_*");
1244         }
1245         END OBJ(endif, 0)
1246         if (blockdepth) {
1247                 blockdepth--;
1248                 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
1249                     text_object_count;
1250         } else {
1251                 ERR("$endif: no matching $if_*");
1252         }
1253         END
1254 #ifdef HAVE_POPEN
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) {
1260                 char buf[256];
1261                 ERR("${execibar <interval> command}");
1262                 obj->type = OBJ_text;
1263                 snprintf(buf, 256, "${%s}", s);
1264                 obj->data.s = strdup(buf);
1265                 } else {
1266                         obj->data.execi.cmd = strdup(arg + n);
1267                     }
1268         END OBJ(execigraph, 0) unsigned int n;
1269         if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1270                 char buf[256];
1271                 ERR("${execigraph <interval> command}");
1272                 obj->type = OBJ_text;
1273                 snprintf(buf, 256, "${%s}", s);
1274                 obj->data.s = strdup(buf);
1275                     } else {
1276                             obj->data.execi.cmd = strdup(arg + n);
1277                     }
1278         END OBJ(execi, 0) unsigned int n;
1279
1280         if (!arg
1281             || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1282                 char buf[256];
1283                 ERR("${execi <interval> command}");
1284                 obj->type = OBJ_text;
1285                 snprintf(buf, 256, "${%s}", s);
1286                 obj->data.s = strdup(buf);
1287         } else {
1288                 obj->data.execi.cmd = strdup(arg + n);
1289                 obj->data.execi.buffer =
1290                     (char *) calloc(1, TEXT_BUFFER_SIZE);
1291         }
1292         END OBJ(texeci, 0) unsigned int n;
1293
1294         if (!arg
1295                     || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1296                 char buf[256];
1297                 ERR("${texeci <interval> command}");
1298                 obj->type = OBJ_text;
1299                 snprintf(buf, 256, "${%s}", s);
1300                 obj->data.s = strdup(buf);
1301                     } else {
1302                             obj->data.execi.cmd = strdup(arg + n);
1303                             obj->data.execi.buffer =
1304                                             (char *) calloc(1, TEXT_BUFFER_SIZE);
1305                     }
1306         END OBJ(pre_exec, 0) obj->type = OBJ_text;
1307         if (arg) {
1308                 FILE *fp = popen(arg, "r");
1309                 unsigned int n;
1310                 char buf[2048];
1311
1312                 n = fread(buf, 1, 2048, fp);
1313                 buf[n] = '\0';
1314
1315                 if (n && buf[n - 1] == '\n')
1316                         buf[n - 1] = '\0';
1317
1318                 (void) pclose(fp);
1319
1320                 obj->data.s = strdup(buf);
1321         } else
1322                 obj->data.s = strdup("");
1323         END
1324 #endif
1325             OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1326         arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1327         if (arg) {
1328                 while (isspace(*arg))
1329                         arg++;
1330                 if (*arg == '\0')
1331                         arg = "/";
1332         } else
1333                 arg = "/";
1334         obj->data.fsbar.fs = prepare_fs_stat(arg);
1335         END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1336         if (arg) {
1337                 unsigned int n;
1338                 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1339                         arg += n;
1340         } else
1341                 arg = "/";
1342         obj->data.fsbar.fs = prepare_fs_stat(arg);
1343         END OBJ(fs_free, INFO_FS) if (!arg)
1344                  arg = "/";
1345         obj->data.fs = prepare_fs_stat(arg);
1346         END OBJ(fs_used_perc, INFO_FS) if (!arg)
1347                  arg = "/";
1348         obj->data.fs = prepare_fs_stat(arg);
1349         END OBJ(fs_free_perc, INFO_FS) if (!arg)
1350                  arg = "/";
1351         obj->data.fs = prepare_fs_stat(arg);
1352         END OBJ(fs_size, INFO_FS) if (!arg)
1353                  arg = "/";
1354         obj->data.fs = prepare_fs_stat(arg);
1355         END OBJ(fs_used, INFO_FS) if (!arg)
1356                  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];
1362         int n;
1363
1364         if (!arg) {
1365                 ERR("i2c needs arguments");
1366                 obj->type = OBJ_text;
1367                 //obj->data.s = strdup("${i2c}");
1368                 return;
1369         }
1370
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
1373                  * default device */
1374                 sscanf(arg, "%63s %d", buf2, &n);
1375                 obj->data.i2c.fd =
1376                     open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1377                                     obj->data.i2c.devtype);
1378                 strncpy(obj->data.i2c.type, buf2, 63);
1379         } else {
1380                 obj->data.i2c.fd =
1381                     open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1382                                     obj->data.i2c.devtype);
1383                 strncpy(obj->data.i2c.type, buf2, 63);
1384         }
1385
1386         END OBJ(top, INFO_TOP)
1387         char buf[64];
1388         int n;
1389         if (!arg) {
1390                 ERR("top needs arguments");
1391                 obj->type = OBJ_text;
1392                 //obj->data.s = strdup("${top}");
1393                 return;
1394         }
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;
1404                 } else {
1405                         ERR("invalid arg for top");
1406                         return;
1407                 }
1408                 if (n < 1 || n > 10) {
1409                         CRIT_ERR("invalid arg for top");
1410                         return;
1411                 } else {
1412                         obj->data.top.num = n - 1;
1413                         top_cpu = 1;
1414                 }
1415         } else {
1416                 ERR("invalid args given for top");
1417                 return;
1418         }
1419         END OBJ(top_mem, INFO_TOP)
1420         char buf[64];
1421         int n;
1422         if (!arg) {
1423                 ERR("top_mem needs arguments");
1424                 obj->type = OBJ_text;
1425                 obj->data.s = strdup("${top_mem}");
1426                 return;
1427         }
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;
1437                 } else {
1438                         ERR("invalid arg for top");
1439                         return;
1440                 }
1441                 if (n < 1 || n > 10) {
1442                         CRIT_ERR("invalid arg for top");
1443                         return;
1444                 } else {
1445                         obj->data.top.num = n - 1;
1446                         top_mem = 1;
1447                 }
1448         } else {
1449                 ERR("invalid args given for top");
1450                 return;
1451         }
1452         END OBJ(addr, INFO_NET)
1453                 if(arg) {
1454                         obj->data.net = get_net_stat(arg);
1455                 }
1456                 else {
1457                         CRIT_ERR("addr needs argument");
1458                 }
1459         END OBJ(linkstatus, INFO_WIFI) 
1460                 if(arg) {
1461                         obj->data.net = get_net_stat(arg);
1462                 }
1463                 else {
1464                         CRIT_ERR("linkstatus needs argument");
1465                 }
1466         END OBJ(tail, 0)
1467         char buf[64];
1468         int n1, n2;
1469         if (!arg) {
1470                 ERR("tail needs arguments");
1471                 obj->type = OBJ_text;
1472                 obj->data.s = strdup("${tail}");
1473                 return;
1474         }
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");
1478                         return;
1479                 } else {
1480                         FILE *fp = NULL;
1481                         fp = fopen(buf, "r");
1482                         if (fp) {
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;
1489                                 fclose(fp);
1490                         } else {
1491                                 //fclose (fp);
1492                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1493                         }
1494                 }
1495         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1496                 if (n1 < 1 || n1 > 30) {
1497                         CRIT_ERR
1498                             ("invalid arg for tail, number of lines must be between 1 and 30");
1499                         return;
1500                 } else if (n2 < 1 || n2 < update_interval) {
1501                         CRIT_ERR
1502                             ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1503                         return;
1504                 } else {
1505                         FILE *fp;
1506                         fp = fopen(buf, "r");
1507                         if (fp != NULL) {
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;
1513                                 fclose(fp);
1514                         } else {
1515                                 //fclose (fp);
1516                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1517                         }
1518                 }
1519         }
1520
1521         else {
1522                 ERR("invalid args given for tail");
1523                 return;
1524         }
1525         obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1526         END OBJ(head, 0)
1527                         char buf[64];
1528         int n1, n2;
1529         if (!arg) {
1530                 ERR("head needs arguments");
1531                 obj->type = OBJ_text;
1532                 obj->data.s = strdup("${head}");
1533                 return;
1534         }
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");
1538                         return;
1539                 } else {
1540                         FILE *fp;
1541                         fp = fopen(buf, "r");
1542                         if (fp != NULL) {
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;
1549                                 fclose(fp);
1550                         } else {
1551                                 //fclose (fp);
1552                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1553                         }
1554                 }
1555         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1556                 if (n1 < 1 || n1 > 30) {
1557                         CRIT_ERR
1558                                         ("invalid arg for head, number of lines must be between 1 and 30");
1559                         return;
1560                 } else if (n2 < 1 || n2 < update_interval) {
1561                         CRIT_ERR
1562                                         ("invalid arg for head, interval must be greater than 0 and Conky's interval");
1563                         return;
1564                 } else {
1565                         FILE *fp;
1566                         fp = fopen(buf, "r");
1567                         if (fp != NULL) {
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;
1573                                 fclose(fp);
1574                         } else {
1575                                 //fclose (fp);
1576                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1577                         }
1578                 }
1579         }
1580
1581         else {
1582                 ERR("invalid args given for head");
1583                 return;
1584         }
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;
1587         if (arg) {
1588                 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1589                 if (r >= 3 && (c < 1 || c > 3))
1590                         r--;
1591                 if (r >= 2 && (b < 1 || b > 3))
1592                         r--, b = c;
1593                 if (r >= 1 && (a < 1 || a > 3))
1594                         r--, a = b, b = c;
1595         }
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");
1602         }
1603         if (!arg) {
1604                 ERR("if_existing needs an argument");
1605                 obj->data.ifblock.s = 0;
1606         } else
1607                 obj->data.ifblock.s = strdup(arg);
1608         blockstart[blockdepth] = text_object_count;
1609         obj->data.ifblock.pos = text_object_count + 2;
1610         blockdepth++;
1611         END OBJ(if_mounted, 0)
1612         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1613                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1614         }
1615         if (!arg) {
1616                 ERR("if_mounted needs an argument");
1617                 obj->data.ifblock.s = 0;
1618         } else
1619                 obj->data.ifblock.s = strdup(arg);
1620         blockstart[blockdepth] = text_object_count;
1621         obj->data.ifblock.pos = text_object_count + 2;
1622         blockdepth++;
1623         END OBJ(if_running, 0)
1624         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1625                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1626         }
1627         if (arg) {
1628                 char buf[256];
1629                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1630                 obj->data.ifblock.s = strdup(buf);
1631         } else {
1632                 ERR("if_running needs an argument");
1633                 obj->data.ifblock.s = 0;
1634         }
1635         blockstart[blockdepth] = text_object_count;
1636         obj->data.ifblock.pos = text_object_count + 2;
1637         blockdepth++;
1638         END OBJ(kernel, 0)
1639         END OBJ(machine, 0)
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);
1660         END
1661 #ifdef MLDONKEY
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
1672 #endif
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)
1678 #ifdef X11
1679             obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1680 #endif /* X11 */
1681         END OBJ(outlinecolor, 0)
1682 #ifdef X11
1683             obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1684 #endif /* X11 */
1685         END OBJ(stippled_hr, 0)
1686 #ifdef X11
1687 int a = stippled_borders, b = 1;
1688         if (arg) {
1689                 if (sscanf(arg, "%d %d", &a, &b) != 2)
1690                         sscanf(arg, "%d", &b);
1691         }
1692         if (a <= 0)
1693                 a = 1;
1694         obj->data.pair.a = a;
1695         obj->data.pair.b = b;
1696 #endif /* X11 */
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;
1703         obj->data.i2c.fd =
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;
1707         obj->data.i2c.fd =
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)
1713                 if(arg) {
1714                         obj->data.net = get_net_stat(arg);
1715                 }
1716                 else {
1717                         CRIT_ERR("totaldown needs argument");
1718                 }
1719         END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1720                 if(arg) {
1721                         obj->data.net = get_net_stat(arg);
1722                 }
1723                 else {
1724                         CRIT_ERR("totalup needs argument");
1725                 }
1726         END OBJ(updates, 0)
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)
1730                 if(arg) {
1731                         obj->data.net = get_net_stat(arg);
1732                 }
1733                 else {
1734                         CRIT_ERR("upspeed needs argument");
1735                 }
1736         END OBJ(upspeedf, INFO_NET) 
1737                 if(arg) {
1738                         obj->data.net = get_net_stat(arg);
1739                 }
1740                 else {
1741                         CRIT_ERR("upspeedf needs argument");
1742                 }
1743
1744         END OBJ(upspeedgraph, INFO_NET)
1745                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1746         char buf[64];
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) {
1751                         obj->a = 0;
1752                         obj->b = 25;
1753                 }
1754         }
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__ */
1762 #ifdef SETI
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
1766 #endif
1767 #ifdef MPD
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,
1778                                                                INFO_MPD)
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);
1782         END
1783 #endif
1784 #ifdef TCP_PORT_MONITOR
1785         OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR) 
1786                 int argc, port_begin, port_end, item, connection_index;
1787                 char itembuf[32];
1788                 memset(itembuf,0,sizeof(itembuf));
1789                 connection_index=0;
1790                 /* massive argument checking */
1791                 if (!arg) {
1792                         CRIT_ERR("tcp_portmon: needs arguments");
1793                 }
1794                 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
1795                 if ( (argc != 3) && (argc != 4) ) 
1796                 {
1797                         CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
1798                 }
1799                 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
1800                 {
1801                         CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
1802                 }
1803                 if ( port_begin > port_end )
1804                 {
1805                         CRIT_ERR("tcp_portmon: starting port must be <= ending port");
1806                 }
1807                 if ( strncmp(itembuf,"count",31) == 0 )
1808                         item=COUNT;
1809                 else if ( strncmp(itembuf,"rip",31) == 0 )
1810                         item=REMOTEIP;
1811                 else if ( strncmp(itembuf,"rhost",31) == 0 )
1812                         item=REMOTEHOST;
1813                 else if ( strncmp(itembuf,"rport",31) == 0 )
1814                         item=REMOTEPORT;
1815                 else if ( strncmp(itembuf,"lip",31) == 0 )
1816                         item=LOCALIP;
1817                 else if ( strncmp(itembuf,"lhost",31) == 0 )
1818                         item=LOCALHOST;
1819                 else if ( strncmp(itembuf,"lport",31) == 0 )
1820                         item=LOCALPORT;
1821                 else if ( strncmp(itembuf,"lservice",31) == 0 )
1822                         item=LOCALSERVICE;
1823                 else
1824                 {
1825                         CRIT_ERR("tcp_portmon: invalid item specified"); 
1826                 }
1827                 if ( (argc==3) && (item!=COUNT) )
1828                 {
1829                         CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
1830                 }
1831                 if ( (argc==4) && (connection_index<0) )
1832                 {
1833                         CRIT_ERR("tcp_portmon: connection index must be non-negative");
1834                 }
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;
1840
1841                 /* if the port monitor collection hasn't been created, we must create it */
1842                 if ( !info.p_tcp_port_monitor_collection )
1843                 {
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 )
1847                         {
1848                                 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
1849                         }
1850                 }
1851
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 )
1854                 {
1855                         tcp_port_monitor_t * p_monitor = 
1856                                 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
1857                         if ( !p_monitor )
1858                         {
1859                                 CRIT_ERR("tcp_portmon: unable to create port monitor");
1860                         }
1861                         /* add the newly created monitor to the collection */
1862                         if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
1863                                                                       p_monitor ) != 0 )
1864                         {
1865                                 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
1866                         }
1867                 }
1868         END
1869 #endif
1870         {
1871                 char buf[256];
1872                 ERR("unknown variable %s", s);
1873                 obj->type = OBJ_text;
1874                 snprintf(buf, 256, "${%s}", s);
1875                 obj->data.s = strdup(buf);
1876         }
1877 #undef OBJ
1878 }
1879
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)
1883 {
1884         struct text_object *obj;
1885
1886         if (s == NULL || *s == '\0')
1887                 return;
1888
1889         obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1890
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);
1896         } else {
1897                 /* append */
1898                 obj->data.s = (char *) realloc(obj->data.s,
1899                                                strlen(obj->data.s) +
1900                                                strlen(s) + 1);
1901                 strcat(obj->data.s, s);
1902         }
1903 }
1904
1905 static void extract_variable_text(const char *p)
1906 {
1907         const char *s = p;
1908
1909         free_text_objects();
1910
1911         while (*p) {
1912                 if (*p == '$') {
1913                         *(char *) p = '\0';
1914                         append_text(s);
1915                         *(char *) p = '$';
1916                         p++;
1917                         s = p;
1918
1919                         if (*p != '$') {
1920                                 char buf[256];
1921                                 const char *var;
1922                                 unsigned int len;
1923
1924                                 /* variable is either $foo or ${foo} */
1925                                 if (*p == '{') {
1926                                         p++;
1927                                         s = p;
1928                                         while (*p && *p != '}')
1929                                                 p++;
1930                                 } else {
1931                                         s = p;
1932                                         if (*p == '#')
1933                                                 p++;
1934                                         while (*p && (isalnum((int) *p)
1935                                                       || *p == '_'))
1936                                                 p++;
1937                                 }
1938
1939                                 /* copy variable to buffer */
1940                                 len = (p - s > 255) ? 255 : (p - s);
1941                                 strncpy(buf, s, len);
1942                                 buf[len] = '\0';
1943
1944                                 if (*p == '}')
1945                                         p++;
1946                                 s = p;
1947
1948                                 var = getenv(buf);
1949
1950                                 /* if variable wasn't found from environment, use some special */
1951                                 if (!var) {
1952                                         char *p;
1953                                         char *arg = 0;
1954
1955                                         /* split arg */
1956                                         if (strchr(buf, ' ')) {
1957                                                 arg = strchr(buf, ' ');
1958                                                 *arg = '\0';
1959                                                 arg++;
1960                                                 while (isspace((int) *arg))
1961                                                         arg++;
1962                                                 if (!*arg)
1963                                                         arg = 0;
1964                                         }
1965
1966                                         /* lowercase variable name */
1967                                         p = buf;
1968                                         while (*p) {
1969                                                 *p = tolower(*p);
1970                                                 p++;
1971                                         }
1972
1973                                         construct_text_object(buf, arg);
1974                                 }
1975                                 continue;
1976                         } else
1977                                 append_text("$");
1978                 }
1979
1980                 p++;
1981         }
1982         append_text(s);
1983         if (blockdepth) {
1984                 ERR("one or more $endif's are missing");
1985         }
1986 }
1987
1988 double current_update_time, last_update_time;
1989
1990 static void generate_text()
1991 {
1992         unsigned int i, n;
1993         struct information *cur = &info;
1994         char *p;
1995
1996         special_count = 0;
1997
1998         /* update info */
1999
2000         current_update_time = get_time();
2001
2002         update_stuff(cur);
2003
2004         /* generate text */
2005
2006         n = TEXT_BUFFER_SIZE * 4 - 2;
2007         p = text_buffer;
2008
2009         for (i = 0; i < text_object_count; i++) {
2010                 struct text_object *obj = &text_objects[i];
2011
2012 #define OBJ(a) break; case OBJ_##a:
2013
2014                 switch (obj->type) {
2015                 default:
2016                         {
2017                                 ERR("not implemented obj type %d",
2018                                     obj->type);
2019                         }
2020                         OBJ(acpitemp) {
2021                                 /* does anyone have decimals in acpi temperature? */
2022                                 if (!use_spacer)
2023                                         snprintf(p, n, "%d", (int)
2024                                                         get_acpi_temperature(obj->
2025                                                                         data.
2026                                                                         i));
2027                                 else
2028                                         snprintf(p, 5, "%d    ", (int)
2029                                                         get_acpi_temperature(obj->
2030                                                                         data.
2031                                                                         i));
2032                         }
2033                         OBJ(acpitempf) {
2034                                 /* does anyone have decimals in acpi temperature? */
2035                                 if (!use_spacer)
2036                                         snprintf(p, n, "%d", (int)
2037                                                         ((get_acpi_temperature(obj->
2038                                                                         data.
2039                                                                         i)+ 40) * 9.0 / 5 - 40));
2040                                 else
2041                                         snprintf(p, 5, "%d    ", (int)
2042                                                         ((get_acpi_temperature(obj->
2043                                                                         data.
2044                                                                         i)+ 40) * 9.0 / 5 - 40));
2045                         }
2046                         OBJ(freq) {
2047                                 get_freq(p, n, "%.0f", 1); /* pk */
2048                         }
2049                         OBJ(freq_g) {
2050                                 get_freq(p, n, "%'.2f", 1000); /* pk */
2051                         }
2052                         OBJ(freq_dyn) {
2053                                 get_freq_dynamic(p, n, "%.0f", 1 ); /* pk */
2054                         }
2055                         OBJ(freq_dyn_g) {
2056                                 get_freq_dynamic(p, n, "%'.2f", 1000); /* pk */
2057                         }
2058                         OBJ(adt746xcpu) {
2059                                 get_adt746x_cpu(p, n); /* pk */
2060                         }
2061                         OBJ(adt746xfan) {
2062                                 get_adt746x_fan(p, n); /* pk */
2063                         }
2064                         OBJ(acpifan) {
2065                                 get_acpi_fan(p, n);  /* pk */
2066                         }
2067                         OBJ(acpiacadapter) {
2068                                 get_acpi_ac_adapter(p, n); /* pk */
2069                         }
2070                         OBJ(battery) {
2071                                 get_battery_stuff(p, n, obj->data.s);
2072                         }
2073                         OBJ(buffers) {
2074                                 human_readable(cur->buffers * 1024, p,
2075                                                255);
2076                         }
2077                         OBJ(cached) {
2078                                 human_readable(cur->cached * 1024, p, 255);
2079                         }
2080                         OBJ(cpu) {
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!");
2084                                 }
2085                                 if (!use_spacer)
2086                                         snprintf(p, n, "%*d", pad_percents,
2087                                                 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2088                                                         100.0));
2089                                 else
2090                                         snprintf(p, 4, "%*d    ",
2091                                                  pad_percents,
2092                                                  (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2093                                                         100.0));
2094                         }
2095                         OBJ(cpubar) {
2096                                 new_bar(p, obj->a,
2097                                         obj->b,
2098                                         (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
2099                         }
2100                         OBJ(cpugraph) {
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] *
2104                                                           100), 100, 1);
2105                         }
2106                         OBJ(color) {
2107                                 new_fg(p, obj->data.l);
2108                         }
2109 #if defined(__linux__)
2110                         OBJ(i8k_version) {
2111                                 snprintf(p, n, "%s", i8k.version);
2112                         }
2113                         OBJ(i8k_bios) {
2114                                 snprintf(p, n, "%s", i8k.bios);
2115                         }
2116                         OBJ(i8k_serial) { 
2117                                 snprintf(p, n, "%s", i8k.serial);
2118                         }
2119                         OBJ(i8k_cpu_temp) { 
2120                                 snprintf(p, n, "%s", i8k.cpu_temp);
2121                         }
2122                         OBJ(i8k_cpu_tempf) { 
2123                                 int cpu_temp;
2124                                 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
2125                                 snprintf(p, n, "%.1f", cpu_temp*(9.0/5.0)+32.0);
2126                         }
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");
2136                                 }
2137
2138                         }
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");
2148                                 }
2149                         }
2150                         OBJ(i8k_left_fan_rpm) { 
2151                                 snprintf(p, n, "%s", i8k.left_fan_rpm);
2152                         }
2153                         OBJ(i8k_right_fan_rpm) { 
2154                                 snprintf(p, n, "%s", i8k.right_fan_rpm);
2155                         }
2156                         OBJ(i8k_ac_status) { 
2157                                 int 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");
2165                                 }
2166                         }
2167                         OBJ(i8k_buttons_status) {
2168                                 snprintf(p, n, "%s", i8k.buttons_status); 
2169
2170                         }
2171 #endif /* __linux__ */
2172
2173 #ifdef X11
2174                         OBJ(font) {
2175                                 new_font(p, obj->data.s);
2176                         }
2177 #endif /* X11 */
2178                         OBJ(diskio) {
2179                                 if (!use_spacer) {
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);
2188                                         } else {
2189                                                 snprintf(p, n, "%d", diskio_value);
2190                                         }
2191                                 } else {
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);
2200                                         } else {
2201                                                 snprintf(p, 6, "%d     ", diskio_value);
2202                                         }
2203                                 }
2204                         }
2205                         OBJ(diskiograph) {
2206                                 new_graph(p, obj->a,
2207                                           obj->b, obj->c, obj->d,
2208                                           diskio_value, obj->e, 1);
2209                         }
2210         
2211                         OBJ(downspeed) {
2212                                 if (!use_spacer) {
2213                                         snprintf(p, n, "%d",
2214                                                  (int) (obj->data.net->
2215                                                         recv_speed /
2216                                                         1024));
2217                                 } else
2218                                         snprintf(p, 6, "%d     ",
2219                                                  (int) (obj->data.net->
2220                                                         recv_speed /
2221                                                         1024));
2222                         }
2223                         OBJ(downspeedf) {
2224                                 if (!use_spacer)
2225                                         snprintf(p, n, "%.1f",
2226                                                  obj->data.net->
2227                                                  recv_speed / 1024.0);
2228                                 else
2229                                         snprintf(p, 8, "%.1f       ",
2230                                                  obj->data.net->
2231                                                  recv_speed / 1024.0);
2232                         }
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);
2239                         }
2240                         OBJ(
2241                                    else
2242                         ) {
2243                                 if (!if_jumped) {
2244                                         i = obj->data.ifblock.pos - 2;
2245                                 } else {
2246                                         if_jumped = 0;
2247                                 }
2248                         }
2249                         OBJ(endif) {
2250                                 if_jumped = 0;
2251                         }
2252 #ifdef HAVE_POPEN
2253                         OBJ(addr) {
2254                                 snprintf(p, n, "%u.%u.%u.%u",
2255                                          obj->data.net->addr.
2256                                          sa_data[2] & 255,
2257                                          obj->data.net->addr.
2258                                          sa_data[3] & 255,
2259                                          obj->data.net->addr.
2260                                          sa_data[4] & 255,
2261                                          obj->data.net->addr.
2262                                          sa_data[5] & 255);
2263
2264                         }
2265                         OBJ(linkstatus) {
2266                                 snprintf(p, n, "%d",
2267                                          obj->data.net->linkstatus);
2268                         }
2269
2270                         OBJ(exec) {
2271                                 char *p2 = p;
2272                                 FILE *fp = popen(obj->data.s, "r");
2273                                 int n2 = fread(p, 1, n, fp);
2274                                 (void) pclose(fp);
2275
2276                                 p[n2] = '\0';
2277                                 if (n2 && p[n2 - 1] == '\n')
2278                                         p[n2 - 1] = '\0';
2279
2280                                 while (*p2) {
2281                                         if (*p2 == '\001')
2282                                                 *p2 = ' ';
2283                                         p2++;
2284                                 }
2285                         }
2286                         OBJ(execbar) {
2287                                 char *p2 = p;
2288                                 FILE *fp = popen(obj->data.s, "r");
2289                                 int n2 = fread(p, 1, n, fp);
2290                                 (void) pclose(fp);
2291
2292                                 p[n2] = '\0';
2293                                 if (n2 && p[n2 - 1] == '\n')
2294                                         p[n2 - 1] = '\0';
2295
2296                                 while (*p2) {
2297                                         if (*p2 == '\001')
2298                                                 *p2 = ' ';
2299                                         p2++;
2300                                 }
2301                                 double barnum;
2302                                 if (sscanf(p, "%lf", &barnum) == 0) {
2303                                         ERR("reading execbar value failed (perhaps it's not the correct format?)");
2304                                 }
2305                                 if (barnum > 100 || barnum < 0) {
2306                                         ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
2307                                 } else {
2308                                         barnum = barnum / 100.0;
2309                                         new_bar(p, 0, 4, (int) (barnum * 255.0));
2310                                 }
2311
2312                         }
2313                         OBJ(execgraph) {
2314                                 char *p2 = p;
2315                                 FILE *fp = popen(obj->data.s, "r");
2316                                 int n2 = fread(p, 1, n, fp);
2317                                 (void) pclose(fp);
2318
2319                                 p[n2] = '\0';
2320                                 if (n2 && p[n2 - 1] == '\n')
2321                                         p[n2 - 1] = '\0';
2322
2323                                 while (*p2) {
2324                                         if (*p2 == '\001')
2325                                                 *p2 = ' ';
2326                                         p2++;
2327                                 }
2328                                 double barnum;
2329                                 if (sscanf(p, "%lf", &barnum) == 0) {
2330                                         ERR("reading execgraph value failed (perhaps it's not the correct format?)");
2331                                 }
2332                                 if (barnum > 100 || barnum < 0) {
2333                                         ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
2334                                 } else {
2335                                         new_graph(p, 0,
2336                                         25, obj->c, obj->d, (int) (barnum), obj->e, 1);
2337                                 }
2338
2339                         }
2340                         OBJ(execibar) {
2341                                 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2342                                         new_bar(p, 0, 4, (int) obj->f);
2343                                 } else {
2344                                         char *p2 = p;
2345                                         FILE *fp = popen(obj->data.execi.cmd, "r");
2346                                         int n2 = fread(p, 1, n, fp);
2347                                         (void) pclose(fp);
2348                                         p[n2] = '\0';
2349                                         if (n2 && p[n2 - 1] == '\n')
2350                                                 p[n2 - 1] = '\0';
2351
2352                                         while (*p2) {
2353                                                 if (*p2 == '\001')
2354                                                         *p2 = ' ';
2355                                                 p2++;
2356                                         }
2357                                         float barnum;
2358                                         if (sscanf(p, "%f", &barnum) == 0) {
2359                                                 ERR("reading execibar value failed (perhaps it's not the correct format?)");
2360                                         }
2361                                         if (barnum > 100 || barnum < 0) {
2362                                                 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
2363                                         } else {
2364                                                 obj->f = 255 * barnum / 100.0;
2365                                                 new_bar(p, 0, 4, (int) obj->f);
2366                                         }
2367                                         obj->data.execi.last_update =
2368                                                         current_update_time;
2369                                 }
2370                         }
2371                         OBJ(execigraph) {
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);
2374                                 } else {
2375                                         char *p2 = p;
2376                                         FILE *fp = popen(obj->data.execi.cmd, "r");
2377                                         int n2 = fread(p, 1, n, fp);
2378                                         (void) pclose(fp);
2379                                         p[n2] = '\0';
2380                                         if (n2 && p[n2 - 1] == '\n')
2381                                                 p[n2 - 1] = '\0';
2382
2383                                         while (*p2) {
2384                                                 if (*p2 == '\001')
2385                                                         *p2 = ' ';
2386                                                 p2++;
2387                                         }
2388                                         float barnum;
2389                                         if (sscanf(p, "%f", &barnum) == 0) {
2390                                                 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
2391                                         }
2392                                         if (barnum > 100 || barnum < 0) {
2393                                                 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
2394                                         } else {
2395                                                 obj->f = barnum;
2396                                                 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
2397                                         }
2398                                         obj->data.execi.last_update = current_update_time;
2399         
2400                                 }
2401
2402                         }
2403                         OBJ(execi) {
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);
2409                                 } else {
2410                                         char *p2 = obj->data.execi.buffer;
2411                                         FILE *fp =
2412                                             popen(obj->data.execi.cmd,
2413                                                   "r");
2414                                         int n2 =
2415                                             fread(p2, 1, TEXT_BUFFER_SIZE,
2416                                                   fp);
2417                                         (void) pclose(fp);
2418
2419                                         p2[n2] = '\0';
2420                                         if (n2 && p2[n2 - 1] == '\n')
2421                                                 p2[n2 - 1] = '\0';
2422
2423                                         while (*p2) {
2424                                                 if (*p2 == '\001')
2425                                                         *p2 = ' ';
2426                                                 p2++;
2427                                         }
2428
2429                                         snprintf(p, n, "%s",
2430                                                  obj->data.execi.buffer);
2431
2432                                         obj->data.execi.last_update =
2433                                             current_update_time;
2434                                 }
2435                         }
2436                         OBJ(texeci) {
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);
2440                                 } else {
2441                                         static pthread_t execthread;
2442                                         if (!running) {
2443                                                 running = 1;
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 );
2448                                         } else {
2449                                                 pthread_join( execthread, NULL);
2450                                                 running = 0;
2451                                         }
2452                                         snprintf(p, n, "%s", obj->data.execi.buffer);
2453                                 }
2454                         }
2455 #endif
2456                         OBJ(fs_bar) {
2457                                 if (obj->data.fs != NULL) {
2458                                         if (obj->data.fs->size == 0)
2459                                                 new_bar(p,
2460                                                         obj->data.fsbar.w,
2461                                                         obj->data.fsbar.h,
2462                                                         255);
2463                                         else
2464                                                 new_bar(p,
2465                                                         obj->data.fsbar.w,
2466                                                         obj->data.fsbar.h,
2467                                                         (int) (255 -
2468                                                                obj->data.
2469                                                                fsbar.fs->
2470                                                                avail *
2471                                                                255 /
2472                                                                obj->data.
2473                                                                fs->size));
2474                                 }
2475                         }
2476                         OBJ(fs_free) {
2477                                 if (obj->data.fs != NULL)
2478                                         human_readable(obj->data.fs->avail,
2479                                                        p, 255);
2480                         }
2481                         OBJ(fs_free_perc) {
2482                                 if (obj->data.fs != NULL) {
2483                                         if (obj->data.fs->size)
2484                                                 snprintf(p, n, "%*d",
2485                                                          pad_percents,
2486                                                          (int) ((obj->data.
2487                                                                  fs->
2488                                                                  avail *
2489                                                                  100) /
2490                                                                 obj->data.
2491                                                                 fs->size));
2492                                         else
2493                                                 snprintf(p, n, "0");
2494                                 }
2495                         }
2496                         OBJ(fs_size) {
2497                                 if (obj->data.fs != NULL)
2498                                         human_readable(obj->data.fs->size,
2499                                                        p, 255);
2500                         }
2501                         OBJ(fs_used) {
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),
2505                                                        p, 255);
2506                         }
2507                         OBJ(fs_bar_free) {
2508                                 if (obj->data.fs != NULL) {
2509                                         if (obj->data.fs->size == 0)
2510                                                 new_bar(p,
2511                                                         obj->data.fsbar.w,
2512                                                         obj->data.fsbar.h,
2513                                                         255);
2514                                         else
2515                                                 new_bar(p,
2516                                                         obj->data.fsbar.w,
2517                                                         obj->data.fsbar.h,
2518                                                         (int) (obj->data.
2519                                                                fsbar.fs->
2520                                                                avail *
2521                                                                255 /
2522                                                                obj->data.
2523                                                                fs->size));
2524                                 }
2525                         }
2526                         OBJ(fs_used_perc) {
2527                                 if (obj->data.fs != NULL) {
2528                                         if (obj->data.fs->size)
2529                                                 snprintf(p, 4, "%d",
2530                                                          100 - ((int)
2531                                                                 ((obj->
2532                                                                   data.fs->
2533                                                                   avail *
2534                                                                   100) /
2535                                                                  obj->data.
2536                                                                  fs->
2537                                                                  size)));
2538                                         else
2539                                                 snprintf(p, n, "0");
2540                                 }
2541                         }
2542                         OBJ(loadavg) {
2543                                 float *v = info.loadavg;
2544
2545                                 if (obj->data.loadavg[2])
2546                                         snprintf(p, n, "%.2f %.2f %.2f",
2547                                                  v[obj->data.loadavg[0] -
2548                                                    1],
2549                                                  v[obj->data.loadavg[1] -
2550                                                    1],
2551                                                  v[obj->data.loadavg[2] -
2552                                                    1]);
2553                                 else if (obj->data.loadavg[1])
2554                                         snprintf(p, n, "%.2f %.2f",
2555                                                  v[obj->data.loadavg[0] -
2556                                                    1],
2557                                                  v[obj->data.loadavg[1] -
2558                                                    1]);
2559                                 else if (obj->data.loadavg[0])
2560                                         snprintf(p, n, "%.2f",
2561                                                  v[obj->data.loadavg[0] -
2562                                                    1]);
2563                         }
2564                         OBJ(hr) {
2565                                 new_hr(p, obj->data.i);
2566                         }
2567                         OBJ(offset) {
2568                                 new_offset(p, obj->data.i);
2569                         }
2570                         OBJ(voffset) {
2571                                 new_voffset(p, obj->data.i);
2572                         }
2573                         OBJ(i2c) {
2574                                 double r;
2575
2576                                 r = get_i2c_info(&obj->data.i2c.fd,
2577                                                  obj->data.i2c.arg,
2578                                                  obj->data.i2c.devtype,
2579                                                  obj->data.i2c.type);
2580
2581                                 if (r >= 100.0 || r == 0)
2582                                         snprintf(p, n, "%d", (int) r);
2583                                 else
2584                                         snprintf(p, n, "%.1f", r);
2585                         }
2586                         OBJ(alignr) {
2587                                 new_alignr(p, obj->data.i);
2588                         }
2589                         OBJ(alignc) {
2590                                 new_alignc(p, obj->data.i);
2591                         }
2592                         OBJ(if_existing) {
2593                                 struct stat tmp;
2594                                 if ((obj->data.ifblock.s)
2595                                     && (stat(obj->data.ifblock.s, &tmp) ==
2596                                         -1)) {
2597                                         i = obj->data.ifblock.pos - 2;
2598                                         if_jumped = 1;
2599                                 } else
2600                                         if_jumped = 0;
2601                         }
2602                         OBJ(if_mounted) {
2603                                 if ((obj->data.ifblock.s)
2604                                     && (!check_mount(obj->data.ifblock.s))) {
2605                                         i = obj->data.ifblock.pos - 2;
2606                                         if_jumped = 1;
2607                                 } else
2608                                         if_jumped = 0;
2609                         }
2610                         OBJ(if_running) {
2611                                 if ((obj->data.ifblock.s)
2612                                     && system(obj->data.ifblock.s)) {
2613                                         i = obj->data.ifblock.pos - 2;
2614                                         if_jumped = 1;
2615                                 } else
2616                                         if_jumped = 0;
2617                         }
2618                         OBJ(kernel) {
2619                                 snprintf(p, n, "%s", cur->uname_s.release);
2620                         }
2621                         OBJ(machine) {
2622                                 snprintf(p, n, "%s", cur->uname_s.machine);
2623                         }
2624
2625                         /* memory stuff */
2626                         OBJ(mem) {
2627                                 human_readable(cur->mem * 1024, p, 6);
2628                         }
2629                         OBJ(memmax) {
2630                                 human_readable(cur->memmax * 1024, p, 255);
2631                         }
2632                         OBJ(memperc) {
2633                                 if (cur->memmax) {
2634                                         if (!use_spacer)
2635                                                 snprintf(p, n, "%*lu",
2636                                                          pad_percents,
2637                                                          (cur->mem * 100) /
2638                                                          (cur->memmax));
2639                                         else
2640                                                 snprintf(p, 4, "%*lu   ",
2641                                                          pad_percents,
2642                                                          (cur->mem * 100) /
2643                                                          (cur->memmax));
2644                                 }
2645                         }
2646                         OBJ(membar) {
2647                                 new_bar(p, obj->data.pair.a,
2648                                         obj->data.pair.b,
2649                                         cur->memmax ? (cur->mem * 255) /
2650                                         (cur->memmax) : 0);
2651                         }
2652
2653                         OBJ(memgraph) {
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);
2658                         }
2659                         /* mixer stuff */
2660                         OBJ(mixer) {
2661                                 snprintf(p, n, "%d",
2662                                          mixer_get_avg(obj->data.l));
2663                         }
2664                         OBJ(mixerl) {
2665                                 snprintf(p, n, "%d",
2666                                          mixer_get_left(obj->data.l));
2667                         }
2668                         OBJ(mixerr) {
2669                                 snprintf(p, n, "%d",
2670                                          mixer_get_right(obj->data.l));
2671                         }
2672                         OBJ(mixerbar) {
2673                                 new_bar(p, obj->data.mixerbar.w,
2674                                         obj->data.mixerbar.h,
2675                                         mixer_get_avg(obj->data.mixerbar.
2676                                                       l) * 255 / 100);
2677                         }
2678                         OBJ(mixerlbar) {
2679                                 new_bar(p, obj->data.mixerbar.w,
2680                                         obj->data.mixerbar.h,
2681                                         mixer_get_left(obj->data.mixerbar.
2682                                                        l) * 255 / 100);
2683                         }
2684                         OBJ(mixerrbar) {
2685                                 new_bar(p, obj->data.mixerbar.w,
2686                                         obj->data.mixerbar.h,
2687                                         mixer_get_right(obj->data.mixerbar.
2688                                                         l) * 255 / 100);
2689                         }
2690
2691                         /* mail stuff */
2692                         OBJ(mails) {
2693                                 snprintf(p, n, "%d", cur->mail_count);
2694                         }
2695                         OBJ(new_mails) {
2696                                 snprintf(p, n, "%d", cur->new_mail_count);
2697                         }
2698 #ifdef MLDONKEY
2699                         OBJ(ml_upload_counter) {
2700                                 snprintf(p, n, "%lld",
2701                                          mlinfo.upload_counter / 1048576);
2702                         }
2703                         OBJ(ml_download_counter) {
2704                                 snprintf(p, n, "%lld",
2705                                          mlinfo.download_counter /
2706                                          1048576);
2707                         }
2708                         OBJ(ml_nshared_files) {
2709                                 snprintf(p, n, "%i", mlinfo.nshared_files);
2710                         }
2711                         OBJ(ml_shared_counter) {
2712                                 snprintf(p, n, "%lld",
2713                                          mlinfo.shared_counter / 1048576);
2714                         }
2715                         OBJ(ml_tcp_upload_rate) {
2716                                 snprintf(p, n, "%.2f",
2717                                          (float) mlinfo.tcp_upload_rate /
2718                                          1024);
2719                         }
2720                         OBJ(ml_tcp_download_rate) {
2721                                 snprintf(p, n, "%.2f",
2722                                          (float) mlinfo.tcp_download_rate /
2723                                          1024);
2724                         }
2725                         OBJ(ml_udp_upload_rate) {
2726                                 snprintf(p, n, "%.2f",
2727                                          (float) mlinfo.udp_upload_rate /
2728                                          1024);
2729                         }
2730                         OBJ(ml_udp_download_rate) {
2731                                 snprintf(p, n, "%.2f",
2732                                          (float) mlinfo.udp_download_rate /
2733                                          1024);
2734                         }
2735                         OBJ(ml_ndownloaded_files) {
2736                                 snprintf(p, n, "%i",
2737                                          mlinfo.ndownloaded_files);
2738                         }
2739                         OBJ(ml_ndownloading_files) {
2740                                 snprintf(p, n, "%i",
2741                                          mlinfo.ndownloading_files);
2742                         }
2743 #endif
2744
2745                         OBJ(nodename) {
2746                                 snprintf(p, n, "%s",
2747                                          cur->uname_s.nodename);
2748                         }
2749                         OBJ(outlinecolor) {
2750                                 new_outline(p, obj->data.l);
2751                         }
2752                         OBJ(processes) {
2753                                 if (!use_spacer)
2754                                         snprintf(p, n, "%hu", cur->procs);
2755                                 else
2756                                         snprintf(p, 5, "%hu    ",
2757                                                  cur->procs);
2758                         }
2759                         OBJ(running_processes) {
2760                                 if (!use_spacer)
2761                                         snprintf(p, n, "%hu",
2762                                                  cur->run_procs);
2763                                 else
2764                                         snprintf(p, 3, "%hu     ",
2765                                                  cur->run_procs);
2766                         }
2767                         OBJ(text) {
2768                                 snprintf(p, n, "%s", obj->data.s);
2769                         }
2770                         OBJ(shadecolor) {
2771                                 new_bg(p, obj->data.l);
2772                         }
2773                         OBJ(stippled_hr) {
2774                                 new_stippled_hr(p, obj->data.pair.a,
2775                                                 obj->data.pair.b);
2776                         }
2777                         OBJ(swap) {
2778                                 human_readable(cur->swap * 1024, p, 255);
2779                         }
2780                         OBJ(swapmax) {
2781                                 human_readable(cur->swapmax * 1024, p,
2782                                                255);
2783                         }
2784                         OBJ(swapperc) {
2785                                 if (cur->swapmax == 0) {
2786                                         strncpy(p, "No swap", 255);
2787                                 } else {
2788                                         if (!use_spacer)
2789                                                 snprintf(p, 255, "%*lu",
2790                                                          pad_percents,
2791                                                          (cur->swap *
2792                                                           100) /
2793                                                          cur->swapmax);
2794                                         else
2795                                                 snprintf(p, 4, "%*lu   ",
2796                                                          pad_percents,
2797                                                          (cur->swap *
2798                                                           100) /
2799                                                          cur->swapmax);
2800                                 }
2801                         }
2802                         OBJ(swapbar) {
2803                                 new_bar(p, obj->data.pair.a,
2804                                         obj->data.pair.b,
2805                                         cur->swapmax ? (cur->swap * 255) /
2806                                         (cur->swapmax) : 0);
2807                         }
2808                         OBJ(sysname) {
2809                                 snprintf(p, n, "%s", cur->uname_s.sysname);
2810                         }
2811                         OBJ(time) {
2812                                 time_t t = time(NULL);
2813                                 struct tm *tm = localtime(&t);
2814                                 setlocale(LC_TIME, "");
2815                                 strftime(p, n, obj->data.s, tm);
2816                         }
2817                         OBJ(utime) {
2818                                 time_t t = time(NULL);
2819                                 struct tm *tm = gmtime(&t);
2820                                 strftime(p, n, obj->data.s, tm);
2821                         }
2822                         OBJ(totaldown) {
2823                                 human_readable(obj->data.net->recv, p,
2824                                                255);
2825                         }
2826                         OBJ(totalup) {
2827                                 human_readable(obj->data.net->trans, p,
2828                                                255);
2829                         }
2830                         OBJ(updates) {
2831                                 snprintf(p, n, "%d", total_updates);
2832                         }
2833                         OBJ(upspeed) {
2834                                 if (!use_spacer)
2835                                         snprintf(p, n, "%d",
2836                                                  (int) (obj->data.net->
2837                                                         trans_speed /
2838                                                         1024));
2839                                 else
2840                                         snprintf(p, 5, "%d     ",
2841                                                  (int) (obj->data.net->
2842                                                         trans_speed /
2843                                                         1024));
2844                         }
2845                         OBJ(upspeedf) {
2846                                 if (!use_spacer)
2847                                         snprintf(p, n, "%.1f",
2848                                                  obj->data.net->
2849                                                  trans_speed / 1024.0);
2850                                 else
2851                                         snprintf(p, 8, "%.1f       ",
2852                                                  obj->data.net->
2853                                                  trans_speed / 1024.0);
2854                         }
2855                         OBJ(upspeedgraph) {
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);
2861                         }
2862                         OBJ(uptime_short) {
2863                                 format_seconds_short(p, n,
2864                                                      (int) cur->uptime);
2865                         }
2866                         OBJ(uptime) {
2867                                 format_seconds(p, n, (int) cur->uptime);
2868                         }
2869
2870 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2871                         OBJ(apm_adapter) {
2872                                 snprintf(p, n, "%s", get_apm_adapter());
2873                         }
2874                         OBJ(apm_battery_life) {
2875                                 char    *msg;
2876                                 msg = get_apm_battery_life();
2877                                 snprintf(p, n, "%s", msg);
2878                                 free(msg);
2879                         }
2880                         OBJ(apm_battery_time) {
2881                                 char    *msg;
2882                                 msg = get_apm_battery_time();
2883                                 snprintf(p, n, "%s", msg);
2884                                 free(msg);
2885                         }
2886 #endif /* __FreeBSD__ */
2887 #ifdef SETI
2888                         OBJ(seti_prog) {
2889                                 snprintf(p, n, "%.2f",
2890                                          cur->seti_prog * 100.0f);
2891                         }
2892                         OBJ(seti_progbar) {
2893                                 new_bar(p, obj->data.pair.a,
2894                                         obj->data.pair.b,
2895                                         (int) (cur->seti_prog * 255.0f));
2896                         }
2897                         OBJ(seti_credit) {
2898                                 snprintf(p, n, "%.0f", cur->seti_credit);
2899                         }
2900 #endif
2901
2902 #ifdef MPD
2903                         OBJ(mpd_title) {
2904                                 snprintf(p, n, "%s", cur->mpd.title);
2905                         }
2906                         OBJ(mpd_artist) {
2907                                 snprintf(p, n, "%s", cur->mpd.artist);
2908                         }
2909                         OBJ(mpd_album) {
2910                                 snprintf(p, n, "%s", cur->mpd.album);
2911                         }
2912                         OBJ(mpd_random) {
2913                                 snprintf(p, n, "%s", cur->mpd.random);
2914                         }
2915                         OBJ(mpd_repeat) {
2916                                 snprintf(p, n, "%s", cur->mpd.repeat);
2917                         }
2918                         OBJ(mpd_track) {
2919                                 snprintf(p, n, "%s", cur->mpd.track);
2920                         }
2921                         OBJ(mpd_vol) {
2922                                 snprintf(p, n, "%i", cur->mpd.volume);
2923                         }
2924                         OBJ(mpd_bitrate) {
2925                                 snprintf(p, n, "%i", cur->mpd.bitrate);
2926                         }
2927                         OBJ(mpd_status) {
2928                                 snprintf(p, n, "%s", cur->mpd.status);
2929                         }
2930                         OBJ(mpd_elapsed) {
2931                                 int days = 0, hours = 0, minutes =
2932                                     0, seconds = 0;
2933                                 int tmp = cur->mpd.elapsed;
2934                                 while (tmp >= 86400) {
2935                                         tmp -= 86400;
2936                                         days++;
2937                                 }
2938                                 while (tmp >= 3600) {
2939                                         tmp -= 3600;
2940                                         hours++;
2941                                 }
2942                                 while (tmp >= 60) {
2943                                         tmp -= 60;
2944                                         minutes++;
2945                                 }
2946                                 seconds = tmp;
2947                                 if (days > 0)
2948                                         snprintf(p, n, "%i days %i:%02i:%02i",
2949                                                  days, hours, minutes,
2950                                                  seconds);
2951                                 else if (hours > 0)
2952                                         snprintf(p, n, "%i:%02i:%02i", hours,
2953                                                  minutes, seconds);
2954                                 else
2955                                         snprintf(p, n, "%i:%02i", minutes,
2956                                                  seconds);
2957                         }
2958                         OBJ(mpd_length) {
2959                                 int days = 0, hours = 0, minutes =
2960                                     0, seconds = 0;
2961                                 int tmp = cur->mpd.length;
2962                                 while (tmp >= 86400) {
2963                                         tmp -= 86400;
2964                                         days++;
2965                                 }
2966                                 while (tmp >= 3600) {
2967                                         tmp -= 3600;
2968                                         hours++;
2969                                 }
2970                                 while (tmp >= 60) {
2971                                         tmp -= 60;
2972                                         minutes++;
2973                                 }
2974                                 seconds = tmp;
2975                                 if (days > 0)
2976                                         snprintf(p, n,
2977                                                  "%i days %i:%02i:%02i",
2978                                                  days, hours, minutes,
2979                                                  seconds);
2980                                 else if (hours > 0)
2981                                         snprintf(p, n, "%i:%02i:%02i", hours,
2982                                                  minutes, seconds);
2983                                 else
2984                                         snprintf(p, n, "%i:%02i", minutes,
2985                                                  seconds);
2986                         }
2987                         OBJ(mpd_percent) {
2988                                 snprintf(p, n, "%2.0f",
2989                                          cur->mpd.progress * 100);
2990                         }
2991                         OBJ(mpd_bar) {
2992                                 new_bar(p, obj->data.pair.a,
2993                                         obj->data.pair.b,
2994                                         (int) (cur->mpd.progress *
2995                                                255.0f));
2996                         }
2997 #endif
2998                         OBJ(top) {
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.
3010                                                           num]->amount);
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.
3016                                                           num]->pid);
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.
3022                                                           num]->totalmem);
3023                                 }
3024                         }
3025                         OBJ(top_mem) {
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
3031                                         snprintf(p, 17,
3032                                                  "%s                              ",
3033                                                  cur->memu[obj->data.top.
3034                                                            num]->name);
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.
3040                                                            num]->amount);
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.
3046                                                            num]->pid);
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.
3052                                                            num]->totalmem);
3053                                 }
3054                         }
3055
3056
3057
3058                         /*
3059                          * I'm tired of everything being packed in
3060                          * pee
3061                          * poop
3062                          */
3063
3064
3065                         OBJ(tail) {
3066                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
3067                                         snprintf(p, n, "%s", obj->data.tail.buffer);
3068                                 } else {
3069                                         obj->data.tail.last_update = current_update_time;
3070                                         FILE *fp;
3071                                         int i;
3072                                         int added = 0;
3073                                         tailstring *head = NULL;
3074                                         tailstring *headtmp = NULL;
3075                                         tailstring *freetmp = NULL;
3076                                         fp = fopen(obj->data.tail.logfile, "rt");
3077                                         if (fp == NULL) {
3078                                                 ERR("tail logfile failed to open");
3079                                         }
3080                                         else {
3081                                                 obj->data.tail.readlines = 0;
3082
3083                                                 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
3084                                                         if (added >= 30) {
3085                                                                 freelasttail(head);
3086                                                         }
3087                                                         else {
3088                                                                 added++;
3089                                                         }
3090                                                         addtail(&head, obj->data.tail.buffer);
3091                                                         obj->data.tail.readlines++;
3092                                                 }
3093
3094                                                 fclose(fp);
3095                                                 freetmp = head;
3096
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);
3100                                                                 head = head->next;
3101                                                         }
3102                                                         freetail(freetmp);
3103                                                         freetmp = headtmp;
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++) {
3107                                                                 if (headtmp) {
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;
3110                                                                 }
3111                                                         }
3112
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';
3116                                                         }
3117                                                         snprintf(p, n, "%s", obj->data.tail.buffer);
3118
3119                                                         freetail(freetmp);
3120                                                 }
3121                                                 else {
3122                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
3123                                                         snprintf(p, n, "Logfile Empty");
3124                                                 }
3125                                         }
3126                                 }
3127                         }
3128                         OBJ(head) {
3129                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
3130                                         snprintf(p, n, "%s", obj->data.tail.buffer);
3131                                 } else {
3132                                         obj->data.tail.last_update = current_update_time;
3133                                         FILE *fp;
3134                                         tailstring *head = NULL;
3135                                         tailstring *headtmp = NULL;
3136                                         tailstring *freetmp = NULL;
3137                                         fp = fopen(obj->data.tail.logfile, "rt");
3138                                         if (fp == NULL) {
3139                                                 ERR("head logfile failed to open");
3140                                         }
3141                                         else {
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++;
3146                                                 }
3147                                                 fclose(fp);
3148                                                 freetmp = head;
3149                                                 if (obj->data.tail.readlines > 0) {
3150                                                         while (head) {
3151                                                                 addtail(&headtmp, head->data);
3152                                                                 head = head->next;
3153                                                         }
3154                                                         freetail(freetmp);
3155                                                         freetmp = headtmp;
3156                                                         strcpy(obj->data.tail.buffer, headtmp->data);
3157                                                         headtmp = headtmp->next;
3158                                                         while (headtmp) {
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;
3161                                                         }
3162                                                         freetail(freetmp);
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';
3166                                                         }
3167                                                         snprintf(p, n, "%s", obj->data.tail.buffer);
3168                                                 }
3169                                                 else {
3170                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
3171                                                         snprintf(p, n, "Logfile Empty");
3172                                                 }
3173                                         }
3174                                 }
3175                         }
3176 #ifdef TCP_PORT_MONITOR
3177                         OBJ(tcp_portmon)
3178                         {
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 );
3184                                 if ( !p_monitor ) {
3185                                         snprintf(p, n, "monitor not found");
3186                                         break;
3187                                 }
3188
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,
3193                                                             p, n ) != 0 )
3194                                 {
3195                                         snprintf(p, n, "monitor peek error");
3196                                         break;
3197                                 }
3198                         }
3199 #endif
3200
3201                         break;
3202                 }
3203
3204                 {
3205                         unsigned int a = strlen(p);
3206                         p += a;
3207                         n -= a;
3208                 }
3209         }
3210
3211         if (stuff_in_upper_case) {
3212                 char *p;
3213
3214                 p = text_buffer;
3215                 while (*p) {
3216                         *p = toupper(*p);
3217                         p++;
3218                 }
3219         }
3220
3221         last_update_time = current_update_time;
3222         total_updates++;
3223         //free(p);
3224 }
3225
3226 #ifdef X11
3227 static void set_font()
3228 {
3229 #ifdef XFT
3230         if (use_xft) {
3231                         if (window.xftdraw != NULL) {
3232                                 XftDrawDestroy(window.xftdraw);
3233                         }
3234                         window.xftdraw = XftDrawCreate(display, window.drawable,
3235                                         DefaultVisual(display,
3236                                                         screen),
3237                                         DefaultColormap(display,
3238                                                         screen));
3239                 } else
3240 #endif
3241 {
3242         XSetFont(display, window.gc, fonts[selected_font].font->fid);
3243 }
3244 }
3245
3246
3247 /*
3248  * text size
3249  */
3250
3251 static int text_start_x, text_start_y;  /* text start position in window */
3252 static int text_width, text_height;
3253
3254 #endif /* X11 */
3255
3256 static inline int get_string_width(const char *s)
3257 {
3258 #ifdef X11
3259         return *s ? calc_text_width(s, strlen(s)) : 0;
3260 #else
3261         return strlen(s);
3262 #endif /* X11 */
3263 }
3264
3265 static inline int get_string_width_special(char *s)
3266 {
3267         if (!s) {
3268                 return 0;
3269         }
3270 #ifdef X11
3271         char *p, *final;
3272         p = strdup(s);
3273         final = p;
3274         int index = 1;
3275         int width = 0;
3276         unsigned int i;
3277         while (*p) {
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);
3282                         }
3283                         if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
3284                                 width += specials[special_index+index].width;
3285                         }
3286                         index++;
3287                 } else {
3288                         p++;
3289                 }
3290         }
3291         if (strlen(final) > 1) {
3292                 width += calc_text_width(final, strlen(final));
3293         }
3294         free(final);
3295         return width;
3296 #else
3297         return strlen(s);
3298 #endif /* X11 */
3299 }
3300
3301 int fontchange = 0;
3302
3303 #ifdef X11
3304 static void text_size_updater(char *s)
3305 {
3306         int w = 0;
3307         char *p;
3308         int h = font_height();
3309         /* get string widths and skip specials */
3310         p = s;
3311         while (*p) {
3312                 if (*p == SPECIAL_CHAR) {
3313                         *p = '\0';
3314                         w += get_string_width(s);
3315                         *p = SPECIAL_CHAR;
3316
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;
3322                                         h += font_ascent();
3323                                 }
3324                         }
3325                         
3326                         else if (specials[special_index].type == OFFSET) {
3327                                 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
3328                         }
3329                         else if (specials[special_index].type == VOFFSET) {
3330                                 h += specials[special_index].arg;
3331                         }
3332                         else if (specials[special_index].type == FONT) {
3333                                 fontchange = specials[special_index].font_added;
3334                                 selected_font = specials[special_index].font_added;
3335                                 h = font_height();
3336                         }
3337
3338                         
3339                         special_index++;
3340                         s = p + 1;
3341                 }
3342                 p++;
3343         }
3344                 w += get_string_width(s);
3345         if (w > text_width)
3346                 text_width = w;
3347         if (text_width > maximum_width && maximum_width)
3348                 text_width = maximum_width;
3349
3350         text_height += h;
3351         if (fontchange) {
3352                 selected_font = 0;
3353         }
3354 }
3355 #endif /* X11 */
3356
3357
3358 #ifdef X11
3359 static void update_text_area()
3360 {
3361         int x, y;
3362
3363         /* update text size if it isn't fixed */
3364 #ifdef OWN_WINDOW
3365         if (!fixed_size)
3366 #endif
3367         {
3368                 text_width = minimum_width;
3369                 text_height = 0;
3370                 special_index = 0;
3371                 for_each_line(text_buffer, text_size_updater);
3372                 text_width += 1;
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;
3377         }
3378
3379         /* get text position on workarea */
3380         switch (text_alignment) {
3381         case TOP_LEFT:
3382                 x = gap_x;
3383                 y = gap_y;
3384                 break;
3385
3386         case TOP_RIGHT:
3387                 x = workarea[2] - text_width - gap_x;
3388                 y = gap_y;
3389                 break;
3390
3391         default:
3392         case BOTTOM_LEFT:
3393                 x = gap_x;
3394                 y = workarea[3] - text_height - gap_y;
3395                 break;
3396
3397         case BOTTOM_RIGHT:
3398                 x = workarea[2] - text_width - gap_x;
3399                 y = workarea[3] - text_height - gap_y;
3400                 break;
3401         
3402 #ifdef OWN_WINDOW
3403         case NONE: // Let the WM manage the window
3404                 x = window.x;
3405                 y = window.y;
3406
3407                 fixed_pos  = 1;
3408                 fixed_size = 1;
3409                 break;
3410 #endif
3411         }
3412 #ifdef OWN_WINDOW
3413
3414         if (own_window && !fixed_pos) {
3415                 x += workarea[0];
3416                 y += workarea[1];
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;
3421         } else
3422 #endif
3423         {
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) {
3429                         y += workarea[1];
3430                         x += workarea[0];
3431                 }
3432
3433                 text_start_x = x;
3434                 text_start_y = y;
3435         }
3436 }
3437
3438 /*
3439  * drawing stuff
3440  */
3441
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;
3445
3446 static inline void set_foreground_color(long c)
3447 {
3448         current_color = c;
3449         XSetForeground(display, window.gc, c);
3450 }
3451 #endif /* X11 */
3452
3453 static void draw_string(const char *s)
3454 {
3455         if (s[0] == '\0')
3456                 return;
3457         int i, i2, pos, width_of_s;
3458         int max=0;
3459         int added;
3460         width_of_s = get_string_width(s);
3461         if (out_to_console) {
3462                 printf("%s\n", s);
3463         }
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);
3468         pos = 0;
3469         added = 0;
3470         char space[2];
3471         snprintf(space, 2, " ");
3472 #ifdef X11
3473         max = ((text_width - width_of_s) / get_string_width(space));
3474 #endif /* X11 */
3475         /*
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.
3480          */
3481         for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
3482                 if (tmpstring1[i] == '\t')      // 9 is ascii tab
3483                 {
3484                         i2 = 0;
3485                         for (i2 = 0;
3486                              i2 < (8 - (1 + pos) % 8) && added <= max;
3487                              i2++) {
3488                                 /*
3489                                 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
3490                                         fprintf(stderr,"buffer overrun detected\n");
3491                                 */
3492                                 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
3493                                 added++;
3494                         }
3495                         pos += i2;
3496                 } else {
3497                         if (tmpstring1[i] != 9) {
3498                                 /*
3499                                 if ( pos > TEXT_BUFFER_SIZE-1 )
3500                                          fprintf(stderr,"buffer overrun detected\n");
3501                                 */
3502                                 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
3503                                 pos++;
3504                         }
3505                 }
3506         }
3507 #ifdef X11
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';
3512                 }
3513         }
3514 #endif /* X11 */
3515         s = tmpstring2;
3516 #ifdef X11
3517 #ifdef XFT
3518         if (use_xft) {
3519                 XColor c;
3520                 XftColor c2;
3521                 c.pixel = current_color;
3522                 XQueryColor(display, DefaultColormap(display, screen), &c);
3523
3524                 c2.pixel = c.pixel;
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;
3529                 if (utf8_mode) {
3530                         XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3531                                           cur_x, cur_y, (XftChar8 *) s,
3532                                           strlen(s));
3533                 } else {
3534                         XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3535                                        cur_x, cur_y, (XftChar8 *) s,
3536                                        strlen(s));
3537                 }
3538         } else
3539 #endif
3540         {
3541                 XDrawString(display, window.drawable, window.gc,
3542                             cur_x, cur_y, s, strlen(s));
3543         }
3544         cur_x += width_of_s;
3545 #endif /* X11 */
3546         memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
3547 }
3548
3549 long redmask, greenmask, bluemask;
3550
3551 void set_up_gradient()
3552 {
3553 #ifdef X11
3554         colour_depth = DisplayPlanes(display, screen);
3555 #else
3556         colour_depth = 16;
3557 #endif /* X11 */
3558         if (colour_depth != 24 && colour_depth != 16) {
3559                 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
3560         }
3561         int i;
3562         redmask = 0;
3563         greenmask = 0;
3564         bluemask = 0;
3565         for(i = (colour_depth / 3)-1; i>=0; i--) {
3566                 redmask |= 1 << i;
3567                 greenmask |= 1 << i;
3568                 bluemask |= 1 << i;
3569         }
3570         if (colour_depth%3 == 1) {
3571                 greenmask |= 1 << (colour_depth / 3);
3572         }
3573         redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
3574         greenmask = greenmask << (colour_depth / 3);
3575 }
3576
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 */
3578         int tmp_color = 0;
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;
3590         if (red1 > red2) {
3591                 red3 = -1;
3592         }
3593         if (red1 < red2) {
3594                 red3 = 1;
3595         }
3596         if (green1 > green2) {
3597                 green3 = -1;
3598         }
3599         if (green1 < green2) {
3600                 green3 = 1;
3601         }
3602         if (blue1 > blue2) {
3603                 blue3 = -1;
3604         }
3605         if (blue1 < blue2) {
3606                 blue3 = 1;
3607         }
3608         red1 += red3;
3609         green1 += green3;
3610         blue1 += blue3;
3611         if (red1 < 0) {
3612                 red1 = 0;
3613         }
3614         if (green1 < 0) {
3615                 green1 = 0;
3616         }
3617         if (blue1 < 0) {
3618                 blue1 = 0;
3619         }
3620         if (red1 > bluemask) {
3621                 red1 = bluemask;
3622         }
3623         if (green1 > bluemask) {
3624                 green1 = bluemask;
3625         }
3626         if (blue1 > bluemask) {
3627                 blue1 = bluemask;
3628         }
3629         tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
3630         return tmp_color;
3631 }
3632
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) {
3635                 set_up_gradient();
3636         }
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);
3651         int max = red3;
3652         if (green3 > max)
3653                 max = green3;
3654         if (blue3 > max)
3655                 max = blue3;
3656         return max;
3657 }
3658
3659 static void draw_line(char *s)
3660 {
3661 #ifdef X11
3662         char *p;
3663         cur_x = text_start_x;
3664         cur_y += font_ascent();
3665         int cur_y_add = 0;
3666         short font_h = font_height();
3667
3668         /* find specials and draw stuff */
3669         p = s;
3670         while (*p) {
3671                 if (*p == SPECIAL_CHAR) {
3672                         int w = 0;
3673
3674                         /* draw string before special */
3675                         *p = '\0';
3676                         draw_string(s);
3677                         *p = SPECIAL_CHAR;
3678                         s = p + 1;
3679
3680                         /* draw special */
3681                         switch (specials[special_index].type) {
3682                         case HORIZONTAL_LINE:
3683                                 {
3684                                         int h =
3685                                             specials[special_index].height;
3686                                         int mid = font_ascent() / 2;
3687                                         w = text_start_x + text_width -
3688                                             cur_x;
3689
3690                                         XSetLineAttributes(display,
3691                                                            window.gc, h,
3692                                                            LineSolid,
3693                                                            CapButt,
3694                                                            JoinMiter);
3695                                         XDrawLine(display, window.drawable,
3696                                                   window.gc, cur_x,
3697                                                   cur_y - mid / 2,
3698                                                   cur_x + w,
3699                                                   cur_y - mid / 2);
3700                                 }
3701                                 break;
3702
3703                         case STIPPLED_HR:
3704                                 {
3705                                         int h =
3706                                             specials[special_index].height;
3707                                         int s =
3708                                             specials[special_index].arg;
3709                                         int mid = font_ascent() / 2;
3710                                         char ss[2] = { s, s };
3711                                         w = text_start_x + text_width -
3712                                             cur_x - 1;
3713
3714                                         XSetLineAttributes(display,
3715                                                            window.gc, h,
3716                                                            LineOnOffDash,
3717                                                            CapButt,
3718                                                            JoinMiter);
3719                                         XSetDashes(display, window.gc, 0,
3720                                                    ss, 2);
3721                                         XDrawLine(display, window.drawable,
3722                                                   window.gc, cur_x,
3723                                                   cur_y - mid / 2,
3724                                                   cur_x + w,
3725                                                   cur_y - mid / 2);
3726                                 }
3727                                 break;
3728
3729                         case BAR:
3730                                 {
3731                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3732                                                 break;
3733                                         }
3734                                         int h =
3735                                             specials[special_index].height;
3736                                         int bar_usage =
3737                                             specials[special_index].arg;
3738                                         int by;
3739
3740 #ifdef XFT
3741                                         if (use_xft) {
3742                                                 by = cur_y - (font_ascent() + h) / 2 - 1;
3743                                         } else 
3744 #endif
3745                                         {
3746                                                 by = cur_y - (font_ascent()/2) - 1;
3747                                         }
3748                                         if (h < (font_height())) {
3749                                                 by -= h / 2 - 1;
3750                                         }
3751                                         w = specials[special_index].width;
3752                                         if (w == 0)
3753                                                 w = text_start_x +
3754                                                     text_width - cur_x - 1;
3755                                         if (w < 0)
3756                                                 w = 0;
3757
3758                                         XSetLineAttributes(display,
3759                                                            window.gc, 1,
3760                                                            LineSolid,
3761                                                            CapButt,
3762                                                            JoinMiter);
3763
3764                                         XDrawRectangle(display,
3765                                                        window.drawable,
3766                                                        window.gc, cur_x,
3767                                                        by, w, h);
3768                                         XFillRectangle(display,
3769                                                        window.drawable,
3770                                                        window.gc, cur_x,
3771                                                        by,
3772                                                        w * bar_usage / 255,
3773                                                        h);
3774                                         if (specials[special_index].
3775                                             height > cur_y_add
3776                                             && specials[special_index].
3777                                             height > font_h) {
3778                                                 cur_y_add =
3779                                                     specials
3780                                                     [special_index].height;
3781                                         }
3782                                 }
3783                                 break;
3784
3785                         case GRAPH:
3786                         {
3787                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3788                                                 break;
3789                                         }
3790                                         int h =
3791                                             specials[special_index].height;
3792                                         int by;
3793 #ifdef XFT
3794                                         if (use_xft) {
3795                                             by = cur_y - (font_ascent() + h) / 2 - 1;
3796                                         } else
3797 #endif
3798                                         {
3799                                                 by = cur_y - (font_ascent()/2) - 1;
3800                                         }
3801                                         if (h < (font_height())) {
3802                                                 by -= h / 2 - 1;
3803                                         }
3804                                         w = specials[special_index].width;
3805                                         if (w == 0)
3806                                                 w = text_start_x + text_width - cur_x - 1;
3807                                         if (w < 0)
3808                                                 w = 0;
3809                                         XSetLineAttributes(display,
3810                                                            window.gc, 1,
3811                                                            LineSolid,
3812                                                            CapButt,
3813                                                            JoinMiter);
3814                                         XDrawRectangle(display,
3815                                                        window.drawable,
3816                                                        window.gc, cur_x,
3817                                                        by, w, h);
3818                                         XSetLineAttributes(display,
3819                                                            window.gc, 1,
3820                                                            LineSolid,
3821                                                            CapButt,
3822                                                            JoinMiter);
3823         int i;
3824         int j = 0;
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);
3833         }
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);
3840                                 gradient_update--;
3841                         }
3842                 }
3843                 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3844                         j++;
3845                                                 }
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 */
3847                                         }
3848                                         if (specials[special_index].
3849                                             height > cur_y_add
3850                                             && specials[special_index].
3851                                             height > font_h) {
3852                                                 cur_y_add =
3853                                                     specials
3854                                                     [special_index].height;
3855                                         }
3856                                 }
3857                                 if (draw_mode == BG) {
3858                                         set_foreground_color(default_bg_color);
3859                                 }
3860                                 else if (draw_mode == OUTLINE) {
3861                                         set_foreground_color(default_out_color);
3862                                 } else {
3863                                         set_foreground_color(default_fg_color);
3864                                 }
3865                                 break;
3866                         
3867                                 case FONT:
3868                                 if (fontchange) {
3869                                         cur_y -= font_ascent();
3870                                         selected_font = specials[special_index].font_added;
3871                                         cur_y += font_ascent();
3872 #ifdef XFT
3873                                         if (!use_xft || use_xdbe)
3874 #endif
3875                                         {
3876                                                 set_font();
3877                                         }
3878                                 }
3879                                 break;
3880                         case FG:
3881                                 if (draw_mode == FG)
3882                                         set_foreground_color(specials
3883                                                              [special_index].
3884                                                              arg);
3885                                 break;
3886
3887                         case BG:
3888                                 if (draw_mode == BG)
3889                                         set_foreground_color(specials
3890                                                              [special_index].
3891                                                              arg);
3892                                 break;
3893
3894                         case OUTLINE:
3895                                 if (draw_mode == OUTLINE)
3896                                         set_foreground_color(specials
3897                                                              [special_index].
3898                                                              arg);
3899                                 break;
3900
3901                                 case OFFSET:
3902                                 {
3903                                         w += specials[special_index].arg;
3904                                 }
3905                                 break;
3906                                 case VOFFSET:
3907                                 {
3908                                         cur_y += specials[special_index].arg;
3909                                 }
3910                                 break;
3911
3912                         case ALIGNR:
3913                                 {
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;
3918                                 }
3919                                 }
3920                                 break;
3921
3922                         case ALIGNC:
3923                                 {
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);*/
3927                                         if (pos_x >
3928                                             specials[special_index].arg)
3929                                                 w = pos_x -
3930                                                     specials
3931                                                     [special_index].arg;
3932                                 }
3933                                 break;
3934
3935                         }
3936
3937                         cur_x += w;
3938
3939                         special_index++;
3940                 }
3941
3942                 p++;
3943         }
3944 #else
3945         draw_string(s);
3946 #endif
3947 #ifdef X11
3948         if (cur_y_add > 0) {
3949                 cur_y += cur_y_add;
3950                 cur_y -= font_descent();
3951         }
3952
3953         draw_string(s);
3954
3955         cur_y += font_descent();
3956         if (fontchange) {
3957                 selected_font = 0;
3958         }
3959 #endif /* X11 */
3960 }
3961
3962 static void draw_text()
3963 {
3964 #ifdef X11
3965         cur_y = text_start_y;
3966
3967         /* draw borders */
3968         if (draw_borders && border_width > 0) {
3969                 unsigned int b = (border_width + 1) / 2;
3970
3971                 if (stippled_borders) {
3972                         char ss[2] =
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);
3978                 } else {
3979                         XSetLineAttributes(display, window.gc,
3980                                            border_width, LineSolid,
3981                                            CapButt, JoinMiter);
3982                 }
3983
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 -
3989                                b * 2);
3990         }
3991
3992         /* draw text */
3993         special_index = 0;
3994 #endif /* X11 */
3995         for_each_line(text_buffer, draw_line);
3996 }
3997
3998 static void draw_stuff()
3999 {
4000 #ifdef X11
4001         if (draw_shades && !draw_outline) {
4002                 text_start_x++;
4003                 text_start_y++;
4004                 set_foreground_color(default_bg_color);
4005                 draw_mode = BG;
4006                 draw_text();
4007                 text_start_x--;
4008                 text_start_y--;
4009         }
4010
4011         if (draw_outline) {
4012                 int i, j;
4013                 for (i = -1; i < 2; i++)
4014                         for (j = -1; j < 2; j++) {
4015                                 if (i == 0 && j == 0)
4016                                         continue;
4017                                 text_start_x += i;
4018                                 text_start_y += j;
4019                                 set_foreground_color(default_out_color);
4020                                 draw_mode = OUTLINE;
4021                                 draw_text();
4022                                 text_start_x -= i;
4023                                 text_start_y -= j;
4024                         }
4025         }
4026
4027         set_foreground_color(default_fg_color);
4028         draw_mode = FG;
4029 #endif /* X11 */
4030         draw_text();
4031 #ifdef X11
4032 #ifdef XDBE
4033         if (use_xdbe) {
4034                 XdbeSwapInfo swap;
4035                 swap.swap_window = window.window;
4036                 swap.swap_action = XdbeBackground;
4037                 XdbeSwapBuffers(display, &swap, 1);
4038         }
4039 #endif
4040 #endif /* X11 */
4041 }
4042 #ifdef X11
4043 static void clear_text(int exposures)
4044 {
4045 #ifdef XDBE
4046         if (use_xdbe) {
4047                 return;         /* The swap action is XdbeBackground, which clears */
4048         } else
4049 #endif
4050         {
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);
4058         }
4059 }
4060 #endif /* X11 */
4061
4062 static int need_to_update;
4063
4064 /* update_text() generates new text and clears old text area */
4065 static void update_text()
4066 {
4067         generate_text();
4068 #ifdef X11
4069         clear_text(1);
4070 #endif /* X11 */
4071         need_to_update = 1;
4072 }
4073
4074 static void main_loop()
4075 {
4076 #ifdef SIGNAL_BLOCKING
4077         sigset_t  newmask, oldmask;
4078
4079         sigemptyset(&newmask);
4080         sigaddset(&newmask,SIGINT);
4081         sigaddset(&newmask,SIGTERM);
4082         sigaddset(&newmask,SIGUSR1);
4083 #endif
4084
4085 #ifdef X11
4086         Region region = XCreateRegion();
4087 #endif /* X11 */
4088
4089         info.looped = 0;
4090         while (total_run_times == 0 || info.looped < total_run_times - 1) {
4091                 info.looped++;
4092
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()");
4097 #endif
4098
4099 #ifdef X11
4100                 XFlush(display);
4101
4102                 /* wait for X event or timeout */
4103
4104                 if (!XPending(display)) {
4105                         fd_set fdsr;
4106                         struct timeval tv;
4107                         int s;
4108                         double t =
4109                             update_interval - (get_time() -
4110                                                last_update_time);
4111
4112                         if (t < 0)
4113                                 t = 0;
4114
4115                         tv.tv_sec = (long) t;
4116                         tv.tv_usec = (long) (t * 1000000) % 1000000;
4117                         FD_ZERO(&fdsr);
4118                         FD_SET(ConnectionNumber(display), &fdsr);
4119
4120
4121                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
4122                                    0, &tv);
4123 #else
4124                         usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
4125 #endif /* X11 */
4126 #ifdef X11
4127                         if (s == -1) {
4128                                 if (errno != EINTR)
4129                                         ERR("can't select(): %s",
4130                                             strerror(errno));
4131                         } else {
4132                                 /* timeout */
4133                                 if (s == 0)
4134 #endif /* X11 */
4135                                         update_text();
4136 #ifdef X11
4137                         }
4138 #ifdef OWN_WINDOW
4139                         if (own_window) {
4140                                 set_transparent_background(window.window);
4141                         }
4142 #endif
4143                 }
4144                 
4145                 if (need_to_update) {
4146 #ifdef OWN_WINDOW
4147                         int wx = window.x, wy = window.y;
4148 #endif
4149
4150                         need_to_update = 0;
4151
4152                         update_text_area();
4153 #ifdef OWN_WINDOW
4154                         if (own_window) {
4155                                 /* resize window if it isn't right size */
4156                                 if (!fixed_size &&
4157                                     (text_width + border_margin * 2 !=
4158                                      window.width
4159                                      || text_height + border_margin * 2 !=
4160                                      window.height)) {
4161                                         window.width =
4162                                             text_width +
4163                                             border_margin * 2 + 1;
4164                                         window.height =
4165                                             text_height +
4166                                             border_margin * 2 + 1;
4167                                         XResizeWindow(display,
4168                                                       window.window,
4169                                                       window.width,
4170                                                       window.height);
4171                                      }
4172
4173                                 /* move window if it isn't in right position */
4174                                 if (!fixed_pos
4175                                     && (window.x != wx
4176                                         || window.y != wy)) {
4177                                         XMoveWindow(display, window.window,
4178                                                     window.x, window.y);
4179                                 }
4180                         }
4181 #endif
4182
4183                         clear_text(1);
4184
4185 #ifdef XDBE
4186                         if (use_xdbe) {
4187                                 XRectangle r;
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);
4193                         }
4194 #endif
4195                 }
4196
4197                 /* handle X events */
4198
4199                 while (XPending(display)) {
4200                         XEvent ev;
4201                         XNextEvent(display, &ev);
4202                         switch (ev.type) {
4203                         case Expose:
4204                                 {
4205                                         XRectangle r;
4206                                         r.x = ev.xexpose.x;
4207                                         r.y = ev.xexpose.y;
4208                                         r.width = ev.xexpose.width;
4209                                         r.height = ev.xexpose.height;
4210                                         XUnionRectWithRegion(&r, region,
4211                                                              region);
4212                                 }
4213                                 break;
4214
4215 #ifdef OWN_WINDOW
4216                         case ReparentNotify:
4217                                 /* set background to ParentRelative for all parents */
4218                                 if (own_window) {
4219                                         set_transparent_background(window.
4220                                         window);
4221                                 }
4222                                 break;
4223
4224                         case ConfigureNotify:
4225                                 if (own_window) {
4226                                         /* if window size isn't what expected, set fixed size */
4227                                         if (ev.xconfigure.width !=
4228                                             window.width
4229                                             || ev.xconfigure.height !=
4230                                             window.height) {
4231                                                 if (window.width != 0
4232                                                     && window.height != 0)
4233                                                         fixed_size = 1;
4234
4235                                                 /* clear old stuff before screwing up size and pos */
4236                                                 clear_text(1);
4237
4238                                                 {
4239                                                         XWindowAttributes
4240                                                             attrs;
4241                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
4242                                                                 window.
4243                                                                     width =
4244                                                                     attrs.
4245                                                                     width;
4246                                                                 window.
4247                                                                     height
4248                                                                     =
4249                                                                     attrs.
4250                                                                     height;
4251                                                         }
4252                                                 }
4253
4254                                                 text_width =
4255                                                     window.width -
4256                                                     border_margin * 2 - 1;
4257                                                 text_height =
4258                                                     window.height -
4259                                                     border_margin * 2 - 1;
4260                                                 if (text_width > maximum_width && maximum_width > 0)
4261                                                         text_width = maximum_width;
4262                                         }
4263
4264                                         /* if position isn't what expected, set fixed pos, total_updates
4265                                          * avoids setting fixed_pos when window is set to weird locations
4266                                          * when started */
4267                                         /*if (total_updates >= 2 this is broken
4268                                             && !fixed_pos
4269                                             && (window.x != ev.xconfigure.x
4270                                                 || window.y !=
4271                                                 ev.xconfigure.y)
4272                                             && (ev.xconfigure.x != 0
4273                                                 || ev.xconfigure.y != 0)) {
4274                                                 fixed_pos = 1;
4275                                 }*/
4276                                         set_font();
4277                                 }
4278                                 break;
4279 #endif
4280
4281                         default:
4282                                 break;
4283                         }
4284                 }
4285
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.
4292                  */
4293
4294                 if (!XEmptyRegion(region)) {
4295 #ifdef XDBE
4296                         if (use_xdbe) {
4297                                 XRectangle r;
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);
4303                 }
4304 #endif
4305                         XSetRegion(display, window.gc, region);
4306 #ifdef XFT
4307                         if (use_xft) {
4308                                 XftDrawSetClip(window.xftdraw, region);
4309                         }
4310 #endif
4311 #endif /* X11 */
4312                         draw_stuff();
4313 #ifdef X11
4314                         XDestroyRegion(region);
4315                         region = XCreateRegion();
4316                 }
4317 #endif /* X11 */
4318
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()");
4323 #endif
4324
4325                 switch(g_signal_pending) {
4326                 case SIGUSR1:
4327                         {
4328                         ERR("received SIGUSR1. reloading the config file.");
4329                         reload_config();
4330                         break;
4331                         }
4332                 case SIGINT:
4333                 case SIGTERM:
4334                         {
4335                         ERR("received SIGINT or SIGTERM to terminate. bye!");
4336                         clean_up();
4337 #ifdef X11
4338                         XDestroyRegion(region);
4339                         region = NULL;
4340 #endif /* X11 */
4341                         return;  /* return from main_loop */
4342                         /*break;*/
4343                         }
4344                 default:
4345                         {
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);
4351                         break;
4352                         }
4353                 }
4354                 g_signal_pending=0;
4355         
4356         }
4357 #ifdef X11
4358         XDestroyRegion(region);
4359         region = NULL;
4360 #endif /* X11 */
4361 }
4362
4363 static void load_config_file(const char *);
4364
4365 /* reload the config file */
4366 void reload_config(void)
4367 {
4368         if (current_config) {
4369                 clear_fs_stats();
4370                 load_config_file(current_config);
4371 #ifdef X11
4372                 load_fonts();
4373                 set_font();
4374                 XClearWindow(display, RootWindow(display, screen)); // clear the window first
4375
4376 #endif /* X11 */
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; 
4380 #endif
4381                 extract_variable_text(text);
4382                 free(text);
4383                 text = NULL;
4384                 update_text();
4385         }
4386 }
4387
4388 void clean_up(void)
4389 {
4390 #ifdef X11
4391 #ifdef XDBE
4392         if (use_xdbe) {
4393                 XdbeDeallocateBackBufferName(display, window.back_buffer);
4394         }
4395 #endif
4396 #ifdef OWN_WINDOW
4397         if (own_window)
4398                 XDestroyWindow(display, window.window);
4399         else
4400 #endif
4401         {
4402                 XClearWindow(display, RootWindow(display, screen));
4403                 clear_text(1);
4404                 XFlush(display);
4405         }
4406
4407         XFreeGC(display, window.gc);
4408 #endif /* X11 */
4409
4410
4411         /* it is really pointless to free() memory at the end of program but ak|ra
4412          * wants me to do this */
4413
4414         free_text_objects();
4415
4416         if (text != original_text)
4417                 free(text);
4418
4419         free(current_config);
4420         free(current_mail_spool);
4421 #ifdef SETI
4422         free(seti_dir);
4423 #endif
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;
4427 #endif
4428 }
4429
4430 static int string_to_bool(const char *s)
4431 {
4432         if (!s)
4433                 return 1;
4434         if (strcasecmp(s, "yes") == 0)
4435                 return 1;
4436         if (strcasecmp(s, "true") == 0)
4437                 return 1;
4438         if (strcasecmp(s, "1") == 0)
4439                 return 1;
4440         return 0;
4441 }
4442 #ifdef X11
4443 static enum alignment string_to_alignment(const char *s)
4444 {
4445         if (strcasecmp(s, "top_left") == 0)
4446                 return TOP_LEFT;
4447         else if (strcasecmp(s, "top_right") == 0)
4448                 return TOP_RIGHT;
4449         else if (strcasecmp(s, "bottom_left") == 0)
4450                 return BOTTOM_LEFT;
4451         else if (strcasecmp(s, "bottom_right") == 0)
4452                 return BOTTOM_RIGHT;
4453         else if (strcasecmp(s, "tl") == 0)
4454                 return TOP_LEFT;
4455         else if (strcasecmp(s, "tr") == 0)
4456                 return TOP_RIGHT;
4457         else if (strcasecmp(s, "bl") == 0)
4458                 return BOTTOM_LEFT;
4459         else if (strcasecmp(s, "br") == 0)
4460                 return BOTTOM_RIGHT;
4461         else if (strcasecmp(s, "none") == 0)
4462                 return NONE;
4463         return TOP_LEFT;
4464 }
4465 #endif /* X11 */
4466
4467
4468 static void set_default_configurations(void)
4469 {
4470         fork_to_background = 0;
4471         total_run_times = 0;
4472         info.cpu_avg_samples = 2;
4473         info.net_avg_samples = 2;
4474         info.memmax = 0;
4475         top_cpu = 0;
4476         top_mem = 0;
4477 #ifdef MPD
4478         strcpy(info.mpd.host, "localhost");
4479         info.mpd.port = 6600;
4480         info.mpd.status = "Checking status...";
4481 #endif
4482         use_spacer = 0;
4483 #ifdef X11
4484         out_to_console = 0;
4485 #else
4486         out_to_console = 1;
4487 #endif
4488 #ifdef X11
4489         default_fg_color = WhitePixel(display, screen);
4490         default_bg_color = BlackPixel(display, screen);
4491         default_out_color = BlackPixel(display, screen);
4492         draw_borders = 0;
4493         draw_shades = 1;
4494         draw_outline = 0;
4495         set_first_font("6x10");
4496         gap_x = 5;
4497         gap_y = 5;
4498         minimum_width = 5;
4499         minimum_height = 5;
4500         maximum_width = 0;
4501 #ifdef OWN_WINDOW
4502         own_window = 0;
4503         strcpy(wm_class_name, "conky"); 
4504 #endif
4505         stippled_borders = 0;
4506         border_margin = 3;
4507         border_width = 1;
4508         text_alignment = BOTTOM_LEFT;
4509         on_bottom = 1;
4510 #endif /* X11 */
4511
4512         free(current_mail_spool);
4513         {
4514                 char buf[256];
4515                 variable_substitute(MAIL_FILE, buf, 256);
4516                 if (buf[0] != '\0')
4517                         current_mail_spool = strdup(buf);
4518         }
4519
4520         no_buffers = 1;
4521         update_interval = 10.0;
4522         stuff_in_upper_case = 0;
4523 #ifdef MLDONKEY
4524         mlconfig.mldonkey_hostname = strdup("127.0.0.1");
4525         mlconfig.mldonkey_port = 4001;
4526         mlconfig.mldonkey_login = NULL;
4527         mlconfig.mldonkey_password = NULL;
4528 #endif
4529
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;
4533 #endif
4534 }
4535
4536 static void load_config_file(const char *f)
4537 {
4538 #define CONF_ERR ERR("%s: %d: config file error", f, line)
4539         int line = 0;
4540         FILE *fp;
4541
4542         set_default_configurations();
4543         fp = fopen(f, "r");
4544         if (!fp)
4545                 return;
4546
4547         while (!feof(fp)) {
4548                 char buf[256], *p, *p2, *name, *value;
4549                 line++;
4550                 if (fgets(buf, 256, fp) == NULL)
4551                         break;
4552
4553                 p = buf;
4554
4555                 /* break at comment */
4556                 p2 = strchr(p, '#');
4557                 if (p2)
4558                         *p2 = '\0';
4559
4560                 /* skip spaces */
4561                 while (*p && isspace((int) *p))
4562                         p++;
4563                 if (*p == '\0')
4564                         continue;       /* empty line */
4565
4566                 name = p;
4567
4568                 /* skip name */
4569                 p2 = p;
4570                 while (*p2 && !isspace((int) *p2))
4571                         p2++;
4572                 if (*p2 != '\0') {
4573                         *p2 = '\0';     /* break at name's end */
4574                         p2++;
4575                 }
4576
4577                 /* get value */
4578                 if (*p2) {
4579                         p = p2;
4580                         while (*p && isspace((int) *p))
4581                                 p++;
4582
4583                         value = p;
4584
4585                         p2 = value + strlen(value);
4586                         while (isspace((int) *(p2 - 1)))
4587                                 *--p2 = '\0';
4588                 } else {
4589                         value = 0;
4590                 }
4591
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)
4596
4597
4598 #ifdef X11
4599                 CONF2("alignment") {
4600         if (value) {
4601                 int a = string_to_alignment(value);
4602                 if (a <= 0)
4603                         CONF_ERR;
4604                 else
4605                         text_alignment = a;
4606         } else
4607                 CONF_ERR;
4608                 }
4609                 CONF("on_bottom") {
4610                         if(value)
4611                                 on_bottom = string_to_bool(value);
4612                         else
4613                                 CONF_ERR;
4614                 }
4615                 CONF("background") {
4616                         fork_to_background = string_to_bool(value);
4617                 }
4618
4619 #else
4620                 CONF2("background") {
4621         fork_to_background = string_to_bool(value);
4622                 }
4623 #endif /* X11 */
4624 #ifdef X11
4625                 CONF("border_margin") {
4626                         if (value)
4627                                 border_margin = strtol(value, 0, 0);
4628                         else
4629                                 CONF_ERR;
4630                 }
4631                 CONF("border_width") {
4632                         if (value)
4633                                 border_width = strtol(value, 0, 0);
4634                         else
4635                                 CONF_ERR;
4636                 }
4637                 CONF("default_color") {
4638                         if (value)
4639                                 default_fg_color = get_x11_color(value);
4640                         else
4641                                 CONF_ERR;
4642                 }
4643                 CONF3("default_shade_color", "default_shadecolor") {
4644                         if (value)
4645                                 default_bg_color = get_x11_color(value);
4646                         else
4647                                 CONF_ERR;
4648                 }
4649                 CONF3("default_outline_color", "default_outlinecolor") {
4650                         if (value)
4651                                 default_out_color = get_x11_color(value);
4652                         else
4653                                 CONF_ERR;
4654                 }
4655 #endif /* X11 */
4656 #ifdef MPD
4657                 CONF("mpd_host") {
4658                         if (value)
4659                                 strcpy(info.mpd.host, value);
4660                         else
4661                                 CONF_ERR;
4662                 }
4663                 CONF("mpd_port") {
4664                         if (value) {
4665                                 info.mpd.port = strtol(value, 0, 0);
4666                                 if (info.mpd.port < 1
4667                                     || info.mpd.port > 0xffff)
4668                                         CONF_ERR;
4669                         }
4670                 }
4671 #endif
4672                 CONF("cpu_avg_samples") {
4673                         if (value) {
4674                                 cpu_avg_samples = strtol(value, 0, 0);
4675                                 if (cpu_avg_samples < 1
4676                                     || cpu_avg_samples > 14)
4677                                         CONF_ERR;
4678                                 else
4679                                         info.
4680                                             cpu_avg_samples
4681                                             = cpu_avg_samples;
4682                         } else
4683                                 CONF_ERR;
4684                 }
4685                 CONF("net_avg_samples") {
4686                         if (value) {
4687                                 net_avg_samples = strtol(value, 0, 0);
4688                                 if (net_avg_samples < 1
4689                                     || net_avg_samples > 14)
4690                                         CONF_ERR;
4691                                 else
4692                                         info.
4693                                             net_avg_samples
4694                                             = net_avg_samples;
4695                         } else
4696                                 CONF_ERR;
4697                 }
4698
4699
4700
4701
4702
4703
4704 #ifdef XDBE
4705                 CONF("double_buffer") {
4706                 use_xdbe = string_to_bool(value);
4707                 }
4708 #endif
4709 #ifdef X11
4710                 CONF("override_utf8_locale") {
4711         utf8_mode = string_to_bool(value);
4712                 }
4713
4714                 CONF("draw_borders") {
4715                         draw_borders = string_to_bool(value);
4716                 }
4717                 CONF("draw_shades") {
4718                         draw_shades = string_to_bool(value);
4719                 }
4720                 CONF("draw_outline") {
4721                         draw_outline = string_to_bool(value);
4722                 }
4723 #endif /* X11 */
4724                 CONF("out_to_console") {
4725                         out_to_console = string_to_bool(value);
4726                 }
4727                 CONF("use_spacer") {
4728                         use_spacer = string_to_bool(value);
4729                 }
4730 #ifdef X11
4731 #ifdef XFT
4732                 CONF("use_xft") {
4733                         use_xft = string_to_bool(value);
4734                 }
4735                 CONF("font") {
4736                         if (!use_xft) {
4737                                 if (value) {
4738                                         set_first_font(value);
4739                                 } else
4740                                         CONF_ERR;
4741                         }
4742                 }
4743                 CONF("xftalpha") {
4744                         if (value && font_count >= 0)
4745                                 fonts[0].font_alpha = atof(value)
4746                                     * 65535.0;
4747                         else
4748                                 CONF_ERR;
4749                 }
4750                 CONF("xftfont") {
4751 #else
4752                 CONF("use_xft") {
4753                         if (string_to_bool(value))
4754                                 ERR("Xft not enabled");
4755                 }
4756                 CONF("xftfont") {
4757                         /* xftfont silently ignored when no Xft */
4758                 }
4759                 CONF("xftalpha") {
4760                         /* xftalpha is silently ignored when no Xft */
4761                 }
4762                 CONF("font") {
4763 #endif
4764                         if (value) {
4765                                 set_first_font(value);
4766                         } else
4767                                 CONF_ERR;
4768                 }
4769                 CONF("gap_x") {
4770                         if (value)
4771                                 gap_x = atoi(value);
4772                         else
4773                                 CONF_ERR;
4774                 }
4775                 CONF("gap_y") {
4776                         if (value)
4777                                 gap_y = atoi(value);
4778                         else
4779                                 CONF_ERR;
4780                 }
4781 #endif /* X11 */
4782                 CONF("mail_spool") {
4783                         if (value) {
4784                                 char buf[256];
4785                                 variable_substitute(value, buf, 256);
4786
4787                                 if (buf[0]
4788                                     != '\0') {
4789                                         if (current_mail_spool)
4790                                                 free(current_mail_spool);
4791                                         current_mail_spool = strdup(buf);
4792                                 }
4793                         } else
4794                                 CONF_ERR;
4795                 }
4796 #ifdef X11
4797                 CONF("minimum_size") {
4798         if (value) {
4799                 if (sscanf
4800                                   (value, "%d %d", &minimum_width,
4801                                    &minimum_height) != 2)
4802                         if (sscanf
4803                                                  (value, "%d",
4804                                                    &minimum_width) != 1)
4805                                 CONF_ERR;
4806         } else
4807                 CONF_ERR;
4808                 }
4809                 CONF("maximum_width") {
4810                         if (value) {
4811                                 if (sscanf(value, "%d", &maximum_width) != 1)
4812                                         CONF_ERR;
4813                         } else
4814                                 CONF_ERR;
4815                 }
4816 #endif /* X11 */
4817                 CONF("no_buffers") {
4818                         no_buffers = string_to_bool(value);
4819                 }
4820 #ifdef MLDONKEY
4821                 CONF("mldonkey_hostname") {
4822                         if (value) {
4823                                 if (mlconfig.mldonkey_hostname != NULL) {
4824                                         free(mlconfig.mldonkey_hostname);
4825                                 }
4826                         mlconfig.mldonkey_hostname = strdup(value);
4827                         }
4828                         else
4829                                 CONF_ERR;
4830                 }
4831                 CONF("mldonkey_port") {
4832                         if (value)
4833                                 mlconfig.mldonkey_port = atoi(value);
4834                         else
4835                                 CONF_ERR;
4836                 }
4837                 CONF("mldonkey_login") {
4838                         if (value) {
4839                                 if (mlconfig.mldonkey_login != NULL) {
4840                                         free(mlconfig.mldonkey_login);
4841                                 }
4842                                 mlconfig.mldonkey_login = strdup(value);
4843                         }
4844                         else
4845                                 CONF_ERR;
4846                 }
4847                 CONF("mldonkey_password") {
4848                         if (value) {
4849                                 if (mlconfig.mldonkey_password != NULL) {
4850                                         free(mlconfig.mldonkey_password);
4851                                 }
4852                                 mlconfig.mldonkey_password = strdup(value);
4853                         }
4854                         else
4855                                 CONF_ERR;
4856                 }
4857 #endif
4858                 CONF("pad_percents") {
4859         pad_percents = atoi(value);
4860                 }
4861 #ifdef X11
4862 #ifdef OWN_WINDOW
4863                 CONF("own_window") {
4864                         own_window = string_to_bool(value);
4865                 }
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;
4869                 }
4870                 CONF("own_window_transparent") {
4871                         set_transparent = string_to_bool(value);
4872                 }
4873                 CONF("own_window_colour") {
4874                         background_colour = get_x11_color(value);
4875                 }
4876 #endif
4877                 CONF("stippled_borders") {
4878                         if (value)
4879                                 stippled_borders = strtol(value, 0, 0);
4880                         else
4881                                 stippled_borders = 4;
4882                 }
4883 #endif /* X11 */
4884                 CONF("temp1") {
4885                         ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
4886                 }
4887                 CONF("temp1") {
4888                         ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
4889                 }
4890                 CONF("update_interval") {
4891                         if (value)
4892                                 update_interval = strtod(value, 0);
4893                         else
4894                                 CONF_ERR;
4895                 }
4896                 CONF("total_run_times") {
4897                         if (value)
4898                                 total_run_times = strtod(value, 0);
4899                         else
4900                                 CONF_ERR;
4901                 }
4902                 CONF("uppercase") {
4903                         stuff_in_upper_case = string_to_bool(value);
4904                 }
4905 #ifdef SETI
4906                 CONF("seti_dir") {
4907                         seti_dir = (char *)
4908                             malloc(strlen(value)
4909                                    + 1);
4910                         strcpy(seti_dir, value);
4911                 }
4912 #endif
4913                 CONF("text") {
4914                         if (text != original_text)
4915                                 free(text);
4916
4917                         text = (char *)
4918                             malloc(1);
4919                         text[0]
4920                             = '\0';
4921
4922                         while (!feof(fp)) {
4923                                 unsigned
4924                                 int l = strlen(text);
4925                                 if (fgets(buf, 256, fp) == NULL)
4926                                         break;
4927                                 text = (char *)
4928                                     realloc(text, l + strlen(buf)
4929                                             + 1);
4930                                 strcat(text, buf);
4931
4932                                 if (strlen(text) > 1024 * 8)
4933                                         break;
4934                         }
4935                         fclose(fp);
4936                         return;
4937                 }
4938 #ifdef TCP_PORT_MONITOR
4939                 CONF("min_port_monitors") 
4940                 {
4941                         if ( !value || 
4942                              (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) || 
4943                              tcp_port_monitor_collection_args.min_port_monitors < 0 )
4944                         {
4945                                 /* an error. use default, warn and continue. */
4946                                 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4947                                 CONF_ERR;
4948                         }
4949                         else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
4950                         {
4951                                 /* no error, just use default */
4952                                 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4953                         }
4954                         /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
4955                 }
4956                 CONF("min_port_monitor_connections") 
4957                 {
4958                         if ( !value || 
4959                              (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1) 
4960                              || tcp_port_monitor_args.min_port_monitor_connections < 0 )
4961                         {
4962                                 /* an error. use default, warni and continue. */
4963                                 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4964                                 CONF_ERR;
4965                         }
4966                         else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
4967                         {
4968                                 /* no error, just use default */
4969                                 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4970                         }
4971                         /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
4972                 }
4973 #endif
4974                 else
4975                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
4976
4977 #undef CONF
4978 #undef CONF2
4979         }
4980
4981         fclose(fp);
4982 #undef CONF_ERR
4983 }
4984
4985                                                                                                                                                                                         /* : means that character before that takes an argument */
4986 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
4987 #ifdef X11
4988                 "x:y:w:a:f:"
4989 #ifdef OWN_WINDOW
4990     "o"
4991 #endif
4992 #ifdef XDBE
4993     "b"
4994 #endif
4995 #endif /* X11 */
4996     ;
4997
4998
4999 int main(int argc, char **argv)
5000 {
5001         struct sigaction act, oact;
5002
5003         g_signal_pending=0;
5004         memset(&info, 0, sizeof(info) );
5005
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;
5009 #endif
5010                 
5011         /* handle command line parameters that don't change configs */
5012 #ifdef X11
5013         char *s;
5014         char temp[10];
5015         unsigned int x;
5016
5017         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && 
5018                      *s) || ((s = getenv("LANG")) && *s)) {
5019                 strcpy(temp, s);
5020                 for(x = 0; x < strlen(s) ; x++) {
5021                         temp[x] = tolower(s[x]);
5022                 }
5023                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5024                         utf8_mode = 1;
5025                 }
5026         }
5027         if (!setlocale(LC_CTYPE, "")) {
5028                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5029         }
5030 #endif /* X11 */
5031         while (1) {
5032                 int c = getopt(argc,
5033                                argv,
5034                                getopt_string);
5035                 if (c == -1)
5036                         break;
5037
5038                 switch (c) {
5039                 case 'v':
5040                 case 'V':
5041                         printf
5042                             ("Conky " VERSION " compiled " __DATE__ "\n");
5043                         return 0;
5044
5045                 case 'c':
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 */
5048                         if (current_config)
5049                                 free(current_config);
5050                         current_config = strdup(optarg);
5051                         break;
5052
5053                 case 'h':
5054                         printf
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"
5058                                         "file.\n"
5059                                         "   -V            version\n"
5060                                         "   -c FILE       config file to load instead of "
5061                                         CONFIG_FILE
5062                                         "\n"
5063                                         "   -d            daemonize, fork to background\n"
5064                                         "   -h            help\n"
5065 #ifdef X11
5066                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
5067                                         "   -f FONT       font to use\n"
5068 #ifdef OWN_WINDOW
5069                                         "   -o            create own window to draw\n"
5070 #endif
5071 #ifdef XDBE
5072                                         "   -b            double buffer (prevents flickering)\n"
5073 #endif
5074                                         "   -w WIN_ID     window id to draw\n"
5075                                         "   -x X          x position\n"
5076                                         "   -y Y          y position\n"
5077 #endif /* X11 */
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]);
5081                         return 0;
5082 #ifdef X11
5083                 case 'w':
5084                         window.window = strtol(optarg, 0, 0);
5085                         break;
5086 #endif /* X11 */
5087
5088                 case '?':
5089                         exit(EXIT_FAILURE);
5090                 }
5091         }
5092 #ifdef X11
5093         /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
5094         init_X11();
5095 #endif /* X11 */
5096
5097         /* load current_config or CONFIG_FILE */
5098
5099 #ifdef CONFIG_FILE
5100         if (current_config == NULL) {
5101                 /* load default config file */
5102                 char buf[256];
5103
5104                 variable_substitute(CONFIG_FILE, buf, 256);
5105
5106                 if (buf[0] != '\0')
5107                         current_config = strdup(buf);
5108         }
5109 #endif
5110
5111         if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
5112                 load_config_file(current_config);
5113         else { 
5114                 set_default_configurations();
5115         }
5116
5117 #ifdef MAIL_FILE
5118         if (current_mail_spool == NULL) {
5119                 char buf[256];
5120                 variable_substitute(MAIL_FILE, buf, 256);
5121
5122                 if (buf[0] != '\0')
5123                         current_mail_spool = strdup(buf);
5124         }
5125 #endif
5126
5127         /* handle other command line arguments */
5128
5129 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
5130         optind = optreset = 1;
5131 #else
5132         optind = 0;
5133 #endif
5134         
5135         while (1) {
5136                 int c = getopt(argc,
5137                                argv,
5138                                getopt_string);
5139                 if (c == -1)
5140                         break;
5141
5142                 switch (c) {
5143                 case 'd':
5144                         fork_to_background = 1;
5145                         break;
5146
5147 #ifdef X11
5148                         case 'f':
5149                         set_first_font(optarg);
5150                         break;
5151                         case 'a':
5152                                 text_alignment = string_to_alignment(optarg);
5153                                 break;
5154
5155 #ifdef OWN_WINDOW
5156                 case 'o':
5157                         own_window = 1;
5158                         break;
5159 #endif
5160 #ifdef XDBE
5161                 case 'b':
5162                                 use_xdbe = 1;
5163                         break;
5164 #endif
5165 #endif /* X11 */
5166                 case 't':
5167                         if (text != original_text)
5168                                 free(text);
5169                         text = strdup(optarg);
5170                         convert_escapes(text);
5171                         break;
5172
5173                 case 'u':
5174                         update_interval = strtod(optarg, 0);
5175                         break;
5176
5177                 case 'i':
5178                         total_run_times = strtod(optarg, 0);
5179                         break;
5180 #ifdef X11
5181                 case 'x':
5182                         gap_x = atoi(optarg);
5183                         break;
5184
5185                 case 'y':
5186                         gap_y = atoi(optarg);
5187                         break;
5188 #endif /* X11 */
5189
5190                 case '?':
5191                         exit(EXIT_FAILURE);
5192                 }
5193         }
5194
5195 #ifdef X11
5196         /* load font */
5197         load_fonts();
5198 #endif /* X11 */
5199
5200         /* generate text and get initial size */
5201         extract_variable_text(text);
5202         if (text != original_text) {
5203                 free(text);
5204         }
5205         text = NULL;
5206
5207         update_uname();
5208
5209         generate_text();
5210 #ifdef X11
5211         update_text_area();     /* to get initial size of the window */
5212
5213 #if defined OWN_WINDOW
5214         init_window
5215             (own_window,
5216              wm_class_name,
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);
5220 #else
5221         init_window
5222                 (own_window,
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);
5226         
5227 #endif
5228
5229         update_text_area();     /* to position text/window on screen */
5230 #endif /* X11 */
5231
5232 /*#ifdef CAIRO
5233 // why the fuck not?
5234 //do_it();
5235 #endif*/
5236
5237 #ifdef X11
5238 #ifdef OWN_WINDOW
5239         if (own_window && !fixed_pos) {
5240                 XMoveWindow(display, window.window, window.x, window.y);
5241         }
5242         if (own_window) {
5243                 set_transparent_background(window.window);
5244         }
5245 #endif
5246
5247         create_gc();
5248
5249         set_font();
5250         draw_stuff();
5251 #endif /* X11 */
5252
5253         /* fork */
5254         if (fork_to_background) {
5255                 int ret = fork();
5256                 switch (ret) {
5257                 case -1:
5258                         ERR("can't fork() to background: %s",
5259                             strerror(errno));
5260                         break;
5261
5262                 case 0:
5263                         break;
5264
5265                 default:
5266                         fprintf
5267                             (stderr,
5268                              "Conky: forked to background, pid is %d\n",
5269                              ret);
5270                         return 0;
5271                 }
5272         }
5273
5274         /* Set signal handlers */
5275         act.sa_handler = signal_handler;
5276         sigemptyset(&act.sa_mask);
5277         act.sa_flags = 0;
5278 #ifdef SA_RESTART
5279         act.sa_flags |= SA_RESTART;
5280 #endif
5281
5282         if ( sigaction(SIGINT,&act,&oact) < 0 ||
5283              sigaction(SIGUSR1,&act,&oact) < 0 ||
5284              sigaction(SIGTERM,&act,&oact) < 0 )
5285         {
5286                 ERR("error setting signal handler: %s", strerror(errno) );
5287         }
5288
5289         main_loop();
5290
5291         return 0;
5292 }
5293
5294 void signal_handler(int sig)
5295 {
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;
5300 }