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