lol
[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 <termios.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 #include <X11/extensions/Xdamage.h>
30 #ifdef IMLIB2
31 #include <Imlib2.h>
32 #endif /* IMLIB2 */
33 #endif /* X11 */
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
38
39
40 #ifdef HAVE_ICONV
41 #include <iconv.h>
42 #endif
43
44 #include "build.h"
45
46 #define CONFIG_FILE "$HOME/.conkyrc"
47 #define MAIL_FILE "$MAIL"
48 #define MAX_IF_BLOCK_DEPTH 5
49
50 /* #define SIGNAL_BLOCKING */
51 #undef SIGNAL_BLOCKING
52
53 static void print_version()
54 {
55         printf("Conky %s compiled %s for %s\n",
56                         VERSION, BUILD_DATE, BUILD_ARCH);
57
58         printf(
59         "\nCompiled in features:\n\n"
60 #ifdef X11
61         " X11:\n"
62 # ifdef XFT
63         "  * xft\n"
64 # endif /* XFT */
65 # ifdef HAVE_XDAMAGE
66         "  * Xdamage extension\n"
67 # endif /* HAVE_XDAMAGE */
68 # ifdef HAVE_XDBE
69         "  * Xdbe extension (double buffer)\n"
70 # endif /* HAVE_XDBE */
71 #endif /* X11 */
72         "\n Music detection:\n"
73 #ifdef XMMS
74         "  * xmms\n"
75 #endif /* XMMS */
76 #ifdef BMP
77         "  * bmp\n"
78 #endif /* BMP */
79 #ifdef AUDACIOUS
80         "  * audacious\n"
81 #endif /* AUDACIOUS */
82 #ifdef INFOPIPE
83         "  * infopipe\n"
84 #endif /* INFOPIPE */
85 #ifdef BMPX
86         "  * bmpx\n"
87 #endif /* BMPX */
88 #ifdef XMMS2
89         "  * xmms2\n"
90 #endif /* XMMS2 */
91 #ifdef MPD
92         "  * mpd\n"
93 #endif /* MPD */
94         "\n General features:\n"
95 #ifdef TCP_PORT_MONITOR
96         "  * portmon\n"
97 #endif /* TCP_PORT_MONITOR */
98 #ifdef MLDONKEY
99         "  * mldonkey\n"
100 #endif /* MLDONKEY */
101 #ifdef HDDTEMP
102         "  * hddtemp\n"
103 #endif /* HDDTEMP */
104 #ifdef SETI
105         "  * seti\n"
106 #endif /* SETI*/
107         "\n");  
108
109         exit(0);
110 }
111
112 #ifdef X11
113
114 /*
115  * text size
116  */
117
118 static int text_start_x, text_start_y;  /* text start position in window */
119 static int text_width, text_height;
120
121 /* alignments */
122 enum alignment {
123         TOP_LEFT = 1,
124         TOP_RIGHT,
125         BOTTOM_LEFT,
126         BOTTOM_RIGHT,
127         NONE
128 };
129
130
131 /* for fonts */
132 struct font_list {
133
134         char name[TEXT_BUFFER_SIZE];
135         int num;
136         XFontStruct *font;
137
138 #ifdef XFT
139         XftFont *xftfont;
140         int font_alpha;
141 #endif  
142
143 };
144 static int selected_font = 0;
145 static int font_count = -1;
146 struct font_list *fonts = NULL;
147
148 #ifdef XFT
149
150 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
151 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent))
152 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent)
153 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent)
154
155 #else
156
157 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
158 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
159 #define font_descent() fonts[selected_font].font->max_bounds.descent
160
161 #endif
162
163 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
164
165
166 static void set_font();
167
168 int addfont(const char *data_in)
169 {
170         if (font_count > MAX_FONTS) {
171                 CRIT_ERR("you don't need that many fonts, sorry.");
172         }
173         font_count++;
174         if (font_count == 0) {
175                 if (fonts != NULL) {
176                         free(fonts);
177                 }
178                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
179                         CRIT_ERR("malloc");
180                 }
181         }
182         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
183         if (fonts == NULL) {
184                 CRIT_ERR("realloc in addfont");
185         }
186         if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
187                 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
188 #ifdef XFT
189                 fonts[font_count].font_alpha = 0xffff;
190 #endif
191         } else {
192                 CRIT_ERR("Oops...looks like something overflowed in addfont().");
193         }
194         return font_count;
195 }
196
197 void set_first_font(const char *data_in)
198 {
199         if (font_count < 0) {
200                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
201                         CRIT_ERR("malloc");
202                 }
203                 font_count++;
204         }
205         if (strlen(data_in) > 1) {
206                 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
207 #ifdef XFT
208                 fonts[0].font_alpha = 0xffff;
209 #endif
210         }
211 }
212
213 void free_fonts()
214 {
215         int i;
216         for (i=0;i<=font_count;i++) {
217 #ifdef XFT
218                 if (use_xft) {
219                         XftFontClose(display, fonts[i].xftfont);
220                 } else
221 #endif
222                 {
223                         XFreeFont(display, fonts[i].font);
224                 }
225 }
226         free(fonts);
227         fonts = NULL;
228         font_count = -1;
229         selected_font = 0;
230         set_first_font("6x10");
231 }
232
233
234 static void load_fonts()
235 {
236         int i;
237         for (i=0; i <= font_count; i++) {
238 #ifdef XFT
239         /* load Xft font */
240         if (use_xft) {
241         /*if (fonts[i].xftfont != NULL && selected_font == 0) {
242                         XftFontClose(display, fonts[i].xftfont);
243         }*/
244                 if ((fonts[i].xftfont =
245                         XftFontOpenName(display, screen, fonts[i].name)) != NULL)
246                         continue;
247                 
248                 ERR("can't load Xft font '%s'", fonts[i].name);
249                 if ((fonts[i].xftfont =
250                         XftFontOpenName(display, screen,
251                                         "courier-12")) != NULL)
252                         continue;
253                 
254                 ERR("can't load Xft font '%s'", "courier-12");
255                 
256                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
257                         CRIT_ERR("can't load font '%s'", "fixed");
258                 }
259                 use_xft = 0;
260                 
261                 continue;
262         }
263 #endif
264         /* load normal font */
265 /*      if (fonts[i].font != NULL)
266                 XFreeFont(display, fonts[i].font);*/
267         
268         if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
269                 ERR("can't load font '%s'", fonts[i].name);
270                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
271                         CRIT_ERR("can't load font '%s'", "fixed");
272                         printf("loaded fixed?\n");
273                 }
274         }
275         }
276 }
277
278 #endif /* X11 */
279
280 /* default config file */
281 static char *current_config;
282
283 /* set to 1 if you want all text to be in uppercase */
284 static unsigned int stuff_in_upper_case;
285
286 /* Update interval */
287 static double update_interval;
288
289 /* Run how many times? */
290 static unsigned long total_run_times;
291
292 /* fork? */
293 static int fork_to_background;
294
295 static int cpu_avg_samples, net_avg_samples;
296
297 #ifdef X11
298
299 /* Always on bottom */
300 static int on_bottom;
301
302 /* Position on the screen */
303 static int text_alignment;
304 static int gap_x, gap_y;
305
306 /* border */
307 static int draw_borders;
308 static int draw_graph_borders;
309 static int stippled_borders;
310
311 static int draw_shades, draw_outline;
312
313 static int border_margin, border_width;
314
315 static long default_fg_color, default_bg_color, default_out_color;
316
317 /* create own window or draw stuff to root? */
318 static int set_transparent = 0;
319
320
321 #ifdef OWN_WINDOW
322 static int own_window = 0;
323 static int background_colour = 0;
324 /* fixed size/pos is set if wm/user changes them */
325 static int fixed_size = 0, fixed_pos = 0;
326 #endif
327
328 static int minimum_width, minimum_height;
329 static int maximum_width;
330
331 #endif /* X11 */
332
333 #ifdef HAVE_ICONV
334 #define CODEPAGE_LENGTH 20
335 long iconv_selected;
336 long iconv_count;
337 char iconv_converting;
338 static iconv_t **iconv_cd = 0;
339
340 int register_iconv(iconv_t *new_iconv)
341 {
342         if (iconv_cd) {
343                 iconv_cd = realloc(iconv, sizeof(iconv_t *) * (iconv_count + 1));
344         } else {
345                 iconv_cd = malloc(sizeof(iconv_t *));
346         }
347         if (!iconv_cd) {
348                 CRIT_ERR("Out of memory");
349         }
350         iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
351         if (!iconv_cd[iconv_count]) {
352                 CRIT_ERR("Out of memory");
353         }
354         memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
355         iconv_count++;
356         return iconv_count;
357 }
358
359 void free_iconv(void)
360 {
361         if (iconv_cd) {
362                 long i;
363                 for (i = iconv_count; i < 0; i++) {
364                         if (iconv_cd[i]) {
365                                 free(iconv_cd[i]);
366                         }
367                 }
368                 free(iconv_cd);
369         }
370         iconv_cd = 0;
371                 
372 }
373
374 #endif
375
376 /* UTF-8 */
377 int utf8_mode = 0;
378
379 /* no buffers in used memory? */
380 int no_buffers;
381
382 /* pad percentages to decimals? */
383 static int pad_percents = 0;
384
385 #ifdef TCP_PORT_MONITOR
386 tcp_port_monitor_collection_args_t      tcp_port_monitor_collection_args;
387 tcp_port_monitor_args_t                 tcp_port_monitor_args;
388 #endif
389
390 /* Text that is shown */
391 static char original_text[] =
392     "$nodename - $sysname $kernel on $machine\n"
393     "$hr\n"
394     "${color grey}Uptime:$color $uptime\n"
395     "${color grey}Frequency (in MHz):$color $freq\n"
396     "${color grey}Frequency (in GHz):$color $freq_g\n"
397     "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
398     "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
399     "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
400     "${color grey}Processes:$color $processes  ${color grey}Running:$color $running_processes\n"
401     "$hr\n"
402     "${color grey}File systems:\n"
403     " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
404     "${color grey}Networking:\n"
405     " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
406     "$hr\n"
407 #ifdef SETI
408     "${color grey}SETI@Home Statistics:\n"
409     "${color grey}Seti Unit Number:$color $seti_credit\n"
410     "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
411 #endif
412 #ifdef MPD
413     "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
414     "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
415 #endif
416 #ifdef XMMS2
417     "${color grey}XMMS2: $xmms2_status $xmms2_artist - $xmms2_title from $xmms2_album\n"
418     "Progress: $xmms2_bar\n"
419 #endif
420     "${color grey}Name          PID     CPU%    MEM%\n"
421     " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
422     " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
423     " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
424     " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
425     "${tail /var/log/Xorg.0.log 3}";
426
427 static char *text = original_text;
428 long text_lines;
429
430 static int total_updates;
431
432 /* if-blocks */
433 static int blockdepth = 0;
434 static int if_jumped = 0;
435 static int blockstart[MAX_IF_BLOCK_DEPTH];
436
437 int check_mount(char *s)
438 {
439 #if defined(__linux__)
440         int ret = 0;
441         FILE *mtab = fopen("/etc/mtab", "r");
442         if (mtab) {
443                 char buf1[256], buf2[128];
444                 while (fgets(buf1, 256, mtab)) {
445                         sscanf(buf1, "%*s %128s", buf2);
446                         if (!strcmp(s, buf2)) {
447                                 ret = 1;
448                                 break;
449                         }
450                 }
451                 fclose(mtab);
452         } else {
453                 ERR("Could not open mtab");
454         }
455         return ret;
456 #elif defined(__FreeBSD__)
457         struct statfs *mntbuf;
458         int i, mntsize;
459
460         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
461         for (i = mntsize - 1; i >= 0; i--)
462                 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
463                         return 1;
464
465         return 0;
466 #endif
467 }
468
469
470 #ifdef X11
471 static inline int calc_text_width(const char *s, int l)
472 {
473 #ifdef XFT
474         if (use_xft) {
475                 XGlyphInfo gi;
476                 if (utf8_mode) {
477                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
478                 } else {
479                         XftTextExtents8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
480                 }
481                 return gi.xOff;
482         } else
483 #endif
484         {
485                 return XTextWidth(fonts[selected_font].font, s, l);
486         }
487 }
488 #endif /* X11 */
489
490 /* formatted text to render on screen, generated in generate_text(),
491  * drawn in draw_stuff() */
492
493 static char text_buffer[TEXT_BUFFER_SIZE * 4];
494
495 /* special stuff in text_buffer */
496
497 #define SPECIAL_CHAR '\x01'
498
499 enum {
500         HORIZONTAL_LINE,
501         STIPPLED_HR,
502         BAR,
503         FG,
504         BG,
505         OUTLINE,
506         ALIGNR,
507         ALIGNC,
508         GRAPH,
509         OFFSET,
510         VOFFSET,
511         FONT,
512         GOTO,
513         TAB,
514 };
515
516 static struct special_t {
517         int type;
518         short height;
519         short width;
520         long arg;
521         double *graph;
522         double graph_scale;
523         int graph_width;
524         int scaled;
525         short font_added;
526         unsigned long first_colour; // for graph gradient
527         unsigned long last_colour;
528 } specials[128];
529
530 static int special_count;
531 #ifdef X11
532 static int special_index;       /* used when drawing */
533 #endif /* X11 */
534
535 #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? */
536
537 static struct special_t *new_special(char *buf, int t)
538 {
539         if (special_count >= 512)
540                 CRIT_ERR("too many special things in text");
541
542         buf[0] = SPECIAL_CHAR;
543         buf[1] = '\0';
544         specials[special_count].type = t;
545         return &specials[special_count++];
546 }
547
548 typedef struct tailstring_list {
549         char data[TEXT_BUFFER_SIZE];
550         struct tailstring_list *next;
551         struct tailstring_list *first;
552 } tailstring;
553
554 void addtail(tailstring ** head, char *data_in)
555 {
556         tailstring *tmp;
557         if ((tmp = malloc(sizeof(*tmp))) == NULL) {
558                 CRIT_ERR("malloc");
559         }
560         if (*head == NULL) {
561                 tmp->first = tmp;
562         } else {
563                 tmp->first = (*head)->first;
564         }
565         strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
566         tmp->next = *head;
567         *head = tmp;
568 }
569
570 void freetail(tailstring * head)
571 {
572         tailstring *tmp;
573         while (head != NULL) {
574                 tmp = head->next;
575                 free(head);
576                 head = tmp;
577         }
578 }
579
580 void freelasttail(tailstring * head)
581 {
582         tailstring * tmp = head;
583         while(tmp != NULL) {
584                 if (tmp->next == head->first) {
585                         tmp->next = NULL;
586                         break;
587                 }
588                 tmp = tmp->next;
589         }
590         free(head->first);
591         while(head != NULL && tmp != NULL) {
592                 head->first = tmp;
593                 head = head->next;
594         }
595 }
596
597 static void new_bar(char *buf, int w, int h, int usage)
598 {
599         struct special_t *s = new_special(buf, BAR);
600         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
601         s->width = w;
602         s->height = h;
603 }
604
605 static const char *scan_bar(const char *args, int *w, int *h)
606 {
607         *w = 0;                 /* zero width means all space that is available */
608         *h = 6;
609         /* bar's argument is either height or height,width */
610         if (args) {
611                 int n = 0;
612                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
613                         sscanf(args, "%d %n", h, &n);
614                 args += n;
615         }
616
617         return args;
618 }
619
620 static char *scan_font(const char *args)
621 {
622         if (args && sizeof(args) < 127) {
623                 return strdup(args);
624         }
625         return NULL;
626 }
627
628 #ifdef X11
629 static void new_font(char *buf, char * args) {
630         if (args) {
631                 struct special_t *s = new_special(buf, FONT);
632                 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
633                         int tmp = selected_font;
634                         selected_font = s->font_added = addfont(args);
635                         load_fonts();
636                         selected_font = tmp;
637                 }
638         } else {
639                 struct special_t *s = new_special(buf, FONT);
640                 int tmp = selected_font;
641                 selected_font = s->font_added = 0;
642                 load_fonts();
643                 selected_font = tmp;
644         }
645 }
646 #endif
647
648 inline void graph_append(struct special_t *graph, double f)
649 {
650         if (!graph->scaled && f > graph->graph_scale) {
651                 f = graph->graph_scale;
652         }
653         int i;
654         if (graph->scaled) {
655                 graph->graph_scale = 1;
656         }
657         graph->graph[0] = f; /* add new data */
658         for (i = graph->graph_width - 1; i > 0; i--) { /* shift all the data by 1 */
659                 graph->graph[i] = graph->graph[i - 1];
660                 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
661                         graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
662                 }
663         }
664 }
665
666 short colour_depth = 0;
667 void set_up_gradient();
668
669 /* precalculated: 31/255, and 63/255 */
670 #define CONST_8_TO_5_BITS 0.12156862745098
671 #define CONST_8_TO_6_BITS 0.247058823529412
672
673 /* adjust color values depending on color depth*/
674 static unsigned int adjust_colors(unsigned int color)
675 {
676         double r, g, b;
677         if (colour_depth == 0) {
678                 set_up_gradient();
679         }
680         if (colour_depth == 16) {
681                 r = (color & 0xff0000) >> 16;
682                 g = (color & 0xff00) >> 8;
683                 b =  color & 0xff;
684                 color  = (int)(r * CONST_8_TO_5_BITS) << 11;
685                 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
686                 color |= (int)(b * CONST_8_TO_5_BITS);
687         }
688         return color;
689 }
690
691 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
692 {
693         struct special_t *s = new_special(buf, GRAPH);
694         s->width = w;
695         if (s->graph == NULL) {
696                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
697                         s->graph_width = s->width/* - 2*/;      // subtract 2 for the box
698                 } else {
699                         s->graph_width = MAX_GRAPH_DEPTH - 2;
700                 }
701                 s->graph = malloc(s->graph_width * sizeof(double));
702                 memset(s->graph, 0, s->graph_width * sizeof(double));
703                 s->graph_scale = 100;
704         }
705         s->height = h;
706         s->first_colour = adjust_colors(first_colour);
707         s->last_colour = adjust_colors(second_colour);
708         if (scale != 0) {
709                 s->scaled = 0;
710         } else {
711                 s->scaled = 1;
712         }
713         /*if (s->width) {
714                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
715         }*/
716         if (s->scaled) {
717                 s->graph_scale = 1;
718         } else {
719                 s->graph_scale = scale;
720         }
721         if (append) {
722                 graph_append(s, i);
723         }
724 }
725
726 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
727 {
728         *w = 0;                 /* zero width means all space that is available */
729         *h = 25;
730         *first_colour = 0;
731         *last_colour = 0;
732         /* graph's argument is either height or height,width */
733         if (args) {
734                 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
735                         if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
736                                 *scale = 0;
737                                 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
738                                         *scale = 0;
739                                         if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
740                                                 *w = 0;
741                                 *h = 25;                        
742                                 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
743                                 *w = 0;
744                                 *h = 25;
745                                 *scale = 0;
746                                 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
747                                         *w = 0;
748                                         *h = 25;
749                                         if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
750                                                 *first_colour = 0;
751                                                 *last_colour = 0;
752                                                 *scale = 0;
753                                                 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
754                                         *first_colour = 0;
755                                         *last_colour = 0;
756                                         if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
757                                                 *first_colour = 0;
758                                                 *last_colour = 0;
759                                                 *scale = 0;
760                                                 if (sscanf(args, "%d,%d", h, w) < 2) {
761                                                         *first_colour = 0;
762                                                         *last_colour = 0;
763                                                         sscanf(args, "%*s %d,%d", h, w);
764         }}}}}}}}}}} // haha
765         return args;
766 }
767
768
769 static inline void new_hr(char *buf, int a)
770 {
771         new_special(buf, HORIZONTAL_LINE)->height = a;
772 }
773
774 static inline void new_stippled_hr(char *buf, int a, int b)
775 {
776         struct special_t *s = new_special(buf, STIPPLED_HR);
777         s->height = b;
778         s->arg = a;
779 }
780
781 static inline void new_fg(char *buf, long c)
782 {
783         new_special(buf, FG)->arg = c;
784 }
785
786 static inline void new_bg(char *buf, long c)
787 {
788         new_special(buf, BG)->arg = c;
789 }
790
791 static inline void new_outline(char *buf, long c)
792 {
793         new_special(buf, OUTLINE)->arg = c;
794 }
795
796 static inline void new_offset(char *buf, long c)
797 {
798         new_special(buf, OFFSET)->arg = c;
799 }
800
801 static inline void new_voffset(char *buf, long c)
802 {
803         new_special(buf, VOFFSET)->arg = c;
804 }
805
806 static inline void new_alignr(char *buf, long c)
807 {
808         new_special(buf, ALIGNR)->arg = c;
809 }
810
811 static inline void new_alignc(char *buf, long c)
812 {
813         new_special(buf, ALIGNC)->arg = c;
814 }
815
816 static inline void new_goto(char *buf, long c)
817 {
818         new_special(buf, GOTO)->arg = c;
819 }
820
821 static inline void new_tab(char *buf, int a, int b) {
822         struct special_t *s = new_special(buf, TAB);
823         s->width = a;
824         s->arg = b;
825 }
826
827 /* quite boring functions */
828
829 static inline void for_each_line(char *b, void (*f) (char *))
830 {
831         char *ps, *pe;
832
833         for (ps = b, pe = b; *pe; pe++) {
834                 if (*pe == '\n') {
835                         *pe = '\0';
836                         f(ps);
837                         *pe = '\n';
838                         ps = pe + 1;
839                 }
840         }
841
842         if (ps < pe)
843                 f(ps);
844 }
845
846 static void convert_escapes(char *buf)
847 {
848         char *p = buf, *s = buf;
849
850         while (*s) {
851                 if (*s == '\\') {
852                         s++;
853                         if (*s == 'n')
854                                 *p++ = '\n';
855                         else if (*s == '\\')
856                                 *p++ = '\\';
857                         s++;
858                 } else
859                         *p++ = *s++;
860         }
861         *p = '\0';
862 }
863
864 /* converts from bytes to human readable format (k, M, G, T) */
865 static void human_readable(long long a, char *buf, int size)
866 {
867         // Strange conditional due to possible overflows
868         if(a / 1024 / 1024 / 1024.0 > 1024.0){
869                 snprintf(buf, size, "%.2fTiB", (a / 1024 / 1024 / 1024) / 1024.0);
870         }
871         else if (a >= 1024 * 1024 * 1024) {
872                 snprintf(buf, size, "%.2fGiB", (a / 1024 / 1024) / 1024.0);
873         }
874         else if (a >= 1024 * 1024) {
875                 double m = (a / 1024) / 1024.0;
876                 if (m >= 100.0)
877                         snprintf(buf, size, "%.0fMiB", m);
878                 else
879                         snprintf(buf, size, "%.1fMiB", m);
880         } else if (a >= 1024)
881                 snprintf(buf, size, "%LdKiB", a / (long long) 1024);
882         else
883                 snprintf(buf, size, "%LdB", a);
884 }
885
886 /* text handling */
887
888 enum text_object_type {
889         OBJ_acpiacadapter,
890         OBJ_adt746xcpu,
891         OBJ_adt746xfan,
892         OBJ_acpifan,
893         OBJ_addr,
894         OBJ_linkstatus,
895         OBJ_acpitemp,
896         OBJ_acpitempf,
897         OBJ_battery,
898         OBJ_buffers,
899         OBJ_cached,
900         OBJ_color,
901         OBJ_font,
902         OBJ_cpu,
903         OBJ_cpubar,
904         OBJ_cpugraph,
905         OBJ_diskio,
906         OBJ_diskiograph,
907         OBJ_downspeed,
908         OBJ_downspeedf,
909         OBJ_downspeedgraph,
910         OBJ_else,
911         OBJ_endif,
912         OBJ_image,
913         OBJ_exec,
914         OBJ_execi,
915         OBJ_texeci,
916         OBJ_execbar,
917         OBJ_execgraph,
918         OBJ_execibar,
919         OBJ_execigraph,
920         OBJ_freq,
921         OBJ_freq_g,
922         OBJ_freq_dyn,
923         OBJ_freq_dyn_g,
924         OBJ_fs_bar,
925         OBJ_fs_bar_free,
926         OBJ_fs_free,
927         OBJ_fs_free_perc,
928         OBJ_fs_size,
929         OBJ_fs_used,
930         OBJ_fs_used_perc,
931         OBJ_goto,
932         OBJ_tab,
933         OBJ_hr,
934         OBJ_offset,
935         OBJ_voffset,
936         OBJ_alignr,
937         OBJ_alignc,
938         OBJ_i2c,
939 #if defined(__linux__)
940         OBJ_i8k_version,
941         OBJ_i8k_bios,
942         OBJ_i8k_serial,
943         OBJ_i8k_cpu_temp,
944         OBJ_i8k_cpu_tempf,
945         OBJ_i8k_left_fan_status,
946         OBJ_i8k_right_fan_status,
947         OBJ_i8k_left_fan_rpm,
948         OBJ_i8k_right_fan_rpm,
949         OBJ_i8k_ac_status,      
950         OBJ_i8k_buttons_status,
951         OBJ_ibm_fan,
952         OBJ_ibm_temps,
953         OBJ_ibm_volume,
954         OBJ_ibm_brightness,
955         OBJ_pb_battery,
956         OBJ_voltage_mv,
957         OBJ_voltage_v,
958 #endif /* __linux__ */
959         OBJ_if_existing,
960         OBJ_if_mounted,
961         OBJ_if_running,
962         OBJ_top,
963         OBJ_top_mem,
964         OBJ_tail,
965         OBJ_head,
966         OBJ_kernel,
967         OBJ_loadavg,
968         OBJ_machine,
969         OBJ_mails,
970         OBJ_mem,
971         OBJ_membar,
972         OBJ_memgraph,
973         OBJ_memmax,
974         OBJ_memperc,
975         OBJ_mixer,
976         OBJ_mixerl,
977         OBJ_mixerr,
978         OBJ_mixerbar,
979         OBJ_mixerlbar,
980         OBJ_mixerrbar,
981         OBJ_new_mails,
982         OBJ_nodename,
983         OBJ_pre_exec,
984 #ifdef MLDONKEY
985         OBJ_ml_upload_counter,
986         OBJ_ml_download_counter,
987         OBJ_ml_nshared_files,
988         OBJ_ml_shared_counter,
989         OBJ_ml_tcp_upload_rate,
990         OBJ_ml_tcp_download_rate,
991         OBJ_ml_udp_upload_rate,
992         OBJ_ml_udp_download_rate,
993         OBJ_ml_ndownloaded_files,
994         OBJ_ml_ndownloading_files,
995 #endif
996         OBJ_processes,
997         OBJ_running_processes,
998         OBJ_shadecolor,
999         OBJ_outlinecolor,
1000         OBJ_stippled_hr,
1001         OBJ_swap,
1002         OBJ_swapbar,
1003         OBJ_swapmax,
1004         OBJ_swapperc,
1005         OBJ_sysname,
1006         OBJ_temp1,              /* i2c is used instead in these */
1007         OBJ_temp2,
1008         OBJ_text,
1009         OBJ_time,
1010         OBJ_utime,
1011         OBJ_tztime,
1012         OBJ_totaldown,
1013         OBJ_totalup,
1014         OBJ_updates,
1015         OBJ_upspeed,
1016         OBJ_upspeedf,
1017         OBJ_upspeedgraph,
1018         OBJ_uptime,
1019         OBJ_uptime_short,
1020         OBJ_imap,
1021         OBJ_imap_messages,
1022         OBJ_imap_unseen,
1023         OBJ_pop3,
1024         OBJ_pop3_unseen,
1025         OBJ_pop3_used,
1026 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
1027         OBJ_apm_adapter,
1028         OBJ_apm_battery_time,
1029         OBJ_apm_battery_life,
1030 #endif /* __FreeBSD__ */
1031 #ifdef SETI
1032         OBJ_seti_prog,
1033         OBJ_seti_progbar,
1034         OBJ_seti_credit,
1035 #endif
1036 #ifdef MPD
1037         OBJ_mpd_title,
1038         OBJ_mpd_artist,
1039         OBJ_mpd_album,
1040         OBJ_mpd_random,
1041         OBJ_mpd_repeat,
1042         OBJ_mpd_vol,
1043         OBJ_mpd_bitrate,
1044         OBJ_mpd_status,
1045         OBJ_mpd_host,
1046         OBJ_mpd_port,
1047         OBJ_mpd_password,
1048         OBJ_mpd_bar,
1049         OBJ_mpd_elapsed,
1050         OBJ_mpd_length,
1051         OBJ_mpd_track,
1052         OBJ_mpd_name,
1053         OBJ_mpd_file,
1054         OBJ_mpd_percent,
1055         OBJ_mpd_smart,
1056 #endif
1057 #ifdef XMMS2
1058     OBJ_xmms2_artist,
1059     OBJ_xmms2_album,
1060     OBJ_xmms2_title,
1061     OBJ_xmms2_genre,
1062     OBJ_xmms2_comment,
1063     OBJ_xmms2_decoder,
1064     OBJ_xmms2_transport,
1065     OBJ_xmms2_url,
1066     OBJ_xmms2_date,
1067     OBJ_xmms2_tracknr,
1068     OBJ_xmms2_bitrate,
1069     OBJ_xmms2_id,
1070     OBJ_xmms2_duration,
1071     OBJ_xmms2_elapsed,
1072     OBJ_xmms2_size,
1073         OBJ_xmms2_percent,
1074         OBJ_xmms2_status,
1075         OBJ_xmms2_bar,
1076         OBJ_xmms2_smart,
1077 #endif
1078 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
1079         OBJ_xmms_status,
1080         OBJ_xmms_title,
1081         OBJ_xmms_length,
1082         OBJ_xmms_length_seconds,
1083         OBJ_xmms_position,
1084         OBJ_xmms_position_seconds,
1085         OBJ_xmms_bitrate,
1086         OBJ_xmms_frequency,
1087         OBJ_xmms_channels,
1088         OBJ_xmms_filename,
1089         OBJ_xmms_playlist_length,
1090         OBJ_xmms_playlist_position,
1091         OBJ_xmms_bar,
1092 #endif
1093 #ifdef BMPX
1094         OBJ_bmpx_title,
1095         OBJ_bmpx_artist,
1096         OBJ_bmpx_album,
1097         OBJ_bmpx_track,
1098         OBJ_bmpx_uri,
1099         OBJ_bmpx_bitrate,
1100 #endif
1101 #ifdef TCP_PORT_MONITOR
1102         OBJ_tcp_portmon,
1103 #endif
1104
1105 #ifdef HAVE_ICONV
1106         OBJ_iconv_start,
1107         OBJ_iconv_stop,
1108 #endif
1109 #ifdef HDDTEMP
1110         OBJ_hddtemp,
1111 #endif
1112 };
1113
1114 struct text_object {
1115         int type;
1116         int a, b;
1117         long line;
1118         unsigned int c, d, e;
1119         float f;
1120         char global_mode;
1121         union {
1122                 char *s;        /* some string */
1123                 int i;          /* some integer */
1124                 long l;         /* some other integer */
1125                 unsigned int sensor;
1126                 struct net_stat *net;
1127                 struct fs_stat *fs;
1128                 unsigned char loadavg[3];
1129                 unsigned int cpu_index;
1130                 struct mail_s *mail;
1131
1132                 struct {
1133                         char *tz;    /* timezone variable */
1134                         char *fmt;   /* time display formatting */
1135                 } tztime;
1136
1137                 struct {
1138                         struct fs_stat *fs;
1139                         int w, h;
1140                 } fsbar;        /* 3 */
1141
1142                 struct {
1143                         int l;
1144                         int w, h;
1145                 } mixerbar;     /* 3 */
1146
1147                 struct {
1148                         int fd;
1149                         int arg;
1150                         char devtype[256];
1151                         char type[64];
1152                 } i2c;          /* 2 */
1153                 struct {
1154                         int pos;
1155                         char *s;
1156                 } ifblock;
1157                 struct {
1158                         int num;
1159                         int type;
1160                 } top;
1161
1162                 struct {
1163                         int wantedlines;
1164                         int readlines;
1165                         char *logfile;
1166                         double last_update;
1167                         float interval;
1168                         char *buffer;
1169                 } tail;
1170
1171                 struct {
1172                         double last_update;
1173                         float interval;
1174                         char *cmd;
1175                         char *buffer;
1176                         double data;
1177                         int pos;
1178                         struct thread_info_s thread_info;
1179                 } execi;        /* 5 */
1180
1181                 struct {
1182                         int a, b;
1183                 } pair;         /* 2 */
1184 #ifdef TCP_PORT_MONITOR
1185                 struct {
1186                         in_port_t  port_range_begin;  /* starting port to monitor */
1187                         in_port_t  port_range_end;    /* ending port to monitor */
1188                         int        item;              /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
1189                         int        connection_index;  /* 0 to n-1 connections. */
1190                 } tcp_port_monitor;
1191 #endif
1192                 struct {
1193                         char *addr;
1194                         int port;
1195                         char *dev;
1196                 } hddtemp; /* 2 */
1197         } data;
1198 };
1199
1200 struct text_object_list {
1201         unsigned int text_object_count;
1202         struct text_object *text_objects;
1203 };
1204
1205 static unsigned int text_object_count;
1206 static struct text_object *text_objects;
1207 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
1208
1209 #define MAX_THREADS 512 // sure whatever
1210 typedef struct thread_info_s *thread_info;
1211 static thread_info thread_list[MAX_THREADS];
1212 static int thread_count = 0;
1213 static int threads_runnable = 1;
1214
1215 int register_thread(struct thread_info_s *new_thread)
1216 {
1217         if (thread_count >= MAX_THREADS) {
1218                 CRIT_ERR("Uh oh, tried to register too many threads");
1219         } else {
1220                 thread_list[thread_count] = new_thread;
1221                 thread_count++;
1222                 // may as well fix the mutex for them as well
1223                 pthread_mutex_init(&(new_thread->mutex), NULL);
1224         }
1225         return thread_count - 1;
1226 }
1227
1228 #define MAXDATASIZE 1000
1229 #define POP3 1
1230 #define IMAP 2
1231
1232 struct mail_s* parse_mail_args(char type, const char *arg) {
1233         struct mail_s *mail;
1234         mail = malloc(sizeof(struct mail_s));
1235         memset(mail, 0, sizeof(struct mail_s));
1236         char *tmp;
1237         if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass) != 3) {
1238                 if (type == POP3) {
1239                         ERR("Scanning IMAP args failed");
1240                 } else  if (type == IMAP) {
1241                         ERR("Scanning POP3 args failed");
1242                 }
1243         }
1244         // see if password needs prompting
1245         if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1246                 int fp = fileno(stdin);
1247                 struct termios term;
1248                 tcgetattr(fp, &term);
1249                 term.c_lflag &= ~ECHO;
1250                 tcsetattr(fp, TCSANOW, &term);
1251                 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1252                 scanf("%128s", mail->pass);
1253                 printf("\n");
1254                 term.c_lflag |= ECHO;
1255                 tcsetattr(fp, TCSANOW, &term);
1256         }
1257         // now we check for optional args
1258         tmp = strstr(arg, "-i ");
1259         if (tmp) {
1260                 tmp += 3;
1261                 sscanf(tmp, "%f", &mail->interval);
1262         } else {
1263                 mail->interval = 300;   // 5 minutes
1264         }
1265         tmp = strstr(arg, "-p ");
1266         if (tmp) {
1267                 tmp += 3;
1268                 sscanf(tmp, "%lu", &mail->port);
1269         } else {
1270                 if (type == POP3) {
1271                         mail->port = 110;       // default pop3 port
1272                 } else if (type == IMAP) {
1273                         mail->port = 143;       // default imap port
1274                 }
1275         }
1276         if (type == IMAP) {
1277                 tmp = strstr(arg, "-f ");
1278                 if (tmp) {
1279                         tmp += 3;
1280                         sscanf(tmp, "%s", mail->folder);
1281                 } else {
1282                         strncpy(mail->folder, "INBOX", 128);    // default imap inbox
1283                 }
1284         }
1285         tmp = strstr(arg, "-e ");
1286         if (tmp) {
1287                 tmp += 3;
1288                 int len = 1024;
1289                 if (tmp[0] == '\'') {
1290                         len = strstr(tmp+1, "'") - tmp - 1;
1291                         if (len > 1024) {
1292                                 len = 1024;
1293                         }
1294                 }
1295                 strncpy(mail->command, tmp+1, len);
1296         } else {
1297                 mail->command[0] = '\0';
1298         }
1299         mail->pos = -1;
1300         return mail;
1301 }
1302
1303 void *imap_thread(struct mail_s* mail)
1304 {                               // pthreads are really beginning to piss me off
1305         double update_time;
1306         int run_code = threads_runnable;
1307         update_time = get_time();
1308         int sockfd, numbytes;
1309         char recvbuf[MAXDATASIZE];
1310         char sendbuf[MAXDATASIZE];
1311         char *reply;
1312         int fail = 0;
1313         unsigned int old_unseen = UINT_MAX;
1314         unsigned int old_messages = UINT_MAX;
1315         struct hostent *he;
1316         struct sockaddr_in their_addr;  // connector's address information
1317         if ((he = gethostbyname(mail->host)) == NULL) { // get the host info 
1318                 herror("gethostbyname");
1319                 exit(1);
1320         }
1321         while (threads_runnable == run_code && fail < 5) {
1322                 if (fail > 0) {
1323                         ERR("Trying IMAP connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1324                         sleep((int)mail->interval);
1325                 }
1326                 update_time = get_time();
1327                 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1328                         perror("socket");
1329                         fail++;
1330                         continue;
1331                 }
1332
1333                 their_addr.sin_family = AF_INET;        // host byte order 
1334                 their_addr.sin_port = htons(mail->port);        // short, network byte order 
1335                 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1336                 memset(&(their_addr.sin_zero), '\0', 8);        // zero the rest of the struct 
1337
1338                 if (connect
1339                     (sockfd, (struct sockaddr *) &their_addr,
1340                      sizeof(struct sockaddr)) == -1) {
1341                         perror("connect");
1342                         fail++;
1343                         continue;
1344                 }
1345                 struct timeval timeout;
1346                 int res;
1347                 fd_set fdset;
1348                 timeout.tv_sec = 60;    // 60 second timeout i guess
1349                 timeout.tv_usec = 0;
1350                 FD_ZERO(&fdset);
1351                 FD_SET(sockfd, &fdset);
1352                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1353                 if (res > 0) {
1354                         if ((numbytes =
1355                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1356                                   0)) == -1) {
1357                                 perror("recv");
1358                                 fail++;
1359                                 continue;
1360                         }
1361                 } else {
1362                         ERR("IMAP connection failed: timeout");
1363                         fail++;
1364                         continue;
1365                 }
1366                 recvbuf[numbytes] = '\0';
1367                 if (strstr(recvbuf, "* OK") != recvbuf) {
1368                         ERR("IMAP connection failed, probably not an IMAP server");
1369                         fail++;
1370                         continue;
1371                 }
1372                 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1373                 strncat(sendbuf, mail->user,
1374                         MAXDATASIZE - strlen(sendbuf) - 1);
1375                 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1376                 strncat(sendbuf, mail->pass,
1377                         MAXDATASIZE - strlen(sendbuf) - 1);
1378                 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1379                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1380                         perror("send a1");
1381                         fail++;
1382                         continue;
1383                 }
1384                 timeout.tv_sec = 60;    // 60 second timeout i guess
1385                 timeout.tv_usec = 0;
1386                 FD_ZERO(&fdset);
1387                 FD_SET(sockfd, &fdset);
1388                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1389                 if (res > 0) {
1390                         if ((numbytes =
1391                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1392                                   0)) == -1) {
1393                                 perror("recv a1");
1394                                 fail++;
1395                                 continue;
1396                         }
1397                 }
1398                 recvbuf[numbytes] = '\0';
1399                 if (strstr(recvbuf, "a1 OK") == NULL) {
1400                         ERR("IMAP server login failed: %s", recvbuf);
1401                         fail++;
1402                         continue;
1403                 }
1404                 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1405                 strncat(sendbuf, mail->folder,
1406                         MAXDATASIZE - strlen(sendbuf) - 1);
1407                 strncat(sendbuf, " (MESSAGES UNSEEN)\n",
1408                         MAXDATASIZE - strlen(sendbuf) - 1);
1409                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1410                         perror("send a2");
1411                         fail++;
1412                         continue;
1413                 }
1414                 timeout.tv_sec = 60;    // 60 second timeout i guess
1415                 timeout.tv_usec = 0;
1416                 FD_ZERO(&fdset);
1417                 FD_SET(sockfd, &fdset);
1418                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1419                 if (res > 0) {
1420                         if ((numbytes =
1421                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1422                                   0)) == -1) {
1423                                 perror("recv a2");
1424                                 fail++;
1425                                 continue;
1426                         }
1427                 }
1428                 recvbuf[numbytes] = '\0';
1429                 if (strstr(recvbuf, "a2 OK") == NULL) {
1430                         ERR("IMAP status failed: %s", recvbuf);
1431                         fail++;
1432                         continue;
1433                 }
1434                 // now we get the data
1435                 reply = strstr(recvbuf, " (MESSAGES ");
1436                 reply += 2;
1437                 *strchr(reply, ')') = '\0';
1438                 if (reply == NULL) {
1439                         ERR("Error parsing IMAP response: %s", recvbuf);
1440                         fail++;
1441                         continue;
1442                 } else {
1443                         pthread_mutex_lock(&(mail->thread_info.mutex));
1444                         sscanf(reply, "MESSAGES %lu UNSEEN %lu",
1445                                &mail->messages,
1446                                &mail->unseen);
1447                         pthread_mutex_unlock(&(mail->thread_info.mutex));
1448                 }
1449                 strncpy(sendbuf, "a3 logout\n", MAXDATASIZE);
1450                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1451                         perror("send a3");
1452                         fail++;
1453                         continue;
1454                 }
1455                 timeout.tv_sec = 60;    // 60 second timeout i guess
1456                 timeout.tv_usec = 0;
1457                 FD_ZERO(&fdset);
1458                 FD_SET(sockfd, &fdset);
1459                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1460                 if (res > 0) {
1461                         if ((numbytes =
1462                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1463                                   0)) == -1) {
1464                                 perror("recv a3");
1465                                 fail++;
1466                                 continue;
1467                         }
1468                 }
1469                 recvbuf[numbytes] = '\0';
1470                 if (strstr(recvbuf, "a3 OK") == NULL) {
1471                         ERR("IMAP logout failed: %s", recvbuf);
1472                         fail++;
1473                         continue;
1474                 }
1475                 close(sockfd);
1476                 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen || (mail->messages > old_messages && mail->unseen > 0))) {  // new mail goodie
1477                         if (system(mail->command) == -1) {
1478                                 perror("system()");
1479                         }
1480                 }
1481                 fail = 0;
1482                 old_unseen = mail->unseen;
1483                 old_messages = mail->messages;
1484                 mail->last_update = update_time;
1485                 usleep(100);    // prevent race condition
1486                 if (get_time() - mail->last_update >
1487                     mail->interval) {
1488                         continue;
1489                 } else {
1490                         unsigned int delay =
1491                             1000000.0 * (mail->interval -
1492                                          (get_time() -
1493                                           mail->last_update));
1494                         if (delay < update_interval * 500000) {
1495                                 delay = update_interval * 1000000;
1496                         }
1497                         usleep(delay);
1498                 }
1499         }
1500         ERR("exiting imap thread");
1501         pthread_exit(NULL);
1502         return 0;
1503 }
1504
1505 void *pop3_thread(struct mail_s *mail)
1506 {                               // pthreads are really beginning to piss me off
1507         double update_time;
1508         int run_code = threads_runnable;
1509         update_time = get_time();
1510         int sockfd, numbytes;
1511         char recvbuf[MAXDATASIZE];
1512         char sendbuf[MAXDATASIZE];
1513         char *reply;
1514         int fail = 0;
1515         unsigned int old_unseen = UINT_MAX;
1516         struct hostent *he;
1517         struct sockaddr_in their_addr;  // connector's address information
1518         if ((he = gethostbyname(mail->host)) == NULL) { // get the host info 
1519                 herror("gethostbyname");
1520                 exit(1);
1521         }
1522         while (threads_runnable == run_code && fail < 5) {
1523                 if (fail > 0) {
1524                         ERR("Trying POP3 connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1525                         sleep((int)mail->interval);
1526                 }
1527                 update_time = get_time();
1528                 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1529                         perror("socket");
1530                         fail++;
1531                         continue;
1532                 }
1533
1534                 their_addr.sin_family = AF_INET;        // host byte order 
1535                 their_addr.sin_port = htons(mail->port);        // short, network byte order 
1536                 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1537                 memset(&(their_addr.sin_zero), '\0', 8);        // zero the rest of the struct 
1538
1539                 if (connect
1540                     (sockfd, (struct sockaddr *) &their_addr,
1541                      sizeof(struct sockaddr)) == -1) {
1542                         perror("connect");
1543                         fail++;
1544                         continue;
1545                 }
1546                 struct timeval timeout;
1547                 int res;
1548                 fd_set fdset;
1549                 timeout.tv_sec = 60;    // 60 second timeout i guess
1550                 timeout.tv_usec = 0;
1551                 FD_ZERO(&fdset);
1552                 FD_SET(sockfd, &fdset);
1553                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1554                 if (res > 0) {
1555                         if ((numbytes =
1556                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1557                                   0)) == -1) {
1558                                 perror("recv");
1559                                 fail++;
1560                                 continue;
1561                         }
1562                 } else {
1563                         ERR("POP3 connection failed: timeout\n");
1564                         fail++;
1565                         continue;
1566                 }
1567                 recvbuf[numbytes] = '\0';
1568                 if (strstr(recvbuf, "+OK ") != recvbuf) {
1569                         ERR("POP3 connection failed, probably not a POP3 server");
1570                         fail++;
1571                         continue;
1572                 }
1573                 strncpy(sendbuf, "USER ", MAXDATASIZE);
1574                 strncat(sendbuf, mail->user,
1575                         MAXDATASIZE - strlen(sendbuf) - 1);
1576                 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1577                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1578                         perror("send USER");
1579                         fail++;
1580                         continue;
1581                 }
1582                 timeout.tv_sec = 60;    // 60 second timeout i guess
1583                 timeout.tv_usec = 0;
1584                 FD_ZERO(&fdset);
1585                 FD_SET(sockfd, &fdset);
1586                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1587                 if (res > 0) {
1588                         if ((numbytes =
1589                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1590                                   0)) == -1) {
1591                                 perror("recv USER");
1592                                 fail++;
1593                                 continue;
1594                         }
1595                 }
1596                 recvbuf[numbytes] = '\0';
1597                 if (strstr(recvbuf, "+OK ") == NULL) {
1598                         ERR("POP3 server login failed: %s", recvbuf);
1599                         fail++;
1600                         continue;
1601                 }
1602                 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1603                 strncat(sendbuf, mail->pass,
1604                         MAXDATASIZE - strlen(sendbuf) - 1);
1605                 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1606                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1607                         perror("send PASS");
1608                         fail++;
1609                         continue;
1610                 }
1611                 timeout.tv_sec = 60;    // 60 second timeout i guess
1612                 timeout.tv_usec = 0;
1613                 FD_ZERO(&fdset);
1614                 FD_SET(sockfd, &fdset);
1615                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1616                 if (res > 0) {
1617                         if ((numbytes =
1618                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1619                                   0)) == -1) {
1620                                 perror("recv PASS");
1621                                 fail++;
1622                                 continue;
1623                         }
1624                 }
1625                 recvbuf[numbytes] = '\0';
1626                 if (strstr(recvbuf, "+OK ") == NULL) {
1627                         ERR("POP3 server login failed: %s", recvbuf);
1628                         fail++;
1629                         continue;
1630                 }
1631                 strncpy(sendbuf, "STAT\n", MAXDATASIZE);
1632                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1633                         perror("send STAT");
1634                         fail++;
1635                         continue;
1636                 }
1637                 timeout.tv_sec = 60;    // 60 second timeout i guess
1638                 timeout.tv_usec = 0;
1639                 FD_ZERO(&fdset);
1640                 FD_SET(sockfd, &fdset);
1641                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1642                 if (res > 0) {
1643                         if ((numbytes =
1644                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1645                                   0)) == -1) {
1646                                 perror("recv STAT");
1647                                 fail++;
1648                                 continue;
1649                         }
1650                 }
1651                 recvbuf[numbytes] = '\0';
1652                 if (strstr(recvbuf, "+OK ") == NULL) {
1653                         ERR("POP3 status failed: %s", recvbuf);
1654                         fail++;
1655                         continue;
1656                 }
1657                 // now we get the data
1658                 reply = recvbuf + 4;
1659                 if (reply == NULL) {
1660                         ERR("Error parsing POP3 response: %s", recvbuf);
1661                         fail++;
1662                         continue;
1663                 } else {
1664                         pthread_mutex_lock(&(mail->thread_info.mutex));
1665                         sscanf(reply, "%lu %lu", &mail->unseen,
1666                                &mail->used);
1667 //                      sleep(60);
1668                         pthread_mutex_unlock(&(mail->thread_info.mutex));
1669                 }
1670                 strncpy(sendbuf, "QUIT\n", MAXDATASIZE);
1671                 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1672                         perror("send QUIT");
1673                         fail++;
1674                         continue;
1675                 }
1676                 timeout.tv_sec = 60;    // 60 second timeout i guess
1677                 timeout.tv_usec = 0;
1678                 FD_ZERO(&fdset);
1679                 FD_SET(sockfd, &fdset);
1680                 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1681                 if (res > 0) {
1682                         if ((numbytes =
1683                              recv(sockfd, recvbuf, MAXDATASIZE - 1,
1684                                   0)) == -1) {
1685                                 perror("recv QUIT");
1686                                 fail++;
1687                                 continue;
1688                         }
1689                 }
1690                 recvbuf[numbytes] = '\0';
1691                 if (strstr(recvbuf, "+OK") == NULL) {
1692                         ERR("POP3 logout failed: %s", recvbuf);
1693                         fail++;
1694                         continue;
1695                 }
1696                 close(sockfd);
1697                 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) {   // new mail goodie
1698                         if (system(mail->command) == -1) {
1699                                 perror("system()");
1700                         }
1701                 }
1702                 fail = 0;
1703                 old_unseen = mail->unseen;
1704                 mail->last_update = update_time;
1705                 usleep(100);    // prevent race condition
1706                 if (get_time() - mail->last_update >
1707                     mail->interval) {
1708                         continue;
1709                 } else {
1710                         unsigned int delay =
1711                             1000000.0 * (mail->interval -
1712                                          (get_time() -
1713                                           mail->last_update));
1714                         if (delay < update_interval * 500000) {
1715                                 delay = update_interval * 1000000;
1716                         }
1717                         usleep(delay);
1718                 }
1719         }
1720         ERR("exiting pop3 thread");
1721         pthread_exit(NULL);
1722         return 0;
1723 }
1724
1725
1726 void *threaded_exec(struct text_object *obj) { // pthreads are really beginning to piss me off
1727         double update_time;
1728         int run_code = threads_runnable;
1729         while (threads_runnable == run_code) {
1730                 update_time = get_time();
1731                 char *p2 = obj->data.execi.buffer;
1732                 FILE *fp = popen(obj->data.execi.cmd,"r");
1733                 pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
1734                 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
1735                 (void) pclose(fp);
1736                 p2[n2] = '\0';
1737                 if (n2 && p2[n2 - 1] == '\n') {
1738                         p2[n2 - 1] = '\0';
1739                 }
1740                 while (*p2) {
1741                         if (*p2 == '\001') {
1742                                 *p2 = ' ';
1743                         }
1744                         p2++;
1745                 }
1746                 pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
1747                 obj->data.execi.last_update = update_time;
1748                 usleep(100); // prevent race condition
1749                 if (get_time() - obj->data.execi.last_update > obj->data.execi.interval) {
1750                         continue;
1751                 } else {
1752                         unsigned int delay = 1000000.0 * (obj->data.execi.interval -(get_time() - obj->data.execi.last_update));
1753                         if (delay < update_interval * 500000) {
1754                                 delay = update_interval * 1000000;
1755                         }
1756                         usleep(delay);
1757                 }
1758         }
1759         ERR("exiting thread");
1760         pthread_exit(NULL);
1761         return 0;
1762 }
1763
1764 static struct text_object *new_text_object_internal()
1765 {
1766         struct text_object *obj = malloc(sizeof(struct text_object));
1767         memset(obj, 0, sizeof(struct text_object));
1768         return obj;
1769 }
1770
1771 #ifdef MLDONKEY
1772 void ml_cleanup()
1773 {
1774         if (mlconfig.mldonkey_hostname) {
1775                 free(mlconfig.mldonkey_hostname);
1776                 mlconfig.mldonkey_hostname = NULL;
1777         }
1778 }
1779 #endif
1780
1781 static void free_text_objects(unsigned int count, struct text_object *objs)
1782 {
1783         unsigned int i;
1784         for (i = 0; i < count; i++) {
1785                 switch (objs[i].type) {
1786                         case OBJ_acpitemp:
1787                                 close(objs[i].data.i);
1788                                 break;
1789                         case OBJ_acpitempf:
1790                                 close(objs[i].data.i);
1791                                 break;
1792                         case OBJ_i2c:
1793                                 close(objs[i].data.i2c.fd);
1794                                 break;
1795                         case OBJ_time:
1796                                 free(objs[i].data.s);
1797                                 break;
1798                         case OBJ_utime:
1799                                 free(objs[i].data.s);
1800                                 break;
1801                         case OBJ_tztime:
1802                                 free(objs[i].data.tztime.tz);
1803                                 free(objs[i].data.tztime.fmt);
1804                                 break;
1805                         case OBJ_imap:
1806                                 free(info.mail);
1807                                 break;
1808                         case OBJ_imap_unseen:
1809                                 if (!objs[i].global_mode) {
1810                                         free(objs[i].data.mail);
1811                                 }
1812                                 break;
1813                         case OBJ_imap_messages:
1814                                 if (!objs[i].global_mode) {
1815                                         free(objs[i].data.mail);
1816                                 }
1817                                 break;
1818                         case OBJ_pop3:
1819                                 free(info.mail);
1820                                 break;
1821                         case OBJ_pop3_unseen:
1822                                 if (!objs[i].global_mode) {
1823                                         free(objs[i].data.mail);
1824                                 }
1825                                 break;
1826                         case OBJ_pop3_used:
1827                                 if (!objs[i].global_mode) {
1828                                         free(objs[i].data.mail);
1829                                 }
1830                                 break;
1831                         case OBJ_if_existing:
1832                         case OBJ_if_mounted:
1833                         case OBJ_if_running:
1834                                 free(objs[i].data.ifblock.s);
1835                                 break;
1836                         case OBJ_tail:
1837                                 free(objs[i].data.tail.logfile);
1838                                 free(objs[i].data.tail.buffer);
1839                                 break;
1840                         case OBJ_text: case OBJ_font:
1841                                 free(objs[i].data.s);
1842                                 break;
1843                         case OBJ_image:
1844                                 free(objs[i].data.s);
1845                                 break;
1846                         case OBJ_exec:
1847                                 free(objs[i].data.s);
1848                                 break;
1849                         case OBJ_execbar:
1850                                 free(objs[i].data.s);
1851                                 break;
1852                         case OBJ_execgraph:
1853                                 free(objs[i].data.s);
1854                                 break;
1855                                 /*              case OBJ_execibar:
1856                                                 free(objs[i].data.s);
1857                                                 break;
1858                                                 case OBJ_execigraph:
1859                                                 free(objs[i].data.s);
1860                                                 break;*/
1861 #ifdef HAVE_ICONV
1862                         case OBJ_iconv_start:
1863                                 free_iconv();
1864                                 break;
1865 #endif
1866 #ifdef MPD
1867                         case OBJ_mpd_title:
1868                                 if (info.mpd.title) {
1869                                         free(info.mpd.title);
1870                                         info.mpd.title = 0;
1871                                 }
1872                                 break;
1873                         case OBJ_mpd_artist:
1874                                 if (info.mpd.artist) {
1875                                         free(info.mpd.artist);
1876                                         info.mpd.artist = 0;
1877                                 }
1878                                 break;
1879                         case OBJ_mpd_album:
1880                                 if (info.mpd.album) {
1881                                         free(info.mpd.album);
1882                                         info.mpd.album = 0;
1883                                 }
1884                                 break;
1885                         case OBJ_mpd_random:
1886                                 if (info.mpd.random) {
1887                                         free(info.mpd.random);
1888                                         info.mpd.random = 0;
1889                                 }
1890                                 break;
1891                         case OBJ_mpd_repeat:
1892                                 if (info.mpd.repeat) {
1893                                         free(info.mpd.repeat);
1894                                         info.mpd.repeat = 0;
1895                                 }
1896                                 break;
1897                         case OBJ_mpd_track:
1898                                 if (info.mpd.track) {
1899                                         free(info.mpd.track);
1900                                         info.mpd.track = 0;
1901                                 }
1902                                 break;
1903                         case OBJ_mpd_name:
1904                                 if (info.mpd.name) {
1905                                         free(info.mpd.name);
1906                                         info.mpd.name = 0;
1907                                 }
1908                                 break;
1909                         case OBJ_mpd_file:
1910                                 if (info.mpd.file) {
1911                                         free(info.mpd.file);
1912                                         info.mpd.file = 0;
1913                                 }
1914                                 break;
1915                         case OBJ_mpd_status:
1916                                 if (info.mpd.status) {
1917                                         free(info.mpd.status);
1918                                         info.mpd.status = 0;
1919                                 }
1920                                 break;
1921                         case OBJ_mpd_smart:
1922                                 if (info.mpd.artist) {
1923                                         free(info.mpd.artist);
1924                                         info.mpd.artist = 0;
1925                                 }
1926                                 if (info.mpd.title) {
1927                                         free(info.mpd.title);
1928                                         info.mpd.title = 0;
1929                                 }
1930                                 if (info.mpd.file) {
1931                                         free(info.mpd.file);
1932                                         info.mpd.file = 0;
1933                                 }
1934                                 break;
1935                         case OBJ_mpd_host:
1936 #endif
1937 #ifdef XMMS2
1938                         case OBJ_xmms2_artist:
1939                                 if (info.xmms2.artist) {
1940                                         free(info.xmms2.artist);
1941                                         info.xmms2.artist = 0;
1942                                 }
1943                                 break;
1944                         case OBJ_xmms2_album:
1945                                 if (info.xmms2.album) {
1946                                         free(info.xmms2.album);
1947                                         info.xmms2.album = 0;
1948                                 }
1949                                 break;
1950                         case OBJ_xmms2_title:
1951                                 if (info.xmms2.title) {
1952                                         free(info.xmms2.title);
1953                                         info.xmms2.title = 0;
1954                                 }
1955                                 break;
1956                         case OBJ_xmms2_genre:
1957                                 if (info.xmms2.genre) {
1958                                         free(info.xmms2.genre);
1959                                         info.xmms2.genre = 0;
1960                                 }
1961                                 break;
1962                         case OBJ_xmms2_comment:
1963                                 if (info.xmms2.comment) {
1964                                         free(info.xmms2.comment);
1965                                         info.xmms2.comment = 0;
1966                                 }
1967                                 break;
1968                         case OBJ_xmms2_decoder:
1969                                 if (info.xmms2.decoder) {
1970                                         free(info.xmms2.decoder);
1971                                         info.xmms2.url = 0;
1972                                 }
1973                                 break;
1974                         case OBJ_xmms2_transport:
1975                                 if (info.xmms2.transport) {
1976                                         free(info.xmms2.transport);
1977                                         info.xmms2.url = 0;
1978                                 }
1979                                 break;
1980                         case OBJ_xmms2_url:
1981                                 if (info.xmms2.url) {
1982                                         free(info.xmms2.url);
1983                                         info.xmms2.url = 0;
1984                                 }
1985                                 break;
1986                         case OBJ_xmms2_date:
1987                                 if (info.xmms2.date) {
1988                                         free(info.xmms2.date);
1989                                         info.xmms2.date = 0;
1990                                 }
1991                                 break;
1992                         case OBJ_xmms2_status:
1993                                 if (info.xmms2.status) {
1994                                         free(info.xmms2.status);
1995                                         info.xmms2.status = 0;
1996                                 }
1997                                 break;
1998                         case OBJ_xmms2_smart:
1999                                 if (info.xmms2.artist) {
2000                                         free(info.xmms2.artist);
2001                                         info.xmms2.artist = 0;
2002                                 }
2003                                 if (info.xmms2.title) {
2004                                         free(info.xmms2.title);
2005                                         info.xmms2.title = 0;
2006                                 }
2007                                 if (info.xmms2.url) {
2008                                         free(info.xmms2.url);
2009                                         info.xmms2.url = 0;
2010                                 }
2011                                 break;
2012 #endif
2013 #ifdef BMPX
2014                         case OBJ_bmpx_title:
2015                         case OBJ_bmpx_artist:
2016                         case OBJ_bmpx_album:
2017                         case OBJ_bmpx_track:
2018                         case OBJ_bmpx_uri:
2019                         case OBJ_bmpx_bitrate:
2020 #endif
2021                         case OBJ_pre_exec:
2022                         case OBJ_battery:
2023                                 free(objs[i].data.s);
2024                                 break;
2025
2026                         case OBJ_execi:
2027                                 free(objs[i].data.execi.cmd);
2028                                 free(objs[i].data.execi.buffer);
2029                                 break;
2030                         case OBJ_texeci:
2031                                 free(objs[i].data.execi.cmd);
2032                                 free(objs[i].data.execi.buffer);
2033                                 break;
2034                         case OBJ_top:
2035                                 if (info.first_process) {
2036                                         free_all_processes();
2037                                         info.first_process = NULL;
2038                                 }
2039                                 break;
2040                         case OBJ_top_mem:
2041                                 if (info.first_process) {
2042                                         free_all_processes();
2043                                         info.first_process = NULL;
2044                                 }
2045                                 break;
2046 #ifdef HDDTEMP
2047                         case OBJ_hddtemp:
2048                                 free(objs[i].data.hddtemp.dev);
2049                                 free(objs[i].data.hddtemp.addr);
2050                                 break;
2051 #endif
2052                 }
2053         }
2054         free(objs);
2055         //text_objects = NULL;
2056         //text_object_count = 0;
2057 }
2058
2059 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2060 {
2061         char buf1[64];
2062         int n;
2063
2064         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2065                 *a = mixer_init(buf1);
2066                 (void) scan_bar(arg + n, w, h);
2067         } else {
2068                 *a = mixer_init(0);
2069                 (void) scan_bar(arg, w, h);
2070         }
2071 }
2072
2073
2074 /* construct_text_object() creates a new text_object */
2075 static struct text_object *construct_text_object(const char *s, const char *arg, unsigned int object_count, struct text_object *text_objects, long line)
2076 {
2077         //struct text_object *obj = new_text_object();
2078         struct text_object *obj = new_text_object_internal();
2079         obj->line = line;
2080
2081 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
2082 #define END ; } } else
2083
2084 #ifdef X11      
2085         if (s[0] == '#') {
2086                 obj->type = OBJ_color;
2087                 obj->data.l = get_x11_color(s);
2088         } else
2089 #endif /* X11 */
2090                 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
2091         END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
2092         END OBJ(acpiacadapter, 0)
2093 #if defined(__linux__)
2094             END OBJ(freq, 0)
2095             get_cpu_count();
2096         if (!arg
2097             || !isdigit(arg[0])
2098             || strlen(arg) >=2
2099             || atoi(&arg[0])==0
2100             || (unsigned int)atoi(&arg[0])>info.cpu_count)
2101         {
2102             obj->data.cpu_index=1;
2103 //          ERR("freq: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2104         }
2105         else 
2106         {
2107             obj->data.cpu_index=atoi(&arg[0]);
2108         }
2109         obj->a = 1;
2110         END OBJ(freq_g, 0)
2111             get_cpu_count();
2112         if (!arg
2113             || !isdigit(arg[0])
2114             || strlen(arg) >=2
2115             || atoi(&arg[0])==0
2116             || (unsigned int)atoi(&arg[0])>info.cpu_count)
2117         {
2118             obj->data.cpu_index=1;
2119 //          ERR("freq_g: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2120         }
2121         else 
2122         {
2123             obj->data.cpu_index=atoi(&arg[0]);
2124         }
2125         obj->a = 1;
2126         END OBJ(voltage_mv, 0)
2127             get_cpu_count();
2128         if (!arg
2129             || !isdigit(arg[0])
2130             || strlen(arg) >=2
2131             || atoi(&arg[0])==0
2132             || (unsigned int)atoi(&arg[0])>info.cpu_count)
2133         {
2134             obj->data.cpu_index=1;
2135 //          ERR("voltage_mv: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2136         }
2137         else 
2138         {
2139             obj->data.cpu_index=atoi(&arg[0]);
2140         }
2141         obj->a = 1;
2142         END OBJ(voltage_v, 0)
2143             get_cpu_count();
2144         if (!arg
2145             || !isdigit(arg[0])
2146             || strlen(arg) >=2
2147             || atoi(&arg[0])==0
2148             || (unsigned int)atoi(&arg[0])>info.cpu_count)
2149         {
2150             obj->data.cpu_index=1;
2151 //          ERR("voltage_v: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2152         }
2153         else 
2154         {
2155             obj->data.cpu_index=atoi(&arg[0]);
2156         }
2157         obj->a = 1;
2158 #else 
2159         END OBJ(freq, 0);
2160         END OBJ(freq_g, 0);
2161 #endif /* __linux__ */
2162         END OBJ(freq_dyn, 0);
2163         END OBJ(freq_dyn_g, 0);
2164         END OBJ(acpifan, 0);
2165         END OBJ(battery, 0);
2166         char bat[64];
2167         if (arg)
2168                 sscanf(arg, "%63s", bat);
2169         else
2170                 strcpy(bat, "BAT0");
2171         obj->data.s = strdup(bat);
2172 #if defined(__linux__)
2173         END OBJ(i8k_version, INFO_I8K)
2174                 END OBJ(i8k_bios, INFO_I8K)
2175                 END OBJ(i8k_serial, INFO_I8K)
2176                 END OBJ(i8k_cpu_temp, INFO_I8K)
2177                 END OBJ(i8k_cpu_tempf, INFO_I8K)
2178                 END OBJ(i8k_left_fan_status, INFO_I8K)  
2179                 END OBJ(i8k_right_fan_status, INFO_I8K)
2180                 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2181                 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2182                 END OBJ(i8k_ac_status, INFO_I8K)
2183                 END OBJ(i8k_buttons_status, INFO_I8K)
2184         END OBJ(ibm_fan, 0)
2185         END OBJ(ibm_temps, 0)
2186             if (!isdigit(arg[0])
2187                          || strlen(arg) >=2
2188                          || atoi(&arg[0]) >=8)
2189             {
2190                 obj->data.sensor=0;
2191                 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. Using 0 (CPU temp sensor).");
2192             }
2193                 obj->data.sensor = atoi(&arg[0]);
2194         END OBJ(ibm_volume, 0)
2195         END OBJ(ibm_brightness, 0)
2196         END OBJ(pb_battery, 0)
2197                 if (arg && strcmp(arg, "status") == 0) {
2198                         obj->data.i = PB_BATT_STATUS;
2199                 } else if (arg && strcmp(arg, "percent") == 0) {
2200                         obj->data.i = PB_BATT_PERCENT;
2201                 } else if (arg && strcmp(arg, "time") == 0) {
2202                         obj->data.i = PB_BATT_TIME;
2203                 } else {
2204                         ERR("pb_battery: needs one argument: status, percent or time");
2205                         free(obj);
2206                         return NULL;
2207                 }
2208
2209 #endif /* __linux__ */
2210                 END OBJ(buffers, INFO_BUFFERS)
2211                 END OBJ(cached, INFO_BUFFERS)
2212                 END OBJ(cpu, INFO_CPU)
2213                 if (arg) {
2214                         if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2215                                 obj->data.cpu_index = atoi(&arg[3]);
2216                                 arg += 4;
2217                         } else {obj->data.cpu_index = 0; }
2218                 } else {
2219                         obj->data.cpu_index = 0;
2220                 }
2221         END OBJ(cpubar, INFO_CPU)
2222                 if (arg) {
2223                         if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2224                                 obj->data.cpu_index = atoi(&arg[3]);
2225                                 arg += 4;
2226                         }
2227                         else {obj->data.cpu_index = 0;}
2228                         (void) scan_bar(arg, &obj->a, &obj->b);
2229                 } else {
2230                         (void) scan_bar(arg, &obj->a, &obj->b);
2231                         obj->data.cpu_index = 0;
2232                 }
2233         END OBJ(cpugraph, INFO_CPU)
2234                 if (arg) {
2235                         if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2236                                 obj->data.cpu_index = atoi(&arg[3]);
2237                                 arg += 4;
2238                         }
2239                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2240                 } else {
2241                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2242                         obj->data.cpu_index = 0;
2243                 }
2244         END OBJ(diskio, INFO_DISKIO)
2245                 END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2246         END OBJ(color, 0) 
2247 #ifdef X11
2248                 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2249 #endif /* X11 */
2250         END
2251                 OBJ(font, 0)
2252                 obj->data.s = scan_font(arg);
2253         END
2254                 OBJ(downspeed, INFO_NET) 
2255                 if(arg) {
2256                         obj->data.net = get_net_stat(arg);
2257                 }
2258                 else {
2259                         CRIT_ERR("downspeed needs argument");
2260                 }
2261         END OBJ(downspeedf, INFO_NET)
2262                 if(arg) {
2263                         obj->data.net = get_net_stat(arg);
2264                 }
2265                 else {
2266                         CRIT_ERR("downspeedf needs argument");
2267                 }
2268         END OBJ(downspeedgraph, INFO_NET)
2269                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2270         char buf[64];
2271         sscanf(arg, "%63s %*i,%*i %*i", buf);
2272         obj->data.net = get_net_stat(buf);
2273         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2274                 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
2275                         obj->a = 0;
2276                         obj->b = 25;
2277                 }
2278         }
2279         END OBJ(else, 0)
2280                 if (blockdepth) {
2281                         (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos = object_count;
2282                         blockstart[blockdepth - 1] = object_count;
2283                         obj->data.ifblock.pos = object_count + 2;
2284                 } else {
2285                         ERR("$else: no matching $if_*");
2286                 }
2287         END OBJ(endif, 0)
2288                 if (blockdepth) {
2289                         blockdepth--;
2290                         text_objects[blockstart[blockdepth]].data.ifblock.pos = object_count;
2291                 } else {
2292                         ERR("$endif: no matching $if_*");
2293                 }
2294         END
2295         OBJ(image, 0) obj->data.s = strdup(arg ? arg : "");
2296         END
2297 #ifdef HAVE_POPEN
2298         OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
2299         END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
2300         END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
2301         END OBJ(execibar, 0) unsigned int n;
2302         if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2303                 char buf[256];
2304                 ERR("${execibar <interval> command}");
2305                 obj->type = OBJ_text;
2306                 snprintf(buf, 256, "${%s}", s);
2307                 obj->data.s = strdup(buf);
2308         } else {
2309                 obj->data.execi.cmd = strdup(arg + n);
2310         }
2311         END OBJ(execigraph, 0) unsigned int n;
2312         if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2313                 char buf[256];
2314                 ERR("${execigraph <interval> command}");
2315                 obj->type = OBJ_text;
2316                 snprintf(buf, 256, "${%s}", s);
2317                 obj->data.s = strdup(buf);
2318         } else {
2319                 obj->data.execi.cmd = strdup(arg + n);
2320         }
2321         END OBJ(execi, 0) unsigned int n;
2322
2323         if (!arg
2324                         || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2325                 char buf[256];
2326                 ERR("${execi <interval> command}");
2327                 obj->type = OBJ_text;
2328                 snprintf(buf, 256, "${%s}", s);
2329                 obj->data.s = strdup(buf);
2330         } else {
2331                 obj->data.execi.cmd = strdup(arg + n);
2332                 obj->data.execi.buffer =
2333                         (char *) calloc(1, TEXT_BUFFER_SIZE);
2334         }
2335         END OBJ(texeci, 0) unsigned int n;
2336
2337         if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2338                 char buf[256];
2339                 ERR("${texeci <interval> command}");
2340                 obj->type = OBJ_text;
2341                 snprintf(buf, 256, "${%s}", s);
2342                 obj->data.s = strdup(buf);
2343         } else {
2344                 obj->data.execi.cmd = strdup(arg + n);
2345                 obj->data.execi.buffer =
2346                         (char *) calloc(1, TEXT_BUFFER_SIZE);
2347         }
2348         obj->data.execi.pos = -1;
2349         END OBJ(pre_exec, 0) obj->type = OBJ_text;
2350         if (arg) {
2351                 FILE *fp = popen(arg, "r");
2352                 unsigned int n;
2353                 char buf[2048];
2354
2355                 n = fread(buf, 1, 2048, fp);
2356                 buf[n] = '\0';
2357
2358                 if (n && buf[n - 1] == '\n')
2359                         buf[n - 1] = '\0';
2360
2361                 (void) pclose(fp);
2362
2363                 obj->data.s = strdup(buf);
2364         } else
2365                 obj->data.s = strdup("");
2366         END
2367 #endif
2368                 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
2369         arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2370         if (arg) {
2371                 while (isspace(*arg))
2372                         arg++;
2373                 if (*arg == '\0')
2374                         arg = "/";
2375         } else
2376                 arg = "/";
2377         obj->data.fsbar.fs = prepare_fs_stat(arg);
2378         END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
2379         if (arg) {
2380                 unsigned int n;
2381                 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
2382                         arg += n;
2383         } else
2384                 arg = "/";
2385         obj->data.fsbar.fs = prepare_fs_stat(arg);
2386         END OBJ(fs_free, INFO_FS) if (!arg)
2387                 arg = "/";
2388         obj->data.fs = prepare_fs_stat(arg);
2389         END OBJ(fs_used_perc, INFO_FS) if (!arg)
2390                 arg = "/";
2391         obj->data.fs = prepare_fs_stat(arg);
2392         END OBJ(fs_free_perc, INFO_FS) if (!arg)
2393                 arg = "/";
2394         obj->data.fs = prepare_fs_stat(arg);
2395         END OBJ(fs_size, INFO_FS) if (!arg)
2396                 arg = "/";
2397         obj->data.fs = prepare_fs_stat(arg);
2398         END OBJ(fs_used, INFO_FS) if (!arg)
2399                 arg = "/";
2400         obj->data.fs = prepare_fs_stat(arg);
2401         END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
2402         END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
2403         END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
2404         END OBJ(goto, 0)
2405
2406         if (!arg) {
2407                 ERR("goto needs arguments");
2408                 obj->type = OBJ_text;
2409                 obj->data.s = strdup("${goto}");
2410                 return NULL;
2411         }
2412         
2413         obj->data.i = atoi(arg);
2414         
2415         END OBJ(tab, 0)
2416         int a = 10, b = 0;
2417         if (arg) {
2418                 if (sscanf(arg, "%d %d", &a, &b) != 2)
2419                         sscanf(arg, "%d", &b);
2420         }
2421         if (a <= 0) 
2422                 a = 1;
2423         obj->data.pair.a = a;
2424         obj->data.pair.b = b;
2425
2426         END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
2427         int n;
2428
2429         if (!arg) {
2430                 ERR("i2c needs arguments");
2431                 obj->type = OBJ_text;
2432                 //obj->data.s = strdup("${i2c}");
2433                 return NULL;
2434         }
2435
2436         if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2437                 /* if scanf couldn't read three values, read type and num and use
2438                  * default device */
2439                 sscanf(arg, "%63s %d", buf2, &n);
2440                 obj->data.i2c.fd =
2441                         open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
2442                                         obj->data.i2c.devtype);
2443                 strncpy(obj->data.i2c.type, buf2, 63);
2444         } else {
2445                 obj->data.i2c.fd =
2446                         open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
2447                                         obj->data.i2c.devtype);
2448                 strncpy(obj->data.i2c.type, buf2, 63);
2449         }
2450
2451         END OBJ(top, INFO_TOP)
2452                 char buf[64];
2453         int n;
2454         if (!arg) {
2455                 ERR("top needs arguments");
2456                 obj->type = OBJ_text;
2457                 //obj->data.s = strdup("${top}");
2458                 return NULL;
2459         }
2460         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2461                 if (strcmp(buf, "name") == 0) {
2462                         obj->data.top.type = TOP_NAME;
2463                 } else if (strcmp(buf, "cpu") == 0) {
2464                         obj->data.top.type = TOP_CPU;
2465                 } else if (strcmp(buf, "pid") == 0) {
2466                         obj->data.top.type = TOP_PID;
2467                 } else if (strcmp(buf, "mem") == 0) {
2468                         obj->data.top.type = TOP_MEM;
2469                 } else {
2470                         ERR("invalid arg for top");
2471                         return NULL;
2472                 }
2473                 if (n < 1 || n > 10) {
2474                         CRIT_ERR("invalid arg for top");
2475                         return NULL;
2476                 } else {
2477                         obj->data.top.num = n - 1;
2478                         top_cpu = 1;
2479                 }
2480         } else {
2481                 ERR("invalid args given for top");
2482                 return NULL;
2483         }
2484         END OBJ(top_mem, INFO_TOP)
2485                 char buf[64];
2486         int n;
2487         if (!arg) {
2488                 ERR("top_mem needs arguments");
2489                 obj->type = OBJ_text;
2490                 obj->data.s = strdup("${top_mem}");
2491                 return NULL;
2492         }
2493         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2494                 if (strcmp(buf, "name") == 0) {
2495                         obj->data.top.type = TOP_NAME;
2496                 } else if (strcmp(buf, "cpu") == 0) {
2497                         obj->data.top.type = TOP_CPU;
2498                 } else if (strcmp(buf, "pid") == 0) {
2499                         obj->data.top.type = TOP_PID;
2500                 } else if (strcmp(buf, "mem") == 0) {
2501                         obj->data.top.type = TOP_MEM;
2502                 } else {
2503                         ERR("invalid arg for top");
2504                         return NULL;
2505                 }
2506                 if (n < 1 || n > 10) {
2507                         CRIT_ERR("invalid arg for top");
2508                         return NULL;
2509                 } else {
2510                         obj->data.top.num = n - 1;
2511                         top_mem = 1;
2512                 }
2513         } else {
2514                 ERR("invalid args given for top");
2515                 return NULL;
2516         }
2517         END OBJ(addr, INFO_NET)
2518                 if(arg) {
2519                         obj->data.net = get_net_stat(arg);
2520                 }
2521                 else {
2522                         CRIT_ERR("addr needs argument");
2523                 }
2524         END OBJ(linkstatus, INFO_WIFI) 
2525                 if(arg) {
2526                         obj->data.net = get_net_stat(arg);
2527                 }
2528                 else {
2529                         CRIT_ERR("linkstatus needs argument");
2530                 }
2531         END OBJ(tail, 0)
2532                 char buf[64];
2533         int n1, n2;
2534         if (!arg) {
2535                 ERR("tail needs arguments");
2536                 obj->type = OBJ_text;
2537                 obj->data.s = strdup("${tail}");
2538                 return NULL;
2539         }
2540         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2541                 if (n1 < 1 || n1 > 30) {
2542                         CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
2543                         return NULL;
2544                 } else {
2545                         FILE *fp = NULL;
2546                         fp = fopen(buf, "r");
2547                         if (fp) {
2548                                 obj->data.tail.logfile =
2549                                         malloc(TEXT_BUFFER_SIZE);
2550                                 strcpy(obj->data.tail.logfile, buf);
2551                                 obj->data.tail.wantedlines = n1 - 1;
2552                                 obj->data.tail.interval =
2553                                         update_interval * 2;
2554                                 fclose(fp);
2555                         } else {
2556                                 //fclose (fp);
2557                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2558                         }
2559                 }
2560         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2561                 if (n1 < 1 || n1 > 30) {
2562                         CRIT_ERR
2563                                 ("invalid arg for tail, number of lines must be between 1 and 30");
2564                         return NULL;
2565                 } else if (n2 < 1 || n2 < update_interval) {
2566                         CRIT_ERR
2567                                 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
2568                         return NULL;
2569                 } else {
2570                         FILE *fp;
2571                         fp = fopen(buf, "r");
2572                         if (fp != NULL) {
2573                                 obj->data.tail.logfile =
2574                                         malloc(TEXT_BUFFER_SIZE);
2575                                 strcpy(obj->data.tail.logfile, buf);
2576                                 obj->data.tail.wantedlines = n1 - 1;
2577                                 obj->data.tail.interval = n2;
2578                                 fclose(fp);
2579                         } else {
2580                                 //fclose (fp);
2581                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2582                         }
2583                 }
2584         }
2585
2586         else {
2587                 ERR("invalid args given for tail");
2588                 return NULL;
2589         }
2590         obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2591         END OBJ(head, 0)
2592                 char buf[64];
2593         int n1, n2;
2594         if (!arg) {
2595                 ERR("head needs arguments");
2596                 obj->type = OBJ_text;
2597                 obj->data.s = strdup("${head}");
2598                 return NULL;
2599         }
2600         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2601                 if (n1 < 1 || n1 > 30) {
2602                         CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
2603                         return NULL;
2604                 } else {
2605                         FILE *fp;
2606                         fp = fopen(buf, "r");
2607                         if (fp != NULL) {
2608                                 obj->data.tail.logfile =
2609                                         malloc(TEXT_BUFFER_SIZE);
2610                                 strcpy(obj->data.tail.logfile, buf);
2611                                 obj->data.tail.wantedlines = n1 - 1;
2612                                 obj->data.tail.interval =
2613                                         update_interval * 2;
2614                                 fclose(fp);
2615                         } else {
2616                                 //fclose (fp);
2617                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2618                         }
2619                 }
2620         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2621                 if (n1 < 1 || n1 > 30) {
2622                         CRIT_ERR
2623                                 ("invalid arg for head, number of lines must be between 1 and 30");
2624                         return NULL;
2625                 } else if (n2 < 1 || n2 < update_interval) {
2626                         CRIT_ERR
2627                                 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
2628                         return NULL;
2629                 } else {
2630                         FILE *fp;
2631                         fp = fopen(buf, "r");
2632                         if (fp != NULL) {
2633                                 obj->data.tail.logfile =
2634                                         malloc(TEXT_BUFFER_SIZE);
2635                                 strcpy(obj->data.tail.logfile, buf);
2636                                 obj->data.tail.wantedlines = n1 - 1;
2637                                 obj->data.tail.interval = n2;
2638                                 fclose(fp);
2639                         } else {
2640                                 //fclose (fp);
2641                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2642                         }
2643                 }
2644         }
2645
2646         else {
2647                 ERR("invalid args given for head");
2648                 return NULL;
2649         }
2650         obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2651         END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
2652         if (arg) {
2653                 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2654                 if (r >= 3 && (c < 1 || c > 3))
2655                         r--;
2656                 if (r >= 2 && (b < 1 || b > 3))
2657                         r--, b = c;
2658                 if (r >= 1 && (a < 1 || a > 3))
2659                         r--, a = b, b = c;
2660         }
2661         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2662         obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2663         obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2664         END OBJ(if_existing, 0)
2665                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2666                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2667                 }
2668         if (!arg) {
2669                 ERR("if_existing needs an argument");
2670                 obj->data.ifblock.s = 0;
2671         } else
2672                 obj->data.ifblock.s = strdup(arg);
2673         blockstart[blockdepth] = object_count;
2674         obj->data.ifblock.pos = object_count + 2;
2675         blockdepth++;
2676         END OBJ(if_mounted, 0)
2677                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2678                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2679                 }
2680         if (!arg) {
2681                 ERR("if_mounted needs an argument");
2682                 obj->data.ifblock.s = 0;
2683         } else
2684                 obj->data.ifblock.s = strdup(arg);
2685         blockstart[blockdepth] = object_count;
2686         obj->data.ifblock.pos = object_count + 2;
2687         blockdepth++;
2688         END OBJ(if_running, 0)
2689                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2690                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2691                 }
2692         if (arg) {
2693                 char buf[256];
2694                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2695                 obj->data.ifblock.s = strdup(buf);
2696         } else {
2697                 ERR("if_running needs an argument");
2698                 obj->data.ifblock.s = 0;
2699         }
2700         blockstart[blockdepth] = object_count;
2701         obj->data.ifblock.pos = object_count + 2;
2702         blockdepth++;
2703         END OBJ(kernel, 0)
2704                 END OBJ(machine, 0)
2705                 END OBJ(mails, INFO_MAIL)
2706                 END OBJ(mem, INFO_MEM)
2707                 END OBJ(memmax, INFO_MEM)
2708                 END OBJ(memperc, INFO_MEM)
2709                 END OBJ(membar, INFO_MEM)
2710                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2711         END OBJ(memgraph, INFO_MEM)
2712                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2713         END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
2714         END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
2715         END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
2716         END OBJ(mixerbar, INFO_MIXER)
2717                 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2718                                 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2719         END OBJ(mixerlbar, INFO_MIXER)
2720                 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2721                                 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2722         END OBJ(mixerrbar, INFO_MIXER)
2723                 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2724                                 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2725         END
2726 #ifdef MLDONKEY
2727                 OBJ(ml_upload_counter, INFO_MLDONKEY)
2728                 END OBJ(ml_download_counter, INFO_MLDONKEY)
2729                 END OBJ(ml_nshared_files, INFO_MLDONKEY)
2730                 END OBJ(ml_shared_counter, INFO_MLDONKEY)
2731                 END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
2732                 END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
2733                 END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
2734                 END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
2735                 END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
2736                 END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
2737 #endif
2738                 OBJ(new_mails, INFO_MAIL)
2739                 END OBJ(nodename, 0)
2740                 END OBJ(processes, INFO_PROCS)
2741                 END OBJ(running_processes, INFO_RUN_PROCS)
2742                 END OBJ(shadecolor, 0)
2743 #ifdef X11
2744                 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2745 #endif /* X11 */
2746         END OBJ(outlinecolor, 0)
2747 #ifdef X11
2748                 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2749 #endif /* X11 */
2750         END OBJ(stippled_hr, 0)
2751 #ifdef X11
2752                 int a = stippled_borders, b = 1;
2753         if (arg) {
2754                 if (sscanf(arg, "%d %d", &a, &b) != 2)
2755                         sscanf(arg, "%d", &b);
2756         }
2757         if (a <= 0)
2758                 a = 1;
2759         obj->data.pair.a = a;
2760         obj->data.pair.b = b;
2761 #endif /* X11 */
2762         END OBJ(swap, INFO_MEM)
2763                 END OBJ(swapmax, INFO_MEM)
2764                 END OBJ(swapperc, INFO_MEM)
2765                 END OBJ(swapbar, INFO_MEM)
2766                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2767         END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
2768         obj->data.i2c.fd =
2769                 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
2770                                 obj->data.i2c.devtype);
2771         END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
2772         obj->data.i2c.fd =
2773                 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
2774                                 obj->data.i2c.devtype);
2775         END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2776         END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2777         END OBJ(tztime, 0)
2778                 char buf1[256], buf2[256], *fmt, *tz;
2779                 fmt = tz = NULL;
2780                 if (arg) {
2781                         int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2782                         switch (nArgs) {
2783                                 case 2:
2784                                         tz = buf1;
2785                                 case 1:
2786                                         fmt = buf2;
2787                         }
2788                 }
2789
2790                 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
2791                 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
2792 #ifdef HAVE_ICONV
2793         END OBJ(iconv_start, 0)
2794                 if (iconv_converting) {
2795                         CRIT_ERR("You must stop your last iconv conversion before starting another");
2796                 }
2797                 if (arg) {
2798                         char iconv_from[CODEPAGE_LENGTH];
2799                         char iconv_to[CODEPAGE_LENGTH];
2800                         if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2801                                 CRIT_ERR("Invalid arguments for iconv_start");
2802                         } else {
2803                                 iconv_t new_iconv;
2804                                 new_iconv = iconv_open(iconv_to, iconv_from);
2805                                 if (new_iconv == (iconv_t)(-1)) {
2806                                         ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2807                                 } else {
2808                                         obj->a = register_iconv(&new_iconv);
2809                                         iconv_converting = 1;
2810                                 }
2811                         }
2812                 } else {
2813                         CRIT_ERR("Iconv requires arguments");
2814                 }
2815         END OBJ(iconv_stop, 0)
2816                 iconv_converting = 0;
2817         
2818 #endif
2819         END OBJ(totaldown, INFO_NET)
2820                 if(arg) {
2821                         obj->data.net = get_net_stat(arg);
2822                 }
2823                 else {
2824                         CRIT_ERR("totaldown needs argument");
2825                 }
2826         END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
2827         if(arg) {
2828                 obj->data.net = get_net_stat(arg);
2829         }
2830         else {
2831                 CRIT_ERR("totalup needs argument");
2832         }
2833         END OBJ(updates, 0)
2834                 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
2835         END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
2836         END OBJ(upspeed, INFO_NET)
2837                 if(arg) {
2838                         obj->data.net = get_net_stat(arg);
2839                 }
2840                 else {
2841                         CRIT_ERR("upspeed needs argument");
2842                 }
2843         END OBJ(upspeedf, INFO_NET) 
2844                 if(arg) {
2845                         obj->data.net = get_net_stat(arg);
2846                 }
2847                 else {
2848                         CRIT_ERR("upspeedf needs argument");
2849                 }
2850
2851         END OBJ(upspeedgraph, INFO_NET)
2852                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2853         char buf[64];
2854         sscanf(arg, "%63s %*i,%*i %*i", buf);
2855         obj->data.net = get_net_stat(buf);
2856         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2857                 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
2858                         obj->a = 0;
2859                         obj->b = 25;
2860                 }
2861         }
2862         END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
2863                 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
2864 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2865                 OBJ(apm_adapter, 0) END
2866                 OBJ(apm_battery_life, 0) END
2867                 OBJ(apm_battery_time, 0) END
2868 #endif /* __FreeBSD__ */
2869 #ifdef SETI
2870                 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
2871                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2872         END OBJ(seti_credit, INFO_SETI) END
2873 #endif
2874                 OBJ(imap_unseen, 0)
2875                 if (arg) {
2876                         // proccss
2877                         obj->data.mail = parse_mail_args(IMAP, arg);
2878                         obj->global_mode = 0;
2879                 } else {
2880                         obj->global_mode = 1;
2881                 }
2882                 END
2883                 OBJ(imap_messages, 0)
2884                 if (arg) {
2885                         // proccss
2886                         obj->data.mail = parse_mail_args(IMAP, arg);
2887                         obj->global_mode = 0;
2888                 } else {
2889                         obj->global_mode = 1;
2890                 }
2891                 END
2892                 OBJ(pop3_unseen, 0)
2893                 if (arg) {
2894                         // proccss
2895                         obj->data.mail = parse_mail_args(POP3, arg);
2896                         obj->global_mode = 0;
2897                 } else {
2898                         obj->global_mode = 1;
2899                 }
2900                 END
2901                 OBJ(pop3_used, 0)
2902                 if (arg) {
2903                         // proccss
2904                         obj->data.mail = parse_mail_args(POP3, arg);
2905                         obj->global_mode = 0;
2906                 } else {
2907                         obj->global_mode = 1;
2908                 }
2909                 END
2910 #ifdef MPD
2911                 OBJ(mpd_artist, INFO_MPD)
2912                 END OBJ(mpd_title, INFO_MPD)
2913                 END OBJ(mpd_random, INFO_MPD)
2914                 END OBJ(mpd_repeat, INFO_MPD)
2915                 END OBJ(mpd_elapsed, INFO_MPD)
2916                 END OBJ(mpd_length, INFO_MPD)
2917                 END OBJ(mpd_track, INFO_MPD)
2918                 END OBJ(mpd_name, INFO_MPD)
2919                 END OBJ(mpd_file, INFO_MPD)
2920                 END OBJ(mpd_percent, INFO_MPD)
2921                 END OBJ(mpd_album, INFO_MPD)
2922                 END OBJ(mpd_vol, INFO_MPD)
2923                 END OBJ(mpd_bitrate, INFO_MPD)
2924                 END OBJ(mpd_status, INFO_MPD)
2925                 END OBJ(mpd_bar, INFO_MPD)
2926                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2927                 END OBJ(mpd_smart, INFO_MPD) END
2928 #endif
2929 #ifdef XMMS2
2930                 OBJ(xmms2_artist, INFO_XMMS2)
2931                 END OBJ(xmms2_album, INFO_XMMS2)
2932                 END OBJ(xmms2_title, INFO_XMMS2)
2933                 END OBJ(xmms2_genre, INFO_XMMS2)
2934                 END OBJ(xmms2_comment, INFO_XMMS2)
2935                 END OBJ(xmms2_decoder, INFO_XMMS2)
2936                 END OBJ(xmms2_transport, INFO_XMMS2)
2937                 END OBJ(xmms2_url, INFO_XMMS2)
2938                 END OBJ(xmms2_tracknr, INFO_XMMS2)
2939                 END OBJ(xmms2_bitrate, INFO_XMMS2)
2940                 END OBJ(xmms2_date, INFO_XMMS2)
2941                 END OBJ(xmms2_id, INFO_XMMS2)
2942                 END OBJ(xmms2_duration, INFO_XMMS2)
2943         END OBJ(xmms2_elapsed, INFO_XMMS2)
2944                 END OBJ(xmms2_size, INFO_XMMS2)
2945                 END OBJ(xmms2_status, INFO_XMMS2)
2946                 END OBJ(xmms2_percent, INFO_XMMS2)
2947                 END OBJ(xmms2_bar, INFO_XMMS2)
2948                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2949                 END OBJ(xmms2_smart, INFO_XMMS2)
2950                 END
2951 #endif
2952 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
2953                 OBJ(xmms_status, INFO_XMMS) END
2954                 OBJ(xmms_title, INFO_XMMS) END
2955                 OBJ(xmms_length, INFO_XMMS) END
2956                 OBJ(xmms_length_seconds, INFO_XMMS) END
2957                 OBJ(xmms_position, INFO_XMMS) END
2958                 OBJ(xmms_position_seconds, INFO_XMMS) END
2959                 OBJ(xmms_bitrate, INFO_XMMS) END
2960                 OBJ(xmms_frequency, INFO_XMMS) END
2961                 OBJ(xmms_channels, INFO_XMMS) END
2962                 OBJ(xmms_filename, INFO_XMMS) END
2963                 OBJ(xmms_playlist_length, INFO_XMMS) END
2964                 OBJ(xmms_playlist_position, INFO_XMMS) END
2965                 OBJ(xmms_bar, INFO_XMMS)
2966                 (void) scan_bar(arg, &obj->a, &obj->b);
2967         END
2968 #endif
2969 #ifdef BMPX
2970                 OBJ(bmpx_title, INFO_BMPX)
2971                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2972         END
2973                 OBJ(bmpx_artist, INFO_BMPX)
2974                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2975         END
2976                 OBJ(bmpx_album, INFO_BMPX)
2977                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2978         END
2979                 OBJ(bmpx_track, INFO_BMPX)
2980                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2981         END
2982                 OBJ(bmpx_uri, INFO_BMPX)
2983                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2984         END
2985                 OBJ(bmpx_bitrate, INFO_BMPX)
2986                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2987         END
2988 #endif
2989 #ifdef HDDTEMP
2990         OBJ(hddtemp, 0)
2991                 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev, 
2992                         &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2993                         ERR("hddtemp needs arguments");
2994                         obj->type = OBJ_text;
2995                         obj->data.s = strdup("${hddtemp}");
2996                         return NULL;
2997                 }
2998         END
2999 #endif
3000 #ifdef TCP_PORT_MONITOR
3001                 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR) 
3002                 int argc, port_begin, port_end, item, connection_index;
3003         char itembuf[32];
3004         memset(itembuf,0,sizeof(itembuf));
3005         connection_index=0;
3006         /* massive argument checking */
3007         if (!arg) {
3008                 CRIT_ERR("tcp_portmon: needs arguments");
3009         }
3010         argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
3011         if ( (argc != 3) && (argc != 4) ) 
3012         {
3013                 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
3014         }
3015         if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
3016         {
3017                 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
3018         }
3019         if ( port_begin > port_end )
3020         {
3021                 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
3022         }
3023         if ( strncmp(itembuf,"count",31) == 0 )
3024                 item=COUNT;
3025         else if ( strncmp(itembuf,"rip",31) == 0 )
3026                 item=REMOTEIP;
3027         else if ( strncmp(itembuf,"rhost",31) == 0 )
3028                 item=REMOTEHOST;
3029         else if ( strncmp(itembuf,"rport",31) == 0 )
3030                 item=REMOTEPORT;
3031         else if ( strncmp(itembuf,"rservice",31) == 0 )
3032                 item=REMOTESERVICE;
3033         else if ( strncmp(itembuf,"lip",31) == 0 )
3034                 item=LOCALIP;
3035         else if ( strncmp(itembuf,"lhost",31) == 0 )
3036                 item=LOCALHOST;
3037         else if ( strncmp(itembuf,"lport",31) == 0 )
3038                 item=LOCALPORT;
3039         else if ( strncmp(itembuf,"lservice",31) == 0 )
3040                 item=LOCALSERVICE;
3041         else
3042         {
3043                 CRIT_ERR("tcp_portmon: invalid item specified"); 
3044         }
3045         if ( (argc==3) && (item!=COUNT) )
3046         {
3047                 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
3048         }
3049         if ( (argc==4) && (connection_index<0) )
3050         {
3051                 CRIT_ERR("tcp_portmon: connection index must be non-negative");
3052         }
3053         /* ok, args looks good. save the text object data */
3054         obj->data.tcp_port_monitor.port_range_begin = (in_port_t)port_begin;
3055         obj->data.tcp_port_monitor.port_range_end = (in_port_t)port_end;
3056         obj->data.tcp_port_monitor.item = item;
3057         obj->data.tcp_port_monitor.connection_index = connection_index;
3058
3059         /* if the port monitor collection hasn't been created, we must create it */
3060         if ( !info.p_tcp_port_monitor_collection )
3061         {
3062                 info.p_tcp_port_monitor_collection = 
3063                         create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
3064                 if ( !info.p_tcp_port_monitor_collection )
3065                 {
3066                         CRIT_ERR("tcp_portmon: unable to create port monitor collection");
3067                 }
3068         }
3069
3070         /* if a port monitor for this port does not exist, create one and add it to the collection */
3071         if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
3072         {
3073                 tcp_port_monitor_t * p_monitor = 
3074                         create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
3075                 if ( !p_monitor )
3076                 {
3077                         CRIT_ERR("tcp_portmon: unable to create port monitor");
3078                 }
3079                 /* add the newly created monitor to the collection */
3080                 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
3081                                         p_monitor ) != 0 )
3082                 {
3083                         CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
3084                 }
3085         }
3086         END
3087 #endif
3088         {
3089                 char buf[256];
3090                 ERR("unknown variable %s", s);
3091                 obj->type = OBJ_text;
3092                 snprintf(buf, 256, "${%s}", s);
3093                 obj->data.s = strdup(buf);
3094         }
3095 #undef OBJ
3096
3097         return obj;
3098 }
3099
3100 static struct text_object *create_plain_text(const char *s)
3101 {
3102         struct text_object *obj;
3103
3104         if (s == NULL || *s == '\0') {
3105                 return NULL;
3106         }
3107
3108         obj = new_text_object_internal();
3109
3110         obj->type = OBJ_text;
3111         obj->data.s = strdup(s);
3112         return obj;
3113 }
3114
3115 static struct text_object_list *extract_variable_text_internal(const char *p)
3116 {
3117         struct text_object_list *retval;
3118         struct text_object *obj;
3119         const char *s = p;
3120
3121         retval = malloc(sizeof(struct text_object_list));
3122         memset(retval, 0, sizeof(struct text_object_list));
3123         retval->text_object_count = 0;
3124
3125         long line = text_lines;
3126
3127         while (*p) {
3128                 if (*p == '\n') {
3129                         line++;
3130                 }
3131                 if (*p == '$') {
3132                         *(char *) p = '\0';
3133                         obj = create_plain_text(s);
3134                         if(obj != NULL) {
3135                                 // allocate memory for the object
3136                                 retval->text_objects = realloc(retval->text_objects, 
3137                                                 sizeof(struct text_object) * (retval->text_object_count+1));
3138                                 // assign the new object to the end of the list.
3139                                 memcpy(&retval->text_objects[retval->text_object_count++],
3140                                                 obj, sizeof(struct text_object));
3141                                 free(obj);
3142                         }
3143                         *(char *) p = '$';
3144                         p++;
3145                         s = p;
3146
3147                         if (*p != '$') {
3148                                 char buf[256];
3149                                 const char *var;
3150                                 unsigned int len;
3151
3152                                 /* variable is either $foo or ${foo} */
3153                                 if (*p == '{') {
3154                                         p++;
3155                                         s = p;
3156                                         while (*p && *p != '}')
3157                                                 p++;
3158                                 } else {
3159                                         s = p;
3160                                         if (*p == '#')
3161                                                 p++;
3162                                         while (*p && (isalnum((int) *p)
3163                                                                 || *p == '_'))
3164                                                 p++;
3165                                 }
3166
3167                                 /* copy variable to buffer */
3168                                 len = (p - s > 255) ? 255 : (p - s);
3169                                 strncpy(buf, s, len);
3170                                 buf[len] = '\0';
3171
3172                                 if (*p == '}')
3173                                         p++;
3174                                 s = p;
3175
3176                                 var = getenv(buf);
3177
3178                                 /* if variable wasn't found from environment, use some special */
3179                                 if (!var) {
3180                                         char *p;
3181                                         char *arg = 0;
3182
3183                                         /* split arg */
3184                                         if (strchr(buf, ' ')) {
3185                                                 arg = strchr(buf, ' ');
3186                                                 *arg = '\0';
3187                                                 arg++;
3188                                                 while (isspace((int) *arg))
3189                                                         arg++;
3190                                                 if (!*arg)
3191                                                         arg = 0;
3192                                         }
3193
3194                                         /* lowercase variable name */
3195                                         p = buf;
3196                                         while (*p) {
3197                                                 *p = tolower(*p);
3198                                                 p++;
3199                                         }
3200
3201                                         // create new object
3202                                         obj = construct_text_object(buf, arg, retval->text_object_count, retval->text_objects, line);
3203                                         if(obj != NULL) {
3204                                                 // allocate memory for the object
3205                                                 retval->text_objects = realloc(retval->text_objects, 
3206                                                                 sizeof(struct text_object) * (retval->text_object_count+1));
3207                                                 // assign the new object to the end of the list.
3208                                                 memcpy(&retval->text_objects[retval->text_object_count++],
3209                                                                 obj, sizeof(struct text_object));
3210                                                 free(obj);
3211                                         }
3212                                 }
3213                                 continue;
3214                         } else {
3215                                 obj = create_plain_text("$");
3216                                 if(obj != NULL) {
3217                                         // allocate memory for the object
3218                                         retval->text_objects = realloc(retval->text_objects, 
3219                                                         sizeof(struct text_object) * (retval->text_object_count+1));
3220                                         // assign the new object to the end of the list.
3221                                         memcpy(&retval->text_objects[retval->text_object_count++],
3222                                                         obj, sizeof(struct text_object));
3223                                         free(obj);
3224                                 }
3225                         }
3226                 }
3227                 p++;
3228         }
3229         obj = create_plain_text(s);
3230         if(obj != NULL) {
3231                 // allocate memory for the object
3232                 retval->text_objects = realloc(retval->text_objects,
3233                                 sizeof(struct text_object) * (retval->text_object_count+1));
3234                 // assign the new object to the end of the list.
3235                 memcpy(&retval->text_objects[retval->text_object_count++],
3236                                 obj, sizeof(struct text_object));
3237                 free(obj);
3238         }
3239
3240         if (blockdepth) {
3241                 ERR("one or more $endif's are missing");
3242         }
3243
3244         return retval;
3245 }
3246
3247 static void extract_variable_text(const char *p)
3248 {
3249         struct text_object_list *list;
3250
3251         free_text_objects(text_object_count, text_objects);
3252         text_object_count = 0;
3253         text_objects = NULL;
3254
3255 #ifdef MLDONKEY 
3256         ml_cleanup();
3257 #endif /* MLDONKEY */
3258
3259
3260         list = extract_variable_text_internal(p);
3261         text_objects = list->text_objects;
3262         text_object_count = list->text_object_count;
3263
3264         free(list);
3265
3266         return;
3267 }
3268
3269 void parse_conky_vars(char * text, char * p, struct information *cur) { 
3270         struct text_object_list *object_list = extract_variable_text_internal(text);
3271         generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
3272         free(object_list);
3273 }
3274
3275 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
3276 {
3277         unsigned int i;
3278
3279 #ifdef HAVE_ICONV
3280         char buff_in[P_MAX_SIZE] = {0};
3281         iconv_converting = 0;
3282 #endif
3283
3284         for (i = 0; i < object_count; i++) {
3285                 struct text_object *obj = &objs[i];
3286
3287 #define OBJ(a) break; case OBJ_##a:
3288
3289                 switch (obj->type) {
3290                         default:
3291                                 {
3292                                         ERR("not implemented obj type %d",
3293                                                         obj->type);
3294                                 }
3295                                 OBJ(acpitemp) {
3296                                         /* does anyone have decimals in acpi temperature? */
3297                                         if (!use_spacer)
3298                                                 snprintf(p, p_max_size, "%d", (int)
3299                                                                 get_acpi_temperature(obj->
3300                                                                         data.
3301                                                                         i));
3302                                         else
3303                                                 snprintf(p, 5, "%d    ", (int)
3304                                                                 get_acpi_temperature(obj->
3305                                                                         data.
3306                                                                         i));
3307                                 }
3308                                 OBJ(acpitempf) {
3309                                         /* does anyone have decimals in acpi temperature? */
3310                                         if (!use_spacer)
3311                                                 snprintf(p, p_max_size, "%d", (int)
3312                                                                 ((get_acpi_temperature(obj->
3313                                                                                        data.
3314                                                                                        i)+ 40) * 9.0 / 5 - 40));
3315                                         else
3316                                                 snprintf(p, 5, "%d    ", (int)
3317                                                                 ((get_acpi_temperature(obj->
3318                                                                                        data.
3319                                                                                        i)+ 40) * 9.0 / 5 - 40));
3320                                 }
3321                                 OBJ(freq) {
3322                                         if (obj->a) {
3323                                                 obj->a = get_freq(p, p_max_size, "%.0f", 1, obj->data.cpu_index); 
3324                                         }
3325                                 }
3326                                 OBJ(freq_g) {
3327                                         if (obj->a) {
3328                                                 obj->a = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.cpu_index); 
3329                                         }
3330                                 }
3331 #if defined(__linux__)
3332                                 OBJ(voltage_mv) {
3333                                         if (obj->a) {
3334                                                 obj->a = get_voltage(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3335                                         }
3336                                 }
3337                                 OBJ(voltage_v) {
3338                                         if (obj->a) {
3339                                                 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.cpu_index);
3340                                         }
3341                                 }
3342 #endif /* __linux__ */
3343
3344                                 OBJ(freq_dyn) {
3345                                         if (use_spacer) {
3346                                                 get_freq_dynamic(p, 6, "%.0f     ", 1 ); 
3347                                         } else {
3348                                                 get_freq_dynamic(p, p_max_size, "%.0f", 1 ); 
3349                                         }
3350                                 }
3351                                 OBJ(freq_dyn_g) {
3352                                         if (use_spacer) {
3353                                                 get_freq_dynamic(p, 6, "%'.2f     ", 1000); 
3354                                         } else {
3355                                                 get_freq_dynamic(p, p_max_size, "%'.2f", 1000); 
3356                                         }
3357                                 }
3358                                 OBJ(adt746xcpu) {
3359                                         get_adt746x_cpu(p, p_max_size); 
3360                                 }
3361                                 OBJ(adt746xfan) {
3362                                         get_adt746x_fan(p, p_max_size); 
3363                                 }
3364                                 OBJ(acpifan) {
3365                                         get_acpi_fan(p, p_max_size);  
3366                                 }
3367                                 OBJ(acpiacadapter) {
3368                                         get_acpi_ac_adapter(p, p_max_size); 
3369                                 }
3370                                 OBJ(battery) {
3371                                         get_battery_stuff(p, p_max_size, obj->data.s);
3372                                 }
3373                                 OBJ(buffers) {
3374                                         human_readable(cur->buffers * 1024, p, 255);
3375                                 }
3376                                 OBJ(cached) {
3377                                         human_readable(cur->cached * 1024, p, 255);
3378                                 }
3379                                 OBJ(cpu) {
3380                                         if (obj->data.cpu_index > info.cpu_count) {
3381                                                 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
3382                                                 CRIT_ERR("attempting to use more CPUs then you have!");
3383                                         }
3384                                         if (!use_spacer)
3385                                                 snprintf(p, p_max_size, "%*d", pad_percents,
3386                                                                 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3387                                                                                    100.0));
3388                                         else
3389                                                 snprintf(p, 4, "%*d    ",
3390                                                                 pad_percents,
3391                                                                 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3392                                                                                    100.0));
3393                                 }
3394                                 OBJ(cpubar) {
3395                                         new_bar(p, obj->a,
3396                                                         obj->b,
3397                                                         (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3398                                 }
3399                                 OBJ(cpugraph) {
3400                                         new_graph(p, obj->a,
3401                                                         obj->b, obj->c, obj->d,
3402                                                         (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3403                                                                                     100), 100, 1);
3404                                 }
3405                                 OBJ(color) {
3406                                         new_fg(p, obj->data.l);
3407                                 }
3408 #if defined(__linux__)
3409                                 OBJ(i8k_version) {
3410                                         snprintf(p, p_max_size, "%s", i8k.version);
3411                                 }
3412                                 OBJ(i8k_bios) {
3413                                         snprintf(p, p_max_size, "%s", i8k.bios);
3414                                 }
3415                                 OBJ(i8k_serial) { 
3416                                         snprintf(p, p_max_size, "%s", i8k.serial);
3417                                 }
3418                                 OBJ(i8k_cpu_temp) { 
3419                                         snprintf(p, p_max_size, "%s", i8k.cpu_temp);
3420                                 }
3421                                 OBJ(i8k_cpu_tempf) { 
3422                                         int cpu_temp;
3423                                         sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3424                                         snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
3425                                 }
3426                                 OBJ(i8k_left_fan_status) { 
3427                                         int left_fan_status;
3428                                         sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3429                                         if(left_fan_status == 0) {
3430                                                 snprintf(p, p_max_size,"off");
3431                                         } if(left_fan_status == 1) {
3432                                                 snprintf(p, p_max_size, "low");
3433                                         }       if(left_fan_status == 2) {
3434                                                 snprintf(p, p_max_size, "high");
3435                                         }
3436
3437                                 }
3438                                 OBJ(i8k_right_fan_status) { 
3439                                         int right_fan_status;
3440                                         sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3441                                         if(right_fan_status == 0) {
3442                                                 snprintf(p, p_max_size,"off");
3443                                         } if(right_fan_status == 1) {
3444                                                 snprintf(p, p_max_size, "low");
3445                                         }       if(right_fan_status == 2) {
3446                                                 snprintf(p, p_max_size, "high");
3447                                         }
3448                                 }
3449                                 OBJ(i8k_left_fan_rpm) { 
3450                                         snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3451                                 }
3452                                 OBJ(i8k_right_fan_rpm) { 
3453                                         snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3454                                 }
3455                                 OBJ(i8k_ac_status) { 
3456                                         int ac_status;
3457                                         sscanf(i8k.ac_status, "%d", &ac_status);
3458                                         if(ac_status == -1) {
3459                                                 snprintf(p, p_max_size,"disabled (read i8k docs)");
3460                                         } if(ac_status == 0) {
3461                                                 snprintf(p, p_max_size, "off");
3462                                         }       if(ac_status == 1) {
3463                                                 snprintf(p, p_max_size, "on");
3464                                         }
3465                                 }
3466                                 OBJ(i8k_buttons_status) {
3467                                         snprintf(p, p_max_size, "%s", i8k.buttons_status); 
3468
3469                                 }
3470                                 OBJ(ibm_fan) {
3471                                     get_ibm_acpi_fan(p, p_max_size);
3472                                 }
3473                                 OBJ(ibm_temps) {
3474                                     get_ibm_acpi_temps();
3475                                     snprintf(p, p_max_size, "%d",
3476                                              ibm_acpi.temps[obj->data.sensor]);
3477                                 }
3478                                 OBJ(ibm_volume) {
3479                                     get_ibm_acpi_volume(p, p_max_size);
3480                                 }
3481                                 OBJ(ibm_brightness) {
3482                                     get_ibm_acpi_brightness(p, p_max_size);
3483                                 }
3484                                 OBJ(pb_battery) {
3485                                     get_powerbook_batt_info(p, p_max_size, obj->data.i);
3486                                 }
3487 #endif /* __linux__ */
3488
3489 #ifdef X11
3490                                 OBJ(font) {
3491                                         new_font(p, obj->data.s);
3492                                 }
3493 #endif /* X11 */
3494                                 OBJ(diskio) {
3495                                         if (!use_spacer) {
3496                                                 if (diskio_value > 1024*1024) {
3497                                                         snprintf(p, p_max_size, "%.1fGiB",
3498                                                                         (double)diskio_value/1024/1024);
3499                                                 } else if (diskio_value > 1024) {
3500                                                         snprintf(p, p_max_size, "%.1fMiB",
3501                                                                         (double)diskio_value/1024);
3502                                                 } else if (diskio_value > 0) {
3503                                                         snprintf(p, p_max_size, "%dKiB", diskio_value);
3504                                                 } else {
3505                                                         snprintf(p, p_max_size, "%dB", diskio_value);
3506                                                 }
3507                                         } else {
3508                                                 if (diskio_value > 1024*1024) {
3509                                                         snprintf(p, 6, "%.1fGiB   ",
3510                                                                         (double)diskio_value/1024/1024);
3511                                                 } else if (diskio_value > 1024) {
3512                                                         snprintf(p, 6, "%.1fMiB   ",
3513                                                                         (double)diskio_value/1024);
3514                                                 } else if (diskio_value > 0) {
3515                                                         snprintf(p, 6, "%dKiB ", diskio_value);
3516                                                 } else {
3517                                                         snprintf(p, 6, "%dB     ", diskio_value);
3518                                                 }
3519                                         }
3520                                 }
3521                                 OBJ(diskiograph) {
3522                                         new_graph(p, obj->a,
3523                                                         obj->b, obj->c, obj->d,
3524                                                         diskio_value, obj->e, 1);
3525                                 }
3526
3527                                 OBJ(downspeed) {
3528                                         if (!use_spacer) {
3529                                                 snprintf(p, p_max_size, "%d",
3530                                                                 (int) (obj->data.net->
3531                                                                        recv_speed /
3532                                                                        1024));
3533                                         } else
3534                                                 snprintf(p, 6, "%d     ",
3535                                                                 (int) (obj->data.net->
3536                                                                        recv_speed /
3537                                                                        1024));
3538                                 }
3539                                 OBJ(downspeedf) {
3540                                         if (!use_spacer)
3541                                                 snprintf(p, p_max_size, "%.1f",
3542                                                                 obj->data.net->
3543                                                                 recv_speed / 1024.0);
3544                                         else
3545                                                 snprintf(p, 8, "%.1f       ",
3546                                                                 obj->data.net->
3547                                                                 recv_speed / 1024.0);
3548                                 }
3549                                 OBJ(downspeedgraph) {
3550                                         if (obj->data.net->recv_speed == 0)     // this is just to make the ugliness at start go away
3551                                                 obj->data.net->recv_speed = 0.01;
3552                                         new_graph(p, obj->a, obj->b, obj->c, obj->d,
3553                                                         (obj->data.net->recv_speed /
3554                                                          1024.0), obj->e, 1);
3555                                 }
3556                                 OBJ(else) {
3557                                         if (!if_jumped) {
3558                                                 i = obj->data.ifblock.pos - 1;
3559                                         } else {
3560                                                 if_jumped = 0;
3561                                         }
3562                                 }
3563                                 OBJ(endif) {
3564                                         if_jumped = 0;
3565                                 }
3566 #ifdef HAVE_POPEN
3567                                 OBJ(addr) {
3568                                         snprintf(p, p_max_size, "%u.%u.%u.%u",
3569                                                         obj->data.net->addr.
3570                                                         sa_data[2] & 255,
3571                                                         obj->data.net->addr.
3572                                                         sa_data[3] & 255,
3573                                                         obj->data.net->addr.
3574                                                         sa_data[4] & 255,
3575                                                         obj->data.net->addr.
3576                                                         sa_data[5] & 255);
3577
3578                                 }
3579                                 OBJ(linkstatus) {
3580                                         snprintf(p, p_max_size, "%d",
3581                                                         obj->data.net->linkstatus);
3582                                 }
3583 #if defined(IMLIB2) && defined(X11)
3584                                 OBJ(image) {
3585                                         if (obj->a < 1) {
3586                                                 obj->a++;
3587                                         } else {
3588                                                 Imlib_Image image, buffer;
3589                                                 image = imlib_load_image(obj->data.s);
3590                                                 imlib_context_set_image(image);
3591                                                 if (image) {
3592                                                         int w, h;
3593                                                         w = imlib_image_get_width();
3594                                                         h = imlib_image_get_height();
3595                                                         buffer = imlib_create_image(w, h);
3596                                                         imlib_context_set_display(display);
3597                                                         imlib_context_set_drawable(window.drawable);
3598                                                         imlib_context_set_colormap(DefaultColormap(display, screen));
3599                                                         imlib_context_set_visual(DefaultVisual(display, screen));
3600                                                         imlib_context_set_image(buffer);
3601                                                         imlib_blend_image_onto_image(image, 0, 0, 0, w, h, text_start_x, text_start_y, w, h);
3602                                                         imlib_render_image_on_drawable(text_start_x, text_start_y);
3603                                                         imlib_free_image();
3604                                                         imlib_context_set_image(image);
3605                                                         imlib_free_image();
3606                                                 }
3607                                         }
3608                                 }
3609 #endif /* IMLIB2 */
3610                                 OBJ(exec) {
3611                                         FILE *fp = popen(obj->data.s, "r");
3612                                         int length = fread(p, 1, p_max_size, fp);
3613                                         (void) pclose(fp);
3614
3615                                         /*output[length] = '\0';
3616                                           if (length > 0 && output[length - 1] == '\n') {
3617                                           output[length - 1] = '\0';
3618                                           }*/
3619                                         p[length] = '\0';
3620                                         if (length > 0 && p[length - 1] == '\n') {
3621                                                 p[length - 1] = '\0';
3622                                         }
3623
3624                                         //parse_conky_vars(output, p, cur);
3625                                 }
3626                                 OBJ(execbar) {
3627                                         char *p2 = p;
3628                                         FILE *fp = popen(obj->data.s, "r");
3629                                         int n2 = fread(p, 1, p_max_size, fp);
3630                                         (void) pclose(fp);
3631
3632                                         p[n2] = '\0';
3633                                         if (n2 && p[n2 - 1] == '\n')
3634                                                 p[n2 - 1] = '\0';
3635
3636                                         while (*p2) {
3637                                                 if (*p2 == '\001')
3638                                                         *p2 = ' ';
3639                                                 p2++;
3640                                         }
3641                                         double barnum;
3642                                         if (sscanf(p, "%lf", &barnum) == 0) {
3643                                                 ERR("reading execbar value failed (perhaps it's not the correct format?)");
3644                                         }
3645                                         if (barnum > 100 || barnum < 0) {
3646                                                 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
3647                                         } else {
3648                                                 barnum = barnum / 100.0;
3649                                                 new_bar(p, 0, 4, (int) (barnum * 255.0));
3650                                         }
3651
3652                                 }
3653                                 OBJ(execgraph) {
3654                                         char *p2 = p;
3655                                         FILE *fp = popen(obj->data.s, "r");
3656                                         int n2 = fread(p, 1, p_max_size, fp);
3657                                         (void) pclose(fp);
3658
3659                                         p[n2] = '\0';
3660                                         if (n2 && p[n2 - 1] == '\n')
3661                                                 p[n2 - 1] = '\0';
3662
3663                                         while (*p2) {
3664                                                 if (*p2 == '\001')
3665                                                         *p2 = ' ';
3666                                                 p2++;
3667                                         }
3668                                         double barnum;
3669                                         if (sscanf(p, "%lf", &barnum) == 0) {
3670                                                 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
3671                                         }
3672                                         if (barnum > 100 || barnum < 0) {
3673                                                 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
3674                                         } else {
3675                                                 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
3676                                         }
3677
3678                                 }
3679                                 OBJ(execibar) {
3680                                         if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3681                                                 new_bar(p, 0, 4, (int) obj->f);
3682                                         } else {
3683                                                 char *p2 = p;
3684                                                 FILE *fp = popen(obj->data.execi.cmd, "r");
3685                                                 int n2 = fread(p, 1, p_max_size, fp);
3686                                                 (void) pclose(fp);
3687                                                 p[n2] = '\0';
3688                                                 if (n2 && p[n2 - 1] == '\n')
3689                                                         p[n2 - 1] = '\0';
3690
3691                                                 while (*p2) {
3692                                                         if (*p2 == '\001')
3693                                                                 *p2 = ' ';
3694                                                         p2++;
3695                                                 }
3696                                                 float barnum;
3697                                                 if (sscanf(p, "%f", &barnum) == 0) {
3698                                                         ERR("reading execibar value failed (perhaps it's not the correct format?)");
3699                                                 }
3700                                                 if (barnum > 100 || barnum < 0) {
3701                                                         ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
3702                                                 } else {
3703                                                         obj->f = 255 * barnum / 100.0;
3704                                                         new_bar(p, 0, 4, (int) obj->f);
3705                                                 }
3706                                                 obj->data.execi.last_update =
3707                                                         current_update_time;
3708                                         }
3709                                 }
3710                                 OBJ(execigraph) {
3711                                         if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3712                                                 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
3713                                         } else {
3714                                                 char *p2 = p;
3715                                                 FILE *fp = popen(obj->data.execi.cmd, "r");
3716                                                 int n2 = fread(p, 1, p_max_size, fp);
3717                                                 (void) pclose(fp);
3718                                                 p[n2] = '\0';
3719                                                 if (n2 && p[n2 - 1] == '\n')
3720                                                         p[n2 - 1] = '\0';
3721
3722                                                 while (*p2) {
3723                                                         if (*p2 == '\001')
3724                                                                 *p2 = ' ';
3725                                                         p2++;
3726                                                 }
3727                                                 float barnum;
3728                                                 if (sscanf(p, "%f", &barnum) == 0) {
3729                                                         ERR("reading execigraph value failed (perhaps it's not the correct format?)");
3730                                                 }
3731                                                 if (barnum > 100 || barnum < 0) {
3732                                                         ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
3733                                                 } else {
3734                                                         obj->f = barnum;
3735                                                         new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
3736                                                 }
3737                                                 obj->data.execi.last_update = current_update_time;
3738
3739                                         }
3740
3741                                 }
3742                                 OBJ(execi) {
3743                                         if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
3744                                                 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3745                                         } else {
3746                                                 char *output = obj->data.execi.buffer;
3747                                                 FILE *fp = popen(obj->data.execi.cmd, "r");
3748                                                 //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3749                                                 int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3750                                                 (void) pclose(fp);
3751
3752                                                 output[length] = '\0';
3753                                                 if (length > 0 && output[length - 1] == '\n') {
3754                                                         output[length - 1] = '\0';
3755                                                 }
3756                                                 obj->data.execi.last_update = current_update_time;
3757                                                 snprintf(p, p_max_size, "%s", output);
3758                                         }
3759                                         //parse_conky_vars(output, p, cur);
3760                                 }
3761                                 OBJ(texeci) {
3762                                         if (obj->data.execi.pos < 0) {
3763                                                 obj->data.execi.last_update = current_update_time;
3764                                                 if (pthread_create(&(obj->data.execi.thread_info.thread), NULL, (void*)threaded_exec, (void*) obj)) {
3765                                                         ERR("Error starting thread");
3766                                                 }
3767                                                 obj->data.execi.pos = register_thread(&(obj->data.execi.thread_info));
3768                                         }
3769                                         pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
3770                                         snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3771                                         pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
3772                                 }
3773 #endif /* HAVE_POPEN */
3774                                 OBJ(imap_unseen) {
3775                                         if (obj->global_mode && info.mail) { // this means we use info
3776                                                 if (info.mail->pos < 0) {
3777                                                         info.mail->last_update = current_update_time;
3778                                                         if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) info.mail)) {
3779                                                                 ERR("Error starting thread");
3780                                                         }
3781                                                         info.mail->pos = register_thread(&(info.mail->thread_info));
3782                                                 }
3783                                                 // get a lock before reading
3784                                                 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3785                                                 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3786                                                 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3787                                         } else if (obj->data.mail) { // this means we use obj
3788                                                 if (obj->data.mail->pos < 0) {
3789                                                         obj->data.mail->last_update = current_update_time;
3790                                                         if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) obj->data.mail)) {
3791                                                                 ERR("Error starting thread");
3792                                                         }
3793                                                         obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3794                                                 }
3795                                                 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3796                                                 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3797                                                 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3798                                         } else if (!obj->a) { // something is wrong, warn once then stop
3799                                                 ERR("Theres a problem with your imap_unseen settings.  Check that the global IMAP settings are defined properly (line %li).", obj->line);
3800                                                         obj->a++;
3801                                         }
3802                                 }
3803                                 OBJ(imap_messages) {
3804                                         if (obj->global_mode && info.mail) { // this means we use info
3805                                                 if (info.mail->pos < 0) {
3806                                                         info.mail->last_update = current_update_time;
3807                                                         if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) info.mail)) {
3808                                                                 ERR("Error starting thread");
3809                                                         }
3810                                                         info.mail->pos = register_thread(&(info.mail->thread_info));
3811                                                 }
3812                                                 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3813                                                 snprintf(p, p_max_size, "%lu", info.mail->messages);
3814                                                 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3815                                         } else if (obj->data.mail) { // this means we use obj
3816                                                 if (obj->data.mail->pos < 0) {
3817                                                         obj->data.mail->last_update = current_update_time;
3818                                                         if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) obj->data.mail)) {
3819                                                                 ERR("Error starting thread");
3820                                                         }
3821                                                         obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3822                                                 }
3823                                                 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3824                                                 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
3825                                                 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3826                                         } else if (!obj->a) { // something is wrong, warn once then stop
3827                                                 ERR("Theres a problem with your imap_messages settings.  Check that the global IMAP settings are defined properly (line %li).", obj->line);
3828                                                         obj->a++;
3829                                         }
3830                                 }
3831                                 OBJ(pop3_unseen) {
3832                                         if (obj->global_mode && info.mail) { // this means we use info
3833                                                 if (info.mail->pos < 0) {
3834                                                         info.mail->last_update = current_update_time;
3835                                                         if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) info.mail)) {
3836                                                                 ERR("Error starting thread");
3837                                                         }
3838                                                         info.mail->pos = register_thread(&(info.mail->thread_info));
3839                                                 }
3840                                                 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3841                                                 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3842                                                 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3843                                         } else if (obj->data.mail) { // this means we use obj
3844                                                 if (obj->data.mail->pos < 0) {
3845                                                         obj->data.mail->last_update = current_update_time;
3846                                                         if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) obj->data.mail)) {
3847                                                                 ERR("Error starting thread");
3848                                                         }
3849                                                         obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3850                                                 }
3851                                                 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3852                                                 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3853                                                 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3854                                         } else if (!obj->a) { // something is wrong, warn once then stop
3855                                                 ERR("Theres a problem with your pop3_unseen settings.  Check that the global POP3 settings are defined properly (line %li).", obj->line);
3856                                                         obj->a++;
3857                                         }
3858                                 }
3859                                 OBJ(pop3_used) {
3860                                         if (obj->global_mode && info.mail) { // this means we use info
3861                                                 if (info.mail->pos < 0) {
3862                                                         info.mail->last_update = current_update_time;
3863                                                         if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) info.mail)) {
3864                                                                 ERR("Error starting thread");
3865                                                         }
3866                                                         info.mail->pos = register_thread(&(info.mail->thread_info));
3867                                                 }
3868                                                 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3869                                                 snprintf(p, p_max_size, "%.1f", info.mail->used/1024.0/1024.0);
3870                                                 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3871                                         } else if (obj->data.mail) { // this means we use obj
3872                                                 if (obj->data.mail->pos < 0) {
3873                                                         obj->data.mail->last_update = current_update_time;
3874                                                         if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) obj->data.mail)) {
3875                                                                 ERR("Error starting thread");
3876                                                         }
3877                                                         obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3878                                                 }
3879                                                 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3880                                                 snprintf(p, p_max_size, "%.1f", obj->data.mail->used/1024.0/1024.0);
3881                                                 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3882                                         } else if (!obj->a) { // something is wrong, warn once then stop
3883                                                 ERR("Theres a problem with your pop3_used settings.  Check that the global POP3 settings are defined properly (line %li).", obj->line);
3884                                                         obj->a++;
3885                                         }
3886                                 }
3887                         OBJ(fs_bar) {
3888                                 if (obj->data.fs != NULL) {
3889                                         if (obj->data.fs->size == 0)
3890                                                 new_bar(p,
3891                                                         obj->data.fsbar.w,
3892                                                         obj->data.fsbar.h,
3893                                                         255);
3894                                         else
3895                                                 new_bar(p,
3896                                                         obj->data.fsbar.w,
3897                                                         obj->data.fsbar.h,
3898                                                         (int) (255 -
3899                                                                obj->data.
3900                                                                fsbar.fs->
3901                                                                avail *
3902                                                                255 /
3903                                                                obj->data.
3904                                                                fs->size));
3905                                 }
3906                         }
3907                         OBJ(fs_free) {
3908                                 if (obj->data.fs != NULL)
3909                                         human_readable(obj->data.fs->avail,
3910                                                        p, 255);
3911                         }
3912                         OBJ(fs_free_perc) {
3913                                 if (obj->data.fs != NULL) {
3914                                         if (obj->data.fs->size)
3915                                                 snprintf(p, p_max_size, "%*d",
3916                                                          pad_percents,
3917                                                          (int) ((obj->data.
3918                                                                  fs->
3919                                                                  avail *
3920                                                                  100) /
3921                                                                 obj->data.
3922                                                                 fs->size));
3923                                         else
3924                                                 snprintf(p, p_max_size, "0");
3925                                 }
3926                         }
3927                         OBJ(fs_size) {
3928                                 if (obj->data.fs != NULL)
3929                                         human_readable(obj->data.fs->size,
3930                                                        p, 255);
3931                         }
3932                         OBJ(fs_used) {
3933                                 if (obj->data.fs != NULL)
3934                                         human_readable(obj->data.fs->size -
3935                                                        (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
3936                                                        p, 255);
3937                         }
3938                         OBJ(fs_bar_free) {
3939                                 if (obj->data.fs != NULL) {
3940                                         if (obj->data.fs->size == 0)
3941                                                 new_bar(p,
3942                                                         obj->data.fsbar.w,
3943                                                         obj->data.fsbar.h,
3944                                                         255);
3945                                         else
3946                                                 new_bar(p,
3947                                                         obj->data.fsbar.w,
3948                                                         obj->data.fsbar.h,
3949                                                         (int) (obj->data.
3950                                                                fsbar.fs->
3951                                                                avail *
3952                                                                255 /
3953                                                                obj->data.
3954                                                                fs->size));
3955                                 }
3956                         }
3957                         OBJ(fs_used_perc) {
3958                                 if (obj->data.fs != NULL) {
3959                                         if (obj->data.fs->size)
3960                                                 snprintf(p, 4, "%d",
3961                                                          100 - ((int)
3962                                                                 ((obj->
3963                                                                   data.fs->
3964                                                                   avail *
3965                                                                   100) /
3966                                                                  obj->data.
3967                                                                  fs->
3968                                                                  size)));
3969                                         else
3970                                                 snprintf(p, p_max_size, "0");
3971                                 }
3972                         }
3973                         OBJ(loadavg) {
3974                                 float *v = info.loadavg;
3975
3976                                 if (obj->data.loadavg[2])
3977                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
3978                                                  v[obj->data.loadavg[0] -
3979                                                    1],
3980                                                  v[obj->data.loadavg[1] -
3981                                                    1],
3982                                                  v[obj->data.loadavg[2] -
3983                                                    1]);
3984                                 else if (obj->data.loadavg[1])
3985                                         snprintf(p, p_max_size, "%.2f %.2f",
3986                                                  v[obj->data.loadavg[0] -
3987                                                    1],
3988                                                  v[obj->data.loadavg[1] -
3989                                                    1]);
3990                                 else if (obj->data.loadavg[0])
3991                                         snprintf(p, p_max_size, "%.2f",
3992                                                  v[obj->data.loadavg[0] -
3993                                                    1]);
3994                         }
3995                         OBJ(goto) {
3996                                 new_goto(p, obj->data.i);
3997                         }
3998                         OBJ(tab) {
3999                                 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4000                         }
4001                         OBJ(hr) {
4002                                 new_hr(p, obj->data.i);
4003                         }
4004                         OBJ(hddtemp) {
4005                                 char *temp;
4006                                 char unit;
4007                                 
4008                                 temp = get_hddtemp_info(obj->data.hddtemp.dev, 
4009                                                 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
4010                                 if (!temp) {
4011                                         snprintf(p, p_max_size, "N/A");
4012                                 } else if (unit == '*') {
4013                                         snprintf(p, p_max_size, "%s", temp);
4014                                 } else {
4015                                          snprintf(p, p_max_size, "%s°%c", temp, unit);
4016                                 }
4017                         }
4018                         OBJ(offset) {
4019                                 new_offset(p, obj->data.i);
4020                         }
4021                         OBJ(voffset) {
4022                                 new_voffset(p, obj->data.i);
4023                         }
4024                         OBJ(i2c) {
4025                                 double r;
4026
4027                                 r = get_i2c_info(&obj->data.i2c.fd,
4028                                                  obj->data.i2c.arg,
4029                                                  obj->data.i2c.devtype,
4030                                                  obj->data.i2c.type);
4031
4032                                 if (r >= 100.0 || r == 0)
4033                                         snprintf(p, p_max_size, "%d", (int) r);
4034                                 else
4035                                         snprintf(p, p_max_size, "%.1f", r);
4036                         }
4037                         OBJ(alignr) {
4038                                 new_alignr(p, obj->data.i);
4039                         }
4040                         OBJ(alignc) {
4041                                 new_alignc(p, obj->data.i);
4042                         }
4043                         OBJ(if_existing) {
4044                                 struct stat tmp;
4045                                 if ((obj->data.ifblock.s)
4046                                     && (stat(obj->data.ifblock.s, &tmp) ==
4047                                         -1)) {
4048                                         i = obj->data.ifblock.pos;
4049                                         if_jumped = 1;
4050                                 } else {
4051                                         if_jumped = 0;
4052                                 }
4053                         }
4054                         OBJ(if_mounted) {
4055                                 if ((obj->data.ifblock.s)
4056                                     && (!check_mount(obj->data.ifblock.s))) {
4057                                         i = obj->data.ifblock.pos;
4058                                         if_jumped = 1;
4059                                 } else {
4060                                         if_jumped = 0;
4061                                 }
4062                         }
4063                         OBJ(if_running) {
4064                                 if ((obj->data.ifblock.s)
4065                                     && system(obj->data.ifblock.s)) {
4066                                         i = obj->data.ifblock.pos;
4067                                         if_jumped = 1;
4068                                 } else {
4069                                         if_jumped = 0;
4070                                 }
4071                         }
4072                         OBJ(kernel) {
4073                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4074                         }
4075                         OBJ(machine) {
4076                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4077                         }
4078
4079                         /* memory stuff */
4080                         OBJ(mem) {
4081                                 human_readable(cur->mem * 1024, p, 255);
4082                         }
4083                         OBJ(memmax) {
4084                                 human_readable(cur->memmax * 1024, p, 255);
4085                         }
4086                         OBJ(memperc) {
4087                                 if (cur->memmax) {
4088                                         if (!use_spacer)
4089                                                 snprintf(p, p_max_size, "%*lu",
4090                                                          pad_percents,
4091                                                          (cur->mem * 100) /
4092                                                          (cur->memmax));
4093                                         else
4094                                                 snprintf(p, 4, "%*lu   ",
4095                                                          pad_percents,
4096                                                          (cur->mem * 100) /
4097                                                          (cur->memmax));
4098                                 }
4099                         }
4100                         OBJ(membar) {
4101                                 new_bar(p, obj->data.pair.a,
4102                                         obj->data.pair.b,
4103                                         cur->memmax ? (cur->mem * 255) /
4104                                         (cur->memmax) : 0);
4105                         }
4106
4107                         OBJ(memgraph) {
4108                                 new_graph(p, obj->a,
4109                                 obj->b, obj->c, obj->d,
4110                                 cur->memmax ? (cur->mem * 100.0) /
4111                                                 (cur->memmax) : 0.0, 100, 1);
4112                         }
4113                         /* mixer stuff */
4114                         OBJ(mixer) {
4115                                 snprintf(p, p_max_size, "%d",
4116                                          mixer_get_avg(obj->data.l));
4117                         }
4118                         OBJ(mixerl) {
4119                                 snprintf(p, p_max_size, "%d",
4120                                          mixer_get_left(obj->data.l));
4121                         }
4122                         OBJ(mixerr) {
4123                                 snprintf(p, p_max_size, "%d",
4124                                          mixer_get_right(obj->data.l));
4125                         }
4126                         OBJ(mixerbar) {
4127                                 new_bar(p, obj->data.mixerbar.w,
4128                                         obj->data.mixerbar.h,
4129                                         mixer_get_avg(obj->data.mixerbar.
4130                                                       l) * 255 / 100);
4131                         }
4132                         OBJ(mixerlbar) {
4133                                 new_bar(p, obj->data.mixerbar.w,
4134                                         obj->data.mixerbar.h,
4135                                         mixer_get_left(obj->data.mixerbar.
4136                                                        l) * 255 / 100);
4137                         }
4138                         OBJ(mixerrbar) {
4139                                 new_bar(p, obj->data.mixerbar.w,
4140                                         obj->data.mixerbar.h,
4141                                         mixer_get_right(obj->data.mixerbar.
4142                                                         l) * 255 / 100);
4143                         }
4144
4145                         /* mail stuff */
4146                         OBJ(mails) {
4147                                 snprintf(p, p_max_size, "%d", cur->mail_count);
4148                         }
4149                         OBJ(new_mails) {
4150                                 snprintf(p, p_max_size, "%d", cur->new_mail_count);
4151                         }
4152 #ifdef MLDONKEY
4153                         OBJ(ml_upload_counter) {
4154                                 snprintf(p, p_max_size, "%lld",
4155                                          mlinfo.upload_counter / 1048576);
4156                         }
4157                         OBJ(ml_download_counter) {
4158                                 snprintf(p, p_max_size, "%lld",
4159                                          mlinfo.download_counter /
4160                                          1048576);
4161                         }
4162                         OBJ(ml_nshared_files) {
4163                                 snprintf(p, p_max_size, "%i", mlinfo.nshared_files);
4164                         }
4165                         OBJ(ml_shared_counter) {
4166                                 snprintf(p, p_max_size, "%lld",
4167                                          mlinfo.shared_counter / 1048576);
4168                         }
4169                         OBJ(ml_tcp_upload_rate) {
4170                                 snprintf(p, p_max_size, "%.2f",
4171                                          (float) mlinfo.tcp_upload_rate /
4172                                          1024);
4173                         }
4174                         OBJ(ml_tcp_download_rate) {
4175                                 snprintf(p, p_max_size, "%.2f",
4176                                          (float) mlinfo.tcp_download_rate /
4177                                          1024);
4178                         }
4179                         OBJ(ml_udp_upload_rate) {
4180                                 snprintf(p, p_max_size, "%.2f",
4181                                          (float) mlinfo.udp_upload_rate /
4182                                          1024);
4183                         }
4184                         OBJ(ml_udp_download_rate) {
4185                                 snprintf(p, p_max_size, "%.2f",
4186                                          (float) mlinfo.udp_download_rate /
4187                                          1024);
4188                         }
4189                         OBJ(ml_ndownloaded_files) {
4190                                 snprintf(p, p_max_size, "%i",
4191                                          mlinfo.ndownloaded_files);
4192                         }
4193                         OBJ(ml_ndownloading_files) {
4194                                 snprintf(p, p_max_size, "%i",
4195                                          mlinfo.ndownloading_files);
4196                         }
4197 #endif
4198
4199                         OBJ(nodename) {
4200                                 snprintf(p, p_max_size, "%s",
4201                                          cur->uname_s.nodename);
4202                         }
4203                         OBJ(outlinecolor) {
4204                                 new_outline(p, obj->data.l);
4205                         }
4206                         OBJ(processes) {
4207                                 if (!use_spacer)
4208                                         snprintf(p, p_max_size, "%hu", cur->procs);
4209                                 else
4210                                         snprintf(p, 5, "%hu    ",
4211                                                  cur->procs);
4212                         }
4213                         OBJ(running_processes) {
4214                                 if (!use_spacer)
4215                                         snprintf(p, p_max_size, "%hu",
4216                                                  cur->run_procs);
4217                                 else
4218                                         snprintf(p, 3, "%hu     ",
4219                                                  cur->run_procs);
4220                         }
4221                         OBJ(text) {
4222                                 snprintf(p, p_max_size, "%s", obj->data.s);
4223                         }
4224                         OBJ(shadecolor) {
4225                                 new_bg(p, obj->data.l);
4226                         }
4227                         OBJ(stippled_hr) {
4228                                 new_stippled_hr(p, obj->data.pair.a,
4229                                                 obj->data.pair.b);
4230                         }
4231                         OBJ(swap) {
4232                                 human_readable(cur->swap * 1024, p, 255);
4233                         }
4234                         OBJ(swapmax) {
4235                                 human_readable(cur->swapmax * 1024, p,
4236                                                255);
4237                         }
4238                         OBJ(swapperc) {
4239                                 if (cur->swapmax == 0) {
4240                                         strncpy(p, "No swap", 255);
4241                                 } else {
4242                                         if (!use_spacer)
4243                                                 snprintf(p, 255, "%*lu",
4244                                                          pad_percents,
4245                                                          (cur->swap *
4246                                                           100) /
4247                                                          cur->swapmax);
4248                                         else
4249                                                 snprintf(p, 4, "%*lu   ",
4250                                                          pad_percents,
4251                                                          (cur->swap *
4252                                                           100) /
4253                                                          cur->swapmax);
4254                                 }
4255                         }
4256                         OBJ(swapbar) {
4257                                 new_bar(p, obj->data.pair.a,
4258                                         obj->data.pair.b,
4259                                         cur->swapmax ? (cur->swap * 255) /
4260                                         (cur->swapmax) : 0);
4261                         }
4262                         OBJ(sysname) {
4263                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4264                         }
4265                         OBJ(time) {
4266                                 time_t t = time(NULL);
4267                                 struct tm *tm = localtime(&t);
4268                                 setlocale(LC_TIME, "");
4269                                 strftime(p, p_max_size, obj->data.s, tm);
4270                         }
4271                         OBJ(utime) {
4272                                 time_t t = time(NULL);
4273                                 struct tm *tm = gmtime(&t);
4274                                 strftime(p, p_max_size, obj->data.s, tm);
4275                         }
4276                         OBJ(tztime) {
4277                                 char* oldTZ = NULL;
4278                                 if (obj->data.tztime.tz) {
4279                                         oldTZ = getenv("TZ");
4280                                         setenv("TZ", obj->data.tztime.tz, 1);
4281                                         tzset();
4282                                 }
4283                                 time_t t = time(NULL);
4284                                 struct tm *tm = localtime(&t);
4285                                 setlocale(LC_TIME, "");
4286                                 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4287                                 if (oldTZ) {
4288                                         setenv("TZ", oldTZ, 1);
4289                                         tzset();
4290                                 } else {
4291                                         unsetenv("TZ");
4292                                 }
4293                                 // Needless to free oldTZ since getenv gives ptr to static data 
4294                         }
4295                         OBJ(totaldown) {
4296                                 human_readable(obj->data.net->recv, p,
4297                                                255);
4298                         }
4299                         OBJ(totalup) {
4300                                 human_readable(obj->data.net->trans, p,
4301                                                255);
4302                         }
4303                         OBJ(updates) {
4304                                 snprintf(p, p_max_size, "%d", total_updates);
4305                         }
4306                         OBJ(upspeed) {
4307                                 if (!use_spacer)
4308                                         snprintf(p, p_max_size, "%d",
4309                                                  (int) (obj->data.net->
4310                                                         trans_speed /
4311                                                         1024));
4312                                 else
4313                                         snprintf(p, 5, "%d     ",
4314                                                  (int) (obj->data.net->
4315                                                         trans_speed /
4316                                                         1024));
4317                         }
4318                         OBJ(upspeedf) {
4319                                 if (!use_spacer)
4320                                         snprintf(p, p_max_size, "%.1f",
4321                                                  obj->data.net->
4322                                                  trans_speed / 1024.0);
4323                                 else
4324                                         snprintf(p, 8, "%.1f       ",
4325                                                  obj->data.net->
4326                                                  trans_speed / 1024.0);
4327                         }
4328                         OBJ(upspeedgraph) {
4329                                 if (obj->data.net->trans_speed == 0)    // this is just to make the ugliness at start go away
4330                                         obj->data.net->trans_speed = 0.01;
4331                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4332                                           (obj->data.net->trans_speed /
4333                                 1024.0), obj->e, 1);
4334                         }
4335                         OBJ(uptime_short) {
4336                                 format_seconds_short(p, p_max_size,
4337                                                      (int) cur->uptime);
4338                         }
4339                         OBJ(uptime) {
4340                                 format_seconds(p, p_max_size, (int) cur->uptime);
4341                         }
4342
4343 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
4344                         OBJ(apm_adapter) {
4345                                 snprintf(p, p_max_size, "%s", get_apm_adapter());
4346                         }
4347                         OBJ(apm_battery_life) {
4348                                 char    *msg;
4349                                 msg = get_apm_battery_life();
4350                                 snprintf(p, p_max_size, "%s", msg);
4351                                 free(msg);
4352                         }
4353                         OBJ(apm_battery_time) {
4354                                 char    *msg;
4355                                 msg = get_apm_battery_time();
4356                                 snprintf(p, p_max_size, "%s", msg);
4357                                 free(msg);
4358                         }
4359 #endif /* __FreeBSD__ */
4360 #ifdef SETI
4361                         OBJ(seti_prog) {
4362                                 snprintf(p, p_max_size, "%.2f",
4363                                          cur->seti_prog * 100.0f);
4364                         }
4365                         OBJ(seti_progbar) {
4366                                 new_bar(p, obj->data.pair.a,
4367                                         obj->data.pair.b,
4368                                         (int) (cur->seti_prog * 255.0f));
4369                         }
4370                         OBJ(seti_credit) {
4371                                 snprintf(p, p_max_size, "%.0f", cur->seti_credit);
4372                         }
4373 #endif
4374
4375 #ifdef MPD
4376                         OBJ(mpd_title) {
4377                                 snprintf(p, p_max_size, "%s", cur->mpd.title);
4378                         }
4379                         OBJ(mpd_artist) {
4380                                 snprintf(p, p_max_size, "%s", cur->mpd.artist);
4381                         }
4382                         OBJ(mpd_album) {
4383                                 snprintf(p, p_max_size, "%s", cur->mpd.album);
4384                         }
4385                         OBJ(mpd_random) {
4386                                 snprintf(p, p_max_size, "%s", cur->mpd.random);
4387                         }
4388                         OBJ(mpd_repeat) {
4389                                 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4390                         }
4391                         OBJ(mpd_track) {
4392                                 snprintf(p, p_max_size, "%s", cur->mpd.track);
4393                         }
4394                         OBJ(mpd_name) {
4395                                 snprintf(p, p_max_size, "%s", cur->mpd.name);
4396                         }
4397                         OBJ(mpd_file) {
4398                                 snprintf(p, p_max_size, "%s", cur->mpd.file);
4399                         }
4400                         OBJ(mpd_vol) {
4401                                 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4402                         }
4403                         OBJ(mpd_bitrate) {
4404                                 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4405                         }
4406                         OBJ(mpd_status) {
4407                                 snprintf(p, p_max_size, "%s", cur->mpd.status);
4408                         }
4409                         OBJ(mpd_elapsed) {
4410                                 int days = 0, hours = 0, minutes =
4411                                     0, seconds = 0;
4412                                 int tmp = cur->mpd.elapsed;
4413                                 while (tmp >= 86400) {
4414                                         tmp -= 86400;
4415                                         days++;
4416                                 }
4417                                 while (tmp >= 3600) {
4418                                         tmp -= 3600;
4419                                         hours++;
4420                                 }
4421                                 while (tmp >= 60) {
4422                                         tmp -= 60;
4423                                         minutes++;
4424                                 }
4425                                 seconds = tmp;
4426                                 if (days > 0)
4427                                         snprintf(p, p_max_size, "%i days %i:%02i:%02i",
4428                                                  days, hours, minutes,
4429                                                  seconds);
4430                                 else if (hours > 0)
4431                                         snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4432                                                  minutes, seconds);
4433                                 else
4434                                         snprintf(p, p_max_size, "%i:%02i", minutes,
4435                                                  seconds);
4436                         }
4437                         OBJ(mpd_length) {
4438                                 int days = 0, hours = 0, minutes =
4439                                     0, seconds = 0;
4440                                 int tmp = cur->mpd.length;
4441                                 while (tmp >= 86400) {
4442                                         tmp -= 86400;
4443                                         days++;
4444                                 }
4445                                 while (tmp >= 3600) {
4446                                         tmp -= 3600;
4447                                         hours++;
4448                                 }
4449                                 while (tmp >= 60) {
4450                                         tmp -= 60;
4451                                         minutes++;
4452                                 }
4453                                 seconds = tmp;
4454                                 if (days > 0)
4455                                         snprintf(p, p_max_size,
4456                                                  "%i days %i:%02i:%02i",
4457                                                  days, hours, minutes,
4458                                                  seconds);
4459                                 else if (hours > 0)
4460                                         snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4461                                                  minutes, seconds);
4462                                 else
4463                                         snprintf(p, p_max_size, "%i:%02i", minutes,
4464                                                  seconds);
4465                         }
4466                         OBJ(mpd_percent) {
4467                                 snprintf(p, p_max_size, "%2.0f",
4468                                          cur->mpd.progress * 100);
4469                         }
4470                         OBJ(mpd_bar) {
4471                                 new_bar(p, obj->data.pair.a,
4472                                         obj->data.pair.b,
4473                                         (int) (cur->mpd.progress *
4474                                                255.0f));
4475                         }
4476                         OBJ(mpd_smart) {
4477                                 if (strlen(cur->mpd.title) < 2 && strlen(cur->mpd.title) < 2) {
4478                                         snprintf(p, p_max_size, "%s", cur->mpd.file);
4479                                 } else {
4480                                         snprintf(p, p_max_size, "%s - %s", cur->mpd.artist, cur->mpd.title);
4481                                 }
4482                         }
4483 #endif
4484 #ifdef XMMS2
4485                         OBJ(xmms2_artist) {
4486                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4487                         }
4488                         OBJ(xmms2_album) {
4489                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4490                         }
4491                         OBJ(xmms2_title) {
4492                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4493                         }
4494                         OBJ(xmms2_genre) {
4495                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4496                         }
4497                         OBJ(xmms2_comment) {
4498                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4499                         }
4500                         OBJ(xmms2_decoder) {
4501                                 snprintf(p, p_max_size, "%s", cur->xmms2.decoder);
4502                         }
4503                         OBJ(xmms2_transport) {
4504                                 snprintf(p, p_max_size, "%s", cur->xmms2.transport);
4505                         }
4506                         OBJ(xmms2_url) {
4507                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4508                         }
4509                         OBJ(xmms2_status) {
4510                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4511                         }
4512             OBJ(xmms2_date) {
4513                     snprintf(p, p_max_size, "%s", cur->xmms2.date);
4514                         }
4515                         OBJ(xmms2_tracknr) {
4516                             if (cur->xmms2.tracknr != -1)
4517                     snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4518                         }
4519                         OBJ(xmms2_bitrate) {
4520                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4521                         }
4522             OBJ(xmms2_id) {
4523                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4524                         }
4525             OBJ(xmms2_size) {
4526                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4527                         }
4528                         OBJ(xmms2_elapsed) {
4529                                 int tmp = cur->xmms2.elapsed;
4530                                 snprintf(p, p_max_size, "%02d:%02d",
4531                                     tmp / 60000, (tmp / 1000) % 60);
4532                         }
4533                         OBJ(xmms2_duration) {
4534                                 int tmp = cur->xmms2.duration;
4535                                 snprintf(p, p_max_size, "%02d:%02d",
4536                                     tmp / 60000, (tmp / 1000) % 60);
4537                         }
4538                         OBJ(xmms2_percent) {
4539                                 snprintf(p, p_max_size, "%2.0f",
4540                                          cur->xmms2.progress * 100);
4541                         }
4542                         OBJ(xmms2_bar) {
4543                                 new_bar(p, obj->data.pair.a,
4544                                         obj->data.pair.b,
4545                                         (int) (cur->xmms2.progress *
4546                                                255.0f));
4547                         }
4548                         OBJ(xmms2_smart) {
4549                                 if (strlen(cur->xmms2.title) < 2 && strlen(cur->xmms2.title) < 2) {
4550                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
4551                                 } else {
4552                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist, cur->xmms2.title);
4553                                 }
4554                         }
4555 #endif
4556 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
4557                         OBJ(xmms_status) {
4558                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_STATUS]);
4559                         }
4560                         OBJ(xmms_title) {
4561                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_TITLE]);
4562                         }
4563                         OBJ(xmms_length) {
4564                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_LENGTH]);
4565                         }
4566                         OBJ(xmms_length_seconds) {
4567                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_LENGTH_SECONDS]);
4568                         }
4569                         OBJ(xmms_position) {
4570                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_POSITION]);
4571                         }
4572                         OBJ(xmms_position_seconds) {
4573                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_POSITION_SECONDS]);
4574                         }
4575                         OBJ(xmms_bitrate) {
4576                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_BITRATE]);
4577                         }
4578                         OBJ(xmms_frequency) {
4579                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_FREQUENCY]);
4580                         }
4581                         OBJ(xmms_channels) {
4582                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_CHANNELS]);
4583                         }
4584                         OBJ(xmms_filename) {
4585                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_FILENAME]);
4586                         }
4587                         OBJ(xmms_playlist_length) {
4588                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_PLAYLIST_LENGTH]);
4589                         }
4590                         OBJ(xmms_playlist_position) {
4591                             snprintf(p, p_max_size, "%s", cur->xmms.items[XMMS_PLAYLIST_POSITION]);
4592                         }
4593                         OBJ(xmms_bar) {
4594                             double progress;
4595                             progress= atof(cur->xmms.items[XMMS_POSITION_SECONDS]) /
4596                                       atof(cur->xmms.items[XMMS_LENGTH_SECONDS]);
4597                             new_bar(p,obj->a,obj->b,(int)(progress*255.0f));
4598                         }
4599 #endif
4600 #ifdef BMPX
4601                         OBJ(bmpx_title) {
4602                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4603                         }
4604                         OBJ(bmpx_artist) {
4605                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4606                         }
4607                         OBJ(bmpx_album) {
4608                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4609                         }
4610                         OBJ(bmpx_uri) {
4611                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4612                         }
4613                         OBJ(bmpx_track) {
4614                                  snprintf(p, p_max_size, "%i", cur->bmpx.track);
4615                         }
4616                         OBJ(bmpx_bitrate) {
4617                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4618                         }
4619 #endif
4620                         OBJ(top) {
4621                                 if (obj->data.top.type == TOP_NAME
4622                                     && obj->data.top.num >= 0
4623                                     && obj->data.top.num < 10) {
4624                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
4625                                         // moving other shit around, which is really fucking annoying
4626                                         snprintf(p, 17, "%s                              ", cur->cpu[obj->data.top.num]->name);
4627                                 } else if (obj->data.top.type == TOP_CPU
4628                                            && obj->data.top.num >= 0
4629                                            && obj->data.top.num < 10) {
4630                                         snprintf(p, 7, "%3.2f      ",
4631                                                  cur->cpu[obj->data.top.
4632                                                           num]->amount);
4633                                 } else if (obj->data.top.type == TOP_PID
4634                                            && obj->data.top.num >= 0
4635                                            && obj->data.top.num < 10) {
4636                                         snprintf(p, 8, "%i           ",
4637                                                  cur->cpu[obj->data.top.
4638                                                           num]->pid);
4639                                 } else if (obj->data.top.type == TOP_MEM
4640                                            && obj->data.top.num >= 0
4641                                            && obj->data.top.num < 10) {
4642                                         snprintf(p, 7, "%3.2f       ",
4643                                                  cur->cpu[obj->data.top.
4644                                                           num]->totalmem);
4645                                 }
4646                         }
4647                         OBJ(top_mem) {
4648                                 if (obj->data.top.type == TOP_NAME
4649                                     && obj->data.top.num >= 0
4650                                     && obj->data.top.num < 10) {
4651                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
4652                                         // moving other shit around, which is really fucking annoying
4653                                         snprintf(p, 17,
4654                                                  "%s                              ",
4655                                                  cur->memu[obj->data.top.
4656                                                            num]->name);
4657                                 } else if (obj->data.top.type == TOP_CPU
4658                                            && obj->data.top.num >= 0
4659                                            && obj->data.top.num < 10) {
4660                                         snprintf(p, 7, "%3.2f      ",
4661                                                  cur->memu[obj->data.top.
4662                                                            num]->amount);
4663                                 } else if (obj->data.top.type == TOP_PID
4664                                            && obj->data.top.num >= 0
4665                                            && obj->data.top.num < 10) {
4666                                         snprintf(p, 8, "%i           ",
4667                                                  cur->memu[obj->data.top.
4668                                                            num]->pid);
4669                                 } else if (obj->data.top.type == TOP_MEM
4670                                            && obj->data.top.num >= 0
4671                                            && obj->data.top.num < 10) {
4672                                         snprintf(p, 7, "%3.2f       ",
4673                                                  cur->memu[obj->data.top.
4674                                                            num]->totalmem);
4675                                 }
4676                         }
4677
4678
4679                         OBJ(tail) {
4680                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4681                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4682                                 } else {
4683                                         obj->data.tail.last_update = current_update_time;
4684                                         FILE *fp;
4685                                         int i;
4686                                         int added = 0;
4687                                         tailstring *head = NULL;
4688                                         tailstring *headtmp = NULL;
4689                                         tailstring *freetmp = NULL;
4690                                         fp = fopen(obj->data.tail.logfile, "rt");
4691                                         if (fp == NULL) {
4692                                                 ERR("tail logfile failed to open");
4693                                         }
4694                                         else {
4695                                                 obj->data.tail.readlines = 0;
4696
4697                                                 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
4698                                                         if (added >= 30) {
4699                                                                 freelasttail(head);
4700                                                         }
4701                                                         else {
4702                                                                 added++;
4703                                                         }
4704                                                         addtail(&head, obj->data.tail.buffer);
4705                                                         obj->data.tail.readlines++;
4706                                                 }
4707
4708                                                 fclose(fp);
4709                                                 freetmp = head;
4710
4711                                                 if (obj->data.tail.readlines > 0) {
4712                                                         for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4713                                                                 addtail(&headtmp, head->data);
4714                                                                 head = head->next;
4715                                                         }
4716                                                         freetail(freetmp);
4717                                                         freetmp = headtmp;
4718                                                         strcpy(obj->data.tail.buffer, headtmp->data);
4719                                                         headtmp = headtmp->next;
4720                                                         for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4721                                                                 if (headtmp) {
4722                                                                         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 */
4723                                                                         headtmp = headtmp->next;
4724                                                                 }
4725                                                         }
4726
4727                                                         /* get rid of any ugly newlines at the end */
4728                                                         if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4729                                                                 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4730                                                         }
4731                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4732
4733                                                         freetail(freetmp);
4734                                                 } else {
4735                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
4736                                                         snprintf(p, p_max_size, "Logfile Empty");
4737                                                 }  /* if readlines */
4738                                         } /*  fp == NULL  */
4739                                 } /* if cur_upd_time >= */
4740         
4741                                 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4742
4743                         }
4744                         OBJ(head) {
4745                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4746                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4747                                 } else {
4748                                         obj->data.tail.last_update = current_update_time;
4749                                         FILE *fp;
4750                                         tailstring *head = NULL;
4751                                         tailstring *headtmp = NULL;
4752                                         tailstring *freetmp = NULL;
4753                                         fp = fopen(obj->data.tail.logfile, "rt");
4754                                         if (fp == NULL) {
4755                                                 ERR("head logfile failed to open");
4756                                         } else {
4757                                                 obj->data.tail.readlines = 0;
4758                                                 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
4759                                                         addtail(&head, obj->data.tail.buffer);
4760                                                         obj->data.tail.readlines++;
4761                                                 }
4762                                                 fclose(fp);
4763                                                 freetmp = head;
4764                                                 if (obj->data.tail.readlines > 0) {
4765                                                         while (head) {
4766                                                                 addtail(&headtmp, head->data);
4767                                                                 head = head->next;
4768                                                         }
4769                                                         freetail(freetmp);
4770                                                         freetmp = headtmp;
4771                                                         strcpy(obj->data.tail.buffer, headtmp->data);
4772                                                         headtmp = headtmp->next;
4773                                                         while (headtmp) {
4774                                                                 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 */
4775                                                                 headtmp = headtmp->next;
4776                                                         }
4777                                                         freetail(freetmp);
4778                                                         /* get rid of any ugly newlines at the end */
4779                                                         if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4780                                                                 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4781                                                         }
4782                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4783                                                 } else {
4784                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
4785                                                         snprintf(p, p_max_size, "Logfile Empty");
4786                                                 } /* if readlines > 0 */
4787                                         } /* if fp == null */
4788                                 } /* cur_upd_time >= */
4789
4790                                 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4791
4792                         }
4793 #ifdef TCP_PORT_MONITOR
4794                         OBJ(tcp_portmon)
4795                         {
4796                                 /* grab a pointer to this port monitor */
4797                                 tcp_port_monitor_t * p_monitor = 
4798                                         find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
4799                                                                 obj->data.tcp_port_monitor.port_range_begin,
4800                                                                 obj->data.tcp_port_monitor.port_range_end );
4801                                 if ( !p_monitor ) {
4802                                         snprintf(p, p_max_size, "monitor not found");
4803                                         break;
4804                                 }
4805
4806                                 /* now grab the text of interest */
4807                                 if ( peek_tcp_port_monitor( p_monitor, 
4808                                                             obj->data.tcp_port_monitor.item, 
4809                                                             obj->data.tcp_port_monitor.connection_index,
4810                                                             p, p_max_size ) != 0 )
4811                                 {
4812                                         snprintf(p, p_max_size, "monitor peek error");
4813                                         break;
4814                                 }
4815                         }
4816 #endif
4817
4818 #ifdef HAVE_ICONV
4819                         OBJ(iconv_start)
4820                         {
4821                                 iconv_converting = 1;
4822                                 iconv_selected = obj->a;
4823                                 
4824                         }
4825                         OBJ(iconv_stop)
4826                         {
4827                                 iconv_converting = 0;
4828                                 iconv_selected = 0;
4829                         }
4830 #endif
4831
4832                         break;
4833                 }
4834
4835                 {
4836                         unsigned int a = strlen(p);
4837
4838 #ifdef HAVE_ICONV
4839                         if (a > 0 && iconv_converting && iconv_selected > 0 && (iconv_cd[iconv_selected - 1] != (iconv_t)(-1))) {
4840                                 int bytes;
4841                                 size_t dummy1, dummy2;
4842                                 char *ptr = buff_in;
4843                                 char *outptr = p;
4844
4845                                 dummy1 = dummy2 = a;
4846                                 
4847                                 strncpy(buff_in, p, P_MAX_SIZE);
4848
4849                                 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
4850                                 while (dummy1 > 0) {
4851                                         bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1, &outptr, &dummy2);
4852                                         if (bytes == -1) {
4853                                                 ERR("Iconv codeset conversion failed");
4854                                                 break;
4855                                         }
4856                                 }
4857
4858                                 /* It is nessecary when we are converting from multibyte to singlebyte codepage */
4859                                 a = outptr - p;
4860                         }
4861 #endif
4862                         p += a;
4863                         p_max_size -= a;
4864                 }
4865         }
4866 }
4867
4868
4869 double current_update_time, last_update_time;
4870
4871 static void generate_text()
4872 {
4873         struct information *cur = &info;
4874         char *p;
4875
4876
4877         special_count = 0;
4878
4879         /* update info */
4880
4881         current_update_time = get_time();
4882
4883         update_stuff(cur);
4884
4885         /* add things to the buffer */
4886
4887         /* generate text */
4888
4889         p = text_buffer;
4890
4891         generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
4892
4893         if (stuff_in_upper_case) {
4894                 char *p;
4895
4896                 p = text_buffer;
4897                 while (*p) {
4898                         *p = toupper(*p);
4899                         p++;
4900                 }
4901         }
4902
4903
4904         last_update_time = current_update_time;
4905         total_updates++;
4906         //free(p);
4907 }
4908
4909
4910 #ifdef X11
4911 static void set_font()
4912 {
4913 #ifdef XFT
4914         if (use_xft) {
4915                         if (window.xftdraw != NULL) {
4916                                 XftDrawDestroy(window.xftdraw);
4917                         }
4918                         window.xftdraw = XftDrawCreate(display, window.drawable,
4919                                         DefaultVisual(display,
4920                                                         screen),
4921                                         DefaultColormap(display,
4922                                                         screen));
4923                 } else
4924 #endif
4925 {
4926         XSetFont(display, window.gc, fonts[selected_font].font->fid);
4927 }
4928 }
4929
4930 #endif /* X11 */
4931
4932 static inline int get_string_width(const char *s)
4933 {
4934 #ifdef X11
4935         return *s ? calc_text_width(s, strlen(s)) : 0;
4936 #else
4937         return strlen(s);
4938 #endif /* X11 */
4939 }
4940
4941 static inline int get_string_width_special(char *s)
4942 {
4943         if (!s) {
4944                 return 0;
4945         }
4946 #ifdef X11
4947         char *p, *final;
4948         p = strdup(s);
4949         final = p;
4950         int index = 1;
4951         int width = 0;
4952         unsigned int i;
4953         while (*p) {
4954                 if (*p == SPECIAL_CHAR) {
4955                         /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
4956                         for (i = 0; i < strlen(p); i++) {
4957                                 *(p + i) = *(p + i + 1);
4958                         }
4959                         if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
4960                                 width += specials[special_index+index].width;
4961                         }
4962                         index++;
4963                 } else {
4964                         p++;
4965                 }
4966         }
4967         if (strlen(final) > 1) {
4968                 width += calc_text_width(final, strlen(final));
4969         }
4970         free(final);
4971         return width;
4972 #else
4973         return strlen(s);
4974 #endif /* X11 */
4975 }
4976
4977 #ifdef X11
4978 static void text_size_updater(char *s);
4979
4980 int last_font_height;
4981 static void update_text_area()
4982 {
4983         int x, y;
4984
4985         /* update text size if it isn't fixed */
4986 #ifdef OWN_WINDOW
4987         if (!fixed_size)
4988 #endif
4989         {
4990                 text_width = minimum_width;
4991                 text_height = 0;
4992                 special_index = 0;
4993                 last_font_height = font_height();
4994                 for_each_line(text_buffer, text_size_updater);
4995                 text_width += 1;
4996                 if (text_height < minimum_height)
4997                         text_height = minimum_height;
4998                 if (text_width > maximum_width && maximum_width > 0)
4999                         text_width = maximum_width;
5000         }
5001
5002         /* get text position on workarea */
5003         switch (text_alignment) {
5004         case TOP_LEFT:
5005                 x = gap_x;
5006                 y = gap_y;
5007                 break;
5008
5009         case TOP_RIGHT:
5010                 x = workarea[2] - text_width - gap_x;
5011                 y = gap_y;
5012                 break;
5013
5014         default:
5015         case BOTTOM_LEFT:
5016                 x = gap_x;
5017                 y = workarea[3] - text_height - gap_y;
5018                 break;
5019
5020         case BOTTOM_RIGHT:
5021                 x = workarea[2] - text_width - gap_x;
5022                 y = workarea[3] - text_height - gap_y;
5023                 break;
5024         
5025 #ifdef OWN_WINDOW
5026         case NONE: // Let the WM manage the window
5027                 x = window.x;
5028                 y = window.y;
5029
5030                 fixed_pos  = 1;
5031                 fixed_size = 1;
5032                 break;
5033 #endif
5034         }
5035 #ifdef OWN_WINDOW
5036
5037         if (own_window && !fixed_pos) {
5038                 x += workarea[0];
5039                 y += workarea[1];
5040                 text_start_x = border_margin + 1;
5041                 text_start_y = border_margin + 1;
5042                 window.x = x - border_margin - 1;
5043                 window.y = y - border_margin - 1;
5044         } else
5045 #endif
5046         {
5047                 /* If window size doesn't match to workarea's size, then window
5048                  * probably includes panels (gnome).
5049                  * Blah, doesn't work on KDE. */
5050                 if (workarea[2] != window.width
5051                     || workarea[3] != window.height) {
5052                         y += workarea[1];
5053                         x += workarea[0];
5054                 }
5055
5056                 text_start_x = x;
5057                 text_start_y = y;
5058         }
5059 }
5060
5061 /*
5062  * drawing stuff
5063  */
5064
5065 static int cur_x, cur_y;        /* current x and y for drawing */
5066 static int draw_mode;           /* FG, BG or OUTLINE */
5067 static long current_color;
5068
5069 #ifdef X11
5070 static void text_size_updater(char *s)
5071 {
5072         int w = 0;
5073         char *p;
5074         /* get string widths and skip specials */
5075         p = s;
5076         while (*p) {
5077                 if (*p == SPECIAL_CHAR) {
5078                         *p = '\0';
5079                         w += get_string_width(s);
5080                         *p = SPECIAL_CHAR;
5081
5082                         if (specials[special_index].type == BAR
5083                             || specials[special_index].type == GRAPH) {
5084                                 w += specials[special_index].width;
5085                                 if (specials[special_index].height > last_font_height) {
5086                                         last_font_height = specials[special_index].height;
5087                                         last_font_height += font_ascent();
5088                                 }
5089                         } else if (specials[special_index].type == OFFSET) {
5090                                 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
5091                         } else if (specials[special_index].type == VOFFSET) {
5092                                 last_font_height += specials[special_index].arg;
5093                         } else if (specials[special_index].type == GOTO) {
5094                                 if (specials[special_index].arg >= 0)
5095                                         w += (int)specials[special_index].arg - cur_x;
5096                         } else if (specials[special_index].type == TAB) { 
5097                                 int start = specials[special_index].arg;
5098                                 int step = specials[special_index].width;
5099                                 if (!step || step < 0)
5100                                         step = 10;
5101                                 w += step - (cur_x - text_start_x - start) % step;
5102                         } else if (specials[special_index].type == FONT) {
5103                                 selected_font = specials[special_index].font_added;
5104                                 if (font_height() > last_font_height) {
5105                                         last_font_height = font_height();
5106                                 }
5107                         }
5108                         
5109                         special_index++;
5110                         s = p + 1;
5111                 }
5112                 p++;
5113         }
5114         w += get_string_width(s);
5115         if (w > text_width) {
5116                 text_width = w;
5117         }
5118         if (text_width > maximum_width && maximum_width) {
5119                 text_width = maximum_width;
5120         }
5121
5122         text_height += last_font_height;
5123         last_font_height = font_height();
5124 }
5125 #endif /* X11 */
5126
5127 static inline void set_foreground_color(long c)
5128 {
5129         current_color = c;
5130         XSetForeground(display, window.gc, c);
5131 }
5132 #endif /* X11 */
5133
5134 static void draw_string(const char *s)
5135 {
5136         if (s[0] == '\0')
5137                 return;
5138         int i, i2, pos, width_of_s;
5139         int max=0;
5140         int added;
5141         width_of_s = get_string_width(s);
5142         if (out_to_console) {
5143                 printf("%s\n", s);
5144                 fflush(stdout);   /* output immediately, don't buffer */
5145         }
5146         /* 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 */
5147         memset(tmpstring1,0,TEXT_BUFFER_SIZE);
5148         memset(tmpstring2,0,TEXT_BUFFER_SIZE);
5149         strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
5150         pos = 0;
5151         added = 0;
5152         char space[2];
5153         snprintf(space, 2, " ");
5154 #ifdef X11
5155         max = ((text_width - width_of_s) / get_string_width(space));
5156 #endif /* X11 */
5157         /*
5158          * This code looks for tabs in the text and coverts them to spaces.
5159          * The trick is getting the correct number of spaces,
5160          * and not going over the window's size without forcing
5161          * the window larger.
5162          */
5163         for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
5164                 if (tmpstring1[i] == '\t')      // 9 is ascii tab
5165                 {
5166                         i2 = 0;
5167                         for (i2 = 0;
5168                              i2 < (8 - (1 + pos) % 8) && added <= max;
5169                              i2++) {
5170                                 /*
5171                                 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
5172                                         fprintf(stderr,"buffer overrun detected\n");
5173                                 */
5174                                 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
5175                                 added++;
5176                         }
5177                         pos += i2;
5178                 } else {
5179                         if (tmpstring1[i] != 9) {
5180                                 /*
5181                                 if ( pos > TEXT_BUFFER_SIZE-1 )
5182                                          fprintf(stderr,"buffer overrun detected\n");
5183                                 */
5184                                 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
5185                                 pos++;
5186                         }
5187                 }
5188         }
5189 #ifdef X11
5190         if (text_width == maximum_width) {
5191                 /* this means the text is probably pushing the limit, so we'll chop it */
5192                 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
5193                         tmpstring2[strlen(tmpstring2)-1] = '\0';
5194                 }
5195         }
5196 #endif /* X11 */
5197         s = tmpstring2;
5198 #ifdef X11
5199 #ifdef XFT
5200         if (use_xft) {
5201                 XColor c;
5202                 XftColor c2;
5203                 c.pixel = current_color;
5204                 XQueryColor(display, DefaultColormap(display, screen), &c);
5205
5206                 c2.pixel = c.pixel;
5207                 c2.color.red = c.red;
5208                 c2.color.green = c.green;
5209                 c2.color.blue = c.blue;
5210                 c2.color.alpha = fonts[selected_font].font_alpha;
5211                 if (utf8_mode) {
5212                         XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5213                                           cur_x, cur_y, (XftChar8 *) s,
5214                                           strlen(s));
5215                 } else {
5216                         XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5217                                        cur_x, cur_y, (XftChar8 *) s,
5218                                        strlen(s));
5219                 }
5220         } else
5221 #endif
5222         {
5223                 XDrawString(display, window.drawable, window.gc,
5224                             cur_x, cur_y, s, strlen(s));
5225         }
5226         cur_x += width_of_s;
5227 #endif /* X11 */
5228         memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
5229 }
5230
5231 long redmask, greenmask, bluemask;
5232
5233 void set_up_gradient()
5234 {
5235 #ifdef X11
5236         colour_depth = DisplayPlanes(display, screen);
5237 #else
5238         colour_depth = 16;
5239 #endif /* X11 */
5240         if (colour_depth != 24 && colour_depth != 16) {
5241                 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
5242         }
5243         int i;
5244         redmask = 0;
5245         greenmask = 0;
5246         bluemask = 0;
5247         for(i = (colour_depth / 3)-1; i>=0; i--) {
5248                 redmask |= 1 << i;
5249                 greenmask |= 1 << i;
5250                 bluemask |= 1 << i;
5251         }
5252         if (colour_depth%3 == 1) {
5253                 greenmask |= 1 << (colour_depth / 3);
5254         }
5255         redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
5256         greenmask = greenmask << (colour_depth / 3);
5257 }
5258
5259 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 */
5260         int tmp_color = 0;
5261         int red1, green1, blue1; // first colour
5262         int red2, green2, blue2; // second colour
5263         int red3 = 0, green3 = 0, blue3 = 0; // difference
5264         short redshift = (2*colour_depth / 3 + colour_depth%3);
5265         short greenshift = (colour_depth / 3);
5266         red1 = (first_colour & redmask) >> redshift;
5267         green1 = (first_colour & greenmask) >> greenshift;
5268         blue1 = first_colour & bluemask;
5269         red2 = (last_colour & redmask) >> redshift;
5270         green2 = (last_colour & greenmask) >> greenshift;
5271         blue2 = last_colour & bluemask;
5272         if (red1 > red2) {
5273                 red3 = -1;
5274         }
5275         if (red1 < red2) {
5276                 red3 = 1;
5277         }
5278         if (green1 > green2) {
5279                 green3 = -1;
5280         }
5281         if (green1 < green2) {
5282                 green3 = 1;
5283         }
5284         if (blue1 > blue2) {
5285                 blue3 = -1;
5286         }
5287         if (blue1 < blue2) {
5288                 blue3 = 1;
5289         }
5290         red1 += red3;
5291         green1 += green3;
5292         blue1 += blue3;
5293         if (red1 < 0) {
5294                 red1 = 0;
5295         }
5296         if (green1 < 0) {
5297                 green1 = 0;
5298         }
5299         if (blue1 < 0) {
5300                 blue1 = 0;
5301         }
5302         if (red1 > bluemask) {
5303                 red1 = bluemask;
5304         }
5305         if (green1 > bluemask) {
5306                 green1 = bluemask;
5307         }
5308         if (blue1 > bluemask) {
5309                 blue1 = bluemask;
5310         }
5311         tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
5312         return tmp_color;
5313 }
5314
5315 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
5316         if (colour_depth == 0) {
5317                 set_up_gradient();
5318         }
5319         int red1, green1, blue1; // first colour
5320         int red2, green2, blue2; // second colour
5321         long redshift = (2*colour_depth / 3 + colour_depth%3);
5322         long greenshift = (colour_depth / 3);
5323         int red3 = 0, green3 = 0, blue3 = 0; // difference
5324         red1 = (first_colour & redmask) >> redshift;
5325         green1 = (first_colour & greenmask) >> greenshift;
5326         blue1 = first_colour & bluemask;
5327         red2 = (last_colour & redmask) >> redshift;
5328         green2 = (last_colour & greenmask) >> greenshift;
5329         blue2 = last_colour & bluemask;
5330         red3 = abs(red1 - red2);
5331         green3 = abs(green1 - green2);
5332         blue3 = abs(blue1 - blue2);
5333         int max = red3;
5334         if (green3 > max)
5335                 max = green3;
5336         if (blue3 > max)
5337                 max = blue3;
5338         return max;
5339 }
5340
5341 static void draw_line(char *s)
5342 {
5343 #ifdef X11
5344         char *p;
5345         cur_x = text_start_x;
5346         cur_y += font_ascent();
5347         int cur_y_add = 0;
5348         short font_h = font_height();
5349
5350         /* find specials and draw stuff */
5351         p = s;
5352         while (*p) {
5353                 if (*p == SPECIAL_CHAR) {
5354                         int w = 0;
5355
5356                         /* draw string before special */
5357                         *p = '\0';
5358                         draw_string(s);
5359                         *p = SPECIAL_CHAR;
5360                         s = p + 1;
5361
5362                         /* draw special */
5363                         switch (specials[special_index].type) {
5364                         case HORIZONTAL_LINE:
5365                                 {
5366                                         int h =
5367                                             specials[special_index].height;
5368                                         int mid = font_ascent() / 2;
5369                                         w = text_start_x + text_width -
5370                                             cur_x;
5371
5372                                         XSetLineAttributes(display,
5373                                                            window.gc, h,
5374                                                            LineSolid,
5375                                                            CapButt,
5376                                                            JoinMiter);
5377                                         XDrawLine(display, window.drawable,
5378                                                   window.gc, cur_x,
5379                                                   cur_y - mid / 2,
5380                                                   cur_x + w,
5381                                                   cur_y - mid / 2);
5382                                 }
5383                                 break;
5384
5385                         case STIPPLED_HR:
5386                                 {
5387                                         int h =
5388                                             specials[special_index].height;
5389                                         int s =
5390                                             specials[special_index].arg;
5391                                         int mid = font_ascent() / 2;
5392                                         char ss[2] = { s, s };
5393                                         w = text_start_x + text_width -
5394                                             cur_x - 1;
5395
5396                                         XSetLineAttributes(display,
5397                                                            window.gc, h,
5398                                                            LineOnOffDash,
5399                                                            CapButt,
5400                                                            JoinMiter);
5401                                         XSetDashes(display, window.gc, 0,
5402                                                    ss, 2);
5403                                         XDrawLine(display, window.drawable,
5404                                                   window.gc, cur_x,
5405                                                   cur_y - mid / 2,
5406                                                   cur_x + w,
5407                                                   cur_y - mid / 2);
5408                                 }
5409                                 break;
5410
5411                         case BAR:
5412                                 {
5413                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5414                                                 break;
5415                                         }
5416                                         int h =
5417                                             specials[special_index].height;
5418                                         int bar_usage =
5419                                             specials[special_index].arg;
5420                                         int by = cur_y - (font_ascent() / 2) - 1;
5421                                         if (h < font_height()) {
5422                                                 by -= h / 2 - 1;
5423                                         }
5424                                         w = specials[special_index].width;
5425                                         if (w == 0)
5426                                                 w = text_start_x +
5427                                                     text_width - cur_x - 1;
5428                                         if (w < 0)
5429                                                 w = 0;
5430
5431                                         XSetLineAttributes(display,
5432                                                            window.gc, 1,
5433                                                            LineSolid,
5434                                                            CapButt,
5435                                                            JoinMiter);
5436
5437                                         XDrawRectangle(display,
5438                                                        window.drawable,
5439                                                        window.gc, cur_x,
5440                                                        by, w, h);
5441                                         XFillRectangle(display,
5442                                                        window.drawable,
5443                                                        window.gc, cur_x,
5444                                                        by,
5445                                                        w * bar_usage / 255,
5446                                                        h);
5447                                         if (specials[special_index].
5448                                             height > cur_y_add
5449                                             && specials[special_index].
5450                                             height > font_h) {
5451                                                 cur_y_add =
5452                                                     specials
5453                                                     [special_index].height;
5454                                         }
5455                                 }
5456                                 break;
5457
5458                         case GRAPH:
5459                         {
5460                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5461                                                 break;
5462                                         }
5463                                         int h =
5464                                             specials[special_index].height;
5465                                         unsigned long last_colour = current_color;
5466                                         int by = cur_y - (font_ascent()/2) - 1;
5467                                         if (h < font_height()) {
5468                                                 by -= h / 2 - 1;
5469                                         }
5470                                         w = specials[special_index].width;
5471                                         if (w == 0)
5472                                                 w = text_start_x + text_width - cur_x - 1;
5473                                         if (w < 0)
5474                                                 w = 0;
5475                                         if (draw_graph_borders) {
5476                                                 XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
5477                                                 XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
5478                                         }
5479                                         XSetLineAttributes(display,
5480                                                            window.gc, 1,
5481                                                            LineSolid,
5482                                                            CapButt,
5483                                                            JoinMiter);
5484         int i;
5485         int j = 0;
5486         int gradient_size = 0;
5487         float gradient_factor = 0;
5488         float gradient_update = 0;
5489         unsigned long tmpcolour = current_color;
5490         if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5491                 tmpcolour = specials[special_index].last_colour;
5492                 gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
5493                 gradient_factor = (float)gradient_size / (w - 2);
5494         }
5495         for (i = w - 2; i > -1; i--) {
5496                 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5497                         XSetForeground(display, window.gc, tmpcolour);
5498                         gradient_update += gradient_factor;
5499                         while (gradient_update > 0) {
5500                                 tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
5501                                 gradient_update--;
5502                         }
5503                 }
5504                 if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
5505                         j++;
5506                 }
5507                                                 XDrawLine(display,  window.drawable, window.gc, cur_x + i + 1, by + h, cur_x + i + 1, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale);       /* this is mugfugly, but it works */
5508                                         }
5509                                         if (specials[special_index].
5510                                             height > cur_y_add
5511                                             && specials[special_index].
5512                                             height > font_h) {
5513                                                 cur_y_add =
5514                                                     specials
5515                                                     [special_index].height;
5516                                         }
5517 /*                              if (draw_mode == BG) {
5518                                         set_foreground_color(default_bg_color);
5519                                 }
5520                                 else if (draw_mode == OUTLINE) {
5521                                         set_foreground_color(default_out_color);
5522                                 } else {
5523                                         set_foreground_color(default_fg_color);
5524                                 }*/
5525                                 set_foreground_color(last_colour);
5526                         }
5527                                 break;
5528                         
5529                                 case FONT:
5530                                 {
5531                                         int old = font_ascent();
5532                                         cur_y -= font_ascent();
5533                                         selected_font = specials[special_index].font_added;
5534                                         if (cur_y + font_ascent() < cur_y + old) {
5535                                                 cur_y += old;
5536                                         } else {
5537                                                 cur_y += font_ascent();
5538                                         }
5539                                         set_font();
5540                                 }
5541                                 break;
5542                         case FG:
5543                                 if (draw_mode == FG)
5544                                         set_foreground_color(specials
5545                                                              [special_index].
5546                                                              arg);
5547                                 break;
5548
5549                         case BG:
5550                                 if (draw_mode == BG)
5551                                         set_foreground_color(specials
5552                                                              [special_index].
5553                                                              arg);
5554                                 break;
5555
5556                         case OUTLINE:
5557                                 if (draw_mode == OUTLINE)
5558                                         set_foreground_color(specials
5559                                                              [special_index].
5560                                                              arg);
5561                                 break;
5562
5563                         case OFFSET:
5564                                 w += specials[special_index].arg;
5565                                 break;
5566                 
5567                         case VOFFSET:
5568                                 cur_y += specials[special_index].arg;
5569                                 break;
5570
5571                         case GOTO:
5572                                 if (specials[special_index].arg >= 0)
5573                                         cur_x = (int)specials[special_index].arg;
5574                                 break;
5575
5576                         case TAB:
5577                                 {
5578                                         int start = specials[special_index].arg;
5579                                         int step = specials[special_index].width;
5580                                         if (!step || step < 0)
5581                                                 step = 10;
5582                                         w = step - (cur_x - text_start_x - start) % step;
5583                                 }
5584                                 break;
5585
5586                         case ALIGNR:
5587                                 {
5588                                         int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
5589                                         /*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);*/
5590                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5591                                                 cur_x = pos_x - specials[special_index].arg;
5592                                 }
5593                                 }
5594                                 break;
5595
5596                         case ALIGNC:
5597                                 {
5598                                         int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
5599                                         /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
5600                                         /*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);*/
5601                                         if (pos_x >
5602                                             specials[special_index].arg)
5603                                                 w = pos_x -
5604                                                     specials
5605                                                     [special_index].arg;
5606                                 }
5607                                 break;
5608
5609                         }
5610
5611                         cur_x += w;
5612
5613                         special_index++;
5614                 }
5615
5616                 p++;
5617         }
5618 #else
5619         draw_string(s);
5620 #endif
5621 #ifdef X11
5622         if (cur_y_add > 0) {
5623                 cur_y += cur_y_add;
5624                 cur_y -= font_descent();
5625         }
5626
5627         draw_string(s);
5628
5629         cur_y += font_descent();
5630 #endif /* X11 */
5631 }
5632
5633 static void draw_text()
5634 {
5635 #ifdef X11
5636         cur_y = text_start_y;
5637
5638         /* draw borders */
5639         if (draw_borders && border_width > 0) {
5640                 unsigned int b = (border_width + 1) / 2;
5641
5642                 if (stippled_borders) {
5643                         char ss[2] =
5644                             { stippled_borders, stippled_borders };
5645                         XSetLineAttributes(display, window.gc,
5646                                            border_width, LineOnOffDash,
5647                                            CapButt, JoinMiter);
5648                         XSetDashes(display, window.gc, 0, ss, 2);
5649                 } else {
5650                         XSetLineAttributes(display, window.gc,
5651                                            border_width, LineSolid,
5652                                            CapButt, JoinMiter);
5653                 }
5654
5655                 XDrawRectangle(display, window.drawable, window.gc,
5656                                text_start_x - border_margin + b,
5657                                text_start_y - border_margin + b,
5658                                text_width + border_margin * 2 - 1 - b * 2,
5659                                text_height + border_margin * 2 - 1 -
5660                                b * 2);
5661         }
5662
5663         /* draw text */
5664         special_index = 0;
5665 #endif /* X11 */
5666         for_each_line(text_buffer, draw_line);
5667 }
5668
5669 static void draw_stuff()
5670 {
5671 #ifdef X11
5672         selected_font = 0;
5673         if (draw_shades && !draw_outline) {
5674                 text_start_x++;
5675                 text_start_y++;
5676                 set_foreground_color(default_bg_color);
5677                 draw_mode = BG;
5678                 draw_text();
5679                 text_start_x--;
5680                 text_start_y--;
5681         }
5682
5683         if (draw_outline) {
5684                 selected_font = 0;
5685                 int i, j;
5686                 for (i = -1; i < 2; i++)
5687                         for (j = -1; j < 2; j++) {
5688                                 if (i == 0 && j == 0)
5689                                         continue;
5690                                 text_start_x += i;
5691                                 text_start_y += j;
5692                                 set_foreground_color(default_out_color);
5693                                 draw_mode = OUTLINE;
5694                                 draw_text();
5695                                 text_start_x -= i;
5696                                 text_start_y -= j;
5697                         }
5698         }
5699
5700         set_foreground_color(default_fg_color);
5701         draw_mode = FG;
5702 #endif /* X11 */
5703         draw_text();
5704 #ifdef X11
5705 #ifdef XDBE
5706         if (use_xdbe) {
5707                 XdbeSwapInfo swap;
5708                 swap.swap_window = window.window;
5709                 swap.swap_action = XdbeBackground;
5710                 XdbeSwapBuffers(display, &swap, 1);
5711         }
5712 #endif
5713 #endif /* X11 */
5714 }
5715 #ifdef X11
5716 static void clear_text(int exposures)
5717 {
5718 #ifdef XDBE
5719         if (use_xdbe) {
5720                 return;         /* The swap action is XdbeBackground, which clears */
5721         } else
5722 #endif
5723         {
5724         /* there is some extra space for borders and outlines */
5725         XClearArea(display, window.drawable,
5726                    text_start_x - border_margin - 1,
5727                    text_start_y - border_margin - 1,
5728                    text_width + border_margin * 2 + 2,
5729                    text_height + border_margin * 2 + 2,
5730                    exposures ? True : 0);
5731         }
5732 }
5733 #endif /* X11 */
5734
5735 static int need_to_update;
5736
5737 /* update_text() generates new text and clears old text area */
5738 static void update_text()
5739 {
5740         generate_text();
5741 #ifdef X11
5742         clear_text(1);
5743 #endif /* X11 */
5744         need_to_update = 1;
5745 }
5746
5747 static void main_loop()
5748 {
5749 #ifdef SIGNAL_BLOCKING
5750         sigset_t  newmask, oldmask;
5751
5752         sigemptyset(&newmask);
5753         sigaddset(&newmask,SIGINT);
5754         sigaddset(&newmask,SIGTERM);
5755         sigaddset(&newmask,SIGUSR1);
5756 #endif
5757
5758 #ifdef X11
5759         Region region = XCreateRegion();
5760         int event_base, error_base;
5761 #ifdef HAVE_XDAMAGE
5762         if (!XDamageQueryExtension (display, &event_base, &error_base)) {
5763                 ERR("Xdamage extension unavailable");
5764         }
5765         Damage damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5766         XserverRegion region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5767         XserverRegion part = XFixesCreateRegionFromWindow(display, window.window, 0);
5768 #endif /* HAVE_XDAMAGE */
5769 #endif /* X11 */
5770
5771         info.looped = 0;
5772         while (total_run_times == 0 || info.looped < total_run_times) {
5773                 info.looped++;
5774
5775 #ifdef SIGNAL_BLOCKING
5776                 /* block signals.  we will inspect for pending signals later */
5777                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
5778                         CRIT_ERR("unable to sigprocmask()");
5779 #endif
5780
5781 #ifdef X11
5782                 XFlush(display);
5783
5784                 /* wait for X event or timeout */
5785
5786                 if (!XPending(display)) {
5787                         fd_set fdsr;
5788                         struct timeval tv;
5789                         int s;
5790                         double t =
5791                             update_interval - (get_time() -
5792                                                last_update_time);
5793
5794                         if (t < 0)
5795                                 t = 0;
5796
5797                         tv.tv_sec = (long) t;
5798                         tv.tv_usec = (long) (t * 1000000) % 1000000;
5799                         FD_ZERO(&fdsr);
5800                         FD_SET(ConnectionNumber(display), &fdsr);
5801
5802
5803                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
5804                                    0, &tv);
5805 #else
5806                         usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
5807 #endif /* X11 */
5808 #ifdef X11
5809                         if (s == -1) {
5810                                 if (errno != EINTR)
5811                                         ERR("can't select(): %s",
5812                                             strerror(errno));
5813                         } else {
5814                                 /* timeout */
5815                                 if (s == 0)
5816 #endif /* X11 */
5817                                         update_text();
5818 #ifdef X11
5819                         }
5820                 }
5821                 
5822                 if (need_to_update) {
5823 #ifdef OWN_WINDOW
5824                         int wx = window.x, wy = window.y;
5825 #endif
5826
5827                         need_to_update = 0;
5828                         selected_font = 0;
5829                         update_text_area();
5830 #ifdef OWN_WINDOW
5831                         if (own_window) {
5832                                 /* resize window if it isn't right size */
5833                                 if (!fixed_size &&
5834                                     (text_width + border_margin * 2 + 1 !=
5835                                      window.width
5836                                      || text_height + border_margin * 2 + 1 !=
5837                                      window.height)) {
5838                                         window.width =
5839                                             text_width +
5840                                             border_margin * 2 + 1;
5841                                         window.height =
5842                                             text_height +
5843                                             border_margin * 2 + 1;
5844                                         XResizeWindow(display,
5845                                                       window.drawable,
5846                                                       window.width,
5847                                                       window.height);
5848                         if (own_window) {
5849                                 set_transparent_background(window.drawable);
5850                         }
5851                                      }
5852
5853                                 /* move window if it isn't in right position */
5854                                 if (!fixed_pos
5855                                     && (window.x != wx
5856                                         || window.y != wy)) {
5857                                         XMoveWindow(display, window.window,
5858                                                     window.x, window.y);
5859                                 }
5860                         }
5861 #endif
5862
5863                         clear_text(1);
5864
5865 #ifdef XDBE
5866                         if (use_xdbe) {
5867                                 XRectangle r;
5868                                 r.x = text_start_x - border_margin;
5869                                 r.y = text_start_y - border_margin;
5870                                 r.width = text_width + border_margin * 2;
5871                                 r.height = text_height + border_margin * 2;
5872                                 XUnionRectWithRegion(&r, region, region);
5873                         }
5874 #endif
5875                 }
5876
5877                 /* handle X events */
5878
5879                 while (XPending(display)) {
5880                         XEvent ev;
5881                         XNextEvent(display, &ev);
5882                         switch (ev.type) {
5883                         case Expose:
5884                                 {
5885                                         XRectangle r;
5886                                         r.x = ev.xexpose.x;
5887                                         r.y = ev.xexpose.y;
5888                                         r.width = ev.xexpose.width;
5889                                         r.height = ev.xexpose.height;
5890                                         XUnionRectWithRegion(&r, region,
5891                                                              region);
5892                                 }
5893                                 break;
5894
5895 #ifdef OWN_WINDOW
5896                         case ReparentNotify:
5897                                 /* set background to ParentRelative for all parents */
5898                                 if (own_window) {
5899                                         set_transparent_background(window.
5900                                         window);
5901                                 }
5902                                 break;
5903
5904                         case ConfigureNotify:
5905                                 if (own_window) {
5906                                         /* if window size isn't what expected, set fixed size */
5907                                         if (ev.xconfigure.width !=
5908                                             window.width
5909                                             || ev.xconfigure.height !=
5910                                             window.height) {
5911                                                 if (window.width != 0
5912                                                     && window.height != 0)
5913                                                         fixed_size = 1;
5914
5915                                                 /* clear old stuff before screwing up size and pos */
5916                                                 clear_text(1);
5917
5918                                                 {
5919                                                         XWindowAttributes
5920                                                             attrs;
5921                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
5922                                                                 window.
5923                                                                     width =
5924                                                                     attrs.
5925                                                                     width;
5926                                                                 window.
5927                                                                     height
5928                                                                     =
5929                                                                     attrs.
5930                                                                     height;
5931                                                         }
5932                                                 }
5933
5934                                                 text_width =
5935                                                     window.width -
5936                                                     border_margin * 2 - 1;
5937                                                 text_height =
5938                                                     window.height -
5939                                                     border_margin * 2 - 1;
5940                                                 if (text_width > maximum_width && maximum_width > 0)
5941                                                         text_width = maximum_width;
5942                                         }
5943
5944                                         /* if position isn't what expected, set fixed pos, total_updates
5945                                          * avoids setting fixed_pos when window is set to weird locations
5946                                          * when started */
5947                                         /*if (total_updates >= 2 this is broken
5948                                             && !fixed_pos
5949                                             && (window.x != ev.xconfigure.x
5950                                                 || window.y !=
5951                                                 ev.xconfigure.y)
5952                                             && (ev.xconfigure.x != 0
5953                                                 || ev.xconfigure.y != 0)) {
5954                                                 fixed_pos = 1;
5955                                 }*/
5956                                         set_font();
5957                                 }
5958                                 break;
5959
5960                         case ButtonPress:
5961                                 if (own_window)
5962                                 {
5963                                     /* forward the click to the desktop window */
5964                                     XUngrabPointer(display, ev.xbutton.time);
5965                                     ev.xbutton.window = window.desktop;
5966                                     XSendEvent(display, ev.xbutton.window, False, ButtonPressMask, &ev);
5967                                 }
5968                                 break;
5969
5970                         case ButtonRelease:
5971                                 if (own_window)
5972                                 {
5973                                     /* forward the release to the desktop window */
5974                                     ev.xbutton.window = window.desktop;
5975                                     XSendEvent(display, ev.xbutton.window, False, ButtonReleaseMask, &ev);
5976                                 }
5977                                 break;
5978
5979 #endif
5980
5981                         default:
5982 #ifdef HAVE_XDAMAGE
5983                                 if (ev.type == event_base + XDamageNotify) {
5984                                         XDamageNotifyEvent  *dev = (XDamageNotifyEvent *) &ev;
5985                                         XFixesSetRegion(display, part, &dev->area, 1);
5986                                         XFixesUnionRegion(display, region2, region2, part);
5987                                 }
5988 #endif /* HAVE_XDAMAGE */
5989                                 break;
5990                         }
5991         }
5992 #ifdef HAVE_XDAMAGE
5993                 XDamageSubtract(display, damage, region2, None);
5994                 XFixesSetRegion(display, region2, 0, 0);
5995 #endif /* HAVE_XDAMAGE */
5996
5997                 /* XDBE doesn't seem to provide a way to clear the back buffer without
5998                  * interfering with the front buffer, other than passing XdbeBackground
5999                  * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6000                  * redraw the text even if it wasn't part of the exposed area. OTOH,
6001                  * if we're not going to call draw_stuff at all, then no swap happens
6002                  * and we can safely do nothing.
6003                  */
6004
6005                 if (!XEmptyRegion(region)) {
6006 #ifdef XDBE
6007                         if (use_xdbe) {
6008                                 XRectangle r;
6009                                 r.x = text_start_x - border_margin;
6010                                 r.y = text_start_y - border_margin;
6011                                 r.width = text_width + border_margin * 2;
6012                                 r.height = text_height + border_margin * 2;
6013                                 XUnionRectWithRegion(&r, region, region);
6014                 }
6015 #endif
6016                         XSetRegion(display, window.gc, region);
6017 #ifdef XFT
6018                         if (use_xft) {
6019                                 XftDrawSetClip(window.xftdraw, region);
6020                         }
6021 #endif
6022 #endif /* X11 */
6023                         draw_stuff();
6024 #ifdef X11
6025                         XDestroyRegion(region);
6026                         region = XCreateRegion();
6027                 }
6028 #endif /* X11 */
6029
6030 #ifdef SIGNAL_BLOCKING
6031                 /* unblock signals of interest and let handler fly */
6032                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
6033                         CRIT_ERR("unable to sigprocmask()");
6034 #endif
6035
6036                 switch(g_signal_pending) {
6037                 case SIGUSR1:
6038                         {
6039                         ERR("received SIGUSR1. reloading the config file.");
6040                         reload_config();
6041                         break;
6042                         }
6043                 case SIGINT:
6044                 case SIGTERM:
6045                         {
6046                         ERR("received SIGINT or SIGTERM to terminate. bye!");
6047                         clean_up();
6048 #ifdef X11
6049                         XDestroyRegion(region);
6050                         region = NULL;
6051 #endif /* X11 */
6052                         return;  /* return from main_loop */
6053                         /*break;*/
6054                         }
6055                 default:
6056                         {
6057                         /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
6058                          * but didn't write any code to deal with it.   if you don't want to
6059                          * handle a signal, don't set a handler on it in the first place. */
6060                         if (g_signal_pending)
6061                                 ERR("ignoring signal (%d)", g_signal_pending);
6062                         break;
6063                         }
6064                 }
6065                 g_signal_pending=0;
6066         
6067         }
6068 #if defined(X11) && defined(HAVE_XDAMAGE)
6069         XDamageDestroy(display, damage);
6070         XFixesDestroyRegion(display, region2);
6071         XFixesDestroyRegion(display, part);
6072         XDestroyRegion(region);
6073         region = NULL;
6074 #endif /* X11 && HAVE_XDAMAGE */
6075 }
6076
6077 static void load_config_file(const char *);
6078
6079 /* reload the config file */
6080 void reload_config(void)
6081 {
6082         //lock_all_threads();
6083         threads_runnable++;
6084         if (info.cpu_usage) {
6085                 free(info.cpu_usage);
6086                 info.cpu_usage = NULL;
6087         }
6088 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
6089         if (info.xmms.thread) {
6090                 if (destroy_xmms_thread()!=0)
6091                 {
6092                         ERR("error destroying xmms_player thread");
6093                 }
6094         }
6095 #endif
6096 #ifdef TCP_PORT_MONITOR
6097         destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6098 #endif
6099
6100         if (current_config) {
6101                 clear_fs_stats();
6102                 load_config_file(current_config);
6103 #ifdef X11
6104                 load_fonts();
6105                 set_font();
6106                 XClearWindow(display, RootWindow(display, screen)); // clear the window first
6107
6108 #endif /* X11 */
6109 #ifdef TCP_PORT_MONITOR
6110                 info.p_tcp_port_monitor_collection = NULL; 
6111 #endif
6112 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
6113                 if ( (!info.xmms.thread) && (info.xmms.current_project > PROJECT_NONE) && 
6114                      (create_xmms_thread() !=0) )
6115                     {
6116                         CRIT_ERR("unable to create xmms_player thread!");
6117                     }
6118 #endif
6119                 extract_variable_text(text);
6120                 free(text);
6121                 text = NULL;
6122                 update_text();
6123         }
6124         thread_count = 0;
6125 }
6126
6127 void clean_up(void)
6128 {
6129         //lock_all_threads();
6130         threads_runnable++;
6131         if (info.cpu_usage) {
6132                 free(info.cpu_usage);
6133                 info.cpu_usage = NULL;
6134         }
6135 #ifdef X11
6136 #ifdef XDBE
6137         if (use_xdbe) {
6138                 XdbeDeallocateBackBufferName(display, window.back_buffer);
6139         }
6140 #endif
6141 #ifdef OWN_WINDOW
6142         if (own_window) 
6143         {
6144                 XDestroyWindow(display, window.window);
6145                 XClearWindow(display, RootWindow(display, screen));
6146                 XFlush(display);
6147         }
6148         else
6149 #endif
6150         {
6151                 XClearWindow(display, RootWindow(display, screen));
6152                 clear_text(1);
6153                 XFlush(display);
6154         }
6155
6156         XFreeGC(display, window.gc);
6157 #endif /* X11 */
6158
6159
6160         /* it is really pointless to free() memory at the end of program but ak|ra
6161          * wants me to do this */
6162
6163         free_text_objects(text_object_count, text_objects);
6164         text_object_count = 0;
6165         text_objects = NULL;
6166
6167 #ifdef MLDONKEY 
6168         ml_cleanup();
6169 #endif /* MLDONKEY */
6170
6171         if (text != original_text)
6172                 free(text);
6173
6174         free(current_config);
6175         free(current_mail_spool);
6176 #ifdef SETI
6177         free(seti_dir);
6178 #endif
6179 #ifdef TCP_PORT_MONITOR
6180         destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6181         info.p_tcp_port_monitor_collection = NULL;
6182 #endif
6183 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
6184         if ( info.xmms.thread && (destroy_xmms_thread()!=0) )
6185         {
6186             ERR("error destroying xmms_player thread");
6187         }
6188 #endif
6189 }
6190
6191 static int string_to_bool(const char *s)
6192 {
6193         if (!s)
6194                 return 1;
6195         if (strcasecmp(s, "yes") == 0)
6196                 return 1;
6197         if (strcasecmp(s, "true") == 0)
6198                 return 1;
6199         if (strcasecmp(s, "1") == 0)
6200                 return 1;
6201         return 0;
6202 }
6203 #ifdef X11
6204 static enum alignment string_to_alignment(const char *s)
6205 {
6206         if (strcasecmp(s, "top_left") == 0)
6207                 return TOP_LEFT;
6208         else if (strcasecmp(s, "top_right") == 0)
6209                 return TOP_RIGHT;
6210         else if (strcasecmp(s, "bottom_left") == 0)
6211                 return BOTTOM_LEFT;
6212         else if (strcasecmp(s, "bottom_right") == 0)
6213                 return BOTTOM_RIGHT;
6214         else if (strcasecmp(s, "tl") == 0)
6215                 return TOP_LEFT;
6216         else if (strcasecmp(s, "tr") == 0)
6217                 return TOP_RIGHT;
6218         else if (strcasecmp(s, "bl") == 0)
6219                 return BOTTOM_LEFT;
6220         else if (strcasecmp(s, "br") == 0)
6221                 return BOTTOM_RIGHT;
6222         else if (strcasecmp(s, "none") == 0)
6223                 return NONE;
6224         return TOP_LEFT;
6225 }
6226 #endif /* X11 */
6227
6228
6229 static void set_default_configurations(void)
6230 {
6231         fork_to_background = 0;
6232         total_run_times = 0;
6233         info.cpu_avg_samples = 2;
6234         info.net_avg_samples = 2;
6235         info.memmax = 0;
6236         top_cpu = 0;
6237         top_mem = 0;
6238 #ifdef MPD
6239         strcpy(info.mpd.host, "localhost");
6240         info.mpd.port = 6600;
6241         info.mpd.status = NULL;
6242         info.mpd.artist = NULL;
6243         info.mpd.album = NULL;
6244         info.mpd.title = NULL;
6245         info.mpd.random = NULL;
6246         info.mpd.track = NULL;
6247         info.mpd.name = NULL;
6248         info.mpd.file = NULL;
6249 #endif
6250 #ifdef XMMS2
6251     info.xmms2.artist = NULL;
6252     info.xmms2.album = NULL;
6253     info.xmms2.title = NULL;
6254     info.xmms2.genre = NULL;
6255     info.xmms2.comment = NULL;
6256     info.xmms2.decoder = NULL;
6257     info.xmms2.transport = NULL;
6258     info.xmms2.url = NULL;
6259     info.xmms2.status = NULL;
6260 #endif
6261         use_spacer = 0;
6262 #ifdef X11
6263         out_to_console = 0;
6264 #else
6265         out_to_console = 1;
6266 #endif
6267 #ifdef X11
6268         default_fg_color = WhitePixel(display, screen);
6269         default_bg_color = BlackPixel(display, screen);
6270         default_out_color = BlackPixel(display, screen);
6271         draw_shades = 1;
6272         draw_borders = 0;
6273         draw_graph_borders = 1;
6274         draw_outline = 0;
6275         set_first_font("6x10");
6276         gap_x = 5;
6277         gap_y = 5;
6278         minimum_width = 5;
6279         minimum_height = 5;
6280         maximum_width = 0;
6281 #ifdef OWN_WINDOW
6282         own_window = 0;
6283         window.type=TYPE_NORMAL;
6284         window.hints=0;
6285         strcpy(window.wm_class_name, "conky");  
6286 #endif
6287         stippled_borders = 0;
6288         border_margin = 3;
6289         border_width = 1;
6290         text_alignment = BOTTOM_LEFT;
6291         on_bottom = 1;
6292 #endif /* X11 */
6293
6294         free(current_mail_spool);
6295         {
6296                 char buf[256];
6297                 variable_substitute(MAIL_FILE, buf, 256);
6298                 if (buf[0] != '\0')
6299                         current_mail_spool = strdup(buf);
6300         }
6301
6302         no_buffers = 1;
6303         update_interval = 10.0;
6304         stuff_in_upper_case = 0;
6305 #ifdef MLDONKEY
6306         mlconfig.mldonkey_hostname = strdup("127.0.0.1");
6307         mlconfig.mldonkey_port = 4001;
6308         mlconfig.mldonkey_login = NULL;
6309         mlconfig.mldonkey_password = NULL;
6310 #endif
6311
6312 #ifdef TCP_PORT_MONITOR
6313         tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6314         tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6315 #endif
6316
6317 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
6318         info.xmms.current_project=PROJECT_NONE;
6319 #endif
6320 }
6321
6322 static void load_config_file(const char *f)
6323 {
6324 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6325         int line = 0;
6326         FILE *fp;
6327
6328         set_default_configurations();
6329         fp = fopen(f, "r");
6330         if (!fp)
6331                 return;
6332
6333         while (!feof(fp)) {
6334                 char buf[256], *p, *p2, *name, *value;
6335                 line++;
6336                 if (fgets(buf, 256, fp) == NULL)
6337                         break;
6338
6339                 p = buf;
6340
6341                 /* break at comment */
6342                 p2 = strchr(p, '#');
6343                 if (p2)
6344                         *p2 = '\0';
6345
6346                 /* skip spaces */
6347                 while (*p && isspace((int) *p))
6348                         p++;
6349                 if (*p == '\0')
6350                         continue;       /* empty line */
6351
6352                 name = p;
6353
6354                 /* skip name */
6355                 p2 = p;
6356                 while (*p2 && !isspace((int) *p2))
6357                         p2++;
6358                 if (*p2 != '\0') {
6359                         *p2 = '\0';     /* break at name's end */
6360                         p2++;
6361                 }
6362
6363                 /* get value */
6364                 if (*p2) {
6365                         p = p2;
6366                         while (*p && isspace((int) *p))
6367                                 p++;
6368
6369                         value = p;
6370
6371                         p2 = value + strlen(value);
6372                         while (isspace((int) *(p2 - 1)))
6373                                 *--p2 = '\0';
6374                 } else {
6375                         value = 0;
6376                 }
6377
6378 #define CONF2(a) if (strcasecmp(name, a) == 0)
6379 #define CONF(a) else CONF2(a)
6380 #define CONF3(a,b) \
6381 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
6382
6383
6384 #ifdef X11
6385                 CONF2("alignment") {
6386         if (value) {
6387                 int a = string_to_alignment(value);
6388                 if (a <= 0)
6389                         CONF_ERR;
6390                 else
6391                         text_alignment = a;
6392         } else
6393                 CONF_ERR;
6394                 }
6395                 CONF("on_bottom") {
6396                         if(value)
6397                                 ERR("on_bottom is deprecated.  use own_window_hints below");
6398                                 on_bottom = string_to_bool(value);
6399                                 if (on_bottom)
6400                                     SET_HINT(window.hints,HINT_BELOW);
6401
6402                         else
6403                                 CONF_ERR;
6404                 }
6405                 CONF("background") {
6406                         fork_to_background = string_to_bool(value);
6407                 }
6408
6409 #else
6410                 CONF2("background") {
6411         fork_to_background = string_to_bool(value);
6412                 }
6413 #endif /* X11 */
6414 #ifdef X11
6415                 CONF("border_margin") {
6416                         if (value)
6417                                 border_margin = strtol(value, 0, 0);
6418                         else
6419                                 CONF_ERR;
6420                 }
6421                 CONF("border_width") {
6422                         if (value)
6423                                 border_width = strtol(value, 0, 0);
6424                         else
6425                                 CONF_ERR;
6426                 }
6427                 CONF("default_color") {
6428                         if (value)
6429                                 default_fg_color = get_x11_color(value);
6430                         else
6431                                 CONF_ERR;
6432                 }
6433                 CONF3("default_shade_color", "default_shadecolor") {
6434                         if (value)
6435                                 default_bg_color = get_x11_color(value);
6436                         else
6437                                 CONF_ERR;
6438                 }
6439                 CONF3("default_outline_color", "default_outlinecolor") {
6440                         if (value)
6441                                 default_out_color = get_x11_color(value);
6442                         else
6443                                 CONF_ERR;
6444                 }
6445 #endif /* X11 */
6446 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
6447                 CONF("xmms_player") {
6448                         if (value) {
6449                                 if (strncmp(value,"none",4)==0)
6450                                     info.xmms.current_project=PROJECT_NONE;
6451                                 else if (strncmp(value,"xmms",4)==0)
6452                                     info.xmms.current_project=PROJECT_XMMS;
6453                                 else if (strncmp(value,"bmp",3)==0)
6454                                     info.xmms.current_project=PROJECT_BMP;
6455                                 else if (strncmp(value,"audacious",9)==0)
6456                                     info.xmms.current_project=PROJECT_AUDACIOUS;
6457                                 else if  (strncmp(value,"infopipe",8)==0)
6458                                     info.xmms.current_project=PROJECT_INFOPIPE;
6459                                 else
6460                                         CONF_ERR;
6461                         }
6462                         else
6463                                 CONF_ERR;
6464                 }
6465 #endif
6466                 CONF("imap") {
6467                         if (value) {
6468                                 info.mail = parse_mail_args(IMAP, value);
6469                         } else {
6470                                 CONF_ERR;
6471                         }
6472                 }
6473                 CONF("pop3") {
6474                         if (value) {
6475                                 info.mail = parse_mail_args(POP3, value);
6476                         } else {
6477                                 CONF_ERR;
6478                         }
6479                 }
6480 #ifdef MPD
6481                 CONF("mpd_host") {
6482                         if (value)
6483                                 strncpy(info.mpd.host, value, 127);
6484                         else
6485                                 CONF_ERR;
6486                 }
6487                 CONF("mpd_port") {
6488                         if (value) {
6489                                 info.mpd.port = strtol(value, 0, 0);
6490                                 if (info.mpd.port < 1
6491                                     || info.mpd.port > 0xffff)
6492                                         CONF_ERR;
6493                         }
6494                 }
6495                 CONF("mpd_password") {
6496                         if (value)
6497                                 strncpy(info.mpd.password, value, 127);
6498                         else
6499                                 CONF_ERR;
6500                 }
6501 #endif
6502                 CONF("cpu_avg_samples") {
6503                         if (value) {
6504                                 cpu_avg_samples = strtol(value, 0, 0);
6505                                 if (cpu_avg_samples < 1
6506                                     || cpu_avg_samples > 14)
6507                                         CONF_ERR;
6508                                 else
6509                                         info.
6510                                             cpu_avg_samples
6511                                             = cpu_avg_samples;
6512                         } else
6513                                 CONF_ERR;
6514                 }
6515                 CONF("net_avg_samples") {
6516                         if (value) {
6517                                 net_avg_samples = strtol(value, 0, 0);
6518                                 if (net_avg_samples < 1
6519                                     || net_avg_samples > 14)
6520                                         CONF_ERR;
6521                                 else
6522                                         info.
6523                                             net_avg_samples
6524                                             = net_avg_samples;
6525                         } else
6526                                 CONF_ERR;
6527                 }
6528
6529
6530
6531
6532
6533
6534 #ifdef XDBE
6535                 CONF("double_buffer") {
6536                 use_xdbe = string_to_bool(value);
6537                 }
6538 #endif
6539 #ifdef X11
6540                 CONF("override_utf8_locale") {
6541         utf8_mode = string_to_bool(value);
6542                 }
6543
6544                 CONF("draw_borders") {
6545                         draw_borders = string_to_bool(value);
6546                 }
6547                 CONF("draw_graph_borders") {
6548                         draw_graph_borders = string_to_bool(value);
6549                 }
6550                 CONF("draw_shades") {
6551                         draw_shades = string_to_bool(value);
6552                 }
6553                 CONF("draw_outline") {
6554                         draw_outline = string_to_bool(value);
6555                 }
6556 #endif /* X11 */
6557                 CONF("out_to_console") {
6558                         out_to_console = string_to_bool(value);
6559                 }
6560                 CONF("use_spacer") {
6561                         use_spacer = string_to_bool(value);
6562                 }
6563 #ifdef X11
6564 #ifdef XFT
6565                 CONF("use_xft") {
6566                         use_xft = string_to_bool(value);
6567                 }
6568                 CONF("font") {
6569                         if (value) {
6570                                 set_first_font(value);
6571                         } else
6572                                 CONF_ERR;
6573                 }
6574                 CONF("xftalpha") {
6575                         if (value && font_count >= 0)
6576                                 fonts[0].font_alpha = atof(value)
6577                                     * 65535.0;
6578                         else
6579                                 CONF_ERR;
6580                 }
6581                 CONF("xftfont") {
6582                         if (use_xft) {
6583 #else
6584                 CONF("use_xft") {
6585                         if (string_to_bool(value))
6586                                 ERR("Xft not enabled");
6587                 }
6588                 CONF("xftfont") {
6589                         /* xftfont silently ignored when no Xft */
6590                 }
6591                 CONF("xftalpha") {
6592                         /* xftalpha is silently ignored when no Xft */
6593                 }
6594                 CONF("font") {
6595 #endif
6596                                 if (value) {
6597                                         set_first_font(value);
6598                                 } else
6599                                         CONF_ERR;
6600 #ifdef XFT
6601                         }
6602 #endif
6603                 }
6604                 CONF("gap_x") {
6605                         if (value)
6606                                 gap_x = atoi(value);
6607                         else
6608                                 CONF_ERR;
6609                 }
6610                 CONF("gap_y") {
6611                         if (value)
6612                                 gap_y = atoi(value);
6613                         else
6614                                 CONF_ERR;
6615                 }
6616 #endif /* X11 */
6617                 CONF("mail_spool") {
6618                         if (value) {
6619                                 char buf[256];
6620                                 variable_substitute(value, buf, 256);
6621
6622                                 if (buf[0]
6623                                     != '\0') {
6624                                         if (current_mail_spool)
6625                                                 free(current_mail_spool);
6626                                         current_mail_spool = strdup(buf);
6627                                 }
6628                         } else
6629                                 CONF_ERR;
6630                 }
6631 #ifdef X11
6632                 CONF("minimum_size") {
6633         if (value) {
6634                 if (sscanf
6635                                   (value, "%d %d", &minimum_width,
6636                                    &minimum_height) != 2)
6637                         if (sscanf
6638                                                  (value, "%d",
6639                                                    &minimum_width) != 1)
6640                                 CONF_ERR;
6641         } else
6642                 CONF_ERR;
6643                 }
6644                 CONF("maximum_width") {
6645                         if (value) {
6646                                 if (sscanf(value, "%d", &maximum_width) != 1)
6647                                         CONF_ERR;
6648                         } else
6649                                 CONF_ERR;
6650                 }
6651 #endif /* X11 */
6652                 CONF("no_buffers") {
6653                         no_buffers = string_to_bool(value);
6654                 }
6655 #ifdef MLDONKEY
6656                 CONF("mldonkey_hostname") {
6657                         if (value) {
6658                                 if (mlconfig.mldonkey_hostname != NULL) {
6659                                         free(mlconfig.mldonkey_hostname);
6660                                 }
6661                         mlconfig.mldonkey_hostname = strdup(value);
6662                         }
6663                         else
6664                                 CONF_ERR;
6665                 }
6666                 CONF("mldonkey_port") {
6667                         if (value)
6668                                 mlconfig.mldonkey_port = atoi(value);
6669                         else
6670                                 CONF_ERR;
6671                 }
6672                 CONF("mldonkey_login") {
6673                         if (value) {
6674                                 if (mlconfig.mldonkey_login != NULL) {
6675                                         free(mlconfig.mldonkey_login);
6676                                 }
6677                                 mlconfig.mldonkey_login = strdup(value);
6678                         }
6679                         else
6680                                 CONF_ERR;
6681                 }
6682                 CONF("mldonkey_password") {
6683                         if (value) {
6684                                 if (mlconfig.mldonkey_password != NULL) {
6685                                         free(mlconfig.mldonkey_password);
6686                                 }
6687                                 mlconfig.mldonkey_password = strdup(value);
6688                         }
6689                         else
6690                                 CONF_ERR;
6691                 }
6692 #endif
6693                 CONF("pad_percents") {
6694         pad_percents = atoi(value);
6695                 }
6696 #ifdef X11
6697 #ifdef OWN_WINDOW
6698                 CONF("own_window") {
6699                         own_window = string_to_bool(value);
6700                 }
6701                 CONF("wm_class_name") {
6702                         memset(window.wm_class_name,0,sizeof(window.wm_class_name));
6703                         strncpy(window.wm_class_name, value, sizeof(window.wm_class_name)-1);
6704                 }
6705                 CONF("own_window_transparent") {
6706                         set_transparent = string_to_bool(value);
6707                 }
6708                 CONF("own_window_colour") {
6709                         if (value) {
6710                                 background_colour = get_x11_color(value);
6711                         } else {
6712                                 ERR("Invalid colour for own_winder_colour (try omitting the '#' for hex colours");
6713                         }
6714                 }
6715                 CONF("own_window_hints") {
6716                         if (value) {
6717                                 char *p_hint, *p_save;
6718                                 char delim[] = ", ";
6719
6720                                 /* tokenize the value into individual hints */
6721                                 if ((p_hint=strtok_r(value, delim, &p_save)) != NULL)
6722                                 do {
6723                                          /*fprintf(stderr, "hint [%s] parsed\n", p_hint);*/
6724                                          if (strncmp(p_hint,"undecorate",10) == 0)
6725                                              SET_HINT(window.hints,HINT_UNDECORATED);
6726                                          else if (strncmp(p_hint,"below",5) == 0)
6727                                              SET_HINT(window.hints,HINT_BELOW);
6728                                          else if (strncmp(p_hint,"above",5) == 0)
6729                                              SET_HINT(window.hints,HINT_ABOVE);
6730                                          else if (strncmp(p_hint,"sticky",6) == 0)
6731                                              SET_HINT(window.hints,HINT_STICKY);
6732                                          else if (strncmp(p_hint,"skip_taskbar",12) == 0)
6733                                              SET_HINT(window.hints,HINT_SKIP_TASKBAR);
6734                                          else if (strncmp(p_hint,"skip_pager",10) == 0)
6735                                              SET_HINT(window.hints,HINT_SKIP_PAGER);
6736                                          else
6737                                              CONF_ERR;
6738
6739                                          p_hint=strtok_r(NULL, delim, &p_save);
6740                                 }
6741                                 while (p_hint!=NULL);
6742                         }
6743                 }
6744                 CONF("own_window_type") {
6745                         if (value) {
6746                                 if (strncmp(value,"normal",6)==0)
6747                                         window.type = TYPE_NORMAL;
6748                                 else if  (strncmp(value,"desktop",7)==0)
6749                                         window.type = TYPE_DESKTOP;
6750                                 else if (strncmp(value,"override",8)==0)
6751                                         window.type = TYPE_OVERRIDE;
6752                                 else
6753                                         CONF_ERR;
6754                         }
6755                 }
6756 #endif
6757                 CONF("stippled_borders") {
6758                         if (value)
6759                                 stippled_borders = strtol(value, 0, 0);
6760                         else
6761                                 stippled_borders = 4;
6762                 }
6763 #endif /* X11 */
6764                 CONF("temp1") {
6765                         ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
6766                 }
6767                 CONF("temp1") {
6768                         ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
6769                 }
6770                 CONF("update_interval") {
6771                         if (value)
6772                                 update_interval = strtod(value, 0);
6773                         else
6774                                 CONF_ERR;
6775                 }
6776                 CONF("total_run_times") {
6777                         if (value)
6778                                 total_run_times = strtod(value, 0);
6779                         else
6780                                 CONF_ERR;
6781                 }
6782                 CONF("uppercase") {
6783                         stuff_in_upper_case = string_to_bool(value);
6784                 }
6785 #ifdef SETI
6786                 CONF("seti_dir") {
6787                         seti_dir = (char *)
6788                             malloc(strlen(value)
6789                                    + 1);
6790                         strcpy(seti_dir, value);
6791                 }
6792 #endif
6793                 CONF("text") {
6794                         if (text != original_text)
6795                                 free(text);
6796
6797                         text = (char *)
6798                             malloc(1);
6799                         text[0]
6800                             = '\0';
6801
6802                         while (!feof(fp)) {
6803                                 unsigned
6804                                 int l = strlen(text);
6805                                 if (fgets(buf, 256, fp) == NULL)
6806                                         break;
6807                                 text = (char *)
6808                                     realloc(text, l + strlen(buf)
6809                                             + 1);
6810                                 strcat(text, buf);
6811
6812                                 if (strlen(text) > 1024 * 8)
6813                                         break;
6814                         }
6815                         fclose(fp);
6816                         text_lines = line + 1;
6817                         return;
6818                 }
6819 #ifdef TCP_PORT_MONITOR
6820                 CONF("min_port_monitors") 
6821                 {
6822                         if ( !value || 
6823                              (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) || 
6824                              tcp_port_monitor_collection_args.min_port_monitors < 0 )
6825                         {
6826                                 /* an error. use default, warn and continue. */
6827                                 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6828                                 CONF_ERR;
6829                         }
6830                         else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
6831                         {
6832                                 /* no error, just use default */
6833                                 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6834                         }
6835                         /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
6836                 }
6837                 CONF("min_port_monitor_connections") 
6838                 {
6839                         if ( !value || 
6840                              (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1) 
6841                              || tcp_port_monitor_args.min_port_monitor_connections < 0 )
6842                         {
6843                                 /* an error. use default, warni and continue. */
6844                                 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6845                                 CONF_ERR;
6846                         }
6847                         else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
6848                         {
6849                                 /* no error, just use default */
6850                                 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6851                         }
6852                         /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
6853                 }
6854 #endif
6855                 else
6856                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
6857
6858 #undef CONF
6859 #undef CONF2
6860         }
6861
6862         fclose(fp);
6863 #undef CONF_ERR
6864
6865
6866 }
6867
6868                                                                                                                                                                                         /* : means that character before that takes an argument */
6869 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
6870 #ifdef X11
6871                 "x:y:w:a:f:"
6872 #ifdef OWN_WINDOW
6873     "o"
6874 #endif
6875 #ifdef XDBE
6876     "b"
6877 #endif
6878 #endif /* X11 */
6879     ;
6880
6881
6882 int main(int argc, char **argv)
6883 {
6884         struct sigaction act, oact;
6885
6886         g_signal_pending=0;
6887         memset(&info, 0, sizeof(info) );
6888
6889 #ifdef TCP_PORT_MONITOR
6890         tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6891         tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6892 #endif
6893
6894 #if defined(XMMS)
6895         SET_XMMS_PROJECT_AVAILABLE(info.xmms.project_mask, PROJECT_XMMS);
6896 #endif
6897 #if defined(BMP)
6898         SET_XMMS_PROJECT_AVAILABLE(info.xmms.project_mask, PROJECT_BMP);
6899 #endif
6900 #if defined(AUDACIOUS)
6901         SET_XMMS_PROJECT_AVAILABLE(info.xmms.project_mask, PROJECT_AUDACIOUS);
6902 #endif
6903 #if defined(INFOPIPE)
6904         SET_XMMS_PROJECT_AVAILABLE(info.xmms.project_mask, PROJECT_INFOPIPE);
6905 #endif
6906                 
6907         /* handle command line parameters that don't change configs */
6908 #ifdef X11
6909         char *s, *temp;
6910         unsigned int x;
6911
6912         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && 
6913                      *s) || ((s = getenv("LANG")) && *s)) {
6914                 temp = (char *)malloc((strlen(s) + 1) * sizeof(char));
6915                 if (temp == NULL) {
6916                         ERR("malloc failed");
6917                 }
6918                 for (x = 0; x < strlen(s); x++) {
6919                         temp[x] = tolower(s[x]);
6920                 }
6921                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6922                         utf8_mode = 1;
6923                 }
6924                 
6925                 free(temp);
6926         }
6927         if (!setlocale(LC_CTYPE, "")) {
6928                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6929         }
6930 #endif /* X11 */
6931         while (1) {
6932                 int c = getopt(argc,
6933                                argv,
6934                                getopt_string);
6935                 if (c == -1)
6936                         break;
6937
6938                 switch (c) {
6939                 case 'v':
6940                 case 'V':
6941                         print_version();
6942                 case 'c':
6943                         /* if current_config is set to a strdup of CONFIG_FILE, free it (even
6944                          * though free() does the NULL check itself;), then load optarg value */
6945                         if (current_config)
6946                                 free(current_config);
6947                         current_config = strdup(optarg);
6948                         break;
6949
6950                 case 'h':
6951                         printf
6952                                         ("Usage: %s [OPTION]...\n"
6953                                         "Conky is a system monitor that renders text on desktop or to own transparent\n"
6954                                         "window. Command line options will override configurations defined in config\n"
6955                                         "file.\n"
6956                                         "   -V            version\n"
6957                                         "   -c FILE       config file to load instead of "
6958                                         CONFIG_FILE
6959                                         "\n"
6960                                         "   -d            daemonize, fork to background\n"
6961                                         "   -h            help\n"
6962 #ifdef X11
6963                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
6964                                         "   -f FONT       font to use\n"
6965 #ifdef OWN_WINDOW
6966                                         "   -o            create own window to draw\n"
6967 #endif
6968 #ifdef XDBE
6969                                         "   -b            double buffer (prevents flickering)\n"
6970 #endif
6971                                         "   -w WIN_ID     window id to draw\n"
6972                                         "   -x X          x position\n"
6973                                         "   -y Y          y position\n"
6974 #endif /* X11 */
6975                                         "   -t TEXT       text to render, remember single quotes, like -t '$uptime'\n"
6976                                         "   -u SECS       update interval\n"
6977                                         "   -i NUM        number of times to update Conky\n", argv[0]);
6978                         return 0;
6979 #ifdef X11
6980                 case 'w':
6981                         window.window = strtol(optarg, 0, 0);
6982                         break;
6983 #endif /* X11 */
6984
6985                 case '?':
6986                         exit(EXIT_FAILURE);
6987                 }
6988         }
6989 #ifdef X11
6990         /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
6991         init_X11();
6992 #endif /* X11 */
6993
6994         /* load current_config or CONFIG_FILE */
6995
6996 #ifdef CONFIG_FILE
6997         if (current_config == NULL) {
6998                 /* load default config file */
6999                 char buf[256];
7000
7001                 variable_substitute(CONFIG_FILE, buf, 256);
7002
7003                 if (buf[0] != '\0')
7004                         current_config = strdup(buf);
7005         }
7006 #endif
7007
7008         if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
7009                 load_config_file(current_config);
7010         else { 
7011                 set_default_configurations();
7012         }
7013
7014 #ifdef MAIL_FILE
7015         if (current_mail_spool == NULL) {
7016                 char buf[256];
7017                 variable_substitute(MAIL_FILE, buf, 256);
7018
7019                 if (buf[0] != '\0')
7020                         current_mail_spool = strdup(buf);
7021         }
7022 #endif
7023
7024         /* handle other command line arguments */
7025
7026 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
7027         optind = optreset = 1;
7028 #else
7029         optind = 0;
7030 #endif
7031
7032 #if defined(__FreeBSD__)
7033         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
7034                         O_RDONLY, "kvm_open")) == NULL)
7035                 CRIT_ERR( "cannot read kvm");
7036 #endif
7037         
7038         while (1) {
7039                 int c = getopt(argc,
7040                                argv,
7041                                getopt_string);
7042                 if (c == -1)
7043                         break;
7044
7045                 switch (c) {
7046                 case 'd':
7047                         fork_to_background = 1;
7048                         break;
7049
7050 #ifdef X11
7051                         case 'f':
7052                         set_first_font(optarg);
7053                         break;
7054                         case 'a':
7055                                 text_alignment = string_to_alignment(optarg);
7056                                 break;
7057
7058 #ifdef OWN_WINDOW
7059                 case 'o':
7060                         own_window = 1;
7061                         break;
7062 #endif
7063 #ifdef XDBE
7064                 case 'b':
7065                                 use_xdbe = 1;
7066                         break;
7067 #endif
7068 #endif /* X11 */
7069                 case 't':
7070                         if (text != original_text)
7071                                 free(text);
7072                         text = strdup(optarg);
7073                         convert_escapes(text);
7074                         break;
7075
7076                 case 'u':
7077                         update_interval = strtod(optarg, 0);
7078                         break;
7079
7080                 case 'i':
7081                         total_run_times = strtod(optarg, 0);
7082                         break;
7083 #ifdef X11
7084                 case 'x':
7085                         gap_x = atoi(optarg);
7086                         break;
7087
7088                 case 'y':
7089                         gap_y = atoi(optarg);
7090                         break;
7091 #endif /* X11 */
7092
7093                 case '?':
7094                         exit(EXIT_FAILURE);
7095                 }
7096         }
7097
7098 #ifdef X11
7099         /* load font */
7100         load_fonts();
7101 #endif /* X11 */
7102
7103         /* generate text and get initial size */
7104         extract_variable_text(text);
7105         if (text != original_text) {
7106                 free(text);
7107         }
7108         text = NULL;
7109         /* fork */
7110         if (fork_to_background) {
7111                 int pid = fork();
7112                 switch (pid) {
7113                 case -1:
7114                         ERR("can't fork() to background: %s",
7115                             strerror(errno));
7116                         break;
7117
7118                 case 0:
7119                         /* child process */
7120                         sleep(1);
7121                         fprintf(stderr,"\n");fflush(stderr);
7122                         break;
7123
7124                 default:
7125                         /* parent process */
7126                         fprintf(stderr,"Conky: forked to background, pid is %d\n",pid);
7127                         fflush(stderr);
7128                         return 0;
7129                 }
7130         }
7131
7132         update_uname();
7133
7134         generate_text();
7135 #ifdef X11
7136         selected_font = 0;
7137         update_text_area();     /* to get initial size of the window */
7138
7139         init_window
7140                 (own_window,
7141                  text_width + border_margin * 2 + 1,
7142                  text_height + border_margin * 2 + 1,
7143                  set_transparent, background_colour, info.uname_s.nodename, argv, argc);
7144         
7145         selected_font = 0;
7146         update_text_area();     /* to position text/window on screen */
7147 #endif /* X11 */
7148
7149 /*#ifdef CAIRO
7150 // why the fuck not?
7151 //do_it();
7152 #endif*/
7153
7154 #ifdef X11
7155 #ifdef OWN_WINDOW
7156         if (own_window && !fixed_pos) {
7157                 XMoveWindow(display, window.window, window.x, window.y);
7158         }
7159         if (own_window) {
7160                 set_transparent_background(window.window);
7161         }
7162 #endif
7163
7164         create_gc();
7165
7166         set_font();
7167         draw_stuff();
7168 #endif /* X11 */
7169
7170
7171         /* Set signal handlers */
7172         act.sa_handler = signal_handler;
7173         sigemptyset(&act.sa_mask);
7174         act.sa_flags = 0;
7175 #ifdef SA_RESTART
7176         act.sa_flags |= SA_RESTART;
7177 #endif
7178
7179         if ( sigaction(SIGINT,&act,&oact) < 0 ||
7180              sigaction(SIGUSR1,&act,&oact) < 0 ||
7181              sigaction(SIGTERM,&act,&oact) < 0 )
7182         {
7183                 ERR("error setting signal handler: %s", strerror(errno) );
7184         }
7185
7186 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
7187         if ( (info.xmms.current_project > PROJECT_NONE) && (create_xmms_thread() !=0) )
7188         {
7189             CRIT_ERR("unable to create xmms_player thread!");
7190         }
7191 #endif
7192
7193         main_loop();
7194
7195 #if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
7196         if ( info.xmms.thread && (destroy_xmms_thread()!=0) )
7197         {
7198             ERR("error destroying xmms_player thread");
7199         }
7200 #endif  
7201
7202 #if defined(__FreeBSD__)
7203         kvm_close(kd);
7204 #endif
7205         
7206         return 0;
7207 }
7208
7209 void signal_handler(int sig)
7210 {
7211         /* signal handler is light as a feather, as it should be. 
7212          * we will poll g_signal_pending with each loop of conky
7213          * and do any signal processing there, NOT here. */
7214         g_signal_pending=sig;
7215 }