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