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