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>
87 #include "iconv_tools.h"
100 #include "temphelper.h"
101 #include "template.h"
102 #include "tailhead.h"
103 #include "timeinfo.h"
106 /* check for OS and include appropriate headers */
107 #if defined(__linux__)
109 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
111 #elif defined(__OpenBSD__)
115 #if defined(__FreeBSD_kernel__)
119 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
120 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
121 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
122 int apm_getinfo(int fd, apm_info_t aip);
123 char *get_apm_adapter(void);
124 char *get_apm_battery_life(void);
125 char *get_apm_battery_time(void);
129 #include "defconfig.h"
130 #include "conf_cookie.h"
134 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
137 #define MAIL_FILE "$MAIL"
138 #define MAX_IF_BLOCK_DEPTH 5
140 //#define SIGNAL_BLOCKING
141 #undef SIGNAL_BLOCKING
143 /* debugging level, used by logging.h */
144 int global_debug_level = 0;
146 /* two strings for internal use */
147 static char *tmpstring1, *tmpstring2;
149 /* variables holding various config settings */
151 int format_human_readable;
158 int top_cpu, top_mem, top_time;
165 static unsigned int top_name_width = 15;
167 static int extra_newline;
168 enum x_initialiser_state x_initialised = NO;
169 static volatile int g_signal_pending;
170 /* Update interval */
171 double update_interval;
172 double update_interval_old;
173 double update_interval_bat;
174 void *global_cpu = NULL;
179 /* prototypes for internally used functions */
180 static void signal_handler(int);
181 static void print_version(void) __attribute__((noreturn));
182 static void reload_config(void);
184 static void print_version(void)
186 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
188 printf("\nCompiled in features:\n\n"
189 "System config file: "SYSTEM_CONFIG_FILE"\n"
190 "Package library path: "PACKAGE_LIBDIR"\n\n"
194 " * Xdamage extension\n"
195 # endif /* HAVE_XDAMAGE */
197 " * XDBE (double buffer extension)\n"
198 # endif /* HAVE_XDBE */
203 "\n Music detection:\n"
206 #endif /* AUDACIOUS */
222 #endif /* HAVE_OPENMP */
229 #ifdef TCP_PORT_MONITOR
231 #endif /* TCP_PORT_MONITOR */
234 #endif /* HAVE_CURL */
239 " * Weather (METAR)\n"
241 " * Weather (XOAP)\n"
246 #endif /* HAVE_IWLIB */
248 " * support for IBM/Lenovo notebooks\n"
258 #endif /* CONFIG_OUTPUT */
263 " * ALSA mixer support\n"
264 #endif /* MIXER_IS_ALSA */
274 #ifdef HAVE_LUA_CAIRO
276 #endif /* HAVE_LUA_CAIRO */
277 #ifdef HAVE_LUA_IMLIB2
280 #endif /* HAVE_LUA */
286 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
291 static void X11_create_window(void);
292 static void X11_initialisation(void);
294 struct _x11_stuff_s {
298 XserverRegion region2, part;
299 int event_base, error_base;
305 static int text_start_x, text_start_y; /* text start position in window */
306 static int text_width, text_height;
321 /* display to connect to */
322 static char *disp = NULL;
326 /* struct that has all info to be shared between
327 * instances of the same text object */
328 struct information info;
330 /* path to config file */
331 char *current_config;
333 /* set to 1 if you want all text to be in uppercase */
334 static unsigned int stuff_in_uppercase;
336 /* Run how many times? */
337 static unsigned long total_run_times;
340 static int fork_to_background;
342 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
344 /* filenames for output */
345 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
346 char *append_file = NULL; FILE *append_fpointer = NULL;
350 static int show_graph_scale;
351 static int show_graph_range;
353 /* Position on the screen */
354 static int text_alignment;
355 static int gap_x, gap_y;
358 static int draw_borders;
359 static int draw_graph_borders;
360 static int stippled_borders;
362 int get_stippled_borders(void)
364 return stippled_borders;
367 static int draw_shades, draw_outline;
369 long default_fg_color, default_bg_color, default_out_color;
371 /* create own window or draw stuff to root? */
372 static int set_transparent = 0;
375 static int own_window = 0;
376 static int background_colour = 0;
378 /* fixed size/pos is set if wm/user changes them */
379 static int fixed_size = 0, fixed_pos = 0;
382 static int minimum_width, minimum_height;
383 static int maximum_width;
388 static int sensor_device;
391 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
394 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
395 unsigned int max_user_text;
397 /* maximum size of individual text buffers, ie $exec buffer size */
398 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
403 /* no buffers in used memory? */
406 /* pad percentages to decimals? */
407 static int pad_percents = 0;
409 static char *global_text = 0;
411 char *get_global_text(void)
416 long global_text_lines;
418 static int total_updates;
419 static int updatereset;
421 void set_updatereset(int i)
426 int get_updatereset(void)
431 int check_contains(char *f, char *s)
434 FILE *where = open_file(f, 0);
439 while (fgets(buf1, 256, where)) {
440 if (strstr(buf1, s)) {
447 NORM_ERR("Could not open the file");
452 #define SECRIT_MULTILINE_CHAR '\x02'
456 static inline int calc_text_width(const char *s)
458 size_t slen = strlen(s);
460 if ((output_methods & TO_X) == 0) {
468 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
469 (const FcChar8 *) s, slen, &gi);
471 XftTextExtents8(display, fonts[selected_font].xftfont,
472 (const FcChar8 *) s, slen, &gi);
478 return XTextWidth(fonts[selected_font].font, s, slen);
483 /* formatted text to render on screen, generated in generate_text(),
484 * drawn in draw_stuff() */
486 static char *text_buffer;
488 /* quite boring functions */
490 static inline void for_each_line(char *b, int f(char *, int))
493 int special_index = 0; /* specials index */
495 for (ps = b, pe = b; *pe; pe++) {
498 special_index = f(ps, special_index);
505 f(ps, special_index);
509 static void convert_escapes(char *buf)
511 char *p = buf, *s = buf;
518 } else if (*s == '\\') {
529 /* Prints anything normally printed with snprintf according to the current value
530 * of use_spacer. Actually slightly more flexible than snprintf, as you can
531 * safely specify the destination buffer as one of your inputs. */
532 int spaced_print(char *buf, int size, const char *format, int width, ...)
541 tempbuf = malloc(size * sizeof(char));
543 // Passes the varargs along to vsnprintf
544 va_start(argp, width);
545 vsnprintf(tempbuf, size, format, argp);
548 switch (use_spacer) {
550 len = snprintf(buf, size, "%s", tempbuf);
553 len = snprintf(buf, size, "%*s", width, tempbuf);
556 len = snprintf(buf, size, "%-*s", width, tempbuf);
563 /* print percentage values
565 * - i.e., unsigned values between 0 and 100
566 * - respect the value of pad_percents */
567 int percent_print(char *buf, int size, unsigned value)
569 return spaced_print(buf, size, "%u", pad_percents, value);
572 /* converts from bytes to human readable format (K, M, G, T)
574 * The algorithm always divides by 1024, as unit-conversion of byte
575 * counts suggests. But for output length determination we need to
576 * compare with 1000 here, as we print in decimal form. */
577 void human_readable(long long num, char *buf, int size)
579 const char **suffix = suffixes;
585 /* Possibly just output as usual, for example for stdout usage */
586 if (!format_human_readable) {
587 spaced_print(buf, size, "%d", 6, round_to_int(num));
598 if (llabs(num) < 1000LL) {
599 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
603 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
611 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
613 * The goal is to always have a significance of 3, by
614 * adjusting the decimal part of the number. Sample output:
618 * so the point of alignment resides between number and unit. The
619 * upside of this is that there is minimal padding necessary, though
620 * there should be a way to make alignment take place at the decimal
621 * dot (then with fixed width decimal part).
623 * Note the repdigits below: when given a precision value, printf()
624 * rounds the float to it, not just cuts off the remaining digits. So
625 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
626 * printed with a precision of 0. Yay. */
628 precision = 0; /* print 100-999 without decimal part */
630 precision = 1; /* print 10-99 with one decimal place */
632 precision = 2; /* print 0-9 with two decimal places */
634 spaced_print(buf, size, format, width, precision, fnum, *suffix);
637 /* global object list root element */
638 static struct text_object global_root_object;
640 static long current_text_color;
642 void set_current_text_color(long colour)
644 current_text_color = colour;
647 long get_current_text_color(void)
649 return current_text_color;
652 //adds newstring to to the tree unless you can already see it when travelling back.
653 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
654 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
655 struct conftree* node;
656 struct conftree* node2;
658 for(node = previous; node != NULL; node = node->back) {
659 if(strcmp(node->string, newstring) == 0) {
663 node = malloc(sizeof(struct conftree));
664 if (previous != NULL) {
665 if(previous->vert_next == NULL) {
666 previous->vert_next = node;
668 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
669 node2->horz_next = node;
672 node->string = strdup(newstring);
673 node->horz_next = NULL;
674 node->vert_next = NULL;
675 node->back = previous;
679 void conftree_empty(struct conftree* tree) {
681 conftree_empty(tree->horz_next);
682 conftree_empty(tree->vert_next);
688 struct conftree *currentconffile;
690 static void extract_variable_text(const char *p)
692 free_text_objects(&global_root_object, 0);
706 extract_variable_text_internal(&global_root_object, p);
709 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
711 extract_variable_text_internal(root, txt);
712 generate_text_internal(p, max_user_text, *root, cur);
715 static inline void format_media_player_time(char *buf, const int size,
718 int days, hours, minutes;
720 days = seconds / (24 * 60 * 60);
721 seconds %= (24 * 60 * 60);
722 hours = seconds / (60 * 60);
723 seconds %= (60 * 60);
724 minutes = seconds / 60;
728 snprintf(buf, size, "%i days %i:%02i:%02i", days,
729 hours, minutes, seconds);
730 } else if (hours > 0) {
731 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
734 snprintf(buf, size, "%i:%02i", minutes, seconds);
738 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
739 * multiline objects like $exec work with $align[rc] and friends
741 void substitute_newlines(char *p, long l)
745 while (p && *p && p < s + l) {
747 /* only substitute if it's not the last newline */
748 *p = SECRIT_MULTILINE_CHAR;
754 void generate_text_internal(char *p, int p_max_size,
755 struct text_object root, struct information *cur)
757 struct text_object *obj;
759 int need_to_load_fonts = 0;
762 /* for the OBJ_top* handler */
763 struct process **needed = 0;
766 char buff_in[p_max_size];
768 #endif /* HAVE_ICONV */
772 while (obj && p_max_size > 0) {
773 needed = 0; /* reset for top stuff */
775 /* IFBLOCK jumping algorithm
777 * This is easier as it looks like:
778 * - each IF checks it's condition
779 * - on FALSE: call DO_JUMP
780 * - on TRUE: don't care
781 * - each ELSE calls DO_JUMP unconditionally
782 * - each ENDIF is silently being ignored
785 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
786 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
787 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
788 * regularly, "obj" is being updated to point to obj->next, so object parsing
789 * continues right after the corresponding ELSE or ENDIF. This means that if we
790 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
791 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
792 * jumped, and there is nothing to do.
796 obj = obj->data.ifblock.next; \
799 #define OBJ(a) break; case OBJ_##a:
803 NORM_ERR("not implemented obj type %d", obj->type);
805 print_read_tcp(obj, p, p_max_size);
809 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
811 #endif /* !__OpenBSD__ */
814 obj->a = get_freq(p, p_max_size, "%.0f", 1,
821 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
824 /* OpenBSD has no such flag (SUSv2) */
825 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
827 #endif /* __OpenBSD */
830 #if defined(__linux__)
833 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
839 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
845 OBJ(wireless_essid) {
846 print_wireless_essid(obj, p, p_max_size);
849 print_wireless_mode(obj, p, p_max_size);
851 OBJ(wireless_bitrate) {
852 print_wireless_bitrate(obj, p, p_max_size);
855 print_wireless_ap(obj, p, p_max_size);
857 OBJ(wireless_link_qual) {
858 print_wireless_link_qual(obj, p, p_max_size);
860 OBJ(wireless_link_qual_max) {
861 print_wireless_link_qual_max(obj, p, p_max_size);
863 OBJ(wireless_link_qual_perc) {
864 print_wireless_link_qual_perc(obj, p, p_max_size);
866 OBJ(wireless_link_bar) {
867 print_wireless_link_bar(obj, p, p_max_size);
869 #endif /* HAVE_IWLIB */
871 #endif /* __linux__ */
875 get_adt746x_cpu(p, p_max_size);
878 get_adt746x_fan(p, p_max_size);
881 get_acpi_fan(p, p_max_size);
884 get_acpi_ac_adapter(p, p_max_size);
887 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
890 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
892 OBJ(battery_percent) {
893 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
897 if(output_methods & TO_X) {
898 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
901 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
904 get_battery_short_status(p, p_max_size, obj->data.s);
906 #endif /* __OpenBSD__ */
909 human_readable(cur->buffers * 1024, p, 255);
912 human_readable(cur->cached * 1024, p, 255);
915 if (obj->data.i > info.cpu_count) {
916 NORM_ERR("obj->data.i %i info.cpu_count %i",
917 obj->data.i, info.cpu_count);
918 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
920 percent_print(p, p_max_size,
921 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
925 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
929 if(output_methods & TO_X) {
930 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
933 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
937 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
940 print_loadgraph(obj, p);
944 new_fg(p, obj->data.l);
979 snprintf(p, p_max_size, "%s", VERSION);
981 OBJ(conky_build_date) {
982 snprintf(p, p_max_size, "%s", BUILD_DATE);
984 OBJ(conky_build_arch) {
985 snprintf(p, p_max_size, "%s", BUILD_ARCH);
987 #if defined(__linux__)
989 snprintf(p, p_max_size, "%s",
990 get_disk_protect_queue(obj->data.s));
993 snprintf(p, p_max_size, "%s", i8k.version);
996 snprintf(p, p_max_size, "%s", i8k.bios);
999 snprintf(p, p_max_size, "%s", i8k.serial);
1004 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1005 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1007 OBJ(i8k_left_fan_status) {
1008 int left_fan_status;
1010 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1011 if (left_fan_status == 0) {
1012 snprintf(p, p_max_size, "off");
1014 if (left_fan_status == 1) {
1015 snprintf(p, p_max_size, "low");
1017 if (left_fan_status == 2) {
1018 snprintf(p, p_max_size, "high");
1021 OBJ(i8k_right_fan_status) {
1022 int right_fan_status;
1024 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1025 if (right_fan_status == 0) {
1026 snprintf(p, p_max_size, "off");
1028 if (right_fan_status == 1) {
1029 snprintf(p, p_max_size, "low");
1031 if (right_fan_status == 2) {
1032 snprintf(p, p_max_size, "high");
1035 OBJ(i8k_left_fan_rpm) {
1036 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1038 OBJ(i8k_right_fan_rpm) {
1039 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1041 OBJ(i8k_ac_status) {
1044 sscanf(i8k.ac_status, "%d", &ac_status);
1045 if (ac_status == -1) {
1046 snprintf(p, p_max_size, "disabled (read i8k docs)");
1048 if (ac_status == 0) {
1049 snprintf(p, p_max_size, "off");
1051 if (ac_status == 1) {
1052 snprintf(p, p_max_size, "on");
1055 OBJ(i8k_buttons_status) {
1056 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1060 get_ibm_acpi_fan(p, p_max_size);
1063 print_ibm_temps(obj, p, p_max_size);
1066 get_ibm_acpi_volume(p, p_max_size);
1068 OBJ(ibm_brightness) {
1069 get_ibm_acpi_brightness(p, p_max_size);
1072 /* information from sony_laptop kernel module
1073 * /sys/devices/platform/sony-laptop */
1074 OBJ(sony_fanspeed) {
1075 get_sony_fanspeed(p, p_max_size);
1078 if (!gateway_exists()) {
1083 print_gateway_iface(p, p_max_size);
1086 print_gateway_ip(p, p_max_size);
1089 snprintf(p, p_max_size, "%d", get_laptop_mode());
1092 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1094 #endif /* __linux__ */
1095 #if (defined(__FreeBSD__) || defined(__linux__))
1097 if (!interface_up(obj)) {
1103 OBJ(obsd_sensors_temp) {
1104 print_obsd_sensors_temp(obj, p, p_max_size);
1106 OBJ(obsd_sensors_fan) {
1107 print_obsd_sensors_fan(obj, p, p_max_size);
1109 OBJ(obsd_sensors_volt) {
1110 print_obsd_sensors_volt(obj, p, p_max_size);
1113 get_obsd_vendor(p, p_max_size);
1116 get_obsd_product(p, p_max_size);
1118 #endif /* __OpenBSD__ */
1121 new_font(p, obj->data.s);
1122 need_to_load_fonts = 1;
1126 print_diskio(obj, 0, p, p_max_size);
1129 print_diskio(obj, 1, p, p_max_size);
1132 print_diskio(obj, -1, p, p_max_size);
1136 print_diskiograph(obj, 0, p);
1138 OBJ(diskiograph_read) {
1139 print_diskiograph(obj, -1, p);
1141 OBJ(diskiograph_write) {
1142 print_diskiograph(obj, 1, p);
1146 print_downspeed(obj, p, p_max_size);
1149 print_downspeedf(obj, p, p_max_size);
1152 OBJ(downspeedgraph) {
1153 print_downspeedgraph(obj, p);
1157 /* Since we see you, you're if has not jumped.
1158 * Do Ninja jump here: without leaving traces.
1159 * This is to prevent us from stale jumped flags.
1161 obj = obj->data.ifblock.next;
1165 /* harmless object, just ignore */
1168 print_addr(obj, p, p_max_size);
1170 #if defined(__linux__)
1172 print_addrs(obj, p, p_max_size);
1174 #endif /* __linux__ */
1175 #if defined(IMLIB2) && defined(X11)
1177 /* doesn't actually draw anything, just queues it omp. the
1178 * image will get drawn after the X event loop */
1179 cimlib_add_image(obj->data.s);
1183 evaluate(obj->data.s, p);
1186 print_exec(obj, p, p_max_size);
1189 print_execp(obj, p, p_max_size);
1193 print_execgauge(obj, p, p_max_size);
1197 print_execbar(obj, p, p_max_size);
1201 print_execgraph(obj, p, p_max_size);
1205 print_execibar(obj, p, p_max_size);
1209 print_execigraph(obj, p, p_max_size);
1212 print_execigauge(obj, p, p_max_size);
1216 print_execi(obj, p, p_max_size);
1219 print_execpi(obj, p);
1222 print_texeci(obj, p, p_max_size);
1225 print_imap_unseen(obj, p, p_max_size);
1227 OBJ(imap_messages) {
1228 print_imap_messages(obj, p, p_max_size);
1231 print_pop3_unseen(obj, p, p_max_size);
1234 print_pop3_used(obj, p, p_max_size);
1237 print_fs_bar(obj, 0, p, p_max_size);
1240 print_fs_free(obj, p, p_max_size);
1243 print_fs_perc(obj, 1, p, p_max_size);
1246 print_fs_size(obj, p, p_max_size);
1249 print_fs_type(obj, p, p_max_size);
1252 print_fs_used(obj, p, p_max_size);
1255 print_fs_bar(obj, 1, p, p_max_size);
1258 print_fs_perc(obj, 0, p, p_max_size);
1261 print_loadavg(obj, p, p_max_size);
1264 new_goto(p, obj->data.i);
1267 new_tab(p, obj->data.pair.a, obj->data.pair.b);
1271 new_hr(p, obj->data.i);
1275 print_nameserver(obj, p, p_max_size);
1279 print_eve(obj, p, p_max_size);
1284 curl_print(obj, p, p_max_size);
1289 rss_print_info(obj, p, p_max_size);
1294 print_weather(obj, p, p_max_size);
1298 OBJ(weather_forecast) {
1299 print_weather_forecast(obj, p, p_max_size);
1304 char *str = llua_getstring(obj->data.s);
1306 snprintf(p, p_max_size, "%s", str);
1311 char *str = llua_getstring(obj->data.s);
1319 if (llua_getnumber(obj->data.s, &per)) {
1321 if(output_methods & TO_X) {
1322 new_bar(obj, p, (per/100.0 * 255));
1325 new_bar_in_shell(obj, p, p_max_size, per);
1331 if (llua_getnumber(obj->data.s, &per)) {
1332 new_graph(obj, p, per);
1337 if (llua_getnumber(obj->data.s, &per)) {
1338 new_gauge(obj, p, (per/100.0 * 255));
1342 #endif /* HAVE_LUA */
1348 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1349 snprintf(p, p_max_size, "N/A");
1351 temp_print(p, p_max_size, (double)val,
1352 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1357 new_offset(p, obj->data.i);
1360 new_voffset(p, obj->data.i);
1364 print_sysfs_sensor(obj, p, p_max_size);
1367 print_sysfs_sensor(obj, p, p_max_size);
1370 print_sysfs_sensor(obj, p, p_max_size);
1372 #endif /* __linux__ */
1374 new_alignr(p, obj->data.i);
1377 new_alignc(p, obj->data.i);
1380 char buf[max_user_text];
1381 struct information *tmp_info =
1382 malloc(sizeof(struct information));
1383 memcpy(tmp_info, cur, sizeof(struct information));
1384 generate_text_internal(buf, max_user_text,
1385 *obj->sub, tmp_info);
1387 if (strlen(buf) != 0) {
1393 char expression[max_user_text];
1395 struct information *tmp_info;
1397 tmp_info = malloc(sizeof(struct information));
1398 memcpy(tmp_info, cur, sizeof(struct information));
1399 generate_text_internal(expression, max_user_text,
1400 *obj->sub, tmp_info);
1401 DBGP("parsed arg into '%s'", expression);
1403 val = compare(expression);
1405 NORM_ERR("compare failed for expression '%s'",
1413 if (obj->data.ifblock.str
1414 && !check_contains(obj->data.ifblock.s,
1415 obj->data.ifblock.str)) {
1417 } else if (obj->data.ifblock.s
1418 && access(obj->data.ifblock.s, F_OK)) {
1423 if ((obj->data.ifblock.s)
1424 && (!check_mount(obj->data.ifblock.s))) {
1430 if (!get_process_by_name(obj->data.ifblock.s)) {
1432 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
1437 #if defined(__linux__)
1439 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1443 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1446 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1451 human_readable(cur->mem * 1024, p, 255);
1454 human_readable(cur->memeasyfree * 1024, p, 255);
1457 human_readable(cur->memfree * 1024, p, 255);
1460 human_readable(cur->memmax * 1024, p, 255);
1464 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1468 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1473 if(output_methods & TO_X) {
1474 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1477 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1481 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1486 print_mixer(obj, 0, p, p_max_size);
1489 print_mixer(obj, -1, p, p_max_size);
1492 print_mixer(obj, 1, p, p_max_size);
1496 print_mixer_bar(obj, 0, p);
1499 print_mixer_bar(obj, -1, p);
1502 print_mixer_bar(obj, 1, p);
1505 OBJ(if_mixer_mute) {
1506 if (!check_mixer_muted(obj)) {
1511 #define NOT_IN_X "Not running in X"
1513 if(x_initialised != YES) {
1514 strncpy(p, NOT_IN_X, p_max_size);
1516 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1519 OBJ(monitor_number) {
1520 if(x_initialised != YES) {
1521 strncpy(p, NOT_IN_X, p_max_size);
1523 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1527 if(x_initialised != YES) {
1528 strncpy(p, NOT_IN_X, p_max_size);
1530 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1533 OBJ(desktop_number) {
1534 if(x_initialised != YES) {
1535 strncpy(p, NOT_IN_X, p_max_size);
1537 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1541 if(x_initialised != YES) {
1542 strncpy(p, NOT_IN_X, p_max_size);
1543 }else if(cur->x11.desktop.name != NULL) {
1544 strncpy(p, cur->x11.desktop.name, p_max_size);
1551 print_mails(obj, p, p_max_size);
1554 print_new_mails(obj, p, p_max_size);
1557 print_seen_mails(obj, p, p_max_size);
1560 print_unseen_mails(obj, p, p_max_size);
1562 OBJ(flagged_mails) {
1563 print_flagged_mails(obj, p, p_max_size);
1565 OBJ(unflagged_mails) {
1566 print_unflagged_mails(obj, p, p_max_size);
1568 OBJ(forwarded_mails) {
1569 print_forwarded_mails(obj, p, p_max_size);
1571 OBJ(unforwarded_mails) {
1572 print_unforwarded_mails(obj, p, p_max_size);
1574 OBJ(replied_mails) {
1575 print_replied_mails(obj, p, p_max_size);
1577 OBJ(unreplied_mails) {
1578 print_unreplied_mails(obj, p, p_max_size);
1581 print_draft_mails(obj, p, p_max_size);
1583 OBJ(trashed_mails) {
1584 print_trashed_mails(obj, p, p_max_size);
1587 print_mboxscan(obj, p, p_max_size);
1590 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1593 new_outline(p, obj->data.l);
1596 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1598 OBJ(running_processes) {
1599 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1602 snprintf(p, p_max_size, "%s", obj->data.s);
1606 new_bg(p, obj->data.l);
1609 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
1613 human_readable(cur->swap * 1024, p, 255);
1616 human_readable(cur->swapfree * 1024, p, 255);
1619 human_readable(cur->swapmax * 1024, p, 255);
1622 if (cur->swapmax == 0) {
1623 strncpy(p, "No swap", p_max_size);
1625 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1630 if(output_methods & TO_X) {
1631 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1634 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1637 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1640 print_time(obj, p, p_max_size);
1643 print_utime(obj, p, p_max_size);
1646 print_tztime(obj, p, p_max_size);
1649 print_totaldown(obj, p, p_max_size);
1652 print_totalup(obj, p, p_max_size);
1655 snprintf(p, p_max_size, "%d", total_updates);
1658 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
1663 print_upspeed(obj, p, p_max_size);
1666 print_upspeedf(obj, p, p_max_size);
1670 print_upspeedgraph(obj, p);
1674 format_seconds_short(p, p_max_size, (int) cur->uptime);
1677 format_seconds(p, p_max_size, (int) cur->uptime);
1680 snprintf(p, p_max_size, "%s", cur->users.names);
1683 snprintf(p, p_max_size, "%s", cur->users.terms);
1686 snprintf(p, p_max_size, "%s", cur->users.times);
1689 snprintf(p, p_max_size, "%d", cur->users.number);
1691 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1692 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1696 msg = get_apm_adapter();
1697 snprintf(p, p_max_size, "%s", msg);
1700 OBJ(apm_battery_life) {
1703 msg = get_apm_battery_life();
1704 snprintf(p, p_max_size, "%s", msg);
1707 OBJ(apm_battery_time) {
1710 msg = get_apm_battery_time();
1711 snprintf(p, p_max_size, "%s", msg);
1714 #endif /* __FreeBSD__ __OpenBSD__ */
1717 #define mpd_printf(fmt, val) \
1718 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1719 #define mpd_sprintf(val) { \
1720 if (!obj->data.i || obj->data.i > p_max_size) \
1721 mpd_printf("%s", val); \
1723 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
1728 mpd_sprintf(artist);
1732 mpd_printf("%s", random);
1734 mpd_printf("%s", repeat);
1742 mpd_printf("%d", volume);
1744 mpd_printf("%d", bitrate);
1746 mpd_printf("%s", status);
1748 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
1751 format_media_player_time(p, p_max_size, mpd_get_info()->length);
1754 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
1758 if(output_methods & TO_X) {
1759 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
1762 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
1765 struct mpd_s *mpd = mpd_get_info();
1766 int len = obj->data.i;
1767 if (len == 0 || len > p_max_size)
1770 memset(p, 0, p_max_size);
1771 if (mpd->artist && *mpd->artist &&
1772 mpd->title && *mpd->title) {
1773 snprintf(p, len, "%s - %s", mpd->artist,
1775 } else if (mpd->title && *mpd->title) {
1776 snprintf(p, len, "%s", mpd->title);
1777 } else if (mpd->artist && *mpd->artist) {
1778 snprintf(p, len, "%s", mpd->artist);
1779 } else if (mpd->file && *mpd->file) {
1780 snprintf(p, len, "%s", mpd->file);
1785 OBJ(if_mpd_playing) {
1786 if (!mpd_get_info()->is_playing) {
1795 #define MOC_PRINT(t, a) \
1796 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1798 MOC_PRINT(state, "??");
1801 MOC_PRINT(file, "no file");
1804 MOC_PRINT(title, "no title");
1807 MOC_PRINT(artist, "no artist");
1810 MOC_PRINT(song, "no song");
1813 MOC_PRINT(album, "no album");
1815 OBJ(moc_totaltime) {
1816 MOC_PRINT(totaltime, "0:00");
1819 MOC_PRINT(timeleft, "0:00");
1822 MOC_PRINT(curtime, "0:00");
1825 MOC_PRINT(bitrate, "0Kbps");
1828 MOC_PRINT(rate, "0KHz");
1834 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
1837 snprintf(p, p_max_size, "%s", cur->xmms2.album);
1840 snprintf(p, p_max_size, "%s", cur->xmms2.title);
1843 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
1845 OBJ(xmms2_comment) {
1846 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
1849 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1852 snprintf(p, p_max_size, "%s", cur->xmms2.status);
1855 snprintf(p, p_max_size, "%s", cur->xmms2.date);
1857 OBJ(xmms2_tracknr) {
1858 if (cur->xmms2.tracknr != -1) {
1859 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
1862 OBJ(xmms2_bitrate) {
1863 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
1866 snprintf(p, p_max_size, "%u", cur->xmms2.id);
1869 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
1871 OBJ(xmms2_elapsed) {
1872 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
1873 (cur->xmms2.elapsed / 1000) % 60);
1875 OBJ(xmms2_duration) {
1876 snprintf(p, p_max_size, "%02d:%02d",
1877 cur->xmms2.duration / 60000,
1878 (cur->xmms2.duration / 1000) % 60);
1880 OBJ(xmms2_percent) {
1881 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
1885 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
1888 OBJ(xmms2_playlist) {
1889 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
1891 OBJ(xmms2_timesplayed) {
1892 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
1895 if (strlen(cur->xmms2.title) < 2
1896 && strlen(cur->xmms2.title) < 2) {
1897 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1899 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
1903 OBJ(if_xmms2_connected) {
1904 if (cur->xmms2.conn_state != 1) {
1910 OBJ(audacious_status) {
1911 snprintf(p, p_max_size, "%s",
1912 cur->audacious.items[AUDACIOUS_STATUS]);
1914 OBJ(audacious_title) {
1915 snprintf(p, cur->audacious.max_title_len > 0
1916 ? cur->audacious.max_title_len : p_max_size, "%s",
1917 cur->audacious.items[AUDACIOUS_TITLE]);
1919 OBJ(audacious_length) {
1920 snprintf(p, p_max_size, "%s",
1921 cur->audacious.items[AUDACIOUS_LENGTH]);
1923 OBJ(audacious_length_seconds) {
1924 snprintf(p, p_max_size, "%s",
1925 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1927 OBJ(audacious_position) {
1928 snprintf(p, p_max_size, "%s",
1929 cur->audacious.items[AUDACIOUS_POSITION]);
1931 OBJ(audacious_position_seconds) {
1932 snprintf(p, p_max_size, "%s",
1933 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
1935 OBJ(audacious_bitrate) {
1936 snprintf(p, p_max_size, "%s",
1937 cur->audacious.items[AUDACIOUS_BITRATE]);
1939 OBJ(audacious_frequency) {
1940 snprintf(p, p_max_size, "%s",
1941 cur->audacious.items[AUDACIOUS_FREQUENCY]);
1943 OBJ(audacious_channels) {
1944 snprintf(p, p_max_size, "%s",
1945 cur->audacious.items[AUDACIOUS_CHANNELS]);
1947 OBJ(audacious_filename) {
1948 snprintf(p, p_max_size, "%s",
1949 cur->audacious.items[AUDACIOUS_FILENAME]);
1951 OBJ(audacious_playlist_length) {
1952 snprintf(p, p_max_size, "%s",
1953 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
1955 OBJ(audacious_playlist_position) {
1956 snprintf(p, p_max_size, "%s",
1957 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
1959 OBJ(audacious_main_volume) {
1960 snprintf(p, p_max_size, "%s",
1961 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
1964 OBJ(audacious_bar) {
1968 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
1969 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1970 new_bar(obj, p, (int) (progress * 255.0f));
1973 #endif /* AUDACIOUS */
1977 snprintf(p, p_max_size, "%s", cur->bmpx.title);
1980 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
1983 snprintf(p, p_max_size, "%s", cur->bmpx.album);
1986 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
1989 snprintf(p, p_max_size, "%i", cur->bmpx.track);
1992 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
1995 /* we have four different types of top (top, top_mem,
1996 * top_time and top_io). To avoid having almost-same code four
1997 * times, we have this special handler. */
2005 /* yes, passing top_name_width instead
2006 * of p_max_size is intended here */
2007 print_top(obj, p, top_name_width);
2009 print_tailhead("tail", obj, p, p_max_size);
2012 print_tailhead("head", obj, p, p_max_size);
2015 FILE *fp = open_file(obj->data.s, &obj->a);
2018 /* FIXME: use something more general (see also tail.c, head.c */
2019 #define BUFSZ 0x1000
2024 while(fgets(buf, BUFSZ, fp) != NULL){
2025 for(j = 0; buf[j] != 0; j++) {
2026 if(buf[j] == '\n') {
2031 sprintf(p, "%d", lines);
2034 sprintf(p, "File Unreadable");
2039 FILE *fp = open_file(obj->data.s, &obj->a);
2044 char inword = FALSE;
2047 while(fgets(buf, BUFSZ, fp) != NULL){
2048 for(j = 0; buf[j] != 0; j++) {
2049 if(!isspace(buf[j])) {
2050 if(inword == FALSE) {
2059 sprintf(p, "%d", words);
2062 sprintf(p, "File Unreadable");
2065 #ifdef TCP_PORT_MONITOR
2067 tcp_portmon_action(obj, p, p_max_size);
2069 #endif /* TCP_PORT_MONITOR */
2073 do_iconv_start(obj);
2078 #endif /* HAVE_ICONV */
2080 OBJ(entropy_avail) {
2081 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2084 percent_print(p, p_max_size,
2085 cur->entropy.entropy_avail *
2086 100 / cur->entropy.poolsize);
2088 OBJ(entropy_poolsize) {
2089 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2092 double entropy_perc;
2094 entropy_perc = (double) cur->entropy.entropy_avail /
2095 (double) cur->entropy.poolsize;
2097 if(output_methods & TO_X) {
2098 new_bar(obj, p, (int) (entropy_perc * 255.0f));
2101 new_bar_in_shell(obj, p, p_max_size, (int) (entropy_perc * 100.0f));
2107 s = smapi_get_val(obj->data.s);
2108 snprintf(p, p_max_size, "%s", s);
2112 OBJ(if_smapi_bat_installed) {
2114 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
2115 if(!smapi_bat_installed(idx)) {
2119 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2121 OBJ(smapi_bat_perc) {
2123 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2124 val = smapi_bat_installed(idx) ?
2125 smapi_get_bat_int(idx, "remaining_percent") : 0;
2126 percent_print(p, p_max_size, val);
2128 NORM_ERR("argument to smapi_bat_perc must be an integer");
2130 OBJ(smapi_bat_temp) {
2132 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2133 val = smapi_bat_installed(idx) ?
2134 smapi_get_bat_int(idx, "temperature") : 0;
2135 /* temperature is in milli degree celsius */
2136 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2138 NORM_ERR("argument to smapi_bat_temp must be an integer");
2140 OBJ(smapi_bat_power) {
2142 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2143 val = smapi_bat_installed(idx) ?
2144 smapi_get_bat_int(idx, "power_now") : 0;
2145 /* power_now is in mW, set to W with one digit precision */
2146 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2148 NORM_ERR("argument to smapi_bat_power must be an integer");
2151 OBJ(smapi_bat_bar) {
2152 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2153 new_bar(obj, p, (int)
2154 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2162 char buf[max_user_text];
2164 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2165 snprintf(p, p_max_size, "%s", buf);
2171 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2172 char buf[max_user_text];
2175 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2176 snprintf(p, p_max_size, "%s", buf);
2177 if(total_updates % 2) {
2178 for(j=0; p[j] != 0; j++) {
2184 char buf[max_user_text];
2186 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2188 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2189 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2190 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2191 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2192 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2193 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2194 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2196 snprintf(p, p_max_size, "%s", buf);
2199 print_scroll(obj, p, p_max_size, cur);
2202 print_combine(obj, p, cur);
2206 print_nvidia_value(obj, display, p, p_max_size);
2211 /* This is just a meta-object to set host:port */
2214 snprintf(p, p_max_size, "%s",
2215 cur->apcupsd.items[APCUPSD_NAME]);
2217 OBJ(apcupsd_model) {
2218 snprintf(p, p_max_size, "%s",
2219 cur->apcupsd.items[APCUPSD_MODEL]);
2221 OBJ(apcupsd_upsmode) {
2222 snprintf(p, p_max_size, "%s",
2223 cur->apcupsd.items[APCUPSD_UPSMODE]);
2225 OBJ(apcupsd_cable) {
2226 snprintf(p, p_max_size, "%s",
2227 cur->apcupsd.items[APCUPSD_CABLE]);
2229 OBJ(apcupsd_status) {
2230 snprintf(p, p_max_size, "%s",
2231 cur->apcupsd.items[APCUPSD_STATUS]);
2233 OBJ(apcupsd_linev) {
2234 snprintf(p, p_max_size, "%s",
2235 cur->apcupsd.items[APCUPSD_LINEV]);
2238 snprintf(p, p_max_size, "%s",
2239 cur->apcupsd.items[APCUPSD_LOAD]);
2241 OBJ(apcupsd_loadbar) {
2244 if(output_methods & TO_X) {
2245 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2246 new_bar(obj, p, (int) progress);
2250 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2251 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2255 OBJ(apcupsd_loadgraph) {
2257 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2258 new_graph(obj, p, (int)progress);
2260 OBJ(apcupsd_loadgauge) {
2262 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2263 new_gauge(obj, p, (int)progress);
2266 OBJ(apcupsd_charge) {
2267 snprintf(p, p_max_size, "%s",
2268 cur->apcupsd.items[APCUPSD_CHARGE]);
2270 OBJ(apcupsd_timeleft) {
2271 snprintf(p, p_max_size, "%s",
2272 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2275 snprintf(p, p_max_size, "%s",
2276 cur->apcupsd.items[APCUPSD_TEMP]);
2278 OBJ(apcupsd_lastxfer) {
2279 snprintf(p, p_max_size, "%s",
2280 cur->apcupsd.items[APCUPSD_LASTXFER]);
2282 #endif /* APCUPSD */
2289 size_t a = strlen(p);
2292 iconv_convert(&a, buff_in, p, p_max_size);
2293 #endif /* HAVE_ICONV */
2294 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2295 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse) {
2296 substitute_newlines(p, a - 2);
2305 /* load any new fonts we may have had */
2306 if (need_to_load_fonts) {
2312 void evaluate(const char *text, char *buffer)
2314 struct information *tmp_info;
2315 struct text_object subroot;
2317 tmp_info = malloc(sizeof(struct information));
2318 memcpy(tmp_info, &info, sizeof(struct information));
2319 parse_conky_vars(&subroot, text, buffer, tmp_info);
2320 DBGP("evaluated '%s' to '%s'", text, buffer);
2322 free_text_objects(&subroot, 1);
2326 double current_update_time, next_update_time, last_update_time;
2328 static void generate_text(void)
2330 struct information *cur = &info;
2337 current_update_time = get_time();
2341 /* add things to the buffer */
2347 generate_text_internal(p, max_user_text, global_root_object, cur);
2349 if (stuff_in_uppercase) {
2352 tmp_p = text_buffer;
2354 *tmp_p = toupper(*tmp_p);
2359 next_update_time += update_interval;
2360 if (next_update_time < get_time()) {
2361 next_update_time = get_time() + update_interval;
2362 } else if (next_update_time > get_time() + update_interval) {
2363 next_update_time = get_time() + update_interval;
2365 last_update_time = current_update_time;
2369 void set_update_interval(double interval)
2371 update_interval = interval;
2372 update_interval_old = interval;
2375 static inline int get_string_width(const char *s)
2377 return *s ? calc_text_width(s) : 0;
2381 static int get_string_width_special(char *s, int special_index)
2391 if ((output_methods & TO_X) == 0)
2394 p = strndup(s, text_buffer_size);
2398 if (*p == SPECIAL_CHAR) {
2399 /* shift everything over by 1 so that the special char
2400 * doesn't mess up the size calculation */
2401 for (i = 0; i < (long)strlen(p); i++) {
2402 *(p + i) = *(p + i + 1);
2404 if (specials[special_index + idx].type == GRAPH
2405 || specials[special_index + idx].type == GAUGE
2406 || specials[special_index + idx].type == BAR) {
2407 width += specials[special_index + idx].width;
2410 } else if (*p == SECRIT_MULTILINE_CHAR) {
2417 if (strlen(final) > 1) {
2418 width += calc_text_width(final);
2424 static int text_size_updater(char *s, int special_index);
2426 int last_font_height;
2427 static void update_text_area(void)
2431 if ((output_methods & TO_X) == 0)
2433 /* update text size if it isn't fixed */
2438 text_width = minimum_width;
2440 last_font_height = font_height();
2441 for_each_line(text_buffer, text_size_updater);
2443 if (text_height < minimum_height) {
2444 text_height = minimum_height;
2446 if (text_width > maximum_width && maximum_width > 0) {
2447 text_width = maximum_width;
2451 /* get text position on workarea */
2452 switch (text_alignment) {
2459 x = workarea[2] - text_width - gap_x;
2464 x = workarea[2] / 2 - text_width / 2 - gap_x;
2471 y = workarea[3] - text_height - gap_y;
2475 x = workarea[2] - text_width - gap_x;
2476 y = workarea[3] - text_height - gap_y;
2480 x = workarea[2] / 2 - text_width / 2 - gap_x;
2481 y = workarea[3] - text_height - gap_y;
2486 y = workarea[3] / 2 - text_height / 2 - gap_y;
2490 x = workarea[2] - text_width - gap_x;
2491 y = workarea[3] / 2 - text_height / 2 - gap_y;
2495 case NONE: // Let the WM manage the window
2506 if (own_window && !fixed_pos) {
2509 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2510 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2511 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2512 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2516 /* If window size doesn't match to workarea's size,
2517 * then window probably includes panels (gnome).
2518 * Blah, doesn't work on KDE. */
2519 if (workarea[2] != window.width || workarea[3] != window.height) {
2528 /* update lua window globals */
2529 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2530 #endif /* HAVE_LUA */
2535 static int cur_x, cur_y; /* current x and y for drawing */
2537 //draw_mode also without X11 because we only need to print to stdout with FG
2538 static int draw_mode; /* FG, BG or OUTLINE */
2540 static long current_color;
2542 static int text_size_updater(char *s, int special_index)
2547 if ((output_methods & TO_X) == 0)
2549 /* get string widths and skip specials */
2552 if (*p == SPECIAL_CHAR) {
2554 w += get_string_width(s);
2557 if (specials[special_index].type == BAR
2558 || specials[special_index].type == GAUGE
2559 || specials[special_index].type == GRAPH) {
2560 w += specials[special_index].width;
2561 if (specials[special_index].height > last_font_height) {
2562 last_font_height = specials[special_index].height;
2563 last_font_height += font_height();
2565 } else if (specials[special_index].type == OFFSET) {
2566 if (specials[special_index].arg > 0) {
2567 w += specials[special_index].arg;
2569 } else if (specials[special_index].type == VOFFSET) {
2570 last_font_height += specials[special_index].arg;
2571 } else if (specials[special_index].type == GOTO) {
2572 if (specials[special_index].arg > cur_x) {
2573 w = (int) specials[special_index].arg;
2575 } else if (specials[special_index].type == TAB) {
2576 int start = specials[special_index].arg;
2577 int step = specials[special_index].width;
2579 if (!step || step < 0) {
2582 w += step - (cur_x - text_start_x - start) % step;
2583 } else if (specials[special_index].type == FONT) {
2584 selected_font = specials[special_index].font_added;
2585 if (font_height() > last_font_height) {
2586 last_font_height = font_height();
2592 } else if (*p == SECRIT_MULTILINE_CHAR) {
2595 lw = get_string_width(s);
2596 *p = SECRIT_MULTILINE_CHAR;
2598 w = lw > w ? lw : w;
2599 text_height += last_font_height;
2603 w += get_string_width(s);
2604 if (w > text_width) {
2607 if (text_width > maximum_width && maximum_width) {
2608 text_width = maximum_width;
2611 text_height += last_font_height;
2612 last_font_height = font_height();
2613 return special_index;
2617 static inline void set_foreground_color(long c)
2620 if (output_methods & TO_X) {
2622 XSetForeground(display, window.gc, c);
2626 if (output_methods & TO_NCURSES) {
2627 attron(COLOR_PAIR(c));
2629 #endif /* NCURSES */
2634 static void draw_string(const char *s)
2636 int i, i2, pos, width_of_s;
2639 char *s_with_newlines;
2645 width_of_s = get_string_width(s);
2646 s_with_newlines = strdup(s);
2647 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2648 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2649 s_with_newlines[i] = '\n';
2652 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2653 printf("%s\n", s_with_newlines);
2654 if (extra_newline) fputc('\n', stdout);
2655 fflush(stdout); /* output immediately, don't buffer */
2657 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2658 fprintf(stderr, "%s\n", s_with_newlines);
2659 fflush(stderr); /* output immediately, don't buffer */
2661 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2662 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2664 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2665 fprintf(append_fpointer, "%s\n", s_with_newlines);
2668 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2669 printw("%s", s_with_newlines);
2672 free(s_with_newlines);
2673 memset(tmpstring1, 0, text_buffer_size);
2674 memset(tmpstring2, 0, text_buffer_size);
2675 strncpy(tmpstring1, s, text_buffer_size - 1);
2680 if (output_methods & TO_X) {
2681 max = ((text_width - width_of_s) / get_string_width(" "));
2684 /* This code looks for tabs in the text and coverts them to spaces.
2685 * The trick is getting the correct number of spaces, and not going
2686 * over the window's size without forcing the window larger. */
2687 for (i = 0; i < (int) text_buffer_size; i++) {
2688 if (tmpstring1[i] == '\t') {
2690 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2691 /* guard against overrun */
2692 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2697 /* guard against overrun */
2698 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2703 if (output_methods & TO_X) {
2704 if (text_width == maximum_width) {
2705 /* this means the text is probably pushing the limit,
2706 * so we'll chop it */
2707 while (cur_x + get_string_width(tmpstring2) - text_start_x
2708 > maximum_width && strlen(tmpstring2) > 0) {
2709 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2716 if (output_methods & TO_X) {
2722 c.pixel = current_color;
2723 XQueryColor(display, DefaultColormap(display, screen), &c);
2726 c2.color.red = c.red;
2727 c2.color.green = c.green;
2728 c2.color.blue = c.blue;
2729 c2.color.alpha = fonts[selected_font].font_alpha;
2731 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2732 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2734 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2735 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2740 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2743 cur_x += width_of_s;
2746 memcpy(tmpstring1, s, text_buffer_size);
2749 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2757 int last_special_needed = -1;
2758 int orig_special_index = special_index;
2761 if (output_methods & TO_X) {
2762 font_h = font_height();
2763 cur_y += font_ascent();
2765 cur_x = text_start_x;
2769 if (*p == SECRIT_MULTILINE_CHAR) {
2770 /* special newline marker for multiline objects */
2775 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2780 /* draw string before special, unless we're dealing multiline
2782 if (last_special_applied > -1) {
2783 special_index = last_special_applied;
2791 switch (specials[special_index].type) {
2793 case HORIZONTAL_LINE:
2795 int h = specials[special_index].height;
2796 int mid = font_ascent() / 2;
2798 w = text_start_x + text_width - cur_x;
2800 XSetLineAttributes(display, window.gc, h, LineSolid,
2801 CapButt, JoinMiter);
2802 XDrawLine(display, window.drawable, window.gc, cur_x,
2803 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2809 int h = specials[special_index].height;
2810 int tmp_s = specials[special_index].arg;
2811 int mid = font_ascent() / 2;
2812 char ss[2] = { tmp_s, tmp_s };
2814 w = text_start_x + text_width - cur_x - 1;
2815 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2816 CapButt, JoinMiter);
2817 XSetDashes(display, window.gc, 0, ss, 2);
2818 XDrawLine(display, window.drawable, window.gc, cur_x,
2819 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2825 int h, bar_usage, by;
2826 if (cur_x - text_start_x > maximum_width
2827 && maximum_width > 0) {
2830 h = specials[special_index].height;
2831 bar_usage = specials[special_index].arg;
2832 by = cur_y - (font_ascent() / 2) - 1;
2837 w = specials[special_index].width;
2839 w = text_start_x + text_width - cur_x - 1;
2845 XSetLineAttributes(display, window.gc, 1, LineSolid,
2846 CapButt, JoinMiter);
2848 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2850 XFillRectangle(display, window.drawable, window.gc, cur_x,
2851 by, w * bar_usage / 255, h);
2859 case GAUGE: /* new GAUGE */
2862 unsigned long last_colour = current_color;
2864 float angle, px, py;
2868 if (cur_x - text_start_x > maximum_width
2869 && maximum_width > 0) {
2873 h = specials[special_index].height;
2874 by = cur_y - (font_ascent() / 2) - 1;
2879 w = specials[special_index].width;
2881 w = text_start_x + text_width - cur_x - 1;
2887 XSetLineAttributes(display, window.gc, 1, LineSolid,
2888 CapButt, JoinMiter);
2890 XDrawArc(display, window.drawable, window.gc,
2891 cur_x, by, w, h * 2, 0, 180*64);
2894 usage = specials[special_index].arg;
2895 angle = (M_PI)*(float)(usage)/255.;
2896 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2897 py = (float)(by+(h))-(float)(h)*sin(angle);
2899 XDrawLine(display, window.drawable, window.gc,
2900 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2908 set_foreground_color(last_colour);
2916 int h, by, i = 0, j = 0;
2918 unsigned long last_colour = current_color;
2919 unsigned long *tmpcolour = 0;
2920 if (cur_x - text_start_x > maximum_width
2921 && maximum_width > 0) {
2924 h = specials[special_index].height;
2925 by = cur_y - (font_ascent() / 2) - 1;
2930 w = specials[special_index].width;
2932 w = text_start_x + text_width - cur_x - 1;
2937 if (draw_graph_borders) {
2938 XSetLineAttributes(display, window.gc, 1, LineSolid,
2939 CapButt, JoinMiter);
2940 XDrawRectangle(display, window.drawable, window.gc,
2943 XSetLineAttributes(display, window.gc, 1, LineSolid,
2944 CapButt, JoinMiter);
2946 if (specials[special_index].last_colour != 0
2947 || specials[special_index].first_colour != 0) {
2948 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
2951 for (i = w - 2; i > -1; i--) {
2952 if (specials[special_index].last_colour != 0
2953 || specials[special_index].first_colour != 0) {
2954 if (specials[special_index].tempgrad) {
2957 (int)((float)(w - 2) - specials[special_index].graph[j] *
2958 (w - 2) / (float)specials[special_index].graph_scale)
2962 (int)((float)(w - 2) - specials[special_index].graph[j] *
2963 (w - 2) / (float)specials[special_index].graph_scale)
2966 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
2968 (int)((float)(w - 2) - specials[special_index].graph[j] *
2969 (w - 2) / (float)specials[special_index].graph_scale)
2973 #endif /* DEBUG_lol */
2974 XSetForeground(display, window.gc, tmpcolour[
2975 (int)((float)(w - 2) - specials[special_index].graph[j] *
2976 (w - 2) / (float)specials[special_index].graph_scale)
2979 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
2982 /* this is mugfugly, but it works */
2983 XDrawLine(display, window.drawable, window.gc,
2984 cur_x + i + 1, by + h, cur_x + i + 1,
2985 round_to_int((double)by + h - specials[special_index].graph[j] *
2986 (h - 1) / specials[special_index].graph_scale));
2987 if ((w - i) / ((float) (w - 2) /
2988 (specials[special_index].graph_width)) > j
2989 && j < MAX_GRAPH_DEPTH - 3) {
2993 if (tmpcolour) free(tmpcolour);
2998 /* if (draw_mode == BG) {
2999 set_foreground_color(default_bg_color);
3000 } else if (draw_mode == OUTLINE) {
3001 set_foreground_color(default_out_color);
3003 set_foreground_color(default_fg_color);
3005 if (show_graph_range) {
3008 unsigned short int seconds = update_interval * w;
3014 unsigned short int timeunits;
3016 timeunits = seconds / 86400; seconds %= 86400;
3017 if (timeunits > 0) {
3018 asprintf(&tmp_day_str, "%dd", timeunits);
3020 tmp_day_str = strdup("");
3022 timeunits = seconds / 3600; seconds %= 3600;
3023 if (timeunits > 0) {
3024 asprintf(&tmp_hour_str, "%dh", timeunits);
3026 tmp_hour_str = strdup("");
3028 timeunits = seconds / 60; seconds %= 60;
3029 if (timeunits > 0) {
3030 asprintf(&tmp_min_str, "%dm", timeunits);
3032 tmp_min_str = strdup("");
3035 asprintf(&tmp_sec_str, "%ds", seconds);
3037 tmp_sec_str = strdup("");
3039 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3040 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3042 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3044 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3045 cur_y += font_h / 2;
3046 draw_string(tmp_str);
3052 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3056 cur_x += font_ascent() / 2;
3057 cur_y += font_h / 2;
3059 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3061 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3062 draw_string(tmp_str);
3068 set_foreground_color(last_colour);
3074 int old = font_ascent();
3076 cur_y -= font_ascent();
3077 selected_font = specials[special_index].font_added;
3079 if (cur_y + font_ascent() < cur_y + old) {
3082 cur_y += font_ascent();
3084 font_h = font_height();
3089 if (draw_mode == FG) {
3090 set_foreground_color(specials[special_index].arg);
3096 if (draw_mode == BG) {
3097 set_foreground_color(specials[special_index].arg);
3102 if (draw_mode == OUTLINE) {
3103 set_foreground_color(specials[special_index].arg);
3108 w += specials[special_index].arg;
3109 last_special_needed = special_index;
3113 cur_y += specials[special_index].arg;
3117 if (specials[special_index].arg >= 0) {
3118 cur_x = (int) specials[special_index].arg;
3120 last_special_needed = special_index;
3125 int start = specials[special_index].arg;
3126 int step = specials[special_index].width;
3128 if (!step || step < 0) {
3131 w = step - (cur_x - text_start_x - start) % step;
3132 last_special_needed = special_index;
3138 /* TODO: add back in "+ window.border_inner_margin" to the end of
3140 int pos_x = text_start_x + text_width -
3141 get_string_width_special(s, special_index);
3143 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3144 "get_string_width(p) %i gap_x %i "
3145 "specials[special_index].arg %i window.border_inner_margin %i "
3146 "window.border_width %i\n", pos_x, text_start_x, text_width,
3147 cur_x, get_string_width_special(s), gap_x,
3148 specials[special_index].arg, window.border_inner_margin,
3149 window.border_width); */
3150 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3151 cur_x = pos_x - specials[special_index].arg;
3153 last_special_needed = special_index;
3159 int pos_x = (text_width) / 2 - get_string_width_special(s,
3160 special_index) / 2 - (cur_x -
3162 /* int pos_x = text_start_x + text_width / 2 -
3163 get_string_width_special(s) / 2; */
3165 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3166 "get_string_width(p) %i gap_x %i "
3167 "specials[special_index].arg %i\n", pos_x, text_start_x,
3168 text_width, cur_x, get_string_width(s), gap_x,
3169 specials[special_index].arg); */
3170 if (pos_x > specials[special_index].arg) {
3171 w = pos_x - specials[special_index].arg;
3173 last_special_needed = special_index;
3183 if (special_index != last_special_applied) {
3186 special_index = orig_special_index;
3187 last_special_applied = -1;
3198 if (output_methods & TO_NCURSES) {
3201 #endif /* NCURSES */
3203 if (output_methods & TO_X)
3204 cur_y += font_descent();
3206 if (recurse && *recurse) {
3207 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3208 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3210 return special_index;
3213 static int draw_line(char *s, int special_index)
3216 if (output_methods & TO_X) {
3217 return draw_each_line_inner(s, special_index, -1);
3221 if (output_methods & TO_NCURSES) {
3222 return draw_each_line_inner(s, special_index, -1);
3224 #endif /* NCURSES */
3226 UNUSED(special_index);
3230 static void draw_text(void)
3234 llua_draw_pre_hook();
3235 #endif /* HAVE_LUA */
3236 if (output_methods & TO_X) {
3237 cur_y = text_start_y;
3240 if (draw_borders && window.border_width > 0) {
3241 if (stippled_borders) {
3242 char ss[2] = { stippled_borders, stippled_borders };
3243 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3244 CapButt, JoinMiter);
3245 XSetDashes(display, window.gc, 0, ss, 2);
3247 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3248 CapButt, JoinMiter);
3251 XDrawRectangle(display, window.drawable, window.gc,
3252 text_start_x - window.border_inner_margin - window.border_width,
3253 text_start_y - window.border_inner_margin - window.border_width,
3254 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3255 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3263 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3264 attron(COLOR_PAIR(COLOR_WHITE));
3265 #endif /* NCURSES */
3266 for_each_line(text_buffer, draw_line);
3267 #if defined(HAVE_LUA) && defined(X11)
3268 llua_draw_post_hook();
3269 #endif /* HAVE_LUA */
3272 static void draw_stuff(void)
3275 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3277 if (overwrite_file) {
3278 overwrite_fpointer = fopen(overwrite_file, "w");
3279 if(!overwrite_fpointer)
3280 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3283 append_fpointer = fopen(append_file, "a");
3284 if(!append_fpointer)
3285 NORM_ERR("Can't append '%s' anymore", append_file);
3288 if (output_methods & TO_X) {
3290 if (draw_shades && !draw_outline) {
3293 set_foreground_color(default_bg_color);
3304 for (i = -1; i < 2; i++) {
3305 for (j = -1; j < 2; j++) {
3306 if (i == 0 && j == 0) {
3311 set_foreground_color(default_out_color);
3312 draw_mode = OUTLINE;
3320 set_foreground_color(default_fg_color);
3325 #if defined(X11) && defined(HAVE_XDBE)
3326 if (output_methods & TO_X) {
3327 xdbe_swap_buffers();
3329 #endif /* X11 && HAVE_XDBE */
3330 if(overwrite_fpointer) {
3331 fclose(overwrite_fpointer);
3332 overwrite_fpointer = 0;
3334 if(append_fpointer) {
3335 fclose(append_fpointer);
3336 append_fpointer = 0;
3341 static void clear_text(int exposures)
3345 /* The swap action is XdbeBackground, which clears */
3349 if (display && window.window) { // make sure these are !null
3350 /* there is some extra space for borders and outlines */
3351 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3352 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3353 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3354 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3359 static int need_to_update;
3361 /* update_text() generates new text and clears old text area */
3362 static void update_text(void)
3369 if (output_methods & TO_X)
3374 llua_update_info(&info, update_interval);
3375 #endif /* HAVE_LUA */
3378 #ifdef HAVE_SYS_INOTIFY_H
3382 static void main_loop(void)
3385 #ifdef SIGNAL_BLOCKING
3386 sigset_t newmask, oldmask;
3389 #ifdef HAVE_SYS_INOTIFY_H
3390 int inotify_config_wd = -1;
3391 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3392 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3393 char inotify_buff[INOTIFY_BUF_LEN];
3394 #endif /* HAVE_SYS_INOTIFY_H */
3397 #ifdef SIGNAL_BLOCKING
3398 sigemptyset(&newmask);
3399 sigaddset(&newmask, SIGINT);
3400 sigaddset(&newmask, SIGTERM);
3401 sigaddset(&newmask, SIGUSR1);
3404 last_update_time = 0.0;
3405 next_update_time = get_time();
3407 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3408 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3409 char buf[max_user_text];
3411 get_battery_short_status(buf, max_user_text, "BAT0");
3413 update_interval = update_interval_bat;
3415 update_interval = update_interval_old;
3420 #ifdef SIGNAL_BLOCKING
3421 /* block signals. we will inspect for pending signals later */
3422 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3423 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3428 if (output_methods & TO_X) {
3431 /* wait for X event or timeout */
3433 if (!XPending(display)) {
3437 t = next_update_time - get_time();
3441 } else if (t > update_interval) {
3442 t = update_interval;
3445 tv.tv_sec = (long) t;
3446 tv.tv_usec = (long) (t * 1000000) % 1000000;
3448 FD_SET(ConnectionNumber(display), &fdsr);
3450 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3452 if (errno != EINTR) {
3453 NORM_ERR("can't select(): %s", strerror(errno));
3463 if (need_to_update) {
3465 int wx = window.x, wy = window.y;
3475 /* resize window if it isn't right size */
3477 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3478 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3479 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3480 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3481 draw_stuff(); /* redraw everything in our newly sized window */
3482 XResizeWindow(display, window.window, window.width,
3483 window.height); /* resize window */
3484 set_transparent_background(window.window);
3487 xdbe_swap_buffers();
3492 /* update lua window globals */
3493 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3494 #endif /* HAVE_LUA */
3497 /* move window if it isn't in right position */
3498 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3499 XMoveWindow(display, window.window, window.x, window.y);
3504 if (changed && window.type == TYPE_PANEL) {
3507 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3510 switch (text_alignment) {
3537 set_struts(sidenum);
3548 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3549 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3550 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3551 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3552 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3557 /* handle X events */
3558 while (XPending(display)) {
3561 XNextEvent(display, &ev);
3568 r.width = ev.xexpose.width;
3569 r.height = ev.xexpose.height;
3570 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3574 case PropertyNotify:
3576 if ( ev.xproperty.state == PropertyNewValue ) {
3577 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3583 case ReparentNotify:
3584 /* set background to ParentRelative for all parents */
3586 set_transparent_background(window.window);
3590 case ConfigureNotify:
3592 /* if window size isn't what expected, set fixed size */
3593 if (ev.xconfigure.width != window.width
3594 || ev.xconfigure.height != window.height) {
3595 if (window.width != 0 && window.height != 0) {
3599 /* clear old stuff before screwing up
3604 XWindowAttributes attrs;
3605 if (XGetWindowAttributes(display,
3606 window.window, &attrs)) {
3607 window.width = attrs.width;
3608 window.height = attrs.height;
3612 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3613 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3614 if (text_width > maximum_width
3615 && maximum_width > 0) {
3616 text_width = maximum_width;
3620 /* if position isn't what expected, set fixed pos
3621 * total_updates avoids setting fixed_pos when window
3622 * is set to weird locations when started */
3623 /* // this is broken
3624 if (total_updates >= 2 && !fixed_pos
3625 && (window.x != ev.xconfigure.x
3626 || window.y != ev.xconfigure.y)
3627 && (ev.xconfigure.x != 0
3628 || ev.xconfigure.y != 0)) {
3636 /* if an ordinary window with decorations */
3637 if ((window.type == TYPE_NORMAL &&
3638 (!TEST_HINT(window.hints,
3639 HINT_UNDECORATED))) ||
3640 window.type == TYPE_DESKTOP) {
3641 /* allow conky to hold input focus. */
3644 /* forward the click to the desktop window */
3645 XUngrabPointer(display, ev.xbutton.time);
3646 ev.xbutton.window = window.desktop;
3647 ev.xbutton.x = ev.xbutton.x_root;
3648 ev.xbutton.y = ev.xbutton.y_root;
3649 XSendEvent(display, ev.xbutton.window, False,
3650 ButtonPressMask, &ev);
3651 XSetInputFocus(display, ev.xbutton.window,
3652 RevertToParent, ev.xbutton.time);
3659 /* if an ordinary window with decorations */
3660 if ((window.type == TYPE_NORMAL)
3661 && (!TEST_HINT(window.hints,
3662 HINT_UNDECORATED))) {
3663 /* allow conky to hold input focus. */
3666 /* forward the release to the desktop window */
3667 ev.xbutton.window = window.desktop;
3668 ev.xbutton.x = ev.xbutton.x_root;
3669 ev.xbutton.y = ev.xbutton.y_root;
3670 XSendEvent(display, ev.xbutton.window, False,
3671 ButtonReleaseMask, &ev);
3680 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3681 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3683 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3684 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3686 #endif /* HAVE_XDAMAGE */
3692 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3693 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3694 #endif /* HAVE_XDAMAGE */
3696 /* XDBE doesn't seem to provide a way to clear the back buffer
3697 * without interfering with the front buffer, other than passing
3698 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3699 * using XDBE, we need to redraw the text even if it wasn't part of
3700 * the exposed area. OTOH, if we're not going to call draw_stuff at
3701 * all, then no swap happens and we can safely do nothing. */
3703 if (!XEmptyRegion(x11_stuff.region)) {
3708 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3709 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3710 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3711 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3712 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3715 XSetRegion(display, window.gc, x11_stuff.region);
3718 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3722 XDestroyRegion(x11_stuff.region);
3723 x11_stuff.region = XCreateRegion();
3727 t = (next_update_time - get_time()) * 1000000;
3728 if(t > 0) usleep((useconds_t)t);
3732 if(output_methods & TO_NCURSES) {
3741 #ifdef SIGNAL_BLOCKING
3742 /* unblock signals of interest and let handler fly */
3743 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3744 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3748 switch (g_signal_pending) {
3751 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3756 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3759 if (output_methods & TO_X) {
3760 XDestroyRegion(x11_stuff.region);
3761 x11_stuff.region = NULL;
3763 XDamageDestroy(display, x11_stuff.damage);
3764 XFixesDestroyRegion(display, x11_stuff.region2);
3765 XFixesDestroyRegion(display, x11_stuff.part);
3766 #endif /* HAVE_XDAMAGE */
3772 if(overwrite_file) {
3773 free(overwrite_file);
3782 /* Reaching here means someone set a signal
3783 * (SIGXXXX, signal_handler), but didn't write any code
3785 * If you don't want to handle a signal, don't set a handler on
3786 * it in the first place. */
3787 if (g_signal_pending) {
3788 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3792 #ifdef HAVE_SYS_INOTIFY_H
3793 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3794 inotify_config_wd = inotify_add_watch(inotify_fd,
3798 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3799 int len = 0, idx = 0;
3801 struct timeval time_to_wait;
3803 FD_ZERO(&descriptors);
3804 FD_SET(inotify_fd, &descriptors);
3806 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3808 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3809 if (FD_ISSET(inotify_fd, &descriptors)) {
3810 /* process inotify events */
3811 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3812 while (len > 0 && idx < len) {
3813 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3814 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3815 /* current_config should be reloaded */
3816 NORM_ERR("'%s' modified, reloading...", current_config);
3818 if (ev->mask & IN_IGNORED) {
3819 /* for some reason we get IN_IGNORED here
3820 * sometimes, so we need to re-add the watch */
3821 inotify_config_wd = inotify_add_watch(inotify_fd,
3828 llua_inotify_query(ev->wd, ev->mask);
3830 #endif /* HAVE_LUA */
3831 idx += INOTIFY_EVENT_SIZE + ev->len;
3835 #endif /* HAVE_SYS_INOTIFY_H */
3838 llua_update_info(&info, update_interval);
3839 #endif /* HAVE_LUA */
3840 g_signal_pending = 0;
3842 clean_up(NULL, NULL);
3844 #ifdef HAVE_SYS_INOTIFY_H
3845 if (inotify_fd != -1) {
3846 inotify_rm_watch(inotify_fd, inotify_config_wd);
3848 inotify_fd = inotify_config_wd = 0;
3850 #endif /* HAVE_SYS_INOTIFY_H */
3854 static void load_config_file_x11(const char *);
3856 void initialisation(int argc, char** argv);
3858 /* reload the config file */
3859 static void reload_config(void)
3861 char *current_config_copy = strdup(current_config);
3862 clean_up(NULL, NULL);
3863 current_config = current_config_copy;
3864 initialisation(argc_copy, argv_copy);
3867 void clean_up(void *memtofree1, void* memtofree2)
3872 if(output_methods & TO_NCURSES) {
3876 conftree_empty(currentconffile);
3877 currentconffile = NULL;
3884 timed_thread_destroy_registered_threads();
3886 if (info.cpu_usage) {
3887 free(info.cpu_usage);
3888 info.cpu_usage = NULL;
3891 if (x_initialised == YES) {
3892 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3893 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3894 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3895 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3898 if(x11_stuff.region) {
3899 XDestroyRegion(x11_stuff.region);
3900 x11_stuff.region = NULL;
3902 XCloseDisplay(display);
3904 if(info.x11.desktop.all_names) {
3905 free(info.x11.desktop.all_names);
3906 info.x11.desktop.all_names = NULL;
3908 if (info.x11.desktop.name) {
3909 free(info.x11.desktop.name);
3910 info.x11.desktop.name = NULL;
3914 free(fonts); //in set_default_configurations a font is set but not loaded
3920 free_update_callbacks();
3924 free_text_objects(&global_root_object, 0);
3943 free(current_config);
3946 #ifdef TCP_PORT_MONITOR
3947 tcp_portmon_clear();
3956 weather_free_info();
3959 llua_shutdown_hook();
3961 #endif /* HAVE_LUA */
3963 if (output_methods & TO_X)
3971 for (i = 0; i < special_count; i++) {
3972 if (specials[i].type == GRAPH) {
3973 free(specials[i].graph);
3981 clear_diskio_stats();
3982 if(global_cpu != NULL) {
3988 static int string_to_bool(const char *s)
3991 // Assumes an option without a true/false means true
3993 } else if (strcasecmp(s, "yes") == EQUAL) {
3995 } else if (strcasecmp(s, "true") == EQUAL) {
3997 } else if (strcasecmp(s, "1") == EQUAL) {
4004 static enum alignment string_to_alignment(const char *s)
4006 if (strcasecmp(s, "top_left") == EQUAL) {
4008 } else if (strcasecmp(s, "top_right") == EQUAL) {
4010 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4012 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4014 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4015 return BOTTOM_RIGHT;
4016 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4017 return BOTTOM_MIDDLE;
4018 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4020 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4021 return MIDDLE_RIGHT;
4022 } else if (strcasecmp(s, "tl") == EQUAL) {
4024 } else if (strcasecmp(s, "tr") == EQUAL) {
4026 } else if (strcasecmp(s, "tm") == EQUAL) {
4028 } else if (strcasecmp(s, "bl") == EQUAL) {
4030 } else if (strcasecmp(s, "br") == EQUAL) {
4031 return BOTTOM_RIGHT;
4032 } else if (strcasecmp(s, "bm") == EQUAL) {
4033 return BOTTOM_MIDDLE;
4034 } else if (strcasecmp(s, "ml") == EQUAL) {
4036 } else if (strcasecmp(s, "mr") == EQUAL) {
4037 return MIDDLE_RIGHT;
4038 } else if (strcasecmp(s, "none") == EQUAL) {
4046 static void set_default_configurations_for_x(void)
4048 default_fg_color = WhitePixel(display, screen);
4049 default_bg_color = BlackPixel(display, screen);
4050 default_out_color = BlackPixel(display, screen);
4051 color0 = default_fg_color;
4052 color1 = default_fg_color;
4053 color2 = default_fg_color;
4054 color3 = default_fg_color;
4055 color4 = default_fg_color;
4056 color5 = default_fg_color;
4057 color6 = default_fg_color;
4058 color7 = default_fg_color;
4059 color8 = default_fg_color;
4060 color9 = default_fg_color;
4061 current_text_color = default_fg_color;
4065 static void set_default_configurations(void)
4072 fork_to_background = 0;
4073 total_run_times = 0;
4074 info.cpu_avg_samples = 2;
4075 info.net_avg_samples = 2;
4076 info.diskio_avg_samples = 2;
4081 format_human_readable = 1;
4091 mpd_env_host = getenv("MPD_HOST");
4092 mpd_env_port = getenv("MPD_PORT");
4094 if (!mpd_env_host || !strlen(mpd_env_host)) {
4095 mpd_set_host("localhost");
4097 /* MPD_HOST environment variable is set */
4098 char *mpd_hostpart = strchr(mpd_env_host, '@');
4099 if (!mpd_hostpart) {
4100 mpd_set_host(mpd_env_host);
4102 /* MPD_HOST contains a password */
4103 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4104 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4106 if (!strlen(mpd_hostpart + 1)) {
4107 mpd_set_host("localhost");
4109 mpd_set_host(mpd_hostpart + 1);
4112 mpd_set_password(mpd_password, 1);
4117 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4118 /* failed to set port from environment variable */
4119 mpd_set_port("6600");
4123 info.xmms2.artist = NULL;
4124 info.xmms2.album = NULL;
4125 info.xmms2.title = NULL;
4126 info.xmms2.genre = NULL;
4127 info.xmms2.comment = NULL;
4128 info.xmms2.url = NULL;
4129 info.xmms2.status = NULL;
4130 info.xmms2.playlist = NULL;
4132 use_spacer = NO_SPACER;
4134 output_methods = TO_X;
4136 output_methods = TO_STDOUT;
4139 show_graph_scale = 0;
4140 show_graph_range = 0;
4143 draw_graph_borders = 1;
4145 set_first_font("6x10");
4153 window.type = TYPE_NORMAL;
4155 strcpy(window.class_name, PACKAGE_NAME);
4156 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4158 stippled_borders = 0;
4159 window.border_inner_margin = 3;
4160 window.border_outer_margin = 1;
4161 window.border_width = 1;
4162 text_alignment = BOTTOM_LEFT;
4163 info.x11.monitor.number = 1;
4164 info.x11.monitor.current = 0;
4165 info.x11.desktop.current = 1;
4166 info.x11.desktop.number = 1;
4167 info.x11.desktop.nitems = 0;
4168 info.x11.desktop.all_names = NULL;
4169 info.x11.desktop.name = NULL;
4174 free(current_mail_spool);
4178 variable_substitute(MAIL_FILE, buf, 256);
4179 if (buf[0] != '\0') {
4180 current_mail_spool = strndup(buf, text_buffer_size);
4185 set_update_interval(3);
4186 update_interval_bat = NOBATTERY;
4187 info.music_player_interval = 1.0;
4188 stuff_in_uppercase = 0;
4189 info.users.number = 1;
4191 #ifdef TCP_PORT_MONITOR
4192 /* set default connection limit */
4193 tcp_portmon_set_max_connections(0);
4197 /* returns 1 if you can overwrite or create the file at 'path' */
4198 static _Bool overwrite_works(const char *path)
4202 if (!(filepointer = fopen(path, "w")))
4204 fclose(filepointer);
4208 /* returns 1 if you can append or create the file at 'path' */
4209 static _Bool append_works(const char *path)
4213 if (!(filepointer = fopen(path, "a")))
4215 fclose(filepointer);
4221 /* WARNING, this type not in Xlib spec */
4222 int x11_error_handler(Display *d, XErrorEvent *err)
4223 __attribute__((noreturn));
4224 int x11_error_handler(Display *d, XErrorEvent *err)
4226 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",
4228 (long unsigned)err->display,
4229 (long)err->resourceid,
4239 int x11_ioerror_handler(Display *d)
4240 __attribute__((noreturn));
4241 int x11_ioerror_handler(Display *d)
4243 NORM_ERR("X Error: Display %lx\n",
4250 static void X11_initialisation(void)
4252 if (x_initialised == YES) return;
4253 output_methods |= TO_X;
4255 set_default_configurations_for_x();
4256 x_initialised = YES;
4259 /* WARNING, this type not in Xlib spec */
4260 XSetErrorHandler(&x11_error_handler);
4261 XSetIOErrorHandler(&x11_ioerror_handler);
4265 static char **xargv = 0;
4266 static int xargc = 0;
4268 static void X11_create_window(void)
4270 if (output_methods & TO_X) {
4272 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4273 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4275 #else /* OWN_WINDOW */
4276 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4277 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4279 #endif /* OWN_WINDOW */
4283 update_text_area(); /* to position text/window on screen */
4286 if (own_window && !fixed_pos) {
4287 XMoveWindow(display, window.window, window.x, window.y);
4290 set_transparent_background(window.window);
4298 x11_stuff.region = XCreateRegion();
4300 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4301 NORM_ERR("Xdamage extension unavailable");
4303 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4304 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4305 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4306 #endif /* HAVE_XDAMAGE */
4309 update_text_area(); /* to get initial size of the window */
4312 /* setup lua window globals */
4313 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4314 #endif /* HAVE_LUA */
4318 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4319 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4320 #define CONF2(a) if (strcasecmp(name, a) == 0)
4321 #define CONF(a) else CONF2(a)
4322 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4323 || strcasecmp(name, b) == 0)
4324 #define CONF_CONTINUE 1
4325 #define CONF_BREAK 2
4326 #define CONF_BUFF_SIZE 512
4328 static FILE *open_config_file(const char *f)
4330 #ifdef CONFIG_OUTPUT
4331 if (!strcmp(f, "==builtin==")) {
4332 return conf_cookie_open();
4334 #endif /* CONFIG_OUTPUT */
4335 return fopen(f, "r");
4338 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4342 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4345 remove_comments(buf);
4350 while (*p && isspace((int) *p)) {
4354 return CONF_CONTINUE; /* empty line */
4361 while (*p2 && !isspace((int) *p2)) {
4365 *p2 = '\0'; /* break at name's end */
4372 while (*p && isspace((int) *p)) {
4378 p2 = *value + strlen(*value);
4379 while (isspace((int) *(p2 - 1))) {
4388 char load_config_file(const char *f)
4393 fp = open_config_file(f);
4397 DBGP("reading contents from config file '%s'", f);
4400 char buff[CONF_BUFF_SIZE], *name, *value;
4401 int ret = do_config_step(&line, fp, buff, &name, &value);
4402 if (ret == CONF_BREAK) {
4404 } else if (ret == CONF_CONTINUE) {
4410 /* don't listen if X is already initialised or
4411 * if we already know we don't want it */
4412 if(x_initialised != YES) {
4413 if (string_to_bool(value)) {
4414 output_methods &= TO_X;
4416 output_methods &= ~TO_X;
4417 x_initialised = NEVER;
4422 if (!value || x_initialised == YES) {
4427 disp = strdup(value);
4432 if (window.type == TYPE_DOCK)
4435 #endif /*OWN_WINDOW */
4437 int a = string_to_alignment(value);
4448 CONF("background") {
4449 fork_to_background = string_to_bool(value);
4452 CONF2("background") {
4453 fork_to_background = string_to_bool(value);
4457 CONF("show_graph_scale") {
4458 show_graph_scale = string_to_bool(value);
4460 CONF("show_graph_range") {
4461 show_graph_range = string_to_bool(value);
4463 CONF("border_inner_margin") {
4465 window.border_inner_margin = strtol(value, 0, 0);
4466 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4471 CONF("border_outer_margin") {
4473 window.border_outer_margin = strtol(value, 0, 0);
4474 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4479 CONF("border_width") {
4481 window.border_width = strtol(value, 0, 0);
4482 if (window.border_width < 0) window.border_width = 0;
4488 #define TEMPLATE_CONF(n) \
4489 CONF("template"#n) { \
4490 if (set_template(n, value)) \
4505 parse_global_imap_mail_args(value);
4512 parse_global_pop3_mail_args(value);
4517 CONF("default_bar_size") {
4520 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4527 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4531 CONF("default_graph_size") {
4534 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4541 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4544 CONF("default_gauge_size") {
4547 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4554 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4561 mpd_set_host(value);
4567 if (value && mpd_set_port(value)) {
4571 CONF("mpd_password") {
4573 mpd_set_password(value, 0);
4579 CONF("music_player_interval") {
4581 info.music_player_interval = strtod(value, 0);
4587 CONF("sensor_device") {
4589 sensor_device = strtol(value, 0, 0);
4595 CONF("cpu_avg_samples") {
4597 cpu_avg_samples = strtol(value, 0, 0);
4598 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4601 info.cpu_avg_samples = cpu_avg_samples;
4607 CONF("net_avg_samples") {
4609 net_avg_samples = strtol(value, 0, 0);
4610 if (net_avg_samples < 1 || net_avg_samples > 14) {
4613 info.net_avg_samples = net_avg_samples;
4619 CONF("diskio_avg_samples") {
4621 diskio_avg_samples = strtol(value, 0, 0);
4622 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4625 info.diskio_avg_samples = diskio_avg_samples;
4633 CONF("double_buffer") {
4634 use_xdbe = string_to_bool(value);
4638 CONF("override_utf8_locale") {
4639 utf8_mode = string_to_bool(value);
4641 CONF("draw_borders") {
4642 draw_borders = string_to_bool(value);
4644 CONF("draw_graph_borders") {
4645 draw_graph_borders = string_to_bool(value);
4647 CONF("draw_shades") {
4648 draw_shades = string_to_bool(value);
4650 CONF("draw_outline") {
4651 draw_outline = string_to_bool(value);
4654 CONF("out_to_console") {
4655 if(string_to_bool(value)) {
4656 output_methods |= TO_STDOUT;
4658 output_methods &= ~TO_STDOUT;
4661 CONF("extra_newline") {
4662 extra_newline = string_to_bool(value);
4664 CONF("out_to_stderr") {
4665 if(string_to_bool(value))
4666 output_methods |= TO_STDERR;
4669 CONF("out_to_ncurses") {
4670 if(string_to_bool(value)) {
4673 output_methods |= TO_NCURSES;
4677 CONF("overwrite_file") {
4678 if(overwrite_file) {
4679 free(overwrite_file);
4682 if(overwrite_works(value)) {
4683 overwrite_file = strdup(value);
4684 output_methods |= OVERWRITE_FILE;
4686 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4688 CONF("append_file") {
4693 if(append_works(value)) {
4694 append_file = strdup(value);
4695 output_methods |= APPEND_FILE;
4697 NORM_ERR("append_file won't be able to create/append '%s'", value);
4699 CONF("use_spacer") {
4701 if (strcasecmp(value, "left") == EQUAL) {
4702 use_spacer = LEFT_SPACER;
4703 } else if (strcasecmp(value, "right") == EQUAL) {
4704 use_spacer = RIGHT_SPACER;
4705 } else if (strcasecmp(value, "none") == EQUAL) {
4706 use_spacer = NO_SPACER;
4708 use_spacer = string_to_bool(value);
4709 NORM_ERR("use_spacer should have an argument of left, right, or"
4710 " none. '%s' seems to be some form of '%s', so"
4711 " defaulting to %s.", value,
4712 use_spacer ? "true" : "false",
4713 use_spacer ? "right" : "none");
4715 use_spacer = RIGHT_SPACER;
4717 use_spacer = NO_SPACER;
4721 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4722 use_spacer = RIGHT_SPACER;
4728 use_xft = string_to_bool(value);
4732 set_first_font(value);
4736 if (value && font_count >= 0) {
4737 fonts[0].font_alpha = atof(value) * 65535.0;
4744 if (string_to_bool(value)) {
4745 NORM_ERR("Xft not enabled at compile time");
4749 /* xftfont silently ignored when no Xft */
4752 /* xftalpha is silently ignored when no Xft */
4757 set_first_font(value);
4765 gap_x = atoi(value);
4772 gap_y = atoi(value);
4778 CONF("mail_spool") {
4782 variable_substitute(value, buffer, 256);
4784 if (buffer[0] != '\0') {
4785 if (current_mail_spool) {
4786 free(current_mail_spool);
4788 current_mail_spool = strndup(buffer, text_buffer_size);
4795 CONF("minimum_size") {
4797 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4799 if (sscanf(value, "%d", &minimum_width) != 1) {
4807 CONF("maximum_width") {
4809 if (sscanf(value, "%d", &maximum_width) != 1) {
4817 CONF("no_buffers") {
4818 no_buffers = string_to_bool(value);
4820 CONF("top_name_width") {
4822 if (sscanf(value, "%u", &top_name_width) != 1) {
4828 if (top_name_width >= max_user_text) {
4829 top_name_width = max_user_text - 1;
4832 CONF("top_cpu_separate") {
4833 cpu_separate = string_to_bool(value);
4835 CONF("short_units") {
4836 short_units = string_to_bool(value);
4838 CONF("format_human_readable") {
4839 format_human_readable = string_to_bool(value);
4842 CONF("hddtemp_host") {
4843 set_hddtemp_host(value);
4845 CONF("hddtemp_port") {
4846 set_hddtemp_port(value);
4848 #endif /* HDDTEMP */
4849 CONF("pad_percents") {
4850 pad_percents = atoi(value);
4854 CONF("own_window") {
4856 own_window = string_to_bool(value);
4859 CONF("own_window_class") {
4861 memset(window.class_name, 0, sizeof(window.class_name));
4862 strncpy(window.class_name, value,
4863 sizeof(window.class_name) - 1);
4866 CONF("own_window_title") {
4868 memset(window.title, 0, sizeof(window.title));
4869 strncpy(window.title, value, sizeof(window.title) - 1);
4872 CONF("own_window_transparent") {
4874 set_transparent = string_to_bool(value);
4877 CONF("own_window_hints") {
4879 char *p_hint, *p_save;
4880 char delim[] = ", ";
4882 /* tokenize the value into individual hints */
4883 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4885 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4886 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4887 SET_HINT(window.hints, HINT_UNDECORATED);
4888 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4889 SET_HINT(window.hints, HINT_BELOW);
4890 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4891 SET_HINT(window.hints, HINT_ABOVE);
4892 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4893 SET_HINT(window.hints, HINT_STICKY);
4894 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4895 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4896 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4897 SET_HINT(window.hints, HINT_SKIP_PAGER);
4902 p_hint = strtok_r(NULL, delim, &p_save);
4903 } while (p_hint != NULL);
4909 CONF("own_window_type") {
4911 if (strncmp(value, "normal", 6) == EQUAL) {
4912 window.type = TYPE_NORMAL;
4913 } else if (strncmp(value, "desktop", 7) == EQUAL) {
4914 window.type = TYPE_DESKTOP;
4915 } else if (strncmp(value, "dock", 4) == EQUAL) {
4916 window.type = TYPE_DOCK;
4917 text_alignment = TOP_LEFT;
4918 } else if (strncmp(value, "panel", 5) == EQUAL) {
4919 window.type = TYPE_PANEL;
4920 } else if (strncmp(value, "override", 8) == EQUAL) {
4921 window.type = TYPE_OVERRIDE;
4930 CONF("stippled_borders") {
4932 stippled_borders = strtol(value, 0, 0);
4934 stippled_borders = 4;
4938 CONF("imlib_cache_size") {
4940 cimlib_set_cache_size(atoi(value));
4943 CONF("imlib_cache_flush_interval") {
4945 cimlib_set_cache_flush_interval(atoi(value));
4950 CONF("update_interval_on_battery") {
4952 update_interval_bat = strtod(value, 0);
4957 CONF("update_interval") {
4959 set_update_interval(strtod(value, 0));
4963 if (info.music_player_interval == 0) {
4964 // default to update_interval
4965 info.music_player_interval = update_interval;
4968 CONF("total_run_times") {
4970 total_run_times = strtod(value, 0);
4976 stuff_in_uppercase = string_to_bool(value);
4978 CONF("max_specials") {
4980 max_specials = atoi(value);
4985 CONF("max_user_text") {
4987 max_user_text = atoi(value);
4992 CONF("text_buffer_size") {
4994 text_buffer_size = atoi(value);
4995 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
4996 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
4997 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5005 if (output_methods & TO_X) {
5006 X11_initialisation();
5015 global_text = (char *) malloc(1);
5016 global_text[0] = '\0';
5019 unsigned int l = strlen(global_text);
5021 char buf[CONF_BUFF_SIZE];
5023 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5029 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5037 /* Check for continuation of \\-\n. */
5038 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5039 global_text[l-1] = '\0';
5043 global_text = (char *) realloc(global_text, l + bl + 1);
5044 strcat(global_text, buf);
5046 if (strlen(global_text) > max_user_text) {
5050 global_text_lines = line + 1;
5053 #ifdef TCP_PORT_MONITOR
5054 CONF("max_port_monitor_connections") {
5056 if (!value || (sscanf(value, "%d", &max) != 1)) {
5057 /* an error. use default, warn and continue. */
5058 tcp_portmon_set_max_connections(0);
5060 } else if (tcp_portmon_set_max_connections(max)) {
5061 /* max is < 0, default has been set*/
5066 CONF("if_up_strictness") {
5068 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5069 ifup_strictness = IFUP_UP;
5070 } else if (strcasecmp(value, "up") == EQUAL) {
5071 ifup_strictness = IFUP_UP;
5072 } else if (strcasecmp(value, "link") == EQUAL) {
5073 ifup_strictness = IFUP_LINK;
5074 } else if (strcasecmp(value, "address") == EQUAL) {
5075 ifup_strictness = IFUP_ADDR;
5077 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5078 ifup_strictness = IFUP_UP;
5082 CONF("temperature_unit") {
5084 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5085 } else if (set_temp_output_unit(value)) {
5086 NORM_ERR("temperature_unit: incorrect argument");
5093 char *ptr = strtok(value, " ");
5096 ptr = strtok(NULL, " ");
5103 CONF("lua_draw_hook_pre") {
5105 llua_set_draw_pre_hook(value);
5110 CONF("lua_draw_hook_post") {
5112 llua_set_draw_post_hook(value);
5117 CONF("lua_startup_hook") {
5119 llua_set_startup_hook(value);
5124 CONF("lua_shutdown_hook") {
5126 llua_set_shutdown_hook(value);
5132 #endif /* HAVE_LUA */
5144 CONF("default_color"){}
5145 CONF3("default_shade_color", "default_shadecolor"){}
5146 CONF3("default_outline_color", "default_outlinecolor") {}
5147 CONF("own_window_colour") {}
5150 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5156 if (info.music_player_interval == 0) {
5157 // default to update_interval
5158 info.music_player_interval = update_interval;
5160 if (!global_text) { // didn't supply any text
5161 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5163 if (!output_methods) {
5164 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5166 #if defined(NCURSES)
5168 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5169 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5170 output_methods &= ~TO_NCURSES;
5174 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5175 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5176 output_methods &= ~(TO_STDOUT | TO_STDERR);
5178 #endif /* NCURSES */
5183 static void load_config_file_x11(const char *f)
5188 fp = open_config_file(f);
5192 DBGP("reading contents from config file '%s'", f);
5195 char buff[CONF_BUFF_SIZE], *name, *value;
5196 int ret = do_config_step(&line, fp, buff, &name, &value);
5197 if (ret == CONF_BREAK) {
5199 } else if (ret == CONF_CONTINUE) {
5204 X11_initialisation();
5205 if (x_initialised == YES) {
5207 color0 = get_x11_color(value);
5214 X11_initialisation();
5215 if (x_initialised == YES) {
5217 color1 = get_x11_color(value);
5224 X11_initialisation();
5225 if (x_initialised == YES) {
5227 color2 = get_x11_color(value);
5234 X11_initialisation();
5235 if (x_initialised == YES) {
5237 color3 = get_x11_color(value);
5244 X11_initialisation();
5245 if (x_initialised == YES) {
5247 color4 = get_x11_color(value);
5254 X11_initialisation();
5255 if (x_initialised == YES) {
5257 color5 = get_x11_color(value);
5264 X11_initialisation();
5265 if (x_initialised == YES) {
5267 color6 = get_x11_color(value);
5274 X11_initialisation();
5275 if (x_initialised == YES) {
5277 color7 = get_x11_color(value);
5284 X11_initialisation();
5285 if (x_initialised == YES) {
5287 color8 = get_x11_color(value);
5294 X11_initialisation();
5295 if (x_initialised == YES) {
5297 color9 = get_x11_color(value);
5303 CONF("default_color") {
5304 X11_initialisation();
5305 if (x_initialised == YES) {
5307 default_fg_color = get_x11_color(value);
5313 CONF3("default_shade_color", "default_shadecolor") {
5314 X11_initialisation();
5315 if (x_initialised == YES) {
5317 default_bg_color = get_x11_color(value);
5323 CONF3("default_outline_color", "default_outlinecolor") {
5324 X11_initialisation();
5325 if (x_initialised == YES) {
5327 default_out_color = get_x11_color(value);
5334 CONF("own_window_colour") {
5335 X11_initialisation();
5336 if (x_initialised == YES) {
5338 background_colour = get_x11_color(value);
5340 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5341 "'#' for hex colours");
5347 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5348 if(output_methods & TO_X) {
5349 X11_initialisation();
5358 #undef CONF_CONTINUE
5359 #undef CONF_BUFF_SIZE
5367 static void print_help(const char *prog_name) {
5368 printf("Usage: %s [OPTION]...\n"
5369 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5370 "window. Command line options will override configurations defined in config\n"
5372 " -v, --version version\n"
5373 " -q, --quiet quiet mode\n"
5374 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5375 " -c, --config=FILE config file to load\n"
5376 #ifdef CONFIG_OUTPUT
5377 " -C, --print-config print the builtin default config to stdout\n"
5378 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5380 " -d, --daemonize daemonize, fork to background\n"
5381 " -h, --help help\n"
5383 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5384 " -f, --font=FONT font to use\n"
5385 " -X, --display=DISPLAY X11 display to use\n"
5387 " -o, --own-window create own window to draw\n"
5390 " -b, --double-buffer double buffer (prevents flickering)\n"
5392 " -w, --window-id=WIN_ID window id to draw\n"
5393 " -x X x position\n"
5394 " -y Y y position\n"
5396 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5397 " -u, --interval=SECS update interval\n"
5398 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5399 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5404 /* : means that character before that takes an argument */
5405 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5415 #ifdef CONFIG_OUTPUT
5420 static const struct option longopts[] = {
5421 { "help", 0, NULL, 'h' },
5422 { "version", 0, NULL, 'V' },
5423 { "debug", 0, NULL, 'D' },
5424 { "config", 1, NULL, 'c' },
5425 #ifdef CONFIG_OUTPUT
5426 { "print-config", 0, NULL, 'C' },
5428 { "daemonize", 0, NULL, 'd' },
5430 { "alignment", 1, NULL, 'a' },
5431 { "font", 1, NULL, 'f' },
5432 { "display", 1, NULL, 'X' },
5434 { "own-window", 0, NULL, 'o' },
5437 { "double-buffer", 0, NULL, 'b' },
5439 { "window-id", 1, NULL, 'w' },
5441 { "text", 1, NULL, 't' },
5442 { "interval", 0, NULL, 'u' },
5443 { "pause", 0, NULL, 'p' },
5447 void initialisation(int argc, char **argv) {
5448 struct sigaction act, oact;
5450 set_default_configurations();
5451 load_config_file(current_config);
5452 currentconffile = conftree_add(currentconffile, current_config);
5454 /* init specials array */
5455 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5456 NORM_ERR("failed to create specials array");
5460 if (current_mail_spool == NULL) {
5463 variable_substitute(MAIL_FILE, buf, 256);
5465 if (buf[0] != '\0') {
5466 current_mail_spool = strndup(buf, text_buffer_size);
5471 /* handle other command line arguments */
5473 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5474 || defined(__NetBSD__)
5475 optind = optreset = 1;
5480 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5481 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5482 "kvm_open")) == NULL) {
5483 CRIT_ERR(NULL, NULL, "cannot read kvm");
5488 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5497 fork_to_background = 1;
5500 global_debug_level++;
5504 set_first_font(optarg);
5507 text_alignment = string_to_alignment(optarg);
5526 global_text = strndup(optarg, max_user_text);
5527 convert_escapes(global_text);
5531 update_interval = strtod(optarg, 0);
5532 update_interval_old = update_interval;
5533 if (info.music_player_interval == 0) {
5534 // default to update_interval
5535 info.music_player_interval = update_interval;
5540 total_run_times = strtod(optarg, 0);
5544 gap_x = atoi(optarg);
5548 gap_y = atoi(optarg);
5552 startup_pause = atoi(optarg);
5553 sleep(startup_pause);
5563 if (output_methods & TO_X) {
5564 load_config_file_x11(current_config);
5568 /* generate text and get initial size */
5569 extract_variable_text(global_text);
5576 if (fork_to_background) {
5581 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5588 fprintf(stderr, "\n");
5593 /* parent process */
5594 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5601 start_update_threading();
5603 text_buffer = malloc(max_user_text);
5604 memset(text_buffer, 0, max_user_text);
5605 tmpstring1 = malloc(text_buffer_size);
5606 memset(tmpstring1, 0, text_buffer_size);
5607 tmpstring2 = malloc(text_buffer_size);
5608 memset(tmpstring2, 0, text_buffer_size);
5613 X11_create_window();
5616 llua_setup_info(&info, update_interval);
5617 #endif /* HAVE_LUA */
5622 /* Set signal handlers */
5623 act.sa_handler = signal_handler;
5624 sigemptyset(&act.sa_mask);
5627 act.sa_flags |= SA_RESTART;
5630 if ( sigaction(SIGINT, &act, &oact) < 0
5631 || sigaction(SIGALRM, &act, &oact) < 0
5632 || sigaction(SIGUSR1, &act, &oact) < 0
5633 || sigaction(SIGHUP, &act, &oact) < 0
5634 || sigaction(SIGTERM, &act, &oact) < 0) {
5635 NORM_ERR("error setting signal handler: %s", strerror(errno));
5639 llua_startup_hook();
5640 #endif /* HAVE_LUA */
5643 int main(int argc, char **argv)
5652 g_signal_pending = 0;
5653 max_user_text = MAX_USER_TEXT_DEFAULT;
5655 memset(&info, 0, sizeof(info));
5659 #ifdef TCP_PORT_MONITOR
5660 /* set default connection limit */
5661 tcp_portmon_set_max_connections(0);
5664 /* handle command line parameters that don't change configs */
5666 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5667 || ((s = getenv("LANG")) && *s)) {
5668 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5670 NORM_ERR("malloc failed");
5672 for (x = 0; x < strlen(s); x++) {
5673 temp[x] = tolower(s[x]);
5676 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5682 if (!setlocale(LC_CTYPE, "")) {
5683 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5687 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5698 if (current_config) {
5699 free(current_config);
5701 current_config = strndup(optarg, max_user_text);
5704 freopen("/dev/null", "w", stderr);
5707 print_help(argv[0]);
5709 #ifdef CONFIG_OUTPUT
5716 window.window = strtol(optarg, 0, 0);
5721 disp = strdup(optarg);
5730 /* check if specified config file is valid */
5731 if (current_config) {
5733 if (stat(current_config, &sb) ||
5734 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5735 NORM_ERR("invalid configuration file '%s'\n", current_config);
5736 free(current_config);
5741 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5743 if (!current_config) {
5744 /* load default config file */
5745 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5748 /* Try to use personal config file first */
5749 to_real_path(buf, CONFIG_FILE);
5750 if (buf[0] && (fp = fopen(buf, "r"))) {
5751 current_config = strndup(buf, max_user_text);
5755 /* Try to use system config file if personal config not readable */
5756 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5757 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5761 /* No readable config found */
5762 if (!current_config) {
5763 #ifdef CONFIG_OUTPUT
5764 current_config = strdup("==builtin==");
5765 NORM_ERR("no readable personal or system-wide config file found,"
5766 " using builtin default");
5768 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5769 #endif /* ! CONF_OUTPUT */
5774 /* Load xoap keys, if existing */
5778 #ifdef HAVE_SYS_INOTIFY_H
5779 inotify_fd = inotify_init1(IN_NONBLOCK);
5780 #endif /* HAVE_SYS_INOTIFY_H */
5782 initialisation(argc, argv);
5786 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5794 void alarm_handler(void) {
5796 kill(childpid, SIGTERM);
5800 static void signal_handler(int sig)
5802 /* signal handler is light as a feather, as it should be.
5803 * we will poll g_signal_pending with each loop of conky
5804 * and do any signal processing there, NOT here (except
5805 * SIGALRM because this is caused when conky is hanging) */
5806 if(sig == SIGALRM) {
5809 g_signal_pending = sig;