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