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