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