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