Add support for $pid_parent
[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(processes) {
1698                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1699                         }
1700                         OBJ(running_processes) {
1701                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1702                         }
1703                         OBJ(threads) {
1704                                 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1705                         }
1706                         OBJ(text) {
1707                                 snprintf(p, p_max_size, "%s", obj->data.s);
1708                         }
1709 #ifdef X11
1710                         OBJ(shadecolor) {
1711                                 new_bg(p, obj->data.l);
1712                         }
1713                         OBJ(stippled_hr) {
1714                                 new_stippled_hr(obj, p);
1715                         }
1716 #endif /* X11 */
1717                         OBJ(swap) {
1718                                 human_readable(cur->swap * 1024, p, 255);
1719                         }
1720                         OBJ(swapfree) {
1721                                 human_readable(cur->swapfree * 1024, p, 255);
1722                         }
1723                         OBJ(swapmax) {
1724                                 human_readable(cur->swapmax * 1024, p, 255);
1725                         }
1726                         OBJ(swapperc) {
1727                                 if (cur->swapmax == 0) {
1728                                         strncpy(p, "No swap", p_max_size);
1729                                 } else {
1730                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1731                                 }
1732                         }
1733                         OBJ(swapbar) {
1734 #ifdef X11
1735                                 if(output_methods & TO_X) {
1736                                         new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1737                                 }else
1738 #endif /* X11 */
1739                                         new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1740                         }
1741                         OBJ(sysname) {
1742                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1743                         }
1744                         OBJ(time) {
1745                                 print_time(obj, p, p_max_size);
1746                         }
1747                         OBJ(utime) {
1748                                 print_utime(obj, p, p_max_size);
1749                         }
1750                         OBJ(tztime) {
1751                                 print_tztime(obj, p, p_max_size);
1752                         }
1753                         OBJ(totaldown) {
1754                                 print_totaldown(obj, p, p_max_size);
1755                         }
1756                         OBJ(totalup) {
1757                                 print_totalup(obj, p, p_max_size);
1758                         }
1759                         OBJ(updates) {
1760                                 snprintf(p, p_max_size, "%d", total_updates);
1761                         }
1762                         OBJ(if_updatenr) {
1763                                 if(total_updates % updatereset != obj->data.i - 1) {
1764                                         DO_JUMP;
1765                                 }
1766                         }
1767                         OBJ(upspeed) {
1768                                 print_upspeed(obj, p, p_max_size);
1769                         }
1770                         OBJ(upspeedf) {
1771                                 print_upspeedf(obj, p, p_max_size);
1772                         }
1773 #ifdef X11
1774                         OBJ(upspeedgraph) {
1775                                 print_upspeedgraph(obj, p);
1776                         }
1777 #endif /* X11 */
1778                         OBJ(uptime_short) {
1779                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
1780                         }
1781                         OBJ(uptime) {
1782                                 format_seconds(p, p_max_size, (int) cur->uptime);
1783                         }
1784 #ifdef __linux__
1785                         OBJ(user_names) {
1786                                 snprintf(p, p_max_size, "%s", cur->users.names);
1787                         }
1788                         OBJ(user_terms) {
1789                                 snprintf(p, p_max_size, "%s", cur->users.terms);
1790                         }
1791                         OBJ(user_times) {
1792                                 snprintf(p, p_max_size, "%s", cur->users.times);
1793                         }
1794                         OBJ(user_time) {
1795                                 update_user_time(obj->data.s);
1796                                 snprintf(p, p_max_size, "%s", cur->users.ctime);
1797                         }
1798                         OBJ(user_number) {
1799                                 snprintf(p, p_max_size, "%d", cur->users.number);
1800                         }
1801 #endif /* __linux__ */
1802 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1803                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1804                         OBJ(apm_adapter) {
1805                                 char *msg;
1806
1807                                 msg = get_apm_adapter();
1808                                 snprintf(p, p_max_size, "%s", msg);
1809                                 free(msg);
1810                         }
1811                         OBJ(apm_battery_life) {
1812                                 char *msg;
1813
1814                                 msg = get_apm_battery_life();
1815                                 snprintf(p, p_max_size, "%s", msg);
1816                                 free(msg);
1817                         }
1818                         OBJ(apm_battery_time) {
1819                                 char *msg;
1820
1821                                 msg = get_apm_battery_time();
1822                                 snprintf(p, p_max_size, "%s", msg);
1823                                 free(msg);
1824                         }
1825 #endif /* __FreeBSD__ __OpenBSD__ */
1826
1827 #ifdef MPD
1828 #define mpd_printf(fmt, val) \
1829         snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1830 #define mpd_sprintf(val) { \
1831         if (!obj->data.i || obj->data.i > p_max_size) \
1832                 mpd_printf("%s", val); \
1833         else \
1834                 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
1835 }
1836                         OBJ(mpd_title)
1837                                 mpd_sprintf(title);
1838                         OBJ(mpd_artist)
1839                                 mpd_sprintf(artist);
1840                         OBJ(mpd_album)
1841                                 mpd_sprintf(album);
1842                         OBJ(mpd_random)
1843                                 mpd_printf("%s", random);
1844                         OBJ(mpd_repeat)
1845                                 mpd_printf("%s", repeat);
1846                         OBJ(mpd_track)
1847                                 mpd_sprintf(track);
1848                         OBJ(mpd_name)
1849                                 mpd_sprintf(name);
1850                         OBJ(mpd_file)
1851                                 mpd_sprintf(file);
1852                         OBJ(mpd_vol)
1853                                 mpd_printf("%d", volume);
1854                         OBJ(mpd_bitrate)
1855                                 mpd_printf("%d", bitrate);
1856                         OBJ(mpd_status)
1857                                 mpd_printf("%s", status);
1858                         OBJ(mpd_elapsed) {
1859                                 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
1860                         }
1861                         OBJ(mpd_length) {
1862                                 format_media_player_time(p, p_max_size, mpd_get_info()->length);
1863                         }
1864                         OBJ(mpd_percent) {
1865                                 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
1866                         }
1867                         OBJ(mpd_bar) {
1868 #ifdef X11
1869                                 if(output_methods & TO_X) {
1870                                         new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
1871                                 } else
1872 #endif /* X11 */
1873                                         new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
1874                         }
1875                         OBJ(mpd_smart) {
1876                                 struct mpd_s *mpd = mpd_get_info();
1877                                 int len = obj->data.i;
1878                                 if (len == 0 || len > p_max_size)
1879                                         len = p_max_size;
1880
1881                                 memset(p, 0, p_max_size);
1882                                 if (mpd->artist && *mpd->artist &&
1883                                     mpd->title && *mpd->title) {
1884                                         snprintf(p, len, "%s - %s", mpd->artist,
1885                                                 mpd->title);
1886                                 } else if (mpd->title && *mpd->title) {
1887                                         snprintf(p, len, "%s", mpd->title);
1888                                 } else if (mpd->artist && *mpd->artist) {
1889                                         snprintf(p, len, "%s", mpd->artist);
1890                                 } else if (mpd->file && *mpd->file) {
1891                                         snprintf(p, len, "%s", mpd->file);
1892                                 } else {
1893                                         *p = 0;
1894                                 }
1895                         }
1896                         OBJ(if_mpd_playing) {
1897                                 if (!mpd_get_info()->is_playing) {
1898                                         DO_JUMP;
1899                                 }
1900                         }
1901 #undef mpd_sprintf
1902 #undef mpd_printf
1903 #endif
1904
1905 #ifdef MOC
1906 #define MOC_PRINT(t, a) \
1907         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1908                         OBJ(moc_state) {
1909                                 MOC_PRINT(state, "??");
1910                         }
1911                         OBJ(moc_file) {
1912                                 MOC_PRINT(file, "no file");
1913                         }
1914                         OBJ(moc_title) {
1915                                 MOC_PRINT(title, "no title");
1916                         }
1917                         OBJ(moc_artist) {
1918                                 MOC_PRINT(artist, "no artist");
1919                         }
1920                         OBJ(moc_song) {
1921                                 MOC_PRINT(song, "no song");
1922                         }
1923                         OBJ(moc_album) {
1924                                 MOC_PRINT(album, "no album");
1925                         }
1926                         OBJ(moc_totaltime) {
1927                                 MOC_PRINT(totaltime, "0:00");
1928                         }
1929                         OBJ(moc_timeleft) {
1930                                 MOC_PRINT(timeleft, "0:00");
1931                         }
1932                         OBJ(moc_curtime) {
1933                                 MOC_PRINT(curtime, "0:00");
1934                         }
1935                         OBJ(moc_bitrate) {
1936                                 MOC_PRINT(bitrate, "0Kbps");
1937                         }
1938                         OBJ(moc_rate) {
1939                                 MOC_PRINT(rate, "0KHz");
1940                         }
1941 #undef MOC_PRINT
1942 #endif /* MOC */
1943 #ifdef XMMS2
1944                         OBJ(xmms2_artist) {
1945                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
1946                         }
1947                         OBJ(xmms2_album) {
1948                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
1949                         }
1950                         OBJ(xmms2_title) {
1951                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
1952                         }
1953                         OBJ(xmms2_genre) {
1954                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
1955                         }
1956                         OBJ(xmms2_comment) {
1957                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
1958                         }
1959                         OBJ(xmms2_url) {
1960                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1961                         }
1962                         OBJ(xmms2_status) {
1963                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
1964                         }
1965                         OBJ(xmms2_date) {
1966                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
1967                         }
1968                         OBJ(xmms2_tracknr) {
1969                                 if (cur->xmms2.tracknr != -1) {
1970                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
1971                                 }
1972                         }
1973                         OBJ(xmms2_bitrate) {
1974                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
1975                         }
1976                         OBJ(xmms2_id) {
1977                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
1978                         }
1979                         OBJ(xmms2_size) {
1980                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
1981                         }
1982                         OBJ(xmms2_elapsed) {
1983                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
1984                                         (cur->xmms2.elapsed / 1000) % 60);
1985                         }
1986                         OBJ(xmms2_duration) {
1987                                 snprintf(p, p_max_size, "%02d:%02d",
1988                                         cur->xmms2.duration / 60000,
1989                                         (cur->xmms2.duration / 1000) % 60);
1990                         }
1991                         OBJ(xmms2_percent) {
1992                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
1993                         }
1994 #ifdef X11
1995                         OBJ(xmms2_bar) {
1996                                 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
1997                         }
1998 #endif /* X11 */
1999                         OBJ(xmms2_playlist) {
2000                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2001                         }
2002                         OBJ(xmms2_timesplayed) {
2003                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2004                         }
2005                         OBJ(xmms2_smart) {
2006                                 if (strlen(cur->xmms2.title) < 2
2007                                                 && strlen(cur->xmms2.title) < 2) {
2008                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
2009                                 } else {
2010                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2011                                                 cur->xmms2.title);
2012                                 }
2013                         }
2014                         OBJ(if_xmms2_connected) {
2015                                 if (cur->xmms2.conn_state != 1) {
2016                                         DO_JUMP;
2017                                 }
2018                         }
2019 #endif /* XMMS */
2020 #ifdef AUDACIOUS
2021                         OBJ(audacious_status) {
2022                                 snprintf(p, p_max_size, "%s",
2023                                         cur->audacious.items[AUDACIOUS_STATUS]);
2024                         }
2025                         OBJ(audacious_title) {
2026                                 snprintf(p, cur->audacious.max_title_len > 0
2027                                         ? cur->audacious.max_title_len : p_max_size, "%s",
2028                                         cur->audacious.items[AUDACIOUS_TITLE]);
2029                         }
2030                         OBJ(audacious_length) {
2031                                 snprintf(p, p_max_size, "%s",
2032                                         cur->audacious.items[AUDACIOUS_LENGTH]);
2033                         }
2034                         OBJ(audacious_length_seconds) {
2035                                 snprintf(p, p_max_size, "%s",
2036                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2037                         }
2038                         OBJ(audacious_position) {
2039                                 snprintf(p, p_max_size, "%s",
2040                                         cur->audacious.items[AUDACIOUS_POSITION]);
2041                         }
2042                         OBJ(audacious_position_seconds) {
2043                                 snprintf(p, p_max_size, "%s",
2044                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2045                         }
2046                         OBJ(audacious_bitrate) {
2047                                 snprintf(p, p_max_size, "%s",
2048                                         cur->audacious.items[AUDACIOUS_BITRATE]);
2049                         }
2050                         OBJ(audacious_frequency) {
2051                                 snprintf(p, p_max_size, "%s",
2052                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
2053                         }
2054                         OBJ(audacious_channels) {
2055                                 snprintf(p, p_max_size, "%s",
2056                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
2057                         }
2058                         OBJ(audacious_filename) {
2059                                 snprintf(p, p_max_size, "%s",
2060                                         cur->audacious.items[AUDACIOUS_FILENAME]);
2061                         }
2062                         OBJ(audacious_playlist_length) {
2063                                 snprintf(p, p_max_size, "%s",
2064                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2065                         }
2066                         OBJ(audacious_playlist_position) {
2067                                 snprintf(p, p_max_size, "%s",
2068                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2069                         }
2070                         OBJ(audacious_main_volume) {
2071                                 snprintf(p, p_max_size, "%s",
2072                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2073                         }
2074 #ifdef X11
2075                         OBJ(audacious_bar) {
2076                                 double progress;
2077
2078                                 progress =
2079                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2080                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2081                                 new_bar(obj, p, (int) (progress * 255.0f));
2082                         }
2083 #endif /* X11 */
2084 #endif /* AUDACIOUS */
2085
2086 #ifdef BMPX
2087                         OBJ(bmpx_title) {
2088                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2089                         }
2090                         OBJ(bmpx_artist) {
2091                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2092                         }
2093                         OBJ(bmpx_album) {
2094                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2095                         }
2096                         OBJ(bmpx_uri) {
2097                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2098                         }
2099                         OBJ(bmpx_track) {
2100                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2101                         }
2102                         OBJ(bmpx_bitrate) {
2103                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2104                         }
2105 #endif /* BMPX */
2106                         /* we have four different types of top (top, top_mem,
2107                          * top_time and top_io). To avoid having almost-same code four
2108                          * times, we have this special handler. */
2109 #ifdef __linux__
2110                         break;
2111                         case OBJ_top:
2112                         case OBJ_top_mem:
2113                         case OBJ_top_time:
2114 #ifdef IOSTATS
2115                         case OBJ_top_io:
2116 #endif
2117                                 /* yes, passing top_name_width instead
2118                                  * of p_max_size is intended here */
2119                                 print_top(obj, p, top_name_width);
2120 #endif /* __linux__ */
2121                         OBJ(tail) {
2122                                 print_tailhead("tail", obj, p, p_max_size);
2123                         }
2124                         OBJ(head) {
2125                                 print_tailhead("head", obj, p, p_max_size);
2126                         }
2127                         OBJ(lines) {
2128                                 static int rep = 0;
2129                                 FILE *fp = open_file(obj->data.s, &rep);
2130
2131                                 if(fp != NULL) {
2132 /* FIXME: use something more general (see also tail.c, head.c */
2133 #define BUFSZ 0x1000
2134                                         char buf[BUFSZ];
2135                                         int j, lines;
2136
2137                                         lines = 0;
2138                                         while(fgets(buf, BUFSZ, fp) != NULL){
2139                                                 for(j = 0; buf[j] != 0; j++) {
2140                                                         if(buf[j] == '\n') {
2141                                                                 lines++;
2142                                                         }
2143                                                 }
2144                                         }
2145                                         sprintf(p, "%d", lines);
2146                                         fclose(fp);
2147                                 } else {
2148                                         sprintf(p, "File Unreadable");
2149                                 }
2150                         }
2151
2152                         OBJ(words) {
2153                                 static int rep = 0;
2154                                 FILE *fp = open_file(obj->data.s, &rep);
2155
2156                                 if(fp != NULL) {
2157                                         char buf[BUFSZ];
2158                                         int j, words;
2159                                         char inword = FALSE;
2160
2161                                         words = 0;
2162                                         while(fgets(buf, BUFSZ, fp) != NULL){
2163                                                 for(j = 0; buf[j] != 0; j++) {
2164                                                         if(!isspace(buf[j])) {
2165                                                                 if(inword == FALSE) {
2166                                                                         words++;
2167                                                                         inword = TRUE;
2168                                                                 }
2169                                                         } else {
2170                                                                 inword = FALSE;
2171                                                         }
2172                                                 }
2173                                         }
2174                                         sprintf(p, "%d", words);
2175                                         fclose(fp);
2176                                 } else {
2177                                         sprintf(p, "File Unreadable");
2178                                 }
2179                         }
2180 #ifdef TCP_PORT_MONITOR
2181                         OBJ(tcp_portmon) {
2182                                 tcp_portmon_action(obj, p, p_max_size);
2183                         }
2184 #endif /* TCP_PORT_MONITOR */
2185
2186 #ifdef HAVE_ICONV
2187                         OBJ(iconv_start) {
2188                                 do_iconv_start(obj);
2189                         }
2190                         OBJ(iconv_stop) {
2191                                 do_iconv_stop();
2192                         }
2193 #endif /* HAVE_ICONV */
2194
2195                         OBJ(entropy_avail) {
2196                                 print_entropy_avail(obj, p, p_max_size);
2197                         }
2198                         OBJ(entropy_perc) {
2199                                 print_entropy_perc(obj, p, p_max_size);
2200                         }
2201                         OBJ(entropy_poolsize) {
2202                                 print_entropy_poolsize(obj, p, p_max_size);
2203                         }
2204                         OBJ(entropy_bar) {
2205                                 print_entropy_bar(obj, p, p_max_size);
2206                         }
2207 #ifdef IBM
2208                         OBJ(smapi) {
2209                                 char *s;
2210                                 if(obj->data.s) {
2211                                         s = smapi_get_val(obj->data.s);
2212                                         snprintf(p, p_max_size, "%s", s);
2213                                         free(s);
2214                                 }
2215                         }
2216                         OBJ(if_smapi_bat_installed) {
2217                                 int idx;
2218                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2219                                         if(!smapi_bat_installed(idx)) {
2220                                                 DO_JUMP;
2221                                         }
2222                                 } else
2223                                         NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2224                         }
2225                         OBJ(smapi_bat_perc) {
2226                                 int idx, val;
2227                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2228                                         val = smapi_bat_installed(idx) ?
2229                                                 smapi_get_bat_int(idx, "remaining_percent") : 0;
2230                                         percent_print(p, p_max_size, val);
2231                                 } else
2232                                         NORM_ERR("argument to smapi_bat_perc must be an integer");
2233                         }
2234                         OBJ(smapi_bat_temp) {
2235                                 int idx, val;
2236                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2237                                         val = smapi_bat_installed(idx) ?
2238                                                 smapi_get_bat_int(idx, "temperature") : 0;
2239                                         /* temperature is in milli degree celsius */
2240                                         temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2241                                 } else
2242                                         NORM_ERR("argument to smapi_bat_temp must be an integer");
2243                         }
2244                         OBJ(smapi_bat_power) {
2245                                 int idx, val;
2246                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2247                                         val = smapi_bat_installed(idx) ?
2248                                                 smapi_get_bat_int(idx, "power_now") : 0;
2249                                         /* power_now is in mW, set to W with one digit precision */
2250                                         snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2251                                 } else
2252                                         NORM_ERR("argument to smapi_bat_power must be an integer");
2253                         }
2254 #ifdef X11
2255                         OBJ(smapi_bat_bar) {
2256                                 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2257                                         new_bar(obj, p, (int)
2258                                                         (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2259                                 else
2260                                         new_bar(obj, p, 0);
2261                         }
2262 #endif /* X11 */
2263 #endif /* IBM */
2264                         OBJ(include) {
2265                                 if(obj->sub) {
2266                                         char buf[max_user_text];
2267
2268                                         generate_text_internal(buf, max_user_text, *obj->sub, cur);
2269                                         snprintf(p, p_max_size, "%s", buf);
2270                                 } else {
2271                                         p[0] = 0;
2272                                 }
2273                         }
2274                         OBJ(blink) {
2275                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2276                                 char buf[max_user_text];
2277                                 unsigned int j;
2278
2279                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2280                                 snprintf(p, p_max_size, "%s", buf);
2281                                 if(total_updates % 2) {
2282                                         for(j=0; p[j] != 0; j++) {
2283                                                 p[j] = ' ';
2284                                         }
2285                                 }
2286                         }
2287                         OBJ(to_bytes) {
2288                                 char buf[max_user_text];
2289                                 long long bytes;
2290                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
2291
2292                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2293                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2294                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2295                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2296                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2297                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2298                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2299                                 }
2300                                 snprintf(p, p_max_size, "%s", buf);
2301                         }
2302                         OBJ(scroll) {
2303                                 print_scroll(obj, p, p_max_size, cur);
2304                         }
2305                         OBJ(combine) {
2306                                 print_combine(obj, p, cur);
2307                         }
2308 #ifdef NVIDIA
2309                         OBJ(nvidia) {
2310                                 print_nvidia_value(obj, display, p, p_max_size);
2311                         }
2312 #endif /* NVIDIA */
2313 #ifdef APCUPSD
2314                         OBJ(apcupsd) {
2315                                 /* This is just a meta-object to set host:port */
2316                         }
2317                         OBJ(apcupsd_name) {
2318                                 snprintf(p, p_max_size, "%s",
2319                                                  cur->apcupsd.items[APCUPSD_NAME]);
2320                         }
2321                         OBJ(apcupsd_model) {
2322                                 snprintf(p, p_max_size, "%s",
2323                                                  cur->apcupsd.items[APCUPSD_MODEL]);
2324                         }
2325                         OBJ(apcupsd_upsmode) {
2326                                 snprintf(p, p_max_size, "%s",
2327                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
2328                         }
2329                         OBJ(apcupsd_cable) {
2330                                 snprintf(p, p_max_size, "%s",
2331                                                  cur->apcupsd.items[APCUPSD_CABLE]);
2332                         }
2333                         OBJ(apcupsd_status) {
2334                                 snprintf(p, p_max_size, "%s",
2335                                                  cur->apcupsd.items[APCUPSD_STATUS]);
2336                         }
2337                         OBJ(apcupsd_linev) {
2338                                 snprintf(p, p_max_size, "%s",
2339                                                  cur->apcupsd.items[APCUPSD_LINEV]);
2340                         }
2341                         OBJ(apcupsd_load) {
2342                                 snprintf(p, p_max_size, "%s",
2343                                                  cur->apcupsd.items[APCUPSD_LOAD]);
2344                         }
2345                         OBJ(apcupsd_loadbar) {
2346                                 double progress;
2347 #ifdef X11
2348                                 if(output_methods & TO_X) {
2349                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2350                                         new_bar(obj, p, (int) progress);
2351                                 } else
2352 #endif /* X11 */
2353                                 {
2354                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2355                                         new_bar_in_shell(obj, p, p_max_size, (int) progress);
2356                                 }
2357                         }
2358 #ifdef X11
2359                         OBJ(apcupsd_loadgraph) {
2360                                 double progress;
2361                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
2362                                 new_graph(obj, p, (int)progress);
2363                         }
2364                         OBJ(apcupsd_loadgauge) {
2365                                 double progress;
2366                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2367                                 new_gauge(obj, p, (int)progress);
2368                         }
2369 #endif /* X11 */
2370                         OBJ(apcupsd_charge) {
2371                                 snprintf(p, p_max_size, "%s",
2372                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
2373                         }
2374                         OBJ(apcupsd_timeleft) {
2375                                 snprintf(p, p_max_size, "%s",
2376                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
2377                         }
2378                         OBJ(apcupsd_temp) {
2379                                 snprintf(p, p_max_size, "%s",
2380                                                  cur->apcupsd.items[APCUPSD_TEMP]);
2381                         }
2382                         OBJ(apcupsd_lastxfer) {
2383                                 snprintf(p, p_max_size, "%s",
2384                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
2385                         }
2386 #endif /* APCUPSD */
2387                         break;
2388                 }
2389 #undef DO_JUMP
2390
2391
2392                 {
2393                         size_t a = strlen(p);
2394
2395 #ifdef HAVE_ICONV
2396                         iconv_convert(&a, buff_in, p, p_max_size);
2397 #endif /* HAVE_ICONV */
2398                         if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2399 #ifdef HAVE_LUA
2400                                         && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2401 #endif /* HAVE_LUA */
2402                                         ) {
2403                                 substitute_newlines(p, a - 2);
2404                         }
2405                         p += a;
2406                         p_max_size -= a;
2407                         (*p) = 0;
2408                 }
2409                 obj = obj->next;
2410         }
2411 #ifdef X11
2412         /* load any new fonts we may have had */
2413         if (need_to_load_fonts) {
2414                 load_fonts();
2415         }
2416 #endif /* X11 */
2417 }
2418
2419 void evaluate(const char *text, char *buffer)
2420 {
2421         struct information *tmp_info;
2422         struct text_object subroot;
2423
2424         tmp_info = malloc(sizeof(struct information));
2425         memcpy(tmp_info, &info, sizeof(struct information));
2426         parse_conky_vars(&subroot, text, buffer, tmp_info);
2427         DBGP("evaluated '%s' to '%s'", text, buffer);
2428
2429         free_text_objects(&subroot, 1);
2430         free(tmp_info);
2431 }
2432
2433 double current_update_time, next_update_time, last_update_time;
2434
2435 static void generate_text(void)
2436 {
2437         struct information *cur = &info;
2438         char *p;
2439
2440         special_count = 0;
2441
2442         /* update info */
2443
2444         current_update_time = get_time();
2445
2446         update_stuff();
2447
2448         /* add things to the buffer */
2449
2450         /* generate text */
2451
2452         p = text_buffer;
2453
2454         generate_text_internal(p, max_user_text, global_root_object, cur);
2455
2456         if (stuff_in_uppercase) {
2457                 char *tmp_p;
2458
2459                 tmp_p = text_buffer;
2460                 while (*tmp_p) {
2461                         *tmp_p = toupper(*tmp_p);
2462                         tmp_p++;
2463                 }
2464         }
2465
2466         next_update_time += update_interval;
2467         if (next_update_time < get_time()) {
2468                 next_update_time = get_time() + update_interval;
2469         } else if (next_update_time > get_time() + update_interval) {
2470                 next_update_time = get_time() + update_interval;
2471         }
2472         last_update_time = current_update_time;
2473         total_updates++;
2474 }
2475
2476 void set_update_interval(double interval)
2477 {
2478         update_interval = interval;
2479         update_interval_old = interval;
2480 }
2481
2482 static inline int get_string_width(const char *s)
2483 {
2484         return *s ? calc_text_width(s) : 0;
2485 }
2486
2487 #ifdef X11
2488 static int get_string_width_special(char *s, int special_index)
2489 {
2490         char *p, *final;
2491         int idx = 1;
2492         int width = 0;
2493         long i;
2494
2495         if (!s)
2496                 return 0;
2497
2498         if ((output_methods & TO_X) == 0)
2499                 return strlen(s);
2500
2501         p = strndup(s, text_buffer_size);
2502         final = p;
2503
2504         while (*p) {
2505                 if (*p == SPECIAL_CHAR) {
2506                         /* shift everything over by 1 so that the special char
2507                          * doesn't mess up the size calculation */
2508                         for (i = 0; i < (long)strlen(p); i++) {
2509                                 *(p + i) = *(p + i + 1);
2510                         }
2511                         if (specials[special_index + idx].type == GRAPH
2512                                         || specials[special_index + idx].type == GAUGE
2513                                         || specials[special_index + idx].type == BAR) {
2514                                 width += specials[special_index + idx].width;
2515                         }
2516                         idx++;
2517                 } else if (*p == SECRIT_MULTILINE_CHAR) {
2518                         *p = 0;
2519                         break;
2520                 } else {
2521                         p++;
2522                 }
2523         }
2524         if (strlen(final) > 1) {
2525                 width += calc_text_width(final);
2526         }
2527         free(final);
2528         return width;
2529 }
2530
2531 static int text_size_updater(char *s, int special_index);
2532
2533 int last_font_height;
2534 static void update_text_area(void)
2535 {
2536         int x = 0, y = 0;
2537
2538         if ((output_methods & TO_X) == 0)
2539                 return;
2540         /* update text size if it isn't fixed */
2541 #ifdef OWN_WINDOW
2542         if (!fixed_size)
2543 #endif
2544         {
2545                 text_width = minimum_width;
2546                 text_height = 0;
2547                 last_font_height = font_height();
2548                 for_each_line(text_buffer, text_size_updater);
2549                 text_width += 1;
2550                 if (text_height < minimum_height) {
2551                         text_height = minimum_height;
2552                 }
2553                 if (text_width > maximum_width && maximum_width > 0) {
2554                         text_width = maximum_width;
2555                 }
2556         }
2557
2558         /* get text position on workarea */
2559         switch (text_alignment) {
2560                 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2561                         y = gap_y;
2562                         break;
2563
2564                 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2565                         y = workarea[3] - text_height - gap_y;
2566                         break;
2567
2568                 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2569                         y = workarea[3] / 2 - text_height / 2 - gap_y;
2570                         break;
2571         }
2572         switch (text_alignment) {
2573                 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2574                         x = gap_x;
2575                         break;
2576
2577                 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2578                         x = workarea[2] - text_width - gap_x;
2579                         break;
2580
2581                 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2582                         x = workarea[2] / 2 - text_width / 2 - gap_x;
2583                         break;
2584         }
2585 #ifdef OWN_WINDOW
2586         if (text_alignment == NONE) {   // Let the WM manage the window
2587                         x = window.x;
2588                         y = window.y;
2589
2590                         fixed_pos = 1;
2591                         fixed_size = 1;
2592         }
2593 #endif /* OWN_WINDOW */
2594 #ifdef OWN_WINDOW
2595
2596         if (own_window && !fixed_pos) {
2597                 x += workarea[0];
2598                 y += workarea[1];
2599                 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2600                 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2601                 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2602                 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2603         } else
2604 #endif
2605         {
2606                 /* If window size doesn't match to workarea's size,
2607                  * then window probably includes panels (gnome).
2608                  * Blah, doesn't work on KDE. */
2609                 if (workarea[2] != window.width || workarea[3] != window.height) {
2610                         y += workarea[1];
2611                         x += workarea[0];
2612                 }
2613
2614                 text_start_x = x;
2615                 text_start_y = y;
2616         }
2617 #ifdef HAVE_LUA
2618         /* update lua window globals */
2619         llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2620 #endif /* HAVE_LUA */
2621 }
2622
2623 /* drawing stuff */
2624
2625 static int cur_x, cur_y;        /* current x and y for drawing */
2626 #endif
2627 //draw_mode also without X11 because we only need to print to stdout with FG
2628 static int draw_mode;           /* FG, BG or OUTLINE */
2629 #ifdef X11
2630 static long current_color;
2631
2632 static int text_size_updater(char *s, int special_index)
2633 {
2634         int w = 0;
2635         char *p;
2636
2637         if ((output_methods & TO_X) == 0)
2638                 return 0;
2639         /* get string widths and skip specials */
2640         p = s;
2641         while (*p) {
2642                 if (*p == SPECIAL_CHAR) {
2643                         *p = '\0';
2644                         w += get_string_width(s);
2645                         *p = SPECIAL_CHAR;
2646
2647                         if (specials[special_index].type == BAR
2648                                         || specials[special_index].type == GAUGE
2649                                         || specials[special_index].type == GRAPH) {
2650                                 w += specials[special_index].width;
2651                                 if (specials[special_index].height > last_font_height) {
2652                                         last_font_height = specials[special_index].height;
2653                                         last_font_height += font_height();
2654                                 }
2655                         } else if (specials[special_index].type == OFFSET) {
2656                                 if (specials[special_index].arg > 0) {
2657                                         w += specials[special_index].arg;
2658                                 }
2659                         } else if (specials[special_index].type == VOFFSET) {
2660                                 last_font_height += specials[special_index].arg;
2661                         } else if (specials[special_index].type == GOTO) {
2662                                 if (specials[special_index].arg > cur_x) {
2663                                         w = (int) specials[special_index].arg;
2664                                 }
2665                         } else if (specials[special_index].type == TAB) {
2666                                 int start = specials[special_index].arg;
2667                                 int step = specials[special_index].width;
2668
2669                                 if (!step || step < 0) {
2670                                         step = 10;
2671                                 }
2672                                 w += step - (cur_x - text_start_x - start) % step;
2673                         } else if (specials[special_index].type == FONT) {
2674                                 selected_font = specials[special_index].font_added;
2675                                 if (font_height() > last_font_height) {
2676                                         last_font_height = font_height();
2677                                 }
2678                         }
2679
2680                         special_index++;
2681                         s = p + 1;
2682                 } else if (*p == SECRIT_MULTILINE_CHAR) {
2683                         int lw;
2684                         *p = '\0';
2685                         lw = get_string_width(s);
2686                         *p = SECRIT_MULTILINE_CHAR;
2687                         s = p + 1;
2688                         w = lw > w ? lw : w;
2689                         text_height += last_font_height;
2690                 }
2691                 p++;
2692         }
2693         w += get_string_width(s);
2694         if (w > text_width) {
2695                 text_width = w;
2696         }
2697         if (text_width > maximum_width && maximum_width) {
2698                 text_width = maximum_width;
2699         }
2700
2701         text_height += last_font_height;
2702         last_font_height = font_height();
2703         return special_index;
2704 }
2705 #endif /* X11 */
2706
2707 static inline void set_foreground_color(long c)
2708 {
2709 #ifdef X11
2710         if (output_methods & TO_X) {
2711                 current_color = c;
2712                 XSetForeground(display, window.gc, c);
2713         }
2714 #endif /* X11 */
2715 #ifdef NCURSES
2716         if (output_methods & TO_NCURSES) {
2717                 attron(COLOR_PAIR(c));
2718         }
2719 #endif /* NCURSES */
2720         UNUSED(c);
2721         return;
2722 }
2723
2724 static void draw_string(const char *s)
2725 {
2726         int i, i2, pos, width_of_s;
2727         int max = 0;
2728         int added;
2729         char *s_with_newlines;
2730
2731         if (s[0] == '\0') {
2732                 return;
2733         }
2734
2735         width_of_s = get_string_width(s);
2736         s_with_newlines = strdup(s);
2737         for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2738                 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2739                         s_with_newlines[i] = '\n';
2740                 }
2741         }
2742         if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2743                 printf("%s\n", s_with_newlines);
2744                 if (extra_newline) fputc('\n', stdout);
2745                 fflush(stdout); /* output immediately, don't buffer */
2746         }
2747         if ((output_methods & TO_STDERR) && draw_mode == FG) {
2748                 fprintf(stderr, "%s\n", s_with_newlines);
2749                 fflush(stderr); /* output immediately, don't buffer */
2750         }
2751         if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2752                 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2753         }
2754         if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2755                 fprintf(append_fpointer, "%s\n", s_with_newlines);
2756         }
2757 #ifdef NCURSES
2758         if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2759                 printw("%s", s_with_newlines);
2760         }
2761 #endif
2762         free(s_with_newlines);
2763         memset(tmpstring1, 0, text_buffer_size);
2764         memset(tmpstring2, 0, text_buffer_size);
2765         strncpy(tmpstring1, s, text_buffer_size - 1);
2766         pos = 0;
2767         added = 0;
2768
2769 #ifdef X11
2770         if (output_methods & TO_X) {
2771                 max = ((text_width - width_of_s) / get_string_width(" "));
2772         }
2773 #endif /* X11 */
2774         /* This code looks for tabs in the text and coverts them to spaces.
2775          * The trick is getting the correct number of spaces, and not going
2776          * over the window's size without forcing the window larger. */
2777         for (i = 0; i < (int) text_buffer_size; i++) {
2778                 if (tmpstring1[i] == '\t') {
2779                         i2 = 0;
2780                         for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2781                                 /* guard against overrun */
2782                                 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2783                                 added++;
2784                         }
2785                         pos += i2;
2786                 } else {
2787                         /* guard against overrun */
2788                         tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2789                         pos++;
2790                 }
2791         }
2792 #ifdef X11
2793         if (output_methods & TO_X) {
2794                 if (text_width == maximum_width) {
2795                         /* this means the text is probably pushing the limit,
2796                          * so we'll chop it */
2797                         while (cur_x + get_string_width(tmpstring2) - text_start_x
2798                                         > maximum_width && strlen(tmpstring2) > 0) {
2799                                 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2800                         }
2801                 }
2802         }
2803 #endif /* X11 */
2804         s = tmpstring2;
2805 #ifdef X11
2806         if (output_methods & TO_X) {
2807 #ifdef XFT
2808                 if (use_xft) {
2809                         XColor c;
2810                         XftColor c2;
2811
2812                         c.pixel = current_color;
2813                         XQueryColor(display, DefaultColormap(display, screen), &c);
2814
2815                         c2.pixel = c.pixel;
2816                         c2.color.red = c.red;
2817                         c2.color.green = c.green;
2818                         c2.color.blue = c.blue;
2819                         c2.color.alpha = fonts[selected_font].font_alpha;
2820                         if (utf8_mode) {
2821                                 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2822                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2823                         } else {
2824                                 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2825                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2826                         }
2827                 } else
2828 #endif
2829                 {
2830                         XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2831                                 strlen(s));
2832                 }
2833                 cur_x += width_of_s;
2834         }
2835 #endif /* X11 */
2836         memcpy(tmpstring1, s, text_buffer_size);
2837 }
2838
2839 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2840 {
2841 #ifdef X11
2842         int font_h;
2843         int cur_y_add = 0;
2844 #endif /* X11 */
2845         char *recurse = 0;
2846         char *p = s;
2847         int last_special_needed = -1;
2848         int orig_special_index = special_index;
2849
2850 #ifdef X11
2851         if (output_methods & TO_X) {
2852                 font_h = font_height();
2853                 cur_y += font_ascent();
2854         }
2855         cur_x = text_start_x;
2856 #endif /* X11 */
2857
2858         while (*p) {
2859                 if (*p == SECRIT_MULTILINE_CHAR) {
2860                         /* special newline marker for multiline objects */
2861                         recurse = p + 1;
2862                         *p = '\0';
2863                         break;
2864                 }
2865                 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2866 #ifdef X11
2867                         int w = 0;
2868 #endif /* X11 */
2869
2870                         /* draw string before special, unless we're dealing multiline
2871                          * specials */
2872                         if (last_special_applied > -1) {
2873                                 special_index = last_special_applied;
2874                         } else {
2875                                 *p = '\0';
2876                                 draw_string(s);
2877                                 *p = SPECIAL_CHAR;
2878                                 s = p + 1;
2879                         }
2880                         /* draw special */
2881                         switch (specials[special_index].type) {
2882 #ifdef X11
2883                                 case HORIZONTAL_LINE:
2884                                 {
2885                                         int h = specials[special_index].height;
2886                                         int mid = font_ascent() / 2;
2887
2888                                         w = text_start_x + text_width - cur_x;
2889
2890                                         XSetLineAttributes(display, window.gc, h, LineSolid,
2891                                                 CapButt, JoinMiter);
2892                                         XDrawLine(display, window.drawable, window.gc, cur_x,
2893                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2894                                         break;
2895                                 }
2896
2897                                 case STIPPLED_HR:
2898                                 {
2899                                         int h = specials[special_index].height;
2900                                         int tmp_s = specials[special_index].arg;
2901                                         int mid = font_ascent() / 2;
2902                                         char ss[2] = { tmp_s, tmp_s };
2903
2904                                         w = text_start_x + text_width - cur_x - 1;
2905                                         XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2906                                                 CapButt, JoinMiter);
2907                                         XSetDashes(display, window.gc, 0, ss, 2);
2908                                         XDrawLine(display, window.drawable, window.gc, cur_x,
2909                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2910                                         break;
2911                                 }
2912
2913                                 case BAR:
2914                                 {
2915                                         int h, bar_usage, by;
2916                                         if (cur_x - text_start_x > maximum_width
2917                                                         && maximum_width > 0) {
2918                                                 break;
2919                                         }
2920                                         h = specials[special_index].height;
2921                                         bar_usage = specials[special_index].arg;
2922                                         by = cur_y - (font_ascent() / 2) - 1;
2923
2924                                         if (h < font_h) {
2925                                                 by -= h / 2 - 1;
2926                                         }
2927                                         w = specials[special_index].width;
2928                                         if (w == 0) {
2929                                                 w = text_start_x + text_width - cur_x - 1;
2930                                         }
2931                                         if (w < 0) {
2932                                                 w = 0;
2933                                         }
2934
2935                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
2936                                                 CapButt, JoinMiter);
2937
2938                                         XDrawRectangle(display, window.drawable, window.gc, cur_x,
2939                                                 by, w, h);
2940                                         XFillRectangle(display, window.drawable, window.gc, cur_x,
2941                                                 by, w * bar_usage / 255, h);
2942                                         if (h > cur_y_add
2943                                                         && h > font_h) {
2944                                                 cur_y_add = h;
2945                                         }
2946                                         break;
2947                                 }
2948
2949                                 case GAUGE: /* new GAUGE  */
2950                                 {
2951                                         int h, by = 0;
2952                                         unsigned long last_colour = current_color;
2953 #ifdef MATH
2954                                         float angle, px, py;
2955                                         int usage;
2956 #endif /* MATH */
2957
2958                                         if (cur_x - text_start_x > maximum_width
2959                                                         && maximum_width > 0) {
2960                                                 break;
2961                                         }
2962
2963                                         h = specials[special_index].height;
2964                                         by = cur_y - (font_ascent() / 2) - 1;
2965
2966                                         if (h < font_h) {
2967                                                 by -= h / 2 - 1;
2968                                         }
2969                                         w = specials[special_index].width;
2970                                         if (w == 0) {
2971                                                 w = text_start_x + text_width - cur_x - 1;
2972                                         }
2973                                         if (w < 0) {
2974                                                 w = 0;
2975                                         }
2976
2977                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
2978                                                         CapButt, JoinMiter);
2979
2980                                         XDrawArc(display, window.drawable, window.gc,
2981                                                         cur_x, by, w, h * 2, 0, 180*64);
2982
2983 #ifdef MATH
2984                                         usage = specials[special_index].arg;
2985                                         angle = (M_PI)*(float)(usage)/255.;
2986                                         px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2987                                         py = (float)(by+(h))-(float)(h)*sin(angle);
2988
2989                                         XDrawLine(display, window.drawable, window.gc,
2990                                                         cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2991 #endif /* MATH */
2992
2993                                         if (h > cur_y_add
2994                                                         && h > font_h) {
2995                                                 cur_y_add = h;
2996                                         }
2997
2998                                         set_foreground_color(last_colour);
2999
3000                                         break;
3001
3002                                 }
3003
3004                                 case GRAPH:
3005                                 {
3006                                         int h, by, i = 0, j = 0;
3007                                         int colour_idx = 0;
3008                                         unsigned long last_colour = current_color;
3009                                         unsigned long *tmpcolour = 0;
3010                                         if (cur_x - text_start_x > maximum_width
3011                                                         && maximum_width > 0) {
3012                                                 break;
3013                                         }
3014                                         h = specials[special_index].height;
3015                                         by = cur_y - (font_ascent() / 2) - 1;
3016
3017                                         if (h < font_h) {
3018                                                 by -= h / 2 - 1;
3019                                         }
3020                                         w = specials[special_index].width;
3021                                         if (w == 0) {
3022                                                 w = text_start_x + text_width - cur_x - 1;
3023                                         }
3024                                         if (w < 0) {
3025                                                 w = 0;
3026                                         }
3027                                         if (draw_graph_borders) {
3028                                                 XSetLineAttributes(display, window.gc, 1, LineSolid,
3029                                                         CapButt, JoinMiter);
3030                                                 XDrawRectangle(display, window.drawable, window.gc,
3031                                                         cur_x, by, w, h);
3032                                         }
3033                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
3034                                                 CapButt, JoinMiter);
3035
3036                                         if (specials[special_index].last_colour != 0
3037                                                         || specials[special_index].first_colour != 0) {
3038                                                 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3039                                         }
3040                                         colour_idx = 0;
3041                                         for (i = w - 2; i > -1; i--) {
3042                                                 if (specials[special_index].last_colour != 0
3043                                                                 || specials[special_index].first_colour != 0) {
3044                                                         if (specials[special_index].tempgrad) {
3045 #ifdef DEBUG_lol
3046                                                                 assert(
3047                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3048                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3049                                                                                 < w - 1
3050                                                                           );
3051                                                                 assert(
3052                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3053                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3054                                                                                 > -1
3055                                                                           );
3056                                                                 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3057                                                                         assert(
3058                                                                                         (int)((float)(w - 2) - specials[special_index].graph[j] *
3059                                                                                                 (w - 2) / (float)specials[special_index].graph_scale)
3060                                                                                         == 0
3061                                                                                   );
3062                                                                 }
3063 #endif /* DEBUG_lol */
3064                                                                 XSetForeground(display, window.gc, tmpcolour[
3065                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3066                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3067                                                                                 ]);
3068                                                         } else {
3069                                                                 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3070                                                         }
3071                                                 }
3072                                                 /* this is mugfugly, but it works */
3073                                                 XDrawLine(display, window.drawable, window.gc,
3074                                                                 cur_x + i + 1, by + h, cur_x + i + 1,
3075                                                                 round_to_int((double)by + h - specials[special_index].graph[j] *
3076                                                                         (h - 1) / specials[special_index].graph_scale));
3077                                                 if ((w - i) / ((float) (w - 2) /
3078                                                                         (specials[special_index].graph_width)) > j
3079                                                                 && j < MAX_GRAPH_DEPTH - 3) {
3080                                                         j++;
3081                                                 }
3082                                         }
3083                                         if (tmpcolour) free(tmpcolour);
3084                                         if (h > cur_y_add
3085                                                         && h > font_h) {
3086                                                 cur_y_add = h;
3087                                         }
3088                                         /* if (draw_mode == BG) {
3089                                                 set_foreground_color(default_bg_color);
3090                                         } else if (draw_mode == OUTLINE) {
3091                                                 set_foreground_color(default_out_color);
3092                                         } else {
3093                                                 set_foreground_color(default_fg_color);
3094                                         } */
3095                                         if (show_graph_range) {
3096                                                 int tmp_x = cur_x;
3097                                                 int tmp_y = cur_y;
3098                                                 unsigned short int seconds = update_interval * w;
3099                                                 char *tmp_day_str;
3100                                                 char *tmp_hour_str;
3101                                                 char *tmp_min_str;
3102                                                 char *tmp_sec_str;
3103                                                 char *tmp_str;
3104                                                 unsigned short int timeunits;
3105                                                 if (seconds != 0) {
3106                                                         timeunits = seconds / 86400; seconds %= 86400;
3107                                                         if (timeunits > 0) {
3108                                                                 asprintf(&tmp_day_str, "%dd", timeunits);
3109                                                         } else {
3110                                                                 tmp_day_str = strdup("");
3111                                                         }
3112                                                         timeunits = seconds / 3600; seconds %= 3600;
3113                                                         if (timeunits > 0) {
3114                                                                 asprintf(&tmp_hour_str, "%dh", timeunits);
3115                                                         } else {
3116                                                                 tmp_hour_str = strdup("");
3117                                                         }
3118                                                         timeunits = seconds / 60; seconds %= 60;
3119                                                         if (timeunits > 0) {
3120                                                                 asprintf(&tmp_min_str, "%dm", timeunits);
3121                                                         } else {
3122                                                                 tmp_min_str = strdup("");
3123                                                         }
3124                                                         if (seconds > 0) {
3125                                                                 asprintf(&tmp_sec_str, "%ds", seconds);
3126                                                         } else {
3127                                                                 tmp_sec_str = strdup("");
3128                                                         }
3129                                                         asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3130                                                         free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3131                                                 } else {
3132                                                         asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3133                                                 }
3134                                                 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3135                                                 cur_y += font_h / 2;
3136                                                 draw_string(tmp_str);
3137                                                 free(tmp_str);
3138                                                 cur_x = tmp_x;
3139                                                 cur_y = tmp_y;
3140                                         }
3141 #ifdef MATH
3142                                         if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3143                                                 int tmp_x = cur_x;
3144                                                 int tmp_y = cur_y;
3145                                                 char *tmp_str;
3146                                                 cur_x += font_ascent() / 2;
3147                                                 cur_y += font_h / 2;
3148                                                 tmp_str = (char *)
3149                                                         calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3150                                                                         sizeof(char));
3151                                                 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3152                                                 draw_string(tmp_str);
3153                                                 free(tmp_str);
3154                                                 cur_x = tmp_x;
3155                                                 cur_y = tmp_y;
3156                                         }
3157 #endif
3158                                         set_foreground_color(last_colour);
3159                                         break;
3160                                 }
3161
3162                                 case FONT:
3163                                 {
3164                                         int old = font_ascent();
3165
3166                                         cur_y -= font_ascent();
3167                                         selected_font = specials[special_index].font_added;
3168                                         set_font();
3169                                         if (cur_y + font_ascent() < cur_y + old) {
3170                                                 cur_y += old;
3171                                         } else {
3172                                                 cur_y += font_ascent();
3173                                         }
3174                                         font_h = font_height();
3175                                         break;
3176                                 }
3177 #endif /* X11 */
3178                                 case FG:
3179                                         if (draw_mode == FG) {
3180                                                 set_foreground_color(specials[special_index].arg);
3181                                         }
3182                                         break;
3183
3184 #ifdef X11
3185                                 case BG:
3186                                         if (draw_mode == BG) {
3187                                                 set_foreground_color(specials[special_index].arg);
3188                                         }
3189                                         break;
3190
3191                                 case OUTLINE:
3192                                         if (draw_mode == OUTLINE) {
3193                                                 set_foreground_color(specials[special_index].arg);
3194                                         }
3195                                         break;
3196
3197                                 case OFFSET:
3198                                         w += specials[special_index].arg;
3199                                         last_special_needed = special_index;
3200                                         break;
3201
3202                                 case VOFFSET:
3203                                         cur_y += specials[special_index].arg;
3204                                         break;
3205
3206                                 case GOTO:
3207                                         if (specials[special_index].arg >= 0) {
3208                                                 cur_x = (int) specials[special_index].arg;
3209                                         }
3210                                         last_special_needed = special_index;
3211                                         break;
3212
3213                                 case TAB:
3214                                 {
3215                                         int start = specials[special_index].arg;
3216                                         int step = specials[special_index].width;
3217
3218                                         if (!step || step < 0) {
3219                                                 step = 10;
3220                                         }
3221                                         w = step - (cur_x - text_start_x - start) % step;
3222                                         last_special_needed = special_index;
3223                                         break;
3224                                 }
3225
3226                                 case ALIGNR:
3227                                 {
3228                                         /* TODO: add back in "+ window.border_inner_margin" to the end of
3229                                          * this line? */
3230                                         int pos_x = text_start_x + text_width -
3231                                                 get_string_width_special(s, special_index);
3232
3233                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3234                                                 "get_string_width(p) %i gap_x %i "
3235                                                 "specials[special_index].arg %i window.border_inner_margin %i "
3236                                                 "window.border_width %i\n", pos_x, text_start_x, text_width,
3237                                                 cur_x, get_string_width_special(s), gap_x,
3238                                                 specials[special_index].arg, window.border_inner_margin,
3239                                                 window.border_width); */
3240                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3241                                                 cur_x = pos_x - specials[special_index].arg;
3242                                         }
3243                                         last_special_needed = special_index;
3244                                         break;
3245                                 }
3246
3247                                 case ALIGNC:
3248                                 {
3249                                         int pos_x = (text_width) / 2 - get_string_width_special(s,
3250                                                         special_index) / 2 - (cur_x -
3251                                                                 text_start_x);
3252                                         /* int pos_x = text_start_x + text_width / 2 -
3253                                                 get_string_width_special(s) / 2; */
3254
3255                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3256                                                 "get_string_width(p) %i gap_x %i "
3257                                                 "specials[special_index].arg %i\n", pos_x, text_start_x,
3258                                                 text_width, cur_x, get_string_width(s), gap_x,
3259                                                 specials[special_index].arg); */
3260                                         if (pos_x > specials[special_index].arg) {
3261                                                 w = pos_x - specials[special_index].arg;
3262                                         }
3263                                         last_special_needed = special_index;
3264                                         break;
3265                                 }
3266 #endif /* X11 */
3267                         }
3268
3269 #ifdef X11
3270                         cur_x += w;
3271 #endif /* X11 */
3272
3273                         if (special_index != last_special_applied) {
3274                                 special_index++;
3275                         } else {
3276                                 special_index = orig_special_index;
3277                                 last_special_applied = -1;
3278                         }
3279                 }
3280                 p++;
3281         }
3282
3283 #ifdef X11
3284         cur_y += cur_y_add;
3285 #endif /* X11 */
3286         draw_string(s);
3287 #ifdef NCURSES
3288         if (output_methods & TO_NCURSES) {
3289                 printw("\n");
3290         }
3291 #endif /* NCURSES */
3292 #ifdef X11
3293         if (output_methods & TO_X)
3294                 cur_y += font_descent();
3295 #endif /* X11 */
3296         if (recurse && *recurse) {
3297                 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3298                 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3299         }
3300         return special_index;
3301 }
3302
3303 static int draw_line(char *s, int special_index)
3304 {
3305 #ifdef X11
3306         if (output_methods & TO_X) {
3307                 return draw_each_line_inner(s, special_index, -1);
3308         }
3309 #endif /* X11 */
3310 #ifdef NCURSES
3311         if (output_methods & TO_NCURSES) {
3312                 return draw_each_line_inner(s, special_index, -1);
3313         }
3314 #endif /* NCURSES */
3315         draw_string(s);
3316         UNUSED(special_index);
3317         return 0;
3318 }
3319
3320 static void draw_text(void)
3321 {
3322 #ifdef X11
3323 #ifdef HAVE_LUA
3324         llua_draw_pre_hook();
3325 #endif /* HAVE_LUA */
3326         if (output_methods & TO_X) {
3327                 cur_y = text_start_y;
3328
3329                 /* draw borders */
3330                 if (draw_borders && window.border_width > 0) {
3331                         if (stippled_borders) {
3332                                 char ss[2] = { stippled_borders, stippled_borders };
3333                                 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3334                                         CapButt, JoinMiter);
3335                                 XSetDashes(display, window.gc, 0, ss, 2);
3336                         } else {
3337                                 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3338                                         CapButt, JoinMiter);
3339                         }
3340
3341                         XDrawRectangle(display, window.drawable, window.gc,
3342                                 text_start_x - window.border_inner_margin - window.border_width,
3343                                 text_start_y - window.border_inner_margin - window.border_width,
3344                                 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3345                                 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3346                 }
3347
3348                 /* draw text */
3349         }
3350         setup_fonts();
3351 #endif /* X11 */
3352 #ifdef NCURSES
3353         init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3354         attron(COLOR_PAIR(COLOR_WHITE));
3355 #endif /* NCURSES */
3356         for_each_line(text_buffer, draw_line);
3357 #if defined(HAVE_LUA) && defined(X11)
3358         llua_draw_post_hook();
3359 #endif /* HAVE_LUA */
3360 }
3361
3362 static void draw_stuff(void)
3363 {
3364 #ifdef IMLIB2
3365         cimlib_render(text_start_x, text_start_y, window.width, window.height);
3366 #endif /* IMLIB2 */
3367         if (overwrite_file) {
3368                 overwrite_fpointer = fopen(overwrite_file, "w");
3369                 if(!overwrite_fpointer)
3370                         NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3371         }
3372         if (append_file) {
3373                 append_fpointer = fopen(append_file, "a");
3374                 if(!append_fpointer)
3375                         NORM_ERR("Can't append '%s' anymore", append_file);
3376         }
3377 #ifdef X11
3378         if (output_methods & TO_X) {
3379                 selected_font = 0;
3380                 if (draw_shades && !draw_outline) {
3381                         text_start_x++;
3382                         text_start_y++;
3383                         set_foreground_color(default_bg_color);
3384                         draw_mode = BG;
3385                         draw_text();
3386                         text_start_x--;
3387                         text_start_y--;
3388                 }
3389
3390                 if (draw_outline) {
3391                         int i, j;
3392                         selected_font = 0;
3393
3394                         for (i = -1; i < 2; i++) {
3395                                 for (j = -1; j < 2; j++) {
3396                                         if (i == 0 && j == 0) {
3397                                                 continue;
3398                                         }
3399                                         text_start_x += i;
3400                                         text_start_y += j;
3401                                         set_foreground_color(default_out_color);
3402                                         draw_mode = OUTLINE;
3403                                         draw_text();
3404                                         text_start_x -= i;
3405                                         text_start_y -= j;
3406                                 }
3407                         }
3408                 }
3409
3410                 set_foreground_color(default_fg_color);
3411         }
3412 #endif /* X11 */
3413         draw_mode = FG;
3414         draw_text();
3415 #if defined(X11) && defined(HAVE_XDBE)
3416         if (output_methods & TO_X) {
3417                 xdbe_swap_buffers();
3418         }
3419 #endif /* X11 && HAVE_XDBE */
3420         if(overwrite_fpointer) {
3421                 fclose(overwrite_fpointer);
3422                 overwrite_fpointer = 0;
3423         }
3424         if(append_fpointer) {
3425                 fclose(append_fpointer);
3426                 append_fpointer = 0;
3427         }
3428 }
3429
3430 #ifdef X11
3431 static void clear_text(int exposures)
3432 {
3433 #ifdef HAVE_XDBE
3434         if (use_xdbe) {
3435                 /* The swap action is XdbeBackground, which clears */
3436                 return;
3437         } else
3438 #endif
3439         if (display && window.window) { // make sure these are !null
3440                 /* there is some extra space for borders and outlines */
3441                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3442                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3443                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3444                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3445         }
3446 }
3447 #endif /* X11 */
3448
3449 static int need_to_update;
3450
3451 /* update_text() generates new text and clears old text area */
3452 static void update_text(void)
3453 {
3454 #ifdef IMLIB2
3455         cimlib_cleanup();
3456 #endif /* IMLIB2 */
3457         generate_text();
3458 #ifdef X11
3459         if (output_methods & TO_X)
3460                 clear_text(1);
3461 #endif /* X11 */
3462         need_to_update = 1;
3463 #ifdef HAVE_LUA
3464         llua_update_info(&info, update_interval);
3465 #endif /* HAVE_LUA */
3466 }
3467
3468 #ifdef HAVE_SYS_INOTIFY_H
3469 int inotify_fd;
3470 #endif
3471
3472 static void main_loop(void)
3473 {
3474         int terminate = 0;
3475 #ifdef SIGNAL_BLOCKING
3476         sigset_t newmask, oldmask;
3477 #endif
3478         double t;
3479 #ifdef HAVE_SYS_INOTIFY_H
3480         int inotify_config_wd = -1;
3481 #define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
3482 #define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
3483         char inotify_buff[INOTIFY_BUF_LEN];
3484 #endif /* HAVE_SYS_INOTIFY_H */
3485
3486
3487 #ifdef SIGNAL_BLOCKING
3488         sigemptyset(&newmask);
3489         sigaddset(&newmask, SIGINT);
3490         sigaddset(&newmask, SIGTERM);
3491         sigaddset(&newmask, SIGUSR1);
3492 #endif
3493
3494         last_update_time = 0.0;
3495         next_update_time = get_time();
3496         info.looped = 0;
3497         while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3498                 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3499                         char buf[max_user_text];
3500
3501                         get_battery_short_status(buf, max_user_text, "BAT0");
3502                         if(buf[0] == 'D') {
3503                                 update_interval = update_interval_bat;
3504                         } else {
3505                                 update_interval = update_interval_old;
3506                         }
3507                 }
3508                 info.looped++;
3509
3510 #ifdef SIGNAL_BLOCKING
3511                 /* block signals.  we will inspect for pending signals later */
3512                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3513                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3514                 }
3515 #endif
3516
3517 #ifdef X11
3518                 if (output_methods & TO_X) {
3519                         XFlush(display);
3520
3521                         /* wait for X event or timeout */
3522
3523                         if (!XPending(display)) {
3524                                 fd_set fdsr;
3525                                 struct timeval tv;
3526                                 int s;
3527                                 t = next_update_time - get_time();
3528
3529                                 if (t < 0) {
3530                                         t = 0;
3531                                 } else if (t > update_interval) {
3532                                         t = update_interval;
3533                                 }
3534
3535                                 tv.tv_sec = (long) t;
3536                                 tv.tv_usec = (long) (t * 1000000) % 1000000;
3537                                 FD_ZERO(&fdsr);
3538                                 FD_SET(ConnectionNumber(display), &fdsr);
3539
3540                                 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3541                                 if (s == -1) {
3542                                         if (errno != EINTR) {
3543                                                 NORM_ERR("can't select(): %s", strerror(errno));
3544                                         }
3545                                 } else {
3546                                         /* timeout */
3547                                         if (s == 0) {
3548                                                 update_text();
3549                                         }
3550                                 }
3551                         }
3552
3553                         if (need_to_update) {
3554 #ifdef OWN_WINDOW
3555                                 int wx = window.x, wy = window.y;
3556 #endif
3557
3558                                 need_to_update = 0;
3559                                 selected_font = 0;
3560                                 update_text_area();
3561 #ifdef OWN_WINDOW
3562                                 if (own_window) {
3563                                         int changed = 0;
3564
3565                                         /* resize window if it isn't right size */
3566                                         if (!fixed_size
3567                                                         && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3568                                                                 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3569                                                 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3570                                                 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3571                                                 draw_stuff(); /* redraw everything in our newly sized window */
3572                                                 XResizeWindow(display, window.window, window.width,
3573                                                                 window.height); /* resize window */
3574                                                 set_transparent_background(window.window);
3575 #ifdef HAVE_XDBE
3576                                                 /* swap buffers */
3577                                                 xdbe_swap_buffers();
3578 #endif
3579
3580                                                 changed++;
3581 #ifdef HAVE_LUA
3582                                                 /* update lua window globals */
3583                                                 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3584 #endif /* HAVE_LUA */
3585                                         }
3586
3587                                         /* move window if it isn't in right position */
3588                                         if (!fixed_pos && (window.x != wx || window.y != wy)) {
3589                                                 XMoveWindow(display, window.window, window.x, window.y);
3590                                                 changed++;
3591                                         }
3592
3593                                         /* update struts */
3594                                         if (changed && window.type == TYPE_PANEL) {
3595                                                 int sidenum = -1;
3596
3597                                                 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3598                                                 fflush(stderr);
3599
3600                                                 switch (text_alignment) {
3601                                                         case TOP_LEFT:
3602                                                         case TOP_RIGHT:
3603                                                         case TOP_MIDDLE:
3604                                                                 {
3605                                                                         sidenum = 2;
3606                                                                         break;
3607                                                                 }
3608                                                         case BOTTOM_LEFT:
3609                                                         case BOTTOM_RIGHT:
3610                                                         case BOTTOM_MIDDLE:
3611                                                                 {
3612                                                                         sidenum = 3;
3613                                                                         break;
3614                                                                 }
3615                                                         case MIDDLE_LEFT:
3616                                                                 {
3617                                                                         sidenum = 0;
3618                                                                         break;
3619                                                                 }
3620                                                         case MIDDLE_RIGHT:
3621                                                                 {
3622                                                                         sidenum = 1;
3623                                                                         break;
3624                                                                 }
3625                                                 }
3626
3627                                                 set_struts(sidenum);
3628                                         }
3629                                 }
3630 #endif
3631
3632                                 clear_text(1);
3633
3634 #ifdef HAVE_XDBE
3635                                 if (use_xdbe) {
3636                                         XRectangle r;
3637
3638                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3639                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3640                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3641                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3642                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3643                                 }
3644 #endif
3645                         }
3646
3647                         /* handle X events */
3648                         while (XPending(display)) {
3649                                 XEvent ev;
3650
3651                                 XNextEvent(display, &ev);
3652                                 switch (ev.type) {
3653                                         case Expose:
3654                                         {
3655                                                 XRectangle r;
3656                                                 r.x = ev.xexpose.x;
3657                                                 r.y = ev.xexpose.y;
3658                                                 r.width = ev.xexpose.width;
3659                                                 r.height = ev.xexpose.height;
3660                                                 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3661                                                 break;
3662                                         }
3663
3664                                         case PropertyNotify:
3665                                         {
3666                                                 if ( ev.xproperty.state == PropertyNewValue ) {
3667                                                         get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3668                                                 }
3669                                                 break;
3670                                         }
3671
3672 #ifdef OWN_WINDOW
3673                                         case ReparentNotify:
3674                                                 /* set background to ParentRelative for all parents */
3675                                                 if (own_window) {
3676                                                         set_transparent_background(window.window);
3677                                                 }
3678                                                 break;
3679
3680                                         case ConfigureNotify:
3681                                                 if (own_window) {
3682                                                         /* if window size isn't what expected, set fixed size */
3683                                                         if (ev.xconfigure.width != window.width
3684                                                                         || ev.xconfigure.height != window.height) {
3685                                                                 if (window.width != 0 && window.height != 0) {
3686                                                                         fixed_size = 1;
3687                                                                 }
3688
3689                                                                 /* clear old stuff before screwing up
3690                                                                  * size and pos */
3691                                                                 clear_text(1);
3692
3693                                                                 {
3694                                                                         XWindowAttributes attrs;
3695                                                                         if (XGetWindowAttributes(display,
3696                                                                                         window.window, &attrs)) {
3697                                                                                 window.width = attrs.width;
3698                                                                                 window.height = attrs.height;
3699                                                                         }
3700                                                                 }
3701
3702                                                                 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3703                                                                 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3704                                                                 if (text_width > maximum_width
3705                                                                                 && maximum_width > 0) {
3706                                                                         text_width = maximum_width;
3707                                                                 }
3708                                                         }
3709
3710                                                         /* if position isn't what expected, set fixed pos
3711                                                          * total_updates avoids setting fixed_pos when window
3712                                                          * is set to weird locations when started */
3713                                                         /* // this is broken
3714                                                         if (total_updates >= 2 && !fixed_pos
3715                                                                         && (window.x != ev.xconfigure.x
3716                                                                         || window.y != ev.xconfigure.y)
3717                                                                         && (ev.xconfigure.x != 0
3718                                                                         || ev.xconfigure.y != 0)) {
3719                                                                 fixed_pos = 1;
3720                                                         } */
3721                                                 }
3722                                                 break;
3723
3724                                         case ButtonPress:
3725                                                 if (own_window) {
3726                                                         /* if an ordinary window with decorations */
3727                                                         if ((window.type == TYPE_NORMAL &&
3728                                                                                 (!TEST_HINT(window.hints,
3729                                                                                                         HINT_UNDECORATED))) ||
3730                                                                         window.type == TYPE_DESKTOP) {
3731                                                                 /* allow conky to hold input focus. */
3732                                                                 break;
3733                                                         } else {
3734                                                                 /* forward the click to the desktop window */
3735                                                                 XUngrabPointer(display, ev.xbutton.time);
3736                                                                 ev.xbutton.window = window.desktop;
3737                                                                 ev.xbutton.x = ev.xbutton.x_root;
3738                                                                 ev.xbutton.y = ev.xbutton.y_root;
3739                                                                 XSendEvent(display, ev.xbutton.window, False,
3740                                                                         ButtonPressMask, &ev);
3741                                                                 XSetInputFocus(display, ev.xbutton.window,
3742                                                                         RevertToParent, ev.xbutton.time);
3743                                                         }
3744                                                 }
3745                                                 break;
3746
3747                                         case ButtonRelease:
3748                                                 if (own_window) {
3749                                                         /* if an ordinary window with decorations */
3750                                                         if ((window.type == TYPE_NORMAL)
3751                                                                         && (!TEST_HINT(window.hints,
3752                                                                         HINT_UNDECORATED))) {
3753                                                                 /* allow conky to hold input focus. */
3754                                                                 break;
3755                                                         } else {
3756                                                                 /* forward the release to the desktop window */
3757                                                                 ev.xbutton.window = window.desktop;
3758                                                                 ev.xbutton.x = ev.xbutton.x_root;
3759                                                                 ev.xbutton.y = ev.xbutton.y_root;
3760                                                                 XSendEvent(display, ev.xbutton.window, False,
3761                                                                         ButtonReleaseMask, &ev);
3762                                                         }
3763                                                 }
3764                                                 break;
3765
3766 #endif
3767
3768                                         default:
3769 #ifdef HAVE_XDAMAGE
3770                                                 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3771                                                         XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3772
3773                                                         XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3774                                                         XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3775                                                 }
3776 #endif /* HAVE_XDAMAGE */
3777                                                 break;
3778                                 }
3779                         }
3780
3781 #ifdef HAVE_XDAMAGE
3782                         XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3783                         XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3784 #endif /* HAVE_XDAMAGE */
3785
3786                         /* XDBE doesn't seem to provide a way to clear the back buffer
3787                          * without interfering with the front buffer, other than passing
3788                          * XdbeBackground to XdbeSwapBuffers. That means that if we're
3789                          * using XDBE, we need to redraw the text even if it wasn't part of
3790                          * the exposed area. OTOH, if we're not going to call draw_stuff at
3791                          * all, then no swap happens and we can safely do nothing. */
3792
3793                         if (!XEmptyRegion(x11_stuff.region)) {
3794 #ifdef HAVE_XDBE
3795                                 if (use_xdbe) {
3796                                         XRectangle r;
3797
3798                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3799                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3800                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3801                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3802                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3803                                 }
3804 #endif
3805                                 XSetRegion(display, window.gc, x11_stuff.region);
3806 #ifdef XFT
3807                                 if (use_xft) {
3808                                         XftDrawSetClip(window.xftdraw, x11_stuff.region);
3809                                 }
3810 #endif
3811                                 draw_stuff();
3812                                 XDestroyRegion(x11_stuff.region);
3813                                 x11_stuff.region = XCreateRegion();
3814                         }
3815                 } else {
3816 #endif /* X11 */
3817                         t = (next_update_time - get_time()) * 1000000;
3818                         if(t > 0) usleep((useconds_t)t);
3819                         update_text();
3820                         draw_stuff();
3821 #ifdef NCURSES
3822                         if(output_methods & TO_NCURSES) {
3823                                 refresh();
3824                                 clear();
3825                         }
3826 #endif
3827 #ifdef X11
3828                 }
3829 #endif /* X11 */
3830
3831 #ifdef SIGNAL_BLOCKING
3832                 /* unblock signals of interest and let handler fly */
3833                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3834                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3835                 }
3836 #endif
3837
3838                 switch (g_signal_pending) {
3839                         case SIGHUP:
3840                         case SIGUSR1:
3841                                 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3842                                 reload_config();
3843                                 break;
3844                         case SIGINT:
3845                         case SIGTERM:
3846                                 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3847                                 terminate = 1;
3848 #ifdef X11
3849                                 if (output_methods & TO_X) {
3850                                         XDestroyRegion(x11_stuff.region);
3851                                         x11_stuff.region = NULL;
3852 #ifdef HAVE_XDAMAGE
3853                                         XDamageDestroy(display, x11_stuff.damage);
3854                                         XFixesDestroyRegion(display, x11_stuff.region2);
3855                                         XFixesDestroyRegion(display, x11_stuff.part);
3856 #endif /* HAVE_XDAMAGE */
3857                                         if (disp) {
3858                                                 free(disp);
3859                                         }
3860                                 }
3861 #endif /* X11 */
3862                                 if(overwrite_file) {
3863                                         free(overwrite_file);
3864                                         overwrite_file = 0;
3865                                 }
3866                                 if(append_file) {
3867                                         free(append_file);
3868                                         append_file = 0;
3869                                 }
3870                                 break;
3871                         default:
3872                                 /* Reaching here means someone set a signal
3873                                  * (SIGXXXX, signal_handler), but didn't write any code
3874                                  * to deal with it.
3875                                  * If you don't want to handle a signal, don't set a handler on
3876                                  * it in the first place. */
3877                                 if (g_signal_pending) {
3878                                         NORM_ERR("ignoring signal (%d)", g_signal_pending);
3879                                 }
3880                                 break;
3881                 }
3882 #ifdef HAVE_SYS_INOTIFY_H
3883                 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3884                         inotify_config_wd = inotify_add_watch(inotify_fd,
3885                                         current_config,
3886                                         IN_MODIFY);
3887                 }
3888                 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3889                         int len = 0, idx = 0;
3890                         fd_set descriptors;
3891                         struct timeval time_to_wait;
3892
3893                         FD_ZERO(&descriptors);
3894                         FD_SET(inotify_fd, &descriptors);
3895
3896                         time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3897
3898                         select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3899                         if (FD_ISSET(inotify_fd, &descriptors)) {
3900                                 /* process inotify events */
3901                                 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3902                                 while (len > 0 && idx < len) {
3903                                         struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3904                                         if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3905                                                 /* current_config should be reloaded */
3906                                                 NORM_ERR("'%s' modified, reloading...", current_config);
3907                                                 reload_config();
3908                                                 if (ev->mask & IN_IGNORED) {
3909                                                         /* for some reason we get IN_IGNORED here
3910                                                          * sometimes, so we need to re-add the watch */
3911                                                         inotify_config_wd = inotify_add_watch(inotify_fd,
3912                                                                         current_config,
3913                                                                         IN_MODIFY);
3914                                                 }
3915                                         }
3916 #ifdef HAVE_LUA
3917                                         else {
3918                                                 llua_inotify_query(ev->wd, ev->mask);
3919                                         }
3920 #endif /* HAVE_LUA */
3921                                         idx += INOTIFY_EVENT_SIZE + ev->len;
3922                                 }
3923                         }
3924                 }
3925 #endif /* HAVE_SYS_INOTIFY_H */
3926
3927 #ifdef HAVE_LUA
3928                 llua_update_info(&info, update_interval);
3929 #endif /* HAVE_LUA */
3930                 g_signal_pending = 0;
3931         }
3932         clean_up(NULL, NULL);
3933
3934 #ifdef HAVE_SYS_INOTIFY_H
3935         if (inotify_fd != -1) {
3936                 inotify_rm_watch(inotify_fd, inotify_config_wd);
3937                 close(inotify_fd);
3938                 inotify_fd = inotify_config_wd = 0;
3939         }
3940 #endif /* HAVE_SYS_INOTIFY_H */
3941 }
3942
3943 #ifdef X11
3944 static void load_config_file_x11(const char *);
3945 #endif /* X11 */
3946 void initialisation(int argc, char** argv);
3947
3948         /* reload the config file */
3949 static void reload_config(void)
3950 {
3951         char *current_config_copy = strdup(current_config);
3952         clean_up(NULL, NULL);
3953         current_config = current_config_copy;
3954         initialisation(argc_copy, argv_copy);
3955 }
3956
3957 void clean_up(void *memtofree1, void* memtofree2)
3958 {
3959         int i;
3960
3961 #ifdef NCURSES
3962         if(output_methods & TO_NCURSES) {
3963                 endwin();
3964         }
3965 #endif
3966         conftree_empty(currentconffile);
3967         currentconffile = NULL;
3968         if(memtofree1) {
3969                 free(memtofree1);
3970         }
3971         if(memtofree2) {
3972                 free(memtofree2);
3973         }
3974         timed_thread_destroy_registered_threads();
3975
3976         if (info.cpu_usage) {
3977                 free(info.cpu_usage);
3978                 info.cpu_usage = NULL;
3979         }
3980 #ifdef X11
3981         if (x_initialised == YES) {
3982                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3983                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3984                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3985                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3986                 destroy_window();
3987                 free_fonts();
3988                 if(x11_stuff.region) {
3989                         XDestroyRegion(x11_stuff.region);
3990                         x11_stuff.region = NULL;
3991                 }
3992                 XCloseDisplay(display);
3993                 display = NULL;
3994                 if(info.x11.desktop.all_names) {
3995                         free(info.x11.desktop.all_names);
3996                         info.x11.desktop.all_names = NULL;
3997                 }
3998                 if (info.x11.desktop.name) {
3999                         free(info.x11.desktop.name);
4000                         info.x11.desktop.name = NULL;
4001                 }
4002                 x_initialised = NO;
4003         }else{
4004                 free(fonts);    //in set_default_configurations a font is set but not loaded
4005                 font_count = -1;
4006         }
4007
4008 #endif /* X11 */
4009
4010         free_update_callbacks();
4011
4012         free_templates();
4013
4014         free_text_objects(&global_root_object, 0);
4015         if (tmpstring1) {
4016                 free(tmpstring1);
4017                 tmpstring1 = 0;
4018         }
4019         if (tmpstring2) {
4020                 free(tmpstring2);
4021                 tmpstring2 = 0;
4022         }
4023         if (text_buffer) {
4024                 free(text_buffer);
4025                 text_buffer = 0;
4026         }
4027
4028         if (global_text) {
4029                 free(global_text);
4030                 global_text = 0;
4031         }
4032
4033         free(current_config);
4034         current_config = 0;
4035
4036 #ifdef TCP_PORT_MONITOR
4037         tcp_portmon_clear();
4038 #endif
4039 #ifdef HAVE_CURL
4040         ccurl_free_info();
4041 #endif
4042 #ifdef RSS
4043         rss_free_info();
4044 #endif
4045 #ifdef WEATHER
4046         weather_free_info();
4047 #endif
4048 #ifdef HAVE_LUA
4049         llua_shutdown_hook();
4050         llua_close();
4051 #endif /* HAVE_LUA */
4052 #ifdef IMLIB2
4053         if (output_methods & TO_X)
4054                 cimlib_deinit();
4055 #endif /* IMLIB2 */
4056 #ifdef XOAP
4057         xmlCleanupParser();
4058 #endif /* XOAP */
4059
4060         if (specials) {
4061                 for (i = 0; i < special_count; i++) {
4062                         if (specials[i].type == GRAPH) {
4063                                 free(specials[i].graph);
4064                         }
4065                 }
4066                 free(specials);
4067                 specials = NULL;
4068         }
4069
4070         clear_net_stats();
4071         clear_diskio_stats();
4072         if(global_cpu != NULL) {
4073                 free(global_cpu);
4074                 global_cpu = NULL;
4075         }
4076 }
4077
4078 static int string_to_bool(const char *s)
4079 {
4080         if (!s) {
4081                 // Assumes an option without a true/false means true
4082                 return 1;
4083         } else if (strcasecmp(s, "yes") == EQUAL) {
4084                 return 1;
4085         } else if (strcasecmp(s, "true") == EQUAL) {
4086                 return 1;
4087         } else if (strcasecmp(s, "1") == EQUAL) {
4088                 return 1;
4089         }
4090         return 0;
4091 }
4092
4093 #ifdef X11
4094 static enum alignment string_to_alignment(const char *s)
4095 {
4096         if (strcasecmp(s, "top_left") == EQUAL) {
4097                 return TOP_LEFT;
4098         } else if (strcasecmp(s, "top_right") == EQUAL) {
4099                 return TOP_RIGHT;
4100         } else if (strcasecmp(s, "top_middle") == EQUAL) {
4101                 return TOP_MIDDLE;
4102         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4103                 return BOTTOM_LEFT;
4104         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4105                 return BOTTOM_RIGHT;
4106         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4107                 return BOTTOM_MIDDLE;
4108         } else if (strcasecmp(s, "middle_left") == EQUAL) {
4109                 return MIDDLE_LEFT;
4110         } else if (strcasecmp(s, "middle_right") == EQUAL) {
4111                 return MIDDLE_RIGHT;
4112         } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4113                 return MIDDLE_MIDDLE;
4114         } else if (strcasecmp(s, "tl") == EQUAL) {
4115                 return TOP_LEFT;
4116         } else if (strcasecmp(s, "tr") == EQUAL) {
4117                 return TOP_RIGHT;
4118         } else if (strcasecmp(s, "tm") == EQUAL) {
4119                 return TOP_MIDDLE;
4120         } else if (strcasecmp(s, "bl") == EQUAL) {
4121                 return BOTTOM_LEFT;
4122         } else if (strcasecmp(s, "br") == EQUAL) {
4123                 return BOTTOM_RIGHT;
4124         } else if (strcasecmp(s, "bm") == EQUAL) {
4125                 return BOTTOM_MIDDLE;
4126         } else if (strcasecmp(s, "ml") == EQUAL) {
4127                 return MIDDLE_LEFT;
4128         } else if (strcasecmp(s, "mr") == EQUAL) {
4129                 return MIDDLE_RIGHT;
4130         } else if (strcasecmp(s, "mm") == EQUAL) {
4131                 return MIDDLE_MIDDLE;
4132         } else if (strcasecmp(s, "none") == EQUAL) {
4133                 return NONE;
4134         }
4135         return TOP_LEFT;
4136 }
4137 #endif /* X11 */
4138
4139 #ifdef X11
4140 static void set_default_configurations_for_x(void)
4141 {
4142         default_fg_color = WhitePixel(display, screen);
4143         default_bg_color = BlackPixel(display, screen);
4144         default_out_color = BlackPixel(display, screen);
4145         color0 = default_fg_color;
4146         color1 = default_fg_color;
4147         color2 = default_fg_color;
4148         color3 = default_fg_color;
4149         color4 = default_fg_color;
4150         color5 = default_fg_color;
4151         color6 = default_fg_color;
4152         color7 = default_fg_color;
4153         color8 = default_fg_color;
4154         color9 = default_fg_color;
4155         current_text_color = default_fg_color;
4156 }
4157 #endif /* X11 */
4158
4159 static void set_default_configurations(void)
4160 {
4161 #ifdef MPD
4162         char *mpd_env_host;
4163         char *mpd_env_port;
4164 #endif
4165         update_uname();
4166         fork_to_background = 0;
4167         total_run_times = 0;
4168         info.cpu_avg_samples = 2;
4169         info.net_avg_samples = 2;
4170         info.diskio_avg_samples = 2;
4171         info.memmax = 0;
4172         top_cpu = 0;
4173         cpu_separate = 0;
4174         short_units = 0;
4175         format_human_readable = 1;
4176         top_mem = 0;
4177         top_time = 0;
4178 #ifdef IOSTATS
4179         top_io = 0;
4180 #endif
4181 #ifdef __linux__
4182         top_running = 0;
4183 #endif
4184 #ifdef MPD
4185         mpd_env_host = getenv("MPD_HOST");
4186         mpd_env_port = getenv("MPD_PORT");
4187
4188         if (!mpd_env_host || !strlen(mpd_env_host)) {
4189                 mpd_set_host("localhost");
4190         } else {
4191                 /* MPD_HOST environment variable is set */
4192                 char *mpd_hostpart = strchr(mpd_env_host, '@');
4193                 if (!mpd_hostpart) {
4194                         mpd_set_host(mpd_env_host);
4195                 } else {
4196                         /* MPD_HOST contains a password */
4197                         char mpd_password[mpd_hostpart - mpd_env_host + 1];
4198                         snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4199
4200                         if (!strlen(mpd_hostpart + 1)) {
4201                                 mpd_set_host("localhost");
4202                         } else {
4203                                 mpd_set_host(mpd_hostpart + 1);
4204                         }
4205
4206                         mpd_set_password(mpd_password, 1);
4207                 }
4208         }
4209
4210
4211         if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4212                 /* failed to set port from environment variable */
4213                 mpd_set_port("6600");
4214         }
4215 #endif
4216 #ifdef XMMS2
4217         info.xmms2.artist = NULL;
4218         info.xmms2.album = NULL;
4219         info.xmms2.title = NULL;
4220         info.xmms2.genre = NULL;
4221         info.xmms2.comment = NULL;
4222         info.xmms2.url = NULL;
4223         info.xmms2.status = NULL;
4224         info.xmms2.playlist = NULL;
4225 #endif
4226         use_spacer = NO_SPACER;
4227 #ifdef X11
4228         output_methods = TO_X;
4229 #else
4230         output_methods = TO_STDOUT;
4231 #endif
4232 #ifdef X11
4233         show_graph_scale = 0;
4234         show_graph_range = 0;
4235         draw_shades = 1;
4236         draw_borders = 0;
4237         draw_graph_borders = 1;
4238         draw_outline = 0;
4239         set_first_font("6x10");
4240         gap_x = 5;
4241         gap_y = 60;
4242         minimum_width = 5;
4243         minimum_height = 5;
4244         maximum_width = 0;
4245 #ifdef OWN_WINDOW
4246         own_window = 0;
4247         window.type = TYPE_NORMAL;
4248         window.hints = 0;
4249         strcpy(window.class_name, PACKAGE_NAME);
4250         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4251 #endif
4252         stippled_borders = 0;
4253         window.border_inner_margin = 3;
4254         window.border_outer_margin = 1;
4255         window.border_width = 1;
4256         text_alignment = BOTTOM_LEFT;
4257         info.x11.monitor.number = 1;
4258         info.x11.monitor.current = 0;
4259         info.x11.desktop.current = 1; 
4260         info.x11.desktop.number = 1;
4261         info.x11.desktop.nitems = 0;
4262         info.x11.desktop.all_names = NULL; 
4263         info.x11.desktop.name = NULL; 
4264 #endif /* X11 */
4265
4266         free_templates();
4267
4268         free(current_mail_spool);
4269         {
4270                 char buf[256];
4271
4272                 variable_substitute(MAIL_FILE, buf, 256);
4273                 if (buf[0] != '\0') {
4274                         current_mail_spool = strndup(buf, text_buffer_size);
4275                 }
4276         }
4277
4278         no_buffers = 1;
4279         set_update_interval(3);
4280         update_interval_bat = NOBATTERY;
4281         info.music_player_interval = 1.0;
4282         stuff_in_uppercase = 0;
4283         info.users.number = 1;
4284
4285 #ifdef TCP_PORT_MONITOR
4286         /* set default connection limit */
4287         tcp_portmon_set_max_connections(0);
4288 #endif
4289 }
4290
4291 /* returns 1 if you can overwrite or create the file at 'path' */
4292 static _Bool overwrite_works(const char *path)
4293 {
4294         FILE *filepointer;
4295
4296         if (!(filepointer = fopen(path, "w")))
4297                 return 0;
4298         fclose(filepointer);
4299         return 1;
4300 }
4301
4302 /* returns 1 if you can append or create the file at 'path' */
4303 static _Bool append_works(const char *path)
4304 {
4305         FILE *filepointer;
4306
4307         if (!(filepointer = fopen(path, "a")))
4308                 return 0;
4309         fclose(filepointer);
4310         return 1;
4311 }
4312
4313 #ifdef X11
4314 #ifdef DEBUG
4315 /* WARNING, this type not in Xlib spec */
4316 int x11_error_handler(Display *d, XErrorEvent *err)
4317         __attribute__((noreturn));
4318 int x11_error_handler(Display *d, XErrorEvent *err)
4319 {
4320         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",
4321                         err->type,
4322                         (long unsigned)err->display,
4323                         (long)err->resourceid,
4324                         err->serial,
4325                         err->error_code,
4326                         err->request_code,
4327                         err->minor_code,
4328                         (long unsigned)d
4329                         );
4330         abort();
4331 }
4332
4333 int x11_ioerror_handler(Display *d)
4334         __attribute__((noreturn));
4335 int x11_ioerror_handler(Display *d)
4336 {
4337         NORM_ERR("X Error: Display %lx\n",
4338                         (long unsigned)d
4339                         );
4340         abort();
4341 }
4342 #endif /* DEBUG */
4343
4344 static void X11_initialisation(void)
4345 {
4346         if (x_initialised == YES) return;
4347         output_methods |= TO_X;
4348         init_X11(disp);
4349         set_default_configurations_for_x();
4350         x_initialised = YES;
4351 #ifdef DEBUG
4352         _Xdebug = 1;
4353         /* WARNING, this type not in Xlib spec */
4354         XSetErrorHandler(&x11_error_handler);
4355         XSetIOErrorHandler(&x11_ioerror_handler);
4356 #endif /* DEBUG */
4357 }
4358
4359 static char **xargv = 0;
4360 static int xargc = 0;
4361
4362 static void X11_create_window(void)
4363 {
4364         if (output_methods & TO_X) {
4365 #ifdef OWN_WINDOW
4366                 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4367                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4368                                 xargv, xargc);
4369 #else /* OWN_WINDOW */
4370                 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4371                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4372                                 xargv, xargc);
4373 #endif /* OWN_WINDOW */
4374
4375                 setup_fonts();
4376                 load_fonts();
4377                 update_text_area();     /* to position text/window on screen */
4378
4379 #ifdef OWN_WINDOW
4380                 if (own_window && !fixed_pos) {
4381                         XMoveWindow(display, window.window, window.x, window.y);
4382                 }
4383                 if (own_window) {
4384                         set_transparent_background(window.window);
4385                 }
4386 #endif
4387
4388                 create_gc();
4389
4390                 draw_stuff();
4391
4392                 x11_stuff.region = XCreateRegion();
4393 #ifdef HAVE_XDAMAGE
4394                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4395                         NORM_ERR("Xdamage extension unavailable");
4396                 }
4397                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4398                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4399                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4400 #endif /* HAVE_XDAMAGE */
4401
4402                 selected_font = 0;
4403                 update_text_area();     /* to get initial size of the window */
4404         }
4405 #ifdef HAVE_LUA
4406         /* setup lua window globals */
4407         llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4408 #endif /* HAVE_LUA */
4409 }
4410 #endif /* X11 */
4411
4412 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4413 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4414 #define CONF2(a) if (strcasecmp(name, a) == 0)
4415 #define CONF(a) else CONF2(a)
4416 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4417                 || strcasecmp(name, b) == 0)
4418 #define CONF_CONTINUE 1
4419 #define CONF_BREAK 2
4420 #define CONF_BUFF_SIZE 512
4421
4422 static FILE *open_config_file(const char *f)
4423 {
4424 #ifdef CONFIG_OUTPUT
4425         if (!strcmp(f, "==builtin==")) {
4426                 return conf_cookie_open();
4427         } else
4428 #endif /* CONFIG_OUTPUT */
4429                 return fopen(f, "r");
4430 }
4431
4432 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4433 {
4434         char *p, *p2;
4435         (*line)++;
4436         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4437                 return CONF_BREAK;
4438         }
4439         remove_comments(buf);
4440
4441         p = buf;
4442
4443         /* skip spaces */
4444         while (*p && isspace((int) *p)) {
4445                 p++;
4446         }
4447         if (*p == '\0') {
4448                 return CONF_CONTINUE;   /* empty line */
4449         }
4450
4451         *name = p;
4452
4453         /* skip name */
4454         p2 = p;
4455         while (*p2 && !isspace((int) *p2)) {
4456                 p2++;
4457         }
4458         if (*p2 != '\0') {
4459                 *p2 = '\0';     /* break at name's end */
4460                 p2++;
4461         }
4462
4463         /* get value */
4464         if (*p2) {
4465                 p = p2;
4466                 while (*p && isspace((int) *p)) {
4467                         p++;
4468                 }
4469
4470                 *value = p;
4471
4472                 p2 = *value + strlen(*value);
4473                 while (isspace((int) *(p2 - 1))) {
4474                         *--p2 = '\0';
4475                 }
4476         } else {
4477                 *value = 0;
4478         }
4479         return 0;
4480 }
4481
4482 char load_config_file(const char *f)
4483 {
4484         int line = 0;
4485         FILE *fp;
4486
4487         fp = open_config_file(f);
4488         if (!fp) {
4489                 return FALSE;
4490         }
4491         DBGP("reading contents from config file '%s'", f);
4492
4493         while (!feof(fp)) {
4494                 char buff[CONF_BUFF_SIZE], *name, *value;
4495                 int ret = do_config_step(&line, fp, buff, &name, &value);
4496                 if (ret == CONF_BREAK) {
4497                         break;
4498                 } else if (ret == CONF_CONTINUE) {
4499                         continue;
4500                 }
4501
4502 #ifdef X11
4503                 CONF2("out_to_x") {
4504                         /* don't listen if X is already initialised or
4505                          * if we already know we don't want it */
4506                         if(x_initialised != YES) {
4507                                 if (string_to_bool(value)) {
4508                                         output_methods &= TO_X;
4509                                 } else {
4510                                         output_methods &= ~TO_X;
4511                                         x_initialised = NEVER;
4512                                 }
4513                         }
4514                 }
4515                 CONF("display") {
4516                         if (!value || x_initialised == YES) {
4517                                 CONF_ERR;
4518                         } else {
4519                                 if (disp)
4520                                         free(disp);
4521                                 disp = strdup(value);
4522                         }
4523                 }
4524                 CONF("alignment") {
4525 #ifdef OWN_WINDOW
4526                         if (window.type == TYPE_DOCK)
4527                                 ;
4528                         else
4529 #endif /*OWN_WINDOW */
4530                         if (value) {
4531                                 int a = string_to_alignment(value);
4532
4533                                 if (a <= 0) {
4534                                         CONF_ERR;
4535                                 } else {
4536                                         text_alignment = a;
4537                                 }
4538                         } else {
4539                                 CONF_ERR;
4540                         }
4541                 }
4542                 CONF("background") {
4543                         fork_to_background = string_to_bool(value);
4544                 }
4545 #else
4546                 CONF2("background") {
4547                         fork_to_background = string_to_bool(value);
4548                 }
4549 #endif /* X11 */
4550 #ifdef X11
4551                 CONF("show_graph_scale") {
4552                         show_graph_scale = string_to_bool(value);
4553                 }
4554                 CONF("show_graph_range") {
4555                         show_graph_range = string_to_bool(value);
4556                 }
4557                 CONF("border_inner_margin") {
4558                         if (value) {
4559                                 window.border_inner_margin = strtol(value, 0, 0);
4560                                 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4561                         } else {
4562                                 CONF_ERR;
4563                         }
4564                 }
4565                 CONF("border_outer_margin") {
4566                         if (value) {
4567                                 window.border_outer_margin = strtol(value, 0, 0);
4568                                 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4569                         } else {
4570                                 CONF_ERR;
4571                         }
4572                 }
4573                 CONF("border_width") {
4574                         if (value) {
4575                                 window.border_width = strtol(value, 0, 0);
4576                                 if (window.border_width < 0) window.border_width = 0;
4577                         } else {
4578                                 CONF_ERR;
4579                         }
4580                 }
4581 #endif /* X11 */
4582 #define TEMPLATE_CONF(n) \
4583                 CONF("template"#n) { \
4584                         if (set_template(n, value)) \
4585                                 CONF_ERR; \
4586                 }
4587                 TEMPLATE_CONF(0)
4588                 TEMPLATE_CONF(1)
4589                 TEMPLATE_CONF(2)
4590                 TEMPLATE_CONF(3)
4591                 TEMPLATE_CONF(4)
4592                 TEMPLATE_CONF(5)
4593                 TEMPLATE_CONF(6)
4594                 TEMPLATE_CONF(7)
4595                 TEMPLATE_CONF(8)
4596                 TEMPLATE_CONF(9)
4597                 CONF("imap") {
4598                         if (value) {
4599                                 parse_global_imap_mail_args(value);
4600                         } else {
4601                                 CONF_ERR;
4602                         }
4603                 }
4604                 CONF("pop3") {
4605                         if (value) {
4606                                 parse_global_pop3_mail_args(value);
4607                         } else {
4608                                 CONF_ERR;
4609                         }
4610                 }
4611                 CONF("default_bar_size") {
4612                         char err = 0;
4613                         if (value) {
4614                                 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4615                                         err = 1;
4616                                 }
4617                         } else {
4618                                 err = 1;
4619                         }
4620                         if (err) {
4621                                 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4622                         }
4623                 }
4624 #ifdef X11
4625                 CONF("default_graph_size") {
4626                         char err = 0;
4627                         if (value) {
4628                                 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4629                                         err = 1;
4630                                 }
4631                         } else {
4632                                 err = 1;
4633                         }
4634                         if (err) {
4635                                 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4636                         }
4637                 }
4638                 CONF("default_gauge_size") {
4639                         char err = 0;
4640                         if (value) {
4641                                 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4642                                         err = 1;
4643                                 }
4644                         } else {
4645                                 err = 1;
4646                         }
4647                         if (err) {
4648                                 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4649                         }
4650                 }
4651 #endif
4652 #ifdef MPD
4653                 CONF("mpd_host") {
4654                         if (value) {
4655                                 mpd_set_host(value);
4656                         } else {
4657                                 CONF_ERR;
4658                         }
4659                 }
4660                 CONF("mpd_port") {
4661                         if (value && mpd_set_port(value)) {
4662                                 CONF_ERR;
4663                         }
4664                 }
4665                 CONF("mpd_password") {
4666                         if (value) {
4667                                 mpd_set_password(value, 0);
4668                         } else {
4669                                 CONF_ERR;
4670                         }
4671                 }
4672 #endif
4673                 CONF("music_player_interval") {
4674                         if (value) {
4675                                 info.music_player_interval = strtod(value, 0);
4676                         } else {
4677                                 CONF_ERR;
4678                         }
4679                 }
4680 #ifdef __OpenBSD__
4681                 CONF("sensor_device") {
4682                         if (value) {
4683                                 sensor_device = strtol(value, 0, 0);
4684                         } else {
4685                                 CONF_ERR;
4686                         }
4687                 }
4688 #endif
4689                 CONF("cpu_avg_samples") {
4690                         if (value) {
4691                                 cpu_avg_samples = strtol(value, 0, 0);
4692                                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4693                                         CONF_ERR;
4694                                 } else {
4695                                         info.cpu_avg_samples = cpu_avg_samples;
4696                                 }
4697                         } else {
4698                                 CONF_ERR;
4699                         }
4700                 }
4701                 CONF("net_avg_samples") {
4702                         if (value) {
4703                                 net_avg_samples = strtol(value, 0, 0);
4704                                 if (net_avg_samples < 1 || net_avg_samples > 14) {
4705                                         CONF_ERR;
4706                                 } else {
4707                                         info.net_avg_samples = net_avg_samples;
4708                                 }
4709                         } else {
4710                                 CONF_ERR;
4711                         }
4712                 }
4713                 CONF("diskio_avg_samples") {
4714                         if (value) {
4715                                 diskio_avg_samples = strtol(value, 0, 0);
4716                                 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4717                                         CONF_ERR;
4718                                 } else {
4719                                         info.diskio_avg_samples = diskio_avg_samples;
4720                                 }
4721                         } else {
4722                                 CONF_ERR;
4723                         }
4724                 }
4725
4726 #ifdef HAVE_XDBE
4727                 CONF("double_buffer") {
4728                         use_xdbe = string_to_bool(value);
4729                 }
4730 #endif
4731 #ifdef X11
4732                 CONF("override_utf8_locale") {
4733                         utf8_mode = string_to_bool(value);
4734                 }
4735                 CONF("draw_borders") {
4736                         draw_borders = string_to_bool(value);
4737                 }
4738                 CONF("draw_graph_borders") {
4739                         draw_graph_borders = string_to_bool(value);
4740                 }
4741                 CONF("draw_shades") {
4742                         draw_shades = string_to_bool(value);
4743                 }
4744                 CONF("draw_outline") {
4745                         draw_outline = string_to_bool(value);
4746                 }
4747 #endif /* X11 */
4748                 CONF("out_to_console") {
4749                         if(string_to_bool(value)) {
4750                                 output_methods |= TO_STDOUT;
4751                         } else {
4752                                 output_methods &= ~TO_STDOUT;
4753                         }
4754                 }
4755                 CONF("extra_newline") {
4756                         extra_newline = string_to_bool(value);
4757                 }
4758                 CONF("out_to_stderr") {
4759                         if(string_to_bool(value))
4760                                 output_methods |= TO_STDERR;
4761                 }
4762 #ifdef NCURSES
4763                 CONF("out_to_ncurses") {
4764                         if(string_to_bool(value)) {
4765                                 initscr();
4766                                 start_color();
4767                                 output_methods |= TO_NCURSES;
4768                         }
4769                 }
4770 #endif
4771                 CONF("overwrite_file") {
4772                         if(overwrite_file) {
4773                                 free(overwrite_file);
4774                                 overwrite_file = 0;
4775                         }
4776                         if(overwrite_works(value)) {
4777                                 overwrite_file = strdup(value);
4778                                 output_methods |= OVERWRITE_FILE;
4779                         } else
4780                                 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4781                 }
4782                 CONF("append_file") {
4783                         if(append_file) {
4784                                 free(append_file);
4785                                 append_file = 0;
4786                         }
4787                         if(append_works(value)) {
4788                                 append_file = strdup(value);
4789                                 output_methods |= APPEND_FILE;
4790                         } else
4791                                 NORM_ERR("append_file won't be able to create/append '%s'", value);
4792                 }
4793                 CONF("use_spacer") {
4794                         if (value) {
4795                                 if (strcasecmp(value, "left") == EQUAL) {
4796                                         use_spacer = LEFT_SPACER;
4797                                 } else if (strcasecmp(value, "right") == EQUAL) {
4798                                         use_spacer = RIGHT_SPACER;
4799                                 } else if (strcasecmp(value, "none") == EQUAL) {
4800                                         use_spacer = NO_SPACER;
4801                                 } else {
4802                                         use_spacer = string_to_bool(value);
4803                                         NORM_ERR("use_spacer should have an argument of left, right, or"
4804                                                 " none.  '%s' seems to be some form of '%s', so"
4805                                                 " defaulting to %s.", value,
4806                                                 use_spacer ? "true" : "false",
4807                                                 use_spacer ? "right" : "none");
4808                                         if (use_spacer) {
4809                                                 use_spacer = RIGHT_SPACER;
4810                                         } else {
4811                                                 use_spacer = NO_SPACER;
4812                                         }
4813                                 }
4814                         } else {
4815                                 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4816                                 use_spacer = RIGHT_SPACER;
4817                         }
4818                 }
4819 #ifdef X11
4820 #ifdef XFT
4821                 CONF("use_xft") {
4822                         use_xft = string_to_bool(value);
4823                 }
4824                 CONF("font") {
4825                         if (value) {
4826                                 set_first_font(value);
4827                         }
4828                 }
4829                 CONF("xftalpha") {
4830                         if (value && font_count >= 0) {
4831                                 fonts[0].font_alpha = atof(value) * 65535.0;
4832                         }
4833                 }
4834                 CONF("xftfont") {
4835                         if (use_xft) {
4836 #else
4837                 CONF("use_xft") {
4838                         if (string_to_bool(value)) {
4839                                 NORM_ERR("Xft not enabled at compile time");
4840                         }
4841                 }
4842                 CONF("xftfont") {
4843                         /* xftfont silently ignored when no Xft */
4844                 }
4845                 CONF("xftalpha") {
4846                         /* xftalpha is silently ignored when no Xft */
4847                 }
4848                 CONF("font") {
4849 #endif
4850                         if (value) {
4851                                 set_first_font(value);
4852                         }
4853 #ifdef XFT
4854                         }
4855 #endif
4856                 }
4857                 CONF("gap_x") {
4858                         if (value) {
4859                                 gap_x = atoi(value);
4860                         } else {
4861                                 CONF_ERR;
4862                         }
4863                 }
4864                 CONF("gap_y") {
4865                         if (value) {
4866                                 gap_y = atoi(value);
4867                         } else {
4868                                 CONF_ERR;
4869                         }
4870                 }
4871 #endif /* X11 */
4872                 CONF("mail_spool") {
4873                         if (value) {
4874                                 char buffer[256];
4875
4876                                 variable_substitute(value, buffer, 256);
4877
4878                                 if (buffer[0] != '\0') {
4879                                         if (current_mail_spool) {
4880                                                 free(current_mail_spool);
4881                                         }
4882                                         current_mail_spool = strndup(buffer, text_buffer_size);
4883                                 }
4884                         } else {
4885                                 CONF_ERR;
4886                         }
4887                 }
4888 #ifdef X11
4889                 CONF("minimum_size") {
4890                         if (value) {
4891                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4892                                                 != 2) {
4893                                         if (sscanf(value, "%d", &minimum_width) != 1) {
4894                                                 CONF_ERR;
4895                                         }
4896                                 }
4897                         } else {
4898                                 CONF_ERR;
4899                         }
4900                 }
4901                 CONF("maximum_width") {
4902                         if (value) {
4903                                 if (sscanf(value, "%d", &maximum_width) != 1) {
4904                                         CONF_ERR;
4905                                 }
4906                         } else {
4907                                 CONF_ERR;
4908                         }
4909                 }
4910 #endif /* X11 */
4911                 CONF("no_buffers") {
4912                         no_buffers = string_to_bool(value);
4913                 }
4914                 CONF("top_name_width") {
4915                         if (value) {
4916                                 if (sscanf(value, "%u", &top_name_width) != 1) {
4917                                         CONF_ERR;
4918                                 }
4919                         } else {
4920                                 CONF_ERR;
4921                         }
4922                         if (top_name_width >= max_user_text) {
4923                                 top_name_width = max_user_text - 1;
4924                         }
4925                 }
4926                 CONF("top_cpu_separate") {
4927                         cpu_separate = string_to_bool(value);
4928                 }
4929                 CONF("short_units") {
4930                         short_units = string_to_bool(value);
4931                 }
4932                 CONF("format_human_readable") {
4933                         format_human_readable = string_to_bool(value);
4934                 }
4935 #ifdef HDDTEMP
4936                 CONF("hddtemp_host") {
4937                         set_hddtemp_host(value);
4938                 }
4939                 CONF("hddtemp_port") {
4940                         set_hddtemp_port(value);
4941                 }
4942 #endif /* HDDTEMP */
4943                 CONF("pad_percents") {
4944                         pad_percents = atoi(value);
4945                 }
4946 #ifdef X11
4947 #ifdef OWN_WINDOW
4948                 CONF("own_window") {
4949                         if (value) {
4950                                 own_window = string_to_bool(value);
4951                         }
4952                 }
4953                 CONF("own_window_class") {
4954                         if (value) {
4955                                 memset(window.class_name, 0, sizeof(window.class_name));
4956                                 strncpy(window.class_name, value,
4957                                                 sizeof(window.class_name) - 1);
4958                         }
4959                 }
4960                 CONF("own_window_title") {
4961                         if (value) {
4962                                 memset(window.title, 0, sizeof(window.title));
4963                                 strncpy(window.title, value, sizeof(window.title) - 1);
4964                         }
4965                 }
4966                 CONF("own_window_transparent") {
4967                         if (value) {
4968                                 set_transparent = string_to_bool(value);
4969                         }
4970                 }
4971                 CONF("own_window_hints") {
4972                         if (value) {
4973                                 char *p_hint, *p_save;
4974                                 char delim[] = ", ";
4975
4976                                 /* tokenize the value into individual hints */
4977                                 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4978                                         do {
4979                                                 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4980                                                 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4981                                                         SET_HINT(window.hints, HINT_UNDECORATED);
4982                                                 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4983                                                         SET_HINT(window.hints, HINT_BELOW);
4984                                                 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4985                                                         SET_HINT(window.hints, HINT_ABOVE);
4986                                                 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4987                                                         SET_HINT(window.hints, HINT_STICKY);
4988                                                 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4989                                                         SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4990                                                 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4991                                                         SET_HINT(window.hints, HINT_SKIP_PAGER);
4992                                                 } else {
4993                                                         CONF_ERR;
4994                                                 }
4995
4996                                                 p_hint = strtok_r(NULL, delim, &p_save);
4997                                         } while (p_hint != NULL);
4998                                 }
4999                         } else {
5000                                 CONF_ERR;
5001                         }
5002                 }
5003                 CONF("own_window_type") {
5004                         if (value) {
5005                                 if (strncmp(value, "normal", 6) == EQUAL) {
5006                                         window.type = TYPE_NORMAL;
5007                                 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5008                                         window.type = TYPE_DESKTOP;
5009                                 } else if (strncmp(value, "dock", 4) == EQUAL) {
5010                                         window.type = TYPE_DOCK;
5011                                         text_alignment = TOP_LEFT;
5012                                 } else if (strncmp(value, "panel", 5) == EQUAL) {
5013                                         window.type = TYPE_PANEL;
5014                                 } else if (strncmp(value, "override", 8) == EQUAL) {
5015                                         window.type = TYPE_OVERRIDE;
5016                                 } else {
5017                                         CONF_ERR;
5018                                 }
5019                         } else {
5020                                 CONF_ERR;
5021                         }
5022                 }
5023 #endif
5024                 CONF("stippled_borders") {
5025                         if (value) {
5026                                 stippled_borders = strtol(value, 0, 0);
5027                         } else {
5028                                 stippled_borders = 4;
5029                         }
5030                 }
5031 #ifdef IMLIB2
5032                 CONF("imlib_cache_size") {
5033                         if (value) {
5034                                 cimlib_set_cache_size(atoi(value));
5035                         }
5036                 }
5037                 CONF("imlib_cache_flush_interval") {
5038                         if (value) {
5039                                 cimlib_set_cache_flush_interval(atoi(value));
5040                         }
5041                 }
5042 #endif /* IMLIB2 */
5043 #endif /* X11 */
5044                 CONF("update_interval_on_battery") {
5045                         if (value) {
5046                                 update_interval_bat = strtod(value, 0);
5047                         } else {
5048                                 CONF_ERR;
5049                         }
5050                 }
5051                 CONF("update_interval") {
5052                         if (value) {
5053                                 set_update_interval(strtod(value, 0));
5054                         } else {
5055                                 CONF_ERR;
5056                         }
5057                         if (info.music_player_interval == 0) {
5058                                 // default to update_interval
5059                                 info.music_player_interval = update_interval;
5060                         }
5061                 }
5062                 CONF("total_run_times") {
5063                         if (value) {
5064                                 total_run_times = strtod(value, 0);
5065                         } else {
5066                                 CONF_ERR;
5067                         }
5068                 }
5069                 CONF("uppercase") {
5070                         stuff_in_uppercase = string_to_bool(value);
5071                 }
5072                 CONF("max_specials") {
5073                         if (value) {
5074                                 max_specials = atoi(value);
5075                         } else {
5076                                 CONF_ERR;
5077                         }
5078                 }
5079                 CONF("max_user_text") {
5080                         if (value) {
5081                                 max_user_text = atoi(value);
5082                         } else {
5083                                 CONF_ERR;
5084                         }
5085                 }
5086                 CONF("text_buffer_size") {
5087                         if (value) {
5088                                 text_buffer_size = atoi(value);
5089                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5090                                         NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5091                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5092                                 }
5093                         } else {
5094                                 CONF_ERR;
5095                         }
5096                 }
5097                 CONF("text") {
5098 #ifdef X11
5099                         if (output_methods & TO_X) {
5100                                 X11_initialisation();
5101                         }
5102 #endif
5103
5104                         if (global_text) {
5105                                 free(global_text);
5106                                 global_text = 0;
5107                         }
5108
5109                         global_text = (char *) malloc(1);
5110                         global_text[0] = '\0';
5111
5112                         while (!feof(fp)) {
5113                                 unsigned int l = strlen(global_text);
5114                                 unsigned int bl;
5115                                 char buf[CONF_BUFF_SIZE];
5116
5117                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5118                                         break;
5119                                 }
5120
5121                                 /* Remove \\-\n. */
5122                                 bl = strlen(buf);
5123                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5124                                         buf[bl-2] = '\0';
5125                                         bl -= 2;
5126                                         if (bl == 0) {
5127                                                 continue;
5128                                         }
5129                                 }
5130
5131                                 /* Check for continuation of \\-\n. */
5132                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5133                                         global_text[l-1] = '\0';
5134                                         continue;
5135                                 }
5136
5137                                 global_text = (char *) realloc(global_text, l + bl + 1);
5138                                 strcat(global_text, buf);
5139
5140                                 if (strlen(global_text) > max_user_text) {
5141                                         break;
5142                                 }
5143                         }
5144                         global_text_lines = line + 1;
5145                         break;
5146                 }
5147 #ifdef TCP_PORT_MONITOR
5148                 CONF("max_port_monitor_connections") {
5149                         int max;
5150                         if (!value || (sscanf(value, "%d", &max) != 1)) {
5151                                 /* an error. use default, warn and continue. */
5152                                 tcp_portmon_set_max_connections(0);
5153                                 CONF_ERR;
5154                         } else if (tcp_portmon_set_max_connections(max)) {
5155                                 /* max is < 0, default has been set*/
5156                                 CONF_ERR;
5157                         }
5158                 }
5159 #endif
5160                 CONF("if_up_strictness") {
5161                         if (!value) {
5162                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5163                                 ifup_strictness = IFUP_UP;
5164                         } else if (strcasecmp(value, "up") == EQUAL) {
5165                                 ifup_strictness = IFUP_UP;
5166                         } else if (strcasecmp(value, "link") == EQUAL) {
5167                                 ifup_strictness = IFUP_LINK;
5168                         } else if (strcasecmp(value, "address") == EQUAL) {
5169                                 ifup_strictness = IFUP_ADDR;
5170                         } else {
5171                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5172                                 ifup_strictness = IFUP_UP;
5173                         }
5174                 }
5175
5176                 CONF("temperature_unit") {
5177                         if (!value) {
5178                                 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5179                         } else if (set_temp_output_unit(value)) {
5180                                 NORM_ERR("temperature_unit: incorrect argument");
5181                         }
5182                 }
5183
5184 #ifdef HAVE_LUA
5185                 CONF("lua_load") {
5186                         if (value) {
5187                                 char *ptr = strtok(value, " ");
5188                                 while (ptr) {
5189                                         llua_load(ptr);
5190                                         ptr = strtok(NULL, " ");
5191                                 }
5192                         } else {
5193                                 CONF_ERR;
5194                         }
5195                 }
5196 #ifdef X11
5197                 CONF("lua_draw_hook_pre") {
5198                         if (value) {
5199                                 llua_set_draw_pre_hook(value);
5200                         } else {
5201                                 CONF_ERR;
5202                         }
5203                 }
5204                 CONF("lua_draw_hook_post") {
5205                         if (value) {
5206                                 llua_set_draw_post_hook(value);
5207                         } else {
5208                                 CONF_ERR;
5209                         }
5210                 }
5211                 CONF("lua_startup_hook") {
5212                         if (value) {
5213                                 llua_set_startup_hook(value);
5214                         } else {
5215                                 CONF_ERR;
5216                         }
5217                 }
5218                 CONF("lua_shutdown_hook") {
5219                         if (value) {
5220                                 llua_set_shutdown_hook(value);
5221                         } else {
5222                                 CONF_ERR;
5223                         }
5224                 }
5225 #endif /* X11 */
5226 #endif /* HAVE_LUA */
5227
5228                 CONF("color0"){}
5229                 CONF("color1"){}
5230                 CONF("color2"){}
5231                 CONF("color3"){}
5232                 CONF("color4"){}
5233                 CONF("color5"){}
5234                 CONF("color6"){}
5235                 CONF("color7"){}
5236                 CONF("color8"){}
5237                 CONF("color9"){}
5238                 CONF("default_color"){}
5239                 CONF3("default_shade_color", "default_shadecolor"){}
5240                 CONF3("default_outline_color", "default_outlinecolor") {}
5241                 CONF("own_window_colour") {}
5242
5243                 else {
5244                         NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5245                 }
5246         }
5247
5248         fclose(fp);
5249
5250         if (info.music_player_interval == 0) {
5251                 // default to update_interval
5252                 info.music_player_interval = update_interval;
5253         }
5254         if (!global_text) { // didn't supply any text
5255                 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5256         }
5257         if (!output_methods) {
5258                 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5259         }
5260 #if defined(NCURSES)
5261 #if defined(X11)
5262         if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5263                 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5264                 output_methods &= ~TO_NCURSES;
5265                 endwin();
5266         }
5267 #endif /* X11 */
5268         if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5269                 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5270                 output_methods &= ~(TO_STDOUT | TO_STDERR);
5271         }
5272 #endif /* NCURSES */
5273         return TRUE;
5274 }
5275
5276 #ifdef X11
5277 static void load_config_file_x11(const char *f)
5278 {
5279         int line = 0;
5280         FILE *fp;
5281
5282         fp = open_config_file(f);
5283         if (!fp) {
5284                 return;
5285         }
5286         DBGP("reading contents from config file '%s'", f);
5287
5288         while (!feof(fp)) {
5289                 char buff[CONF_BUFF_SIZE], *name, *value;
5290                 int ret = do_config_step(&line, fp, buff, &name, &value);
5291                 if (ret == CONF_BREAK) {
5292                         break;
5293                 } else if (ret == CONF_CONTINUE) {
5294                         continue;
5295                 }
5296
5297                 CONF2("color0") {
5298                         X11_initialisation();
5299                         if (x_initialised == YES) {
5300                                 if (value) {
5301                                         color0 = get_x11_color(value);
5302                                 } else {
5303                                         CONF_ERR;
5304                                 }
5305                         }
5306                 }
5307                 CONF("color1") {
5308                         X11_initialisation();
5309                         if (x_initialised == YES) {
5310                                 if (value) {
5311                                         color1 = get_x11_color(value);
5312                                 } else {
5313                                         CONF_ERR;
5314                                 }
5315                         }
5316                 }
5317                 CONF("color2") {
5318                         X11_initialisation();
5319                         if (x_initialised == YES) {
5320                                 if (value) {
5321                                         color2 = get_x11_color(value);
5322                                 } else {
5323                                         CONF_ERR;
5324                                 }
5325                         }
5326                 }
5327                 CONF("color3") {
5328                         X11_initialisation();
5329                         if (x_initialised == YES) {
5330                                 if (value) {
5331                                         color3 = get_x11_color(value);
5332                                 } else {
5333                                         CONF_ERR;
5334                                 }
5335                         }
5336                 }
5337                 CONF("color4") {
5338                         X11_initialisation();
5339                         if (x_initialised == YES) {
5340                                 if (value) {
5341                                         color4 = get_x11_color(value);
5342                                 } else {
5343                                         CONF_ERR;
5344                                 }
5345                         }
5346                 }
5347                 CONF("color5") {
5348                         X11_initialisation();
5349                         if (x_initialised == YES) {
5350                                 if (value) {
5351                                         color5 = get_x11_color(value);
5352                                 } else {
5353                                         CONF_ERR;
5354                                 }
5355                         }
5356                 }
5357                 CONF("color6") {
5358                         X11_initialisation();
5359                         if (x_initialised == YES) {
5360                                 if (value) {
5361                                         color6 = get_x11_color(value);
5362                                 } else {
5363                                         CONF_ERR;
5364                                 }
5365                         }
5366                 }
5367                 CONF("color7") {
5368                         X11_initialisation();
5369                         if (x_initialised == YES) {
5370                                 if (value) {
5371                                         color7 = get_x11_color(value);
5372                                 } else {
5373                                         CONF_ERR;
5374                                 }
5375                         }
5376                 }
5377                 CONF("color8") {
5378                         X11_initialisation();
5379                         if (x_initialised == YES) {
5380                                 if (value) {
5381                                         color8 = get_x11_color(value);
5382                                 } else {
5383                                         CONF_ERR;
5384                                 }
5385                         }
5386                 }
5387                 CONF("color9") {
5388                         X11_initialisation();
5389                         if (x_initialised == YES) {
5390                                 if (value) {
5391                                         color9 = get_x11_color(value);
5392                                 } else {
5393                                         CONF_ERR;
5394                                 }
5395                         }
5396                 }
5397                 CONF("default_color") {
5398                         X11_initialisation();
5399                         if (x_initialised == YES) {
5400                                 if (value) {
5401                                         default_fg_color = get_x11_color(value);
5402                                 } else {
5403                                         CONF_ERR;
5404                                 }
5405                         }
5406                 }
5407                 CONF3("default_shade_color", "default_shadecolor") {
5408                         X11_initialisation();
5409                         if (x_initialised == YES) {
5410                                 if (value) {
5411                                         default_bg_color = get_x11_color(value);
5412                                 } else {
5413                                         CONF_ERR;
5414                                 }
5415                         }
5416                 }
5417                 CONF3("default_outline_color", "default_outlinecolor") {
5418                         X11_initialisation();
5419                         if (x_initialised == YES) {
5420                                 if (value) {
5421                                         default_out_color = get_x11_color(value);
5422                                 } else {
5423                                         CONF_ERR;
5424                                 }
5425                         }
5426                 }
5427 #ifdef OWN_WINDOW
5428                 CONF("own_window_colour") {
5429                         X11_initialisation();
5430                         if (x_initialised == YES) {
5431                                 if (value) {
5432                                         background_colour = get_x11_color(value);
5433                                 } else {
5434                                         NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5435                                                 "'#' for hex colours");
5436                                 }
5437                         }
5438                 }
5439 #endif
5440                 CONF("text") {
5441                         /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5442                         if(output_methods & TO_X) {
5443                                 X11_initialisation();
5444                         }
5445                 }
5446 #undef CONF
5447 #undef CONF2
5448 #undef CONF3
5449 #undef CONF_ERR
5450 #undef CONF_ERR2
5451 #undef CONF_BREAK
5452 #undef CONF_CONTINUE
5453 #undef CONF_BUFF_SIZE
5454         }
5455
5456         fclose(fp);
5457
5458 }
5459 #endif /* X11 */
5460
5461 static void print_help(const char *prog_name) {
5462         printf("Usage: %s [OPTION]...\n"
5463                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5464                         "window. Command line options will override configurations defined in config\n"
5465                         "file.\n"
5466                         "   -v, --version             version\n"
5467                         "   -q, --quiet               quiet mode\n"
5468                         "   -D, --debug               increase debugging output, ie. -DD for more debugging\n"
5469                         "   -c, --config=FILE         config file to load\n"
5470 #ifdef CONFIG_OUTPUT
5471                         "   -C, --print-config        print the builtin default config to stdout\n"
5472                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5473 #endif
5474                         "   -d, --daemonize           daemonize, fork to background\n"
5475                         "   -h, --help                help\n"
5476 #ifdef X11
5477                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5478                         "   -f, --font=FONT           font to use\n"
5479                         "   -X, --display=DISPLAY     X11 display to use\n"
5480 #ifdef OWN_WINDOW
5481                         "   -o, --own-window          create own window to draw\n"
5482 #endif
5483 #ifdef HAVE_XDBE
5484                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
5485 #endif
5486                         "   -w, --window-id=WIN_ID    window id to draw\n"
5487                         "   -x X                      x position\n"
5488                         "   -y Y                      y position\n"
5489 #endif /* X11 */
5490                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
5491                         "   -u, --interval=SECS       update interval\n"
5492                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n"
5493                         "   -p, --pause=SECS          pause for SECS seconds at startup before doing anything\n",
5494                         prog_name
5495         );
5496 }
5497
5498 /* : means that character before that takes an argument */
5499 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5500 #ifdef X11
5501         "x:y:w:a:f:X:"
5502 #ifdef OWN_WINDOW
5503         "o"
5504 #endif
5505 #ifdef HAVE_XDBE
5506         "b"
5507 #endif
5508 #endif /* X11 */
5509 #ifdef CONFIG_OUTPUT
5510         "C"
5511 #endif
5512         ;
5513
5514 static const struct option longopts[] = {
5515         { "help", 0, NULL, 'h' },
5516         { "version", 0, NULL, 'V' },
5517         { "debug", 0, NULL, 'D' },
5518         { "config", 1, NULL, 'c' },
5519 #ifdef CONFIG_OUTPUT
5520         { "print-config", 0, NULL, 'C' },
5521 #endif
5522         { "daemonize", 0, NULL, 'd' },
5523 #ifdef X11
5524         { "alignment", 1, NULL, 'a' },
5525         { "font", 1, NULL, 'f' },
5526         { "display", 1, NULL, 'X' },
5527 #ifdef OWN_WINDOW
5528         { "own-window", 0, NULL, 'o' },
5529 #endif
5530 #ifdef HAVE_XDBE
5531         { "double-buffer", 0, NULL, 'b' },
5532 #endif
5533         { "window-id", 1, NULL, 'w' },
5534 #endif /* X11 */
5535         { "text", 1, NULL, 't' },
5536         { "interval", 0, NULL, 'u' },
5537         { "pause", 0, NULL, 'p' },
5538         { 0, 0, 0, 0 }
5539 };
5540
5541 void initialisation(int argc, char **argv) {
5542         struct sigaction act, oact;
5543
5544         set_default_configurations();
5545         load_config_file(current_config);
5546         currentconffile = conftree_add(currentconffile, current_config);
5547
5548         /* init specials array */
5549         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5550                 NORM_ERR("failed to create specials array");
5551         }
5552
5553 #ifdef MAIL_FILE
5554         if (current_mail_spool == NULL) {
5555                 char buf[256];
5556
5557                 variable_substitute(MAIL_FILE, buf, 256);
5558
5559                 if (buf[0] != '\0') {
5560                         current_mail_spool = strndup(buf, text_buffer_size);
5561                 }
5562         }
5563 #endif
5564
5565         /* handle other command line arguments */
5566
5567 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5568                 || defined(__NetBSD__)
5569         optind = optreset = 1;
5570 #else
5571         optind = 0;
5572 #endif
5573
5574 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5575         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5576                         "kvm_open")) == NULL) {
5577                 CRIT_ERR(NULL, NULL, "cannot read kvm");
5578         }
5579 #endif
5580
5581         while (1) {
5582                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5583                 int startup_pause;
5584
5585                 if (c == -1) {
5586                         break;
5587                 }
5588
5589                 switch (c) {
5590                         case 'd':
5591                                 fork_to_background = 1;
5592                                 break;
5593                         case 'D':
5594                                 global_debug_level++;
5595                                 break;
5596 #ifdef X11
5597                         case 'f':
5598                                 set_first_font(optarg);
5599                                 break;
5600                         case 'a':
5601                                 text_alignment = string_to_alignment(optarg);
5602                                 break;
5603
5604 #ifdef OWN_WINDOW
5605                         case 'o':
5606                                 own_window = 1;
5607                                 break;
5608 #endif
5609 #ifdef HAVE_XDBE
5610                         case 'b':
5611                                 use_xdbe = 1;
5612                                 break;
5613 #endif
5614 #endif /* X11 */
5615                         case 't':
5616                                 if (global_text) {
5617                                         free(global_text);
5618                                         global_text = 0;
5619                                 }
5620                                 global_text = strndup(optarg, max_user_text);
5621                                 convert_escapes(global_text);
5622                                 break;
5623
5624                         case 'u':
5625                                 update_interval = strtod(optarg, 0);
5626                                 update_interval_old = update_interval;
5627                                 if (info.music_player_interval == 0) {
5628                                         // default to update_interval
5629                                         info.music_player_interval = update_interval;
5630                                 }
5631                                 break;
5632
5633                         case 'i':
5634                                 total_run_times = strtod(optarg, 0);
5635                                 break;
5636 #ifdef X11
5637                         case 'x':
5638                                 gap_x = atoi(optarg);
5639                                 break;
5640
5641                         case 'y':
5642                                 gap_y = atoi(optarg);
5643                                 break;
5644 #endif /* X11 */
5645                         case 'p':
5646                                 startup_pause = atoi(optarg);
5647                                 sleep(startup_pause);
5648                                 break;
5649
5650                         case '?':
5651                                 exit(EXIT_FAILURE);
5652                 }
5653         }
5654
5655 #ifdef X11
5656         /* load font */
5657         if (output_methods & TO_X) {
5658                 load_config_file_x11(current_config);
5659         }
5660 #endif /* X11 */
5661
5662         /* generate text and get initial size */
5663         extract_variable_text(global_text);
5664         if (global_text) {
5665                 free(global_text);
5666                 global_text = 0;
5667         }
5668         global_text = NULL;
5669         /* fork */
5670         if (fork_to_background) {
5671                 int pid = fork();
5672
5673                 switch (pid) {
5674                         case -1:
5675                                 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5676                                         strerror(errno));
5677                                 break;
5678
5679                         case 0:
5680                                 /* child process */
5681                                 usleep(25000);
5682                                 fprintf(stderr, "\n");
5683                                 fflush(stderr);
5684                                 break;
5685
5686                         default:
5687                                 /* parent process */
5688                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5689                                         pid);
5690                                 fflush(stderr);
5691                                 exit(EXIT_SUCCESS);
5692                 }
5693         }
5694
5695         start_update_threading();
5696
5697         text_buffer = malloc(max_user_text);
5698         memset(text_buffer, 0, max_user_text);
5699         tmpstring1 = malloc(text_buffer_size);
5700         memset(tmpstring1, 0, text_buffer_size);
5701         tmpstring2 = malloc(text_buffer_size);
5702         memset(tmpstring2, 0, text_buffer_size);
5703
5704 #ifdef X11
5705         xargc = argc;
5706         xargv = argv;
5707         X11_create_window();
5708 #endif /* X11 */
5709 #ifdef HAVE_LUA
5710         llua_setup_info(&info, update_interval);
5711 #endif /* HAVE_LUA */
5712 #ifdef XOAP
5713         xmlInitParser();
5714 #endif /* XOAP */
5715
5716         /* Set signal handlers */
5717         act.sa_handler = signal_handler;
5718         sigemptyset(&act.sa_mask);
5719         act.sa_flags = 0;
5720 #ifdef SA_RESTART
5721         act.sa_flags |= SA_RESTART;
5722 #endif
5723
5724         if (            sigaction(SIGINT,  &act, &oact) < 0
5725                         ||      sigaction(SIGALRM, &act, &oact) < 0
5726                         ||      sigaction(SIGUSR1, &act, &oact) < 0
5727                         ||      sigaction(SIGHUP,  &act, &oact) < 0
5728                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
5729                 NORM_ERR("error setting signal handler: %s", strerror(errno));
5730         }
5731
5732 #ifdef HAVE_LUA
5733         llua_startup_hook();
5734 #endif /* HAVE_LUA */
5735 }
5736
5737 int main(int argc, char **argv)
5738 {
5739 #ifdef X11
5740         char *s, *temp;
5741         unsigned int x;
5742 #endif
5743
5744         argc_copy = argc;
5745         argv_copy = argv;
5746         g_signal_pending = 0;
5747         max_user_text = MAX_USER_TEXT_DEFAULT;
5748         current_config = 0;
5749         memset(&info, 0, sizeof(info));
5750         free_templates();
5751         clear_net_stats();
5752
5753 #ifdef TCP_PORT_MONITOR
5754         /* set default connection limit */
5755         tcp_portmon_set_max_connections(0);
5756 #endif
5757
5758         /* handle command line parameters that don't change configs */
5759 #ifdef X11
5760         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5761                         || ((s = getenv("LANG")) && *s)) {
5762                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5763                 if (temp == NULL) {
5764                         NORM_ERR("malloc failed");
5765                 }
5766                 for (x = 0; x < strlen(s); x++) {
5767                         temp[x] = tolower(s[x]);
5768                 }
5769                 temp[x] = 0;
5770                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5771                         utf8_mode = 1;
5772                 }
5773
5774                 free(temp);
5775         }
5776         if (!setlocale(LC_CTYPE, "")) {
5777                 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5778         }
5779 #endif /* X11 */
5780         while (1) {
5781                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5782
5783                 if (c == -1) {
5784                         break;
5785                 }
5786
5787                 switch (c) {
5788                         case 'v':
5789                         case 'V':
5790                                 print_version();
5791                         case 'c':
5792                                 if (current_config) {
5793                                         free(current_config);
5794                                 }
5795                                 current_config = strndup(optarg, max_user_text);
5796                                 break;
5797                         case 'q':
5798                                 freopen("/dev/null", "w", stderr);
5799                                 break;
5800                         case 'h':
5801                                 print_help(argv[0]);
5802                                 return 0;
5803 #ifdef CONFIG_OUTPUT
5804                         case 'C':
5805                                 print_defconfig();
5806                                 return 0;
5807 #endif
5808 #ifdef X11
5809                         case 'w':
5810                                 window.window = strtol(optarg, 0, 0);
5811                                 break;
5812                         case 'X':
5813                                 if (disp)
5814                                         free(disp);
5815                                 disp = strdup(optarg);
5816                                 break;
5817 #endif /* X11 */
5818
5819                         case '?':
5820                                 exit(EXIT_FAILURE);
5821                 }
5822         }
5823
5824         /* check if specified config file is valid */
5825         if (current_config) {
5826                 struct stat sb;
5827                 if (stat(current_config, &sb) ||
5828                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5829                         NORM_ERR("invalid configuration file '%s'\n", current_config);
5830                         free(current_config);
5831                         current_config = 0;
5832                 }
5833         }
5834
5835         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5836
5837         if (!current_config) {
5838                 /* load default config file */
5839                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5840                 FILE *fp;
5841
5842                 /* Try to use personal config file first */
5843                 to_real_path(buf, CONFIG_FILE);
5844                 if (buf[0] && (fp = fopen(buf, "r"))) {
5845                         current_config = strndup(buf, max_user_text);
5846                         fclose(fp);
5847                 }
5848
5849                 /* Try to use system config file if personal config not readable */
5850                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5851                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5852                         fclose(fp);
5853                 }
5854
5855                 /* No readable config found */
5856                 if (!current_config) {
5857 #ifdef CONFIG_OUTPUT
5858                         current_config = strdup("==builtin==");
5859                         NORM_ERR("no readable personal or system-wide config file found,"
5860                                         " using builtin default");
5861 #else
5862                         CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5863 #endif /* ! CONF_OUTPUT */
5864                 }
5865         }
5866
5867 #ifdef XOAP
5868         /* Load xoap keys, if existing */
5869         load_xoap_keys();
5870 #endif /* XOAP */
5871
5872 #ifdef HAVE_SYS_INOTIFY_H
5873         inotify_fd = inotify_init1(IN_NONBLOCK);
5874 #endif /* HAVE_SYS_INOTIFY_H */
5875
5876         initialisation(argc, argv);
5877
5878         main_loop();
5879
5880 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5881         kvm_close(kd);
5882 #endif
5883
5884         return 0;
5885
5886 }
5887
5888 void alarm_handler(void) {
5889         if(childpid > 0) {
5890                 kill(childpid, SIGTERM);
5891         }
5892 }
5893
5894 static void signal_handler(int sig)
5895 {
5896         /* signal handler is light as a feather, as it should be.
5897          * we will poll g_signal_pending with each loop of conky
5898          * and do any signal processing there, NOT here (except 
5899          * SIGALRM because this is caused when conky is hanging) */
5900         if(sig == SIGALRM) {
5901                 alarm_handler();
5902         } else {
5903                 g_signal_pending = sig;
5904         }
5905 }