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