cfe087cb11068a6fcc50aa8f3c89304645438333
[monky] / conky.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * This program is licensed under BSD license, read COPYING
4  */
5
6 #include "conky.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <time.h>
12 #include <locale.h>
13 #include <signal.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <limits.h>
19 #if HAVE_DIRENT_H
20 #include <dirent.h>
21 #endif
22 #include <sys/time.h>
23 #include <X11/Xutil.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26   
27 #define CONFIG_FILE "$HOME/.conkyrc"
28 #define MAIL_FILE "$MAIL"
29 #define MAX_IF_BLOCK_DEPTH 5
30
31 /* alignments */
32 enum alignment {
33         TOP_LEFT = 1,
34         TOP_RIGHT,
35         BOTTOM_LEFT,
36         BOTTOM_RIGHT,
37 };
38
39 /* default config file */
40 static char *current_config;
41
42 /* set to 1 if you want all text to be in uppercase */
43 static unsigned int stuff_in_upper_case;
44
45 /* Position on the screen */
46 static int text_alignment;
47 static int gap_x, gap_y;
48
49 /* Font used */
50 static char *font_name;
51
52 /* Update interval */
53 static double update_interval;
54
55 /* Run how many times? */
56 static unsigned long total_run_times;
57
58 /* fork? */
59 static int fork_to_background;
60
61 /* border */
62 static int draw_borders;
63 static int stippled_borders;
64
65 static int draw_shades, draw_outline;
66
67 static int border_margin, border_width;
68
69 static long default_fg_color, default_bg_color, default_out_color;
70
71 static int cpu_avg_samples, net_avg_samples;
72
73 /*#ifdef OWN_WINDOW*/
74 /* create own window or draw stuff to root? */
75 static int own_window = 0;
76
77 /* fixed size/pos is set if wm/user changes them */
78 static int fixed_size = 0, fixed_pos = 0;
79 /*#endif*/
80
81 static int minimum_width, minimum_height;
82
83 /* no buffers in used memory? */
84 int no_buffers;
85
86 /* pad percentages to decimals? */
87 static int pad_percents = 0;
88
89 /* Text that is shown */
90 static char original_text[] =
91     "$nodename - $sysname $kernel on $machine\n"
92     "$hr\n"
93     "${color grey}Uptime:$color $uptime\n"
94     "${color grey}Frequency (in MHz):$color $freq\n"
95     "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
96     "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
97     "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
98     "${color grey}Processes:$color $processes  ${color grey}Running:$color $running_processes\n"
99     "$hr\n"
100     "${color grey}File systems:\n"
101     " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
102     "${color grey}Networking:\n"
103     " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
104     "${color grey}Temperatures:\n"
105     " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
106     "$hr\n"
107 #ifdef SETI
108     "${color grey}SETI@Home Statistics:\n"
109     "${color grey}Seti Unit Number:$color $seti_credit\n"
110     "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
111 #endif
112 #ifdef MPD
113     "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
114     "Bitrate: $mpd_bitrate\n"
115     "Progress: $mpd_bar\n"
116 #endif
117     "${color grey}Name          PID     CPU%    MEM%\n"
118     " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
119     " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
120     " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
121     " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
122                 "${tail /var/log/Xorg.0.log 3}" ;
123
124 static char *text = original_text;
125
126 static int total_updates;
127
128 /* font stuff */
129
130 static XFontStruct *font;
131
132 #ifdef XFT
133 static XftFont *xftfont;
134 static int font_alpha = 65535;
135 #endif
136
137 /* if-blocks */
138  static int blockdepth = 0;
139  static int if_jumped = 0;
140  static int blockstart[MAX_IF_BLOCK_DEPTH];
141  
142  int check_mount (char* s)
143  {
144    int ret=0;
145    FILE* mtab = fopen("/etc/mtab","r");
146    if(mtab) {
147      char buf1[256],buf2[128];
148      while(fgets (buf1,256,mtab))
149      {
150        sscanf(buf1,"%*s %128s",buf2);
151        if(!strcmp(s,buf2)) { ret=1; break; }
152     }
153    fclose(mtab);
154   } else { ERR("Could not open mtab"); }
155    return ret;
156 }
157  
158
159
160 static inline int calc_text_width(const char *s, unsigned int l)
161 {
162 #ifdef XFT
163         if (use_xft) {
164                 XGlyphInfo gi;
165                 XftTextExtents8(display, xftfont, s, l, &gi);
166                 return gi.xOff;
167         } else
168 #endif
169         {
170                 return XTextWidth(font, s, l);
171         }
172 }
173
174 #ifdef XFT
175
176 #define font_height() use_xft ? (xftfont->ascent + xftfont->descent) : \
177     (font->max_bounds.ascent + font->max_bounds.descent)
178 #define font_ascent() use_xft ? xftfont->ascent : font->max_bounds.ascent
179 #define font_descent() use_xft ? xftfont->descent : font->max_bounds.descent
180
181 #else
182
183 #define font_height() (font->max_bounds.ascent + font->max_bounds.descent)
184 #define font_ascent() font->max_bounds.ascent
185 #define font_descent() font->max_bounds.descent
186
187 #endif
188
189 /* formatted text to render on screen, generated in generate_text(),
190  * drawn in draw_stuff() */
191
192 static char text_buffer[TEXT_BUFFER_SIZE*4];
193
194 /* special stuff in text_buffer */
195
196 #define SPECIAL_CHAR '\x01'
197
198 enum {
199         HORIZONTAL_LINE,
200         STIPPLED_HR,
201         BAR,
202         FG,
203         BG,
204         OUTLINE,
205         ALIGNR,
206         ALIGNC,
207 };
208
209 static struct special_t {
210         int type;
211         short height;
212         short width;
213         long arg;
214 } specials[128];
215
216 static int special_count;
217 static int special_index;       /* used when drawing */
218
219 static struct special_t *new_special(char *buf, int t)
220 {
221         if (special_count >= 128)
222                 CRIT_ERR("too much special things in text");
223
224         buf[0] = SPECIAL_CHAR;
225         buf[1] = '\0';
226         specials[special_count].type = t;
227         return &specials[special_count++];
228 }
229
230 typedef struct tailstring_list {
231         char data[TEXT_BUFFER_SIZE];
232         struct tailstring_list *next;
233 } tailstring;
234
235 void addtail(tailstring **head, char *data_in) {
236         tailstring *tmp;
237         if ((tmp = malloc(sizeof(*tmp))) == NULL) {
238                 CRIT_ERR("malloc");
239         }
240         strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
241         tmp->next = *head;
242         *head = tmp;
243 }
244
245 void freetail(tailstring *head) {
246           tailstring *tmp;
247
248           while (head != NULL) {
249                   tmp = head->next;
250                   free(head);
251                   head = tmp;
252           }
253   }
254
255
256 static void new_bar(char *buf, int w, int h, int usage)
257 {
258         struct special_t *s = new_special(buf, BAR);
259         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
260         s->width = w;
261         s->height = h;
262 }
263
264 static const char *scan_bar(const char *args, int *w, int *h)
265 {
266         *w = 0;                 /* zero width means all space that is available */
267         *h = 6;
268         /* bar's argument is either height or height,width */
269         if (args) {
270                 int n = 0;
271                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
272                         sscanf(args, "%d %n", h, &n);
273                 args += n;
274         }
275
276         return args;
277 }
278
279 static inline void new_hr(char *buf, int a)
280 {
281         new_special(buf, HORIZONTAL_LINE)->height = a;
282 }
283
284 static inline void new_stippled_hr(char *buf, int a, int b)
285 {
286         struct special_t *s = new_special(buf, STIPPLED_HR);
287         s->height = b;
288         s->arg = a;
289 }
290
291 static inline void new_fg(char *buf, long c)
292 {
293         new_special(buf, FG)->arg = c;
294 }
295
296 static inline void new_bg(char *buf, long c)
297 {
298         new_special(buf, BG)->arg = c;
299 }
300
301 static inline void new_outline(char *buf, long c)
302 {
303         new_special(buf, OUTLINE)->arg = c;
304 }
305
306 static inline void new_alignr(char *buf, long c) {
307         new_special(buf, ALIGNR)->arg = c;
308 }
309
310 static inline void new_alignc(char *buf, long c) {
311         new_special(buf, ALIGNC)->arg = c;
312 }
313
314 /* quite boring functions */
315
316 static inline void for_each_line(char *b, void (*f) (char *))
317 {
318         char *ps, *pe;
319
320         for (ps = b, pe = b; *pe; pe++) {
321                 if (*pe == '\n') {
322                         *pe = '\0';
323                         f(ps);
324                         *pe = '\n';
325                         ps = pe + 1;
326                 }
327         }
328
329         if (ps < pe)
330                 f(ps);
331 }
332
333 static void convert_escapes(char *buf)
334 {
335         char *p = buf, *s = buf;
336
337         while (*s) {
338                 if (*s == '\\') {
339                         s++;
340                         if (*s == 'n')
341                                 *p++ = '\n';
342                         else if (*s == '\\')
343                                 *p++ = '\\';
344                         s++;
345                 } else
346                         *p++ = *s++;
347         }
348         *p = '\0';
349 }
350
351 /* converts from bytes to human readable format (k, M, G) */
352 static void human_readable(long long a, char *buf, int size)
353 {
354         if (a >= 1024 * 1024 * 1024)
355                 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
356         else if (a >= 1024 * 1024) {
357                 double m = (a / 1024) / 1024.0;
358                 if (m >= 100.0)
359                         snprintf(buf, size, "%.0fM", m);
360                 else
361                         snprintf(buf, size, "%.1fM", m);
362         } else if (a >= 1024)
363                 snprintf(buf, size, "%Ldk", a / (long long) 1024);
364         else
365                 snprintf(buf, size, "%Ld", a);
366 }
367
368 /* text handling */
369
370 enum text_object_type {
371         OBJ_acpiacadapter,
372         OBJ_adt746xcpu,
373         OBJ_adt746xfan,
374         OBJ_acpifan,
375   OBJ_addr,
376   OBJ_linkstatus,
377         OBJ_acpitemp,
378         OBJ_battery,
379         OBJ_buffers,
380         OBJ_cached,
381         OBJ_color,
382         OBJ_cpu,
383         OBJ_cpubar,
384         OBJ_downspeed,
385         OBJ_downspeedf,
386         OBJ_else,
387         OBJ_endif,
388         OBJ_exec,
389         OBJ_execi,
390         OBJ_execbar,
391         OBJ_freq,
392         OBJ_fs_bar,
393         OBJ_fs_bar_free,
394         OBJ_fs_free,
395         OBJ_fs_free_perc,
396         OBJ_fs_size,
397         OBJ_fs_used,
398         OBJ_fs_used_perc,
399         OBJ_hr,
400         OBJ_alignr,
401         OBJ_alignc,
402         OBJ_i2c,
403         OBJ_if_existing,
404         OBJ_if_mounted,
405         OBJ_if_running,
406         OBJ_top,
407         OBJ_tail,
408         OBJ_kernel,
409         OBJ_loadavg,
410         OBJ_machine,
411         OBJ_mails,
412         OBJ_mem,
413         OBJ_membar,
414         OBJ_memmax,
415         OBJ_memperc,
416         OBJ_mixer,
417         OBJ_mixerl,
418         OBJ_mixerr,
419         OBJ_mixerbar,
420         OBJ_mixerlbar,
421         OBJ_mixerrbar,
422         OBJ_new_mails,
423         OBJ_nodename,
424         OBJ_pre_exec,
425 #ifdef MLDONKEY
426 OBJ_ml_upload_counter,
427           OBJ_ml_download_counter,
428            OBJ_ml_nshared_files,
429            OBJ_ml_shared_counter,
430            OBJ_ml_tcp_upload_rate,
431            OBJ_ml_tcp_download_rate,
432            OBJ_ml_udp_upload_rate,
433            OBJ_ml_udp_download_rate,
434            OBJ_ml_ndownloaded_files,
435            OBJ_ml_ndownloading_files,
436 #endif
437         OBJ_processes,
438         OBJ_running_processes,
439         OBJ_shadecolor,
440         OBJ_outlinecolor,
441         OBJ_stippled_hr,
442         OBJ_swap,
443         OBJ_swapbar,
444         OBJ_swapmax,
445         OBJ_swapperc,
446         OBJ_sysname,
447         OBJ_temp1,              /* i2c is used instead in these */
448         OBJ_temp2,
449         OBJ_text,
450         OBJ_time,
451         OBJ_utime,
452         OBJ_totaldown,
453         OBJ_totalup,
454         OBJ_updates,
455         OBJ_upspeed,
456         OBJ_upspeedf,
457         OBJ_uptime,
458         OBJ_uptime_short,
459 #ifdef SETI
460         OBJ_seti_prog,
461         OBJ_seti_progbar,
462         OBJ_seti_credit,
463 #endif
464 #ifdef MPD
465         OBJ_mpd_title,
466         OBJ_mpd_artist,
467         OBJ_mpd_album,
468         OBJ_mpd_vol,
469         OBJ_mpd_bitrate,
470         OBJ_mpd_status,
471         OBJ_mpd_host,
472         OBJ_mpd_port,
473         OBJ_mpd_bar,
474 #endif
475 #ifdef METAR
476   OBJ_metar_ob_time,
477   OBJ_metar_temp,
478   OBJ_metar_tempf,
479   OBJ_metar_windchill,
480   OBJ_metar_dew_point,
481   OBJ_metar_rh,
482   OBJ_metar_windspeed,
483   OBJ_metar_winddir,
484   OBJ_metar_swinddir,
485   OBJ_metar_cloud,
486   OBJ_metar_u2d_time,
487 #endif
488 };
489
490 struct text_object {
491         int type;
492         union {
493                 char *s;        /* some string */
494                 int i;          /* some integer */
495                 long l;         /* some other integer */
496                 struct net_stat *net;
497                 struct fs_stat *fs;
498                 unsigned char loadavg[3];
499
500                 struct {
501                         struct fs_stat *fs;
502                         int w, h;
503                 } fsbar;        /* 3 */
504
505                 struct {
506                         int l;
507                         int w, h;
508                 } mixerbar;     /* 3 */
509
510                 struct {
511                         int fd;
512                         int arg;
513                         char devtype[256];
514                         char type[64];
515                 } i2c;          /* 2 */
516                 struct {
517                        int pos;
518                        char* s;
519                 } ifblock;
520                 struct {
521                         int num;
522                         int type;
523                 } top;
524
525                         struct {
526                         int wantedlines;
527                         int readlines;
528                         char *logfile;
529                         double last_update;
530                         float interval;
531                         char buffer[TEXT_BUFFER_SIZE*4];
532         } tail;
533
534                 struct {
535                         double last_update;
536                         float interval;
537                         char *cmd;
538                         char *buffer;
539                 } execi;        /* 5 */
540
541                 struct {
542                         int a, b;
543                 } pair;         /* 2 */
544
545
546         } data;
547 };
548
549 static unsigned int text_object_count;
550 static struct text_object *text_objects;
551
552 /* new_text_object() allocates a new zeroed text_object */
553 static struct text_object *new_text_object()
554 {
555         text_object_count++;
556         text_objects = (struct text_object *) realloc(text_objects,
557                                                       sizeof(struct
558                                                              text_object) *
559                                                       text_object_count);
560         memset(&text_objects[text_object_count - 1], 0,
561                sizeof(struct text_object));
562
563         return &text_objects[text_object_count - 1];
564 }
565
566 static void free_text_objects()
567 {
568         unsigned int i;
569
570         for (i = 0; i < text_object_count; i++) {
571                 switch (text_objects[i].type) {
572                 case OBJ_acpitemp:
573                         close(text_objects[i].data.i);
574                         break;
575
576                 case OBJ_i2c:
577                         close(text_objects[i].data.i2c.fd);
578                         break;
579                 case OBJ_time:
580                 case OBJ_utime:
581         case OBJ_if_existing:
582         case OBJ_if_mounted:
583         case OBJ_if_running:
584           free(text_objects[i].data.ifblock.s);
585        break;
586                 case OBJ_text:
587                 case OBJ_exec:
588                 case OBJ_execbar:
589 #ifdef MPD
590                 case OBJ_mpd_title:
591                         case OBJ_mpd_artist:
592                         case OBJ_mpd_album:
593                         case OBJ_mpd_status:
594                         case OBJ_mpd_host:
595 #endif
596                 case OBJ_pre_exec:
597                 case OBJ_battery:
598                         free(text_objects[i].data.s);
599                         break;
600
601                 case OBJ_execi:
602                         free(text_objects[i].data.execi.cmd);
603                         free(text_objects[i].data.execi.buffer);
604                         break;
605                 }
606         }
607
608         free(text_objects);
609         text_objects = NULL;
610         text_object_count = 0;
611 }
612
613 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
614 {
615         char buf1[64];
616         int n;
617
618         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
619                 *a = mixer_init(buf1);
620                 (void) scan_bar(arg + n, w, h);
621         } else {
622                 *a = mixer_init(0);
623                 (void) scan_bar(arg, w, h);
624         }
625 }
626
627 /* construct_text_object() creates a new text_object */
628 static void construct_text_object(const char *s, const char *arg)
629 {
630         struct text_object *obj = new_text_object();
631
632 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
633 #define END ; } } else
634
635         if (s[0] == '#') {
636                 obj->type = OBJ_color;
637                 obj->data.l = get_x11_color(s);
638         } else
639                 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
640         END OBJ(acpiacadapter, 0)
641         END OBJ(freq, 0) END OBJ(acpifan, 0) END OBJ(battery,
642                                                      0) char bat[64];
643         if (arg)
644                 sscanf(arg, "%63s", bat);
645         else
646                 strcpy(bat, "BAT0");
647         obj->data.s = strdup(bat);
648         END OBJ(buffers, INFO_BUFFERS)
649         END OBJ(cached, INFO_BUFFERS)
650         END OBJ(cpu, INFO_CPU)
651         END OBJ(cpubar, INFO_CPU)
652          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
653         END OBJ(color, 0) obj->data.l =
654             arg ? get_x11_color(arg) : default_fg_color;
655         END OBJ(downspeed, INFO_NET) obj->data.net = get_net_stat(arg);
656         END OBJ(downspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
657         END
658                            OBJ(else,0)
659                                 if(blockdepth) {
660                           text_objects[blockstart[blockdepth-1]-1].data.ifblock.pos = text_object_count;
661                           blockstart[blockdepth-1] = text_object_count;
662                           obj->data.ifblock.pos = text_object_count + 2;
663                      } else { ERR("$else: no matching $if_*"); }
664                    END
665                                    OBJ(endif,0)
666                                      if(blockdepth) {
667                                blockdepth--;
668                                text_objects[blockstart[blockdepth]-1].data.ifblock.pos = text_object_count;
669                              } else { ERR("$endif: no matching $if_*"); }
670                            END
671 #ifdef HAVE_POPEN
672         OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
673         END
674         OBJ(execbar, 0) 
675                 obj->data.s = strdup(arg ? arg : "");
676         END
677         OBJ(execi, 0) unsigned int n;
678
679         if (!arg
680             || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
681                 char buf[256];
682                 ERR("${execi <interval> command}");
683                 obj->type = OBJ_text;
684                 snprintf(buf, 256, "${%s}", s);
685                 obj->data.s = strdup(buf);
686         } else {
687                 obj->data.execi.cmd = strdup(arg + n);
688                 obj->data.execi.buffer =
689                     (char *) calloc(1, TEXT_BUFFER_SIZE);
690         }
691         END OBJ(pre_exec, 0) obj->type = OBJ_text;
692         if (arg) {
693                 FILE *fp = popen(arg, "r");
694                 unsigned int n;
695                 char buf[2048];
696
697                 n = fread(buf, 1, 2048, fp);
698                 buf[n] = '\0';
699
700                 if (n && buf[n - 1] == '\n')
701                         buf[n - 1] = '\0';
702
703                 (void) pclose(fp);
704
705                 obj->data.s = strdup(buf);
706         } else
707                 obj->data.s = strdup("");
708         END
709 #endif
710             OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
711         arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
712         if (arg) {
713                 while (isspace(*arg))
714                         arg++;
715                 if (*arg == '\0')
716                         arg = "/";
717         } else
718                 arg = "/";
719         obj->data.fsbar.fs = prepare_fs_stat(arg);
720         END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
721         if (arg) {
722                 unsigned int n;
723                 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
724                         arg += n;
725         } else
726                 arg = "/";
727         obj->data.fsbar.fs = prepare_fs_stat(arg);
728         END OBJ(fs_free, INFO_FS) if (!arg)
729                  arg = "/";
730         obj->data.fs = prepare_fs_stat(arg);
731         END OBJ(fs_used_perc, INFO_FS) if (!arg)
732                  arg = "/";
733         obj->data.fs = prepare_fs_stat(arg);
734         END OBJ(fs_free_perc, INFO_FS) if (!arg)
735                  arg = "/";
736         obj->data.fs = prepare_fs_stat(arg);
737         END OBJ(fs_size, INFO_FS) if (!arg)
738                  arg = "/";
739         obj->data.fs = prepare_fs_stat(arg);
740         END OBJ(fs_used, INFO_FS) if (!arg)
741                  arg = "/";
742         obj->data.fs = prepare_fs_stat(arg);
743         END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
744         END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
745         int n;
746
747         if (!arg) {
748                 ERR("i2c needs arguments");
749                 obj->type = OBJ_text;
750                 obj->data.s = strdup("${i2c}");
751                 return;
752         }
753
754         if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
755                 /* if scanf couldn't read three values, read type and num and use
756                  * default device */
757                 sscanf(arg, "%63s %d", buf2, &n);
758                 obj->data.i2c.fd =
759                     open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
760                                     obj->data.i2c.devtype);
761                 strcpy(obj->data.i2c.type, buf2);
762         } else {
763                 obj->data.i2c.fd =
764                     open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
765                                     obj->data.i2c.devtype);
766                 strcpy(obj->data.i2c.type, buf2);
767         }
768         
769         END OBJ(top, INFO_TOP)
770                         char buf[64];
771         int n;
772         if(!arg) {
773                 ERR("top needs arguments");
774                 obj->type = OBJ_text;
775                 obj->data.s = strdup("${top}");
776                 return;
777         }
778         if(sscanf(arg, "%63s %i", buf, &n) == 2) {
779                 if(strcmp(buf, "name") == 0) {
780                         obj->data.top.type = TOP_NAME;
781                 }
782                 else if(strcmp(buf, "cpu") == 0) {
783                         obj->data.top.type = TOP_CPU;
784                 }
785                 else if(strcmp(buf, "pid") == 0) {
786                         obj->data.top.type = TOP_PID;
787                 }
788                 else if(strcmp(buf, "mem") == 0) {
789                         obj->data.top.type = TOP_MEM;
790                 }
791                 else
792                 {
793                         ERR("invalid arg for top");
794                         return;
795                 }
796                 if(n < 1 || n > 10) {
797                         CRIT_ERR("invalid arg for top");
798                         return;
799                 }
800                 else {
801                         obj->data.top.num = n-1;
802                 }
803         }
804         else {
805                 ERR("invalid args given for top");
806                 return;
807         }
808         END 
809   OBJ(addr, INFO_NET)
810      obj->data.net = get_net_stat(arg);
811   END
812   OBJ(linkstatus, INFO_WIFI)
813      obj->data.net = get_net_stat(arg);
814   END
815         OBJ(tail, 0)
816         char buf[64];
817         int n1, n2;
818         if(!arg) {
819                 ERR("tail needs arguments");
820                 obj->type = OBJ_text;
821                 obj->data.s = strdup("${tail}");
822                 return;
823         }
824         if(sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
825                 if(n1 < 1 || n1 > 30) {
826                         CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
827                         return;
828                 }
829                 else {
830                         FILE * fp;
831                         fp = fopen (buf,"rt");
832                         if (fp != NULL) {
833                                 obj->data.tail.logfile = malloc(TEXT_BUFFER_SIZE);
834                                 strcpy(obj->data.tail.logfile, buf);
835                                 obj->data.tail.wantedlines = n1-1;
836                                 obj->data.tail.interval = update_interval*2;
837                                 fclose (fp);
838                         }
839                         else {
840                                 //fclose (fp);
841                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
842                         }
843                 }
844         }
845         else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
846                 if(n1 < 1 || n1 > 30) {
847                         CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
848                         return;
849                 }
850                 else if(n2 < 1 || n2 < update_interval) {
851                         CRIT_ERR("invalid arg for tail, interval must be greater than 0 and Conky's interval");
852                         return;
853                 }
854                 else {
855                         FILE * fp;
856                         fp = fopen (buf,"rt");
857                         if (fp != NULL) {
858                                 obj->data.tail.logfile = malloc(TEXT_BUFFER_SIZE);
859                                 strcpy(obj->data.tail.logfile, buf);
860                                 obj->data.tail.wantedlines = n1-1;
861                                 obj->data.tail.interval = n2;
862                                 fclose (fp);
863                         }
864                         else {
865                                 //fclose (fp);
866                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
867                         }
868                 }
869         }
870
871         else {
872                 ERR("invalid args given for tail");
873                 return;
874         }
875                         END 
876                         OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
877         if (arg) {
878                 r = sscanf(arg, "%d %d %d", &a, &b, &c);
879                 if (r >= 3 && (c < 1 || c > 3))
880                         r--;
881                 if (r >= 2 && (b < 1 || b > 3))
882                         r--, b = c;
883                 if (r >= 1 && (a < 1 || a > 3))
884                         r--, a = b, b = c;
885         }
886         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
887         obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
888         obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
889         END
890                            OBJ(if_existing,0)
891                                 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
892                                   {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
893                 if(!arg) {
894                           ERR("if_existing needs an argument");
895                           obj->data.ifblock.s = 0;
896                         }
897                         else
898                           obj->data.ifblock.s = strdup(arg);
899                         blockstart[blockdepth] = text_object_count;
900                         obj->data.ifblock.pos = text_object_count + 2;
901                         blockdepth++;
902                    END
903                                    OBJ(if_mounted,0)
904                                         if(blockdepth>=MAX_IF_BLOCK_DEPTH)
905                                           {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
906                         if(!arg) {
907                                   ERR("if_mounted needs an argument");
908                                   obj->data.ifblock.s = 0;
909                                 }
910                                 else
911                                           obj->data.ifblock.s = strdup(arg);
912                                 blockstart[blockdepth] = text_object_count;
913                                 obj->data.ifblock.pos = text_object_count + 2;
914                                 blockdepth++;
915                            END
916                                            OBJ(if_running,0)
917                                                 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
918                                                   {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
919                                 if(arg) {
920                                           char buf[256];
921                                           snprintf(buf,256,"pidof %s >/dev/null",arg);
922                                           obj->data.ifblock.s = strdup(buf);
923                                         } else {
924                                                   ERR("if_running needs an argument");
925                                                   obj->data.ifblock.s = 0;
926                                                 }
927                                                 blockstart[blockdepth] = text_object_count;
928                                                 obj->data.ifblock.pos = text_object_count + 2;
929                                                 blockdepth++;
930                                            END  
931                         OBJ(kernel, 0)
932         END OBJ(machine, 0)
933         END OBJ(mails, INFO_MAIL)
934         END OBJ(mem, INFO_MEM)
935         END OBJ(memmax, INFO_MEM)
936         END OBJ(memperc, INFO_MEM)
937         END OBJ(membar, INFO_MEM)
938          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
939         END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
940         END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
941         END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
942         END OBJ(mixerbar, INFO_MIXER)
943             scan_mixer_bar(arg, &obj->data.mixerbar.l,
944                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
945         END OBJ(mixerlbar, INFO_MIXER)
946             scan_mixer_bar(arg, &obj->data.mixerbar.l,
947                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
948         END OBJ(mixerrbar, INFO_MIXER)
949             scan_mixer_bar(arg, &obj->data.mixerbar.l,
950                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
951         END
952                          #ifdef MLDONKEY
953                            OBJ(ml_upload_counter, INFO_MLDONKEY)
954                            END
955                           OBJ(ml_download_counter, INFO_MLDONKEY)
956                            END
957                            OBJ(ml_nshared_files, INFO_MLDONKEY)
958                           END
959                            OBJ(ml_shared_counter, INFO_MLDONKEY)
960                            END
961                            OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
962                            END
963                            OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
964                            END
965                            OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
966                            END
967                            OBJ(ml_udp_download_rate, INFO_MLDONKEY)
968                            END
969                            OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
970                            END
971                            OBJ(ml_ndownloading_files, INFO_MLDONKEY)
972                            END
973                          #endif
974                         OBJ(new_mails, INFO_MAIL)
975         END OBJ(nodename, 0)
976         END OBJ(processes, INFO_PROCS)
977         END OBJ(running_processes, INFO_RUN_PROCS)
978         END OBJ(shadecolor, 0)
979             obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
980         END OBJ(outlinecolor, 0)
981             obj->data.l = arg ? get_x11_color(arg) : default_out_color;
982         END OBJ(stippled_hr, 0) int a = stippled_borders, b = 1;
983         if (arg) {
984                 if (sscanf(arg, "%d %d", &a, &b) != 2)
985                         sscanf(arg, "%d", &b);
986         }
987         if (a <= 0)
988                 a = 1;
989         obj->data.pair.a = a;
990         obj->data.pair.b = b;
991         END OBJ(swap, INFO_MEM)
992         END OBJ(swapmax, INFO_MEM)
993         END OBJ(swapperc, INFO_MEM)
994         END OBJ(swapbar, INFO_MEM)
995          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
996         END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
997         obj->data.i2c.fd =
998             open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
999                             obj->data.i2c.devtype);
1000         END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1001         obj->data.i2c.fd =
1002             open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1003                             obj->data.i2c.devtype);
1004         END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1005         END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1006         END OBJ(totaldown, INFO_NET) obj->data.net = get_net_stat(arg);
1007         END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1008         END OBJ(updates, 0)
1009 END
1010                         OBJ(alignr, 0)
1011                         obj->data.i = arg ? atoi(arg) : 1;
1012         END
1013
1014                         OBJ(alignc, 0)
1015                         obj->data.i = arg ? atoi(arg) : 1;
1016         END OBJ(upspeed, INFO_NET) obj->data.net = get_net_stat(arg);
1017         END OBJ(upspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1018         END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1019                         OBJ(adt746xcpu, 0)
1020                         END
1021                         OBJ(adt746xfan, 0)
1022                         END
1023
1024 #ifdef SETI
1025          OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1026          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1027         END OBJ(seti_credit, INFO_SETI) END
1028 #endif
1029 #ifdef METAR
1030                            OBJ(metar_ob_time, INFO_METAR)
1031                            END
1032                         OBJ(metar_temp, INFO_METAR)
1033                         END
1034                         OBJ(metar_tempf, INFO_METAR)
1035                         END
1036                         OBJ(metar_windchill, INFO_METAR)
1037                            END
1038                            OBJ(metar_dew_point, INFO_METAR)
1039                            END
1040                            OBJ(metar_rh, INFO_METAR)
1041    END
1042
1043                            OBJ(metar_windspeed, INFO_METAR)
1044                            END
1045                            OBJ(metar_winddir, INFO_METAR)
1046                            END
1047                            OBJ(metar_swinddir, INFO_METAR)
1048                            END
1049
1050                            OBJ(metar_cloud, INFO_METAR)
1051                            END
1052                            OBJ(metar_u2d_time, INFO_METAR)
1053                         END
1054 #endif
1055                         
1056 #ifdef MPD
1057          OBJ(mpd_artist, INFO_MPD) 
1058                         END OBJ(mpd_title, INFO_MPD)
1059                         END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol, INFO_MPD) END OBJ(mpd_bitrate, INFO_MPD) END
1060                         OBJ(mpd_status, INFO_MPD) END
1061                         OBJ(mpd_bar, INFO_MPD)
1062                         (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1063         END
1064 #endif
1065 {
1066                 char buf[256];
1067                 ERR("unknown variable %s", s);
1068                 obj->type = OBJ_text;
1069                 snprintf(buf, 256, "${%s}", s);
1070                 obj->data.s = strdup(buf);
1071 }
1072 #undef OBJ
1073 }
1074
1075 /* append_text() appends text to last text_object if it's text, if it isn't
1076  * it creates a new text_object */
1077 static void append_text(const char *s)
1078 {
1079         struct text_object *obj;
1080
1081         if (s == NULL || *s == '\0')
1082                 return;
1083
1084         obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1085
1086         /* create a new text object? */
1087         if (!obj || obj->type != OBJ_text) {
1088                 obj = new_text_object();
1089                 obj->type = OBJ_text;
1090                 obj->data.s = strdup(s);
1091         } else {
1092                 /* append */
1093                 obj->data.s = (char *) realloc(obj->data.s,
1094                                                strlen(obj->data.s) +
1095                                                strlen(s) + 1);
1096                 strcat(obj->data.s, s);
1097         }
1098 }
1099
1100 static void extract_variable_text(const char *p)
1101 {
1102         const char *s = p;
1103
1104         free_text_objects();
1105
1106         while (*p) {
1107                 if (*p == '$') {
1108                         *(char *) p = '\0';
1109                         append_text(s);
1110                         *(char *) p = '$';
1111                         p++;
1112                         s = p;
1113
1114                         if (*p != '$') {
1115                                 char buf[256];
1116                                 const char *var;
1117                                 unsigned int len;
1118
1119                                 /* variable is either $foo or ${foo} */
1120                                 if (*p == '{') {
1121                                         p++;
1122                                         s = p;
1123                                         while (*p && *p != '}')
1124                                                 p++;
1125                                 } else {
1126                                         s = p;
1127                                         if (*p == '#')
1128                                                 p++;
1129                                         while (*p && (isalnum((int) *p)
1130                                                       || *p == '_'))
1131                                                 p++;
1132                                 }
1133
1134                                 /* copy variable to buffer */
1135                                 len = (p - s > 255) ? 255 : (p - s);
1136                                 strncpy(buf, s, len);
1137                                 buf[len] = '\0';
1138
1139                                 if (*p == '}')
1140                                         p++;
1141                                 s = p;
1142
1143                                 var = getenv(buf);
1144
1145                                 /* if variable wasn't found from environment, use some special */
1146                                 if (!var) {
1147                                         char *p;
1148                                         char *arg = 0;
1149
1150                                         /* split arg */
1151                                         if (strchr(buf, ' ')) {
1152                                                 arg = strchr(buf, ' ');
1153                                                 *arg = '\0';
1154                                                 arg++;
1155                                                 while (isspace((int) *arg))
1156                                                         arg++;
1157                                                 if (!*arg)
1158                                                         arg = 0;
1159                                         }
1160
1161                                         /* lowercase variable name */
1162                                         p = buf;
1163                                         while (*p) {
1164                                                 *p = tolower(*p);
1165                                                 p++;
1166                                         }
1167
1168                                         construct_text_object(buf, arg);
1169                                 }
1170                                 continue;
1171                         } else
1172                                 append_text("$");
1173                 }
1174
1175                 p++;
1176         }
1177         append_text(s);
1178         if(blockdepth) ERR("one or more $endif's are missing");
1179 }
1180
1181 double current_update_time, last_update_time;
1182
1183 static void generate_text()
1184 {
1185         unsigned int i, n;
1186         struct information *cur = &info;
1187         char *p;
1188
1189         special_count = 0;
1190
1191         /* update info */
1192
1193         current_update_time = get_time();
1194
1195         update_stuff(cur);
1196
1197         /* generate text */
1198
1199         n = TEXT_BUFFER_SIZE*4 - 2;
1200         p = text_buffer;
1201
1202         for (i = 0; i < text_object_count; i++) {
1203                 struct text_object *obj = &text_objects[i];
1204
1205 #define OBJ(a) break; case OBJ_##a:
1206
1207                 switch (obj->type) {
1208                 default:
1209                         {
1210                                 ERR("not implemented obj type %d",
1211                                     obj->type);
1212                         }
1213                         OBJ(acpitemp) {
1214                                 /* does anyone have decimals in acpi temperature? */                            
1215                                 if(!use_spacer)
1216                                         snprintf(p, n, "%d",
1217                                                 (int) get_acpi_temperature(obj->
1218                                                                 data.
1219                                                                 i));
1220                                 else
1221                                         snprintf(p, 5, "%d    ",
1222                                                 (int) get_acpi_temperature(obj->
1223                                                                 data.
1224                                                                 i));
1225                         }
1226                         OBJ(freq) {
1227                                 snprintf(p, n, "%s", get_freq());
1228                         }
1229                         OBJ(adt746xcpu) {
1230                                 snprintf(p, n, "%s", get_adt746x_cpu());
1231                         }
1232                         OBJ(adt746xfan) {
1233                                 snprintf(p, n, "%s", get_adt746x_fan());
1234                         }
1235                         OBJ(acpifan) {
1236                                 snprintf(p, n, "%s", get_acpi_fan());
1237                         }
1238                         OBJ(acpiacadapter) {
1239                                 snprintf(p, n, "%s",
1240                                          get_acpi_ac_adapter());
1241                         }
1242                         OBJ(battery) {
1243                                 get_battery_stuff(p, n, obj->data.s);
1244                         }
1245                         OBJ(buffers) {
1246                                 human_readable(cur->buffers * 1024, p, 255);
1247                         }
1248                         OBJ(cached) {
1249                                 human_readable(cur->cached * 1024, p, 255);
1250                         }
1251                         OBJ(cpu) {
1252                                 if(!use_spacer)
1253                                 snprintf(p, n, "%*d", pad_percents,
1254                                                                                 (int) (cur->cpu_usage * 100.0));                
1255                                 else
1256                                 snprintf(p, 4, "%*d    ", pad_percents,
1257                                          (int) (cur->cpu_usage * 100.0));
1258                         }
1259                         OBJ(cpubar) {
1260                                 new_bar(p, obj->data.pair.a,
1261                                         obj->data.pair.b,
1262                                         (int) (cur->cpu_usage * 255.0));
1263                         }
1264                         OBJ(color) {
1265                                 new_fg(p, obj->data.l);
1266                         }
1267                         OBJ(downspeed) {
1268                                 if(!use_spacer)
1269                                         snprintf(p, n, "%d",
1270                                          (int) (obj->data.net->recv_speed /
1271                                                 1024));
1272                                 else
1273                                         snprintf(p, 6, "%d     ",
1274                                                 (int) (obj->data.net->recv_speed /
1275                                                                 1024));
1276                         }
1277                         OBJ(downspeedf) {
1278                                 if(!use_spacer)
1279                                         snprintf(p, n, "%.1f",
1280                                          obj->data.net->recv_speed /
1281                                          1024.0);
1282                                 else
1283                                         snprintf(p, 8, "%.1f       ",
1284                                                 obj->data.net->recv_speed /
1285                                                                 1024.0);
1286                         }
1287                                 OBJ(else) {
1288                                   if(!if_jumped)
1289                                   {
1290                                                         i=obj->data.ifblock.pos-2;
1291                                                   }
1292                                                   else
1293                                                           {
1294                                                                 if_jumped = 0;
1295                                                           }
1296                                                      }
1297                                                         OBJ(endif) {
1298                                                                   if_jumped = 0;
1299                                                              }
1300                         
1301 #ifdef HAVE_POPEN
1302                         OBJ(exec) {
1303                                 char *p2 = p;
1304                                 FILE *fp = popen(obj->data.s, "r");
1305                                 int n2 = fread(p, 1, n, fp);
1306                                 (void) pclose(fp);
1307                         
1308                                 p[n2] = '\0';
1309                                 if (n2 && p[n2 - 1] == '\n')
1310                                         p[n2 - 1] = '\0';
1311                         
1312                                 while (*p2) {
1313                                         if (*p2 == '\001')
1314                                                 *p2 = ' ';
1315                                         p2++;
1316                                 }
1317                         }
1318                         OBJ(execbar) {
1319                                 char *p2 = p;
1320                                 FILE *fp = popen(obj->data.s, "r");
1321                                 int n2 = fread(p, 1, n, fp);
1322                                 (void) pclose(fp);
1323
1324                                 p[n2] = '\0';
1325                                 if (n2 && p[n2 - 1] == '\n')
1326                                         p[n2 - 1] = '\0';
1327     OBJ(addr) {
1328       snprintf(p, n, "%u.%u.%u.%u",
1329                obj->data.net->addr.sa_data[2] & 255,
1330                obj->data.net->addr.sa_data[3] & 255,
1331                obj->data.net->addr.sa_data[4] & 255,
1332                obj->data.net->addr.sa_data[5] & 255);
1333                
1334                }
1335     OBJ(linkstatus) {
1336       snprintf(p, n, "%d", obj->data.net->linkstatus);
1337     }
1338                                 while (*p2) {
1339                                         if (*p2 == '\001')
1340                                                 *p2 = ' ';
1341                                         p2++;
1342                                 }
1343                                 double barnum;
1344                                 if (sscanf(p, "%lf", &barnum) == 0)
1345                                 {
1346                                         ERR("reading execbar value failed (perhaps it's not the correct format?)");
1347                                 }
1348                                 if (barnum > 100 || barnum < 0)
1349                                 {
1350                                         ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
1351                                 }
1352                                 else
1353                                 {
1354                                         barnum = barnum/100.0;
1355                                         new_bar(p, 0,
1356                                                         4,
1357                                                         (int) (barnum * 255.0));
1358                         }
1359                                 
1360                         }
1361                         OBJ(execi) {
1362                                 if (current_update_time -
1363                                     obj->data.execi.last_update <
1364                                     obj->data.execi.interval) {
1365                                         snprintf(p, n, "%s",
1366                                                  obj->data.execi.buffer);
1367                                 } else {
1368                                         char *p2 = obj->data.execi.buffer;
1369                                         FILE *fp =
1370                                             popen(obj->data.execi.cmd,
1371                                                   "r");
1372                                         int n2 =
1373                                             fread(p2, 1, TEXT_BUFFER_SIZE,
1374                                                   fp);
1375                                         (void) pclose(fp);
1376
1377                                         p2[n2] = '\0';
1378                                         if (n2 && p2[n2 - 1] == '\n')
1379                                                 p2[n2 - 1] = '\0';
1380
1381                                         while (*p2) {
1382                                                 if (*p2 == '\001')
1383                                                         *p2 = ' ';
1384                                                 p2++;
1385                                         }
1386
1387                                         snprintf(p, n, "%s",
1388                                                  obj->data.execi.buffer);
1389
1390                                         obj->data.execi.last_update =
1391                                             current_update_time;
1392                                 }
1393                         }
1394 #endif
1395                         OBJ(fs_bar) {
1396                                 if (obj->data.fs != NULL) {
1397                                         if (obj->data.fs->size == 0)
1398                                                 new_bar(p,
1399                                                         obj->data.fsbar.w,
1400                                                         obj->data.fsbar.h,
1401                                                         255);
1402                                         else
1403                                                 new_bar(p,
1404                                                         obj->data.fsbar.w,
1405                                                         obj->data.fsbar.h,
1406                                                         (int) (255 -
1407                                                                obj->data.
1408                                                                fsbar.fs->
1409                                                                avail *
1410                                                                255 /
1411                                                                obj->data.
1412                                                                fs->size));
1413                                 }
1414                         }
1415                         OBJ(fs_free) {
1416                                 if (obj->data.fs != NULL)
1417                                         human_readable(obj->data.fs->avail,
1418                                                        p, 255);
1419                         }
1420                         OBJ(fs_free_perc) {
1421                                 if (obj->data.fs != NULL) {
1422                                         if (obj->data.fs->size)
1423                                                 snprintf(p, n, "%*d", pad_percents, (int) ((obj->data.fs->avail *100) / obj->data.fs->size));
1424                                         else
1425                                                         snprintf(p, n, "0");
1426                         }
1427                         }
1428                         OBJ(fs_size) {
1429                                 if (obj->data.fs != NULL)
1430                                         human_readable(obj->data.fs->size,
1431                                                        p, 255);
1432                         }
1433                         OBJ(fs_used) {
1434                                 if (obj->data.fs != NULL)
1435                                         human_readable(obj->data.fs->size -
1436                                                        obj->data.fs->avail,
1437                                                        p, 255);
1438                         }
1439                         OBJ(fs_bar_free) {
1440                                 if (obj->data.fs != NULL) {
1441                                         if (obj->data.fs->size == 0)
1442                                                 new_bar(p,
1443                                                         obj->data.fsbar.w,
1444                                                         obj->data.fsbar.h,
1445                                                         255);
1446                                         else
1447                                                 new_bar(p,
1448                                                         obj->data.fsbar.w,
1449                                                         obj->data.fsbar.h,
1450                                                         (int) (obj->data.
1451                                                                fsbar.fs->
1452                                                                avail *
1453                                                                255 /
1454                                                                obj->data.
1455                                                                fs->size));
1456                                 }
1457                         }
1458                         OBJ(fs_used_perc) {
1459                                 if (obj->data.fs != NULL) {
1460                                         if (obj->data.fs->size)
1461                                                 snprintf(p, 4, "%d",
1462                                                          100 - ((int)
1463                                                                 ((obj->
1464                                                                   data.fs->
1465                                                                   avail *
1466                                                                   100) /
1467                                                                  obj->data.
1468                                                                  fs->
1469                                                                  size)));
1470                                         else
1471                                                 snprintf(p, n, "0");
1472                                 }
1473                         }
1474                         OBJ(loadavg) {
1475                                 float *v = info.loadavg;
1476
1477                                 if (obj->data.loadavg[2])
1478                                         snprintf(p, n, "%.2f %.2f %.2f",
1479                                                  v[obj->data.loadavg[0] -
1480                                                    1],
1481                                                  v[obj->data.loadavg[1] -
1482                                                    1],
1483                                                  v[obj->data.loadavg[2] -
1484                                                    1]);
1485                                 else if (obj->data.loadavg[1])
1486                                         snprintf(p, n, "%.2f %.2f",
1487                                                  v[obj->data.loadavg[0] -
1488                                                    1],
1489                                                  v[obj->data.loadavg[1] -
1490                                                    1]);
1491                                 else if (obj->data.loadavg[0])
1492                                         snprintf(p, n, "%.2f",
1493                                                  v[obj->data.loadavg[0] -
1494                                                    1]);
1495                         }
1496                         OBJ(hr) {
1497                                 new_hr(p, obj->data.i);
1498                         }
1499                         OBJ(i2c) {
1500                                 double r;
1501
1502                                 r = get_i2c_info(&obj->data.i2c.fd,
1503                                                  obj->data.i2c.arg,
1504                                                  obj->data.i2c.devtype,
1505                                                  obj->data.i2c.type);
1506
1507                                 if (r >= 100.0 || r == 0)
1508                                         snprintf(p, n,"%d", (int) r);
1509                                 else
1510                                         snprintf(p, n, "%.1f", r);
1511                         }
1512                         OBJ(alignr) {
1513                                 new_alignr(p, obj->data.i);
1514                         }
1515                         OBJ(alignc) {
1516                                 new_alignc(p, obj->data.i);
1517                         }
1518                             OBJ(if_existing) {
1519                                        struct stat tmp;
1520                                        if((obj->data.ifblock.s)&&(stat(obj->data.ifblock.s,&tmp) == -1)) {
1521                                                         i=obj->data.ifblock.pos-2;
1522                                                         if_jumped = 1;
1523                                                   } else if_jumped = 0;
1524                                              }
1525                                              OBJ(if_mounted) {
1526                                                           if((obj->data.ifblock.s)&&(!check_mount(obj->data.ifblock.s))) {
1527                                                                         i=obj->data.ifblock.pos-2;
1528                                                                         if_jumped = 1;
1529                                                                   } else if_jumped = 0;
1530                                                              }
1531                                                              OBJ(if_running) {
1532                                                                           if((obj->data.ifblock.s)&&system(obj->data.ifblock.s)) {
1533                                                                                     i=obj->data.ifblock.pos-2;
1534                                                                                         if_jumped = 1;
1535                                                                                   } else if_jumped = 0;
1536                                                                              }
1537                         OBJ(kernel) {
1538                                 snprintf(p, n, "%s", cur->uname_s.release);
1539                         }
1540                         OBJ(machine) {
1541                                 snprintf(p, n, "%s", cur->uname_s.machine);
1542                         }
1543
1544                         /* memory stuff */
1545                         OBJ(mem) {
1546                                 human_readable(cur->mem * 1024, p, 6);
1547                         }
1548                         OBJ(memmax) {
1549                                 human_readable(cur->memmax * 1024, p, 255);
1550                         }
1551                         OBJ(memperc) {
1552                                 if (cur->memmax)
1553                                 {
1554                                         if(!use_spacer)
1555                                                 snprintf(p, n, "%*d", pad_percents,
1556                                                         (cur->mem * 100) /
1557                                                                         (cur->memmax));
1558                                         else
1559                                                 snprintf(p, 4, "%*d   ", pad_percents,
1560                                                         (cur->mem * 100) /
1561                                                                         (cur->memmax));
1562                                 }
1563                         }
1564                         OBJ(membar) {
1565                                 new_bar(p, obj->data.pair.a,
1566                                         obj->data.pair.b,
1567                                         cur->memmax ? (cur->mem * 255) /
1568                                         (cur->memmax) : 0);
1569                         }
1570
1571                         /* mixer stuff */
1572                         OBJ(mixer) {
1573                                 snprintf(p, n, "%d",
1574                                          mixer_get_avg(obj->data.l));
1575                         }
1576                         OBJ(mixerl) {
1577                                 snprintf(p, n, "%d",
1578                                          mixer_get_left(obj->data.l));
1579                         }
1580                         OBJ(mixerr) {
1581                                 snprintf(p, n, "%d",
1582                                          mixer_get_right(obj->data.l));
1583                         }
1584                         OBJ(mixerbar) {
1585                                 new_bar(p, obj->data.mixerbar.w,
1586                                         obj->data.mixerbar.h,
1587                                         mixer_get_avg(obj->data.mixerbar.
1588                                                       l) * 255 / 100);
1589                         }
1590                         OBJ(mixerlbar) {
1591                                 new_bar(p, obj->data.mixerbar.w,
1592                                         obj->data.mixerbar.h,
1593                                         mixer_get_left(obj->data.mixerbar.
1594                                                        l) * 255 / 100);
1595                         }
1596                         OBJ(mixerrbar) {
1597                                 new_bar(p, obj->data.mixerbar.w,
1598                                         obj->data.mixerbar.h,
1599                                         mixer_get_right(obj->data.mixerbar.
1600                                                         l) * 255 / 100);
1601                         }
1602
1603                         /* mail stuff */
1604                         OBJ(mails) {
1605                                 snprintf(p, n, "%d", cur->mail_count);
1606                         }
1607                         OBJ(new_mails) {
1608                                 snprintf(p, n, "%d", cur->new_mail_count);
1609                         }
1610 #ifdef MLDONKEY
1611         OBJ(ml_upload_counter) {
1612                   snprintf(p, n, "%lld", mlinfo.upload_counter/1048576);
1613                 }
1614              OBJ(ml_download_counter) {
1615                           snprintf(p, n, "%lld", mlinfo.download_counter/1048576);
1616                         }
1617                      OBJ(ml_nshared_files) {
1618                                   snprintf(p, n, "%i", mlinfo.nshared_files);
1619                                 }
1620                              OBJ(ml_shared_counter) {
1621                                           snprintf(p, n, "%lld", mlinfo.shared_counter/1048576);
1622                                         }
1623                                      OBJ(ml_tcp_upload_rate) {
1624                                                   snprintf(p, n, "%.2f", (float)mlinfo.tcp_upload_rate/1024);
1625                                                 }
1626                                              OBJ(ml_tcp_download_rate) {
1627                                                           snprintf(p, n, "%.2f",(float)mlinfo.tcp_download_rate/1024);
1628                                                         }
1629                                                      OBJ(ml_udp_upload_rate) {
1630                                                                   snprintf(p, n, "%.2f",(float)mlinfo.udp_upload_rate/1024);
1631                                                                 }
1632                                                              OBJ(ml_udp_download_rate) {
1633                                                                           snprintf(p, n, "%.2f",(float)mlinfo.udp_download_rate/1024);
1634                                                                         }
1635                                                                      OBJ(ml_ndownloaded_files) {
1636                                                                                   snprintf(p, n, "%i", mlinfo.ndownloaded_files);
1637                                                                                 }
1638                                                                              OBJ(ml_ndownloading_files) {
1639                                                                                           snprintf(p, n, "%i", mlinfo.ndownloading_files);
1640                                                                                         }
1641                                                                                  #endif
1642
1643                         OBJ(nodename) {
1644                                 snprintf(p, n, "%s",
1645                                          cur->uname_s.nodename);
1646                         }
1647                         OBJ(outlinecolor) {
1648                                 new_outline(p, obj->data.l);
1649                         }
1650                         OBJ(processes) {
1651                                 if(!use_spacer)
1652                                         snprintf(p, n, "%d", cur->procs);
1653                                 else
1654                                         snprintf(p, 5, "%d    ", cur->procs);
1655                         }
1656                         OBJ(running_processes) {
1657                                 if(!use_spacer)
1658                                         snprintf(p, n, "%d", cur->run_procs);
1659                                 else
1660                                         snprintf(p, 3, "%d     ", cur->run_procs);
1661                         }
1662                         OBJ(text) {
1663                                 snprintf(p, n, "%s", obj->data.s);
1664                         }
1665                         OBJ(shadecolor) {
1666                                 new_bg(p, obj->data.l);
1667                         }
1668                         OBJ(stippled_hr) {
1669                                 new_stippled_hr(p, obj->data.pair.a,
1670                                                 obj->data.pair.b);
1671                         }
1672                         OBJ(swap) {
1673                                 human_readable(cur->swap * 1024, p, 255);
1674                         }
1675                         OBJ(swapmax) {
1676                                 human_readable(cur->swapmax * 1024, p, 255);
1677                         }
1678                         OBJ(swapperc) {
1679                                 if (cur->swapmax == 0) {
1680                                         strncpy(p, "No swap", 255);
1681                                 } else {
1682                                         if(!use_spacer)
1683                                                 snprintf(p, 255, "%*u",
1684                                                         pad_percents,
1685                                                         (cur->swap * 100) /
1686                                                                         cur->swapmax);
1687                                         else
1688                                                 snprintf(p, 4, "%*u   ",
1689                                                         pad_percents,
1690                                                         (cur->swap * 100) /
1691                                                                         cur->swapmax);
1692                                 }
1693                         }
1694                         OBJ(swapbar) {
1695                                 new_bar(p, obj->data.pair.a,
1696                                         obj->data.pair.b,
1697                                         cur->swapmax ? (cur->swap * 255) /
1698                                         (cur->swapmax) : 0);
1699                         }
1700                         OBJ(sysname) {
1701                                 snprintf(p, n, "%s", cur->uname_s.sysname);
1702                         }
1703                         OBJ(time) {
1704                                 time_t t = time(NULL);
1705                                 struct tm *tm = localtime(&t);
1706                                 setlocale(LC_TIME, "");
1707                                 strftime(p, n, obj->data.s, tm);
1708                         }
1709                         OBJ(utime) {
1710                                 time_t t = time(NULL);
1711                                 struct tm *tm = gmtime(&t);
1712                                 strftime(p, n, obj->data.s, tm);
1713                         }
1714                         OBJ(totaldown) {
1715                                 human_readable(obj->data.net->recv, p, 255);
1716                         }
1717                         OBJ(totalup) {
1718                                 human_readable(obj->data.net->trans, p, 255);
1719                         }
1720                         OBJ(updates) {
1721                                 snprintf(p, n, "%d", total_updates);
1722                         }
1723                         OBJ(upspeed) {
1724                                 if(!use_spacer)
1725                                         snprintf(p, n, "%d",
1726                                                 (int) (obj->data.net->
1727                                                                 trans_speed / 1024));
1728                                 else
1729                                         snprintf(p, 5, "%d     ",
1730                                                 (int) (obj->data.net->
1731                                                                 trans_speed / 1024));
1732                         }
1733                         OBJ(upspeedf) {
1734                                 if(!use_spacer)
1735                                         snprintf(p, n, "%.1f",
1736                                                 obj->data.net->trans_speed /
1737                                                                 1024.0);
1738                                 else
1739                                         snprintf(p, 8, "%.1f       ",
1740                                                 obj->data.net->trans_speed /
1741                                                                 1024.0);
1742                         }
1743                         OBJ(uptime_short) {
1744                                 format_seconds_short(p, n,
1745                                                      (int) cur->uptime);
1746                         }
1747                         OBJ(uptime) {
1748                                 format_seconds(p, n, (int) cur->uptime);
1749                         }
1750
1751 #ifdef SETI
1752                         OBJ(seti_prog) {
1753                                 snprintf(p, n, "%.2f",
1754                                          cur->seti_prog * 100.0f);
1755                         }
1756                         OBJ(seti_progbar) {
1757                                 new_bar(p, obj->data.pair.a,
1758                                         obj->data.pair.b,
1759                                         (int) (cur->seti_prog * 255.0f));
1760                         }
1761                         OBJ(seti_credit) {
1762                                 snprintf(p, n, "%.0f", cur->seti_credit);
1763                         }
1764 #endif
1765
1766 #ifdef MPD
1767                         OBJ(mpd_title) {
1768                                 snprintf(p, n, "%s", cur->mpd.title);
1769                         }
1770                         OBJ(mpd_artist) {
1771                                 snprintf(p, n, "%s", cur->mpd.artist);
1772                         }
1773                         OBJ(mpd_album) {
1774                                 snprintf(p, n, "%s", cur->mpd.album);
1775                         }
1776                         OBJ(mpd_vol) {
1777                                 snprintf(p, n, "%i", cur->mpd.volume);
1778                         }
1779                         OBJ(mpd_bitrate) {
1780                                 snprintf(p, n, "%i", cur->mpd.bitrate);
1781                         }
1782                         OBJ(mpd_status) {
1783                                 snprintf(p, n, "%s", cur->mpd.status);
1784                         }
1785                         OBJ(mpd_bar) {
1786                                 new_bar(p, obj->data.pair.a,
1787                                         obj->data.pair.b,
1788                                         (int) (cur->mpd.progress * 255.0f));
1789                         }
1790 #endif
1791                         OBJ(top) {
1792         if (obj->data.top.type == TOP_NAME && obj->data.top.num >= 0 && obj->data.top.num < 10){
1793                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
1794                                         // moving other shit around, which is really fucking annoying
1795                 snprintf(p, 17, "%s                              ", cur->tops[obj->data.top.num]->name);
1796         }
1797         else if (obj->data.top.type == TOP_CPU && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1798                 snprintf(p, 7, "%3.2f      ", cur->tops[obj->data.top.num]->amount);
1799         }
1800         else if (obj->data.top.type == TOP_PID && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1801                 snprintf(p, 8, "%i           ", cur->tops[obj->data.top.num]->pid);
1802         }
1803         else if (obj->data.top.type == TOP_MEM && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1804                 snprintf(p, 7, "%3.2f       ", cur->tops[obj->data.top.num]->totalmem);
1805         }
1806                         }
1807                         
1808                         
1809                         
1810                         /*
1811                          * I'm tired of everything being packed in
1812                          * pee
1813                          * poop
1814                          */
1815                         
1816                         
1817                         OBJ(tail) {
1818                                 if (current_update_time -
1819                                 obj->data.tail.last_update <
1820                                                 obj->data.tail.interval) {
1821                                         snprintf(p, n, "%s",
1822                                                         obj->data.tail.buffer);
1823                                                 } else {
1824                                                         
1825                                                         obj->data.tail.last_update =
1826                                                                         current_update_time;
1827                         
1828                                 FILE * fp;
1829                                 int i;
1830                                 tailstring *head = NULL;
1831                                 tailstring *headtmp1 = NULL;
1832                                 tailstring *headtmp2 = NULL;
1833                                 tailstring *headtmp2tmp = NULL;
1834                                 fp = fopen (obj->data.tail.logfile , "rt");
1835                                 if (fp == NULL) ERR("tail logfile failed to open");
1836                                 else {
1837                                         obj->data.tail.readlines = 0;
1838                                         
1839                                         while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL) {
1840                                                 addtail(&head, obj->data.tail.buffer);
1841                                                 obj->data.tail.readlines++;
1842                                         }
1843                                         
1844                                         fclose(fp);
1845                                         
1846                                         headtmp1 = head;
1847                                         if (obj->data.tail.readlines > 0) {
1848                                                 for(i=0;i<obj->data.tail.wantedlines;i++) {
1849                                                         if(head)
1850                                                         {
1851                                                                 addtail(&headtmp2, head->data);
1852                                                                 head = head->next;
1853                                                         }
1854                                                         else
1855                                                                 break;
1856                                                 }
1857                                                 headtmp2tmp = headtmp2;
1858                                                 for(i=0;i<obj->data.tail.wantedlines;i++) {
1859                                                         if(headtmp2) {
1860                                                                 strncat(obj->data.tail.buffer, headtmp2->data, TEXT_BUFFER_SIZE*4/obj->data.tail.wantedlines);
1861                                                                 headtmp2 = headtmp2->next;
1862                                                         }
1863                                                 }
1864                                                 
1865                                                 /* this is good enough for now. */
1866                                                 snprintf(p, n, "%s", obj->data.tail.buffer);
1867                                                 
1868                                                 freetail(headtmp2tmp);
1869                                         }
1870                                         freetail(headtmp1);
1871                                 }
1872                         }
1873                         }       
1874                         
1875                         
1876                          #ifdef METAR
1877                          // Hmm, it's expensive to calculate this shit every time FIXME
1878                              OBJ(metar_ob_time){
1879                                if ( data.ob_hour != INT_MAX && data.ob_minute != INT_MAX && metar_worked )
1880                                        format_seconds(p, n, data.ob_hour*3600+data.ob_minute*60);
1881                                else
1882                                                        format_seconds(p, n, 0);
1883                                      }
1884                                      OBJ(metar_temp){
1885                                        if ( data.temp != INT_MAX && metar_worked)
1886                                                snprintf(p, n, "%i", data.temp);
1887                                        else
1888                                          snprintf(p, n, "-");
1889                                      }
1890                                      OBJ(metar_tempf){
1891                                              if ( data.temp != INT_MAX && metar_worked)
1892                                                      snprintf(p, n, "%3.1f", (data.temp+40)*9.0/5-40);
1893                                              else
1894                                                      snprintf(p, n, "-");
1895                                      }
1896                                      OBJ(metar_windchill){
1897                                        if ( data.temp != INT_MAX && data.winData.windSpeed != INT_MAX && metar_worked)
1898                                                  snprintf(p, n, "%i", calculateWindChill(data.temp, data.winData.windSpeed));
1899                                                else
1900                                                  snprintf(p, n, "-");
1901                                              }
1902                                              OBJ(metar_dew_point){
1903                                                if ( data.dew_pt_temp != INT_MAX && metar_worked)
1904                                                  snprintf(p, n, "%i", data.dew_pt_temp);
1905                                                else
1906                                                  snprintf(p, n, "-");
1907                                              }
1908                                                   OBJ(metar_rh){
1909                                                             if ( data.temp != INT_MAX && data.dew_pt_temp != INT_MAX && metar_worked)
1910                                                                       snprintf(p, n, "%i", calculateRelativeHumidity(data.temp, data.dew_pt_temp));
1911                                                             else
1912                                                                       snprintf(p, n, "-");
1913                                                           }
1914
1915                                              OBJ(metar_windspeed){
1916                                                if ( data.winData.windSpeed != INT_MAX && metar_worked)
1917                                                  snprintf(p, n, "%i", knTokph(data.winData.windSpeed));
1918                                                else
1919                                                  snprintf(p, n, "-");
1920                                              }
1921                                              OBJ(metar_winddir){
1922                                                      if ( data.winData.windDir != INT_MAX && metar_worked)
1923                                                                 snprintf(p, n, "%s", calculateWindDirectionString(data.winData.windDir));
1924                                                else
1925                                                  snprintf(p, n, "-");
1926                                              }
1927                                                   OBJ(metar_swinddir){
1928                                                             if ( data.winData.windDir != INT_MAX && metar_worked)
1929                                                                       snprintf(p, n, "%s", calculateShortWindDirectionString(data.winData.windDir));
1930                                                             else
1931                                                                       snprintf(p, n, "-");
1932                                                           }
1933
1934                                              OBJ(metar_cloud){
1935                                                if (data.cldTypHgt[0].cloud_type[0] != '\0' && metar_worked){
1936                                                  if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "SKC") == 0 )
1937                                                    snprintf(p, n, "Clear Sky");
1938                                                  else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "CLR") == 0 )
1939                                                    snprintf(p, n, "Clear Sky");
1940                                                  else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "FEW") == 0 )
1941                                                    snprintf(p, n, "Few clouds");
1942                                                  else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "SCT") == 0 )
1943                                                    snprintf(p, n, "Scattered");
1944                                                  else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "BKN") == 0 )
1945                                                    snprintf(p, n, "Broken");
1946                                                  else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "OVC") == 0 )
1947                                                    snprintf(p, n, "Overcast");
1948                                                                                          else
1949                                                                                            snprintf(p, n, "Checking...");
1950                                                                                        }
1951                                                                                        else
1952                                                                                          snprintf(p, n, "Checking...");
1953                                                                                      }
1954                                                                                      OBJ(metar_u2d_time){
1955                                                                                       format_seconds(p, n, (int)last_metar_update%(3600*24)+3600);
1956                                                                                      }
1957                                                                                  #endif
1958
1959                         break;
1960                 }
1961
1962                 {
1963                         unsigned int a = strlen(p);
1964                         p += a;
1965                         n -= a;
1966                 }
1967         }
1968
1969         if (stuff_in_upper_case) {
1970                 char *p;
1971
1972                 p = text_buffer;
1973                 while (*p) {
1974                         *p = toupper(*p);
1975                         p++;
1976                 }
1977         }
1978
1979         last_update_time = current_update_time;
1980         total_updates++;
1981         //free(p);
1982 }
1983
1984 /*
1985  * text size
1986  */
1987
1988 static int text_start_x, text_start_y;  /* text start position in window */
1989 static int text_width, text_height;
1990
1991 static inline int get_string_width(const char *s)
1992 {
1993         return *s ? calc_text_width(s, strlen(s)) : 0;
1994 }
1995
1996 static void text_size_updater(char *s)
1997 {
1998         int w = 0;
1999         char *p;
2000
2001         /* get string widths and skip specials */
2002         p = s;
2003         while (*p) {
2004                 if (*p == SPECIAL_CHAR) {
2005                         *p = '\0';
2006                         w += get_string_width(s);
2007                         *p = SPECIAL_CHAR;
2008
2009                         if (specials[special_index].type == BAR) {
2010                                 w += specials[special_index].width;
2011                         }
2012
2013                         special_index++;
2014                         s = p + 1;
2015                 }
2016                 p++;
2017         }
2018
2019         w += get_string_width(s);
2020
2021         if (w > text_width)
2022                 text_width = w;
2023
2024         text_height += font_height();
2025 }
2026
2027 static void update_text_area()
2028 {
2029         int x, y;
2030
2031         /* update text size if it isn't fixed */
2032 #ifdef OWN_WINDOW
2033         if (!fixed_size)
2034 #endif
2035         {
2036                 text_width = minimum_width;
2037                 text_height = 0;
2038                 special_index = 0;
2039                 for_each_line(text_buffer, text_size_updater);
2040                 text_width += 1;
2041                 if (text_height < minimum_height)
2042                         text_height = minimum_height;
2043         }
2044
2045         /* get text position on workarea */
2046         switch (text_alignment) {
2047         case TOP_LEFT:
2048                 x = gap_x;
2049                 y = gap_y;
2050                 break;
2051
2052         case TOP_RIGHT:
2053                 x = workarea[2] - text_width - gap_x;
2054                 y = gap_y;
2055                 break;
2056
2057         default:
2058         case BOTTOM_LEFT:
2059                 x = gap_x;
2060                 y = workarea[3] - text_height - gap_y;
2061                 break;
2062
2063         case BOTTOM_RIGHT:
2064                 x = workarea[2] - text_width - gap_x;
2065                 y = workarea[3] - text_height - gap_y;
2066                 break;
2067         }
2068
2069 #ifdef OWN_WINDOW
2070         if (own_window) {
2071                 x += workarea[0];
2072                 y += workarea[1];
2073                 text_start_x = border_margin + 1;
2074                 text_start_y = border_margin + 1;
2075                 window.x = x - border_margin - 1;
2076                 window.y = y - border_margin - 1;
2077         } else
2078 #endif
2079         {
2080                 /* If window size doesn't match to workarea's size, then window
2081                  * probably includes panels (gnome).
2082                  * Blah, doesn't work on KDE. */
2083                 if (workarea[2] != window.width
2084                     || workarea[3] != window.height) {
2085                         y += workarea[1];
2086                         x += workarea[0];
2087                 }
2088
2089                 text_start_x = x;
2090                 text_start_y = y;
2091         }
2092 }
2093
2094 /*
2095  * drawing stuff
2096  */
2097
2098 static int cur_x, cur_y;        /* current x and y for drawing */
2099 static int draw_mode;           /* FG, BG or OUTLINE */
2100 static long current_color;
2101
2102 static inline void set_foreground_color(long c)
2103 {
2104         current_color = c;
2105         XSetForeground(display, window.gc, c);
2106 }
2107
2108 static void draw_string(const char *s)
2109 {
2110         if (s[0] == '\0')
2111                 return;
2112         int i, i2, pos, width_of_s;
2113         int max, added;
2114         width_of_s = get_string_width(s);
2115         if (out_to_console)
2116         {
2117                 printf("%s\n", s);
2118         }
2119         strcpy(tmpstring1, s);
2120         pos = 0;
2121         added = 0;
2122         char space[2];
2123         snprintf(space, 2, " ");
2124         max = ((text_width-width_of_s)/get_string_width(space));
2125         //printf("width: %i, length: %i, max: %i space: %i\n", text_width, width_of_s, max, get_string_width(space));
2126         /*
2127          * This code looks for tabs in the text and coverts them to spaces.
2128          * The trick is getting the correct number of spaces,
2129          * and not going over the window's size without forcing
2130          * the window larger.
2131          */
2132         for(i=0;i<TEXT_BUFFER_SIZE;i++)
2133         {
2134                 if (tmpstring1[i] == '\t') // 9 is ascii tab
2135                 {
2136                         i2=0;
2137                         for(i2=0;i2<(8-(1+pos)%8) && added <= max;i2++)
2138                         {
2139                                 tmpstring2[pos+i2] = ' ';
2140                                 added++;
2141                         }
2142                         pos += i2;
2143                 }
2144                 else
2145                 {
2146                         if (tmpstring1[i] != 9) {
2147                                 tmpstring2[pos] = tmpstring1[i];
2148                                 pos++;
2149                         }
2150         }
2151 }
2152         s = tmpstring2;
2153 #ifdef XFT
2154         if (use_xft) {
2155                 XColor c;
2156                 XftColor c2;
2157                 c.pixel = current_color;
2158                 XQueryColor(display, DefaultColormap(display, screen), &c);
2159
2160                 c2.pixel = c.pixel;
2161                 c2.color.red = c.red;
2162                 c2.color.green = c.green;
2163                 c2.color.blue = c.blue;
2164                 c2.color.alpha = font_alpha;
2165                 XftDrawStringUtf8(window.xftdraw, &c2, xftfont,
2166                                   cur_x, cur_y, (XftChar8 *) s, strlen(s));
2167         } else
2168 #endif
2169         {
2170                 XDrawString(display, window.drawable, window.gc,
2171                             cur_x, cur_y, s, strlen(s));
2172         }
2173         memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
2174         cur_x += width_of_s;
2175
2176
2177 }
2178
2179 static void draw_line(char *s)
2180 {
2181         char *p;
2182
2183         cur_x = text_start_x;
2184         cur_y += font_ascent();
2185
2186         /* find specials and draw stuff */
2187         p = s;
2188         while (*p) {
2189                 if (*p == SPECIAL_CHAR) {
2190                         int w = 0;
2191
2192                         /* draw string before special */
2193                         *p = '\0';
2194                         draw_string(s);
2195                         *p = SPECIAL_CHAR;
2196                         s = p + 1;
2197
2198                         /* draw special */
2199                         switch (specials[special_index].type) {
2200                         case HORIZONTAL_LINE:
2201                                 {
2202                                         int h =
2203                                             specials[special_index].height;
2204                                         int mid = font_ascent() / 2;
2205                                         w = text_start_x + text_width -
2206                                             cur_x;
2207
2208                                         XSetLineAttributes(display,
2209                                                            window.gc, h,
2210                                                            LineSolid,
2211                                                            CapButt,
2212                                                            JoinMiter);
2213                                         XDrawLine(display, window.drawable,
2214                                                   window.gc, cur_x,
2215                                                   cur_y - mid/2, cur_x + w,
2216                                                   cur_y - mid/2);
2217                                 }
2218                                 break;
2219
2220                         case STIPPLED_HR:
2221                                 {
2222                                         int h =
2223                                             specials[special_index].height;
2224                                         int s =
2225                                             specials[special_index].arg;
2226                                         int mid = font_ascent() / 2;
2227                                         char ss[2] = { s, s };
2228                                         w = text_start_x + text_width -
2229                                             cur_x - 1;
2230
2231                                         XSetLineAttributes(display,
2232                                                            window.gc, h,
2233                                                            LineOnOffDash,
2234                                                            CapButt,
2235                                                            JoinMiter);
2236                                         XSetDashes(display, window.gc, 0,
2237                                                    ss, 2);
2238                                         XDrawLine(display, window.drawable,
2239                                                   window.gc, cur_x,
2240                                                   cur_y - mid/2, cur_x + w,
2241                                                   cur_y - mid/2);
2242                                 }
2243                                 break;
2244
2245                         case BAR:
2246                                 {
2247                                         int h =
2248                                             specials[special_index].height;
2249                                         int bar_usage =
2250                                             specials[special_index].arg;
2251                                         int by =
2252                                             cur_y - (font_ascent() +
2253                                                      h) / 2 - 1;
2254                                         w = specials[special_index].width;
2255                                         if (w == 0)
2256                                                 w = text_start_x +
2257                                                     text_width - cur_x - 1;
2258                                         if (w < 0)
2259                                                 w = 0;
2260
2261                                         XSetLineAttributes(display,
2262                                                            window.gc, 1,
2263                                                            LineSolid,
2264                                                            CapButt,
2265                                                            JoinMiter);
2266
2267                                         XDrawRectangle(display,
2268                                                        window.drawable,
2269                                                        window.gc, cur_x,
2270                                                        by, w, h);
2271                                         XFillRectangle(display,
2272                                                        window.drawable,
2273                                                        window.gc, cur_x,
2274                                                        by,
2275                                                        w * bar_usage / 255,
2276                                                        h);
2277                                 }
2278                                 break;
2279
2280                         case FG:
2281                                 if (draw_mode == FG)
2282                                         set_foreground_color(specials
2283                                                              [special_index].
2284                                                              arg);
2285                                 break;
2286
2287                         case BG:
2288                                 if (draw_mode == BG)
2289                                         set_foreground_color(specials
2290                                                              [special_index].
2291                                                              arg);
2292                                 break;
2293
2294                                 case OUTLINE:
2295                                         if (draw_mode == OUTLINE)
2296                                                 set_foreground_color(specials
2297                                                                 [special_index].
2298                                                                 arg);
2299                                         break;
2300
2301                                 case ALIGNR:
2302                                 {
2303                                         int pos_x = text_start_x + text_width - cur_x - 1 - get_string_width(p);
2304                                         if ( pos_x > specials[special_index].arg)
2305                                                 w = pos_x - specials[special_index].arg;
2306                                 }
2307                                 break;
2308
2309                                 case ALIGNC:
2310                                 {
2311                                         int pos_x = text_start_x + text_width - cur_x - 1 - get_string_width(p)/2 - (text_width/2);
2312                                         if ( pos_x > specials[special_index].arg)
2313                                                 w = pos_x - specials[special_index].arg;
2314                                 }
2315                                 break;
2316
2317                         }
2318
2319                         cur_x += w;
2320
2321                         special_index++;
2322                 }
2323
2324                 p++;
2325         }
2326
2327         draw_string(s);
2328
2329         cur_y += font_descent();
2330 }
2331
2332 static void draw_text()
2333 {
2334         cur_y = text_start_y;
2335
2336         /* draw borders */
2337         if (draw_borders && border_width > 0) {
2338                 unsigned int b = (border_width + 1) / 2;
2339
2340                 if (stippled_borders) {
2341                         char ss[2] =
2342                             { stippled_borders, stippled_borders };
2343                         XSetLineAttributes(display, window.gc,
2344                                            border_width, LineOnOffDash,
2345                                            CapButt, JoinMiter);
2346                         XSetDashes(display, window.gc, 0, ss, 2);
2347                 } else {
2348                         XSetLineAttributes(display, window.gc,
2349                                            border_width, LineSolid,
2350                                            CapButt, JoinMiter);
2351                 }
2352
2353                 XDrawRectangle(display, window.drawable, window.gc,
2354                                text_start_x - border_margin + b,
2355                                text_start_y - border_margin + b,
2356                                text_width + border_margin * 2 - 1 - b * 2,
2357                                text_height + border_margin * 2 - 1 -
2358                                b * 2);
2359         }
2360
2361         /* draw text */
2362         special_index = 0;
2363         for_each_line(text_buffer, draw_line);
2364 }
2365
2366 static void draw_stuff()
2367 {
2368         if (draw_shades && !draw_outline) {
2369                 text_start_x++;
2370                 text_start_y++;
2371                 set_foreground_color(default_bg_color);
2372                 draw_mode = BG;
2373                 draw_text();
2374                 text_start_x--;
2375                 text_start_y--;
2376         }
2377
2378         if (draw_outline) {
2379                 int i, j;
2380                 for (i = -1; i < 2; i++)
2381                         for (j = -1; j < 2; j++) {
2382                                 if (i == 0 && j == 0)
2383                                         continue;
2384                                 text_start_x += i;
2385                                 text_start_y += j;
2386                                 set_foreground_color(default_out_color);
2387                                 draw_mode = OUTLINE;
2388                                 draw_text();
2389                                 text_start_x -= i;
2390                                 text_start_y -= j;
2391                         }
2392         }
2393
2394         set_foreground_color(default_fg_color);
2395         draw_mode = FG;
2396         draw_text();
2397
2398 #ifdef XDBE
2399         if (use_xdbe) {
2400                 XdbeSwapInfo swap;
2401                 swap.swap_window = window.window;
2402                 swap.swap_action = XdbeBackground;
2403                 XdbeSwapBuffers(display, &swap, 1);
2404         }
2405 #endif
2406 /*#ifdef METAR wtf is this for exactly? aside from trying to cause segfaults?
2407 if (metar_station != NULL) {
2408         free(metar_station);
2409         metar_station = NULL;
2410 }
2411 if (metar_server != NULL) {
2412         free(metar_server);
2413         metar_server = NULL;
2414 }
2415 if (metar_path != NULL) {
2416         free(metar_path);
2417         metar_path = NULL;
2418 }
2419 #endif*/
2420
2421 }
2422
2423 static void clear_text(int exposures)
2424 {
2425 #ifdef XDBE
2426         if (use_xdbe)
2427                 return;         /* The swap action is XdbeBackground, which clears */
2428 #endif
2429         /* there is some extra space for borders and outlines */
2430         XClearArea(display, window.drawable,
2431                    text_start_x - border_margin - 1,
2432                    text_start_y - border_margin - 1,
2433                    text_width + border_margin * 2 + 2,
2434                    text_height + border_margin * 2 + 2,
2435                    exposures ? True : 0);
2436 }
2437
2438 static int need_to_update;
2439
2440 /* update_text() generates new text and clears old text area */
2441 static void update_text()
2442 {
2443         generate_text();
2444         clear_text(1);
2445         need_to_update = 1;
2446 }
2447
2448 static void main_loop()
2449 {
2450         Region region = XCreateRegion();
2451         info.looped = -1;
2452         while (total_run_times == 0 || info.looped < total_run_times-1) {
2453                 info.looped++;
2454                 XFlush(display);
2455
2456                 /* wait for X event or timeout */
2457
2458                 if (!XPending(display)) {
2459                         fd_set fdsr;
2460                         struct timeval tv;
2461                         int s;
2462                         double t =
2463                             update_interval - (get_time() -
2464                                                last_update_time);
2465
2466                         if (t < 0)
2467                                 t = 0;
2468
2469                         tv.tv_sec = (long) t;
2470                         tv.tv_usec = (long) (t * 1000000) % 1000000;
2471
2472                         FD_ZERO(&fdsr);
2473                         FD_SET(ConnectionNumber(display), &fdsr);
2474
2475                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
2476                                    0, &tv);
2477                         if (s == -1) {
2478                                 if (errno != EINTR)
2479                                         ERR("can't select(): %s",
2480                                             strerror(errno));
2481                         } else {
2482                                 /* timeout */
2483                                 if (s == 0)
2484                                         update_text();
2485                         }
2486                 }
2487
2488                 if (need_to_update) {
2489 #ifdef OWN_WINDOW
2490                         int wx = window.x, wy = window.y;
2491 #endif
2492
2493                         need_to_update = 0;
2494
2495                         update_text_area();
2496
2497 #ifdef OWN_WINDOW
2498                         if (own_window) {
2499                                 /* resize window if it isn't right size */
2500                                 if (!fixed_size &&
2501                                     (text_width + border_margin * 2 !=
2502                                      window.width
2503                                      || text_height + border_margin * 2 !=
2504                                      window.height)) {
2505                                         window.width =
2506                                             text_width +
2507                                             border_margin * 2 + 1;
2508                                         window.height =
2509                                             text_height +
2510                                             border_margin * 2 + 1;
2511                                         XResizeWindow(display,
2512                                                       window.window,
2513                                                       window.width,
2514                                                       window.height);
2515                                 }
2516
2517                                 /* move window if it isn't in right position */
2518                                 if (!fixed_pos
2519                                     && (window.x != wx
2520                                         || window.y != wy)) {
2521                                         XMoveWindow(display, window.window,
2522                                                     window.x, window.y);
2523                                 }
2524                         }
2525 #endif
2526
2527                         clear_text(1);
2528
2529 #ifdef XDBE
2530                         if (use_xdbe) {
2531                                 XRectangle r;
2532                                 r.x = text_start_x - border_margin;
2533                                 r.y = text_start_y - border_margin;
2534                                 r.width = text_width + border_margin * 2;
2535                                 r.height = text_height + border_margin * 2;
2536                                 XUnionRectWithRegion(&r, region, region);
2537                         }
2538 #endif
2539                 }
2540
2541                 /* handle X events */
2542
2543                 while (XPending(display)) {
2544                         XEvent ev;
2545                         XNextEvent(display, &ev);
2546
2547                         switch (ev.type) {
2548                         case Expose:
2549                                 {
2550                                         XRectangle r;
2551                                         r.x = ev.xexpose.x;
2552                                         r.y = ev.xexpose.y;
2553                                         r.width = ev.xexpose.width;
2554                                         r.height = ev.xexpose.height;
2555                                         XUnionRectWithRegion(&r, region,
2556                                                              region);
2557                                 }
2558                                 break;
2559
2560 #ifdef OWN_WINDOW
2561                         case ReparentNotify:
2562                                 /* set background to ParentRelative for all parents */
2563                                 if (own_window)
2564                                         set_transparent_background(window.
2565                                                                    window);
2566                                 break;
2567
2568                         case ConfigureNotify:
2569                                 if (own_window) {
2570                                         /* if window size isn't what expected, set fixed size */
2571                                         if (ev.xconfigure.width !=
2572                                             window.width
2573                                             || ev.xconfigure.height !=
2574                                             window.height) {
2575                                                 if (window.width != 0
2576                                                     && window.height != 0)
2577                                                         fixed_size = 1;
2578
2579                                                 /* clear old stuff before screwing up size and pos */
2580                                                 clear_text(1);
2581
2582                                                 {
2583                                                         XWindowAttributes
2584                                                             attrs;
2585                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
2586                                                                 window.
2587                                                                     width =
2588                                                                     attrs.
2589                                                                     width;
2590                                                                 window.
2591                                                                     height
2592                                                                     =
2593                                                                     attrs.
2594                                                                     height;
2595                                                         }
2596                                                 }
2597
2598                                                 text_width =
2599                                                     window.width -
2600                                                     border_margin * 2 - 1;
2601                                                 text_height =
2602                                                     window.height -
2603                                                     border_margin * 2 - 1;
2604                                         }
2605
2606                                         /* if position isn't what expected, set fixed pos, total_updates
2607                                          * avoids setting fixed_pos when window is set to weird locations
2608                                          * when started */
2609                                         if (total_updates >= 2
2610                                             && !fixed_pos
2611                                             && (window.x != ev.xconfigure.x
2612                                                 || window.y !=
2613                                                 ev.xconfigure.y)
2614                                             && (ev.xconfigure.x != 0
2615                                                 || ev.xconfigure.y != 0)) {
2616                                                 fixed_pos = 1;
2617                                         }
2618                                 }
2619                                 break;
2620 #endif
2621
2622                         default:
2623                                 break;
2624                         }
2625                 }
2626
2627                 /* XDBE doesn't seem to provide a way to clear the back buffer without
2628                  * interfering with the front buffer, other than passing XdbeBackground
2629                  * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
2630                  * redraw the text even if it wasn't part of the exposed area. OTOH,
2631                  * if we're not going to call draw_stuff at all, then no swap happens
2632                  * and we can safely do nothing.
2633                  */
2634
2635                 if (!XEmptyRegion(region)) {
2636 #ifdef XDBE
2637                         if (use_xdbe) {
2638                                 XRectangle r;
2639                                 r.x = text_start_x - border_margin;
2640                                 r.y = text_start_y - border_margin;
2641                                 r.width = text_width + border_margin * 2;
2642                                 r.height = text_height + border_margin * 2;
2643                                 XUnionRectWithRegion(&r, region, region);
2644                         }
2645 #endif
2646                         XSetRegion(display, window.gc, region);
2647 #ifdef XFT
2648                         if (use_xft)
2649                                 XftDrawSetClip(window.xftdraw, region);
2650 #endif
2651                         draw_stuff();
2652                         XDestroyRegion(region);
2653                         region = XCreateRegion();
2654                 }
2655         }
2656 }
2657
2658 static void load_font()
2659 {
2660 #ifdef XFT
2661         /* load Xft font */
2662         if (use_xft) {
2663                 if (xftfont != NULL)
2664                         XftFontClose(display, xftfont);
2665
2666                 if ((xftfont =
2667                      XftFontOpenName(display, screen, font_name)) != NULL)
2668                         return;
2669
2670                 ERR("can't load Xft font '%s'", font_name);
2671                 if ((xftfont =
2672                      XftFontOpenName(display, screen,
2673                                      "courier-12")) != NULL)
2674                         return;
2675
2676                 ERR("can't load Xft font '%s'", "courier-12");
2677
2678                 if ((font = XLoadQueryFont(display, "fixed")) == NULL) {
2679                         CRIT_ERR("can't load font '%s'", "fixed");
2680                 }
2681                 use_xft = 0;
2682
2683                 return;
2684         }
2685 #endif
2686
2687         /* load normal font */
2688         if (font != NULL)
2689                 XFreeFont(display, font);
2690
2691         if ((font = XLoadQueryFont(display, font_name)) == NULL) {
2692                 ERR("can't load font '%s'", font_name);
2693                 if ((font = XLoadQueryFont(display, "fixed")) == NULL) {
2694                         CRIT_ERR("can't load font '%s'", "fixed");
2695                 }
2696         }
2697 }
2698
2699 static void set_font()
2700 {
2701 #ifdef XFT
2702         if (use_xft) {
2703                 if (window.xftdraw != NULL)
2704                         XftDrawDestroy(window.xftdraw);
2705                 window.xftdraw = XftDrawCreate(display, window.drawable,
2706                                                DefaultVisual(display,
2707                                                              screen),
2708                                                DefaultColormap(display,
2709                                                                screen));
2710         } else
2711 #endif
2712         {
2713                 XSetFont(display, window.gc, font->fid);
2714         }
2715 }
2716
2717 static void load_config_file(const char *);
2718
2719 /* signal handler that reloads config file */
2720 static void reload_handler(int a)
2721 {
2722         fprintf(stderr, "Conky: received signal %d, reloading config\n",
2723                 a);
2724
2725         if (current_config) {
2726                 clear_fs_stats();
2727                 load_config_file(current_config);
2728                 load_font();
2729                 set_font();
2730                 extract_variable_text(text);
2731                 free(text);
2732                 text = NULL;
2733                 update_text();
2734         }
2735 }
2736
2737 static void clean_up()
2738 {
2739 #ifdef XDBE
2740         if (use_xdbe)
2741                 XdbeDeallocateBackBufferName(display, window.back_buffer);
2742 #endif
2743 #ifdef OWN_WINDOW
2744         if (own_window)
2745                 XDestroyWindow(display, window.window);
2746         else
2747 #endif
2748         {
2749                 clear_text(1);
2750                 XFlush(display);
2751         }
2752
2753         XFreeGC(display, window.gc);
2754
2755         /* it is really pointless to free() memory at the end of program but ak|ra
2756          * wants me to do this */
2757
2758         free_text_objects();
2759
2760         if (text != original_text)
2761                 free(text);
2762
2763         free(current_config);
2764         free(current_mail_spool);
2765 #ifdef SETI
2766         free(seti_dir);
2767 #endif
2768 }
2769
2770 static void term_handler(int a)
2771 {
2772         a = a;                  /* to get rid of warning */
2773         clean_up();
2774         exit(0);
2775 }
2776
2777 static int string_to_bool(const char *s)
2778 {
2779         if (!s)
2780                 return 1;
2781         if (strcasecmp(s, "yes") == 0)
2782                 return 1;
2783         if (strcasecmp(s, "true") == 0)
2784                 return 1;
2785         if (strcasecmp(s, "1") == 0)
2786                 return 1;
2787         return 0;
2788 }
2789
2790 static enum alignment string_to_alignment(const char *s)
2791 {
2792         if (strcasecmp(s, "top_left") == 0)
2793                 return TOP_LEFT;
2794         else if (strcasecmp(s, "top_right") == 0)
2795                 return TOP_RIGHT;
2796         else if (strcasecmp(s, "bottom_left") == 0)
2797                 return BOTTOM_LEFT;
2798         else if (strcasecmp(s, "bottom_right") == 0)
2799                 return BOTTOM_RIGHT;
2800         else if (strcasecmp(s, "tl") == 0)
2801                 return TOP_LEFT;
2802         else if (strcasecmp(s, "tr") == 0)
2803                 return TOP_RIGHT;
2804         else if (strcasecmp(s, "bl") == 0)
2805                 return BOTTOM_LEFT;
2806         else if (strcasecmp(s, "br") == 0)
2807                 return BOTTOM_RIGHT;
2808
2809         return TOP_LEFT;
2810 }
2811
2812 static void set_default_configurations(void)
2813 {
2814         text_alignment = BOTTOM_LEFT;
2815         fork_to_background = 0;
2816         border_margin = 3;
2817         border_width = 1;
2818         total_run_times = 0;
2819         info.cpu_avg_samples = 2;
2820         info.net_avg_samples = 2;
2821         info.memmax = 0;
2822 #ifdef MPD
2823         strcpy(info.mpd.host, "localhost");
2824         info.mpd.port = 6600;
2825         info.mpd.status = "Checking status...";
2826 #endif
2827         out_to_console = 0;
2828         top_sort_cpu = 1;
2829         use_spacer = 0;
2830         default_fg_color = WhitePixel(display, screen);
2831         default_bg_color = BlackPixel(display, screen);
2832         default_out_color = BlackPixel(display, screen);
2833         draw_borders = 0;
2834         draw_shades = 1;
2835         draw_outline = 0;
2836         free(font_name);
2837 #ifdef XFT
2838         use_xft = 1;
2839         font_name = strdup("courier-12");
2840 #else
2841         font_name = strdup("6x10");
2842 #endif
2843         gap_x = 5;
2844         gap_y = 5;
2845
2846         free(current_mail_spool);
2847         {
2848                 char buf[256];
2849                 variable_substitute(MAIL_FILE, buf, 256);
2850                 if (buf[0] != '\0')
2851                         current_mail_spool = strdup(buf);
2852         }
2853
2854         minimum_width = 5;
2855         minimum_height = 5;
2856         no_buffers = 1;
2857 #ifdef OWN_WINDOW
2858         own_window = 0;
2859 #endif
2860         stippled_borders = 0;
2861         update_interval = 10.0;
2862         stuff_in_upper_case = 0;
2863          #ifdef MLDONKEY
2864            mlconfig.mldonkey_hostname="127.0.0.1";
2865            mlconfig.mldonkey_port=4001;
2866            mlconfig.mldonkey_login=NULL;
2867            mlconfig.mldonkey_password=NULL;
2868          #endif
2869 #ifdef METAR
2870          metar_station = NULL;
2871          metar_server = NULL;
2872          metar_path = NULL;
2873          last_metar_update = 0;
2874 #endif
2875 }
2876
2877 static void load_config_file(const char *f)
2878 {
2879 #define CONF_ERR ERR("%s: %d: config file error", f, line);
2880         int line = 0;
2881         FILE *fp;
2882
2883         set_default_configurations();
2884
2885         fp = open_file(f, 0);
2886         if (!fp)
2887                 return;
2888
2889         while (!feof(fp)) {
2890                 char buf[256], *p, *p2, *name, *value;
2891                 line++;
2892                 if (fgets(buf, 256, fp) == NULL)
2893                         break;
2894
2895                 p = buf;
2896
2897                 /* break at comment */
2898                 p2 = strchr(p, '#');
2899                 if (p2)
2900                         *p2 = '\0';
2901
2902                 /* skip spaces */
2903                 while (*p && isspace((int) *p))
2904                         p++;
2905                 if (*p == '\0')
2906                         continue;       /* empty line */
2907
2908                 name = p;
2909
2910                 /* skip name */
2911                 p2 = p;
2912                 while (*p2 && !isspace((int) *p2))
2913                         p2++;
2914                 if (*p2 != '\0') {
2915                         *p2 = '\0';     /* break at name's end */
2916                         p2++;
2917                 }
2918
2919                 /* get value */
2920                 if (*p2) {
2921                         p = p2;
2922                         while (*p && isspace((int) *p))
2923                                 p++;
2924
2925                         value = p;
2926
2927                         p2 = value + strlen(value);
2928                         while (isspace((int) *(p2 - 1)))
2929                                 *--p2 = '\0';
2930                 } else {
2931                         value = 0;
2932                 }
2933
2934 #define CONF2(a) if (strcasecmp(name, a) == 0)
2935 #define CONF(a) else CONF2(a)
2936 #define CONF3(a,b) \
2937 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
2938
2939
2940                 CONF2("alignment") {
2941         if (value) {
2942                 int a = string_to_alignment(value);
2943                 if (a <= 0)
2944                         CONF_ERR
2945                                         else
2946                                         text_alignment = a;
2947         } else
2948                 CONF_ERR}
2949                 CONF("background") {
2950                         fork_to_background = string_to_bool(value);
2951                 }
2952                 CONF("border_margin") {
2953                         if (value)
2954                                 border_margin =
2955                                                 strtol(value, 0, 0);
2956                         else
2957                                 CONF_ERR}
2958                         CONF("border_width") {
2959                                 if (value)
2960                                         border_width =
2961                                                         strtol(value, 0, 0);
2962                                 else
2963                                         CONF_ERR}
2964                                 CONF("default_color") {
2965                                         if (value)
2966                                                 default_fg_color =
2967                                                                 get_x11_color
2968                                                                 (value);
2969                                         else
2970                                                 CONF_ERR}
2971                                         CONF3
2972                                                         ("default_shade_color",
2973                                                         "default_shadecolor")
2974                                         {
2975                                                 if (value)
2976                                                         default_bg_color
2977                                                                         =
2978                                                                         get_x11_color
2979                                                                         (value);
2980                                                 else
2981                                                         CONF_ERR}
2982                                                 CONF3
2983                                                                 ("default_outline_color",
2984                                                                 "default_outlinecolor")
2985                                                 {
2986                                                         if (value)
2987                                                                 default_out_color
2988                                                                                 =
2989                                                                                 get_x11_color
2990                                                                                 (value);
2991                                                         else
2992                                                                 CONF_ERR}
2993 #ifdef MPD
2994                                                                         CONF("mpd_host") {
2995         if (value)
2996                 strcpy(info.
2997                                 mpd.
2998                                 host
2999                                 ,                               value);
3000         else
3001                 CONF_ERR}
3002         CONF("mpd_port") {
3003                 if (value) {
3004                         info.
3005                                         mpd.
3006                                         port
3007                                         =
3008                                         strtol
3009                                         (value,
3010                                          0,
3011                                          0);
3012                         if (info.mpd.port < 1 || info.mpd.port > 0xffff)
3013                                 CONF_ERR}
3014         }
3015 #endif
3016                                                                                                         CONF("cpu_avg_samples") {
3017         if (value) {
3018                 cpu_avg_samples
3019                                 =
3020                                 strtol
3021                                 (value,
3022                                  0,
3023                                  0);
3024                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14)
3025                         CONF_ERR
3026                                         else
3027                                         info.
3028                                         cpu_avg_samples
3029                                         =
3030                                         cpu_avg_samples;
3031         } else
3032                 CONF_ERR}
3033                 CONF("net_avg_samples") {
3034                         if (value) {
3035                                 net_avg_samples
3036                                                 =
3037                                                 strtol
3038                                                 (value,
3039                                                 0,
3040                                                 0);
3041                                 if (net_avg_samples < 1 || net_avg_samples > 14)
3042                                         CONF_ERR
3043                                                         else
3044                                                         info.
3045                                                         net_avg_samples
3046                                                         =
3047                                                         net_avg_samples;
3048                         } else
3049                                 CONF_ERR}
3050
3051
3052
3053
3054
3055
3056 #ifdef XDBE
3057                                                                                                                         CONF("double_buffer") {
3058         use_xdbe
3059                         =
3060                         string_to_bool
3061                         (value);
3062                                                                                 }
3063 #endif
3064                                                                                                                 CONF("draw_borders") {
3065         draw_borders
3066                         =
3067                         string_to_bool
3068                         (value);
3069                                                                                 }
3070                                                                                 CONF("draw_shades") {
3071                                                                                 draw_shades
3072                                                                                 =
3073                                                                                 string_to_bool
3074                                                                                 (value);
3075                                                                                 }
3076                                                                                 CONF("draw_outline") {
3077                                                                                 draw_outline
3078                                                                                 =
3079                                                                                 string_to_bool
3080                                                                                 (value);
3081                                                                                 }
3082                                                                                 CONF("out_to_console") {
3083                                                                                 out_to_console
3084                                                                                 =
3085                                                                                 string_to_bool
3086                                                                                 (value);
3087                                                                                 }
3088                                                                                 CONF("top_sort_cpu") {
3089                                                                                 top_sort_cpu
3090                                                                                 =
3091                                                                                 string_to_bool
3092                                                                                 (value);
3093                                                                                 }
3094                                                                                 CONF("use_spacer") {
3095                                                                                 use_spacer
3096                                                                                 =
3097                                                                                 string_to_bool
3098                                                                                 (value);
3099                                                                                 }
3100 #ifdef XFT
3101                                                                                                                 CONF("use_xft") {
3102         use_xft
3103                         =
3104                         string_to_bool
3105                         (value);
3106                                                                                 }
3107                                                                                 CONF("font") {
3108                                                                                 /* font silently ignored when Xft */
3109                                                                                 }
3110                                                                                 CONF("xftalpha") {
3111                                                                                 if (value)
3112                                                                                 font_alpha
3113                                                                                 =
3114                                                                                 atof
3115                                                                                 (value)
3116                                                                                 *
3117                                                                                 65535.0;
3118                                                                                 else
3119                                                                                 CONF_ERR}
3120                                                                                 CONF("xftfont") {
3121 #else
3122                                                                                                                 CONF("use_xft") {
3123         if (string_to_bool(value))
3124                 ERR("Xft not enabled");
3125                                                                                 }
3126                                                                                 CONF("xftfont") {
3127                                                                                 /* xftfont silently ignored when no Xft */
3128                                                                                 }
3129                                                                                 CONF("xftalpha") {
3130                                                                                 /* xftalpha is silently ignored when no Xft */
3131                                                                                 }
3132                                                                                 CONF("font") {
3133 #endif
3134                                                                                                                         if (value) {
3135         free(font_name);
3136         font_name
3137                         =
3138                         strdup
3139                         (value);
3140                                                                                 } else
3141                                                                                 CONF_ERR}
3142                                                                                 CONF("gap_x") {
3143                                                                                 if (value)
3144                                                                                 gap_x
3145                                                                                 =
3146                                                                                 atoi
3147                                                                                 (value);
3148                                                                                 else
3149                                                                                 CONF_ERR}
3150                                                                                 CONF("gap_y") {
3151                                                                                 if (value)
3152                                                                                 gap_y
3153                                                                                 =
3154                                                                                 atoi
3155                                                                                 (value);
3156                                                                                 else
3157                                                                                 CONF_ERR}
3158                                                                                 CONF("mail_spool") {
3159                                                                                 if (value) {
3160                                                                                 char buf[256];
3161                                                                                 variable_substitute
3162                                                                                 (value,
3163                                                                                 buf,
3164                                                                                 256);
3165
3166                                                                                 if (buf[0]
3167                                                                                 !=
3168                                                                                 '\0')
3169                                                                                 {
3170                                                                                 if (current_mail_spool)
3171                                                                                 free(current_mail_spool);
3172                                                                                 current_mail_spool
3173                                                                                 =
3174                                                                                 strdup
3175                                                                                 (buf);
3176                                                                                 }
3177                                                                                 } else
3178                                                                                 CONF_ERR}
3179                                                                                 CONF("minimum_size") {
3180                                                                                 if (value) {
3181                                                                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height) != 2)
3182                                                                                 if (sscanf(value, "%d", &minimum_width) != 1)
3183                                                                                 CONF_ERR}
3184                                                                                 else
3185                                                                                 CONF_ERR}
3186                                                                                 CONF("no_buffers") {
3187                                                                                 no_buffers
3188                                                                                 =
3189                                                                                 string_to_bool(value);
3190                                                                                 }
3191 #ifdef MLDONKEY
3192 CONF("mldonkey_hostname") {
3193 if (value)
3194         mlconfig.mldonkey_hostname = strdup(value);
3195 else
3196 CONF_ERR
3197 }
3198 CONF("mldonkey_port") {
3199 if (value)
3200         mlconfig.mldonkey_port = atoi(value);
3201 else
3202         CONF_ERR
3203 }
3204 CONF("mldonkey_login") {
3205 if (value)
3206         mlconfig.mldonkey_login = strdup(value);
3207 else
3208         CONF_ERR
3209 }
3210 CONF("mldonkey_password") {
3211 if (value)
3212         mlconfig.mldonkey_password = strdup(value);
3213 else
3214         CONF_ERR
3215 }
3216 #endif
3217 #ifdef OWN_WINDOW
3218                                                                                                                                                                 CONF("own_window") {
3219         own_window
3220                         =
3221                         string_to_bool
3222                         (value);
3223                                                                                 }
3224 #endif
3225                                                                                                                                                                 CONF("pad_percents") {
3226         pad_percents
3227                         =
3228                         atoi
3229                         (value);
3230                                                                                 }
3231                                                                                 CONF("stippled_borders") {
3232                                                                                 if (value)
3233                                                                                 stippled_borders
3234                                                                                 =
3235                                                                                 strtol
3236                                                                                 (value,
3237                                                                                 0,
3238                                                                                 0);
3239                                                                                 else
3240                                                                                 stippled_borders
3241                                                                                 =
3242                                                                                 4;
3243                                                                                 }
3244                                                                                 CONF("temp1") {
3245                                                                                 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
3246                                                                                 }
3247                                                                                 CONF("temp1") {
3248                                                                                 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
3249                                                                                 }
3250                                                                                 CONF("update_interval") {
3251                                                                                 if (value)
3252                                                                                 update_interval
3253                                                                                 =
3254                                                                                 strtod
3255                                                                                 (value,
3256                                                                                 0);
3257                                                                                 else
3258                                                                                 CONF_ERR}                                                                               CONF("total_run_times") {
3259                                                                                 if (value)
3260                                                                                 total_run_times
3261                                                                                 =
3262                                                                                 strtod
3263                                                                                 (value,
3264                                                                                 0);
3265                                                                                 else
3266                                                                                 CONF_ERR}
3267                                                                                 CONF("uppercase") {
3268                                                                                 stuff_in_upper_case
3269                                                                                 =
3270                                                                                 string_to_bool
3271                                                                                 (value);
3272                                                                                 }
3273 #ifdef SETI
3274                                                                                                                                                                         CONF("seti_dir") {
3275         seti_dir
3276                         =
3277                         (char
3278                         *)
3279                         malloc
3280                         (strlen
3281                         (value)
3282                         +
3283                         1);
3284         strcpy
3285                         (seti_dir,
3286                          value);
3287                                                                                 }
3288 #endif
3289 #ifdef METAR
3290      CONF("metar_station"){
3291                metar_station = (char*)malloc(strlen(value) + 5);
3292                strcpy(metar_station, value);
3293                strcat(metar_station,".TXT");
3294              }
3295              CONF("metar_server"){
3296                        metar_server = (char*)malloc(strlen(value) + 1);
3297                        strcpy(metar_server, value);      
3298                      }
3299                      CONF("metar_path"){
3300                                metar_path = (char*)malloc(strlen(value) + 1);
3301                                strcpy(metar_path, value);
3302
3303              }
3304          #endif
3305                                                                                                                                                                         CONF("text") {
3306         if (text != original_text)
3307                 free(text);
3308
3309         text = (char *)
3310                         malloc
3311                         (1);
3312         text[0]
3313                         =
3314                         '\0';
3315
3316         while
3317         (!feof
3318                  (fp))
3319         {
3320                 unsigned
3321                                 int
3322                                 l
3323                                 =
3324                                 strlen
3325                                 (text);
3326                 if (fgets(buf, 256, fp) == NULL)
3327                         break;
3328                 text = (char *)
3329                                 realloc
3330                                 (text,
3331                                  l
3332                                                  +
3333                                                  strlen
3334                                                  (buf)
3335                                                  +
3336                                                  1);
3337                 strcat
3338                                 (text,
3339                                  buf);
3340
3341                 if (strlen(text) > 1024 * 8)
3342                         break;
3343         }
3344         fclose
3345                         (fp);
3346         return;
3347                                                                                 }
3348                                                                                 else
3349                                                                                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
3350
3351 #undef CONF
3352 #undef CONF2
3353                                                                                 }
3354
3355                                                                                 fclose
3356                                                                                 (fp);
3357 #undef CONF_ERR
3358         }
3359
3360         /* : means that character before that takes an argument */
3361         static
3362                         const
3363                         char
3364                         *getopt_string
3365                         =
3366                         "vVdt:f:u:i:hc:w:x:y:a:"
3367 #ifdef OWN_WINDOW
3368                         "o"
3369 #endif
3370 #ifdef XDBE
3371                         "b"
3372 #endif
3373 ;
3374
3375
3376         int main(int argc, char **argv) {
3377                 /* handle command line parameters that don't change configs */
3378                 char *s;
3379                 int utf8_mode = 0;
3380
3381                 if (((s = getenv("LC_ALL"))   && *s) ||
3382                                     ((s = getenv("LC_CTYPE")) && *s) ||
3383                                     ((s = getenv("LANG"))     && *s)) {
3384                         if (strstr(s, "UTF-8"))
3385                                 utf8_mode = 1;
3386                                     }
3387                 if (!setlocale(LC_CTYPE, "")) {
3388                         fprintf(stderr, "Can't set the specified locale! "
3389                                         "Check LANG, LC_CTYPE, LC_ALL.\n");
3390                         return 1;
3391                 }
3392                 while
3393                 (1)
3394                 {
3395                         int c = getopt(argc,
3396                                 argv,
3397                                 getopt_string);
3398                         if (c == -1)
3399                                 break;
3400
3401                         switch
3402                         (c)
3403                         {
3404                                 case 'v':
3405                                 case 'V':
3406                                         printf
3407                                                         ("Conky "
3408                                                         VERSION
3409                                                         " compiled "
3410                                                         __DATE__
3411                                                         "\n");
3412                                         return
3413                                                         0;
3414
3415                                 case 'c':
3416                                                                                                                                                                                 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
3417                                                                                 * though free() does the NULL check itself;), then load optarg value */
3418                                         if (current_config)
3419                                                 free(current_config);
3420                                         current_config
3421                                                         =
3422                                                         strdup
3423                                                         (optarg);
3424                                         break;
3425
3426                                 case 'h':
3427                                         printf
3428                                                         ("Usage: %s [OPTION]...\n"
3429                                                         "Conky is a system monitor that renders text on desktop or to own transparent\n"
3430                                                         "window. Command line options will override configurations defined in config\n"
3431                                                         "file.\n"
3432                                                         "   -V            version\n"
3433                                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
3434                                                         "   -c FILE       config file to load instead of "
3435                                                         CONFIG_FILE
3436                                                         "\n"
3437                                                         "   -d            daemonize, fork to background\n"
3438                                                         "   -f FONT       font to use\n"
3439                                                         "   -h            help\n"
3440 #ifdef OWN_WINDOW
3441                                                         "   -o            create own window to draw\n"
3442 #endif
3443 #ifdef XDBE
3444                                                         "   -b            double buffer (prevents flickering)\n"
3445 #endif
3446                                                         "   -t TEXT       text to render, remember single quotes, like -t '$uptime'\n"
3447                                                         "   -u SECS       update interval\n"
3448                                                         "   -i NUM        number of times to update Conky\n"
3449                                                         "   -w WIN_ID     window id to draw\n"
3450                                                         "   -x X          x position\n"
3451                                                         "   -y Y          y position\n",
3452                                         argv
3453                                                         [0]);
3454                                         return
3455                                                         0;
3456
3457                                 case 'w':
3458                                         window.
3459                                                         window
3460                                                         =
3461                                                         strtol
3462                                                         (optarg,
3463                                                         0,
3464                                                         0);
3465                                         break;
3466
3467                                 case '?':
3468                                         exit(EXIT_FAILURE);
3469                         }
3470                 }
3471                 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
3472                 init_X11
3473                                 ();
3474                 
3475                 tmpstring1 = (char*)malloc(2*TEXT_BUFFER_SIZE);
3476                 tmpstring2 = (char*)malloc(2*TEXT_BUFFER_SIZE);
3477
3478                 /* load current_config or CONFIG_FILE */
3479
3480 #ifdef CONFIG_FILE
3481                                                                                                                                                                 if (current_config == NULL) {
3482         /* load default config file */
3483         char buf[256];
3484
3485         variable_substitute
3486                         (CONFIG_FILE,
3487                         buf,
3488                         256);
3489
3490         if (buf[0] != '\0')
3491                 current_config
3492                                 =
3493                                 strdup
3494                                 (buf);
3495                                                                                 }
3496 #endif
3497
3498                                                                                                                                                                 if (current_config != NULL)
3499                                                                                                                                                                         load_config_file
3500                                                                                                                                                                         (current_config);
3501                                                                                                                                                                 else
3502                                                                                 set_default_configurations
3503                                                                                 ();
3504
3505 #ifdef MAIL_FILE
3506                                                                                                                                                                 if (current_mail_spool == NULL) {
3507         char buf[256];
3508         variable_substitute
3509                         (MAIL_FILE,
3510                         buf,
3511                         256);
3512
3513         if (buf[0] != '\0')
3514                 current_mail_spool
3515                                 =
3516                                 strdup
3517                                 (buf);
3518                                                                                 }
3519 #endif
3520
3521                                                                                                                                                                 /* handle other command line arguments */
3522
3523                                                                                                                                                                 optind = 0;
3524
3525                                                                                                                                                                 while
3526                                                                                 (1)
3527                                                                                 {
3528                                                                                 int c = getopt(argc,
3529                                                                                 argv,
3530                                                                                 getopt_string);
3531                                                                                 if (c == -1)
3532                                                                                 break;
3533
3534                                                                                 switch
3535                                                                                 (c)
3536                                                                                 {
3537                                                                                 case 'a':
3538                                                                                 text_alignment
3539                                                                                 =
3540                                                                                 string_to_alignment
3541                                                                                 (optarg);
3542                                                                                 break;
3543
3544                                                                                 case 'd':
3545                                                                                 fork_to_background
3546                                                                                 =
3547                                                                                 1;
3548                                                                                 break;
3549
3550                                                                                 case 'f':
3551                                                                                 font_name
3552                                                                                 =
3553                                                                                 strdup
3554                                                                                 (optarg);
3555                                                                                 break;
3556
3557 #ifdef OWN_WINDOW
3558                                                                                 case 'o':
3559                                                                                 own_window
3560                                                                                 =
3561                                                                                 1;
3562                                                                                 break;
3563 #endif
3564 #ifdef XDBE
3565                                                                                 case 'b':
3566                                                                                 use_xdbe
3567                                                                                 =
3568                                                                                 1;
3569                                                                                 break;
3570 #endif
3571
3572                                                                                 case 't':
3573                                                                                 if (text != original_text)
3574                                                                                 free(text);
3575                                                                                 text = strdup(optarg);
3576                                                                                 convert_escapes
3577                                                                                 (text);
3578                                                                                 break;
3579
3580                                                                                 case 'u':
3581                                                                                 update_interval
3582                                                                                 =
3583                                                                                 strtod
3584                                                                                 (optarg,
3585                                                                                 0);
3586                                                                                 break;
3587                                                                                 
3588                                                                                 case 'i':
3589                                                                                 total_run_times
3590                                                                                 =
3591                                                                                 strtod
3592                                                                                 (optarg,
3593                                                                                 0);
3594                                                                                 break;
3595
3596                                                                                 case 'x':
3597                                                                                 gap_x
3598                                                                                 =
3599                                                                                 atoi
3600                                                                                 (optarg);
3601                                                                                 break;
3602
3603                                                                                 case 'y':
3604                                                                                 gap_y
3605                                                                                 =
3606                                                                                 atoi
3607                                                                                 (optarg);
3608                                                                                 break;
3609
3610                                                                                 case '?':
3611                                                                                 exit(EXIT_FAILURE);
3612                                                                                 }
3613                                                                                 }
3614
3615                                                                                 /* load font */
3616                                                                                 load_font
3617                                                                                 ();
3618
3619                                                                                 /* generate text and get initial size */
3620                                                                                 extract_variable_text
3621                                                                                 (text);
3622                                                                                 if (text != original_text)
3623                                                                                 free(text);
3624                                                                                 text = NULL;
3625
3626                                                                                 update_uname
3627                                                                                 ();
3628
3629                                                                                 generate_text
3630                                                                                 ();
3631                                                                                 update_text_area();     /* to get initial size of the window */
3632
3633                                                                                 init_window
3634                                                                                 (own_window,
3635                                                                                 text_width
3636                                                                                 +
3637                                                                                 border_margin
3638                                                                                 *
3639                                                                                 2
3640                                                                                 +
3641                                                                                 1,
3642                                                                                 text_height
3643                                                                                 +
3644                                                                                 border_margin
3645                                                                                 *
3646                                                                                 2
3647                                                                                 +
3648                                                                                 1);
3649
3650                                                                                 update_text_area();     /* to position text/window on screen */
3651
3652 #ifdef CAIRO
3653 // why the fuck not?
3654 //do_it();
3655 #endif
3656
3657 #ifdef OWN_WINDOW
3658                                                                                                                                                                 if (own_window)
3659                                                                                                                                                                         XMoveWindow
3660                                                                                                                                                                         (display,
3661                 window.
3662                                 window,
3663                 window.
3664                                 x,
3665                 window.
3666                                 y);
3667 #endif
3668
3669                                                                                                                                                                 create_gc
3670                                                                                                                                                                 ();
3671
3672                                                                                                                                                                 set_font
3673                                                                                 ();
3674
3675                                                                                 draw_stuff
3676                                                                                 ();
3677
3678                                                                                 /* fork */
3679                                                                                 if (fork_to_background) {
3680                                                                                 int ret = fork();
3681                                                                                 switch
3682                                                                                 (ret)
3683                                                                                 {
3684                                                                                 case -1:
3685                                                                                 ERR("can't fork() to background: %s", strerror(errno));
3686                                                                                 break;
3687
3688                                                                                 case 0:
3689                                                                                 break;
3690
3691                                                                                 default:
3692                                                                                 fprintf
3693                                                                                 (stderr,
3694                                                                                 "Conky: forked to background, pid is %d\n",
3695                                                                                 ret);
3696                                                                                 return 0;
3697                                                                                 }
3698                                                                                 }
3699
3700                                                                                 /* set SIGUSR1, SIGINT and SIGTERM handlers */
3701                                                                                 {
3702                                                                                 struct
3703                                                                                 sigaction
3704                                                                                 sa;
3705
3706                                                                                 sa.sa_handler = reload_handler;
3707                                                                                 sigemptyset
3708                                                                                 (&sa.
3709                                                                                 sa_mask);
3710                                                                                 sa.sa_flags = SA_RESTART;
3711                                                                                 if (sigaction(SIGUSR1, &sa, NULL) != 0)
3712                                                                                 ERR("can't set signal handler for SIGUSR1: %s", strerror(errno));
3713
3714                                                                                 sa.sa_handler = term_handler;
3715                                                                                 sigemptyset
3716                                                                                 (&sa.
3717                                                                                 sa_mask);
3718                                                                                 sa.sa_flags = SA_RESTART;
3719                                                                                 if (sigaction(SIGINT, &sa, NULL) != 0)
3720                                                                                 ERR("can't set signal handler for SIGINT: %s", strerror(errno));
3721
3722                                                                                 sa.sa_handler = term_handler;
3723                                                                                 sigemptyset
3724                                                                                 (&sa.
3725                                                                                 sa_mask);
3726                                                                                 sa.sa_flags = SA_RESTART;
3727                                                                                 if (sigaction(SIGTERM, &sa, NULL) != 0)
3728                                                                                 ERR("can't set signal handler for SIGTERM: %s", strerror(errno));
3729                                                                                 }
3730                                                                                 main_loop
3731                                                                                 ();
3732                                                                                 free(tmpstring1);
3733                                                                                 free(tmpstring2);
3734                                                                                 return
3735                                                                                 0;
3736         }