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