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