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