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