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