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