Add ARGB visual support.
[monky] / src / conky.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "text_object.h"
33 #include "conky.h"
34 #include "common.h"
35 #include "timed_thread.h"
36 #include <stdarg.h>
37 #include <math.h>
38 #include <time.h>
39 #include <locale.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <limits.h>
43 #if HAVE_DIRENT_H
44 #include <dirent.h>
45 #endif
46 #include <sys/time.h>
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
51 #ifdef X11
52 #include "x11.h"
53 #include <X11/Xutil.h>
54 #ifdef HAVE_XDAMAGE
55 #include <X11/extensions/Xdamage.h>
56 #endif
57 #ifdef IMLIB2
58 #include "imlib2.h"
59 #endif /* IMLIB2 */
60 #endif /* X11 */
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <netinet/in.h>
64 #include <netdb.h>
65 #include <fcntl.h>
66 #include <getopt.h>
67 #ifdef NCURSES
68 #include <ncurses.h>
69 #endif
70 #ifdef XOAP
71 #include <libxml/parser.h>
72 #endif /* XOAP */
73
74 /* local headers */
75 #include "core.h"
76 #include "algebra.h"
77 #include "build.h"
78 #include "colours.h"
79 #include "combine.h"
80 #include "diskio.h"
81 #include "entropy.h"
82 #include "exec.h"
83 #include "i8k.h"
84 #include "proc.h"
85 #include "user.h"
86 #ifdef X11
87 #include "fonts.h"
88 #endif
89 #include "fs.h"
90 #ifdef HAVE_ICONV
91 #include "iconv_tools.h"
92 #endif
93 #include "logging.h"
94 #include "mixer.h"
95 #include "mail.h"
96 #include "mboxscan.h"
97 #include "net_stat.h"
98 #ifdef NVIDIA
99 #include "nvidia.h"
100 #endif
101 #include "read_tcp.h"
102 #include "scroll.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
108 #include "top.h"
109
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
112 #include "linux.h"
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
114 #include "freebsd.h"
115 #elif defined(__OpenBSD__)
116 #include "openbsd.h"
117 #endif
118
119 #if defined(__FreeBSD_kernel__)
120 #include <bsd/bsd.h>
121 #endif
122
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
130 #endif
131
132 #ifdef CONFIG_OUTPUT
133 #include "defconfig.h"
134 #include "conf_cookie.h"
135 #endif
136
137 #ifndef S_ISSOCK
138 #define S_ISSOCK(x)   ((x & S_IFMT) == S_IFSOCK)
139 #endif
140
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
143
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
146
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
149
150 /* two strings for internal use */
151 static char *tmpstring1, *tmpstring2;
152
153 /* variables holding various config settings */
154 int short_units;
155 int format_human_readable;
156 int cpu_separate;
157 enum {
158         NO_SPACER = 0,
159         LEFT_SPACER,
160         RIGHT_SPACER
161 } use_spacer;
162 int top_cpu, top_mem, top_time;
163 #ifdef IOSTATS
164 int top_io;
165 #endif
166 #ifdef __linux__
167 int top_running;
168 #endif
169 int output_methods;
170 static int extra_newline;
171 enum x_initialiser_state x_initialised = NO;
172 static volatile int g_signal_pending;
173 /* Update interval */
174 double update_interval;
175 double update_interval_old;
176 double update_interval_bat;
177 void *global_cpu = NULL;
178 unsigned int max_text_width = 0;
179
180 int argc_copy;
181 char** argv_copy;
182
183 /* prototypes for internally used functions */
184 static void signal_handler(int);
185 static void print_version(void) __attribute__((noreturn));
186 static void reload_config(void);
187
188 static void print_version(void)
189 {
190         printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
191
192         printf("\nCompiled in features:\n\n"
193                    "System config file: "SYSTEM_CONFIG_FILE"\n"
194                    "Package library path: "PACKAGE_LIBDIR"\n\n"
195 #ifdef X11
196                    " X11:\n"
197 # ifdef HAVE_XDAMAGE
198                    "  * Xdamage extension\n"
199 # endif /* HAVE_XDAMAGE */
200 # ifdef HAVE_XDBE
201                    "  * XDBE (double buffer extension)\n"
202 # endif /* HAVE_XDBE */
203 # ifdef XFT
204                    "  * Xft\n"
205 # endif /* XFT */
206 # ifdef USE_ARGB
207                    "  * ARGB visual\n"
208 # endif /* USE_ARGB */
209 #endif /* X11 */
210                    "\n Music detection:\n"
211 #ifdef AUDACIOUS
212                    "  * Audacious\n"
213 #endif /* AUDACIOUS */
214 #ifdef BMPX
215                    "  * BMPx\n"
216 #endif /* BMPX */
217 #ifdef MPD
218                    "  * MPD\n"
219 #endif /* MPD */
220 #ifdef MOC
221                    "  * MOC\n"
222 #endif /* MOC */
223 #ifdef XMMS2
224                    "  * XMMS2\n"
225 #endif /* XMMS2 */
226                    "\n General:\n"
227 #ifdef HAVE_OPENMP
228                    "  * OpenMP\n"
229 #endif /* HAVE_OPENMP */
230 #ifdef MATH
231                    "  * math\n"
232 #endif /* Math */
233 #ifdef HDDTEMP
234                    "  * hddtemp\n"
235 #endif /* HDDTEMP */
236 #ifdef TCP_PORT_MONITOR
237                    "  * portmon\n"
238 #endif /* TCP_PORT_MONITOR */
239 #ifdef HAVE_CURL
240                    "  * Curl\n"
241 #endif /* HAVE_CURL */
242 #ifdef RSS
243                    "  * RSS\n"
244 #endif /* RSS */
245 #ifdef WEATHER
246                    "  * Weather (METAR)\n"
247 #ifdef XOAP
248                    "  * Weather (XOAP)\n"
249 #endif /* XOAP */
250 #endif /* WEATHER */
251 #ifdef HAVE_IWLIB
252                    "  * wireless\n"
253 #endif /* HAVE_IWLIB */
254 #ifdef IBM
255                    "  * support for IBM/Lenovo notebooks\n"
256 #endif /* IBM */
257 #ifdef NVIDIA
258                    "  * nvidia\n"
259 #endif /* NVIDIA */
260 #ifdef EVE
261                    "  * eve-online\n"
262 #endif /* EVE */
263 #ifdef CONFIG_OUTPUT
264                    "  * config-output\n"
265 #endif /* CONFIG_OUTPUT */
266 #ifdef IMLIB2
267                    "  * Imlib2\n"
268 #endif /* IMLIB2 */
269 #ifdef MIXER_IS_ALSA
270                    "  * ALSA mixer support\n"
271 #endif /* MIXER_IS_ALSA */
272 #ifdef APCUPSD
273                    "  * apcupsd\n"
274 #endif /* APCUPSD */
275 #ifdef IOSTATS
276                    "  * iostats\n"
277 #endif /* IOSTATS */
278 #ifdef NCURSES
279                    "  * ncurses\n"
280 #endif /* NCURSES */
281 #ifdef HAVE_LUA
282                    "  * Lua\n"
283                    "\n  Lua bindings:\n"
284 #ifdef HAVE_LUA_CAIRO
285                    "   * Cairo\n"
286 #endif /* HAVE_LUA_CAIRO */
287 #ifdef HAVE_LUA_IMLIB2
288                    "   * Imlib2\n"
289 #endif /* IMLIB2 */
290 #endif /* HAVE_LUA */
291         );
292
293         exit(EXIT_SUCCESS);
294 }
295
296 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
297
298
299 #ifdef X11
300
301 static void X11_create_window(void);
302 static void X11_initialisation(void);
303
304 struct _x11_stuff_s {
305         Region region;
306 #ifdef HAVE_XDAMAGE
307         Damage damage;
308         XserverRegion region2, part;
309         int event_base, error_base;
310 #endif
311 } x11_stuff;
312
313 /* text size */
314
315 static int text_start_x, text_start_y;  /* text start position in window */
316 static int text_width, text_height;
317
318 /* alignments */
319 enum alignment {
320         TOP_LEFT = 1,
321         TOP_RIGHT,
322         TOP_MIDDLE,
323         BOTTOM_LEFT,
324         BOTTOM_RIGHT,
325         BOTTOM_MIDDLE,
326         MIDDLE_LEFT,
327         MIDDLE_MIDDLE,
328         MIDDLE_RIGHT,
329         NONE
330 };
331
332 /* display to connect to */
333 static char *disp = NULL;
334
335 #endif /* X11 */
336
337 /* struct that has all info to be shared between
338  * instances of the same text object */
339 struct information info;
340
341 /* path to config file */
342 char *current_config;
343
344 /* set to 1 if you want all text to be in uppercase */
345 static unsigned int stuff_in_uppercase;
346
347 /* Run how many times? */
348 static unsigned long total_run_times;
349
350 /* fork? */
351 static int fork_to_background;
352
353 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
354
355 /* filenames for output */
356 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
357 char *append_file = NULL; FILE *append_fpointer = NULL;
358
359 #ifdef X11
360
361 static int show_graph_scale;
362 static int show_graph_range;
363
364 /* Position on the screen */
365 static int text_alignment;
366 static int gap_x, gap_y;
367
368 /* border */
369 static int draw_borders;
370 static int draw_graph_borders;
371 static int stippled_borders;
372
373 int get_stippled_borders(void)
374 {
375         return stippled_borders;
376 }
377
378 static int draw_shades, draw_outline;
379
380 long default_fg_color, default_bg_color, default_out_color;
381
382 /* create own window or draw stuff to root? */
383 static int set_transparent = 0;
384
385 #ifdef OWN_WINDOW
386 static int own_window = 0;
387 static int background_colour = 0;
388
389 /* fixed size/pos is set if wm/user changes them */
390 static int fixed_size = 0, fixed_pos = 0;
391 #endif
392
393 static int minimum_width, minimum_height;
394 static int maximum_width;
395
396 #endif /* X11 */
397
398 #ifdef __OpenBSD__
399 static int sensor_device;
400 #endif
401
402 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
403          color9;
404
405 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
406 unsigned int max_user_text;
407
408 /* maximum size of individual text buffers, ie $exec buffer size */
409 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
410
411 /* UTF-8 */
412 int utf8_mode = 0;
413
414 /* no buffers in used memory? */
415 int no_buffers;
416
417 /* pad percentages to decimals? */
418 static int pad_percents = 0;
419
420 static char *global_text = 0;
421
422 char *get_global_text(void)
423 {
424         return global_text;
425 }
426
427 long global_text_lines;
428
429 static int total_updates;
430 static int updatereset;
431
432 void set_updatereset(int i)
433 {
434         updatereset = i;
435 }
436
437 int get_updatereset(void)
438 {
439         return updatereset;
440 }
441
442 int check_contains(char *f, char *s)
443 {
444         int ret = 0;
445         FILE *where = open_file(f, 0);
446
447         if (where) {
448                 char buf1[256];
449
450                 while (fgets(buf1, 256, where)) {
451                         if (strstr(buf1, s)) {
452                                 ret = 1;
453                                 break;
454                         }
455                 }
456                 fclose(where);
457         } else {
458                 NORM_ERR("Could not open the file");
459         }
460         return ret;
461 }
462
463 #define SECRIT_MULTILINE_CHAR '\x02'
464
465 static inline int calc_text_width(const char *s)
466 {
467         size_t slen = strlen(s);
468
469 #ifdef X11
470         if ((output_methods & TO_X) == 0) {
471 #endif /* X11 */
472                 return slen;
473 #ifdef X11
474         }
475 #ifdef XFT
476         if (use_xft) {
477                 XGlyphInfo gi;
478
479                 if (utf8_mode) {
480                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
481                                 (const FcChar8 *) s, slen, &gi);
482                 } else {
483                         XftTextExtents8(display, fonts[selected_font].xftfont,
484                                 (const FcChar8 *) s, slen, &gi);
485                 }
486                 return gi.xOff;
487         } else
488 #endif
489         {
490                 return XTextWidth(fonts[selected_font].font, s, slen);
491         }
492 #endif /* X11 */
493 }
494
495 /* formatted text to render on screen, generated in generate_text(),
496  * drawn in draw_stuff() */
497
498 static char *text_buffer;
499
500 /* quite boring functions */
501
502 static inline void for_each_line(char *b, int f(char *, int))
503 {
504         char *ps, *pe;
505         int special_index = 0; /* specials index */
506
507         for (ps = b, pe = b; *pe; pe++) {
508                 if (*pe == '\n') {
509                         *pe = '\0';
510                         special_index = f(ps, special_index);
511                         *pe = '\n';
512                         ps = pe + 1;
513                 }
514         }
515
516         if (ps < pe) {
517                 f(ps, special_index);
518         }
519 }
520
521 static void convert_escapes(char *buf)
522 {
523         char *p = buf, *s = buf;
524
525         while (*s) {
526                 if (*s == '\\') {
527                         s++;
528                         if (*s == 'n') {
529                                 *p++ = '\n';
530                         } else if (*s == '\\') {
531                                 *p++ = '\\';
532                         }
533                         s++;
534                 } else {
535                         *p++ = *s++;
536                 }
537         }
538         *p = '\0';
539 }
540
541 /* Prints anything normally printed with snprintf according to the current value
542  * of use_spacer.  Actually slightly more flexible than snprintf, as you can
543  * safely specify the destination buffer as one of your inputs.  */
544 int spaced_print(char *buf, int size, const char *format, int width, ...)
545 {
546         int len = 0;
547         va_list argp;
548         char *tempbuf;
549
550         if (size < 1) {
551                 return 0;
552         }
553         tempbuf = malloc(size * sizeof(char));
554
555         // Passes the varargs along to vsnprintf
556         va_start(argp, width);
557         vsnprintf(tempbuf, size, format, argp);
558         va_end(argp);
559
560         switch (use_spacer) {
561                 case NO_SPACER:
562                         len = snprintf(buf, size, "%s", tempbuf);
563                         break;
564                 case LEFT_SPACER:
565                         len = snprintf(buf, size, "%*s", width, tempbuf);
566                         break;
567                 case RIGHT_SPACER:
568                         len = snprintf(buf, size, "%-*s", width, tempbuf);
569                         break;
570         }
571         free(tempbuf);
572         return len;
573 }
574
575 /* print percentage values
576  *
577  * - i.e., unsigned values between 0 and 100
578  * - respect the value of pad_percents */
579 int percent_print(char *buf, int size, unsigned value)
580 {
581         return spaced_print(buf, size, "%u", pad_percents, value);
582 }
583
584 /* converts from bytes to human readable format (K, M, G, T)
585  *
586  * The algorithm always divides by 1024, as unit-conversion of byte
587  * counts suggests. But for output length determination we need to
588  * compare with 1000 here, as we print in decimal form. */
589 void human_readable(long long num, char *buf, int size)
590 {
591         const char **suffix = suffixes;
592         float fnum;
593         int precision;
594         int width;
595         const char *format;
596
597         /* Possibly just output as usual, for example for stdout usage */
598         if (!format_human_readable) {
599                 spaced_print(buf, size, "%d", 6, round_to_int(num));
600                 return;
601         }
602         if (short_units) {
603                 width = 5;
604                 format = "%.*f%.1s";
605         } else {
606                 width = 7;
607                 format = "%.*f%-3s";
608         }
609
610         if (llabs(num) < 1000LL) {
611                 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
612                 return;
613         }
614
615         while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
616                 num /= 1024;
617                 suffix++;
618         }
619
620         suffix++;
621         fnum = num / 1024.0;
622
623         /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
624          *
625          * The goal is to always have a significance of 3, by
626          * adjusting the decimal part of the number. Sample output:
627          *  123MiB
628          * 23.4GiB
629          * 5.12B   
630          * so the point of alignment resides between number and unit. The
631          * upside of this is that there is minimal padding necessary, though
632          * there should be a way to make alignment take place at the decimal
633          * dot (then with fixed width decimal part). 
634          *
635          * Note the repdigits below: when given a precision value, printf()
636          * rounds the float to it, not just cuts off the remaining digits. So
637          * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
638          * printed with a precision of 0. Yay. */
639
640         precision = 0;          /* print 100-999 without decimal part */
641         if (fnum < 99.95)
642                 precision = 1;  /* print 10-99 with one decimal place */
643         if (fnum < 9.995)
644                 precision = 2;  /* print 0-9 with two decimal places */
645
646         spaced_print(buf, size, format, width, precision, fnum, *suffix);
647 }
648
649 /* global object list root element */
650 static struct text_object global_root_object;
651
652 static long current_text_color;
653
654 void set_current_text_color(long colour)
655 {
656         current_text_color = colour;
657 }
658
659 long get_current_text_color(void)
660 {
661         return current_text_color;
662 }
663
664 //adds newstring to to the tree unless you can already see it when travelling back.
665 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
666 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
667         struct conftree* node;
668         struct conftree* node2;
669
670         for(node = previous; node != NULL; node = node->back) {
671                 if(strcmp(node->string, newstring) == 0) {
672                         return NULL;
673                 }
674         }
675         node = malloc(sizeof(struct conftree));
676         if (previous != NULL) {
677                 if(previous->vert_next == NULL) {
678                         previous->vert_next = node;
679                 } else {
680                         for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
681                         node2->horz_next = node;
682                 }
683         }
684         node->string = strdup(newstring);
685         node->horz_next = NULL;
686         node->vert_next = NULL;
687         node->back = previous;
688         return node;
689 }
690
691 void conftree_empty(struct conftree* tree) {
692         if(tree) {
693                 conftree_empty(tree->horz_next);
694                 conftree_empty(tree->vert_next);
695                 free(tree->string);
696                 free(tree);
697         }
698 }
699
700 struct conftree *currentconffile;
701
702 static void extract_variable_text(const char *p)
703 {
704         free_text_objects(&global_root_object, 0);
705         if (tmpstring1) {
706                 free(tmpstring1);
707                 tmpstring1 = 0;
708         }
709         if (tmpstring2) {
710                 free(tmpstring2);
711                 tmpstring2 = 0;
712         }
713         if (text_buffer) {
714                 free(text_buffer);
715                 text_buffer = 0;
716         }
717
718         extract_variable_text_internal(&global_root_object, p);
719 }
720
721 void parse_conky_vars(struct text_object *root, const char *txt,
722                 char *p, int p_max_size, struct information *cur)
723 {
724         extract_variable_text_internal(root, txt);
725         generate_text_internal(p, p_max_size, *root, cur);
726 }
727
728 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
729  * multiline objects like $exec work with $align[rc] and friends
730  */
731 void substitute_newlines(char *p, long l)
732 {
733         char *s = p;
734         if (l < 0) return;
735         while (p && *p && p < s + l) {
736                 if (*p == '\n') {
737                         /* only substitute if it's not the last newline */
738                         *p = SECRIT_MULTILINE_CHAR;
739                 }
740                 p++;
741         }
742 }
743
744 void generate_text_internal(char *p, int p_max_size,
745                 struct text_object root, struct information *cur)
746 {
747         struct text_object *obj;
748 #ifdef X11
749         int need_to_load_fonts = 0;
750 #endif /* X11 */
751
752         /* for the OBJ_top* handler */
753         struct process **needed = 0;
754
755 #ifdef HAVE_ICONV
756         char buff_in[p_max_size];
757         buff_in[0] = 0;
758 #endif /* HAVE_ICONV */
759
760         p[0] = 0;
761         obj = root.next;
762         while (obj && p_max_size > 0) {
763                 needed = 0; /* reset for top stuff */
764
765 /* IFBLOCK jumping algorithm
766  *
767  * This is easier as it looks like:
768  * - each IF checks it's condition
769  *   - on FALSE: call DO_JUMP
770  *   - on TRUE: don't care
771  * - each ELSE calls DO_JUMP unconditionally
772  * - each ENDIF is silently being ignored
773  *
774  * Why this works:
775  * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
776  * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
777  * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
778  * regularly, "obj" is being updated to point to obj->next, so object parsing
779  * continues right after the corresponding ELSE or ENDIF. This means that if we
780  * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
781  * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
782  * jumped, and there is nothing to do.
783  */
784 #define DO_JUMP { \
785         DBGP2("jumping"); \
786         if (obj->ifblock_next) \
787                 obj = obj->ifblock_next; \
788 }
789
790 #define OBJ(a) break; case OBJ_##a:
791
792                 switch (obj->type) {
793                         default:
794                                 NORM_ERR("not implemented obj type %d", obj->type);
795                         OBJ(read_tcp) {
796                                 print_read_tcp(obj, p, p_max_size);
797                         }
798 #ifndef __OpenBSD__
799                         OBJ(acpitemp) {
800                                 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
801                         }
802 #endif /* !__OpenBSD__ */
803                         OBJ(freq) {
804                                 static int ok = 1;
805                                 if (ok) {
806                                         ok = get_freq(p, p_max_size, "%.0f", 1,
807                                                         obj->data.i);
808                                 }
809                         }
810                         OBJ(freq_g) {
811                                 static int ok = 1;
812                                 if (ok) {
813 #ifndef __OpenBSD__
814                                         ok = get_freq(p, p_max_size, "%'.2f", 1000,
815                                                         obj->data.i);
816 #else
817                                         /* OpenBSD has no such flag (SUSv2) */
818                                         ok = get_freq(p, p_max_size, "%.2f", 1000,
819                                                         obj->data.i);
820 #endif /* __OpenBSD */
821                                 }
822                         }
823 #if defined(__linux__)
824                         OBJ(voltage_mv) {
825                                 print_voltage_mv(obj, p, p_max_size);
826                         }
827                         OBJ(voltage_v) {
828                                 print_voltage_v(obj, p, p_max_size);
829                         }
830
831 #ifdef HAVE_IWLIB
832                         OBJ(wireless_essid) {
833                                 print_wireless_essid(obj, p, p_max_size);
834                         }
835                         OBJ(wireless_mode) {
836                                 print_wireless_mode(obj, p, p_max_size);
837                         }
838                         OBJ(wireless_bitrate) {
839                                 print_wireless_bitrate(obj, p, p_max_size);
840                         }
841                         OBJ(wireless_ap) {
842                                 print_wireless_ap(obj, p, p_max_size);
843                         }
844                         OBJ(wireless_link_qual) {
845                                 print_wireless_link_qual(obj, p, p_max_size);
846                         }
847                         OBJ(wireless_link_qual_max) {
848                                 print_wireless_link_qual_max(obj, p, p_max_size);
849                         }
850                         OBJ(wireless_link_qual_perc) {
851                                 print_wireless_link_qual_perc(obj, p, p_max_size);
852                         }
853                         OBJ(wireless_link_bar) {
854                                 print_wireless_link_bar(obj, p, p_max_size);
855                         }
856 #endif /* HAVE_IWLIB */
857
858 #endif /* __linux__ */
859
860 #ifndef __OpenBSD__
861                         OBJ(acpifan) {
862                                 get_acpi_fan(p, p_max_size);
863                         }
864                         OBJ(acpiacadapter) {
865                                 get_acpi_ac_adapter(p, p_max_size);
866                         }
867                         OBJ(battery) {
868                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
869                         }
870                         OBJ(battery_time) {
871                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
872                         }
873                         OBJ(battery_percent) {
874                                 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
875                         }
876                         OBJ(battery_bar) {
877                                 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
878                         }
879                         OBJ(battery_short) {
880                                 get_battery_short_status(p, p_max_size, obj->data.s);
881                         }
882 #endif /* __OpenBSD__ */
883
884                         OBJ(buffers) {
885                                 human_readable(cur->buffers * 1024, p, 255);
886                         }
887                         OBJ(cached) {
888                                 human_readable(cur->cached * 1024, p, 255);
889                         }
890                         OBJ(cmdline_to_pid) {
891                                 print_cmdline_to_pid(obj, p, p_max_size);
892                         }
893                         OBJ(cpu) {
894                                 if (obj->data.i > info.cpu_count) {
895                                         NORM_ERR("obj->data.i %i info.cpu_count %i",
896                                                         obj->data.i, info.cpu_count);
897                                         CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
898                                 }
899                                 percent_print(p, p_max_size,
900                                               round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
901                         }
902                         OBJ(cpugauge)
903                                 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
904                         OBJ(cpubar) {
905                                 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
906                         }
907 #ifdef X11
908                         OBJ(cpugraph) {
909                                 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
910                         }
911                         OBJ(loadgraph) {
912                                 print_loadgraph(obj, p, p_max_size);
913                         }
914 #endif /* X11 */
915                         OBJ(color) {
916                                 new_fg(p, obj->data.l);
917                         }
918 #ifdef X11
919                         OBJ(color0) {
920                                 new_fg(p, color0);
921                         }
922                         OBJ(color1) {
923                                 new_fg(p, color1);
924                         }
925                         OBJ(color2) {
926                                 new_fg(p, color2);
927                         }
928                         OBJ(color3) {
929                                 new_fg(p, color3);
930                         }
931                         OBJ(color4) {
932                                 new_fg(p, color4);
933                         }
934                         OBJ(color5) {
935                                 new_fg(p, color5);
936                         }
937                         OBJ(color6) {
938                                 new_fg(p, color6);
939                         }
940                         OBJ(color7) {
941                                 new_fg(p, color7);
942                         }
943                         OBJ(color8) {
944                                 new_fg(p, color8);
945                         }
946                         OBJ(color9) {
947                                 new_fg(p, color9);
948                         }
949 #endif /* X11 */
950 #if defined(__linux__)
951                         OBJ(disk_protect) {
952                                 snprintf(p, p_max_size, "%s",
953                                                 get_disk_protect_queue(obj->data.s));
954                         }
955                         OBJ(i8k_version) {
956                                 print_i8k_version(obj, p, p_max_size);
957                         }
958                         OBJ(i8k_bios) {
959                                 print_i8k_bios(obj, p, p_max_size);
960                         }
961                         OBJ(i8k_serial) {
962                                 print_i8k_serial(obj, p, p_max_size);
963                         }
964                         OBJ(i8k_cpu_temp) {
965                                 print_i8k_cpu_temp(obj, p, p_max_size);
966                         }
967                         OBJ(i8k_left_fan_status) {
968                                 print_i8k_left_fan_status(obj, p, p_max_size);
969                         }
970                         OBJ(i8k_right_fan_status) {
971                                 print_i8k_right_fan_status(obj, p, p_max_size);
972                         }
973                         OBJ(i8k_left_fan_rpm) {
974                                 print_i8k_left_fan_rpm(obj, p, p_max_size);
975                         }
976                         OBJ(i8k_right_fan_rpm) {
977                                 print_i8k_right_fan_rpm(obj, p, p_max_size);
978                         }
979                         OBJ(i8k_ac_status) {
980                                 print_i8k_ac_status(obj, p, p_max_size);
981                         }
982                         OBJ(i8k_buttons_status) {
983                                 print_i8k_buttons_status(obj, p, p_max_size);
984                         }
985 #if defined(IBM)
986                         OBJ(ibm_fan) {
987                                 get_ibm_acpi_fan(obj, p, p_max_size);
988                         }
989                         OBJ(ibm_temps) {
990                                 print_ibm_temps(obj, p, p_max_size);
991                         }
992                         OBJ(ibm_volume) {
993                                 get_ibm_acpi_volume(obj, p, p_max_size);
994                         }
995                         OBJ(ibm_brightness) {
996                                 get_ibm_acpi_brightness(obj, p, p_max_size);
997                         }
998 #endif /* IBM */
999                         /* information from sony_laptop kernel module
1000                          * /sys/devices/platform/sony-laptop */
1001                         OBJ(sony_fanspeed) {
1002                                 get_sony_fanspeed(p, p_max_size);
1003                         }
1004                         OBJ(if_gw) {
1005                                 if (!gateway_exists()) {
1006                                         DO_JUMP;
1007                                 }
1008                         }
1009                         OBJ(gw_iface) {
1010                                 print_gateway_iface(p, p_max_size);
1011                         }
1012                         OBJ(gw_ip) {
1013                                 print_gateway_ip(p, p_max_size);
1014                         }
1015                         OBJ(laptop_mode) {
1016                                 snprintf(p, p_max_size, "%d", get_laptop_mode());
1017                         }
1018                         OBJ(pb_battery) {
1019                                 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1020                         }
1021 #endif /* __linux__ */
1022 #if (defined(__FreeBSD__) || defined(__linux__))
1023                         OBJ(if_up) {
1024                                 if (!interface_up(obj)) {
1025                                         DO_JUMP;
1026                                 }
1027                         }
1028 #endif
1029 #ifdef __OpenBSD__
1030                         OBJ(obsd_sensors_temp) {
1031                                 print_obsd_sensors_temp(obj, p, p_max_size);
1032                         }
1033                         OBJ(obsd_sensors_fan) {
1034                                 print_obsd_sensors_fan(obj, p, p_max_size);
1035                         }
1036                         OBJ(obsd_sensors_volt) {
1037                                 print_obsd_sensors_volt(obj, p, p_max_size);
1038                         }
1039                         OBJ(obsd_vendor) {
1040                                 get_obsd_vendor(p, p_max_size);
1041                         }
1042                         OBJ(obsd_product) {
1043                                 get_obsd_product(p, p_max_size);
1044                         }
1045 #endif /* __OpenBSD__ */
1046 #ifdef X11
1047                         OBJ(font) {
1048                                 new_font(p, obj->data.s);
1049                                 need_to_load_fonts = 1;
1050                         }
1051 #endif /* X11 */
1052                         OBJ(diskio) {
1053                                 print_diskio(obj, p, p_max_size);
1054                         }
1055                         OBJ(diskio_write) {
1056                                 print_diskio_write(obj, p, p_max_size);
1057                         }
1058                         OBJ(diskio_read) {
1059                                 print_diskio_read(obj, p, p_max_size);
1060                         }
1061 #ifdef X11
1062                         OBJ(diskiograph) {
1063                                 print_diskiograph(obj, p, p_max_size);
1064                         }
1065                         OBJ(diskiograph_read) {
1066                                 print_diskiograph(obj, p, p_max_size);
1067                         }
1068                         OBJ(diskiograph_write) {
1069                                 print_diskiograph(obj, p, p_max_size);
1070                         }
1071 #endif /* X11 */
1072                         OBJ(downspeed) {
1073                                 print_downspeed(obj, p, p_max_size);
1074                         }
1075                         OBJ(downspeedf) {
1076                                 print_downspeedf(obj, p, p_max_size);
1077                         }
1078 #ifdef X11
1079                         OBJ(downspeedgraph) {
1080                                 print_downspeedgraph(obj, p, p_max_size);
1081                         }
1082 #endif /* X11 */
1083                         OBJ(else) {
1084                                 /* Since we see you, you're if has not jumped.
1085                                  * Do Ninja jump here: without leaving traces.
1086                                  * This is to prevent us from stale jumped flags.
1087                                  */
1088                                 obj = obj->ifblock_next;
1089                                 continue;
1090                         }
1091                         OBJ(endif) {
1092                                 /* harmless object, just ignore */
1093                         }
1094                         OBJ(addr) {
1095                                 print_addr(obj, p, p_max_size);
1096                         }
1097 #if defined(__linux__)
1098                         OBJ(addrs) {
1099                                 print_addrs(obj, p, p_max_size);
1100                         }
1101 #endif /* __linux__ */
1102 #if defined(IMLIB2) && defined(X11)
1103                         OBJ(image) {
1104                                 /* doesn't actually draw anything, just queues it omp.  the
1105                                  * image will get drawn after the X event loop */
1106                                 cimlib_add_image(obj->data.s);
1107                         }
1108 #endif /* IMLIB2 */
1109                         OBJ(eval) {
1110                                 evaluate(obj->data.s, p, p_max_size);
1111                         }
1112                         OBJ(exec) {
1113                                 print_exec(obj, p, p_max_size);
1114                         }
1115                         OBJ(execp) {
1116                                 print_execp(obj, p, p_max_size);
1117                         }
1118                         OBJ(execgauge) {
1119                                 print_execgauge(obj, p, p_max_size);
1120                         }
1121                         OBJ(execbar) {
1122                                 print_execbar(obj, p, p_max_size);
1123                         }
1124 #ifdef X11
1125                         OBJ(execgraph) {
1126                                 print_execgraph(obj, p, p_max_size);
1127                         }
1128 #endif /* X11 */
1129                         OBJ(execibar) {
1130                                 print_execibar(obj, p, p_max_size);
1131                         }
1132 #ifdef X11
1133                         OBJ(execigraph) {
1134                                 print_execigraph(obj, p, p_max_size);
1135                         }
1136 #endif /* X11 */
1137                         OBJ(execigauge) {
1138                                 print_execigauge(obj, p, p_max_size);
1139                         }
1140                         OBJ(execi) {
1141                                 print_execi(obj, p, p_max_size);
1142                         }
1143                         OBJ(execpi) {
1144                                 print_execpi(obj, p, p_max_size);
1145                         }
1146                         OBJ(texeci) {
1147                                 print_texeci(obj, p, p_max_size);
1148                         }
1149                         OBJ(imap_unseen) {
1150                                 print_imap_unseen(obj, p, p_max_size);
1151                         }
1152                         OBJ(imap_messages) {
1153                                 print_imap_messages(obj, p, p_max_size);
1154                         }
1155                         OBJ(pop3_unseen) {
1156                                 print_pop3_unseen(obj, p, p_max_size);
1157                         }
1158                         OBJ(pop3_used) {
1159                                 print_pop3_used(obj, p, p_max_size);
1160                         }
1161                         OBJ(fs_bar) {
1162                                 print_fs_bar(obj, 0, p, p_max_size);
1163                         }
1164                         OBJ(fs_free) {
1165                                 print_fs_free(obj, p, p_max_size);
1166                         }
1167                         OBJ(fs_free_perc) {
1168                                 print_fs_perc(obj, 1, p, p_max_size);
1169                         }
1170                         OBJ(fs_size) {
1171                                 print_fs_size(obj, p, p_max_size);
1172                         }
1173                         OBJ(fs_type) {
1174                                 print_fs_type(obj, p, p_max_size);
1175                         }
1176                         OBJ(fs_used) {
1177                                 print_fs_used(obj, p, p_max_size);
1178                         }
1179                         OBJ(fs_bar_free) {
1180                                 print_fs_bar(obj, 1, p, p_max_size);
1181                         }
1182                         OBJ(fs_used_perc) {
1183                                 print_fs_perc(obj, 0, p, p_max_size);
1184                         }
1185                         OBJ(loadavg) {
1186                                 print_loadavg(obj, p, p_max_size);
1187                         }
1188                         OBJ(goto) {
1189                                 new_goto(p, obj->data.i);
1190                         }
1191                         OBJ(tab) {
1192                                 new_tab(obj, p);
1193                         }
1194 #ifdef X11
1195                         OBJ(hr) {
1196                                 new_hr(p, obj->data.i);
1197                         }
1198 #endif
1199                         OBJ(nameserver) {
1200                                 print_nameserver(obj, p, p_max_size);
1201                         }
1202 #ifdef EVE
1203                         OBJ(eve) {
1204                                 print_eve(obj, p, p_max_size);
1205                         }
1206 #endif
1207 #ifdef HAVE_CURL
1208                         OBJ(curl) {
1209                                 curl_print(obj, p, p_max_size);
1210                         }
1211 #endif
1212 #ifdef RSS
1213                         OBJ(rss) {
1214                                 rss_print_info(obj, p, p_max_size);
1215                         }
1216 #endif
1217 #ifdef WEATHER
1218                         OBJ(weather) {
1219                                 print_weather(obj, p, p_max_size);
1220                         }
1221 #endif
1222 #ifdef XOAP
1223                         OBJ(weather_forecast) {
1224                                 print_weather_forecast(obj, p, p_max_size);
1225                         }
1226 #endif
1227 #ifdef HAVE_LUA
1228                         OBJ(lua) {
1229                                 print_lua(obj, p, p_max_size);
1230                         }
1231                         OBJ(lua_parse) {
1232                                 print_lua_parse(obj, p, p_max_size);
1233                         }
1234                         OBJ(lua_bar) {
1235                                 print_lua_bar(obj, p, p_max_size);
1236                         }
1237 #ifdef X11
1238                         OBJ(lua_graph) {
1239                                 print_lua_graph(obj, p, p_max_size);
1240                         }
1241 #endif /* X11 */
1242                         OBJ(lua_gauge) {
1243                                 print_lua_gauge(obj, p, p_max_size);
1244                         }
1245 #endif /* HAVE_LUA */
1246 #ifdef HDDTEMP
1247                         OBJ(hddtemp) {
1248                                 short val;
1249                                 char unit;
1250
1251                                 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1252                                         snprintf(p, p_max_size, "N/A");
1253                                 } else {
1254                                         temp_print(p, p_max_size, (double)val,
1255                                                         (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1256                                 }
1257                         }
1258 #endif
1259                         OBJ(offset) {
1260                                 new_offset(p, obj->data.i);
1261                         }
1262                         OBJ(voffset) {
1263                                 new_voffset(p, obj->data.i);
1264                         }
1265 #ifdef __linux__
1266                         OBJ(i2c) {
1267                                 print_sysfs_sensor(obj, p, p_max_size);
1268                         }
1269                         OBJ(platform) {
1270                                 print_sysfs_sensor(obj, p, p_max_size);
1271                         }
1272                         OBJ(hwmon) {
1273                                 print_sysfs_sensor(obj, p, p_max_size);
1274                         }
1275 #endif /* __linux__ */
1276                         OBJ(alignr) {
1277                                 new_alignr(p, obj->data.i);
1278                         }
1279                         OBJ(alignc) {
1280                                 new_alignc(p, obj->data.i);
1281                         }
1282                         OBJ(if_empty) {
1283                                 char buf[max_user_text];
1284                                 struct information *tmp_info =
1285                                         malloc(sizeof(struct information));
1286                                 memcpy(tmp_info, cur, sizeof(struct information));
1287                                 generate_text_internal(buf, max_user_text,
1288                                                        *obj->sub, tmp_info);
1289
1290                                 if (strlen(buf) != 0) {
1291                                         DO_JUMP;
1292                                 }
1293                                 free(tmp_info);
1294                         }
1295                         OBJ(if_match) {
1296                                 char expression[max_user_text];
1297                                 int val;
1298                                 struct information *tmp_info;
1299
1300                                 tmp_info = malloc(sizeof(struct information));
1301                                 memcpy(tmp_info, cur, sizeof(struct information));
1302                                 generate_text_internal(expression, max_user_text,
1303                                                        *obj->sub, tmp_info);
1304                                 DBGP("parsed arg into '%s'", expression);
1305
1306                                 val = compare(expression);
1307                                 if (val == -2) {
1308                                         NORM_ERR("compare failed for expression '%s'",
1309                                                         expression);
1310                                 } else if (!val) {
1311                                         DO_JUMP;
1312                                 }
1313                                 free(tmp_info);
1314                         }
1315                         OBJ(if_existing) {
1316                                 char *spc;
1317
1318                                 spc = strchr(obj->data.s, ' ');
1319                                 if (!spc && access(obj->data.s, F_OK)) {
1320                                         DO_JUMP;
1321                                 } else if (spc) {
1322                                         *spc = '\0';
1323                                         if (check_contains(obj->data.s, spc + 1))
1324                                                 DO_JUMP;
1325                                         *spc = ' ';
1326                                 }
1327                         }
1328                         OBJ(if_mounted) {
1329                                 if ((obj->data.s)
1330                                                 && (!check_mount(obj->data.s))) {
1331                                         DO_JUMP;
1332                                 }
1333                         }
1334                         OBJ(if_running) {
1335 #ifdef __linux__
1336                                 if (!get_process_by_name(obj->data.s)) {
1337 #else
1338                                 if ((obj->data.s) && system(obj->data.s)) {
1339 #endif
1340                                         DO_JUMP;
1341                                 }
1342                         }
1343 #if defined(__linux__)
1344                         OBJ(ioscheduler) {
1345                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1346                         }
1347 #endif
1348                         OBJ(kernel) {
1349                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1350                         }
1351                         OBJ(machine) {
1352                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1353                         }
1354
1355                         /* memory stuff */
1356                         OBJ(mem) {
1357                                 human_readable(cur->mem * 1024, p, 255);
1358                         }
1359                         OBJ(memeasyfree) {
1360                                 human_readable(cur->memeasyfree * 1024, p, 255);
1361                         }
1362                         OBJ(memfree) {
1363                                 human_readable(cur->memfree * 1024, p, 255);
1364                         }
1365                         OBJ(memmax) {
1366                                 human_readable(cur->memmax * 1024, p, 255);
1367                         }
1368                         OBJ(memperc) {
1369                                 if (cur->memmax)
1370                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1371                         }
1372                         OBJ(memgauge){
1373                                 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1374                         }
1375                         OBJ(membar) {
1376                                 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1377                         }
1378 #ifdef X11
1379                         OBJ(memgraph) {
1380                                 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1381                         }
1382 #endif /* X11 */
1383                         /* mixer stuff */
1384                         OBJ(mixer) {
1385                                 print_mixer(obj, p, p_max_size);
1386                         }
1387                         OBJ(mixerl) {
1388                                 print_mixerl(obj, p, p_max_size);
1389                         }
1390                         OBJ(mixerr) {
1391                                 print_mixerr(obj, p, p_max_size);
1392                         }
1393                         OBJ(mixerbar) {
1394                                 print_mixer_bar(obj, p, p_max_size);
1395                         }
1396                         OBJ(mixerlbar) {
1397                                 print_mixerl_bar(obj, p, p_max_size);
1398                         }
1399                         OBJ(mixerrbar) {
1400                                 print_mixerr_bar(obj, p, p_max_size);
1401                         }
1402                         OBJ(if_mixer_mute) {
1403                                 if (!check_mixer_muted(obj)) {
1404                                         DO_JUMP;
1405                                 }
1406                         }
1407 #ifdef X11
1408 #define NOT_IN_X "Not running in X"
1409                         OBJ(monitor) {
1410                                 if(x_initialised != YES) {
1411                                         strncpy(p, NOT_IN_X, p_max_size);
1412                                 }else{
1413                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1414                                 }
1415                         }
1416                         OBJ(monitor_number) {
1417                                 if(x_initialised != YES) {
1418                                         strncpy(p, NOT_IN_X, p_max_size);
1419                                 }else{
1420                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1421                                 }
1422                         }
1423                         OBJ(desktop) {
1424                                 if(x_initialised != YES) {
1425                                         strncpy(p, NOT_IN_X, p_max_size);
1426                                 }else{
1427                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1428                                 }
1429                         }
1430                         OBJ(desktop_number) {
1431                                 if(x_initialised != YES) {
1432                                         strncpy(p, NOT_IN_X, p_max_size);
1433                                 }else{
1434                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1435                                 }
1436                         }
1437                         OBJ(desktop_name) {
1438                                 if(x_initialised != YES) {
1439                                         strncpy(p, NOT_IN_X, p_max_size);
1440                                 }else if(cur->x11.desktop.name != NULL) {
1441                                         strncpy(p, cur->x11.desktop.name, p_max_size);
1442                                 }
1443                         }
1444 #endif /* X11 */
1445
1446                         OBJ(format_time) {
1447                                 char buf[max_user_text];
1448
1449                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1450                                 obj->data.s = buf;
1451                                 print_format_time(obj, p, p_max_size);
1452                         }
1453                         /* mail stuff */
1454                         OBJ(mails) {
1455                                 print_mails(obj, p, p_max_size);
1456                         }
1457                         OBJ(new_mails) {
1458                                 print_new_mails(obj, p, p_max_size);
1459                         }
1460                         OBJ(seen_mails) {
1461                                 print_seen_mails(obj, p, p_max_size);
1462                         }
1463                         OBJ(unseen_mails) {
1464                                 print_unseen_mails(obj, p, p_max_size);
1465                         }
1466                         OBJ(flagged_mails) {
1467                                 print_flagged_mails(obj, p, p_max_size);
1468                         }
1469                         OBJ(unflagged_mails) {
1470                                 print_unflagged_mails(obj, p, p_max_size);
1471                         }
1472                         OBJ(forwarded_mails) {
1473                                 print_forwarded_mails(obj, p, p_max_size);
1474                         }
1475                         OBJ(unforwarded_mails) {
1476                                 print_unforwarded_mails(obj, p, p_max_size);
1477                         }
1478                         OBJ(replied_mails) {
1479                                 print_replied_mails(obj, p, p_max_size);
1480                         }
1481                         OBJ(unreplied_mails) {
1482                                 print_unreplied_mails(obj, p, p_max_size);
1483                         }
1484                         OBJ(draft_mails) {
1485                                 print_draft_mails(obj, p, p_max_size);
1486                         }
1487                         OBJ(trashed_mails) {
1488                                 print_trashed_mails(obj, p, p_max_size);
1489                         }
1490                         OBJ(mboxscan) {
1491                                 print_mboxscan(obj, p, p_max_size);
1492                         }
1493                         OBJ(nodename) {
1494                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1495                         }
1496                         OBJ(outlinecolor) {
1497                                 new_outline(p, obj->data.l);
1498                         }
1499                         OBJ(pid_chroot) {
1500                                 char buf[max_user_text];
1501
1502                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1503                                 obj->data.s = buf;
1504                                 print_pid_chroot(obj, p, p_max_size);
1505                         }
1506                         OBJ(pid_cmdline) {
1507                                 char buf[max_user_text];
1508
1509                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1510                                 obj->data.s = buf;
1511                                 print_pid_cmdline(obj, p, p_max_size);
1512                         }
1513                         OBJ(pid_cwd) {
1514                                 char buf[max_user_text];
1515
1516                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1517                                 obj->data.s = buf;
1518                                 print_pid_cwd(obj, p, p_max_size);
1519                         }
1520                         OBJ(pid_environ) {
1521                                 char buf[max_user_text];
1522
1523                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1524                                 obj->data.s = buf;
1525                                 print_pid_environ(obj, p, p_max_size);
1526                         }
1527                         OBJ(pid_environ_list) {
1528                                 char buf[max_user_text];
1529
1530                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1531                                 obj->data.s = buf;
1532                                 print_pid_environ_list(obj, p, p_max_size);
1533                         }
1534                         OBJ(pid_exe) {
1535                                 char buf[max_user_text];
1536
1537                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1538                                 obj->data.s = buf;
1539                                 print_pid_exe(obj, p, p_max_size);
1540                         }
1541                         OBJ(pid_nice) {
1542                                 char buf[max_user_text];
1543
1544                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1545                                 obj->data.s = buf;
1546                                 print_pid_nice(obj, p, p_max_size);
1547                         }
1548                         OBJ(pid_openfiles) {
1549                                 char buf[max_user_text];
1550
1551                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1552                                 obj->data.s = buf;
1553                                 print_pid_openfiles(obj, p, p_max_size);
1554                         }
1555                         OBJ(pid_parent) {
1556                                 char buf[max_user_text];
1557
1558                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1559                                 obj->data.s = buf;
1560                                 print_pid_parent(obj, p, p_max_size);
1561                         }
1562                         OBJ(pid_priority) {
1563                                 char buf[max_user_text];
1564
1565                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1566                                 obj->data.s = buf;
1567                                 print_pid_priority(obj, p, p_max_size);
1568                         }
1569                         OBJ(pid_state) {
1570                                 char buf[max_user_text];
1571
1572                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1573                                 obj->data.s = buf;
1574                                 print_pid_state(obj, p, p_max_size);
1575                         }
1576                         OBJ(pid_state_short) {
1577                                 char buf[max_user_text];
1578
1579                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1580                                 obj->data.s = buf;
1581                                 print_pid_state_short(obj, p, p_max_size);
1582                         }
1583                         OBJ(pid_stderr) {
1584                                 char buf[max_user_text];
1585
1586                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1587                                 obj->data.s = buf;
1588                                 print_pid_stderr(obj, p, p_max_size);
1589                         }
1590                         OBJ(pid_stdin) {
1591                                 char buf[max_user_text];
1592
1593                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1594                                 obj->data.s = buf;
1595                                 print_pid_stdin(obj, p, p_max_size);
1596                         }
1597                         OBJ(pid_stdout) {
1598                                 char buf[max_user_text];
1599
1600                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1601                                 obj->data.s = buf;
1602                                 print_pid_stdout(obj, p, p_max_size);
1603                         }
1604                         OBJ(pid_threads) {
1605                                 char buf[max_user_text];
1606
1607                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1608                                 obj->data.s = buf;
1609                                 print_pid_threads(obj, p, p_max_size);
1610                         }
1611                         OBJ(pid_thread_list) {
1612                                 char buf[max_user_text];
1613
1614                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1615                                 obj->data.s = buf;
1616                                 print_pid_thread_list(obj, p, p_max_size);
1617                         }
1618                         OBJ(pid_time_kernelmode) {
1619                                 char buf[max_user_text];
1620
1621                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1622                                 obj->data.s = buf;
1623                                 print_pid_time_kernelmode(obj, p, p_max_size);
1624                         }
1625                         OBJ(pid_time_usermode) {
1626                                 char buf[max_user_text];
1627
1628                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1629                                 obj->data.s = buf;
1630                                 print_pid_time_usermode(obj, p, p_max_size);
1631                         }
1632                         OBJ(pid_time) {
1633                                 char buf[max_user_text];
1634
1635                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1636                                 obj->data.s = buf;
1637                                 print_pid_time(obj, p, p_max_size);
1638                         }
1639                         OBJ(pid_uid) {
1640                                 char buf[max_user_text];
1641
1642                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1643                                 obj->data.s = buf;
1644                                 print_pid_uid(obj, p, p_max_size);
1645                         }
1646                         OBJ(pid_euid) {
1647                                 char buf[max_user_text];
1648
1649                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1650                                 obj->data.s = buf;
1651                                 print_pid_euid(obj, p, p_max_size);
1652                         }
1653                         OBJ(pid_suid) {
1654                                 char buf[max_user_text];
1655
1656                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1657                                 obj->data.s = buf;
1658                                 print_pid_suid(obj, p, p_max_size);
1659                         }
1660                         OBJ(pid_fsuid) {
1661                                 char buf[max_user_text];
1662
1663                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1664                                 obj->data.s = buf;
1665                                 print_pid_fsuid(obj, p, p_max_size);
1666                         }
1667                         OBJ(pid_gid) {
1668                                 char buf[max_user_text];
1669
1670                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1671                                 obj->data.s = buf;
1672                                 print_pid_gid(obj, p, p_max_size);
1673                         }
1674                         OBJ(pid_egid) {
1675                                 char buf[max_user_text];
1676
1677                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1678                                 obj->data.s = buf;
1679                                 print_pid_egid(obj, p, p_max_size);
1680                         }
1681                         OBJ(pid_sgid) {
1682                                 char buf[max_user_text];
1683
1684                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1685                                 obj->data.s = buf;
1686                                 print_pid_sgid(obj, p, p_max_size);
1687                         }
1688                         OBJ(pid_fsgid) {
1689                                 char buf[max_user_text];
1690
1691                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1692                                 obj->data.s = buf;
1693                                 print_pid_fsgid(obj, p, p_max_size);
1694                         }
1695                         OBJ(pid_read) {
1696                                 char buf[max_user_text];
1697
1698                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1699                                 obj->data.s = buf;
1700                                 print_pid_read(obj, p, p_max_size);
1701                         }
1702                         OBJ(pid_vmpeak) {
1703                                 char buf[max_user_text];
1704
1705                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1706                                 obj->data.s = buf;
1707                                 print_pid_vmpeak(obj, p, p_max_size);
1708                         }
1709                         OBJ(pid_vmsize) {
1710                                 char buf[max_user_text];
1711
1712                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1713                                 obj->data.s = buf;
1714                                 print_pid_vmsize(obj, p, p_max_size);
1715                         }
1716                         OBJ(pid_vmlck) {
1717                                 char buf[max_user_text];
1718
1719                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1720                                 obj->data.s = buf;
1721                                 print_pid_vmlck(obj, p, p_max_size);
1722                         }
1723                         OBJ(pid_vmhwm) {
1724                                 char buf[max_user_text];
1725
1726                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1727                                 obj->data.s = buf;
1728                                 print_pid_vmhwm(obj, p, p_max_size);
1729                         }
1730                         OBJ(pid_vmrss) {
1731                                 char buf[max_user_text];
1732
1733                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1734                                 obj->data.s = buf;
1735                                 print_pid_vmrss(obj, p, p_max_size);
1736                         }
1737                         OBJ(pid_vmdata) {
1738                                 char buf[max_user_text];
1739
1740                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1741                                 obj->data.s = buf;
1742                                 print_pid_vmdata(obj, p, p_max_size);
1743                         }
1744                         OBJ(pid_vmstk) {
1745                                 char buf[max_user_text];
1746
1747                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1748                                 obj->data.s = buf;
1749                                 print_pid_vmstk(obj, p, p_max_size);
1750                         }
1751                         OBJ(pid_vmexe) {
1752                                 char buf[max_user_text];
1753
1754                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1755                                 obj->data.s = buf;
1756                                 print_pid_vmexe(obj, p, p_max_size);
1757                         }
1758                         OBJ(pid_vmlib) {
1759                                 char buf[max_user_text];
1760
1761                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1762                                 obj->data.s = buf;
1763                                 print_pid_vmlib(obj, p, p_max_size);
1764                         }
1765                         OBJ(pid_vmpte) {
1766                                 char buf[max_user_text];
1767
1768                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1769                                 obj->data.s = buf;
1770                                 print_pid_vmpte(obj, p, p_max_size);
1771                         }
1772                         OBJ(pid_write) {
1773                                 char buf[max_user_text];
1774
1775                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1776                                 obj->data.s = buf;
1777                                 print_pid_write(obj, p, p_max_size);
1778                         }
1779                         OBJ(processes) {
1780                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1781                         }
1782                         OBJ(running_processes) {
1783                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1784                         }
1785                         OBJ(running_threads) {
1786                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1787                         }
1788                         OBJ(threads) {
1789                                 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1790                         }
1791                         OBJ(text) {
1792                                 snprintf(p, p_max_size, "%s", obj->data.s);
1793                         }
1794 #ifdef X11
1795                         OBJ(shadecolor) {
1796                                 new_bg(p, obj->data.l);
1797                         }
1798                         OBJ(stippled_hr) {
1799                                 new_stippled_hr(obj, p);
1800                         }
1801 #endif /* X11 */
1802                         OBJ(swap) {
1803                                 human_readable(cur->swap * 1024, p, 255);
1804                         }
1805                         OBJ(swapfree) {
1806                                 human_readable(cur->swapfree * 1024, p, 255);
1807                         }
1808                         OBJ(swapmax) {
1809                                 human_readable(cur->swapmax * 1024, p, 255);
1810                         }
1811                         OBJ(swapperc) {
1812                                 if (cur->swapmax == 0) {
1813                                         strncpy(p, "No swap", p_max_size);
1814                                 } else {
1815                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1816                                 }
1817                         }
1818                         OBJ(swapbar) {
1819                                 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1820                         }
1821                         OBJ(sysname) {
1822                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1823                         }
1824                         OBJ(time) {
1825                                 print_time(obj, p, p_max_size);
1826                         }
1827                         OBJ(utime) {
1828                                 print_utime(obj, p, p_max_size);
1829                         }
1830                         OBJ(tztime) {
1831                                 print_tztime(obj, p, p_max_size);
1832                         }
1833                         OBJ(totaldown) {
1834                                 print_totaldown(obj, p, p_max_size);
1835                         }
1836                         OBJ(totalup) {
1837                                 print_totalup(obj, p, p_max_size);
1838                         }
1839                         OBJ(gid_name) {
1840                                 char buf[max_user_text];
1841
1842                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1843                                 obj->data.s = buf;
1844                                 print_gid_name(obj, p, p_max_size);
1845                         }
1846                         OBJ(uid_name) {
1847                                 char buf[max_user_text];
1848
1849                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1850                                 obj->data.s = buf;
1851                                 print_uid_name(obj, p, p_max_size);
1852                         }
1853                         OBJ(updates) {
1854                                 snprintf(p, p_max_size, "%d", total_updates);
1855                         }
1856                         OBJ(if_updatenr) {
1857                                 if(total_updates % updatereset != obj->data.i - 1) {
1858                                         DO_JUMP;
1859                                 }
1860                         }
1861                         OBJ(upspeed) {
1862                                 print_upspeed(obj, p, p_max_size);
1863                         }
1864                         OBJ(upspeedf) {
1865                                 print_upspeedf(obj, p, p_max_size);
1866                         }
1867 #ifdef X11
1868                         OBJ(upspeedgraph) {
1869                                 print_upspeedgraph(obj, p, p_max_size);
1870                         }
1871 #endif /* X11 */
1872                         OBJ(uptime_short) {
1873                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
1874                         }
1875                         OBJ(uptime) {
1876                                 format_seconds(p, p_max_size, (int) cur->uptime);
1877                         }
1878 #ifdef __linux__
1879                         OBJ(user_names) {
1880                                 snprintf(p, p_max_size, "%s", cur->users.names);
1881                         }
1882                         OBJ(user_terms) {
1883                                 snprintf(p, p_max_size, "%s", cur->users.terms);
1884                         }
1885                         OBJ(user_times) {
1886                                 snprintf(p, p_max_size, "%s", cur->users.times);
1887                         }
1888                         OBJ(user_time) {
1889                                 update_user_time(obj->data.s);
1890                                 snprintf(p, p_max_size, "%s", cur->users.ctime);
1891                         }
1892                         OBJ(user_number) {
1893                                 snprintf(p, p_max_size, "%d", cur->users.number);
1894                         }
1895 #endif /* __linux__ */
1896 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1897                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1898                         OBJ(apm_adapter) {
1899                                 char *msg;
1900
1901                                 msg = get_apm_adapter();
1902                                 snprintf(p, p_max_size, "%s", msg);
1903                                 free(msg);
1904                         }
1905                         OBJ(apm_battery_life) {
1906                                 char *msg;
1907
1908                                 msg = get_apm_battery_life();
1909                                 snprintf(p, p_max_size, "%s", msg);
1910                                 free(msg);
1911                         }
1912                         OBJ(apm_battery_time) {
1913                                 char *msg;
1914
1915                                 msg = get_apm_battery_time();
1916                                 snprintf(p, p_max_size, "%s", msg);
1917                                 free(msg);
1918                         }
1919 #endif /* __FreeBSD__ __OpenBSD__ */
1920
1921 #ifdef MPD
1922                         OBJ(mpd_title)
1923                                 print_mpd_title(obj, p, p_max_size);
1924                         OBJ(mpd_artist)
1925                                 print_mpd_artist(obj, p, p_max_size);
1926                         OBJ(mpd_album)
1927                                 print_mpd_album(obj, p, p_max_size);
1928                         OBJ(mpd_random)
1929                                 print_mpd_random(obj, p, p_max_size);
1930                         OBJ(mpd_repeat)
1931                                 print_mpd_repeat(obj, p, p_max_size);
1932                         OBJ(mpd_track)
1933                                 print_mpd_track(obj, p, p_max_size);
1934                         OBJ(mpd_name)
1935                                 print_mpd_name(obj, p, p_max_size);
1936                         OBJ(mpd_file)
1937                                 print_mpd_file(obj, p, p_max_size);
1938                         OBJ(mpd_vol)
1939                                 print_mpd_vol(obj, p, p_max_size);
1940                         OBJ(mpd_bitrate)
1941                                 print_mpd_bitrate(obj, p, p_max_size);
1942                         OBJ(mpd_status)
1943                                 print_mpd_status(obj, p, p_max_size);
1944                         OBJ(mpd_elapsed) {
1945                                 print_mpd_elapsed(obj, p, p_max_size);
1946                         }
1947                         OBJ(mpd_length) {
1948                                 print_mpd_length(obj, p, p_max_size);
1949                         }
1950                         OBJ(mpd_percent) {
1951                                 print_mpd_percent(obj, p, p_max_size);
1952                         }
1953                         OBJ(mpd_bar) {
1954                                 print_mpd_bar(obj, p, p_max_size);
1955                         }
1956                         OBJ(mpd_smart) {
1957                                 print_mpd_smart(obj, p, p_max_size);
1958                         }
1959                         OBJ(if_mpd_playing) {
1960                                 if (!mpd_get_info()->is_playing) {
1961                                         DO_JUMP;
1962                                 }
1963                         }
1964 #endif
1965
1966 #ifdef MOC
1967 #define MOC_PRINT(t, a) \
1968         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1969                         OBJ(moc_state) {
1970                                 MOC_PRINT(state, "??");
1971                         }
1972                         OBJ(moc_file) {
1973                                 MOC_PRINT(file, "no file");
1974                         }
1975                         OBJ(moc_title) {
1976                                 MOC_PRINT(title, "no title");
1977                         }
1978                         OBJ(moc_artist) {
1979                                 MOC_PRINT(artist, "no artist");
1980                         }
1981                         OBJ(moc_song) {
1982                                 MOC_PRINT(song, "no song");
1983                         }
1984                         OBJ(moc_album) {
1985                                 MOC_PRINT(album, "no album");
1986                         }
1987                         OBJ(moc_totaltime) {
1988                                 MOC_PRINT(totaltime, "0:00");
1989                         }
1990                         OBJ(moc_timeleft) {
1991                                 MOC_PRINT(timeleft, "0:00");
1992                         }
1993                         OBJ(moc_curtime) {
1994                                 MOC_PRINT(curtime, "0:00");
1995                         }
1996                         OBJ(moc_bitrate) {
1997                                 MOC_PRINT(bitrate, "0Kbps");
1998                         }
1999                         OBJ(moc_rate) {
2000                                 MOC_PRINT(rate, "0KHz");
2001                         }
2002 #undef MOC_PRINT
2003 #endif /* MOC */
2004 #ifdef XMMS2
2005                         OBJ(xmms2_artist) {
2006                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2007                         }
2008                         OBJ(xmms2_album) {
2009                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2010                         }
2011                         OBJ(xmms2_title) {
2012                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2013                         }
2014                         OBJ(xmms2_genre) {
2015                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2016                         }
2017                         OBJ(xmms2_comment) {
2018                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2019                         }
2020                         OBJ(xmms2_url) {
2021                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2022                         }
2023                         OBJ(xmms2_status) {
2024                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2025                         }
2026                         OBJ(xmms2_date) {
2027                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2028                         }
2029                         OBJ(xmms2_tracknr) {
2030                                 if (cur->xmms2.tracknr != -1) {
2031                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2032                                 }
2033                         }
2034                         OBJ(xmms2_bitrate) {
2035                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2036                         }
2037                         OBJ(xmms2_id) {
2038                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2039                         }
2040                         OBJ(xmms2_size) {
2041                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2042                         }
2043                         OBJ(xmms2_elapsed) {
2044                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2045                                         (cur->xmms2.elapsed / 1000) % 60);
2046                         }
2047                         OBJ(xmms2_duration) {
2048                                 snprintf(p, p_max_size, "%02d:%02d",
2049                                         cur->xmms2.duration / 60000,
2050                                         (cur->xmms2.duration / 1000) % 60);
2051                         }
2052                         OBJ(xmms2_percent) {
2053                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2054                         }
2055                         OBJ(xmms2_bar) {
2056                                 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2057                         }
2058                         OBJ(xmms2_playlist) {
2059                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2060                         }
2061                         OBJ(xmms2_timesplayed) {
2062                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2063                         }
2064                         OBJ(xmms2_smart) {
2065                                 if (strlen(cur->xmms2.title) < 2
2066                                                 && strlen(cur->xmms2.title) < 2) {
2067                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
2068                                 } else {
2069                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2070                                                 cur->xmms2.title);
2071                                 }
2072                         }
2073                         OBJ(if_xmms2_connected) {
2074                                 if (cur->xmms2.conn_state != 1) {
2075                                         DO_JUMP;
2076                                 }
2077                         }
2078 #endif /* XMMS */
2079 #ifdef AUDACIOUS
2080                         OBJ(audacious_status) {
2081                                 snprintf(p, p_max_size, "%s",
2082                                         cur->audacious.items[AUDACIOUS_STATUS]);
2083                         }
2084                         OBJ(audacious_title) {
2085                                 snprintf(p, cur->audacious.max_title_len > 0
2086                                         ? cur->audacious.max_title_len : p_max_size, "%s",
2087                                         cur->audacious.items[AUDACIOUS_TITLE]);
2088                         }
2089                         OBJ(audacious_length) {
2090                                 snprintf(p, p_max_size, "%s",
2091                                         cur->audacious.items[AUDACIOUS_LENGTH]);
2092                         }
2093                         OBJ(audacious_length_seconds) {
2094                                 snprintf(p, p_max_size, "%s",
2095                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2096                         }
2097                         OBJ(audacious_position) {
2098                                 snprintf(p, p_max_size, "%s",
2099                                         cur->audacious.items[AUDACIOUS_POSITION]);
2100                         }
2101                         OBJ(audacious_position_seconds) {
2102                                 snprintf(p, p_max_size, "%s",
2103                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2104                         }
2105                         OBJ(audacious_bitrate) {
2106                                 snprintf(p, p_max_size, "%s",
2107                                         cur->audacious.items[AUDACIOUS_BITRATE]);
2108                         }
2109                         OBJ(audacious_frequency) {
2110                                 snprintf(p, p_max_size, "%s",
2111                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
2112                         }
2113                         OBJ(audacious_channels) {
2114                                 snprintf(p, p_max_size, "%s",
2115                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
2116                         }
2117                         OBJ(audacious_filename) {
2118                                 snprintf(p, p_max_size, "%s",
2119                                         cur->audacious.items[AUDACIOUS_FILENAME]);
2120                         }
2121                         OBJ(audacious_playlist_length) {
2122                                 snprintf(p, p_max_size, "%s",
2123                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2124                         }
2125                         OBJ(audacious_playlist_position) {
2126                                 snprintf(p, p_max_size, "%s",
2127                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2128                         }
2129                         OBJ(audacious_main_volume) {
2130                                 snprintf(p, p_max_size, "%s",
2131                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2132                         }
2133                         OBJ(audacious_bar) {
2134                                 double progress;
2135
2136                                 progress =
2137                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2138                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2139                                 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2140                         }
2141 #endif /* AUDACIOUS */
2142
2143 #ifdef BMPX
2144                         OBJ(bmpx_title) {
2145                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2146                         }
2147                         OBJ(bmpx_artist) {
2148                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2149                         }
2150                         OBJ(bmpx_album) {
2151                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2152                         }
2153                         OBJ(bmpx_uri) {
2154                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2155                         }
2156                         OBJ(bmpx_track) {
2157                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2158                         }
2159                         OBJ(bmpx_bitrate) {
2160                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2161                         }
2162 #endif /* BMPX */
2163                         /* we have four different types of top (top, top_mem,
2164                          * top_time and top_io). To avoid having almost-same code four
2165                          * times, we have this special handler. */
2166 #ifdef __linux__
2167                         break;
2168                         case OBJ_top:
2169                         case OBJ_top_mem:
2170                         case OBJ_top_time:
2171 #ifdef IOSTATS
2172                         case OBJ_top_io:
2173 #endif
2174                                 print_top(obj, p, p_max_size);
2175 #endif /* __linux__ */
2176                         OBJ(tail) {
2177                                 print_tailhead("tail", obj, p, p_max_size);
2178                         }
2179                         OBJ(head) {
2180                                 print_tailhead("head", obj, p, p_max_size);
2181                         }
2182                         OBJ(lines) {
2183                                 print_lines(obj, p, p_max_size);
2184                         }
2185
2186                         OBJ(words) {
2187                                 print_words(obj, p, p_max_size);
2188                         }
2189 #ifdef TCP_PORT_MONITOR
2190                         OBJ(tcp_portmon) {
2191                                 tcp_portmon_action(obj, p, p_max_size);
2192                         }
2193 #endif /* TCP_PORT_MONITOR */
2194
2195 #ifdef HAVE_ICONV
2196                         OBJ(iconv_start) {
2197                                 do_iconv_start(obj);
2198                         }
2199                         OBJ(iconv_stop) {
2200                                 do_iconv_stop();
2201                         }
2202 #endif /* HAVE_ICONV */
2203
2204                         OBJ(entropy_avail) {
2205                                 print_entropy_avail(obj, p, p_max_size);
2206                         }
2207                         OBJ(entropy_perc) {
2208                                 print_entropy_perc(obj, p, p_max_size);
2209                         }
2210                         OBJ(entropy_poolsize) {
2211                                 print_entropy_poolsize(obj, p, p_max_size);
2212                         }
2213                         OBJ(entropy_bar) {
2214                                 print_entropy_bar(obj, p, p_max_size);
2215                         }
2216 #ifdef IBM
2217                         OBJ(smapi) {
2218                                 print_smapi(obj, p, p_max_size);
2219                         }
2220                         OBJ(if_smapi_bat_installed) {
2221                                 int idx;
2222                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2223                                         if(!smapi_bat_installed(idx)) {
2224                                                 DO_JUMP;
2225                                         }
2226                                 } else
2227                                         NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2228                         }
2229                         OBJ(smapi_bat_perc) {
2230                                 print_smapi_bat_perc(obj, p, p_max_size);
2231                         }
2232                         OBJ(smapi_bat_temp) {
2233                                 print_smapi_bat_temp(obj, p, p_max_size);
2234                         }
2235                         OBJ(smapi_bat_power) {
2236                                 print_smapi_bat_power(obj, p, p_max_size);
2237                         }
2238                         OBJ(smapi_bat_bar) {
2239                                 print_smapi_bat_bar(obj, p, p_max_size);
2240                         }
2241 #endif /* IBM */
2242                         OBJ(include) {
2243                                 if(obj->sub) {
2244                                         char buf[max_user_text];
2245
2246                                         generate_text_internal(buf, max_user_text, *obj->sub, cur);
2247                                         snprintf(p, p_max_size, "%s", buf);
2248                                 } else {
2249                                         p[0] = 0;
2250                                 }
2251                         }
2252                         OBJ(blink) {
2253                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2254                                 char buf[max_user_text];
2255                                 unsigned int j;
2256
2257                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2258                                 snprintf(p, p_max_size, "%s", buf);
2259                                 if(total_updates % 2) {
2260                                         for(j=0; p[j] != 0; j++) {
2261                                                 p[j] = ' ';
2262                                         }
2263                                 }
2264                         }
2265                         OBJ(to_bytes) {
2266                                 char buf[max_user_text];
2267                                 long long bytes;
2268                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
2269
2270                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2271                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2272                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2273                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2274                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2275                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2276                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2277                                 }
2278                                 snprintf(p, p_max_size, "%s", buf);
2279                         }
2280                         OBJ(scroll) {
2281                                 print_scroll(obj, p, p_max_size, cur);
2282                         }
2283                         OBJ(combine) {
2284                                 print_combine(obj, p, cur);
2285                         }
2286 #ifdef NVIDIA
2287                         OBJ(nvidia) {
2288                                 print_nvidia_value(obj, display, p, p_max_size);
2289                         }
2290 #endif /* NVIDIA */
2291 #ifdef APCUPSD
2292                         OBJ(apcupsd) {
2293                                 /* This is just a meta-object to set host:port */
2294                         }
2295                         OBJ(apcupsd_name) {
2296                                 snprintf(p, p_max_size, "%s",
2297                                                  cur->apcupsd.items[APCUPSD_NAME]);
2298                         }
2299                         OBJ(apcupsd_model) {
2300                                 snprintf(p, p_max_size, "%s",
2301                                                  cur->apcupsd.items[APCUPSD_MODEL]);
2302                         }
2303                         OBJ(apcupsd_upsmode) {
2304                                 snprintf(p, p_max_size, "%s",
2305                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
2306                         }
2307                         OBJ(apcupsd_cable) {
2308                                 snprintf(p, p_max_size, "%s",
2309                                                  cur->apcupsd.items[APCUPSD_CABLE]);
2310                         }
2311                         OBJ(apcupsd_status) {
2312                                 snprintf(p, p_max_size, "%s",
2313                                                  cur->apcupsd.items[APCUPSD_STATUS]);
2314                         }
2315                         OBJ(apcupsd_linev) {
2316                                 snprintf(p, p_max_size, "%s",
2317                                                  cur->apcupsd.items[APCUPSD_LINEV]);
2318                         }
2319                         OBJ(apcupsd_load) {
2320                                 snprintf(p, p_max_size, "%s",
2321                                                  cur->apcupsd.items[APCUPSD_LOAD]);
2322                         }
2323                         OBJ(apcupsd_loadbar) {
2324                                 double progress;
2325                                 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2326                                 new_bar(obj, p, p_max_size, (int) progress);
2327                         }
2328 #ifdef X11
2329                         OBJ(apcupsd_loadgraph) {
2330                                 double progress;
2331                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
2332                                 new_graph(obj, p, p_max_size, (int)progress);
2333                         }
2334 #endif /* X11 */
2335                         OBJ(apcupsd_loadgauge) {
2336                                 double progress;
2337                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2338                                 new_gauge(obj, p, p_max_size, (int)progress);
2339                         }
2340                         OBJ(apcupsd_charge) {
2341                                 snprintf(p, p_max_size, "%s",
2342                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
2343                         }
2344                         OBJ(apcupsd_timeleft) {
2345                                 snprintf(p, p_max_size, "%s",
2346                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
2347                         }
2348                         OBJ(apcupsd_temp) {
2349                                 snprintf(p, p_max_size, "%s",
2350                                                  cur->apcupsd.items[APCUPSD_TEMP]);
2351                         }
2352                         OBJ(apcupsd_lastxfer) {
2353                                 snprintf(p, p_max_size, "%s",
2354                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
2355                         }
2356 #endif /* APCUPSD */
2357                         break;
2358                 }
2359 #undef DO_JUMP
2360
2361
2362                 {
2363                         size_t a = strlen(p);
2364
2365 #ifdef HAVE_ICONV
2366                         iconv_convert(&a, buff_in, p, p_max_size);
2367 #endif /* HAVE_ICONV */
2368                         if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2369 #ifdef HAVE_LUA
2370                                         && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2371 #endif /* HAVE_LUA */
2372                                         ) {
2373                                 substitute_newlines(p, a - 2);
2374                         }
2375                         p += a;
2376                         p_max_size -= a;
2377                         (*p) = 0;
2378                 }
2379                 obj = obj->next;
2380         }
2381 #ifdef X11
2382         /* load any new fonts we may have had */
2383         if (need_to_load_fonts) {
2384                 load_fonts();
2385         }
2386 #endif /* X11 */
2387 }
2388
2389 void evaluate(const char *text, char *p, int p_max_size)
2390 {
2391         struct information *tmp_info;
2392         struct text_object subroot;
2393
2394         tmp_info = malloc(sizeof(struct information));
2395         memcpy(tmp_info, &info, sizeof(struct information));
2396         parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2397         DBGP("evaluated '%s' to '%s'", text, p);
2398
2399         free_text_objects(&subroot, 1);
2400         free(tmp_info);
2401 }
2402
2403 double current_update_time, next_update_time, last_update_time;
2404
2405 static void generate_text(void)
2406 {
2407         struct information *cur = &info;
2408         char *p;
2409         unsigned int i, j, k;
2410
2411         special_count = 0;
2412
2413         /* update info */
2414
2415         current_update_time = get_time();
2416
2417         update_stuff();
2418
2419         /* add things to the buffer */
2420
2421         /* generate text */
2422
2423         p = text_buffer;
2424
2425         generate_text_internal(p, max_user_text, global_root_object, cur);
2426         if(max_text_width > 0) {
2427                 for(i = 0, j = 0; p[i] != 0; i++) {
2428                         if(p[i] == '\n') j = 0;
2429                         else if(j == max_text_width) {
2430                                 k = i + strlen(p + i) + 1;
2431                                 if(k < text_buffer_size) {
2432                                         while(k != i) {
2433                                                 p[k] = p[k-1];
2434                                                 k--;
2435                                         }
2436                                         p[k] = '\n';
2437                                         j = 0;
2438                                 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2439                         } else j++;
2440                 }
2441         }
2442
2443         if (stuff_in_uppercase) {
2444                 char *tmp_p;
2445
2446                 tmp_p = text_buffer;
2447                 while (*tmp_p) {
2448                         *tmp_p = toupper(*tmp_p);
2449                         tmp_p++;
2450                 }
2451         }
2452
2453         next_update_time += update_interval;
2454         if (next_update_time < get_time()) {
2455                 next_update_time = get_time() + update_interval;
2456         } else if (next_update_time > get_time() + update_interval) {
2457                 next_update_time = get_time() + update_interval;
2458         }
2459         last_update_time = current_update_time;
2460         total_updates++;
2461 }
2462
2463 void set_update_interval(double interval)
2464 {
2465         update_interval = interval;
2466         update_interval_old = interval;
2467 }
2468
2469 static inline int get_string_width(const char *s)
2470 {
2471         return *s ? calc_text_width(s) : 0;
2472 }
2473
2474 #ifdef X11
2475 static int get_string_width_special(char *s, int special_index)
2476 {
2477         char *p, *final;
2478         int idx = 1;
2479         int width = 0;
2480         long i;
2481
2482         if (!s)
2483                 return 0;
2484
2485         if ((output_methods & TO_X) == 0)
2486                 return strlen(s);
2487
2488         p = strndup(s, text_buffer_size);
2489         final = p;
2490
2491         while (*p) {
2492                 if (*p == SPECIAL_CHAR) {
2493                         /* shift everything over by 1 so that the special char
2494                          * doesn't mess up the size calculation */
2495                         for (i = 0; i < (long)strlen(p); i++) {
2496                                 *(p + i) = *(p + i + 1);
2497                         }
2498                         if (specials[special_index + idx].type == GRAPH
2499                                         || specials[special_index + idx].type == GAUGE
2500                                         || specials[special_index + idx].type == BAR) {
2501                                 width += specials[special_index + idx].width;
2502                         }
2503                         idx++;
2504                 } else if (*p == SECRIT_MULTILINE_CHAR) {
2505                         *p = 0;
2506                         break;
2507                 } else {
2508                         p++;
2509                 }
2510         }
2511         if (strlen(final) > 1) {
2512                 width += calc_text_width(final);
2513         }
2514         free(final);
2515         return width;
2516 }
2517
2518 static int text_size_updater(char *s, int special_index);
2519
2520 int last_font_height;
2521 static void update_text_area(void)
2522 {
2523         int x = 0, y = 0;
2524
2525         if ((output_methods & TO_X) == 0)
2526                 return;
2527         /* update text size if it isn't fixed */
2528 #ifdef OWN_WINDOW
2529         if (!fixed_size)
2530 #endif
2531         {
2532                 text_width = minimum_width;
2533                 text_height = 0;
2534                 last_font_height = font_height();
2535                 for_each_line(text_buffer, text_size_updater);
2536                 text_width += 1;
2537                 if (text_height < minimum_height) {
2538                         text_height = minimum_height;
2539                 }
2540                 if (text_width > maximum_width && maximum_width > 0) {
2541                         text_width = maximum_width;
2542                 }
2543         }
2544
2545         /* get text position on workarea */
2546         switch (text_alignment) {
2547                 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2548                         y = gap_y;
2549                         break;
2550
2551                 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2552                         y = workarea[3] - text_height - gap_y;
2553                         break;
2554
2555                 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2556                         y = workarea[3] / 2 - text_height / 2 - gap_y;
2557                         break;
2558         }
2559         switch (text_alignment) {
2560                 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2561                         x = gap_x;
2562                         break;
2563
2564                 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2565                         x = workarea[2] - text_width - gap_x;
2566                         break;
2567
2568                 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2569                         x = workarea[2] / 2 - text_width / 2 - gap_x;
2570                         break;
2571         }
2572 #ifdef OWN_WINDOW
2573         if (text_alignment == NONE) {   // Let the WM manage the window
2574                         x = window.x;
2575                         y = window.y;
2576
2577                         fixed_pos = 1;
2578                         fixed_size = 1;
2579         }
2580 #endif /* OWN_WINDOW */
2581 #ifdef OWN_WINDOW
2582
2583         if (own_window && !fixed_pos) {
2584                 x += workarea[0];
2585                 y += workarea[1];
2586                 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2587                 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2588                 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2589                 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2590         } else
2591 #endif
2592         {
2593                 /* If window size doesn't match to workarea's size,
2594                  * then window probably includes panels (gnome).
2595                  * Blah, doesn't work on KDE. */
2596                 if (workarea[2] != window.width || workarea[3] != window.height) {
2597                         y += workarea[1];
2598                         x += workarea[0];
2599                 }
2600
2601                 text_start_x = x;
2602                 text_start_y = y;
2603         }
2604 #ifdef HAVE_LUA
2605         /* update lua window globals */
2606         llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2607 #endif /* HAVE_LUA */
2608 }
2609
2610 /* drawing stuff */
2611
2612 static int cur_x, cur_y;        /* current x and y for drawing */
2613 #endif
2614 //draw_mode also without X11 because we only need to print to stdout with FG
2615 static int draw_mode;           /* FG, BG or OUTLINE */
2616 #ifdef X11
2617 static long current_color;
2618
2619 static int text_size_updater(char *s, int special_index)
2620 {
2621         int w = 0;
2622         char *p;
2623
2624         if ((output_methods & TO_X) == 0)
2625                 return 0;
2626         /* get string widths and skip specials */
2627         p = s;
2628         while (*p) {
2629                 if (*p == SPECIAL_CHAR) {
2630                         *p = '\0';
2631                         w += get_string_width(s);
2632                         *p = SPECIAL_CHAR;
2633
2634                         if (specials[special_index].type == BAR
2635                                         || specials[special_index].type == GAUGE
2636                                         || specials[special_index].type == GRAPH) {
2637                                 w += specials[special_index].width;
2638                                 if (specials[special_index].height > last_font_height) {
2639                                         last_font_height = specials[special_index].height;
2640                                         last_font_height += font_height();
2641                                 }
2642                         } else if (specials[special_index].type == OFFSET) {
2643                                 if (specials[special_index].arg > 0) {
2644                                         w += specials[special_index].arg;
2645                                 }
2646                         } else if (specials[special_index].type == VOFFSET) {
2647                                 last_font_height += specials[special_index].arg;
2648                         } else if (specials[special_index].type == GOTO) {
2649                                 if (specials[special_index].arg > cur_x) {
2650                                         w = (int) specials[special_index].arg;
2651                                 }
2652                         } else if (specials[special_index].type == TAB) {
2653                                 int start = specials[special_index].arg;
2654                                 int step = specials[special_index].width;
2655
2656                                 if (!step || step < 0) {
2657                                         step = 10;
2658                                 }
2659                                 w += step - (cur_x - text_start_x - start) % step;
2660                         } else if (specials[special_index].type == FONT) {
2661                                 selected_font = specials[special_index].font_added;
2662                                 if (font_height() > last_font_height) {
2663                                         last_font_height = font_height();
2664                                 }
2665                         }
2666
2667                         special_index++;
2668                         s = p + 1;
2669                 } else if (*p == SECRIT_MULTILINE_CHAR) {
2670                         int lw;
2671                         *p = '\0';
2672                         lw = get_string_width(s);
2673                         *p = SECRIT_MULTILINE_CHAR;
2674                         s = p + 1;
2675                         w = lw > w ? lw : w;
2676                         text_height += last_font_height;
2677                 }
2678                 p++;
2679         }
2680         w += get_string_width(s);
2681         if (w > text_width) {
2682                 text_width = w;
2683         }
2684         if (text_width > maximum_width && maximum_width) {
2685                 text_width = maximum_width;
2686         }
2687
2688         text_height += last_font_height;
2689         last_font_height = font_height();
2690         return special_index;
2691 }
2692 #endif /* X11 */
2693
2694 static inline void set_foreground_color(long c)
2695 {
2696 #ifdef X11
2697         if (output_methods & TO_X) {
2698 #ifdef USE_ARGB
2699                 if (have_argb_visual) {
2700                         current_color = c | (0xff << 24);
2701                 } else {
2702 #endif /* USE_ARGB */
2703                         current_color = c;
2704 #ifdef USE_ARGB
2705                 }
2706 #endif /* USE_ARGB */
2707                 XSetForeground(display, window.gc, current_color);
2708         }
2709 #endif /* X11 */
2710 #ifdef NCURSES
2711         if (output_methods & TO_NCURSES) {
2712                 attron(COLOR_PAIR(c));
2713         }
2714 #endif /* NCURSES */
2715         UNUSED(c);
2716         return;
2717 }
2718
2719 static void draw_string(const char *s)
2720 {
2721         int i, i2, pos, width_of_s;
2722         int max = 0;
2723         int added;
2724         char *s_with_newlines;
2725
2726         if (s[0] == '\0') {
2727                 return;
2728         }
2729
2730         width_of_s = get_string_width(s);
2731         s_with_newlines = strdup(s);
2732         for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2733                 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2734                         s_with_newlines[i] = '\n';
2735                 }
2736         }
2737         if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2738                 printf("%s\n", s_with_newlines);
2739                 if (extra_newline) fputc('\n', stdout);
2740                 fflush(stdout); /* output immediately, don't buffer */
2741         }
2742         if ((output_methods & TO_STDERR) && draw_mode == FG) {
2743                 fprintf(stderr, "%s\n", s_with_newlines);
2744                 fflush(stderr); /* output immediately, don't buffer */
2745         }
2746         if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2747                 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2748         }
2749         if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2750                 fprintf(append_fpointer, "%s\n", s_with_newlines);
2751         }
2752 #ifdef NCURSES
2753         if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2754                 printw("%s", s_with_newlines);
2755         }
2756 #endif
2757         free(s_with_newlines);
2758         memset(tmpstring1, 0, text_buffer_size);
2759         memset(tmpstring2, 0, text_buffer_size);
2760         strncpy(tmpstring1, s, text_buffer_size - 1);
2761         pos = 0;
2762         added = 0;
2763
2764 #ifdef X11
2765         if (output_methods & TO_X) {
2766                 max = ((text_width - width_of_s) / get_string_width(" "));
2767         }
2768 #endif /* X11 */
2769         /* This code looks for tabs in the text and coverts them to spaces.
2770          * The trick is getting the correct number of spaces, and not going
2771          * over the window's size without forcing the window larger. */
2772         for (i = 0; i < (int) text_buffer_size; i++) {
2773                 if (tmpstring1[i] == '\t') {
2774                         i2 = 0;
2775                         for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2776                                 /* guard against overrun */
2777                                 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2778                                 added++;
2779                         }
2780                         pos += i2;
2781                 } else {
2782                         /* guard against overrun */
2783                         tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2784                         pos++;
2785                 }
2786         }
2787 #ifdef X11
2788         if (output_methods & TO_X) {
2789                 if (text_width == maximum_width) {
2790                         /* this means the text is probably pushing the limit,
2791                          * so we'll chop it */
2792                         while (cur_x + get_string_width(tmpstring2) - text_start_x
2793                                         > maximum_width && strlen(tmpstring2) > 0) {
2794                                 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2795                         }
2796                 }
2797         }
2798 #endif /* X11 */
2799         s = tmpstring2;
2800 #ifdef X11
2801         if (output_methods & TO_X) {
2802 #ifdef XFT
2803                 if (use_xft) {
2804                         XColor c;
2805                         XftColor c2;
2806
2807                         c.pixel = current_color;
2808                         // query color on custom colormap
2809                         XQueryColor(display, window.colourmap, &c);
2810
2811                         c2.pixel = c.pixel;
2812                         c2.color.red = c.red;
2813                         c2.color.green = c.green;
2814                         c2.color.blue = c.blue;
2815                         c2.color.alpha = fonts[selected_font].font_alpha;
2816                         if (utf8_mode) {
2817                                 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2818                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2819                         } else {
2820                                 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2821                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2822                         }
2823                 } else
2824 #endif
2825                 {
2826                         XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2827                                 strlen(s));
2828                 }
2829                 cur_x += width_of_s;
2830         }
2831 #endif /* X11 */
2832         memcpy(tmpstring1, s, text_buffer_size);
2833 }
2834
2835 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2836 {
2837 #ifdef X11
2838         int font_h;
2839         int cur_y_add = 0;
2840 #endif /* X11 */
2841         char *recurse = 0;
2842         char *p = s;
2843         int last_special_needed = -1;
2844         int orig_special_index = special_index;
2845
2846 #ifdef X11
2847         if (output_methods & TO_X) {
2848                 font_h = font_height();
2849                 cur_y += font_ascent();
2850         }
2851         cur_x = text_start_x;
2852 #endif /* X11 */
2853
2854         while (*p) {
2855                 if (*p == SECRIT_MULTILINE_CHAR) {
2856                         /* special newline marker for multiline objects */
2857                         recurse = p + 1;
2858                         *p = '\0';
2859                         break;
2860                 }
2861                 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2862 #ifdef X11
2863                         int w = 0;
2864 #endif /* X11 */
2865
2866                         /* draw string before special, unless we're dealing multiline
2867                          * specials */
2868                         if (last_special_applied > -1) {
2869                                 special_index = last_special_applied;
2870                         } else {
2871                                 *p = '\0';
2872                                 draw_string(s);
2873                                 *p = SPECIAL_CHAR;
2874                                 s = p + 1;
2875                         }
2876                         /* draw special */
2877                         switch (specials[special_index].type) {
2878 #ifdef X11
2879                                 case HORIZONTAL_LINE:
2880                                 {
2881                                         int h = specials[special_index].height;
2882                                         int mid = font_ascent() / 2;
2883
2884                                         w = text_start_x + text_width - cur_x;
2885
2886                                         XSetLineAttributes(display, window.gc, h, LineSolid,
2887                                                 CapButt, JoinMiter);
2888                                         XDrawLine(display, window.drawable, window.gc, cur_x,
2889                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2890                                         break;
2891                                 }
2892
2893                                 case STIPPLED_HR:
2894                                 {
2895                                         int h = specials[special_index].height;
2896                                         int tmp_s = specials[special_index].arg;
2897                                         int mid = font_ascent() / 2;
2898                                         char ss[2] = { tmp_s, tmp_s };
2899
2900                                         w = text_start_x + text_width - cur_x - 1;
2901                                         XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2902                                                 CapButt, JoinMiter);
2903                                         XSetDashes(display, window.gc, 0, ss, 2);
2904                                         XDrawLine(display, window.drawable, window.gc, cur_x,
2905                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2906                                         break;
2907                                 }
2908
2909                                 case BAR:
2910                                 {
2911                                         int h, bar_usage, by;
2912                                         if (cur_x - text_start_x > maximum_width
2913                                                         && maximum_width > 0) {
2914                                                 break;
2915                                         }
2916                                         h = specials[special_index].height;
2917                                         bar_usage = specials[special_index].arg;
2918                                         by = cur_y - (font_ascent() / 2) - 1;
2919
2920                                         if (h < font_h) {
2921                                                 by -= h / 2 - 1;
2922                                         }
2923                                         w = specials[special_index].width;
2924                                         if (w == 0) {
2925                                                 w = text_start_x + text_width - cur_x - 1;
2926                                         }
2927                                         if (w < 0) {
2928                                                 w = 0;
2929                                         }
2930
2931                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
2932                                                 CapButt, JoinMiter);
2933
2934                                         XDrawRectangle(display, window.drawable, window.gc, cur_x,
2935                                                 by, w, h);
2936                                         XFillRectangle(display, window.drawable, window.gc, cur_x,
2937                                                 by, w * bar_usage / 255, h);
2938                                         if (h > cur_y_add
2939                                                         && h > font_h) {
2940                                                 cur_y_add = h;
2941                                         }
2942                                         break;
2943                                 }
2944
2945                                 case GAUGE: /* new GAUGE  */
2946                                 {
2947                                         int h, by = 0;
2948                                         unsigned long last_colour = current_color;
2949 #ifdef MATH
2950                                         float angle, px, py;
2951                                         int usage;
2952 #endif /* MATH */
2953
2954                                         if (cur_x - text_start_x > maximum_width
2955                                                         && maximum_width > 0) {
2956                                                 break;
2957                                         }
2958
2959                                         h = specials[special_index].height;
2960                                         by = cur_y - (font_ascent() / 2) - 1;
2961
2962                                         if (h < font_h) {
2963                                                 by -= h / 2 - 1;
2964                                         }
2965                                         w = specials[special_index].width;
2966                                         if (w == 0) {
2967                                                 w = text_start_x + text_width - cur_x - 1;
2968                                         }
2969                                         if (w < 0) {
2970                                                 w = 0;
2971                                         }
2972
2973                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
2974                                                         CapButt, JoinMiter);
2975
2976                                         XDrawArc(display, window.drawable, window.gc,
2977                                                         cur_x, by, w, h * 2, 0, 180*64);
2978
2979 #ifdef MATH
2980                                         usage = specials[special_index].arg;
2981                                         angle = (M_PI)*(float)(usage)/255.;
2982                                         px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2983                                         py = (float)(by+(h))-(float)(h)*sin(angle);
2984
2985                                         XDrawLine(display, window.drawable, window.gc,
2986                                                         cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2987 #endif /* MATH */
2988
2989                                         if (h > cur_y_add
2990                                                         && h > font_h) {
2991                                                 cur_y_add = h;
2992                                         }
2993
2994                                         set_foreground_color(last_colour);
2995
2996                                         break;
2997
2998                                 }
2999
3000                                 case GRAPH:
3001                                 {
3002                                         int h, by, i = 0, j = 0;
3003                                         int colour_idx = 0;
3004                                         unsigned long last_colour = current_color;
3005                                         unsigned long *tmpcolour = 0;
3006                                         if (cur_x - text_start_x > maximum_width
3007                                                         && maximum_width > 0) {
3008                                                 break;
3009                                         }
3010                                         h = specials[special_index].height;
3011                                         by = cur_y - (font_ascent() / 2) - 1;
3012
3013                                         if (h < font_h) {
3014                                                 by -= h / 2 - 1;
3015                                         }
3016                                         w = specials[special_index].width;
3017                                         if (w == 0) {
3018                                                 w = text_start_x + text_width - cur_x - 1;
3019                                         }
3020                                         if (w < 0) {
3021                                                 w = 0;
3022                                         }
3023                                         if (draw_graph_borders) {
3024                                                 XSetLineAttributes(display, window.gc, 1, LineSolid,
3025                                                         CapButt, JoinMiter);
3026                                                 XDrawRectangle(display, window.drawable, window.gc,
3027                                                         cur_x, by, w, h);
3028                                         }
3029                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
3030                                                 CapButt, JoinMiter);
3031
3032                                         if (specials[special_index].last_colour != 0
3033                                                         || specials[special_index].first_colour != 0) {
3034                                                 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3035                                         }
3036                                         colour_idx = 0;
3037                                         for (i = w - 2; i > -1; i--) {
3038                                                 if (specials[special_index].last_colour != 0
3039                                                                 || specials[special_index].first_colour != 0) {
3040                                                         if (specials[special_index].tempgrad) {
3041 #ifdef DEBUG_lol
3042                                                                 assert(
3043                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3044                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3045                                                                                 < w - 1
3046                                                                           );
3047                                                                 assert(
3048                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3049                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3050                                                                                 > -1
3051                                                                           );
3052                                                                 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3053                                                                         assert(
3054                                                                                         (int)((float)(w - 2) - specials[special_index].graph[j] *
3055                                                                                                 (w - 2) / (float)specials[special_index].graph_scale)
3056                                                                                         == 0
3057                                                                                   );
3058                                                                 }
3059 #endif /* DEBUG_lol */
3060                                                                 XSetForeground(display, window.gc, tmpcolour[
3061                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3062                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3063                                                                                 ]);
3064                                                         } else {
3065                                                                 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3066                                                         }
3067                                                 }
3068                                                 /* this is mugfugly, but it works */
3069                                                 XDrawLine(display, window.drawable, window.gc,
3070                                                                 cur_x + i + 1, by + h, cur_x + i + 1,
3071                                                                 round_to_int((double)by + h - specials[special_index].graph[j] *
3072                                                                         (h - 1) / specials[special_index].graph_scale));
3073                                                 if ((w - i) / ((float) (w - 2) /
3074                                                                         (specials[special_index].graph_width)) > j
3075                                                                 && j < MAX_GRAPH_DEPTH - 3) {
3076                                                         j++;
3077                                                 }
3078                                         }
3079                                         if (tmpcolour) free(tmpcolour);
3080                                         if (h > cur_y_add
3081                                                         && h > font_h) {
3082                                                 cur_y_add = h;
3083                                         }
3084                                         /* if (draw_mode == BG) {
3085                                                 set_foreground_color(default_bg_color);
3086                                         } else if (draw_mode == OUTLINE) {
3087                                                 set_foreground_color(default_out_color);
3088                                         } else {
3089                                                 set_foreground_color(default_fg_color);
3090                                         } */
3091                                         if (show_graph_range) {
3092                                                 int tmp_x = cur_x;
3093                                                 int tmp_y = cur_y;
3094                                                 unsigned short int seconds = update_interval * w;
3095                                                 char *tmp_day_str;
3096                                                 char *tmp_hour_str;
3097                                                 char *tmp_min_str;
3098                                                 char *tmp_sec_str;
3099                                                 char *tmp_str;
3100                                                 unsigned short int timeunits;
3101                                                 if (seconds != 0) {
3102                                                         timeunits = seconds / 86400; seconds %= 86400;
3103                                                         if (timeunits > 0) {
3104                                                                 asprintf(&tmp_day_str, "%dd", timeunits);
3105                                                         } else {
3106                                                                 tmp_day_str = strdup("");
3107                                                         }
3108                                                         timeunits = seconds / 3600; seconds %= 3600;
3109                                                         if (timeunits > 0) {
3110                                                                 asprintf(&tmp_hour_str, "%dh", timeunits);
3111                                                         } else {
3112                                                                 tmp_hour_str = strdup("");
3113                                                         }
3114                                                         timeunits = seconds / 60; seconds %= 60;
3115                                                         if (timeunits > 0) {
3116                                                                 asprintf(&tmp_min_str, "%dm", timeunits);
3117                                                         } else {
3118                                                                 tmp_min_str = strdup("");
3119                                                         }
3120                                                         if (seconds > 0) {
3121                                                                 asprintf(&tmp_sec_str, "%ds", seconds);
3122                                                         } else {
3123                                                                 tmp_sec_str = strdup("");
3124                                                         }
3125                                                         asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3126                                                         free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3127                                                 } else {
3128                                                         asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3129                                                 }
3130                                                 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3131                                                 cur_y += font_h / 2;
3132                                                 draw_string(tmp_str);
3133                                                 free(tmp_str);
3134                                                 cur_x = tmp_x;
3135                                                 cur_y = tmp_y;
3136                                         }
3137 #ifdef MATH
3138                                         if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3139                                                 int tmp_x = cur_x;
3140                                                 int tmp_y = cur_y;
3141                                                 char *tmp_str;
3142                                                 cur_x += font_ascent() / 2;
3143                                                 cur_y += font_h / 2;
3144                                                 tmp_str = (char *)
3145                                                         calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3146                                                                         sizeof(char));
3147                                                 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3148                                                 draw_string(tmp_str);
3149                                                 free(tmp_str);
3150                                                 cur_x = tmp_x;
3151                                                 cur_y = tmp_y;
3152                                         }
3153 #endif
3154                                         set_foreground_color(last_colour);
3155                                         break;
3156                                 }
3157
3158                                 case FONT:
3159                                 {
3160                                         int old = font_ascent();
3161
3162                                         cur_y -= font_ascent();
3163                                         selected_font = specials[special_index].font_added;
3164                                         set_font();
3165                                         if (cur_y + font_ascent() < cur_y + old) {
3166                                                 cur_y += old;
3167                                         } else {
3168                                                 cur_y += font_ascent();
3169                                         }
3170                                         font_h = font_height();
3171                                         break;
3172                                 }
3173 #endif /* X11 */
3174                                 case FG:
3175                                         if (draw_mode == FG) {
3176                                                 set_foreground_color(specials[special_index].arg);
3177                                         }
3178                                         break;
3179
3180 #ifdef X11
3181                                 case BG:
3182                                         if (draw_mode == BG) {
3183                                                 set_foreground_color(specials[special_index].arg);
3184                                         }
3185                                         break;
3186
3187                                 case OUTLINE:
3188                                         if (draw_mode == OUTLINE) {
3189                                                 set_foreground_color(specials[special_index].arg);
3190                                         }
3191                                         break;
3192
3193                                 case OFFSET:
3194                                         w += specials[special_index].arg;
3195                                         last_special_needed = special_index;
3196                                         break;
3197
3198                                 case VOFFSET:
3199                                         cur_y += specials[special_index].arg;
3200                                         break;
3201
3202                                 case GOTO:
3203                                         if (specials[special_index].arg >= 0) {
3204                                                 cur_x = (int) specials[special_index].arg;
3205                                         }
3206                                         last_special_needed = special_index;
3207                                         break;
3208
3209                                 case TAB:
3210                                 {
3211                                         int start = specials[special_index].arg;
3212                                         int step = specials[special_index].width;
3213
3214                                         if (!step || step < 0) {
3215                                                 step = 10;
3216                                         }
3217                                         w = step - (cur_x - text_start_x - start) % step;
3218                                         last_special_needed = special_index;
3219                                         break;
3220                                 }
3221
3222                                 case ALIGNR:
3223                                 {
3224                                         /* TODO: add back in "+ window.border_inner_margin" to the end of
3225                                          * this line? */
3226                                         int pos_x = text_start_x + text_width -
3227                                                 get_string_width_special(s, special_index);
3228
3229                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3230                                                 "get_string_width(p) %i gap_x %i "
3231                                                 "specials[special_index].arg %i window.border_inner_margin %i "
3232                                                 "window.border_width %i\n", pos_x, text_start_x, text_width,
3233                                                 cur_x, get_string_width_special(s), gap_x,
3234                                                 specials[special_index].arg, window.border_inner_margin,
3235                                                 window.border_width); */
3236                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3237                                                 cur_x = pos_x - specials[special_index].arg;
3238                                         }
3239                                         last_special_needed = special_index;
3240                                         break;
3241                                 }
3242
3243                                 case ALIGNC:
3244                                 {
3245                                         int pos_x = (text_width) / 2 - get_string_width_special(s,
3246                                                         special_index) / 2 - (cur_x -
3247                                                                 text_start_x);
3248                                         /* int pos_x = text_start_x + text_width / 2 -
3249                                                 get_string_width_special(s) / 2; */
3250
3251                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3252                                                 "get_string_width(p) %i gap_x %i "
3253                                                 "specials[special_index].arg %i\n", pos_x, text_start_x,
3254                                                 text_width, cur_x, get_string_width(s), gap_x,
3255                                                 specials[special_index].arg); */
3256                                         if (pos_x > specials[special_index].arg) {
3257                                                 w = pos_x - specials[special_index].arg;
3258                                         }
3259                                         last_special_needed = special_index;
3260                                         break;
3261                                 }
3262 #endif /* X11 */
3263                         }
3264
3265 #ifdef X11
3266                         cur_x += w;
3267 #endif /* X11 */
3268
3269                         if (special_index != last_special_applied) {
3270                                 special_index++;
3271                         } else {
3272                                 special_index = orig_special_index;
3273                                 last_special_applied = -1;
3274                         }
3275                 }
3276                 p++;
3277         }
3278
3279 #ifdef X11
3280         cur_y += cur_y_add;
3281 #endif /* X11 */
3282         draw_string(s);
3283 #ifdef NCURSES
3284         if (output_methods & TO_NCURSES) {
3285                 printw("\n");
3286         }
3287 #endif /* NCURSES */
3288 #ifdef X11
3289         if (output_methods & TO_X)
3290                 cur_y += font_descent();
3291 #endif /* X11 */
3292         if (recurse && *recurse) {
3293                 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3294                 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3295         }
3296         return special_index;
3297 }
3298
3299 static int draw_line(char *s, int special_index)
3300 {
3301 #ifdef X11
3302         if (output_methods & TO_X) {
3303                 return draw_each_line_inner(s, special_index, -1);
3304         }
3305 #endif /* X11 */
3306 #ifdef NCURSES
3307         if (output_methods & TO_NCURSES) {
3308                 return draw_each_line_inner(s, special_index, -1);
3309         }
3310 #endif /* NCURSES */
3311         draw_string(s);
3312         UNUSED(special_index);
3313         return 0;
3314 }
3315
3316 static void draw_text(void)
3317 {
3318 #ifdef X11
3319 #ifdef HAVE_LUA
3320         llua_draw_pre_hook();
3321 #endif /* HAVE_LUA */
3322         if (output_methods & TO_X) {
3323                 cur_y = text_start_y;
3324
3325                 /* draw borders */
3326                 if (draw_borders && window.border_width > 0) {
3327                         if (stippled_borders) {
3328                                 char ss[2] = { stippled_borders, stippled_borders };
3329                                 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3330                                         CapButt, JoinMiter);
3331                                 XSetDashes(display, window.gc, 0, ss, 2);
3332                         } else {
3333                                 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3334                                         CapButt, JoinMiter);
3335                         }
3336
3337                         XDrawRectangle(display, window.drawable, window.gc,
3338                                 text_start_x - window.border_inner_margin - window.border_width,
3339                                 text_start_y - window.border_inner_margin - window.border_width,
3340                                 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3341                                 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3342                 }
3343
3344                 /* draw text */
3345         }
3346         setup_fonts();
3347 #endif /* X11 */
3348 #ifdef NCURSES
3349         init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3350         attron(COLOR_PAIR(COLOR_WHITE));
3351 #endif /* NCURSES */
3352         for_each_line(text_buffer, draw_line);
3353 #if defined(HAVE_LUA) && defined(X11)
3354         llua_draw_post_hook();
3355 #endif /* HAVE_LUA */
3356 }
3357
3358 static void draw_stuff(void)
3359 {
3360 #ifdef IMLIB2
3361         cimlib_render(text_start_x, text_start_y, window.width, window.height);
3362 #endif /* IMLIB2 */
3363         if (overwrite_file) {
3364                 overwrite_fpointer = fopen(overwrite_file, "w");
3365                 if(!overwrite_fpointer)
3366                         NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3367         }
3368         if (append_file) {
3369                 append_fpointer = fopen(append_file, "a");
3370                 if(!append_fpointer)
3371                         NORM_ERR("Can't append '%s' anymore", append_file);
3372         }
3373 #ifdef X11
3374         if (output_methods & TO_X) {
3375                 selected_font = 0;
3376                 if (draw_shades && !draw_outline) {
3377                         text_start_x++;
3378                         text_start_y++;
3379                         set_foreground_color(default_bg_color);
3380                         draw_mode = BG;
3381                         draw_text();
3382                         text_start_x--;
3383                         text_start_y--;
3384                 }
3385
3386                 if (draw_outline) {
3387                         int i, j;
3388                         selected_font = 0;
3389
3390                         for (i = -1; i < 2; i++) {
3391                                 for (j = -1; j < 2; j++) {
3392                                         if (i == 0 && j == 0) {
3393                                                 continue;
3394                                         }
3395                                         text_start_x += i;
3396                                         text_start_y += j;
3397                                         set_foreground_color(default_out_color);
3398                                         draw_mode = OUTLINE;
3399                                         draw_text();
3400                                         text_start_x -= i;
3401                                         text_start_y -= j;
3402                                 }
3403                         }
3404                 }
3405
3406                 set_foreground_color(default_fg_color);
3407         }
3408 #endif /* X11 */
3409         draw_mode = FG;
3410         draw_text();
3411 #if defined(X11) && defined(HAVE_XDBE)
3412         if (output_methods & TO_X) {
3413                 xdbe_swap_buffers();
3414         }
3415 #endif /* X11 && HAVE_XDBE */
3416         if(overwrite_fpointer) {
3417                 fclose(overwrite_fpointer);
3418                 overwrite_fpointer = 0;
3419         }
3420         if(append_fpointer) {
3421                 fclose(append_fpointer);
3422                 append_fpointer = 0;
3423         }
3424 }
3425
3426 #ifdef X11
3427 static void clear_text(int exposures)
3428 {
3429 #ifdef HAVE_XDBE
3430         if (use_xdbe) {
3431                 /* The swap action is XdbeBackground, which clears */
3432                 return;
3433         } else
3434 #endif
3435         if (display && window.window) { // make sure these are !null
3436                 /* there is some extra space for borders and outlines */
3437                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3438                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3439                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3440                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3441         }
3442 }
3443 #endif /* X11 */
3444
3445 static int need_to_update;
3446
3447 /* update_text() generates new text and clears old text area */
3448 static void update_text(void)
3449 {
3450 #ifdef IMLIB2
3451         cimlib_cleanup();
3452 #endif /* IMLIB2 */
3453         generate_text();
3454 #ifdef X11
3455         if (output_methods & TO_X)
3456                 clear_text(1);
3457 #endif /* X11 */
3458         need_to_update = 1;
3459 #ifdef HAVE_LUA
3460         llua_update_info(&info, update_interval);
3461 #endif /* HAVE_LUA */
3462 }
3463
3464 #ifdef HAVE_SYS_INOTIFY_H
3465 int inotify_fd;
3466 #endif
3467
3468 static void main_loop(void)
3469 {
3470         int terminate = 0;
3471 #ifdef SIGNAL_BLOCKING
3472         sigset_t newmask, oldmask;
3473 #endif
3474         double t;
3475 #ifdef HAVE_SYS_INOTIFY_H
3476         int inotify_config_wd = -1;
3477 #define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
3478 #define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
3479         char inotify_buff[INOTIFY_BUF_LEN];
3480 #endif /* HAVE_SYS_INOTIFY_H */
3481
3482
3483 #ifdef SIGNAL_BLOCKING
3484         sigemptyset(&newmask);
3485         sigaddset(&newmask, SIGINT);
3486         sigaddset(&newmask, SIGTERM);
3487         sigaddset(&newmask, SIGUSR1);
3488 #endif
3489
3490         last_update_time = 0.0;
3491         next_update_time = get_time();
3492         info.looped = 0;
3493         while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3494                 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3495                         char buf[max_user_text];
3496
3497                         get_battery_short_status(buf, max_user_text, "BAT0");
3498                         if(buf[0] == 'D') {
3499                                 update_interval = update_interval_bat;
3500                         } else {
3501                                 update_interval = update_interval_old;
3502                         }
3503                 }
3504                 info.looped++;
3505
3506 #ifdef SIGNAL_BLOCKING
3507                 /* block signals.  we will inspect for pending signals later */
3508                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3509                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3510                 }
3511 #endif
3512
3513 #ifdef X11
3514                 if (output_methods & TO_X) {
3515                         XFlush(display);
3516
3517                         /* wait for X event or timeout */
3518
3519                         if (!XPending(display)) {
3520                                 fd_set fdsr;
3521                                 struct timeval tv;
3522                                 int s;
3523                                 t = next_update_time - get_time();
3524
3525                                 if (t < 0) {
3526                                         t = 0;
3527                                 } else if (t > update_interval) {
3528                                         t = update_interval;
3529                                 }
3530
3531                                 tv.tv_sec = (long) t;
3532                                 tv.tv_usec = (long) (t * 1000000) % 1000000;
3533                                 FD_ZERO(&fdsr);
3534                                 FD_SET(ConnectionNumber(display), &fdsr);
3535
3536                                 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3537                                 if (s == -1) {
3538                                         if (errno != EINTR) {
3539                                                 NORM_ERR("can't select(): %s", strerror(errno));
3540                                         }
3541                                 } else {
3542                                         /* timeout */
3543                                         if (s == 0) {
3544                                                 update_text();
3545                                         }
3546                                 }
3547                         }
3548
3549                         if (need_to_update) {
3550 #ifdef OWN_WINDOW
3551                                 int wx = window.x, wy = window.y;
3552 #endif
3553
3554                                 need_to_update = 0;
3555                                 selected_font = 0;
3556                                 update_text_area();
3557 #ifdef OWN_WINDOW
3558                                 if (own_window) {
3559                                         int changed = 0;
3560
3561                                         /* resize window if it isn't right size */
3562                                         if (!fixed_size
3563                                                         && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3564                                                                 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3565                                                 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3566                                                 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3567                                                 draw_stuff(); /* redraw everything in our newly sized window */
3568                                                 XResizeWindow(display, window.window, window.width,
3569                                                                 window.height); /* resize window */
3570                                                 set_transparent_background(window.window);
3571 #ifdef HAVE_XDBE
3572                                                 /* swap buffers */
3573                                                 xdbe_swap_buffers();
3574 #endif
3575
3576                                                 changed++;
3577 #ifdef HAVE_LUA
3578                                                 /* update lua window globals */
3579                                                 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3580 #endif /* HAVE_LUA */
3581                                         }
3582
3583                                         /* move window if it isn't in right position */
3584                                         if (!fixed_pos && (window.x != wx || window.y != wy)) {
3585                                                 XMoveWindow(display, window.window, window.x, window.y);
3586                                                 changed++;
3587                                         }
3588
3589                                         /* update struts */
3590                                         if (changed && window.type == TYPE_PANEL) {
3591                                                 int sidenum = -1;
3592
3593                                                 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3594                                                 fflush(stderr);
3595
3596                                                 switch (text_alignment) {
3597                                                         case TOP_LEFT:
3598                                                         case TOP_RIGHT:
3599                                                         case TOP_MIDDLE:
3600                                                                 {
3601                                                                         sidenum = 2;
3602                                                                         break;
3603                                                                 }
3604                                                         case BOTTOM_LEFT:
3605                                                         case BOTTOM_RIGHT:
3606                                                         case BOTTOM_MIDDLE:
3607                                                                 {
3608                                                                         sidenum = 3;
3609                                                                         break;
3610                                                                 }
3611                                                         case MIDDLE_LEFT:
3612                                                                 {
3613                                                                         sidenum = 0;
3614                                                                         break;
3615                                                                 }
3616                                                         case MIDDLE_RIGHT:
3617                                                                 {
3618                                                                         sidenum = 1;
3619                                                                         break;
3620                                                                 }
3621                                                 }
3622
3623                                                 set_struts(sidenum);
3624                                         }
3625                                 }
3626 #endif
3627
3628                                 clear_text(1);
3629
3630 #ifdef HAVE_XDBE
3631                                 if (use_xdbe) {
3632                                         XRectangle r;
3633
3634                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3635                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3636                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3637                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3638                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3639                                 }
3640 #endif
3641                         }
3642
3643                         /* handle X events */
3644                         while (XPending(display)) {
3645                                 XEvent ev;
3646
3647                                 XNextEvent(display, &ev);
3648                                 switch (ev.type) {
3649                                         case Expose:
3650                                         {
3651                                                 XRectangle r;
3652                                                 r.x = ev.xexpose.x;
3653                                                 r.y = ev.xexpose.y;
3654                                                 r.width = ev.xexpose.width;
3655                                                 r.height = ev.xexpose.height;
3656                                                 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3657                                                 break;
3658                                         }
3659
3660                                         case PropertyNotify:
3661                                         {
3662                                                 if ( ev.xproperty.state == PropertyNewValue ) {
3663                                                         get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3664                                                 }
3665                                                 break;
3666                                         }
3667
3668 #ifdef OWN_WINDOW
3669                                         case ReparentNotify:
3670                                                 /* make background transparent */
3671                                                 if (own_window) {
3672                                                         set_transparent_background(window.window);
3673                                                 }
3674                                                 break;
3675
3676                                         case ConfigureNotify:
3677                                                 if (own_window) {
3678                                                         /* if window size isn't what expected, set fixed size */
3679                                                         if (ev.xconfigure.width != window.width
3680                                                                         || ev.xconfigure.height != window.height) {
3681                                                                 if (window.width != 0 && window.height != 0) {
3682                                                                         fixed_size = 1;
3683                                                                 }
3684
3685                                                                 /* clear old stuff before screwing up
3686                                                                  * size and pos */
3687                                                                 clear_text(1);
3688
3689                                                                 {
3690                                                                         XWindowAttributes attrs;
3691                                                                         if (XGetWindowAttributes(display,
3692                                                                                         window.window, &attrs)) {
3693                                                                                 window.width = attrs.width;
3694                                                                                 window.height = attrs.height;
3695                                                                         }
3696                                                                 }
3697
3698                                                                 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3699                                                                 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3700                                                                 if (text_width > maximum_width
3701                                                                                 && maximum_width > 0) {
3702                                                                         text_width = maximum_width;
3703                                                                 }
3704                                                         }
3705
3706                                                         /* if position isn't what expected, set fixed pos
3707                                                          * total_updates avoids setting fixed_pos when window
3708                                                          * is set to weird locations when started */
3709                                                         /* // this is broken
3710                                                         if (total_updates >= 2 && !fixed_pos
3711                                                                         && (window.x != ev.xconfigure.x
3712                                                                         || window.y != ev.xconfigure.y)
3713                                                                         && (ev.xconfigure.x != 0
3714                                                                         || ev.xconfigure.y != 0)) {
3715                                                                 fixed_pos = 1;
3716                                                         } */
3717                                                 }
3718                                                 break;
3719
3720                                         case ButtonPress:
3721                                                 if (own_window) {
3722                                                         /* if an ordinary window with decorations */
3723                                                         if ((window.type == TYPE_NORMAL &&
3724                                                                                 (!TEST_HINT(window.hints,
3725                                                                                                         HINT_UNDECORATED))) ||
3726                                                                         window.type == TYPE_DESKTOP) {
3727                                                                 /* allow conky to hold input focus. */
3728                                                                 break;
3729                                                         } else {
3730                                                                 /* forward the click to the desktop window */
3731                                                                 XUngrabPointer(display, ev.xbutton.time);
3732                                                                 ev.xbutton.window = window.desktop;
3733                                                                 ev.xbutton.x = ev.xbutton.x_root;
3734                                                                 ev.xbutton.y = ev.xbutton.y_root;
3735                                                                 XSendEvent(display, ev.xbutton.window, False,
3736                                                                         ButtonPressMask, &ev);
3737                                                                 XSetInputFocus(display, ev.xbutton.window,
3738                                                                         RevertToParent, ev.xbutton.time);
3739                                                         }
3740                                                 }
3741                                                 break;
3742
3743                                         case ButtonRelease:
3744                                                 if (own_window) {
3745                                                         /* if an ordinary window with decorations */
3746                                                         if ((window.type == TYPE_NORMAL)
3747                                                                         && (!TEST_HINT(window.hints,
3748                                                                         HINT_UNDECORATED))) {
3749                                                                 /* allow conky to hold input focus. */
3750                                                                 break;
3751                                                         } else {
3752                                                                 /* forward the release to the desktop window */
3753                                                                 ev.xbutton.window = window.desktop;
3754                                                                 ev.xbutton.x = ev.xbutton.x_root;
3755                                                                 ev.xbutton.y = ev.xbutton.y_root;
3756                                                                 XSendEvent(display, ev.xbutton.window, False,
3757                                                                         ButtonReleaseMask, &ev);
3758                                                         }
3759                                                 }
3760                                                 break;
3761
3762 #endif
3763
3764                                         default:
3765 #ifdef HAVE_XDAMAGE
3766                                                 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3767                                                         XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3768
3769                                                         XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3770                                                         XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3771                                                 }
3772 #endif /* HAVE_XDAMAGE */
3773                                                 break;
3774                                 }
3775                         }
3776
3777 #ifdef HAVE_XDAMAGE
3778                         XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3779                         XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3780 #endif /* HAVE_XDAMAGE */
3781
3782                         /* XDBE doesn't seem to provide a way to clear the back buffer
3783                          * without interfering with the front buffer, other than passing
3784                          * XdbeBackground to XdbeSwapBuffers. That means that if we're
3785                          * using XDBE, we need to redraw the text even if it wasn't part of
3786                          * the exposed area. OTOH, if we're not going to call draw_stuff at
3787                          * all, then no swap happens and we can safely do nothing. */
3788
3789                         if (!XEmptyRegion(x11_stuff.region)) {
3790 #ifdef HAVE_XDBE
3791                                 if (use_xdbe) {
3792                                         XRectangle r;
3793
3794                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3795                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3796                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3797                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3798                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3799                                 }
3800 #endif
3801                                 XSetRegion(display, window.gc, x11_stuff.region);
3802 #ifdef XFT
3803                                 if (use_xft) {
3804                                         XftDrawSetClip(window.xftdraw, x11_stuff.region);
3805                                 }
3806 #endif
3807                                 draw_stuff();
3808                                 XDestroyRegion(x11_stuff.region);
3809                                 x11_stuff.region = XCreateRegion();
3810                         }
3811                 } else {
3812 #endif /* X11 */
3813                         t = (next_update_time - get_time()) * 1000000;
3814                         if(t > 0) usleep((useconds_t)t);
3815                         update_text();
3816                         draw_stuff();
3817 #ifdef NCURSES
3818                         if(output_methods & TO_NCURSES) {
3819                                 refresh();
3820                                 clear();
3821                         }
3822 #endif
3823 #ifdef X11
3824                 }
3825 #endif /* X11 */
3826
3827 #ifdef SIGNAL_BLOCKING
3828                 /* unblock signals of interest and let handler fly */
3829                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3830                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3831                 }
3832 #endif
3833
3834                 switch (g_signal_pending) {
3835                         case SIGHUP:
3836                         case SIGUSR1:
3837                                 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3838                                 reload_config();
3839                                 break;
3840                         case SIGINT:
3841                         case SIGTERM:
3842                                 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3843                                 terminate = 1;
3844 #ifdef X11
3845                                 if (output_methods & TO_X) {
3846                                         XDestroyRegion(x11_stuff.region);
3847                                         x11_stuff.region = NULL;
3848 #ifdef HAVE_XDAMAGE
3849                                         XDamageDestroy(display, x11_stuff.damage);
3850                                         XFixesDestroyRegion(display, x11_stuff.region2);
3851                                         XFixesDestroyRegion(display, x11_stuff.part);
3852 #endif /* HAVE_XDAMAGE */
3853                                         if (disp) {
3854                                                 free(disp);
3855                                         }
3856                                 }
3857 #endif /* X11 */
3858                                 if(overwrite_file) {
3859                                         free(overwrite_file);
3860                                         overwrite_file = 0;
3861                                 }
3862                                 if(append_file) {
3863                                         free(append_file);
3864                                         append_file = 0;
3865                                 }
3866                                 break;
3867                         default:
3868                                 /* Reaching here means someone set a signal
3869                                  * (SIGXXXX, signal_handler), but didn't write any code
3870                                  * to deal with it.
3871                                  * If you don't want to handle a signal, don't set a handler on
3872                                  * it in the first place. */
3873                                 if (g_signal_pending) {
3874                                         NORM_ERR("ignoring signal (%d)", g_signal_pending);
3875                                 }
3876                                 break;
3877                 }
3878 #ifdef HAVE_SYS_INOTIFY_H
3879                 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3880                         inotify_config_wd = inotify_add_watch(inotify_fd,
3881                                         current_config,
3882                                         IN_MODIFY);
3883                 }
3884                 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3885                         int len = 0, idx = 0;
3886                         fd_set descriptors;
3887                         struct timeval time_to_wait;
3888
3889                         FD_ZERO(&descriptors);
3890                         FD_SET(inotify_fd, &descriptors);
3891
3892                         time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3893
3894                         select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3895                         if (FD_ISSET(inotify_fd, &descriptors)) {
3896                                 /* process inotify events */
3897                                 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3898                                 while (len > 0 && idx < len) {
3899                                         struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3900                                         if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3901                                                 /* current_config should be reloaded */
3902                                                 NORM_ERR("'%s' modified, reloading...", current_config);
3903                                                 reload_config();
3904                                                 if (ev->mask & IN_IGNORED) {
3905                                                         /* for some reason we get IN_IGNORED here
3906                                                          * sometimes, so we need to re-add the watch */
3907                                                         inotify_config_wd = inotify_add_watch(inotify_fd,
3908                                                                         current_config,
3909                                                                         IN_MODIFY);
3910                                                 }
3911                                         }
3912 #ifdef HAVE_LUA
3913                                         else {
3914                                                 llua_inotify_query(ev->wd, ev->mask);
3915                                         }
3916 #endif /* HAVE_LUA */
3917                                         idx += INOTIFY_EVENT_SIZE + ev->len;
3918                                 }
3919                         }
3920                 }
3921 #endif /* HAVE_SYS_INOTIFY_H */
3922
3923 #ifdef HAVE_LUA
3924                 llua_update_info(&info, update_interval);
3925 #endif /* HAVE_LUA */
3926                 g_signal_pending = 0;
3927         }
3928         clean_up(NULL, NULL);
3929
3930 #ifdef HAVE_SYS_INOTIFY_H
3931         if (inotify_fd != -1) {
3932                 inotify_rm_watch(inotify_fd, inotify_config_wd);
3933                 close(inotify_fd);
3934                 inotify_fd = inotify_config_wd = 0;
3935         }
3936 #endif /* HAVE_SYS_INOTIFY_H */
3937 }
3938
3939 #ifdef X11
3940 static void load_config_file_x11(const char *);
3941 #endif /* X11 */
3942 void initialisation(int argc, char** argv);
3943
3944         /* reload the config file */
3945 static void reload_config(void)
3946 {
3947         char *current_config_copy = strdup(current_config);
3948         clean_up(NULL, NULL);
3949         current_config = current_config_copy;
3950         initialisation(argc_copy, argv_copy);
3951 }
3952
3953 void clean_up(void *memtofree1, void* memtofree2)
3954 {
3955         int i;
3956
3957 #ifdef NCURSES
3958         if(output_methods & TO_NCURSES) {
3959                 endwin();
3960         }
3961 #endif
3962         conftree_empty(currentconffile);
3963         currentconffile = NULL;
3964         if(memtofree1) {
3965                 free(memtofree1);
3966         }
3967         if(memtofree2) {
3968                 free(memtofree2);
3969         }
3970         timed_thread_destroy_registered_threads();
3971
3972         if (info.cpu_usage) {
3973                 free(info.cpu_usage);
3974                 info.cpu_usage = NULL;
3975         }
3976 #ifdef X11
3977         if (x_initialised == YES) {
3978                 if(window_created == 1) {
3979                         XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3980                                 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3981                                 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3982                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3983                 }
3984                 destroy_window();
3985                 free_fonts();
3986                 if(x11_stuff.region) {
3987                         XDestroyRegion(x11_stuff.region);
3988                         x11_stuff.region = NULL;
3989                 }
3990                 XCloseDisplay(display);
3991                 display = NULL;
3992                 if(info.x11.desktop.all_names) {
3993                         free(info.x11.desktop.all_names);
3994                         info.x11.desktop.all_names = NULL;
3995                 }
3996                 if (info.x11.desktop.name) {
3997                         free(info.x11.desktop.name);
3998                         info.x11.desktop.name = NULL;
3999                 }
4000                 x_initialised = NO;
4001         }else{
4002                 free(fonts);    //in set_default_configurations a font is set but not loaded
4003                 font_count = -1;
4004         }
4005
4006 #endif /* X11 */
4007
4008         free_update_callbacks();
4009
4010         free_templates();
4011
4012         free_text_objects(&global_root_object, 0);
4013         if (tmpstring1) {
4014                 free(tmpstring1);
4015                 tmpstring1 = 0;
4016         }
4017         if (tmpstring2) {
4018                 free(tmpstring2);
4019                 tmpstring2 = 0;
4020         }
4021         if (text_buffer) {
4022                 free(text_buffer);
4023                 text_buffer = 0;
4024         }
4025
4026         if (global_text) {
4027                 free(global_text);
4028                 global_text = 0;
4029         }
4030
4031         free(current_config);
4032         current_config = 0;
4033
4034 #ifdef TCP_PORT_MONITOR
4035         tcp_portmon_clear();
4036 #endif
4037 #ifdef HAVE_CURL
4038         ccurl_free_info();
4039 #endif
4040 #ifdef RSS
4041         rss_free_info();
4042 #endif
4043 #ifdef WEATHER
4044         weather_free_info();
4045 #endif
4046 #ifdef HAVE_LUA
4047         llua_shutdown_hook();
4048         llua_close();
4049 #endif /* HAVE_LUA */
4050 #ifdef IMLIB2
4051         if (output_methods & TO_X)
4052                 cimlib_deinit();
4053 #endif /* IMLIB2 */
4054 #ifdef XOAP
4055         xmlCleanupParser();
4056 #endif /* XOAP */
4057
4058         if (specials) {
4059                 for (i = 0; i < special_count; i++) {
4060                         if (specials[i].type == GRAPH) {
4061                                 free(specials[i].graph);
4062                         }
4063                 }
4064                 free(specials);
4065                 specials = NULL;
4066         }
4067
4068         clear_net_stats();
4069         clear_diskio_stats();
4070         if(global_cpu != NULL) {
4071                 free(global_cpu);
4072                 global_cpu = NULL;
4073         }
4074 }
4075
4076 static int string_to_bool(const char *s)
4077 {
4078         if (!s) {
4079                 // Assumes an option without a true/false means true
4080                 return 1;
4081         } else if (strcasecmp(s, "yes") == EQUAL) {
4082                 return 1;
4083         } else if (strcasecmp(s, "true") == EQUAL) {
4084                 return 1;
4085         } else if (strcasecmp(s, "1") == EQUAL) {
4086                 return 1;
4087         }
4088         return 0;
4089 }
4090
4091 #ifdef X11
4092 static enum alignment string_to_alignment(const char *s)
4093 {
4094         if (strcasecmp(s, "top_left") == EQUAL) {
4095                 return TOP_LEFT;
4096         } else if (strcasecmp(s, "top_right") == EQUAL) {
4097                 return TOP_RIGHT;
4098         } else if (strcasecmp(s, "top_middle") == EQUAL) {
4099                 return TOP_MIDDLE;
4100         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4101                 return BOTTOM_LEFT;
4102         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4103                 return BOTTOM_RIGHT;
4104         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4105                 return BOTTOM_MIDDLE;
4106         } else if (strcasecmp(s, "middle_left") == EQUAL) {
4107                 return MIDDLE_LEFT;
4108         } else if (strcasecmp(s, "middle_right") == EQUAL) {
4109                 return MIDDLE_RIGHT;
4110         } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4111                 return MIDDLE_MIDDLE;
4112         } else if (strcasecmp(s, "tl") == EQUAL) {
4113                 return TOP_LEFT;
4114         } else if (strcasecmp(s, "tr") == EQUAL) {
4115                 return TOP_RIGHT;
4116         } else if (strcasecmp(s, "tm") == EQUAL) {
4117                 return TOP_MIDDLE;
4118         } else if (strcasecmp(s, "bl") == EQUAL) {
4119                 return BOTTOM_LEFT;
4120         } else if (strcasecmp(s, "br") == EQUAL) {
4121                 return BOTTOM_RIGHT;
4122         } else if (strcasecmp(s, "bm") == EQUAL) {
4123                 return BOTTOM_MIDDLE;
4124         } else if (strcasecmp(s, "ml") == EQUAL) {
4125                 return MIDDLE_LEFT;
4126         } else if (strcasecmp(s, "mr") == EQUAL) {
4127                 return MIDDLE_RIGHT;
4128         } else if (strcasecmp(s, "mm") == EQUAL) {
4129                 return MIDDLE_MIDDLE;
4130         } else if (strcasecmp(s, "none") == EQUAL) {
4131                 return NONE;
4132         }
4133         return TOP_LEFT;
4134 }
4135 #endif /* X11 */
4136
4137 #ifdef X11
4138 static void set_default_configurations_for_x(void)
4139 {
4140         default_fg_color = WhitePixel(display, screen);
4141         default_bg_color = BlackPixel(display, screen);
4142         default_out_color = BlackPixel(display, screen);
4143         color0 = default_fg_color;
4144         color1 = default_fg_color;
4145         color2 = default_fg_color;
4146         color3 = default_fg_color;
4147         color4 = default_fg_color;
4148         color5 = default_fg_color;
4149         color6 = default_fg_color;
4150         color7 = default_fg_color;
4151         color8 = default_fg_color;
4152         color9 = default_fg_color;
4153         current_text_color = default_fg_color;
4154 }
4155 #endif /* X11 */
4156
4157 static void set_default_configurations(void)
4158 {
4159 #ifdef MPD
4160         char *mpd_env_host;
4161         char *mpd_env_port;
4162 #endif
4163         update_uname();
4164         fork_to_background = 0;
4165         total_run_times = 0;
4166         info.cpu_avg_samples = 2;
4167         info.net_avg_samples = 2;
4168         info.diskio_avg_samples = 2;
4169         info.memmax = 0;
4170         top_cpu = 0;
4171         cpu_separate = 0;
4172         short_units = 0;
4173         format_human_readable = 1;
4174         top_mem = 0;
4175         top_time = 0;
4176 #ifdef IOSTATS
4177         top_io = 0;
4178 #endif
4179 #ifdef __linux__
4180         top_running = 0;
4181 #endif
4182 #ifdef MPD
4183         mpd_env_host = getenv("MPD_HOST");
4184         mpd_env_port = getenv("MPD_PORT");
4185
4186         if (!mpd_env_host || !strlen(mpd_env_host)) {
4187                 mpd_set_host("localhost");
4188         } else {
4189                 /* MPD_HOST environment variable is set */
4190                 char *mpd_hostpart = strchr(mpd_env_host, '@');
4191                 if (!mpd_hostpart) {
4192                         mpd_set_host(mpd_env_host);
4193                 } else {
4194                         /* MPD_HOST contains a password */
4195                         char mpd_password[mpd_hostpart - mpd_env_host + 1];
4196                         snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4197
4198                         if (!strlen(mpd_hostpart + 1)) {
4199                                 mpd_set_host("localhost");
4200                         } else {
4201                                 mpd_set_host(mpd_hostpart + 1);
4202                         }
4203
4204                         mpd_set_password(mpd_password, 1);
4205                 }
4206         }
4207
4208
4209         if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4210                 /* failed to set port from environment variable */
4211                 mpd_set_port("6600");
4212         }
4213 #endif
4214 #ifdef XMMS2
4215         info.xmms2.artist = NULL;
4216         info.xmms2.album = NULL;
4217         info.xmms2.title = NULL;
4218         info.xmms2.genre = NULL;
4219         info.xmms2.comment = NULL;
4220         info.xmms2.url = NULL;
4221         info.xmms2.status = NULL;
4222         info.xmms2.playlist = NULL;
4223 #endif
4224         use_spacer = NO_SPACER;
4225 #ifdef X11
4226         output_methods = TO_X;
4227 #else
4228         output_methods = TO_STDOUT;
4229 #endif
4230 #ifdef X11
4231         show_graph_scale = 0;
4232         show_graph_range = 0;
4233         draw_shades = 1;
4234         draw_borders = 0;
4235         draw_graph_borders = 1;
4236         draw_outline = 0;
4237         set_first_font("6x10");
4238         gap_x = 5;
4239         gap_y = 60;
4240         minimum_width = 5;
4241         minimum_height = 5;
4242         maximum_width = 0;
4243 #ifdef OWN_WINDOW
4244         own_window = 0;
4245         window.type = TYPE_NORMAL;
4246         window.hints = 0;
4247         strcpy(window.class_name, PACKAGE_NAME);
4248         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4249 #ifdef USE_ARGB
4250         use_argb_visual = 0;
4251 #endif
4252 #endif
4253         stippled_borders = 0;
4254         window.border_inner_margin = 3;
4255         window.border_outer_margin = 1;
4256         window.border_width = 1;
4257         text_alignment = BOTTOM_LEFT;
4258         info.x11.monitor.number = 1;
4259         info.x11.monitor.current = 0;
4260         info.x11.desktop.current = 1; 
4261         info.x11.desktop.number = 1;
4262         info.x11.desktop.nitems = 0;
4263         info.x11.desktop.all_names = NULL; 
4264         info.x11.desktop.name = NULL; 
4265 #endif /* X11 */
4266
4267         free_templates();
4268
4269         free(current_mail_spool);
4270         {
4271                 char buf[256];
4272
4273                 variable_substitute(MAIL_FILE, buf, 256);
4274                 if (buf[0] != '\0') {
4275                         current_mail_spool = strndup(buf, text_buffer_size);
4276                 }
4277         }
4278
4279         no_buffers = 1;
4280         set_update_interval(3);
4281         update_interval_bat = NOBATTERY;
4282         info.music_player_interval = 1.0;
4283         stuff_in_uppercase = 0;
4284         info.users.number = 1;
4285
4286         set_times_in_seconds(0);
4287
4288 #ifdef TCP_PORT_MONITOR
4289         /* set default connection limit */
4290         tcp_portmon_set_max_connections(0);
4291 #endif
4292 }
4293
4294 /* returns 1 if you can overwrite or create the file at 'path' */
4295 static _Bool overwrite_works(const char *path)
4296 {
4297         FILE *filepointer;
4298
4299         if (!(filepointer = fopen(path, "w")))
4300                 return 0;
4301         fclose(filepointer);
4302         return 1;
4303 }
4304
4305 /* returns 1 if you can append or create the file at 'path' */
4306 static _Bool append_works(const char *path)
4307 {
4308         FILE *filepointer;
4309
4310         if (!(filepointer = fopen(path, "a")))
4311                 return 0;
4312         fclose(filepointer);
4313         return 1;
4314 }
4315
4316 #ifdef X11
4317 #ifdef DEBUG
4318 /* WARNING, this type not in Xlib spec */
4319 int x11_error_handler(Display *d, XErrorEvent *err)
4320         __attribute__((noreturn));
4321 int x11_error_handler(Display *d, XErrorEvent *err)
4322 {
4323         NORM_ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
4324                         err->type,
4325                         (long unsigned)err->display,
4326                         (long)err->resourceid,
4327                         err->serial,
4328                         err->error_code,
4329                         err->request_code,
4330                         err->minor_code,
4331                         (long unsigned)d
4332                         );
4333         abort();
4334 }
4335
4336 int x11_ioerror_handler(Display *d)
4337         __attribute__((noreturn));
4338 int x11_ioerror_handler(Display *d)
4339 {
4340         NORM_ERR("X Error: Display %lx\n",
4341                         (long unsigned)d
4342                         );
4343         exit(1);
4344 }
4345 #endif /* DEBUG */
4346
4347 static void X11_initialisation(void)
4348 {
4349         if (x_initialised == YES) return;
4350         output_methods |= TO_X;
4351         init_X11(disp);
4352         set_default_configurations_for_x();
4353         x_initialised = YES;
4354 #ifdef DEBUG
4355         _Xdebug = 1;
4356         /* WARNING, this type not in Xlib spec */
4357         XSetErrorHandler(&x11_error_handler);
4358         XSetIOErrorHandler(&x11_ioerror_handler);
4359 #endif /* DEBUG */
4360 }
4361
4362 static char **xargv = 0;
4363 static int xargc = 0;
4364
4365 static void X11_create_window(void)
4366 {
4367         if (output_methods & TO_X) {
4368 #ifdef OWN_WINDOW
4369                 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4370                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4371                                 xargv, xargc);
4372 #else /* OWN_WINDOW */
4373                 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4374                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4375                                 xargv, xargc);
4376 #endif /* OWN_WINDOW */
4377
4378                 setup_fonts();
4379                 load_fonts();
4380                 update_text_area();     /* to position text/window on screen */
4381
4382 #ifdef OWN_WINDOW
4383                 if (own_window && !fixed_pos) {
4384                         XMoveWindow(display, window.window, window.x, window.y);
4385                 }
4386                 if (own_window) {
4387                         set_transparent_background(window.window);
4388                 }
4389 #endif
4390
4391                 create_gc();
4392
4393                 draw_stuff();
4394
4395                 x11_stuff.region = XCreateRegion();
4396 #ifdef HAVE_XDAMAGE
4397                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4398                         NORM_ERR("Xdamage extension unavailable");
4399                 }
4400                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4401                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4402                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4403 #endif /* HAVE_XDAMAGE */
4404
4405                 selected_font = 0;
4406                 update_text_area();     /* to get initial size of the window */
4407         }
4408 #ifdef HAVE_LUA
4409         /* setup lua window globals */
4410         llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4411 #endif /* HAVE_LUA */
4412 }
4413 #endif /* X11 */
4414
4415 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4416 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4417 #define CONF2(a) if (strcasecmp(name, a) == 0)
4418 #define CONF(a) else CONF2(a)
4419 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4420                 || strcasecmp(name, b) == 0)
4421 #define CONF_CONTINUE 1
4422 #define CONF_BREAK 2
4423 #define CONF_BUFF_SIZE 512
4424
4425 static FILE *open_config_file(const char *f)
4426 {
4427 #ifdef CONFIG_OUTPUT
4428         if (!strcmp(f, "==builtin==")) {
4429                 return conf_cookie_open();
4430         } else
4431 #endif /* CONFIG_OUTPUT */
4432                 return fopen(f, "r");
4433 }
4434
4435 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4436 {
4437         char *p, *p2;
4438         (*line)++;
4439         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4440                 return CONF_BREAK;
4441         }
4442         remove_comments(buf);
4443
4444         p = buf;
4445
4446         /* skip spaces */
4447         while (*p && isspace((int) *p)) {
4448                 p++;
4449         }
4450         if (*p == '\0') {
4451                 return CONF_CONTINUE;   /* empty line */
4452         }
4453
4454         *name = p;
4455
4456         /* skip name */
4457         p2 = p;
4458         while (*p2 && !isspace((int) *p2)) {
4459                 p2++;
4460         }
4461         if (*p2 != '\0') {
4462                 *p2 = '\0';     /* break at name's end */
4463                 p2++;
4464         }
4465
4466         /* get value */
4467         if (*p2) {
4468                 p = p2;
4469                 while (*p && isspace((int) *p)) {
4470                         p++;
4471                 }
4472
4473                 *value = p;
4474
4475                 p2 = *value + strlen(*value);
4476                 while (isspace((int) *(p2 - 1))) {
4477                         *--p2 = '\0';
4478                 }
4479         } else {
4480                 *value = 0;
4481         }
4482         return 0;
4483 }
4484
4485 char load_config_file(const char *f)
4486 {
4487         int line = 0;
4488         FILE *fp;
4489
4490         fp = open_config_file(f);
4491         if (!fp) {
4492                 return FALSE;
4493         }
4494         DBGP("reading contents from config file '%s'", f);
4495
4496         while (!feof(fp)) {
4497                 char buff[CONF_BUFF_SIZE], *name, *value;
4498                 int ret = do_config_step(&line, fp, buff, &name, &value);
4499                 if (ret == CONF_BREAK) {
4500                         break;
4501                 } else if (ret == CONF_CONTINUE) {
4502                         continue;
4503                 }
4504
4505 #ifdef X11
4506                 CONF2("out_to_x") {
4507                         /* don't listen if X is already initialised or
4508                          * if we already know we don't want it */
4509                         if(x_initialised != YES) {
4510                                 if (string_to_bool(value)) {
4511                                         output_methods &= TO_X;
4512                                 } else {
4513                                         output_methods &= ~TO_X;
4514                                         x_initialised = NEVER;
4515                                 }
4516                         }
4517                 }
4518                 CONF("display") {
4519                         if (!value || x_initialised == YES) {
4520                                 CONF_ERR;
4521                         } else {
4522                                 if (disp)
4523                                         free(disp);
4524                                 disp = strdup(value);
4525                         }
4526                 }
4527                 CONF("alignment") {
4528 #ifdef OWN_WINDOW
4529                         if (window.type == TYPE_DOCK)
4530                                 ;
4531                         else
4532 #endif /*OWN_WINDOW */
4533                         if (value) {
4534                                 int a = string_to_alignment(value);
4535
4536                                 if (a <= 0) {
4537                                         CONF_ERR;
4538                                 } else {
4539                                         text_alignment = a;
4540                                 }
4541                         } else {
4542                                 CONF_ERR;
4543                         }
4544                 }
4545                 CONF("background") {
4546                         fork_to_background = string_to_bool(value);
4547                 }
4548 #else
4549                 CONF2("background") {
4550                         fork_to_background = string_to_bool(value);
4551                 }
4552 #endif /* X11 */
4553 #ifdef X11
4554                 CONF("show_graph_scale") {
4555                         show_graph_scale = string_to_bool(value);
4556                 }
4557                 CONF("show_graph_range") {
4558                         show_graph_range = string_to_bool(value);
4559                 }
4560                 CONF("border_inner_margin") {
4561                         if (value) {
4562                                 window.border_inner_margin = strtol(value, 0, 0);
4563                                 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4564                         } else {
4565                                 CONF_ERR;
4566                         }
4567                 }
4568                 CONF("border_outer_margin") {
4569                         if (value) {
4570                                 window.border_outer_margin = strtol(value, 0, 0);
4571                                 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4572                         } else {
4573                                 CONF_ERR;
4574                         }
4575                 }
4576                 CONF("border_width") {
4577                         if (value) {
4578                                 window.border_width = strtol(value, 0, 0);
4579                                 if (window.border_width < 0) window.border_width = 0;
4580                         } else {
4581                                 CONF_ERR;
4582                         }
4583                 }
4584 #endif /* X11 */
4585 #define TEMPLATE_CONF(n) \
4586                 CONF("template"#n) { \
4587                         if (set_template(n, value)) \
4588                                 CONF_ERR; \
4589                 }
4590                 TEMPLATE_CONF(0)
4591                 TEMPLATE_CONF(1)
4592                 TEMPLATE_CONF(2)
4593                 TEMPLATE_CONF(3)
4594                 TEMPLATE_CONF(4)
4595                 TEMPLATE_CONF(5)
4596                 TEMPLATE_CONF(6)
4597                 TEMPLATE_CONF(7)
4598                 TEMPLATE_CONF(8)
4599                 TEMPLATE_CONF(9)
4600                 CONF("imap") {
4601                         if (value) {
4602                                 parse_global_imap_mail_args(value);
4603                         } else {
4604                                 CONF_ERR;
4605                         }
4606                 }
4607                 CONF("pop3") {
4608                         if (value) {
4609                                 parse_global_pop3_mail_args(value);
4610                         } else {
4611                                 CONF_ERR;
4612                         }
4613                 }
4614                 CONF("default_bar_size") {
4615                         char err = 0;
4616                         if (value) {
4617                                 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4618                                         err = 1;
4619                                 }
4620                         } else {
4621                                 err = 1;
4622                         }
4623                         if (err) {
4624                                 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4625                         }
4626                 }
4627 #ifdef X11
4628                 CONF("default_graph_size") {
4629                         char err = 0;
4630                         if (value) {
4631                                 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4632                                         err = 1;
4633                                 }
4634                         } else {
4635                                 err = 1;
4636                         }
4637                         if (err) {
4638                                 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4639                         }
4640                 }
4641                 CONF("default_gauge_size") {
4642                         char err = 0;
4643                         if (value) {
4644                                 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4645                                         err = 1;
4646                                 }
4647                         } else {
4648                                 err = 1;
4649                         }
4650                         if (err) {
4651                                 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4652                         }
4653                 }
4654 #endif
4655 #ifdef MPD
4656                 CONF("mpd_host") {
4657                         if (value) {
4658                                 mpd_set_host(value);
4659                         } else {
4660                                 CONF_ERR;
4661                         }
4662                 }
4663                 CONF("mpd_port") {
4664                         if (value && mpd_set_port(value)) {
4665                                 CONF_ERR;
4666                         }
4667                 }
4668                 CONF("mpd_password") {
4669                         if (value) {
4670                                 mpd_set_password(value, 0);
4671                         } else {
4672                                 CONF_ERR;
4673                         }
4674                 }
4675 #endif
4676                 CONF("music_player_interval") {
4677                         if (value) {
4678                                 info.music_player_interval = strtod(value, 0);
4679                         } else {
4680                                 CONF_ERR;
4681                         }
4682                 }
4683 #ifdef __OpenBSD__
4684                 CONF("sensor_device") {
4685                         if (value) {
4686                                 sensor_device = strtol(value, 0, 0);
4687                         } else {
4688                                 CONF_ERR;
4689                         }
4690                 }
4691 #endif
4692                 CONF("cpu_avg_samples") {
4693                         if (value) {
4694                                 cpu_avg_samples = strtol(value, 0, 0);
4695                                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4696                                         CONF_ERR;
4697                                 } else {
4698                                         info.cpu_avg_samples = cpu_avg_samples;
4699                                 }
4700                         } else {
4701                                 CONF_ERR;
4702                         }
4703                 }
4704                 CONF("net_avg_samples") {
4705                         if (value) {
4706                                 net_avg_samples = strtol(value, 0, 0);
4707                                 if (net_avg_samples < 1 || net_avg_samples > 14) {
4708                                         CONF_ERR;
4709                                 } else {
4710                                         info.net_avg_samples = net_avg_samples;
4711                                 }
4712                         } else {
4713                                 CONF_ERR;
4714                         }
4715                 }
4716                 CONF("diskio_avg_samples") {
4717                         if (value) {
4718                                 diskio_avg_samples = strtol(value, 0, 0);
4719                                 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4720                                         CONF_ERR;
4721                                 } else {
4722                                         info.diskio_avg_samples = diskio_avg_samples;
4723                                 }
4724                         } else {
4725                                 CONF_ERR;
4726                         }
4727                 }
4728
4729 #ifdef HAVE_XDBE
4730                 CONF("double_buffer") {
4731                         use_xdbe = string_to_bool(value);
4732                 }
4733 #endif
4734 #ifdef X11
4735                 CONF("override_utf8_locale") {
4736                         utf8_mode = string_to_bool(value);
4737                 }
4738                 CONF("draw_borders") {
4739                         draw_borders = string_to_bool(value);
4740                 }
4741                 CONF("draw_graph_borders") {
4742                         draw_graph_borders = string_to_bool(value);
4743                 }
4744                 CONF("draw_shades") {
4745                         draw_shades = string_to_bool(value);
4746                 }
4747                 CONF("draw_outline") {
4748                         draw_outline = string_to_bool(value);
4749                 }
4750 #endif /* X11 */
4751                 CONF("times_in_seconds") {
4752                         set_times_in_seconds(string_to_bool(value));
4753                 }
4754                 CONF("max_text_width") {
4755                         max_text_width = atoi(value);
4756                 }
4757                 CONF("out_to_console") {
4758                         if(string_to_bool(value)) {
4759                                 output_methods |= TO_STDOUT;
4760                         } else {
4761                                 output_methods &= ~TO_STDOUT;
4762                         }
4763                 }
4764                 CONF("extra_newline") {
4765                         extra_newline = string_to_bool(value);
4766                 }
4767                 CONF("out_to_stderr") {
4768                         if(string_to_bool(value))
4769                                 output_methods |= TO_STDERR;
4770                 }
4771 #ifdef NCURSES
4772                 CONF("out_to_ncurses") {
4773                         if(string_to_bool(value)) {
4774                                 initscr();
4775                                 start_color();
4776                                 output_methods |= TO_NCURSES;
4777                         }
4778                 }
4779 #endif
4780                 CONF("overwrite_file") {
4781                         if(overwrite_file) {
4782                                 free(overwrite_file);
4783                                 overwrite_file = 0;
4784                         }
4785                         if(overwrite_works(value)) {
4786                                 overwrite_file = strdup(value);
4787                                 output_methods |= OVERWRITE_FILE;
4788                         } else
4789                                 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4790                 }
4791                 CONF("append_file") {
4792                         if(append_file) {
4793                                 free(append_file);
4794                                 append_file = 0;
4795                         }
4796                         if(append_works(value)) {
4797                                 append_file = strdup(value);
4798                                 output_methods |= APPEND_FILE;
4799                         } else
4800                                 NORM_ERR("append_file won't be able to create/append '%s'", value);
4801                 }
4802                 CONF("use_spacer") {
4803                         if (value) {
4804                                 if (strcasecmp(value, "left") == EQUAL) {
4805                                         use_spacer = LEFT_SPACER;
4806                                 } else if (strcasecmp(value, "right") == EQUAL) {
4807                                         use_spacer = RIGHT_SPACER;
4808                                 } else if (strcasecmp(value, "none") == EQUAL) {
4809                                         use_spacer = NO_SPACER;
4810                                 } else {
4811                                         use_spacer = string_to_bool(value);
4812                                         NORM_ERR("use_spacer should have an argument of left, right, or"
4813                                                 " none.  '%s' seems to be some form of '%s', so"
4814                                                 " defaulting to %s.", value,
4815                                                 use_spacer ? "true" : "false",
4816                                                 use_spacer ? "right" : "none");
4817                                         if (use_spacer) {
4818                                                 use_spacer = RIGHT_SPACER;
4819                                         } else {
4820                                                 use_spacer = NO_SPACER;
4821                                         }
4822                                 }
4823                         } else {
4824                                 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4825                                 use_spacer = RIGHT_SPACER;
4826                         }
4827                 }
4828 #ifdef X11
4829 #ifdef XFT
4830                 CONF("use_xft") {
4831                         use_xft = string_to_bool(value);
4832                 }
4833                 CONF("font") {
4834                         if (value) {
4835                                 set_first_font(value);
4836                         }
4837                 }
4838                 CONF("xftalpha") {
4839                         if (value && font_count >= 0) {
4840                                 fonts[0].font_alpha = atof(value) * 65535.0;
4841                         }
4842                 }
4843                 CONF("xftfont") {
4844                         if (use_xft) {
4845 #else
4846                 CONF("use_xft") {
4847                         if (string_to_bool(value)) {
4848                                 NORM_ERR("Xft not enabled at compile time");
4849                         }
4850                 }
4851                 CONF("xftfont") {
4852                         /* xftfont silently ignored when no Xft */
4853                 }
4854                 CONF("xftalpha") {
4855                         /* xftalpha is silently ignored when no Xft */
4856                 }
4857                 CONF("font") {
4858 #endif
4859                         if (value) {
4860                                 set_first_font(value);
4861                         }
4862 #ifdef XFT
4863                         }
4864 #endif
4865                 }
4866                 CONF("gap_x") {
4867                         if (value) {
4868                                 gap_x = atoi(value);
4869                         } else {
4870                                 CONF_ERR;
4871                         }
4872                 }
4873                 CONF("gap_y") {
4874                         if (value) {
4875                                 gap_y = atoi(value);
4876                         } else {
4877                                 CONF_ERR;
4878                         }
4879                 }
4880 #endif /* X11 */
4881                 CONF("mail_spool") {
4882                         if (value) {
4883                                 char buffer[256];
4884
4885                                 variable_substitute(value, buffer, 256);
4886
4887                                 if (buffer[0] != '\0') {
4888                                         if (current_mail_spool) {
4889                                                 free(current_mail_spool);
4890                                         }
4891                                         current_mail_spool = strndup(buffer, text_buffer_size);
4892                                 }
4893                         } else {
4894                                 CONF_ERR;
4895                         }
4896                 }
4897 #ifdef X11
4898                 CONF("minimum_size") {
4899                         if (value) {
4900                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4901                                                 != 2) {
4902                                         if (sscanf(value, "%d", &minimum_width) != 1) {
4903                                                 CONF_ERR;
4904                                         }
4905                                 }
4906                         } else {
4907                                 CONF_ERR;
4908                         }
4909                 }
4910                 CONF("maximum_width") {
4911                         if (value) {
4912                                 if (sscanf(value, "%d", &maximum_width) != 1) {
4913                                         CONF_ERR;
4914                                 }
4915                         } else {
4916                                 CONF_ERR;
4917                         }
4918                 }
4919 #endif /* X11 */
4920                 CONF("no_buffers") {
4921                         no_buffers = string_to_bool(value);
4922                 }
4923                 CONF("top_name_width") {
4924                         if (set_top_name_width(value))
4925                                 CONF_ERR;
4926                 }
4927                 CONF("top_cpu_separate") {
4928                         cpu_separate = string_to_bool(value);
4929                 }
4930                 CONF("short_units") {
4931                         short_units = string_to_bool(value);
4932                 }
4933                 CONF("format_human_readable") {
4934                         format_human_readable = string_to_bool(value);
4935                 }
4936 #ifdef HDDTEMP
4937                 CONF("hddtemp_host") {
4938                         set_hddtemp_host(value);
4939                 }
4940                 CONF("hddtemp_port") {
4941                         set_hddtemp_port(value);
4942                 }
4943 #endif /* HDDTEMP */
4944                 CONF("pad_percents") {
4945                         pad_percents = atoi(value);
4946                 }
4947 #ifdef X11
4948 #ifdef OWN_WINDOW
4949                 CONF("own_window") {
4950                         if (value) {
4951                                 own_window = string_to_bool(value);
4952                         }
4953                 }
4954                 CONF("own_window_class") {
4955                         if (value) {
4956                                 memset(window.class_name, 0, sizeof(window.class_name));
4957                                 strncpy(window.class_name, value,
4958                                                 sizeof(window.class_name) - 1);
4959                         }
4960                 }
4961                 CONF("own_window_title") {
4962                         if (value) {
4963                                 memset(window.title, 0, sizeof(window.title));
4964                                 strncpy(window.title, value, sizeof(window.title) - 1);
4965                         }
4966                 }
4967                 CONF("own_window_transparent") {
4968                         if (value) {
4969                                 set_transparent = string_to_bool(value);
4970                         }
4971                 }
4972                 CONF("own_window_hints") {
4973                         if (value) {
4974                                 char *p_hint, *p_save;
4975                                 char delim[] = ", ";
4976
4977                                 /* tokenize the value into individual hints */
4978                                 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4979                                         do {
4980                                                 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4981                                                 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4982                                                         SET_HINT(window.hints, HINT_UNDECORATED);
4983                                                 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4984                                                         SET_HINT(window.hints, HINT_BELOW);
4985                                                 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4986                                                         SET_HINT(window.hints, HINT_ABOVE);
4987                                                 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4988                                                         SET_HINT(window.hints, HINT_STICKY);
4989                                                 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4990                                                         SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4991                                                 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4992                                                         SET_HINT(window.hints, HINT_SKIP_PAGER);
4993                                                 } else {
4994                                                         CONF_ERR;
4995                                                 }
4996
4997                                                 p_hint = strtok_r(NULL, delim, &p_save);
4998                                         } while (p_hint != NULL);
4999                                 }
5000                         } else {
5001                                 CONF_ERR;
5002                         }
5003                 }
5004                 CONF("own_window_type") {
5005                         if (value) {
5006                                 if (strncmp(value, "normal", 6) == EQUAL) {
5007                                         window.type = TYPE_NORMAL;
5008                                 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5009                                         window.type = TYPE_DESKTOP;
5010                                 } else if (strncmp(value, "dock", 4) == EQUAL) {
5011                                         window.type = TYPE_DOCK;
5012                                         text_alignment = TOP_LEFT;
5013                                 } else if (strncmp(value, "panel", 5) == EQUAL) {
5014                                         window.type = TYPE_PANEL;
5015                                 } else if (strncmp(value, "override", 8) == EQUAL) {
5016                                         window.type = TYPE_OVERRIDE;
5017                                 } else {
5018                                         CONF_ERR;
5019                                 }
5020                         } else {
5021                                 CONF_ERR;
5022                         }
5023                 }
5024 #ifdef USE_ARGB
5025                 CONF("own_window_argb_visual") {
5026                         use_argb_visual = string_to_bool(value);
5027                 }
5028 #endif /* USE_ARGB */
5029 #endif
5030                 CONF("stippled_borders") {
5031                         if (value) {
5032                                 stippled_borders = strtol(value, 0, 0);
5033                         } else {
5034                                 stippled_borders = 4;
5035                         }
5036                 }
5037 #ifdef IMLIB2
5038                 CONF("imlib_cache_size") {
5039                         if (value) {
5040                                 cimlib_set_cache_size(atoi(value));
5041                         }
5042                 }
5043                 CONF("imlib_cache_flush_interval") {
5044                         if (value) {
5045                                 cimlib_set_cache_flush_interval(atoi(value));
5046                         }
5047                 }
5048 #endif /* IMLIB2 */
5049 #endif /* X11 */
5050                 CONF("update_interval_on_battery") {
5051                         if (value) {
5052                                 update_interval_bat = strtod(value, 0);
5053                         } else {
5054                                 CONF_ERR;
5055                         }
5056                 }
5057                 CONF("update_interval") {
5058                         if (value) {
5059                                 set_update_interval(strtod(value, 0));
5060                         } else {
5061                                 CONF_ERR;
5062                         }
5063                         if (info.music_player_interval == 0) {
5064                                 // default to update_interval
5065                                 info.music_player_interval = update_interval;
5066                         }
5067                 }
5068                 CONF("total_run_times") {
5069                         if (value) {
5070                                 total_run_times = strtod(value, 0);
5071                         } else {
5072                                 CONF_ERR;
5073                         }
5074                 }
5075                 CONF("uppercase") {
5076                         stuff_in_uppercase = string_to_bool(value);
5077                 }
5078                 CONF("max_specials") {
5079                         if (value) {
5080                                 max_specials = atoi(value);
5081                         } else {
5082                                 CONF_ERR;
5083                         }
5084                 }
5085                 CONF("max_user_text") {
5086                         if (value) {
5087                                 max_user_text = atoi(value);
5088                         } else {
5089                                 CONF_ERR;
5090                         }
5091                 }
5092                 CONF("text_buffer_size") {
5093                         if (value) {
5094                                 text_buffer_size = atoi(value);
5095                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5096                                         NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5097                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5098                                 }
5099                         } else {
5100                                 CONF_ERR;
5101                         }
5102                 }
5103                 CONF("text") {
5104 #ifdef X11
5105                         if (output_methods & TO_X) {
5106                                 X11_initialisation();
5107                         }
5108 #endif
5109
5110                         if (global_text) {
5111                                 free(global_text);
5112                                 global_text = 0;
5113                         }
5114
5115                         global_text = (char *) malloc(1);
5116                         global_text[0] = '\0';
5117
5118                         while (!feof(fp)) {
5119                                 unsigned int l = strlen(global_text);
5120                                 unsigned int bl;
5121                                 char buf[CONF_BUFF_SIZE];
5122
5123                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5124                                         break;
5125                                 }
5126
5127                                 /* Remove \\-\n. */
5128                                 bl = strlen(buf);
5129                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5130                                         buf[bl-2] = '\0';
5131                                         bl -= 2;
5132                                         if (bl == 0) {
5133                                                 continue;
5134                                         }
5135                                 }
5136
5137                                 /* Check for continuation of \\-\n. */
5138                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5139                                         global_text[l-1] = '\0';
5140                                         continue;
5141                                 }
5142
5143                                 global_text = (char *) realloc(global_text, l + bl + 1);
5144                                 strcat(global_text, buf);
5145
5146                                 if (strlen(global_text) > max_user_text) {
5147                                         break;
5148                                 }
5149                         }
5150                         global_text_lines = line + 1;
5151                         break;
5152                 }
5153 #ifdef TCP_PORT_MONITOR
5154                 CONF("max_port_monitor_connections") {
5155                         int max;
5156                         if (!value || (sscanf(value, "%d", &max) != 1)) {
5157                                 /* an error. use default, warn and continue. */
5158                                 tcp_portmon_set_max_connections(0);
5159                                 CONF_ERR;
5160                         } else if (tcp_portmon_set_max_connections(max)) {
5161                                 /* max is < 0, default has been set*/
5162                                 CONF_ERR;
5163                         }
5164                 }
5165 #endif
5166                 CONF("if_up_strictness") {
5167                         if (!value) {
5168                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5169                                 ifup_strictness = IFUP_UP;
5170                         } else if (strcasecmp(value, "up") == EQUAL) {
5171                                 ifup_strictness = IFUP_UP;
5172                         } else if (strcasecmp(value, "link") == EQUAL) {
5173                                 ifup_strictness = IFUP_LINK;
5174                         } else if (strcasecmp(value, "address") == EQUAL) {
5175                                 ifup_strictness = IFUP_ADDR;
5176                         } else {
5177                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5178                                 ifup_strictness = IFUP_UP;
5179                         }
5180                 }
5181
5182                 CONF("temperature_unit") {
5183                         if (!value) {
5184                                 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5185                         } else if (set_temp_output_unit(value)) {
5186                                 NORM_ERR("temperature_unit: incorrect argument");
5187                         }
5188                 }
5189
5190 #ifdef HAVE_LUA
5191                 CONF("lua_load") {
5192                         if (value) {
5193                                 char *ptr = strtok(value, " ");
5194                                 while (ptr) {
5195                                         llua_load(ptr);
5196                                         ptr = strtok(NULL, " ");
5197                                 }
5198                         } else {
5199                                 CONF_ERR;
5200                         }
5201                 }
5202 #ifdef X11
5203                 CONF("lua_draw_hook_pre") {
5204                         if (value) {
5205                                 llua_set_draw_pre_hook(value);
5206                         } else {
5207                                 CONF_ERR;
5208                         }
5209                 }
5210                 CONF("lua_draw_hook_post") {
5211                         if (value) {
5212                                 llua_set_draw_post_hook(value);
5213                         } else {
5214                                 CONF_ERR;
5215                         }
5216                 }
5217                 CONF("lua_startup_hook") {
5218                         if (value) {
5219                                 llua_set_startup_hook(value);
5220                         } else {
5221                                 CONF_ERR;
5222                         }
5223                 }
5224                 CONF("lua_shutdown_hook") {
5225                         if (value) {
5226                                 llua_set_shutdown_hook(value);
5227                         } else {
5228                                 CONF_ERR;
5229                         }
5230                 }
5231 #endif /* X11 */
5232 #endif /* HAVE_LUA */
5233
5234                 CONF("color0"){}
5235                 CONF("color1"){}
5236                 CONF("color2"){}
5237                 CONF("color3"){}
5238                 CONF("color4"){}
5239                 CONF("color5"){}
5240                 CONF("color6"){}
5241                 CONF("color7"){}
5242                 CONF("color8"){}
5243                 CONF("color9"){}
5244                 CONF("default_color"){}
5245                 CONF3("default_shade_color", "default_shadecolor"){}
5246                 CONF3("default_outline_color", "default_outlinecolor") {}
5247                 CONF("own_window_colour") {}
5248
5249                 else {
5250                         NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5251                 }
5252         }
5253
5254         fclose(fp);
5255
5256         if (info.music_player_interval == 0) {
5257                 // default to update_interval
5258                 info.music_player_interval = update_interval;
5259         }
5260         if (!global_text) { // didn't supply any text
5261                 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5262         }
5263         if (!output_methods) {
5264                 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5265         }
5266 #if defined(NCURSES)
5267 #if defined(X11)
5268         if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5269                 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5270                 output_methods &= ~TO_NCURSES;
5271                 endwin();
5272         }
5273 #endif /* X11 */
5274         if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5275                 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5276                 output_methods &= ~(TO_STDOUT | TO_STDERR);
5277         }
5278 #endif /* NCURSES */
5279         return TRUE;
5280 }
5281
5282 #ifdef X11
5283 static void load_config_file_x11(const char *f)
5284 {
5285         int line = 0;
5286         FILE *fp;
5287
5288         fp = open_config_file(f);
5289         if (!fp) {
5290                 return;
5291         }
5292         DBGP("reading contents from config file '%s'", f);
5293
5294         while (!feof(fp)) {
5295                 char buff[CONF_BUFF_SIZE], *name, *value;
5296                 int ret = do_config_step(&line, fp, buff, &name, &value);
5297                 if (ret == CONF_BREAK) {
5298                         break;
5299                 } else if (ret == CONF_CONTINUE) {
5300                         continue;
5301                 }
5302
5303                 CONF2("color0") {
5304                         X11_initialisation();
5305                         if (x_initialised == YES) {
5306                                 if (value) {
5307                                         color0 = get_x11_color(value);
5308                                 } else {
5309                                         CONF_ERR;
5310                                 }
5311                         }
5312                 }
5313                 CONF("color1") {
5314                         X11_initialisation();
5315                         if (x_initialised == YES) {
5316                                 if (value) {
5317                                         color1 = get_x11_color(value);
5318                                 } else {
5319                                         CONF_ERR;
5320                                 }
5321                         }
5322                 }
5323                 CONF("color2") {
5324                         X11_initialisation();
5325                         if (x_initialised == YES) {
5326                                 if (value) {
5327                                         color2 = get_x11_color(value);
5328                                 } else {
5329                                         CONF_ERR;
5330                                 }
5331                         }
5332                 }
5333                 CONF("color3") {
5334                         X11_initialisation();
5335                         if (x_initialised == YES) {
5336                                 if (value) {
5337                                         color3 = get_x11_color(value);
5338                                 } else {
5339                                         CONF_ERR;
5340                                 }
5341                         }
5342                 }
5343                 CONF("color4") {
5344                         X11_initialisation();
5345                         if (x_initialised == YES) {
5346                                 if (value) {
5347                                         color4 = get_x11_color(value);
5348                                 } else {
5349                                         CONF_ERR;
5350                                 }
5351                         }
5352                 }
5353                 CONF("color5") {
5354                         X11_initialisation();
5355                         if (x_initialised == YES) {
5356                                 if (value) {
5357                                         color5 = get_x11_color(value);
5358                                 } else {
5359                                         CONF_ERR;
5360                                 }
5361                         }
5362                 }
5363                 CONF("color6") {
5364                         X11_initialisation();
5365                         if (x_initialised == YES) {
5366                                 if (value) {
5367                                         color6 = get_x11_color(value);
5368                                 } else {
5369                                         CONF_ERR;
5370                                 }
5371                         }
5372                 }
5373                 CONF("color7") {
5374                         X11_initialisation();
5375                         if (x_initialised == YES) {
5376                                 if (value) {
5377                                         color7 = get_x11_color(value);
5378                                 } else {
5379                                         CONF_ERR;
5380                                 }
5381                         }
5382                 }
5383                 CONF("color8") {
5384                         X11_initialisation();
5385                         if (x_initialised == YES) {
5386                                 if (value) {
5387                                         color8 = get_x11_color(value);
5388                                 } else {
5389                                         CONF_ERR;
5390                                 }
5391                         }
5392                 }
5393                 CONF("color9") {
5394                         X11_initialisation();
5395                         if (x_initialised == YES) {
5396                                 if (value) {
5397                                         color9 = get_x11_color(value);
5398                                 } else {
5399                                         CONF_ERR;
5400                                 }
5401                         }
5402                 }
5403                 CONF("default_color") {
5404                         X11_initialisation();
5405                         if (x_initialised == YES) {
5406                                 if (value) {
5407                                         default_fg_color = get_x11_color(value);
5408                                 } else {
5409                                         CONF_ERR;
5410                                 }
5411                         }
5412                 }
5413                 CONF3("default_shade_color", "default_shadecolor") {
5414                         X11_initialisation();
5415                         if (x_initialised == YES) {
5416                                 if (value) {
5417                                         default_bg_color = get_x11_color(value);
5418                                 } else {
5419                                         CONF_ERR;
5420                                 }
5421                         }
5422                 }
5423                 CONF3("default_outline_color", "default_outlinecolor") {
5424                         X11_initialisation();
5425                         if (x_initialised == YES) {
5426                                 if (value) {
5427                                         default_out_color = get_x11_color(value);
5428                                 } else {
5429                                         CONF_ERR;
5430                                 }
5431                         }
5432                 }
5433 #ifdef OWN_WINDOW
5434                 CONF("own_window_colour") {
5435                         X11_initialisation();
5436                         if (x_initialised == YES) {
5437                                 if (value) {
5438                                         background_colour = get_x11_color(value);
5439                                 } else {
5440                                         NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5441                                                 "'#' for hex colours");
5442                                 }
5443                         }
5444                 }
5445 #endif
5446                 CONF("text") {
5447                         /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5448                         if(output_methods & TO_X) {
5449                                 X11_initialisation();
5450                         }
5451                 }
5452 #undef CONF
5453 #undef CONF2
5454 #undef CONF3
5455 #undef CONF_ERR
5456 #undef CONF_ERR2
5457 #undef CONF_BREAK
5458 #undef CONF_CONTINUE
5459 #undef CONF_BUFF_SIZE
5460         }
5461
5462         fclose(fp);
5463
5464 }
5465 #endif /* X11 */
5466
5467 static void print_help(const char *prog_name) {
5468         printf("Usage: %s [OPTION]...\n"
5469                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5470                         "window. Command line options will override configurations defined in config\n"
5471                         "file.\n"
5472                         "   -v, --version             version\n"
5473                         "   -q, --quiet               quiet mode\n"
5474                         "   -D, --debug               increase debugging output, ie. -DD for more debugging\n"
5475                         "   -c, --config=FILE         config file to load\n"
5476 #ifdef CONFIG_OUTPUT
5477                         "   -C, --print-config        print the builtin default config to stdout\n"
5478                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5479 #endif
5480                         "   -d, --daemonize           daemonize, fork to background\n"
5481                         "   -h, --help                help\n"
5482 #ifdef X11
5483                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5484                         "   -f, --font=FONT           font to use\n"
5485                         "   -X, --display=DISPLAY     X11 display to use\n"
5486 #ifdef OWN_WINDOW
5487                         "   -o, --own-window          create own window to draw\n"
5488 #endif
5489 #ifdef HAVE_XDBE
5490                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
5491 #endif
5492                         "   -w, --window-id=WIN_ID    window id to draw\n"
5493                         "   -x X                      x position\n"
5494                         "   -y Y                      y position\n"
5495 #endif /* X11 */
5496                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
5497                         "   -u, --interval=SECS       update interval\n"
5498                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n"
5499                         "   -p, --pause=SECS          pause for SECS seconds at startup before doing anything\n",
5500                         prog_name
5501         );
5502 }
5503
5504 /* : means that character before that takes an argument */
5505 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5506 #ifdef X11
5507         "x:y:w:a:f:X:"
5508 #ifdef OWN_WINDOW
5509         "o"
5510 #endif
5511 #ifdef HAVE_XDBE
5512         "b"
5513 #endif
5514 #endif /* X11 */
5515 #ifdef CONFIG_OUTPUT
5516         "C"
5517 #endif
5518         ;
5519
5520 static const struct option longopts[] = {
5521         { "help", 0, NULL, 'h' },
5522         { "version", 0, NULL, 'V' },
5523         { "debug", 0, NULL, 'D' },
5524         { "config", 1, NULL, 'c' },
5525 #ifdef CONFIG_OUTPUT
5526         { "print-config", 0, NULL, 'C' },
5527 #endif
5528         { "daemonize", 0, NULL, 'd' },
5529 #ifdef X11
5530         { "alignment", 1, NULL, 'a' },
5531         { "font", 1, NULL, 'f' },
5532         { "display", 1, NULL, 'X' },
5533 #ifdef OWN_WINDOW
5534         { "own-window", 0, NULL, 'o' },
5535 #endif
5536 #ifdef HAVE_XDBE
5537         { "double-buffer", 0, NULL, 'b' },
5538 #endif
5539         { "window-id", 1, NULL, 'w' },
5540 #endif /* X11 */
5541         { "text", 1, NULL, 't' },
5542         { "interval", 0, NULL, 'u' },
5543         { "pause", 0, NULL, 'p' },
5544         { 0, 0, 0, 0 }
5545 };
5546
5547 void initialisation(int argc, char **argv) {
5548         struct sigaction act, oact;
5549
5550         set_default_configurations();
5551         load_config_file(current_config);
5552         currentconffile = conftree_add(currentconffile, current_config);
5553
5554         /* init specials array */
5555         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5556                 NORM_ERR("failed to create specials array");
5557         }
5558
5559 #ifdef MAIL_FILE
5560         if (current_mail_spool == NULL) {
5561                 char buf[256];
5562
5563                 variable_substitute(MAIL_FILE, buf, 256);
5564
5565                 if (buf[0] != '\0') {
5566                         current_mail_spool = strndup(buf, text_buffer_size);
5567                 }
5568         }
5569 #endif
5570
5571         /* handle other command line arguments */
5572
5573 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5574                 || defined(__NetBSD__)
5575         optind = optreset = 1;
5576 #else
5577         optind = 0;
5578 #endif
5579
5580 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5581         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5582                         "kvm_open")) == NULL) {
5583                 CRIT_ERR(NULL, NULL, "cannot read kvm");
5584         }
5585 #endif
5586
5587         while (1) {
5588                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5589                 int startup_pause;
5590
5591                 if (c == -1) {
5592                         break;
5593                 }
5594
5595                 switch (c) {
5596                         case 'd':
5597                                 fork_to_background = 1;
5598                                 break;
5599                         case 'D':
5600                                 global_debug_level++;
5601                                 break;
5602 #ifdef X11
5603                         case 'f':
5604                                 set_first_font(optarg);
5605                                 break;
5606                         case 'a':
5607                                 text_alignment = string_to_alignment(optarg);
5608                                 break;
5609
5610 #ifdef OWN_WINDOW
5611                         case 'o':
5612                                 own_window = 1;
5613                                 break;
5614 #endif
5615 #ifdef HAVE_XDBE
5616                         case 'b':
5617                                 use_xdbe = 1;
5618                                 break;
5619 #endif
5620 #endif /* X11 */
5621                         case 't':
5622                                 if (global_text) {
5623                                         free(global_text);
5624                                         global_text = 0;
5625                                 }
5626                                 global_text = strndup(optarg, max_user_text);
5627                                 convert_escapes(global_text);
5628                                 break;
5629
5630                         case 'u':
5631                                 update_interval = strtod(optarg, 0);
5632                                 update_interval_old = update_interval;
5633                                 if (info.music_player_interval == 0) {
5634                                         // default to update_interval
5635                                         info.music_player_interval = update_interval;
5636                                 }
5637                                 break;
5638
5639                         case 'i':
5640                                 total_run_times = strtod(optarg, 0);
5641                                 break;
5642 #ifdef X11
5643                         case 'x':
5644                                 gap_x = atoi(optarg);
5645                                 break;
5646
5647                         case 'y':
5648                                 gap_y = atoi(optarg);
5649                                 break;
5650 #endif /* X11 */
5651                         case 'p':
5652                                 startup_pause = atoi(optarg);
5653                                 sleep(startup_pause);
5654                                 break;
5655
5656                         case '?':
5657                                 exit(EXIT_FAILURE);
5658                 }
5659         }
5660
5661 #ifdef X11
5662         /* load font */
5663         if (output_methods & TO_X) {
5664                 load_config_file_x11(current_config);
5665         }
5666 #endif /* X11 */
5667
5668         /* generate text and get initial size */
5669         extract_variable_text(global_text);
5670         if (global_text) {
5671                 free(global_text);
5672                 global_text = 0;
5673         }
5674         global_text = NULL;
5675         /* fork */
5676         if (fork_to_background) {
5677                 int pid = fork();
5678
5679                 switch (pid) {
5680                         case -1:
5681                                 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5682                                         strerror(errno));
5683                                 break;
5684
5685                         case 0:
5686                                 /* child process */
5687                                 usleep(25000);
5688                                 fprintf(stderr, "\n");
5689                                 fflush(stderr);
5690                                 break;
5691
5692                         default:
5693                                 /* parent process */
5694                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5695                                         pid);
5696                                 fflush(stderr);
5697                                 exit(EXIT_SUCCESS);
5698                 }
5699         }
5700
5701         start_update_threading();
5702
5703         text_buffer = malloc(max_user_text);
5704         memset(text_buffer, 0, max_user_text);
5705         tmpstring1 = malloc(text_buffer_size);
5706         memset(tmpstring1, 0, text_buffer_size);
5707         tmpstring2 = malloc(text_buffer_size);
5708         memset(tmpstring2, 0, text_buffer_size);
5709
5710 #ifdef X11
5711         xargc = argc;
5712         xargv = argv;
5713         X11_create_window();
5714 #endif /* X11 */
5715 #ifdef HAVE_LUA
5716         llua_setup_info(&info, update_interval);
5717 #endif /* HAVE_LUA */
5718 #ifdef XOAP
5719         xmlInitParser();
5720 #endif /* XOAP */
5721
5722         /* Set signal handlers */
5723         act.sa_handler = signal_handler;
5724         sigemptyset(&act.sa_mask);
5725         act.sa_flags = 0;
5726 #ifdef SA_RESTART
5727         act.sa_flags |= SA_RESTART;
5728 #endif
5729
5730         if (            sigaction(SIGINT,  &act, &oact) < 0
5731                         ||      sigaction(SIGALRM, &act, &oact) < 0
5732                         ||      sigaction(SIGUSR1, &act, &oact) < 0
5733                         ||      sigaction(SIGHUP,  &act, &oact) < 0
5734                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
5735                 NORM_ERR("error setting signal handler: %s", strerror(errno));
5736         }
5737
5738 #ifdef HAVE_LUA
5739         llua_startup_hook();
5740 #endif /* HAVE_LUA */
5741 }
5742
5743 int main(int argc, char **argv)
5744 {
5745 #ifdef X11
5746         char *s, *temp;
5747         unsigned int x;
5748 #endif
5749
5750         argc_copy = argc;
5751         argv_copy = argv;
5752         g_signal_pending = 0;
5753         max_user_text = MAX_USER_TEXT_DEFAULT;
5754         current_config = 0;
5755         memset(&info, 0, sizeof(info));
5756         free_templates();
5757         clear_net_stats();
5758
5759 #ifdef TCP_PORT_MONITOR
5760         /* set default connection limit */
5761         tcp_portmon_set_max_connections(0);
5762 #endif
5763
5764         /* handle command line parameters that don't change configs */
5765 #ifdef X11
5766         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5767                         || ((s = getenv("LANG")) && *s)) {
5768                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5769                 if (temp == NULL) {
5770                         NORM_ERR("malloc failed");
5771                 }
5772                 for (x = 0; x < strlen(s); x++) {
5773                         temp[x] = tolower(s[x]);
5774                 }
5775                 temp[x] = 0;
5776                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5777                         utf8_mode = 1;
5778                 }
5779
5780                 free(temp);
5781         }
5782         if (!setlocale(LC_CTYPE, "")) {
5783                 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5784         }
5785 #endif /* X11 */
5786         while (1) {
5787                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5788
5789                 if (c == -1) {
5790                         break;
5791                 }
5792
5793                 switch (c) {
5794                         case 'v':
5795                         case 'V':
5796                                 print_version();
5797                         case 'c':
5798                                 if (current_config) {
5799                                         free(current_config);
5800                                 }
5801                                 current_config = strndup(optarg, max_user_text);
5802                                 break;
5803                         case 'q':
5804                                 freopen("/dev/null", "w", stderr);
5805                                 break;
5806                         case 'h':
5807                                 print_help(argv[0]);
5808                                 return 0;
5809 #ifdef CONFIG_OUTPUT
5810                         case 'C':
5811                                 print_defconfig();
5812                                 return 0;
5813 #endif
5814 #ifdef X11
5815                         case 'w':
5816                                 window.window = strtol(optarg, 0, 0);
5817                                 break;
5818                         case 'X':
5819                                 if (disp)
5820                                         free(disp);
5821                                 disp = strdup(optarg);
5822                                 break;
5823 #endif /* X11 */
5824
5825                         case '?':
5826                                 exit(EXIT_FAILURE);
5827                 }
5828         }
5829
5830         /* check if specified config file is valid */
5831         if (current_config) {
5832                 struct stat sb;
5833                 if (stat(current_config, &sb) ||
5834                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5835                         NORM_ERR("invalid configuration file '%s'\n", current_config);
5836                         free(current_config);
5837                         current_config = 0;
5838                 }
5839         }
5840
5841         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5842
5843         if (!current_config) {
5844                 /* load default config file */
5845                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5846                 FILE *fp;
5847
5848                 /* Try to use personal config file first */
5849                 to_real_path(buf, CONFIG_FILE);
5850                 if (buf[0] && (fp = fopen(buf, "r"))) {
5851                         current_config = strndup(buf, max_user_text);
5852                         fclose(fp);
5853                 }
5854
5855                 /* Try to use system config file if personal config not readable */
5856                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5857                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5858                         fclose(fp);
5859                 }
5860
5861                 /* No readable config found */
5862                 if (!current_config) {
5863 #ifdef CONFIG_OUTPUT
5864                         current_config = strdup("==builtin==");
5865                         NORM_ERR("no readable personal or system-wide config file found,"
5866                                         " using builtin default");
5867 #else
5868                         CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5869 #endif /* ! CONF_OUTPUT */
5870                 }
5871         }
5872
5873 #ifdef XOAP
5874         /* Load xoap keys, if existing */
5875         load_xoap_keys();
5876 #endif /* XOAP */
5877
5878 #ifdef HAVE_SYS_INOTIFY_H
5879         inotify_fd = inotify_init1(IN_NONBLOCK);
5880 #endif /* HAVE_SYS_INOTIFY_H */
5881
5882         initialisation(argc, argv);
5883
5884         main_loop();
5885
5886 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5887         kvm_close(kd);
5888 #endif
5889
5890         return 0;
5891
5892 }
5893
5894 void alarm_handler(void) {
5895         if(childpid > 0) {
5896                 kill(childpid, SIGTERM);
5897         }
5898 }
5899
5900 static void signal_handler(int sig)
5901 {
5902         /* signal handler is light as a feather, as it should be.
5903          * we will poll g_signal_pending with each loop of conky
5904          * and do any signal processing there, NOT here (except 
5905          * SIGALRM because this is caused when conky is hanging) */
5906         if(sig == SIGALRM) {
5907                 alarm_handler();
5908         } else {
5909                 g_signal_pending = sig;
5910         }
5911 }