Fix gcc warning
[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 int top_running;
170 int output_methods;
171 static int extra_newline;
172 enum x_initialiser_state x_initialised = NO;
173 static volatile int g_signal_pending;
174 /* Update interval */
175 double update_interval;
176 double update_interval_old;
177 double update_interval_bat;
178 void *global_cpu = NULL;
179 unsigned int max_text_width = 0;
180
181 int argc_copy;
182 char** argv_copy;
183
184 /* prototypes for internally used functions */
185 static void signal_handler(int);
186 static void print_version(void) __attribute__((noreturn));
187 static void reload_config(void);
188
189 static void print_version(void)
190 {
191         printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
192
193         printf("\nCompiled in features:\n\n"
194                    "System config file: "SYSTEM_CONFIG_FILE"\n"
195                    "Package library path: "PACKAGE_LIBDIR"\n\n"
196 #ifdef X11
197                    " X11:\n"
198 # ifdef HAVE_XDAMAGE
199                    "  * Xdamage extension\n"
200 # endif /* HAVE_XDAMAGE */
201 # ifdef HAVE_XDBE
202                    "  * XDBE (double buffer extension)\n"
203 # endif /* HAVE_XDBE */
204 # ifdef XFT
205                    "  * Xft\n"
206 # endif /* XFT */
207 # ifdef USE_ARGB
208                    "  * ARGB visual\n"
209 # endif /* USE_ARGB */
210 #endif /* X11 */
211                    "\n Music detection:\n"
212 #ifdef AUDACIOUS
213                    "  * Audacious\n"
214 #endif /* AUDACIOUS */
215 #ifdef BMPX
216                    "  * BMPx\n"
217 #endif /* BMPX */
218 #ifdef MPD
219                    "  * MPD\n"
220 #endif /* MPD */
221 #ifdef MOC
222                    "  * MOC\n"
223 #endif /* MOC */
224 #ifdef XMMS2
225                    "  * XMMS2\n"
226 #endif /* XMMS2 */
227                    "\n General:\n"
228 #ifdef HAVE_OPENMP
229                    "  * OpenMP\n"
230 #endif /* HAVE_OPENMP */
231 #ifdef MATH
232                    "  * math\n"
233 #endif /* Math */
234 #ifdef HDDTEMP
235                    "  * hddtemp\n"
236 #endif /* HDDTEMP */
237 #ifdef TCP_PORT_MONITOR
238                    "  * portmon\n"
239 #endif /* TCP_PORT_MONITOR */
240 #ifdef HAVE_CURL
241                    "  * Curl\n"
242 #endif /* HAVE_CURL */
243 #ifdef RSS
244                    "  * RSS\n"
245 #endif /* RSS */
246 #ifdef WEATHER
247                    "  * Weather (METAR)\n"
248 #ifdef XOAP
249                    "  * Weather (XOAP)\n"
250 #endif /* XOAP */
251 #endif /* WEATHER */
252 #ifdef HAVE_IWLIB
253                    "  * wireless\n"
254 #endif /* HAVE_IWLIB */
255 #ifdef IBM
256                    "  * support for IBM/Lenovo notebooks\n"
257 #endif /* IBM */
258 #ifdef NVIDIA
259                    "  * nvidia\n"
260 #endif /* NVIDIA */
261 #ifdef EVE
262                    "  * eve-online\n"
263 #endif /* EVE */
264 #ifdef CONFIG_OUTPUT
265                    "  * config-output\n"
266 #endif /* CONFIG_OUTPUT */
267 #ifdef IMLIB2
268                    "  * Imlib2\n"
269 #endif /* IMLIB2 */
270 #ifdef APCUPSD
271                    "  * apcupsd\n"
272 #endif /* APCUPSD */
273 #ifdef IOSTATS
274                    "  * iostats\n"
275 #endif /* IOSTATS */
276 #ifdef NCURSES
277                    "  * ncurses\n"
278 #endif /* NCURSES */
279 #ifdef HAVE_LUA
280                    "  * Lua\n"
281                    "\n  Lua bindings:\n"
282 #ifdef HAVE_LUA_CAIRO
283                    "   * Cairo\n"
284 #endif /* HAVE_LUA_CAIRO */
285 #ifdef HAVE_LUA_IMLIB2
286                    "   * Imlib2\n"
287 #endif /* IMLIB2 */
288 #endif /* HAVE_LUA */
289         );
290
291         exit(EXIT_SUCCESS);
292 }
293
294 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
295
296
297 #ifdef X11
298
299 static void X11_create_window(void);
300 static void X11_initialisation(void);
301
302 struct _x11_stuff_s {
303         Region region;
304 #ifdef HAVE_XDAMAGE
305         Damage damage;
306         XserverRegion region2, part;
307         int event_base, error_base;
308 #endif
309 } x11_stuff;
310
311 /* text size */
312
313 static int text_start_x, text_start_y;  /* text start position in window */
314 static int text_width, text_height;
315
316 /* alignments */
317 enum alignment {
318         ALIGNMENT_ERROR,
319         TOP_LEFT,
320         TOP_RIGHT,
321         TOP_MIDDLE,
322         BOTTOM_LEFT,
323         BOTTOM_RIGHT,
324         BOTTOM_MIDDLE,
325         MIDDLE_LEFT,
326         MIDDLE_MIDDLE,
327         MIDDLE_RIGHT,
328         NONE
329 };
330
331 /* display to connect to */
332 static char *disp = NULL;
333
334 #endif /* X11 */
335
336 /* struct that has all info to be shared between
337  * instances of the same text object */
338 struct information info;
339
340 /* path to config file */
341 char *current_config;
342
343 /* set to 1 if you want all text to be in uppercase */
344 static unsigned int stuff_in_uppercase;
345
346 /* Run how many times? */
347 static unsigned long total_run_times;
348
349 /* fork? */
350 static int fork_to_background;
351
352 /* set to 0 after the first time conky is run, so we don't fork again after the
353  * first forking */
354 static int first_pass = 1;
355
356 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
357
358 /* filenames for output */
359 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
360 char *append_file = NULL; FILE *append_fpointer = NULL;
361
362 #ifdef X11
363
364 static int show_graph_scale;
365 static int show_graph_range;
366
367 /* Position on the screen */
368 static int text_alignment;
369 static int gap_x, gap_y;
370
371 /* border */
372 static int draw_borders;
373 static int draw_graph_borders;
374 static int stippled_borders;
375
376 int get_stippled_borders(void)
377 {
378         return stippled_borders;
379 }
380
381 static int draw_shades, draw_outline;
382
383 long default_fg_color, default_bg_color, default_out_color;
384
385 /* create own window or draw stuff to root? */
386 static int set_transparent = 0;
387
388 #ifdef OWN_WINDOW
389 static int own_window = 0;
390 static int background_colour = 0;
391
392 /* fixed size/pos is set if wm/user changes them */
393 static int fixed_size = 0, fixed_pos = 0;
394 #endif
395
396 static int minimum_width, minimum_height;
397 static int maximum_width;
398
399 #endif /* X11 */
400
401 #ifdef __OpenBSD__
402 static int sensor_device;
403 #endif
404
405 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
406          color9;
407
408 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
409 unsigned int max_user_text;
410
411 /* maximum size of individual text buffers, ie $exec buffer size */
412 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
413
414 /* UTF-8 */
415 int utf8_mode = 0;
416
417 /* no buffers in used memory? */
418 int no_buffers;
419
420 /* pad percentages to decimals? */
421 static int pad_percents = 0;
422
423 static char *global_text = 0;
424
425 char *get_global_text(void)
426 {
427         return global_text;
428 }
429
430 long global_text_lines;
431
432 static int total_updates;
433 static int updatereset;
434
435 void set_updatereset(int i)
436 {
437         updatereset = i;
438 }
439
440 int get_updatereset(void)
441 {
442         return updatereset;
443 }
444
445 int check_contains(char *f, char *s)
446 {
447         int ret = 0;
448         FILE *where = open_file(f, 0);
449
450         if (where) {
451                 char buf1[256];
452
453                 while (fgets(buf1, 256, where)) {
454                         if (strstr(buf1, s)) {
455                                 ret = 1;
456                                 break;
457                         }
458                 }
459                 fclose(where);
460         } else {
461                 NORM_ERR("Could not open the file '%s'", f);
462         }
463         return ret;
464 }
465
466 #define SECRIT_MULTILINE_CHAR '\x02'
467
468 int calc_text_width(const char *s)
469 {
470         size_t slen = strlen(s);
471
472 #ifdef X11
473         if ((output_methods & TO_X) == 0) {
474 #endif /* X11 */
475                 return slen;
476 #ifdef X11
477         }
478 #ifdef XFT
479         if (use_xft) {
480                 XGlyphInfo gi;
481
482                 if (utf8_mode) {
483                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
484                                 (const FcChar8 *) s, slen, &gi);
485                 } else {
486                         XftTextExtents8(display, fonts[selected_font].xftfont,
487                                 (const FcChar8 *) s, slen, &gi);
488                 }
489                 return gi.xOff;
490         } else
491 #endif
492         {
493                 return XTextWidth(fonts[selected_font].font, s, slen);
494         }
495 #endif /* X11 */
496 }
497
498 /* formatted text to render on screen, generated in generate_text(),
499  * drawn in draw_stuff() */
500
501 static char *text_buffer;
502
503 /* quite boring functions */
504
505 static inline void for_each_line(char *b, int f(char *, int))
506 {
507         char *ps, *pe;
508         int special_index = 0; /* specials index */
509
510         if(! b) return;
511         for (ps = b, pe = b; *pe; pe++) {
512                 if (*pe == '\n') {
513                         *pe = '\0';
514                         special_index = f(ps, special_index);
515                         *pe = '\n';
516                         ps = pe + 1;
517                 }
518         }
519
520         if (ps < pe) {
521                 f(ps, special_index);
522         }
523 }
524
525 static void convert_escapes(char *buf)
526 {
527         char *p = buf, *s = buf;
528
529         while (*s) {
530                 if (*s == '\\') {
531                         s++;
532                         if (*s == 'n') {
533                                 *p++ = '\n';
534                         } else if (*s == '\\') {
535                                 *p++ = '\\';
536                         }
537                         s++;
538                 } else {
539                         *p++ = *s++;
540                 }
541         }
542         *p = '\0';
543 }
544
545 /* Prints anything normally printed with snprintf according to the current value
546  * of use_spacer.  Actually slightly more flexible than snprintf, as you can
547  * safely specify the destination buffer as one of your inputs.  */
548 int spaced_print(char *buf, int size, const char *format, int width, ...)
549 {
550         int len = 0;
551         va_list argp;
552         char *tempbuf;
553
554         if (size < 1) {
555                 return 0;
556         }
557         tempbuf = malloc(size * sizeof(char));
558
559         // Passes the varargs along to vsnprintf
560         va_start(argp, width);
561         vsnprintf(tempbuf, size, format, argp);
562         va_end(argp);
563
564         switch (use_spacer) {
565                 case NO_SPACER:
566                         len = snprintf(buf, size, "%s", tempbuf);
567                         break;
568                 case LEFT_SPACER:
569                         len = snprintf(buf, size, "%*s", width, tempbuf);
570                         break;
571                 case RIGHT_SPACER:
572                         len = snprintf(buf, size, "%-*s", width, tempbuf);
573                         break;
574         }
575         free(tempbuf);
576         return len;
577 }
578
579 /* print percentage values
580  *
581  * - i.e., unsigned values between 0 and 100
582  * - respect the value of pad_percents */
583 int percent_print(char *buf, int size, unsigned value)
584 {
585         return spaced_print(buf, size, "%u", pad_percents, value);
586 }
587
588 /* converts from bytes to human readable format (K, M, G, T)
589  *
590  * The algorithm always divides by 1024, as unit-conversion of byte
591  * counts suggests. But for output length determination we need to
592  * compare with 1000 here, as we print in decimal form. */
593 void human_readable(long long num, char *buf, int size)
594 {
595         const char **suffix = suffixes;
596         float fnum;
597         int precision;
598         int width;
599         const char *format;
600
601         /* Possibly just output as usual, for example for stdout usage */
602         if (!format_human_readable) {
603                 spaced_print(buf, size, "%d", 6, round_to_int(num));
604                 return;
605         }
606         if (short_units) {
607                 width = 5;
608                 format = "%.*f%.1s";
609         } else {
610                 width = 7;
611                 format = "%.*f%-3s";
612         }
613
614         if (llabs(num) < 1000LL) {
615                 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
616                 return;
617         }
618
619         while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
620                 num /= 1024;
621                 suffix++;
622         }
623
624         suffix++;
625         fnum = num / 1024.0;
626
627         /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
628          *
629          * The goal is to always have a significance of 3, by
630          * adjusting the decimal part of the number. Sample output:
631          *  123MiB
632          * 23.4GiB
633          * 5.12B   
634          * so the point of alignment resides between number and unit. The
635          * upside of this is that there is minimal padding necessary, though
636          * there should be a way to make alignment take place at the decimal
637          * dot (then with fixed width decimal part). 
638          *
639          * Note the repdigits below: when given a precision value, printf()
640          * rounds the float to it, not just cuts off the remaining digits. So
641          * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
642          * printed with a precision of 0. Yay. */
643
644         precision = 0;          /* print 100-999 without decimal part */
645         if (fnum < 99.95)
646                 precision = 1;  /* print 10-99 with one decimal place */
647         if (fnum < 9.995)
648                 precision = 2;  /* print 0-9 with two decimal places */
649
650         spaced_print(buf, size, format, width, precision, fnum, *suffix);
651 }
652
653 /* global object list root element */
654 static struct text_object global_root_object;
655
656 static long current_text_color;
657
658 void set_current_text_color(long colour)
659 {
660         current_text_color = colour;
661 }
662
663 long get_current_text_color(void)
664 {
665         return current_text_color;
666 }
667
668 //adds newstring to to the tree unless you can already see it when travelling back.
669 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
670 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
671         struct conftree* node;
672         struct conftree* node2;
673
674         for(node = previous; node != NULL; node = node->back) {
675                 if(strcmp(node->string, newstring) == 0) {
676                         return NULL;
677                 }
678         }
679         node = malloc(sizeof(struct conftree));
680         if (previous != NULL) {
681                 if(previous->vert_next == NULL) {
682                         previous->vert_next = node;
683                 } else {
684                         for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
685                         node2->horz_next = node;
686                 }
687         }
688         node->string = strdup(newstring);
689         node->horz_next = NULL;
690         node->vert_next = NULL;
691         node->back = previous;
692         return node;
693 }
694
695 void conftree_empty(struct conftree* tree) {
696         if(tree) {
697                 conftree_empty(tree->horz_next);
698                 conftree_empty(tree->vert_next);
699                 free(tree->string);
700                 free(tree);
701         }
702 }
703
704 struct conftree *currentconffile;
705
706 static void extract_variable_text(const char *p)
707 {
708         free_text_objects(&global_root_object, 0);
709         if (tmpstring1) {
710                 free(tmpstring1);
711                 tmpstring1 = 0;
712         }
713         if (tmpstring2) {
714                 free(tmpstring2);
715                 tmpstring2 = 0;
716         }
717         if (text_buffer) {
718                 free(text_buffer);
719                 text_buffer = 0;
720         }
721
722         extract_variable_text_internal(&global_root_object, p);
723 }
724
725 void parse_conky_vars(struct text_object *root, const char *txt,
726                 char *p, int p_max_size, struct information *cur)
727 {
728         extract_variable_text_internal(root, txt);
729         generate_text_internal(p, p_max_size, *root, cur);
730 }
731
732 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
733  * multiline objects like $exec work with $align[rc] and friends
734  */
735 void substitute_newlines(char *p, long l)
736 {
737         char *s = p;
738         if (l < 0) return;
739         while (p && *p && p < s + l) {
740                 if (*p == '\n') {
741                         /* only substitute if it's not the last newline */
742                         *p = SECRIT_MULTILINE_CHAR;
743                 }
744                 p++;
745         }
746 }
747
748 void generate_text_internal(char *p, int p_max_size,
749                 struct text_object root, struct information *cur)
750 {
751         struct text_object *obj;
752 #ifdef X11
753         int need_to_load_fonts = 0;
754 #endif /* X11 */
755
756         /* for the OBJ_top* handler */
757         struct process **needed = 0;
758
759 #ifdef HAVE_ICONV
760         char buff_in[p_max_size];
761         buff_in[0] = 0;
762 #endif /* HAVE_ICONV */
763
764         if(! p) return;
765
766         p[0] = 0;
767         obj = root.next;
768         while (obj && p_max_size > 0) {
769                 needed = 0; /* reset for top stuff */
770
771 /* IFBLOCK jumping algorithm
772  *
773  * This is easier as it looks like:
774  * - each IF checks it's condition
775  *   - on FALSE: call DO_JUMP
776  *   - on TRUE: don't care
777  * - each ELSE calls DO_JUMP unconditionally
778  * - each ENDIF is silently being ignored
779  *
780  * Why this works:
781  * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
782  * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
783  * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
784  * regularly, "obj" is being updated to point to obj->next, so object parsing
785  * continues right after the corresponding ELSE or ENDIF. This means that if we
786  * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
787  * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
788  * jumped, and there is nothing to do.
789  */
790 #define DO_JUMP { \
791         DBGP2("jumping"); \
792         if (obj->ifblock_next) \
793                 obj = obj->ifblock_next; \
794 }
795
796 #define OBJ(a) break; case OBJ_##a:
797
798                 switch (obj->type) {
799                         default:
800                                 NORM_ERR("not implemented obj type %d", obj->type);
801                         OBJ(read_tcp) {
802                                 print_read_tcp(obj, p, p_max_size);
803                         }
804 #ifndef __OpenBSD__
805                         OBJ(acpitemp) {
806                                 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
807                         }
808 #endif /* !__OpenBSD__ */
809                         OBJ(freq) {
810                                 static int ok = 1;
811                                 if (ok) {
812                                         ok = get_freq(p, p_max_size, "%.0f", 1,
813                                                         obj->data.i);
814                                 }
815                         }
816                         OBJ(freq_g) {
817                                 static int ok = 1;
818                                 if (ok) {
819 #ifndef __OpenBSD__
820                                         ok = get_freq(p, p_max_size, "%'.2f", 1000,
821                                                         obj->data.i);
822 #else
823                                         /* OpenBSD has no such flag (SUSv2) */
824                                         ok = get_freq(p, p_max_size, "%.2f", 1000,
825                                                         obj->data.i);
826 #endif /* __OpenBSD */
827                                 }
828                         }
829 #if defined(__linux__)
830                         OBJ(voltage_mv) {
831                                 print_voltage_mv(obj, p, p_max_size);
832                         }
833                         OBJ(voltage_v) {
834                                 print_voltage_v(obj, p, p_max_size);
835                         }
836
837 #ifdef HAVE_IWLIB
838                         OBJ(wireless_essid) {
839                                 print_wireless_essid(obj, p, p_max_size);
840                         }
841                         OBJ(wireless_mode) {
842                                 print_wireless_mode(obj, p, p_max_size);
843                         }
844                         OBJ(wireless_bitrate) {
845                                 print_wireless_bitrate(obj, p, p_max_size);
846                         }
847                         OBJ(wireless_ap) {
848                                 print_wireless_ap(obj, p, p_max_size);
849                         }
850                         OBJ(wireless_link_qual) {
851                                 print_wireless_link_qual(obj, p, p_max_size);
852                         }
853                         OBJ(wireless_link_qual_max) {
854                                 print_wireless_link_qual_max(obj, p, p_max_size);
855                         }
856                         OBJ(wireless_link_qual_perc) {
857                                 print_wireless_link_qual_perc(obj, p, p_max_size);
858                         }
859                         OBJ(wireless_link_bar) {
860                                 print_wireless_link_bar(obj, p, p_max_size);
861                         }
862 #endif /* HAVE_IWLIB */
863
864 #endif /* __linux__ */
865
866 #ifndef __OpenBSD__
867                         OBJ(acpifan) {
868                                 get_acpi_fan(p, p_max_size);
869                         }
870                         OBJ(acpiacadapter) {
871                                 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
872                         }
873                         OBJ(battery) {
874                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
875                         }
876                         OBJ(battery_time) {
877                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
878                         }
879                         OBJ(battery_percent) {
880                                 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
881                         }
882                         OBJ(battery_bar) {
883                                 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
884                         }
885                         OBJ(battery_short) {
886                                 get_battery_short_status(p, p_max_size, obj->data.s);
887                         }
888 #endif /* __OpenBSD__ */
889
890                         OBJ(buffers) {
891                                 human_readable(cur->buffers * 1024, p, 255);
892                         }
893                         OBJ(cached) {
894                                 human_readable(cur->cached * 1024, p, 255);
895                         }
896                         OBJ(cmdline_to_pid) {
897                                 print_cmdline_to_pid(obj, p, p_max_size);
898                         }
899                         OBJ(cpu) {
900                                 if (cur->cpu_usage) {
901                                         if (obj->data.i > info.cpu_count) {
902                                                 NORM_ERR("obj->data.i %i info.cpu_count %i",
903                                                                 obj->data.i, info.cpu_count);
904                                                 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
905                                         }
906                                     percent_print(p, p_max_size,
907                                               round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
908                                 }
909                         }
910                         OBJ(cpugauge)
911                                 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
912                         OBJ(cpubar) {
913                                 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
914                         }
915 #ifdef X11
916                         OBJ(cpugraph) {
917                                 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
918                         }
919                         OBJ(loadgraph) {
920                                 print_loadgraph(obj, p, p_max_size);
921                         }
922 #endif /* X11 */
923                         OBJ(color) {
924                                 new_fg(p, obj->data.l);
925                         }
926 #ifdef X11
927                         OBJ(color0) {
928                                 new_fg(p, color0);
929                         }
930                         OBJ(color1) {
931                                 new_fg(p, color1);
932                         }
933                         OBJ(color2) {
934                                 new_fg(p, color2);
935                         }
936                         OBJ(color3) {
937                                 new_fg(p, color3);
938                         }
939                         OBJ(color4) {
940                                 new_fg(p, color4);
941                         }
942                         OBJ(color5) {
943                                 new_fg(p, color5);
944                         }
945                         OBJ(color6) {
946                                 new_fg(p, color6);
947                         }
948                         OBJ(color7) {
949                                 new_fg(p, color7);
950                         }
951                         OBJ(color8) {
952                                 new_fg(p, color8);
953                         }
954                         OBJ(color9) {
955                                 new_fg(p, color9);
956                         }
957 #endif /* X11 */
958 #if defined(__linux__)
959                         OBJ(disk_protect) {
960                                 snprintf(p, p_max_size, "%s",
961                                                 get_disk_protect_queue(obj->data.s));
962                         }
963                         OBJ(i8k_version) {
964                                 print_i8k_version(obj, p, p_max_size);
965                         }
966                         OBJ(i8k_bios) {
967                                 print_i8k_bios(obj, p, p_max_size);
968                         }
969                         OBJ(i8k_serial) {
970                                 print_i8k_serial(obj, p, p_max_size);
971                         }
972                         OBJ(i8k_cpu_temp) {
973                                 print_i8k_cpu_temp(obj, p, p_max_size);
974                         }
975                         OBJ(i8k_left_fan_status) {
976                                 print_i8k_left_fan_status(obj, p, p_max_size);
977                         }
978                         OBJ(i8k_right_fan_status) {
979                                 print_i8k_right_fan_status(obj, p, p_max_size);
980                         }
981                         OBJ(i8k_left_fan_rpm) {
982                                 print_i8k_left_fan_rpm(obj, p, p_max_size);
983                         }
984                         OBJ(i8k_right_fan_rpm) {
985                                 print_i8k_right_fan_rpm(obj, p, p_max_size);
986                         }
987                         OBJ(i8k_ac_status) {
988                                 print_i8k_ac_status(obj, p, p_max_size);
989                         }
990                         OBJ(i8k_buttons_status) {
991                                 print_i8k_buttons_status(obj, p, p_max_size);
992                         }
993 #if defined(IBM)
994                         OBJ(ibm_fan) {
995                                 get_ibm_acpi_fan(obj, p, p_max_size);
996                         }
997                         OBJ(ibm_temps) {
998                                 print_ibm_temps(obj, p, p_max_size);
999                         }
1000                         OBJ(ibm_volume) {
1001                                 get_ibm_acpi_volume(obj, p, p_max_size);
1002                         }
1003                         OBJ(ibm_brightness) {
1004                                 get_ibm_acpi_brightness(obj, p, p_max_size);
1005                         }
1006 #endif /* IBM */
1007                         /* information from sony_laptop kernel module
1008                          * /sys/devices/platform/sony-laptop */
1009                         OBJ(sony_fanspeed) {
1010                                 get_sony_fanspeed(p, p_max_size);
1011                         }
1012                         OBJ(if_gw) {
1013                                 if (!gateway_exists()) {
1014                                         DO_JUMP;
1015                                 }
1016                         }
1017                         OBJ(gw_iface) {
1018                                 print_gateway_iface(p, p_max_size);
1019                         }
1020                         OBJ(gw_ip) {
1021                                 print_gateway_ip(p, p_max_size);
1022                         }
1023                         OBJ(laptop_mode) {
1024                                 snprintf(p, p_max_size, "%d", get_laptop_mode());
1025                         }
1026                         OBJ(pb_battery) {
1027                                 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1028                         }
1029 #endif /* __linux__ */
1030 #if (defined(__FreeBSD__) || defined(__linux__))
1031                         OBJ(if_up) {
1032                                 if (!interface_up(obj)) {
1033                                         DO_JUMP;
1034                                 }
1035                         }
1036 #endif
1037 #ifdef __OpenBSD__
1038                         OBJ(obsd_sensors_temp) {
1039                                 print_obsd_sensors_temp(obj, p, p_max_size);
1040                         }
1041                         OBJ(obsd_sensors_fan) {
1042                                 print_obsd_sensors_fan(obj, p, p_max_size);
1043                         }
1044                         OBJ(obsd_sensors_volt) {
1045                                 print_obsd_sensors_volt(obj, p, p_max_size);
1046                         }
1047                         OBJ(obsd_vendor) {
1048                                 get_obsd_vendor(p, p_max_size);
1049                         }
1050                         OBJ(obsd_product) {
1051                                 get_obsd_product(p, p_max_size);
1052                         }
1053 #endif /* __OpenBSD__ */
1054 #ifdef X11
1055                         OBJ(font) {
1056                                 new_font(p, obj->data.s);
1057                                 need_to_load_fonts = 1;
1058                         }
1059 #endif /* X11 */
1060                         OBJ(diskio) {
1061                                 print_diskio(obj, p, p_max_size);
1062                         }
1063                         OBJ(diskio_write) {
1064                                 print_diskio_write(obj, p, p_max_size);
1065                         }
1066                         OBJ(diskio_read) {
1067                                 print_diskio_read(obj, p, p_max_size);
1068                         }
1069 #ifdef X11
1070                         OBJ(diskiograph) {
1071                                 print_diskiograph(obj, p, p_max_size);
1072                         }
1073                         OBJ(diskiograph_read) {
1074                                 print_diskiograph_read(obj, p, p_max_size);
1075                         }
1076                         OBJ(diskiograph_write) {
1077                                 print_diskiograph_write(obj, p, p_max_size);
1078                         }
1079 #endif /* X11 */
1080                         OBJ(downspeed) {
1081                                 print_downspeed(obj, p, p_max_size);
1082                         }
1083                         OBJ(downspeedf) {
1084                                 print_downspeedf(obj, p, p_max_size);
1085                         }
1086 #ifdef X11
1087                         OBJ(downspeedgraph) {
1088                                 print_downspeedgraph(obj, p, p_max_size);
1089                         }
1090 #endif /* X11 */
1091                         OBJ(else) {
1092                                 /* Since we see you, you're if has not jumped.
1093                                  * Do Ninja jump here: without leaving traces.
1094                                  * This is to prevent us from stale jumped flags.
1095                                  */
1096                                 obj = obj->ifblock_next;
1097                                 continue;
1098                         }
1099                         OBJ(endif) {
1100                                 /* harmless object, just ignore */
1101                         }
1102                         OBJ(addr) {
1103                                 print_addr(obj, p, p_max_size);
1104                         }
1105 #if defined(__linux__)
1106                         OBJ(addrs) {
1107                                 print_addrs(obj, p, p_max_size);
1108                         }
1109 #endif /* __linux__ */
1110 #if defined(IMLIB2) && defined(X11)
1111                         OBJ(image) {
1112                                 /* doesn't actually draw anything, just queues it omp.  the
1113                                  * image will get drawn after the X event loop */
1114                                 cimlib_add_image(obj->data.s);
1115                         }
1116 #endif /* IMLIB2 */
1117                         OBJ(eval) {
1118                                 evaluate(obj->data.s, p, p_max_size);
1119                         }
1120                         OBJ(exec) {
1121                                 print_exec(obj, p, p_max_size);
1122                         }
1123                         OBJ(execp) {
1124                                 print_execp(obj, p, p_max_size);
1125                         }
1126                         OBJ(execgauge) {
1127                                 print_execgauge(obj, p, p_max_size);
1128                         }
1129                         OBJ(execbar) {
1130                                 print_execbar(obj, p, p_max_size);
1131                         }
1132 #ifdef X11
1133                         OBJ(execgraph) {
1134                                 print_execgraph(obj, p, p_max_size);
1135                         }
1136 #endif /* X11 */
1137                         OBJ(execibar) {
1138                                 print_execibar(obj, p, p_max_size);
1139                         }
1140 #ifdef X11
1141                         OBJ(execigraph) {
1142                                 print_execigraph(obj, p, p_max_size);
1143                         }
1144 #endif /* X11 */
1145                         OBJ(execigauge) {
1146                                 print_execigauge(obj, p, p_max_size);
1147                         }
1148                         OBJ(execi) {
1149                                 print_execi(obj, p, p_max_size);
1150                         }
1151                         OBJ(execpi) {
1152                                 print_execpi(obj, p, p_max_size);
1153                         }
1154                         OBJ(texeci) {
1155                                 print_texeci(obj, p, p_max_size);
1156                         }
1157                         OBJ(imap_unseen) {
1158                                 print_imap_unseen(obj, p, p_max_size);
1159                         }
1160                         OBJ(imap_messages) {
1161                                 print_imap_messages(obj, p, p_max_size);
1162                         }
1163                         OBJ(pop3_unseen) {
1164                                 print_pop3_unseen(obj, p, p_max_size);
1165                         }
1166                         OBJ(pop3_used) {
1167                                 print_pop3_used(obj, p, p_max_size);
1168                         }
1169                         OBJ(fs_bar) {
1170                                 print_fs_bar(obj, 0, p, p_max_size);
1171                         }
1172                         OBJ(fs_free) {
1173                                 print_fs_free(obj, p, p_max_size);
1174                         }
1175                         OBJ(fs_free_perc) {
1176                                 print_fs_perc(obj, 1, p, p_max_size);
1177                         }
1178                         OBJ(fs_size) {
1179                                 print_fs_size(obj, p, p_max_size);
1180                         }
1181                         OBJ(fs_type) {
1182                                 print_fs_type(obj, p, p_max_size);
1183                         }
1184                         OBJ(fs_used) {
1185                                 print_fs_used(obj, p, p_max_size);
1186                         }
1187                         OBJ(fs_bar_free) {
1188                                 print_fs_bar(obj, 1, p, p_max_size);
1189                         }
1190                         OBJ(fs_used_perc) {
1191                                 print_fs_perc(obj, 0, p, p_max_size);
1192                         }
1193                         OBJ(loadavg) {
1194                                 print_loadavg(obj, p, p_max_size);
1195                         }
1196                         OBJ(goto) {
1197                                 new_goto(p, obj->data.i);
1198                         }
1199                         OBJ(tab) {
1200                                 new_tab(obj, p);
1201                         }
1202 #ifdef X11
1203                         OBJ(hr) {
1204                                 new_hr(p, obj->data.i);
1205                         }
1206 #endif
1207                         OBJ(nameserver) {
1208                                 print_nameserver(obj, p, p_max_size);
1209                         }
1210 #ifdef EVE
1211                         OBJ(eve) {
1212                                 print_eve(obj, p, p_max_size);
1213                         }
1214 #endif
1215 #ifdef HAVE_CURL
1216                         OBJ(curl) {
1217                                 curl_print(obj, p, p_max_size);
1218                         }
1219 #endif
1220 #ifdef RSS
1221                         OBJ(rss) {
1222                                 rss_print_info(obj, p, p_max_size);
1223                         }
1224 #endif
1225 #ifdef WEATHER
1226                         OBJ(weather) {
1227                                 print_weather(obj, p, p_max_size);
1228                         }
1229 #endif
1230 #ifdef XOAP
1231                         OBJ(weather_forecast) {
1232                                 print_weather_forecast(obj, p, p_max_size);
1233                         }
1234 #endif
1235 #ifdef HAVE_LUA
1236                         OBJ(lua) {
1237                                 print_lua(obj, p, p_max_size);
1238                         }
1239                         OBJ(lua_parse) {
1240                                 print_lua_parse(obj, p, p_max_size);
1241                         }
1242                         OBJ(lua_bar) {
1243                                 print_lua_bar(obj, p, p_max_size);
1244                         }
1245 #ifdef X11
1246                         OBJ(lua_graph) {
1247                                 print_lua_graph(obj, p, p_max_size);
1248                         }
1249 #endif /* X11 */
1250                         OBJ(lua_gauge) {
1251                                 print_lua_gauge(obj, p, p_max_size);
1252                         }
1253 #endif /* HAVE_LUA */
1254 #ifdef HDDTEMP
1255                         OBJ(hddtemp) {
1256                                 short val;
1257                                 char unit;
1258
1259                                 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1260                                         snprintf(p, p_max_size, "N/A");
1261                                 } else {
1262                                         temp_print(p, p_max_size, (double)val,
1263                                                         (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1264                                 }
1265                         }
1266 #endif
1267                         OBJ(offset) {
1268                                 new_offset(p, obj->data.i);
1269                         }
1270                         OBJ(voffset) {
1271                                 new_voffset(p, obj->data.i);
1272                         }
1273 #ifdef __linux__
1274                         OBJ(i2c) {
1275                                 print_sysfs_sensor(obj, p, p_max_size);
1276                         }
1277                         OBJ(platform) {
1278                                 print_sysfs_sensor(obj, p, p_max_size);
1279                         }
1280                         OBJ(hwmon) {
1281                                 print_sysfs_sensor(obj, p, p_max_size);
1282                         }
1283 #endif /* __linux__ */
1284                         OBJ(alignr) {
1285                                 new_alignr(p, obj->data.i);
1286                         }
1287                         OBJ(alignc) {
1288                                 new_alignc(p, obj->data.i);
1289                         }
1290                         OBJ(if_empty) {
1291                                 char buf[max_user_text];
1292                                 struct information *tmp_info =
1293                                         malloc(sizeof(struct information));
1294                                 memcpy(tmp_info, cur, sizeof(struct information));
1295                                 generate_text_internal(buf, max_user_text,
1296                                                        *obj->sub, tmp_info);
1297
1298                                 if (strlen(buf) != 0) {
1299                                         DO_JUMP;
1300                                 }
1301                                 free(tmp_info);
1302                         }
1303                         OBJ(if_match) {
1304                                 char expression[max_user_text];
1305                                 int val;
1306                                 struct information *tmp_info;
1307
1308                                 tmp_info = malloc(sizeof(struct information));
1309                                 memcpy(tmp_info, cur, sizeof(struct information));
1310                                 generate_text_internal(expression, max_user_text,
1311                                                        *obj->sub, tmp_info);
1312                                 DBGP("parsed arg into '%s'", expression);
1313
1314                                 val = compare(expression);
1315                                 if (val == -2) {
1316                                         NORM_ERR("compare failed for expression '%s'",
1317                                                         expression);
1318                                 } else if (!val) {
1319                                         DO_JUMP;
1320                                 }
1321                                 free(tmp_info);
1322                         }
1323                         OBJ(if_existing) {
1324                                 char *spc;
1325
1326                                 spc = strchr(obj->data.s, ' ');
1327                                 if (!spc && access(obj->data.s, F_OK)) {
1328                                         DO_JUMP;
1329                                 } else if (spc) {
1330                                         *spc = '\0';
1331                                         if (!check_contains(obj->data.s, spc + 1))
1332                                                 DO_JUMP;
1333                                         *spc = ' ';
1334                                 }
1335                         }
1336                         OBJ(if_mounted) {
1337                                 if ((obj->data.s)
1338                                                 && (!check_mount(obj->data.s))) {
1339                                         DO_JUMP;
1340                                 }
1341                         }
1342                         OBJ(if_running) {
1343 #ifdef __linux__
1344                                 if (!get_process_by_name(obj->data.s)) {
1345 #else
1346                                 if ((obj->data.s) && system(obj->data.s)) {
1347 #endif
1348                                         DO_JUMP;
1349                                 }
1350                         }
1351 #if defined(__linux__)
1352                         OBJ(ioscheduler) {
1353                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1354                         }
1355 #endif
1356                         OBJ(kernel) {
1357                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1358                         }
1359                         OBJ(machine) {
1360                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1361                         }
1362
1363                         /* memory stuff */
1364                         OBJ(mem) {
1365                                 human_readable(cur->mem * 1024, p, 255);
1366                         }
1367                         OBJ(memeasyfree) {
1368                                 human_readable(cur->memeasyfree * 1024, p, 255);
1369                         }
1370                         OBJ(memfree) {
1371                                 human_readable(cur->memfree * 1024, p, 255);
1372                         }
1373                         OBJ(memmax) {
1374                                 human_readable(cur->memmax * 1024, p, 255);
1375                         }
1376                         OBJ(memperc) {
1377                                 if (cur->memmax)
1378                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1379                         }
1380                         OBJ(memgauge){
1381                                 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1382                         }
1383                         OBJ(membar) {
1384                                 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1385                         }
1386 #ifdef X11
1387                         OBJ(memgraph) {
1388                                 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1389                         }
1390 #endif /* X11 */
1391                         /* mixer stuff */
1392                         OBJ(mixer) {
1393                                 print_mixer(obj, p, p_max_size);
1394                         }
1395                         OBJ(mixerl) {
1396                                 print_mixerl(obj, p, p_max_size);
1397                         }
1398                         OBJ(mixerr) {
1399                                 print_mixerr(obj, p, p_max_size);
1400                         }
1401                         OBJ(mixerbar) {
1402                                 print_mixer_bar(obj, p, p_max_size);
1403                         }
1404                         OBJ(mixerlbar) {
1405                                 print_mixerl_bar(obj, p, p_max_size);
1406                         }
1407                         OBJ(mixerrbar) {
1408                                 print_mixerr_bar(obj, p, p_max_size);
1409                         }
1410                         OBJ(if_mixer_mute) {
1411                                 if (!check_mixer_muted(obj)) {
1412                                         DO_JUMP;
1413                                 }
1414                         }
1415 #ifdef X11
1416 #define NOT_IN_X "Not running in X"
1417                         OBJ(monitor) {
1418                                 if(x_initialised != YES) {
1419                                         strncpy(p, NOT_IN_X, p_max_size);
1420                                 }else{
1421                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1422                                 }
1423                         }
1424                         OBJ(monitor_number) {
1425                                 if(x_initialised != YES) {
1426                                         strncpy(p, NOT_IN_X, p_max_size);
1427                                 }else{
1428                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1429                                 }
1430                         }
1431                         OBJ(desktop) {
1432                                 if(x_initialised != YES) {
1433                                         strncpy(p, NOT_IN_X, p_max_size);
1434                                 }else{
1435                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1436                                 }
1437                         }
1438                         OBJ(desktop_number) {
1439                                 if(x_initialised != YES) {
1440                                         strncpy(p, NOT_IN_X, p_max_size);
1441                                 }else{
1442                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1443                                 }
1444                         }
1445                         OBJ(desktop_name) {
1446                                 if(x_initialised != YES) {
1447                                         strncpy(p, NOT_IN_X, p_max_size);
1448                                 }else if(cur->x11.desktop.name != NULL) {
1449                                         strncpy(p, cur->x11.desktop.name, p_max_size);
1450                                 }
1451                         }
1452 #endif /* X11 */
1453
1454                         OBJ(format_time) {
1455                                 char buf[max_user_text];
1456
1457                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1458                                 obj->data.s = buf;
1459                                 print_format_time(obj, p, p_max_size);
1460                         }
1461                         /* mail stuff */
1462                         OBJ(mails) {
1463                                 print_mails(obj, p, p_max_size);
1464                         }
1465                         OBJ(new_mails) {
1466                                 print_new_mails(obj, p, p_max_size);
1467                         }
1468                         OBJ(seen_mails) {
1469                                 print_seen_mails(obj, p, p_max_size);
1470                         }
1471                         OBJ(unseen_mails) {
1472                                 print_unseen_mails(obj, p, p_max_size);
1473                         }
1474                         OBJ(flagged_mails) {
1475                                 print_flagged_mails(obj, p, p_max_size);
1476                         }
1477                         OBJ(unflagged_mails) {
1478                                 print_unflagged_mails(obj, p, p_max_size);
1479                         }
1480                         OBJ(forwarded_mails) {
1481                                 print_forwarded_mails(obj, p, p_max_size);
1482                         }
1483                         OBJ(unforwarded_mails) {
1484                                 print_unforwarded_mails(obj, p, p_max_size);
1485                         }
1486                         OBJ(replied_mails) {
1487                                 print_replied_mails(obj, p, p_max_size);
1488                         }
1489                         OBJ(unreplied_mails) {
1490                                 print_unreplied_mails(obj, p, p_max_size);
1491                         }
1492                         OBJ(draft_mails) {
1493                                 print_draft_mails(obj, p, p_max_size);
1494                         }
1495                         OBJ(trashed_mails) {
1496                                 print_trashed_mails(obj, p, p_max_size);
1497                         }
1498                         OBJ(mboxscan) {
1499                                 print_mboxscan(obj, p, p_max_size);
1500                         }
1501                         OBJ(nodename) {
1502                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1503                         }
1504                         OBJ(nodename_short) {
1505                                 char *pos;
1506                                 pos = strstr(cur->uname_s.nodename, ".");
1507                                 if(pos != NULL) {
1508                                         snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1509                                 } else {
1510                                         snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1511                                 }
1512                         }
1513                         OBJ(outlinecolor) {
1514                                 new_outline(p, obj->data.l);
1515                         }
1516                         OBJ(pid_chroot) {
1517                                 char buf[max_user_text];
1518
1519                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1520                                 obj->data.s = buf;
1521                                 print_pid_chroot(obj, p, p_max_size);
1522                         }
1523                         OBJ(pid_cmdline) {
1524                                 char buf[max_user_text];
1525
1526                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1527                                 obj->data.s = buf;
1528                                 print_pid_cmdline(obj, p, p_max_size);
1529                         }
1530                         OBJ(pid_cwd) {
1531                                 char buf[max_user_text];
1532
1533                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1534                                 obj->data.s = buf;
1535                                 print_pid_cwd(obj, p, p_max_size);
1536                         }
1537                         OBJ(pid_environ) {
1538                                 char buf[max_user_text];
1539
1540                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1541                                 obj->data.s = buf;
1542                                 print_pid_environ(obj, p, p_max_size);
1543                         }
1544                         OBJ(pid_environ_list) {
1545                                 char buf[max_user_text];
1546
1547                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1548                                 obj->data.s = buf;
1549                                 print_pid_environ_list(obj, p, p_max_size);
1550                         }
1551                         OBJ(pid_exe) {
1552                                 char buf[max_user_text];
1553
1554                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1555                                 obj->data.s = buf;
1556                                 print_pid_exe(obj, p, p_max_size);
1557                         }
1558                         OBJ(pid_nice) {
1559                                 char buf[max_user_text];
1560
1561                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1562                                 obj->data.s = buf;
1563                                 print_pid_nice(obj, p, p_max_size);
1564                         }
1565                         OBJ(pid_openfiles) {
1566                                 char buf[max_user_text];
1567
1568                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1569                                 obj->data.s = buf;
1570                                 print_pid_openfiles(obj, p, p_max_size);
1571                         }
1572                         OBJ(pid_parent) {
1573                                 char buf[max_user_text];
1574
1575                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1576                                 obj->data.s = buf;
1577                                 print_pid_parent(obj, p, p_max_size);
1578                         }
1579                         OBJ(pid_priority) {
1580                                 char buf[max_user_text];
1581
1582                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1583                                 obj->data.s = buf;
1584                                 print_pid_priority(obj, p, p_max_size);
1585                         }
1586                         OBJ(pid_state) {
1587                                 char buf[max_user_text];
1588
1589                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1590                                 obj->data.s = buf;
1591                                 print_pid_state(obj, p, p_max_size);
1592                         }
1593                         OBJ(pid_state_short) {
1594                                 char buf[max_user_text];
1595
1596                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1597                                 obj->data.s = buf;
1598                                 print_pid_state_short(obj, p, p_max_size);
1599                         }
1600                         OBJ(pid_stderr) {
1601                                 char buf[max_user_text];
1602
1603                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1604                                 obj->data.s = buf;
1605                                 print_pid_stderr(obj, p, p_max_size);
1606                         }
1607                         OBJ(pid_stdin) {
1608                                 char buf[max_user_text];
1609
1610                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1611                                 obj->data.s = buf;
1612                                 print_pid_stdin(obj, p, p_max_size);
1613                         }
1614                         OBJ(pid_stdout) {
1615                                 char buf[max_user_text];
1616
1617                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1618                                 obj->data.s = buf;
1619                                 print_pid_stdout(obj, p, p_max_size);
1620                         }
1621                         OBJ(pid_threads) {
1622                                 char buf[max_user_text];
1623
1624                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1625                                 obj->data.s = buf;
1626                                 print_pid_threads(obj, p, p_max_size);
1627                         }
1628                         OBJ(pid_thread_list) {
1629                                 char buf[max_user_text];
1630
1631                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1632                                 obj->data.s = buf;
1633                                 print_pid_thread_list(obj, p, p_max_size);
1634                         }
1635                         OBJ(pid_time_kernelmode) {
1636                                 char buf[max_user_text];
1637
1638                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1639                                 obj->data.s = buf;
1640                                 print_pid_time_kernelmode(obj, p, p_max_size);
1641                         }
1642                         OBJ(pid_time_usermode) {
1643                                 char buf[max_user_text];
1644
1645                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1646                                 obj->data.s = buf;
1647                                 print_pid_time_usermode(obj, p, p_max_size);
1648                         }
1649                         OBJ(pid_time) {
1650                                 char buf[max_user_text];
1651
1652                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1653                                 obj->data.s = buf;
1654                                 print_pid_time(obj, p, p_max_size);
1655                         }
1656                         OBJ(pid_uid) {
1657                                 char buf[max_user_text];
1658
1659                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1660                                 obj->data.s = buf;
1661                                 print_pid_uid(obj, p, p_max_size);
1662                         }
1663                         OBJ(pid_euid) {
1664                                 char buf[max_user_text];
1665
1666                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1667                                 obj->data.s = buf;
1668                                 print_pid_euid(obj, p, p_max_size);
1669                         }
1670                         OBJ(pid_suid) {
1671                                 char buf[max_user_text];
1672
1673                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1674                                 obj->data.s = buf;
1675                                 print_pid_suid(obj, p, p_max_size);
1676                         }
1677                         OBJ(pid_fsuid) {
1678                                 char buf[max_user_text];
1679
1680                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1681                                 obj->data.s = buf;
1682                                 print_pid_fsuid(obj, p, p_max_size);
1683                         }
1684                         OBJ(pid_gid) {
1685                                 char buf[max_user_text];
1686
1687                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1688                                 obj->data.s = buf;
1689                                 print_pid_gid(obj, p, p_max_size);
1690                         }
1691                         OBJ(pid_egid) {
1692                                 char buf[max_user_text];
1693
1694                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1695                                 obj->data.s = buf;
1696                                 print_pid_egid(obj, p, p_max_size);
1697                         }
1698                         OBJ(pid_sgid) {
1699                                 char buf[max_user_text];
1700
1701                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1702                                 obj->data.s = buf;
1703                                 print_pid_sgid(obj, p, p_max_size);
1704                         }
1705                         OBJ(pid_fsgid) {
1706                                 char buf[max_user_text];
1707
1708                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1709                                 obj->data.s = buf;
1710                                 print_pid_fsgid(obj, p, p_max_size);
1711                         }
1712                         OBJ(pid_read) {
1713                                 char buf[max_user_text];
1714
1715                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1716                                 obj->data.s = buf;
1717                                 print_pid_read(obj, p, p_max_size);
1718                         }
1719                         OBJ(pid_vmpeak) {
1720                                 char buf[max_user_text];
1721
1722                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1723                                 obj->data.s = buf;
1724                                 print_pid_vmpeak(obj, p, p_max_size);
1725                         }
1726                         OBJ(pid_vmsize) {
1727                                 char buf[max_user_text];
1728
1729                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1730                                 obj->data.s = buf;
1731                                 print_pid_vmsize(obj, p, p_max_size);
1732                         }
1733                         OBJ(pid_vmlck) {
1734                                 char buf[max_user_text];
1735
1736                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1737                                 obj->data.s = buf;
1738                                 print_pid_vmlck(obj, p, p_max_size);
1739                         }
1740                         OBJ(pid_vmhwm) {
1741                                 char buf[max_user_text];
1742
1743                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1744                                 obj->data.s = buf;
1745                                 print_pid_vmhwm(obj, p, p_max_size);
1746                         }
1747                         OBJ(pid_vmrss) {
1748                                 char buf[max_user_text];
1749
1750                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1751                                 obj->data.s = buf;
1752                                 print_pid_vmrss(obj, p, p_max_size);
1753                         }
1754                         OBJ(pid_vmdata) {
1755                                 char buf[max_user_text];
1756
1757                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1758                                 obj->data.s = buf;
1759                                 print_pid_vmdata(obj, p, p_max_size);
1760                         }
1761                         OBJ(pid_vmstk) {
1762                                 char buf[max_user_text];
1763
1764                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1765                                 obj->data.s = buf;
1766                                 print_pid_vmstk(obj, p, p_max_size);
1767                         }
1768                         OBJ(pid_vmexe) {
1769                                 char buf[max_user_text];
1770
1771                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1772                                 obj->data.s = buf;
1773                                 print_pid_vmexe(obj, p, p_max_size);
1774                         }
1775                         OBJ(pid_vmlib) {
1776                                 char buf[max_user_text];
1777
1778                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1779                                 obj->data.s = buf;
1780                                 print_pid_vmlib(obj, p, p_max_size);
1781                         }
1782                         OBJ(pid_vmpte) {
1783                                 char buf[max_user_text];
1784
1785                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1786                                 obj->data.s = buf;
1787                                 print_pid_vmpte(obj, p, p_max_size);
1788                         }
1789                         OBJ(pid_write) {
1790                                 char buf[max_user_text];
1791
1792                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1793                                 obj->data.s = buf;
1794                                 print_pid_write(obj, p, p_max_size);
1795                         }
1796                         OBJ(processes) {
1797                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1798                         }
1799                         OBJ(running_processes) {
1800                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1801                         }
1802                         OBJ(running_threads) {
1803                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1804                         }
1805                         OBJ(threads) {
1806                                 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1807                         }
1808                         OBJ(text) {
1809                                 snprintf(p, p_max_size, "%s", obj->data.s);
1810                         }
1811 #ifdef X11
1812                         OBJ(shadecolor) {
1813                                 new_bg(p, obj->data.l);
1814                         }
1815                         OBJ(stippled_hr) {
1816                                 new_stippled_hr(obj, p);
1817                         }
1818 #endif /* X11 */
1819                         OBJ(swap) {
1820                                 human_readable(cur->swap * 1024, p, 255);
1821                         }
1822                         OBJ(swapfree) {
1823                                 human_readable(cur->swapfree * 1024, p, 255);
1824                         }
1825                         OBJ(swapmax) {
1826                                 human_readable(cur->swapmax * 1024, p, 255);
1827                         }
1828                         OBJ(swapperc) {
1829                                 if (cur->swapmax == 0) {
1830                                         strncpy(p, "No swap", p_max_size);
1831                                 } else {
1832                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1833                                 }
1834                         }
1835                         OBJ(swapbar) {
1836                                 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1837                         }
1838                         OBJ(sysname) {
1839                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1840                         }
1841                         OBJ(time) {
1842                                 print_time(obj, p, p_max_size);
1843                         }
1844                         OBJ(utime) {
1845                                 print_utime(obj, p, p_max_size);
1846                         }
1847                         OBJ(tztime) {
1848                                 print_tztime(obj, p, p_max_size);
1849                         }
1850                         OBJ(totaldown) {
1851                                 print_totaldown(obj, p, p_max_size);
1852                         }
1853                         OBJ(totalup) {
1854                                 print_totalup(obj, p, p_max_size);
1855                         }
1856                         OBJ(gid_name) {
1857                                 char buf[max_user_text];
1858
1859                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1860                                 obj->data.s = buf;
1861                                 print_gid_name(obj, p, p_max_size);
1862                         }
1863                         OBJ(uid_name) {
1864                                 char buf[max_user_text];
1865
1866                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1867                                 obj->data.s = buf;
1868                                 print_uid_name(obj, p, p_max_size);
1869                         }
1870                         OBJ(updates) {
1871                                 snprintf(p, p_max_size, "%d", total_updates);
1872                         }
1873                         OBJ(if_updatenr) {
1874                                 if(total_updates % updatereset != obj->data.i - 1) {
1875                                         DO_JUMP;
1876                                 }
1877                         }
1878                         OBJ(upspeed) {
1879                                 print_upspeed(obj, p, p_max_size);
1880                         }
1881                         OBJ(upspeedf) {
1882                                 print_upspeedf(obj, p, p_max_size);
1883                         }
1884 #ifdef X11
1885                         OBJ(upspeedgraph) {
1886                                 print_upspeedgraph(obj, p, p_max_size);
1887                         }
1888 #endif /* X11 */
1889                         OBJ(uptime_short) {
1890                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
1891                         }
1892                         OBJ(uptime) {
1893                                 format_seconds(p, p_max_size, (int) cur->uptime);
1894                         }
1895 #ifdef __linux__
1896                         OBJ(user_names) {
1897                                 snprintf(p, p_max_size, "%s", cur->users.names);
1898                         }
1899                         OBJ(user_terms) {
1900                                 snprintf(p, p_max_size, "%s", cur->users.terms);
1901                         }
1902                         OBJ(user_times) {
1903                                 snprintf(p, p_max_size, "%s", cur->users.times);
1904                         }
1905                         OBJ(user_time) {
1906                                 update_user_time(obj->data.s);
1907                                 snprintf(p, p_max_size, "%s", cur->users.ctime);
1908                         }
1909                         OBJ(user_number) {
1910                                 snprintf(p, p_max_size, "%d", cur->users.number);
1911                         }
1912 #endif /* __linux__ */
1913 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1914                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1915                         OBJ(apm_adapter) {
1916                                 char *msg;
1917
1918                                 msg = get_apm_adapter();
1919                                 snprintf(p, p_max_size, "%s", msg);
1920                                 free(msg);
1921                         }
1922                         OBJ(apm_battery_life) {
1923                                 char *msg;
1924
1925                                 msg = get_apm_battery_life();
1926                                 snprintf(p, p_max_size, "%s", msg);
1927                                 free(msg);
1928                         }
1929                         OBJ(apm_battery_time) {
1930                                 char *msg;
1931
1932                                 msg = get_apm_battery_time();
1933                                 snprintf(p, p_max_size, "%s", msg);
1934                                 free(msg);
1935                         }
1936 #endif /* __FreeBSD__ __OpenBSD__ */
1937
1938 #ifdef MPD
1939                         OBJ(mpd_title)
1940                                 print_mpd_title(obj, p, p_max_size);
1941                         OBJ(mpd_artist)
1942                                 print_mpd_artist(obj, p, p_max_size);
1943                         OBJ(mpd_album)
1944                                 print_mpd_album(obj, p, p_max_size);
1945                         OBJ(mpd_random)
1946                                 print_mpd_random(obj, p, p_max_size);
1947                         OBJ(mpd_repeat)
1948                                 print_mpd_repeat(obj, p, p_max_size);
1949                         OBJ(mpd_track)
1950                                 print_mpd_track(obj, p, p_max_size);
1951                         OBJ(mpd_name)
1952                                 print_mpd_name(obj, p, p_max_size);
1953                         OBJ(mpd_file)
1954                                 print_mpd_file(obj, p, p_max_size);
1955                         OBJ(mpd_vol)
1956                                 print_mpd_vol(obj, p, p_max_size);
1957                         OBJ(mpd_bitrate)
1958                                 print_mpd_bitrate(obj, p, p_max_size);
1959                         OBJ(mpd_status)
1960                                 print_mpd_status(obj, p, p_max_size);
1961                         OBJ(mpd_elapsed) {
1962                                 print_mpd_elapsed(obj, p, p_max_size);
1963                         }
1964                         OBJ(mpd_length) {
1965                                 print_mpd_length(obj, p, p_max_size);
1966                         }
1967                         OBJ(mpd_percent) {
1968                                 print_mpd_percent(obj, p, p_max_size);
1969                         }
1970                         OBJ(mpd_bar) {
1971                                 print_mpd_bar(obj, p, p_max_size);
1972                         }
1973                         OBJ(mpd_smart) {
1974                                 print_mpd_smart(obj, p, p_max_size);
1975                         }
1976                         OBJ(if_mpd_playing) {
1977                                 if (!mpd_get_info()->is_playing) {
1978                                         DO_JUMP;
1979                                 }
1980                         }
1981 #endif
1982
1983 #ifdef MOC
1984 #define MOC_PRINT(t, a) \
1985         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1986                         OBJ(moc_state) {
1987                                 MOC_PRINT(state, "??");
1988                         }
1989                         OBJ(moc_file) {
1990                                 MOC_PRINT(file, "no file");
1991                         }
1992                         OBJ(moc_title) {
1993                                 MOC_PRINT(title, "no title");
1994                         }
1995                         OBJ(moc_artist) {
1996                                 MOC_PRINT(artist, "no artist");
1997                         }
1998                         OBJ(moc_song) {
1999                                 MOC_PRINT(song, "no song");
2000                         }
2001                         OBJ(moc_album) {
2002                                 MOC_PRINT(album, "no album");
2003                         }
2004                         OBJ(moc_totaltime) {
2005                                 MOC_PRINT(totaltime, "0:00");
2006                         }
2007                         OBJ(moc_timeleft) {
2008                                 MOC_PRINT(timeleft, "0:00");
2009                         }
2010                         OBJ(moc_curtime) {
2011                                 MOC_PRINT(curtime, "0:00");
2012                         }
2013                         OBJ(moc_bitrate) {
2014                                 MOC_PRINT(bitrate, "0Kbps");
2015                         }
2016                         OBJ(moc_rate) {
2017                                 MOC_PRINT(rate, "0KHz");
2018                         }
2019 #undef MOC_PRINT
2020 #endif /* MOC */
2021 #ifdef XMMS2
2022                         OBJ(xmms2_artist) {
2023                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2024                         }
2025                         OBJ(xmms2_album) {
2026                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2027                         }
2028                         OBJ(xmms2_title) {
2029                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2030                         }
2031                         OBJ(xmms2_genre) {
2032                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2033                         }
2034                         OBJ(xmms2_comment) {
2035                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2036                         }
2037                         OBJ(xmms2_url) {
2038                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2039                         }
2040                         OBJ(xmms2_status) {
2041                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2042                         }
2043                         OBJ(xmms2_date) {
2044                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2045                         }
2046                         OBJ(xmms2_tracknr) {
2047                                 if (cur->xmms2.tracknr != -1) {
2048                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2049                                 }
2050                         }
2051                         OBJ(xmms2_bitrate) {
2052                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2053                         }
2054                         OBJ(xmms2_id) {
2055                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2056                         }
2057                         OBJ(xmms2_size) {
2058                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2059                         }
2060                         OBJ(xmms2_elapsed) {
2061                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2062                                         (cur->xmms2.elapsed / 1000) % 60);
2063                         }
2064                         OBJ(xmms2_duration) {
2065                                 snprintf(p, p_max_size, "%02d:%02d",
2066                                         cur->xmms2.duration / 60000,
2067                                         (cur->xmms2.duration / 1000) % 60);
2068                         }
2069                         OBJ(xmms2_percent) {
2070                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2071                         }
2072                         OBJ(xmms2_bar) {
2073                                 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2074                         }
2075                         OBJ(xmms2_playlist) {
2076                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2077                         }
2078                         OBJ(xmms2_timesplayed) {
2079                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2080                         }
2081                         OBJ(xmms2_smart) {
2082                                 if (strlen(cur->xmms2.title) < 2
2083                                                 && strlen(cur->xmms2.title) < 2) {
2084                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
2085                                 } else {
2086                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2087                                                 cur->xmms2.title);
2088                                 }
2089                         }
2090                         OBJ(if_xmms2_connected) {
2091                                 if (cur->xmms2.conn_state != 1) {
2092                                         DO_JUMP;
2093                                 }
2094                         }
2095 #endif /* XMMS */
2096 #ifdef AUDACIOUS
2097                         OBJ(audacious_status) {
2098                                 snprintf(p, p_max_size, "%s",
2099                                         cur->audacious.items[AUDACIOUS_STATUS]);
2100                         }
2101                         OBJ(audacious_title) {
2102                                 snprintf(p, cur->audacious.max_title_len > 0
2103                                         ? cur->audacious.max_title_len : p_max_size, "%s",
2104                                         cur->audacious.items[AUDACIOUS_TITLE]);
2105                         }
2106                         OBJ(audacious_length) {
2107                                 snprintf(p, p_max_size, "%s",
2108                                         cur->audacious.items[AUDACIOUS_LENGTH]);
2109                         }
2110                         OBJ(audacious_length_seconds) {
2111                                 snprintf(p, p_max_size, "%s",
2112                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2113                         }
2114                         OBJ(audacious_position) {
2115                                 snprintf(p, p_max_size, "%s",
2116                                         cur->audacious.items[AUDACIOUS_POSITION]);
2117                         }
2118                         OBJ(audacious_position_seconds) {
2119                                 snprintf(p, p_max_size, "%s",
2120                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2121                         }
2122                         OBJ(audacious_bitrate) {
2123                                 snprintf(p, p_max_size, "%s",
2124                                         cur->audacious.items[AUDACIOUS_BITRATE]);
2125                         }
2126                         OBJ(audacious_frequency) {
2127                                 snprintf(p, p_max_size, "%s",
2128                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
2129                         }
2130                         OBJ(audacious_channels) {
2131                                 snprintf(p, p_max_size, "%s",
2132                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
2133                         }
2134                         OBJ(audacious_filename) {
2135                                 snprintf(p, p_max_size, "%s",
2136                                         cur->audacious.items[AUDACIOUS_FILENAME]);
2137                         }
2138                         OBJ(audacious_playlist_length) {
2139                                 snprintf(p, p_max_size, "%s",
2140                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2141                         }
2142                         OBJ(audacious_playlist_position) {
2143                                 snprintf(p, p_max_size, "%s",
2144                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2145                         }
2146                         OBJ(audacious_main_volume) {
2147                                 snprintf(p, p_max_size, "%s",
2148                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2149                         }
2150                         OBJ(audacious_bar) {
2151                                 double progress;
2152
2153                                 progress =
2154                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2155                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2156                                 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2157                         }
2158 #endif /* AUDACIOUS */
2159
2160 #ifdef BMPX
2161                         OBJ(bmpx_title) {
2162                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2163                         }
2164                         OBJ(bmpx_artist) {
2165                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2166                         }
2167                         OBJ(bmpx_album) {
2168                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2169                         }
2170                         OBJ(bmpx_uri) {
2171                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2172                         }
2173                         OBJ(bmpx_track) {
2174                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2175                         }
2176                         OBJ(bmpx_bitrate) {
2177                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2178                         }
2179 #endif /* BMPX */
2180                         /* we have four different types of top (top, top_mem,
2181                          * top_time and top_io). To avoid having almost-same code four
2182                          * times, we have this special handler. */
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                         OBJ(tail) {
2192                                 print_tailhead("tail", obj, p, p_max_size);
2193                         }
2194                         OBJ(head) {
2195                                 print_tailhead("head", obj, p, p_max_size);
2196                         }
2197                         OBJ(lines) {
2198                                 print_lines(obj, p, p_max_size);
2199                         }
2200
2201                         OBJ(words) {
2202                                 print_words(obj, p, p_max_size);
2203                         }
2204 #ifdef TCP_PORT_MONITOR
2205                         OBJ(tcp_portmon) {
2206                                 tcp_portmon_action(obj, p, p_max_size);
2207                         }
2208 #endif /* TCP_PORT_MONITOR */
2209
2210 #ifdef HAVE_ICONV
2211                         OBJ(iconv_start) {
2212                                 do_iconv_start(obj);
2213                         }
2214                         OBJ(iconv_stop) {
2215                                 do_iconv_stop();
2216                         }
2217 #endif /* HAVE_ICONV */
2218
2219                         OBJ(entropy_avail) {
2220                                 print_entropy_avail(obj, p, p_max_size);
2221                         }
2222                         OBJ(entropy_perc) {
2223                                 print_entropy_perc(obj, p, p_max_size);
2224                         }
2225                         OBJ(entropy_poolsize) {
2226                                 print_entropy_poolsize(obj, p, p_max_size);
2227                         }
2228                         OBJ(entropy_bar) {
2229                                 print_entropy_bar(obj, p, p_max_size);
2230                         }
2231 #ifdef IBM
2232                         OBJ(smapi) {
2233                                 print_smapi(obj, p, p_max_size);
2234                         }
2235                         OBJ(if_smapi_bat_installed) {
2236                                 int idx;
2237                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2238                                         if(!smapi_bat_installed(idx)) {
2239                                                 DO_JUMP;
2240                                         }
2241                                 } else
2242                                         NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2243                         }
2244                         OBJ(smapi_bat_perc) {
2245                                 print_smapi_bat_perc(obj, p, p_max_size);
2246                         }
2247                         OBJ(smapi_bat_temp) {
2248                                 print_smapi_bat_temp(obj, p, p_max_size);
2249                         }
2250                         OBJ(smapi_bat_power) {
2251                                 print_smapi_bat_power(obj, p, p_max_size);
2252                         }
2253                         OBJ(smapi_bat_bar) {
2254                                 print_smapi_bat_bar(obj, p, p_max_size);
2255                         }
2256 #endif /* IBM */
2257                         OBJ(include) {
2258                                 if(obj->sub) {
2259                                         char buf[max_user_text];
2260
2261                                         generate_text_internal(buf, max_user_text, *obj->sub, cur);
2262                                         snprintf(p, p_max_size, "%s", buf);
2263                                 } else {
2264                                         p[0] = 0;
2265                                 }
2266                         }
2267                         OBJ(blink) {
2268                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2269                                 char buf[max_user_text];
2270                                 unsigned int j;
2271
2272                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2273                                 snprintf(p, p_max_size, "%s", buf);
2274                                 if(total_updates % 2) {
2275                                         for(j=0; p[j] != 0; j++) {
2276                                                 p[j] = ' ';
2277                                         }
2278                                 }
2279                         }
2280                         OBJ(to_bytes) {
2281                                 char buf[max_user_text];
2282                                 long long bytes;
2283                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
2284
2285                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2286                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2287                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2288                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2289                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2290                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2291                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2292                                 }
2293                                 snprintf(p, p_max_size, "%s", buf);
2294                         }
2295                         OBJ(scroll) {
2296                                 print_scroll(obj, p, p_max_size, cur);
2297                         }
2298                         OBJ(combine) {
2299                                 print_combine(obj, p, cur);
2300                         }
2301 #ifdef NVIDIA
2302                         OBJ(nvidia) {
2303                                 print_nvidia_value(obj, display, p, p_max_size);
2304                         }
2305 #endif /* NVIDIA */
2306 #ifdef APCUPSD
2307                         OBJ(apcupsd) {
2308                                 /* This is just a meta-object to set host:port */
2309                         }
2310                         OBJ(apcupsd_name) {
2311                                 snprintf(p, p_max_size, "%s",
2312                                                  cur->apcupsd.items[APCUPSD_NAME]);
2313                         }
2314                         OBJ(apcupsd_model) {
2315                                 snprintf(p, p_max_size, "%s",
2316                                                  cur->apcupsd.items[APCUPSD_MODEL]);
2317                         }
2318                         OBJ(apcupsd_upsmode) {
2319                                 snprintf(p, p_max_size, "%s",
2320                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
2321                         }
2322                         OBJ(apcupsd_cable) {
2323                                 snprintf(p, p_max_size, "%s",
2324                                                  cur->apcupsd.items[APCUPSD_CABLE]);
2325                         }
2326                         OBJ(apcupsd_status) {
2327                                 snprintf(p, p_max_size, "%s",
2328                                                  cur->apcupsd.items[APCUPSD_STATUS]);
2329                         }
2330                         OBJ(apcupsd_linev) {
2331                                 snprintf(p, p_max_size, "%s",
2332                                                  cur->apcupsd.items[APCUPSD_LINEV]);
2333                         }
2334                         OBJ(apcupsd_load) {
2335                                 snprintf(p, p_max_size, "%s",
2336                                                  cur->apcupsd.items[APCUPSD_LOAD]);
2337                         }
2338                         OBJ(apcupsd_loadbar) {
2339                                 double progress;
2340                                 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2341                                 new_bar(obj, p, p_max_size, (int) progress);
2342                         }
2343 #ifdef X11
2344                         OBJ(apcupsd_loadgraph) {
2345                                 double progress;
2346                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
2347                                 new_graph(obj, p, p_max_size, (int)progress);
2348                         }
2349 #endif /* X11 */
2350                         OBJ(apcupsd_loadgauge) {
2351                                 double progress;
2352                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2353                                 new_gauge(obj, p, p_max_size, (int)progress);
2354                         }
2355                         OBJ(apcupsd_charge) {
2356                                 snprintf(p, p_max_size, "%s",
2357                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
2358                         }
2359                         OBJ(apcupsd_timeleft) {
2360                                 snprintf(p, p_max_size, "%s",
2361                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
2362                         }
2363                         OBJ(apcupsd_temp) {
2364                                 snprintf(p, p_max_size, "%s",
2365                                                  cur->apcupsd.items[APCUPSD_TEMP]);
2366                         }
2367                         OBJ(apcupsd_lastxfer) {
2368                                 snprintf(p, p_max_size, "%s",
2369                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
2370                         }
2371 #endif /* APCUPSD */
2372                         break;
2373                 }
2374 #undef DO_JUMP
2375
2376
2377                 {
2378                         size_t a = strlen(p);
2379
2380 #ifdef HAVE_ICONV
2381                         iconv_convert(&a, buff_in, p, p_max_size);
2382 #endif /* HAVE_ICONV */
2383                         if (obj->type == OBJ_execp || obj->type == OBJ_execpi || obj->type
2384                                         == OBJ_exec
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 #ifdef X11
3997 void clean_up_x11(void)
3998 {
3999         if(window_created == 1) {
4000                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4001                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4002                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4003                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4004         }
4005         destroy_window();
4006         free_fonts();
4007         fonts = NULL;
4008         if(x11_stuff.region) {
4009                 XDestroyRegion(x11_stuff.region);
4010                 x11_stuff.region = NULL;
4011         }
4012         if(display) {
4013                 XCloseDisplay(display);
4014                 display = NULL;
4015         }
4016         if(info.x11.desktop.all_names) {
4017                 free(info.x11.desktop.all_names);
4018                 info.x11.desktop.all_names = NULL;
4019         }
4020         if (info.x11.desktop.name) {
4021                 free(info.x11.desktop.name);
4022                 info.x11.desktop.name = NULL;
4023         }
4024         x_initialised = NO;
4025 }
4026 #endif
4027
4028 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4029         int i;
4030
4031 #ifdef NCURSES
4032         if(output_methods & TO_NCURSES) {
4033                 endwin();
4034         }
4035 #endif
4036         conftree_empty(currentconffile);
4037         currentconffile = NULL;
4038         if(memtofree1) {
4039                 free(memtofree1);
4040         }
4041         if(memtofree2) {
4042                 free(memtofree2);
4043         }
4044         timed_thread_destroy_registered_threads();
4045
4046         if (info.cpu_usage) {
4047                 free(info.cpu_usage);
4048                 info.cpu_usage = NULL;
4049         }
4050 #ifdef X11
4051         if (x_initialised == YES) {
4052                 clean_up_x11();
4053         }else{
4054                 free(fonts);    //in set_default_configurations a font is set but not loaded
4055                 font_count = -1;
4056         }
4057
4058 #endif /* X11 */
4059
4060         free_templates();
4061
4062         free_text_objects(&global_root_object, 0);
4063         if (tmpstring1) {
4064                 free(tmpstring1);
4065                 tmpstring1 = 0;
4066         }
4067         if (tmpstring2) {
4068                 free(tmpstring2);
4069                 tmpstring2 = 0;
4070         }
4071         if (text_buffer) {
4072                 free(text_buffer);
4073                 text_buffer = 0;
4074         }
4075
4076         if (global_text) {
4077                 free(global_text);
4078                 global_text = 0;
4079         }
4080
4081         free(current_config);
4082         current_config = 0;
4083
4084 #ifdef TCP_PORT_MONITOR
4085         tcp_portmon_clear();
4086 #endif
4087 #ifdef HAVE_CURL
4088         ccurl_free_info();
4089 #endif
4090 #ifdef RSS
4091         rss_free_info();
4092 #endif
4093 #ifdef WEATHER
4094         weather_free_info();
4095 #endif
4096 #ifdef HAVE_LUA
4097         llua_shutdown_hook();
4098         llua_close();
4099 #endif /* HAVE_LUA */
4100 #ifdef IMLIB2
4101         if (output_methods & TO_X)
4102                 cimlib_deinit();
4103 #endif /* IMLIB2 */
4104 #ifdef XOAP
4105         xmlCleanupParser();
4106 #endif /* XOAP */
4107
4108         if (specials) {
4109                 for (i = 0; i < special_count; i++) {
4110                         if (specials[i].type == GRAPH) {
4111                                 free(specials[i].graph);
4112                         }
4113                 }
4114                 free(specials);
4115                 specials = NULL;
4116         }
4117
4118         clear_net_stats();
4119         clear_diskio_stats();
4120         if(global_cpu != NULL) {
4121                 free(global_cpu);
4122                 global_cpu = NULL;
4123         }
4124 }
4125
4126 void clean_up(void *memtofree1, void* memtofree2)
4127 {
4128         free_update_callbacks();
4129         clean_up_without_threads(memtofree1, memtofree2);
4130 }
4131
4132 static int string_to_bool(const char *s)
4133 {
4134         if (!s) {
4135                 // Assumes an option without a true/false means true
4136                 return 1;
4137         } else if (strcasecmp(s, "yes") == EQUAL) {
4138                 return 1;
4139         } else if (strcasecmp(s, "true") == EQUAL) {
4140                 return 1;
4141         } else if (strcasecmp(s, "1") == EQUAL) {
4142                 return 1;
4143         }
4144         return 0;
4145 }
4146
4147 #ifdef X11
4148 static enum alignment string_to_alignment(const char *s)
4149 {
4150         if (strcasecmp(s, "top_left") == EQUAL) {
4151                 return TOP_LEFT;
4152         } else if (strcasecmp(s, "top_right") == EQUAL) {
4153                 return TOP_RIGHT;
4154         } else if (strcasecmp(s, "top_middle") == EQUAL) {
4155                 return TOP_MIDDLE;
4156         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4157                 return BOTTOM_LEFT;
4158         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4159                 return BOTTOM_RIGHT;
4160         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4161                 return BOTTOM_MIDDLE;
4162         } else if (strcasecmp(s, "middle_left") == EQUAL) {
4163                 return MIDDLE_LEFT;
4164         } else if (strcasecmp(s, "middle_right") == EQUAL) {
4165                 return MIDDLE_RIGHT;
4166         } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4167                 return MIDDLE_MIDDLE;
4168         } else if (strcasecmp(s, "tl") == EQUAL) {
4169                 return TOP_LEFT;
4170         } else if (strcasecmp(s, "tr") == EQUAL) {
4171                 return TOP_RIGHT;
4172         } else if (strcasecmp(s, "tm") == EQUAL) {
4173                 return TOP_MIDDLE;
4174         } else if (strcasecmp(s, "bl") == EQUAL) {
4175                 return BOTTOM_LEFT;
4176         } else if (strcasecmp(s, "br") == EQUAL) {
4177                 return BOTTOM_RIGHT;
4178         } else if (strcasecmp(s, "bm") == EQUAL) {
4179                 return BOTTOM_MIDDLE;
4180         } else if (strcasecmp(s, "ml") == EQUAL) {
4181                 return MIDDLE_LEFT;
4182         } else if (strcasecmp(s, "mr") == EQUAL) {
4183                 return MIDDLE_RIGHT;
4184         } else if (strcasecmp(s, "mm") == EQUAL) {
4185                 return MIDDLE_MIDDLE;
4186         } else if (strcasecmp(s, "none") == EQUAL) {
4187                 return NONE;
4188         }
4189         return ALIGNMENT_ERROR;
4190 }
4191 #endif /* X11 */
4192
4193 #ifdef X11
4194 static void set_default_configurations_for_x(void)
4195 {
4196         default_fg_color = WhitePixel(display, screen);
4197         default_bg_color = BlackPixel(display, screen);
4198         default_out_color = BlackPixel(display, screen);
4199         color0 = default_fg_color;
4200         color1 = default_fg_color;
4201         color2 = default_fg_color;
4202         color3 = default_fg_color;
4203         color4 = default_fg_color;
4204         color5 = default_fg_color;
4205         color6 = default_fg_color;
4206         color7 = default_fg_color;
4207         color8 = default_fg_color;
4208         color9 = default_fg_color;
4209         current_text_color = default_fg_color;
4210 }
4211 #endif /* X11 */
4212
4213 static void set_default_configurations(void)
4214 {
4215 #ifdef MPD
4216         char *mpd_env_host;
4217         char *mpd_env_port;
4218 #endif
4219         update_uname();
4220         fork_to_background = 0;
4221         total_run_times = 0;
4222         info.cpu_avg_samples = 2;
4223         info.net_avg_samples = 2;
4224         info.diskio_avg_samples = 2;
4225         info.memmax = 0;
4226         top_cpu = 0;
4227         cpu_separate = 0;
4228         short_units = 0;
4229         format_human_readable = 1;
4230         top_mem = 0;
4231         top_time = 0;
4232 #ifdef IOSTATS
4233         top_io = 0;
4234 #endif
4235         top_running = 0;
4236 #ifdef MPD
4237         mpd_env_host = getenv("MPD_HOST");
4238         mpd_env_port = getenv("MPD_PORT");
4239
4240         if (!mpd_env_host || !strlen(mpd_env_host)) {
4241                 mpd_set_host("localhost");
4242         } else {
4243                 /* MPD_HOST environment variable is set */
4244                 char *mpd_hostpart = strchr(mpd_env_host, '@');
4245                 if (!mpd_hostpart) {
4246                         mpd_set_host(mpd_env_host);
4247                 } else {
4248                         /* MPD_HOST contains a password */
4249                         char mpd_password[mpd_hostpart - mpd_env_host + 1];
4250                         snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4251
4252                         if (!strlen(mpd_hostpart + 1)) {
4253                                 mpd_set_host("localhost");
4254                         } else {
4255                                 mpd_set_host(mpd_hostpart + 1);
4256                         }
4257
4258                         mpd_set_password(mpd_password, 1);
4259                 }
4260         }
4261
4262
4263         if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4264                 /* failed to set port from environment variable */
4265                 mpd_set_port("6600");
4266         }
4267 #endif
4268 #ifdef XMMS2
4269         info.xmms2.artist = NULL;
4270         info.xmms2.album = NULL;
4271         info.xmms2.title = NULL;
4272         info.xmms2.genre = NULL;
4273         info.xmms2.comment = NULL;
4274         info.xmms2.url = NULL;
4275         info.xmms2.status = NULL;
4276         info.xmms2.playlist = NULL;
4277 #endif
4278         use_spacer = NO_SPACER;
4279 #ifdef X11
4280         output_methods = TO_X;
4281 #else
4282         output_methods = TO_STDOUT;
4283 #endif
4284 #ifdef X11
4285 #ifdef BUILD_XFT
4286         use_xft = 0;
4287 #endif
4288         show_graph_scale = 0;
4289         show_graph_range = 0;
4290         draw_shades = 1;
4291         draw_borders = 0;
4292         draw_graph_borders = 1;
4293         draw_outline = 0;
4294         set_first_font("6x10");
4295         gap_x = 5;
4296         gap_y = 60;
4297         minimum_width = 5;
4298         minimum_height = 5;
4299         maximum_width = 0;
4300 #ifdef OWN_WINDOW
4301         own_window = 0;
4302         window.type = TYPE_NORMAL;
4303         window.hints = 0;
4304         strcpy(window.class_name, PACKAGE_NAME);
4305         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4306 #ifdef USE_ARGB
4307         use_argb_visual = 0;
4308         own_window_argb_value = 255;
4309 #endif
4310 #endif
4311         stippled_borders = 0;
4312         window.border_inner_margin = 3;
4313         window.border_outer_margin = 1;
4314         window.border_width = 1;
4315         text_alignment = BOTTOM_LEFT;
4316         info.x11.monitor.number = 1;
4317         info.x11.monitor.current = 0;
4318         info.x11.desktop.current = 1;
4319         info.x11.desktop.number = 1;
4320         info.x11.desktop.nitems = 0;
4321         info.x11.desktop.all_names = NULL;
4322         info.x11.desktop.name = NULL;
4323 #endif /* X11 */
4324
4325         free_templates();
4326
4327         free(current_mail_spool);
4328         {
4329                 char buf[256];
4330
4331                 variable_substitute(MAIL_FILE, buf, 256);
4332                 if (buf[0] != '\0') {
4333                         current_mail_spool = strndup(buf, text_buffer_size);
4334                 }
4335         }
4336
4337         no_buffers = 1;
4338         set_update_interval(3);
4339         update_interval_bat = NOBATTERY;
4340         info.music_player_interval = 1.0;
4341         stuff_in_uppercase = 0;
4342         info.users.number = 1;
4343
4344         set_times_in_seconds(0);
4345
4346 #ifdef TCP_PORT_MONITOR
4347         /* set default connection limit */
4348         tcp_portmon_set_max_connections(0);
4349 #endif
4350 }
4351
4352 /* returns 1 if you can overwrite or create the file at 'path' */
4353 static _Bool overwrite_works(const char *path)
4354 {
4355         FILE *filepointer;
4356
4357         if (!(filepointer = fopen(path, "w")))
4358                 return 0;
4359         fclose(filepointer);
4360         return 1;
4361 }
4362
4363 /* returns 1 if you can append or create the file at 'path' */
4364 static _Bool append_works(const char *path)
4365 {
4366         FILE *filepointer;
4367
4368         if (!(filepointer = fopen(path, "a")))
4369                 return 0;
4370         fclose(filepointer);
4371         return 1;
4372 }
4373
4374 #ifdef X11
4375 #ifdef DEBUG
4376 /* WARNING, this type not in Xlib spec */
4377 int x11_error_handler(Display *d, XErrorEvent *err)
4378         __attribute__((noreturn));
4379 int x11_error_handler(Display *d, XErrorEvent *err)
4380 {
4381         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",
4382                         err->type,
4383                         (long unsigned)err->display,
4384                         (long)err->resourceid,
4385                         err->serial,
4386                         err->error_code,
4387                         err->request_code,
4388                         err->minor_code,
4389                         (long unsigned)d
4390                         );
4391         abort();
4392 }
4393
4394 int x11_ioerror_handler(Display *d)
4395         __attribute__((noreturn));
4396 int x11_ioerror_handler(Display *d)
4397 {
4398         NORM_ERR("X Error: Display %lx\n",
4399                         (long unsigned)d
4400                         );
4401         exit(1);
4402 }
4403 #endif /* DEBUG */
4404
4405 static void X11_initialisation(void)
4406 {
4407         if (x_initialised == YES) return;
4408         output_methods |= TO_X;
4409         init_X11(disp);
4410         set_default_configurations_for_x();
4411         x_initialised = YES;
4412 #ifdef DEBUG
4413         _Xdebug = 1;
4414         /* WARNING, this type not in Xlib spec */
4415         XSetErrorHandler(&x11_error_handler);
4416         XSetIOErrorHandler(&x11_ioerror_handler);
4417 #endif /* DEBUG */
4418 }
4419
4420 static char **xargv = 0;
4421 static int xargc = 0;
4422
4423 static void X11_create_window(void)
4424 {
4425         if (output_methods & TO_X) {
4426 #ifdef OWN_WINDOW
4427                 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4428                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4429                                 xargv, xargc);
4430 #else /* OWN_WINDOW */
4431                 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4432                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4433                                 xargv, xargc);
4434 #endif /* OWN_WINDOW */
4435
4436                 setup_fonts();
4437                 load_fonts();
4438                 update_text_area();     /* to position text/window on screen */
4439
4440 #ifdef OWN_WINDOW
4441                 if (own_window && !fixed_pos) {
4442                         XMoveWindow(display, window.window, window.x, window.y);
4443                 }
4444                 if (own_window) {
4445                         set_transparent_background(window.window, own_window_argb_value);
4446                 }
4447 #endif
4448
4449                 create_gc();
4450
4451                 draw_stuff();
4452
4453                 x11_stuff.region = XCreateRegion();
4454 #ifdef HAVE_XDAMAGE
4455                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4456                         NORM_ERR("Xdamage extension unavailable");
4457                 }
4458                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4459                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4460                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4461 #endif /* HAVE_XDAMAGE */
4462
4463                 selected_font = 0;
4464                 update_text_area();     /* to get initial size of the window */
4465         }
4466 #ifdef HAVE_LUA
4467         /* setup lua window globals */
4468         llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4469 #endif /* HAVE_LUA */
4470 }
4471 #endif /* X11 */
4472
4473 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4474 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4475 #define CONF2(a) if (strcasecmp(name, a) == 0)
4476 #define CONF(a) else CONF2(a)
4477 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4478                 || strcasecmp(name, b) == 0)
4479 #define CONF_CONTINUE 1
4480 #define CONF_BREAK 2
4481 #define CONF_BUFF_SIZE 512
4482
4483 static FILE *open_config_file(const char *f)
4484 {
4485 #ifdef CONFIG_OUTPUT
4486         if (!strcmp(f, "==builtin==")) {
4487                 return conf_cookie_open();
4488         } else
4489 #endif /* CONFIG_OUTPUT */
4490                 return fopen(f, "r");
4491 }
4492
4493 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4494 {
4495         char *p, *p2;
4496         (*line)++;
4497         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4498                 return CONF_BREAK;
4499         }
4500         remove_comments(buf);
4501
4502         p = buf;
4503
4504         /* skip spaces */
4505         while (*p && isspace((int) *p)) {
4506                 p++;
4507         }
4508         if (*p == '\0') {
4509                 return CONF_CONTINUE;   /* empty line */
4510         }
4511
4512         *name = p;
4513
4514         /* skip name */
4515         p2 = p;
4516         while (*p2 && !isspace((int) *p2)) {
4517                 p2++;
4518         }
4519         if (*p2 != '\0') {
4520                 *p2 = '\0';     /* break at name's end */
4521                 p2++;
4522         }
4523
4524         /* get value */
4525         if (*p2) {
4526                 p = p2;
4527                 while (*p && isspace((int) *p)) {
4528                         p++;
4529                 }
4530
4531                 *value = p;
4532
4533                 p2 = *value + strlen(*value);
4534                 while (isspace((int) *(p2 - 1))) {
4535                         *--p2 = '\0';
4536                 }
4537         } else {
4538                 *value = 0;
4539         }
4540         return 0;
4541 }
4542
4543 #ifdef X11
4544 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4545 #ifdef OWN_WINDOW
4546         if (windowtype == TYPE_DOCK) {
4547                 NORM_ERR("alignment is disabled when own_window_type is dock");
4548         } else
4549 #endif /*OWN_WINDOW */
4550         if (value) {
4551                 int a = string_to_alignment(value);
4552
4553                 if (a <= 0) {
4554                         if (setbyconffile) {
4555                                 CONF_ERR;
4556                         } else NORM_ERR("'%s' is not a alignment setting", value);
4557                 } else {
4558                         *ltext_alignment = a;
4559                 }
4560         } else if (setbyconffile) {
4561                 CONF_ERR;
4562         }
4563 }
4564 #endif /* X11 */
4565
4566 char load_config_file(const char *f)
4567 {
4568         int line = 0;
4569         FILE *fp;
4570
4571         fp = open_config_file(f);
4572         if (!fp) {
4573                 return FALSE;
4574         }
4575         DBGP("reading contents from config file '%s'", f);
4576
4577         while (!feof(fp)) {
4578                 char buff[CONF_BUFF_SIZE], *name, *value;
4579                 int ret = do_config_step(&line, fp, buff, &name, &value);
4580                 if (ret == CONF_BREAK) {
4581                         break;
4582                 } else if (ret == CONF_CONTINUE) {
4583                         continue;
4584                 }
4585
4586 #ifdef X11
4587                 CONF2("out_to_x") {
4588                         if (string_to_bool(value)) {
4589                                 output_methods &= TO_X;
4590                         } else {
4591                                 clean_up_x11();
4592                                 output_methods &= ~TO_X;
4593                                 x_initialised = NEVER;
4594                         }
4595                 }
4596                 CONF("display") {
4597                         if (!value || x_initialised == YES) {
4598                                 CONF_ERR;
4599                         } else {
4600                                 if (disp)
4601                                         free(disp);
4602                                 disp = strdup(value);
4603                         }
4604                 }
4605                 CONF("alignment") {
4606                         setalignment(&text_alignment, window.type, value, f, line, 1);
4607                 }
4608                 CONF("background") {
4609                         fork_to_background = string_to_bool(value);
4610                 }
4611 #else
4612                 CONF2("background") {
4613                         fork_to_background = string_to_bool(value);
4614                 }
4615 #endif /* X11 */
4616 #ifdef X11
4617                 CONF("show_graph_scale") {
4618                         show_graph_scale = string_to_bool(value);
4619                 }
4620                 CONF("show_graph_range") {
4621                         show_graph_range = string_to_bool(value);
4622                 }
4623                 CONF("border_inner_margin") {
4624                         if (value) {
4625                                 window.border_inner_margin = strtol(value, 0, 0);
4626                                 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4627                         } else {
4628                                 CONF_ERR;
4629                         }
4630                 }
4631                 CONF("border_outer_margin") {
4632                         if (value) {
4633                                 window.border_outer_margin = strtol(value, 0, 0);
4634                                 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4635                         } else {
4636                                 CONF_ERR;
4637                         }
4638                 }
4639                 CONF("border_width") {
4640                         if (value) {
4641                                 window.border_width = strtol(value, 0, 0);
4642                                 if (window.border_width < 1) window.border_width = 1;
4643                         } else {
4644                                 CONF_ERR;
4645                         }
4646                 }
4647 #endif /* X11 */
4648 #define TEMPLATE_CONF(n) \
4649                 CONF("template"#n) { \
4650                         if (set_template(n, value)) \
4651                                 CONF_ERR; \
4652                 }
4653                 TEMPLATE_CONF(0)
4654                 TEMPLATE_CONF(1)
4655                 TEMPLATE_CONF(2)
4656                 TEMPLATE_CONF(3)
4657                 TEMPLATE_CONF(4)
4658                 TEMPLATE_CONF(5)
4659                 TEMPLATE_CONF(6)
4660                 TEMPLATE_CONF(7)
4661                 TEMPLATE_CONF(8)
4662                 TEMPLATE_CONF(9)
4663                 CONF("imap") {
4664                         if (value) {
4665                                 parse_global_imap_mail_args(value);
4666                         } else {
4667                                 CONF_ERR;
4668                         }
4669                 }
4670                 CONF("pop3") {
4671                         if (value) {
4672                                 parse_global_pop3_mail_args(value);
4673                         } else {
4674                                 CONF_ERR;
4675                         }
4676                 }
4677                 CONF("default_bar_size") {
4678                         char err = 0;
4679                         if (value) {
4680                                 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4681                                         err = 1;
4682                                 }
4683                         } else {
4684                                 err = 1;
4685                         }
4686                         if (err) {
4687                                 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4688                         }
4689                 }
4690 #ifdef X11
4691                 CONF("default_graph_size") {
4692                         char err = 0;
4693                         if (value) {
4694                                 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4695                                         err = 1;
4696                                 }
4697                         } else {
4698                                 err = 1;
4699                         }
4700                         if (err) {
4701                                 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4702                         }
4703                 }
4704                 CONF("default_gauge_size") {
4705                         char err = 0;
4706                         if (value) {
4707                                 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4708                                         err = 1;
4709                                 }
4710                         } else {
4711                                 err = 1;
4712                         }
4713                         if (err) {
4714                                 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4715                         }
4716                 }
4717 #endif
4718 #ifdef MPD
4719                 CONF("mpd_host") {
4720                         if (value) {
4721                                 mpd_set_host(value);
4722                         } else {
4723                                 CONF_ERR;
4724                         }
4725                 }
4726                 CONF("mpd_port") {
4727                         if (value && mpd_set_port(value)) {
4728                                 CONF_ERR;
4729                         }
4730                 }
4731                 CONF("mpd_password") {
4732                         if (value) {
4733                                 mpd_set_password(value, 0);
4734                         } else {
4735                                 CONF_ERR;
4736                         }
4737                 }
4738 #endif
4739                 CONF("music_player_interval") {
4740                         if (value) {
4741                                 info.music_player_interval = strtod(value, 0);
4742                         } else {
4743                                 CONF_ERR;
4744                         }
4745                 }
4746 #ifdef __OpenBSD__
4747                 CONF("sensor_device") {
4748                         if (value) {
4749                                 sensor_device = strtol(value, 0, 0);
4750                         } else {
4751                                 CONF_ERR;
4752                         }
4753                 }
4754 #endif
4755                 CONF("cpu_avg_samples") {
4756                         if (value) {
4757                                 cpu_avg_samples = strtol(value, 0, 0);
4758                                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4759                                         CONF_ERR;
4760                                 } else {
4761                                         info.cpu_avg_samples = cpu_avg_samples;
4762                                 }
4763                         } else {
4764                                 CONF_ERR;
4765                         }
4766                 }
4767                 CONF("net_avg_samples") {
4768                         if (value) {
4769                                 net_avg_samples = strtol(value, 0, 0);
4770                                 if (net_avg_samples < 1 || net_avg_samples > 14) {
4771                                         CONF_ERR;
4772                                 } else {
4773                                         info.net_avg_samples = net_avg_samples;
4774                                 }
4775                         } else {
4776                                 CONF_ERR;
4777                         }
4778                 }
4779                 CONF("diskio_avg_samples") {
4780                         if (value) {
4781                                 diskio_avg_samples = strtol(value, 0, 0);
4782                                 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4783                                         CONF_ERR;
4784                                 } else {
4785                                         info.diskio_avg_samples = diskio_avg_samples;
4786                                 }
4787                         } else {
4788                                 CONF_ERR;
4789                         }
4790                 }
4791
4792 #ifdef HAVE_XDBE
4793                 CONF("double_buffer") {
4794                         use_xdbe = string_to_bool(value);
4795                 }
4796 #endif
4797 #ifdef X11
4798                 CONF("override_utf8_locale") {
4799                         utf8_mode = string_to_bool(value);
4800                 }
4801                 CONF("draw_borders") {
4802                         draw_borders = string_to_bool(value);
4803                 }
4804                 CONF("draw_graph_borders") {
4805                         draw_graph_borders = string_to_bool(value);
4806                 }
4807                 CONF("draw_shades") {
4808                         draw_shades = string_to_bool(value);
4809                 }
4810                 CONF("draw_outline") {
4811                         draw_outline = string_to_bool(value);
4812                 }
4813 #endif /* X11 */
4814                 CONF("times_in_seconds") {
4815                         set_times_in_seconds(string_to_bool(value));
4816                 }
4817                 CONF("max_text_width") {
4818                         max_text_width = atoi(value);
4819                 }
4820                 CONF("out_to_console") {
4821                         if(string_to_bool(value)) {
4822                                 output_methods |= TO_STDOUT;
4823                         } else {
4824                                 output_methods &= ~TO_STDOUT;
4825                         }
4826                 }
4827                 CONF("extra_newline") {
4828                         extra_newline = string_to_bool(value);
4829                 }
4830                 CONF("disable_auto_reload") {
4831                         disable_auto_reload = string_to_bool(value);
4832                 }
4833                 CONF("out_to_stderr") {
4834                         if(string_to_bool(value))
4835                                 output_methods |= TO_STDERR;
4836                 }
4837 #ifdef NCURSES
4838                 CONF("out_to_ncurses") {
4839                         if(string_to_bool(value)) {
4840                                 initscr();
4841                                 start_color();
4842                                 output_methods |= TO_NCURSES;
4843                         }
4844                 }
4845 #endif
4846                 CONF("overwrite_file") {
4847                         if(overwrite_file) {
4848                                 free(overwrite_file);
4849                                 overwrite_file = 0;
4850                         }
4851                         if(overwrite_works(value)) {
4852                                 overwrite_file = strdup(value);
4853                                 output_methods |= OVERWRITE_FILE;
4854                         } else
4855                                 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4856                 }
4857                 CONF("append_file") {
4858                         if(append_file) {
4859                                 free(append_file);
4860                                 append_file = 0;
4861                         }
4862                         if(append_works(value)) {
4863                                 append_file = strdup(value);
4864                                 output_methods |= APPEND_FILE;
4865                         } else
4866                                 NORM_ERR("append_file won't be able to create/append '%s'", value);
4867                 }
4868                 CONF("use_spacer") {
4869                         if (value) {
4870                                 if (strcasecmp(value, "left") == EQUAL) {
4871                                         use_spacer = LEFT_SPACER;
4872                                 } else if (strcasecmp(value, "right") == EQUAL) {
4873                                         use_spacer = RIGHT_SPACER;
4874                                 } else if (strcasecmp(value, "none") == EQUAL) {
4875                                         use_spacer = NO_SPACER;
4876                                 } else {
4877                                         use_spacer = string_to_bool(value);
4878                                         NORM_ERR("use_spacer should have an argument of left, right, or"
4879                                                 " none.  '%s' seems to be some form of '%s', so"
4880                                                 " defaulting to %s.", value,
4881                                                 use_spacer ? "true" : "false",
4882                                                 use_spacer ? "right" : "none");
4883                                         if (use_spacer) {
4884                                                 use_spacer = RIGHT_SPACER;
4885                                         } else {
4886                                                 use_spacer = NO_SPACER;
4887                                         }
4888                                 }
4889                         } else {
4890                                 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4891                                 use_spacer = RIGHT_SPACER;
4892                         }
4893                 }
4894 #ifdef X11
4895 #ifdef XFT
4896                 CONF("use_xft") {
4897                         use_xft = string_to_bool(value);
4898                 }
4899                 CONF("font") {
4900                         if (value) {
4901                                 set_first_font(value);
4902                         }
4903                 }
4904                 CONF("xftalpha") {
4905                         if (value && font_count >= 0) {
4906                                 fonts[0].font_alpha = atof(value) * 65535.0;
4907                         }
4908                 }
4909                 CONF("xftfont") {
4910                         if (use_xft) {
4911 #else
4912                 CONF("use_xft") {
4913                         if (string_to_bool(value)) {
4914                                 NORM_ERR("Xft not enabled at compile time");
4915                         }
4916                 }
4917                 CONF("xftfont") {
4918                         /* xftfont silently ignored when no Xft */
4919                 }
4920                 CONF("xftalpha") {
4921                         /* xftalpha is silently ignored when no Xft */
4922                 }
4923                 CONF("font") {
4924 #endif
4925                         if (value) {
4926                                 set_first_font(value);
4927                         }
4928 #ifdef XFT
4929                         }
4930 #endif
4931                 }
4932                 CONF("gap_x") {
4933                         if (value) {
4934                                 gap_x = atoi(value);
4935                         } else {
4936                                 CONF_ERR;
4937                         }
4938                 }
4939                 CONF("gap_y") {
4940                         if (value) {
4941                                 gap_y = atoi(value);
4942                         } else {
4943                                 CONF_ERR;
4944                         }
4945                 }
4946 #endif /* X11 */
4947                 CONF("mail_spool") {
4948                         if (value) {
4949                                 char buffer[256];
4950
4951                                 variable_substitute(value, buffer, 256);
4952
4953                                 if (buffer[0] != '\0') {
4954                                         if (current_mail_spool) {
4955                                                 free(current_mail_spool);
4956                                         }
4957                                         current_mail_spool = strndup(buffer, text_buffer_size);
4958                                 }
4959                         } else {
4960                                 CONF_ERR;
4961                         }
4962                 }
4963 #ifdef X11
4964                 CONF("minimum_size") {
4965                         if (value) {
4966                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4967                                                 != 2) {
4968                                         if (sscanf(value, "%d", &minimum_width) != 1) {
4969                                                 CONF_ERR;
4970                                         }
4971                                 }
4972                         } else {
4973                                 CONF_ERR;
4974                         }
4975                 }
4976                 CONF("maximum_width") {
4977                         if (value) {
4978                                 if (sscanf(value, "%d", &maximum_width) != 1) {
4979                                         CONF_ERR;
4980                                 }
4981                         } else {
4982                                 CONF_ERR;
4983                         }
4984                 }
4985 #endif /* X11 */
4986                 CONF("no_buffers") {
4987                         no_buffers = string_to_bool(value);
4988                 }
4989                 CONF("top_name_width") {
4990                         if (set_top_name_width(value))
4991                                 CONF_ERR;
4992                 }
4993                 CONF("top_cpu_separate") {
4994                         cpu_separate = string_to_bool(value);
4995                 }
4996                 CONF("short_units") {
4997                         short_units = string_to_bool(value);
4998                 }
4999                 CONF("format_human_readable") {
5000                         format_human_readable = string_to_bool(value);
5001                 }
5002 #ifdef HDDTEMP
5003                 CONF("hddtemp_host") {
5004                         set_hddtemp_host(value);
5005                 }
5006                 CONF("hddtemp_port") {
5007                         set_hddtemp_port(value);
5008                 }
5009 #endif /* HDDTEMP */
5010                 CONF("pad_percents") {
5011                         pad_percents = atoi(value);
5012                 }
5013 #ifdef X11
5014 #ifdef OWN_WINDOW
5015                 CONF("own_window") {
5016                         if (value) {
5017                                 own_window = string_to_bool(value);
5018                         }
5019                 }
5020                 CONF("own_window_class") {
5021                         if (value) {
5022                                 memset(window.class_name, 0, sizeof(window.class_name));
5023                                 strncpy(window.class_name, value,
5024                                                 sizeof(window.class_name) - 1);
5025                         }
5026                 }
5027                 CONF("own_window_title") {
5028                         if (value) {
5029                                 memset(window.title, 0, sizeof(window.title));
5030                                 strncpy(window.title, value, sizeof(window.title) - 1);
5031                         }
5032                 }
5033                 CONF("own_window_transparent") {
5034                         if (value) {
5035                                 set_transparent = string_to_bool(value);
5036                         }
5037                 }
5038                 CONF("own_window_hints") {
5039                         if (value) {
5040                                 char *p_hint, *p_save;
5041                                 char delim[] = ", ";
5042
5043                                 /* tokenize the value into individual hints */
5044                                 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5045                                         do {
5046                                                 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5047                                                 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5048                                                         SET_HINT(window.hints, HINT_UNDECORATED);
5049                                                 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5050                                                         SET_HINT(window.hints, HINT_BELOW);
5051                                                 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5052                                                         SET_HINT(window.hints, HINT_ABOVE);
5053                                                 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5054                                                         SET_HINT(window.hints, HINT_STICKY);
5055                                                 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5056                                                         SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5057                                                 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5058                                                         SET_HINT(window.hints, HINT_SKIP_PAGER);
5059                                                 } else {
5060                                                         CONF_ERR;
5061                                                 }
5062
5063                                                 p_hint = strtok_r(NULL, delim, &p_save);
5064                                         } while (p_hint != NULL);
5065                                 }
5066                         } else {
5067                                 CONF_ERR;
5068                         }
5069                 }
5070                 CONF("own_window_type") {
5071                         if (value) {
5072                                 if (strncmp(value, "normal", 6) == EQUAL) {
5073                                         window.type = TYPE_NORMAL;
5074                                 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5075                                         window.type = TYPE_DESKTOP;
5076                                 } else if (strncmp(value, "dock", 4) == EQUAL) {
5077                                         window.type = TYPE_DOCK;
5078                                         text_alignment = TOP_LEFT;
5079                                 } else if (strncmp(value, "panel", 5) == EQUAL) {
5080                                         window.type = TYPE_PANEL;
5081                                 } else if (strncmp(value, "override", 8) == EQUAL) {
5082                                         window.type = TYPE_OVERRIDE;
5083                                 } else {
5084                                         CONF_ERR;
5085                                 }
5086                         } else {
5087                                 CONF_ERR;
5088                         }
5089                 }
5090 #ifdef USE_ARGB
5091                 CONF("own_window_argb_visual") {
5092                         use_argb_visual = string_to_bool(value);
5093                 }
5094                 CONF("own_window_argb_value") {
5095                         own_window_argb_value = strtol(value, 0, 0);
5096                         if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5097                                 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5098                         }
5099                 }
5100 #endif /* USE_ARGB */
5101 #endif
5102                 CONF("stippled_borders") {
5103                         if (value) {
5104                                 stippled_borders = strtol(value, 0, 0);
5105                         } else {
5106                                 stippled_borders = 4;
5107                         }
5108                 }
5109 #ifdef IMLIB2
5110                 CONF("imlib_cache_size") {
5111                         if (value) {
5112                                 cimlib_set_cache_size(atoi(value));
5113                         }
5114                 }
5115                 CONF("imlib_cache_flush_interval") {
5116                         if (value) {
5117                                 cimlib_set_cache_flush_interval(atoi(value));
5118                         }
5119                 }
5120 #endif /* IMLIB2 */
5121 #endif /* X11 */
5122                 CONF("update_interval_on_battery") {
5123                         if (value) {
5124                                 update_interval_bat = strtod(value, 0);
5125                         } else {
5126                                 CONF_ERR;
5127                         }
5128                 }
5129                 CONF("update_interval") {
5130                         if (value) {
5131                                 set_update_interval(strtod(value, 0));
5132                         } else {
5133                                 CONF_ERR;
5134                         }
5135                         if (info.music_player_interval == 0) {
5136                                 // default to update_interval
5137                                 info.music_player_interval = update_interval;
5138                         }
5139                 }
5140                 CONF("total_run_times") {
5141                         if (value) {
5142                                 total_run_times = strtod(value, 0);
5143                         } else {
5144                                 CONF_ERR;
5145                         }
5146                 }
5147                 CONF("uppercase") {
5148                         stuff_in_uppercase = string_to_bool(value);
5149                 }
5150                 CONF("max_specials") {
5151                         if (value) {
5152                                 max_specials = atoi(value);
5153                         } else {
5154                                 CONF_ERR;
5155                         }
5156                 }
5157                 CONF("max_user_text") {
5158                         if (value) {
5159                                 max_user_text = atoi(value);
5160                         } else {
5161                                 CONF_ERR;
5162                         }
5163                 }
5164                 CONF("text_buffer_size") {
5165                         if (value) {
5166                                 text_buffer_size = atoi(value);
5167                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5168                                         NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5169                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5170                                 }
5171                         } else {
5172                                 CONF_ERR;
5173                         }
5174                 }
5175                 CONF("text") {
5176 #ifdef X11
5177                         if (output_methods & TO_X) {
5178                                 X11_initialisation();
5179                         }
5180 #endif
5181
5182                         if (global_text) {
5183                                 free(global_text);
5184                                 global_text = 0;
5185                         }
5186
5187                         global_text = (char *) malloc(1);
5188                         global_text[0] = '\0';
5189
5190                         while (!feof(fp)) {
5191                                 unsigned int l = strlen(global_text);
5192                                 unsigned int bl;
5193                                 char buf[CONF_BUFF_SIZE];
5194
5195                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5196                                         break;
5197                                 }
5198
5199                                 /* Remove \\-\n. */
5200                                 bl = strlen(buf);
5201                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5202                                         buf[bl-2] = '\0';
5203                                         bl -= 2;
5204                                         if (bl == 0) {
5205                                                 continue;
5206                                         }
5207                                 }
5208
5209                                 /* Check for continuation of \\-\n. */
5210                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5211                                         global_text[l-1] = '\0';
5212                                         continue;
5213                                 }
5214
5215                                 global_text = (char *) realloc(global_text, l + bl + 1);
5216                                 strcat(global_text, buf);
5217
5218                                 if (strlen(global_text) > max_user_text) {
5219                                         break;
5220                                 }
5221                         }
5222                         global_text_lines = line + 1;
5223                         break;
5224                 }
5225 #ifdef TCP_PORT_MONITOR
5226                 CONF("max_port_monitor_connections") {
5227                         int max;
5228                         if (!value || (sscanf(value, "%d", &max) != 1)) {
5229                                 /* an error. use default, warn and continue. */
5230                                 tcp_portmon_set_max_connections(0);
5231                                 CONF_ERR;
5232                         } else if (tcp_portmon_set_max_connections(max)) {
5233                                 /* max is < 0, default has been set*/
5234                                 CONF_ERR;
5235                         }
5236                 }
5237 #endif
5238                 CONF("if_up_strictness") {
5239                         if (!value) {
5240                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5241                                 ifup_strictness = IFUP_UP;
5242                         } else if (strcasecmp(value, "up") == EQUAL) {
5243                                 ifup_strictness = IFUP_UP;
5244                         } else if (strcasecmp(value, "link") == EQUAL) {
5245                                 ifup_strictness = IFUP_LINK;
5246                         } else if (strcasecmp(value, "address") == EQUAL) {
5247                                 ifup_strictness = IFUP_ADDR;
5248                         } else {
5249                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5250                                 ifup_strictness = IFUP_UP;
5251                         }
5252                 }
5253
5254                 CONF("temperature_unit") {
5255                         if (!value) {
5256                                 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5257                         } else if (set_temp_output_unit(value)) {
5258                                 NORM_ERR("temperature_unit: incorrect argument");
5259                         }
5260                 }
5261
5262 #ifdef HAVE_LUA
5263                 CONF("lua_load") {
5264                         if (value) {
5265                                 char *ptr = strtok(value, " ");
5266                                 while (ptr) {
5267                                         llua_load(ptr);
5268                                         ptr = strtok(NULL, " ");
5269                                 }
5270                         } else {
5271                                 CONF_ERR;
5272                         }
5273                 }
5274 #ifdef X11
5275                 CONF("lua_draw_hook_pre") {
5276                         if (value) {
5277                                 llua_set_draw_pre_hook(value);
5278                         } else {
5279                                 CONF_ERR;
5280                         }
5281                 }
5282                 CONF("lua_draw_hook_post") {
5283                         if (value) {
5284                                 llua_set_draw_post_hook(value);
5285                         } else {
5286                                 CONF_ERR;
5287                         }
5288                 }
5289                 CONF("lua_startup_hook") {
5290                         if (value) {
5291                                 llua_set_startup_hook(value);
5292                         } else {
5293                                 CONF_ERR;
5294                         }
5295                 }
5296                 CONF("lua_shutdown_hook") {
5297                         if (value) {
5298                                 llua_set_shutdown_hook(value);
5299                         } else {
5300                                 CONF_ERR;
5301                         }
5302                 }
5303 #endif /* X11 */
5304 #endif /* HAVE_LUA */
5305
5306                 CONF("color0"){}
5307                 CONF("color1"){}
5308                 CONF("color2"){}
5309                 CONF("color3"){}
5310                 CONF("color4"){}
5311                 CONF("color5"){}
5312                 CONF("color6"){}
5313                 CONF("color7"){}
5314                 CONF("color8"){}
5315                 CONF("color9"){}
5316                 CONF("default_color"){}
5317                 CONF3("default_shade_color", "default_shadecolor"){}
5318                 CONF3("default_outline_color", "default_outlinecolor") {}
5319                 CONF("own_window_colour") {}
5320
5321                 else {
5322                         NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5323                 }
5324         }
5325
5326         fclose(fp);
5327
5328         if (info.music_player_interval == 0) {
5329                 // default to update_interval
5330                 info.music_player_interval = update_interval;
5331         }
5332         if (!global_text) { // didn't supply any text
5333                 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5334         }
5335         if (!output_methods) {
5336                 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5337         }
5338 #if defined(NCURSES)
5339 #if defined(X11)
5340         if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5341                 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5342                 output_methods &= ~TO_NCURSES;
5343                 endwin();
5344         }
5345 #endif /* X11 */
5346         if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5347                 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5348                 output_methods &= ~(TO_STDOUT | TO_STDERR);
5349         }
5350 #endif /* NCURSES */
5351         return TRUE;
5352 }
5353
5354 #ifdef X11
5355 static void load_config_file_x11(const char *f)
5356 {
5357         int line = 0;
5358         FILE *fp;
5359
5360         fp = open_config_file(f);
5361         if (!fp) {
5362                 return;
5363         }
5364         DBGP("reading contents from config file '%s'", f);
5365
5366         while (!feof(fp)) {
5367                 char buff[CONF_BUFF_SIZE], *name, *value;
5368                 int ret = do_config_step(&line, fp, buff, &name, &value);
5369                 if (ret == CONF_BREAK) {
5370                         break;
5371                 } else if (ret == CONF_CONTINUE) {
5372                         continue;
5373                 }
5374
5375                 CONF2("color0") {
5376                         X11_initialisation();
5377                         if (x_initialised == YES) {
5378                                 if (value) {
5379                                         color0 = get_x11_color(value);
5380                                 } else {
5381                                         CONF_ERR;
5382                                 }
5383                         }
5384                 }
5385                 CONF("color1") {
5386                         X11_initialisation();
5387                         if (x_initialised == YES) {
5388                                 if (value) {
5389                                         color1 = get_x11_color(value);
5390                                 } else {
5391                                         CONF_ERR;
5392                                 }
5393                         }
5394                 }
5395                 CONF("color2") {
5396                         X11_initialisation();
5397                         if (x_initialised == YES) {
5398                                 if (value) {
5399                                         color2 = get_x11_color(value);
5400                                 } else {
5401                                         CONF_ERR;
5402                                 }
5403                         }
5404                 }
5405                 CONF("color3") {
5406                         X11_initialisation();
5407                         if (x_initialised == YES) {
5408                                 if (value) {
5409                                         color3 = get_x11_color(value);
5410                                 } else {
5411                                         CONF_ERR;
5412                                 }
5413                         }
5414                 }
5415                 CONF("color4") {
5416                         X11_initialisation();
5417                         if (x_initialised == YES) {
5418                                 if (value) {
5419                                         color4 = get_x11_color(value);
5420                                 } else {
5421                                         CONF_ERR;
5422                                 }
5423                         }
5424                 }
5425                 CONF("color5") {
5426                         X11_initialisation();
5427                         if (x_initialised == YES) {
5428                                 if (value) {
5429                                         color5 = get_x11_color(value);
5430                                 } else {
5431                                         CONF_ERR;
5432                                 }
5433                         }
5434                 }
5435                 CONF("color6") {
5436                         X11_initialisation();
5437                         if (x_initialised == YES) {
5438                                 if (value) {
5439                                         color6 = get_x11_color(value);
5440                                 } else {
5441                                         CONF_ERR;
5442                                 }
5443                         }
5444                 }
5445                 CONF("color7") {
5446                         X11_initialisation();
5447                         if (x_initialised == YES) {
5448                                 if (value) {
5449                                         color7 = get_x11_color(value);
5450                                 } else {
5451                                         CONF_ERR;
5452                                 }
5453                         }
5454                 }
5455                 CONF("color8") {
5456                         X11_initialisation();
5457                         if (x_initialised == YES) {
5458                                 if (value) {
5459                                         color8 = get_x11_color(value);
5460                                 } else {
5461                                         CONF_ERR;
5462                                 }
5463                         }
5464                 }
5465                 CONF("color9") {
5466                         X11_initialisation();
5467                         if (x_initialised == YES) {
5468                                 if (value) {
5469                                         color9 = get_x11_color(value);
5470                                 } else {
5471                                         CONF_ERR;
5472                                 }
5473                         }
5474                 }
5475                 CONF("default_color") {
5476                         X11_initialisation();
5477                         if (x_initialised == YES) {
5478                                 if (value) {
5479                                         default_fg_color = get_x11_color(value);
5480                                 } else {
5481                                         CONF_ERR;
5482                                 }
5483                         }
5484                 }
5485                 CONF3("default_shade_color", "default_shadecolor") {
5486                         X11_initialisation();
5487                         if (x_initialised == YES) {
5488                                 if (value) {
5489                                         default_bg_color = get_x11_color(value);
5490                                 } else {
5491                                         CONF_ERR;
5492                                 }
5493                         }
5494                 }
5495                 CONF3("default_outline_color", "default_outlinecolor") {
5496                         X11_initialisation();
5497                         if (x_initialised == YES) {
5498                                 if (value) {
5499                                         default_out_color = get_x11_color(value);
5500                                 } else {
5501                                         CONF_ERR;
5502                                 }
5503                         }
5504                 }
5505 #ifdef OWN_WINDOW
5506                 CONF("own_window_colour") {
5507                         X11_initialisation();
5508                         if (x_initialised == YES) {
5509                                 if (value) {
5510                                         background_colour = get_x11_color(value);
5511                                 } else {
5512                                         NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5513                                                 "'#' for hex colours");
5514                                 }
5515                         }
5516                 }
5517 #endif
5518                 CONF("text") {
5519                         /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5520                         if(output_methods & TO_X) {
5521                                 X11_initialisation();
5522                         }
5523                 }
5524 #undef CONF
5525 #undef CONF2
5526 #undef CONF3
5527 #undef CONF_ERR
5528 #undef CONF_ERR2
5529 #undef CONF_BREAK
5530 #undef CONF_CONTINUE
5531 #undef CONF_BUFF_SIZE
5532         }
5533
5534         fclose(fp);
5535
5536 }
5537 #endif /* X11 */
5538
5539 static void print_help(const char *prog_name) {
5540         printf("Usage: %s [OPTION]...\n"
5541                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5542                         "window. Command line options will override configurations defined in config\n"
5543                         "file.\n"
5544                         "   -v, --version             version\n"
5545                         "   -q, --quiet               quiet mode\n"
5546                         "   -D, --debug               increase debugging output, ie. -DD for more debugging\n"
5547                         "   -c, --config=FILE         config file to load\n"
5548 #ifdef CONFIG_OUTPUT
5549                         "   -C, --print-config        print the builtin default config to stdout\n"
5550                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5551 #endif
5552                         "   -d, --daemonize           daemonize, fork to background\n"
5553                         "   -h, --help                help\n"
5554 #ifdef X11
5555                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5556                         "   -f, --font=FONT           font to use\n"
5557                         "   -X, --display=DISPLAY     X11 display to use\n"
5558 #ifdef OWN_WINDOW
5559                         "   -o, --own-window          create own window to draw\n"
5560 #endif
5561 #ifdef HAVE_XDBE
5562                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
5563 #endif
5564                         "   -w, --window-id=WIN_ID    window id to draw\n"
5565                         "   -x X                      x position\n"
5566                         "   -y Y                      y position\n"
5567 #endif /* X11 */
5568                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
5569                         "   -u, --interval=SECS       update interval\n"
5570                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n"
5571                         "   -p, --pause=SECS          pause for SECS seconds at startup before doing anything\n",
5572                         prog_name
5573         );
5574 }
5575
5576 /* : means that character before that takes an argument */
5577 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5578 #ifdef X11
5579         "x:y:w:a:f:X:"
5580 #ifdef OWN_WINDOW
5581         "o"
5582 #endif
5583 #ifdef HAVE_XDBE
5584         "b"
5585 #endif
5586 #endif /* X11 */
5587 #ifdef CONFIG_OUTPUT
5588         "C"
5589 #endif
5590         ;
5591
5592 static const struct option longopts[] = {
5593         { "help", 0, NULL, 'h' },
5594         { "version", 0, NULL, 'V' },
5595         { "quiet", 0, NULL, 'q' },
5596         { "debug", 0, NULL, 'D' },
5597         { "config", 1, NULL, 'c' },
5598 #ifdef CONFIG_OUTPUT
5599         { "print-config", 0, NULL, 'C' },
5600 #endif
5601         { "daemonize", 0, NULL, 'd' },
5602 #ifdef X11
5603         { "alignment", 1, NULL, 'a' },
5604         { "font", 1, NULL, 'f' },
5605         { "display", 1, NULL, 'X' },
5606 #ifdef OWN_WINDOW
5607         { "own-window", 0, NULL, 'o' },
5608 #endif
5609 #ifdef HAVE_XDBE
5610         { "double-buffer", 0, NULL, 'b' },
5611 #endif
5612         { "window-id", 1, NULL, 'w' },
5613 #endif /* X11 */
5614         { "text", 1, NULL, 't' },
5615         { "interval", 1, NULL, 'u' },
5616         { "pause", 1, NULL, 'p' },
5617         { 0, 0, 0, 0 }
5618 };
5619
5620 void set_current_config(void);
5621 void set_current_config(void)
5622 {
5623         /* check if specified config file is valid */
5624         if (current_config) {
5625                 struct stat sb;
5626                 if (stat(current_config, &sb) ||
5627                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5628                         NORM_ERR("invalid configuration file '%s'\n", current_config);
5629                         free(current_config);
5630                         current_config = 0;
5631                 }
5632         }
5633
5634         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5635
5636         if (!current_config) {
5637                 /* load default config file */
5638                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5639                 FILE *fp;
5640
5641                 /* Try to use personal config file first */
5642                 to_real_path(buf, CONFIG_FILE);
5643                 if (buf[0] && (fp = fopen(buf, "r"))) {
5644                         current_config = strndup(buf, max_user_text);
5645                         fclose(fp);
5646                 }
5647
5648                 /* Try to use system config file if personal config not readable */
5649                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5650                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5651                         fclose(fp);
5652                 }
5653
5654                 /* No readable config found */
5655                 if (!current_config) {
5656 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5657 #ifdef BUILD_BUILTIN_CONFIG
5658                         current_config = strdup("==builtin==");
5659                         NORM_ERR(NOCFGFILEFOUND
5660                                         ", using builtin default");
5661 #else
5662                         CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5663 #endif /* ! CONF_OUTPUT */
5664                 }
5665         }
5666 }
5667
5668 void initialisation(int argc, char **argv) {
5669         struct sigaction act, oact;
5670
5671         set_default_configurations();
5672         set_current_config();
5673         load_config_file(current_config);
5674         currentconffile = conftree_add(currentconffile, current_config);
5675
5676         /* init specials array */
5677         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5678                 NORM_ERR("failed to create specials array");
5679         }
5680
5681 #ifdef MAIL_FILE
5682         if (current_mail_spool == NULL) {
5683                 char buf[256];
5684
5685                 variable_substitute(MAIL_FILE, buf, 256);
5686
5687                 if (buf[0] != '\0') {
5688                         current_mail_spool = strndup(buf, text_buffer_size);
5689                 }
5690         }
5691 #endif
5692
5693         /* handle other command line arguments */
5694
5695 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5696                 || defined(__NetBSD__)
5697         optind = optreset = 1;
5698 #else
5699         optind = 0;
5700 #endif
5701
5702 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5703         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5704                         "kvm_open")) == NULL) {
5705                 CRIT_ERR(NULL, NULL, "cannot read kvm");
5706         }
5707 #endif
5708
5709         while (1) {
5710                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5711                 int startup_pause;
5712
5713                 if (c == -1) {
5714                         break;
5715                 }
5716
5717                 switch (c) {
5718                         case 'd':
5719                                 fork_to_background = 1;
5720                                 break;
5721                         case 'D':
5722                                 global_debug_level++;
5723                                 break;
5724 #ifdef X11
5725                         case 'f':
5726                                 set_first_font(optarg);
5727                                 break;
5728                         case 'a':
5729                                 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
5730                                 break;
5731
5732 #ifdef OWN_WINDOW
5733                         case 'o':
5734                                 own_window = 1;
5735                                 break;
5736 #endif
5737 #ifdef HAVE_XDBE
5738                         case 'b':
5739                                 use_xdbe = 1;
5740                                 break;
5741 #endif
5742 #endif /* X11 */
5743                         case 't':
5744                                 if (global_text) {
5745                                         free(global_text);
5746                                         global_text = 0;
5747                                 }
5748                                 global_text = strndup(optarg, max_user_text);
5749                                 convert_escapes(global_text);
5750                                 break;
5751
5752                         case 'u':
5753                                 update_interval = strtod(optarg, 0);
5754                                 update_interval_old = update_interval;
5755                                 if (info.music_player_interval == 0) {
5756                                         // default to update_interval
5757                                         info.music_player_interval = update_interval;
5758                                 }
5759                                 break;
5760
5761                         case 'i':
5762                                 total_run_times = strtod(optarg, 0);
5763                                 break;
5764 #ifdef X11
5765                         case 'x':
5766                                 gap_x = atoi(optarg);
5767                                 break;
5768
5769                         case 'y':
5770                                 gap_y = atoi(optarg);
5771                                 break;
5772 #endif /* X11 */
5773                         case 'p':
5774                                 if (first_pass) {
5775                                         startup_pause = atoi(optarg);
5776                                         sleep(startup_pause);
5777                                 }
5778                                 break;
5779
5780                         case '?':
5781                                 exit(EXIT_FAILURE);
5782                 }
5783         }
5784
5785 #ifdef X11
5786         /* load font */
5787         if (output_methods & TO_X) {
5788                 load_config_file_x11(current_config);
5789         }
5790 #endif /* X11 */
5791
5792         /* generate text and get initial size */
5793         extract_variable_text(global_text);
5794         if (global_text) {
5795                 free(global_text);
5796                 global_text = 0;
5797         }
5798         global_text = NULL;
5799         /* fork */
5800         if (fork_to_background && first_pass) {
5801                 int pid = fork();
5802
5803                 switch (pid) {
5804                         case -1:
5805                                 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5806                                         strerror(errno));
5807                                 break;
5808
5809                         case 0:
5810                                 /* child process */
5811                                 usleep(25000);
5812                                 fprintf(stderr, "\n");
5813                                 fflush(stderr);
5814                                 break;
5815
5816                         default:
5817                                 /* parent process */
5818                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5819                                         pid);
5820                                 fflush(stderr);
5821                                 exit(EXIT_SUCCESS);
5822                 }
5823         }
5824
5825         start_update_threading();
5826
5827         text_buffer = malloc(max_user_text);
5828         memset(text_buffer, 0, max_user_text);
5829         tmpstring1 = malloc(text_buffer_size);
5830         memset(tmpstring1, 0, text_buffer_size);
5831         tmpstring2 = malloc(text_buffer_size);
5832         memset(tmpstring2, 0, text_buffer_size);
5833
5834 #ifdef X11
5835         xargc = argc;
5836         xargv = argv;
5837         X11_create_window();
5838 #endif /* X11 */
5839 #ifdef HAVE_LUA
5840         llua_setup_info(&info, update_interval);
5841 #endif /* HAVE_LUA */
5842 #ifdef XOAP
5843         xmlInitParser();
5844 #endif /* XOAP */
5845
5846         /* Set signal handlers */
5847         act.sa_handler = signal_handler;
5848         sigemptyset(&act.sa_mask);
5849         act.sa_flags = 0;
5850 #ifdef SA_RESTART
5851         act.sa_flags |= SA_RESTART;
5852 #endif
5853
5854         if (            sigaction(SIGINT,  &act, &oact) < 0
5855                         ||      sigaction(SIGALRM, &act, &oact) < 0
5856                         ||      sigaction(SIGUSR1, &act, &oact) < 0
5857                         ||      sigaction(SIGHUP,  &act, &oact) < 0
5858                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
5859                 NORM_ERR("error setting signal handler: %s", strerror(errno));
5860         }
5861
5862 #ifdef HAVE_LUA
5863         llua_startup_hook();
5864 #endif /* HAVE_LUA */
5865 }
5866
5867 int main(int argc, char **argv)
5868 {
5869 #ifdef X11
5870         char *s, *temp;
5871         unsigned int x;
5872 #endif
5873
5874         argc_copy = argc;
5875         argv_copy = argv;
5876         g_signal_pending = 0;
5877         max_user_text = MAX_USER_TEXT_DEFAULT;
5878         current_config = 0;
5879         memset(&info, 0, sizeof(info));
5880         free_templates();
5881         clear_net_stats();
5882
5883 #ifdef TCP_PORT_MONITOR
5884         /* set default connection limit */
5885         tcp_portmon_set_max_connections(0);
5886 #endif
5887
5888         /* handle command line parameters that don't change configs */
5889 #ifdef X11
5890         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5891                         || ((s = getenv("LANG")) && *s)) {
5892                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5893                 if (temp == NULL) {
5894                         NORM_ERR("malloc failed");
5895                 }
5896                 for (x = 0; x < strlen(s); x++) {
5897                         temp[x] = tolower(s[x]);
5898                 }
5899                 temp[x] = 0;
5900                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5901                         utf8_mode = 1;
5902                 }
5903
5904                 free(temp);
5905         }
5906         if (!setlocale(LC_CTYPE, "")) {
5907                 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5908         }
5909 #endif /* X11 */
5910         while (1) {
5911                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5912
5913                 if (c == -1) {
5914                         break;
5915                 }
5916
5917                 switch (c) {
5918                         case 'v':
5919                         case 'V':
5920                                 print_version();
5921                         case 'c':
5922                                 if (current_config) {
5923                                         free(current_config);
5924                                 }
5925                                 current_config = strndup(optarg, max_user_text);
5926                                 break;
5927                         case 'q':
5928                                 if (!freopen("/dev/null", "w", stderr)) {
5929                                         NORM_ERR("unable to redirect stderr to /dev/null");
5930                                 }
5931                                 break;
5932                         case 'h':
5933                                 print_help(argv[0]);
5934                                 return 0;
5935 #ifdef CONFIG_OUTPUT
5936                         case 'C':
5937                                 print_defconfig();
5938                                 return 0;
5939 #endif
5940 #ifdef X11
5941                         case 'w':
5942                                 window.window = strtol(optarg, 0, 0);
5943                                 break;
5944                         case 'X':
5945                                 if (disp)
5946                                         free(disp);
5947                                 disp = strdup(optarg);
5948                                 break;
5949 #endif /* X11 */
5950
5951                         case '?':
5952                                 exit(EXIT_FAILURE);
5953                 }
5954         }
5955
5956         set_current_config();
5957
5958 #ifdef XOAP
5959         /* Load xoap keys, if existing */
5960         load_xoap_keys();
5961 #endif /* XOAP */
5962
5963 #ifdef HAVE_SYS_INOTIFY_H
5964         inotify_fd = inotify_init();
5965         if(inotify_fd != -1) {
5966                 int fl;
5967
5968                 fl = fcntl(inotify_fd, F_GETFL);
5969                 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
5970         }
5971 #endif /* HAVE_SYS_INOTIFY_H */
5972
5973         initialisation(argc, argv);
5974
5975         first_pass = 0; /* don't ever call fork() again */
5976
5977         main_loop();
5978
5979 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5980         kvm_close(kd);
5981 #endif
5982
5983         return 0;
5984
5985 }
5986
5987 void alarm_handler(void) {
5988         if(childpid > 0) {
5989                 kill(childpid, SIGTERM);
5990         }
5991 }
5992
5993 static void signal_handler(int sig)
5994 {
5995         /* signal handler is light as a feather, as it should be.
5996          * we will poll g_signal_pending with each loop of conky
5997          * and do any signal processing there, NOT here (except 
5998          * SIGALRM because this is caused when conky is hanging) */
5999         if(sig == SIGALRM) {
6000                 alarm_handler();
6001         } else {
6002                 g_signal_pending = sig;
6003         }
6004 }