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