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