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