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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
88 #include "iconv_tools.h"
100 #include "specials.h"
101 #include "temphelper.h"
102 #include "template.h"
103 #include "tailhead.h"
104 #include "timeinfo.h"
107 /* check for OS and include appropriate headers */
108 #if defined(__linux__)
110 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
112 #elif defined(__OpenBSD__)
116 #if defined(__FreeBSD_kernel__)
120 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
121 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
122 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
123 int apm_getinfo(int fd, apm_info_t aip);
124 char *get_apm_adapter(void);
125 char *get_apm_battery_life(void);
126 char *get_apm_battery_time(void);
130 #include "defconfig.h"
131 #include "conf_cookie.h"
135 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
138 #define MAIL_FILE "$MAIL"
139 #define MAX_IF_BLOCK_DEPTH 5
141 //#define SIGNAL_BLOCKING
142 #undef SIGNAL_BLOCKING
144 /* debugging level, used by logging.h */
145 int global_debug_level = 0;
147 /* two strings for internal use */
148 static char *tmpstring1, *tmpstring2;
150 /* variables holding various config settings */
152 int format_human_readable;
159 int top_cpu, top_mem, top_time;
166 static unsigned int top_name_width = 15;
168 static int extra_newline;
169 enum x_initialiser_state x_initialised = NO;
170 static volatile int g_signal_pending;
171 /* Update interval */
172 double update_interval;
173 double update_interval_old;
174 double update_interval_bat;
175 void *global_cpu = NULL;
180 /* prototypes for internally used functions */
181 static void signal_handler(int);
182 static void print_version(void) __attribute__((noreturn));
183 static void reload_config(void);
185 static void print_version(void)
187 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
189 printf("\nCompiled in features:\n\n"
190 "System config file: "SYSTEM_CONFIG_FILE"\n"
191 "Package library path: "PACKAGE_LIBDIR"\n\n"
195 " * Xdamage extension\n"
196 # endif /* HAVE_XDAMAGE */
198 " * XDBE (double buffer extension)\n"
199 # endif /* HAVE_XDBE */
204 "\n Music detection:\n"
207 #endif /* AUDACIOUS */
223 #endif /* HAVE_OPENMP */
230 #ifdef TCP_PORT_MONITOR
232 #endif /* TCP_PORT_MONITOR */
235 #endif /* HAVE_CURL */
240 " * Weather (METAR)\n"
242 " * Weather (XOAP)\n"
247 #endif /* HAVE_IWLIB */
249 " * support for IBM/Lenovo notebooks\n"
259 #endif /* CONFIG_OUTPUT */
264 " * ALSA mixer support\n"
265 #endif /* MIXER_IS_ALSA */
278 #ifdef HAVE_LUA_CAIRO
280 #endif /* HAVE_LUA_CAIRO */
281 #ifdef HAVE_LUA_IMLIB2
284 #endif /* HAVE_LUA */
290 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
295 static void X11_create_window(void);
296 static void X11_initialisation(void);
298 struct _x11_stuff_s {
302 XserverRegion region2, part;
303 int event_base, error_base;
309 static int text_start_x, text_start_y; /* text start position in window */
310 static int text_width, text_height;
325 /* display to connect to */
326 static char *disp = NULL;
330 /* struct that has all info to be shared between
331 * instances of the same text object */
332 struct information info;
334 /* path to config file */
335 char *current_config;
337 /* set to 1 if you want all text to be in uppercase */
338 static unsigned int stuff_in_uppercase;
340 /* Run how many times? */
341 static unsigned long total_run_times;
344 static int fork_to_background;
346 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
348 /* filenames for output */
349 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
350 char *append_file = NULL; FILE *append_fpointer = NULL;
354 static int show_graph_scale;
355 static int show_graph_range;
357 /* Position on the screen */
358 static int text_alignment;
359 static int gap_x, gap_y;
362 static int draw_borders;
363 static int draw_graph_borders;
364 static int stippled_borders;
366 int get_stippled_borders(void)
368 return stippled_borders;
371 static int draw_shades, draw_outline;
373 long default_fg_color, default_bg_color, default_out_color;
375 /* create own window or draw stuff to root? */
376 static int set_transparent = 0;
379 static int own_window = 0;
380 static int background_colour = 0;
382 /* fixed size/pos is set if wm/user changes them */
383 static int fixed_size = 0, fixed_pos = 0;
386 static int minimum_width, minimum_height;
387 static int maximum_width;
392 static int sensor_device;
395 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
398 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
399 unsigned int max_user_text;
401 /* maximum size of individual text buffers, ie $exec buffer size */
402 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
407 /* no buffers in used memory? */
410 /* pad percentages to decimals? */
411 static int pad_percents = 0;
413 static char *global_text = 0;
415 char *get_global_text(void)
420 long global_text_lines;
422 static int total_updates;
423 static int updatereset;
425 void set_updatereset(int i)
430 int get_updatereset(void)
435 int check_contains(char *f, char *s)
438 FILE *where = open_file(f, 0);
443 while (fgets(buf1, 256, where)) {
444 if (strstr(buf1, s)) {
451 NORM_ERR("Could not open the file");
456 #define SECRIT_MULTILINE_CHAR '\x02'
458 static inline int calc_text_width(const char *s)
460 size_t slen = strlen(s);
463 if ((output_methods & TO_X) == 0) {
473 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
474 (const FcChar8 *) s, slen, &gi);
476 XftTextExtents8(display, fonts[selected_font].xftfont,
477 (const FcChar8 *) s, slen, &gi);
483 return XTextWidth(fonts[selected_font].font, s, slen);
488 /* formatted text to render on screen, generated in generate_text(),
489 * drawn in draw_stuff() */
491 static char *text_buffer;
493 /* quite boring functions */
495 static inline void for_each_line(char *b, int f(char *, int))
498 int special_index = 0; /* specials index */
500 for (ps = b, pe = b; *pe; pe++) {
503 special_index = f(ps, special_index);
510 f(ps, special_index);
514 static void convert_escapes(char *buf)
516 char *p = buf, *s = buf;
523 } else if (*s == '\\') {
534 /* Prints anything normally printed with snprintf according to the current value
535 * of use_spacer. Actually slightly more flexible than snprintf, as you can
536 * safely specify the destination buffer as one of your inputs. */
537 int spaced_print(char *buf, int size, const char *format, int width, ...)
546 tempbuf = malloc(size * sizeof(char));
548 // Passes the varargs along to vsnprintf
549 va_start(argp, width);
550 vsnprintf(tempbuf, size, format, argp);
553 switch (use_spacer) {
555 len = snprintf(buf, size, "%s", tempbuf);
558 len = snprintf(buf, size, "%*s", width, tempbuf);
561 len = snprintf(buf, size, "%-*s", width, tempbuf);
568 /* print percentage values
570 * - i.e., unsigned values between 0 and 100
571 * - respect the value of pad_percents */
572 int percent_print(char *buf, int size, unsigned value)
574 return spaced_print(buf, size, "%u", pad_percents, value);
577 /* converts from bytes to human readable format (K, M, G, T)
579 * The algorithm always divides by 1024, as unit-conversion of byte
580 * counts suggests. But for output length determination we need to
581 * compare with 1000 here, as we print in decimal form. */
582 void human_readable(long long num, char *buf, int size)
584 const char **suffix = suffixes;
590 /* Possibly just output as usual, for example for stdout usage */
591 if (!format_human_readable) {
592 spaced_print(buf, size, "%d", 6, round_to_int(num));
603 if (llabs(num) < 1000LL) {
604 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
608 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
616 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
618 * The goal is to always have a significance of 3, by
619 * adjusting the decimal part of the number. Sample output:
623 * so the point of alignment resides between number and unit. The
624 * upside of this is that there is minimal padding necessary, though
625 * there should be a way to make alignment take place at the decimal
626 * dot (then with fixed width decimal part).
628 * Note the repdigits below: when given a precision value, printf()
629 * rounds the float to it, not just cuts off the remaining digits. So
630 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
631 * printed with a precision of 0. Yay. */
633 precision = 0; /* print 100-999 without decimal part */
635 precision = 1; /* print 10-99 with one decimal place */
637 precision = 2; /* print 0-9 with two decimal places */
639 spaced_print(buf, size, format, width, precision, fnum, *suffix);
642 /* global object list root element */
643 static struct text_object global_root_object;
645 static long current_text_color;
647 void set_current_text_color(long colour)
649 current_text_color = colour;
652 long get_current_text_color(void)
654 return current_text_color;
657 //adds newstring to to the tree unless you can already see it when travelling back.
658 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
659 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
660 struct conftree* node;
661 struct conftree* node2;
663 for(node = previous; node != NULL; node = node->back) {
664 if(strcmp(node->string, newstring) == 0) {
668 node = malloc(sizeof(struct conftree));
669 if (previous != NULL) {
670 if(previous->vert_next == NULL) {
671 previous->vert_next = node;
673 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
674 node2->horz_next = node;
677 node->string = strdup(newstring);
678 node->horz_next = NULL;
679 node->vert_next = NULL;
680 node->back = previous;
684 void conftree_empty(struct conftree* tree) {
686 conftree_empty(tree->horz_next);
687 conftree_empty(tree->vert_next);
693 struct conftree *currentconffile;
695 static void extract_variable_text(const char *p)
697 free_text_objects(&global_root_object, 0);
711 extract_variable_text_internal(&global_root_object, p);
714 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
716 extract_variable_text_internal(root, txt);
717 generate_text_internal(p, max_user_text, *root, cur);
720 static inline void format_media_player_time(char *buf, const int size,
723 int days, hours, minutes;
725 days = seconds / (24 * 60 * 60);
726 seconds %= (24 * 60 * 60);
727 hours = seconds / (60 * 60);
728 seconds %= (60 * 60);
729 minutes = seconds / 60;
733 snprintf(buf, size, "%i days %i:%02i:%02i", days,
734 hours, minutes, seconds);
735 } else if (hours > 0) {
736 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
739 snprintf(buf, size, "%i:%02i", minutes, seconds);
743 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
744 * multiline objects like $exec work with $align[rc] and friends
746 void substitute_newlines(char *p, long l)
750 while (p && *p && p < s + l) {
752 /* only substitute if it's not the last newline */
753 *p = SECRIT_MULTILINE_CHAR;
759 void generate_text_internal(char *p, int p_max_size,
760 struct text_object root, struct information *cur)
762 struct text_object *obj;
764 int need_to_load_fonts = 0;
767 /* for the OBJ_top* handler */
768 struct process **needed = 0;
771 char buff_in[p_max_size];
773 #endif /* HAVE_ICONV */
777 while (obj && p_max_size > 0) {
778 needed = 0; /* reset for top stuff */
780 /* IFBLOCK jumping algorithm
782 * This is easier as it looks like:
783 * - each IF checks it's condition
784 * - on FALSE: call DO_JUMP
785 * - on TRUE: don't care
786 * - each ELSE calls DO_JUMP unconditionally
787 * - each ENDIF is silently being ignored
790 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
791 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
792 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
793 * regularly, "obj" is being updated to point to obj->next, so object parsing
794 * continues right after the corresponding ELSE or ENDIF. This means that if we
795 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
796 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
797 * jumped, and there is nothing to do.
801 obj = obj->special_data; \
804 #define OBJ(a) break; case OBJ_##a:
808 NORM_ERR("not implemented obj type %d", obj->type);
810 print_read_tcp(obj, p, p_max_size);
814 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
816 #endif /* !__OpenBSD__ */
820 ok = get_freq(p, p_max_size, "%.0f", 1,
828 ok = get_freq(p, p_max_size, "%'.2f", 1000,
831 /* OpenBSD has no such flag (SUSv2) */
832 ok = get_freq(p, p_max_size, "%.2f", 1000,
834 #endif /* __OpenBSD */
837 #if defined(__linux__)
841 ok = get_voltage(p, p_max_size, "%.0f", 1,
848 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
854 OBJ(wireless_essid) {
855 print_wireless_essid(obj, p, p_max_size);
858 print_wireless_mode(obj, p, p_max_size);
860 OBJ(wireless_bitrate) {
861 print_wireless_bitrate(obj, p, p_max_size);
864 print_wireless_ap(obj, p, p_max_size);
866 OBJ(wireless_link_qual) {
867 print_wireless_link_qual(obj, p, p_max_size);
869 OBJ(wireless_link_qual_max) {
870 print_wireless_link_qual_max(obj, p, p_max_size);
872 OBJ(wireless_link_qual_perc) {
873 print_wireless_link_qual_perc(obj, p, p_max_size);
875 OBJ(wireless_link_bar) {
876 print_wireless_link_bar(obj, p, p_max_size);
878 #endif /* HAVE_IWLIB */
880 #endif /* __linux__ */
884 get_adt746x_cpu(p, p_max_size);
887 get_adt746x_fan(p, p_max_size);
890 get_acpi_fan(p, p_max_size);
893 get_acpi_ac_adapter(p, p_max_size);
896 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
899 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
901 OBJ(battery_percent) {
902 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
906 if(output_methods & TO_X) {
907 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
910 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
913 get_battery_short_status(p, p_max_size, obj->data.s);
915 #endif /* __OpenBSD__ */
918 human_readable(cur->buffers * 1024, p, 255);
921 human_readable(cur->cached * 1024, p, 255);
924 if (obj->data.i > info.cpu_count) {
925 NORM_ERR("obj->data.i %i info.cpu_count %i",
926 obj->data.i, info.cpu_count);
927 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
929 percent_print(p, p_max_size,
930 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
934 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
938 if(output_methods & TO_X) {
939 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
942 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
946 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
949 print_loadgraph(obj, p);
953 new_fg(p, obj->data.l);
988 snprintf(p, p_max_size, "%s", VERSION);
990 OBJ(conky_build_date) {
991 snprintf(p, p_max_size, "%s", BUILD_DATE);
993 OBJ(conky_build_arch) {
994 snprintf(p, p_max_size, "%s", BUILD_ARCH);
996 #if defined(__linux__)
998 snprintf(p, p_max_size, "%s",
999 get_disk_protect_queue(obj->data.s));
1002 snprintf(p, p_max_size, "%s", i8k.version);
1005 snprintf(p, p_max_size, "%s", i8k.bios);
1008 snprintf(p, p_max_size, "%s", i8k.serial);
1013 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1014 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1016 OBJ(i8k_left_fan_status) {
1017 int left_fan_status;
1019 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1020 if (left_fan_status == 0) {
1021 snprintf(p, p_max_size, "off");
1023 if (left_fan_status == 1) {
1024 snprintf(p, p_max_size, "low");
1026 if (left_fan_status == 2) {
1027 snprintf(p, p_max_size, "high");
1030 OBJ(i8k_right_fan_status) {
1031 int right_fan_status;
1033 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1034 if (right_fan_status == 0) {
1035 snprintf(p, p_max_size, "off");
1037 if (right_fan_status == 1) {
1038 snprintf(p, p_max_size, "low");
1040 if (right_fan_status == 2) {
1041 snprintf(p, p_max_size, "high");
1044 OBJ(i8k_left_fan_rpm) {
1045 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1047 OBJ(i8k_right_fan_rpm) {
1048 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1050 OBJ(i8k_ac_status) {
1053 sscanf(i8k.ac_status, "%d", &ac_status);
1054 if (ac_status == -1) {
1055 snprintf(p, p_max_size, "disabled (read i8k docs)");
1057 if (ac_status == 0) {
1058 snprintf(p, p_max_size, "off");
1060 if (ac_status == 1) {
1061 snprintf(p, p_max_size, "on");
1064 OBJ(i8k_buttons_status) {
1065 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1069 get_ibm_acpi_fan(p, p_max_size);
1072 print_ibm_temps(obj, p, p_max_size);
1075 get_ibm_acpi_volume(p, p_max_size);
1077 OBJ(ibm_brightness) {
1078 get_ibm_acpi_brightness(p, p_max_size);
1081 /* information from sony_laptop kernel module
1082 * /sys/devices/platform/sony-laptop */
1083 OBJ(sony_fanspeed) {
1084 get_sony_fanspeed(p, p_max_size);
1087 if (!gateway_exists()) {
1092 print_gateway_iface(p, p_max_size);
1095 print_gateway_ip(p, p_max_size);
1098 snprintf(p, p_max_size, "%d", get_laptop_mode());
1101 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1103 #endif /* __linux__ */
1104 #if (defined(__FreeBSD__) || defined(__linux__))
1106 if (!interface_up(obj)) {
1112 OBJ(obsd_sensors_temp) {
1113 print_obsd_sensors_temp(obj, p, p_max_size);
1115 OBJ(obsd_sensors_fan) {
1116 print_obsd_sensors_fan(obj, p, p_max_size);
1118 OBJ(obsd_sensors_volt) {
1119 print_obsd_sensors_volt(obj, p, p_max_size);
1122 get_obsd_vendor(p, p_max_size);
1125 get_obsd_product(p, p_max_size);
1127 #endif /* __OpenBSD__ */
1130 new_font(p, obj->data.s);
1131 need_to_load_fonts = 1;
1135 print_diskio(obj, 0, p, p_max_size);
1138 print_diskio(obj, 1, p, p_max_size);
1141 print_diskio(obj, -1, p, p_max_size);
1145 print_diskiograph(obj, 0, p);
1147 OBJ(diskiograph_read) {
1148 print_diskiograph(obj, -1, p);
1150 OBJ(diskiograph_write) {
1151 print_diskiograph(obj, 1, p);
1155 print_downspeed(obj, p, p_max_size);
1158 print_downspeedf(obj, p, p_max_size);
1161 OBJ(downspeedgraph) {
1162 print_downspeedgraph(obj, p);
1166 /* Since we see you, you're if has not jumped.
1167 * Do Ninja jump here: without leaving traces.
1168 * This is to prevent us from stale jumped flags.
1174 /* harmless object, just ignore */
1177 print_addr(obj, p, p_max_size);
1179 #if defined(__linux__)
1181 print_addrs(obj, p, p_max_size);
1183 #endif /* __linux__ */
1184 #if defined(IMLIB2) && defined(X11)
1186 /* doesn't actually draw anything, just queues it omp. the
1187 * image will get drawn after the X event loop */
1188 cimlib_add_image(obj->data.s);
1192 evaluate(obj->data.s, p);
1195 print_exec(obj, p, p_max_size);
1198 print_execp(obj, p, p_max_size);
1202 print_execgauge(obj, p, p_max_size);
1206 print_execbar(obj, p, p_max_size);
1210 print_execgraph(obj, p, p_max_size);
1214 print_execibar(obj, p, p_max_size);
1218 print_execigraph(obj, p, p_max_size);
1221 print_execigauge(obj, p, p_max_size);
1225 print_execi(obj, p, p_max_size);
1228 print_execpi(obj, p);
1231 print_texeci(obj, p, p_max_size);
1234 print_imap_unseen(obj, p, p_max_size);
1236 OBJ(imap_messages) {
1237 print_imap_messages(obj, p, p_max_size);
1240 print_pop3_unseen(obj, p, p_max_size);
1243 print_pop3_used(obj, p, p_max_size);
1246 print_fs_bar(obj, 0, p, p_max_size);
1249 print_fs_free(obj, p, p_max_size);
1252 print_fs_perc(obj, 1, p, p_max_size);
1255 print_fs_size(obj, p, p_max_size);
1258 print_fs_type(obj, p, p_max_size);
1261 print_fs_used(obj, p, p_max_size);
1264 print_fs_bar(obj, 1, p, p_max_size);
1267 print_fs_perc(obj, 0, p, p_max_size);
1270 print_loadavg(obj, p, p_max_size);
1273 new_goto(p, obj->data.i);
1280 new_hr(p, obj->data.i);
1284 print_nameserver(obj, p, p_max_size);
1288 print_eve(obj, p, p_max_size);
1293 curl_print(obj, p, p_max_size);
1298 rss_print_info(obj, p, p_max_size);
1303 print_weather(obj, p, p_max_size);
1307 OBJ(weather_forecast) {
1308 print_weather_forecast(obj, p, p_max_size);
1313 char *str = llua_getstring(obj->data.s);
1315 snprintf(p, p_max_size, "%s", str);
1320 char *str = llua_getstring(obj->data.s);
1328 if (llua_getnumber(obj->data.s, &per)) {
1330 if(output_methods & TO_X) {
1331 new_bar(obj, p, (per/100.0 * 255));
1334 new_bar_in_shell(obj, p, p_max_size, per);
1340 if (llua_getnumber(obj->data.s, &per)) {
1341 new_graph(obj, p, per);
1346 if (llua_getnumber(obj->data.s, &per)) {
1347 new_gauge(obj, p, (per/100.0 * 255));
1351 #endif /* HAVE_LUA */
1357 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1358 snprintf(p, p_max_size, "N/A");
1360 temp_print(p, p_max_size, (double)val,
1361 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1366 new_offset(p, obj->data.i);
1369 new_voffset(p, obj->data.i);
1373 print_sysfs_sensor(obj, p, p_max_size);
1376 print_sysfs_sensor(obj, p, p_max_size);
1379 print_sysfs_sensor(obj, p, p_max_size);
1381 #endif /* __linux__ */
1383 new_alignr(p, obj->data.i);
1386 new_alignc(p, obj->data.i);
1389 char buf[max_user_text];
1390 struct information *tmp_info =
1391 malloc(sizeof(struct information));
1392 memcpy(tmp_info, cur, sizeof(struct information));
1393 generate_text_internal(buf, max_user_text,
1394 *obj->sub, tmp_info);
1396 if (strlen(buf) != 0) {
1402 char expression[max_user_text];
1404 struct information *tmp_info;
1406 tmp_info = malloc(sizeof(struct information));
1407 memcpy(tmp_info, cur, sizeof(struct information));
1408 generate_text_internal(expression, max_user_text,
1409 *obj->sub, tmp_info);
1410 DBGP("parsed arg into '%s'", expression);
1412 val = compare(expression);
1414 NORM_ERR("compare failed for expression '%s'",
1424 spc = strchr(obj->data.s, ' ');
1425 if (!spc && access(obj->data.s, F_OK)) {
1429 if (check_contains(obj->data.s, spc + 1))
1436 && (!check_mount(obj->data.s))) {
1442 if (!get_process_by_name(obj->data.s)) {
1444 if ((obj->data.s) && system(obj->data.s)) {
1449 #if defined(__linux__)
1451 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1455 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1458 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1463 human_readable(cur->mem * 1024, p, 255);
1466 human_readable(cur->memeasyfree * 1024, p, 255);
1469 human_readable(cur->memfree * 1024, p, 255);
1472 human_readable(cur->memmax * 1024, p, 255);
1476 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1480 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1485 if(output_methods & TO_X) {
1486 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1489 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1493 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1498 print_mixer(obj, 0, p, p_max_size);
1501 print_mixer(obj, -1, p, p_max_size);
1504 print_mixer(obj, 1, p, p_max_size);
1508 print_mixer_bar(obj, 0, p);
1511 print_mixer_bar(obj, -1, p);
1514 print_mixer_bar(obj, 1, p);
1517 OBJ(if_mixer_mute) {
1518 if (!check_mixer_muted(obj)) {
1523 #define NOT_IN_X "Not running in X"
1525 if(x_initialised != YES) {
1526 strncpy(p, NOT_IN_X, p_max_size);
1528 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1531 OBJ(monitor_number) {
1532 if(x_initialised != YES) {
1533 strncpy(p, NOT_IN_X, p_max_size);
1535 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1539 if(x_initialised != YES) {
1540 strncpy(p, NOT_IN_X, p_max_size);
1542 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1545 OBJ(desktop_number) {
1546 if(x_initialised != YES) {
1547 strncpy(p, NOT_IN_X, p_max_size);
1549 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1553 if(x_initialised != YES) {
1554 strncpy(p, NOT_IN_X, p_max_size);
1555 }else if(cur->x11.desktop.name != NULL) {
1556 strncpy(p, cur->x11.desktop.name, p_max_size);
1563 print_mails(obj, p, p_max_size);
1566 print_new_mails(obj, p, p_max_size);
1569 print_seen_mails(obj, p, p_max_size);
1572 print_unseen_mails(obj, p, p_max_size);
1574 OBJ(flagged_mails) {
1575 print_flagged_mails(obj, p, p_max_size);
1577 OBJ(unflagged_mails) {
1578 print_unflagged_mails(obj, p, p_max_size);
1580 OBJ(forwarded_mails) {
1581 print_forwarded_mails(obj, p, p_max_size);
1583 OBJ(unforwarded_mails) {
1584 print_unforwarded_mails(obj, p, p_max_size);
1586 OBJ(replied_mails) {
1587 print_replied_mails(obj, p, p_max_size);
1589 OBJ(unreplied_mails) {
1590 print_unreplied_mails(obj, p, p_max_size);
1593 print_draft_mails(obj, p, p_max_size);
1595 OBJ(trashed_mails) {
1596 print_trashed_mails(obj, p, p_max_size);
1599 print_mboxscan(obj, p, p_max_size);
1602 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1605 new_outline(p, obj->data.l);
1608 print_pid_cmdline(obj, p, p_max_size);
1611 print_pid_cwd(obj, p, p_max_size);
1614 print_pid_environ(obj, p, p_max_size);
1617 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1619 OBJ(running_processes) {
1620 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1623 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1626 snprintf(p, p_max_size, "%s", obj->data.s);
1630 new_bg(p, obj->data.l);
1633 new_stippled_hr(obj, p);
1637 human_readable(cur->swap * 1024, p, 255);
1640 human_readable(cur->swapfree * 1024, p, 255);
1643 human_readable(cur->swapmax * 1024, p, 255);
1646 if (cur->swapmax == 0) {
1647 strncpy(p, "No swap", p_max_size);
1649 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1654 if(output_methods & TO_X) {
1655 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1658 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1661 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1664 print_time(obj, p, p_max_size);
1667 print_utime(obj, p, p_max_size);
1670 print_tztime(obj, p, p_max_size);
1673 print_totaldown(obj, p, p_max_size);
1676 print_totalup(obj, p, p_max_size);
1679 snprintf(p, p_max_size, "%d", total_updates);
1682 if(total_updates % updatereset != obj->data.i - 1) {
1687 print_upspeed(obj, p, p_max_size);
1690 print_upspeedf(obj, p, p_max_size);
1694 print_upspeedgraph(obj, p);
1698 format_seconds_short(p, p_max_size, (int) cur->uptime);
1701 format_seconds(p, p_max_size, (int) cur->uptime);
1704 snprintf(p, p_max_size, "%s", cur->users.names);
1707 snprintf(p, p_max_size, "%s", cur->users.terms);
1710 snprintf(p, p_max_size, "%s", cur->users.times);
1713 snprintf(p, p_max_size, "%d", cur->users.number);
1715 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1716 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1720 msg = get_apm_adapter();
1721 snprintf(p, p_max_size, "%s", msg);
1724 OBJ(apm_battery_life) {
1727 msg = get_apm_battery_life();
1728 snprintf(p, p_max_size, "%s", msg);
1731 OBJ(apm_battery_time) {
1734 msg = get_apm_battery_time();
1735 snprintf(p, p_max_size, "%s", msg);
1738 #endif /* __FreeBSD__ __OpenBSD__ */
1741 #define mpd_printf(fmt, val) \
1742 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1743 #define mpd_sprintf(val) { \
1744 if (!obj->data.i || obj->data.i > p_max_size) \
1745 mpd_printf("%s", val); \
1747 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
1752 mpd_sprintf(artist);
1756 mpd_printf("%s", random);
1758 mpd_printf("%s", repeat);
1766 mpd_printf("%d", volume);
1768 mpd_printf("%d", bitrate);
1770 mpd_printf("%s", status);
1772 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
1775 format_media_player_time(p, p_max_size, mpd_get_info()->length);
1778 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
1782 if(output_methods & TO_X) {
1783 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
1786 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
1789 struct mpd_s *mpd = mpd_get_info();
1790 int len = obj->data.i;
1791 if (len == 0 || len > p_max_size)
1794 memset(p, 0, p_max_size);
1795 if (mpd->artist && *mpd->artist &&
1796 mpd->title && *mpd->title) {
1797 snprintf(p, len, "%s - %s", mpd->artist,
1799 } else if (mpd->title && *mpd->title) {
1800 snprintf(p, len, "%s", mpd->title);
1801 } else if (mpd->artist && *mpd->artist) {
1802 snprintf(p, len, "%s", mpd->artist);
1803 } else if (mpd->file && *mpd->file) {
1804 snprintf(p, len, "%s", mpd->file);
1809 OBJ(if_mpd_playing) {
1810 if (!mpd_get_info()->is_playing) {
1819 #define MOC_PRINT(t, a) \
1820 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1822 MOC_PRINT(state, "??");
1825 MOC_PRINT(file, "no file");
1828 MOC_PRINT(title, "no title");
1831 MOC_PRINT(artist, "no artist");
1834 MOC_PRINT(song, "no song");
1837 MOC_PRINT(album, "no album");
1839 OBJ(moc_totaltime) {
1840 MOC_PRINT(totaltime, "0:00");
1843 MOC_PRINT(timeleft, "0:00");
1846 MOC_PRINT(curtime, "0:00");
1849 MOC_PRINT(bitrate, "0Kbps");
1852 MOC_PRINT(rate, "0KHz");
1858 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
1861 snprintf(p, p_max_size, "%s", cur->xmms2.album);
1864 snprintf(p, p_max_size, "%s", cur->xmms2.title);
1867 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
1869 OBJ(xmms2_comment) {
1870 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
1873 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1876 snprintf(p, p_max_size, "%s", cur->xmms2.status);
1879 snprintf(p, p_max_size, "%s", cur->xmms2.date);
1881 OBJ(xmms2_tracknr) {
1882 if (cur->xmms2.tracknr != -1) {
1883 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
1886 OBJ(xmms2_bitrate) {
1887 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
1890 snprintf(p, p_max_size, "%u", cur->xmms2.id);
1893 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
1895 OBJ(xmms2_elapsed) {
1896 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
1897 (cur->xmms2.elapsed / 1000) % 60);
1899 OBJ(xmms2_duration) {
1900 snprintf(p, p_max_size, "%02d:%02d",
1901 cur->xmms2.duration / 60000,
1902 (cur->xmms2.duration / 1000) % 60);
1904 OBJ(xmms2_percent) {
1905 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
1909 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
1912 OBJ(xmms2_playlist) {
1913 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
1915 OBJ(xmms2_timesplayed) {
1916 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
1919 if (strlen(cur->xmms2.title) < 2
1920 && strlen(cur->xmms2.title) < 2) {
1921 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1923 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
1927 OBJ(if_xmms2_connected) {
1928 if (cur->xmms2.conn_state != 1) {
1934 OBJ(audacious_status) {
1935 snprintf(p, p_max_size, "%s",
1936 cur->audacious.items[AUDACIOUS_STATUS]);
1938 OBJ(audacious_title) {
1939 snprintf(p, cur->audacious.max_title_len > 0
1940 ? cur->audacious.max_title_len : p_max_size, "%s",
1941 cur->audacious.items[AUDACIOUS_TITLE]);
1943 OBJ(audacious_length) {
1944 snprintf(p, p_max_size, "%s",
1945 cur->audacious.items[AUDACIOUS_LENGTH]);
1947 OBJ(audacious_length_seconds) {
1948 snprintf(p, p_max_size, "%s",
1949 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1951 OBJ(audacious_position) {
1952 snprintf(p, p_max_size, "%s",
1953 cur->audacious.items[AUDACIOUS_POSITION]);
1955 OBJ(audacious_position_seconds) {
1956 snprintf(p, p_max_size, "%s",
1957 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
1959 OBJ(audacious_bitrate) {
1960 snprintf(p, p_max_size, "%s",
1961 cur->audacious.items[AUDACIOUS_BITRATE]);
1963 OBJ(audacious_frequency) {
1964 snprintf(p, p_max_size, "%s",
1965 cur->audacious.items[AUDACIOUS_FREQUENCY]);
1967 OBJ(audacious_channels) {
1968 snprintf(p, p_max_size, "%s",
1969 cur->audacious.items[AUDACIOUS_CHANNELS]);
1971 OBJ(audacious_filename) {
1972 snprintf(p, p_max_size, "%s",
1973 cur->audacious.items[AUDACIOUS_FILENAME]);
1975 OBJ(audacious_playlist_length) {
1976 snprintf(p, p_max_size, "%s",
1977 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
1979 OBJ(audacious_playlist_position) {
1980 snprintf(p, p_max_size, "%s",
1981 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
1983 OBJ(audacious_main_volume) {
1984 snprintf(p, p_max_size, "%s",
1985 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
1988 OBJ(audacious_bar) {
1992 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
1993 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1994 new_bar(obj, p, (int) (progress * 255.0f));
1997 #endif /* AUDACIOUS */
2001 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2004 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2007 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2010 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2013 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2016 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2019 /* we have four different types of top (top, top_mem,
2020 * top_time and top_io). To avoid having almost-same code four
2021 * times, we have this special handler. */
2029 /* yes, passing top_name_width instead
2030 * of p_max_size is intended here */
2031 print_top(obj, p, top_name_width);
2033 print_tailhead("tail", obj, p, p_max_size);
2036 print_tailhead("head", obj, p, p_max_size);
2040 FILE *fp = open_file(obj->data.s, &rep);
2043 /* FIXME: use something more general (see also tail.c, head.c */
2044 #define BUFSZ 0x1000
2049 while(fgets(buf, BUFSZ, fp) != NULL){
2050 for(j = 0; buf[j] != 0; j++) {
2051 if(buf[j] == '\n') {
2056 sprintf(p, "%d", lines);
2059 sprintf(p, "File Unreadable");
2065 FILE *fp = open_file(obj->data.s, &rep);
2070 char inword = FALSE;
2073 while(fgets(buf, BUFSZ, fp) != NULL){
2074 for(j = 0; buf[j] != 0; j++) {
2075 if(!isspace(buf[j])) {
2076 if(inword == FALSE) {
2085 sprintf(p, "%d", words);
2088 sprintf(p, "File Unreadable");
2091 #ifdef TCP_PORT_MONITOR
2093 tcp_portmon_action(obj, p, p_max_size);
2095 #endif /* TCP_PORT_MONITOR */
2099 do_iconv_start(obj);
2104 #endif /* HAVE_ICONV */
2106 OBJ(entropy_avail) {
2107 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2110 percent_print(p, p_max_size,
2111 cur->entropy.entropy_avail *
2112 100 / cur->entropy.poolsize);
2114 OBJ(entropy_poolsize) {
2115 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2118 double entropy_perc;
2120 entropy_perc = (double) cur->entropy.entropy_avail /
2121 (double) cur->entropy.poolsize;
2123 if(output_methods & TO_X) {
2124 new_bar(obj, p, (int) (entropy_perc * 255.0f));
2127 new_bar_in_shell(obj, p, p_max_size, (int) (entropy_perc * 100.0f));
2133 s = smapi_get_val(obj->data.s);
2134 snprintf(p, p_max_size, "%s", s);
2138 OBJ(if_smapi_bat_installed) {
2140 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2141 if(!smapi_bat_installed(idx)) {
2145 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2147 OBJ(smapi_bat_perc) {
2149 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2150 val = smapi_bat_installed(idx) ?
2151 smapi_get_bat_int(idx, "remaining_percent") : 0;
2152 percent_print(p, p_max_size, val);
2154 NORM_ERR("argument to smapi_bat_perc must be an integer");
2156 OBJ(smapi_bat_temp) {
2158 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2159 val = smapi_bat_installed(idx) ?
2160 smapi_get_bat_int(idx, "temperature") : 0;
2161 /* temperature is in milli degree celsius */
2162 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2164 NORM_ERR("argument to smapi_bat_temp must be an integer");
2166 OBJ(smapi_bat_power) {
2168 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2169 val = smapi_bat_installed(idx) ?
2170 smapi_get_bat_int(idx, "power_now") : 0;
2171 /* power_now is in mW, set to W with one digit precision */
2172 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2174 NORM_ERR("argument to smapi_bat_power must be an integer");
2177 OBJ(smapi_bat_bar) {
2178 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2179 new_bar(obj, p, (int)
2180 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2188 char buf[max_user_text];
2190 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2191 snprintf(p, p_max_size, "%s", buf);
2197 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2198 char buf[max_user_text];
2201 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2202 snprintf(p, p_max_size, "%s", buf);
2203 if(total_updates % 2) {
2204 for(j=0; p[j] != 0; j++) {
2210 char buf[max_user_text];
2212 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2214 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2215 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2216 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2217 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2218 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2219 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2220 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2222 snprintf(p, p_max_size, "%s", buf);
2225 print_scroll(obj, p, p_max_size, cur);
2228 print_combine(obj, p, cur);
2232 print_nvidia_value(obj, display, p, p_max_size);
2237 /* This is just a meta-object to set host:port */
2240 snprintf(p, p_max_size, "%s",
2241 cur->apcupsd.items[APCUPSD_NAME]);
2243 OBJ(apcupsd_model) {
2244 snprintf(p, p_max_size, "%s",
2245 cur->apcupsd.items[APCUPSD_MODEL]);
2247 OBJ(apcupsd_upsmode) {
2248 snprintf(p, p_max_size, "%s",
2249 cur->apcupsd.items[APCUPSD_UPSMODE]);
2251 OBJ(apcupsd_cable) {
2252 snprintf(p, p_max_size, "%s",
2253 cur->apcupsd.items[APCUPSD_CABLE]);
2255 OBJ(apcupsd_status) {
2256 snprintf(p, p_max_size, "%s",
2257 cur->apcupsd.items[APCUPSD_STATUS]);
2259 OBJ(apcupsd_linev) {
2260 snprintf(p, p_max_size, "%s",
2261 cur->apcupsd.items[APCUPSD_LINEV]);
2264 snprintf(p, p_max_size, "%s",
2265 cur->apcupsd.items[APCUPSD_LOAD]);
2267 OBJ(apcupsd_loadbar) {
2270 if(output_methods & TO_X) {
2271 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2272 new_bar(obj, p, (int) progress);
2276 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2277 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2281 OBJ(apcupsd_loadgraph) {
2283 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2284 new_graph(obj, p, (int)progress);
2286 OBJ(apcupsd_loadgauge) {
2288 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2289 new_gauge(obj, p, (int)progress);
2292 OBJ(apcupsd_charge) {
2293 snprintf(p, p_max_size, "%s",
2294 cur->apcupsd.items[APCUPSD_CHARGE]);
2296 OBJ(apcupsd_timeleft) {
2297 snprintf(p, p_max_size, "%s",
2298 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2301 snprintf(p, p_max_size, "%s",
2302 cur->apcupsd.items[APCUPSD_TEMP]);
2304 OBJ(apcupsd_lastxfer) {
2305 snprintf(p, p_max_size, "%s",
2306 cur->apcupsd.items[APCUPSD_LASTXFER]);
2308 #endif /* APCUPSD */
2315 size_t a = strlen(p);
2318 iconv_convert(&a, buff_in, p, p_max_size);
2319 #endif /* HAVE_ICONV */
2320 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2322 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2323 #endif /* HAVE_LUA */
2325 substitute_newlines(p, a - 2);
2334 /* load any new fonts we may have had */
2335 if (need_to_load_fonts) {
2341 void evaluate(const char *text, char *buffer)
2343 struct information *tmp_info;
2344 struct text_object subroot;
2346 tmp_info = malloc(sizeof(struct information));
2347 memcpy(tmp_info, &info, sizeof(struct information));
2348 parse_conky_vars(&subroot, text, buffer, tmp_info);
2349 DBGP("evaluated '%s' to '%s'", text, buffer);
2351 free_text_objects(&subroot, 1);
2355 double current_update_time, next_update_time, last_update_time;
2357 static void generate_text(void)
2359 struct information *cur = &info;
2366 current_update_time = get_time();
2370 /* add things to the buffer */
2376 generate_text_internal(p, max_user_text, global_root_object, cur);
2378 if (stuff_in_uppercase) {
2381 tmp_p = text_buffer;
2383 *tmp_p = toupper(*tmp_p);
2388 next_update_time += update_interval;
2389 if (next_update_time < get_time()) {
2390 next_update_time = get_time() + update_interval;
2391 } else if (next_update_time > get_time() + update_interval) {
2392 next_update_time = get_time() + update_interval;
2394 last_update_time = current_update_time;
2398 void set_update_interval(double interval)
2400 update_interval = interval;
2401 update_interval_old = interval;
2404 static inline int get_string_width(const char *s)
2406 return *s ? calc_text_width(s) : 0;
2410 static int get_string_width_special(char *s, int special_index)
2420 if ((output_methods & TO_X) == 0)
2423 p = strndup(s, text_buffer_size);
2427 if (*p == SPECIAL_CHAR) {
2428 /* shift everything over by 1 so that the special char
2429 * doesn't mess up the size calculation */
2430 for (i = 0; i < (long)strlen(p); i++) {
2431 *(p + i) = *(p + i + 1);
2433 if (specials[special_index + idx].type == GRAPH
2434 || specials[special_index + idx].type == GAUGE
2435 || specials[special_index + idx].type == BAR) {
2436 width += specials[special_index + idx].width;
2439 } else if (*p == SECRIT_MULTILINE_CHAR) {
2446 if (strlen(final) > 1) {
2447 width += calc_text_width(final);
2453 static int text_size_updater(char *s, int special_index);
2455 int last_font_height;
2456 static void update_text_area(void)
2460 if ((output_methods & TO_X) == 0)
2462 /* update text size if it isn't fixed */
2467 text_width = minimum_width;
2469 last_font_height = font_height();
2470 for_each_line(text_buffer, text_size_updater);
2472 if (text_height < minimum_height) {
2473 text_height = minimum_height;
2475 if (text_width > maximum_width && maximum_width > 0) {
2476 text_width = maximum_width;
2480 /* get text position on workarea */
2481 switch (text_alignment) {
2488 x = workarea[2] - text_width - gap_x;
2493 x = workarea[2] / 2 - text_width / 2 - gap_x;
2500 y = workarea[3] - text_height - gap_y;
2504 x = workarea[2] - text_width - gap_x;
2505 y = workarea[3] - text_height - gap_y;
2509 x = workarea[2] / 2 - text_width / 2 - gap_x;
2510 y = workarea[3] - text_height - gap_y;
2515 y = workarea[3] / 2 - text_height / 2 - gap_y;
2519 x = workarea[2] - text_width - gap_x;
2520 y = workarea[3] / 2 - text_height / 2 - gap_y;
2524 case NONE: // Let the WM manage the window
2535 if (own_window && !fixed_pos) {
2538 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2539 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2540 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2541 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2545 /* If window size doesn't match to workarea's size,
2546 * then window probably includes panels (gnome).
2547 * Blah, doesn't work on KDE. */
2548 if (workarea[2] != window.width || workarea[3] != window.height) {
2557 /* update lua window globals */
2558 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2559 #endif /* HAVE_LUA */
2564 static int cur_x, cur_y; /* current x and y for drawing */
2566 //draw_mode also without X11 because we only need to print to stdout with FG
2567 static int draw_mode; /* FG, BG or OUTLINE */
2569 static long current_color;
2571 static int text_size_updater(char *s, int special_index)
2576 if ((output_methods & TO_X) == 0)
2578 /* get string widths and skip specials */
2581 if (*p == SPECIAL_CHAR) {
2583 w += get_string_width(s);
2586 if (specials[special_index].type == BAR
2587 || specials[special_index].type == GAUGE
2588 || specials[special_index].type == GRAPH) {
2589 w += specials[special_index].width;
2590 if (specials[special_index].height > last_font_height) {
2591 last_font_height = specials[special_index].height;
2592 last_font_height += font_height();
2594 } else if (specials[special_index].type == OFFSET) {
2595 if (specials[special_index].arg > 0) {
2596 w += specials[special_index].arg;
2598 } else if (specials[special_index].type == VOFFSET) {
2599 last_font_height += specials[special_index].arg;
2600 } else if (specials[special_index].type == GOTO) {
2601 if (specials[special_index].arg > cur_x) {
2602 w = (int) specials[special_index].arg;
2604 } else if (specials[special_index].type == TAB) {
2605 int start = specials[special_index].arg;
2606 int step = specials[special_index].width;
2608 if (!step || step < 0) {
2611 w += step - (cur_x - text_start_x - start) % step;
2612 } else if (specials[special_index].type == FONT) {
2613 selected_font = specials[special_index].font_added;
2614 if (font_height() > last_font_height) {
2615 last_font_height = font_height();
2621 } else if (*p == SECRIT_MULTILINE_CHAR) {
2624 lw = get_string_width(s);
2625 *p = SECRIT_MULTILINE_CHAR;
2627 w = lw > w ? lw : w;
2628 text_height += last_font_height;
2632 w += get_string_width(s);
2633 if (w > text_width) {
2636 if (text_width > maximum_width && maximum_width) {
2637 text_width = maximum_width;
2640 text_height += last_font_height;
2641 last_font_height = font_height();
2642 return special_index;
2646 static inline void set_foreground_color(long c)
2649 if (output_methods & TO_X) {
2651 XSetForeground(display, window.gc, c);
2655 if (output_methods & TO_NCURSES) {
2656 attron(COLOR_PAIR(c));
2658 #endif /* NCURSES */
2663 static void draw_string(const char *s)
2665 int i, i2, pos, width_of_s;
2668 char *s_with_newlines;
2674 width_of_s = get_string_width(s);
2675 s_with_newlines = strdup(s);
2676 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2677 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2678 s_with_newlines[i] = '\n';
2681 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2682 printf("%s\n", s_with_newlines);
2683 if (extra_newline) fputc('\n', stdout);
2684 fflush(stdout); /* output immediately, don't buffer */
2686 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2687 fprintf(stderr, "%s\n", s_with_newlines);
2688 fflush(stderr); /* output immediately, don't buffer */
2690 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2691 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2693 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2694 fprintf(append_fpointer, "%s\n", s_with_newlines);
2697 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2698 printw("%s", s_with_newlines);
2701 free(s_with_newlines);
2702 memset(tmpstring1, 0, text_buffer_size);
2703 memset(tmpstring2, 0, text_buffer_size);
2704 strncpy(tmpstring1, s, text_buffer_size - 1);
2709 if (output_methods & TO_X) {
2710 max = ((text_width - width_of_s) / get_string_width(" "));
2713 /* This code looks for tabs in the text and coverts them to spaces.
2714 * The trick is getting the correct number of spaces, and not going
2715 * over the window's size without forcing the window larger. */
2716 for (i = 0; i < (int) text_buffer_size; i++) {
2717 if (tmpstring1[i] == '\t') {
2719 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2720 /* guard against overrun */
2721 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2726 /* guard against overrun */
2727 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2732 if (output_methods & TO_X) {
2733 if (text_width == maximum_width) {
2734 /* this means the text is probably pushing the limit,
2735 * so we'll chop it */
2736 while (cur_x + get_string_width(tmpstring2) - text_start_x
2737 > maximum_width && strlen(tmpstring2) > 0) {
2738 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2745 if (output_methods & TO_X) {
2751 c.pixel = current_color;
2752 XQueryColor(display, DefaultColormap(display, screen), &c);
2755 c2.color.red = c.red;
2756 c2.color.green = c.green;
2757 c2.color.blue = c.blue;
2758 c2.color.alpha = fonts[selected_font].font_alpha;
2760 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2761 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2763 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2764 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2769 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2772 cur_x += width_of_s;
2775 memcpy(tmpstring1, s, text_buffer_size);
2778 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2786 int last_special_needed = -1;
2787 int orig_special_index = special_index;
2790 if (output_methods & TO_X) {
2791 font_h = font_height();
2792 cur_y += font_ascent();
2794 cur_x = text_start_x;
2798 if (*p == SECRIT_MULTILINE_CHAR) {
2799 /* special newline marker for multiline objects */
2804 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2809 /* draw string before special, unless we're dealing multiline
2811 if (last_special_applied > -1) {
2812 special_index = last_special_applied;
2820 switch (specials[special_index].type) {
2822 case HORIZONTAL_LINE:
2824 int h = specials[special_index].height;
2825 int mid = font_ascent() / 2;
2827 w = text_start_x + text_width - cur_x;
2829 XSetLineAttributes(display, window.gc, h, LineSolid,
2830 CapButt, JoinMiter);
2831 XDrawLine(display, window.drawable, window.gc, cur_x,
2832 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2838 int h = specials[special_index].height;
2839 int tmp_s = specials[special_index].arg;
2840 int mid = font_ascent() / 2;
2841 char ss[2] = { tmp_s, tmp_s };
2843 w = text_start_x + text_width - cur_x - 1;
2844 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2845 CapButt, JoinMiter);
2846 XSetDashes(display, window.gc, 0, ss, 2);
2847 XDrawLine(display, window.drawable, window.gc, cur_x,
2848 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2854 int h, bar_usage, by;
2855 if (cur_x - text_start_x > maximum_width
2856 && maximum_width > 0) {
2859 h = specials[special_index].height;
2860 bar_usage = specials[special_index].arg;
2861 by = cur_y - (font_ascent() / 2) - 1;
2866 w = specials[special_index].width;
2868 w = text_start_x + text_width - cur_x - 1;
2874 XSetLineAttributes(display, window.gc, 1, LineSolid,
2875 CapButt, JoinMiter);
2877 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2879 XFillRectangle(display, window.drawable, window.gc, cur_x,
2880 by, w * bar_usage / 255, h);
2888 case GAUGE: /* new GAUGE */
2891 unsigned long last_colour = current_color;
2893 float angle, px, py;
2897 if (cur_x - text_start_x > maximum_width
2898 && maximum_width > 0) {
2902 h = specials[special_index].height;
2903 by = cur_y - (font_ascent() / 2) - 1;
2908 w = specials[special_index].width;
2910 w = text_start_x + text_width - cur_x - 1;
2916 XSetLineAttributes(display, window.gc, 1, LineSolid,
2917 CapButt, JoinMiter);
2919 XDrawArc(display, window.drawable, window.gc,
2920 cur_x, by, w, h * 2, 0, 180*64);
2923 usage = specials[special_index].arg;
2924 angle = (M_PI)*(float)(usage)/255.;
2925 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2926 py = (float)(by+(h))-(float)(h)*sin(angle);
2928 XDrawLine(display, window.drawable, window.gc,
2929 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2937 set_foreground_color(last_colour);
2945 int h, by, i = 0, j = 0;
2947 unsigned long last_colour = current_color;
2948 unsigned long *tmpcolour = 0;
2949 if (cur_x - text_start_x > maximum_width
2950 && maximum_width > 0) {
2953 h = specials[special_index].height;
2954 by = cur_y - (font_ascent() / 2) - 1;
2959 w = specials[special_index].width;
2961 w = text_start_x + text_width - cur_x - 1;
2966 if (draw_graph_borders) {
2967 XSetLineAttributes(display, window.gc, 1, LineSolid,
2968 CapButt, JoinMiter);
2969 XDrawRectangle(display, window.drawable, window.gc,
2972 XSetLineAttributes(display, window.gc, 1, LineSolid,
2973 CapButt, JoinMiter);
2975 if (specials[special_index].last_colour != 0
2976 || specials[special_index].first_colour != 0) {
2977 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
2980 for (i = w - 2; i > -1; i--) {
2981 if (specials[special_index].last_colour != 0
2982 || specials[special_index].first_colour != 0) {
2983 if (specials[special_index].tempgrad) {
2986 (int)((float)(w - 2) - specials[special_index].graph[j] *
2987 (w - 2) / (float)specials[special_index].graph_scale)
2991 (int)((float)(w - 2) - specials[special_index].graph[j] *
2992 (w - 2) / (float)specials[special_index].graph_scale)
2995 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
2997 (int)((float)(w - 2) - specials[special_index].graph[j] *
2998 (w - 2) / (float)specials[special_index].graph_scale)
3002 #endif /* DEBUG_lol */
3003 XSetForeground(display, window.gc, tmpcolour[
3004 (int)((float)(w - 2) - specials[special_index].graph[j] *
3005 (w - 2) / (float)specials[special_index].graph_scale)
3008 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3011 /* this is mugfugly, but it works */
3012 XDrawLine(display, window.drawable, window.gc,
3013 cur_x + i + 1, by + h, cur_x + i + 1,
3014 round_to_int((double)by + h - specials[special_index].graph[j] *
3015 (h - 1) / specials[special_index].graph_scale));
3016 if ((w - i) / ((float) (w - 2) /
3017 (specials[special_index].graph_width)) > j
3018 && j < MAX_GRAPH_DEPTH - 3) {
3022 if (tmpcolour) free(tmpcolour);
3027 /* if (draw_mode == BG) {
3028 set_foreground_color(default_bg_color);
3029 } else if (draw_mode == OUTLINE) {
3030 set_foreground_color(default_out_color);
3032 set_foreground_color(default_fg_color);
3034 if (show_graph_range) {
3037 unsigned short int seconds = update_interval * w;
3043 unsigned short int timeunits;
3045 timeunits = seconds / 86400; seconds %= 86400;
3046 if (timeunits > 0) {
3047 asprintf(&tmp_day_str, "%dd", timeunits);
3049 tmp_day_str = strdup("");
3051 timeunits = seconds / 3600; seconds %= 3600;
3052 if (timeunits > 0) {
3053 asprintf(&tmp_hour_str, "%dh", timeunits);
3055 tmp_hour_str = strdup("");
3057 timeunits = seconds / 60; seconds %= 60;
3058 if (timeunits > 0) {
3059 asprintf(&tmp_min_str, "%dm", timeunits);
3061 tmp_min_str = strdup("");
3064 asprintf(&tmp_sec_str, "%ds", seconds);
3066 tmp_sec_str = strdup("");
3068 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3069 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3071 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3073 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3074 cur_y += font_h / 2;
3075 draw_string(tmp_str);
3081 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3085 cur_x += font_ascent() / 2;
3086 cur_y += font_h / 2;
3088 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3090 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3091 draw_string(tmp_str);
3097 set_foreground_color(last_colour);
3103 int old = font_ascent();
3105 cur_y -= font_ascent();
3106 selected_font = specials[special_index].font_added;
3108 if (cur_y + font_ascent() < cur_y + old) {
3111 cur_y += font_ascent();
3113 font_h = font_height();
3118 if (draw_mode == FG) {
3119 set_foreground_color(specials[special_index].arg);
3125 if (draw_mode == BG) {
3126 set_foreground_color(specials[special_index].arg);
3131 if (draw_mode == OUTLINE) {
3132 set_foreground_color(specials[special_index].arg);
3137 w += specials[special_index].arg;
3138 last_special_needed = special_index;
3142 cur_y += specials[special_index].arg;
3146 if (specials[special_index].arg >= 0) {
3147 cur_x = (int) specials[special_index].arg;
3149 last_special_needed = special_index;
3154 int start = specials[special_index].arg;
3155 int step = specials[special_index].width;
3157 if (!step || step < 0) {
3160 w = step - (cur_x - text_start_x - start) % step;
3161 last_special_needed = special_index;
3167 /* TODO: add back in "+ window.border_inner_margin" to the end of
3169 int pos_x = text_start_x + text_width -
3170 get_string_width_special(s, special_index);
3172 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3173 "get_string_width(p) %i gap_x %i "
3174 "specials[special_index].arg %i window.border_inner_margin %i "
3175 "window.border_width %i\n", pos_x, text_start_x, text_width,
3176 cur_x, get_string_width_special(s), gap_x,
3177 specials[special_index].arg, window.border_inner_margin,
3178 window.border_width); */
3179 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3180 cur_x = pos_x - specials[special_index].arg;
3182 last_special_needed = special_index;
3188 int pos_x = (text_width) / 2 - get_string_width_special(s,
3189 special_index) / 2 - (cur_x -
3191 /* int pos_x = text_start_x + text_width / 2 -
3192 get_string_width_special(s) / 2; */
3194 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3195 "get_string_width(p) %i gap_x %i "
3196 "specials[special_index].arg %i\n", pos_x, text_start_x,
3197 text_width, cur_x, get_string_width(s), gap_x,
3198 specials[special_index].arg); */
3199 if (pos_x > specials[special_index].arg) {
3200 w = pos_x - specials[special_index].arg;
3202 last_special_needed = special_index;
3212 if (special_index != last_special_applied) {
3215 special_index = orig_special_index;
3216 last_special_applied = -1;
3227 if (output_methods & TO_NCURSES) {
3230 #endif /* NCURSES */
3232 if (output_methods & TO_X)
3233 cur_y += font_descent();
3235 if (recurse && *recurse) {
3236 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3237 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3239 return special_index;
3242 static int draw_line(char *s, int special_index)
3245 if (output_methods & TO_X) {
3246 return draw_each_line_inner(s, special_index, -1);
3250 if (output_methods & TO_NCURSES) {
3251 return draw_each_line_inner(s, special_index, -1);
3253 #endif /* NCURSES */
3255 UNUSED(special_index);
3259 static void draw_text(void)
3263 llua_draw_pre_hook();
3264 #endif /* HAVE_LUA */
3265 if (output_methods & TO_X) {
3266 cur_y = text_start_y;
3269 if (draw_borders && window.border_width > 0) {
3270 if (stippled_borders) {
3271 char ss[2] = { stippled_borders, stippled_borders };
3272 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3273 CapButt, JoinMiter);
3274 XSetDashes(display, window.gc, 0, ss, 2);
3276 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3277 CapButt, JoinMiter);
3280 XDrawRectangle(display, window.drawable, window.gc,
3281 text_start_x - window.border_inner_margin - window.border_width,
3282 text_start_y - window.border_inner_margin - window.border_width,
3283 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3284 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3292 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3293 attron(COLOR_PAIR(COLOR_WHITE));
3294 #endif /* NCURSES */
3295 for_each_line(text_buffer, draw_line);
3296 #if defined(HAVE_LUA) && defined(X11)
3297 llua_draw_post_hook();
3298 #endif /* HAVE_LUA */
3301 static void draw_stuff(void)
3304 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3306 if (overwrite_file) {
3307 overwrite_fpointer = fopen(overwrite_file, "w");
3308 if(!overwrite_fpointer)
3309 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3312 append_fpointer = fopen(append_file, "a");
3313 if(!append_fpointer)
3314 NORM_ERR("Can't append '%s' anymore", append_file);
3317 if (output_methods & TO_X) {
3319 if (draw_shades && !draw_outline) {
3322 set_foreground_color(default_bg_color);
3333 for (i = -1; i < 2; i++) {
3334 for (j = -1; j < 2; j++) {
3335 if (i == 0 && j == 0) {
3340 set_foreground_color(default_out_color);
3341 draw_mode = OUTLINE;
3349 set_foreground_color(default_fg_color);
3354 #if defined(X11) && defined(HAVE_XDBE)
3355 if (output_methods & TO_X) {
3356 xdbe_swap_buffers();
3358 #endif /* X11 && HAVE_XDBE */
3359 if(overwrite_fpointer) {
3360 fclose(overwrite_fpointer);
3361 overwrite_fpointer = 0;
3363 if(append_fpointer) {
3364 fclose(append_fpointer);
3365 append_fpointer = 0;
3370 static void clear_text(int exposures)
3374 /* The swap action is XdbeBackground, which clears */
3378 if (display && window.window) { // make sure these are !null
3379 /* there is some extra space for borders and outlines */
3380 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3381 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3382 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3383 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3388 static int need_to_update;
3390 /* update_text() generates new text and clears old text area */
3391 static void update_text(void)
3398 if (output_methods & TO_X)
3403 llua_update_info(&info, update_interval);
3404 #endif /* HAVE_LUA */
3407 #ifdef HAVE_SYS_INOTIFY_H
3411 static void main_loop(void)
3414 #ifdef SIGNAL_BLOCKING
3415 sigset_t newmask, oldmask;
3418 #ifdef HAVE_SYS_INOTIFY_H
3419 int inotify_config_wd = -1;
3420 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3421 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3422 char inotify_buff[INOTIFY_BUF_LEN];
3423 #endif /* HAVE_SYS_INOTIFY_H */
3426 #ifdef SIGNAL_BLOCKING
3427 sigemptyset(&newmask);
3428 sigaddset(&newmask, SIGINT);
3429 sigaddset(&newmask, SIGTERM);
3430 sigaddset(&newmask, SIGUSR1);
3433 last_update_time = 0.0;
3434 next_update_time = get_time();
3436 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3437 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3438 char buf[max_user_text];
3440 get_battery_short_status(buf, max_user_text, "BAT0");
3442 update_interval = update_interval_bat;
3444 update_interval = update_interval_old;
3449 #ifdef SIGNAL_BLOCKING
3450 /* block signals. we will inspect for pending signals later */
3451 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3452 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3457 if (output_methods & TO_X) {
3460 /* wait for X event or timeout */
3462 if (!XPending(display)) {
3466 t = next_update_time - get_time();
3470 } else if (t > update_interval) {
3471 t = update_interval;
3474 tv.tv_sec = (long) t;
3475 tv.tv_usec = (long) (t * 1000000) % 1000000;
3477 FD_SET(ConnectionNumber(display), &fdsr);
3479 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3481 if (errno != EINTR) {
3482 NORM_ERR("can't select(): %s", strerror(errno));
3492 if (need_to_update) {
3494 int wx = window.x, wy = window.y;
3504 /* resize window if it isn't right size */
3506 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3507 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3508 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3509 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3510 draw_stuff(); /* redraw everything in our newly sized window */
3511 XResizeWindow(display, window.window, window.width,
3512 window.height); /* resize window */
3513 set_transparent_background(window.window);
3516 xdbe_swap_buffers();
3521 /* update lua window globals */
3522 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3523 #endif /* HAVE_LUA */
3526 /* move window if it isn't in right position */
3527 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3528 XMoveWindow(display, window.window, window.x, window.y);
3533 if (changed && window.type == TYPE_PANEL) {
3536 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3539 switch (text_alignment) {
3566 set_struts(sidenum);
3577 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3578 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3579 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3580 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3581 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3586 /* handle X events */
3587 while (XPending(display)) {
3590 XNextEvent(display, &ev);
3597 r.width = ev.xexpose.width;
3598 r.height = ev.xexpose.height;
3599 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3603 case PropertyNotify:
3605 if ( ev.xproperty.state == PropertyNewValue ) {
3606 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3612 case ReparentNotify:
3613 /* set background to ParentRelative for all parents */
3615 set_transparent_background(window.window);
3619 case ConfigureNotify:
3621 /* if window size isn't what expected, set fixed size */
3622 if (ev.xconfigure.width != window.width
3623 || ev.xconfigure.height != window.height) {
3624 if (window.width != 0 && window.height != 0) {
3628 /* clear old stuff before screwing up
3633 XWindowAttributes attrs;
3634 if (XGetWindowAttributes(display,
3635 window.window, &attrs)) {
3636 window.width = attrs.width;
3637 window.height = attrs.height;
3641 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3642 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3643 if (text_width > maximum_width
3644 && maximum_width > 0) {
3645 text_width = maximum_width;
3649 /* if position isn't what expected, set fixed pos
3650 * total_updates avoids setting fixed_pos when window
3651 * is set to weird locations when started */
3652 /* // this is broken
3653 if (total_updates >= 2 && !fixed_pos
3654 && (window.x != ev.xconfigure.x
3655 || window.y != ev.xconfigure.y)
3656 && (ev.xconfigure.x != 0
3657 || ev.xconfigure.y != 0)) {
3665 /* if an ordinary window with decorations */
3666 if ((window.type == TYPE_NORMAL &&
3667 (!TEST_HINT(window.hints,
3668 HINT_UNDECORATED))) ||
3669 window.type == TYPE_DESKTOP) {
3670 /* allow conky to hold input focus. */
3673 /* forward the click to the desktop window */
3674 XUngrabPointer(display, ev.xbutton.time);
3675 ev.xbutton.window = window.desktop;
3676 ev.xbutton.x = ev.xbutton.x_root;
3677 ev.xbutton.y = ev.xbutton.y_root;
3678 XSendEvent(display, ev.xbutton.window, False,
3679 ButtonPressMask, &ev);
3680 XSetInputFocus(display, ev.xbutton.window,
3681 RevertToParent, ev.xbutton.time);
3688 /* if an ordinary window with decorations */
3689 if ((window.type == TYPE_NORMAL)
3690 && (!TEST_HINT(window.hints,
3691 HINT_UNDECORATED))) {
3692 /* allow conky to hold input focus. */
3695 /* forward the release to the desktop window */
3696 ev.xbutton.window = window.desktop;
3697 ev.xbutton.x = ev.xbutton.x_root;
3698 ev.xbutton.y = ev.xbutton.y_root;
3699 XSendEvent(display, ev.xbutton.window, False,
3700 ButtonReleaseMask, &ev);
3709 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3710 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3712 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3713 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3715 #endif /* HAVE_XDAMAGE */
3721 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3722 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3723 #endif /* HAVE_XDAMAGE */
3725 /* XDBE doesn't seem to provide a way to clear the back buffer
3726 * without interfering with the front buffer, other than passing
3727 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3728 * using XDBE, we need to redraw the text even if it wasn't part of
3729 * the exposed area. OTOH, if we're not going to call draw_stuff at
3730 * all, then no swap happens and we can safely do nothing. */
3732 if (!XEmptyRegion(x11_stuff.region)) {
3737 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3738 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3739 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3740 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3741 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3744 XSetRegion(display, window.gc, x11_stuff.region);
3747 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3751 XDestroyRegion(x11_stuff.region);
3752 x11_stuff.region = XCreateRegion();
3756 t = (next_update_time - get_time()) * 1000000;
3757 if(t > 0) usleep((useconds_t)t);
3761 if(output_methods & TO_NCURSES) {
3770 #ifdef SIGNAL_BLOCKING
3771 /* unblock signals of interest and let handler fly */
3772 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3773 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3777 switch (g_signal_pending) {
3780 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3785 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3788 if (output_methods & TO_X) {
3789 XDestroyRegion(x11_stuff.region);
3790 x11_stuff.region = NULL;
3792 XDamageDestroy(display, x11_stuff.damage);
3793 XFixesDestroyRegion(display, x11_stuff.region2);
3794 XFixesDestroyRegion(display, x11_stuff.part);
3795 #endif /* HAVE_XDAMAGE */
3801 if(overwrite_file) {
3802 free(overwrite_file);
3811 /* Reaching here means someone set a signal
3812 * (SIGXXXX, signal_handler), but didn't write any code
3814 * If you don't want to handle a signal, don't set a handler on
3815 * it in the first place. */
3816 if (g_signal_pending) {
3817 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3821 #ifdef HAVE_SYS_INOTIFY_H
3822 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3823 inotify_config_wd = inotify_add_watch(inotify_fd,
3827 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3828 int len = 0, idx = 0;
3830 struct timeval time_to_wait;
3832 FD_ZERO(&descriptors);
3833 FD_SET(inotify_fd, &descriptors);
3835 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3837 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3838 if (FD_ISSET(inotify_fd, &descriptors)) {
3839 /* process inotify events */
3840 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3841 while (len > 0 && idx < len) {
3842 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3843 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3844 /* current_config should be reloaded */
3845 NORM_ERR("'%s' modified, reloading...", current_config);
3847 if (ev->mask & IN_IGNORED) {
3848 /* for some reason we get IN_IGNORED here
3849 * sometimes, so we need to re-add the watch */
3850 inotify_config_wd = inotify_add_watch(inotify_fd,
3857 llua_inotify_query(ev->wd, ev->mask);
3859 #endif /* HAVE_LUA */
3860 idx += INOTIFY_EVENT_SIZE + ev->len;
3864 #endif /* HAVE_SYS_INOTIFY_H */
3867 llua_update_info(&info, update_interval);
3868 #endif /* HAVE_LUA */
3869 g_signal_pending = 0;
3871 clean_up(NULL, NULL);
3873 #ifdef HAVE_SYS_INOTIFY_H
3874 if (inotify_fd != -1) {
3875 inotify_rm_watch(inotify_fd, inotify_config_wd);
3877 inotify_fd = inotify_config_wd = 0;
3879 #endif /* HAVE_SYS_INOTIFY_H */
3883 static void load_config_file_x11(const char *);
3885 void initialisation(int argc, char** argv);
3887 /* reload the config file */
3888 static void reload_config(void)
3890 char *current_config_copy = strdup(current_config);
3891 clean_up(NULL, NULL);
3892 current_config = current_config_copy;
3893 initialisation(argc_copy, argv_copy);
3896 void clean_up(void *memtofree1, void* memtofree2)
3901 if(output_methods & TO_NCURSES) {
3905 conftree_empty(currentconffile);
3906 currentconffile = NULL;
3913 timed_thread_destroy_registered_threads();
3915 if (info.cpu_usage) {
3916 free(info.cpu_usage);
3917 info.cpu_usage = NULL;
3920 if (x_initialised == YES) {
3921 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3922 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3923 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3924 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3927 if(x11_stuff.region) {
3928 XDestroyRegion(x11_stuff.region);
3929 x11_stuff.region = NULL;
3931 XCloseDisplay(display);
3933 if(info.x11.desktop.all_names) {
3934 free(info.x11.desktop.all_names);
3935 info.x11.desktop.all_names = NULL;
3937 if (info.x11.desktop.name) {
3938 free(info.x11.desktop.name);
3939 info.x11.desktop.name = NULL;
3943 free(fonts); //in set_default_configurations a font is set but not loaded
3949 free_update_callbacks();
3953 free_text_objects(&global_root_object, 0);
3972 free(current_config);
3975 #ifdef TCP_PORT_MONITOR
3976 tcp_portmon_clear();
3985 weather_free_info();
3988 llua_shutdown_hook();
3990 #endif /* HAVE_LUA */
3992 if (output_methods & TO_X)
4000 for (i = 0; i < special_count; i++) {
4001 if (specials[i].type == GRAPH) {
4002 free(specials[i].graph);
4010 clear_diskio_stats();
4011 if(global_cpu != NULL) {
4017 static int string_to_bool(const char *s)
4020 // Assumes an option without a true/false means true
4022 } else if (strcasecmp(s, "yes") == EQUAL) {
4024 } else if (strcasecmp(s, "true") == EQUAL) {
4026 } else if (strcasecmp(s, "1") == EQUAL) {
4033 static enum alignment string_to_alignment(const char *s)
4035 if (strcasecmp(s, "top_left") == EQUAL) {
4037 } else if (strcasecmp(s, "top_right") == EQUAL) {
4039 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4041 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4043 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4044 return BOTTOM_RIGHT;
4045 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4046 return BOTTOM_MIDDLE;
4047 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4049 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4050 return MIDDLE_RIGHT;
4051 } else if (strcasecmp(s, "tl") == EQUAL) {
4053 } else if (strcasecmp(s, "tr") == EQUAL) {
4055 } else if (strcasecmp(s, "tm") == EQUAL) {
4057 } else if (strcasecmp(s, "bl") == EQUAL) {
4059 } else if (strcasecmp(s, "br") == EQUAL) {
4060 return BOTTOM_RIGHT;
4061 } else if (strcasecmp(s, "bm") == EQUAL) {
4062 return BOTTOM_MIDDLE;
4063 } else if (strcasecmp(s, "ml") == EQUAL) {
4065 } else if (strcasecmp(s, "mr") == EQUAL) {
4066 return MIDDLE_RIGHT;
4067 } else if (strcasecmp(s, "none") == EQUAL) {
4075 static void set_default_configurations_for_x(void)
4077 default_fg_color = WhitePixel(display, screen);
4078 default_bg_color = BlackPixel(display, screen);
4079 default_out_color = BlackPixel(display, screen);
4080 color0 = default_fg_color;
4081 color1 = default_fg_color;
4082 color2 = default_fg_color;
4083 color3 = default_fg_color;
4084 color4 = default_fg_color;
4085 color5 = default_fg_color;
4086 color6 = default_fg_color;
4087 color7 = default_fg_color;
4088 color8 = default_fg_color;
4089 color9 = default_fg_color;
4090 current_text_color = default_fg_color;
4094 static void set_default_configurations(void)
4101 fork_to_background = 0;
4102 total_run_times = 0;
4103 info.cpu_avg_samples = 2;
4104 info.net_avg_samples = 2;
4105 info.diskio_avg_samples = 2;
4110 format_human_readable = 1;
4120 mpd_env_host = getenv("MPD_HOST");
4121 mpd_env_port = getenv("MPD_PORT");
4123 if (!mpd_env_host || !strlen(mpd_env_host)) {
4124 mpd_set_host("localhost");
4126 /* MPD_HOST environment variable is set */
4127 char *mpd_hostpart = strchr(mpd_env_host, '@');
4128 if (!mpd_hostpart) {
4129 mpd_set_host(mpd_env_host);
4131 /* MPD_HOST contains a password */
4132 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4133 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4135 if (!strlen(mpd_hostpart + 1)) {
4136 mpd_set_host("localhost");
4138 mpd_set_host(mpd_hostpart + 1);
4141 mpd_set_password(mpd_password, 1);
4146 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4147 /* failed to set port from environment variable */
4148 mpd_set_port("6600");
4152 info.xmms2.artist = NULL;
4153 info.xmms2.album = NULL;
4154 info.xmms2.title = NULL;
4155 info.xmms2.genre = NULL;
4156 info.xmms2.comment = NULL;
4157 info.xmms2.url = NULL;
4158 info.xmms2.status = NULL;
4159 info.xmms2.playlist = NULL;
4161 use_spacer = NO_SPACER;
4163 output_methods = TO_X;
4165 output_methods = TO_STDOUT;
4168 show_graph_scale = 0;
4169 show_graph_range = 0;
4172 draw_graph_borders = 1;
4174 set_first_font("6x10");
4182 window.type = TYPE_NORMAL;
4184 strcpy(window.class_name, PACKAGE_NAME);
4185 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4187 stippled_borders = 0;
4188 window.border_inner_margin = 3;
4189 window.border_outer_margin = 1;
4190 window.border_width = 1;
4191 text_alignment = BOTTOM_LEFT;
4192 info.x11.monitor.number = 1;
4193 info.x11.monitor.current = 0;
4194 info.x11.desktop.current = 1;
4195 info.x11.desktop.number = 1;
4196 info.x11.desktop.nitems = 0;
4197 info.x11.desktop.all_names = NULL;
4198 info.x11.desktop.name = NULL;
4203 free(current_mail_spool);
4207 variable_substitute(MAIL_FILE, buf, 256);
4208 if (buf[0] != '\0') {
4209 current_mail_spool = strndup(buf, text_buffer_size);
4214 set_update_interval(3);
4215 update_interval_bat = NOBATTERY;
4216 info.music_player_interval = 1.0;
4217 stuff_in_uppercase = 0;
4218 info.users.number = 1;
4220 #ifdef TCP_PORT_MONITOR
4221 /* set default connection limit */
4222 tcp_portmon_set_max_connections(0);
4226 /* returns 1 if you can overwrite or create the file at 'path' */
4227 static _Bool overwrite_works(const char *path)
4231 if (!(filepointer = fopen(path, "w")))
4233 fclose(filepointer);
4237 /* returns 1 if you can append or create the file at 'path' */
4238 static _Bool append_works(const char *path)
4242 if (!(filepointer = fopen(path, "a")))
4244 fclose(filepointer);
4250 /* WARNING, this type not in Xlib spec */
4251 int x11_error_handler(Display *d, XErrorEvent *err)
4252 __attribute__((noreturn));
4253 int x11_error_handler(Display *d, XErrorEvent *err)
4255 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",
4257 (long unsigned)err->display,
4258 (long)err->resourceid,
4268 int x11_ioerror_handler(Display *d)
4269 __attribute__((noreturn));
4270 int x11_ioerror_handler(Display *d)
4272 NORM_ERR("X Error: Display %lx\n",
4279 static void X11_initialisation(void)
4281 if (x_initialised == YES) return;
4282 output_methods |= TO_X;
4284 set_default_configurations_for_x();
4285 x_initialised = YES;
4288 /* WARNING, this type not in Xlib spec */
4289 XSetErrorHandler(&x11_error_handler);
4290 XSetIOErrorHandler(&x11_ioerror_handler);
4294 static char **xargv = 0;
4295 static int xargc = 0;
4297 static void X11_create_window(void)
4299 if (output_methods & TO_X) {
4301 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4302 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4304 #else /* OWN_WINDOW */
4305 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4306 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4308 #endif /* OWN_WINDOW */
4312 update_text_area(); /* to position text/window on screen */
4315 if (own_window && !fixed_pos) {
4316 XMoveWindow(display, window.window, window.x, window.y);
4319 set_transparent_background(window.window);
4327 x11_stuff.region = XCreateRegion();
4329 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4330 NORM_ERR("Xdamage extension unavailable");
4332 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4333 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4334 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4335 #endif /* HAVE_XDAMAGE */
4338 update_text_area(); /* to get initial size of the window */
4341 /* setup lua window globals */
4342 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4343 #endif /* HAVE_LUA */
4347 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4348 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4349 #define CONF2(a) if (strcasecmp(name, a) == 0)
4350 #define CONF(a) else CONF2(a)
4351 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4352 || strcasecmp(name, b) == 0)
4353 #define CONF_CONTINUE 1
4354 #define CONF_BREAK 2
4355 #define CONF_BUFF_SIZE 512
4357 static FILE *open_config_file(const char *f)
4359 #ifdef CONFIG_OUTPUT
4360 if (!strcmp(f, "==builtin==")) {
4361 return conf_cookie_open();
4363 #endif /* CONFIG_OUTPUT */
4364 return fopen(f, "r");
4367 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4371 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4374 remove_comments(buf);
4379 while (*p && isspace((int) *p)) {
4383 return CONF_CONTINUE; /* empty line */
4390 while (*p2 && !isspace((int) *p2)) {
4394 *p2 = '\0'; /* break at name's end */
4401 while (*p && isspace((int) *p)) {
4407 p2 = *value + strlen(*value);
4408 while (isspace((int) *(p2 - 1))) {
4417 char load_config_file(const char *f)
4422 fp = open_config_file(f);
4426 DBGP("reading contents from config file '%s'", f);
4429 char buff[CONF_BUFF_SIZE], *name, *value;
4430 int ret = do_config_step(&line, fp, buff, &name, &value);
4431 if (ret == CONF_BREAK) {
4433 } else if (ret == CONF_CONTINUE) {
4439 /* don't listen if X is already initialised or
4440 * if we already know we don't want it */
4441 if(x_initialised != YES) {
4442 if (string_to_bool(value)) {
4443 output_methods &= TO_X;
4445 output_methods &= ~TO_X;
4446 x_initialised = NEVER;
4451 if (!value || x_initialised == YES) {
4456 disp = strdup(value);
4461 if (window.type == TYPE_DOCK)
4464 #endif /*OWN_WINDOW */
4466 int a = string_to_alignment(value);
4477 CONF("background") {
4478 fork_to_background = string_to_bool(value);
4481 CONF2("background") {
4482 fork_to_background = string_to_bool(value);
4486 CONF("show_graph_scale") {
4487 show_graph_scale = string_to_bool(value);
4489 CONF("show_graph_range") {
4490 show_graph_range = string_to_bool(value);
4492 CONF("border_inner_margin") {
4494 window.border_inner_margin = strtol(value, 0, 0);
4495 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4500 CONF("border_outer_margin") {
4502 window.border_outer_margin = strtol(value, 0, 0);
4503 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4508 CONF("border_width") {
4510 window.border_width = strtol(value, 0, 0);
4511 if (window.border_width < 0) window.border_width = 0;
4517 #define TEMPLATE_CONF(n) \
4518 CONF("template"#n) { \
4519 if (set_template(n, value)) \
4534 parse_global_imap_mail_args(value);
4541 parse_global_pop3_mail_args(value);
4546 CONF("default_bar_size") {
4549 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4556 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4560 CONF("default_graph_size") {
4563 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4570 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4573 CONF("default_gauge_size") {
4576 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4583 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4590 mpd_set_host(value);
4596 if (value && mpd_set_port(value)) {
4600 CONF("mpd_password") {
4602 mpd_set_password(value, 0);
4608 CONF("music_player_interval") {
4610 info.music_player_interval = strtod(value, 0);
4616 CONF("sensor_device") {
4618 sensor_device = strtol(value, 0, 0);
4624 CONF("cpu_avg_samples") {
4626 cpu_avg_samples = strtol(value, 0, 0);
4627 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4630 info.cpu_avg_samples = cpu_avg_samples;
4636 CONF("net_avg_samples") {
4638 net_avg_samples = strtol(value, 0, 0);
4639 if (net_avg_samples < 1 || net_avg_samples > 14) {
4642 info.net_avg_samples = net_avg_samples;
4648 CONF("diskio_avg_samples") {
4650 diskio_avg_samples = strtol(value, 0, 0);
4651 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4654 info.diskio_avg_samples = diskio_avg_samples;
4662 CONF("double_buffer") {
4663 use_xdbe = string_to_bool(value);
4667 CONF("override_utf8_locale") {
4668 utf8_mode = string_to_bool(value);
4670 CONF("draw_borders") {
4671 draw_borders = string_to_bool(value);
4673 CONF("draw_graph_borders") {
4674 draw_graph_borders = string_to_bool(value);
4676 CONF("draw_shades") {
4677 draw_shades = string_to_bool(value);
4679 CONF("draw_outline") {
4680 draw_outline = string_to_bool(value);
4683 CONF("out_to_console") {
4684 if(string_to_bool(value)) {
4685 output_methods |= TO_STDOUT;
4687 output_methods &= ~TO_STDOUT;
4690 CONF("extra_newline") {
4691 extra_newline = string_to_bool(value);
4693 CONF("out_to_stderr") {
4694 if(string_to_bool(value))
4695 output_methods |= TO_STDERR;
4698 CONF("out_to_ncurses") {
4699 if(string_to_bool(value)) {
4702 output_methods |= TO_NCURSES;
4706 CONF("overwrite_file") {
4707 if(overwrite_file) {
4708 free(overwrite_file);
4711 if(overwrite_works(value)) {
4712 overwrite_file = strdup(value);
4713 output_methods |= OVERWRITE_FILE;
4715 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4717 CONF("append_file") {
4722 if(append_works(value)) {
4723 append_file = strdup(value);
4724 output_methods |= APPEND_FILE;
4726 NORM_ERR("append_file won't be able to create/append '%s'", value);
4728 CONF("use_spacer") {
4730 if (strcasecmp(value, "left") == EQUAL) {
4731 use_spacer = LEFT_SPACER;
4732 } else if (strcasecmp(value, "right") == EQUAL) {
4733 use_spacer = RIGHT_SPACER;
4734 } else if (strcasecmp(value, "none") == EQUAL) {
4735 use_spacer = NO_SPACER;
4737 use_spacer = string_to_bool(value);
4738 NORM_ERR("use_spacer should have an argument of left, right, or"
4739 " none. '%s' seems to be some form of '%s', so"
4740 " defaulting to %s.", value,
4741 use_spacer ? "true" : "false",
4742 use_spacer ? "right" : "none");
4744 use_spacer = RIGHT_SPACER;
4746 use_spacer = NO_SPACER;
4750 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4751 use_spacer = RIGHT_SPACER;
4757 use_xft = string_to_bool(value);
4761 set_first_font(value);
4765 if (value && font_count >= 0) {
4766 fonts[0].font_alpha = atof(value) * 65535.0;
4773 if (string_to_bool(value)) {
4774 NORM_ERR("Xft not enabled at compile time");
4778 /* xftfont silently ignored when no Xft */
4781 /* xftalpha is silently ignored when no Xft */
4786 set_first_font(value);
4794 gap_x = atoi(value);
4801 gap_y = atoi(value);
4807 CONF("mail_spool") {
4811 variable_substitute(value, buffer, 256);
4813 if (buffer[0] != '\0') {
4814 if (current_mail_spool) {
4815 free(current_mail_spool);
4817 current_mail_spool = strndup(buffer, text_buffer_size);
4824 CONF("minimum_size") {
4826 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4828 if (sscanf(value, "%d", &minimum_width) != 1) {
4836 CONF("maximum_width") {
4838 if (sscanf(value, "%d", &maximum_width) != 1) {
4846 CONF("no_buffers") {
4847 no_buffers = string_to_bool(value);
4849 CONF("top_name_width") {
4851 if (sscanf(value, "%u", &top_name_width) != 1) {
4857 if (top_name_width >= max_user_text) {
4858 top_name_width = max_user_text - 1;
4861 CONF("top_cpu_separate") {
4862 cpu_separate = string_to_bool(value);
4864 CONF("short_units") {
4865 short_units = string_to_bool(value);
4867 CONF("format_human_readable") {
4868 format_human_readable = string_to_bool(value);
4871 CONF("hddtemp_host") {
4872 set_hddtemp_host(value);
4874 CONF("hddtemp_port") {
4875 set_hddtemp_port(value);
4877 #endif /* HDDTEMP */
4878 CONF("pad_percents") {
4879 pad_percents = atoi(value);
4883 CONF("own_window") {
4885 own_window = string_to_bool(value);
4888 CONF("own_window_class") {
4890 memset(window.class_name, 0, sizeof(window.class_name));
4891 strncpy(window.class_name, value,
4892 sizeof(window.class_name) - 1);
4895 CONF("own_window_title") {
4897 memset(window.title, 0, sizeof(window.title));
4898 strncpy(window.title, value, sizeof(window.title) - 1);
4901 CONF("own_window_transparent") {
4903 set_transparent = string_to_bool(value);
4906 CONF("own_window_hints") {
4908 char *p_hint, *p_save;
4909 char delim[] = ", ";
4911 /* tokenize the value into individual hints */
4912 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4914 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4915 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4916 SET_HINT(window.hints, HINT_UNDECORATED);
4917 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4918 SET_HINT(window.hints, HINT_BELOW);
4919 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4920 SET_HINT(window.hints, HINT_ABOVE);
4921 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4922 SET_HINT(window.hints, HINT_STICKY);
4923 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4924 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4925 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4926 SET_HINT(window.hints, HINT_SKIP_PAGER);
4931 p_hint = strtok_r(NULL, delim, &p_save);
4932 } while (p_hint != NULL);
4938 CONF("own_window_type") {
4940 if (strncmp(value, "normal", 6) == EQUAL) {
4941 window.type = TYPE_NORMAL;
4942 } else if (strncmp(value, "desktop", 7) == EQUAL) {
4943 window.type = TYPE_DESKTOP;
4944 } else if (strncmp(value, "dock", 4) == EQUAL) {
4945 window.type = TYPE_DOCK;
4946 text_alignment = TOP_LEFT;
4947 } else if (strncmp(value, "panel", 5) == EQUAL) {
4948 window.type = TYPE_PANEL;
4949 } else if (strncmp(value, "override", 8) == EQUAL) {
4950 window.type = TYPE_OVERRIDE;
4959 CONF("stippled_borders") {
4961 stippled_borders = strtol(value, 0, 0);
4963 stippled_borders = 4;
4967 CONF("imlib_cache_size") {
4969 cimlib_set_cache_size(atoi(value));
4972 CONF("imlib_cache_flush_interval") {
4974 cimlib_set_cache_flush_interval(atoi(value));
4979 CONF("update_interval_on_battery") {
4981 update_interval_bat = strtod(value, 0);
4986 CONF("update_interval") {
4988 set_update_interval(strtod(value, 0));
4992 if (info.music_player_interval == 0) {
4993 // default to update_interval
4994 info.music_player_interval = update_interval;
4997 CONF("total_run_times") {
4999 total_run_times = strtod(value, 0);
5005 stuff_in_uppercase = string_to_bool(value);
5007 CONF("max_specials") {
5009 max_specials = atoi(value);
5014 CONF("max_user_text") {
5016 max_user_text = atoi(value);
5021 CONF("text_buffer_size") {
5023 text_buffer_size = atoi(value);
5024 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5025 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5026 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5034 if (output_methods & TO_X) {
5035 X11_initialisation();
5044 global_text = (char *) malloc(1);
5045 global_text[0] = '\0';
5048 unsigned int l = strlen(global_text);
5050 char buf[CONF_BUFF_SIZE];
5052 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5058 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5066 /* Check for continuation of \\-\n. */
5067 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5068 global_text[l-1] = '\0';
5072 global_text = (char *) realloc(global_text, l + bl + 1);
5073 strcat(global_text, buf);
5075 if (strlen(global_text) > max_user_text) {
5079 global_text_lines = line + 1;
5082 #ifdef TCP_PORT_MONITOR
5083 CONF("max_port_monitor_connections") {
5085 if (!value || (sscanf(value, "%d", &max) != 1)) {
5086 /* an error. use default, warn and continue. */
5087 tcp_portmon_set_max_connections(0);
5089 } else if (tcp_portmon_set_max_connections(max)) {
5090 /* max is < 0, default has been set*/
5095 CONF("if_up_strictness") {
5097 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5098 ifup_strictness = IFUP_UP;
5099 } else if (strcasecmp(value, "up") == EQUAL) {
5100 ifup_strictness = IFUP_UP;
5101 } else if (strcasecmp(value, "link") == EQUAL) {
5102 ifup_strictness = IFUP_LINK;
5103 } else if (strcasecmp(value, "address") == EQUAL) {
5104 ifup_strictness = IFUP_ADDR;
5106 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5107 ifup_strictness = IFUP_UP;
5111 CONF("temperature_unit") {
5113 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5114 } else if (set_temp_output_unit(value)) {
5115 NORM_ERR("temperature_unit: incorrect argument");
5122 char *ptr = strtok(value, " ");
5125 ptr = strtok(NULL, " ");
5132 CONF("lua_draw_hook_pre") {
5134 llua_set_draw_pre_hook(value);
5139 CONF("lua_draw_hook_post") {
5141 llua_set_draw_post_hook(value);
5146 CONF("lua_startup_hook") {
5148 llua_set_startup_hook(value);
5153 CONF("lua_shutdown_hook") {
5155 llua_set_shutdown_hook(value);
5161 #endif /* HAVE_LUA */
5173 CONF("default_color"){}
5174 CONF3("default_shade_color", "default_shadecolor"){}
5175 CONF3("default_outline_color", "default_outlinecolor") {}
5176 CONF("own_window_colour") {}
5179 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5185 if (info.music_player_interval == 0) {
5186 // default to update_interval
5187 info.music_player_interval = update_interval;
5189 if (!global_text) { // didn't supply any text
5190 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5192 if (!output_methods) {
5193 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5195 #if defined(NCURSES)
5197 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5198 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5199 output_methods &= ~TO_NCURSES;
5203 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5204 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5205 output_methods &= ~(TO_STDOUT | TO_STDERR);
5207 #endif /* NCURSES */
5212 static void load_config_file_x11(const char *f)
5217 fp = open_config_file(f);
5221 DBGP("reading contents from config file '%s'", f);
5224 char buff[CONF_BUFF_SIZE], *name, *value;
5225 int ret = do_config_step(&line, fp, buff, &name, &value);
5226 if (ret == CONF_BREAK) {
5228 } else if (ret == CONF_CONTINUE) {
5233 X11_initialisation();
5234 if (x_initialised == YES) {
5236 color0 = get_x11_color(value);
5243 X11_initialisation();
5244 if (x_initialised == YES) {
5246 color1 = get_x11_color(value);
5253 X11_initialisation();
5254 if (x_initialised == YES) {
5256 color2 = get_x11_color(value);
5263 X11_initialisation();
5264 if (x_initialised == YES) {
5266 color3 = get_x11_color(value);
5273 X11_initialisation();
5274 if (x_initialised == YES) {
5276 color4 = get_x11_color(value);
5283 X11_initialisation();
5284 if (x_initialised == YES) {
5286 color5 = get_x11_color(value);
5293 X11_initialisation();
5294 if (x_initialised == YES) {
5296 color6 = get_x11_color(value);
5303 X11_initialisation();
5304 if (x_initialised == YES) {
5306 color7 = get_x11_color(value);
5313 X11_initialisation();
5314 if (x_initialised == YES) {
5316 color8 = get_x11_color(value);
5323 X11_initialisation();
5324 if (x_initialised == YES) {
5326 color9 = get_x11_color(value);
5332 CONF("default_color") {
5333 X11_initialisation();
5334 if (x_initialised == YES) {
5336 default_fg_color = get_x11_color(value);
5342 CONF3("default_shade_color", "default_shadecolor") {
5343 X11_initialisation();
5344 if (x_initialised == YES) {
5346 default_bg_color = get_x11_color(value);
5352 CONF3("default_outline_color", "default_outlinecolor") {
5353 X11_initialisation();
5354 if (x_initialised == YES) {
5356 default_out_color = get_x11_color(value);
5363 CONF("own_window_colour") {
5364 X11_initialisation();
5365 if (x_initialised == YES) {
5367 background_colour = get_x11_color(value);
5369 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5370 "'#' for hex colours");
5376 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5377 if(output_methods & TO_X) {
5378 X11_initialisation();
5387 #undef CONF_CONTINUE
5388 #undef CONF_BUFF_SIZE
5396 static void print_help(const char *prog_name) {
5397 printf("Usage: %s [OPTION]...\n"
5398 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5399 "window. Command line options will override configurations defined in config\n"
5401 " -v, --version version\n"
5402 " -q, --quiet quiet mode\n"
5403 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5404 " -c, --config=FILE config file to load\n"
5405 #ifdef CONFIG_OUTPUT
5406 " -C, --print-config print the builtin default config to stdout\n"
5407 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5409 " -d, --daemonize daemonize, fork to background\n"
5410 " -h, --help help\n"
5412 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5413 " -f, --font=FONT font to use\n"
5414 " -X, --display=DISPLAY X11 display to use\n"
5416 " -o, --own-window create own window to draw\n"
5419 " -b, --double-buffer double buffer (prevents flickering)\n"
5421 " -w, --window-id=WIN_ID window id to draw\n"
5422 " -x X x position\n"
5423 " -y Y y position\n"
5425 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5426 " -u, --interval=SECS update interval\n"
5427 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5428 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5433 /* : means that character before that takes an argument */
5434 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5444 #ifdef CONFIG_OUTPUT
5449 static const struct option longopts[] = {
5450 { "help", 0, NULL, 'h' },
5451 { "version", 0, NULL, 'V' },
5452 { "debug", 0, NULL, 'D' },
5453 { "config", 1, NULL, 'c' },
5454 #ifdef CONFIG_OUTPUT
5455 { "print-config", 0, NULL, 'C' },
5457 { "daemonize", 0, NULL, 'd' },
5459 { "alignment", 1, NULL, 'a' },
5460 { "font", 1, NULL, 'f' },
5461 { "display", 1, NULL, 'X' },
5463 { "own-window", 0, NULL, 'o' },
5466 { "double-buffer", 0, NULL, 'b' },
5468 { "window-id", 1, NULL, 'w' },
5470 { "text", 1, NULL, 't' },
5471 { "interval", 0, NULL, 'u' },
5472 { "pause", 0, NULL, 'p' },
5476 void initialisation(int argc, char **argv) {
5477 struct sigaction act, oact;
5479 set_default_configurations();
5480 load_config_file(current_config);
5481 currentconffile = conftree_add(currentconffile, current_config);
5483 /* init specials array */
5484 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5485 NORM_ERR("failed to create specials array");
5489 if (current_mail_spool == NULL) {
5492 variable_substitute(MAIL_FILE, buf, 256);
5494 if (buf[0] != '\0') {
5495 current_mail_spool = strndup(buf, text_buffer_size);
5500 /* handle other command line arguments */
5502 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5503 || defined(__NetBSD__)
5504 optind = optreset = 1;
5509 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5510 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5511 "kvm_open")) == NULL) {
5512 CRIT_ERR(NULL, NULL, "cannot read kvm");
5517 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5526 fork_to_background = 1;
5529 global_debug_level++;
5533 set_first_font(optarg);
5536 text_alignment = string_to_alignment(optarg);
5555 global_text = strndup(optarg, max_user_text);
5556 convert_escapes(global_text);
5560 update_interval = strtod(optarg, 0);
5561 update_interval_old = update_interval;
5562 if (info.music_player_interval == 0) {
5563 // default to update_interval
5564 info.music_player_interval = update_interval;
5569 total_run_times = strtod(optarg, 0);
5573 gap_x = atoi(optarg);
5577 gap_y = atoi(optarg);
5581 startup_pause = atoi(optarg);
5582 sleep(startup_pause);
5592 if (output_methods & TO_X) {
5593 load_config_file_x11(current_config);
5597 /* generate text and get initial size */
5598 extract_variable_text(global_text);
5605 if (fork_to_background) {
5610 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5617 fprintf(stderr, "\n");
5622 /* parent process */
5623 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5630 start_update_threading();
5632 text_buffer = malloc(max_user_text);
5633 memset(text_buffer, 0, max_user_text);
5634 tmpstring1 = malloc(text_buffer_size);
5635 memset(tmpstring1, 0, text_buffer_size);
5636 tmpstring2 = malloc(text_buffer_size);
5637 memset(tmpstring2, 0, text_buffer_size);
5642 X11_create_window();
5645 llua_setup_info(&info, update_interval);
5646 #endif /* HAVE_LUA */
5651 /* Set signal handlers */
5652 act.sa_handler = signal_handler;
5653 sigemptyset(&act.sa_mask);
5656 act.sa_flags |= SA_RESTART;
5659 if ( sigaction(SIGINT, &act, &oact) < 0
5660 || sigaction(SIGALRM, &act, &oact) < 0
5661 || sigaction(SIGUSR1, &act, &oact) < 0
5662 || sigaction(SIGHUP, &act, &oact) < 0
5663 || sigaction(SIGTERM, &act, &oact) < 0) {
5664 NORM_ERR("error setting signal handler: %s", strerror(errno));
5668 llua_startup_hook();
5669 #endif /* HAVE_LUA */
5672 int main(int argc, char **argv)
5681 g_signal_pending = 0;
5682 max_user_text = MAX_USER_TEXT_DEFAULT;
5684 memset(&info, 0, sizeof(info));
5688 #ifdef TCP_PORT_MONITOR
5689 /* set default connection limit */
5690 tcp_portmon_set_max_connections(0);
5693 /* handle command line parameters that don't change configs */
5695 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5696 || ((s = getenv("LANG")) && *s)) {
5697 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5699 NORM_ERR("malloc failed");
5701 for (x = 0; x < strlen(s); x++) {
5702 temp[x] = tolower(s[x]);
5705 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5711 if (!setlocale(LC_CTYPE, "")) {
5712 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5716 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5727 if (current_config) {
5728 free(current_config);
5730 current_config = strndup(optarg, max_user_text);
5733 freopen("/dev/null", "w", stderr);
5736 print_help(argv[0]);
5738 #ifdef CONFIG_OUTPUT
5745 window.window = strtol(optarg, 0, 0);
5750 disp = strdup(optarg);
5759 /* check if specified config file is valid */
5760 if (current_config) {
5762 if (stat(current_config, &sb) ||
5763 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5764 NORM_ERR("invalid configuration file '%s'\n", current_config);
5765 free(current_config);
5770 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5772 if (!current_config) {
5773 /* load default config file */
5774 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5777 /* Try to use personal config file first */
5778 to_real_path(buf, CONFIG_FILE);
5779 if (buf[0] && (fp = fopen(buf, "r"))) {
5780 current_config = strndup(buf, max_user_text);
5784 /* Try to use system config file if personal config not readable */
5785 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5786 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5790 /* No readable config found */
5791 if (!current_config) {
5792 #ifdef CONFIG_OUTPUT
5793 current_config = strdup("==builtin==");
5794 NORM_ERR("no readable personal or system-wide config file found,"
5795 " using builtin default");
5797 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5798 #endif /* ! CONF_OUTPUT */
5803 /* Load xoap keys, if existing */
5807 #ifdef HAVE_SYS_INOTIFY_H
5808 inotify_fd = inotify_init1(IN_NONBLOCK);
5809 #endif /* HAVE_SYS_INOTIFY_H */
5811 initialisation(argc, argv);
5815 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5823 void alarm_handler(void) {
5825 kill(childpid, SIGTERM);
5829 static void signal_handler(int sig)
5831 /* signal handler is light as a feather, as it should be.
5832 * we will poll g_signal_pending with each loop of conky
5833 * and do any signal processing there, NOT here (except
5834 * SIGALRM because this is caused when conky is hanging) */
5835 if(sig == SIGALRM) {
5838 g_signal_pending = sig;