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