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>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* two strings for internal use */
151 static char *tmpstring1, *tmpstring2;
153 /* variables holding various config settings */
155 int format_human_readable;
162 int top_cpu, top_mem, top_time;
170 static int extra_newline;
171 enum x_initialiser_state x_initialised = NO;
172 static volatile int g_signal_pending;
173 /* Update interval */
174 double update_interval;
175 double update_interval_old;
176 double update_interval_bat;
177 void *global_cpu = NULL;
178 unsigned int max_text_width = 0;
183 /* prototypes for internally used functions */
184 static void signal_handler(int);
185 static void print_version(void) __attribute__((noreturn));
186 static void reload_config(void);
188 static void print_version(void)
190 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
192 printf("\nCompiled in features:\n\n"
193 "System config file: "SYSTEM_CONFIG_FILE"\n"
194 "Package library path: "PACKAGE_LIBDIR"\n\n"
198 " * Xdamage extension\n"
199 # endif /* HAVE_XDAMAGE */
201 " * XDBE (double buffer extension)\n"
202 # endif /* HAVE_XDBE */
208 # endif /* USE_ARGB */
210 "\n Music detection:\n"
213 #endif /* AUDACIOUS */
229 #endif /* HAVE_OPENMP */
236 #ifdef TCP_PORT_MONITOR
238 #endif /* TCP_PORT_MONITOR */
241 #endif /* HAVE_CURL */
246 " * Weather (METAR)\n"
248 " * Weather (XOAP)\n"
253 #endif /* HAVE_IWLIB */
255 " * support for IBM/Lenovo notebooks\n"
265 #endif /* CONFIG_OUTPUT */
270 " * ALSA mixer support\n"
271 #endif /* MIXER_IS_ALSA */
284 #ifdef HAVE_LUA_CAIRO
286 #endif /* HAVE_LUA_CAIRO */
287 #ifdef HAVE_LUA_IMLIB2
290 #endif /* HAVE_LUA */
296 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
301 static void X11_create_window(void);
302 static void X11_initialisation(void);
304 struct _x11_stuff_s {
308 XserverRegion region2, part;
309 int event_base, error_base;
315 static int text_start_x, text_start_y; /* text start position in window */
316 static int text_width, text_height;
332 /* display to connect to */
333 static char *disp = NULL;
337 /* struct that has all info to be shared between
338 * instances of the same text object */
339 struct information info;
341 /* path to config file */
342 char *current_config;
344 /* set to 1 if you want all text to be in uppercase */
345 static unsigned int stuff_in_uppercase;
347 /* Run how many times? */
348 static unsigned long total_run_times;
351 static int fork_to_background;
353 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
355 /* filenames for output */
356 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
357 char *append_file = NULL; FILE *append_fpointer = NULL;
361 static int show_graph_scale;
362 static int show_graph_range;
364 /* Position on the screen */
365 static int text_alignment;
366 static int gap_x, gap_y;
369 static int draw_borders;
370 static int draw_graph_borders;
371 static int stippled_borders;
373 int get_stippled_borders(void)
375 return stippled_borders;
378 static int draw_shades, draw_outline;
380 long default_fg_color, default_bg_color, default_out_color;
382 /* create own window or draw stuff to root? */
383 static int set_transparent = 0;
386 static int own_window = 0;
387 static int background_colour = 0;
389 /* fixed size/pos is set if wm/user changes them */
390 static int fixed_size = 0, fixed_pos = 0;
393 static int minimum_width, minimum_height;
394 static int maximum_width;
399 static int sensor_device;
402 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
405 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
406 unsigned int max_user_text;
408 /* maximum size of individual text buffers, ie $exec buffer size */
409 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
414 /* no buffers in used memory? */
417 /* pad percentages to decimals? */
418 static int pad_percents = 0;
420 static char *global_text = 0;
422 char *get_global_text(void)
427 long global_text_lines;
429 static int total_updates;
430 static int updatereset;
432 void set_updatereset(int i)
437 int get_updatereset(void)
442 int check_contains(char *f, char *s)
445 FILE *where = open_file(f, 0);
450 while (fgets(buf1, 256, where)) {
451 if (strstr(buf1, s)) {
458 NORM_ERR("Could not open the file");
463 #define SECRIT_MULTILINE_CHAR '\x02'
465 static inline int calc_text_width(const char *s)
467 size_t slen = strlen(s);
470 if ((output_methods & TO_X) == 0) {
480 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
481 (const FcChar8 *) s, slen, &gi);
483 XftTextExtents8(display, fonts[selected_font].xftfont,
484 (const FcChar8 *) s, slen, &gi);
490 return XTextWidth(fonts[selected_font].font, s, slen);
495 /* formatted text to render on screen, generated in generate_text(),
496 * drawn in draw_stuff() */
498 static char *text_buffer;
500 /* quite boring functions */
502 static inline void for_each_line(char *b, int f(char *, int))
505 int special_index = 0; /* specials index */
507 for (ps = b, pe = b; *pe; pe++) {
510 special_index = f(ps, special_index);
517 f(ps, special_index);
521 static void convert_escapes(char *buf)
523 char *p = buf, *s = buf;
530 } else if (*s == '\\') {
541 /* Prints anything normally printed with snprintf according to the current value
542 * of use_spacer. Actually slightly more flexible than snprintf, as you can
543 * safely specify the destination buffer as one of your inputs. */
544 int spaced_print(char *buf, int size, const char *format, int width, ...)
553 tempbuf = malloc(size * sizeof(char));
555 // Passes the varargs along to vsnprintf
556 va_start(argp, width);
557 vsnprintf(tempbuf, size, format, argp);
560 switch (use_spacer) {
562 len = snprintf(buf, size, "%s", tempbuf);
565 len = snprintf(buf, size, "%*s", width, tempbuf);
568 len = snprintf(buf, size, "%-*s", width, tempbuf);
575 /* print percentage values
577 * - i.e., unsigned values between 0 and 100
578 * - respect the value of pad_percents */
579 int percent_print(char *buf, int size, unsigned value)
581 return spaced_print(buf, size, "%u", pad_percents, value);
584 /* converts from bytes to human readable format (K, M, G, T)
586 * The algorithm always divides by 1024, as unit-conversion of byte
587 * counts suggests. But for output length determination we need to
588 * compare with 1000 here, as we print in decimal form. */
589 void human_readable(long long num, char *buf, int size)
591 const char **suffix = suffixes;
597 /* Possibly just output as usual, for example for stdout usage */
598 if (!format_human_readable) {
599 spaced_print(buf, size, "%d", 6, round_to_int(num));
610 if (llabs(num) < 1000LL) {
611 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
615 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
623 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
625 * The goal is to always have a significance of 3, by
626 * adjusting the decimal part of the number. Sample output:
630 * so the point of alignment resides between number and unit. The
631 * upside of this is that there is minimal padding necessary, though
632 * there should be a way to make alignment take place at the decimal
633 * dot (then with fixed width decimal part).
635 * Note the repdigits below: when given a precision value, printf()
636 * rounds the float to it, not just cuts off the remaining digits. So
637 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
638 * printed with a precision of 0. Yay. */
640 precision = 0; /* print 100-999 without decimal part */
642 precision = 1; /* print 10-99 with one decimal place */
644 precision = 2; /* print 0-9 with two decimal places */
646 spaced_print(buf, size, format, width, precision, fnum, *suffix);
649 /* global object list root element */
650 static struct text_object global_root_object;
652 static long current_text_color;
654 void set_current_text_color(long colour)
656 current_text_color = colour;
659 long get_current_text_color(void)
661 return current_text_color;
664 //adds newstring to to the tree unless you can already see it when travelling back.
665 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
666 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
667 struct conftree* node;
668 struct conftree* node2;
670 for(node = previous; node != NULL; node = node->back) {
671 if(strcmp(node->string, newstring) == 0) {
675 node = malloc(sizeof(struct conftree));
676 if (previous != NULL) {
677 if(previous->vert_next == NULL) {
678 previous->vert_next = node;
680 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
681 node2->horz_next = node;
684 node->string = strdup(newstring);
685 node->horz_next = NULL;
686 node->vert_next = NULL;
687 node->back = previous;
691 void conftree_empty(struct conftree* tree) {
693 conftree_empty(tree->horz_next);
694 conftree_empty(tree->vert_next);
700 struct conftree *currentconffile;
702 static void extract_variable_text(const char *p)
704 free_text_objects(&global_root_object, 0);
718 extract_variable_text_internal(&global_root_object, p);
721 void parse_conky_vars(struct text_object *root, const char *txt,
722 char *p, int p_max_size, struct information *cur)
724 extract_variable_text_internal(root, txt);
725 generate_text_internal(p, p_max_size, *root, cur);
728 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
729 * multiline objects like $exec work with $align[rc] and friends
731 void substitute_newlines(char *p, long l)
735 while (p && *p && p < s + l) {
737 /* only substitute if it's not the last newline */
738 *p = SECRIT_MULTILINE_CHAR;
744 void generate_text_internal(char *p, int p_max_size,
745 struct text_object root, struct information *cur)
747 struct text_object *obj;
749 int need_to_load_fonts = 0;
752 /* for the OBJ_top* handler */
753 struct process **needed = 0;
756 char buff_in[p_max_size];
758 #endif /* HAVE_ICONV */
762 while (obj && p_max_size > 0) {
763 needed = 0; /* reset for top stuff */
765 /* IFBLOCK jumping algorithm
767 * This is easier as it looks like:
768 * - each IF checks it's condition
769 * - on FALSE: call DO_JUMP
770 * - on TRUE: don't care
771 * - each ELSE calls DO_JUMP unconditionally
772 * - each ENDIF is silently being ignored
775 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
776 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
777 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
778 * regularly, "obj" is being updated to point to obj->next, so object parsing
779 * continues right after the corresponding ELSE or ENDIF. This means that if we
780 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
781 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
782 * jumped, and there is nothing to do.
786 if (obj->ifblock_next) \
787 obj = obj->ifblock_next; \
790 #define OBJ(a) break; case OBJ_##a:
794 NORM_ERR("not implemented obj type %d", obj->type);
796 print_read_tcp(obj, p, p_max_size);
800 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
802 #endif /* !__OpenBSD__ */
806 ok = get_freq(p, p_max_size, "%.0f", 1,
814 ok = get_freq(p, p_max_size, "%'.2f", 1000,
817 /* OpenBSD has no such flag (SUSv2) */
818 ok = get_freq(p, p_max_size, "%.2f", 1000,
820 #endif /* __OpenBSD */
823 #if defined(__linux__)
825 print_voltage_mv(obj, p, p_max_size);
828 print_voltage_v(obj, p, p_max_size);
832 OBJ(wireless_essid) {
833 print_wireless_essid(obj, p, p_max_size);
836 print_wireless_mode(obj, p, p_max_size);
838 OBJ(wireless_bitrate) {
839 print_wireless_bitrate(obj, p, p_max_size);
842 print_wireless_ap(obj, p, p_max_size);
844 OBJ(wireless_link_qual) {
845 print_wireless_link_qual(obj, p, p_max_size);
847 OBJ(wireless_link_qual_max) {
848 print_wireless_link_qual_max(obj, p, p_max_size);
850 OBJ(wireless_link_qual_perc) {
851 print_wireless_link_qual_perc(obj, p, p_max_size);
853 OBJ(wireless_link_bar) {
854 print_wireless_link_bar(obj, p, p_max_size);
856 #endif /* HAVE_IWLIB */
858 #endif /* __linux__ */
862 get_acpi_fan(p, p_max_size);
865 get_acpi_ac_adapter(p, p_max_size);
868 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
871 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
873 OBJ(battery_percent) {
874 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
877 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
880 get_battery_short_status(p, p_max_size, obj->data.s);
882 #endif /* __OpenBSD__ */
885 human_readable(cur->buffers * 1024, p, 255);
888 human_readable(cur->cached * 1024, p, 255);
890 OBJ(cmdline_to_pid) {
891 print_cmdline_to_pid(obj, p, p_max_size);
894 if (obj->data.i > info.cpu_count) {
895 NORM_ERR("obj->data.i %i info.cpu_count %i",
896 obj->data.i, info.cpu_count);
897 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
899 percent_print(p, p_max_size,
900 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
903 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
905 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
909 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
912 print_loadgraph(obj, p, p_max_size);
916 new_fg(p, obj->data.l);
950 #if defined(__linux__)
952 snprintf(p, p_max_size, "%s",
953 get_disk_protect_queue(obj->data.s));
956 print_i8k_version(obj, p, p_max_size);
959 print_i8k_bios(obj, p, p_max_size);
962 print_i8k_serial(obj, p, p_max_size);
965 print_i8k_cpu_temp(obj, p, p_max_size);
967 OBJ(i8k_left_fan_status) {
968 print_i8k_left_fan_status(obj, p, p_max_size);
970 OBJ(i8k_right_fan_status) {
971 print_i8k_right_fan_status(obj, p, p_max_size);
973 OBJ(i8k_left_fan_rpm) {
974 print_i8k_left_fan_rpm(obj, p, p_max_size);
976 OBJ(i8k_right_fan_rpm) {
977 print_i8k_right_fan_rpm(obj, p, p_max_size);
980 print_i8k_ac_status(obj, p, p_max_size);
982 OBJ(i8k_buttons_status) {
983 print_i8k_buttons_status(obj, p, p_max_size);
987 get_ibm_acpi_fan(obj, p, p_max_size);
990 print_ibm_temps(obj, p, p_max_size);
993 get_ibm_acpi_volume(obj, p, p_max_size);
995 OBJ(ibm_brightness) {
996 get_ibm_acpi_brightness(obj, p, p_max_size);
999 /* information from sony_laptop kernel module
1000 * /sys/devices/platform/sony-laptop */
1001 OBJ(sony_fanspeed) {
1002 get_sony_fanspeed(p, p_max_size);
1005 if (!gateway_exists()) {
1010 print_gateway_iface(p, p_max_size);
1013 print_gateway_ip(p, p_max_size);
1016 snprintf(p, p_max_size, "%d", get_laptop_mode());
1019 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1021 #endif /* __linux__ */
1022 #if (defined(__FreeBSD__) || defined(__linux__))
1024 if (!interface_up(obj)) {
1030 OBJ(obsd_sensors_temp) {
1031 print_obsd_sensors_temp(obj, p, p_max_size);
1033 OBJ(obsd_sensors_fan) {
1034 print_obsd_sensors_fan(obj, p, p_max_size);
1036 OBJ(obsd_sensors_volt) {
1037 print_obsd_sensors_volt(obj, p, p_max_size);
1040 get_obsd_vendor(p, p_max_size);
1043 get_obsd_product(p, p_max_size);
1045 #endif /* __OpenBSD__ */
1048 new_font(p, obj->data.s);
1049 need_to_load_fonts = 1;
1053 print_diskio(obj, p, p_max_size);
1056 print_diskio_write(obj, p, p_max_size);
1059 print_diskio_read(obj, p, p_max_size);
1063 print_diskiograph(obj, p, p_max_size);
1065 OBJ(diskiograph_read) {
1066 print_diskiograph(obj, p, p_max_size);
1068 OBJ(diskiograph_write) {
1069 print_diskiograph(obj, p, p_max_size);
1073 print_downspeed(obj, p, p_max_size);
1076 print_downspeedf(obj, p, p_max_size);
1079 OBJ(downspeedgraph) {
1080 print_downspeedgraph(obj, p, p_max_size);
1084 /* Since we see you, you're if has not jumped.
1085 * Do Ninja jump here: without leaving traces.
1086 * This is to prevent us from stale jumped flags.
1088 obj = obj->ifblock_next;
1092 /* harmless object, just ignore */
1095 print_addr(obj, p, p_max_size);
1097 #if defined(__linux__)
1099 print_addrs(obj, p, p_max_size);
1101 #endif /* __linux__ */
1102 #if defined(IMLIB2) && defined(X11)
1104 /* doesn't actually draw anything, just queues it omp. the
1105 * image will get drawn after the X event loop */
1106 cimlib_add_image(obj->data.s);
1110 evaluate(obj->data.s, p, p_max_size);
1113 print_exec(obj, p, p_max_size);
1116 print_execp(obj, p, p_max_size);
1119 print_execgauge(obj, p, p_max_size);
1122 print_execbar(obj, p, p_max_size);
1126 print_execgraph(obj, p, p_max_size);
1130 print_execibar(obj, p, p_max_size);
1134 print_execigraph(obj, p, p_max_size);
1138 print_execigauge(obj, p, p_max_size);
1141 print_execi(obj, p, p_max_size);
1144 print_execpi(obj, p, p_max_size);
1147 print_texeci(obj, p, p_max_size);
1150 print_imap_unseen(obj, p, p_max_size);
1152 OBJ(imap_messages) {
1153 print_imap_messages(obj, p, p_max_size);
1156 print_pop3_unseen(obj, p, p_max_size);
1159 print_pop3_used(obj, p, p_max_size);
1162 print_fs_bar(obj, 0, p, p_max_size);
1165 print_fs_free(obj, p, p_max_size);
1168 print_fs_perc(obj, 1, p, p_max_size);
1171 print_fs_size(obj, p, p_max_size);
1174 print_fs_type(obj, p, p_max_size);
1177 print_fs_used(obj, p, p_max_size);
1180 print_fs_bar(obj, 1, p, p_max_size);
1183 print_fs_perc(obj, 0, p, p_max_size);
1186 print_loadavg(obj, p, p_max_size);
1189 new_goto(p, obj->data.i);
1196 new_hr(p, obj->data.i);
1200 print_nameserver(obj, p, p_max_size);
1204 print_eve(obj, p, p_max_size);
1209 curl_print(obj, p, p_max_size);
1214 rss_print_info(obj, p, p_max_size);
1219 print_weather(obj, p, p_max_size);
1223 OBJ(weather_forecast) {
1224 print_weather_forecast(obj, p, p_max_size);
1229 print_lua(obj, p, p_max_size);
1232 print_lua_parse(obj, p, p_max_size);
1235 print_lua_bar(obj, p, p_max_size);
1239 print_lua_graph(obj, p, p_max_size);
1243 print_lua_gauge(obj, p, p_max_size);
1245 #endif /* HAVE_LUA */
1251 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1252 snprintf(p, p_max_size, "N/A");
1254 temp_print(p, p_max_size, (double)val,
1255 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1260 new_offset(p, obj->data.i);
1263 new_voffset(p, obj->data.i);
1267 print_sysfs_sensor(obj, p, p_max_size);
1270 print_sysfs_sensor(obj, p, p_max_size);
1273 print_sysfs_sensor(obj, p, p_max_size);
1275 #endif /* __linux__ */
1277 new_alignr(p, obj->data.i);
1280 new_alignc(p, obj->data.i);
1283 char buf[max_user_text];
1284 struct information *tmp_info =
1285 malloc(sizeof(struct information));
1286 memcpy(tmp_info, cur, sizeof(struct information));
1287 generate_text_internal(buf, max_user_text,
1288 *obj->sub, tmp_info);
1290 if (strlen(buf) != 0) {
1296 char expression[max_user_text];
1298 struct information *tmp_info;
1300 tmp_info = malloc(sizeof(struct information));
1301 memcpy(tmp_info, cur, sizeof(struct information));
1302 generate_text_internal(expression, max_user_text,
1303 *obj->sub, tmp_info);
1304 DBGP("parsed arg into '%s'", expression);
1306 val = compare(expression);
1308 NORM_ERR("compare failed for expression '%s'",
1318 spc = strchr(obj->data.s, ' ');
1319 if (!spc && access(obj->data.s, F_OK)) {
1323 if (check_contains(obj->data.s, spc + 1))
1330 && (!check_mount(obj->data.s))) {
1336 if (!get_process_by_name(obj->data.s)) {
1338 if ((obj->data.s) && system(obj->data.s)) {
1343 #if defined(__linux__)
1345 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1349 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1352 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1357 human_readable(cur->mem * 1024, p, 255);
1360 human_readable(cur->memeasyfree * 1024, p, 255);
1363 human_readable(cur->memfree * 1024, p, 255);
1366 human_readable(cur->memmax * 1024, p, 255);
1370 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1373 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1376 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1380 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1385 print_mixer(obj, p, p_max_size);
1388 print_mixerl(obj, p, p_max_size);
1391 print_mixerr(obj, p, p_max_size);
1394 print_mixer_bar(obj, p, p_max_size);
1397 print_mixerl_bar(obj, p, p_max_size);
1400 print_mixerr_bar(obj, p, p_max_size);
1402 OBJ(if_mixer_mute) {
1403 if (!check_mixer_muted(obj)) {
1408 #define NOT_IN_X "Not running in X"
1410 if(x_initialised != YES) {
1411 strncpy(p, NOT_IN_X, p_max_size);
1413 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1416 OBJ(monitor_number) {
1417 if(x_initialised != YES) {
1418 strncpy(p, NOT_IN_X, p_max_size);
1420 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1424 if(x_initialised != YES) {
1425 strncpy(p, NOT_IN_X, p_max_size);
1427 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1430 OBJ(desktop_number) {
1431 if(x_initialised != YES) {
1432 strncpy(p, NOT_IN_X, p_max_size);
1434 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1438 if(x_initialised != YES) {
1439 strncpy(p, NOT_IN_X, p_max_size);
1440 }else if(cur->x11.desktop.name != NULL) {
1441 strncpy(p, cur->x11.desktop.name, p_max_size);
1447 char buf[max_user_text];
1449 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1451 print_format_time(obj, p, p_max_size);
1455 print_mails(obj, p, p_max_size);
1458 print_new_mails(obj, p, p_max_size);
1461 print_seen_mails(obj, p, p_max_size);
1464 print_unseen_mails(obj, p, p_max_size);
1466 OBJ(flagged_mails) {
1467 print_flagged_mails(obj, p, p_max_size);
1469 OBJ(unflagged_mails) {
1470 print_unflagged_mails(obj, p, p_max_size);
1472 OBJ(forwarded_mails) {
1473 print_forwarded_mails(obj, p, p_max_size);
1475 OBJ(unforwarded_mails) {
1476 print_unforwarded_mails(obj, p, p_max_size);
1478 OBJ(replied_mails) {
1479 print_replied_mails(obj, p, p_max_size);
1481 OBJ(unreplied_mails) {
1482 print_unreplied_mails(obj, p, p_max_size);
1485 print_draft_mails(obj, p, p_max_size);
1487 OBJ(trashed_mails) {
1488 print_trashed_mails(obj, p, p_max_size);
1491 print_mboxscan(obj, p, p_max_size);
1494 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1497 new_outline(p, obj->data.l);
1500 char buf[max_user_text];
1502 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1504 print_pid_chroot(obj, p, p_max_size);
1507 char buf[max_user_text];
1509 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1511 print_pid_cmdline(obj, p, p_max_size);
1514 char buf[max_user_text];
1516 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1518 print_pid_cwd(obj, p, p_max_size);
1521 char buf[max_user_text];
1523 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1525 print_pid_environ(obj, p, p_max_size);
1527 OBJ(pid_environ_list) {
1528 char buf[max_user_text];
1530 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1532 print_pid_environ_list(obj, p, p_max_size);
1535 char buf[max_user_text];
1537 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1539 print_pid_exe(obj, p, p_max_size);
1542 char buf[max_user_text];
1544 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1546 print_pid_nice(obj, p, p_max_size);
1548 OBJ(pid_openfiles) {
1549 char buf[max_user_text];
1551 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1553 print_pid_openfiles(obj, p, p_max_size);
1556 char buf[max_user_text];
1558 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1560 print_pid_parent(obj, p, p_max_size);
1563 char buf[max_user_text];
1565 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1567 print_pid_priority(obj, p, p_max_size);
1570 char buf[max_user_text];
1572 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1574 print_pid_state(obj, p, p_max_size);
1576 OBJ(pid_state_short) {
1577 char buf[max_user_text];
1579 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1581 print_pid_state_short(obj, p, p_max_size);
1584 char buf[max_user_text];
1586 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1588 print_pid_stderr(obj, p, p_max_size);
1591 char buf[max_user_text];
1593 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1595 print_pid_stdin(obj, p, p_max_size);
1598 char buf[max_user_text];
1600 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1602 print_pid_stdout(obj, p, p_max_size);
1605 char buf[max_user_text];
1607 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1609 print_pid_threads(obj, p, p_max_size);
1611 OBJ(pid_thread_list) {
1612 char buf[max_user_text];
1614 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1616 print_pid_thread_list(obj, p, p_max_size);
1618 OBJ(pid_time_kernelmode) {
1619 char buf[max_user_text];
1621 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1623 print_pid_time_kernelmode(obj, p, p_max_size);
1625 OBJ(pid_time_usermode) {
1626 char buf[max_user_text];
1628 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1630 print_pid_time_usermode(obj, p, p_max_size);
1633 char buf[max_user_text];
1635 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1637 print_pid_time(obj, p, p_max_size);
1640 char buf[max_user_text];
1642 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1644 print_pid_uid(obj, p, p_max_size);
1647 char buf[max_user_text];
1649 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1651 print_pid_euid(obj, p, p_max_size);
1654 char buf[max_user_text];
1656 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1658 print_pid_suid(obj, p, p_max_size);
1661 char buf[max_user_text];
1663 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1665 print_pid_fsuid(obj, p, p_max_size);
1668 char buf[max_user_text];
1670 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1672 print_pid_gid(obj, p, p_max_size);
1675 char buf[max_user_text];
1677 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1679 print_pid_egid(obj, p, p_max_size);
1682 char buf[max_user_text];
1684 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1686 print_pid_sgid(obj, p, p_max_size);
1689 char buf[max_user_text];
1691 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1693 print_pid_fsgid(obj, p, p_max_size);
1696 char buf[max_user_text];
1698 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1700 print_pid_read(obj, p, p_max_size);
1703 char buf[max_user_text];
1705 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1707 print_pid_vmpeak(obj, p, p_max_size);
1710 char buf[max_user_text];
1712 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1714 print_pid_vmsize(obj, p, p_max_size);
1717 char buf[max_user_text];
1719 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1721 print_pid_vmlck(obj, p, p_max_size);
1724 char buf[max_user_text];
1726 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1728 print_pid_vmhwm(obj, p, p_max_size);
1731 char buf[max_user_text];
1733 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1735 print_pid_vmrss(obj, p, p_max_size);
1738 char buf[max_user_text];
1740 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1742 print_pid_vmdata(obj, p, p_max_size);
1745 char buf[max_user_text];
1747 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1749 print_pid_vmstk(obj, p, p_max_size);
1752 char buf[max_user_text];
1754 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1756 print_pid_vmexe(obj, p, p_max_size);
1759 char buf[max_user_text];
1761 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1763 print_pid_vmlib(obj, p, p_max_size);
1766 char buf[max_user_text];
1768 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1770 print_pid_vmpte(obj, p, p_max_size);
1773 char buf[max_user_text];
1775 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1777 print_pid_write(obj, p, p_max_size);
1780 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1782 OBJ(running_processes) {
1783 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1785 OBJ(running_threads) {
1786 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1789 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1792 snprintf(p, p_max_size, "%s", obj->data.s);
1796 new_bg(p, obj->data.l);
1799 new_stippled_hr(obj, p);
1803 human_readable(cur->swap * 1024, p, 255);
1806 human_readable(cur->swapfree * 1024, p, 255);
1809 human_readable(cur->swapmax * 1024, p, 255);
1812 if (cur->swapmax == 0) {
1813 strncpy(p, "No swap", p_max_size);
1815 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1819 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1822 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1825 print_time(obj, p, p_max_size);
1828 print_utime(obj, p, p_max_size);
1831 print_tztime(obj, p, p_max_size);
1834 print_totaldown(obj, p, p_max_size);
1837 print_totalup(obj, p, p_max_size);
1840 char buf[max_user_text];
1842 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1844 print_gid_name(obj, p, p_max_size);
1847 char buf[max_user_text];
1849 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1851 print_uid_name(obj, p, p_max_size);
1854 snprintf(p, p_max_size, "%d", total_updates);
1857 if(total_updates % updatereset != obj->data.i - 1) {
1862 print_upspeed(obj, p, p_max_size);
1865 print_upspeedf(obj, p, p_max_size);
1869 print_upspeedgraph(obj, p, p_max_size);
1873 format_seconds_short(p, p_max_size, (int) cur->uptime);
1876 format_seconds(p, p_max_size, (int) cur->uptime);
1880 snprintf(p, p_max_size, "%s", cur->users.names);
1883 snprintf(p, p_max_size, "%s", cur->users.terms);
1886 snprintf(p, p_max_size, "%s", cur->users.times);
1889 update_user_time(obj->data.s);
1890 snprintf(p, p_max_size, "%s", cur->users.ctime);
1893 snprintf(p, p_max_size, "%d", cur->users.number);
1895 #endif /* __linux__ */
1896 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1897 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1901 msg = get_apm_adapter();
1902 snprintf(p, p_max_size, "%s", msg);
1905 OBJ(apm_battery_life) {
1908 msg = get_apm_battery_life();
1909 snprintf(p, p_max_size, "%s", msg);
1912 OBJ(apm_battery_time) {
1915 msg = get_apm_battery_time();
1916 snprintf(p, p_max_size, "%s", msg);
1919 #endif /* __FreeBSD__ __OpenBSD__ */
1923 print_mpd_title(obj, p, p_max_size);
1925 print_mpd_artist(obj, p, p_max_size);
1927 print_mpd_album(obj, p, p_max_size);
1929 print_mpd_random(obj, p, p_max_size);
1931 print_mpd_repeat(obj, p, p_max_size);
1933 print_mpd_track(obj, p, p_max_size);
1935 print_mpd_name(obj, p, p_max_size);
1937 print_mpd_file(obj, p, p_max_size);
1939 print_mpd_vol(obj, p, p_max_size);
1941 print_mpd_bitrate(obj, p, p_max_size);
1943 print_mpd_status(obj, p, p_max_size);
1945 print_mpd_elapsed(obj, p, p_max_size);
1948 print_mpd_length(obj, p, p_max_size);
1951 print_mpd_percent(obj, p, p_max_size);
1954 print_mpd_bar(obj, p, p_max_size);
1957 print_mpd_smart(obj, p, p_max_size);
1959 OBJ(if_mpd_playing) {
1960 if (!mpd_get_info()->is_playing) {
1967 #define MOC_PRINT(t, a) \
1968 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1970 MOC_PRINT(state, "??");
1973 MOC_PRINT(file, "no file");
1976 MOC_PRINT(title, "no title");
1979 MOC_PRINT(artist, "no artist");
1982 MOC_PRINT(song, "no song");
1985 MOC_PRINT(album, "no album");
1987 OBJ(moc_totaltime) {
1988 MOC_PRINT(totaltime, "0:00");
1991 MOC_PRINT(timeleft, "0:00");
1994 MOC_PRINT(curtime, "0:00");
1997 MOC_PRINT(bitrate, "0Kbps");
2000 MOC_PRINT(rate, "0KHz");
2006 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2009 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2012 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2015 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2017 OBJ(xmms2_comment) {
2018 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2021 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2024 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2027 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2029 OBJ(xmms2_tracknr) {
2030 if (cur->xmms2.tracknr != -1) {
2031 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2034 OBJ(xmms2_bitrate) {
2035 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2038 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2041 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2043 OBJ(xmms2_elapsed) {
2044 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2045 (cur->xmms2.elapsed / 1000) % 60);
2047 OBJ(xmms2_duration) {
2048 snprintf(p, p_max_size, "%02d:%02d",
2049 cur->xmms2.duration / 60000,
2050 (cur->xmms2.duration / 1000) % 60);
2052 OBJ(xmms2_percent) {
2053 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2056 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2058 OBJ(xmms2_playlist) {
2059 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2061 OBJ(xmms2_timesplayed) {
2062 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2065 if (strlen(cur->xmms2.title) < 2
2066 && strlen(cur->xmms2.title) < 2) {
2067 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2069 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2073 OBJ(if_xmms2_connected) {
2074 if (cur->xmms2.conn_state != 1) {
2080 OBJ(audacious_status) {
2081 snprintf(p, p_max_size, "%s",
2082 cur->audacious.items[AUDACIOUS_STATUS]);
2084 OBJ(audacious_title) {
2085 snprintf(p, cur->audacious.max_title_len > 0
2086 ? cur->audacious.max_title_len : p_max_size, "%s",
2087 cur->audacious.items[AUDACIOUS_TITLE]);
2089 OBJ(audacious_length) {
2090 snprintf(p, p_max_size, "%s",
2091 cur->audacious.items[AUDACIOUS_LENGTH]);
2093 OBJ(audacious_length_seconds) {
2094 snprintf(p, p_max_size, "%s",
2095 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2097 OBJ(audacious_position) {
2098 snprintf(p, p_max_size, "%s",
2099 cur->audacious.items[AUDACIOUS_POSITION]);
2101 OBJ(audacious_position_seconds) {
2102 snprintf(p, p_max_size, "%s",
2103 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2105 OBJ(audacious_bitrate) {
2106 snprintf(p, p_max_size, "%s",
2107 cur->audacious.items[AUDACIOUS_BITRATE]);
2109 OBJ(audacious_frequency) {
2110 snprintf(p, p_max_size, "%s",
2111 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2113 OBJ(audacious_channels) {
2114 snprintf(p, p_max_size, "%s",
2115 cur->audacious.items[AUDACIOUS_CHANNELS]);
2117 OBJ(audacious_filename) {
2118 snprintf(p, p_max_size, "%s",
2119 cur->audacious.items[AUDACIOUS_FILENAME]);
2121 OBJ(audacious_playlist_length) {
2122 snprintf(p, p_max_size, "%s",
2123 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2125 OBJ(audacious_playlist_position) {
2126 snprintf(p, p_max_size, "%s",
2127 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2129 OBJ(audacious_main_volume) {
2130 snprintf(p, p_max_size, "%s",
2131 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2133 OBJ(audacious_bar) {
2137 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2138 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2139 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2141 #endif /* AUDACIOUS */
2145 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2148 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2151 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2154 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2157 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2160 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2163 /* we have four different types of top (top, top_mem,
2164 * top_time and top_io). To avoid having almost-same code four
2165 * times, we have this special handler. */
2174 print_top(obj, p, p_max_size);
2175 #endif /* __linux__ */
2177 print_tailhead("tail", obj, p, p_max_size);
2180 print_tailhead("head", obj, p, p_max_size);
2183 print_lines(obj, p, p_max_size);
2187 print_words(obj, p, p_max_size);
2189 #ifdef TCP_PORT_MONITOR
2191 tcp_portmon_action(obj, p, p_max_size);
2193 #endif /* TCP_PORT_MONITOR */
2197 do_iconv_start(obj);
2202 #endif /* HAVE_ICONV */
2204 OBJ(entropy_avail) {
2205 print_entropy_avail(obj, p, p_max_size);
2208 print_entropy_perc(obj, p, p_max_size);
2210 OBJ(entropy_poolsize) {
2211 print_entropy_poolsize(obj, p, p_max_size);
2214 print_entropy_bar(obj, p, p_max_size);
2218 print_smapi(obj, p, p_max_size);
2220 OBJ(if_smapi_bat_installed) {
2222 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2223 if(!smapi_bat_installed(idx)) {
2227 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2229 OBJ(smapi_bat_perc) {
2230 print_smapi_bat_perc(obj, p, p_max_size);
2232 OBJ(smapi_bat_temp) {
2233 print_smapi_bat_temp(obj, p, p_max_size);
2235 OBJ(smapi_bat_power) {
2236 print_smapi_bat_power(obj, p, p_max_size);
2238 OBJ(smapi_bat_bar) {
2239 print_smapi_bat_bar(obj, p, p_max_size);
2244 char buf[max_user_text];
2246 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2247 snprintf(p, p_max_size, "%s", buf);
2253 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2254 char buf[max_user_text];
2257 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2258 snprintf(p, p_max_size, "%s", buf);
2259 if(total_updates % 2) {
2260 for(j=0; p[j] != 0; j++) {
2266 char buf[max_user_text];
2268 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2270 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2271 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2272 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2273 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2274 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2275 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2276 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2278 snprintf(p, p_max_size, "%s", buf);
2281 print_scroll(obj, p, p_max_size, cur);
2284 print_combine(obj, p, cur);
2288 print_nvidia_value(obj, display, p, p_max_size);
2293 /* This is just a meta-object to set host:port */
2296 snprintf(p, p_max_size, "%s",
2297 cur->apcupsd.items[APCUPSD_NAME]);
2299 OBJ(apcupsd_model) {
2300 snprintf(p, p_max_size, "%s",
2301 cur->apcupsd.items[APCUPSD_MODEL]);
2303 OBJ(apcupsd_upsmode) {
2304 snprintf(p, p_max_size, "%s",
2305 cur->apcupsd.items[APCUPSD_UPSMODE]);
2307 OBJ(apcupsd_cable) {
2308 snprintf(p, p_max_size, "%s",
2309 cur->apcupsd.items[APCUPSD_CABLE]);
2311 OBJ(apcupsd_status) {
2312 snprintf(p, p_max_size, "%s",
2313 cur->apcupsd.items[APCUPSD_STATUS]);
2315 OBJ(apcupsd_linev) {
2316 snprintf(p, p_max_size, "%s",
2317 cur->apcupsd.items[APCUPSD_LINEV]);
2320 snprintf(p, p_max_size, "%s",
2321 cur->apcupsd.items[APCUPSD_LOAD]);
2323 OBJ(apcupsd_loadbar) {
2325 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2326 new_bar(obj, p, p_max_size, (int) progress);
2329 OBJ(apcupsd_loadgraph) {
2331 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2332 new_graph(obj, p, p_max_size, (int)progress);
2335 OBJ(apcupsd_loadgauge) {
2337 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2338 new_gauge(obj, p, p_max_size, (int)progress);
2340 OBJ(apcupsd_charge) {
2341 snprintf(p, p_max_size, "%s",
2342 cur->apcupsd.items[APCUPSD_CHARGE]);
2344 OBJ(apcupsd_timeleft) {
2345 snprintf(p, p_max_size, "%s",
2346 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2349 snprintf(p, p_max_size, "%s",
2350 cur->apcupsd.items[APCUPSD_TEMP]);
2352 OBJ(apcupsd_lastxfer) {
2353 snprintf(p, p_max_size, "%s",
2354 cur->apcupsd.items[APCUPSD_LASTXFER]);
2356 #endif /* APCUPSD */
2363 size_t a = strlen(p);
2366 iconv_convert(&a, buff_in, p, p_max_size);
2367 #endif /* HAVE_ICONV */
2368 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2370 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2371 #endif /* HAVE_LUA */
2373 substitute_newlines(p, a - 2);
2382 /* load any new fonts we may have had */
2383 if (need_to_load_fonts) {
2389 void evaluate(const char *text, char *p, int p_max_size)
2391 struct information *tmp_info;
2392 struct text_object subroot;
2394 tmp_info = malloc(sizeof(struct information));
2395 memcpy(tmp_info, &info, sizeof(struct information));
2396 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2397 DBGP("evaluated '%s' to '%s'", text, p);
2399 free_text_objects(&subroot, 1);
2403 double current_update_time, next_update_time, last_update_time;
2405 static void generate_text(void)
2407 struct information *cur = &info;
2409 unsigned int i, j, k;
2415 current_update_time = get_time();
2419 /* add things to the buffer */
2425 generate_text_internal(p, max_user_text, global_root_object, cur);
2426 if(max_text_width > 0) {
2427 for(i = 0, j = 0; p[i] != 0; i++) {
2428 if(p[i] == '\n') j = 0;
2429 else if(j == max_text_width) {
2430 k = i + strlen(p + i) + 1;
2431 if(k < text_buffer_size) {
2438 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2443 if (stuff_in_uppercase) {
2446 tmp_p = text_buffer;
2448 *tmp_p = toupper(*tmp_p);
2453 next_update_time += update_interval;
2454 if (next_update_time < get_time()) {
2455 next_update_time = get_time() + update_interval;
2456 } else if (next_update_time > get_time() + update_interval) {
2457 next_update_time = get_time() + update_interval;
2459 last_update_time = current_update_time;
2463 void set_update_interval(double interval)
2465 update_interval = interval;
2466 update_interval_old = interval;
2469 static inline int get_string_width(const char *s)
2471 return *s ? calc_text_width(s) : 0;
2475 static int get_string_width_special(char *s, int special_index)
2485 if ((output_methods & TO_X) == 0)
2488 p = strndup(s, text_buffer_size);
2492 if (*p == SPECIAL_CHAR) {
2493 /* shift everything over by 1 so that the special char
2494 * doesn't mess up the size calculation */
2495 for (i = 0; i < (long)strlen(p); i++) {
2496 *(p + i) = *(p + i + 1);
2498 if (specials[special_index + idx].type == GRAPH
2499 || specials[special_index + idx].type == GAUGE
2500 || specials[special_index + idx].type == BAR) {
2501 width += specials[special_index + idx].width;
2504 } else if (*p == SECRIT_MULTILINE_CHAR) {
2511 if (strlen(final) > 1) {
2512 width += calc_text_width(final);
2518 static int text_size_updater(char *s, int special_index);
2520 int last_font_height;
2521 static void update_text_area(void)
2525 if ((output_methods & TO_X) == 0)
2527 /* update text size if it isn't fixed */
2532 text_width = minimum_width;
2534 last_font_height = font_height();
2535 for_each_line(text_buffer, text_size_updater);
2537 if (text_height < minimum_height) {
2538 text_height = minimum_height;
2540 if (text_width > maximum_width && maximum_width > 0) {
2541 text_width = maximum_width;
2545 /* get text position on workarea */
2546 switch (text_alignment) {
2547 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2551 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2552 y = workarea[3] - text_height - gap_y;
2555 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2556 y = workarea[3] / 2 - text_height / 2 - gap_y;
2559 switch (text_alignment) {
2560 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2564 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2565 x = workarea[2] - text_width - gap_x;
2568 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2569 x = workarea[2] / 2 - text_width / 2 - gap_x;
2573 if (text_alignment == NONE) { // Let the WM manage the window
2580 #endif /* OWN_WINDOW */
2583 if (own_window && !fixed_pos) {
2586 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2587 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2588 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2589 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2593 /* If window size doesn't match to workarea's size,
2594 * then window probably includes panels (gnome).
2595 * Blah, doesn't work on KDE. */
2596 if (workarea[2] != window.width || workarea[3] != window.height) {
2605 /* update lua window globals */
2606 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2607 #endif /* HAVE_LUA */
2612 static int cur_x, cur_y; /* current x and y for drawing */
2614 //draw_mode also without X11 because we only need to print to stdout with FG
2615 static int draw_mode; /* FG, BG or OUTLINE */
2617 static long current_color;
2619 static int text_size_updater(char *s, int special_index)
2624 if ((output_methods & TO_X) == 0)
2626 /* get string widths and skip specials */
2629 if (*p == SPECIAL_CHAR) {
2631 w += get_string_width(s);
2634 if (specials[special_index].type == BAR
2635 || specials[special_index].type == GAUGE
2636 || specials[special_index].type == GRAPH) {
2637 w += specials[special_index].width;
2638 if (specials[special_index].height > last_font_height) {
2639 last_font_height = specials[special_index].height;
2640 last_font_height += font_height();
2642 } else if (specials[special_index].type == OFFSET) {
2643 if (specials[special_index].arg > 0) {
2644 w += specials[special_index].arg;
2646 } else if (specials[special_index].type == VOFFSET) {
2647 last_font_height += specials[special_index].arg;
2648 } else if (specials[special_index].type == GOTO) {
2649 if (specials[special_index].arg > cur_x) {
2650 w = (int) specials[special_index].arg;
2652 } else if (specials[special_index].type == TAB) {
2653 int start = specials[special_index].arg;
2654 int step = specials[special_index].width;
2656 if (!step || step < 0) {
2659 w += step - (cur_x - text_start_x - start) % step;
2660 } else if (specials[special_index].type == FONT) {
2661 selected_font = specials[special_index].font_added;
2662 if (font_height() > last_font_height) {
2663 last_font_height = font_height();
2669 } else if (*p == SECRIT_MULTILINE_CHAR) {
2672 lw = get_string_width(s);
2673 *p = SECRIT_MULTILINE_CHAR;
2675 w = lw > w ? lw : w;
2676 text_height += last_font_height;
2680 w += get_string_width(s);
2681 if (w > text_width) {
2684 if (text_width > maximum_width && maximum_width) {
2685 text_width = maximum_width;
2688 text_height += last_font_height;
2689 last_font_height = font_height();
2690 return special_index;
2694 static inline void set_foreground_color(long c)
2697 if (output_methods & TO_X) {
2699 if (have_argb_visual) {
2700 current_color = c | (0xff << 24);
2702 #endif /* USE_ARGB */
2706 #endif /* USE_ARGB */
2707 XSetForeground(display, window.gc, current_color);
2711 if (output_methods & TO_NCURSES) {
2712 attron(COLOR_PAIR(c));
2714 #endif /* NCURSES */
2719 static void draw_string(const char *s)
2721 int i, i2, pos, width_of_s;
2724 char *s_with_newlines;
2730 width_of_s = get_string_width(s);
2731 s_with_newlines = strdup(s);
2732 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2733 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2734 s_with_newlines[i] = '\n';
2737 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2738 printf("%s\n", s_with_newlines);
2739 if (extra_newline) fputc('\n', stdout);
2740 fflush(stdout); /* output immediately, don't buffer */
2742 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2743 fprintf(stderr, "%s\n", s_with_newlines);
2744 fflush(stderr); /* output immediately, don't buffer */
2746 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2747 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2749 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2750 fprintf(append_fpointer, "%s\n", s_with_newlines);
2753 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2754 printw("%s", s_with_newlines);
2757 free(s_with_newlines);
2758 memset(tmpstring1, 0, text_buffer_size);
2759 memset(tmpstring2, 0, text_buffer_size);
2760 strncpy(tmpstring1, s, text_buffer_size - 1);
2765 if (output_methods & TO_X) {
2766 max = ((text_width - width_of_s) / get_string_width(" "));
2769 /* This code looks for tabs in the text and coverts them to spaces.
2770 * The trick is getting the correct number of spaces, and not going
2771 * over the window's size without forcing the window larger. */
2772 for (i = 0; i < (int) text_buffer_size; i++) {
2773 if (tmpstring1[i] == '\t') {
2775 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2776 /* guard against overrun */
2777 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2782 /* guard against overrun */
2783 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2788 if (output_methods & TO_X) {
2789 if (text_width == maximum_width) {
2790 /* this means the text is probably pushing the limit,
2791 * so we'll chop it */
2792 while (cur_x + get_string_width(tmpstring2) - text_start_x
2793 > maximum_width && strlen(tmpstring2) > 0) {
2794 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2801 if (output_methods & TO_X) {
2807 c.pixel = current_color;
2808 // query color on custom colormap
2809 XQueryColor(display, window.colourmap, &c);
2812 c2.color.red = c.red;
2813 c2.color.green = c.green;
2814 c2.color.blue = c.blue;
2815 c2.color.alpha = fonts[selected_font].font_alpha;
2817 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2818 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2820 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2821 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2826 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2829 cur_x += width_of_s;
2832 memcpy(tmpstring1, s, text_buffer_size);
2835 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2843 int last_special_needed = -1;
2844 int orig_special_index = special_index;
2847 if (output_methods & TO_X) {
2848 font_h = font_height();
2849 cur_y += font_ascent();
2851 cur_x = text_start_x;
2855 if (*p == SECRIT_MULTILINE_CHAR) {
2856 /* special newline marker for multiline objects */
2861 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2866 /* draw string before special, unless we're dealing multiline
2868 if (last_special_applied > -1) {
2869 special_index = last_special_applied;
2877 switch (specials[special_index].type) {
2879 case HORIZONTAL_LINE:
2881 int h = specials[special_index].height;
2882 int mid = font_ascent() / 2;
2884 w = text_start_x + text_width - cur_x;
2886 XSetLineAttributes(display, window.gc, h, LineSolid,
2887 CapButt, JoinMiter);
2888 XDrawLine(display, window.drawable, window.gc, cur_x,
2889 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2895 int h = specials[special_index].height;
2896 int tmp_s = specials[special_index].arg;
2897 int mid = font_ascent() / 2;
2898 char ss[2] = { tmp_s, tmp_s };
2900 w = text_start_x + text_width - cur_x - 1;
2901 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2902 CapButt, JoinMiter);
2903 XSetDashes(display, window.gc, 0, ss, 2);
2904 XDrawLine(display, window.drawable, window.gc, cur_x,
2905 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2911 int h, bar_usage, by;
2912 if (cur_x - text_start_x > maximum_width
2913 && maximum_width > 0) {
2916 h = specials[special_index].height;
2917 bar_usage = specials[special_index].arg;
2918 by = cur_y - (font_ascent() / 2) - 1;
2923 w = specials[special_index].width;
2925 w = text_start_x + text_width - cur_x - 1;
2931 XSetLineAttributes(display, window.gc, 1, LineSolid,
2932 CapButt, JoinMiter);
2934 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2936 XFillRectangle(display, window.drawable, window.gc, cur_x,
2937 by, w * bar_usage / 255, h);
2945 case GAUGE: /* new GAUGE */
2948 unsigned long last_colour = current_color;
2950 float angle, px, py;
2954 if (cur_x - text_start_x > maximum_width
2955 && maximum_width > 0) {
2959 h = specials[special_index].height;
2960 by = cur_y - (font_ascent() / 2) - 1;
2965 w = specials[special_index].width;
2967 w = text_start_x + text_width - cur_x - 1;
2973 XSetLineAttributes(display, window.gc, 1, LineSolid,
2974 CapButt, JoinMiter);
2976 XDrawArc(display, window.drawable, window.gc,
2977 cur_x, by, w, h * 2, 0, 180*64);
2980 usage = specials[special_index].arg;
2981 angle = (M_PI)*(float)(usage)/255.;
2982 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2983 py = (float)(by+(h))-(float)(h)*sin(angle);
2985 XDrawLine(display, window.drawable, window.gc,
2986 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2994 set_foreground_color(last_colour);
3002 int h, by, i = 0, j = 0;
3004 unsigned long last_colour = current_color;
3005 unsigned long *tmpcolour = 0;
3006 if (cur_x - text_start_x > maximum_width
3007 && maximum_width > 0) {
3010 h = specials[special_index].height;
3011 by = cur_y - (font_ascent() / 2) - 1;
3016 w = specials[special_index].width;
3018 w = text_start_x + text_width - cur_x - 1;
3023 if (draw_graph_borders) {
3024 XSetLineAttributes(display, window.gc, 1, LineSolid,
3025 CapButt, JoinMiter);
3026 XDrawRectangle(display, window.drawable, window.gc,
3029 XSetLineAttributes(display, window.gc, 1, LineSolid,
3030 CapButt, JoinMiter);
3032 if (specials[special_index].last_colour != 0
3033 || specials[special_index].first_colour != 0) {
3034 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3037 for (i = w - 2; i > -1; i--) {
3038 if (specials[special_index].last_colour != 0
3039 || specials[special_index].first_colour != 0) {
3040 if (specials[special_index].tempgrad) {
3043 (int)((float)(w - 2) - specials[special_index].graph[j] *
3044 (w - 2) / (float)specials[special_index].graph_scale)
3048 (int)((float)(w - 2) - specials[special_index].graph[j] *
3049 (w - 2) / (float)specials[special_index].graph_scale)
3052 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3054 (int)((float)(w - 2) - specials[special_index].graph[j] *
3055 (w - 2) / (float)specials[special_index].graph_scale)
3059 #endif /* DEBUG_lol */
3060 XSetForeground(display, window.gc, tmpcolour[
3061 (int)((float)(w - 2) - specials[special_index].graph[j] *
3062 (w - 2) / (float)specials[special_index].graph_scale)
3065 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3068 /* this is mugfugly, but it works */
3069 XDrawLine(display, window.drawable, window.gc,
3070 cur_x + i + 1, by + h, cur_x + i + 1,
3071 round_to_int((double)by + h - specials[special_index].graph[j] *
3072 (h - 1) / specials[special_index].graph_scale));
3073 if ((w - i) / ((float) (w - 2) /
3074 (specials[special_index].graph_width)) > j
3075 && j < MAX_GRAPH_DEPTH - 3) {
3079 if (tmpcolour) free(tmpcolour);
3084 /* if (draw_mode == BG) {
3085 set_foreground_color(default_bg_color);
3086 } else if (draw_mode == OUTLINE) {
3087 set_foreground_color(default_out_color);
3089 set_foreground_color(default_fg_color);
3091 if (show_graph_range) {
3094 unsigned short int seconds = update_interval * w;
3100 unsigned short int timeunits;
3102 timeunits = seconds / 86400; seconds %= 86400;
3103 if (timeunits > 0) {
3104 asprintf(&tmp_day_str, "%dd", timeunits);
3106 tmp_day_str = strdup("");
3108 timeunits = seconds / 3600; seconds %= 3600;
3109 if (timeunits > 0) {
3110 asprintf(&tmp_hour_str, "%dh", timeunits);
3112 tmp_hour_str = strdup("");
3114 timeunits = seconds / 60; seconds %= 60;
3115 if (timeunits > 0) {
3116 asprintf(&tmp_min_str, "%dm", timeunits);
3118 tmp_min_str = strdup("");
3121 asprintf(&tmp_sec_str, "%ds", seconds);
3123 tmp_sec_str = strdup("");
3125 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3126 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3128 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3130 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3131 cur_y += font_h / 2;
3132 draw_string(tmp_str);
3138 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3142 cur_x += font_ascent() / 2;
3143 cur_y += font_h / 2;
3145 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3147 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3148 draw_string(tmp_str);
3154 set_foreground_color(last_colour);
3160 int old = font_ascent();
3162 cur_y -= font_ascent();
3163 selected_font = specials[special_index].font_added;
3165 if (cur_y + font_ascent() < cur_y + old) {
3168 cur_y += font_ascent();
3170 font_h = font_height();
3175 if (draw_mode == FG) {
3176 set_foreground_color(specials[special_index].arg);
3182 if (draw_mode == BG) {
3183 set_foreground_color(specials[special_index].arg);
3188 if (draw_mode == OUTLINE) {
3189 set_foreground_color(specials[special_index].arg);
3194 w += specials[special_index].arg;
3195 last_special_needed = special_index;
3199 cur_y += specials[special_index].arg;
3203 if (specials[special_index].arg >= 0) {
3204 cur_x = (int) specials[special_index].arg;
3206 last_special_needed = special_index;
3211 int start = specials[special_index].arg;
3212 int step = specials[special_index].width;
3214 if (!step || step < 0) {
3217 w = step - (cur_x - text_start_x - start) % step;
3218 last_special_needed = special_index;
3224 /* TODO: add back in "+ window.border_inner_margin" to the end of
3226 int pos_x = text_start_x + text_width -
3227 get_string_width_special(s, special_index);
3229 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3230 "get_string_width(p) %i gap_x %i "
3231 "specials[special_index].arg %i window.border_inner_margin %i "
3232 "window.border_width %i\n", pos_x, text_start_x, text_width,
3233 cur_x, get_string_width_special(s), gap_x,
3234 specials[special_index].arg, window.border_inner_margin,
3235 window.border_width); */
3236 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3237 cur_x = pos_x - specials[special_index].arg;
3239 last_special_needed = special_index;
3245 int pos_x = (text_width) / 2 - get_string_width_special(s,
3246 special_index) / 2 - (cur_x -
3248 /* int pos_x = text_start_x + text_width / 2 -
3249 get_string_width_special(s) / 2; */
3251 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3252 "get_string_width(p) %i gap_x %i "
3253 "specials[special_index].arg %i\n", pos_x, text_start_x,
3254 text_width, cur_x, get_string_width(s), gap_x,
3255 specials[special_index].arg); */
3256 if (pos_x > specials[special_index].arg) {
3257 w = pos_x - specials[special_index].arg;
3259 last_special_needed = special_index;
3269 if (special_index != last_special_applied) {
3272 special_index = orig_special_index;
3273 last_special_applied = -1;
3284 if (output_methods & TO_NCURSES) {
3287 #endif /* NCURSES */
3289 if (output_methods & TO_X)
3290 cur_y += font_descent();
3292 if (recurse && *recurse) {
3293 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3294 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3296 return special_index;
3299 static int draw_line(char *s, int special_index)
3302 if (output_methods & TO_X) {
3303 return draw_each_line_inner(s, special_index, -1);
3307 if (output_methods & TO_NCURSES) {
3308 return draw_each_line_inner(s, special_index, -1);
3310 #endif /* NCURSES */
3312 UNUSED(special_index);
3316 static void draw_text(void)
3320 llua_draw_pre_hook();
3321 #endif /* HAVE_LUA */
3322 if (output_methods & TO_X) {
3323 cur_y = text_start_y;
3326 if (draw_borders && window.border_width > 0) {
3327 if (stippled_borders) {
3328 char ss[2] = { stippled_borders, stippled_borders };
3329 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3330 CapButt, JoinMiter);
3331 XSetDashes(display, window.gc, 0, ss, 2);
3333 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3334 CapButt, JoinMiter);
3337 XDrawRectangle(display, window.drawable, window.gc,
3338 text_start_x - window.border_inner_margin - window.border_width,
3339 text_start_y - window.border_inner_margin - window.border_width,
3340 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3341 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3349 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3350 attron(COLOR_PAIR(COLOR_WHITE));
3351 #endif /* NCURSES */
3352 for_each_line(text_buffer, draw_line);
3353 #if defined(HAVE_LUA) && defined(X11)
3354 llua_draw_post_hook();
3355 #endif /* HAVE_LUA */
3358 static void draw_stuff(void)
3361 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3363 if (overwrite_file) {
3364 overwrite_fpointer = fopen(overwrite_file, "w");
3365 if(!overwrite_fpointer)
3366 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3369 append_fpointer = fopen(append_file, "a");
3370 if(!append_fpointer)
3371 NORM_ERR("Can't append '%s' anymore", append_file);
3374 if (output_methods & TO_X) {
3376 if (draw_shades && !draw_outline) {
3379 set_foreground_color(default_bg_color);
3390 for (i = -1; i < 2; i++) {
3391 for (j = -1; j < 2; j++) {
3392 if (i == 0 && j == 0) {
3397 set_foreground_color(default_out_color);
3398 draw_mode = OUTLINE;
3406 set_foreground_color(default_fg_color);
3411 #if defined(X11) && defined(HAVE_XDBE)
3412 if (output_methods & TO_X) {
3413 xdbe_swap_buffers();
3415 #endif /* X11 && HAVE_XDBE */
3416 if(overwrite_fpointer) {
3417 fclose(overwrite_fpointer);
3418 overwrite_fpointer = 0;
3420 if(append_fpointer) {
3421 fclose(append_fpointer);
3422 append_fpointer = 0;
3427 static void clear_text(int exposures)
3431 /* The swap action is XdbeBackground, which clears */
3435 if (display && window.window) { // make sure these are !null
3436 /* there is some extra space for borders and outlines */
3437 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3438 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3439 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3440 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3445 static int need_to_update;
3447 /* update_text() generates new text and clears old text area */
3448 static void update_text(void)
3455 if (output_methods & TO_X)
3460 llua_update_info(&info, update_interval);
3461 #endif /* HAVE_LUA */
3464 #ifdef HAVE_SYS_INOTIFY_H
3468 static void main_loop(void)
3471 #ifdef SIGNAL_BLOCKING
3472 sigset_t newmask, oldmask;
3475 #ifdef HAVE_SYS_INOTIFY_H
3476 int inotify_config_wd = -1;
3477 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3478 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3479 char inotify_buff[INOTIFY_BUF_LEN];
3480 #endif /* HAVE_SYS_INOTIFY_H */
3483 #ifdef SIGNAL_BLOCKING
3484 sigemptyset(&newmask);
3485 sigaddset(&newmask, SIGINT);
3486 sigaddset(&newmask, SIGTERM);
3487 sigaddset(&newmask, SIGUSR1);
3490 last_update_time = 0.0;
3491 next_update_time = get_time();
3493 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3494 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3495 char buf[max_user_text];
3497 get_battery_short_status(buf, max_user_text, "BAT0");
3499 update_interval = update_interval_bat;
3501 update_interval = update_interval_old;
3506 #ifdef SIGNAL_BLOCKING
3507 /* block signals. we will inspect for pending signals later */
3508 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3509 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3514 if (output_methods & TO_X) {
3517 /* wait for X event or timeout */
3519 if (!XPending(display)) {
3523 t = next_update_time - get_time();
3527 } else if (t > update_interval) {
3528 t = update_interval;
3531 tv.tv_sec = (long) t;
3532 tv.tv_usec = (long) (t * 1000000) % 1000000;
3534 FD_SET(ConnectionNumber(display), &fdsr);
3536 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3538 if (errno != EINTR) {
3539 NORM_ERR("can't select(): %s", strerror(errno));
3549 if (need_to_update) {
3551 int wx = window.x, wy = window.y;
3561 /* resize window if it isn't right size */
3563 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3564 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3565 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3566 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3567 draw_stuff(); /* redraw everything in our newly sized window */
3568 XResizeWindow(display, window.window, window.width,
3569 window.height); /* resize window */
3570 set_transparent_background(window.window);
3573 xdbe_swap_buffers();
3578 /* update lua window globals */
3579 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3580 #endif /* HAVE_LUA */
3583 /* move window if it isn't in right position */
3584 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3585 XMoveWindow(display, window.window, window.x, window.y);
3590 if (changed && window.type == TYPE_PANEL) {
3593 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3596 switch (text_alignment) {
3623 set_struts(sidenum);
3634 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3635 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3636 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3637 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3638 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3643 /* handle X events */
3644 while (XPending(display)) {
3647 XNextEvent(display, &ev);
3654 r.width = ev.xexpose.width;
3655 r.height = ev.xexpose.height;
3656 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3660 case PropertyNotify:
3662 if ( ev.xproperty.state == PropertyNewValue ) {
3663 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3669 case ReparentNotify:
3670 /* make background transparent */
3672 set_transparent_background(window.window);
3676 case ConfigureNotify:
3678 /* if window size isn't what expected, set fixed size */
3679 if (ev.xconfigure.width != window.width
3680 || ev.xconfigure.height != window.height) {
3681 if (window.width != 0 && window.height != 0) {
3685 /* clear old stuff before screwing up
3690 XWindowAttributes attrs;
3691 if (XGetWindowAttributes(display,
3692 window.window, &attrs)) {
3693 window.width = attrs.width;
3694 window.height = attrs.height;
3698 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3699 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3700 if (text_width > maximum_width
3701 && maximum_width > 0) {
3702 text_width = maximum_width;
3706 /* if position isn't what expected, set fixed pos
3707 * total_updates avoids setting fixed_pos when window
3708 * is set to weird locations when started */
3709 /* // this is broken
3710 if (total_updates >= 2 && !fixed_pos
3711 && (window.x != ev.xconfigure.x
3712 || window.y != ev.xconfigure.y)
3713 && (ev.xconfigure.x != 0
3714 || ev.xconfigure.y != 0)) {
3722 /* if an ordinary window with decorations */
3723 if ((window.type == TYPE_NORMAL &&
3724 (!TEST_HINT(window.hints,
3725 HINT_UNDECORATED))) ||
3726 window.type == TYPE_DESKTOP) {
3727 /* allow conky to hold input focus. */
3730 /* forward the click to the desktop window */
3731 XUngrabPointer(display, ev.xbutton.time);
3732 ev.xbutton.window = window.desktop;
3733 ev.xbutton.x = ev.xbutton.x_root;
3734 ev.xbutton.y = ev.xbutton.y_root;
3735 XSendEvent(display, ev.xbutton.window, False,
3736 ButtonPressMask, &ev);
3737 XSetInputFocus(display, ev.xbutton.window,
3738 RevertToParent, ev.xbutton.time);
3745 /* if an ordinary window with decorations */
3746 if ((window.type == TYPE_NORMAL)
3747 && (!TEST_HINT(window.hints,
3748 HINT_UNDECORATED))) {
3749 /* allow conky to hold input focus. */
3752 /* forward the release to the desktop window */
3753 ev.xbutton.window = window.desktop;
3754 ev.xbutton.x = ev.xbutton.x_root;
3755 ev.xbutton.y = ev.xbutton.y_root;
3756 XSendEvent(display, ev.xbutton.window, False,
3757 ButtonReleaseMask, &ev);
3766 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3767 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3769 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3770 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3772 #endif /* HAVE_XDAMAGE */
3778 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3779 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3780 #endif /* HAVE_XDAMAGE */
3782 /* XDBE doesn't seem to provide a way to clear the back buffer
3783 * without interfering with the front buffer, other than passing
3784 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3785 * using XDBE, we need to redraw the text even if it wasn't part of
3786 * the exposed area. OTOH, if we're not going to call draw_stuff at
3787 * all, then no swap happens and we can safely do nothing. */
3789 if (!XEmptyRegion(x11_stuff.region)) {
3794 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3795 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3796 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3797 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3798 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3801 XSetRegion(display, window.gc, x11_stuff.region);
3804 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3808 XDestroyRegion(x11_stuff.region);
3809 x11_stuff.region = XCreateRegion();
3813 t = (next_update_time - get_time()) * 1000000;
3814 if(t > 0) usleep((useconds_t)t);
3818 if(output_methods & TO_NCURSES) {
3827 #ifdef SIGNAL_BLOCKING
3828 /* unblock signals of interest and let handler fly */
3829 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3830 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3834 switch (g_signal_pending) {
3837 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3842 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3845 if (output_methods & TO_X) {
3846 XDestroyRegion(x11_stuff.region);
3847 x11_stuff.region = NULL;
3849 XDamageDestroy(display, x11_stuff.damage);
3850 XFixesDestroyRegion(display, x11_stuff.region2);
3851 XFixesDestroyRegion(display, x11_stuff.part);
3852 #endif /* HAVE_XDAMAGE */
3858 if(overwrite_file) {
3859 free(overwrite_file);
3868 /* Reaching here means someone set a signal
3869 * (SIGXXXX, signal_handler), but didn't write any code
3871 * If you don't want to handle a signal, don't set a handler on
3872 * it in the first place. */
3873 if (g_signal_pending) {
3874 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3878 #ifdef HAVE_SYS_INOTIFY_H
3879 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3880 inotify_config_wd = inotify_add_watch(inotify_fd,
3884 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3885 int len = 0, idx = 0;
3887 struct timeval time_to_wait;
3889 FD_ZERO(&descriptors);
3890 FD_SET(inotify_fd, &descriptors);
3892 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3894 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3895 if (FD_ISSET(inotify_fd, &descriptors)) {
3896 /* process inotify events */
3897 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3898 while (len > 0 && idx < len) {
3899 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3900 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3901 /* current_config should be reloaded */
3902 NORM_ERR("'%s' modified, reloading...", current_config);
3904 if (ev->mask & IN_IGNORED) {
3905 /* for some reason we get IN_IGNORED here
3906 * sometimes, so we need to re-add the watch */
3907 inotify_config_wd = inotify_add_watch(inotify_fd,
3914 llua_inotify_query(ev->wd, ev->mask);
3916 #endif /* HAVE_LUA */
3917 idx += INOTIFY_EVENT_SIZE + ev->len;
3921 #endif /* HAVE_SYS_INOTIFY_H */
3924 llua_update_info(&info, update_interval);
3925 #endif /* HAVE_LUA */
3926 g_signal_pending = 0;
3928 clean_up(NULL, NULL);
3930 #ifdef HAVE_SYS_INOTIFY_H
3931 if (inotify_fd != -1) {
3932 inotify_rm_watch(inotify_fd, inotify_config_wd);
3934 inotify_fd = inotify_config_wd = 0;
3936 #endif /* HAVE_SYS_INOTIFY_H */
3940 static void load_config_file_x11(const char *);
3942 void initialisation(int argc, char** argv);
3944 /* reload the config file */
3945 static void reload_config(void)
3947 char *current_config_copy = strdup(current_config);
3948 clean_up(NULL, NULL);
3949 current_config = current_config_copy;
3950 initialisation(argc_copy, argv_copy);
3953 void clean_up(void *memtofree1, void* memtofree2)
3958 if(output_methods & TO_NCURSES) {
3962 conftree_empty(currentconffile);
3963 currentconffile = NULL;
3970 timed_thread_destroy_registered_threads();
3972 if (info.cpu_usage) {
3973 free(info.cpu_usage);
3974 info.cpu_usage = NULL;
3977 if (x_initialised == YES) {
3978 if(window_created == 1) {
3979 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3980 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3981 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3982 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3986 if(x11_stuff.region) {
3987 XDestroyRegion(x11_stuff.region);
3988 x11_stuff.region = NULL;
3990 XCloseDisplay(display);
3992 if(info.x11.desktop.all_names) {
3993 free(info.x11.desktop.all_names);
3994 info.x11.desktop.all_names = NULL;
3996 if (info.x11.desktop.name) {
3997 free(info.x11.desktop.name);
3998 info.x11.desktop.name = NULL;
4002 free(fonts); //in set_default_configurations a font is set but not loaded
4008 free_update_callbacks();
4012 free_text_objects(&global_root_object, 0);
4031 free(current_config);
4034 #ifdef TCP_PORT_MONITOR
4035 tcp_portmon_clear();
4044 weather_free_info();
4047 llua_shutdown_hook();
4049 #endif /* HAVE_LUA */
4051 if (output_methods & TO_X)
4059 for (i = 0; i < special_count; i++) {
4060 if (specials[i].type == GRAPH) {
4061 free(specials[i].graph);
4069 clear_diskio_stats();
4070 if(global_cpu != NULL) {
4076 static int string_to_bool(const char *s)
4079 // Assumes an option without a true/false means true
4081 } else if (strcasecmp(s, "yes") == EQUAL) {
4083 } else if (strcasecmp(s, "true") == EQUAL) {
4085 } else if (strcasecmp(s, "1") == EQUAL) {
4092 static enum alignment string_to_alignment(const char *s)
4094 if (strcasecmp(s, "top_left") == EQUAL) {
4096 } else if (strcasecmp(s, "top_right") == EQUAL) {
4098 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4100 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4102 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4103 return BOTTOM_RIGHT;
4104 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4105 return BOTTOM_MIDDLE;
4106 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4108 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4109 return MIDDLE_RIGHT;
4110 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4111 return MIDDLE_MIDDLE;
4112 } else if (strcasecmp(s, "tl") == EQUAL) {
4114 } else if (strcasecmp(s, "tr") == EQUAL) {
4116 } else if (strcasecmp(s, "tm") == EQUAL) {
4118 } else if (strcasecmp(s, "bl") == EQUAL) {
4120 } else if (strcasecmp(s, "br") == EQUAL) {
4121 return BOTTOM_RIGHT;
4122 } else if (strcasecmp(s, "bm") == EQUAL) {
4123 return BOTTOM_MIDDLE;
4124 } else if (strcasecmp(s, "ml") == EQUAL) {
4126 } else if (strcasecmp(s, "mr") == EQUAL) {
4127 return MIDDLE_RIGHT;
4128 } else if (strcasecmp(s, "mm") == EQUAL) {
4129 return MIDDLE_MIDDLE;
4130 } else if (strcasecmp(s, "none") == EQUAL) {
4138 static void set_default_configurations_for_x(void)
4140 default_fg_color = WhitePixel(display, screen);
4141 default_bg_color = BlackPixel(display, screen);
4142 default_out_color = BlackPixel(display, screen);
4143 color0 = default_fg_color;
4144 color1 = default_fg_color;
4145 color2 = default_fg_color;
4146 color3 = default_fg_color;
4147 color4 = default_fg_color;
4148 color5 = default_fg_color;
4149 color6 = default_fg_color;
4150 color7 = default_fg_color;
4151 color8 = default_fg_color;
4152 color9 = default_fg_color;
4153 current_text_color = default_fg_color;
4157 static void set_default_configurations(void)
4164 fork_to_background = 0;
4165 total_run_times = 0;
4166 info.cpu_avg_samples = 2;
4167 info.net_avg_samples = 2;
4168 info.diskio_avg_samples = 2;
4173 format_human_readable = 1;
4183 mpd_env_host = getenv("MPD_HOST");
4184 mpd_env_port = getenv("MPD_PORT");
4186 if (!mpd_env_host || !strlen(mpd_env_host)) {
4187 mpd_set_host("localhost");
4189 /* MPD_HOST environment variable is set */
4190 char *mpd_hostpart = strchr(mpd_env_host, '@');
4191 if (!mpd_hostpart) {
4192 mpd_set_host(mpd_env_host);
4194 /* MPD_HOST contains a password */
4195 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4196 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4198 if (!strlen(mpd_hostpart + 1)) {
4199 mpd_set_host("localhost");
4201 mpd_set_host(mpd_hostpart + 1);
4204 mpd_set_password(mpd_password, 1);
4209 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4210 /* failed to set port from environment variable */
4211 mpd_set_port("6600");
4215 info.xmms2.artist = NULL;
4216 info.xmms2.album = NULL;
4217 info.xmms2.title = NULL;
4218 info.xmms2.genre = NULL;
4219 info.xmms2.comment = NULL;
4220 info.xmms2.url = NULL;
4221 info.xmms2.status = NULL;
4222 info.xmms2.playlist = NULL;
4224 use_spacer = NO_SPACER;
4226 output_methods = TO_X;
4228 output_methods = TO_STDOUT;
4231 show_graph_scale = 0;
4232 show_graph_range = 0;
4235 draw_graph_borders = 1;
4237 set_first_font("6x10");
4245 window.type = TYPE_NORMAL;
4247 strcpy(window.class_name, PACKAGE_NAME);
4248 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4250 use_argb_visual = 0;
4253 stippled_borders = 0;
4254 window.border_inner_margin = 3;
4255 window.border_outer_margin = 1;
4256 window.border_width = 1;
4257 text_alignment = BOTTOM_LEFT;
4258 info.x11.monitor.number = 1;
4259 info.x11.monitor.current = 0;
4260 info.x11.desktop.current = 1;
4261 info.x11.desktop.number = 1;
4262 info.x11.desktop.nitems = 0;
4263 info.x11.desktop.all_names = NULL;
4264 info.x11.desktop.name = NULL;
4269 free(current_mail_spool);
4273 variable_substitute(MAIL_FILE, buf, 256);
4274 if (buf[0] != '\0') {
4275 current_mail_spool = strndup(buf, text_buffer_size);
4280 set_update_interval(3);
4281 update_interval_bat = NOBATTERY;
4282 info.music_player_interval = 1.0;
4283 stuff_in_uppercase = 0;
4284 info.users.number = 1;
4286 set_times_in_seconds(0);
4288 #ifdef TCP_PORT_MONITOR
4289 /* set default connection limit */
4290 tcp_portmon_set_max_connections(0);
4294 /* returns 1 if you can overwrite or create the file at 'path' */
4295 static _Bool overwrite_works(const char *path)
4299 if (!(filepointer = fopen(path, "w")))
4301 fclose(filepointer);
4305 /* returns 1 if you can append or create the file at 'path' */
4306 static _Bool append_works(const char *path)
4310 if (!(filepointer = fopen(path, "a")))
4312 fclose(filepointer);
4318 /* WARNING, this type not in Xlib spec */
4319 int x11_error_handler(Display *d, XErrorEvent *err)
4320 __attribute__((noreturn));
4321 int x11_error_handler(Display *d, XErrorEvent *err)
4323 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",
4325 (long unsigned)err->display,
4326 (long)err->resourceid,
4336 int x11_ioerror_handler(Display *d)
4337 __attribute__((noreturn));
4338 int x11_ioerror_handler(Display *d)
4340 NORM_ERR("X Error: Display %lx\n",
4347 static void X11_initialisation(void)
4349 if (x_initialised == YES) return;
4350 output_methods |= TO_X;
4352 set_default_configurations_for_x();
4353 x_initialised = YES;
4356 /* WARNING, this type not in Xlib spec */
4357 XSetErrorHandler(&x11_error_handler);
4358 XSetIOErrorHandler(&x11_ioerror_handler);
4362 static char **xargv = 0;
4363 static int xargc = 0;
4365 static void X11_create_window(void)
4367 if (output_methods & TO_X) {
4369 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4370 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4372 #else /* OWN_WINDOW */
4373 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4374 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4376 #endif /* OWN_WINDOW */
4380 update_text_area(); /* to position text/window on screen */
4383 if (own_window && !fixed_pos) {
4384 XMoveWindow(display, window.window, window.x, window.y);
4387 set_transparent_background(window.window);
4395 x11_stuff.region = XCreateRegion();
4397 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4398 NORM_ERR("Xdamage extension unavailable");
4400 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4401 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4402 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4403 #endif /* HAVE_XDAMAGE */
4406 update_text_area(); /* to get initial size of the window */
4409 /* setup lua window globals */
4410 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4411 #endif /* HAVE_LUA */
4415 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4416 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4417 #define CONF2(a) if (strcasecmp(name, a) == 0)
4418 #define CONF(a) else CONF2(a)
4419 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4420 || strcasecmp(name, b) == 0)
4421 #define CONF_CONTINUE 1
4422 #define CONF_BREAK 2
4423 #define CONF_BUFF_SIZE 512
4425 static FILE *open_config_file(const char *f)
4427 #ifdef CONFIG_OUTPUT
4428 if (!strcmp(f, "==builtin==")) {
4429 return conf_cookie_open();
4431 #endif /* CONFIG_OUTPUT */
4432 return fopen(f, "r");
4435 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4439 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4442 remove_comments(buf);
4447 while (*p && isspace((int) *p)) {
4451 return CONF_CONTINUE; /* empty line */
4458 while (*p2 && !isspace((int) *p2)) {
4462 *p2 = '\0'; /* break at name's end */
4469 while (*p && isspace((int) *p)) {
4475 p2 = *value + strlen(*value);
4476 while (isspace((int) *(p2 - 1))) {
4485 char load_config_file(const char *f)
4490 fp = open_config_file(f);
4494 DBGP("reading contents from config file '%s'", f);
4497 char buff[CONF_BUFF_SIZE], *name, *value;
4498 int ret = do_config_step(&line, fp, buff, &name, &value);
4499 if (ret == CONF_BREAK) {
4501 } else if (ret == CONF_CONTINUE) {
4507 /* don't listen if X is already initialised or
4508 * if we already know we don't want it */
4509 if(x_initialised != YES) {
4510 if (string_to_bool(value)) {
4511 output_methods &= TO_X;
4513 output_methods &= ~TO_X;
4514 x_initialised = NEVER;
4519 if (!value || x_initialised == YES) {
4524 disp = strdup(value);
4529 if (window.type == TYPE_DOCK)
4532 #endif /*OWN_WINDOW */
4534 int a = string_to_alignment(value);
4545 CONF("background") {
4546 fork_to_background = string_to_bool(value);
4549 CONF2("background") {
4550 fork_to_background = string_to_bool(value);
4554 CONF("show_graph_scale") {
4555 show_graph_scale = string_to_bool(value);
4557 CONF("show_graph_range") {
4558 show_graph_range = string_to_bool(value);
4560 CONF("border_inner_margin") {
4562 window.border_inner_margin = strtol(value, 0, 0);
4563 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4568 CONF("border_outer_margin") {
4570 window.border_outer_margin = strtol(value, 0, 0);
4571 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4576 CONF("border_width") {
4578 window.border_width = strtol(value, 0, 0);
4579 if (window.border_width < 0) window.border_width = 0;
4585 #define TEMPLATE_CONF(n) \
4586 CONF("template"#n) { \
4587 if (set_template(n, value)) \
4602 parse_global_imap_mail_args(value);
4609 parse_global_pop3_mail_args(value);
4614 CONF("default_bar_size") {
4617 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4624 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4628 CONF("default_graph_size") {
4631 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4638 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4641 CONF("default_gauge_size") {
4644 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4651 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4658 mpd_set_host(value);
4664 if (value && mpd_set_port(value)) {
4668 CONF("mpd_password") {
4670 mpd_set_password(value, 0);
4676 CONF("music_player_interval") {
4678 info.music_player_interval = strtod(value, 0);
4684 CONF("sensor_device") {
4686 sensor_device = strtol(value, 0, 0);
4692 CONF("cpu_avg_samples") {
4694 cpu_avg_samples = strtol(value, 0, 0);
4695 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4698 info.cpu_avg_samples = cpu_avg_samples;
4704 CONF("net_avg_samples") {
4706 net_avg_samples = strtol(value, 0, 0);
4707 if (net_avg_samples < 1 || net_avg_samples > 14) {
4710 info.net_avg_samples = net_avg_samples;
4716 CONF("diskio_avg_samples") {
4718 diskio_avg_samples = strtol(value, 0, 0);
4719 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4722 info.diskio_avg_samples = diskio_avg_samples;
4730 CONF("double_buffer") {
4731 use_xdbe = string_to_bool(value);
4735 CONF("override_utf8_locale") {
4736 utf8_mode = string_to_bool(value);
4738 CONF("draw_borders") {
4739 draw_borders = string_to_bool(value);
4741 CONF("draw_graph_borders") {
4742 draw_graph_borders = string_to_bool(value);
4744 CONF("draw_shades") {
4745 draw_shades = string_to_bool(value);
4747 CONF("draw_outline") {
4748 draw_outline = string_to_bool(value);
4751 CONF("times_in_seconds") {
4752 set_times_in_seconds(string_to_bool(value));
4754 CONF("max_text_width") {
4755 max_text_width = atoi(value);
4757 CONF("out_to_console") {
4758 if(string_to_bool(value)) {
4759 output_methods |= TO_STDOUT;
4761 output_methods &= ~TO_STDOUT;
4764 CONF("extra_newline") {
4765 extra_newline = string_to_bool(value);
4767 CONF("out_to_stderr") {
4768 if(string_to_bool(value))
4769 output_methods |= TO_STDERR;
4772 CONF("out_to_ncurses") {
4773 if(string_to_bool(value)) {
4776 output_methods |= TO_NCURSES;
4780 CONF("overwrite_file") {
4781 if(overwrite_file) {
4782 free(overwrite_file);
4785 if(overwrite_works(value)) {
4786 overwrite_file = strdup(value);
4787 output_methods |= OVERWRITE_FILE;
4789 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4791 CONF("append_file") {
4796 if(append_works(value)) {
4797 append_file = strdup(value);
4798 output_methods |= APPEND_FILE;
4800 NORM_ERR("append_file won't be able to create/append '%s'", value);
4802 CONF("use_spacer") {
4804 if (strcasecmp(value, "left") == EQUAL) {
4805 use_spacer = LEFT_SPACER;
4806 } else if (strcasecmp(value, "right") == EQUAL) {
4807 use_spacer = RIGHT_SPACER;
4808 } else if (strcasecmp(value, "none") == EQUAL) {
4809 use_spacer = NO_SPACER;
4811 use_spacer = string_to_bool(value);
4812 NORM_ERR("use_spacer should have an argument of left, right, or"
4813 " none. '%s' seems to be some form of '%s', so"
4814 " defaulting to %s.", value,
4815 use_spacer ? "true" : "false",
4816 use_spacer ? "right" : "none");
4818 use_spacer = RIGHT_SPACER;
4820 use_spacer = NO_SPACER;
4824 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4825 use_spacer = RIGHT_SPACER;
4831 use_xft = string_to_bool(value);
4835 set_first_font(value);
4839 if (value && font_count >= 0) {
4840 fonts[0].font_alpha = atof(value) * 65535.0;
4847 if (string_to_bool(value)) {
4848 NORM_ERR("Xft not enabled at compile time");
4852 /* xftfont silently ignored when no Xft */
4855 /* xftalpha is silently ignored when no Xft */
4860 set_first_font(value);
4868 gap_x = atoi(value);
4875 gap_y = atoi(value);
4881 CONF("mail_spool") {
4885 variable_substitute(value, buffer, 256);
4887 if (buffer[0] != '\0') {
4888 if (current_mail_spool) {
4889 free(current_mail_spool);
4891 current_mail_spool = strndup(buffer, text_buffer_size);
4898 CONF("minimum_size") {
4900 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4902 if (sscanf(value, "%d", &minimum_width) != 1) {
4910 CONF("maximum_width") {
4912 if (sscanf(value, "%d", &maximum_width) != 1) {
4920 CONF("no_buffers") {
4921 no_buffers = string_to_bool(value);
4923 CONF("top_name_width") {
4924 if (set_top_name_width(value))
4927 CONF("top_cpu_separate") {
4928 cpu_separate = string_to_bool(value);
4930 CONF("short_units") {
4931 short_units = string_to_bool(value);
4933 CONF("format_human_readable") {
4934 format_human_readable = string_to_bool(value);
4937 CONF("hddtemp_host") {
4938 set_hddtemp_host(value);
4940 CONF("hddtemp_port") {
4941 set_hddtemp_port(value);
4943 #endif /* HDDTEMP */
4944 CONF("pad_percents") {
4945 pad_percents = atoi(value);
4949 CONF("own_window") {
4951 own_window = string_to_bool(value);
4954 CONF("own_window_class") {
4956 memset(window.class_name, 0, sizeof(window.class_name));
4957 strncpy(window.class_name, value,
4958 sizeof(window.class_name) - 1);
4961 CONF("own_window_title") {
4963 memset(window.title, 0, sizeof(window.title));
4964 strncpy(window.title, value, sizeof(window.title) - 1);
4967 CONF("own_window_transparent") {
4969 set_transparent = string_to_bool(value);
4972 CONF("own_window_hints") {
4974 char *p_hint, *p_save;
4975 char delim[] = ", ";
4977 /* tokenize the value into individual hints */
4978 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4980 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4981 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4982 SET_HINT(window.hints, HINT_UNDECORATED);
4983 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4984 SET_HINT(window.hints, HINT_BELOW);
4985 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4986 SET_HINT(window.hints, HINT_ABOVE);
4987 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4988 SET_HINT(window.hints, HINT_STICKY);
4989 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4990 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4991 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4992 SET_HINT(window.hints, HINT_SKIP_PAGER);
4997 p_hint = strtok_r(NULL, delim, &p_save);
4998 } while (p_hint != NULL);
5004 CONF("own_window_type") {
5006 if (strncmp(value, "normal", 6) == EQUAL) {
5007 window.type = TYPE_NORMAL;
5008 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5009 window.type = TYPE_DESKTOP;
5010 } else if (strncmp(value, "dock", 4) == EQUAL) {
5011 window.type = TYPE_DOCK;
5012 text_alignment = TOP_LEFT;
5013 } else if (strncmp(value, "panel", 5) == EQUAL) {
5014 window.type = TYPE_PANEL;
5015 } else if (strncmp(value, "override", 8) == EQUAL) {
5016 window.type = TYPE_OVERRIDE;
5025 CONF("own_window_argb_visual") {
5026 use_argb_visual = string_to_bool(value);
5028 #endif /* USE_ARGB */
5030 CONF("stippled_borders") {
5032 stippled_borders = strtol(value, 0, 0);
5034 stippled_borders = 4;
5038 CONF("imlib_cache_size") {
5040 cimlib_set_cache_size(atoi(value));
5043 CONF("imlib_cache_flush_interval") {
5045 cimlib_set_cache_flush_interval(atoi(value));
5050 CONF("update_interval_on_battery") {
5052 update_interval_bat = strtod(value, 0);
5057 CONF("update_interval") {
5059 set_update_interval(strtod(value, 0));
5063 if (info.music_player_interval == 0) {
5064 // default to update_interval
5065 info.music_player_interval = update_interval;
5068 CONF("total_run_times") {
5070 total_run_times = strtod(value, 0);
5076 stuff_in_uppercase = string_to_bool(value);
5078 CONF("max_specials") {
5080 max_specials = atoi(value);
5085 CONF("max_user_text") {
5087 max_user_text = atoi(value);
5092 CONF("text_buffer_size") {
5094 text_buffer_size = atoi(value);
5095 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5096 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5097 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5105 if (output_methods & TO_X) {
5106 X11_initialisation();
5115 global_text = (char *) malloc(1);
5116 global_text[0] = '\0';
5119 unsigned int l = strlen(global_text);
5121 char buf[CONF_BUFF_SIZE];
5123 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5129 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5137 /* Check for continuation of \\-\n. */
5138 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5139 global_text[l-1] = '\0';
5143 global_text = (char *) realloc(global_text, l + bl + 1);
5144 strcat(global_text, buf);
5146 if (strlen(global_text) > max_user_text) {
5150 global_text_lines = line + 1;
5153 #ifdef TCP_PORT_MONITOR
5154 CONF("max_port_monitor_connections") {
5156 if (!value || (sscanf(value, "%d", &max) != 1)) {
5157 /* an error. use default, warn and continue. */
5158 tcp_portmon_set_max_connections(0);
5160 } else if (tcp_portmon_set_max_connections(max)) {
5161 /* max is < 0, default has been set*/
5166 CONF("if_up_strictness") {
5168 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5169 ifup_strictness = IFUP_UP;
5170 } else if (strcasecmp(value, "up") == EQUAL) {
5171 ifup_strictness = IFUP_UP;
5172 } else if (strcasecmp(value, "link") == EQUAL) {
5173 ifup_strictness = IFUP_LINK;
5174 } else if (strcasecmp(value, "address") == EQUAL) {
5175 ifup_strictness = IFUP_ADDR;
5177 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5178 ifup_strictness = IFUP_UP;
5182 CONF("temperature_unit") {
5184 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5185 } else if (set_temp_output_unit(value)) {
5186 NORM_ERR("temperature_unit: incorrect argument");
5193 char *ptr = strtok(value, " ");
5196 ptr = strtok(NULL, " ");
5203 CONF("lua_draw_hook_pre") {
5205 llua_set_draw_pre_hook(value);
5210 CONF("lua_draw_hook_post") {
5212 llua_set_draw_post_hook(value);
5217 CONF("lua_startup_hook") {
5219 llua_set_startup_hook(value);
5224 CONF("lua_shutdown_hook") {
5226 llua_set_shutdown_hook(value);
5232 #endif /* HAVE_LUA */
5244 CONF("default_color"){}
5245 CONF3("default_shade_color", "default_shadecolor"){}
5246 CONF3("default_outline_color", "default_outlinecolor") {}
5247 CONF("own_window_colour") {}
5250 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5256 if (info.music_player_interval == 0) {
5257 // default to update_interval
5258 info.music_player_interval = update_interval;
5260 if (!global_text) { // didn't supply any text
5261 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5263 if (!output_methods) {
5264 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5266 #if defined(NCURSES)
5268 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5269 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5270 output_methods &= ~TO_NCURSES;
5274 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5275 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5276 output_methods &= ~(TO_STDOUT | TO_STDERR);
5278 #endif /* NCURSES */
5283 static void load_config_file_x11(const char *f)
5288 fp = open_config_file(f);
5292 DBGP("reading contents from config file '%s'", f);
5295 char buff[CONF_BUFF_SIZE], *name, *value;
5296 int ret = do_config_step(&line, fp, buff, &name, &value);
5297 if (ret == CONF_BREAK) {
5299 } else if (ret == CONF_CONTINUE) {
5304 X11_initialisation();
5305 if (x_initialised == YES) {
5307 color0 = get_x11_color(value);
5314 X11_initialisation();
5315 if (x_initialised == YES) {
5317 color1 = get_x11_color(value);
5324 X11_initialisation();
5325 if (x_initialised == YES) {
5327 color2 = get_x11_color(value);
5334 X11_initialisation();
5335 if (x_initialised == YES) {
5337 color3 = get_x11_color(value);
5344 X11_initialisation();
5345 if (x_initialised == YES) {
5347 color4 = get_x11_color(value);
5354 X11_initialisation();
5355 if (x_initialised == YES) {
5357 color5 = get_x11_color(value);
5364 X11_initialisation();
5365 if (x_initialised == YES) {
5367 color6 = get_x11_color(value);
5374 X11_initialisation();
5375 if (x_initialised == YES) {
5377 color7 = get_x11_color(value);
5384 X11_initialisation();
5385 if (x_initialised == YES) {
5387 color8 = get_x11_color(value);
5394 X11_initialisation();
5395 if (x_initialised == YES) {
5397 color9 = get_x11_color(value);
5403 CONF("default_color") {
5404 X11_initialisation();
5405 if (x_initialised == YES) {
5407 default_fg_color = get_x11_color(value);
5413 CONF3("default_shade_color", "default_shadecolor") {
5414 X11_initialisation();
5415 if (x_initialised == YES) {
5417 default_bg_color = get_x11_color(value);
5423 CONF3("default_outline_color", "default_outlinecolor") {
5424 X11_initialisation();
5425 if (x_initialised == YES) {
5427 default_out_color = get_x11_color(value);
5434 CONF("own_window_colour") {
5435 X11_initialisation();
5436 if (x_initialised == YES) {
5438 background_colour = get_x11_color(value);
5440 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5441 "'#' for hex colours");
5447 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5448 if(output_methods & TO_X) {
5449 X11_initialisation();
5458 #undef CONF_CONTINUE
5459 #undef CONF_BUFF_SIZE
5467 static void print_help(const char *prog_name) {
5468 printf("Usage: %s [OPTION]...\n"
5469 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5470 "window. Command line options will override configurations defined in config\n"
5472 " -v, --version version\n"
5473 " -q, --quiet quiet mode\n"
5474 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5475 " -c, --config=FILE config file to load\n"
5476 #ifdef CONFIG_OUTPUT
5477 " -C, --print-config print the builtin default config to stdout\n"
5478 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5480 " -d, --daemonize daemonize, fork to background\n"
5481 " -h, --help help\n"
5483 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5484 " -f, --font=FONT font to use\n"
5485 " -X, --display=DISPLAY X11 display to use\n"
5487 " -o, --own-window create own window to draw\n"
5490 " -b, --double-buffer double buffer (prevents flickering)\n"
5492 " -w, --window-id=WIN_ID window id to draw\n"
5493 " -x X x position\n"
5494 " -y Y y position\n"
5496 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5497 " -u, --interval=SECS update interval\n"
5498 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5499 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5504 /* : means that character before that takes an argument */
5505 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5515 #ifdef CONFIG_OUTPUT
5520 static const struct option longopts[] = {
5521 { "help", 0, NULL, 'h' },
5522 { "version", 0, NULL, 'V' },
5523 { "debug", 0, NULL, 'D' },
5524 { "config", 1, NULL, 'c' },
5525 #ifdef CONFIG_OUTPUT
5526 { "print-config", 0, NULL, 'C' },
5528 { "daemonize", 0, NULL, 'd' },
5530 { "alignment", 1, NULL, 'a' },
5531 { "font", 1, NULL, 'f' },
5532 { "display", 1, NULL, 'X' },
5534 { "own-window", 0, NULL, 'o' },
5537 { "double-buffer", 0, NULL, 'b' },
5539 { "window-id", 1, NULL, 'w' },
5541 { "text", 1, NULL, 't' },
5542 { "interval", 0, NULL, 'u' },
5543 { "pause", 0, NULL, 'p' },
5547 void initialisation(int argc, char **argv) {
5548 struct sigaction act, oact;
5550 set_default_configurations();
5551 load_config_file(current_config);
5552 currentconffile = conftree_add(currentconffile, current_config);
5554 /* init specials array */
5555 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5556 NORM_ERR("failed to create specials array");
5560 if (current_mail_spool == NULL) {
5563 variable_substitute(MAIL_FILE, buf, 256);
5565 if (buf[0] != '\0') {
5566 current_mail_spool = strndup(buf, text_buffer_size);
5571 /* handle other command line arguments */
5573 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5574 || defined(__NetBSD__)
5575 optind = optreset = 1;
5580 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5581 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5582 "kvm_open")) == NULL) {
5583 CRIT_ERR(NULL, NULL, "cannot read kvm");
5588 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5597 fork_to_background = 1;
5600 global_debug_level++;
5604 set_first_font(optarg);
5607 text_alignment = string_to_alignment(optarg);
5626 global_text = strndup(optarg, max_user_text);
5627 convert_escapes(global_text);
5631 update_interval = strtod(optarg, 0);
5632 update_interval_old = update_interval;
5633 if (info.music_player_interval == 0) {
5634 // default to update_interval
5635 info.music_player_interval = update_interval;
5640 total_run_times = strtod(optarg, 0);
5644 gap_x = atoi(optarg);
5648 gap_y = atoi(optarg);
5652 startup_pause = atoi(optarg);
5653 sleep(startup_pause);
5663 if (output_methods & TO_X) {
5664 load_config_file_x11(current_config);
5668 /* generate text and get initial size */
5669 extract_variable_text(global_text);
5676 if (fork_to_background) {
5681 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5688 fprintf(stderr, "\n");
5693 /* parent process */
5694 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5701 start_update_threading();
5703 text_buffer = malloc(max_user_text);
5704 memset(text_buffer, 0, max_user_text);
5705 tmpstring1 = malloc(text_buffer_size);
5706 memset(tmpstring1, 0, text_buffer_size);
5707 tmpstring2 = malloc(text_buffer_size);
5708 memset(tmpstring2, 0, text_buffer_size);
5713 X11_create_window();
5716 llua_setup_info(&info, update_interval);
5717 #endif /* HAVE_LUA */
5722 /* Set signal handlers */
5723 act.sa_handler = signal_handler;
5724 sigemptyset(&act.sa_mask);
5727 act.sa_flags |= SA_RESTART;
5730 if ( sigaction(SIGINT, &act, &oact) < 0
5731 || sigaction(SIGALRM, &act, &oact) < 0
5732 || sigaction(SIGUSR1, &act, &oact) < 0
5733 || sigaction(SIGHUP, &act, &oact) < 0
5734 || sigaction(SIGTERM, &act, &oact) < 0) {
5735 NORM_ERR("error setting signal handler: %s", strerror(errno));
5739 llua_startup_hook();
5740 #endif /* HAVE_LUA */
5743 int main(int argc, char **argv)
5752 g_signal_pending = 0;
5753 max_user_text = MAX_USER_TEXT_DEFAULT;
5755 memset(&info, 0, sizeof(info));
5759 #ifdef TCP_PORT_MONITOR
5760 /* set default connection limit */
5761 tcp_portmon_set_max_connections(0);
5764 /* handle command line parameters that don't change configs */
5766 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5767 || ((s = getenv("LANG")) && *s)) {
5768 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5770 NORM_ERR("malloc failed");
5772 for (x = 0; x < strlen(s); x++) {
5773 temp[x] = tolower(s[x]);
5776 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5782 if (!setlocale(LC_CTYPE, "")) {
5783 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5787 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5798 if (current_config) {
5799 free(current_config);
5801 current_config = strndup(optarg, max_user_text);
5804 freopen("/dev/null", "w", stderr);
5807 print_help(argv[0]);
5809 #ifdef CONFIG_OUTPUT
5816 window.window = strtol(optarg, 0, 0);
5821 disp = strdup(optarg);
5830 /* check if specified config file is valid */
5831 if (current_config) {
5833 if (stat(current_config, &sb) ||
5834 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5835 NORM_ERR("invalid configuration file '%s'\n", current_config);
5836 free(current_config);
5841 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5843 if (!current_config) {
5844 /* load default config file */
5845 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5848 /* Try to use personal config file first */
5849 to_real_path(buf, CONFIG_FILE);
5850 if (buf[0] && (fp = fopen(buf, "r"))) {
5851 current_config = strndup(buf, max_user_text);
5855 /* Try to use system config file if personal config not readable */
5856 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5857 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5861 /* No readable config found */
5862 if (!current_config) {
5863 #ifdef CONFIG_OUTPUT
5864 current_config = strdup("==builtin==");
5865 NORM_ERR("no readable personal or system-wide config file found,"
5866 " using builtin default");
5868 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5869 #endif /* ! CONF_OUTPUT */
5874 /* Load xoap keys, if existing */
5878 #ifdef HAVE_SYS_INOTIFY_H
5879 inotify_fd = inotify_init1(IN_NONBLOCK);
5880 #endif /* HAVE_SYS_INOTIFY_H */
5882 initialisation(argc, argv);
5886 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5894 void alarm_handler(void) {
5896 kill(childpid, SIGTERM);
5900 static void signal_handler(int sig)
5902 /* signal handler is light as a feather, as it should be.
5903 * we will poll g_signal_pending with each loop of conky
5904 * and do any signal processing there, NOT here (except
5905 * SIGALRM because this is caused when conky is hanging) */
5906 if(sig == SIGALRM) {
5909 g_signal_pending = sig;