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