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