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