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