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-2010 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 DBGP2("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)
2623 int contain_SECRIT_MULTILINE_CHAR = 0;
2626 if ((output_methods & TO_X) == 0)
2628 /* get string widths and skip specials */
2631 if (*p == SPECIAL_CHAR) {
2633 w += get_string_width(s);
2636 if (specials[special_index].type == BAR
2637 || specials[special_index].type == GAUGE
2638 || specials[special_index].type == GRAPH) {
2639 w += specials[special_index].width;
2640 if (specials[special_index].height > last_font_height) {
2641 last_font_height = specials[special_index].height;
2642 last_font_height += font_height();
2644 } else if (specials[special_index].type == OFFSET) {
2645 if (specials[special_index].arg > 0) {
2646 w += specials[special_index].arg;
2648 } else if (specials[special_index].type == VOFFSET) {
2649 last_font_height += specials[special_index].arg;
2650 } else if (specials[special_index].type == GOTO) {
2651 if (specials[special_index].arg > cur_x) {
2652 w = (int) specials[special_index].arg;
2654 } else if (specials[special_index].type == TAB) {
2655 int start = specials[special_index].arg;
2656 int step = specials[special_index].width;
2658 if (!step || step < 0) {
2661 w += step - (cur_x - text_start_x - start) % step;
2662 } else if (specials[special_index].type == FONT) {
2663 selected_font = specials[special_index].font_added;
2664 if (font_height() > last_font_height) {
2665 last_font_height = font_height();
2671 } else if (*p == SECRIT_MULTILINE_CHAR) {
2672 contain_SECRIT_MULTILINE_CHAR = 1;
2674 lw = get_string_width(s);
2675 *p = SECRIT_MULTILINE_CHAR;
2677 w = lw > w ? lw : w;
2678 text_height += last_font_height;
2682 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2683 if (contain_SECRIT_MULTILINE_CHAR) {
2684 lw = get_string_width(s);
2685 w = lw > w ? lw : w;
2687 w += get_string_width(s);
2689 if (w > text_width) {
2692 if (text_width > maximum_width && maximum_width) {
2693 text_width = maximum_width;
2696 text_height += last_font_height;
2697 last_font_height = font_height();
2698 return special_index;
2702 static inline void set_foreground_color(long c)
2705 if (output_methods & TO_X) {
2707 if (have_argb_visual) {
2708 current_color = c | (own_window_argb_value << 24);
2710 #endif /* USE_ARGB */
2714 #endif /* USE_ARGB */
2715 XSetForeground(display, window.gc, current_color);
2719 if (output_methods & TO_NCURSES) {
2720 attron(COLOR_PAIR(c));
2722 #endif /* NCURSES */
2727 static void draw_string(const char *s)
2729 int i, i2, pos, width_of_s;
2732 char *s_with_newlines;
2738 width_of_s = get_string_width(s);
2739 s_with_newlines = strdup(s);
2740 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2741 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2742 s_with_newlines[i] = '\n';
2745 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2746 printf("%s\n", s_with_newlines);
2747 if (extra_newline) fputc('\n', stdout);
2748 fflush(stdout); /* output immediately, don't buffer */
2750 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2751 fprintf(stderr, "%s\n", s_with_newlines);
2752 fflush(stderr); /* output immediately, don't buffer */
2754 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2755 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2757 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2758 fprintf(append_fpointer, "%s\n", s_with_newlines);
2761 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2762 printw("%s", s_with_newlines);
2765 free(s_with_newlines);
2766 memset(tmpstring1, 0, text_buffer_size);
2767 memset(tmpstring2, 0, text_buffer_size);
2768 strncpy(tmpstring1, s, text_buffer_size - 1);
2773 if (output_methods & TO_X) {
2774 max = ((text_width - width_of_s) / get_string_width(" "));
2777 /* This code looks for tabs in the text and coverts them to spaces.
2778 * The trick is getting the correct number of spaces, and not going
2779 * over the window's size without forcing the window larger. */
2780 for (i = 0; i < (int) text_buffer_size; i++) {
2781 if (tmpstring1[i] == '\t') {
2783 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2784 /* guard against overrun */
2785 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2790 /* guard against overrun */
2791 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2796 if (output_methods & TO_X) {
2797 if (text_width == maximum_width) {
2798 /* this means the text is probably pushing the limit,
2799 * so we'll chop it */
2800 while (cur_x + get_string_width(tmpstring2) - text_start_x
2801 > maximum_width && strlen(tmpstring2) > 0) {
2802 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2809 if (output_methods & TO_X) {
2815 c.pixel = current_color;
2816 // query color on custom colormap
2817 XQueryColor(display, window.colourmap, &c);
2820 c2.color.red = c.red;
2821 c2.color.green = c.green;
2822 c2.color.blue = c.blue;
2823 c2.color.alpha = fonts[selected_font].font_alpha;
2825 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2826 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2828 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2829 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2834 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2837 cur_x += width_of_s;
2840 memcpy(tmpstring1, s, text_buffer_size);
2843 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2851 int last_special_needed = -1;
2852 int orig_special_index = special_index;
2855 if (output_methods & TO_X) {
2856 font_h = font_height();
2857 cur_y += font_ascent();
2859 cur_x = text_start_x;
2863 if (*p == SECRIT_MULTILINE_CHAR) {
2864 /* special newline marker for multiline objects */
2869 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2874 /* draw string before special, unless we're dealing multiline
2876 if (last_special_applied > -1) {
2877 special_index = last_special_applied;
2885 switch (specials[special_index].type) {
2887 case HORIZONTAL_LINE:
2889 int h = specials[special_index].height;
2890 int mid = font_ascent() / 2;
2892 w = text_start_x + text_width - cur_x;
2894 XSetLineAttributes(display, window.gc, h, LineSolid,
2895 CapButt, JoinMiter);
2896 XDrawLine(display, window.drawable, window.gc, cur_x,
2897 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2903 int h = specials[special_index].height;
2904 int tmp_s = specials[special_index].arg;
2905 int mid = font_ascent() / 2;
2906 char ss[2] = { tmp_s, tmp_s };
2908 w = text_start_x + text_width - cur_x - 1;
2909 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2910 CapButt, JoinMiter);
2911 XSetDashes(display, window.gc, 0, ss, 2);
2912 XDrawLine(display, window.drawable, window.gc, cur_x,
2913 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2919 int h, bar_usage, by;
2920 if (cur_x - text_start_x > maximum_width
2921 && maximum_width > 0) {
2924 h = specials[special_index].height;
2925 bar_usage = specials[special_index].arg;
2926 by = cur_y - (font_ascent() / 2) - 1;
2931 w = specials[special_index].width;
2933 w = text_start_x + text_width - cur_x - 1;
2939 XSetLineAttributes(display, window.gc, 1, LineSolid,
2940 CapButt, JoinMiter);
2942 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2944 XFillRectangle(display, window.drawable, window.gc, cur_x,
2945 by, w * bar_usage / 255, h);
2953 case GAUGE: /* new GAUGE */
2956 unsigned long last_colour = current_color;
2958 float angle, px, py;
2962 if (cur_x - text_start_x > maximum_width
2963 && maximum_width > 0) {
2967 h = specials[special_index].height;
2968 by = cur_y - (font_ascent() / 2) - 1;
2973 w = specials[special_index].width;
2975 w = text_start_x + text_width - cur_x - 1;
2981 XSetLineAttributes(display, window.gc, 1, LineSolid,
2982 CapButt, JoinMiter);
2984 XDrawArc(display, window.drawable, window.gc,
2985 cur_x, by, w, h * 2, 0, 180*64);
2988 usage = specials[special_index].arg;
2989 angle = (M_PI)*(float)(usage)/255.;
2990 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2991 py = (float)(by+(h))-(float)(h)*sin(angle);
2993 XDrawLine(display, window.drawable, window.gc,
2994 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3002 set_foreground_color(last_colour);
3010 int h, by, i = 0, j = 0;
3012 unsigned long last_colour = current_color;
3013 unsigned long *tmpcolour = 0;
3014 if (cur_x - text_start_x > maximum_width
3015 && maximum_width > 0) {
3018 h = specials[special_index].height;
3019 by = cur_y - (font_ascent() / 2) - 1;
3024 w = specials[special_index].width;
3026 w = text_start_x + text_width - cur_x - 1;
3031 if (draw_graph_borders) {
3032 XSetLineAttributes(display, window.gc, 1, LineSolid,
3033 CapButt, JoinMiter);
3034 XDrawRectangle(display, window.drawable, window.gc,
3037 XSetLineAttributes(display, window.gc, 1, LineSolid,
3038 CapButt, JoinMiter);
3040 if (specials[special_index].last_colour != 0
3041 || specials[special_index].first_colour != 0) {
3042 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3045 for (i = w - 2; i > -1; i--) {
3046 if (specials[special_index].last_colour != 0
3047 || specials[special_index].first_colour != 0) {
3048 if (specials[special_index].tempgrad) {
3051 (int)((float)(w - 2) - specials[special_index].graph[j] *
3052 (w - 2) / (float)specials[special_index].graph_scale)
3056 (int)((float)(w - 2) - specials[special_index].graph[j] *
3057 (w - 2) / (float)specials[special_index].graph_scale)
3060 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3062 (int)((float)(w - 2) - specials[special_index].graph[j] *
3063 (w - 2) / (float)specials[special_index].graph_scale)
3067 #endif /* DEBUG_lol */
3068 XSetForeground(display, window.gc, tmpcolour[
3069 (int)((float)(w - 2) - specials[special_index].graph[j] *
3070 (w - 2) / (float)specials[special_index].graph_scale)
3073 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3076 /* this is mugfugly, but it works */
3077 XDrawLine(display, window.drawable, window.gc,
3078 cur_x + i + 1, by + h, cur_x + i + 1,
3079 round_to_int((double)by + h - specials[special_index].graph[j] *
3080 (h - 1) / specials[special_index].graph_scale));
3081 if ((w - i) / ((float) (w - 2) /
3082 (specials[special_index].graph_width)) > j
3083 && j < MAX_GRAPH_DEPTH - 3) {
3087 if (tmpcolour) free(tmpcolour);
3092 /* if (draw_mode == BG) {
3093 set_foreground_color(default_bg_color);
3094 } else if (draw_mode == OUTLINE) {
3095 set_foreground_color(default_out_color);
3097 set_foreground_color(default_fg_color);
3099 if (show_graph_range) {
3102 unsigned short int seconds = update_interval * w;
3108 unsigned short int timeunits;
3110 timeunits = seconds / 86400; seconds %= 86400;
3111 if (timeunits > 0) {
3112 asprintf(&tmp_day_str, "%dd", timeunits);
3114 tmp_day_str = strdup("");
3116 timeunits = seconds / 3600; seconds %= 3600;
3117 if (timeunits > 0) {
3118 asprintf(&tmp_hour_str, "%dh", timeunits);
3120 tmp_hour_str = strdup("");
3122 timeunits = seconds / 60; seconds %= 60;
3123 if (timeunits > 0) {
3124 asprintf(&tmp_min_str, "%dm", timeunits);
3126 tmp_min_str = strdup("");
3129 asprintf(&tmp_sec_str, "%ds", seconds);
3131 tmp_sec_str = strdup("");
3133 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3134 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3136 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3138 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3139 cur_y += font_h / 2;
3140 draw_string(tmp_str);
3146 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3150 cur_x += font_ascent() / 2;
3151 cur_y += font_h / 2;
3153 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3155 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3156 draw_string(tmp_str);
3162 set_foreground_color(last_colour);
3168 int old = font_ascent();
3170 cur_y -= font_ascent();
3171 selected_font = specials[special_index].font_added;
3173 if (cur_y + font_ascent() < cur_y + old) {
3176 cur_y += font_ascent();
3178 font_h = font_height();
3183 if (draw_mode == FG) {
3184 set_foreground_color(specials[special_index].arg);
3190 if (draw_mode == BG) {
3191 set_foreground_color(specials[special_index].arg);
3196 if (draw_mode == OUTLINE) {
3197 set_foreground_color(specials[special_index].arg);
3202 w += specials[special_index].arg;
3203 last_special_needed = special_index;
3207 cur_y += specials[special_index].arg;
3211 if (specials[special_index].arg >= 0) {
3212 cur_x = (int) specials[special_index].arg;
3214 last_special_needed = special_index;
3219 int start = specials[special_index].arg;
3220 int step = specials[special_index].width;
3222 if (!step || step < 0) {
3225 w = step - (cur_x - text_start_x - start) % step;
3226 last_special_needed = special_index;
3232 /* TODO: add back in "+ window.border_inner_margin" to the end of
3234 int pos_x = text_start_x + text_width -
3235 get_string_width_special(s, special_index);
3237 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3238 "get_string_width(p) %i gap_x %i "
3239 "specials[special_index].arg %i window.border_inner_margin %i "
3240 "window.border_width %i\n", pos_x, text_start_x, text_width,
3241 cur_x, get_string_width_special(s), gap_x,
3242 specials[special_index].arg, window.border_inner_margin,
3243 window.border_width); */
3244 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3245 cur_x = pos_x - specials[special_index].arg;
3247 last_special_needed = special_index;
3253 int pos_x = (text_width) / 2 - get_string_width_special(s,
3254 special_index) / 2 - (cur_x -
3256 /* int pos_x = text_start_x + text_width / 2 -
3257 get_string_width_special(s) / 2; */
3259 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3260 "get_string_width(p) %i gap_x %i "
3261 "specials[special_index].arg %i\n", pos_x, text_start_x,
3262 text_width, cur_x, get_string_width(s), gap_x,
3263 specials[special_index].arg); */
3264 if (pos_x > specials[special_index].arg) {
3265 w = pos_x - specials[special_index].arg;
3267 last_special_needed = special_index;
3277 if (special_index != last_special_applied) {
3280 special_index = orig_special_index;
3281 last_special_applied = -1;
3292 if (output_methods & TO_NCURSES) {
3295 #endif /* NCURSES */
3297 if (output_methods & TO_X)
3298 cur_y += font_descent();
3300 if (recurse && *recurse) {
3301 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3302 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3304 return special_index;
3307 static int draw_line(char *s, int special_index)
3310 if (output_methods & TO_X) {
3311 return draw_each_line_inner(s, special_index, -1);
3315 if (output_methods & TO_NCURSES) {
3316 return draw_each_line_inner(s, special_index, -1);
3318 #endif /* NCURSES */
3320 UNUSED(special_index);
3324 static void draw_text(void)
3328 llua_draw_pre_hook();
3329 #endif /* HAVE_LUA */
3330 if (output_methods & TO_X) {
3331 cur_y = text_start_y;
3334 if (draw_borders && window.border_width > 0) {
3335 if (stippled_borders) {
3336 char ss[2] = { stippled_borders, stippled_borders };
3337 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3338 CapButt, JoinMiter);
3339 XSetDashes(display, window.gc, 0, ss, 2);
3341 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3342 CapButt, JoinMiter);
3345 XDrawRectangle(display, window.drawable, window.gc,
3346 text_start_x - window.border_inner_margin - window.border_width,
3347 text_start_y - window.border_inner_margin - window.border_width,
3348 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3349 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3357 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3358 attron(COLOR_PAIR(COLOR_WHITE));
3359 #endif /* NCURSES */
3360 for_each_line(text_buffer, draw_line);
3361 #if defined(HAVE_LUA) && defined(X11)
3362 llua_draw_post_hook();
3363 #endif /* HAVE_LUA */
3366 static void draw_stuff(void)
3369 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3371 if (overwrite_file) {
3372 overwrite_fpointer = fopen(overwrite_file, "w");
3373 if(!overwrite_fpointer)
3374 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3377 append_fpointer = fopen(append_file, "a");
3378 if(!append_fpointer)
3379 NORM_ERR("Can't append '%s' anymore", append_file);
3382 if (output_methods & TO_X) {
3384 if (draw_shades && !draw_outline) {
3387 set_foreground_color(default_bg_color);
3398 for (i = -1; i < 2; i++) {
3399 for (j = -1; j < 2; j++) {
3400 if (i == 0 && j == 0) {
3405 set_foreground_color(default_out_color);
3406 draw_mode = OUTLINE;
3414 set_foreground_color(default_fg_color);
3419 #if defined(X11) && defined(HAVE_XDBE)
3420 if (output_methods & TO_X) {
3421 xdbe_swap_buffers();
3423 #endif /* X11 && HAVE_XDBE */
3424 if(overwrite_fpointer) {
3425 fclose(overwrite_fpointer);
3426 overwrite_fpointer = 0;
3428 if(append_fpointer) {
3429 fclose(append_fpointer);
3430 append_fpointer = 0;
3435 static void clear_text(int exposures)
3439 /* The swap action is XdbeBackground, which clears */
3443 if (display && window.window) { // make sure these are !null
3444 /* there is some extra space for borders and outlines */
3445 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3446 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3447 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3448 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3453 static int need_to_update;
3455 /* update_text() generates new text and clears old text area */
3456 static void update_text(void)
3463 if (output_methods & TO_X)
3468 llua_update_info(&info, update_interval);
3469 #endif /* HAVE_LUA */
3472 #ifdef HAVE_SYS_INOTIFY_H
3476 static void main_loop(void)
3479 #ifdef SIGNAL_BLOCKING
3480 sigset_t newmask, oldmask;
3483 #ifdef HAVE_SYS_INOTIFY_H
3484 int inotify_config_wd = -1;
3485 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3486 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3487 char inotify_buff[INOTIFY_BUF_LEN];
3488 #endif /* HAVE_SYS_INOTIFY_H */
3491 #ifdef SIGNAL_BLOCKING
3492 sigemptyset(&newmask);
3493 sigaddset(&newmask, SIGINT);
3494 sigaddset(&newmask, SIGTERM);
3495 sigaddset(&newmask, SIGUSR1);
3498 last_update_time = 0.0;
3499 next_update_time = get_time();
3501 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3502 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3503 char buf[max_user_text];
3505 get_battery_short_status(buf, max_user_text, "BAT0");
3507 update_interval = update_interval_bat;
3509 update_interval = update_interval_old;
3514 #ifdef SIGNAL_BLOCKING
3515 /* block signals. we will inspect for pending signals later */
3516 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3517 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3522 if (output_methods & TO_X) {
3525 /* wait for X event or timeout */
3527 if (!XPending(display)) {
3531 t = next_update_time - get_time();
3535 } else if (t > update_interval) {
3536 t = update_interval;
3539 tv.tv_sec = (long) t;
3540 tv.tv_usec = (long) (t * 1000000) % 1000000;
3542 FD_SET(ConnectionNumber(display), &fdsr);
3544 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3546 if (errno != EINTR) {
3547 NORM_ERR("can't select(): %s", strerror(errno));
3557 if (need_to_update) {
3559 int wx = window.x, wy = window.y;
3569 /* resize window if it isn't right size */
3571 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3572 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3573 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3574 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3575 draw_stuff(); /* redraw everything in our newly sized window */
3576 XResizeWindow(display, window.window, window.width,
3577 window.height); /* resize window */
3578 set_transparent_background(window.window, own_window_argb_value);
3581 xdbe_swap_buffers();
3586 /* update lua window globals */
3587 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3588 #endif /* HAVE_LUA */
3591 /* move window if it isn't in right position */
3592 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3593 XMoveWindow(display, window.window, window.x, window.y);
3598 if (changed && window.type == TYPE_PANEL) {
3601 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3604 switch (text_alignment) {
3631 set_struts(sidenum);
3642 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3643 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3644 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3645 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3646 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3651 /* handle X events */
3652 while (XPending(display)) {
3655 XNextEvent(display, &ev);
3662 r.width = ev.xexpose.width;
3663 r.height = ev.xexpose.height;
3664 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3668 case PropertyNotify:
3670 if ( ev.xproperty.state == PropertyNewValue ) {
3671 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3677 case ReparentNotify:
3678 /* make background transparent */
3680 set_transparent_background(window.window, own_window_argb_value);
3684 case ConfigureNotify:
3686 /* if window size isn't what expected, set fixed size */
3687 if (ev.xconfigure.width != window.width
3688 || ev.xconfigure.height != window.height) {
3689 if (window.width != 0 && window.height != 0) {
3693 /* clear old stuff before screwing up
3698 XWindowAttributes attrs;
3699 if (XGetWindowAttributes(display,
3700 window.window, &attrs)) {
3701 window.width = attrs.width;
3702 window.height = attrs.height;
3706 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3707 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3708 if (text_width > maximum_width
3709 && maximum_width > 0) {
3710 text_width = maximum_width;
3714 /* if position isn't what expected, set fixed pos
3715 * total_updates avoids setting fixed_pos when window
3716 * is set to weird locations when started */
3717 /* // this is broken
3718 if (total_updates >= 2 && !fixed_pos
3719 && (window.x != ev.xconfigure.x
3720 || window.y != ev.xconfigure.y)
3721 && (ev.xconfigure.x != 0
3722 || ev.xconfigure.y != 0)) {
3730 /* if an ordinary window with decorations */
3731 if ((window.type == TYPE_NORMAL &&
3732 (!TEST_HINT(window.hints,
3733 HINT_UNDECORATED))) ||
3734 window.type == TYPE_DESKTOP) {
3735 /* allow conky to hold input focus. */
3738 /* forward the click to the desktop window */
3739 XUngrabPointer(display, ev.xbutton.time);
3740 ev.xbutton.window = window.desktop;
3741 ev.xbutton.x = ev.xbutton.x_root;
3742 ev.xbutton.y = ev.xbutton.y_root;
3743 XSendEvent(display, ev.xbutton.window, False,
3744 ButtonPressMask, &ev);
3745 XSetInputFocus(display, ev.xbutton.window,
3746 RevertToParent, ev.xbutton.time);
3753 /* if an ordinary window with decorations */
3754 if ((window.type == TYPE_NORMAL)
3755 && (!TEST_HINT(window.hints,
3756 HINT_UNDECORATED))) {
3757 /* allow conky to hold input focus. */
3760 /* forward the release to the desktop window */
3761 ev.xbutton.window = window.desktop;
3762 ev.xbutton.x = ev.xbutton.x_root;
3763 ev.xbutton.y = ev.xbutton.y_root;
3764 XSendEvent(display, ev.xbutton.window, False,
3765 ButtonReleaseMask, &ev);
3774 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3775 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3777 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3778 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3780 #endif /* HAVE_XDAMAGE */
3786 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3787 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3788 #endif /* HAVE_XDAMAGE */
3790 /* XDBE doesn't seem to provide a way to clear the back buffer
3791 * without interfering with the front buffer, other than passing
3792 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3793 * using XDBE, we need to redraw the text even if it wasn't part of
3794 * the exposed area. OTOH, if we're not going to call draw_stuff at
3795 * all, then no swap happens and we can safely do nothing. */
3797 if (!XEmptyRegion(x11_stuff.region)) {
3802 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3803 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3804 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3805 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3806 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3809 XSetRegion(display, window.gc, x11_stuff.region);
3812 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3816 XDestroyRegion(x11_stuff.region);
3817 x11_stuff.region = XCreateRegion();
3821 t = (next_update_time - get_time()) * 1000000;
3822 if(t > 0) usleep((useconds_t)t);
3826 if(output_methods & TO_NCURSES) {
3835 #ifdef SIGNAL_BLOCKING
3836 /* unblock signals of interest and let handler fly */
3837 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3838 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3842 switch (g_signal_pending) {
3845 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3850 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3853 if (output_methods & TO_X) {
3854 XDestroyRegion(x11_stuff.region);
3855 x11_stuff.region = NULL;
3857 XDamageDestroy(display, x11_stuff.damage);
3858 XFixesDestroyRegion(display, x11_stuff.region2);
3859 XFixesDestroyRegion(display, x11_stuff.part);
3860 #endif /* HAVE_XDAMAGE */
3866 if(overwrite_file) {
3867 free(overwrite_file);
3876 /* Reaching here means someone set a signal
3877 * (SIGXXXX, signal_handler), but didn't write any code
3879 * If you don't want to handle a signal, don't set a handler on
3880 * it in the first place. */
3881 if (g_signal_pending) {
3882 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3886 #ifdef HAVE_SYS_INOTIFY_H
3887 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3888 inotify_config_wd = inotify_add_watch(inotify_fd,
3892 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3893 int len = 0, idx = 0;
3895 struct timeval time_to_wait;
3897 FD_ZERO(&descriptors);
3898 FD_SET(inotify_fd, &descriptors);
3900 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3902 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3903 if (FD_ISSET(inotify_fd, &descriptors)) {
3904 /* process inotify events */
3905 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3906 while (len > 0 && idx < len) {
3907 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3908 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3909 /* current_config should be reloaded */
3910 NORM_ERR("'%s' modified, reloading...", current_config);
3912 if (ev->mask & IN_IGNORED) {
3913 /* for some reason we get IN_IGNORED here
3914 * sometimes, so we need to re-add the watch */
3915 inotify_config_wd = inotify_add_watch(inotify_fd,
3922 llua_inotify_query(ev->wd, ev->mask);
3924 #endif /* HAVE_LUA */
3925 idx += INOTIFY_EVENT_SIZE + ev->len;
3929 #endif /* HAVE_SYS_INOTIFY_H */
3932 llua_update_info(&info, update_interval);
3933 #endif /* HAVE_LUA */
3934 g_signal_pending = 0;
3936 clean_up(NULL, NULL);
3938 #ifdef HAVE_SYS_INOTIFY_H
3939 if (inotify_fd != -1) {
3940 inotify_rm_watch(inotify_fd, inotify_config_wd);
3942 inotify_fd = inotify_config_wd = 0;
3944 #endif /* HAVE_SYS_INOTIFY_H */
3948 static void load_config_file_x11(const char *);
3950 void initialisation(int argc, char** argv);
3952 /* reload the config file */
3953 static void reload_config(void)
3955 char *current_config_copy = strdup(current_config);
3956 clean_up(NULL, NULL);
3957 current_config = current_config_copy;
3958 initialisation(argc_copy, argv_copy);
3961 void clean_up(void *memtofree1, void* memtofree2)
3966 if(output_methods & TO_NCURSES) {
3970 conftree_empty(currentconffile);
3971 currentconffile = NULL;
3978 timed_thread_destroy_registered_threads();
3980 if (info.cpu_usage) {
3981 free(info.cpu_usage);
3982 info.cpu_usage = NULL;
3985 if (x_initialised == YES) {
3986 if(window_created == 1) {
3987 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3988 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3989 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3990 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3994 if(x11_stuff.region) {
3995 XDestroyRegion(x11_stuff.region);
3996 x11_stuff.region = NULL;
3998 XCloseDisplay(display);
4000 if(info.x11.desktop.all_names) {
4001 free(info.x11.desktop.all_names);
4002 info.x11.desktop.all_names = NULL;
4004 if (info.x11.desktop.name) {
4005 free(info.x11.desktop.name);
4006 info.x11.desktop.name = NULL;
4010 free(fonts); //in set_default_configurations a font is set but not loaded
4016 free_update_callbacks();
4020 free_text_objects(&global_root_object, 0);
4039 free(current_config);
4042 #ifdef TCP_PORT_MONITOR
4043 tcp_portmon_clear();
4052 weather_free_info();
4055 llua_shutdown_hook();
4057 #endif /* HAVE_LUA */
4059 if (output_methods & TO_X)
4067 for (i = 0; i < special_count; i++) {
4068 if (specials[i].type == GRAPH) {
4069 free(specials[i].graph);
4077 clear_diskio_stats();
4078 if(global_cpu != NULL) {
4084 static int string_to_bool(const char *s)
4087 // Assumes an option without a true/false means true
4089 } else if (strcasecmp(s, "yes") == EQUAL) {
4091 } else if (strcasecmp(s, "true") == EQUAL) {
4093 } else if (strcasecmp(s, "1") == EQUAL) {
4100 static enum alignment string_to_alignment(const char *s)
4102 if (strcasecmp(s, "top_left") == EQUAL) {
4104 } else if (strcasecmp(s, "top_right") == EQUAL) {
4106 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4108 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4110 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4111 return BOTTOM_RIGHT;
4112 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4113 return BOTTOM_MIDDLE;
4114 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4116 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4117 return MIDDLE_RIGHT;
4118 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4119 return MIDDLE_MIDDLE;
4120 } else if (strcasecmp(s, "tl") == EQUAL) {
4122 } else if (strcasecmp(s, "tr") == EQUAL) {
4124 } else if (strcasecmp(s, "tm") == EQUAL) {
4126 } else if (strcasecmp(s, "bl") == EQUAL) {
4128 } else if (strcasecmp(s, "br") == EQUAL) {
4129 return BOTTOM_RIGHT;
4130 } else if (strcasecmp(s, "bm") == EQUAL) {
4131 return BOTTOM_MIDDLE;
4132 } else if (strcasecmp(s, "ml") == EQUAL) {
4134 } else if (strcasecmp(s, "mr") == EQUAL) {
4135 return MIDDLE_RIGHT;
4136 } else if (strcasecmp(s, "mm") == EQUAL) {
4137 return MIDDLE_MIDDLE;
4138 } else if (strcasecmp(s, "none") == EQUAL) {
4146 static void set_default_configurations_for_x(void)
4148 default_fg_color = WhitePixel(display, screen);
4149 default_bg_color = BlackPixel(display, screen);
4150 default_out_color = BlackPixel(display, screen);
4151 color0 = default_fg_color;
4152 color1 = default_fg_color;
4153 color2 = default_fg_color;
4154 color3 = default_fg_color;
4155 color4 = default_fg_color;
4156 color5 = default_fg_color;
4157 color6 = default_fg_color;
4158 color7 = default_fg_color;
4159 color8 = default_fg_color;
4160 color9 = default_fg_color;
4161 current_text_color = default_fg_color;
4165 static void set_default_configurations(void)
4172 fork_to_background = 0;
4173 total_run_times = 0;
4174 info.cpu_avg_samples = 2;
4175 info.net_avg_samples = 2;
4176 info.diskio_avg_samples = 2;
4181 format_human_readable = 1;
4191 mpd_env_host = getenv("MPD_HOST");
4192 mpd_env_port = getenv("MPD_PORT");
4194 if (!mpd_env_host || !strlen(mpd_env_host)) {
4195 mpd_set_host("localhost");
4197 /* MPD_HOST environment variable is set */
4198 char *mpd_hostpart = strchr(mpd_env_host, '@');
4199 if (!mpd_hostpart) {
4200 mpd_set_host(mpd_env_host);
4202 /* MPD_HOST contains a password */
4203 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4204 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4206 if (!strlen(mpd_hostpart + 1)) {
4207 mpd_set_host("localhost");
4209 mpd_set_host(mpd_hostpart + 1);
4212 mpd_set_password(mpd_password, 1);
4217 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4218 /* failed to set port from environment variable */
4219 mpd_set_port("6600");
4223 info.xmms2.artist = NULL;
4224 info.xmms2.album = NULL;
4225 info.xmms2.title = NULL;
4226 info.xmms2.genre = NULL;
4227 info.xmms2.comment = NULL;
4228 info.xmms2.url = NULL;
4229 info.xmms2.status = NULL;
4230 info.xmms2.playlist = NULL;
4232 use_spacer = NO_SPACER;
4234 output_methods = TO_X;
4236 output_methods = TO_STDOUT;
4239 show_graph_scale = 0;
4240 show_graph_range = 0;
4243 draw_graph_borders = 1;
4245 set_first_font("6x10");
4253 window.type = TYPE_NORMAL;
4255 strcpy(window.class_name, PACKAGE_NAME);
4256 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4258 use_argb_visual = 0;
4259 own_window_argb_value = 255;
4262 stippled_borders = 0;
4263 window.border_inner_margin = 3;
4264 window.border_outer_margin = 1;
4265 window.border_width = 1;
4266 text_alignment = BOTTOM_LEFT;
4267 info.x11.monitor.number = 1;
4268 info.x11.monitor.current = 0;
4269 info.x11.desktop.current = 1;
4270 info.x11.desktop.number = 1;
4271 info.x11.desktop.nitems = 0;
4272 info.x11.desktop.all_names = NULL;
4273 info.x11.desktop.name = NULL;
4278 free(current_mail_spool);
4282 variable_substitute(MAIL_FILE, buf, 256);
4283 if (buf[0] != '\0') {
4284 current_mail_spool = strndup(buf, text_buffer_size);
4289 set_update_interval(3);
4290 update_interval_bat = NOBATTERY;
4291 info.music_player_interval = 1.0;
4292 stuff_in_uppercase = 0;
4293 info.users.number = 1;
4295 set_times_in_seconds(0);
4297 #ifdef TCP_PORT_MONITOR
4298 /* set default connection limit */
4299 tcp_portmon_set_max_connections(0);
4303 /* returns 1 if you can overwrite or create the file at 'path' */
4304 static _Bool overwrite_works(const char *path)
4308 if (!(filepointer = fopen(path, "w")))
4310 fclose(filepointer);
4314 /* returns 1 if you can append or create the file at 'path' */
4315 static _Bool append_works(const char *path)
4319 if (!(filepointer = fopen(path, "a")))
4321 fclose(filepointer);
4327 /* WARNING, this type not in Xlib spec */
4328 int x11_error_handler(Display *d, XErrorEvent *err)
4329 __attribute__((noreturn));
4330 int x11_error_handler(Display *d, XErrorEvent *err)
4332 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",
4334 (long unsigned)err->display,
4335 (long)err->resourceid,
4345 int x11_ioerror_handler(Display *d)
4346 __attribute__((noreturn));
4347 int x11_ioerror_handler(Display *d)
4349 NORM_ERR("X Error: Display %lx\n",
4356 static void X11_initialisation(void)
4358 if (x_initialised == YES) return;
4359 output_methods |= TO_X;
4361 set_default_configurations_for_x();
4362 x_initialised = YES;
4365 /* WARNING, this type not in Xlib spec */
4366 XSetErrorHandler(&x11_error_handler);
4367 XSetIOErrorHandler(&x11_ioerror_handler);
4371 static char **xargv = 0;
4372 static int xargc = 0;
4374 static void X11_create_window(void)
4376 if (output_methods & TO_X) {
4378 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4379 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4381 #else /* OWN_WINDOW */
4382 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4383 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4385 #endif /* OWN_WINDOW */
4389 update_text_area(); /* to position text/window on screen */
4392 if (own_window && !fixed_pos) {
4393 XMoveWindow(display, window.window, window.x, window.y);
4396 set_transparent_background(window.window, own_window_argb_value);
4404 x11_stuff.region = XCreateRegion();
4406 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4407 NORM_ERR("Xdamage extension unavailable");
4409 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4410 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4411 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4412 #endif /* HAVE_XDAMAGE */
4415 update_text_area(); /* to get initial size of the window */
4418 /* setup lua window globals */
4419 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4420 #endif /* HAVE_LUA */
4424 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4425 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4426 #define CONF2(a) if (strcasecmp(name, a) == 0)
4427 #define CONF(a) else CONF2(a)
4428 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4429 || strcasecmp(name, b) == 0)
4430 #define CONF_CONTINUE 1
4431 #define CONF_BREAK 2
4432 #define CONF_BUFF_SIZE 512
4434 static FILE *open_config_file(const char *f)
4436 #ifdef CONFIG_OUTPUT
4437 if (!strcmp(f, "==builtin==")) {
4438 return conf_cookie_open();
4440 #endif /* CONFIG_OUTPUT */
4441 return fopen(f, "r");
4444 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4448 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4451 remove_comments(buf);
4456 while (*p && isspace((int) *p)) {
4460 return CONF_CONTINUE; /* empty line */
4467 while (*p2 && !isspace((int) *p2)) {
4471 *p2 = '\0'; /* break at name's end */
4478 while (*p && isspace((int) *p)) {
4484 p2 = *value + strlen(*value);
4485 while (isspace((int) *(p2 - 1))) {
4494 char load_config_file(const char *f)
4499 fp = open_config_file(f);
4503 DBGP("reading contents from config file '%s'", f);
4506 char buff[CONF_BUFF_SIZE], *name, *value;
4507 int ret = do_config_step(&line, fp, buff, &name, &value);
4508 if (ret == CONF_BREAK) {
4510 } else if (ret == CONF_CONTINUE) {
4516 /* don't listen if X is already initialised or
4517 * if we already know we don't want it */
4518 if(x_initialised != YES) {
4519 if (string_to_bool(value)) {
4520 output_methods &= TO_X;
4522 output_methods &= ~TO_X;
4523 x_initialised = NEVER;
4528 if (!value || x_initialised == YES) {
4533 disp = strdup(value);
4538 if (window.type == TYPE_DOCK)
4541 #endif /*OWN_WINDOW */
4543 int a = string_to_alignment(value);
4554 CONF("background") {
4555 fork_to_background = string_to_bool(value);
4558 CONF2("background") {
4559 fork_to_background = string_to_bool(value);
4563 CONF("show_graph_scale") {
4564 show_graph_scale = string_to_bool(value);
4566 CONF("show_graph_range") {
4567 show_graph_range = string_to_bool(value);
4569 CONF("border_inner_margin") {
4571 window.border_inner_margin = strtol(value, 0, 0);
4572 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4577 CONF("border_outer_margin") {
4579 window.border_outer_margin = strtol(value, 0, 0);
4580 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4585 CONF("border_width") {
4587 window.border_width = strtol(value, 0, 0);
4588 if (window.border_width < 0) window.border_width = 0;
4594 #define TEMPLATE_CONF(n) \
4595 CONF("template"#n) { \
4596 if (set_template(n, value)) \
4611 parse_global_imap_mail_args(value);
4618 parse_global_pop3_mail_args(value);
4623 CONF("default_bar_size") {
4626 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4633 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4637 CONF("default_graph_size") {
4640 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4647 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4650 CONF("default_gauge_size") {
4653 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4660 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4667 mpd_set_host(value);
4673 if (value && mpd_set_port(value)) {
4677 CONF("mpd_password") {
4679 mpd_set_password(value, 0);
4685 CONF("music_player_interval") {
4687 info.music_player_interval = strtod(value, 0);
4693 CONF("sensor_device") {
4695 sensor_device = strtol(value, 0, 0);
4701 CONF("cpu_avg_samples") {
4703 cpu_avg_samples = strtol(value, 0, 0);
4704 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4707 info.cpu_avg_samples = cpu_avg_samples;
4713 CONF("net_avg_samples") {
4715 net_avg_samples = strtol(value, 0, 0);
4716 if (net_avg_samples < 1 || net_avg_samples > 14) {
4719 info.net_avg_samples = net_avg_samples;
4725 CONF("diskio_avg_samples") {
4727 diskio_avg_samples = strtol(value, 0, 0);
4728 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4731 info.diskio_avg_samples = diskio_avg_samples;
4739 CONF("double_buffer") {
4740 use_xdbe = string_to_bool(value);
4744 CONF("override_utf8_locale") {
4745 utf8_mode = string_to_bool(value);
4747 CONF("draw_borders") {
4748 draw_borders = string_to_bool(value);
4750 CONF("draw_graph_borders") {
4751 draw_graph_borders = string_to_bool(value);
4753 CONF("draw_shades") {
4754 draw_shades = string_to_bool(value);
4756 CONF("draw_outline") {
4757 draw_outline = string_to_bool(value);
4760 CONF("times_in_seconds") {
4761 set_times_in_seconds(string_to_bool(value));
4763 CONF("max_text_width") {
4764 max_text_width = atoi(value);
4766 CONF("out_to_console") {
4767 if(string_to_bool(value)) {
4768 output_methods |= TO_STDOUT;
4770 output_methods &= ~TO_STDOUT;
4773 CONF("extra_newline") {
4774 extra_newline = string_to_bool(value);
4776 CONF("out_to_stderr") {
4777 if(string_to_bool(value))
4778 output_methods |= TO_STDERR;
4781 CONF("out_to_ncurses") {
4782 if(string_to_bool(value)) {
4785 output_methods |= TO_NCURSES;
4789 CONF("overwrite_file") {
4790 if(overwrite_file) {
4791 free(overwrite_file);
4794 if(overwrite_works(value)) {
4795 overwrite_file = strdup(value);
4796 output_methods |= OVERWRITE_FILE;
4798 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4800 CONF("append_file") {
4805 if(append_works(value)) {
4806 append_file = strdup(value);
4807 output_methods |= APPEND_FILE;
4809 NORM_ERR("append_file won't be able to create/append '%s'", value);
4811 CONF("use_spacer") {
4813 if (strcasecmp(value, "left") == EQUAL) {
4814 use_spacer = LEFT_SPACER;
4815 } else if (strcasecmp(value, "right") == EQUAL) {
4816 use_spacer = RIGHT_SPACER;
4817 } else if (strcasecmp(value, "none") == EQUAL) {
4818 use_spacer = NO_SPACER;
4820 use_spacer = string_to_bool(value);
4821 NORM_ERR("use_spacer should have an argument of left, right, or"
4822 " none. '%s' seems to be some form of '%s', so"
4823 " defaulting to %s.", value,
4824 use_spacer ? "true" : "false",
4825 use_spacer ? "right" : "none");
4827 use_spacer = RIGHT_SPACER;
4829 use_spacer = NO_SPACER;
4833 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4834 use_spacer = RIGHT_SPACER;
4840 use_xft = string_to_bool(value);
4844 set_first_font(value);
4848 if (value && font_count >= 0) {
4849 fonts[0].font_alpha = atof(value) * 65535.0;
4856 if (string_to_bool(value)) {
4857 NORM_ERR("Xft not enabled at compile time");
4861 /* xftfont silently ignored when no Xft */
4864 /* xftalpha is silently ignored when no Xft */
4869 set_first_font(value);
4877 gap_x = atoi(value);
4884 gap_y = atoi(value);
4890 CONF("mail_spool") {
4894 variable_substitute(value, buffer, 256);
4896 if (buffer[0] != '\0') {
4897 if (current_mail_spool) {
4898 free(current_mail_spool);
4900 current_mail_spool = strndup(buffer, text_buffer_size);
4907 CONF("minimum_size") {
4909 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4911 if (sscanf(value, "%d", &minimum_width) != 1) {
4919 CONF("maximum_width") {
4921 if (sscanf(value, "%d", &maximum_width) != 1) {
4929 CONF("no_buffers") {
4930 no_buffers = string_to_bool(value);
4932 CONF("top_name_width") {
4933 if (set_top_name_width(value))
4936 CONF("top_cpu_separate") {
4937 cpu_separate = string_to_bool(value);
4939 CONF("short_units") {
4940 short_units = string_to_bool(value);
4942 CONF("format_human_readable") {
4943 format_human_readable = string_to_bool(value);
4946 CONF("hddtemp_host") {
4947 set_hddtemp_host(value);
4949 CONF("hddtemp_port") {
4950 set_hddtemp_port(value);
4952 #endif /* HDDTEMP */
4953 CONF("pad_percents") {
4954 pad_percents = atoi(value);
4958 CONF("own_window") {
4960 own_window = string_to_bool(value);
4963 CONF("own_window_class") {
4965 memset(window.class_name, 0, sizeof(window.class_name));
4966 strncpy(window.class_name, value,
4967 sizeof(window.class_name) - 1);
4970 CONF("own_window_title") {
4972 memset(window.title, 0, sizeof(window.title));
4973 strncpy(window.title, value, sizeof(window.title) - 1);
4976 CONF("own_window_transparent") {
4978 set_transparent = string_to_bool(value);
4981 CONF("own_window_hints") {
4983 char *p_hint, *p_save;
4984 char delim[] = ", ";
4986 /* tokenize the value into individual hints */
4987 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4989 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4990 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4991 SET_HINT(window.hints, HINT_UNDECORATED);
4992 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4993 SET_HINT(window.hints, HINT_BELOW);
4994 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4995 SET_HINT(window.hints, HINT_ABOVE);
4996 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4997 SET_HINT(window.hints, HINT_STICKY);
4998 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4999 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5000 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5001 SET_HINT(window.hints, HINT_SKIP_PAGER);
5006 p_hint = strtok_r(NULL, delim, &p_save);
5007 } while (p_hint != NULL);
5013 CONF("own_window_type") {
5015 if (strncmp(value, "normal", 6) == EQUAL) {
5016 window.type = TYPE_NORMAL;
5017 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5018 window.type = TYPE_DESKTOP;
5019 } else if (strncmp(value, "dock", 4) == EQUAL) {
5020 window.type = TYPE_DOCK;
5021 text_alignment = TOP_LEFT;
5022 } else if (strncmp(value, "panel", 5) == EQUAL) {
5023 window.type = TYPE_PANEL;
5024 } else if (strncmp(value, "override", 8) == EQUAL) {
5025 window.type = TYPE_OVERRIDE;
5034 CONF("own_window_argb_visual") {
5035 use_argb_visual = string_to_bool(value);
5037 CONF("own_window_argb_value") {
5038 own_window_argb_value = strtol(value, 0, 0);
5039 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5040 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5043 #endif /* USE_ARGB */
5045 CONF("stippled_borders") {
5047 stippled_borders = strtol(value, 0, 0);
5049 stippled_borders = 4;
5053 CONF("imlib_cache_size") {
5055 cimlib_set_cache_size(atoi(value));
5058 CONF("imlib_cache_flush_interval") {
5060 cimlib_set_cache_flush_interval(atoi(value));
5065 CONF("update_interval_on_battery") {
5067 update_interval_bat = strtod(value, 0);
5072 CONF("update_interval") {
5074 set_update_interval(strtod(value, 0));
5078 if (info.music_player_interval == 0) {
5079 // default to update_interval
5080 info.music_player_interval = update_interval;
5083 CONF("total_run_times") {
5085 total_run_times = strtod(value, 0);
5091 stuff_in_uppercase = string_to_bool(value);
5093 CONF("max_specials") {
5095 max_specials = atoi(value);
5100 CONF("max_user_text") {
5102 max_user_text = atoi(value);
5107 CONF("text_buffer_size") {
5109 text_buffer_size = atoi(value);
5110 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5111 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5112 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5120 if (output_methods & TO_X) {
5121 X11_initialisation();
5130 global_text = (char *) malloc(1);
5131 global_text[0] = '\0';
5134 unsigned int l = strlen(global_text);
5136 char buf[CONF_BUFF_SIZE];
5138 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5144 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5152 /* Check for continuation of \\-\n. */
5153 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5154 global_text[l-1] = '\0';
5158 global_text = (char *) realloc(global_text, l + bl + 1);
5159 strcat(global_text, buf);
5161 if (strlen(global_text) > max_user_text) {
5165 global_text_lines = line + 1;
5168 #ifdef TCP_PORT_MONITOR
5169 CONF("max_port_monitor_connections") {
5171 if (!value || (sscanf(value, "%d", &max) != 1)) {
5172 /* an error. use default, warn and continue. */
5173 tcp_portmon_set_max_connections(0);
5175 } else if (tcp_portmon_set_max_connections(max)) {
5176 /* max is < 0, default has been set*/
5181 CONF("if_up_strictness") {
5183 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5184 ifup_strictness = IFUP_UP;
5185 } else if (strcasecmp(value, "up") == EQUAL) {
5186 ifup_strictness = IFUP_UP;
5187 } else if (strcasecmp(value, "link") == EQUAL) {
5188 ifup_strictness = IFUP_LINK;
5189 } else if (strcasecmp(value, "address") == EQUAL) {
5190 ifup_strictness = IFUP_ADDR;
5192 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5193 ifup_strictness = IFUP_UP;
5197 CONF("temperature_unit") {
5199 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5200 } else if (set_temp_output_unit(value)) {
5201 NORM_ERR("temperature_unit: incorrect argument");
5208 char *ptr = strtok(value, " ");
5211 ptr = strtok(NULL, " ");
5218 CONF("lua_draw_hook_pre") {
5220 llua_set_draw_pre_hook(value);
5225 CONF("lua_draw_hook_post") {
5227 llua_set_draw_post_hook(value);
5232 CONF("lua_startup_hook") {
5234 llua_set_startup_hook(value);
5239 CONF("lua_shutdown_hook") {
5241 llua_set_shutdown_hook(value);
5247 #endif /* HAVE_LUA */
5259 CONF("default_color"){}
5260 CONF3("default_shade_color", "default_shadecolor"){}
5261 CONF3("default_outline_color", "default_outlinecolor") {}
5262 CONF("own_window_colour") {}
5265 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5271 if (info.music_player_interval == 0) {
5272 // default to update_interval
5273 info.music_player_interval = update_interval;
5275 if (!global_text) { // didn't supply any text
5276 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5278 if (!output_methods) {
5279 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5281 #if defined(NCURSES)
5283 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5284 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5285 output_methods &= ~TO_NCURSES;
5289 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5290 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5291 output_methods &= ~(TO_STDOUT | TO_STDERR);
5293 #endif /* NCURSES */
5298 static void load_config_file_x11(const char *f)
5303 fp = open_config_file(f);
5307 DBGP("reading contents from config file '%s'", f);
5310 char buff[CONF_BUFF_SIZE], *name, *value;
5311 int ret = do_config_step(&line, fp, buff, &name, &value);
5312 if (ret == CONF_BREAK) {
5314 } else if (ret == CONF_CONTINUE) {
5319 X11_initialisation();
5320 if (x_initialised == YES) {
5322 color0 = get_x11_color(value);
5329 X11_initialisation();
5330 if (x_initialised == YES) {
5332 color1 = get_x11_color(value);
5339 X11_initialisation();
5340 if (x_initialised == YES) {
5342 color2 = get_x11_color(value);
5349 X11_initialisation();
5350 if (x_initialised == YES) {
5352 color3 = get_x11_color(value);
5359 X11_initialisation();
5360 if (x_initialised == YES) {
5362 color4 = get_x11_color(value);
5369 X11_initialisation();
5370 if (x_initialised == YES) {
5372 color5 = get_x11_color(value);
5379 X11_initialisation();
5380 if (x_initialised == YES) {
5382 color6 = get_x11_color(value);
5389 X11_initialisation();
5390 if (x_initialised == YES) {
5392 color7 = get_x11_color(value);
5399 X11_initialisation();
5400 if (x_initialised == YES) {
5402 color8 = get_x11_color(value);
5409 X11_initialisation();
5410 if (x_initialised == YES) {
5412 color9 = get_x11_color(value);
5418 CONF("default_color") {
5419 X11_initialisation();
5420 if (x_initialised == YES) {
5422 default_fg_color = get_x11_color(value);
5428 CONF3("default_shade_color", "default_shadecolor") {
5429 X11_initialisation();
5430 if (x_initialised == YES) {
5432 default_bg_color = get_x11_color(value);
5438 CONF3("default_outline_color", "default_outlinecolor") {
5439 X11_initialisation();
5440 if (x_initialised == YES) {
5442 default_out_color = get_x11_color(value);
5449 CONF("own_window_colour") {
5450 X11_initialisation();
5451 if (x_initialised == YES) {
5453 background_colour = get_x11_color(value);
5455 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5456 "'#' for hex colours");
5462 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5463 if(output_methods & TO_X) {
5464 X11_initialisation();
5473 #undef CONF_CONTINUE
5474 #undef CONF_BUFF_SIZE
5482 static void print_help(const char *prog_name) {
5483 printf("Usage: %s [OPTION]...\n"
5484 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5485 "window. Command line options will override configurations defined in config\n"
5487 " -v, --version version\n"
5488 " -q, --quiet quiet mode\n"
5489 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5490 " -c, --config=FILE config file to load\n"
5491 #ifdef CONFIG_OUTPUT
5492 " -C, --print-config print the builtin default config to stdout\n"
5493 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5495 " -d, --daemonize daemonize, fork to background\n"
5496 " -h, --help help\n"
5498 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5499 " -f, --font=FONT font to use\n"
5500 " -X, --display=DISPLAY X11 display to use\n"
5502 " -o, --own-window create own window to draw\n"
5505 " -b, --double-buffer double buffer (prevents flickering)\n"
5507 " -w, --window-id=WIN_ID window id to draw\n"
5508 " -x X x position\n"
5509 " -y Y y position\n"
5511 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5512 " -u, --interval=SECS update interval\n"
5513 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5514 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5519 /* : means that character before that takes an argument */
5520 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5530 #ifdef CONFIG_OUTPUT
5535 static const struct option longopts[] = {
5536 { "help", 0, NULL, 'h' },
5537 { "version", 0, NULL, 'V' },
5538 { "debug", 0, NULL, 'D' },
5539 { "config", 1, NULL, 'c' },
5540 #ifdef CONFIG_OUTPUT
5541 { "print-config", 0, NULL, 'C' },
5543 { "daemonize", 0, NULL, 'd' },
5545 { "alignment", 1, NULL, 'a' },
5546 { "font", 1, NULL, 'f' },
5547 { "display", 1, NULL, 'X' },
5549 { "own-window", 0, NULL, 'o' },
5552 { "double-buffer", 0, NULL, 'b' },
5554 { "window-id", 1, NULL, 'w' },
5556 { "text", 1, NULL, 't' },
5557 { "interval", 0, NULL, 'u' },
5558 { "pause", 0, NULL, 'p' },
5562 void initialisation(int argc, char **argv) {
5563 struct sigaction act, oact;
5565 set_default_configurations();
5566 load_config_file(current_config);
5567 currentconffile = conftree_add(currentconffile, current_config);
5569 /* init specials array */
5570 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5571 NORM_ERR("failed to create specials array");
5575 if (current_mail_spool == NULL) {
5578 variable_substitute(MAIL_FILE, buf, 256);
5580 if (buf[0] != '\0') {
5581 current_mail_spool = strndup(buf, text_buffer_size);
5586 /* handle other command line arguments */
5588 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5589 || defined(__NetBSD__)
5590 optind = optreset = 1;
5595 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5596 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5597 "kvm_open")) == NULL) {
5598 CRIT_ERR(NULL, NULL, "cannot read kvm");
5603 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5612 fork_to_background = 1;
5615 global_debug_level++;
5619 set_first_font(optarg);
5622 text_alignment = string_to_alignment(optarg);
5641 global_text = strndup(optarg, max_user_text);
5642 convert_escapes(global_text);
5646 update_interval = strtod(optarg, 0);
5647 update_interval_old = update_interval;
5648 if (info.music_player_interval == 0) {
5649 // default to update_interval
5650 info.music_player_interval = update_interval;
5655 total_run_times = strtod(optarg, 0);
5659 gap_x = atoi(optarg);
5663 gap_y = atoi(optarg);
5667 startup_pause = atoi(optarg);
5668 sleep(startup_pause);
5678 if (output_methods & TO_X) {
5679 load_config_file_x11(current_config);
5683 /* generate text and get initial size */
5684 extract_variable_text(global_text);
5691 if (fork_to_background) {
5696 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5703 fprintf(stderr, "\n");
5708 /* parent process */
5709 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5716 start_update_threading();
5718 text_buffer = malloc(max_user_text);
5719 memset(text_buffer, 0, max_user_text);
5720 tmpstring1 = malloc(text_buffer_size);
5721 memset(tmpstring1, 0, text_buffer_size);
5722 tmpstring2 = malloc(text_buffer_size);
5723 memset(tmpstring2, 0, text_buffer_size);
5728 X11_create_window();
5731 llua_setup_info(&info, update_interval);
5732 #endif /* HAVE_LUA */
5737 /* Set signal handlers */
5738 act.sa_handler = signal_handler;
5739 sigemptyset(&act.sa_mask);
5742 act.sa_flags |= SA_RESTART;
5745 if ( sigaction(SIGINT, &act, &oact) < 0
5746 || sigaction(SIGALRM, &act, &oact) < 0
5747 || sigaction(SIGUSR1, &act, &oact) < 0
5748 || sigaction(SIGHUP, &act, &oact) < 0
5749 || sigaction(SIGTERM, &act, &oact) < 0) {
5750 NORM_ERR("error setting signal handler: %s", strerror(errno));
5754 llua_startup_hook();
5755 #endif /* HAVE_LUA */
5758 int main(int argc, char **argv)
5767 g_signal_pending = 0;
5768 max_user_text = MAX_USER_TEXT_DEFAULT;
5770 memset(&info, 0, sizeof(info));
5774 #ifdef TCP_PORT_MONITOR
5775 /* set default connection limit */
5776 tcp_portmon_set_max_connections(0);
5779 /* handle command line parameters that don't change configs */
5781 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5782 || ((s = getenv("LANG")) && *s)) {
5783 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5785 NORM_ERR("malloc failed");
5787 for (x = 0; x < strlen(s); x++) {
5788 temp[x] = tolower(s[x]);
5791 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5797 if (!setlocale(LC_CTYPE, "")) {
5798 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5802 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5813 if (current_config) {
5814 free(current_config);
5816 current_config = strndup(optarg, max_user_text);
5819 freopen("/dev/null", "w", stderr);
5822 print_help(argv[0]);
5824 #ifdef CONFIG_OUTPUT
5831 window.window = strtol(optarg, 0, 0);
5836 disp = strdup(optarg);
5845 /* check if specified config file is valid */
5846 if (current_config) {
5848 if (stat(current_config, &sb) ||
5849 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5850 NORM_ERR("invalid configuration file '%s'\n", current_config);
5851 free(current_config);
5856 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5858 if (!current_config) {
5859 /* load default config file */
5860 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5863 /* Try to use personal config file first */
5864 to_real_path(buf, CONFIG_FILE);
5865 if (buf[0] && (fp = fopen(buf, "r"))) {
5866 current_config = strndup(buf, max_user_text);
5870 /* Try to use system config file if personal config not readable */
5871 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5872 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5876 /* No readable config found */
5877 if (!current_config) {
5878 #ifdef CONFIG_OUTPUT
5879 current_config = strdup("==builtin==");
5880 NORM_ERR("no readable personal or system-wide config file found,"
5881 " using builtin default");
5883 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5884 #endif /* ! CONF_OUTPUT */
5889 /* Load xoap keys, if existing */
5893 #ifdef HAVE_SYS_INOTIFY_H
5894 inotify_fd = inotify_init1(IN_NONBLOCK);
5895 #endif /* HAVE_SYS_INOTIFY_H */
5897 initialisation(argc, argv);
5901 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5909 void alarm_handler(void) {
5911 kill(childpid, SIGTERM);
5915 static void signal_handler(int sig)
5917 /* signal handler is light as a feather, as it should be.
5918 * we will poll g_signal_pending with each loop of conky
5919 * and do any signal processing there, NOT here (except
5920 * SIGALRM because this is caused when conky is hanging) */
5921 if(sig == SIGALRM) {
5924 g_signal_pending = sig;