Move vi modelines closer to the beginning, so they're more likely to be actually...
[monky] / src / conky.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "text_object.h"
33 #include "conky.h"
34 #include "common.h"
35 #include <stdarg.h>
36 #include <math.h>
37 #include <time.h>
38 #include <locale.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include <limits.h>
42 #if HAVE_DIRENT_H
43 #include <dirent.h>
44 #endif
45 #include <sys/time.h>
46 #include <sys/param.h>
47 #ifdef HAVE_SYS_INOTIFY_H
48 #include <sys/inotify.h>
49 #endif /* HAVE_SYS_INOTIFY_H */
50 #ifdef X11
51 #include "x11.h"
52 #include <X11/Xutil.h>
53 #ifdef HAVE_XDAMAGE
54 #include <X11/extensions/Xdamage.h>
55 #endif
56 #ifdef IMLIB2
57 #include "imlib2.h"
58 #endif /* IMLIB2 */
59 #endif /* X11 */
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <netinet/in.h>
63 #include <netdb.h>
64 #include <fcntl.h>
65 #include <getopt.h>
66 #ifdef NCURSES
67 #include <ncurses.h>
68 #endif
69 #ifdef XOAP
70 #include <libxml/parser.h>
71 #endif /* XOAP */
72
73 /* local headers */
74 #include "core.h"
75 #include "algebra.h"
76 #include "build.h"
77 #include "colours.h"
78 #include "diskio.h"
79 #ifdef X11
80 #include "fonts.h"
81 #endif
82 #include "fs.h"
83 #include "logging.h"
84 #include "mixer.h"
85 #include "mail.h"
86 #include "mboxscan.h"
87 #include "specials.h"
88 #include "temphelper.h"
89 #include "template.h"
90 #include "tailhead.h"
91 #include "top.h"
92
93 /* check for OS and include appropriate headers */
94 #if defined(__linux__)
95 #include "linux.h"
96 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
97 #include "freebsd.h"
98 #elif defined(__OpenBSD__)
99 #include "openbsd.h"
100 #endif
101
102 #if defined(__FreeBSD_kernel__)
103 #include <bsd/bsd.h>
104 #endif
105
106 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
107 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
108                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
109 int apm_getinfo(int fd, apm_info_t aip);
110 char *get_apm_adapter(void);
111 char *get_apm_battery_life(void);
112 char *get_apm_battery_time(void);
113 #endif
114
115 #ifdef CONFIG_OUTPUT
116 #include "defconfig.h"
117 #include "conf_cookie.h"
118 #endif
119
120 #ifndef S_ISSOCK
121 #define S_ISSOCK(x)   ((x & S_IFMT) == S_IFSOCK)
122 #endif
123
124 #define MAIL_FILE "$MAIL"
125 #define MAX_IF_BLOCK_DEPTH 5
126
127 //#define SIGNAL_BLOCKING
128 #undef SIGNAL_BLOCKING
129
130 /* debugging level, used by logging.h */
131 int global_debug_level = 0;
132
133 /* two strings for internal use */
134 static char *tmpstring1, *tmpstring2;
135
136 /* variables holding various config settings */
137 int short_units;
138 int format_human_readable;
139 int cpu_separate;
140 enum {
141         NO_SPACER = 0,
142         LEFT_SPACER,
143         RIGHT_SPACER
144 } use_spacer;
145 int top_cpu, top_mem, top_time;
146 #ifdef IOSTATS
147 int top_io;
148 #endif
149 #ifdef __linux__
150 int top_running;
151 #endif
152 static unsigned int top_name_width = 15;
153 int output_methods;
154 static int extra_newline;
155 enum x_initialiser_state x_initialised = NO;
156 static volatile int g_signal_pending;
157 /* Update interval */
158 double update_interval;
159 double update_interval_old;
160 double update_interval_bat;
161 void *global_cpu = NULL;
162 pid_t childpid = 0;
163
164 int argc_copy;
165 char** argv_copy;
166
167 /* prototypes for internally used functions */
168 static void signal_handler(int);
169 static void print_version(void) __attribute__((noreturn));
170 static void reload_config(void);
171 static void generate_text_internal(char *, int, struct text_object,
172                                    struct information *);
173
174 static void print_version(void)
175 {
176         printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
177
178         printf("\nCompiled in features:\n\n"
179                    "System config file: "SYSTEM_CONFIG_FILE"\n"
180                    "Package library path: "PACKAGE_LIBDIR"\n\n"
181 #ifdef X11
182                    " X11:\n"
183 # ifdef HAVE_XDAMAGE
184                    "  * Xdamage extension\n"
185 # endif /* HAVE_XDAMAGE */
186 # ifdef HAVE_XDBE
187                    "  * XDBE (double buffer extension)\n"
188 # endif /* HAVE_XDBE */
189 # ifdef XFT
190                    "  * Xft\n"
191 # endif /* XFT */
192 #endif /* X11 */
193                    "\n Music detection:\n"
194 #ifdef AUDACIOUS
195                    "  * Audacious\n"
196 #endif /* AUDACIOUS */
197 #ifdef BMPX
198                    "  * BMPx\n"
199 #endif /* BMPX */
200 #ifdef MPD
201                    "  * MPD\n"
202 #endif /* MPD */
203 #ifdef MOC
204                    "  * MOC\n"
205 #endif /* MOC */
206 #ifdef XMMS2
207                    "  * XMMS2\n"
208 #endif /* XMMS2 */
209                    "\n General:\n"
210 #ifdef HAVE_OPENMP
211                    "  * OpenMP\n"
212 #endif /* HAVE_OPENMP */
213 #ifdef MATH
214                    "  * math\n"
215 #endif /* Math */
216 #ifdef HDDTEMP
217                    "  * hddtemp\n"
218 #endif /* HDDTEMP */
219 #ifdef TCP_PORT_MONITOR
220                    "  * portmon\n"
221 #endif /* TCP_PORT_MONITOR */
222 #ifdef HAVE_CURL
223                    "  * Curl\n"
224 #endif /* HAVE_CURL */
225 #ifdef RSS
226                    "  * RSS\n"
227 #endif /* RSS */
228 #ifdef WEATHER
229                    "  * Weather (METAR)\n"
230 #ifdef XOAP
231                    "  * Weather (XOAP)\n"
232 #endif /* XOAP */
233 #endif /* WEATHER */
234 #ifdef HAVE_IWLIB
235                    "  * wireless\n"
236 #endif /* HAVE_IWLIB */
237 #ifdef IBM
238                    "  * support for IBM/Lenovo notebooks\n"
239 #endif /* IBM */
240 #ifdef NVIDIA
241                    "  * nvidia\n"
242 #endif /* NVIDIA */
243 #ifdef EVE
244                    "  * eve-online\n"
245 #endif /* EVE */
246 #ifdef CONFIG_OUTPUT
247                    "  * config-output\n"
248 #endif /* CONFIG_OUTPUT */
249 #ifdef IMLIB2
250                    "  * Imlib2\n"
251 #endif /* IMLIB2 */
252 #ifdef MIXER_IS_ALSA
253                    "  * ALSA mixer support\n"
254 #endif /* MIXER_IS_ALSA */
255 #ifdef APCUPSD
256                    "  * apcupsd\n"
257 #endif /* APCUPSD */
258 #ifdef IOSTATS
259                    "  * iostats\n"
260 #endif /* IOSTATS */
261 #ifdef HAVE_LUA
262                    "  * Lua\n"
263                    "\n  Lua bindings:\n"
264 #ifdef HAVE_LUA_CAIRO
265                    "   * Cairo\n"
266 #endif /* HAVE_LUA_CAIRO */
267 #ifdef HAVE_LUA_IMLIB2
268                    "   * Imlib2\n"
269 #endif /* IMLIB2 */
270 #endif /* HAVE_LUA */
271         );
272
273         exit(EXIT_SUCCESS);
274 }
275
276 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
277
278
279 #ifdef X11
280
281 static void X11_create_window(void);
282 static void X11_initialisation(void);
283
284 struct _x11_stuff_s {
285         Region region;
286 #ifdef HAVE_XDAMAGE
287         Damage damage;
288         XserverRegion region2, part;
289         int event_base, error_base;
290 #endif
291 } x11_stuff;
292
293 /* text size */
294
295 static int text_start_x, text_start_y;  /* text start position in window */
296 static int text_width, text_height;
297
298 /* alignments */
299 enum alignment {
300         TOP_LEFT = 1,
301         TOP_RIGHT,
302         TOP_MIDDLE,
303         BOTTOM_LEFT,
304         BOTTOM_RIGHT,
305         BOTTOM_MIDDLE,
306         MIDDLE_LEFT,
307         MIDDLE_RIGHT,
308         NONE
309 };
310
311 /* display to connect to */
312 static char *disp = NULL;
313
314 #endif /* X11 */
315
316 /* struct that has all info to be shared between
317  * instances of the same text object */
318 struct information info;
319
320 /* path to config file */
321 char *current_config;
322
323 /* set to 1 if you want all text to be in uppercase */
324 static unsigned int stuff_in_uppercase;
325
326 /* Run how many times? */
327 static unsigned long total_run_times;
328
329 /* fork? */
330 static int fork_to_background;
331
332 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
333
334 /* filenames for output */
335 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
336 char *append_file = NULL; FILE *append_fpointer = NULL;
337
338 #ifdef X11
339
340 static int show_graph_scale;
341 static int show_graph_range;
342
343 /* Position on the screen */
344 static int text_alignment;
345 static int gap_x, gap_y;
346
347 /* border */
348 static int draw_borders;
349 static int draw_graph_borders;
350 static int stippled_borders;
351
352 int get_stippled_borders(void)
353 {
354         return stippled_borders;
355 }
356
357 static int draw_shades, draw_outline;
358
359 long default_fg_color, default_bg_color, default_out_color;
360
361 /* create own window or draw stuff to root? */
362 static int set_transparent = 0;
363
364 #ifdef OWN_WINDOW
365 static int own_window = 0;
366 static int background_colour = 0;
367
368 /* fixed size/pos is set if wm/user changes them */
369 static int fixed_size = 0, fixed_pos = 0;
370 #endif
371
372 static int minimum_width, minimum_height;
373 static int maximum_width;
374
375 #endif /* X11 */
376
377 #ifdef __OpenBSD__
378 static int sensor_device;
379 #endif
380
381 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
382          color9;
383
384 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
385 unsigned int max_user_text;
386
387 /* maximum size of individual text buffers, ie $exec buffer size */
388 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
389
390 /* UTF-8 */
391 int utf8_mode = 0;
392
393 /* no buffers in used memory? */
394 int no_buffers;
395
396 /* pad percentages to decimals? */
397 static int pad_percents = 0;
398
399 static char *global_text = 0;
400
401 char *get_global_text(void)
402 {
403         return global_text;
404 }
405
406 long global_text_lines;
407
408 static int total_updates;
409 static int updatereset;
410
411 void set_updatereset(int i)
412 {
413         updatereset = i;
414 }
415
416 int get_updatereset(void)
417 {
418         return updatereset;
419 }
420
421 int check_contains(char *f, char *s)
422 {
423         int ret = 0;
424         FILE *where = open_file(f, 0);
425
426         if (where) {
427                 char buf1[256];
428
429                 while (fgets(buf1, 256, where)) {
430                         if (strstr(buf1, s)) {
431                                 ret = 1;
432                                 break;
433                         }
434                 }
435                 fclose(where);
436         } else {
437                 NORM_ERR("Could not open the file");
438         }
439         return ret;
440 }
441
442 #define SECRIT_MULTILINE_CHAR '\x02'
443
444 #ifdef X11
445
446 static inline int calc_text_width(const char *s, int l)
447 {
448         if ((output_methods & TO_X) == 0) {
449                 return 0;
450         }
451 #ifdef XFT
452         if (use_xft) {
453                 XGlyphInfo gi;
454
455                 if (utf8_mode) {
456                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
457                                 (const FcChar8 *) s, l, &gi);
458                 } else {
459                         XftTextExtents8(display, fonts[selected_font].xftfont,
460                                 (const FcChar8 *) s, l, &gi);
461                 }
462                 return gi.xOff;
463         } else
464 #endif
465         {
466                 return XTextWidth(fonts[selected_font].font, s, l);
467         }
468 }
469 #endif /* X11 */
470
471 /* formatted text to render on screen, generated in generate_text(),
472  * drawn in draw_stuff() */
473
474 static char *text_buffer;
475
476 /* quite boring functions */
477
478 static inline void for_each_line(char *b, int f(char *, int))
479 {
480         char *ps, *pe;
481         int special_index = 0; /* specials index */
482
483         for (ps = b, pe = b; *pe; pe++) {
484                 if (*pe == '\n') {
485                         *pe = '\0';
486                         special_index = f(ps, special_index);
487                         *pe = '\n';
488                         ps = pe + 1;
489                 }
490         }
491
492         if (ps < pe) {
493                 f(ps, special_index);
494         }
495 }
496
497 static void convert_escapes(char *buf)
498 {
499         char *p = buf, *s = buf;
500
501         while (*s) {
502                 if (*s == '\\') {
503                         s++;
504                         if (*s == 'n') {
505                                 *p++ = '\n';
506                         } else if (*s == '\\') {
507                                 *p++ = '\\';
508                         }
509                         s++;
510                 } else {
511                         *p++ = *s++;
512                 }
513         }
514         *p = '\0';
515 }
516
517 /* Prints anything normally printed with snprintf according to the current value
518  * of use_spacer.  Actually slightly more flexible than snprintf, as you can
519  * safely specify the destination buffer as one of your inputs.  */
520 int spaced_print(char *buf, int size, const char *format, int width, ...)
521 {
522         int len = 0;
523         va_list argp;
524         char *tempbuf;
525
526         if (size < 1) {
527                 return 0;
528         }
529         tempbuf = malloc(size * sizeof(char));
530
531         // Passes the varargs along to vsnprintf
532         va_start(argp, width);
533         vsnprintf(tempbuf, size, format, argp);
534         va_end(argp);
535
536         switch (use_spacer) {
537                 case NO_SPACER:
538                         len = snprintf(buf, size, "%s", tempbuf);
539                         break;
540                 case LEFT_SPACER:
541                         len = snprintf(buf, size, "%*s", width, tempbuf);
542                         break;
543                 case RIGHT_SPACER:
544                         len = snprintf(buf, size, "%-*s", width, tempbuf);
545                         break;
546         }
547         free(tempbuf);
548         return len;
549 }
550
551 /* print percentage values
552  *
553  * - i.e., unsigned values between 0 and 100
554  * - respect the value of pad_percents */
555 static int percent_print(char *buf, int size, unsigned value)
556 {
557         return spaced_print(buf, size, "%u", pad_percents, value);
558 }
559
560 /* converts from bytes to human readable format (K, M, G, T)
561  *
562  * The algorithm always divides by 1024, as unit-conversion of byte
563  * counts suggests. But for output length determination we need to
564  * compare with 1000 here, as we print in decimal form. */
565 static void human_readable(long long num, char *buf, int size)
566 {
567         const char **suffix = suffixes;
568         float fnum;
569         int precision;
570         int width;
571         const char *format;
572
573         /* Possibly just output as usual, for example for stdout usage */
574         if (!format_human_readable) {
575                 spaced_print(buf, size, "%d", 6, round_to_int(num));
576                 return;
577         }
578         if (short_units) {
579                 width = 5;
580                 format = "%.*f%.1s";
581         } else {
582                 width = 7;
583                 format = "%.*f%-3s";
584         }
585
586         if (llabs(num) < 1000LL) {
587                 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
588                 return;
589         }
590
591         while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
592                 num /= 1024;
593                 suffix++;
594         }
595
596         suffix++;
597         fnum = num / 1024.0;
598
599         /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
600          *
601          * The goal is to always have a significance of 3, by
602          * adjusting the decimal part of the number. Sample output:
603          *  123MiB
604          * 23.4GiB
605          * 5.12B   
606          * so the point of alignment resides between number and unit. The
607          * upside of this is that there is minimal padding necessary, though
608          * there should be a way to make alignment take place at the decimal
609          * dot (then with fixed width decimal part). 
610          *
611          * Note the repdigits below: when given a precision value, printf()
612          * rounds the float to it, not just cuts off the remaining digits. So
613          * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
614          * printed with a precision of 0. Yay. */
615
616         precision = 0;          /* print 100-999 without decimal part */
617         if (fnum < 99.95)
618                 precision = 1;  /* print 10-99 with one decimal place */
619         if (fnum < 9.995)
620                 precision = 2;  /* print 0-9 with two decimal places */
621
622         spaced_print(buf, size, format, width, precision, fnum, *suffix);
623 }
624
625 /* global object list root element */
626 static struct text_object global_root_object;
627
628 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
629 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
630 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
631 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
632         int ends[2];
633         int parentend, childend;
634
635         //by running pipe after the strcmp's we make sure that we don't have to create a pipe
636         //and close the ends if mode is something illegal
637         if(strcmp(mode, "r") == 0) {
638                 if(pipe(ends) != 0) {
639                         return NULL;
640                 }
641                 parentend = ends[0];
642                 childend = ends[1];
643         } else if(strcmp(mode, "w") == 0) {
644                 if(pipe(ends) != 0) {
645                         return NULL;
646                 }
647                 parentend = ends[1];
648                 childend = ends[0];
649         } else {
650                 return NULL;
651         }
652         *child = fork();
653         if(*child == -1) {
654                 close(parentend);
655                 close(childend);
656                 return NULL;
657         } else if(*child > 0) {
658                 close(childend);
659                 waitpid(*child, NULL, 0);
660         } else {
661                 //don't read from both stdin and pipe or write to both stdout and pipe
662                 if(childend == ends[0]) {
663                         close(0);
664                 } else {
665                         close(1);
666                 }
667                 dup(childend);  //by dupping childend, the returned fd will have close-on-exec turned off
668                 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
669                 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail) 
670         }
671         return fdopen(parentend, mode);
672 }
673
674 static inline void read_exec(const char *data, char *buf, const int size)
675 {
676         FILE *fp;
677
678         alarm(update_interval);
679         fp = pid_popen(data, "r", &childpid);
680         if(fp) {
681                 int length;
682
683                 length = fread(buf, 1, size, fp);
684                 pclose(fp);
685                 buf[length] = '\0';
686                 if (length > 0 && buf[length - 1] == '\n') {
687                         buf[length - 1] = '\0';
688                 }
689         } else {
690                 buf[0] = '\0';
691         }
692         alarm(0);
693 }
694
695 void do_read_exec(const char *data, char *buf, const int size)
696 {
697         read_exec(data, buf, size);
698 }
699
700 void *threaded_exec(void *) __attribute__((noreturn));
701
702 void *threaded_exec(void *arg)
703 {
704         char *buff, *p2;
705         struct text_object *obj = (struct text_object *)arg;
706
707         while (1) {
708                 buff = malloc(text_buffer_size);
709                 read_exec(obj->data.texeci.cmd, buff,
710                         text_buffer_size);
711                 p2 = buff;
712                 while (*p2) {
713                         if (*p2 == '\001') {
714                                 *p2 = ' ';
715                         }
716                         p2++;
717                 }
718                 timed_thread_lock(obj->data.texeci.p_timed_thread);
719                 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
720                 timed_thread_unlock(obj->data.texeci.p_timed_thread);
721                 free(buff);
722                 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
723                         timed_thread_exit(obj->data.texeci.p_timed_thread);
724                 }
725         }
726         /* never reached */
727 }
728
729 static long current_text_color;
730
731 void set_current_text_color(long colour)
732 {
733         current_text_color = colour;
734 }
735
736 long get_current_text_color(void)
737 {
738         return current_text_color;
739 }
740
741 //adds newstring to to the tree unless you can already see it when travelling back.
742 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
743 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
744         struct conftree* node;
745         struct conftree* node2;
746
747         for(node = previous; node != NULL; node = node->back) {
748                 if(strcmp(node->string, newstring) == 0) {
749                         return NULL;
750                 }
751         }
752         node = malloc(sizeof(struct conftree));
753         if (previous != NULL) {
754                 if(previous->vert_next == NULL) {
755                         previous->vert_next = node;
756                 } else {
757                         for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
758                         node2->horz_next = node;
759                 }
760         }
761         node->string = strdup(newstring);
762         node->horz_next = NULL;
763         node->vert_next = NULL;
764         node->back = previous;
765         return node;
766 }
767
768 void conftree_empty(struct conftree* tree) {
769         if(tree) {
770                 conftree_empty(tree->horz_next);
771                 conftree_empty(tree->vert_next);
772                 free(tree->string);
773                 free(tree);
774         }
775 }
776
777 struct conftree *currentconffile;
778
779 static void extract_variable_text(const char *p)
780 {
781         free_text_objects(&global_root_object, 0);
782         if (tmpstring1) {
783                 free(tmpstring1);
784                 tmpstring1 = 0;
785         }
786         if (tmpstring2) {
787                 free(tmpstring2);
788                 tmpstring2 = 0;
789         }
790         if (text_buffer) {
791                 free(text_buffer);
792                 text_buffer = 0;
793         }
794
795         extract_variable_text_internal(&global_root_object, p);
796 }
797
798 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
799 {
800         extract_variable_text_internal(root, txt);
801         generate_text_internal(p, max_user_text, *root, cur);
802 }
803
804 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
805                 void *thread(void *), const char *text)
806 {
807         if (obj->char_b && info.mail) {
808                 // this means we use info
809                 if (!info.mail->p_timed_thread) {
810                         info.mail->p_timed_thread =
811                                 timed_thread_create(thread,
812                                                 (void *) info.mail, info.mail->interval * 1000000);
813                         if (!info.mail->p_timed_thread) {
814                                 NORM_ERR("Error creating %s timed thread", text);
815                         }
816                         timed_thread_register(info.mail->p_timed_thread,
817                                         &info.mail->p_timed_thread);
818                         if (timed_thread_run(info.mail->p_timed_thread)) {
819                                 NORM_ERR("Error running %s timed thread", text);
820                         }
821                 }
822                 return info.mail;
823         } else if (obj->data.mail) {
824                 // this means we use obj
825                 if (!obj->data.mail->p_timed_thread) {
826                         obj->data.mail->p_timed_thread =
827                                 timed_thread_create(thread,
828                                                 (void *) obj->data.mail,
829                                                 obj->data.mail->interval * 1000000);
830                         if (!obj->data.mail->p_timed_thread) {
831                                 NORM_ERR("Error creating %s timed thread", text);
832                         }
833                         timed_thread_register(obj->data.mail->p_timed_thread,
834                                         &obj->data.mail->p_timed_thread);
835                         if (timed_thread_run(obj->data.mail->p_timed_thread)) {
836                                 NORM_ERR("Error running %s timed thread", text);
837                         }
838                 }
839                 return obj->data.mail;
840         } else if (!obj->a) {
841                 // something is wrong, warn once then stop
842                 NORM_ERR("There's a problem with your mail settings.  "
843                                 "Check that the global mail settings are properly defined"
844                                 " (line %li).", obj->line);
845                 obj->a++;
846         }
847         return NULL;
848 }
849
850 char *format_time(unsigned long timeval, const int width)
851 {
852         char buf[10];
853         unsigned long nt;       // narrow time, for speed on 32-bit
854         unsigned cc;            // centiseconds
855         unsigned nn;            // multi-purpose whatever
856
857         nt = timeval;
858         cc = nt % 100;          // centiseconds past second
859         nt /= 100;                      // total seconds
860         nn = nt % 60;           // seconds past the minute
861         nt /= 60;                       // total minutes
862         if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
863                                 nt, nn, cc)) {
864                 return strndup(buf, text_buffer_size);
865         }
866         if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
867                 return strndup(buf, text_buffer_size);
868         }
869         nn = nt % 60;           // minutes past the hour
870         nt /= 60;                       // total hours
871         if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
872                 return strndup(buf, text_buffer_size);
873         }
874         nn = nt;                        // now also hours
875         if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
876                 return strndup(buf, text_buffer_size);
877         }
878         nn /= 24;                       // now days
879         if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
880                 return strndup(buf, text_buffer_size);
881         }
882         nn /= 7;                        // now weeks
883         if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
884                 return strndup(buf, text_buffer_size);
885         }
886         // well shoot, this outta' fit...
887         return strndup("<inf>", text_buffer_size);
888 }
889
890 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
891 //string has to end with \0 and it's length should fit in a int
892 #define BACKSPACE 8
893 void remove_deleted_chars(char *string){
894         int i = 0;
895         while(string[i] != 0){
896                 if(string[i] == BACKSPACE){
897                         if(i != 0){
898                                 strcpy( &(string[i-1]), &(string[i+1]) );
899                                 i--;
900                         }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
901                 }else i++;
902         }
903 }
904
905 static inline void format_media_player_time(char *buf, const int size,
906                 int seconds)
907 {
908         int days, hours, minutes;
909
910         days = seconds / (24 * 60 * 60);
911         seconds %= (24 * 60 * 60);
912         hours = seconds / (60 * 60);
913         seconds %= (60 * 60);
914         minutes = seconds / 60;
915         seconds %= 60;
916
917         if (days > 0) {
918                 snprintf(buf, size, "%i days %i:%02i:%02i", days,
919                                 hours, minutes, seconds);
920         } else if (hours > 0) {
921                 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
922                                 seconds);
923         } else {
924                 snprintf(buf, size, "%i:%02i", minutes, seconds);
925         }
926 }
927
928 static inline double get_barnum(char *buf)
929 {
930         char *c = buf;
931         double barnum;
932
933         while (*c) {
934                 if (*c == '\001') {
935                         *c = ' ';
936                 }
937                 c++;
938         }
939
940         if (sscanf(buf, "%lf", &barnum) == 0) {
941                 NORM_ERR("reading exec value failed (perhaps it's not the "
942                                 "correct format?)");
943                 return -1;
944         }
945         if (barnum > 100.0 || barnum < 0.0) {
946                 NORM_ERR("your exec value is not between 0 and 100, "
947                                 "therefore it will be ignored");
948                 return -1;
949         }
950         return barnum;
951 }
952
953 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
954  * multiline objects like $exec work with $align[rc] and friends
955  */
956 void substitute_newlines(char *p, long l)
957 {
958         char *s = p;
959         if (l < 0) return;
960         while (p && *p && p < s + l) {
961                 if (*p == '\n') {
962                         /* only substitute if it's not the last newline */
963                         *p = SECRIT_MULTILINE_CHAR;
964                 }
965                 p++;
966         }
967 }
968
969 static void generate_text_internal(char *p, int p_max_size,
970                 struct text_object root, struct information *cur)
971 {
972         struct text_object *obj;
973 #ifdef X11
974         int need_to_load_fonts = 0;
975 #endif /* X11 */
976
977         /* for the OBJ_top* handler */
978         struct process **needed = 0;
979
980 #ifdef HAVE_ICONV
981         char buff_in[p_max_size];
982         buff_in[0] = 0;
983         set_iconv_converting(0);
984 #endif /* HAVE_ICONV */
985
986         p[0] = 0;
987         obj = root.next;
988         while (obj && p_max_size > 0) {
989                 needed = 0; /* reset for top stuff */
990
991 /* IFBLOCK jumping algorithm
992  *
993  * This is easier as it looks like:
994  * - each IF checks it's condition
995  *   - on FALSE: call DO_JUMP
996  *   - on TRUE: don't care
997  * - each ELSE calls DO_JUMP unconditionally
998  * - each ENDIF is silently being ignored
999  *
1000  * Why this works:
1001  * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
1002  * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
1003  * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
1004  * regularly, "obj" is being updated to point to obj->next, so object parsing
1005  * continues right after the corresponding ELSE or ENDIF. This means that if we
1006  * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
1007  * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
1008  * jumped, and there is nothing to do.
1009  */
1010 #define DO_JUMP { \
1011         DBGP2("jumping"); \
1012         obj = obj->data.ifblock.next; \
1013 }
1014
1015 #define OBJ(a) break; case OBJ_##a:
1016
1017                 switch (obj->type) {
1018                         default:
1019                                 NORM_ERR("not implemented obj type %d", obj->type);
1020                         OBJ(read_tcp) {
1021                                 int sock, received;
1022                                 struct sockaddr_in addr;
1023                                 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
1024                                 if(he != NULL) {
1025                                         sock = socket(he->h_addrtype, SOCK_STREAM, 0);
1026                                         if(sock != -1) {
1027                                                 memset(&addr, 0, sizeof(addr));
1028                                                 addr.sin_family = AF_INET;
1029                                                 addr.sin_port = obj->data.read_tcp.port;
1030                                                 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
1031                                                 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
1032                                                         fd_set readfds;
1033                                                         struct timeval tv;
1034                                                         FD_ZERO(&readfds);
1035                                                         FD_SET(sock, &readfds);
1036                                                         tv.tv_sec = 1;
1037                                                         tv.tv_usec = 0;
1038                                                         if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
1039                                                                 received = recv(sock, p, p_max_size, 0);
1040                                                                 p[received] = 0;
1041                                                         }
1042                                                         close(sock);
1043                                                 } else {
1044                                                         NORM_ERR("read_tcp: Couldn't create a connection");
1045                                                 }
1046                                         }else{
1047                                                 NORM_ERR("read_tcp: Couldn't create a socket");
1048                                         }
1049                                 }else{
1050                                         NORM_ERR("read_tcp: Problem with resolving the hostname");
1051                                 }
1052                         }
1053 #ifndef __OpenBSD__
1054                         OBJ(acpitemp) {
1055                                 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
1056                         }
1057 #endif /* !__OpenBSD__ */
1058                         OBJ(freq) {
1059                                 if (obj->a) {
1060                                         obj->a = get_freq(p, p_max_size, "%.0f", 1,
1061                                                         obj->data.cpu_index);
1062                                 }
1063                         }
1064                         OBJ(freq_g) {
1065                                 if (obj->a) {
1066 #ifndef __OpenBSD__
1067                                         obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
1068                                                         obj->data.cpu_index);
1069 #else
1070                                         /* OpenBSD has no such flag (SUSv2) */
1071                                         obj->a = get_freq(p, p_max_size, "%.2f", 1000,
1072                                                         obj->data.cpu_index);
1073 #endif /* __OpenBSD */
1074                                 }
1075                         }
1076 #if defined(__linux__)
1077                         OBJ(voltage_mv) {
1078                                 if (obj->a) {
1079                                         obj->a = get_voltage(p, p_max_size, "%.0f", 1,
1080                                                         obj->data.cpu_index);
1081                                 }
1082                         }
1083                         OBJ(voltage_v) {
1084                                 if (obj->a) {
1085                                         obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
1086                                                         obj->data.cpu_index);
1087                                 }
1088                         }
1089
1090 #ifdef HAVE_IWLIB
1091                         OBJ(wireless_essid) {
1092                                 snprintf(p, p_max_size, "%s", obj->data.net->essid);
1093                         }
1094                         OBJ(wireless_mode) {
1095                                 snprintf(p, p_max_size, "%s", obj->data.net->mode);
1096                         }
1097                         OBJ(wireless_bitrate) {
1098                                 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
1099                         }
1100                         OBJ(wireless_ap) {
1101                                 snprintf(p, p_max_size, "%s", obj->data.net->ap);
1102                         }
1103                         OBJ(wireless_link_qual) {
1104                                 spaced_print(p, p_max_size, "%d", 4,
1105                                                 obj->data.net->link_qual);
1106                         }
1107                         OBJ(wireless_link_qual_max) {
1108                                 spaced_print(p, p_max_size, "%d", 4,
1109                                                 obj->data.net->link_qual_max);
1110                         }
1111                         OBJ(wireless_link_qual_perc) {
1112                                 if (obj->data.net->link_qual_max > 0) {
1113                                         spaced_print(p, p_max_size, "%.0f", 5,
1114                                                         (double) obj->data.net->link_qual /
1115                                                         obj->data.net->link_qual_max * 100);
1116                                 } else {
1117                                         spaced_print(p, p_max_size, "unk", 5);
1118                                 }
1119                         }
1120                         OBJ(wireless_link_bar) {
1121 #ifdef X11
1122                                 if(output_methods & TO_X) {
1123                                         new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
1124                                                 obj->data.net->link_qual_max) * 255.0);
1125                                 }else{
1126 #endif /* X11 */
1127                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1128                                         new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
1129                                                 obj->data.net->link_qual_max) * 100.0, obj->a);
1130 #ifdef X11
1131                                 }
1132 #endif /* X11 */
1133                         }
1134 #endif /* HAVE_IWLIB */
1135
1136 #endif /* __linux__ */
1137
1138 #ifndef __OpenBSD__
1139                         OBJ(adt746xcpu) {
1140                                 get_adt746x_cpu(p, p_max_size);
1141                         }
1142                         OBJ(adt746xfan) {
1143                                 get_adt746x_fan(p, p_max_size);
1144                         }
1145                         OBJ(acpifan) {
1146                                 get_acpi_fan(p, p_max_size);
1147                         }
1148                         OBJ(acpiacadapter) {
1149                                 get_acpi_ac_adapter(p, p_max_size);
1150                         }
1151                         OBJ(battery) {
1152                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
1153                         }
1154                         OBJ(battery_time) {
1155                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
1156                         }
1157                         OBJ(battery_percent) {
1158                                 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
1159                         }
1160                         OBJ(battery_bar) {
1161 #ifdef X11
1162                                 if(output_methods & TO_X) {
1163                                         new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
1164                                 }else{
1165 #endif /* X11 */
1166                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1167                                         new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
1168 #ifdef X11
1169                                 }
1170 #endif /* X11 */
1171                         }
1172                         OBJ(battery_short) {
1173                                 get_battery_short_status(p, p_max_size, obj->data.s);
1174                         }
1175 #endif /* __OpenBSD__ */
1176
1177                         OBJ(buffers) {
1178                                 human_readable(cur->buffers * 1024, p, 255);
1179                         }
1180                         OBJ(cached) {
1181                                 human_readable(cur->cached * 1024, p, 255);
1182                         }
1183                         OBJ(cpu) {
1184                                 if (obj->data.cpu_index > info.cpu_count) {
1185                                         NORM_ERR("obj->data.cpu_index %i info.cpu_count %i",
1186                                                         obj->data.cpu_index, info.cpu_count);
1187                                         CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
1188                                 }
1189                                 percent_print(p, p_max_size,
1190                                               round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
1191                         }
1192 #ifdef X11
1193                         OBJ(cpugauge)
1194                                 new_gauge(p, obj->a, obj->b,
1195                                                 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1196 #endif /* X11 */
1197                         OBJ(cpubar) {
1198 #ifdef X11
1199                                 if(output_methods & TO_X) {
1200                                         new_bar(p, obj->a, obj->b,
1201                                                 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1202                                 }else{
1203 #endif /* X11 */
1204                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1205                                         new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
1206 #ifdef X11
1207                                 }
1208 #endif /* X11 */
1209                         }
1210 #ifdef X11
1211                         OBJ(cpugraph) {
1212                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1213                                                 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
1214                                                 100, 1, obj->char_a, obj->char_b);
1215                         }
1216                         OBJ(loadgraph) {
1217                                 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
1218                                                 obj->e, 1, obj->char_a, obj->char_b);
1219                         }
1220 #endif /* X11 */
1221                         OBJ(color) {
1222                                 new_fg(p, obj->data.l);
1223                         }
1224 #ifdef X11
1225                         OBJ(color0) {
1226                                 new_fg(p, color0);
1227                         }
1228                         OBJ(color1) {
1229                                 new_fg(p, color1);
1230                         }
1231                         OBJ(color2) {
1232                                 new_fg(p, color2);
1233                         }
1234                         OBJ(color3) {
1235                                 new_fg(p, color3);
1236                         }
1237                         OBJ(color4) {
1238                                 new_fg(p, color4);
1239                         }
1240                         OBJ(color5) {
1241                                 new_fg(p, color5);
1242                         }
1243                         OBJ(color6) {
1244                                 new_fg(p, color6);
1245                         }
1246                         OBJ(color7) {
1247                                 new_fg(p, color7);
1248                         }
1249                         OBJ(color8) {
1250                                 new_fg(p, color8);
1251                         }
1252                         OBJ(color9) {
1253                                 new_fg(p, color9);
1254                         }
1255 #endif /* X11 */
1256                         OBJ(conky_version) {
1257                                 snprintf(p, p_max_size, "%s", VERSION);
1258                         }
1259                         OBJ(conky_build_date) {
1260                                 snprintf(p, p_max_size, "%s", BUILD_DATE);
1261                         }
1262                         OBJ(conky_build_arch) {
1263                                 snprintf(p, p_max_size, "%s", BUILD_ARCH);
1264                         }
1265 #if defined(__linux__)
1266                         OBJ(disk_protect) {
1267                                 snprintf(p, p_max_size, "%s",
1268                                                 get_disk_protect_queue(obj->data.s));
1269                         }
1270                         OBJ(i8k_version) {
1271                                 snprintf(p, p_max_size, "%s", i8k.version);
1272                         }
1273                         OBJ(i8k_bios) {
1274                                 snprintf(p, p_max_size, "%s", i8k.bios);
1275                         }
1276                         OBJ(i8k_serial) {
1277                                 snprintf(p, p_max_size, "%s", i8k.serial);
1278                         }
1279                         OBJ(i8k_cpu_temp) {
1280                                 int cpu_temp;
1281
1282                                 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1283                                 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1284                         }
1285                         OBJ(i8k_left_fan_status) {
1286                                 int left_fan_status;
1287
1288                                 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1289                                 if (left_fan_status == 0) {
1290                                         snprintf(p, p_max_size, "off");
1291                                 }
1292                                 if (left_fan_status == 1) {
1293                                         snprintf(p, p_max_size, "low");
1294                                 }
1295                                 if (left_fan_status == 2) {
1296                                         snprintf(p, p_max_size, "high");
1297                                 }
1298                         }
1299                         OBJ(i8k_right_fan_status) {
1300                                 int right_fan_status;
1301
1302                                 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1303                                 if (right_fan_status == 0) {
1304                                         snprintf(p, p_max_size, "off");
1305                                 }
1306                                 if (right_fan_status == 1) {
1307                                         snprintf(p, p_max_size, "low");
1308                                 }
1309                                 if (right_fan_status == 2) {
1310                                         snprintf(p, p_max_size, "high");
1311                                 }
1312                         }
1313                         OBJ(i8k_left_fan_rpm) {
1314                                 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1315                         }
1316                         OBJ(i8k_right_fan_rpm) {
1317                                 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1318                         }
1319                         OBJ(i8k_ac_status) {
1320                                 int ac_status;
1321
1322                                 sscanf(i8k.ac_status, "%d", &ac_status);
1323                                 if (ac_status == -1) {
1324                                         snprintf(p, p_max_size, "disabled (read i8k docs)");
1325                                 }
1326                                 if (ac_status == 0) {
1327                                         snprintf(p, p_max_size, "off");
1328                                 }
1329                                 if (ac_status == 1) {
1330                                         snprintf(p, p_max_size, "on");
1331                                 }
1332                         }
1333                         OBJ(i8k_buttons_status) {
1334                                 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1335                         }
1336 #if defined(IBM)
1337                         OBJ(ibm_fan) {
1338                                 get_ibm_acpi_fan(p, p_max_size);
1339                         }
1340                         OBJ(ibm_temps) {
1341                                 get_ibm_acpi_temps();
1342                                 temp_print(p, p_max_size,
1343                                            ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
1344                         }
1345                         OBJ(ibm_volume) {
1346                                 get_ibm_acpi_volume(p, p_max_size);
1347                         }
1348                         OBJ(ibm_brightness) {
1349                                 get_ibm_acpi_brightness(p, p_max_size);
1350                         }
1351 #endif /* IBM */
1352                         /* information from sony_laptop kernel module
1353                          * /sys/devices/platform/sony-laptop */
1354                         OBJ(sony_fanspeed) {
1355                                 get_sony_fanspeed(p, p_max_size);
1356                         }
1357                         OBJ(if_gw) {
1358                                 if (!cur->gw_info.count) {
1359                                         DO_JUMP;
1360                                 }
1361                         }
1362                         OBJ(gw_iface) {
1363                                 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
1364                         }
1365                         OBJ(gw_ip) {
1366                                 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
1367                         }
1368                         OBJ(laptop_mode) {
1369                                 snprintf(p, p_max_size, "%d", get_laptop_mode());
1370                         }
1371                         OBJ(pb_battery) {
1372                                 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1373                         }
1374 #endif /* __linux__ */
1375 #if (defined(__FreeBSD__) || defined(__linux__))
1376                         OBJ(if_up) {
1377                                 if ((obj->data.ifblock.s)
1378                                                 && (!interface_up(obj->data.ifblock.s))) {
1379                                         DO_JUMP;
1380                                 }
1381                         }
1382 #endif
1383 #ifdef __OpenBSD__
1384                         OBJ(obsd_sensors_temp) {
1385                                 obsd_sensors.device = sensor_device;
1386                                 update_obsd_sensors();
1387                                 temp_print(p, p_max_size,
1388                                            obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
1389                                            TEMP_CELSIUS);
1390                         }
1391                         OBJ(obsd_sensors_fan) {
1392                                 obsd_sensors.device = sensor_device;
1393                                 update_obsd_sensors();
1394                                 snprintf(p, p_max_size, "%d",
1395                                                 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
1396                         }
1397                         OBJ(obsd_sensors_volt) {
1398                                 obsd_sensors.device = sensor_device;
1399                                 update_obsd_sensors();
1400                                 snprintf(p, p_max_size, "%.2f",
1401                                                 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
1402                         }
1403                         OBJ(obsd_vendor) {
1404                                 get_obsd_vendor(p, p_max_size);
1405                         }
1406                         OBJ(obsd_product) {
1407                                 get_obsd_product(p, p_max_size);
1408                         }
1409 #endif /* __OpenBSD__ */
1410 #ifdef X11
1411                         OBJ(font) {
1412                                 new_font(p, obj->data.s);
1413                                 need_to_load_fonts = 1;
1414                         }
1415 #endif /* X11 */
1416                         /* TODO: move this correction from kB to kB/s elsewhere
1417                          * (or get rid of it??) */
1418                         OBJ(diskio) {
1419                                 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
1420                                                 p, p_max_size);
1421                         }
1422                         OBJ(diskio_write) {
1423                                 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
1424                                                 p, p_max_size);
1425                         }
1426                         OBJ(diskio_read) {
1427                                 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
1428                                                 p, p_max_size);
1429                         }
1430 #ifdef X11
1431                         OBJ(diskiograph) {
1432                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1433                                           obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
1434                         }
1435                         OBJ(diskiograph_read) {
1436                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1437                                           obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
1438                         }
1439                         OBJ(diskiograph_write) {
1440                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1441                                           obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
1442                         }
1443 #endif /* X11 */
1444                         OBJ(downspeed) {
1445                                 human_readable(obj->data.net->recv_speed, p, 255);
1446                         }
1447                         OBJ(downspeedf) {
1448                                 spaced_print(p, p_max_size, "%.1f", 8,
1449                                                 obj->data.net->recv_speed / 1024.0);
1450                         }
1451 #ifdef X11
1452                         OBJ(downspeedgraph) {
1453                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1454                                         obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
1455                         }
1456 #endif /* X11 */
1457                         OBJ(else) {
1458                                 /* Since we see you, you're if has not jumped.
1459                                  * Do Ninja jump here: without leaving traces.
1460                                  * This is to prevent us from stale jumped flags.
1461                                  */
1462                                 obj = obj->data.ifblock.next;
1463                                 continue;
1464                         }
1465                         OBJ(endif) {
1466                                 /* harmless object, just ignore */
1467                         }
1468                         OBJ(addr) {
1469                                 if ((obj->data.net->addr.sa_data[2] & 255) == 0
1470                                                 && (obj->data.net->addr.sa_data[3] & 255) == 0
1471                                                 && (obj->data.net->addr.sa_data[4] & 255) == 0
1472                                                 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
1473                                         snprintf(p, p_max_size, "No Address");
1474                                 } else {
1475                                         snprintf(p, p_max_size, "%u.%u.%u.%u",
1476                                                 obj->data.net->addr.sa_data[2] & 255,
1477                                                 obj->data.net->addr.sa_data[3] & 255,
1478                                                 obj->data.net->addr.sa_data[4] & 255,
1479                                                 obj->data.net->addr.sa_data[5] & 255);
1480                                 }
1481                         }
1482 #if defined(__linux__)
1483                         OBJ(addrs) {
1484                                 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
1485                                         obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
1486                                         strcpy(p, obj->data.net->addrs);
1487                                 } else {
1488                                         strcpy(p, "0.0.0.0");
1489                                 }
1490                         }
1491 #endif /* __linux__ */
1492 #if defined(IMLIB2) && defined(X11)
1493                         OBJ(image) {
1494                                 /* doesn't actually draw anything, just queues it omp.  the
1495                                  * image will get drawn after the X event loop */
1496                                 cimlib_add_image(obj->data.s);
1497                         }
1498 #endif /* IMLIB2 */
1499                         OBJ(eval) {
1500                                 evaluate(obj->data.s, p);
1501                         }
1502                         OBJ(exec) {
1503                                 read_exec(obj->data.s, p, text_buffer_size);
1504                                 remove_deleted_chars(p);
1505                         }
1506                         OBJ(execp) {
1507                                 struct information *tmp_info;
1508                                 struct text_object subroot;
1509
1510                                 read_exec(obj->data.s, p, text_buffer_size);
1511
1512                                 tmp_info = malloc(sizeof(struct information));
1513                                 memcpy(tmp_info, cur, sizeof(struct information));
1514                                 parse_conky_vars(&subroot, p, p, tmp_info);
1515
1516                                 free_text_objects(&subroot, 1);
1517                                 free(tmp_info);
1518                         }
1519 #ifdef X11
1520                         OBJ(execgauge) {
1521                                 double barnum;
1522
1523                                 read_exec(obj->data.s, p, text_buffer_size);
1524                                 barnum = get_barnum(p); /*using the same function*/
1525
1526                                 if (barnum >= 0.0) {
1527                                         barnum /= 100;
1528                                         new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1529                                 }
1530                         }
1531 #endif /* X11 */
1532                         OBJ(execbar) {
1533                                 double barnum;
1534
1535                                 read_exec(obj->data.s, p, text_buffer_size);
1536                                 barnum = get_barnum(p);
1537
1538                                 if (barnum >= 0.0) {
1539 #ifdef X11
1540                                         if(output_methods & TO_X) {
1541                                                 barnum /= 100;
1542                                                 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1543                                         }else{
1544 #endif /* X11 */
1545                                                 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1546                                                 new_bar_in_shell(p, p_max_size, barnum, obj->a);
1547 #ifdef X11
1548                                         }
1549 #endif /* X11 */
1550                                 }
1551                         }
1552 #ifdef X11
1553                         OBJ(execgraph) {
1554                                 char showaslog = FALSE;
1555                                 char tempgrad = FALSE;
1556                                 double barnum;
1557                                 char *cmd = obj->data.s;
1558
1559                                 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1560                                         tempgrad = TRUE;
1561                                         cmd += strlen(" "TEMPGRAD);
1562                                 }
1563                                 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1564                                         showaslog = TRUE;
1565                                         cmd += strlen(" "LOGGRAPH);
1566                                 }
1567                                 read_exec(cmd, p, text_buffer_size);
1568                                 barnum = get_barnum(p);
1569
1570                                 if (barnum > 0) {
1571                                         new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
1572                                                         100, 1, showaslog, tempgrad);
1573                                 }
1574                         }
1575 #endif /* X11 */
1576                         OBJ(execibar) {
1577                                 if (current_update_time - obj->data.execi.last_update
1578                                                 >= obj->data.execi.interval) {
1579                                         double barnum;
1580
1581                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
1582                                         barnum = get_barnum(p);
1583
1584                                         if (barnum >= 0.0) {
1585                                                 obj->f = barnum;
1586                                         }
1587                                         obj->data.execi.last_update = current_update_time;
1588                                 }
1589 #ifdef X11
1590                                 if(output_methods & TO_X) {
1591                                         new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
1592                                 } else {
1593 #endif /* X11 */
1594                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1595                                         new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
1596 #ifdef X11
1597                                 }
1598 #endif /* X11 */
1599                         }
1600 #ifdef X11
1601                         OBJ(execigraph) {
1602                                 if (current_update_time - obj->data.execi.last_update
1603                                                 >= obj->data.execi.interval) {
1604                                         double barnum;
1605                                         char showaslog = FALSE;
1606                                         char tempgrad = FALSE;
1607                                         char *cmd = obj->data.execi.cmd;
1608
1609                                         if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1610                                                 tempgrad = TRUE;
1611                                                 cmd += strlen(" "TEMPGRAD);
1612                                         }
1613                                         if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1614                                                 showaslog = TRUE;
1615                                                 cmd += strlen(" "LOGGRAPH);
1616                                         }
1617                                         obj->char_a = showaslog;
1618                                         obj->char_b = tempgrad;
1619                                         read_exec(cmd, p, text_buffer_size);
1620                                         barnum = get_barnum(p);
1621
1622                                         if (barnum >= 0.0) {
1623                                                 obj->f = barnum;
1624                                         }
1625                                         obj->data.execi.last_update = current_update_time;
1626                                 }
1627                                 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
1628                         }
1629                         OBJ(execigauge) {
1630                                 if (current_update_time - obj->data.execi.last_update
1631                                                 >= obj->data.execi.interval) {
1632                                         double barnum;
1633
1634                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
1635                                         barnum = get_barnum(p);
1636
1637                                         if (barnum >= 0.0) {
1638                                                 obj->f = 255 * barnum / 100.0;
1639                                         }
1640                                         obj->data.execi.last_update = current_update_time;
1641                                 }
1642                                 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
1643                         }
1644 #endif /* X11 */
1645                         OBJ(execi) {
1646                                 if (current_update_time - obj->data.execi.last_update
1647                                                 >= obj->data.execi.interval
1648                                                 && obj->data.execi.interval != 0) {
1649                                         read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
1650                                                 text_buffer_size);
1651                                         obj->data.execi.last_update = current_update_time;
1652                                 }
1653                                 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
1654                         }
1655                         OBJ(execpi) {
1656                                 struct text_object subroot;
1657                                 struct information *tmp_info =
1658                                         malloc(sizeof(struct information));
1659                                 memcpy(tmp_info, cur, sizeof(struct information));
1660
1661                                 if (current_update_time - obj->data.execi.last_update
1662                                                 < obj->data.execi.interval
1663                                                 || obj->data.execi.interval == 0) {
1664                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1665                                 } else {
1666                                         char *output = obj->data.execi.buffer;
1667                                         FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
1668                                         int length = fread(output, 1, text_buffer_size, fp);
1669
1670                                         pclose(fp);
1671
1672                                         output[length] = '\0';
1673                                         if (length > 0 && output[length - 1] == '\n') {
1674                                                 output[length - 1] = '\0';
1675                                         }
1676
1677                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1678                                         obj->data.execi.last_update = current_update_time;
1679                                 }
1680                                 free_text_objects(&subroot, 1);
1681                                 free(tmp_info);
1682                         }
1683                         OBJ(texeci) {
1684                                 if (!obj->data.texeci.p_timed_thread) {
1685                                         obj->data.texeci.p_timed_thread =
1686                                                 timed_thread_create(&threaded_exec,
1687                                                 (void *) obj, obj->data.texeci.interval * 1000000);
1688                                         if (!obj->data.texeci.p_timed_thread) {
1689                                                 NORM_ERR("Error creating texeci timed thread");
1690                                         }
1691                                         /*
1692                                          * note that we don't register this thread with the
1693                                          * timed_thread list, because we destroy it manually
1694                                          */
1695                                         if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
1696                                                 NORM_ERR("Error running texeci timed thread");
1697                                         }
1698                                 } else {
1699                                         timed_thread_lock(obj->data.texeci.p_timed_thread);
1700                                         snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
1701                                         timed_thread_unlock(obj->data.texeci.p_timed_thread);
1702                                 }
1703                         }
1704                         OBJ(imap_unseen) {
1705                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1706
1707                                 if (mail && mail->p_timed_thread) {
1708                                         timed_thread_lock(mail->p_timed_thread);
1709                                         snprintf(p, p_max_size, "%lu", mail->unseen);
1710                                         timed_thread_unlock(mail->p_timed_thread);
1711                                 }
1712                         }
1713                         OBJ(imap_messages) {
1714                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1715
1716                                 if (mail && mail->p_timed_thread) {
1717                                         timed_thread_lock(mail->p_timed_thread);
1718                                         snprintf(p, p_max_size, "%lu", mail->messages);
1719                                         timed_thread_unlock(mail->p_timed_thread);
1720                                 }
1721                         }
1722                         OBJ(pop3_unseen) {
1723                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1724
1725                                 if (mail && mail->p_timed_thread) {
1726                                         timed_thread_lock(mail->p_timed_thread);
1727                                         snprintf(p, p_max_size, "%lu", mail->unseen);
1728                                         timed_thread_unlock(mail->p_timed_thread);
1729                                 }
1730                         }
1731                         OBJ(pop3_used) {
1732                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1733
1734                                 if (mail && mail->p_timed_thread) {
1735                                         timed_thread_lock(mail->p_timed_thread);
1736                                         snprintf(p, p_max_size, "%.1f",
1737                                                 mail->used / 1024.0 / 1024.0);
1738                                         timed_thread_unlock(mail->p_timed_thread);
1739                                 }
1740                         }
1741                         OBJ(fs_bar) {
1742                                 if (obj->data.fs != NULL) {
1743                                         if (obj->data.fs->size == 0) {
1744 #ifdef X11
1745                                                 if(output_methods & TO_X) {
1746                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1747                                                 }else{
1748 #endif /* X11 */
1749                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1750                                                         new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1751 #ifdef X11
1752                                                 }
1753 #endif /* X11 */
1754                                         } else {
1755 #ifdef X11
1756                                                 if(output_methods & TO_X) {
1757                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1758                                                                 (int) (255 - obj->data.fsbar.fs->avail * 255 /
1759                                                                 obj->data.fs->size));
1760                                                 }else{
1761 #endif /* X11 */
1762                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1763                                                         new_bar_in_shell(p, p_max_size,
1764                                                                 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1765 #ifdef X11
1766                                                 }
1767 #endif /* X11 */
1768                                         }
1769                                 }
1770                         }
1771                         OBJ(fs_free) {
1772                                 if (obj->data.fs != NULL) {
1773                                         human_readable(obj->data.fs->avail, p, 255);
1774                                 }
1775                         }
1776                         OBJ(fs_free_perc) {
1777                                 if (obj->data.fs != NULL) {
1778                                         int val = 0;
1779
1780                                         if (obj->data.fs->size) {
1781                                                 val = obj->data.fs->avail * 100 / obj->data.fs->size;
1782                                         }
1783
1784                                         percent_print(p, p_max_size, val);
1785                                 }
1786                         }
1787                         OBJ(fs_size) {
1788                                 if (obj->data.fs != NULL) {
1789                                         human_readable(obj->data.fs->size, p, 255);
1790                                 }
1791                         }
1792                         OBJ(fs_type) {
1793                                 if (obj->data.fs != NULL)
1794                                         snprintf(p, p_max_size, "%s", obj->data.fs->type);
1795                         }
1796                         OBJ(fs_used) {
1797                                 if (obj->data.fs != NULL) {
1798                                         human_readable(obj->data.fs->size - obj->data.fs->free, p,
1799                                                         255);
1800                                 }
1801                         }
1802                         OBJ(fs_bar_free) {
1803                                 if (obj->data.fs != NULL) {
1804                                         if (obj->data.fs->size == 0) {
1805 #ifdef X11
1806                                                 if(output_methods & TO_X) {
1807                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1808                                                 }else{
1809 #endif /* X11 */
1810                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1811                                                         new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1812 #ifdef X11
1813                                                 }
1814 #endif /* X11 */
1815                                         } else {
1816 #ifdef X11
1817                                                 if(output_methods & TO_X) {
1818                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1819                                                                 (int) (obj->data.fsbar.fs->avail * 255 /
1820                                                                 obj->data.fs->size));
1821                                                 }else{
1822 #endif /* X11 */
1823                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1824                                                         new_bar_in_shell(p, p_max_size,
1825                                                                 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1826 #ifdef X11
1827                                                 }
1828 #endif /* X11 */
1829                                         }
1830                                 }
1831                         }
1832                         OBJ(fs_used_perc) {
1833                                 if (obj->data.fs != NULL) {
1834                                         int val = 0;
1835
1836                                         if (obj->data.fs->size) {
1837                                                 val = obj->data.fs->free
1838                                                                 * 100 /
1839                                                         obj->data.fs->size;
1840                                         }
1841
1842                                         percent_print(p, p_max_size, 100 - val);
1843                                 }
1844                         }
1845                         OBJ(loadavg) {
1846                                 float *v = info.loadavg;
1847
1848                                 if (obj->data.loadavg[2]) {
1849                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
1850                                                 v[obj->data.loadavg[0] - 1],
1851                                                 v[obj->data.loadavg[1] - 1],
1852                                                 v[obj->data.loadavg[2] - 1]);
1853                                 } else if (obj->data.loadavg[1]) {
1854                                         snprintf(p, p_max_size, "%.2f %.2f",
1855                                                 v[obj->data.loadavg[0] - 1],
1856                                                 v[obj->data.loadavg[1] - 1]);
1857                                 } else if (obj->data.loadavg[0]) {
1858                                         snprintf(p, p_max_size, "%.2f",
1859                                                 v[obj->data.loadavg[0] - 1]);
1860                                 }
1861                         }
1862                         OBJ(goto) {
1863                                 new_goto(p, obj->data.i);
1864                         }
1865                         OBJ(tab) {
1866                                 new_tab(p, obj->data.pair.a, obj->data.pair.b);
1867                         }
1868 #ifdef X11
1869                         OBJ(hr) {
1870                                 new_hr(p, obj->data.i);
1871                         }
1872 #endif
1873                         OBJ(nameserver) {
1874                                 if (cur->nameserver_info.nscount > obj->data.i)
1875                                         snprintf(p, p_max_size, "%s",
1876                                                         cur->nameserver_info.ns_list[obj->data.i]);
1877                         }
1878 #ifdef EVE
1879                         OBJ(eve) {
1880                                 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
1881                                 snprintf(p, p_max_size, "%s", skill);
1882                         }
1883 #endif
1884 #ifdef HAVE_CURL
1885                         OBJ(curl) {
1886                                 if (obj->data.curl.uri != NULL) {
1887                                         ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
1888                                 } else {
1889                                         NORM_ERR("error processing Curl data");
1890                                 }
1891                         }
1892 #endif
1893 #ifdef RSS
1894                         OBJ(rss) {
1895                                 if (obj->data.rss.uri != NULL) {
1896                                         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);
1897                                 } else {
1898                                         NORM_ERR("error processing RSS data");
1899                                 }
1900                         }
1901 #endif
1902 #ifdef WEATHER
1903                         OBJ(weather) {
1904                                 if (obj->data.weather.uri != NULL) {
1905                                         weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
1906                                 } else {
1907                                         NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
1908                                 }
1909                         }
1910 #endif
1911 #ifdef XOAP
1912                         OBJ(weather_forecast) {
1913                                 if (obj->data.weather_forecast.uri != NULL) {
1914                                         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);
1915                                 } else {
1916                                         NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
1917                                 }
1918                         }
1919 #endif
1920 #ifdef HAVE_LUA
1921                         OBJ(lua) {
1922                                 char *str = llua_getstring(obj->data.s);
1923                                 if (str) {
1924                                         snprintf(p, p_max_size, "%s", str);
1925                                         free(str);
1926                                 }
1927                         }
1928                         OBJ(lua_parse) {
1929                                 char *str = llua_getstring(obj->data.s);
1930                                 if (str) {
1931                                         evaluate(str, p);
1932                                         free(str);
1933                                 }
1934                         }
1935                         OBJ(lua_bar) {
1936                                 double per;
1937                                 if (llua_getnumber(obj->data.s, &per)) {
1938 #ifdef X11
1939                                         if(output_methods & TO_X) {
1940                                                 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
1941                                         } else {
1942 #endif /* X11 */
1943                                                 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1944                                                 new_bar_in_shell(p, p_max_size, per, obj->a);
1945 #ifdef X11
1946                                         }
1947 #endif /* X11 */
1948                                 }
1949                         }
1950 #ifdef X11
1951                         OBJ(lua_graph) {
1952                                 double per;
1953                                 if (llua_getnumber(obj->data.s, &per)) {
1954                                         new_graph(p, obj->a, obj->b, obj->c, obj->d,
1955                                                         per, obj->e, 1, obj->char_a, obj->char_b);
1956                                 }
1957                         }
1958                         OBJ(lua_gauge) {
1959                                 double per;
1960                                 if (llua_getnumber(obj->data.s, &per)) {
1961                                         new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
1962                                 }
1963                         }
1964 #endif /* X11 */
1965 #endif /* HAVE_LUA */
1966 #ifdef HDDTEMP
1967                         OBJ(hddtemp) {
1968                                 short val;
1969                                 char unit;
1970
1971                                 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1972                                         snprintf(p, p_max_size, "N/A");
1973                                 } else {
1974                                         temp_print(p, p_max_size, (double)val,
1975                                                         (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1976                                 }
1977                         }
1978 #endif
1979                         OBJ(offset) {
1980                                 new_offset(p, obj->data.i);
1981                         }
1982                         OBJ(voffset) {
1983                                 new_voffset(p, obj->data.i);
1984                         }
1985 #ifdef __linux__
1986                         OBJ(i2c) {
1987                                 double r;
1988
1989                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
1990                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
1991
1992                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
1993
1994                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
1995                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
1996                                 } else if (r >= 100.0 || r == 0) {
1997                                         snprintf(p, p_max_size, "%d", (int) r);
1998                                 } else {
1999                                         snprintf(p, p_max_size, "%.1f", r);
2000                                 }
2001                         }
2002                         OBJ(platform) {
2003                                 double r;
2004
2005                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2006                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
2007
2008                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2009
2010                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2011                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
2012                                 } else if (r >= 100.0 || r == 0) {
2013                                         snprintf(p, p_max_size, "%d", (int) r);
2014                                 } else {
2015                                         snprintf(p, p_max_size, "%.1f", r);
2016                                 }
2017                         }
2018                         OBJ(hwmon) {
2019                                 double r;
2020
2021                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2022                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
2023
2024                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2025
2026                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2027                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
2028                                 } else if (r >= 100.0 || r == 0) {
2029                                         snprintf(p, p_max_size, "%d", (int) r);
2030                                 } else {
2031                                         snprintf(p, p_max_size, "%.1f", r);
2032                                 }
2033                         }
2034 #endif /* __linux__ */
2035                         OBJ(alignr) {
2036                                 new_alignr(p, obj->data.i);
2037                         }
2038                         OBJ(alignc) {
2039                                 new_alignc(p, obj->data.i);
2040                         }
2041                         OBJ(if_empty) {
2042                                 char buf[max_user_text];
2043                                 struct information *tmp_info =
2044                                         malloc(sizeof(struct information));
2045                                 memcpy(tmp_info, cur, sizeof(struct information));
2046                                 generate_text_internal(buf, max_user_text,
2047                                                        *obj->sub, tmp_info);
2048
2049                                 if (strlen(buf) != 0) {
2050                                         DO_JUMP;
2051                                 }
2052                                 free(tmp_info);
2053                         }
2054                         OBJ(if_match) {
2055                                 char expression[max_user_text];
2056                                 int val;
2057                                 struct information *tmp_info;
2058
2059                                 tmp_info = malloc(sizeof(struct information));
2060                                 memcpy(tmp_info, cur, sizeof(struct information));
2061                                 generate_text_internal(expression, max_user_text,
2062                                                        *obj->sub, tmp_info);
2063                                 DBGP("parsed arg into '%s'", expression);
2064
2065                                 val = compare(expression);
2066                                 if (val == -2) {
2067                                         NORM_ERR("compare failed for expression '%s'",
2068                                                         expression);
2069                                 } else if (!val) {
2070                                         DO_JUMP;
2071                                 }
2072                                 free(tmp_info);
2073                         }
2074                         OBJ(if_existing) {
2075                                 if (obj->data.ifblock.str
2076                                     && !check_contains(obj->data.ifblock.s,
2077                                                        obj->data.ifblock.str)) {
2078                                         DO_JUMP;
2079                                 } else if (obj->data.ifblock.s
2080                                            && access(obj->data.ifblock.s, F_OK)) {
2081                                         DO_JUMP;
2082                                 }
2083                         }
2084                         OBJ(if_mounted) {
2085                                 if ((obj->data.ifblock.s)
2086                                                 && (!check_mount(obj->data.ifblock.s))) {
2087                                         DO_JUMP;
2088                                 }
2089                         }
2090                         OBJ(if_running) {
2091 #ifdef __linux__
2092                                 if (!get_process_by_name(obj->data.ifblock.s)) {
2093 #else
2094                                 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
2095 #endif
2096                                         DO_JUMP;
2097                                 }
2098                         }
2099 #if defined(__linux__)
2100                         OBJ(ioscheduler) {
2101                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
2102                         }
2103 #endif
2104                         OBJ(kernel) {
2105                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
2106                         }
2107                         OBJ(machine) {
2108                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
2109                         }
2110
2111                         /* memory stuff */
2112                         OBJ(mem) {
2113                                 human_readable(cur->mem * 1024, p, 255);
2114                         }
2115                         OBJ(memeasyfree) {
2116                                 human_readable(cur->memeasyfree * 1024, p, 255);
2117                         }
2118                         OBJ(memfree) {
2119                                 human_readable(cur->memfree * 1024, p, 255);
2120                         }
2121                         OBJ(memmax) {
2122                                 human_readable(cur->memmax * 1024, p, 255);
2123                         }
2124                         OBJ(memperc) {
2125                                 if (cur->memmax)
2126                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
2127                         }
2128 #ifdef X11
2129                         OBJ(memgauge){
2130                                 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
2131                                         cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2132                         }
2133 #endif /* X11 */
2134                         OBJ(membar) {
2135 #ifdef X11
2136                                 if(output_methods & TO_X) {
2137                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
2138                                                 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2139                                 }else{
2140 #endif /* X11 */
2141                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2142                                         new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
2143 #ifdef X11
2144                                 }
2145 #endif /* X11 */
2146                         }
2147 #ifdef X11
2148                         OBJ(memgraph) {
2149                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2150                                         cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
2151                                         100, 1, obj->char_a, obj->char_b);
2152                         }
2153 #endif /* X11 */
2154                         /* mixer stuff */
2155                         OBJ(mixer) {
2156                                 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
2157                         }
2158                         OBJ(mixerl) {
2159                                 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
2160                         }
2161                         OBJ(mixerr) {
2162                                 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
2163                         }
2164 #ifdef X11
2165                         OBJ(mixerbar) {
2166                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2167                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
2168                         }
2169                         OBJ(mixerlbar) {
2170                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2171                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
2172                         }
2173                         OBJ(mixerrbar) {
2174                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2175                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
2176                         }
2177 #endif /* X11 */
2178                         OBJ(if_mixer_mute) {
2179                                 if (!mixer_is_mute(obj->data.ifblock.i)) {
2180                                         DO_JUMP;
2181                                 }
2182                         }
2183 #ifdef X11
2184 #define NOT_IN_X "Not running in X"
2185                         OBJ(monitor) {
2186                                 if(x_initialised != YES) {
2187                                         strncpy(p, NOT_IN_X, p_max_size);
2188                                 }else{
2189                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
2190                                 }
2191                         }
2192                         OBJ(monitor_number) {
2193                                 if(x_initialised != YES) {
2194                                         strncpy(p, NOT_IN_X, p_max_size);
2195                                 }else{
2196                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
2197                                 }
2198                         }
2199                         OBJ(desktop) {
2200                                 if(x_initialised != YES) {
2201                                         strncpy(p, NOT_IN_X, p_max_size);
2202                                 }else{
2203                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
2204                                 }
2205                         }
2206                         OBJ(desktop_number) {
2207                                 if(x_initialised != YES) {
2208                                         strncpy(p, NOT_IN_X, p_max_size);
2209                                 }else{
2210                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
2211                                 }
2212                         }
2213                         OBJ(desktop_name) {
2214                                 if(x_initialised != YES) {
2215                                         strncpy(p, NOT_IN_X, p_max_size);
2216                                 }else if(cur->x11.desktop.name != NULL) {
2217                                         strncpy(p, cur->x11.desktop.name, p_max_size);
2218                                 }
2219                         }
2220 #endif /* X11 */
2221
2222                         /* mail stuff */
2223                         OBJ(mails) {
2224                                 update_mail_count(&obj->data.local_mail);
2225                                 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
2226                         }
2227                         OBJ(new_mails) {
2228                                 update_mail_count(&obj->data.local_mail);
2229                                 snprintf(p, p_max_size, "%d",
2230                                         obj->data.local_mail.new_mail_count);
2231                         }
2232                         OBJ(seen_mails) {
2233                                 update_mail_count(&obj->data.local_mail);
2234                                 snprintf(p, p_max_size, "%d",
2235                                         obj->data.local_mail.seen_mail_count);
2236                         }
2237                         OBJ(unseen_mails) {
2238                                 update_mail_count(&obj->data.local_mail);
2239                                 snprintf(p, p_max_size, "%d",
2240                                         obj->data.local_mail.unseen_mail_count);
2241                         }
2242                         OBJ(flagged_mails) {
2243                                 update_mail_count(&obj->data.local_mail);
2244                                 snprintf(p, p_max_size, "%d",
2245                                         obj->data.local_mail.flagged_mail_count);
2246                         }
2247                         OBJ(unflagged_mails) {
2248                                 update_mail_count(&obj->data.local_mail);
2249                                 snprintf(p, p_max_size, "%d",
2250                                         obj->data.local_mail.unflagged_mail_count);
2251                         }
2252                         OBJ(forwarded_mails) {
2253                                 update_mail_count(&obj->data.local_mail);
2254                                 snprintf(p, p_max_size, "%d",
2255                                         obj->data.local_mail.forwarded_mail_count);
2256                         }
2257                         OBJ(unforwarded_mails) {
2258                                 update_mail_count(&obj->data.local_mail);
2259                                 snprintf(p, p_max_size, "%d",
2260                                         obj->data.local_mail.unforwarded_mail_count);
2261                         }
2262                         OBJ(replied_mails) {
2263                                 update_mail_count(&obj->data.local_mail);
2264                                 snprintf(p, p_max_size, "%d",
2265                                         obj->data.local_mail.replied_mail_count);
2266                         }
2267                         OBJ(unreplied_mails) {
2268                                 update_mail_count(&obj->data.local_mail);
2269                                 snprintf(p, p_max_size, "%d",
2270                                         obj->data.local_mail.unreplied_mail_count);
2271                         }
2272                         OBJ(draft_mails) {
2273                                 update_mail_count(&obj->data.local_mail);
2274                                 snprintf(p, p_max_size, "%d",
2275                                         obj->data.local_mail.draft_mail_count);
2276                         }
2277                         OBJ(trashed_mails) {
2278                                 update_mail_count(&obj->data.local_mail);
2279                                 snprintf(p, p_max_size, "%d",
2280                                         obj->data.local_mail.trashed_mail_count);
2281                         }
2282                         OBJ(mboxscan) {
2283                                 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
2284                                         text_buffer_size);
2285                                 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
2286                         }
2287                         OBJ(nodename) {
2288                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
2289                         }
2290                         OBJ(outlinecolor) {
2291                                 new_outline(p, obj->data.l);
2292                         }
2293                         OBJ(processes) {
2294                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
2295                         }
2296                         OBJ(running_processes) {
2297                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
2298                         }
2299                         OBJ(text) {
2300                                 snprintf(p, p_max_size, "%s", obj->data.s);
2301                         }
2302 #ifdef X11
2303                         OBJ(shadecolor) {
2304                                 new_bg(p, obj->data.l);
2305                         }
2306                         OBJ(stippled_hr) {
2307                                 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
2308                         }
2309 #endif /* X11 */
2310                         OBJ(swap) {
2311                                 human_readable(cur->swap * 1024, p, 255);
2312                         }
2313                         OBJ(swapfree) {
2314                                 human_readable(cur->swapfree * 1024, p, 255);
2315                         }
2316                         OBJ(swapmax) {
2317                                 human_readable(cur->swapmax * 1024, p, 255);
2318                         }
2319                         OBJ(swapperc) {
2320                                 if (cur->swapmax == 0) {
2321                                         strncpy(p, "No swap", p_max_size);
2322                                 } else {
2323                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
2324                                 }
2325                         }
2326                         OBJ(swapbar) {
2327 #ifdef X11
2328                                 if(output_methods & TO_X) {
2329                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
2330                                                 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
2331                                 }else{
2332 #endif /* X11 */
2333                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2334                                         new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
2335 #ifdef X11
2336                                 }
2337 #endif /* X11 */
2338                         }
2339                         OBJ(sysname) {
2340                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
2341                         }
2342                         OBJ(time) {
2343                                 time_t t = time(NULL);
2344                                 struct tm *tm = localtime(&t);
2345
2346                                 setlocale(LC_TIME, "");
2347                                 strftime(p, p_max_size, obj->data.s, tm);
2348                         }
2349                         OBJ(utime) {
2350                                 time_t t = time(NULL);
2351                                 struct tm *tm = gmtime(&t);
2352
2353                                 strftime(p, p_max_size, obj->data.s, tm);
2354                         }
2355                         OBJ(tztime) {
2356                                 char *oldTZ = NULL;
2357                                 time_t t;
2358                                 struct tm *tm;
2359
2360                                 if (obj->data.tztime.tz) {
2361                                         oldTZ = getenv("TZ");
2362                                         setenv("TZ", obj->data.tztime.tz, 1);
2363                                         tzset();
2364                                 }
2365                                 t = time(NULL);
2366                                 tm = localtime(&t);
2367
2368                                 setlocale(LC_TIME, "");
2369                                 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
2370                                 if (oldTZ) {
2371                                         setenv("TZ", oldTZ, 1);
2372                                         tzset();
2373                                 } else {
2374                                         unsetenv("TZ");
2375                                 }
2376                                 // Needless to free oldTZ since getenv gives ptr to static data
2377                         }
2378                         OBJ(totaldown) {
2379                                 human_readable(obj->data.net->recv, p, 255);
2380                         }
2381                         OBJ(totalup) {
2382                                 human_readable(obj->data.net->trans, p, 255);
2383                         }
2384                         OBJ(updates) {
2385                                 snprintf(p, p_max_size, "%d", total_updates);
2386                         }
2387                         OBJ(if_updatenr) {
2388                                 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
2389                                         DO_JUMP;
2390                                 }
2391                         }
2392                         OBJ(upspeed) {
2393                                 human_readable(obj->data.net->trans_speed, p, 255);
2394                         }
2395                         OBJ(upspeedf) {
2396                                 spaced_print(p, p_max_size, "%.1f", 8,
2397                                         obj->data.net->trans_speed / 1024.0);
2398                         }
2399 #ifdef X11
2400                         OBJ(upspeedgraph) {
2401                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2402                                         obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
2403                         }
2404 #endif /* X11 */
2405                         OBJ(uptime_short) {
2406                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
2407                         }
2408                         OBJ(uptime) {
2409                                 format_seconds(p, p_max_size, (int) cur->uptime);
2410                         }
2411                         OBJ(user_names) {
2412                                 snprintf(p, p_max_size, "%s", cur->users.names);
2413                         }
2414                         OBJ(user_terms) {
2415                                 snprintf(p, p_max_size, "%s", cur->users.terms);
2416                         }
2417                         OBJ(user_times) {
2418                                 snprintf(p, p_max_size, "%s", cur->users.times);
2419                         }
2420                         OBJ(user_number) {
2421                                 snprintf(p, p_max_size, "%d", cur->users.number);
2422                         }
2423 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2424                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2425                         OBJ(apm_adapter) {
2426                                 char *msg;
2427
2428                                 msg = get_apm_adapter();
2429                                 snprintf(p, p_max_size, "%s", msg);
2430                                 free(msg);
2431                         }
2432                         OBJ(apm_battery_life) {
2433                                 char *msg;
2434
2435                                 msg = get_apm_battery_life();
2436                                 snprintf(p, p_max_size, "%s", msg);
2437                                 free(msg);
2438                         }
2439                         OBJ(apm_battery_time) {
2440                                 char *msg;
2441
2442                                 msg = get_apm_battery_time();
2443                                 snprintf(p, p_max_size, "%s", msg);
2444                                 free(msg);
2445                         }
2446 #endif /* __FreeBSD__ __OpenBSD__ */
2447
2448 #ifdef MPD
2449 #define mpd_printf(fmt, val) \
2450         snprintf(p, p_max_size, fmt, mpd_get_info()->val)
2451 #define mpd_sprintf(val) { \
2452         if (!obj->data.i || obj->data.i > p_max_size) \
2453                 mpd_printf("%s", val); \
2454         else \
2455                 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2456 }
2457                         OBJ(mpd_title)
2458                                 mpd_sprintf(title);
2459                         OBJ(mpd_artist)
2460                                 mpd_sprintf(artist);
2461                         OBJ(mpd_album)
2462                                 mpd_sprintf(album);
2463                         OBJ(mpd_random)
2464                                 mpd_printf("%s", random);
2465                         OBJ(mpd_repeat)
2466                                 mpd_printf("%s", repeat);
2467                         OBJ(mpd_track)
2468                                 mpd_sprintf(track);
2469                         OBJ(mpd_name)
2470                                 mpd_sprintf(name);
2471                         OBJ(mpd_file)
2472                                 mpd_sprintf(file);
2473                         OBJ(mpd_vol)
2474                                 mpd_printf("%d", volume);
2475                         OBJ(mpd_bitrate)
2476                                 mpd_printf("%d", bitrate);
2477                         OBJ(mpd_status)
2478                                 mpd_printf("%s", status);
2479                         OBJ(mpd_elapsed) {
2480                                 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2481                         }
2482                         OBJ(mpd_length) {
2483                                 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2484                         }
2485                         OBJ(mpd_percent) {
2486                                 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2487                         }
2488                         OBJ(mpd_bar) {
2489 #ifdef X11
2490                                 if(output_methods & TO_X) {
2491                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
2492                                                 (int) (mpd_get_info()->progress * 255.0f));
2493                                 } else {
2494 #endif /* X11 */
2495                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2496                                         new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
2497 #ifdef X11
2498                                 }
2499 #endif /* X11 */
2500                         }
2501                         OBJ(mpd_smart) {
2502                                 struct mpd_s *mpd = mpd_get_info();
2503                                 int len = obj->data.i;
2504                                 if (len == 0 || len > p_max_size)
2505                                         len = p_max_size;
2506
2507                                 memset(p, 0, p_max_size);
2508                                 if (mpd->artist && *mpd->artist &&
2509                                     mpd->title && *mpd->title) {
2510                                         snprintf(p, len, "%s - %s", mpd->artist,
2511                                                 mpd->title);
2512                                 } else if (mpd->title && *mpd->title) {
2513                                         snprintf(p, len, "%s", mpd->title);
2514                                 } else if (mpd->artist && *mpd->artist) {
2515                                         snprintf(p, len, "%s", mpd->artist);
2516                                 } else if (mpd->file && *mpd->file) {
2517                                         snprintf(p, len, "%s", mpd->file);
2518                                 } else {
2519                                         *p = 0;
2520                                 }
2521                         }
2522                         OBJ(if_mpd_playing) {
2523                                 if (!mpd_get_info()->is_playing) {
2524                                         DO_JUMP;
2525                                 }
2526                         }
2527 #undef mpd_sprintf
2528 #undef mpd_printf
2529 #endif
2530
2531 #ifdef MOC
2532 #define MOC_PRINT(t, a) \
2533         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2534                         OBJ(moc_state) {
2535                                 MOC_PRINT(state, "??");
2536                         }
2537                         OBJ(moc_file) {
2538                                 MOC_PRINT(file, "no file");
2539                         }
2540                         OBJ(moc_title) {
2541                                 MOC_PRINT(title, "no title");
2542                         }
2543                         OBJ(moc_artist) {
2544                                 MOC_PRINT(artist, "no artist");
2545                         }
2546                         OBJ(moc_song) {
2547                                 MOC_PRINT(song, "no song");
2548                         }
2549                         OBJ(moc_album) {
2550                                 MOC_PRINT(album, "no album");
2551                         }
2552                         OBJ(moc_totaltime) {
2553                                 MOC_PRINT(totaltime, "0:00");
2554                         }
2555                         OBJ(moc_timeleft) {
2556                                 MOC_PRINT(timeleft, "0:00");
2557                         }
2558                         OBJ(moc_curtime) {
2559                                 MOC_PRINT(curtime, "0:00");
2560                         }
2561                         OBJ(moc_bitrate) {
2562                                 MOC_PRINT(bitrate, "0Kbps");
2563                         }
2564                         OBJ(moc_rate) {
2565                                 MOC_PRINT(rate, "0KHz");
2566                         }
2567 #undef MOC_PRINT
2568 #endif /* MOC */
2569 #ifdef XMMS2
2570                         OBJ(xmms2_artist) {
2571                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2572                         }
2573                         OBJ(xmms2_album) {
2574                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2575                         }
2576                         OBJ(xmms2_title) {
2577                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2578                         }
2579                         OBJ(xmms2_genre) {
2580                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2581                         }
2582                         OBJ(xmms2_comment) {
2583                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2584                         }
2585                         OBJ(xmms2_url) {
2586                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2587                         }
2588                         OBJ(xmms2_status) {
2589                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2590                         }
2591                         OBJ(xmms2_date) {
2592                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2593                         }
2594                         OBJ(xmms2_tracknr) {
2595                                 if (cur->xmms2.tracknr != -1) {
2596                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2597                                 }
2598                         }
2599                         OBJ(xmms2_bitrate) {
2600                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2601                         }
2602                         OBJ(xmms2_id) {
2603                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2604                         }
2605                         OBJ(xmms2_size) {
2606                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2607                         }
2608                         OBJ(xmms2_elapsed) {
2609                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2610                                         (cur->xmms2.elapsed / 1000) % 60);
2611                         }
2612                         OBJ(xmms2_duration) {
2613                                 snprintf(p, p_max_size, "%02d:%02d",
2614                                         cur->xmms2.duration / 60000,
2615                                         (cur->xmms2.duration / 1000) % 60);
2616                         }
2617                         OBJ(xmms2_percent) {
2618                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2619                         }
2620 #ifdef X11
2621                         OBJ(xmms2_bar) {
2622                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2623                                         (int) (cur->xmms2.progress * 255.0f));
2624                         }
2625 #endif /* X11 */
2626                         OBJ(xmms2_playlist) {
2627                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2628                         }
2629                         OBJ(xmms2_timesplayed) {
2630                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2631                         }
2632                         OBJ(xmms2_smart) {
2633                                 if (strlen(cur->xmms2.title) < 2
2634                                                 && strlen(cur->xmms2.title) < 2) {
2635                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
2636                                 } else {
2637                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2638                                                 cur->xmms2.title);
2639                                 }
2640                         }
2641                         OBJ(if_xmms2_connected) {
2642                                 if (cur->xmms2.conn_state != 1) {
2643                                         DO_JUMP;
2644                                 }
2645                         }
2646 #endif /* XMMS */
2647 #ifdef AUDACIOUS
2648                         OBJ(audacious_status) {
2649                                 snprintf(p, p_max_size, "%s",
2650                                         cur->audacious.items[AUDACIOUS_STATUS]);
2651                         }
2652                         OBJ(audacious_title) {
2653                                 snprintf(p, cur->audacious.max_title_len > 0
2654                                         ? cur->audacious.max_title_len : p_max_size, "%s",
2655                                         cur->audacious.items[AUDACIOUS_TITLE]);
2656                         }
2657                         OBJ(audacious_length) {
2658                                 snprintf(p, p_max_size, "%s",
2659                                         cur->audacious.items[AUDACIOUS_LENGTH]);
2660                         }
2661                         OBJ(audacious_length_seconds) {
2662                                 snprintf(p, p_max_size, "%s",
2663                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2664                         }
2665                         OBJ(audacious_position) {
2666                                 snprintf(p, p_max_size, "%s",
2667                                         cur->audacious.items[AUDACIOUS_POSITION]);
2668                         }
2669                         OBJ(audacious_position_seconds) {
2670                                 snprintf(p, p_max_size, "%s",
2671                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2672                         }
2673                         OBJ(audacious_bitrate) {
2674                                 snprintf(p, p_max_size, "%s",
2675                                         cur->audacious.items[AUDACIOUS_BITRATE]);
2676                         }
2677                         OBJ(audacious_frequency) {
2678                                 snprintf(p, p_max_size, "%s",
2679                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
2680                         }
2681                         OBJ(audacious_channels) {
2682                                 snprintf(p, p_max_size, "%s",
2683                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
2684                         }
2685                         OBJ(audacious_filename) {
2686                                 snprintf(p, p_max_size, "%s",
2687                                         cur->audacious.items[AUDACIOUS_FILENAME]);
2688                         }
2689                         OBJ(audacious_playlist_length) {
2690                                 snprintf(p, p_max_size, "%s",
2691                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2692                         }
2693                         OBJ(audacious_playlist_position) {
2694                                 snprintf(p, p_max_size, "%s",
2695                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2696                         }
2697                         OBJ(audacious_main_volume) {
2698                                 snprintf(p, p_max_size, "%s",
2699                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2700                         }
2701 #ifdef X11
2702                         OBJ(audacious_bar) {
2703                                 double progress;
2704
2705                                 progress =
2706                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2707                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2708                                 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
2709                         }
2710 #endif /* X11 */
2711 #endif /* AUDACIOUS */
2712
2713 #ifdef BMPX
2714                         OBJ(bmpx_title) {
2715                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2716                         }
2717                         OBJ(bmpx_artist) {
2718                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2719                         }
2720                         OBJ(bmpx_album) {
2721                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2722                         }
2723                         OBJ(bmpx_uri) {
2724                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2725                         }
2726                         OBJ(bmpx_track) {
2727                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2728                         }
2729                         OBJ(bmpx_bitrate) {
2730                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2731                         }
2732 #endif /* BMPX */
2733                         /* we have four different types of top (top, top_mem,
2734                          * top_time and top_io). To avoid having almost-same code four
2735                          * times, we have this special handler. */
2736                         break;
2737                         case OBJ_top:
2738                                 parse_top_args("top", obj->data.top.s, obj);
2739                                 if (!needed) needed = cur->cpu;
2740                         case OBJ_top_mem:
2741                                 parse_top_args("top_mem", obj->data.top.s, obj);
2742                                 if (!needed) needed = cur->memu;
2743                         case OBJ_top_time:
2744                                 parse_top_args("top_time", obj->data.top.s, obj);
2745                                 if (!needed) needed = cur->time;
2746 #ifdef IOSTATS
2747                         case OBJ_top_io:
2748                                 parse_top_args("top_io", obj->data.top.s, obj);
2749                                 if (!needed) needed = cur->io;
2750 #endif
2751
2752                                 if (needed[obj->data.top.num]) {
2753                                         char *timeval;
2754
2755                                         switch (obj->data.top.type) {
2756                                                 case TOP_NAME:
2757                                                         snprintf(p, top_name_width + 1, "%-*s", top_name_width,
2758                                                                         needed[obj->data.top.num]->name);
2759                                                         break;
2760                                                 case TOP_CPU:
2761                                                         snprintf(p, 7, "%6.2f",
2762                                                                         needed[obj->data.top.num]->amount);
2763                                                         break;
2764                                                 case TOP_PID:
2765                                                         snprintf(p, 6, "%5i",
2766                                                                         needed[obj->data.top.num]->pid);
2767                                                         break;
2768                                                 case TOP_MEM:
2769                                                         snprintf(p, 7, "%6.2f",
2770                                                                         needed[obj->data.top.num]->totalmem);
2771                                                         break;
2772                                                 case TOP_TIME:
2773                                                         timeval = format_time(
2774                                                                         needed[obj->data.top.num]->total_cpu_time, 9);
2775                                                         snprintf(p, 10, "%9s", timeval);
2776                                                         free(timeval);
2777                                                         break;
2778                                                 case TOP_MEM_RES:
2779                                                         human_readable(needed[obj->data.top.num]->rss,
2780                                                                         p, 255);
2781                                                         break;
2782                                                 case TOP_MEM_VSIZE:
2783                                                         human_readable(needed[obj->data.top.num]->vsize,
2784                                                                         p, 255);
2785                                                         break;
2786 #ifdef IOSTATS
2787                                                 case TOP_READ_BYTES:
2788                                                         human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
2789                                                                         p, 255);
2790                                                         break;
2791                                                 case TOP_WRITE_BYTES:
2792                                                         human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
2793                                                                         p, 255);
2794                                                         break;
2795                                                 case TOP_IO_PERC:
2796                                                         snprintf(p, 7, "%6.2f",
2797                                                                         needed[obj->data.top.num]->io_perc);
2798                                                         break;
2799 #endif
2800                                         }
2801                                 }
2802                         OBJ(tail) {
2803                                 print_tailhead("tail", obj, p, p_max_size);
2804                         }
2805                         OBJ(head) {
2806                                 print_tailhead("head", obj, p, p_max_size);
2807                         }
2808                         OBJ(lines) {
2809                                 FILE *fp = open_file(obj->data.s, &obj->a);
2810
2811                                 if(fp != NULL) {
2812 /* FIXME: use something more general (see also tail.c, head.c */
2813 #define BUFSZ 0x1000
2814                                         char buf[BUFSZ];
2815                                         int j, lines;
2816
2817                                         lines = 0;
2818                                         while(fgets(buf, BUFSZ, fp) != NULL){
2819                                                 for(j = 0; buf[j] != 0; j++) {
2820                                                         if(buf[j] == '\n') {
2821                                                                 lines++;
2822                                                         }
2823                                                 }
2824                                         }
2825                                         sprintf(p, "%d", lines);
2826                                         fclose(fp);
2827                                 } else {
2828                                         sprintf(p, "File Unreadable");
2829                                 }
2830                         }
2831
2832                         OBJ(words) {
2833                                 FILE *fp = open_file(obj->data.s, &obj->a);
2834
2835                                 if(fp != NULL) {
2836                                         char buf[BUFSZ];
2837                                         int j, words;
2838                                         char inword = FALSE;
2839
2840                                         words = 0;
2841                                         while(fgets(buf, BUFSZ, fp) != NULL){
2842                                                 for(j = 0; buf[j] != 0; j++) {
2843                                                         if(!isspace(buf[j])) {
2844                                                                 if(inword == FALSE) {
2845                                                                         words++;
2846                                                                         inword = TRUE;
2847                                                                 }
2848                                                         } else {
2849                                                                 inword = FALSE;
2850                                                         }
2851                                                 }
2852                                         }
2853                                         sprintf(p, "%d", words);
2854                                         fclose(fp);
2855                                 } else {
2856                                         sprintf(p, "File Unreadable");
2857                                 }
2858                         }
2859 #ifdef TCP_PORT_MONITOR
2860                         OBJ(tcp_portmon) {
2861                                 tcp_portmon_action(p, p_max_size,
2862                                                    &obj->data.tcp_port_monitor);
2863                         }
2864 #endif /* TCP_PORT_MONITOR */
2865
2866 #ifdef HAVE_ICONV
2867                         OBJ(iconv_start) {
2868                                 set_iconv_converting(1);
2869                                 set_iconv_selected(obj->a);
2870                         }
2871                         OBJ(iconv_stop) {
2872                                 set_iconv_converting(0);
2873                                 set_iconv_selected(0);
2874                         }
2875 #endif /* HAVE_ICONV */
2876
2877                         OBJ(entropy_avail) {
2878                                 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2879                         }
2880                         OBJ(entropy_perc) {
2881                                 percent_print(p, p_max_size,
2882                                               cur->entropy.entropy_avail *
2883                                               100 / cur->entropy.poolsize);
2884                         }
2885                         OBJ(entropy_poolsize) {
2886                                 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2887                         }
2888                         OBJ(entropy_bar) {
2889                                 double entropy_perc;
2890
2891                                 entropy_perc = (double) cur->entropy.entropy_avail /
2892                                         (double) cur->entropy.poolsize;
2893 #ifdef X11
2894                                 if(output_methods & TO_X) {
2895                                         new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
2896                                 } else {
2897 #endif /* X11 */
2898                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
2899                                         new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
2900 #ifdef X11
2901                                 }
2902 #endif /* X11 */
2903                         }
2904 #ifdef IBM
2905                         OBJ(smapi) {
2906                                 char *s;
2907                                 if(obj->data.s) {
2908                                         s = smapi_get_val(obj->data.s);
2909                                         snprintf(p, p_max_size, "%s", s);
2910                                         free(s);
2911                                 }
2912                         }
2913                         OBJ(if_smapi_bat_installed) {
2914                                 int idx;
2915                                 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
2916                                         if(!smapi_bat_installed(idx)) {
2917                                                 DO_JUMP;
2918                                         }
2919                                 } else
2920                                         NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2921                         }
2922                         OBJ(smapi_bat_perc) {
2923                                 int idx, val;
2924                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2925                                         val = smapi_bat_installed(idx) ?
2926                                                 smapi_get_bat_int(idx, "remaining_percent") : 0;
2927                                         percent_print(p, p_max_size, val);
2928                                 } else
2929                                         NORM_ERR("argument to smapi_bat_perc must be an integer");
2930                         }
2931                         OBJ(smapi_bat_temp) {
2932                                 int idx, val;
2933                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2934                                         val = smapi_bat_installed(idx) ?
2935                                                 smapi_get_bat_int(idx, "temperature") : 0;
2936                                         /* temperature is in milli degree celsius */
2937                                         temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2938                                 } else
2939                                         NORM_ERR("argument to smapi_bat_temp must be an integer");
2940                         }
2941                         OBJ(smapi_bat_power) {
2942                                 int idx, val;
2943                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2944                                         val = smapi_bat_installed(idx) ?
2945                                                 smapi_get_bat_int(idx, "power_now") : 0;
2946                                         /* power_now is in mW, set to W with one digit precision */
2947                                         snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2948                                 } else
2949                                         NORM_ERR("argument to smapi_bat_power must be an integer");
2950                         }
2951 #ifdef X11
2952                         OBJ(smapi_bat_bar) {
2953                                 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2954                                         new_bar(p, obj->a, obj->b, (int)
2955                                                         (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2956                                 else
2957                                         new_bar(p, obj->a, obj->b, 0);
2958                         }
2959 #endif /* X11 */
2960 #endif /* IBM */
2961                         OBJ(include) {
2962                                 if(obj->sub) {
2963                                         char buf[max_user_text];
2964
2965                                         generate_text_internal(buf, max_user_text, *obj->sub, cur);
2966                                         snprintf(p, p_max_size, "%s", buf);
2967                                 } else {
2968                                         p[0] = 0;
2969                                 }
2970                         }
2971                         OBJ(blink) {
2972                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2973                                 char buf[max_user_text];
2974                                 unsigned int j;
2975
2976                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2977                                 snprintf(p, p_max_size, "%s", buf);
2978                                 if(total_updates % 2) {
2979                                         for(j=0; p[j] != 0; j++) {
2980                                                 p[j] = ' ';
2981                                         }
2982                                 }
2983                         }
2984                         OBJ(to_bytes) {
2985                                 char buf[max_user_text];
2986                                 long long bytes;
2987                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
2988
2989                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2990                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2991                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2992                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2993                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2994                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2995                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2996                                 }
2997                                 snprintf(p, p_max_size, "%s", buf);
2998                         }
2999                         OBJ(scroll) {
3000                                 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
3001                                 char *pwithcolors;
3002                                 char buf[max_user_text];
3003                                 generate_text_internal(buf, max_user_text,
3004                                                        *obj->sub, cur);
3005                                 for(j = 0; buf[j] != 0; j++) {
3006                                         switch(buf[j]) {
3007                                         case '\n':      //place all the lines behind each other with LINESEPARATOR between them
3008 #define LINESEPARATOR '|'
3009                                                 buf[j]=LINESEPARATOR;
3010                                                 break;
3011                                         case SPECIAL_CHAR:
3012                                                 colorchanges++;
3013                                                 break;
3014                                         }
3015                                 }
3016                                 //no scrolling necessary if the length of the text to scroll is too short
3017                                 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
3018                                         snprintf(p, p_max_size, "%s", buf);
3019                                         break;
3020                                 }
3021                                 //make sure a colorchange at the front is not part of the string we are going to show
3022                                 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
3023                                         obj->data.scroll.start++;
3024                                 }
3025                                 //place all chars that should be visible in p, including colorchanges
3026                                 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
3027                                         p[j] = *(buf + obj->data.scroll.start + j);
3028                                         if(p[j] == SPECIAL_CHAR) {
3029                                                 visibcolorchanges++;
3030                                         }
3031                                         //if there is still room fill it with spaces
3032                                         if( ! p[j]) break;
3033                                 }
3034                                 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
3035                                         p[j] = ' ';
3036                                 }
3037                                 p[j] = 0;
3038                                 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
3039                                 for(j = 0; j < obj->data.scroll.start; j++) {
3040                                         if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
3041                                 }
3042                                 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
3043                                 for(j = 0; j < frontcolorchanges; j++) {
3044                                         pwithcolors[j] = SPECIAL_CHAR;
3045                                 }
3046                                 pwithcolors[j] = 0;
3047                                 strcat(pwithcolors,p);
3048                                 strend = strlen(pwithcolors);
3049                                 //and place the colorchanges not in front or in the visible part behind the visible part
3050                                 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
3051                                         pwithcolors[strend + j] = SPECIAL_CHAR;
3052                                 }
3053                                 pwithcolors[strend + j] = 0;
3054                                 strcpy(p, pwithcolors);
3055                                 free(pwithcolors);
3056                                 //scroll
3057                                 obj->data.scroll.start += obj->data.scroll.step;
3058                                 if(buf[obj->data.scroll.start] == 0){
3059                                          obj->data.scroll.start = 0;
3060                                 }
3061 #ifdef X11
3062                                 //reset color when scroll is finished
3063                                 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
3064 #endif
3065                         }
3066                         OBJ(combine) {
3067                                 char buf[2][max_user_text];
3068                                 int i, j;
3069                                 long longest=0;
3070                                 int nextstart;
3071                                 int nr_rows[2];
3072                                 struct llrows {
3073                                         char* row;
3074                                         struct llrows* next;
3075                                 };
3076                                 struct llrows *ll_rows[2], *current[2];
3077                                 struct text_object * objsub = obj->sub;
3078
3079                                 p[0]=0;
3080                                 for(i=0; i<2; i++) {
3081                                         nr_rows[i] = 1;
3082                                         nextstart = 0;
3083                                         ll_rows[i] = malloc(sizeof(struct llrows));
3084                                         current[i] = ll_rows[i];
3085                                         for(j=0; j<i; j++) objsub = objsub->sub;
3086                                         generate_text_internal(buf[i], max_user_text, *objsub, cur);
3087                                         for(j=0; buf[i][j] != 0; j++) {
3088                                                 if(buf[i][j] == '\t') buf[i][j] = ' ';
3089                                                 if(buf[i][j] == '\n') {
3090                                                         buf[i][j] = 0;
3091                                                         current[i]->row = strdup(buf[i]+nextstart);
3092                                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3093                                                         current[i]->next = malloc(sizeof(struct llrows));
3094                                                         current[i] = current[i]->next;
3095                                                         nextstart = j + 1;
3096                                                         nr_rows[i]++;
3097                                                 }
3098                                         }
3099                                         current[i]->row = strdup(buf[i]+nextstart);
3100                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3101                                         current[i]->next = NULL;
3102                                         current[i] = ll_rows[i];
3103                                 }
3104                                 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
3105                                         if(current[0]) {
3106                                                 strcat(p, current[0]->row);
3107                                                 i=strlen(current[0]->row);
3108                                         }else i = 0;
3109                                         while(i < longest) {
3110                                                 strcat(p, " ");
3111                                                 i++;
3112                                         }
3113                                         if(current[1]) {
3114                                                 strcat(p, obj->data.combine.seperation);
3115                                                 strcat(p, current[1]->row);
3116                                         }
3117                                         strcat(p, "\n");
3118                                         #ifdef HAVE_OPENMP
3119                                         #pragma omp parallel for schedule(dynamic,10)
3120                                         #endif /* HAVE_OPENMP */
3121                                         for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
3122                                 }
3123                                 #ifdef HAVE_OPENMP
3124                                 #pragma omp parallel for schedule(dynamic,10)
3125                                 #endif /* HAVE_OPENMP */
3126                                 for(i=0; i<2; i++) {
3127                                         while(ll_rows[i] != NULL) {
3128                                                 current[i]=ll_rows[i];
3129                                                 free(current[i]->row);
3130                                                 ll_rows[i]=current[i]->next;
3131                                                 free(current[i]);
3132                                         }
3133                                 }
3134                         }
3135 #ifdef NVIDIA
3136                         OBJ(nvidia) {
3137                                 int value = get_nvidia_value(obj->data.nvidia.type, display);
3138                                 if(value == -1)
3139                                         snprintf(p, p_max_size, "N/A");
3140                                 else if (obj->data.nvidia.type == NV_TEMP)
3141                                         temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
3142                                 else if (obj->data.nvidia.print_as_float &&
3143                                                 value > 0 && value < 100)
3144                                         snprintf(p, p_max_size, "%.1f", (float)value);
3145                                 else
3146                                         snprintf(p, p_max_size, "%d", value);
3147                         }
3148 #endif /* NVIDIA */
3149 #ifdef APCUPSD
3150                         OBJ(apcupsd) {
3151                                 /* This is just a meta-object to set host:port */
3152                         }
3153                         OBJ(apcupsd_name) {
3154                                 snprintf(p, p_max_size, "%s",
3155                                                  cur->apcupsd.items[APCUPSD_NAME]);
3156                         }
3157                         OBJ(apcupsd_model) {
3158                                 snprintf(p, p_max_size, "%s",
3159                                                  cur->apcupsd.items[APCUPSD_MODEL]);
3160                         }
3161                         OBJ(apcupsd_upsmode) {
3162                                 snprintf(p, p_max_size, "%s",
3163                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
3164                         }
3165                         OBJ(apcupsd_cable) {
3166                                 snprintf(p, p_max_size, "%s",
3167                                                  cur->apcupsd.items[APCUPSD_CABLE]);
3168                         }
3169                         OBJ(apcupsd_status) {
3170                                 snprintf(p, p_max_size, "%s",
3171                                                  cur->apcupsd.items[APCUPSD_STATUS]);
3172                         }
3173                         OBJ(apcupsd_linev) {
3174                                 snprintf(p, p_max_size, "%s",
3175                                                  cur->apcupsd.items[APCUPSD_LINEV]);
3176                         }
3177                         OBJ(apcupsd_load) {
3178                                 snprintf(p, p_max_size, "%s",
3179                                                  cur->apcupsd.items[APCUPSD_LOAD]);
3180                         }
3181                         OBJ(apcupsd_loadbar) {
3182                                 double progress;
3183 #ifdef X11
3184                                 if(output_methods & TO_X) {
3185                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3186                                         new_bar(p, obj->a, obj->b, (int) progress);
3187                                 } else {
3188 #endif /* X11 */
3189                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3190                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3191                                         new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
3192 #ifdef X11
3193                                 }
3194 #endif /* X11 */
3195                         }
3196 #ifdef X11
3197                         OBJ(apcupsd_loadgraph) {
3198                                 double progress;
3199                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
3200                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3201                                                   (int)progress, 100, 1, obj->char_a, obj->char_b);
3202                         }
3203                         OBJ(apcupsd_loadgauge) {
3204                                 double progress;
3205                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3206                                 new_gauge(p, obj->a, obj->b,
3207                                                   (int)progress);
3208                         }
3209 #endif /* X11 */
3210                         OBJ(apcupsd_charge) {
3211                                 snprintf(p, p_max_size, "%s",
3212                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
3213                         }
3214                         OBJ(apcupsd_timeleft) {
3215                                 snprintf(p, p_max_size, "%s",
3216                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
3217                         }
3218                         OBJ(apcupsd_temp) {
3219                                 snprintf(p, p_max_size, "%s",
3220                                                  cur->apcupsd.items[APCUPSD_TEMP]);
3221                         }
3222                         OBJ(apcupsd_lastxfer) {
3223                                 snprintf(p, p_max_size, "%s",
3224                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
3225                         }
3226 #endif /* APCUPSD */
3227                         break;
3228                 }
3229 #undef DO_JUMP
3230
3231
3232                 {
3233                         size_t a = strlen(p);
3234
3235 #ifdef HAVE_ICONV
3236                         iconv_convert(a, buff_in, p, p_max_size);
3237 #endif /* HAVE_ICONV */
3238                         if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
3239                                         && obj->type != OBJ_lua && obj->type != OBJ_lua_parse) {
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 }