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