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>
89 #include "iconv_tools.h"
101 #include "specials.h"
102 #include "temphelper.h"
103 #include "template.h"
104 #include "tailhead.h"
105 #include "timeinfo.h"
108 /* check for OS and include appropriate headers */
109 #if defined(__linux__)
111 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
113 #elif defined(__OpenBSD__)
117 #if defined(__FreeBSD_kernel__)
121 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
122 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
123 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
124 int apm_getinfo(int fd, apm_info_t aip);
125 char *get_apm_adapter(void);
126 char *get_apm_battery_life(void);
127 char *get_apm_battery_time(void);
131 #include "defconfig.h"
132 #include "conf_cookie.h"
136 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
139 #define MAIL_FILE "$MAIL"
140 #define MAX_IF_BLOCK_DEPTH 5
142 //#define SIGNAL_BLOCKING
143 #undef SIGNAL_BLOCKING
145 /* debugging level, used by logging.h */
146 int global_debug_level = 0;
148 /* two strings for internal use */
149 static char *tmpstring1, *tmpstring2;
151 /* variables holding various config settings */
153 int format_human_readable;
160 int top_cpu, top_mem, top_time;
167 static unsigned int top_name_width = 15;
169 static int extra_newline;
170 enum x_initialiser_state x_initialised = NO;
171 static volatile int g_signal_pending;
172 /* Update interval */
173 double update_interval;
174 double update_interval_old;
175 double update_interval_bat;
176 void *global_cpu = NULL;
181 /* prototypes for internally used functions */
182 static void signal_handler(int);
183 static void print_version(void) __attribute__((noreturn));
184 static void reload_config(void);
186 static void print_version(void)
188 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
190 printf("\nCompiled in features:\n\n"
191 "System config file: "SYSTEM_CONFIG_FILE"\n"
192 "Package library path: "PACKAGE_LIBDIR"\n\n"
196 " * Xdamage extension\n"
197 # endif /* HAVE_XDAMAGE */
199 " * XDBE (double buffer extension)\n"
200 # endif /* HAVE_XDBE */
205 "\n Music detection:\n"
208 #endif /* AUDACIOUS */
224 #endif /* HAVE_OPENMP */
231 #ifdef TCP_PORT_MONITOR
233 #endif /* TCP_PORT_MONITOR */
236 #endif /* HAVE_CURL */
241 " * Weather (METAR)\n"
243 " * Weather (XOAP)\n"
248 #endif /* HAVE_IWLIB */
250 " * support for IBM/Lenovo notebooks\n"
260 #endif /* CONFIG_OUTPUT */
265 " * ALSA mixer support\n"
266 #endif /* MIXER_IS_ALSA */
279 #ifdef HAVE_LUA_CAIRO
281 #endif /* HAVE_LUA_CAIRO */
282 #ifdef HAVE_LUA_IMLIB2
285 #endif /* HAVE_LUA */
291 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
296 static void X11_create_window(void);
297 static void X11_initialisation(void);
299 struct _x11_stuff_s {
303 XserverRegion region2, part;
304 int event_base, error_base;
310 static int text_start_x, text_start_y; /* text start position in window */
311 static int text_width, text_height;
326 /* display to connect to */
327 static char *disp = NULL;
331 /* struct that has all info to be shared between
332 * instances of the same text object */
333 struct information info;
335 /* path to config file */
336 char *current_config;
338 /* set to 1 if you want all text to be in uppercase */
339 static unsigned int stuff_in_uppercase;
341 /* Run how many times? */
342 static unsigned long total_run_times;
345 static int fork_to_background;
347 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
349 /* filenames for output */
350 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
351 char *append_file = NULL; FILE *append_fpointer = NULL;
355 static int show_graph_scale;
356 static int show_graph_range;
358 /* Position on the screen */
359 static int text_alignment;
360 static int gap_x, gap_y;
363 static int draw_borders;
364 static int draw_graph_borders;
365 static int stippled_borders;
367 int get_stippled_borders(void)
369 return stippled_borders;
372 static int draw_shades, draw_outline;
374 long default_fg_color, default_bg_color, default_out_color;
376 /* create own window or draw stuff to root? */
377 static int set_transparent = 0;
380 static int own_window = 0;
381 static int background_colour = 0;
383 /* fixed size/pos is set if wm/user changes them */
384 static int fixed_size = 0, fixed_pos = 0;
387 static int minimum_width, minimum_height;
388 static int maximum_width;
393 static int sensor_device;
396 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
399 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
400 unsigned int max_user_text;
402 /* maximum size of individual text buffers, ie $exec buffer size */
403 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
408 /* no buffers in used memory? */
411 /* pad percentages to decimals? */
412 static int pad_percents = 0;
414 static char *global_text = 0;
416 char *get_global_text(void)
421 long global_text_lines;
423 static int total_updates;
424 static int updatereset;
426 void set_updatereset(int i)
431 int get_updatereset(void)
436 int check_contains(char *f, char *s)
439 FILE *where = open_file(f, 0);
444 while (fgets(buf1, 256, where)) {
445 if (strstr(buf1, s)) {
452 NORM_ERR("Could not open the file");
457 #define SECRIT_MULTILINE_CHAR '\x02'
459 static inline int calc_text_width(const char *s)
461 size_t slen = strlen(s);
464 if ((output_methods & TO_X) == 0) {
474 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
475 (const FcChar8 *) s, slen, &gi);
477 XftTextExtents8(display, fonts[selected_font].xftfont,
478 (const FcChar8 *) s, slen, &gi);
484 return XTextWidth(fonts[selected_font].font, s, slen);
489 /* formatted text to render on screen, generated in generate_text(),
490 * drawn in draw_stuff() */
492 static char *text_buffer;
494 /* quite boring functions */
496 static inline void for_each_line(char *b, int f(char *, int))
499 int special_index = 0; /* specials index */
501 for (ps = b, pe = b; *pe; pe++) {
504 special_index = f(ps, special_index);
511 f(ps, special_index);
515 static void convert_escapes(char *buf)
517 char *p = buf, *s = buf;
524 } else if (*s == '\\') {
535 /* Prints anything normally printed with snprintf according to the current value
536 * of use_spacer. Actually slightly more flexible than snprintf, as you can
537 * safely specify the destination buffer as one of your inputs. */
538 int spaced_print(char *buf, int size, const char *format, int width, ...)
547 tempbuf = malloc(size * sizeof(char));
549 // Passes the varargs along to vsnprintf
550 va_start(argp, width);
551 vsnprintf(tempbuf, size, format, argp);
554 switch (use_spacer) {
556 len = snprintf(buf, size, "%s", tempbuf);
559 len = snprintf(buf, size, "%*s", width, tempbuf);
562 len = snprintf(buf, size, "%-*s", width, tempbuf);
569 /* print percentage values
571 * - i.e., unsigned values between 0 and 100
572 * - respect the value of pad_percents */
573 int percent_print(char *buf, int size, unsigned value)
575 return spaced_print(buf, size, "%u", pad_percents, value);
578 /* converts from bytes to human readable format (K, M, G, T)
580 * The algorithm always divides by 1024, as unit-conversion of byte
581 * counts suggests. But for output length determination we need to
582 * compare with 1000 here, as we print in decimal form. */
583 void human_readable(long long num, char *buf, int size)
585 const char **suffix = suffixes;
591 /* Possibly just output as usual, for example for stdout usage */
592 if (!format_human_readable) {
593 spaced_print(buf, size, "%d", 6, round_to_int(num));
604 if (llabs(num) < 1000LL) {
605 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
609 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
617 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
619 * The goal is to always have a significance of 3, by
620 * adjusting the decimal part of the number. Sample output:
624 * so the point of alignment resides between number and unit. The
625 * upside of this is that there is minimal padding necessary, though
626 * there should be a way to make alignment take place at the decimal
627 * dot (then with fixed width decimal part).
629 * Note the repdigits below: when given a precision value, printf()
630 * rounds the float to it, not just cuts off the remaining digits. So
631 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
632 * printed with a precision of 0. Yay. */
634 precision = 0; /* print 100-999 without decimal part */
636 precision = 1; /* print 10-99 with one decimal place */
638 precision = 2; /* print 0-9 with two decimal places */
640 spaced_print(buf, size, format, width, precision, fnum, *suffix);
643 /* global object list root element */
644 static struct text_object global_root_object;
646 static long current_text_color;
648 void set_current_text_color(long colour)
650 current_text_color = colour;
653 long get_current_text_color(void)
655 return current_text_color;
658 //adds newstring to to the tree unless you can already see it when travelling back.
659 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
660 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
661 struct conftree* node;
662 struct conftree* node2;
664 for(node = previous; node != NULL; node = node->back) {
665 if(strcmp(node->string, newstring) == 0) {
669 node = malloc(sizeof(struct conftree));
670 if (previous != NULL) {
671 if(previous->vert_next == NULL) {
672 previous->vert_next = node;
674 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
675 node2->horz_next = node;
678 node->string = strdup(newstring);
679 node->horz_next = NULL;
680 node->vert_next = NULL;
681 node->back = previous;
685 void conftree_empty(struct conftree* tree) {
687 conftree_empty(tree->horz_next);
688 conftree_empty(tree->vert_next);
694 struct conftree *currentconffile;
696 static void extract_variable_text(const char *p)
698 free_text_objects(&global_root_object, 0);
712 extract_variable_text_internal(&global_root_object, p);
715 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
717 extract_variable_text_internal(root, txt);
718 generate_text_internal(p, max_user_text, *root, cur);
721 static inline void format_media_player_time(char *buf, const int size,
724 int days, hours, minutes;
726 days = seconds / (24 * 60 * 60);
727 seconds %= (24 * 60 * 60);
728 hours = seconds / (60 * 60);
729 seconds %= (60 * 60);
730 minutes = seconds / 60;
734 snprintf(buf, size, "%i days %i:%02i:%02i", days,
735 hours, minutes, seconds);
736 } else if (hours > 0) {
737 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
740 snprintf(buf, size, "%i:%02i", minutes, seconds);
744 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
745 * multiline objects like $exec work with $align[rc] and friends
747 void substitute_newlines(char *p, long l)
751 while (p && *p && p < s + l) {
753 /* only substitute if it's not the last newline */
754 *p = SECRIT_MULTILINE_CHAR;
760 void generate_text_internal(char *p, int p_max_size,
761 struct text_object root, struct information *cur)
763 struct text_object *obj;
765 int need_to_load_fonts = 0;
768 /* for the OBJ_top* handler */
769 struct process **needed = 0;
772 char buff_in[p_max_size];
774 #endif /* HAVE_ICONV */
778 while (obj && p_max_size > 0) {
779 needed = 0; /* reset for top stuff */
781 /* IFBLOCK jumping algorithm
783 * This is easier as it looks like:
784 * - each IF checks it's condition
785 * - on FALSE: call DO_JUMP
786 * - on TRUE: don't care
787 * - each ELSE calls DO_JUMP unconditionally
788 * - each ENDIF is silently being ignored
791 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
792 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
793 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
794 * regularly, "obj" is being updated to point to obj->next, so object parsing
795 * continues right after the corresponding ELSE or ENDIF. This means that if we
796 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
797 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
798 * jumped, and there is nothing to do.
802 obj = obj->special_data; \
805 #define OBJ(a) break; case OBJ_##a:
809 NORM_ERR("not implemented obj type %d", obj->type);
811 print_read_tcp(obj, p, p_max_size);
815 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
817 #endif /* !__OpenBSD__ */
821 ok = get_freq(p, p_max_size, "%.0f", 1,
829 ok = get_freq(p, p_max_size, "%'.2f", 1000,
832 /* OpenBSD has no such flag (SUSv2) */
833 ok = get_freq(p, p_max_size, "%.2f", 1000,
835 #endif /* __OpenBSD */
838 #if defined(__linux__)
842 ok = get_voltage(p, p_max_size, "%.0f", 1,
849 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
855 OBJ(wireless_essid) {
856 print_wireless_essid(obj, p, p_max_size);
859 print_wireless_mode(obj, p, p_max_size);
861 OBJ(wireless_bitrate) {
862 print_wireless_bitrate(obj, p, p_max_size);
865 print_wireless_ap(obj, p, p_max_size);
867 OBJ(wireless_link_qual) {
868 print_wireless_link_qual(obj, p, p_max_size);
870 OBJ(wireless_link_qual_max) {
871 print_wireless_link_qual_max(obj, p, p_max_size);
873 OBJ(wireless_link_qual_perc) {
874 print_wireless_link_qual_perc(obj, p, p_max_size);
876 OBJ(wireless_link_bar) {
877 print_wireless_link_bar(obj, p, p_max_size);
879 #endif /* HAVE_IWLIB */
881 #endif /* __linux__ */
885 get_adt746x_cpu(p, p_max_size);
888 get_adt746x_fan(p, p_max_size);
891 get_acpi_fan(p, p_max_size);
894 get_acpi_ac_adapter(p, p_max_size);
897 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
900 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
902 OBJ(battery_percent) {
903 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
907 if(output_methods & TO_X) {
908 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
911 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
914 get_battery_short_status(p, p_max_size, obj->data.s);
916 #endif /* __OpenBSD__ */
919 human_readable(cur->buffers * 1024, p, 255);
922 human_readable(cur->cached * 1024, p, 255);
925 if (obj->data.i > info.cpu_count) {
926 NORM_ERR("obj->data.i %i info.cpu_count %i",
927 obj->data.i, info.cpu_count);
928 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
930 percent_print(p, p_max_size,
931 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
935 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
939 if(output_methods & TO_X) {
940 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
943 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
947 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
950 print_loadgraph(obj, p);
954 new_fg(p, obj->data.l);
989 snprintf(p, p_max_size, "%s", VERSION);
991 OBJ(conky_build_date) {
992 snprintf(p, p_max_size, "%s", BUILD_DATE);
994 OBJ(conky_build_arch) {
995 snprintf(p, p_max_size, "%s", BUILD_ARCH);
997 #if defined(__linux__)
999 snprintf(p, p_max_size, "%s",
1000 get_disk_protect_queue(obj->data.s));
1003 snprintf(p, p_max_size, "%s", i8k.version);
1006 snprintf(p, p_max_size, "%s", i8k.bios);
1009 snprintf(p, p_max_size, "%s", i8k.serial);
1014 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1015 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1017 OBJ(i8k_left_fan_status) {
1018 int left_fan_status;
1020 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1021 if (left_fan_status == 0) {
1022 snprintf(p, p_max_size, "off");
1024 if (left_fan_status == 1) {
1025 snprintf(p, p_max_size, "low");
1027 if (left_fan_status == 2) {
1028 snprintf(p, p_max_size, "high");
1031 OBJ(i8k_right_fan_status) {
1032 int right_fan_status;
1034 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1035 if (right_fan_status == 0) {
1036 snprintf(p, p_max_size, "off");
1038 if (right_fan_status == 1) {
1039 snprintf(p, p_max_size, "low");
1041 if (right_fan_status == 2) {
1042 snprintf(p, p_max_size, "high");
1045 OBJ(i8k_left_fan_rpm) {
1046 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1048 OBJ(i8k_right_fan_rpm) {
1049 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1051 OBJ(i8k_ac_status) {
1054 sscanf(i8k.ac_status, "%d", &ac_status);
1055 if (ac_status == -1) {
1056 snprintf(p, p_max_size, "disabled (read i8k docs)");
1058 if (ac_status == 0) {
1059 snprintf(p, p_max_size, "off");
1061 if (ac_status == 1) {
1062 snprintf(p, p_max_size, "on");
1065 OBJ(i8k_buttons_status) {
1066 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1070 get_ibm_acpi_fan(p, p_max_size);
1073 print_ibm_temps(obj, p, p_max_size);
1076 get_ibm_acpi_volume(p, p_max_size);
1078 OBJ(ibm_brightness) {
1079 get_ibm_acpi_brightness(p, p_max_size);
1082 /* information from sony_laptop kernel module
1083 * /sys/devices/platform/sony-laptop */
1084 OBJ(sony_fanspeed) {
1085 get_sony_fanspeed(p, p_max_size);
1088 if (!gateway_exists()) {
1093 print_gateway_iface(p, p_max_size);
1096 print_gateway_ip(p, p_max_size);
1099 snprintf(p, p_max_size, "%d", get_laptop_mode());
1102 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1104 #endif /* __linux__ */
1105 #if (defined(__FreeBSD__) || defined(__linux__))
1107 if (!interface_up(obj)) {
1113 OBJ(obsd_sensors_temp) {
1114 print_obsd_sensors_temp(obj, p, p_max_size);
1116 OBJ(obsd_sensors_fan) {
1117 print_obsd_sensors_fan(obj, p, p_max_size);
1119 OBJ(obsd_sensors_volt) {
1120 print_obsd_sensors_volt(obj, p, p_max_size);
1123 get_obsd_vendor(p, p_max_size);
1126 get_obsd_product(p, p_max_size);
1128 #endif /* __OpenBSD__ */
1131 new_font(p, obj->data.s);
1132 need_to_load_fonts = 1;
1136 print_diskio(obj, 0, p, p_max_size);
1139 print_diskio(obj, 1, p, p_max_size);
1142 print_diskio(obj, -1, p, p_max_size);
1146 print_diskiograph(obj, 0, p);
1148 OBJ(diskiograph_read) {
1149 print_diskiograph(obj, -1, p);
1151 OBJ(diskiograph_write) {
1152 print_diskiograph(obj, 1, p);
1156 print_downspeed(obj, p, p_max_size);
1159 print_downspeedf(obj, p, p_max_size);
1162 OBJ(downspeedgraph) {
1163 print_downspeedgraph(obj, p);
1167 /* Since we see you, you're if has not jumped.
1168 * Do Ninja jump here: without leaving traces.
1169 * This is to prevent us from stale jumped flags.
1175 /* harmless object, just ignore */
1178 print_addr(obj, p, p_max_size);
1180 #if defined(__linux__)
1182 print_addrs(obj, p, p_max_size);
1184 #endif /* __linux__ */
1185 #if defined(IMLIB2) && defined(X11)
1187 /* doesn't actually draw anything, just queues it omp. the
1188 * image will get drawn after the X event loop */
1189 cimlib_add_image(obj->data.s);
1193 evaluate(obj->data.s, p);
1196 print_exec(obj, p, p_max_size);
1199 print_execp(obj, p, p_max_size);
1203 print_execgauge(obj, p, p_max_size);
1207 print_execbar(obj, p, p_max_size);
1211 print_execgraph(obj, p, p_max_size);
1215 print_execibar(obj, p, p_max_size);
1219 print_execigraph(obj, p, p_max_size);
1222 print_execigauge(obj, p, p_max_size);
1226 print_execi(obj, p, p_max_size);
1229 print_execpi(obj, p);
1232 print_texeci(obj, p, p_max_size);
1235 print_imap_unseen(obj, p, p_max_size);
1237 OBJ(imap_messages) {
1238 print_imap_messages(obj, p, p_max_size);
1241 print_pop3_unseen(obj, p, p_max_size);
1244 print_pop3_used(obj, p, p_max_size);
1247 print_fs_bar(obj, 0, p, p_max_size);
1250 print_fs_free(obj, p, p_max_size);
1253 print_fs_perc(obj, 1, p, p_max_size);
1256 print_fs_size(obj, p, p_max_size);
1259 print_fs_type(obj, p, p_max_size);
1262 print_fs_used(obj, p, p_max_size);
1265 print_fs_bar(obj, 1, p, p_max_size);
1268 print_fs_perc(obj, 0, p, p_max_size);
1271 print_loadavg(obj, p, p_max_size);
1274 new_goto(p, obj->data.i);
1281 new_hr(p, obj->data.i);
1285 print_nameserver(obj, p, p_max_size);
1289 print_eve(obj, p, p_max_size);
1294 curl_print(obj, p, p_max_size);
1299 rss_print_info(obj, p, p_max_size);
1304 print_weather(obj, p, p_max_size);
1308 OBJ(weather_forecast) {
1309 print_weather_forecast(obj, p, p_max_size);
1314 char *str = llua_getstring(obj->data.s);
1316 snprintf(p, p_max_size, "%s", str);
1321 char *str = llua_getstring(obj->data.s);
1329 if (llua_getnumber(obj->data.s, &per)) {
1331 if(output_methods & TO_X) {
1332 new_bar(obj, p, (per/100.0 * 255));
1335 new_bar_in_shell(obj, p, p_max_size, per);
1341 if (llua_getnumber(obj->data.s, &per)) {
1342 new_graph(obj, p, per);
1347 if (llua_getnumber(obj->data.s, &per)) {
1348 new_gauge(obj, p, (per/100.0 * 255));
1352 #endif /* HAVE_LUA */
1358 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1359 snprintf(p, p_max_size, "N/A");
1361 temp_print(p, p_max_size, (double)val,
1362 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1367 new_offset(p, obj->data.i);
1370 new_voffset(p, obj->data.i);
1374 print_sysfs_sensor(obj, p, p_max_size);
1377 print_sysfs_sensor(obj, p, p_max_size);
1380 print_sysfs_sensor(obj, p, p_max_size);
1382 #endif /* __linux__ */
1384 new_alignr(p, obj->data.i);
1387 new_alignc(p, obj->data.i);
1390 char buf[max_user_text];
1391 struct information *tmp_info =
1392 malloc(sizeof(struct information));
1393 memcpy(tmp_info, cur, sizeof(struct information));
1394 generate_text_internal(buf, max_user_text,
1395 *obj->sub, tmp_info);
1397 if (strlen(buf) != 0) {
1403 char expression[max_user_text];
1405 struct information *tmp_info;
1407 tmp_info = malloc(sizeof(struct information));
1408 memcpy(tmp_info, cur, sizeof(struct information));
1409 generate_text_internal(expression, max_user_text,
1410 *obj->sub, tmp_info);
1411 DBGP("parsed arg into '%s'", expression);
1413 val = compare(expression);
1415 NORM_ERR("compare failed for expression '%s'",
1425 spc = strchr(obj->data.s, ' ');
1426 if (!spc && access(obj->data.s, F_OK)) {
1430 if (check_contains(obj->data.s, spc + 1))
1437 && (!check_mount(obj->data.s))) {
1443 if (!get_process_by_name(obj->data.s)) {
1445 if ((obj->data.s) && system(obj->data.s)) {
1450 #if defined(__linux__)
1452 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1456 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1459 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1464 human_readable(cur->mem * 1024, p, 255);
1467 human_readable(cur->memeasyfree * 1024, p, 255);
1470 human_readable(cur->memfree * 1024, p, 255);
1473 human_readable(cur->memmax * 1024, p, 255);
1477 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1481 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1486 if(output_methods & TO_X) {
1487 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1490 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1494 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1499 print_mixer(obj, 0, p, p_max_size);
1502 print_mixer(obj, -1, p, p_max_size);
1505 print_mixer(obj, 1, p, p_max_size);
1509 print_mixer_bar(obj, 0, p);
1512 print_mixer_bar(obj, -1, p);
1515 print_mixer_bar(obj, 1, p);
1518 OBJ(if_mixer_mute) {
1519 if (!check_mixer_muted(obj)) {
1524 #define NOT_IN_X "Not running in X"
1526 if(x_initialised != YES) {
1527 strncpy(p, NOT_IN_X, p_max_size);
1529 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1532 OBJ(monitor_number) {
1533 if(x_initialised != YES) {
1534 strncpy(p, NOT_IN_X, p_max_size);
1536 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1540 if(x_initialised != YES) {
1541 strncpy(p, NOT_IN_X, p_max_size);
1543 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1546 OBJ(desktop_number) {
1547 if(x_initialised != YES) {
1548 strncpy(p, NOT_IN_X, p_max_size);
1550 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1554 if(x_initialised != YES) {
1555 strncpy(p, NOT_IN_X, p_max_size);
1556 }else if(cur->x11.desktop.name != NULL) {
1557 strncpy(p, cur->x11.desktop.name, p_max_size);
1564 print_mails(obj, p, p_max_size);
1567 print_new_mails(obj, p, p_max_size);
1570 print_seen_mails(obj, p, p_max_size);
1573 print_unseen_mails(obj, p, p_max_size);
1575 OBJ(flagged_mails) {
1576 print_flagged_mails(obj, p, p_max_size);
1578 OBJ(unflagged_mails) {
1579 print_unflagged_mails(obj, p, p_max_size);
1581 OBJ(forwarded_mails) {
1582 print_forwarded_mails(obj, p, p_max_size);
1584 OBJ(unforwarded_mails) {
1585 print_unforwarded_mails(obj, p, p_max_size);
1587 OBJ(replied_mails) {
1588 print_replied_mails(obj, p, p_max_size);
1590 OBJ(unreplied_mails) {
1591 print_unreplied_mails(obj, p, p_max_size);
1594 print_draft_mails(obj, p, p_max_size);
1596 OBJ(trashed_mails) {
1597 print_trashed_mails(obj, p, p_max_size);
1600 print_mboxscan(obj, p, p_max_size);
1603 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1606 new_outline(p, obj->data.l);
1609 print_pid_cmdline(obj, p, p_max_size);
1612 print_pid_cwd(obj, p, p_max_size);
1615 print_pid_environ(obj, p, p_max_size);
1617 OBJ(pid_environ_list) {
1618 print_pid_environ_list(obj, p, p_max_size);
1621 print_pid_exe(obj, p, p_max_size);
1623 OBJ(pid_openfiles) {
1624 print_pid_openfiles(obj, p, p_max_size);
1627 print_pid_stderr(obj, p, p_max_size);
1630 print_pid_stdin(obj, p, p_max_size);
1633 print_pid_stdout(obj, p, p_max_size);
1636 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1638 OBJ(running_processes) {
1639 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1642 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1645 snprintf(p, p_max_size, "%s", obj->data.s);
1649 new_bg(p, obj->data.l);
1652 new_stippled_hr(obj, p);
1656 human_readable(cur->swap * 1024, p, 255);
1659 human_readable(cur->swapfree * 1024, p, 255);
1662 human_readable(cur->swapmax * 1024, p, 255);
1665 if (cur->swapmax == 0) {
1666 strncpy(p, "No swap", p_max_size);
1668 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1673 if(output_methods & TO_X) {
1674 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1677 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1680 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1683 print_time(obj, p, p_max_size);
1686 print_utime(obj, p, p_max_size);
1689 print_tztime(obj, p, p_max_size);
1692 print_totaldown(obj, p, p_max_size);
1695 print_totalup(obj, p, p_max_size);
1698 snprintf(p, p_max_size, "%d", total_updates);
1701 if(total_updates % updatereset != obj->data.i - 1) {
1706 print_upspeed(obj, p, p_max_size);
1709 print_upspeedf(obj, p, p_max_size);
1713 print_upspeedgraph(obj, p);
1717 format_seconds_short(p, p_max_size, (int) cur->uptime);
1720 format_seconds(p, p_max_size, (int) cur->uptime);
1723 snprintf(p, p_max_size, "%s", cur->users.names);
1726 snprintf(p, p_max_size, "%s", cur->users.terms);
1729 snprintf(p, p_max_size, "%s", cur->users.times);
1732 snprintf(p, p_max_size, "%d", cur->users.number);
1734 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1735 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1739 msg = get_apm_adapter();
1740 snprintf(p, p_max_size, "%s", msg);
1743 OBJ(apm_battery_life) {
1746 msg = get_apm_battery_life();
1747 snprintf(p, p_max_size, "%s", msg);
1750 OBJ(apm_battery_time) {
1753 msg = get_apm_battery_time();
1754 snprintf(p, p_max_size, "%s", msg);
1757 #endif /* __FreeBSD__ __OpenBSD__ */
1760 #define mpd_printf(fmt, val) \
1761 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1762 #define mpd_sprintf(val) { \
1763 if (!obj->data.i || obj->data.i > p_max_size) \
1764 mpd_printf("%s", val); \
1766 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
1771 mpd_sprintf(artist);
1775 mpd_printf("%s", random);
1777 mpd_printf("%s", repeat);
1785 mpd_printf("%d", volume);
1787 mpd_printf("%d", bitrate);
1789 mpd_printf("%s", status);
1791 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
1794 format_media_player_time(p, p_max_size, mpd_get_info()->length);
1797 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
1801 if(output_methods & TO_X) {
1802 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
1805 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
1808 struct mpd_s *mpd = mpd_get_info();
1809 int len = obj->data.i;
1810 if (len == 0 || len > p_max_size)
1813 memset(p, 0, p_max_size);
1814 if (mpd->artist && *mpd->artist &&
1815 mpd->title && *mpd->title) {
1816 snprintf(p, len, "%s - %s", mpd->artist,
1818 } else if (mpd->title && *mpd->title) {
1819 snprintf(p, len, "%s", mpd->title);
1820 } else if (mpd->artist && *mpd->artist) {
1821 snprintf(p, len, "%s", mpd->artist);
1822 } else if (mpd->file && *mpd->file) {
1823 snprintf(p, len, "%s", mpd->file);
1828 OBJ(if_mpd_playing) {
1829 if (!mpd_get_info()->is_playing) {
1838 #define MOC_PRINT(t, a) \
1839 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1841 MOC_PRINT(state, "??");
1844 MOC_PRINT(file, "no file");
1847 MOC_PRINT(title, "no title");
1850 MOC_PRINT(artist, "no artist");
1853 MOC_PRINT(song, "no song");
1856 MOC_PRINT(album, "no album");
1858 OBJ(moc_totaltime) {
1859 MOC_PRINT(totaltime, "0:00");
1862 MOC_PRINT(timeleft, "0:00");
1865 MOC_PRINT(curtime, "0:00");
1868 MOC_PRINT(bitrate, "0Kbps");
1871 MOC_PRINT(rate, "0KHz");
1877 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
1880 snprintf(p, p_max_size, "%s", cur->xmms2.album);
1883 snprintf(p, p_max_size, "%s", cur->xmms2.title);
1886 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
1888 OBJ(xmms2_comment) {
1889 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
1892 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1895 snprintf(p, p_max_size, "%s", cur->xmms2.status);
1898 snprintf(p, p_max_size, "%s", cur->xmms2.date);
1900 OBJ(xmms2_tracknr) {
1901 if (cur->xmms2.tracknr != -1) {
1902 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
1905 OBJ(xmms2_bitrate) {
1906 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
1909 snprintf(p, p_max_size, "%u", cur->xmms2.id);
1912 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
1914 OBJ(xmms2_elapsed) {
1915 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
1916 (cur->xmms2.elapsed / 1000) % 60);
1918 OBJ(xmms2_duration) {
1919 snprintf(p, p_max_size, "%02d:%02d",
1920 cur->xmms2.duration / 60000,
1921 (cur->xmms2.duration / 1000) % 60);
1923 OBJ(xmms2_percent) {
1924 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
1928 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
1931 OBJ(xmms2_playlist) {
1932 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
1934 OBJ(xmms2_timesplayed) {
1935 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
1938 if (strlen(cur->xmms2.title) < 2
1939 && strlen(cur->xmms2.title) < 2) {
1940 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1942 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
1946 OBJ(if_xmms2_connected) {
1947 if (cur->xmms2.conn_state != 1) {
1953 OBJ(audacious_status) {
1954 snprintf(p, p_max_size, "%s",
1955 cur->audacious.items[AUDACIOUS_STATUS]);
1957 OBJ(audacious_title) {
1958 snprintf(p, cur->audacious.max_title_len > 0
1959 ? cur->audacious.max_title_len : p_max_size, "%s",
1960 cur->audacious.items[AUDACIOUS_TITLE]);
1962 OBJ(audacious_length) {
1963 snprintf(p, p_max_size, "%s",
1964 cur->audacious.items[AUDACIOUS_LENGTH]);
1966 OBJ(audacious_length_seconds) {
1967 snprintf(p, p_max_size, "%s",
1968 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1970 OBJ(audacious_position) {
1971 snprintf(p, p_max_size, "%s",
1972 cur->audacious.items[AUDACIOUS_POSITION]);
1974 OBJ(audacious_position_seconds) {
1975 snprintf(p, p_max_size, "%s",
1976 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
1978 OBJ(audacious_bitrate) {
1979 snprintf(p, p_max_size, "%s",
1980 cur->audacious.items[AUDACIOUS_BITRATE]);
1982 OBJ(audacious_frequency) {
1983 snprintf(p, p_max_size, "%s",
1984 cur->audacious.items[AUDACIOUS_FREQUENCY]);
1986 OBJ(audacious_channels) {
1987 snprintf(p, p_max_size, "%s",
1988 cur->audacious.items[AUDACIOUS_CHANNELS]);
1990 OBJ(audacious_filename) {
1991 snprintf(p, p_max_size, "%s",
1992 cur->audacious.items[AUDACIOUS_FILENAME]);
1994 OBJ(audacious_playlist_length) {
1995 snprintf(p, p_max_size, "%s",
1996 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
1998 OBJ(audacious_playlist_position) {
1999 snprintf(p, p_max_size, "%s",
2000 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2002 OBJ(audacious_main_volume) {
2003 snprintf(p, p_max_size, "%s",
2004 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2007 OBJ(audacious_bar) {
2011 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2012 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2013 new_bar(obj, p, (int) (progress * 255.0f));
2016 #endif /* AUDACIOUS */
2020 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2023 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2026 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2029 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2032 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2035 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2038 /* we have four different types of top (top, top_mem,
2039 * top_time and top_io). To avoid having almost-same code four
2040 * times, we have this special handler. */
2048 /* yes, passing top_name_width instead
2049 * of p_max_size is intended here */
2050 print_top(obj, p, top_name_width);
2052 print_tailhead("tail", obj, p, p_max_size);
2055 print_tailhead("head", obj, p, p_max_size);
2059 FILE *fp = open_file(obj->data.s, &rep);
2062 /* FIXME: use something more general (see also tail.c, head.c */
2063 #define BUFSZ 0x1000
2068 while(fgets(buf, BUFSZ, fp) != NULL){
2069 for(j = 0; buf[j] != 0; j++) {
2070 if(buf[j] == '\n') {
2075 sprintf(p, "%d", lines);
2078 sprintf(p, "File Unreadable");
2084 FILE *fp = open_file(obj->data.s, &rep);
2089 char inword = FALSE;
2092 while(fgets(buf, BUFSZ, fp) != NULL){
2093 for(j = 0; buf[j] != 0; j++) {
2094 if(!isspace(buf[j])) {
2095 if(inword == FALSE) {
2104 sprintf(p, "%d", words);
2107 sprintf(p, "File Unreadable");
2110 #ifdef TCP_PORT_MONITOR
2112 tcp_portmon_action(obj, p, p_max_size);
2114 #endif /* TCP_PORT_MONITOR */
2118 do_iconv_start(obj);
2123 #endif /* HAVE_ICONV */
2125 OBJ(entropy_avail) {
2126 print_entropy_avail(obj, p, p_max_size);
2129 print_entropy_perc(obj, p, p_max_size);
2131 OBJ(entropy_poolsize) {
2132 print_entropy_poolsize(obj, p, p_max_size);
2135 print_entropy_bar(obj, p, p_max_size);
2141 s = smapi_get_val(obj->data.s);
2142 snprintf(p, p_max_size, "%s", s);
2146 OBJ(if_smapi_bat_installed) {
2148 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2149 if(!smapi_bat_installed(idx)) {
2153 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2155 OBJ(smapi_bat_perc) {
2157 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2158 val = smapi_bat_installed(idx) ?
2159 smapi_get_bat_int(idx, "remaining_percent") : 0;
2160 percent_print(p, p_max_size, val);
2162 NORM_ERR("argument to smapi_bat_perc must be an integer");
2164 OBJ(smapi_bat_temp) {
2166 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2167 val = smapi_bat_installed(idx) ?
2168 smapi_get_bat_int(idx, "temperature") : 0;
2169 /* temperature is in milli degree celsius */
2170 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2172 NORM_ERR("argument to smapi_bat_temp must be an integer");
2174 OBJ(smapi_bat_power) {
2176 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2177 val = smapi_bat_installed(idx) ?
2178 smapi_get_bat_int(idx, "power_now") : 0;
2179 /* power_now is in mW, set to W with one digit precision */
2180 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2182 NORM_ERR("argument to smapi_bat_power must be an integer");
2185 OBJ(smapi_bat_bar) {
2186 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2187 new_bar(obj, p, (int)
2188 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2196 char buf[max_user_text];
2198 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2199 snprintf(p, p_max_size, "%s", buf);
2205 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2206 char buf[max_user_text];
2209 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2210 snprintf(p, p_max_size, "%s", buf);
2211 if(total_updates % 2) {
2212 for(j=0; p[j] != 0; j++) {
2218 char buf[max_user_text];
2220 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2222 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2223 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2224 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2225 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2226 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2227 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2228 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2230 snprintf(p, p_max_size, "%s", buf);
2233 print_scroll(obj, p, p_max_size, cur);
2236 print_combine(obj, p, cur);
2240 print_nvidia_value(obj, display, p, p_max_size);
2245 /* This is just a meta-object to set host:port */
2248 snprintf(p, p_max_size, "%s",
2249 cur->apcupsd.items[APCUPSD_NAME]);
2251 OBJ(apcupsd_model) {
2252 snprintf(p, p_max_size, "%s",
2253 cur->apcupsd.items[APCUPSD_MODEL]);
2255 OBJ(apcupsd_upsmode) {
2256 snprintf(p, p_max_size, "%s",
2257 cur->apcupsd.items[APCUPSD_UPSMODE]);
2259 OBJ(apcupsd_cable) {
2260 snprintf(p, p_max_size, "%s",
2261 cur->apcupsd.items[APCUPSD_CABLE]);
2263 OBJ(apcupsd_status) {
2264 snprintf(p, p_max_size, "%s",
2265 cur->apcupsd.items[APCUPSD_STATUS]);
2267 OBJ(apcupsd_linev) {
2268 snprintf(p, p_max_size, "%s",
2269 cur->apcupsd.items[APCUPSD_LINEV]);
2272 snprintf(p, p_max_size, "%s",
2273 cur->apcupsd.items[APCUPSD_LOAD]);
2275 OBJ(apcupsd_loadbar) {
2278 if(output_methods & TO_X) {
2279 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2280 new_bar(obj, p, (int) progress);
2284 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2285 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2289 OBJ(apcupsd_loadgraph) {
2291 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2292 new_graph(obj, p, (int)progress);
2294 OBJ(apcupsd_loadgauge) {
2296 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2297 new_gauge(obj, p, (int)progress);
2300 OBJ(apcupsd_charge) {
2301 snprintf(p, p_max_size, "%s",
2302 cur->apcupsd.items[APCUPSD_CHARGE]);
2304 OBJ(apcupsd_timeleft) {
2305 snprintf(p, p_max_size, "%s",
2306 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2309 snprintf(p, p_max_size, "%s",
2310 cur->apcupsd.items[APCUPSD_TEMP]);
2312 OBJ(apcupsd_lastxfer) {
2313 snprintf(p, p_max_size, "%s",
2314 cur->apcupsd.items[APCUPSD_LASTXFER]);
2316 #endif /* APCUPSD */
2323 size_t a = strlen(p);
2326 iconv_convert(&a, buff_in, p, p_max_size);
2327 #endif /* HAVE_ICONV */
2328 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2330 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2331 #endif /* HAVE_LUA */
2333 substitute_newlines(p, a - 2);
2342 /* load any new fonts we may have had */
2343 if (need_to_load_fonts) {
2349 void evaluate(const char *text, char *buffer)
2351 struct information *tmp_info;
2352 struct text_object subroot;
2354 tmp_info = malloc(sizeof(struct information));
2355 memcpy(tmp_info, &info, sizeof(struct information));
2356 parse_conky_vars(&subroot, text, buffer, tmp_info);
2357 DBGP("evaluated '%s' to '%s'", text, buffer);
2359 free_text_objects(&subroot, 1);
2363 double current_update_time, next_update_time, last_update_time;
2365 static void generate_text(void)
2367 struct information *cur = &info;
2374 current_update_time = get_time();
2378 /* add things to the buffer */
2384 generate_text_internal(p, max_user_text, global_root_object, cur);
2386 if (stuff_in_uppercase) {
2389 tmp_p = text_buffer;
2391 *tmp_p = toupper(*tmp_p);
2396 next_update_time += update_interval;
2397 if (next_update_time < get_time()) {
2398 next_update_time = get_time() + update_interval;
2399 } else if (next_update_time > get_time() + update_interval) {
2400 next_update_time = get_time() + update_interval;
2402 last_update_time = current_update_time;
2406 void set_update_interval(double interval)
2408 update_interval = interval;
2409 update_interval_old = interval;
2412 static inline int get_string_width(const char *s)
2414 return *s ? calc_text_width(s) : 0;
2418 static int get_string_width_special(char *s, int special_index)
2428 if ((output_methods & TO_X) == 0)
2431 p = strndup(s, text_buffer_size);
2435 if (*p == SPECIAL_CHAR) {
2436 /* shift everything over by 1 so that the special char
2437 * doesn't mess up the size calculation */
2438 for (i = 0; i < (long)strlen(p); i++) {
2439 *(p + i) = *(p + i + 1);
2441 if (specials[special_index + idx].type == GRAPH
2442 || specials[special_index + idx].type == GAUGE
2443 || specials[special_index + idx].type == BAR) {
2444 width += specials[special_index + idx].width;
2447 } else if (*p == SECRIT_MULTILINE_CHAR) {
2454 if (strlen(final) > 1) {
2455 width += calc_text_width(final);
2461 static int text_size_updater(char *s, int special_index);
2463 int last_font_height;
2464 static void update_text_area(void)
2468 if ((output_methods & TO_X) == 0)
2470 /* update text size if it isn't fixed */
2475 text_width = minimum_width;
2477 last_font_height = font_height();
2478 for_each_line(text_buffer, text_size_updater);
2480 if (text_height < minimum_height) {
2481 text_height = minimum_height;
2483 if (text_width > maximum_width && maximum_width > 0) {
2484 text_width = maximum_width;
2488 /* get text position on workarea */
2489 switch (text_alignment) {
2496 x = workarea[2] - text_width - gap_x;
2501 x = workarea[2] / 2 - text_width / 2 - gap_x;
2508 y = workarea[3] - text_height - gap_y;
2512 x = workarea[2] - text_width - gap_x;
2513 y = workarea[3] - text_height - gap_y;
2517 x = workarea[2] / 2 - text_width / 2 - gap_x;
2518 y = workarea[3] - text_height - gap_y;
2523 y = workarea[3] / 2 - text_height / 2 - gap_y;
2527 x = workarea[2] - text_width - gap_x;
2528 y = workarea[3] / 2 - text_height / 2 - gap_y;
2532 case NONE: // Let the WM manage the window
2543 if (own_window && !fixed_pos) {
2546 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2547 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2548 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2549 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2553 /* If window size doesn't match to workarea's size,
2554 * then window probably includes panels (gnome).
2555 * Blah, doesn't work on KDE. */
2556 if (workarea[2] != window.width || workarea[3] != window.height) {
2565 /* update lua window globals */
2566 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2567 #endif /* HAVE_LUA */
2572 static int cur_x, cur_y; /* current x and y for drawing */
2574 //draw_mode also without X11 because we only need to print to stdout with FG
2575 static int draw_mode; /* FG, BG or OUTLINE */
2577 static long current_color;
2579 static int text_size_updater(char *s, int special_index)
2584 if ((output_methods & TO_X) == 0)
2586 /* get string widths and skip specials */
2589 if (*p == SPECIAL_CHAR) {
2591 w += get_string_width(s);
2594 if (specials[special_index].type == BAR
2595 || specials[special_index].type == GAUGE
2596 || specials[special_index].type == GRAPH) {
2597 w += specials[special_index].width;
2598 if (specials[special_index].height > last_font_height) {
2599 last_font_height = specials[special_index].height;
2600 last_font_height += font_height();
2602 } else if (specials[special_index].type == OFFSET) {
2603 if (specials[special_index].arg > 0) {
2604 w += specials[special_index].arg;
2606 } else if (specials[special_index].type == VOFFSET) {
2607 last_font_height += specials[special_index].arg;
2608 } else if (specials[special_index].type == GOTO) {
2609 if (specials[special_index].arg > cur_x) {
2610 w = (int) specials[special_index].arg;
2612 } else if (specials[special_index].type == TAB) {
2613 int start = specials[special_index].arg;
2614 int step = specials[special_index].width;
2616 if (!step || step < 0) {
2619 w += step - (cur_x - text_start_x - start) % step;
2620 } else if (specials[special_index].type == FONT) {
2621 selected_font = specials[special_index].font_added;
2622 if (font_height() > last_font_height) {
2623 last_font_height = font_height();
2629 } else if (*p == SECRIT_MULTILINE_CHAR) {
2632 lw = get_string_width(s);
2633 *p = SECRIT_MULTILINE_CHAR;
2635 w = lw > w ? lw : w;
2636 text_height += last_font_height;
2640 w += get_string_width(s);
2641 if (w > text_width) {
2644 if (text_width > maximum_width && maximum_width) {
2645 text_width = maximum_width;
2648 text_height += last_font_height;
2649 last_font_height = font_height();
2650 return special_index;
2654 static inline void set_foreground_color(long c)
2657 if (output_methods & TO_X) {
2659 XSetForeground(display, window.gc, c);
2663 if (output_methods & TO_NCURSES) {
2664 attron(COLOR_PAIR(c));
2666 #endif /* NCURSES */
2671 static void draw_string(const char *s)
2673 int i, i2, pos, width_of_s;
2676 char *s_with_newlines;
2682 width_of_s = get_string_width(s);
2683 s_with_newlines = strdup(s);
2684 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2685 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2686 s_with_newlines[i] = '\n';
2689 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2690 printf("%s\n", s_with_newlines);
2691 if (extra_newline) fputc('\n', stdout);
2692 fflush(stdout); /* output immediately, don't buffer */
2694 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2695 fprintf(stderr, "%s\n", s_with_newlines);
2696 fflush(stderr); /* output immediately, don't buffer */
2698 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2699 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2701 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2702 fprintf(append_fpointer, "%s\n", s_with_newlines);
2705 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2706 printw("%s", s_with_newlines);
2709 free(s_with_newlines);
2710 memset(tmpstring1, 0, text_buffer_size);
2711 memset(tmpstring2, 0, text_buffer_size);
2712 strncpy(tmpstring1, s, text_buffer_size - 1);
2717 if (output_methods & TO_X) {
2718 max = ((text_width - width_of_s) / get_string_width(" "));
2721 /* This code looks for tabs in the text and coverts them to spaces.
2722 * The trick is getting the correct number of spaces, and not going
2723 * over the window's size without forcing the window larger. */
2724 for (i = 0; i < (int) text_buffer_size; i++) {
2725 if (tmpstring1[i] == '\t') {
2727 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2728 /* guard against overrun */
2729 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2734 /* guard against overrun */
2735 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2740 if (output_methods & TO_X) {
2741 if (text_width == maximum_width) {
2742 /* this means the text is probably pushing the limit,
2743 * so we'll chop it */
2744 while (cur_x + get_string_width(tmpstring2) - text_start_x
2745 > maximum_width && strlen(tmpstring2) > 0) {
2746 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2753 if (output_methods & TO_X) {
2759 c.pixel = current_color;
2760 XQueryColor(display, DefaultColormap(display, screen), &c);
2763 c2.color.red = c.red;
2764 c2.color.green = c.green;
2765 c2.color.blue = c.blue;
2766 c2.color.alpha = fonts[selected_font].font_alpha;
2768 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2769 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2771 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2772 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2777 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2780 cur_x += width_of_s;
2783 memcpy(tmpstring1, s, text_buffer_size);
2786 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2794 int last_special_needed = -1;
2795 int orig_special_index = special_index;
2798 if (output_methods & TO_X) {
2799 font_h = font_height();
2800 cur_y += font_ascent();
2802 cur_x = text_start_x;
2806 if (*p == SECRIT_MULTILINE_CHAR) {
2807 /* special newline marker for multiline objects */
2812 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2817 /* draw string before special, unless we're dealing multiline
2819 if (last_special_applied > -1) {
2820 special_index = last_special_applied;
2828 switch (specials[special_index].type) {
2830 case HORIZONTAL_LINE:
2832 int h = specials[special_index].height;
2833 int mid = font_ascent() / 2;
2835 w = text_start_x + text_width - cur_x;
2837 XSetLineAttributes(display, window.gc, h, LineSolid,
2838 CapButt, JoinMiter);
2839 XDrawLine(display, window.drawable, window.gc, cur_x,
2840 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2846 int h = specials[special_index].height;
2847 int tmp_s = specials[special_index].arg;
2848 int mid = font_ascent() / 2;
2849 char ss[2] = { tmp_s, tmp_s };
2851 w = text_start_x + text_width - cur_x - 1;
2852 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2853 CapButt, JoinMiter);
2854 XSetDashes(display, window.gc, 0, ss, 2);
2855 XDrawLine(display, window.drawable, window.gc, cur_x,
2856 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2862 int h, bar_usage, by;
2863 if (cur_x - text_start_x > maximum_width
2864 && maximum_width > 0) {
2867 h = specials[special_index].height;
2868 bar_usage = specials[special_index].arg;
2869 by = cur_y - (font_ascent() / 2) - 1;
2874 w = specials[special_index].width;
2876 w = text_start_x + text_width - cur_x - 1;
2882 XSetLineAttributes(display, window.gc, 1, LineSolid,
2883 CapButt, JoinMiter);
2885 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2887 XFillRectangle(display, window.drawable, window.gc, cur_x,
2888 by, w * bar_usage / 255, h);
2896 case GAUGE: /* new GAUGE */
2899 unsigned long last_colour = current_color;
2901 float angle, px, py;
2905 if (cur_x - text_start_x > maximum_width
2906 && maximum_width > 0) {
2910 h = specials[special_index].height;
2911 by = cur_y - (font_ascent() / 2) - 1;
2916 w = specials[special_index].width;
2918 w = text_start_x + text_width - cur_x - 1;
2924 XSetLineAttributes(display, window.gc, 1, LineSolid,
2925 CapButt, JoinMiter);
2927 XDrawArc(display, window.drawable, window.gc,
2928 cur_x, by, w, h * 2, 0, 180*64);
2931 usage = specials[special_index].arg;
2932 angle = (M_PI)*(float)(usage)/255.;
2933 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2934 py = (float)(by+(h))-(float)(h)*sin(angle);
2936 XDrawLine(display, window.drawable, window.gc,
2937 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2945 set_foreground_color(last_colour);
2953 int h, by, i = 0, j = 0;
2955 unsigned long last_colour = current_color;
2956 unsigned long *tmpcolour = 0;
2957 if (cur_x - text_start_x > maximum_width
2958 && maximum_width > 0) {
2961 h = specials[special_index].height;
2962 by = cur_y - (font_ascent() / 2) - 1;
2967 w = specials[special_index].width;
2969 w = text_start_x + text_width - cur_x - 1;
2974 if (draw_graph_borders) {
2975 XSetLineAttributes(display, window.gc, 1, LineSolid,
2976 CapButt, JoinMiter);
2977 XDrawRectangle(display, window.drawable, window.gc,
2980 XSetLineAttributes(display, window.gc, 1, LineSolid,
2981 CapButt, JoinMiter);
2983 if (specials[special_index].last_colour != 0
2984 || specials[special_index].first_colour != 0) {
2985 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
2988 for (i = w - 2; i > -1; i--) {
2989 if (specials[special_index].last_colour != 0
2990 || specials[special_index].first_colour != 0) {
2991 if (specials[special_index].tempgrad) {
2994 (int)((float)(w - 2) - specials[special_index].graph[j] *
2995 (w - 2) / (float)specials[special_index].graph_scale)
2999 (int)((float)(w - 2) - specials[special_index].graph[j] *
3000 (w - 2) / (float)specials[special_index].graph_scale)
3003 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3005 (int)((float)(w - 2) - specials[special_index].graph[j] *
3006 (w - 2) / (float)specials[special_index].graph_scale)
3010 #endif /* DEBUG_lol */
3011 XSetForeground(display, window.gc, tmpcolour[
3012 (int)((float)(w - 2) - specials[special_index].graph[j] *
3013 (w - 2) / (float)specials[special_index].graph_scale)
3016 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3019 /* this is mugfugly, but it works */
3020 XDrawLine(display, window.drawable, window.gc,
3021 cur_x + i + 1, by + h, cur_x + i + 1,
3022 round_to_int((double)by + h - specials[special_index].graph[j] *
3023 (h - 1) / specials[special_index].graph_scale));
3024 if ((w - i) / ((float) (w - 2) /
3025 (specials[special_index].graph_width)) > j
3026 && j < MAX_GRAPH_DEPTH - 3) {
3030 if (tmpcolour) free(tmpcolour);
3035 /* if (draw_mode == BG) {
3036 set_foreground_color(default_bg_color);
3037 } else if (draw_mode == OUTLINE) {
3038 set_foreground_color(default_out_color);
3040 set_foreground_color(default_fg_color);
3042 if (show_graph_range) {
3045 unsigned short int seconds = update_interval * w;
3051 unsigned short int timeunits;
3053 timeunits = seconds / 86400; seconds %= 86400;
3054 if (timeunits > 0) {
3055 asprintf(&tmp_day_str, "%dd", timeunits);
3057 tmp_day_str = strdup("");
3059 timeunits = seconds / 3600; seconds %= 3600;
3060 if (timeunits > 0) {
3061 asprintf(&tmp_hour_str, "%dh", timeunits);
3063 tmp_hour_str = strdup("");
3065 timeunits = seconds / 60; seconds %= 60;
3066 if (timeunits > 0) {
3067 asprintf(&tmp_min_str, "%dm", timeunits);
3069 tmp_min_str = strdup("");
3072 asprintf(&tmp_sec_str, "%ds", seconds);
3074 tmp_sec_str = strdup("");
3076 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3077 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3079 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3081 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3082 cur_y += font_h / 2;
3083 draw_string(tmp_str);
3089 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3093 cur_x += font_ascent() / 2;
3094 cur_y += font_h / 2;
3096 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3098 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3099 draw_string(tmp_str);
3105 set_foreground_color(last_colour);
3111 int old = font_ascent();
3113 cur_y -= font_ascent();
3114 selected_font = specials[special_index].font_added;
3116 if (cur_y + font_ascent() < cur_y + old) {
3119 cur_y += font_ascent();
3121 font_h = font_height();
3126 if (draw_mode == FG) {
3127 set_foreground_color(specials[special_index].arg);
3133 if (draw_mode == BG) {
3134 set_foreground_color(specials[special_index].arg);
3139 if (draw_mode == OUTLINE) {
3140 set_foreground_color(specials[special_index].arg);
3145 w += specials[special_index].arg;
3146 last_special_needed = special_index;
3150 cur_y += specials[special_index].arg;
3154 if (specials[special_index].arg >= 0) {
3155 cur_x = (int) specials[special_index].arg;
3157 last_special_needed = special_index;
3162 int start = specials[special_index].arg;
3163 int step = specials[special_index].width;
3165 if (!step || step < 0) {
3168 w = step - (cur_x - text_start_x - start) % step;
3169 last_special_needed = special_index;
3175 /* TODO: add back in "+ window.border_inner_margin" to the end of
3177 int pos_x = text_start_x + text_width -
3178 get_string_width_special(s, special_index);
3180 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3181 "get_string_width(p) %i gap_x %i "
3182 "specials[special_index].arg %i window.border_inner_margin %i "
3183 "window.border_width %i\n", pos_x, text_start_x, text_width,
3184 cur_x, get_string_width_special(s), gap_x,
3185 specials[special_index].arg, window.border_inner_margin,
3186 window.border_width); */
3187 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3188 cur_x = pos_x - specials[special_index].arg;
3190 last_special_needed = special_index;
3196 int pos_x = (text_width) / 2 - get_string_width_special(s,
3197 special_index) / 2 - (cur_x -
3199 /* int pos_x = text_start_x + text_width / 2 -
3200 get_string_width_special(s) / 2; */
3202 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3203 "get_string_width(p) %i gap_x %i "
3204 "specials[special_index].arg %i\n", pos_x, text_start_x,
3205 text_width, cur_x, get_string_width(s), gap_x,
3206 specials[special_index].arg); */
3207 if (pos_x > specials[special_index].arg) {
3208 w = pos_x - specials[special_index].arg;
3210 last_special_needed = special_index;
3220 if (special_index != last_special_applied) {
3223 special_index = orig_special_index;
3224 last_special_applied = -1;
3235 if (output_methods & TO_NCURSES) {
3238 #endif /* NCURSES */
3240 if (output_methods & TO_X)
3241 cur_y += font_descent();
3243 if (recurse && *recurse) {
3244 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3245 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3247 return special_index;
3250 static int draw_line(char *s, int special_index)
3253 if (output_methods & TO_X) {
3254 return draw_each_line_inner(s, special_index, -1);
3258 if (output_methods & TO_NCURSES) {
3259 return draw_each_line_inner(s, special_index, -1);
3261 #endif /* NCURSES */
3263 UNUSED(special_index);
3267 static void draw_text(void)
3271 llua_draw_pre_hook();
3272 #endif /* HAVE_LUA */
3273 if (output_methods & TO_X) {
3274 cur_y = text_start_y;
3277 if (draw_borders && window.border_width > 0) {
3278 if (stippled_borders) {
3279 char ss[2] = { stippled_borders, stippled_borders };
3280 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3281 CapButt, JoinMiter);
3282 XSetDashes(display, window.gc, 0, ss, 2);
3284 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3285 CapButt, JoinMiter);
3288 XDrawRectangle(display, window.drawable, window.gc,
3289 text_start_x - window.border_inner_margin - window.border_width,
3290 text_start_y - window.border_inner_margin - window.border_width,
3291 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3292 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3300 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3301 attron(COLOR_PAIR(COLOR_WHITE));
3302 #endif /* NCURSES */
3303 for_each_line(text_buffer, draw_line);
3304 #if defined(HAVE_LUA) && defined(X11)
3305 llua_draw_post_hook();
3306 #endif /* HAVE_LUA */
3309 static void draw_stuff(void)
3312 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3314 if (overwrite_file) {
3315 overwrite_fpointer = fopen(overwrite_file, "w");
3316 if(!overwrite_fpointer)
3317 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3320 append_fpointer = fopen(append_file, "a");
3321 if(!append_fpointer)
3322 NORM_ERR("Can't append '%s' anymore", append_file);
3325 if (output_methods & TO_X) {
3327 if (draw_shades && !draw_outline) {
3330 set_foreground_color(default_bg_color);
3341 for (i = -1; i < 2; i++) {
3342 for (j = -1; j < 2; j++) {
3343 if (i == 0 && j == 0) {
3348 set_foreground_color(default_out_color);
3349 draw_mode = OUTLINE;
3357 set_foreground_color(default_fg_color);
3362 #if defined(X11) && defined(HAVE_XDBE)
3363 if (output_methods & TO_X) {
3364 xdbe_swap_buffers();
3366 #endif /* X11 && HAVE_XDBE */
3367 if(overwrite_fpointer) {
3368 fclose(overwrite_fpointer);
3369 overwrite_fpointer = 0;
3371 if(append_fpointer) {
3372 fclose(append_fpointer);
3373 append_fpointer = 0;
3378 static void clear_text(int exposures)
3382 /* The swap action is XdbeBackground, which clears */
3386 if (display && window.window) { // make sure these are !null
3387 /* there is some extra space for borders and outlines */
3388 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3389 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3390 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3391 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3396 static int need_to_update;
3398 /* update_text() generates new text and clears old text area */
3399 static void update_text(void)
3406 if (output_methods & TO_X)
3411 llua_update_info(&info, update_interval);
3412 #endif /* HAVE_LUA */
3415 #ifdef HAVE_SYS_INOTIFY_H
3419 static void main_loop(void)
3422 #ifdef SIGNAL_BLOCKING
3423 sigset_t newmask, oldmask;
3426 #ifdef HAVE_SYS_INOTIFY_H
3427 int inotify_config_wd = -1;
3428 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3429 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3430 char inotify_buff[INOTIFY_BUF_LEN];
3431 #endif /* HAVE_SYS_INOTIFY_H */
3434 #ifdef SIGNAL_BLOCKING
3435 sigemptyset(&newmask);
3436 sigaddset(&newmask, SIGINT);
3437 sigaddset(&newmask, SIGTERM);
3438 sigaddset(&newmask, SIGUSR1);
3441 last_update_time = 0.0;
3442 next_update_time = get_time();
3444 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3445 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3446 char buf[max_user_text];
3448 get_battery_short_status(buf, max_user_text, "BAT0");
3450 update_interval = update_interval_bat;
3452 update_interval = update_interval_old;
3457 #ifdef SIGNAL_BLOCKING
3458 /* block signals. we will inspect for pending signals later */
3459 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3460 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3465 if (output_methods & TO_X) {
3468 /* wait for X event or timeout */
3470 if (!XPending(display)) {
3474 t = next_update_time - get_time();
3478 } else if (t > update_interval) {
3479 t = update_interval;
3482 tv.tv_sec = (long) t;
3483 tv.tv_usec = (long) (t * 1000000) % 1000000;
3485 FD_SET(ConnectionNumber(display), &fdsr);
3487 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3489 if (errno != EINTR) {
3490 NORM_ERR("can't select(): %s", strerror(errno));
3500 if (need_to_update) {
3502 int wx = window.x, wy = window.y;
3512 /* resize window if it isn't right size */
3514 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3515 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3516 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3517 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3518 draw_stuff(); /* redraw everything in our newly sized window */
3519 XResizeWindow(display, window.window, window.width,
3520 window.height); /* resize window */
3521 set_transparent_background(window.window);
3524 xdbe_swap_buffers();
3529 /* update lua window globals */
3530 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3531 #endif /* HAVE_LUA */
3534 /* move window if it isn't in right position */
3535 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3536 XMoveWindow(display, window.window, window.x, window.y);
3541 if (changed && window.type == TYPE_PANEL) {
3544 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3547 switch (text_alignment) {
3574 set_struts(sidenum);
3585 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3586 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3587 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3588 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3589 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3594 /* handle X events */
3595 while (XPending(display)) {
3598 XNextEvent(display, &ev);
3605 r.width = ev.xexpose.width;
3606 r.height = ev.xexpose.height;
3607 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3611 case PropertyNotify:
3613 if ( ev.xproperty.state == PropertyNewValue ) {
3614 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3620 case ReparentNotify:
3621 /* set background to ParentRelative for all parents */
3623 set_transparent_background(window.window);
3627 case ConfigureNotify:
3629 /* if window size isn't what expected, set fixed size */
3630 if (ev.xconfigure.width != window.width
3631 || ev.xconfigure.height != window.height) {
3632 if (window.width != 0 && window.height != 0) {
3636 /* clear old stuff before screwing up
3641 XWindowAttributes attrs;
3642 if (XGetWindowAttributes(display,
3643 window.window, &attrs)) {
3644 window.width = attrs.width;
3645 window.height = attrs.height;
3649 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3650 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3651 if (text_width > maximum_width
3652 && maximum_width > 0) {
3653 text_width = maximum_width;
3657 /* if position isn't what expected, set fixed pos
3658 * total_updates avoids setting fixed_pos when window
3659 * is set to weird locations when started */
3660 /* // this is broken
3661 if (total_updates >= 2 && !fixed_pos
3662 && (window.x != ev.xconfigure.x
3663 || window.y != ev.xconfigure.y)
3664 && (ev.xconfigure.x != 0
3665 || ev.xconfigure.y != 0)) {
3673 /* if an ordinary window with decorations */
3674 if ((window.type == TYPE_NORMAL &&
3675 (!TEST_HINT(window.hints,
3676 HINT_UNDECORATED))) ||
3677 window.type == TYPE_DESKTOP) {
3678 /* allow conky to hold input focus. */
3681 /* forward the click to the desktop window */
3682 XUngrabPointer(display, ev.xbutton.time);
3683 ev.xbutton.window = window.desktop;
3684 ev.xbutton.x = ev.xbutton.x_root;
3685 ev.xbutton.y = ev.xbutton.y_root;
3686 XSendEvent(display, ev.xbutton.window, False,
3687 ButtonPressMask, &ev);
3688 XSetInputFocus(display, ev.xbutton.window,
3689 RevertToParent, ev.xbutton.time);
3696 /* if an ordinary window with decorations */
3697 if ((window.type == TYPE_NORMAL)
3698 && (!TEST_HINT(window.hints,
3699 HINT_UNDECORATED))) {
3700 /* allow conky to hold input focus. */
3703 /* forward the release to the desktop window */
3704 ev.xbutton.window = window.desktop;
3705 ev.xbutton.x = ev.xbutton.x_root;
3706 ev.xbutton.y = ev.xbutton.y_root;
3707 XSendEvent(display, ev.xbutton.window, False,
3708 ButtonReleaseMask, &ev);
3717 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3718 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3720 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3721 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3723 #endif /* HAVE_XDAMAGE */
3729 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3730 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3731 #endif /* HAVE_XDAMAGE */
3733 /* XDBE doesn't seem to provide a way to clear the back buffer
3734 * without interfering with the front buffer, other than passing
3735 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3736 * using XDBE, we need to redraw the text even if it wasn't part of
3737 * the exposed area. OTOH, if we're not going to call draw_stuff at
3738 * all, then no swap happens and we can safely do nothing. */
3740 if (!XEmptyRegion(x11_stuff.region)) {
3745 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3746 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3747 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3748 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3749 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3752 XSetRegion(display, window.gc, x11_stuff.region);
3755 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3759 XDestroyRegion(x11_stuff.region);
3760 x11_stuff.region = XCreateRegion();
3764 t = (next_update_time - get_time()) * 1000000;
3765 if(t > 0) usleep((useconds_t)t);
3769 if(output_methods & TO_NCURSES) {
3778 #ifdef SIGNAL_BLOCKING
3779 /* unblock signals of interest and let handler fly */
3780 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3781 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3785 switch (g_signal_pending) {
3788 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3793 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3796 if (output_methods & TO_X) {
3797 XDestroyRegion(x11_stuff.region);
3798 x11_stuff.region = NULL;
3800 XDamageDestroy(display, x11_stuff.damage);
3801 XFixesDestroyRegion(display, x11_stuff.region2);
3802 XFixesDestroyRegion(display, x11_stuff.part);
3803 #endif /* HAVE_XDAMAGE */
3809 if(overwrite_file) {
3810 free(overwrite_file);
3819 /* Reaching here means someone set a signal
3820 * (SIGXXXX, signal_handler), but didn't write any code
3822 * If you don't want to handle a signal, don't set a handler on
3823 * it in the first place. */
3824 if (g_signal_pending) {
3825 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3829 #ifdef HAVE_SYS_INOTIFY_H
3830 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3831 inotify_config_wd = inotify_add_watch(inotify_fd,
3835 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3836 int len = 0, idx = 0;
3838 struct timeval time_to_wait;
3840 FD_ZERO(&descriptors);
3841 FD_SET(inotify_fd, &descriptors);
3843 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3845 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3846 if (FD_ISSET(inotify_fd, &descriptors)) {
3847 /* process inotify events */
3848 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3849 while (len > 0 && idx < len) {
3850 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3851 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3852 /* current_config should be reloaded */
3853 NORM_ERR("'%s' modified, reloading...", current_config);
3855 if (ev->mask & IN_IGNORED) {
3856 /* for some reason we get IN_IGNORED here
3857 * sometimes, so we need to re-add the watch */
3858 inotify_config_wd = inotify_add_watch(inotify_fd,
3865 llua_inotify_query(ev->wd, ev->mask);
3867 #endif /* HAVE_LUA */
3868 idx += INOTIFY_EVENT_SIZE + ev->len;
3872 #endif /* HAVE_SYS_INOTIFY_H */
3875 llua_update_info(&info, update_interval);
3876 #endif /* HAVE_LUA */
3877 g_signal_pending = 0;
3879 clean_up(NULL, NULL);
3881 #ifdef HAVE_SYS_INOTIFY_H
3882 if (inotify_fd != -1) {
3883 inotify_rm_watch(inotify_fd, inotify_config_wd);
3885 inotify_fd = inotify_config_wd = 0;
3887 #endif /* HAVE_SYS_INOTIFY_H */
3891 static void load_config_file_x11(const char *);
3893 void initialisation(int argc, char** argv);
3895 /* reload the config file */
3896 static void reload_config(void)
3898 char *current_config_copy = strdup(current_config);
3899 clean_up(NULL, NULL);
3900 current_config = current_config_copy;
3901 initialisation(argc_copy, argv_copy);
3904 void clean_up(void *memtofree1, void* memtofree2)
3909 if(output_methods & TO_NCURSES) {
3913 conftree_empty(currentconffile);
3914 currentconffile = NULL;
3921 timed_thread_destroy_registered_threads();
3923 if (info.cpu_usage) {
3924 free(info.cpu_usage);
3925 info.cpu_usage = NULL;
3928 if (x_initialised == YES) {
3929 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3930 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3931 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3932 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3935 if(x11_stuff.region) {
3936 XDestroyRegion(x11_stuff.region);
3937 x11_stuff.region = NULL;
3939 XCloseDisplay(display);
3941 if(info.x11.desktop.all_names) {
3942 free(info.x11.desktop.all_names);
3943 info.x11.desktop.all_names = NULL;
3945 if (info.x11.desktop.name) {
3946 free(info.x11.desktop.name);
3947 info.x11.desktop.name = NULL;
3951 free(fonts); //in set_default_configurations a font is set but not loaded
3957 free_update_callbacks();
3961 free_text_objects(&global_root_object, 0);
3980 free(current_config);
3983 #ifdef TCP_PORT_MONITOR
3984 tcp_portmon_clear();
3993 weather_free_info();
3996 llua_shutdown_hook();
3998 #endif /* HAVE_LUA */
4000 if (output_methods & TO_X)
4008 for (i = 0; i < special_count; i++) {
4009 if (specials[i].type == GRAPH) {
4010 free(specials[i].graph);
4018 clear_diskio_stats();
4019 if(global_cpu != NULL) {
4025 static int string_to_bool(const char *s)
4028 // Assumes an option without a true/false means true
4030 } else if (strcasecmp(s, "yes") == EQUAL) {
4032 } else if (strcasecmp(s, "true") == EQUAL) {
4034 } else if (strcasecmp(s, "1") == EQUAL) {
4041 static enum alignment string_to_alignment(const char *s)
4043 if (strcasecmp(s, "top_left") == EQUAL) {
4045 } else if (strcasecmp(s, "top_right") == EQUAL) {
4047 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4049 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4051 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4052 return BOTTOM_RIGHT;
4053 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4054 return BOTTOM_MIDDLE;
4055 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4057 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4058 return MIDDLE_RIGHT;
4059 } else if (strcasecmp(s, "tl") == EQUAL) {
4061 } else if (strcasecmp(s, "tr") == EQUAL) {
4063 } else if (strcasecmp(s, "tm") == EQUAL) {
4065 } else if (strcasecmp(s, "bl") == EQUAL) {
4067 } else if (strcasecmp(s, "br") == EQUAL) {
4068 return BOTTOM_RIGHT;
4069 } else if (strcasecmp(s, "bm") == EQUAL) {
4070 return BOTTOM_MIDDLE;
4071 } else if (strcasecmp(s, "ml") == EQUAL) {
4073 } else if (strcasecmp(s, "mr") == EQUAL) {
4074 return MIDDLE_RIGHT;
4075 } else if (strcasecmp(s, "none") == EQUAL) {
4083 static void set_default_configurations_for_x(void)
4085 default_fg_color = WhitePixel(display, screen);
4086 default_bg_color = BlackPixel(display, screen);
4087 default_out_color = BlackPixel(display, screen);
4088 color0 = default_fg_color;
4089 color1 = default_fg_color;
4090 color2 = default_fg_color;
4091 color3 = default_fg_color;
4092 color4 = default_fg_color;
4093 color5 = default_fg_color;
4094 color6 = default_fg_color;
4095 color7 = default_fg_color;
4096 color8 = default_fg_color;
4097 color9 = default_fg_color;
4098 current_text_color = default_fg_color;
4102 static void set_default_configurations(void)
4109 fork_to_background = 0;
4110 total_run_times = 0;
4111 info.cpu_avg_samples = 2;
4112 info.net_avg_samples = 2;
4113 info.diskio_avg_samples = 2;
4118 format_human_readable = 1;
4128 mpd_env_host = getenv("MPD_HOST");
4129 mpd_env_port = getenv("MPD_PORT");
4131 if (!mpd_env_host || !strlen(mpd_env_host)) {
4132 mpd_set_host("localhost");
4134 /* MPD_HOST environment variable is set */
4135 char *mpd_hostpart = strchr(mpd_env_host, '@');
4136 if (!mpd_hostpart) {
4137 mpd_set_host(mpd_env_host);
4139 /* MPD_HOST contains a password */
4140 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4141 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4143 if (!strlen(mpd_hostpart + 1)) {
4144 mpd_set_host("localhost");
4146 mpd_set_host(mpd_hostpart + 1);
4149 mpd_set_password(mpd_password, 1);
4154 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4155 /* failed to set port from environment variable */
4156 mpd_set_port("6600");
4160 info.xmms2.artist = NULL;
4161 info.xmms2.album = NULL;
4162 info.xmms2.title = NULL;
4163 info.xmms2.genre = NULL;
4164 info.xmms2.comment = NULL;
4165 info.xmms2.url = NULL;
4166 info.xmms2.status = NULL;
4167 info.xmms2.playlist = NULL;
4169 use_spacer = NO_SPACER;
4171 output_methods = TO_X;
4173 output_methods = TO_STDOUT;
4176 show_graph_scale = 0;
4177 show_graph_range = 0;
4180 draw_graph_borders = 1;
4182 set_first_font("6x10");
4190 window.type = TYPE_NORMAL;
4192 strcpy(window.class_name, PACKAGE_NAME);
4193 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4195 stippled_borders = 0;
4196 window.border_inner_margin = 3;
4197 window.border_outer_margin = 1;
4198 window.border_width = 1;
4199 text_alignment = BOTTOM_LEFT;
4200 info.x11.monitor.number = 1;
4201 info.x11.monitor.current = 0;
4202 info.x11.desktop.current = 1;
4203 info.x11.desktop.number = 1;
4204 info.x11.desktop.nitems = 0;
4205 info.x11.desktop.all_names = NULL;
4206 info.x11.desktop.name = NULL;
4211 free(current_mail_spool);
4215 variable_substitute(MAIL_FILE, buf, 256);
4216 if (buf[0] != '\0') {
4217 current_mail_spool = strndup(buf, text_buffer_size);
4222 set_update_interval(3);
4223 update_interval_bat = NOBATTERY;
4224 info.music_player_interval = 1.0;
4225 stuff_in_uppercase = 0;
4226 info.users.number = 1;
4228 #ifdef TCP_PORT_MONITOR
4229 /* set default connection limit */
4230 tcp_portmon_set_max_connections(0);
4234 /* returns 1 if you can overwrite or create the file at 'path' */
4235 static _Bool overwrite_works(const char *path)
4239 if (!(filepointer = fopen(path, "w")))
4241 fclose(filepointer);
4245 /* returns 1 if you can append or create the file at 'path' */
4246 static _Bool append_works(const char *path)
4250 if (!(filepointer = fopen(path, "a")))
4252 fclose(filepointer);
4258 /* WARNING, this type not in Xlib spec */
4259 int x11_error_handler(Display *d, XErrorEvent *err)
4260 __attribute__((noreturn));
4261 int x11_error_handler(Display *d, XErrorEvent *err)
4263 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",
4265 (long unsigned)err->display,
4266 (long)err->resourceid,
4276 int x11_ioerror_handler(Display *d)
4277 __attribute__((noreturn));
4278 int x11_ioerror_handler(Display *d)
4280 NORM_ERR("X Error: Display %lx\n",
4287 static void X11_initialisation(void)
4289 if (x_initialised == YES) return;
4290 output_methods |= TO_X;
4292 set_default_configurations_for_x();
4293 x_initialised = YES;
4296 /* WARNING, this type not in Xlib spec */
4297 XSetErrorHandler(&x11_error_handler);
4298 XSetIOErrorHandler(&x11_ioerror_handler);
4302 static char **xargv = 0;
4303 static int xargc = 0;
4305 static void X11_create_window(void)
4307 if (output_methods & TO_X) {
4309 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4310 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4312 #else /* OWN_WINDOW */
4313 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4314 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4316 #endif /* OWN_WINDOW */
4320 update_text_area(); /* to position text/window on screen */
4323 if (own_window && !fixed_pos) {
4324 XMoveWindow(display, window.window, window.x, window.y);
4327 set_transparent_background(window.window);
4335 x11_stuff.region = XCreateRegion();
4337 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4338 NORM_ERR("Xdamage extension unavailable");
4340 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4341 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4342 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4343 #endif /* HAVE_XDAMAGE */
4346 update_text_area(); /* to get initial size of the window */
4349 /* setup lua window globals */
4350 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4351 #endif /* HAVE_LUA */
4355 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4356 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4357 #define CONF2(a) if (strcasecmp(name, a) == 0)
4358 #define CONF(a) else CONF2(a)
4359 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4360 || strcasecmp(name, b) == 0)
4361 #define CONF_CONTINUE 1
4362 #define CONF_BREAK 2
4363 #define CONF_BUFF_SIZE 512
4365 static FILE *open_config_file(const char *f)
4367 #ifdef CONFIG_OUTPUT
4368 if (!strcmp(f, "==builtin==")) {
4369 return conf_cookie_open();
4371 #endif /* CONFIG_OUTPUT */
4372 return fopen(f, "r");
4375 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4379 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4382 remove_comments(buf);
4387 while (*p && isspace((int) *p)) {
4391 return CONF_CONTINUE; /* empty line */
4398 while (*p2 && !isspace((int) *p2)) {
4402 *p2 = '\0'; /* break at name's end */
4409 while (*p && isspace((int) *p)) {
4415 p2 = *value + strlen(*value);
4416 while (isspace((int) *(p2 - 1))) {
4425 char load_config_file(const char *f)
4430 fp = open_config_file(f);
4434 DBGP("reading contents from config file '%s'", f);
4437 char buff[CONF_BUFF_SIZE], *name, *value;
4438 int ret = do_config_step(&line, fp, buff, &name, &value);
4439 if (ret == CONF_BREAK) {
4441 } else if (ret == CONF_CONTINUE) {
4447 /* don't listen if X is already initialised or
4448 * if we already know we don't want it */
4449 if(x_initialised != YES) {
4450 if (string_to_bool(value)) {
4451 output_methods &= TO_X;
4453 output_methods &= ~TO_X;
4454 x_initialised = NEVER;
4459 if (!value || x_initialised == YES) {
4464 disp = strdup(value);
4469 if (window.type == TYPE_DOCK)
4472 #endif /*OWN_WINDOW */
4474 int a = string_to_alignment(value);
4485 CONF("background") {
4486 fork_to_background = string_to_bool(value);
4489 CONF2("background") {
4490 fork_to_background = string_to_bool(value);
4494 CONF("show_graph_scale") {
4495 show_graph_scale = string_to_bool(value);
4497 CONF("show_graph_range") {
4498 show_graph_range = string_to_bool(value);
4500 CONF("border_inner_margin") {
4502 window.border_inner_margin = strtol(value, 0, 0);
4503 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4508 CONF("border_outer_margin") {
4510 window.border_outer_margin = strtol(value, 0, 0);
4511 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4516 CONF("border_width") {
4518 window.border_width = strtol(value, 0, 0);
4519 if (window.border_width < 0) window.border_width = 0;
4525 #define TEMPLATE_CONF(n) \
4526 CONF("template"#n) { \
4527 if (set_template(n, value)) \
4542 parse_global_imap_mail_args(value);
4549 parse_global_pop3_mail_args(value);
4554 CONF("default_bar_size") {
4557 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4564 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4568 CONF("default_graph_size") {
4571 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4578 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4581 CONF("default_gauge_size") {
4584 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4591 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4598 mpd_set_host(value);
4604 if (value && mpd_set_port(value)) {
4608 CONF("mpd_password") {
4610 mpd_set_password(value, 0);
4616 CONF("music_player_interval") {
4618 info.music_player_interval = strtod(value, 0);
4624 CONF("sensor_device") {
4626 sensor_device = strtol(value, 0, 0);
4632 CONF("cpu_avg_samples") {
4634 cpu_avg_samples = strtol(value, 0, 0);
4635 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4638 info.cpu_avg_samples = cpu_avg_samples;
4644 CONF("net_avg_samples") {
4646 net_avg_samples = strtol(value, 0, 0);
4647 if (net_avg_samples < 1 || net_avg_samples > 14) {
4650 info.net_avg_samples = net_avg_samples;
4656 CONF("diskio_avg_samples") {
4658 diskio_avg_samples = strtol(value, 0, 0);
4659 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4662 info.diskio_avg_samples = diskio_avg_samples;
4670 CONF("double_buffer") {
4671 use_xdbe = string_to_bool(value);
4675 CONF("override_utf8_locale") {
4676 utf8_mode = string_to_bool(value);
4678 CONF("draw_borders") {
4679 draw_borders = string_to_bool(value);
4681 CONF("draw_graph_borders") {
4682 draw_graph_borders = string_to_bool(value);
4684 CONF("draw_shades") {
4685 draw_shades = string_to_bool(value);
4687 CONF("draw_outline") {
4688 draw_outline = string_to_bool(value);
4691 CONF("out_to_console") {
4692 if(string_to_bool(value)) {
4693 output_methods |= TO_STDOUT;
4695 output_methods &= ~TO_STDOUT;
4698 CONF("extra_newline") {
4699 extra_newline = string_to_bool(value);
4701 CONF("out_to_stderr") {
4702 if(string_to_bool(value))
4703 output_methods |= TO_STDERR;
4706 CONF("out_to_ncurses") {
4707 if(string_to_bool(value)) {
4710 output_methods |= TO_NCURSES;
4714 CONF("overwrite_file") {
4715 if(overwrite_file) {
4716 free(overwrite_file);
4719 if(overwrite_works(value)) {
4720 overwrite_file = strdup(value);
4721 output_methods |= OVERWRITE_FILE;
4723 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4725 CONF("append_file") {
4730 if(append_works(value)) {
4731 append_file = strdup(value);
4732 output_methods |= APPEND_FILE;
4734 NORM_ERR("append_file won't be able to create/append '%s'", value);
4736 CONF("use_spacer") {
4738 if (strcasecmp(value, "left") == EQUAL) {
4739 use_spacer = LEFT_SPACER;
4740 } else if (strcasecmp(value, "right") == EQUAL) {
4741 use_spacer = RIGHT_SPACER;
4742 } else if (strcasecmp(value, "none") == EQUAL) {
4743 use_spacer = NO_SPACER;
4745 use_spacer = string_to_bool(value);
4746 NORM_ERR("use_spacer should have an argument of left, right, or"
4747 " none. '%s' seems to be some form of '%s', so"
4748 " defaulting to %s.", value,
4749 use_spacer ? "true" : "false",
4750 use_spacer ? "right" : "none");
4752 use_spacer = RIGHT_SPACER;
4754 use_spacer = NO_SPACER;
4758 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4759 use_spacer = RIGHT_SPACER;
4765 use_xft = string_to_bool(value);
4769 set_first_font(value);
4773 if (value && font_count >= 0) {
4774 fonts[0].font_alpha = atof(value) * 65535.0;
4781 if (string_to_bool(value)) {
4782 NORM_ERR("Xft not enabled at compile time");
4786 /* xftfont silently ignored when no Xft */
4789 /* xftalpha is silently ignored when no Xft */
4794 set_first_font(value);
4802 gap_x = atoi(value);
4809 gap_y = atoi(value);
4815 CONF("mail_spool") {
4819 variable_substitute(value, buffer, 256);
4821 if (buffer[0] != '\0') {
4822 if (current_mail_spool) {
4823 free(current_mail_spool);
4825 current_mail_spool = strndup(buffer, text_buffer_size);
4832 CONF("minimum_size") {
4834 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4836 if (sscanf(value, "%d", &minimum_width) != 1) {
4844 CONF("maximum_width") {
4846 if (sscanf(value, "%d", &maximum_width) != 1) {
4854 CONF("no_buffers") {
4855 no_buffers = string_to_bool(value);
4857 CONF("top_name_width") {
4859 if (sscanf(value, "%u", &top_name_width) != 1) {
4865 if (top_name_width >= max_user_text) {
4866 top_name_width = max_user_text - 1;
4869 CONF("top_cpu_separate") {
4870 cpu_separate = string_to_bool(value);
4872 CONF("short_units") {
4873 short_units = string_to_bool(value);
4875 CONF("format_human_readable") {
4876 format_human_readable = string_to_bool(value);
4879 CONF("hddtemp_host") {
4880 set_hddtemp_host(value);
4882 CONF("hddtemp_port") {
4883 set_hddtemp_port(value);
4885 #endif /* HDDTEMP */
4886 CONF("pad_percents") {
4887 pad_percents = atoi(value);
4891 CONF("own_window") {
4893 own_window = string_to_bool(value);
4896 CONF("own_window_class") {
4898 memset(window.class_name, 0, sizeof(window.class_name));
4899 strncpy(window.class_name, value,
4900 sizeof(window.class_name) - 1);
4903 CONF("own_window_title") {
4905 memset(window.title, 0, sizeof(window.title));
4906 strncpy(window.title, value, sizeof(window.title) - 1);
4909 CONF("own_window_transparent") {
4911 set_transparent = string_to_bool(value);
4914 CONF("own_window_hints") {
4916 char *p_hint, *p_save;
4917 char delim[] = ", ";
4919 /* tokenize the value into individual hints */
4920 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4922 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4923 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4924 SET_HINT(window.hints, HINT_UNDECORATED);
4925 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4926 SET_HINT(window.hints, HINT_BELOW);
4927 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4928 SET_HINT(window.hints, HINT_ABOVE);
4929 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4930 SET_HINT(window.hints, HINT_STICKY);
4931 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4932 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4933 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4934 SET_HINT(window.hints, HINT_SKIP_PAGER);
4939 p_hint = strtok_r(NULL, delim, &p_save);
4940 } while (p_hint != NULL);
4946 CONF("own_window_type") {
4948 if (strncmp(value, "normal", 6) == EQUAL) {
4949 window.type = TYPE_NORMAL;
4950 } else if (strncmp(value, "desktop", 7) == EQUAL) {
4951 window.type = TYPE_DESKTOP;
4952 } else if (strncmp(value, "dock", 4) == EQUAL) {
4953 window.type = TYPE_DOCK;
4954 text_alignment = TOP_LEFT;
4955 } else if (strncmp(value, "panel", 5) == EQUAL) {
4956 window.type = TYPE_PANEL;
4957 } else if (strncmp(value, "override", 8) == EQUAL) {
4958 window.type = TYPE_OVERRIDE;
4967 CONF("stippled_borders") {
4969 stippled_borders = strtol(value, 0, 0);
4971 stippled_borders = 4;
4975 CONF("imlib_cache_size") {
4977 cimlib_set_cache_size(atoi(value));
4980 CONF("imlib_cache_flush_interval") {
4982 cimlib_set_cache_flush_interval(atoi(value));
4987 CONF("update_interval_on_battery") {
4989 update_interval_bat = strtod(value, 0);
4994 CONF("update_interval") {
4996 set_update_interval(strtod(value, 0));
5000 if (info.music_player_interval == 0) {
5001 // default to update_interval
5002 info.music_player_interval = update_interval;
5005 CONF("total_run_times") {
5007 total_run_times = strtod(value, 0);
5013 stuff_in_uppercase = string_to_bool(value);
5015 CONF("max_specials") {
5017 max_specials = atoi(value);
5022 CONF("max_user_text") {
5024 max_user_text = atoi(value);
5029 CONF("text_buffer_size") {
5031 text_buffer_size = atoi(value);
5032 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5033 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5034 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5042 if (output_methods & TO_X) {
5043 X11_initialisation();
5052 global_text = (char *) malloc(1);
5053 global_text[0] = '\0';
5056 unsigned int l = strlen(global_text);
5058 char buf[CONF_BUFF_SIZE];
5060 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5066 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5074 /* Check for continuation of \\-\n. */
5075 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5076 global_text[l-1] = '\0';
5080 global_text = (char *) realloc(global_text, l + bl + 1);
5081 strcat(global_text, buf);
5083 if (strlen(global_text) > max_user_text) {
5087 global_text_lines = line + 1;
5090 #ifdef TCP_PORT_MONITOR
5091 CONF("max_port_monitor_connections") {
5093 if (!value || (sscanf(value, "%d", &max) != 1)) {
5094 /* an error. use default, warn and continue. */
5095 tcp_portmon_set_max_connections(0);
5097 } else if (tcp_portmon_set_max_connections(max)) {
5098 /* max is < 0, default has been set*/
5103 CONF("if_up_strictness") {
5105 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5106 ifup_strictness = IFUP_UP;
5107 } else if (strcasecmp(value, "up") == EQUAL) {
5108 ifup_strictness = IFUP_UP;
5109 } else if (strcasecmp(value, "link") == EQUAL) {
5110 ifup_strictness = IFUP_LINK;
5111 } else if (strcasecmp(value, "address") == EQUAL) {
5112 ifup_strictness = IFUP_ADDR;
5114 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5115 ifup_strictness = IFUP_UP;
5119 CONF("temperature_unit") {
5121 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5122 } else if (set_temp_output_unit(value)) {
5123 NORM_ERR("temperature_unit: incorrect argument");
5130 char *ptr = strtok(value, " ");
5133 ptr = strtok(NULL, " ");
5140 CONF("lua_draw_hook_pre") {
5142 llua_set_draw_pre_hook(value);
5147 CONF("lua_draw_hook_post") {
5149 llua_set_draw_post_hook(value);
5154 CONF("lua_startup_hook") {
5156 llua_set_startup_hook(value);
5161 CONF("lua_shutdown_hook") {
5163 llua_set_shutdown_hook(value);
5169 #endif /* HAVE_LUA */
5181 CONF("default_color"){}
5182 CONF3("default_shade_color", "default_shadecolor"){}
5183 CONF3("default_outline_color", "default_outlinecolor") {}
5184 CONF("own_window_colour") {}
5187 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5193 if (info.music_player_interval == 0) {
5194 // default to update_interval
5195 info.music_player_interval = update_interval;
5197 if (!global_text) { // didn't supply any text
5198 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5200 if (!output_methods) {
5201 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5203 #if defined(NCURSES)
5205 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5206 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5207 output_methods &= ~TO_NCURSES;
5211 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5212 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5213 output_methods &= ~(TO_STDOUT | TO_STDERR);
5215 #endif /* NCURSES */
5220 static void load_config_file_x11(const char *f)
5225 fp = open_config_file(f);
5229 DBGP("reading contents from config file '%s'", f);
5232 char buff[CONF_BUFF_SIZE], *name, *value;
5233 int ret = do_config_step(&line, fp, buff, &name, &value);
5234 if (ret == CONF_BREAK) {
5236 } else if (ret == CONF_CONTINUE) {
5241 X11_initialisation();
5242 if (x_initialised == YES) {
5244 color0 = get_x11_color(value);
5251 X11_initialisation();
5252 if (x_initialised == YES) {
5254 color1 = get_x11_color(value);
5261 X11_initialisation();
5262 if (x_initialised == YES) {
5264 color2 = get_x11_color(value);
5271 X11_initialisation();
5272 if (x_initialised == YES) {
5274 color3 = get_x11_color(value);
5281 X11_initialisation();
5282 if (x_initialised == YES) {
5284 color4 = get_x11_color(value);
5291 X11_initialisation();
5292 if (x_initialised == YES) {
5294 color5 = get_x11_color(value);
5301 X11_initialisation();
5302 if (x_initialised == YES) {
5304 color6 = get_x11_color(value);
5311 X11_initialisation();
5312 if (x_initialised == YES) {
5314 color7 = get_x11_color(value);
5321 X11_initialisation();
5322 if (x_initialised == YES) {
5324 color8 = get_x11_color(value);
5331 X11_initialisation();
5332 if (x_initialised == YES) {
5334 color9 = get_x11_color(value);
5340 CONF("default_color") {
5341 X11_initialisation();
5342 if (x_initialised == YES) {
5344 default_fg_color = get_x11_color(value);
5350 CONF3("default_shade_color", "default_shadecolor") {
5351 X11_initialisation();
5352 if (x_initialised == YES) {
5354 default_bg_color = get_x11_color(value);
5360 CONF3("default_outline_color", "default_outlinecolor") {
5361 X11_initialisation();
5362 if (x_initialised == YES) {
5364 default_out_color = get_x11_color(value);
5371 CONF("own_window_colour") {
5372 X11_initialisation();
5373 if (x_initialised == YES) {
5375 background_colour = get_x11_color(value);
5377 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5378 "'#' for hex colours");
5384 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5385 if(output_methods & TO_X) {
5386 X11_initialisation();
5395 #undef CONF_CONTINUE
5396 #undef CONF_BUFF_SIZE
5404 static void print_help(const char *prog_name) {
5405 printf("Usage: %s [OPTION]...\n"
5406 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5407 "window. Command line options will override configurations defined in config\n"
5409 " -v, --version version\n"
5410 " -q, --quiet quiet mode\n"
5411 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5412 " -c, --config=FILE config file to load\n"
5413 #ifdef CONFIG_OUTPUT
5414 " -C, --print-config print the builtin default config to stdout\n"
5415 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5417 " -d, --daemonize daemonize, fork to background\n"
5418 " -h, --help help\n"
5420 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5421 " -f, --font=FONT font to use\n"
5422 " -X, --display=DISPLAY X11 display to use\n"
5424 " -o, --own-window create own window to draw\n"
5427 " -b, --double-buffer double buffer (prevents flickering)\n"
5429 " -w, --window-id=WIN_ID window id to draw\n"
5430 " -x X x position\n"
5431 " -y Y y position\n"
5433 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5434 " -u, --interval=SECS update interval\n"
5435 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5436 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5441 /* : means that character before that takes an argument */
5442 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5452 #ifdef CONFIG_OUTPUT
5457 static const struct option longopts[] = {
5458 { "help", 0, NULL, 'h' },
5459 { "version", 0, NULL, 'V' },
5460 { "debug", 0, NULL, 'D' },
5461 { "config", 1, NULL, 'c' },
5462 #ifdef CONFIG_OUTPUT
5463 { "print-config", 0, NULL, 'C' },
5465 { "daemonize", 0, NULL, 'd' },
5467 { "alignment", 1, NULL, 'a' },
5468 { "font", 1, NULL, 'f' },
5469 { "display", 1, NULL, 'X' },
5471 { "own-window", 0, NULL, 'o' },
5474 { "double-buffer", 0, NULL, 'b' },
5476 { "window-id", 1, NULL, 'w' },
5478 { "text", 1, NULL, 't' },
5479 { "interval", 0, NULL, 'u' },
5480 { "pause", 0, NULL, 'p' },
5484 void initialisation(int argc, char **argv) {
5485 struct sigaction act, oact;
5487 set_default_configurations();
5488 load_config_file(current_config);
5489 currentconffile = conftree_add(currentconffile, current_config);
5491 /* init specials array */
5492 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5493 NORM_ERR("failed to create specials array");
5497 if (current_mail_spool == NULL) {
5500 variable_substitute(MAIL_FILE, buf, 256);
5502 if (buf[0] != '\0') {
5503 current_mail_spool = strndup(buf, text_buffer_size);
5508 /* handle other command line arguments */
5510 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5511 || defined(__NetBSD__)
5512 optind = optreset = 1;
5517 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5518 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5519 "kvm_open")) == NULL) {
5520 CRIT_ERR(NULL, NULL, "cannot read kvm");
5525 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5534 fork_to_background = 1;
5537 global_debug_level++;
5541 set_first_font(optarg);
5544 text_alignment = string_to_alignment(optarg);
5563 global_text = strndup(optarg, max_user_text);
5564 convert_escapes(global_text);
5568 update_interval = strtod(optarg, 0);
5569 update_interval_old = update_interval;
5570 if (info.music_player_interval == 0) {
5571 // default to update_interval
5572 info.music_player_interval = update_interval;
5577 total_run_times = strtod(optarg, 0);
5581 gap_x = atoi(optarg);
5585 gap_y = atoi(optarg);
5589 startup_pause = atoi(optarg);
5590 sleep(startup_pause);
5600 if (output_methods & TO_X) {
5601 load_config_file_x11(current_config);
5605 /* generate text and get initial size */
5606 extract_variable_text(global_text);
5613 if (fork_to_background) {
5618 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5625 fprintf(stderr, "\n");
5630 /* parent process */
5631 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5638 start_update_threading();
5640 text_buffer = malloc(max_user_text);
5641 memset(text_buffer, 0, max_user_text);
5642 tmpstring1 = malloc(text_buffer_size);
5643 memset(tmpstring1, 0, text_buffer_size);
5644 tmpstring2 = malloc(text_buffer_size);
5645 memset(tmpstring2, 0, text_buffer_size);
5650 X11_create_window();
5653 llua_setup_info(&info, update_interval);
5654 #endif /* HAVE_LUA */
5659 /* Set signal handlers */
5660 act.sa_handler = signal_handler;
5661 sigemptyset(&act.sa_mask);
5664 act.sa_flags |= SA_RESTART;
5667 if ( sigaction(SIGINT, &act, &oact) < 0
5668 || sigaction(SIGALRM, &act, &oact) < 0
5669 || sigaction(SIGUSR1, &act, &oact) < 0
5670 || sigaction(SIGHUP, &act, &oact) < 0
5671 || sigaction(SIGTERM, &act, &oact) < 0) {
5672 NORM_ERR("error setting signal handler: %s", strerror(errno));
5676 llua_startup_hook();
5677 #endif /* HAVE_LUA */
5680 int main(int argc, char **argv)
5689 g_signal_pending = 0;
5690 max_user_text = MAX_USER_TEXT_DEFAULT;
5692 memset(&info, 0, sizeof(info));
5696 #ifdef TCP_PORT_MONITOR
5697 /* set default connection limit */
5698 tcp_portmon_set_max_connections(0);
5701 /* handle command line parameters that don't change configs */
5703 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5704 || ((s = getenv("LANG")) && *s)) {
5705 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5707 NORM_ERR("malloc failed");
5709 for (x = 0; x < strlen(s); x++) {
5710 temp[x] = tolower(s[x]);
5713 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5719 if (!setlocale(LC_CTYPE, "")) {
5720 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5724 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5735 if (current_config) {
5736 free(current_config);
5738 current_config = strndup(optarg, max_user_text);
5741 freopen("/dev/null", "w", stderr);
5744 print_help(argv[0]);
5746 #ifdef CONFIG_OUTPUT
5753 window.window = strtol(optarg, 0, 0);
5758 disp = strdup(optarg);
5767 /* check if specified config file is valid */
5768 if (current_config) {
5770 if (stat(current_config, &sb) ||
5771 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5772 NORM_ERR("invalid configuration file '%s'\n", current_config);
5773 free(current_config);
5778 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5780 if (!current_config) {
5781 /* load default config file */
5782 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5785 /* Try to use personal config file first */
5786 to_real_path(buf, CONFIG_FILE);
5787 if (buf[0] && (fp = fopen(buf, "r"))) {
5788 current_config = strndup(buf, max_user_text);
5792 /* Try to use system config file if personal config not readable */
5793 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5794 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5798 /* No readable config found */
5799 if (!current_config) {
5800 #ifdef CONFIG_OUTPUT
5801 current_config = strdup("==builtin==");
5802 NORM_ERR("no readable personal or system-wide config file found,"
5803 " using builtin default");
5805 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5806 #endif /* ! CONF_OUTPUT */
5811 /* Load xoap keys, if existing */
5815 #ifdef HAVE_SYS_INOTIFY_H
5816 inotify_fd = inotify_init1(IN_NONBLOCK);
5817 #endif /* HAVE_SYS_INOTIFY_H */
5819 initialisation(argc, argv);
5823 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5831 void alarm_handler(void) {
5833 kill(childpid, SIGTERM);
5837 static void signal_handler(int sig)
5839 /* signal handler is light as a feather, as it should be.
5840 * we will poll g_signal_pending with each loop of conky
5841 * and do any signal processing there, NOT here (except
5842 * SIGALRM because this is caused when conky is hanging) */
5843 if(sig == SIGALRM) {
5846 g_signal_pending = sig;