1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* two strings for internal use */
151 static char *tmpstring1, *tmpstring2;
153 /* variables holding various config settings */
155 int format_human_readable;
162 int top_cpu, top_mem, top_time;
170 static int extra_newline;
171 enum x_initialiser_state x_initialised = NO;
172 static volatile int g_signal_pending;
173 /* Update interval */
174 double update_interval;
175 double update_interval_old;
176 double update_interval_bat;
177 void *global_cpu = NULL;
182 /* prototypes for internally used functions */
183 static void signal_handler(int);
184 static void print_version(void) __attribute__((noreturn));
185 static void reload_config(void);
187 static void print_version(void)
189 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
191 printf("\nCompiled in features:\n\n"
192 "System config file: "SYSTEM_CONFIG_FILE"\n"
193 "Package library path: "PACKAGE_LIBDIR"\n\n"
197 " * Xdamage extension\n"
198 # endif /* HAVE_XDAMAGE */
200 " * XDBE (double buffer extension)\n"
201 # endif /* HAVE_XDBE */
206 "\n Music detection:\n"
209 #endif /* AUDACIOUS */
225 #endif /* HAVE_OPENMP */
232 #ifdef TCP_PORT_MONITOR
234 #endif /* TCP_PORT_MONITOR */
237 #endif /* HAVE_CURL */
242 " * Weather (METAR)\n"
244 " * Weather (XOAP)\n"
249 #endif /* HAVE_IWLIB */
251 " * support for IBM/Lenovo notebooks\n"
261 #endif /* CONFIG_OUTPUT */
266 " * ALSA mixer support\n"
267 #endif /* MIXER_IS_ALSA */
280 #ifdef HAVE_LUA_CAIRO
282 #endif /* HAVE_LUA_CAIRO */
283 #ifdef HAVE_LUA_IMLIB2
286 #endif /* HAVE_LUA */
292 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
297 static void X11_create_window(void);
298 static void X11_initialisation(void);
300 struct _x11_stuff_s {
304 XserverRegion region2, part;
305 int event_base, error_base;
311 static int text_start_x, text_start_y; /* text start position in window */
312 static int text_width, text_height;
328 /* display to connect to */
329 static char *disp = NULL;
333 /* struct that has all info to be shared between
334 * instances of the same text object */
335 struct information info;
337 /* path to config file */
338 char *current_config;
340 /* set to 1 if you want all text to be in uppercase */
341 static unsigned int stuff_in_uppercase;
343 /* Run how many times? */
344 static unsigned long total_run_times;
347 static int fork_to_background;
349 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
351 /* filenames for output */
352 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
353 char *append_file = NULL; FILE *append_fpointer = NULL;
357 static int show_graph_scale;
358 static int show_graph_range;
360 /* Position on the screen */
361 static int text_alignment;
362 static int gap_x, gap_y;
365 static int draw_borders;
366 static int draw_graph_borders;
367 static int stippled_borders;
369 int get_stippled_borders(void)
371 return stippled_borders;
374 static int draw_shades, draw_outline;
376 long default_fg_color, default_bg_color, default_out_color;
378 /* create own window or draw stuff to root? */
379 static int set_transparent = 0;
382 static int own_window = 0;
383 static int background_colour = 0;
385 /* fixed size/pos is set if wm/user changes them */
386 static int fixed_size = 0, fixed_pos = 0;
389 static int minimum_width, minimum_height;
390 static int maximum_width;
395 static int sensor_device;
398 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
401 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
402 unsigned int max_user_text;
404 /* maximum size of individual text buffers, ie $exec buffer size */
405 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
410 /* no buffers in used memory? */
413 /* pad percentages to decimals? */
414 static int pad_percents = 0;
416 static char *global_text = 0;
418 char *get_global_text(void)
423 long global_text_lines;
425 static int total_updates;
426 static int updatereset;
428 void set_updatereset(int i)
433 int get_updatereset(void)
438 int check_contains(char *f, char *s)
441 FILE *where = open_file(f, 0);
446 while (fgets(buf1, 256, where)) {
447 if (strstr(buf1, s)) {
454 NORM_ERR("Could not open the file");
459 #define SECRIT_MULTILINE_CHAR '\x02'
461 static inline int calc_text_width(const char *s)
463 size_t slen = strlen(s);
466 if ((output_methods & TO_X) == 0) {
476 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
477 (const FcChar8 *) s, slen, &gi);
479 XftTextExtents8(display, fonts[selected_font].xftfont,
480 (const FcChar8 *) s, slen, &gi);
486 return XTextWidth(fonts[selected_font].font, s, slen);
491 /* formatted text to render on screen, generated in generate_text(),
492 * drawn in draw_stuff() */
494 static char *text_buffer;
496 /* quite boring functions */
498 static inline void for_each_line(char *b, int f(char *, int))
501 int special_index = 0; /* specials index */
503 for (ps = b, pe = b; *pe; pe++) {
506 special_index = f(ps, special_index);
513 f(ps, special_index);
517 static void convert_escapes(char *buf)
519 char *p = buf, *s = buf;
526 } else if (*s == '\\') {
537 /* Prints anything normally printed with snprintf according to the current value
538 * of use_spacer. Actually slightly more flexible than snprintf, as you can
539 * safely specify the destination buffer as one of your inputs. */
540 int spaced_print(char *buf, int size, const char *format, int width, ...)
549 tempbuf = malloc(size * sizeof(char));
551 // Passes the varargs along to vsnprintf
552 va_start(argp, width);
553 vsnprintf(tempbuf, size, format, argp);
556 switch (use_spacer) {
558 len = snprintf(buf, size, "%s", tempbuf);
561 len = snprintf(buf, size, "%*s", width, tempbuf);
564 len = snprintf(buf, size, "%-*s", width, tempbuf);
571 /* print percentage values
573 * - i.e., unsigned values between 0 and 100
574 * - respect the value of pad_percents */
575 int percent_print(char *buf, int size, unsigned value)
577 return spaced_print(buf, size, "%u", pad_percents, value);
580 /* converts from bytes to human readable format (K, M, G, T)
582 * The algorithm always divides by 1024, as unit-conversion of byte
583 * counts suggests. But for output length determination we need to
584 * compare with 1000 here, as we print in decimal form. */
585 void human_readable(long long num, char *buf, int size)
587 const char **suffix = suffixes;
593 /* Possibly just output as usual, for example for stdout usage */
594 if (!format_human_readable) {
595 spaced_print(buf, size, "%d", 6, round_to_int(num));
606 if (llabs(num) < 1000LL) {
607 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
611 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
619 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
621 * The goal is to always have a significance of 3, by
622 * adjusting the decimal part of the number. Sample output:
626 * so the point of alignment resides between number and unit. The
627 * upside of this is that there is minimal padding necessary, though
628 * there should be a way to make alignment take place at the decimal
629 * dot (then with fixed width decimal part).
631 * Note the repdigits below: when given a precision value, printf()
632 * rounds the float to it, not just cuts off the remaining digits. So
633 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
634 * printed with a precision of 0. Yay. */
636 precision = 0; /* print 100-999 without decimal part */
638 precision = 1; /* print 10-99 with one decimal place */
640 precision = 2; /* print 0-9 with two decimal places */
642 spaced_print(buf, size, format, width, precision, fnum, *suffix);
645 /* global object list root element */
646 static struct text_object global_root_object;
648 static long current_text_color;
650 void set_current_text_color(long colour)
652 current_text_color = colour;
655 long get_current_text_color(void)
657 return current_text_color;
660 //adds newstring to to the tree unless you can already see it when travelling back.
661 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
662 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
663 struct conftree* node;
664 struct conftree* node2;
666 for(node = previous; node != NULL; node = node->back) {
667 if(strcmp(node->string, newstring) == 0) {
671 node = malloc(sizeof(struct conftree));
672 if (previous != NULL) {
673 if(previous->vert_next == NULL) {
674 previous->vert_next = node;
676 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
677 node2->horz_next = node;
680 node->string = strdup(newstring);
681 node->horz_next = NULL;
682 node->vert_next = NULL;
683 node->back = previous;
687 void conftree_empty(struct conftree* tree) {
689 conftree_empty(tree->horz_next);
690 conftree_empty(tree->vert_next);
696 struct conftree *currentconffile;
698 static void extract_variable_text(const char *p)
700 free_text_objects(&global_root_object, 0);
714 extract_variable_text_internal(&global_root_object, p);
717 void parse_conky_vars(struct text_object *root, const char *txt,
718 char *p, int p_max_size, struct information *cur)
720 extract_variable_text_internal(root, txt);
721 generate_text_internal(p, p_max_size, *root, cur);
724 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
725 * multiline objects like $exec work with $align[rc] and friends
727 void substitute_newlines(char *p, long l)
731 while (p && *p && p < s + l) {
733 /* only substitute if it's not the last newline */
734 *p = SECRIT_MULTILINE_CHAR;
740 void generate_text_internal(char *p, int p_max_size,
741 struct text_object root, struct information *cur)
743 struct text_object *obj;
745 int need_to_load_fonts = 0;
748 /* for the OBJ_top* handler */
749 struct process **needed = 0;
752 char buff_in[p_max_size];
754 #endif /* HAVE_ICONV */
758 while (obj && p_max_size > 0) {
759 needed = 0; /* reset for top stuff */
761 /* IFBLOCK jumping algorithm
763 * This is easier as it looks like:
764 * - each IF checks it's condition
765 * - on FALSE: call DO_JUMP
766 * - on TRUE: don't care
767 * - each ELSE calls DO_JUMP unconditionally
768 * - each ENDIF is silently being ignored
771 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
772 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
773 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
774 * regularly, "obj" is being updated to point to obj->next, so object parsing
775 * continues right after the corresponding ELSE or ENDIF. This means that if we
776 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
777 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
778 * jumped, and there is nothing to do.
782 if (obj->ifblock_next) \
783 obj = obj->ifblock_next; \
786 #define OBJ(a) break; case OBJ_##a:
790 NORM_ERR("not implemented obj type %d", obj->type);
792 print_read_tcp(obj, p, p_max_size);
796 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
798 #endif /* !__OpenBSD__ */
802 ok = get_freq(p, p_max_size, "%.0f", 1,
810 ok = get_freq(p, p_max_size, "%'.2f", 1000,
813 /* OpenBSD has no such flag (SUSv2) */
814 ok = get_freq(p, p_max_size, "%.2f", 1000,
816 #endif /* __OpenBSD */
819 #if defined(__linux__)
821 print_voltage_mv(obj, p, p_max_size);
824 print_voltage_v(obj, p, p_max_size);
828 OBJ(wireless_essid) {
829 print_wireless_essid(obj, p, p_max_size);
832 print_wireless_mode(obj, p, p_max_size);
834 OBJ(wireless_bitrate) {
835 print_wireless_bitrate(obj, p, p_max_size);
838 print_wireless_ap(obj, p, p_max_size);
840 OBJ(wireless_link_qual) {
841 print_wireless_link_qual(obj, p, p_max_size);
843 OBJ(wireless_link_qual_max) {
844 print_wireless_link_qual_max(obj, p, p_max_size);
846 OBJ(wireless_link_qual_perc) {
847 print_wireless_link_qual_perc(obj, p, p_max_size);
849 OBJ(wireless_link_bar) {
850 print_wireless_link_bar(obj, p, p_max_size);
852 #endif /* HAVE_IWLIB */
854 #endif /* __linux__ */
858 get_acpi_fan(p, p_max_size);
861 get_acpi_ac_adapter(p, p_max_size);
864 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
867 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
869 OBJ(battery_percent) {
870 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
873 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
876 get_battery_short_status(p, p_max_size, obj->data.s);
878 #endif /* __OpenBSD__ */
881 human_readable(cur->buffers * 1024, p, 255);
884 human_readable(cur->cached * 1024, p, 255);
886 OBJ(cmdline_to_pid) {
887 print_cmdline_to_pid(obj, p, p_max_size);
890 if (obj->data.i > info.cpu_count) {
891 NORM_ERR("obj->data.i %i info.cpu_count %i",
892 obj->data.i, info.cpu_count);
893 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
895 percent_print(p, p_max_size,
896 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
900 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
903 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
907 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
910 print_loadgraph(obj, p);
914 new_fg(p, obj->data.l);
948 #if defined(__linux__)
950 snprintf(p, p_max_size, "%s",
951 get_disk_protect_queue(obj->data.s));
954 print_i8k_version(obj, p, p_max_size);
957 print_i8k_bios(obj, p, p_max_size);
960 print_i8k_serial(obj, p, p_max_size);
963 print_i8k_cpu_temp(obj, p, p_max_size);
965 OBJ(i8k_left_fan_status) {
966 print_i8k_left_fan_status(obj, p, p_max_size);
968 OBJ(i8k_right_fan_status) {
969 print_i8k_right_fan_status(obj, p, p_max_size);
971 OBJ(i8k_left_fan_rpm) {
972 print_i8k_left_fan_rpm(obj, p, p_max_size);
974 OBJ(i8k_right_fan_rpm) {
975 print_i8k_right_fan_rpm(obj, p, p_max_size);
978 print_i8k_ac_status(obj, p, p_max_size);
980 OBJ(i8k_buttons_status) {
981 print_i8k_buttons_status(obj, p, p_max_size);
985 get_ibm_acpi_fan(obj, p, p_max_size);
988 print_ibm_temps(obj, p, p_max_size);
991 get_ibm_acpi_volume(obj, p, p_max_size);
993 OBJ(ibm_brightness) {
994 get_ibm_acpi_brightness(obj, p, p_max_size);
997 /* information from sony_laptop kernel module
998 * /sys/devices/platform/sony-laptop */
1000 get_sony_fanspeed(p, p_max_size);
1003 if (!gateway_exists()) {
1008 print_gateway_iface(p, p_max_size);
1011 print_gateway_ip(p, p_max_size);
1014 snprintf(p, p_max_size, "%d", get_laptop_mode());
1017 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1019 #endif /* __linux__ */
1020 #if (defined(__FreeBSD__) || defined(__linux__))
1022 if (!interface_up(obj)) {
1028 OBJ(obsd_sensors_temp) {
1029 print_obsd_sensors_temp(obj, p, p_max_size);
1031 OBJ(obsd_sensors_fan) {
1032 print_obsd_sensors_fan(obj, p, p_max_size);
1034 OBJ(obsd_sensors_volt) {
1035 print_obsd_sensors_volt(obj, p, p_max_size);
1038 get_obsd_vendor(p, p_max_size);
1041 get_obsd_product(p, p_max_size);
1043 #endif /* __OpenBSD__ */
1046 new_font(p, obj->data.s);
1047 need_to_load_fonts = 1;
1051 print_diskio(obj, p, p_max_size);
1054 print_diskio_write(obj, p, p_max_size);
1057 print_diskio_read(obj, p, p_max_size);
1061 print_diskiograph(obj, p, p_max_size);
1063 OBJ(diskiograph_read) {
1064 print_diskiograph(obj, p, p_max_size);
1066 OBJ(diskiograph_write) {
1067 print_diskiograph(obj, p, p_max_size);
1071 print_downspeed(obj, p, p_max_size);
1074 print_downspeedf(obj, p, p_max_size);
1077 OBJ(downspeedgraph) {
1078 print_downspeedgraph(obj, p, p_max_size);
1082 /* Since we see you, you're if has not jumped.
1083 * Do Ninja jump here: without leaving traces.
1084 * This is to prevent us from stale jumped flags.
1086 obj = obj->ifblock_next;
1090 /* harmless object, just ignore */
1093 print_addr(obj, p, p_max_size);
1095 #if defined(__linux__)
1097 print_addrs(obj, p, p_max_size);
1099 #endif /* __linux__ */
1100 #if defined(IMLIB2) && defined(X11)
1102 /* doesn't actually draw anything, just queues it omp. the
1103 * image will get drawn after the X event loop */
1104 cimlib_add_image(obj->data.s);
1108 evaluate(obj->data.s, p, p_max_size);
1111 print_exec(obj, p, p_max_size);
1114 print_execp(obj, p, p_max_size);
1118 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);
1137 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);
1242 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);
1374 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1378 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1382 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1387 print_mixer(obj, p, p_max_size);
1390 print_mixerl(obj, p, p_max_size);
1393 print_mixerr(obj, p, p_max_size);
1396 print_mixer_bar(obj, p, p_max_size);
1399 print_mixerl_bar(obj, p, p_max_size);
1402 print_mixerr_bar(obj, p, p_max_size);
1404 OBJ(if_mixer_mute) {
1405 if (!check_mixer_muted(obj)) {
1410 #define NOT_IN_X "Not running in X"
1412 if(x_initialised != YES) {
1413 strncpy(p, NOT_IN_X, p_max_size);
1415 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1418 OBJ(monitor_number) {
1419 if(x_initialised != YES) {
1420 strncpy(p, NOT_IN_X, p_max_size);
1422 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1426 if(x_initialised != YES) {
1427 strncpy(p, NOT_IN_X, p_max_size);
1429 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1432 OBJ(desktop_number) {
1433 if(x_initialised != YES) {
1434 strncpy(p, NOT_IN_X, p_max_size);
1436 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1440 if(x_initialised != YES) {
1441 strncpy(p, NOT_IN_X, p_max_size);
1442 }else if(cur->x11.desktop.name != NULL) {
1443 strncpy(p, cur->x11.desktop.name, p_max_size);
1449 char buf[max_user_text];
1451 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1453 print_format_time(obj, p, p_max_size);
1457 print_mails(obj, p, p_max_size);
1460 print_new_mails(obj, p, p_max_size);
1463 print_seen_mails(obj, p, p_max_size);
1466 print_unseen_mails(obj, p, p_max_size);
1468 OBJ(flagged_mails) {
1469 print_flagged_mails(obj, p, p_max_size);
1471 OBJ(unflagged_mails) {
1472 print_unflagged_mails(obj, p, p_max_size);
1474 OBJ(forwarded_mails) {
1475 print_forwarded_mails(obj, p, p_max_size);
1477 OBJ(unforwarded_mails) {
1478 print_unforwarded_mails(obj, p, p_max_size);
1480 OBJ(replied_mails) {
1481 print_replied_mails(obj, p, p_max_size);
1483 OBJ(unreplied_mails) {
1484 print_unreplied_mails(obj, p, p_max_size);
1487 print_draft_mails(obj, p, p_max_size);
1489 OBJ(trashed_mails) {
1490 print_trashed_mails(obj, p, p_max_size);
1493 print_mboxscan(obj, p, p_max_size);
1496 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1499 new_outline(p, obj->data.l);
1502 char buf[max_user_text];
1504 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1506 print_pid_chroot(obj, p, p_max_size);
1509 char buf[max_user_text];
1511 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1513 print_pid_cmdline(obj, p, p_max_size);
1516 char buf[max_user_text];
1518 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1520 print_pid_cwd(obj, p, p_max_size);
1523 char buf[max_user_text];
1525 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1527 print_pid_environ(obj, p, p_max_size);
1529 OBJ(pid_environ_list) {
1530 char buf[max_user_text];
1532 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1534 print_pid_environ_list(obj, p, p_max_size);
1537 char buf[max_user_text];
1539 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1541 print_pid_exe(obj, p, p_max_size);
1544 char buf[max_user_text];
1546 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1548 print_pid_nice(obj, p, p_max_size);
1550 OBJ(pid_openfiles) {
1551 char buf[max_user_text];
1553 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1555 print_pid_openfiles(obj, p, p_max_size);
1558 char buf[max_user_text];
1560 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1562 print_pid_parent(obj, p, p_max_size);
1565 char buf[max_user_text];
1567 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1569 print_pid_priority(obj, p, p_max_size);
1572 char buf[max_user_text];
1574 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1576 print_pid_state(obj, p, p_max_size);
1578 OBJ(pid_state_short) {
1579 char buf[max_user_text];
1581 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1583 print_pid_state_short(obj, p, p_max_size);
1586 char buf[max_user_text];
1588 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1590 print_pid_stderr(obj, p, p_max_size);
1593 char buf[max_user_text];
1595 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1597 print_pid_stdin(obj, p, p_max_size);
1600 char buf[max_user_text];
1602 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1604 print_pid_stdout(obj, p, p_max_size);
1607 char buf[max_user_text];
1609 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1611 print_pid_threads(obj, p, p_max_size);
1613 OBJ(pid_thread_list) {
1614 char buf[max_user_text];
1616 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1618 print_pid_thread_list(obj, p, p_max_size);
1620 OBJ(pid_time_kernelmode) {
1621 char buf[max_user_text];
1623 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1625 print_pid_time_kernelmode(obj, p, p_max_size);
1627 OBJ(pid_time_usermode) {
1628 char buf[max_user_text];
1630 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1632 print_pid_time_usermode(obj, p, p_max_size);
1635 char buf[max_user_text];
1637 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1639 print_pid_time(obj, p, p_max_size);
1642 char buf[max_user_text];
1644 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1646 print_pid_uid(obj, p, p_max_size);
1649 char buf[max_user_text];
1651 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1653 print_pid_euid(obj, p, p_max_size);
1656 char buf[max_user_text];
1658 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1660 print_pid_suid(obj, p, p_max_size);
1663 char buf[max_user_text];
1665 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1667 print_pid_fsuid(obj, p, p_max_size);
1670 char buf[max_user_text];
1672 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1674 print_pid_gid(obj, p, p_max_size);
1677 char buf[max_user_text];
1679 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1681 print_pid_egid(obj, p, p_max_size);
1684 char buf[max_user_text];
1686 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1688 print_pid_sgid(obj, p, p_max_size);
1691 char buf[max_user_text];
1693 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1695 print_pid_fsgid(obj, p, p_max_size);
1698 char buf[max_user_text];
1700 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1702 print_pid_read(obj, p, p_max_size);
1705 char buf[max_user_text];
1707 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1709 print_pid_vmpeak(obj, p, p_max_size);
1712 char buf[max_user_text];
1714 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1716 print_pid_vmsize(obj, p, p_max_size);
1719 char buf[max_user_text];
1721 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1723 print_pid_vmlck(obj, p, p_max_size);
1726 char buf[max_user_text];
1728 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1730 print_pid_vmhwm(obj, p, p_max_size);
1733 char buf[max_user_text];
1735 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1737 print_pid_vmrss(obj, p, p_max_size);
1740 char buf[max_user_text];
1742 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1744 print_pid_vmdata(obj, p, p_max_size);
1747 char buf[max_user_text];
1749 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1751 print_pid_vmstk(obj, p, p_max_size);
1754 char buf[max_user_text];
1756 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1758 print_pid_vmexe(obj, p, p_max_size);
1761 char buf[max_user_text];
1763 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1765 print_pid_vmlib(obj, p, p_max_size);
1768 char buf[max_user_text];
1770 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1772 print_pid_vmpte(obj, p, p_max_size);
1775 char buf[max_user_text];
1777 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1779 print_pid_write(obj, p, p_max_size);
1782 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1784 OBJ(running_processes) {
1785 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1787 OBJ(running_threads) {
1788 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1791 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1794 snprintf(p, p_max_size, "%s", obj->data.s);
1798 new_bg(p, obj->data.l);
1801 new_stippled_hr(obj, p);
1805 human_readable(cur->swap * 1024, p, 255);
1808 human_readable(cur->swapfree * 1024, p, 255);
1811 human_readable(cur->swapmax * 1024, p, 255);
1814 if (cur->swapmax == 0) {
1815 strncpy(p, "No swap", p_max_size);
1817 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1821 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1824 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1827 print_time(obj, p, p_max_size);
1830 print_utime(obj, p, p_max_size);
1833 print_tztime(obj, p, p_max_size);
1836 print_totaldown(obj, p, p_max_size);
1839 print_totalup(obj, p, p_max_size);
1842 char buf[max_user_text];
1844 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1846 print_gid_name(obj, p, p_max_size);
1849 char buf[max_user_text];
1851 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1853 print_uid_name(obj, p, p_max_size);
1856 snprintf(p, p_max_size, "%d", total_updates);
1859 if(total_updates % updatereset != obj->data.i - 1) {
1864 print_upspeed(obj, p, p_max_size);
1867 print_upspeedf(obj, p, p_max_size);
1871 print_upspeedgraph(obj, p, p_max_size);
1875 format_seconds_short(p, p_max_size, (int) cur->uptime);
1878 format_seconds(p, p_max_size, (int) cur->uptime);
1882 snprintf(p, p_max_size, "%s", cur->users.names);
1885 snprintf(p, p_max_size, "%s", cur->users.terms);
1888 snprintf(p, p_max_size, "%s", cur->users.times);
1891 update_user_time(obj->data.s);
1892 snprintf(p, p_max_size, "%s", cur->users.ctime);
1895 snprintf(p, p_max_size, "%d", cur->users.number);
1897 #endif /* __linux__ */
1898 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1899 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1903 msg = get_apm_adapter();
1904 snprintf(p, p_max_size, "%s", msg);
1907 OBJ(apm_battery_life) {
1910 msg = get_apm_battery_life();
1911 snprintf(p, p_max_size, "%s", msg);
1914 OBJ(apm_battery_time) {
1917 msg = get_apm_battery_time();
1918 snprintf(p, p_max_size, "%s", msg);
1921 #endif /* __FreeBSD__ __OpenBSD__ */
1925 print_mpd_title(obj, p, p_max_size);
1927 print_mpd_artist(obj, p, p_max_size);
1929 print_mpd_album(obj, p, p_max_size);
1931 print_mpd_random(obj, p, p_max_size);
1933 print_mpd_repeat(obj, p, p_max_size);
1935 print_mpd_track(obj, p, p_max_size);
1937 print_mpd_name(obj, p, p_max_size);
1939 print_mpd_file(obj, p, p_max_size);
1941 print_mpd_vol(obj, p, p_max_size);
1943 print_mpd_bitrate(obj, p, p_max_size);
1945 print_mpd_status(obj, p, p_max_size);
1947 print_mpd_elapsed(obj, p, p_max_size);
1950 print_mpd_length(obj, p, p_max_size);
1953 print_mpd_percent(obj, p, p_max_size);
1956 print_mpd_bar(obj, p, p_max_size);
1959 print_mpd_smart(obj, p, p_max_size);
1961 OBJ(if_mpd_playing) {
1962 if (!mpd_get_info()->is_playing) {
1969 #define MOC_PRINT(t, a) \
1970 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1972 MOC_PRINT(state, "??");
1975 MOC_PRINT(file, "no file");
1978 MOC_PRINT(title, "no title");
1981 MOC_PRINT(artist, "no artist");
1984 MOC_PRINT(song, "no song");
1987 MOC_PRINT(album, "no album");
1989 OBJ(moc_totaltime) {
1990 MOC_PRINT(totaltime, "0:00");
1993 MOC_PRINT(timeleft, "0:00");
1996 MOC_PRINT(curtime, "0:00");
1999 MOC_PRINT(bitrate, "0Kbps");
2002 MOC_PRINT(rate, "0KHz");
2008 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2011 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2014 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2017 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2019 OBJ(xmms2_comment) {
2020 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2023 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2026 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2029 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2031 OBJ(xmms2_tracknr) {
2032 if (cur->xmms2.tracknr != -1) {
2033 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2036 OBJ(xmms2_bitrate) {
2037 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2040 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2043 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2045 OBJ(xmms2_elapsed) {
2046 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2047 (cur->xmms2.elapsed / 1000) % 60);
2049 OBJ(xmms2_duration) {
2050 snprintf(p, p_max_size, "%02d:%02d",
2051 cur->xmms2.duration / 60000,
2052 (cur->xmms2.duration / 1000) % 60);
2054 OBJ(xmms2_percent) {
2055 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2058 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2060 OBJ(xmms2_playlist) {
2061 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2063 OBJ(xmms2_timesplayed) {
2064 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2067 if (strlen(cur->xmms2.title) < 2
2068 && strlen(cur->xmms2.title) < 2) {
2069 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2071 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2075 OBJ(if_xmms2_connected) {
2076 if (cur->xmms2.conn_state != 1) {
2082 OBJ(audacious_status) {
2083 snprintf(p, p_max_size, "%s",
2084 cur->audacious.items[AUDACIOUS_STATUS]);
2086 OBJ(audacious_title) {
2087 snprintf(p, cur->audacious.max_title_len > 0
2088 ? cur->audacious.max_title_len : p_max_size, "%s",
2089 cur->audacious.items[AUDACIOUS_TITLE]);
2091 OBJ(audacious_length) {
2092 snprintf(p, p_max_size, "%s",
2093 cur->audacious.items[AUDACIOUS_LENGTH]);
2095 OBJ(audacious_length_seconds) {
2096 snprintf(p, p_max_size, "%s",
2097 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2099 OBJ(audacious_position) {
2100 snprintf(p, p_max_size, "%s",
2101 cur->audacious.items[AUDACIOUS_POSITION]);
2103 OBJ(audacious_position_seconds) {
2104 snprintf(p, p_max_size, "%s",
2105 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2107 OBJ(audacious_bitrate) {
2108 snprintf(p, p_max_size, "%s",
2109 cur->audacious.items[AUDACIOUS_BITRATE]);
2111 OBJ(audacious_frequency) {
2112 snprintf(p, p_max_size, "%s",
2113 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2115 OBJ(audacious_channels) {
2116 snprintf(p, p_max_size, "%s",
2117 cur->audacious.items[AUDACIOUS_CHANNELS]);
2119 OBJ(audacious_filename) {
2120 snprintf(p, p_max_size, "%s",
2121 cur->audacious.items[AUDACIOUS_FILENAME]);
2123 OBJ(audacious_playlist_length) {
2124 snprintf(p, p_max_size, "%s",
2125 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2127 OBJ(audacious_playlist_position) {
2128 snprintf(p, p_max_size, "%s",
2129 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2131 OBJ(audacious_main_volume) {
2132 snprintf(p, p_max_size, "%s",
2133 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2135 OBJ(audacious_bar) {
2139 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2140 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2141 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2143 #endif /* AUDACIOUS */
2147 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2150 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2153 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2156 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2159 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2162 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2165 /* we have four different types of top (top, top_mem,
2166 * top_time and top_io). To avoid having almost-same code four
2167 * times, we have this special handler. */
2176 print_top(obj, p, p_max_size);
2177 #endif /* __linux__ */
2179 print_tailhead("tail", obj, p, p_max_size);
2182 print_tailhead("head", obj, p, p_max_size);
2185 print_lines(obj, p, p_max_size);
2189 print_words(obj, p, p_max_size);
2191 #ifdef TCP_PORT_MONITOR
2193 tcp_portmon_action(obj, p, p_max_size);
2195 #endif /* TCP_PORT_MONITOR */
2199 do_iconv_start(obj);
2204 #endif /* HAVE_ICONV */
2206 OBJ(entropy_avail) {
2207 print_entropy_avail(obj, p, p_max_size);
2210 print_entropy_perc(obj, p, p_max_size);
2212 OBJ(entropy_poolsize) {
2213 print_entropy_poolsize(obj, p, p_max_size);
2216 print_entropy_bar(obj, p, p_max_size);
2220 print_smapi(obj, p, p_max_size);
2222 OBJ(if_smapi_bat_installed) {
2224 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2225 if(!smapi_bat_installed(idx)) {
2229 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2231 OBJ(smapi_bat_perc) {
2232 print_smapi_bat_perc(obj, p, p_max_size);
2234 OBJ(smapi_bat_temp) {
2235 print_smapi_bat_temp(obj, p, p_max_size);
2237 OBJ(smapi_bat_power) {
2238 print_smapi_bat_power(obj, p, p_max_size);
2240 OBJ(smapi_bat_bar) {
2241 print_smapi_bat_bar(obj, p, p_max_size);
2246 char buf[max_user_text];
2248 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2249 snprintf(p, p_max_size, "%s", buf);
2255 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2256 char buf[max_user_text];
2259 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2260 snprintf(p, p_max_size, "%s", buf);
2261 if(total_updates % 2) {
2262 for(j=0; p[j] != 0; j++) {
2268 char buf[max_user_text];
2270 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2272 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2273 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2274 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2275 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2276 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2277 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2278 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2280 snprintf(p, p_max_size, "%s", buf);
2283 print_scroll(obj, p, p_max_size, cur);
2286 print_combine(obj, p, cur);
2290 print_nvidia_value(obj, display, p, p_max_size);
2295 /* This is just a meta-object to set host:port */
2298 snprintf(p, p_max_size, "%s",
2299 cur->apcupsd.items[APCUPSD_NAME]);
2301 OBJ(apcupsd_model) {
2302 snprintf(p, p_max_size, "%s",
2303 cur->apcupsd.items[APCUPSD_MODEL]);
2305 OBJ(apcupsd_upsmode) {
2306 snprintf(p, p_max_size, "%s",
2307 cur->apcupsd.items[APCUPSD_UPSMODE]);
2309 OBJ(apcupsd_cable) {
2310 snprintf(p, p_max_size, "%s",
2311 cur->apcupsd.items[APCUPSD_CABLE]);
2313 OBJ(apcupsd_status) {
2314 snprintf(p, p_max_size, "%s",
2315 cur->apcupsd.items[APCUPSD_STATUS]);
2317 OBJ(apcupsd_linev) {
2318 snprintf(p, p_max_size, "%s",
2319 cur->apcupsd.items[APCUPSD_LINEV]);
2322 snprintf(p, p_max_size, "%s",
2323 cur->apcupsd.items[APCUPSD_LOAD]);
2325 OBJ(apcupsd_loadbar) {
2327 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2328 new_bar(obj, p, p_max_size, (int) progress);
2331 OBJ(apcupsd_loadgraph) {
2333 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2334 new_graph(obj, p, (int)progress);
2336 OBJ(apcupsd_loadgauge) {
2338 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2339 new_gauge(obj, p, (int)progress);
2342 OBJ(apcupsd_charge) {
2343 snprintf(p, p_max_size, "%s",
2344 cur->apcupsd.items[APCUPSD_CHARGE]);
2346 OBJ(apcupsd_timeleft) {
2347 snprintf(p, p_max_size, "%s",
2348 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2351 snprintf(p, p_max_size, "%s",
2352 cur->apcupsd.items[APCUPSD_TEMP]);
2354 OBJ(apcupsd_lastxfer) {
2355 snprintf(p, p_max_size, "%s",
2356 cur->apcupsd.items[APCUPSD_LASTXFER]);
2358 #endif /* APCUPSD */
2365 size_t a = strlen(p);
2368 iconv_convert(&a, buff_in, p, p_max_size);
2369 #endif /* HAVE_ICONV */
2370 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2372 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2373 #endif /* HAVE_LUA */
2375 substitute_newlines(p, a - 2);
2384 /* load any new fonts we may have had */
2385 if (need_to_load_fonts) {
2391 void evaluate(const char *text, char *p, int p_max_size)
2393 struct information *tmp_info;
2394 struct text_object subroot;
2396 tmp_info = malloc(sizeof(struct information));
2397 memcpy(tmp_info, &info, sizeof(struct information));
2398 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2399 DBGP("evaluated '%s' to '%s'", text, p);
2401 free_text_objects(&subroot, 1);
2405 double current_update_time, next_update_time, last_update_time;
2407 static void generate_text(void)
2409 struct information *cur = &info;
2416 current_update_time = get_time();
2420 /* add things to the buffer */
2426 generate_text_internal(p, max_user_text, global_root_object, cur);
2428 if (stuff_in_uppercase) {
2431 tmp_p = text_buffer;
2433 *tmp_p = toupper(*tmp_p);
2438 next_update_time += update_interval;
2439 if (next_update_time < get_time()) {
2440 next_update_time = get_time() + update_interval;
2441 } else if (next_update_time > get_time() + update_interval) {
2442 next_update_time = get_time() + update_interval;
2444 last_update_time = current_update_time;
2448 void set_update_interval(double interval)
2450 update_interval = interval;
2451 update_interval_old = interval;
2454 static inline int get_string_width(const char *s)
2456 return *s ? calc_text_width(s) : 0;
2460 static int get_string_width_special(char *s, int special_index)
2470 if ((output_methods & TO_X) == 0)
2473 p = strndup(s, text_buffer_size);
2477 if (*p == SPECIAL_CHAR) {
2478 /* shift everything over by 1 so that the special char
2479 * doesn't mess up the size calculation */
2480 for (i = 0; i < (long)strlen(p); i++) {
2481 *(p + i) = *(p + i + 1);
2483 if (specials[special_index + idx].type == GRAPH
2484 || specials[special_index + idx].type == GAUGE
2485 || specials[special_index + idx].type == BAR) {
2486 width += specials[special_index + idx].width;
2489 } else if (*p == SECRIT_MULTILINE_CHAR) {
2496 if (strlen(final) > 1) {
2497 width += calc_text_width(final);
2503 static int text_size_updater(char *s, int special_index);
2505 int last_font_height;
2506 static void update_text_area(void)
2510 if ((output_methods & TO_X) == 0)
2512 /* update text size if it isn't fixed */
2517 text_width = minimum_width;
2519 last_font_height = font_height();
2520 for_each_line(text_buffer, text_size_updater);
2522 if (text_height < minimum_height) {
2523 text_height = minimum_height;
2525 if (text_width > maximum_width && maximum_width > 0) {
2526 text_width = maximum_width;
2530 /* get text position on workarea */
2531 switch (text_alignment) {
2532 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2536 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2537 y = workarea[3] - text_height - gap_y;
2540 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2541 y = workarea[3] / 2 - text_height / 2 - gap_y;
2544 switch (text_alignment) {
2545 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2549 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2550 x = workarea[2] - text_width - gap_x;
2553 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2554 x = workarea[2] / 2 - text_width / 2 - gap_x;
2558 if (text_alignment == NONE) { // Let the WM manage the window
2565 #endif /* OWN_WINDOW */
2568 if (own_window && !fixed_pos) {
2571 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2572 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2573 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2574 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2578 /* If window size doesn't match to workarea's size,
2579 * then window probably includes panels (gnome).
2580 * Blah, doesn't work on KDE. */
2581 if (workarea[2] != window.width || workarea[3] != window.height) {
2590 /* update lua window globals */
2591 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2592 #endif /* HAVE_LUA */
2597 static int cur_x, cur_y; /* current x and y for drawing */
2599 //draw_mode also without X11 because we only need to print to stdout with FG
2600 static int draw_mode; /* FG, BG or OUTLINE */
2602 static long current_color;
2604 static int text_size_updater(char *s, int special_index)
2609 if ((output_methods & TO_X) == 0)
2611 /* get string widths and skip specials */
2614 if (*p == SPECIAL_CHAR) {
2616 w += get_string_width(s);
2619 if (specials[special_index].type == BAR
2620 || specials[special_index].type == GAUGE
2621 || specials[special_index].type == GRAPH) {
2622 w += specials[special_index].width;
2623 if (specials[special_index].height > last_font_height) {
2624 last_font_height = specials[special_index].height;
2625 last_font_height += font_height();
2627 } else if (specials[special_index].type == OFFSET) {
2628 if (specials[special_index].arg > 0) {
2629 w += specials[special_index].arg;
2631 } else if (specials[special_index].type == VOFFSET) {
2632 last_font_height += specials[special_index].arg;
2633 } else if (specials[special_index].type == GOTO) {
2634 if (specials[special_index].arg > cur_x) {
2635 w = (int) specials[special_index].arg;
2637 } else if (specials[special_index].type == TAB) {
2638 int start = specials[special_index].arg;
2639 int step = specials[special_index].width;
2641 if (!step || step < 0) {
2644 w += step - (cur_x - text_start_x - start) % step;
2645 } else if (specials[special_index].type == FONT) {
2646 selected_font = specials[special_index].font_added;
2647 if (font_height() > last_font_height) {
2648 last_font_height = font_height();
2654 } else if (*p == SECRIT_MULTILINE_CHAR) {
2657 lw = get_string_width(s);
2658 *p = SECRIT_MULTILINE_CHAR;
2660 w = lw > w ? lw : w;
2661 text_height += last_font_height;
2665 w += get_string_width(s);
2666 if (w > text_width) {
2669 if (text_width > maximum_width && maximum_width) {
2670 text_width = maximum_width;
2673 text_height += last_font_height;
2674 last_font_height = font_height();
2675 return special_index;
2679 static inline void set_foreground_color(long c)
2682 if (output_methods & TO_X) {
2684 XSetForeground(display, window.gc, c);
2688 if (output_methods & TO_NCURSES) {
2689 attron(COLOR_PAIR(c));
2691 #endif /* NCURSES */
2696 static void draw_string(const char *s)
2698 int i, i2, pos, width_of_s;
2701 char *s_with_newlines;
2707 width_of_s = get_string_width(s);
2708 s_with_newlines = strdup(s);
2709 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2710 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2711 s_with_newlines[i] = '\n';
2714 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2715 printf("%s\n", s_with_newlines);
2716 if (extra_newline) fputc('\n', stdout);
2717 fflush(stdout); /* output immediately, don't buffer */
2719 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2720 fprintf(stderr, "%s\n", s_with_newlines);
2721 fflush(stderr); /* output immediately, don't buffer */
2723 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2724 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2726 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2727 fprintf(append_fpointer, "%s\n", s_with_newlines);
2730 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2731 printw("%s", s_with_newlines);
2734 free(s_with_newlines);
2735 memset(tmpstring1, 0, text_buffer_size);
2736 memset(tmpstring2, 0, text_buffer_size);
2737 strncpy(tmpstring1, s, text_buffer_size - 1);
2742 if (output_methods & TO_X) {
2743 max = ((text_width - width_of_s) / get_string_width(" "));
2746 /* This code looks for tabs in the text and coverts them to spaces.
2747 * The trick is getting the correct number of spaces, and not going
2748 * over the window's size without forcing the window larger. */
2749 for (i = 0; i < (int) text_buffer_size; i++) {
2750 if (tmpstring1[i] == '\t') {
2752 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2753 /* guard against overrun */
2754 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2759 /* guard against overrun */
2760 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2765 if (output_methods & TO_X) {
2766 if (text_width == maximum_width) {
2767 /* this means the text is probably pushing the limit,
2768 * so we'll chop it */
2769 while (cur_x + get_string_width(tmpstring2) - text_start_x
2770 > maximum_width && strlen(tmpstring2) > 0) {
2771 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2778 if (output_methods & TO_X) {
2784 c.pixel = current_color;
2785 XQueryColor(display, DefaultColormap(display, screen), &c);
2788 c2.color.red = c.red;
2789 c2.color.green = c.green;
2790 c2.color.blue = c.blue;
2791 c2.color.alpha = fonts[selected_font].font_alpha;
2793 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2794 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2796 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2797 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2802 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2805 cur_x += width_of_s;
2808 memcpy(tmpstring1, s, text_buffer_size);
2811 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2819 int last_special_needed = -1;
2820 int orig_special_index = special_index;
2823 if (output_methods & TO_X) {
2824 font_h = font_height();
2825 cur_y += font_ascent();
2827 cur_x = text_start_x;
2831 if (*p == SECRIT_MULTILINE_CHAR) {
2832 /* special newline marker for multiline objects */
2837 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2842 /* draw string before special, unless we're dealing multiline
2844 if (last_special_applied > -1) {
2845 special_index = last_special_applied;
2853 switch (specials[special_index].type) {
2855 case HORIZONTAL_LINE:
2857 int h = specials[special_index].height;
2858 int mid = font_ascent() / 2;
2860 w = text_start_x + text_width - cur_x;
2862 XSetLineAttributes(display, window.gc, h, LineSolid,
2863 CapButt, JoinMiter);
2864 XDrawLine(display, window.drawable, window.gc, cur_x,
2865 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2871 int h = specials[special_index].height;
2872 int tmp_s = specials[special_index].arg;
2873 int mid = font_ascent() / 2;
2874 char ss[2] = { tmp_s, tmp_s };
2876 w = text_start_x + text_width - cur_x - 1;
2877 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2878 CapButt, JoinMiter);
2879 XSetDashes(display, window.gc, 0, ss, 2);
2880 XDrawLine(display, window.drawable, window.gc, cur_x,
2881 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2887 int h, bar_usage, by;
2888 if (cur_x - text_start_x > maximum_width
2889 && maximum_width > 0) {
2892 h = specials[special_index].height;
2893 bar_usage = specials[special_index].arg;
2894 by = cur_y - (font_ascent() / 2) - 1;
2899 w = specials[special_index].width;
2901 w = text_start_x + text_width - cur_x - 1;
2907 XSetLineAttributes(display, window.gc, 1, LineSolid,
2908 CapButt, JoinMiter);
2910 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2912 XFillRectangle(display, window.drawable, window.gc, cur_x,
2913 by, w * bar_usage / 255, h);
2921 case GAUGE: /* new GAUGE */
2924 unsigned long last_colour = current_color;
2926 float angle, px, py;
2930 if (cur_x - text_start_x > maximum_width
2931 && maximum_width > 0) {
2935 h = specials[special_index].height;
2936 by = cur_y - (font_ascent() / 2) - 1;
2941 w = specials[special_index].width;
2943 w = text_start_x + text_width - cur_x - 1;
2949 XSetLineAttributes(display, window.gc, 1, LineSolid,
2950 CapButt, JoinMiter);
2952 XDrawArc(display, window.drawable, window.gc,
2953 cur_x, by, w, h * 2, 0, 180*64);
2956 usage = specials[special_index].arg;
2957 angle = (M_PI)*(float)(usage)/255.;
2958 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2959 py = (float)(by+(h))-(float)(h)*sin(angle);
2961 XDrawLine(display, window.drawable, window.gc,
2962 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2970 set_foreground_color(last_colour);
2978 int h, by, i = 0, j = 0;
2980 unsigned long last_colour = current_color;
2981 unsigned long *tmpcolour = 0;
2982 if (cur_x - text_start_x > maximum_width
2983 && maximum_width > 0) {
2986 h = specials[special_index].height;
2987 by = cur_y - (font_ascent() / 2) - 1;
2992 w = specials[special_index].width;
2994 w = text_start_x + text_width - cur_x - 1;
2999 if (draw_graph_borders) {
3000 XSetLineAttributes(display, window.gc, 1, LineSolid,
3001 CapButt, JoinMiter);
3002 XDrawRectangle(display, window.drawable, window.gc,
3005 XSetLineAttributes(display, window.gc, 1, LineSolid,
3006 CapButt, JoinMiter);
3008 if (specials[special_index].last_colour != 0
3009 || specials[special_index].first_colour != 0) {
3010 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3013 for (i = w - 2; i > -1; i--) {
3014 if (specials[special_index].last_colour != 0
3015 || specials[special_index].first_colour != 0) {
3016 if (specials[special_index].tempgrad) {
3019 (int)((float)(w - 2) - specials[special_index].graph[j] *
3020 (w - 2) / (float)specials[special_index].graph_scale)
3024 (int)((float)(w - 2) - specials[special_index].graph[j] *
3025 (w - 2) / (float)specials[special_index].graph_scale)
3028 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3030 (int)((float)(w - 2) - specials[special_index].graph[j] *
3031 (w - 2) / (float)specials[special_index].graph_scale)
3035 #endif /* DEBUG_lol */
3036 XSetForeground(display, window.gc, tmpcolour[
3037 (int)((float)(w - 2) - specials[special_index].graph[j] *
3038 (w - 2) / (float)specials[special_index].graph_scale)
3041 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3044 /* this is mugfugly, but it works */
3045 XDrawLine(display, window.drawable, window.gc,
3046 cur_x + i + 1, by + h, cur_x + i + 1,
3047 round_to_int((double)by + h - specials[special_index].graph[j] *
3048 (h - 1) / specials[special_index].graph_scale));
3049 if ((w - i) / ((float) (w - 2) /
3050 (specials[special_index].graph_width)) > j
3051 && j < MAX_GRAPH_DEPTH - 3) {
3055 if (tmpcolour) free(tmpcolour);
3060 /* if (draw_mode == BG) {
3061 set_foreground_color(default_bg_color);
3062 } else if (draw_mode == OUTLINE) {
3063 set_foreground_color(default_out_color);
3065 set_foreground_color(default_fg_color);
3067 if (show_graph_range) {
3070 unsigned short int seconds = update_interval * w;
3076 unsigned short int timeunits;
3078 timeunits = seconds / 86400; seconds %= 86400;
3079 if (timeunits > 0) {
3080 asprintf(&tmp_day_str, "%dd", timeunits);
3082 tmp_day_str = strdup("");
3084 timeunits = seconds / 3600; seconds %= 3600;
3085 if (timeunits > 0) {
3086 asprintf(&tmp_hour_str, "%dh", timeunits);
3088 tmp_hour_str = strdup("");
3090 timeunits = seconds / 60; seconds %= 60;
3091 if (timeunits > 0) {
3092 asprintf(&tmp_min_str, "%dm", timeunits);
3094 tmp_min_str = strdup("");
3097 asprintf(&tmp_sec_str, "%ds", seconds);
3099 tmp_sec_str = strdup("");
3101 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3102 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3104 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3106 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3107 cur_y += font_h / 2;
3108 draw_string(tmp_str);
3114 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3118 cur_x += font_ascent() / 2;
3119 cur_y += font_h / 2;
3121 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3123 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3124 draw_string(tmp_str);
3130 set_foreground_color(last_colour);
3136 int old = font_ascent();
3138 cur_y -= font_ascent();
3139 selected_font = specials[special_index].font_added;
3141 if (cur_y + font_ascent() < cur_y + old) {
3144 cur_y += font_ascent();
3146 font_h = font_height();
3151 if (draw_mode == FG) {
3152 set_foreground_color(specials[special_index].arg);
3158 if (draw_mode == BG) {
3159 set_foreground_color(specials[special_index].arg);
3164 if (draw_mode == OUTLINE) {
3165 set_foreground_color(specials[special_index].arg);
3170 w += specials[special_index].arg;
3171 last_special_needed = special_index;
3175 cur_y += specials[special_index].arg;
3179 if (specials[special_index].arg >= 0) {
3180 cur_x = (int) specials[special_index].arg;
3182 last_special_needed = special_index;
3187 int start = specials[special_index].arg;
3188 int step = specials[special_index].width;
3190 if (!step || step < 0) {
3193 w = step - (cur_x - text_start_x - start) % step;
3194 last_special_needed = special_index;
3200 /* TODO: add back in "+ window.border_inner_margin" to the end of
3202 int pos_x = text_start_x + text_width -
3203 get_string_width_special(s, special_index);
3205 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3206 "get_string_width(p) %i gap_x %i "
3207 "specials[special_index].arg %i window.border_inner_margin %i "
3208 "window.border_width %i\n", pos_x, text_start_x, text_width,
3209 cur_x, get_string_width_special(s), gap_x,
3210 specials[special_index].arg, window.border_inner_margin,
3211 window.border_width); */
3212 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3213 cur_x = pos_x - specials[special_index].arg;
3215 last_special_needed = special_index;
3221 int pos_x = (text_width) / 2 - get_string_width_special(s,
3222 special_index) / 2 - (cur_x -
3224 /* int pos_x = text_start_x + text_width / 2 -
3225 get_string_width_special(s) / 2; */
3227 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3228 "get_string_width(p) %i gap_x %i "
3229 "specials[special_index].arg %i\n", pos_x, text_start_x,
3230 text_width, cur_x, get_string_width(s), gap_x,
3231 specials[special_index].arg); */
3232 if (pos_x > specials[special_index].arg) {
3233 w = pos_x - specials[special_index].arg;
3235 last_special_needed = special_index;
3245 if (special_index != last_special_applied) {
3248 special_index = orig_special_index;
3249 last_special_applied = -1;
3260 if (output_methods & TO_NCURSES) {
3263 #endif /* NCURSES */
3265 if (output_methods & TO_X)
3266 cur_y += font_descent();
3268 if (recurse && *recurse) {
3269 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3270 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3272 return special_index;
3275 static int draw_line(char *s, int special_index)
3278 if (output_methods & TO_X) {
3279 return draw_each_line_inner(s, special_index, -1);
3283 if (output_methods & TO_NCURSES) {
3284 return draw_each_line_inner(s, special_index, -1);
3286 #endif /* NCURSES */
3288 UNUSED(special_index);
3292 static void draw_text(void)
3296 llua_draw_pre_hook();
3297 #endif /* HAVE_LUA */
3298 if (output_methods & TO_X) {
3299 cur_y = text_start_y;
3302 if (draw_borders && window.border_width > 0) {
3303 if (stippled_borders) {
3304 char ss[2] = { stippled_borders, stippled_borders };
3305 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3306 CapButt, JoinMiter);
3307 XSetDashes(display, window.gc, 0, ss, 2);
3309 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3310 CapButt, JoinMiter);
3313 XDrawRectangle(display, window.drawable, window.gc,
3314 text_start_x - window.border_inner_margin - window.border_width,
3315 text_start_y - window.border_inner_margin - window.border_width,
3316 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3317 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3325 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3326 attron(COLOR_PAIR(COLOR_WHITE));
3327 #endif /* NCURSES */
3328 for_each_line(text_buffer, draw_line);
3329 #if defined(HAVE_LUA) && defined(X11)
3330 llua_draw_post_hook();
3331 #endif /* HAVE_LUA */
3334 static void draw_stuff(void)
3337 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3339 if (overwrite_file) {
3340 overwrite_fpointer = fopen(overwrite_file, "w");
3341 if(!overwrite_fpointer)
3342 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3345 append_fpointer = fopen(append_file, "a");
3346 if(!append_fpointer)
3347 NORM_ERR("Can't append '%s' anymore", append_file);
3350 if (output_methods & TO_X) {
3352 if (draw_shades && !draw_outline) {
3355 set_foreground_color(default_bg_color);
3366 for (i = -1; i < 2; i++) {
3367 for (j = -1; j < 2; j++) {
3368 if (i == 0 && j == 0) {
3373 set_foreground_color(default_out_color);
3374 draw_mode = OUTLINE;
3382 set_foreground_color(default_fg_color);
3387 #if defined(X11) && defined(HAVE_XDBE)
3388 if (output_methods & TO_X) {
3389 xdbe_swap_buffers();
3391 #endif /* X11 && HAVE_XDBE */
3392 if(overwrite_fpointer) {
3393 fclose(overwrite_fpointer);
3394 overwrite_fpointer = 0;
3396 if(append_fpointer) {
3397 fclose(append_fpointer);
3398 append_fpointer = 0;
3403 static void clear_text(int exposures)
3407 /* The swap action is XdbeBackground, which clears */
3411 if (display && window.window) { // make sure these are !null
3412 /* there is some extra space for borders and outlines */
3413 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3414 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3415 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3416 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3421 static int need_to_update;
3423 /* update_text() generates new text and clears old text area */
3424 static void update_text(void)
3431 if (output_methods & TO_X)
3436 llua_update_info(&info, update_interval);
3437 #endif /* HAVE_LUA */
3440 #ifdef HAVE_SYS_INOTIFY_H
3444 static void main_loop(void)
3447 #ifdef SIGNAL_BLOCKING
3448 sigset_t newmask, oldmask;
3451 #ifdef HAVE_SYS_INOTIFY_H
3452 int inotify_config_wd = -1;
3453 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3454 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3455 char inotify_buff[INOTIFY_BUF_LEN];
3456 #endif /* HAVE_SYS_INOTIFY_H */
3459 #ifdef SIGNAL_BLOCKING
3460 sigemptyset(&newmask);
3461 sigaddset(&newmask, SIGINT);
3462 sigaddset(&newmask, SIGTERM);
3463 sigaddset(&newmask, SIGUSR1);
3466 last_update_time = 0.0;
3467 next_update_time = get_time();
3469 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3470 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3471 char buf[max_user_text];
3473 get_battery_short_status(buf, max_user_text, "BAT0");
3475 update_interval = update_interval_bat;
3477 update_interval = update_interval_old;
3482 #ifdef SIGNAL_BLOCKING
3483 /* block signals. we will inspect for pending signals later */
3484 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3485 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3490 if (output_methods & TO_X) {
3493 /* wait for X event or timeout */
3495 if (!XPending(display)) {
3499 t = next_update_time - get_time();
3503 } else if (t > update_interval) {
3504 t = update_interval;
3507 tv.tv_sec = (long) t;
3508 tv.tv_usec = (long) (t * 1000000) % 1000000;
3510 FD_SET(ConnectionNumber(display), &fdsr);
3512 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3514 if (errno != EINTR) {
3515 NORM_ERR("can't select(): %s", strerror(errno));
3525 if (need_to_update) {
3527 int wx = window.x, wy = window.y;
3537 /* resize window if it isn't right size */
3539 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3540 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3541 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3542 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3543 draw_stuff(); /* redraw everything in our newly sized window */
3544 XResizeWindow(display, window.window, window.width,
3545 window.height); /* resize window */
3546 set_transparent_background(window.window);
3549 xdbe_swap_buffers();
3554 /* update lua window globals */
3555 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3556 #endif /* HAVE_LUA */
3559 /* move window if it isn't in right position */
3560 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3561 XMoveWindow(display, window.window, window.x, window.y);
3566 if (changed && window.type == TYPE_PANEL) {
3569 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3572 switch (text_alignment) {
3599 set_struts(sidenum);
3610 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3611 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3612 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3613 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3614 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3619 /* handle X events */
3620 while (XPending(display)) {
3623 XNextEvent(display, &ev);
3630 r.width = ev.xexpose.width;
3631 r.height = ev.xexpose.height;
3632 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3636 case PropertyNotify:
3638 if ( ev.xproperty.state == PropertyNewValue ) {
3639 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3645 case ReparentNotify:
3646 /* set background to ParentRelative for all parents */
3648 set_transparent_background(window.window);
3652 case ConfigureNotify:
3654 /* if window size isn't what expected, set fixed size */
3655 if (ev.xconfigure.width != window.width
3656 || ev.xconfigure.height != window.height) {
3657 if (window.width != 0 && window.height != 0) {
3661 /* clear old stuff before screwing up
3666 XWindowAttributes attrs;
3667 if (XGetWindowAttributes(display,
3668 window.window, &attrs)) {
3669 window.width = attrs.width;
3670 window.height = attrs.height;
3674 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3675 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3676 if (text_width > maximum_width
3677 && maximum_width > 0) {
3678 text_width = maximum_width;
3682 /* if position isn't what expected, set fixed pos
3683 * total_updates avoids setting fixed_pos when window
3684 * is set to weird locations when started */
3685 /* // this is broken
3686 if (total_updates >= 2 && !fixed_pos
3687 && (window.x != ev.xconfigure.x
3688 || window.y != ev.xconfigure.y)
3689 && (ev.xconfigure.x != 0
3690 || ev.xconfigure.y != 0)) {
3698 /* if an ordinary window with decorations */
3699 if ((window.type == TYPE_NORMAL &&
3700 (!TEST_HINT(window.hints,
3701 HINT_UNDECORATED))) ||
3702 window.type == TYPE_DESKTOP) {
3703 /* allow conky to hold input focus. */
3706 /* forward the click to the desktop window */
3707 XUngrabPointer(display, ev.xbutton.time);
3708 ev.xbutton.window = window.desktop;
3709 ev.xbutton.x = ev.xbutton.x_root;
3710 ev.xbutton.y = ev.xbutton.y_root;
3711 XSendEvent(display, ev.xbutton.window, False,
3712 ButtonPressMask, &ev);
3713 XSetInputFocus(display, ev.xbutton.window,
3714 RevertToParent, ev.xbutton.time);
3721 /* if an ordinary window with decorations */
3722 if ((window.type == TYPE_NORMAL)
3723 && (!TEST_HINT(window.hints,
3724 HINT_UNDECORATED))) {
3725 /* allow conky to hold input focus. */
3728 /* forward the release to the desktop window */
3729 ev.xbutton.window = window.desktop;
3730 ev.xbutton.x = ev.xbutton.x_root;
3731 ev.xbutton.y = ev.xbutton.y_root;
3732 XSendEvent(display, ev.xbutton.window, False,
3733 ButtonReleaseMask, &ev);
3742 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3743 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3745 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3746 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3748 #endif /* HAVE_XDAMAGE */
3754 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3755 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3756 #endif /* HAVE_XDAMAGE */
3758 /* XDBE doesn't seem to provide a way to clear the back buffer
3759 * without interfering with the front buffer, other than passing
3760 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3761 * using XDBE, we need to redraw the text even if it wasn't part of
3762 * the exposed area. OTOH, if we're not going to call draw_stuff at
3763 * all, then no swap happens and we can safely do nothing. */
3765 if (!XEmptyRegion(x11_stuff.region)) {
3770 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3771 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3772 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3773 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3774 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3777 XSetRegion(display, window.gc, x11_stuff.region);
3780 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3784 XDestroyRegion(x11_stuff.region);
3785 x11_stuff.region = XCreateRegion();
3789 t = (next_update_time - get_time()) * 1000000;
3790 if(t > 0) usleep((useconds_t)t);
3794 if(output_methods & TO_NCURSES) {
3803 #ifdef SIGNAL_BLOCKING
3804 /* unblock signals of interest and let handler fly */
3805 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3806 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3810 switch (g_signal_pending) {
3813 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3818 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3821 if (output_methods & TO_X) {
3822 XDestroyRegion(x11_stuff.region);
3823 x11_stuff.region = NULL;
3825 XDamageDestroy(display, x11_stuff.damage);
3826 XFixesDestroyRegion(display, x11_stuff.region2);
3827 XFixesDestroyRegion(display, x11_stuff.part);
3828 #endif /* HAVE_XDAMAGE */
3834 if(overwrite_file) {
3835 free(overwrite_file);
3844 /* Reaching here means someone set a signal
3845 * (SIGXXXX, signal_handler), but didn't write any code
3847 * If you don't want to handle a signal, don't set a handler on
3848 * it in the first place. */
3849 if (g_signal_pending) {
3850 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3854 #ifdef HAVE_SYS_INOTIFY_H
3855 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3856 inotify_config_wd = inotify_add_watch(inotify_fd,
3860 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3861 int len = 0, idx = 0;
3863 struct timeval time_to_wait;
3865 FD_ZERO(&descriptors);
3866 FD_SET(inotify_fd, &descriptors);
3868 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3870 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3871 if (FD_ISSET(inotify_fd, &descriptors)) {
3872 /* process inotify events */
3873 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3874 while (len > 0 && idx < len) {
3875 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3876 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3877 /* current_config should be reloaded */
3878 NORM_ERR("'%s' modified, reloading...", current_config);
3880 if (ev->mask & IN_IGNORED) {
3881 /* for some reason we get IN_IGNORED here
3882 * sometimes, so we need to re-add the watch */
3883 inotify_config_wd = inotify_add_watch(inotify_fd,
3890 llua_inotify_query(ev->wd, ev->mask);
3892 #endif /* HAVE_LUA */
3893 idx += INOTIFY_EVENT_SIZE + ev->len;
3897 #endif /* HAVE_SYS_INOTIFY_H */
3900 llua_update_info(&info, update_interval);
3901 #endif /* HAVE_LUA */
3902 g_signal_pending = 0;
3904 clean_up(NULL, NULL);
3906 #ifdef HAVE_SYS_INOTIFY_H
3907 if (inotify_fd != -1) {
3908 inotify_rm_watch(inotify_fd, inotify_config_wd);
3910 inotify_fd = inotify_config_wd = 0;
3912 #endif /* HAVE_SYS_INOTIFY_H */
3916 static void load_config_file_x11(const char *);
3918 void initialisation(int argc, char** argv);
3920 /* reload the config file */
3921 static void reload_config(void)
3923 char *current_config_copy = strdup(current_config);
3924 clean_up(NULL, NULL);
3925 current_config = current_config_copy;
3926 initialisation(argc_copy, argv_copy);
3929 void clean_up(void *memtofree1, void* memtofree2)
3934 if(output_methods & TO_NCURSES) {
3938 conftree_empty(currentconffile);
3939 currentconffile = NULL;
3946 timed_thread_destroy_registered_threads();
3948 if (info.cpu_usage) {
3949 free(info.cpu_usage);
3950 info.cpu_usage = NULL;
3953 if (x_initialised == YES) {
3954 if(window_created == 1) {
3955 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3956 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3957 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3958 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3962 if(x11_stuff.region) {
3963 XDestroyRegion(x11_stuff.region);
3964 x11_stuff.region = NULL;
3966 XCloseDisplay(display);
3968 if(info.x11.desktop.all_names) {
3969 free(info.x11.desktop.all_names);
3970 info.x11.desktop.all_names = NULL;
3972 if (info.x11.desktop.name) {
3973 free(info.x11.desktop.name);
3974 info.x11.desktop.name = NULL;
3978 free(fonts); //in set_default_configurations a font is set but not loaded
3984 free_update_callbacks();
3988 free_text_objects(&global_root_object, 0);
4007 free(current_config);
4010 #ifdef TCP_PORT_MONITOR
4011 tcp_portmon_clear();
4020 weather_free_info();
4023 llua_shutdown_hook();
4025 #endif /* HAVE_LUA */
4027 if (output_methods & TO_X)
4035 for (i = 0; i < special_count; i++) {
4036 if (specials[i].type == GRAPH) {
4037 free(specials[i].graph);
4045 clear_diskio_stats();
4046 if(global_cpu != NULL) {
4052 static int string_to_bool(const char *s)
4055 // Assumes an option without a true/false means true
4057 } else if (strcasecmp(s, "yes") == EQUAL) {
4059 } else if (strcasecmp(s, "true") == EQUAL) {
4061 } else if (strcasecmp(s, "1") == EQUAL) {
4068 static enum alignment string_to_alignment(const char *s)
4070 if (strcasecmp(s, "top_left") == EQUAL) {
4072 } else if (strcasecmp(s, "top_right") == EQUAL) {
4074 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4076 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4078 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4079 return BOTTOM_RIGHT;
4080 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4081 return BOTTOM_MIDDLE;
4082 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4084 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4085 return MIDDLE_RIGHT;
4086 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4087 return MIDDLE_MIDDLE;
4088 } else if (strcasecmp(s, "tl") == EQUAL) {
4090 } else if (strcasecmp(s, "tr") == EQUAL) {
4092 } else if (strcasecmp(s, "tm") == EQUAL) {
4094 } else if (strcasecmp(s, "bl") == EQUAL) {
4096 } else if (strcasecmp(s, "br") == EQUAL) {
4097 return BOTTOM_RIGHT;
4098 } else if (strcasecmp(s, "bm") == EQUAL) {
4099 return BOTTOM_MIDDLE;
4100 } else if (strcasecmp(s, "ml") == EQUAL) {
4102 } else if (strcasecmp(s, "mr") == EQUAL) {
4103 return MIDDLE_RIGHT;
4104 } else if (strcasecmp(s, "mm") == EQUAL) {
4105 return MIDDLE_MIDDLE;
4106 } else if (strcasecmp(s, "none") == EQUAL) {
4114 static void set_default_configurations_for_x(void)
4116 default_fg_color = WhitePixel(display, screen);
4117 default_bg_color = BlackPixel(display, screen);
4118 default_out_color = BlackPixel(display, screen);
4119 color0 = default_fg_color;
4120 color1 = default_fg_color;
4121 color2 = default_fg_color;
4122 color3 = default_fg_color;
4123 color4 = default_fg_color;
4124 color5 = default_fg_color;
4125 color6 = default_fg_color;
4126 color7 = default_fg_color;
4127 color8 = default_fg_color;
4128 color9 = default_fg_color;
4129 current_text_color = default_fg_color;
4133 static void set_default_configurations(void)
4140 fork_to_background = 0;
4141 total_run_times = 0;
4142 info.cpu_avg_samples = 2;
4143 info.net_avg_samples = 2;
4144 info.diskio_avg_samples = 2;
4149 format_human_readable = 1;
4159 mpd_env_host = getenv("MPD_HOST");
4160 mpd_env_port = getenv("MPD_PORT");
4162 if (!mpd_env_host || !strlen(mpd_env_host)) {
4163 mpd_set_host("localhost");
4165 /* MPD_HOST environment variable is set */
4166 char *mpd_hostpart = strchr(mpd_env_host, '@');
4167 if (!mpd_hostpart) {
4168 mpd_set_host(mpd_env_host);
4170 /* MPD_HOST contains a password */
4171 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4172 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4174 if (!strlen(mpd_hostpart + 1)) {
4175 mpd_set_host("localhost");
4177 mpd_set_host(mpd_hostpart + 1);
4180 mpd_set_password(mpd_password, 1);
4185 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4186 /* failed to set port from environment variable */
4187 mpd_set_port("6600");
4191 info.xmms2.artist = NULL;
4192 info.xmms2.album = NULL;
4193 info.xmms2.title = NULL;
4194 info.xmms2.genre = NULL;
4195 info.xmms2.comment = NULL;
4196 info.xmms2.url = NULL;
4197 info.xmms2.status = NULL;
4198 info.xmms2.playlist = NULL;
4200 use_spacer = NO_SPACER;
4202 output_methods = TO_X;
4204 output_methods = TO_STDOUT;
4207 show_graph_scale = 0;
4208 show_graph_range = 0;
4211 draw_graph_borders = 1;
4213 set_first_font("6x10");
4221 window.type = TYPE_NORMAL;
4223 strcpy(window.class_name, PACKAGE_NAME);
4224 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4226 stippled_borders = 0;
4227 window.border_inner_margin = 3;
4228 window.border_outer_margin = 1;
4229 window.border_width = 1;
4230 text_alignment = BOTTOM_LEFT;
4231 info.x11.monitor.number = 1;
4232 info.x11.monitor.current = 0;
4233 info.x11.desktop.current = 1;
4234 info.x11.desktop.number = 1;
4235 info.x11.desktop.nitems = 0;
4236 info.x11.desktop.all_names = NULL;
4237 info.x11.desktop.name = NULL;
4242 free(current_mail_spool);
4246 variable_substitute(MAIL_FILE, buf, 256);
4247 if (buf[0] != '\0') {
4248 current_mail_spool = strndup(buf, text_buffer_size);
4253 set_update_interval(3);
4254 update_interval_bat = NOBATTERY;
4255 info.music_player_interval = 1.0;
4256 stuff_in_uppercase = 0;
4257 info.users.number = 1;
4259 set_times_in_seconds(0);
4261 #ifdef TCP_PORT_MONITOR
4262 /* set default connection limit */
4263 tcp_portmon_set_max_connections(0);
4267 /* returns 1 if you can overwrite or create the file at 'path' */
4268 static _Bool overwrite_works(const char *path)
4272 if (!(filepointer = fopen(path, "w")))
4274 fclose(filepointer);
4278 /* returns 1 if you can append or create the file at 'path' */
4279 static _Bool append_works(const char *path)
4283 if (!(filepointer = fopen(path, "a")))
4285 fclose(filepointer);
4291 /* WARNING, this type not in Xlib spec */
4292 int x11_error_handler(Display *d, XErrorEvent *err)
4293 __attribute__((noreturn));
4294 int x11_error_handler(Display *d, XErrorEvent *err)
4296 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",
4298 (long unsigned)err->display,
4299 (long)err->resourceid,
4309 int x11_ioerror_handler(Display *d)
4310 __attribute__((noreturn));
4311 int x11_ioerror_handler(Display *d)
4313 NORM_ERR("X Error: Display %lx\n",
4320 static void X11_initialisation(void)
4322 if (x_initialised == YES) return;
4323 output_methods |= TO_X;
4325 set_default_configurations_for_x();
4326 x_initialised = YES;
4329 /* WARNING, this type not in Xlib spec */
4330 XSetErrorHandler(&x11_error_handler);
4331 XSetIOErrorHandler(&x11_ioerror_handler);
4335 static char **xargv = 0;
4336 static int xargc = 0;
4338 static void X11_create_window(void)
4340 if (output_methods & TO_X) {
4342 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4343 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4345 #else /* OWN_WINDOW */
4346 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4347 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4349 #endif /* OWN_WINDOW */
4353 update_text_area(); /* to position text/window on screen */
4356 if (own_window && !fixed_pos) {
4357 XMoveWindow(display, window.window, window.x, window.y);
4360 set_transparent_background(window.window);
4368 x11_stuff.region = XCreateRegion();
4370 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4371 NORM_ERR("Xdamage extension unavailable");
4373 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4374 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4375 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4376 #endif /* HAVE_XDAMAGE */
4379 update_text_area(); /* to get initial size of the window */
4382 /* setup lua window globals */
4383 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4384 #endif /* HAVE_LUA */
4388 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4389 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4390 #define CONF2(a) if (strcasecmp(name, a) == 0)
4391 #define CONF(a) else CONF2(a)
4392 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4393 || strcasecmp(name, b) == 0)
4394 #define CONF_CONTINUE 1
4395 #define CONF_BREAK 2
4396 #define CONF_BUFF_SIZE 512
4398 static FILE *open_config_file(const char *f)
4400 #ifdef CONFIG_OUTPUT
4401 if (!strcmp(f, "==builtin==")) {
4402 return conf_cookie_open();
4404 #endif /* CONFIG_OUTPUT */
4405 return fopen(f, "r");
4408 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4412 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4415 remove_comments(buf);
4420 while (*p && isspace((int) *p)) {
4424 return CONF_CONTINUE; /* empty line */
4431 while (*p2 && !isspace((int) *p2)) {
4435 *p2 = '\0'; /* break at name's end */
4442 while (*p && isspace((int) *p)) {
4448 p2 = *value + strlen(*value);
4449 while (isspace((int) *(p2 - 1))) {
4458 char load_config_file(const char *f)
4463 fp = open_config_file(f);
4467 DBGP("reading contents from config file '%s'", f);
4470 char buff[CONF_BUFF_SIZE], *name, *value;
4471 int ret = do_config_step(&line, fp, buff, &name, &value);
4472 if (ret == CONF_BREAK) {
4474 } else if (ret == CONF_CONTINUE) {
4480 /* don't listen if X is already initialised or
4481 * if we already know we don't want it */
4482 if(x_initialised != YES) {
4483 if (string_to_bool(value)) {
4484 output_methods &= TO_X;
4486 output_methods &= ~TO_X;
4487 x_initialised = NEVER;
4492 if (!value || x_initialised == YES) {
4497 disp = strdup(value);
4502 if (window.type == TYPE_DOCK)
4505 #endif /*OWN_WINDOW */
4507 int a = string_to_alignment(value);
4518 CONF("background") {
4519 fork_to_background = string_to_bool(value);
4522 CONF2("background") {
4523 fork_to_background = string_to_bool(value);
4527 CONF("show_graph_scale") {
4528 show_graph_scale = string_to_bool(value);
4530 CONF("show_graph_range") {
4531 show_graph_range = string_to_bool(value);
4533 CONF("border_inner_margin") {
4535 window.border_inner_margin = strtol(value, 0, 0);
4536 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4541 CONF("border_outer_margin") {
4543 window.border_outer_margin = strtol(value, 0, 0);
4544 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4549 CONF("border_width") {
4551 window.border_width = strtol(value, 0, 0);
4552 if (window.border_width < 0) window.border_width = 0;
4558 #define TEMPLATE_CONF(n) \
4559 CONF("template"#n) { \
4560 if (set_template(n, value)) \
4575 parse_global_imap_mail_args(value);
4582 parse_global_pop3_mail_args(value);
4587 CONF("default_bar_size") {
4590 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4597 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4601 CONF("default_graph_size") {
4604 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4611 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4614 CONF("default_gauge_size") {
4617 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4624 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4631 mpd_set_host(value);
4637 if (value && mpd_set_port(value)) {
4641 CONF("mpd_password") {
4643 mpd_set_password(value, 0);
4649 CONF("music_player_interval") {
4651 info.music_player_interval = strtod(value, 0);
4657 CONF("sensor_device") {
4659 sensor_device = strtol(value, 0, 0);
4665 CONF("cpu_avg_samples") {
4667 cpu_avg_samples = strtol(value, 0, 0);
4668 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4671 info.cpu_avg_samples = cpu_avg_samples;
4677 CONF("net_avg_samples") {
4679 net_avg_samples = strtol(value, 0, 0);
4680 if (net_avg_samples < 1 || net_avg_samples > 14) {
4683 info.net_avg_samples = net_avg_samples;
4689 CONF("diskio_avg_samples") {
4691 diskio_avg_samples = strtol(value, 0, 0);
4692 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4695 info.diskio_avg_samples = diskio_avg_samples;
4703 CONF("double_buffer") {
4704 use_xdbe = string_to_bool(value);
4708 CONF("override_utf8_locale") {
4709 utf8_mode = string_to_bool(value);
4711 CONF("draw_borders") {
4712 draw_borders = string_to_bool(value);
4714 CONF("draw_graph_borders") {
4715 draw_graph_borders = string_to_bool(value);
4717 CONF("draw_shades") {
4718 draw_shades = string_to_bool(value);
4720 CONF("draw_outline") {
4721 draw_outline = string_to_bool(value);
4724 CONF("times_in_seconds") {
4725 set_times_in_seconds(string_to_bool(value));
4727 CONF("out_to_console") {
4728 if(string_to_bool(value)) {
4729 output_methods |= TO_STDOUT;
4731 output_methods &= ~TO_STDOUT;
4734 CONF("extra_newline") {
4735 extra_newline = string_to_bool(value);
4737 CONF("out_to_stderr") {
4738 if(string_to_bool(value))
4739 output_methods |= TO_STDERR;
4742 CONF("out_to_ncurses") {
4743 if(string_to_bool(value)) {
4746 output_methods |= TO_NCURSES;
4750 CONF("overwrite_file") {
4751 if(overwrite_file) {
4752 free(overwrite_file);
4755 if(overwrite_works(value)) {
4756 overwrite_file = strdup(value);
4757 output_methods |= OVERWRITE_FILE;
4759 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4761 CONF("append_file") {
4766 if(append_works(value)) {
4767 append_file = strdup(value);
4768 output_methods |= APPEND_FILE;
4770 NORM_ERR("append_file won't be able to create/append '%s'", value);
4772 CONF("use_spacer") {
4774 if (strcasecmp(value, "left") == EQUAL) {
4775 use_spacer = LEFT_SPACER;
4776 } else if (strcasecmp(value, "right") == EQUAL) {
4777 use_spacer = RIGHT_SPACER;
4778 } else if (strcasecmp(value, "none") == EQUAL) {
4779 use_spacer = NO_SPACER;
4781 use_spacer = string_to_bool(value);
4782 NORM_ERR("use_spacer should have an argument of left, right, or"
4783 " none. '%s' seems to be some form of '%s', so"
4784 " defaulting to %s.", value,
4785 use_spacer ? "true" : "false",
4786 use_spacer ? "right" : "none");
4788 use_spacer = RIGHT_SPACER;
4790 use_spacer = NO_SPACER;
4794 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4795 use_spacer = RIGHT_SPACER;
4801 use_xft = string_to_bool(value);
4805 set_first_font(value);
4809 if (value && font_count >= 0) {
4810 fonts[0].font_alpha = atof(value) * 65535.0;
4817 if (string_to_bool(value)) {
4818 NORM_ERR("Xft not enabled at compile time");
4822 /* xftfont silently ignored when no Xft */
4825 /* xftalpha is silently ignored when no Xft */
4830 set_first_font(value);
4838 gap_x = atoi(value);
4845 gap_y = atoi(value);
4851 CONF("mail_spool") {
4855 variable_substitute(value, buffer, 256);
4857 if (buffer[0] != '\0') {
4858 if (current_mail_spool) {
4859 free(current_mail_spool);
4861 current_mail_spool = strndup(buffer, text_buffer_size);
4868 CONF("minimum_size") {
4870 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4872 if (sscanf(value, "%d", &minimum_width) != 1) {
4880 CONF("maximum_width") {
4882 if (sscanf(value, "%d", &maximum_width) != 1) {
4890 CONF("no_buffers") {
4891 no_buffers = string_to_bool(value);
4893 CONF("top_name_width") {
4894 if (set_top_name_width(value))
4897 CONF("top_cpu_separate") {
4898 cpu_separate = string_to_bool(value);
4900 CONF("short_units") {
4901 short_units = string_to_bool(value);
4903 CONF("format_human_readable") {
4904 format_human_readable = string_to_bool(value);
4907 CONF("hddtemp_host") {
4908 set_hddtemp_host(value);
4910 CONF("hddtemp_port") {
4911 set_hddtemp_port(value);
4913 #endif /* HDDTEMP */
4914 CONF("pad_percents") {
4915 pad_percents = atoi(value);
4919 CONF("own_window") {
4921 own_window = string_to_bool(value);
4924 CONF("own_window_class") {
4926 memset(window.class_name, 0, sizeof(window.class_name));
4927 strncpy(window.class_name, value,
4928 sizeof(window.class_name) - 1);
4931 CONF("own_window_title") {
4933 memset(window.title, 0, sizeof(window.title));
4934 strncpy(window.title, value, sizeof(window.title) - 1);
4937 CONF("own_window_transparent") {
4939 set_transparent = string_to_bool(value);
4942 CONF("own_window_hints") {
4944 char *p_hint, *p_save;
4945 char delim[] = ", ";
4947 /* tokenize the value into individual hints */
4948 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4950 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4951 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4952 SET_HINT(window.hints, HINT_UNDECORATED);
4953 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4954 SET_HINT(window.hints, HINT_BELOW);
4955 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4956 SET_HINT(window.hints, HINT_ABOVE);
4957 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4958 SET_HINT(window.hints, HINT_STICKY);
4959 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4960 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4961 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4962 SET_HINT(window.hints, HINT_SKIP_PAGER);
4967 p_hint = strtok_r(NULL, delim, &p_save);
4968 } while (p_hint != NULL);
4974 CONF("own_window_type") {
4976 if (strncmp(value, "normal", 6) == EQUAL) {
4977 window.type = TYPE_NORMAL;
4978 } else if (strncmp(value, "desktop", 7) == EQUAL) {
4979 window.type = TYPE_DESKTOP;
4980 } else if (strncmp(value, "dock", 4) == EQUAL) {
4981 window.type = TYPE_DOCK;
4982 text_alignment = TOP_LEFT;
4983 } else if (strncmp(value, "panel", 5) == EQUAL) {
4984 window.type = TYPE_PANEL;
4985 } else if (strncmp(value, "override", 8) == EQUAL) {
4986 window.type = TYPE_OVERRIDE;
4995 CONF("stippled_borders") {
4997 stippled_borders = strtol(value, 0, 0);
4999 stippled_borders = 4;
5003 CONF("imlib_cache_size") {
5005 cimlib_set_cache_size(atoi(value));
5008 CONF("imlib_cache_flush_interval") {
5010 cimlib_set_cache_flush_interval(atoi(value));
5015 CONF("update_interval_on_battery") {
5017 update_interval_bat = strtod(value, 0);
5022 CONF("update_interval") {
5024 set_update_interval(strtod(value, 0));
5028 if (info.music_player_interval == 0) {
5029 // default to update_interval
5030 info.music_player_interval = update_interval;
5033 CONF("total_run_times") {
5035 total_run_times = strtod(value, 0);
5041 stuff_in_uppercase = string_to_bool(value);
5043 CONF("max_specials") {
5045 max_specials = atoi(value);
5050 CONF("max_user_text") {
5052 max_user_text = atoi(value);
5057 CONF("text_buffer_size") {
5059 text_buffer_size = atoi(value);
5060 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5061 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5062 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5070 if (output_methods & TO_X) {
5071 X11_initialisation();
5080 global_text = (char *) malloc(1);
5081 global_text[0] = '\0';
5084 unsigned int l = strlen(global_text);
5086 char buf[CONF_BUFF_SIZE];
5088 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5094 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5102 /* Check for continuation of \\-\n. */
5103 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5104 global_text[l-1] = '\0';
5108 global_text = (char *) realloc(global_text, l + bl + 1);
5109 strcat(global_text, buf);
5111 if (strlen(global_text) > max_user_text) {
5115 global_text_lines = line + 1;
5118 #ifdef TCP_PORT_MONITOR
5119 CONF("max_port_monitor_connections") {
5121 if (!value || (sscanf(value, "%d", &max) != 1)) {
5122 /* an error. use default, warn and continue. */
5123 tcp_portmon_set_max_connections(0);
5125 } else if (tcp_portmon_set_max_connections(max)) {
5126 /* max is < 0, default has been set*/
5131 CONF("if_up_strictness") {
5133 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5134 ifup_strictness = IFUP_UP;
5135 } else if (strcasecmp(value, "up") == EQUAL) {
5136 ifup_strictness = IFUP_UP;
5137 } else if (strcasecmp(value, "link") == EQUAL) {
5138 ifup_strictness = IFUP_LINK;
5139 } else if (strcasecmp(value, "address") == EQUAL) {
5140 ifup_strictness = IFUP_ADDR;
5142 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5143 ifup_strictness = IFUP_UP;
5147 CONF("temperature_unit") {
5149 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5150 } else if (set_temp_output_unit(value)) {
5151 NORM_ERR("temperature_unit: incorrect argument");
5158 char *ptr = strtok(value, " ");
5161 ptr = strtok(NULL, " ");
5168 CONF("lua_draw_hook_pre") {
5170 llua_set_draw_pre_hook(value);
5175 CONF("lua_draw_hook_post") {
5177 llua_set_draw_post_hook(value);
5182 CONF("lua_startup_hook") {
5184 llua_set_startup_hook(value);
5189 CONF("lua_shutdown_hook") {
5191 llua_set_shutdown_hook(value);
5197 #endif /* HAVE_LUA */
5209 CONF("default_color"){}
5210 CONF3("default_shade_color", "default_shadecolor"){}
5211 CONF3("default_outline_color", "default_outlinecolor") {}
5212 CONF("own_window_colour") {}
5215 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5221 if (info.music_player_interval == 0) {
5222 // default to update_interval
5223 info.music_player_interval = update_interval;
5225 if (!global_text) { // didn't supply any text
5226 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5228 if (!output_methods) {
5229 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5231 #if defined(NCURSES)
5233 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5234 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5235 output_methods &= ~TO_NCURSES;
5239 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5240 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5241 output_methods &= ~(TO_STDOUT | TO_STDERR);
5243 #endif /* NCURSES */
5248 static void load_config_file_x11(const char *f)
5253 fp = open_config_file(f);
5257 DBGP("reading contents from config file '%s'", f);
5260 char buff[CONF_BUFF_SIZE], *name, *value;
5261 int ret = do_config_step(&line, fp, buff, &name, &value);
5262 if (ret == CONF_BREAK) {
5264 } else if (ret == CONF_CONTINUE) {
5269 X11_initialisation();
5270 if (x_initialised == YES) {
5272 color0 = get_x11_color(value);
5279 X11_initialisation();
5280 if (x_initialised == YES) {
5282 color1 = get_x11_color(value);
5289 X11_initialisation();
5290 if (x_initialised == YES) {
5292 color2 = get_x11_color(value);
5299 X11_initialisation();
5300 if (x_initialised == YES) {
5302 color3 = get_x11_color(value);
5309 X11_initialisation();
5310 if (x_initialised == YES) {
5312 color4 = get_x11_color(value);
5319 X11_initialisation();
5320 if (x_initialised == YES) {
5322 color5 = get_x11_color(value);
5329 X11_initialisation();
5330 if (x_initialised == YES) {
5332 color6 = get_x11_color(value);
5339 X11_initialisation();
5340 if (x_initialised == YES) {
5342 color7 = get_x11_color(value);
5349 X11_initialisation();
5350 if (x_initialised == YES) {
5352 color8 = get_x11_color(value);
5359 X11_initialisation();
5360 if (x_initialised == YES) {
5362 color9 = get_x11_color(value);
5368 CONF("default_color") {
5369 X11_initialisation();
5370 if (x_initialised == YES) {
5372 default_fg_color = get_x11_color(value);
5378 CONF3("default_shade_color", "default_shadecolor") {
5379 X11_initialisation();
5380 if (x_initialised == YES) {
5382 default_bg_color = get_x11_color(value);
5388 CONF3("default_outline_color", "default_outlinecolor") {
5389 X11_initialisation();
5390 if (x_initialised == YES) {
5392 default_out_color = get_x11_color(value);
5399 CONF("own_window_colour") {
5400 X11_initialisation();
5401 if (x_initialised == YES) {
5403 background_colour = get_x11_color(value);
5405 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5406 "'#' for hex colours");
5412 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5413 if(output_methods & TO_X) {
5414 X11_initialisation();
5423 #undef CONF_CONTINUE
5424 #undef CONF_BUFF_SIZE
5432 static void print_help(const char *prog_name) {
5433 printf("Usage: %s [OPTION]...\n"
5434 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5435 "window. Command line options will override configurations defined in config\n"
5437 " -v, --version version\n"
5438 " -q, --quiet quiet mode\n"
5439 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5440 " -c, --config=FILE config file to load\n"
5441 #ifdef CONFIG_OUTPUT
5442 " -C, --print-config print the builtin default config to stdout\n"
5443 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5445 " -d, --daemonize daemonize, fork to background\n"
5446 " -h, --help help\n"
5448 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5449 " -f, --font=FONT font to use\n"
5450 " -X, --display=DISPLAY X11 display to use\n"
5452 " -o, --own-window create own window to draw\n"
5455 " -b, --double-buffer double buffer (prevents flickering)\n"
5457 " -w, --window-id=WIN_ID window id to draw\n"
5458 " -x X x position\n"
5459 " -y Y y position\n"
5461 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5462 " -u, --interval=SECS update interval\n"
5463 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5464 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5469 /* : means that character before that takes an argument */
5470 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5480 #ifdef CONFIG_OUTPUT
5485 static const struct option longopts[] = {
5486 { "help", 0, NULL, 'h' },
5487 { "version", 0, NULL, 'V' },
5488 { "debug", 0, NULL, 'D' },
5489 { "config", 1, NULL, 'c' },
5490 #ifdef CONFIG_OUTPUT
5491 { "print-config", 0, NULL, 'C' },
5493 { "daemonize", 0, NULL, 'd' },
5495 { "alignment", 1, NULL, 'a' },
5496 { "font", 1, NULL, 'f' },
5497 { "display", 1, NULL, 'X' },
5499 { "own-window", 0, NULL, 'o' },
5502 { "double-buffer", 0, NULL, 'b' },
5504 { "window-id", 1, NULL, 'w' },
5506 { "text", 1, NULL, 't' },
5507 { "interval", 0, NULL, 'u' },
5508 { "pause", 0, NULL, 'p' },
5512 void initialisation(int argc, char **argv) {
5513 struct sigaction act, oact;
5515 set_default_configurations();
5516 load_config_file(current_config);
5517 currentconffile = conftree_add(currentconffile, current_config);
5519 /* init specials array */
5520 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5521 NORM_ERR("failed to create specials array");
5525 if (current_mail_spool == NULL) {
5528 variable_substitute(MAIL_FILE, buf, 256);
5530 if (buf[0] != '\0') {
5531 current_mail_spool = strndup(buf, text_buffer_size);
5536 /* handle other command line arguments */
5538 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5539 || defined(__NetBSD__)
5540 optind = optreset = 1;
5545 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5546 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5547 "kvm_open")) == NULL) {
5548 CRIT_ERR(NULL, NULL, "cannot read kvm");
5553 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5562 fork_to_background = 1;
5565 global_debug_level++;
5569 set_first_font(optarg);
5572 text_alignment = string_to_alignment(optarg);
5591 global_text = strndup(optarg, max_user_text);
5592 convert_escapes(global_text);
5596 update_interval = strtod(optarg, 0);
5597 update_interval_old = update_interval;
5598 if (info.music_player_interval == 0) {
5599 // default to update_interval
5600 info.music_player_interval = update_interval;
5605 total_run_times = strtod(optarg, 0);
5609 gap_x = atoi(optarg);
5613 gap_y = atoi(optarg);
5617 startup_pause = atoi(optarg);
5618 sleep(startup_pause);
5628 if (output_methods & TO_X) {
5629 load_config_file_x11(current_config);
5633 /* generate text and get initial size */
5634 extract_variable_text(global_text);
5641 if (fork_to_background) {
5646 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5653 fprintf(stderr, "\n");
5658 /* parent process */
5659 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5666 start_update_threading();
5668 text_buffer = malloc(max_user_text);
5669 memset(text_buffer, 0, max_user_text);
5670 tmpstring1 = malloc(text_buffer_size);
5671 memset(tmpstring1, 0, text_buffer_size);
5672 tmpstring2 = malloc(text_buffer_size);
5673 memset(tmpstring2, 0, text_buffer_size);
5678 X11_create_window();
5681 llua_setup_info(&info, update_interval);
5682 #endif /* HAVE_LUA */
5687 /* Set signal handlers */
5688 act.sa_handler = signal_handler;
5689 sigemptyset(&act.sa_mask);
5692 act.sa_flags |= SA_RESTART;
5695 if ( sigaction(SIGINT, &act, &oact) < 0
5696 || sigaction(SIGALRM, &act, &oact) < 0
5697 || sigaction(SIGUSR1, &act, &oact) < 0
5698 || sigaction(SIGHUP, &act, &oact) < 0
5699 || sigaction(SIGTERM, &act, &oact) < 0) {
5700 NORM_ERR("error setting signal handler: %s", strerror(errno));
5704 llua_startup_hook();
5705 #endif /* HAVE_LUA */
5708 int main(int argc, char **argv)
5717 g_signal_pending = 0;
5718 max_user_text = MAX_USER_TEXT_DEFAULT;
5720 memset(&info, 0, sizeof(info));
5724 #ifdef TCP_PORT_MONITOR
5725 /* set default connection limit */
5726 tcp_portmon_set_max_connections(0);
5729 /* handle command line parameters that don't change configs */
5731 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5732 || ((s = getenv("LANG")) && *s)) {
5733 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5735 NORM_ERR("malloc failed");
5737 for (x = 0; x < strlen(s); x++) {
5738 temp[x] = tolower(s[x]);
5741 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5747 if (!setlocale(LC_CTYPE, "")) {
5748 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5752 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5763 if (current_config) {
5764 free(current_config);
5766 current_config = strndup(optarg, max_user_text);
5769 freopen("/dev/null", "w", stderr);
5772 print_help(argv[0]);
5774 #ifdef CONFIG_OUTPUT
5781 window.window = strtol(optarg, 0, 0);
5786 disp = strdup(optarg);
5795 /* check if specified config file is valid */
5796 if (current_config) {
5798 if (stat(current_config, &sb) ||
5799 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5800 NORM_ERR("invalid configuration file '%s'\n", current_config);
5801 free(current_config);
5806 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5808 if (!current_config) {
5809 /* load default config file */
5810 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5813 /* Try to use personal config file first */
5814 to_real_path(buf, CONFIG_FILE);
5815 if (buf[0] && (fp = fopen(buf, "r"))) {
5816 current_config = strndup(buf, max_user_text);
5820 /* Try to use system config file if personal config not readable */
5821 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5822 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5826 /* No readable config found */
5827 if (!current_config) {
5828 #ifdef CONFIG_OUTPUT
5829 current_config = strdup("==builtin==");
5830 NORM_ERR("no readable personal or system-wide config file found,"
5831 " using builtin default");
5833 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5834 #endif /* ! CONF_OUTPUT */
5839 /* Load xoap keys, if existing */
5843 #ifdef HAVE_SYS_INOTIFY_H
5844 inotify_fd = inotify_init1(IN_NONBLOCK);
5845 #endif /* HAVE_SYS_INOTIFY_H */
5847 initialisation(argc, argv);
5851 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5859 void alarm_handler(void) {
5861 kill(childpid, SIGTERM);
5865 static void signal_handler(int sig)
5867 /* signal handler is light as a feather, as it should be.
5868 * we will poll g_signal_pending with each loop of conky
5869 * and do any signal processing there, NOT here (except
5870 * SIGALRM because this is caused when conky is hanging) */
5871 if(sig == SIGALRM) {
5874 g_signal_pending = sig;