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