1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* disable inotify auto reload feature if desired */
151 int disable_auto_reload = 0;
153 /* two strings for internal use */
154 static char *tmpstring1, *tmpstring2;
156 /* variables holding various config settings */
158 int format_human_readable;
165 int top_cpu, top_mem, top_time;
173 static int extra_newline;
174 enum x_initialiser_state x_initialised = NO;
175 static volatile int g_signal_pending;
176 /* Update interval */
177 double update_interval;
178 double update_interval_old;
179 double update_interval_bat;
180 void *global_cpu = NULL;
181 unsigned int max_text_width = 0;
186 /* prototypes for internally used functions */
187 static void signal_handler(int);
188 static void print_version(void) __attribute__((noreturn));
189 static void reload_config(void);
191 static void print_version(void)
193 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
195 printf("\nCompiled in features:\n\n"
196 "System config file: "SYSTEM_CONFIG_FILE"\n"
197 "Package library path: "PACKAGE_LIBDIR"\n\n"
201 " * Xdamage extension\n"
202 # endif /* HAVE_XDAMAGE */
204 " * XDBE (double buffer extension)\n"
205 # endif /* HAVE_XDBE */
211 # endif /* USE_ARGB */
213 "\n Music detection:\n"
216 #endif /* AUDACIOUS */
232 #endif /* HAVE_OPENMP */
239 #ifdef TCP_PORT_MONITOR
241 #endif /* TCP_PORT_MONITOR */
244 #endif /* HAVE_CURL */
249 " * Weather (METAR)\n"
251 " * Weather (XOAP)\n"
256 #endif /* HAVE_IWLIB */
258 " * support for IBM/Lenovo notebooks\n"
268 #endif /* CONFIG_OUTPUT */
273 " * ALSA mixer support\n"
274 #endif /* MIXER_IS_ALSA */
287 #ifdef HAVE_LUA_CAIRO
289 #endif /* HAVE_LUA_CAIRO */
290 #ifdef HAVE_LUA_IMLIB2
293 #endif /* HAVE_LUA */
299 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
304 static void X11_create_window(void);
305 static void X11_initialisation(void);
307 struct _x11_stuff_s {
311 XserverRegion region2, part;
312 int event_base, error_base;
318 static int text_start_x, text_start_y; /* text start position in window */
319 static int text_width, text_height;
335 /* display to connect to */
336 static char *disp = NULL;
340 /* struct that has all info to be shared between
341 * instances of the same text object */
342 struct information info;
344 /* path to config file */
345 char *current_config;
347 /* set to 1 if you want all text to be in uppercase */
348 static unsigned int stuff_in_uppercase;
350 /* Run how many times? */
351 static unsigned long total_run_times;
354 static int fork_to_background;
356 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
358 /* filenames for output */
359 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
360 char *append_file = NULL; FILE *append_fpointer = NULL;
364 static int show_graph_scale;
365 static int show_graph_range;
367 /* Position on the screen */
368 static int text_alignment;
369 static int gap_x, gap_y;
372 static int draw_borders;
373 static int draw_graph_borders;
374 static int stippled_borders;
376 int get_stippled_borders(void)
378 return stippled_borders;
381 static int draw_shades, draw_outline;
383 long default_fg_color, default_bg_color, default_out_color;
385 /* create own window or draw stuff to root? */
386 static int set_transparent = 0;
389 static int own_window = 0;
390 static int background_colour = 0;
392 /* fixed size/pos is set if wm/user changes them */
393 static int fixed_size = 0, fixed_pos = 0;
396 static int minimum_width, minimum_height;
397 static int maximum_width;
402 static int sensor_device;
405 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
408 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
409 unsigned int max_user_text;
411 /* maximum size of individual text buffers, ie $exec buffer size */
412 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
417 /* no buffers in used memory? */
420 /* pad percentages to decimals? */
421 static int pad_percents = 0;
423 static char *global_text = 0;
425 char *get_global_text(void)
430 long global_text_lines;
432 static int total_updates;
433 static int updatereset;
435 void set_updatereset(int i)
440 int get_updatereset(void)
445 int check_contains(char *f, char *s)
448 FILE *where = open_file(f, 0);
453 while (fgets(buf1, 256, where)) {
454 if (strstr(buf1, s)) {
461 NORM_ERR("Could not open the file");
466 #define SECRIT_MULTILINE_CHAR '\x02'
468 int calc_text_width(const char *s)
470 size_t slen = strlen(s);
473 if ((output_methods & TO_X) == 0) {
483 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
484 (const FcChar8 *) s, slen, &gi);
486 XftTextExtents8(display, fonts[selected_font].xftfont,
487 (const FcChar8 *) s, slen, &gi);
493 return XTextWidth(fonts[selected_font].font, s, slen);
498 /* formatted text to render on screen, generated in generate_text(),
499 * drawn in draw_stuff() */
501 static char *text_buffer;
503 /* quite boring functions */
505 static inline void for_each_line(char *b, int f(char *, int))
508 int special_index = 0; /* specials index */
510 for (ps = b, pe = b; *pe; pe++) {
513 special_index = f(ps, special_index);
520 f(ps, special_index);
524 static void convert_escapes(char *buf)
526 char *p = buf, *s = buf;
533 } else if (*s == '\\') {
544 /* Prints anything normally printed with snprintf according to the current value
545 * of use_spacer. Actually slightly more flexible than snprintf, as you can
546 * safely specify the destination buffer as one of your inputs. */
547 int spaced_print(char *buf, int size, const char *format, int width, ...)
556 tempbuf = malloc(size * sizeof(char));
558 // Passes the varargs along to vsnprintf
559 va_start(argp, width);
560 vsnprintf(tempbuf, size, format, argp);
563 switch (use_spacer) {
565 len = snprintf(buf, size, "%s", tempbuf);
568 len = snprintf(buf, size, "%*s", width, tempbuf);
571 len = snprintf(buf, size, "%-*s", width, tempbuf);
578 /* print percentage values
580 * - i.e., unsigned values between 0 and 100
581 * - respect the value of pad_percents */
582 int percent_print(char *buf, int size, unsigned value)
584 return spaced_print(buf, size, "%u", pad_percents, value);
587 /* converts from bytes to human readable format (K, M, G, T)
589 * The algorithm always divides by 1024, as unit-conversion of byte
590 * counts suggests. But for output length determination we need to
591 * compare with 1000 here, as we print in decimal form. */
592 void human_readable(long long num, char *buf, int size)
594 const char **suffix = suffixes;
600 /* Possibly just output as usual, for example for stdout usage */
601 if (!format_human_readable) {
602 spaced_print(buf, size, "%d", 6, round_to_int(num));
613 if (llabs(num) < 1000LL) {
614 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
618 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
626 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
628 * The goal is to always have a significance of 3, by
629 * adjusting the decimal part of the number. Sample output:
633 * so the point of alignment resides between number and unit. The
634 * upside of this is that there is minimal padding necessary, though
635 * there should be a way to make alignment take place at the decimal
636 * dot (then with fixed width decimal part).
638 * Note the repdigits below: when given a precision value, printf()
639 * rounds the float to it, not just cuts off the remaining digits. So
640 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
641 * printed with a precision of 0. Yay. */
643 precision = 0; /* print 100-999 without decimal part */
645 precision = 1; /* print 10-99 with one decimal place */
647 precision = 2; /* print 0-9 with two decimal places */
649 spaced_print(buf, size, format, width, precision, fnum, *suffix);
652 /* global object list root element */
653 static struct text_object global_root_object;
655 static long current_text_color;
657 void set_current_text_color(long colour)
659 current_text_color = colour;
662 long get_current_text_color(void)
664 return current_text_color;
667 //adds newstring to to the tree unless you can already see it when travelling back.
668 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
669 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
670 struct conftree* node;
671 struct conftree* node2;
673 for(node = previous; node != NULL; node = node->back) {
674 if(strcmp(node->string, newstring) == 0) {
678 node = malloc(sizeof(struct conftree));
679 if (previous != NULL) {
680 if(previous->vert_next == NULL) {
681 previous->vert_next = node;
683 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
684 node2->horz_next = node;
687 node->string = strdup(newstring);
688 node->horz_next = NULL;
689 node->vert_next = NULL;
690 node->back = previous;
694 void conftree_empty(struct conftree* tree) {
696 conftree_empty(tree->horz_next);
697 conftree_empty(tree->vert_next);
703 struct conftree *currentconffile;
705 static void extract_variable_text(const char *p)
707 free_text_objects(&global_root_object, 0);
721 extract_variable_text_internal(&global_root_object, p);
724 void parse_conky_vars(struct text_object *root, const char *txt,
725 char *p, int p_max_size, struct information *cur)
727 extract_variable_text_internal(root, txt);
728 generate_text_internal(p, p_max_size, *root, cur);
731 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
732 * multiline objects like $exec work with $align[rc] and friends
734 void substitute_newlines(char *p, long l)
738 while (p && *p && p < s + l) {
740 /* only substitute if it's not the last newline */
741 *p = SECRIT_MULTILINE_CHAR;
747 void generate_text_internal(char *p, int p_max_size,
748 struct text_object root, struct information *cur)
750 struct text_object *obj;
752 int need_to_load_fonts = 0;
755 /* for the OBJ_top* handler */
756 struct process **needed = 0;
759 char buff_in[p_max_size];
761 #endif /* HAVE_ICONV */
765 while (obj && p_max_size > 0) {
766 needed = 0; /* reset for top stuff */
768 /* IFBLOCK jumping algorithm
770 * This is easier as it looks like:
771 * - each IF checks it's condition
772 * - on FALSE: call DO_JUMP
773 * - on TRUE: don't care
774 * - each ELSE calls DO_JUMP unconditionally
775 * - each ENDIF is silently being ignored
778 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
779 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
780 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
781 * regularly, "obj" is being updated to point to obj->next, so object parsing
782 * continues right after the corresponding ELSE or ENDIF. This means that if we
783 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
784 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
785 * jumped, and there is nothing to do.
789 if (obj->ifblock_next) \
790 obj = obj->ifblock_next; \
793 #define OBJ(a) break; case OBJ_##a:
797 NORM_ERR("not implemented obj type %d", obj->type);
799 print_read_tcp(obj, p, p_max_size);
803 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
805 #endif /* !__OpenBSD__ */
809 ok = get_freq(p, p_max_size, "%.0f", 1,
817 ok = get_freq(p, p_max_size, "%'.2f", 1000,
820 /* OpenBSD has no such flag (SUSv2) */
821 ok = get_freq(p, p_max_size, "%.2f", 1000,
823 #endif /* __OpenBSD */
826 #if defined(__linux__)
828 print_voltage_mv(obj, p, p_max_size);
831 print_voltage_v(obj, p, p_max_size);
835 OBJ(wireless_essid) {
836 print_wireless_essid(obj, p, p_max_size);
839 print_wireless_mode(obj, p, p_max_size);
841 OBJ(wireless_bitrate) {
842 print_wireless_bitrate(obj, p, p_max_size);
845 print_wireless_ap(obj, p, p_max_size);
847 OBJ(wireless_link_qual) {
848 print_wireless_link_qual(obj, p, p_max_size);
850 OBJ(wireless_link_qual_max) {
851 print_wireless_link_qual_max(obj, p, p_max_size);
853 OBJ(wireless_link_qual_perc) {
854 print_wireless_link_qual_perc(obj, p, p_max_size);
856 OBJ(wireless_link_bar) {
857 print_wireless_link_bar(obj, p, p_max_size);
859 #endif /* HAVE_IWLIB */
861 #endif /* __linux__ */
865 get_acpi_fan(p, p_max_size);
868 get_acpi_ac_adapter(p, p_max_size);
871 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
874 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
876 OBJ(battery_percent) {
877 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
880 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
883 get_battery_short_status(p, p_max_size, obj->data.s);
885 #endif /* __OpenBSD__ */
888 human_readable(cur->buffers * 1024, p, 255);
891 human_readable(cur->cached * 1024, p, 255);
893 OBJ(cmdline_to_pid) {
894 print_cmdline_to_pid(obj, p, p_max_size);
897 if (obj->data.i > info.cpu_count) {
898 NORM_ERR("obj->data.i %i info.cpu_count %i",
899 obj->data.i, info.cpu_count);
900 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
902 percent_print(p, p_max_size,
903 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
906 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
908 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
912 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
915 print_loadgraph(obj, p, p_max_size);
919 new_fg(p, obj->data.l);
953 #if defined(__linux__)
955 snprintf(p, p_max_size, "%s",
956 get_disk_protect_queue(obj->data.s));
959 print_i8k_version(obj, p, p_max_size);
962 print_i8k_bios(obj, p, p_max_size);
965 print_i8k_serial(obj, p, p_max_size);
968 print_i8k_cpu_temp(obj, p, p_max_size);
970 OBJ(i8k_left_fan_status) {
971 print_i8k_left_fan_status(obj, p, p_max_size);
973 OBJ(i8k_right_fan_status) {
974 print_i8k_right_fan_status(obj, p, p_max_size);
976 OBJ(i8k_left_fan_rpm) {
977 print_i8k_left_fan_rpm(obj, p, p_max_size);
979 OBJ(i8k_right_fan_rpm) {
980 print_i8k_right_fan_rpm(obj, p, p_max_size);
983 print_i8k_ac_status(obj, p, p_max_size);
985 OBJ(i8k_buttons_status) {
986 print_i8k_buttons_status(obj, p, p_max_size);
990 get_ibm_acpi_fan(obj, p, p_max_size);
993 print_ibm_temps(obj, p, p_max_size);
996 get_ibm_acpi_volume(obj, p, p_max_size);
998 OBJ(ibm_brightness) {
999 get_ibm_acpi_brightness(obj, p, p_max_size);
1002 /* information from sony_laptop kernel module
1003 * /sys/devices/platform/sony-laptop */
1004 OBJ(sony_fanspeed) {
1005 get_sony_fanspeed(p, p_max_size);
1008 if (!gateway_exists()) {
1013 print_gateway_iface(p, p_max_size);
1016 print_gateway_ip(p, p_max_size);
1019 snprintf(p, p_max_size, "%d", get_laptop_mode());
1022 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1024 #endif /* __linux__ */
1025 #if (defined(__FreeBSD__) || defined(__linux__))
1027 if (!interface_up(obj)) {
1033 OBJ(obsd_sensors_temp) {
1034 print_obsd_sensors_temp(obj, p, p_max_size);
1036 OBJ(obsd_sensors_fan) {
1037 print_obsd_sensors_fan(obj, p, p_max_size);
1039 OBJ(obsd_sensors_volt) {
1040 print_obsd_sensors_volt(obj, p, p_max_size);
1043 get_obsd_vendor(p, p_max_size);
1046 get_obsd_product(p, p_max_size);
1048 #endif /* __OpenBSD__ */
1051 new_font(p, obj->data.s);
1052 need_to_load_fonts = 1;
1056 print_diskio(obj, p, p_max_size);
1059 print_diskio_write(obj, p, p_max_size);
1062 print_diskio_read(obj, p, p_max_size);
1066 print_diskiograph(obj, p, p_max_size);
1068 OBJ(diskiograph_read) {
1069 print_diskiograph(obj, p, p_max_size);
1071 OBJ(diskiograph_write) {
1072 print_diskiograph(obj, p, p_max_size);
1076 print_downspeed(obj, p, p_max_size);
1079 print_downspeedf(obj, p, p_max_size);
1082 OBJ(downspeedgraph) {
1083 print_downspeedgraph(obj, p, p_max_size);
1087 /* Since we see you, you're if has not jumped.
1088 * Do Ninja jump here: without leaving traces.
1089 * This is to prevent us from stale jumped flags.
1091 obj = obj->ifblock_next;
1095 /* harmless object, just ignore */
1098 print_addr(obj, p, p_max_size);
1100 #if defined(__linux__)
1102 print_addrs(obj, p, p_max_size);
1104 #endif /* __linux__ */
1105 #if defined(IMLIB2) && defined(X11)
1107 /* doesn't actually draw anything, just queues it omp. the
1108 * image will get drawn after the X event loop */
1109 cimlib_add_image(obj->data.s);
1113 evaluate(obj->data.s, p, p_max_size);
1116 print_exec(obj, p, p_max_size);
1119 print_execp(obj, p, p_max_size);
1122 print_execgauge(obj, p, p_max_size);
1125 print_execbar(obj, p, p_max_size);
1129 print_execgraph(obj, p, p_max_size);
1133 print_execibar(obj, p, p_max_size);
1137 print_execigraph(obj, p, p_max_size);
1141 print_execigauge(obj, p, p_max_size);
1144 print_execi(obj, p, p_max_size);
1147 print_execpi(obj, p, p_max_size);
1150 print_texeci(obj, p, p_max_size);
1153 print_imap_unseen(obj, p, p_max_size);
1155 OBJ(imap_messages) {
1156 print_imap_messages(obj, p, p_max_size);
1159 print_pop3_unseen(obj, p, p_max_size);
1162 print_pop3_used(obj, p, p_max_size);
1165 print_fs_bar(obj, 0, p, p_max_size);
1168 print_fs_free(obj, p, p_max_size);
1171 print_fs_perc(obj, 1, p, p_max_size);
1174 print_fs_size(obj, p, p_max_size);
1177 print_fs_type(obj, p, p_max_size);
1180 print_fs_used(obj, p, p_max_size);
1183 print_fs_bar(obj, 1, p, p_max_size);
1186 print_fs_perc(obj, 0, p, p_max_size);
1189 print_loadavg(obj, p, p_max_size);
1192 new_goto(p, obj->data.i);
1199 new_hr(p, obj->data.i);
1203 print_nameserver(obj, p, p_max_size);
1207 print_eve(obj, p, p_max_size);
1212 curl_print(obj, p, p_max_size);
1217 rss_print_info(obj, p, p_max_size);
1222 print_weather(obj, p, p_max_size);
1226 OBJ(weather_forecast) {
1227 print_weather_forecast(obj, p, p_max_size);
1232 print_lua(obj, p, p_max_size);
1235 print_lua_parse(obj, p, p_max_size);
1238 print_lua_bar(obj, p, p_max_size);
1242 print_lua_graph(obj, p, p_max_size);
1246 print_lua_gauge(obj, p, p_max_size);
1248 #endif /* HAVE_LUA */
1254 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1255 snprintf(p, p_max_size, "N/A");
1257 temp_print(p, p_max_size, (double)val,
1258 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1263 new_offset(p, obj->data.i);
1266 new_voffset(p, obj->data.i);
1270 print_sysfs_sensor(obj, p, p_max_size);
1273 print_sysfs_sensor(obj, p, p_max_size);
1276 print_sysfs_sensor(obj, p, p_max_size);
1278 #endif /* __linux__ */
1280 new_alignr(p, obj->data.i);
1283 new_alignc(p, obj->data.i);
1286 char buf[max_user_text];
1287 struct information *tmp_info =
1288 malloc(sizeof(struct information));
1289 memcpy(tmp_info, cur, sizeof(struct information));
1290 generate_text_internal(buf, max_user_text,
1291 *obj->sub, tmp_info);
1293 if (strlen(buf) != 0) {
1299 char expression[max_user_text];
1301 struct information *tmp_info;
1303 tmp_info = malloc(sizeof(struct information));
1304 memcpy(tmp_info, cur, sizeof(struct information));
1305 generate_text_internal(expression, max_user_text,
1306 *obj->sub, tmp_info);
1307 DBGP("parsed arg into '%s'", expression);
1309 val = compare(expression);
1311 NORM_ERR("compare failed for expression '%s'",
1321 spc = strchr(obj->data.s, ' ');
1322 if (!spc && access(obj->data.s, F_OK)) {
1326 if (check_contains(obj->data.s, spc + 1))
1333 && (!check_mount(obj->data.s))) {
1339 if (!get_process_by_name(obj->data.s)) {
1341 if ((obj->data.s) && system(obj->data.s)) {
1346 #if defined(__linux__)
1348 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1352 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1355 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1360 human_readable(cur->mem * 1024, p, 255);
1363 human_readable(cur->memeasyfree * 1024, p, 255);
1366 human_readable(cur->memfree * 1024, p, 255);
1369 human_readable(cur->memmax * 1024, p, 255);
1373 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1376 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1379 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1383 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1388 print_mixer(obj, p, p_max_size);
1391 print_mixerl(obj, p, p_max_size);
1394 print_mixerr(obj, p, p_max_size);
1397 print_mixer_bar(obj, p, p_max_size);
1400 print_mixerl_bar(obj, p, p_max_size);
1403 print_mixerr_bar(obj, p, p_max_size);
1405 OBJ(if_mixer_mute) {
1406 if (!check_mixer_muted(obj)) {
1411 #define NOT_IN_X "Not running in X"
1413 if(x_initialised != YES) {
1414 strncpy(p, NOT_IN_X, p_max_size);
1416 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1419 OBJ(monitor_number) {
1420 if(x_initialised != YES) {
1421 strncpy(p, NOT_IN_X, p_max_size);
1423 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1427 if(x_initialised != YES) {
1428 strncpy(p, NOT_IN_X, p_max_size);
1430 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1433 OBJ(desktop_number) {
1434 if(x_initialised != YES) {
1435 strncpy(p, NOT_IN_X, p_max_size);
1437 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1441 if(x_initialised != YES) {
1442 strncpy(p, NOT_IN_X, p_max_size);
1443 }else if(cur->x11.desktop.name != NULL) {
1444 strncpy(p, cur->x11.desktop.name, p_max_size);
1450 char buf[max_user_text];
1452 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1454 print_format_time(obj, p, p_max_size);
1458 print_mails(obj, p, p_max_size);
1461 print_new_mails(obj, p, p_max_size);
1464 print_seen_mails(obj, p, p_max_size);
1467 print_unseen_mails(obj, p, p_max_size);
1469 OBJ(flagged_mails) {
1470 print_flagged_mails(obj, p, p_max_size);
1472 OBJ(unflagged_mails) {
1473 print_unflagged_mails(obj, p, p_max_size);
1475 OBJ(forwarded_mails) {
1476 print_forwarded_mails(obj, p, p_max_size);
1478 OBJ(unforwarded_mails) {
1479 print_unforwarded_mails(obj, p, p_max_size);
1481 OBJ(replied_mails) {
1482 print_replied_mails(obj, p, p_max_size);
1484 OBJ(unreplied_mails) {
1485 print_unreplied_mails(obj, p, p_max_size);
1488 print_draft_mails(obj, p, p_max_size);
1490 OBJ(trashed_mails) {
1491 print_trashed_mails(obj, p, p_max_size);
1494 print_mboxscan(obj, p, p_max_size);
1497 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1500 new_outline(p, obj->data.l);
1503 char buf[max_user_text];
1505 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1507 print_pid_chroot(obj, p, p_max_size);
1510 char buf[max_user_text];
1512 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1514 print_pid_cmdline(obj, p, p_max_size);
1517 char buf[max_user_text];
1519 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1521 print_pid_cwd(obj, p, p_max_size);
1524 char buf[max_user_text];
1526 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1528 print_pid_environ(obj, p, p_max_size);
1530 OBJ(pid_environ_list) {
1531 char buf[max_user_text];
1533 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1535 print_pid_environ_list(obj, p, p_max_size);
1538 char buf[max_user_text];
1540 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1542 print_pid_exe(obj, p, p_max_size);
1545 char buf[max_user_text];
1547 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1549 print_pid_nice(obj, p, p_max_size);
1551 OBJ(pid_openfiles) {
1552 char buf[max_user_text];
1554 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1556 print_pid_openfiles(obj, p, p_max_size);
1559 char buf[max_user_text];
1561 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1563 print_pid_parent(obj, p, p_max_size);
1566 char buf[max_user_text];
1568 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1570 print_pid_priority(obj, p, p_max_size);
1573 char buf[max_user_text];
1575 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1577 print_pid_state(obj, p, p_max_size);
1579 OBJ(pid_state_short) {
1580 char buf[max_user_text];
1582 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1584 print_pid_state_short(obj, p, p_max_size);
1587 char buf[max_user_text];
1589 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1591 print_pid_stderr(obj, p, p_max_size);
1594 char buf[max_user_text];
1596 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1598 print_pid_stdin(obj, p, p_max_size);
1601 char buf[max_user_text];
1603 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1605 print_pid_stdout(obj, p, p_max_size);
1608 char buf[max_user_text];
1610 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1612 print_pid_threads(obj, p, p_max_size);
1614 OBJ(pid_thread_list) {
1615 char buf[max_user_text];
1617 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1619 print_pid_thread_list(obj, p, p_max_size);
1621 OBJ(pid_time_kernelmode) {
1622 char buf[max_user_text];
1624 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1626 print_pid_time_kernelmode(obj, p, p_max_size);
1628 OBJ(pid_time_usermode) {
1629 char buf[max_user_text];
1631 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1633 print_pid_time_usermode(obj, p, p_max_size);
1636 char buf[max_user_text];
1638 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1640 print_pid_time(obj, p, p_max_size);
1643 char buf[max_user_text];
1645 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1647 print_pid_uid(obj, p, p_max_size);
1650 char buf[max_user_text];
1652 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1654 print_pid_euid(obj, p, p_max_size);
1657 char buf[max_user_text];
1659 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1661 print_pid_suid(obj, p, p_max_size);
1664 char buf[max_user_text];
1666 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1668 print_pid_fsuid(obj, p, p_max_size);
1671 char buf[max_user_text];
1673 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1675 print_pid_gid(obj, p, p_max_size);
1678 char buf[max_user_text];
1680 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1682 print_pid_egid(obj, p, p_max_size);
1685 char buf[max_user_text];
1687 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1689 print_pid_sgid(obj, p, p_max_size);
1692 char buf[max_user_text];
1694 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1696 print_pid_fsgid(obj, p, p_max_size);
1699 char buf[max_user_text];
1701 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1703 print_pid_read(obj, p, p_max_size);
1706 char buf[max_user_text];
1708 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1710 print_pid_vmpeak(obj, p, p_max_size);
1713 char buf[max_user_text];
1715 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1717 print_pid_vmsize(obj, p, p_max_size);
1720 char buf[max_user_text];
1722 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1724 print_pid_vmlck(obj, p, p_max_size);
1727 char buf[max_user_text];
1729 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1731 print_pid_vmhwm(obj, p, p_max_size);
1734 char buf[max_user_text];
1736 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1738 print_pid_vmrss(obj, p, p_max_size);
1741 char buf[max_user_text];
1743 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1745 print_pid_vmdata(obj, p, p_max_size);
1748 char buf[max_user_text];
1750 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1752 print_pid_vmstk(obj, p, p_max_size);
1755 char buf[max_user_text];
1757 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1759 print_pid_vmexe(obj, p, p_max_size);
1762 char buf[max_user_text];
1764 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1766 print_pid_vmlib(obj, p, p_max_size);
1769 char buf[max_user_text];
1771 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1773 print_pid_vmpte(obj, p, p_max_size);
1776 char buf[max_user_text];
1778 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1780 print_pid_write(obj, p, p_max_size);
1783 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1785 OBJ(running_processes) {
1786 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1788 OBJ(running_threads) {
1789 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1792 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1795 snprintf(p, p_max_size, "%s", obj->data.s);
1799 new_bg(p, obj->data.l);
1802 new_stippled_hr(obj, p);
1806 human_readable(cur->swap * 1024, p, 255);
1809 human_readable(cur->swapfree * 1024, p, 255);
1812 human_readable(cur->swapmax * 1024, p, 255);
1815 if (cur->swapmax == 0) {
1816 strncpy(p, "No swap", p_max_size);
1818 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1822 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1825 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1828 print_time(obj, p, p_max_size);
1831 print_utime(obj, p, p_max_size);
1834 print_tztime(obj, p, p_max_size);
1837 print_totaldown(obj, p, p_max_size);
1840 print_totalup(obj, p, p_max_size);
1843 char buf[max_user_text];
1845 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1847 print_gid_name(obj, p, p_max_size);
1850 char buf[max_user_text];
1852 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1854 print_uid_name(obj, p, p_max_size);
1857 snprintf(p, p_max_size, "%d", total_updates);
1860 if(total_updates % updatereset != obj->data.i - 1) {
1865 print_upspeed(obj, p, p_max_size);
1868 print_upspeedf(obj, p, p_max_size);
1872 print_upspeedgraph(obj, p, p_max_size);
1876 format_seconds_short(p, p_max_size, (int) cur->uptime);
1879 format_seconds(p, p_max_size, (int) cur->uptime);
1883 snprintf(p, p_max_size, "%s", cur->users.names);
1886 snprintf(p, p_max_size, "%s", cur->users.terms);
1889 snprintf(p, p_max_size, "%s", cur->users.times);
1892 update_user_time(obj->data.s);
1893 snprintf(p, p_max_size, "%s", cur->users.ctime);
1896 snprintf(p, p_max_size, "%d", cur->users.number);
1898 #endif /* __linux__ */
1899 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1900 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1904 msg = get_apm_adapter();
1905 snprintf(p, p_max_size, "%s", msg);
1908 OBJ(apm_battery_life) {
1911 msg = get_apm_battery_life();
1912 snprintf(p, p_max_size, "%s", msg);
1915 OBJ(apm_battery_time) {
1918 msg = get_apm_battery_time();
1919 snprintf(p, p_max_size, "%s", msg);
1922 #endif /* __FreeBSD__ __OpenBSD__ */
1926 print_mpd_title(obj, p, p_max_size);
1928 print_mpd_artist(obj, p, p_max_size);
1930 print_mpd_album(obj, p, p_max_size);
1932 print_mpd_random(obj, p, p_max_size);
1934 print_mpd_repeat(obj, p, p_max_size);
1936 print_mpd_track(obj, p, p_max_size);
1938 print_mpd_name(obj, p, p_max_size);
1940 print_mpd_file(obj, p, p_max_size);
1942 print_mpd_vol(obj, p, p_max_size);
1944 print_mpd_bitrate(obj, p, p_max_size);
1946 print_mpd_status(obj, p, p_max_size);
1948 print_mpd_elapsed(obj, p, p_max_size);
1951 print_mpd_length(obj, p, p_max_size);
1954 print_mpd_percent(obj, p, p_max_size);
1957 print_mpd_bar(obj, p, p_max_size);
1960 print_mpd_smart(obj, p, p_max_size);
1962 OBJ(if_mpd_playing) {
1963 if (!mpd_get_info()->is_playing) {
1970 #define MOC_PRINT(t, a) \
1971 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1973 MOC_PRINT(state, "??");
1976 MOC_PRINT(file, "no file");
1979 MOC_PRINT(title, "no title");
1982 MOC_PRINT(artist, "no artist");
1985 MOC_PRINT(song, "no song");
1988 MOC_PRINT(album, "no album");
1990 OBJ(moc_totaltime) {
1991 MOC_PRINT(totaltime, "0:00");
1994 MOC_PRINT(timeleft, "0:00");
1997 MOC_PRINT(curtime, "0:00");
2000 MOC_PRINT(bitrate, "0Kbps");
2003 MOC_PRINT(rate, "0KHz");
2009 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2012 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2015 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2018 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2020 OBJ(xmms2_comment) {
2021 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2024 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2027 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2030 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2032 OBJ(xmms2_tracknr) {
2033 if (cur->xmms2.tracknr != -1) {
2034 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2037 OBJ(xmms2_bitrate) {
2038 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2041 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2044 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2046 OBJ(xmms2_elapsed) {
2047 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2048 (cur->xmms2.elapsed / 1000) % 60);
2050 OBJ(xmms2_duration) {
2051 snprintf(p, p_max_size, "%02d:%02d",
2052 cur->xmms2.duration / 60000,
2053 (cur->xmms2.duration / 1000) % 60);
2055 OBJ(xmms2_percent) {
2056 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2059 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2061 OBJ(xmms2_playlist) {
2062 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2064 OBJ(xmms2_timesplayed) {
2065 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2068 if (strlen(cur->xmms2.title) < 2
2069 && strlen(cur->xmms2.title) < 2) {
2070 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2072 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2076 OBJ(if_xmms2_connected) {
2077 if (cur->xmms2.conn_state != 1) {
2083 OBJ(audacious_status) {
2084 snprintf(p, p_max_size, "%s",
2085 cur->audacious.items[AUDACIOUS_STATUS]);
2087 OBJ(audacious_title) {
2088 snprintf(p, cur->audacious.max_title_len > 0
2089 ? cur->audacious.max_title_len : p_max_size, "%s",
2090 cur->audacious.items[AUDACIOUS_TITLE]);
2092 OBJ(audacious_length) {
2093 snprintf(p, p_max_size, "%s",
2094 cur->audacious.items[AUDACIOUS_LENGTH]);
2096 OBJ(audacious_length_seconds) {
2097 snprintf(p, p_max_size, "%s",
2098 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2100 OBJ(audacious_position) {
2101 snprintf(p, p_max_size, "%s",
2102 cur->audacious.items[AUDACIOUS_POSITION]);
2104 OBJ(audacious_position_seconds) {
2105 snprintf(p, p_max_size, "%s",
2106 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2108 OBJ(audacious_bitrate) {
2109 snprintf(p, p_max_size, "%s",
2110 cur->audacious.items[AUDACIOUS_BITRATE]);
2112 OBJ(audacious_frequency) {
2113 snprintf(p, p_max_size, "%s",
2114 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2116 OBJ(audacious_channels) {
2117 snprintf(p, p_max_size, "%s",
2118 cur->audacious.items[AUDACIOUS_CHANNELS]);
2120 OBJ(audacious_filename) {
2121 snprintf(p, p_max_size, "%s",
2122 cur->audacious.items[AUDACIOUS_FILENAME]);
2124 OBJ(audacious_playlist_length) {
2125 snprintf(p, p_max_size, "%s",
2126 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2128 OBJ(audacious_playlist_position) {
2129 snprintf(p, p_max_size, "%s",
2130 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2132 OBJ(audacious_main_volume) {
2133 snprintf(p, p_max_size, "%s",
2134 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2136 OBJ(audacious_bar) {
2140 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2141 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2142 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2144 #endif /* AUDACIOUS */
2148 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2151 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2154 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2157 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2160 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2163 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2166 /* we have four different types of top (top, top_mem,
2167 * top_time and top_io). To avoid having almost-same code four
2168 * times, we have this special handler. */
2177 print_top(obj, p, p_max_size);
2178 #endif /* __linux__ */
2180 print_tailhead("tail", obj, p, p_max_size);
2183 print_tailhead("head", obj, p, p_max_size);
2186 print_lines(obj, p, p_max_size);
2190 print_words(obj, p, p_max_size);
2192 #ifdef TCP_PORT_MONITOR
2194 tcp_portmon_action(obj, p, p_max_size);
2196 #endif /* TCP_PORT_MONITOR */
2200 do_iconv_start(obj);
2205 #endif /* HAVE_ICONV */
2207 OBJ(entropy_avail) {
2208 print_entropy_avail(obj, p, p_max_size);
2211 print_entropy_perc(obj, p, p_max_size);
2213 OBJ(entropy_poolsize) {
2214 print_entropy_poolsize(obj, p, p_max_size);
2217 print_entropy_bar(obj, p, p_max_size);
2221 print_smapi(obj, p, p_max_size);
2223 OBJ(if_smapi_bat_installed) {
2225 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2226 if(!smapi_bat_installed(idx)) {
2230 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2232 OBJ(smapi_bat_perc) {
2233 print_smapi_bat_perc(obj, p, p_max_size);
2235 OBJ(smapi_bat_temp) {
2236 print_smapi_bat_temp(obj, p, p_max_size);
2238 OBJ(smapi_bat_power) {
2239 print_smapi_bat_power(obj, p, p_max_size);
2241 OBJ(smapi_bat_bar) {
2242 print_smapi_bat_bar(obj, p, p_max_size);
2247 char buf[max_user_text];
2249 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2250 snprintf(p, p_max_size, "%s", buf);
2256 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2257 char buf[max_user_text];
2260 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2261 snprintf(p, p_max_size, "%s", buf);
2262 if(total_updates % 2) {
2263 for(j=0; p[j] != 0; j++) {
2269 char buf[max_user_text];
2271 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2273 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2274 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2275 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2276 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2277 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2278 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2279 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2281 snprintf(p, p_max_size, "%s", buf);
2284 print_scroll(obj, p, p_max_size, cur);
2287 print_combine(obj, p, cur);
2291 print_nvidia_value(obj, display, p, p_max_size);
2296 /* This is just a meta-object to set host:port */
2299 snprintf(p, p_max_size, "%s",
2300 cur->apcupsd.items[APCUPSD_NAME]);
2302 OBJ(apcupsd_model) {
2303 snprintf(p, p_max_size, "%s",
2304 cur->apcupsd.items[APCUPSD_MODEL]);
2306 OBJ(apcupsd_upsmode) {
2307 snprintf(p, p_max_size, "%s",
2308 cur->apcupsd.items[APCUPSD_UPSMODE]);
2310 OBJ(apcupsd_cable) {
2311 snprintf(p, p_max_size, "%s",
2312 cur->apcupsd.items[APCUPSD_CABLE]);
2314 OBJ(apcupsd_status) {
2315 snprintf(p, p_max_size, "%s",
2316 cur->apcupsd.items[APCUPSD_STATUS]);
2318 OBJ(apcupsd_linev) {
2319 snprintf(p, p_max_size, "%s",
2320 cur->apcupsd.items[APCUPSD_LINEV]);
2323 snprintf(p, p_max_size, "%s",
2324 cur->apcupsd.items[APCUPSD_LOAD]);
2326 OBJ(apcupsd_loadbar) {
2328 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2329 new_bar(obj, p, p_max_size, (int) progress);
2332 OBJ(apcupsd_loadgraph) {
2334 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2335 new_graph(obj, p, p_max_size, (int)progress);
2338 OBJ(apcupsd_loadgauge) {
2340 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2341 new_gauge(obj, p, p_max_size, (int)progress);
2343 OBJ(apcupsd_charge) {
2344 snprintf(p, p_max_size, "%s",
2345 cur->apcupsd.items[APCUPSD_CHARGE]);
2347 OBJ(apcupsd_timeleft) {
2348 snprintf(p, p_max_size, "%s",
2349 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2352 snprintf(p, p_max_size, "%s",
2353 cur->apcupsd.items[APCUPSD_TEMP]);
2355 OBJ(apcupsd_lastxfer) {
2356 snprintf(p, p_max_size, "%s",
2357 cur->apcupsd.items[APCUPSD_LASTXFER]);
2359 #endif /* APCUPSD */
2366 size_t a = strlen(p);
2369 iconv_convert(&a, buff_in, p, p_max_size);
2370 #endif /* HAVE_ICONV */
2371 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2373 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2374 #endif /* HAVE_LUA */
2376 substitute_newlines(p, a - 2);
2385 /* load any new fonts we may have had */
2386 if (need_to_load_fonts) {
2392 void evaluate(const char *text, char *p, int p_max_size)
2394 struct information *tmp_info;
2395 struct text_object subroot;
2397 tmp_info = malloc(sizeof(struct information));
2398 memcpy(tmp_info, &info, sizeof(struct information));
2399 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2400 DBGP2("evaluated '%s' to '%s'", text, p);
2402 free_text_objects(&subroot, 1);
2406 double current_update_time, next_update_time, last_update_time;
2408 static void generate_text(void)
2410 struct information *cur = &info;
2412 unsigned int i, j, k;
2418 current_update_time = get_time();
2422 /* add things to the buffer */
2428 generate_text_internal(p, max_user_text, global_root_object, cur);
2429 if(max_text_width > 0) {
2430 for(i = 0, j = 0; p[i] != 0; i++) {
2431 if(p[i] == '\n') j = 0;
2432 else if(j == max_text_width) {
2433 k = i + strlen(p + i) + 1;
2434 if(k < text_buffer_size) {
2441 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2446 if (stuff_in_uppercase) {
2449 tmp_p = text_buffer;
2451 *tmp_p = toupper(*tmp_p);
2456 next_update_time += update_interval;
2457 if (next_update_time < get_time()) {
2458 next_update_time = get_time() + update_interval;
2459 } else if (next_update_time > get_time() + update_interval) {
2460 next_update_time = get_time() + update_interval;
2462 last_update_time = current_update_time;
2466 void set_update_interval(double interval)
2468 update_interval = interval;
2469 update_interval_old = interval;
2472 int get_string_width(const char *s)
2474 return *s ? calc_text_width(s) : 0;
2478 static int get_string_width_special(char *s, int special_index)
2488 if ((output_methods & TO_X) == 0)
2491 p = strndup(s, text_buffer_size);
2495 if (*p == SPECIAL_CHAR) {
2496 /* shift everything over by 1 so that the special char
2497 * doesn't mess up the size calculation */
2498 for (i = 0; i < (long)strlen(p); i++) {
2499 *(p + i) = *(p + i + 1);
2501 if (specials[special_index + idx].type == GRAPH
2502 || specials[special_index + idx].type == GAUGE
2503 || specials[special_index + idx].type == BAR) {
2504 width += specials[special_index + idx].width;
2507 } else if (*p == SECRIT_MULTILINE_CHAR) {
2514 if (strlen(final) > 1) {
2515 width += calc_text_width(final);
2521 static int text_size_updater(char *s, int special_index);
2523 int last_font_height;
2524 static void update_text_area(void)
2528 if ((output_methods & TO_X) == 0)
2530 /* update text size if it isn't fixed */
2535 text_width = minimum_width;
2537 last_font_height = font_height();
2538 for_each_line(text_buffer, text_size_updater);
2540 if (text_height < minimum_height) {
2541 text_height = minimum_height;
2543 if (text_width > maximum_width && maximum_width > 0) {
2544 text_width = maximum_width;
2548 /* get text position on workarea */
2549 switch (text_alignment) {
2550 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2554 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2555 y = workarea[3] - text_height - gap_y;
2558 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2559 y = workarea[3] / 2 - text_height / 2 - gap_y;
2562 switch (text_alignment) {
2563 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2567 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2568 x = workarea[2] - text_width - gap_x;
2571 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2572 x = workarea[2] / 2 - text_width / 2 - gap_x;
2576 if (text_alignment == NONE) { // Let the WM manage the window
2583 #endif /* OWN_WINDOW */
2586 if (own_window && !fixed_pos) {
2589 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2590 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2591 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2592 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2596 /* If window size doesn't match to workarea's size,
2597 * then window probably includes panels (gnome).
2598 * Blah, doesn't work on KDE. */
2599 if (workarea[2] != window.width || workarea[3] != window.height) {
2608 /* update lua window globals */
2609 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2610 #endif /* HAVE_LUA */
2615 static int cur_x, cur_y; /* current x and y for drawing */
2617 //draw_mode also without X11 because we only need to print to stdout with FG
2618 static int draw_mode; /* FG, BG or OUTLINE */
2620 static long current_color;
2622 static int text_size_updater(char *s, int special_index)
2626 int contain_SECRIT_MULTILINE_CHAR = 0;
2629 if ((output_methods & TO_X) == 0)
2631 /* get string widths and skip specials */
2634 if (*p == SPECIAL_CHAR) {
2636 w += get_string_width(s);
2639 if (specials[special_index].type == BAR
2640 || specials[special_index].type == GAUGE
2641 || specials[special_index].type == GRAPH) {
2642 w += specials[special_index].width;
2643 if (specials[special_index].height > last_font_height) {
2644 last_font_height = specials[special_index].height;
2645 last_font_height += font_height();
2647 } else if (specials[special_index].type == OFFSET) {
2648 if (specials[special_index].arg > 0) {
2649 w += specials[special_index].arg;
2651 } else if (specials[special_index].type == VOFFSET) {
2652 last_font_height += specials[special_index].arg;
2653 } else if (specials[special_index].type == GOTO) {
2654 if (specials[special_index].arg > cur_x) {
2655 w = (int) specials[special_index].arg;
2657 } else if (specials[special_index].type == TAB) {
2658 int start = specials[special_index].arg;
2659 int step = specials[special_index].width;
2661 if (!step || step < 0) {
2664 w += step - (cur_x - text_start_x - start) % step;
2665 } else if (specials[special_index].type == FONT) {
2666 selected_font = specials[special_index].font_added;
2667 if (font_height() > last_font_height) {
2668 last_font_height = font_height();
2674 } else if (*p == SECRIT_MULTILINE_CHAR) {
2675 contain_SECRIT_MULTILINE_CHAR = 1;
2677 lw = get_string_width(s);
2678 *p = SECRIT_MULTILINE_CHAR;
2680 w = lw > w ? lw : w;
2681 text_height += last_font_height;
2685 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2686 if (contain_SECRIT_MULTILINE_CHAR) {
2687 lw = get_string_width(s);
2688 w = lw > w ? lw : w;
2690 w += get_string_width(s);
2692 if (w > text_width) {
2695 if (text_width > maximum_width && maximum_width) {
2696 text_width = maximum_width;
2699 text_height += last_font_height;
2700 last_font_height = font_height();
2701 return special_index;
2705 static inline void set_foreground_color(long c)
2708 if (output_methods & TO_X) {
2710 if (have_argb_visual) {
2711 current_color = c | (own_window_argb_value << 24);
2713 #endif /* USE_ARGB */
2717 #endif /* USE_ARGB */
2718 XSetForeground(display, window.gc, current_color);
2722 if (output_methods & TO_NCURSES) {
2723 attron(COLOR_PAIR(c));
2725 #endif /* NCURSES */
2730 static void draw_string(const char *s)
2732 int i, i2, pos, width_of_s;
2735 char *s_with_newlines;
2741 width_of_s = get_string_width(s);
2742 s_with_newlines = strdup(s);
2743 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2744 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2745 s_with_newlines[i] = '\n';
2748 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2749 printf("%s\n", s_with_newlines);
2750 if (extra_newline) fputc('\n', stdout);
2751 fflush(stdout); /* output immediately, don't buffer */
2753 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2754 fprintf(stderr, "%s\n", s_with_newlines);
2755 fflush(stderr); /* output immediately, don't buffer */
2757 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2758 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2760 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2761 fprintf(append_fpointer, "%s\n", s_with_newlines);
2764 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2765 printw("%s", s_with_newlines);
2768 free(s_with_newlines);
2769 memset(tmpstring1, 0, text_buffer_size);
2770 memset(tmpstring2, 0, text_buffer_size);
2771 strncpy(tmpstring1, s, text_buffer_size - 1);
2776 if (output_methods & TO_X) {
2777 max = ((text_width - width_of_s) / get_string_width(" "));
2780 /* This code looks for tabs in the text and coverts them to spaces.
2781 * The trick is getting the correct number of spaces, and not going
2782 * over the window's size without forcing the window larger. */
2783 for (i = 0; i < (int) text_buffer_size; i++) {
2784 if (tmpstring1[i] == '\t') {
2786 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2787 /* guard against overrun */
2788 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2793 /* guard against overrun */
2794 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2799 if (output_methods & TO_X) {
2800 if (text_width == maximum_width) {
2801 /* this means the text is probably pushing the limit,
2802 * so we'll chop it */
2803 while (cur_x + get_string_width(tmpstring2) - text_start_x
2804 > maximum_width && strlen(tmpstring2) > 0) {
2805 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2812 if (output_methods & TO_X) {
2818 c.pixel = current_color;
2819 // query color on custom colormap
2820 XQueryColor(display, window.colourmap, &c);
2823 c2.color.red = c.red;
2824 c2.color.green = c.green;
2825 c2.color.blue = c.blue;
2826 c2.color.alpha = fonts[selected_font].font_alpha;
2828 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2829 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2831 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2832 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2837 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2840 cur_x += width_of_s;
2843 memcpy(tmpstring1, s, text_buffer_size);
2846 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2854 int last_special_needed = -1;
2855 int orig_special_index = special_index;
2858 if (output_methods & TO_X) {
2859 font_h = font_height();
2860 cur_y += font_ascent();
2862 cur_x = text_start_x;
2866 if (*p == SECRIT_MULTILINE_CHAR) {
2867 /* special newline marker for multiline objects */
2872 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2877 /* draw string before special, unless we're dealing multiline
2879 if (last_special_applied > -1) {
2880 special_index = last_special_applied;
2888 switch (specials[special_index].type) {
2890 case HORIZONTAL_LINE:
2892 int h = specials[special_index].height;
2893 int mid = font_ascent() / 2;
2895 w = text_start_x + text_width - cur_x;
2897 XSetLineAttributes(display, window.gc, h, LineSolid,
2898 CapButt, JoinMiter);
2899 XDrawLine(display, window.drawable, window.gc, cur_x,
2900 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2906 int h = specials[special_index].height;
2907 int tmp_s = specials[special_index].arg;
2908 int mid = font_ascent() / 2;
2909 char ss[2] = { tmp_s, tmp_s };
2911 w = text_start_x + text_width - cur_x - 1;
2912 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2913 CapButt, JoinMiter);
2914 XSetDashes(display, window.gc, 0, ss, 2);
2915 XDrawLine(display, window.drawable, window.gc, cur_x,
2916 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2922 int h, bar_usage, by;
2923 if (cur_x - text_start_x > maximum_width
2924 && maximum_width > 0) {
2927 h = specials[special_index].height;
2928 bar_usage = specials[special_index].arg;
2929 by = cur_y - (font_ascent() / 2) - 1;
2934 w = specials[special_index].width;
2936 w = text_start_x + text_width - cur_x - 1;
2942 XSetLineAttributes(display, window.gc, 1, LineSolid,
2943 CapButt, JoinMiter);
2945 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2947 XFillRectangle(display, window.drawable, window.gc, cur_x,
2948 by, w * bar_usage / 255, h);
2956 case GAUGE: /* new GAUGE */
2959 unsigned long last_colour = current_color;
2961 float angle, px, py;
2965 if (cur_x - text_start_x > maximum_width
2966 && maximum_width > 0) {
2970 h = specials[special_index].height;
2971 by = cur_y - (font_ascent() / 2) - 1;
2976 w = specials[special_index].width;
2978 w = text_start_x + text_width - cur_x - 1;
2984 XSetLineAttributes(display, window.gc, 1, LineSolid,
2985 CapButt, JoinMiter);
2987 XDrawArc(display, window.drawable, window.gc,
2988 cur_x, by, w, h * 2, 0, 180*64);
2991 usage = specials[special_index].arg;
2992 angle = (M_PI)*(float)(usage)/255.;
2993 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2994 py = (float)(by+(h))-(float)(h)*sin(angle);
2996 XDrawLine(display, window.drawable, window.gc,
2997 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3005 set_foreground_color(last_colour);
3013 int h, by, i = 0, j = 0;
3015 unsigned long last_colour = current_color;
3016 unsigned long *tmpcolour = 0;
3017 if (cur_x - text_start_x > maximum_width
3018 && maximum_width > 0) {
3021 h = specials[special_index].height;
3022 by = cur_y - (font_ascent() / 2) - 1;
3027 w = specials[special_index].width;
3029 w = text_start_x + text_width - cur_x - 1;
3034 if (draw_graph_borders) {
3035 XSetLineAttributes(display, window.gc, 1, LineSolid,
3036 CapButt, JoinMiter);
3037 XDrawRectangle(display, window.drawable, window.gc,
3040 XSetLineAttributes(display, window.gc, 1, LineSolid,
3041 CapButt, JoinMiter);
3043 if (specials[special_index].last_colour != 0
3044 || specials[special_index].first_colour != 0) {
3045 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3048 for (i = w - 2; i > -1; i--) {
3049 if (specials[special_index].last_colour != 0
3050 || specials[special_index].first_colour != 0) {
3051 if (specials[special_index].tempgrad) {
3054 (int)((float)(w - 2) - specials[special_index].graph[j] *
3055 (w - 2) / (float)specials[special_index].graph_scale)
3059 (int)((float)(w - 2) - specials[special_index].graph[j] *
3060 (w - 2) / (float)specials[special_index].graph_scale)
3063 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3065 (int)((float)(w - 2) - specials[special_index].graph[j] *
3066 (w - 2) / (float)specials[special_index].graph_scale)
3070 #endif /* DEBUG_lol */
3071 XSetForeground(display, window.gc, tmpcolour[
3072 (int)((float)(w - 2) - specials[special_index].graph[j] *
3073 (w - 2) / (float)specials[special_index].graph_scale)
3076 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3079 /* this is mugfugly, but it works */
3080 XDrawLine(display, window.drawable, window.gc,
3081 cur_x + i + 1, by + h, cur_x + i + 1,
3082 round_to_int((double)by + h - specials[special_index].graph[j] *
3083 (h - 1) / specials[special_index].graph_scale));
3084 if ((w - i) / ((float) (w - 2) /
3085 (specials[special_index].graph_width)) > j
3086 && j < MAX_GRAPH_DEPTH - 3) {
3090 if (tmpcolour) free(tmpcolour);
3095 /* if (draw_mode == BG) {
3096 set_foreground_color(default_bg_color);
3097 } else if (draw_mode == OUTLINE) {
3098 set_foreground_color(default_out_color);
3100 set_foreground_color(default_fg_color);
3102 if (show_graph_range) {
3105 unsigned short int seconds = update_interval * w;
3111 unsigned short int timeunits;
3113 timeunits = seconds / 86400; seconds %= 86400;
3114 if (timeunits > 0) {
3115 asprintf(&tmp_day_str, "%dd", timeunits);
3117 tmp_day_str = strdup("");
3119 timeunits = seconds / 3600; seconds %= 3600;
3120 if (timeunits > 0) {
3121 asprintf(&tmp_hour_str, "%dh", timeunits);
3123 tmp_hour_str = strdup("");
3125 timeunits = seconds / 60; seconds %= 60;
3126 if (timeunits > 0) {
3127 asprintf(&tmp_min_str, "%dm", timeunits);
3129 tmp_min_str = strdup("");
3132 asprintf(&tmp_sec_str, "%ds", seconds);
3134 tmp_sec_str = strdup("");
3136 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3137 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3139 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3141 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3142 cur_y += font_h / 2;
3143 draw_string(tmp_str);
3149 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3153 cur_x += font_ascent() / 2;
3154 cur_y += font_h / 2;
3156 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3158 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3159 draw_string(tmp_str);
3165 set_foreground_color(last_colour);
3171 int old = font_ascent();
3173 cur_y -= font_ascent();
3174 selected_font = specials[special_index].font_added;
3176 if (cur_y + font_ascent() < cur_y + old) {
3179 cur_y += font_ascent();
3181 font_h = font_height();
3186 if (draw_mode == FG) {
3187 set_foreground_color(specials[special_index].arg);
3193 if (draw_mode == BG) {
3194 set_foreground_color(specials[special_index].arg);
3199 if (draw_mode == OUTLINE) {
3200 set_foreground_color(specials[special_index].arg);
3205 w += specials[special_index].arg;
3206 last_special_needed = special_index;
3210 cur_y += specials[special_index].arg;
3214 if (specials[special_index].arg >= 0) {
3215 cur_x = (int) specials[special_index].arg;
3217 last_special_needed = special_index;
3222 int start = specials[special_index].arg;
3223 int step = specials[special_index].width;
3225 if (!step || step < 0) {
3228 w = step - (cur_x - text_start_x - start) % step;
3229 last_special_needed = special_index;
3235 /* TODO: add back in "+ window.border_inner_margin" to the end of
3237 int pos_x = text_start_x + text_width -
3238 get_string_width_special(s, special_index);
3240 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3241 "get_string_width(p) %i gap_x %i "
3242 "specials[special_index].arg %i window.border_inner_margin %i "
3243 "window.border_width %i\n", pos_x, text_start_x, text_width,
3244 cur_x, get_string_width_special(s), gap_x,
3245 specials[special_index].arg, window.border_inner_margin,
3246 window.border_width); */
3247 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3248 cur_x = pos_x - specials[special_index].arg;
3250 last_special_needed = special_index;
3256 int pos_x = (text_width) / 2 - get_string_width_special(s,
3257 special_index) / 2 - (cur_x -
3259 /* int pos_x = text_start_x + text_width / 2 -
3260 get_string_width_special(s) / 2; */
3262 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3263 "get_string_width(p) %i gap_x %i "
3264 "specials[special_index].arg %i\n", pos_x, text_start_x,
3265 text_width, cur_x, get_string_width(s), gap_x,
3266 specials[special_index].arg); */
3267 if (pos_x > specials[special_index].arg) {
3268 w = pos_x - specials[special_index].arg;
3270 last_special_needed = special_index;
3280 if (special_index != last_special_applied) {
3283 special_index = orig_special_index;
3284 last_special_applied = -1;
3295 if (output_methods & TO_NCURSES) {
3298 #endif /* NCURSES */
3300 if (output_methods & TO_X)
3301 cur_y += font_descent();
3303 if (recurse && *recurse) {
3304 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3305 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3307 return special_index;
3310 static int draw_line(char *s, int special_index)
3313 if (output_methods & TO_X) {
3314 return draw_each_line_inner(s, special_index, -1);
3318 if (output_methods & TO_NCURSES) {
3319 return draw_each_line_inner(s, special_index, -1);
3321 #endif /* NCURSES */
3323 UNUSED(special_index);
3327 static void draw_text(void)
3331 llua_draw_pre_hook();
3332 #endif /* HAVE_LUA */
3333 if (output_methods & TO_X) {
3334 cur_y = text_start_y;
3337 if (draw_borders && window.border_width > 0) {
3338 if (stippled_borders) {
3339 char ss[2] = { stippled_borders, stippled_borders };
3340 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3341 CapButt, JoinMiter);
3342 XSetDashes(display, window.gc, 0, ss, 2);
3344 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3345 CapButt, JoinMiter);
3348 XDrawRectangle(display, window.drawable, window.gc,
3349 text_start_x - window.border_inner_margin - window.border_width,
3350 text_start_y - window.border_inner_margin - window.border_width,
3351 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3352 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3360 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3361 attron(COLOR_PAIR(COLOR_WHITE));
3362 #endif /* NCURSES */
3363 for_each_line(text_buffer, draw_line);
3364 #if defined(HAVE_LUA) && defined(X11)
3365 llua_draw_post_hook();
3366 #endif /* HAVE_LUA */
3369 static void draw_stuff(void)
3372 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3374 if (overwrite_file) {
3375 overwrite_fpointer = fopen(overwrite_file, "w");
3376 if(!overwrite_fpointer)
3377 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3380 append_fpointer = fopen(append_file, "a");
3381 if(!append_fpointer)
3382 NORM_ERR("Can't append '%s' anymore", append_file);
3385 if (output_methods & TO_X) {
3387 if (draw_shades && !draw_outline) {
3390 set_foreground_color(default_bg_color);
3401 for (i = -1; i < 2; i++) {
3402 for (j = -1; j < 2; j++) {
3403 if (i == 0 && j == 0) {
3408 set_foreground_color(default_out_color);
3409 draw_mode = OUTLINE;
3417 set_foreground_color(default_fg_color);
3422 #if defined(X11) && defined(HAVE_XDBE)
3423 if (output_methods & TO_X) {
3424 xdbe_swap_buffers();
3426 #endif /* X11 && HAVE_XDBE */
3427 if(overwrite_fpointer) {
3428 fclose(overwrite_fpointer);
3429 overwrite_fpointer = 0;
3431 if(append_fpointer) {
3432 fclose(append_fpointer);
3433 append_fpointer = 0;
3438 static void clear_text(int exposures)
3442 /* The swap action is XdbeBackground, which clears */
3446 if (display && window.window) { // make sure these are !null
3447 /* there is some extra space for borders and outlines */
3448 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3449 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3450 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3451 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3456 static int need_to_update;
3458 /* update_text() generates new text and clears old text area */
3459 static void update_text(void)
3466 if (output_methods & TO_X)
3471 llua_update_info(&info, update_interval);
3472 #endif /* HAVE_LUA */
3475 #ifdef HAVE_SYS_INOTIFY_H
3479 static void main_loop(void)
3482 #ifdef SIGNAL_BLOCKING
3483 sigset_t newmask, oldmask;
3486 #ifdef HAVE_SYS_INOTIFY_H
3487 int inotify_config_wd = -1;
3488 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3489 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3490 char inotify_buff[INOTIFY_BUF_LEN];
3491 #endif /* HAVE_SYS_INOTIFY_H */
3494 #ifdef SIGNAL_BLOCKING
3495 sigemptyset(&newmask);
3496 sigaddset(&newmask, SIGINT);
3497 sigaddset(&newmask, SIGTERM);
3498 sigaddset(&newmask, SIGUSR1);
3501 last_update_time = 0.0;
3502 next_update_time = get_time();
3504 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3505 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3506 char buf[max_user_text];
3508 get_battery_short_status(buf, max_user_text, "BAT0");
3510 update_interval = update_interval_bat;
3512 update_interval = update_interval_old;
3517 #ifdef SIGNAL_BLOCKING
3518 /* block signals. we will inspect for pending signals later */
3519 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3520 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3525 if (output_methods & TO_X) {
3528 /* wait for X event or timeout */
3530 if (!XPending(display)) {
3534 t = next_update_time - get_time();
3538 } else if (t > update_interval) {
3539 t = update_interval;
3542 tv.tv_sec = (long) t;
3543 tv.tv_usec = (long) (t * 1000000) % 1000000;
3545 FD_SET(ConnectionNumber(display), &fdsr);
3547 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3549 if (errno != EINTR) {
3550 NORM_ERR("can't select(): %s", strerror(errno));
3560 if (need_to_update) {
3562 int wx = window.x, wy = window.y;
3572 /* resize window if it isn't right size */
3574 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3575 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3576 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3577 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3578 draw_stuff(); /* redraw everything in our newly sized window */
3579 XResizeWindow(display, window.window, window.width,
3580 window.height); /* resize window */
3581 set_transparent_background(window.window, own_window_argb_value);
3584 xdbe_swap_buffers();
3589 /* update lua window globals */
3590 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3591 #endif /* HAVE_LUA */
3594 /* move window if it isn't in right position */
3595 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3596 XMoveWindow(display, window.window, window.x, window.y);
3601 if (changed && window.type == TYPE_PANEL) {
3604 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3607 switch (text_alignment) {
3634 set_struts(sidenum);
3645 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3646 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3647 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3648 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3649 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3654 /* handle X events */
3655 while (XPending(display)) {
3658 XNextEvent(display, &ev);
3665 r.width = ev.xexpose.width;
3666 r.height = ev.xexpose.height;
3667 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3671 case PropertyNotify:
3673 if ( ev.xproperty.state == PropertyNewValue ) {
3674 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3680 case ReparentNotify:
3681 /* make background transparent */
3683 set_transparent_background(window.window, own_window_argb_value);
3687 case ConfigureNotify:
3689 /* if window size isn't what expected, set fixed size */
3690 if (ev.xconfigure.width != window.width
3691 || ev.xconfigure.height != window.height) {
3692 if (window.width != 0 && window.height != 0) {
3696 /* clear old stuff before screwing up
3701 XWindowAttributes attrs;
3702 if (XGetWindowAttributes(display,
3703 window.window, &attrs)) {
3704 window.width = attrs.width;
3705 window.height = attrs.height;
3709 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3710 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3711 if (text_width > maximum_width
3712 && maximum_width > 0) {
3713 text_width = maximum_width;
3717 /* if position isn't what expected, set fixed pos
3718 * total_updates avoids setting fixed_pos when window
3719 * is set to weird locations when started */
3720 /* // this is broken
3721 if (total_updates >= 2 && !fixed_pos
3722 && (window.x != ev.xconfigure.x
3723 || window.y != ev.xconfigure.y)
3724 && (ev.xconfigure.x != 0
3725 || ev.xconfigure.y != 0)) {
3733 /* if an ordinary window with decorations */
3734 if ((window.type == TYPE_NORMAL &&
3735 (!TEST_HINT(window.hints,
3736 HINT_UNDECORATED))) ||
3737 window.type == TYPE_DESKTOP) {
3738 /* allow conky to hold input focus. */
3741 /* forward the click to the desktop window */
3742 XUngrabPointer(display, ev.xbutton.time);
3743 ev.xbutton.window = window.desktop;
3744 ev.xbutton.x = ev.xbutton.x_root;
3745 ev.xbutton.y = ev.xbutton.y_root;
3746 XSendEvent(display, ev.xbutton.window, False,
3747 ButtonPressMask, &ev);
3748 XSetInputFocus(display, ev.xbutton.window,
3749 RevertToParent, ev.xbutton.time);
3756 /* if an ordinary window with decorations */
3757 if ((window.type == TYPE_NORMAL)
3758 && (!TEST_HINT(window.hints,
3759 HINT_UNDECORATED))) {
3760 /* allow conky to hold input focus. */
3763 /* forward the release to the desktop window */
3764 ev.xbutton.window = window.desktop;
3765 ev.xbutton.x = ev.xbutton.x_root;
3766 ev.xbutton.y = ev.xbutton.y_root;
3767 XSendEvent(display, ev.xbutton.window, False,
3768 ButtonReleaseMask, &ev);
3777 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3778 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3780 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3781 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3783 #endif /* HAVE_XDAMAGE */
3789 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3790 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3791 #endif /* HAVE_XDAMAGE */
3793 /* XDBE doesn't seem to provide a way to clear the back buffer
3794 * without interfering with the front buffer, other than passing
3795 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3796 * using XDBE, we need to redraw the text even if it wasn't part of
3797 * the exposed area. OTOH, if we're not going to call draw_stuff at
3798 * all, then no swap happens and we can safely do nothing. */
3800 if (!XEmptyRegion(x11_stuff.region)) {
3805 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3806 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3807 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3808 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3809 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3812 XSetRegion(display, window.gc, x11_stuff.region);
3815 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3819 XDestroyRegion(x11_stuff.region);
3820 x11_stuff.region = XCreateRegion();
3824 t = (next_update_time - get_time()) * 1000000;
3825 if(t > 0) usleep((useconds_t)t);
3829 if(output_methods & TO_NCURSES) {
3838 #ifdef SIGNAL_BLOCKING
3839 /* unblock signals of interest and let handler fly */
3840 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3841 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3845 switch (g_signal_pending) {
3848 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3853 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3856 if (output_methods & TO_X) {
3857 XDestroyRegion(x11_stuff.region);
3858 x11_stuff.region = NULL;
3860 XDamageDestroy(display, x11_stuff.damage);
3861 XFixesDestroyRegion(display, x11_stuff.region2);
3862 XFixesDestroyRegion(display, x11_stuff.part);
3863 #endif /* HAVE_XDAMAGE */
3869 if(overwrite_file) {
3870 free(overwrite_file);
3879 /* Reaching here means someone set a signal
3880 * (SIGXXXX, signal_handler), but didn't write any code
3882 * If you don't want to handle a signal, don't set a handler on
3883 * it in the first place. */
3884 if (g_signal_pending) {
3885 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3889 #ifdef HAVE_SYS_INOTIFY_H
3890 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3891 inotify_config_wd = inotify_add_watch(inotify_fd,
3895 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3896 int len = 0, idx = 0;
3898 struct timeval time_to_wait;
3900 FD_ZERO(&descriptors);
3901 FD_SET(inotify_fd, &descriptors);
3903 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3905 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3906 if (FD_ISSET(inotify_fd, &descriptors)) {
3907 /* process inotify events */
3908 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3909 while (len > 0 && idx < len) {
3910 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3911 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3912 /* current_config should be reloaded */
3913 NORM_ERR("'%s' modified, reloading...", current_config);
3915 if (ev->mask & IN_IGNORED) {
3916 /* for some reason we get IN_IGNORED here
3917 * sometimes, so we need to re-add the watch */
3918 inotify_config_wd = inotify_add_watch(inotify_fd,
3925 llua_inotify_query(ev->wd, ev->mask);
3927 #endif /* HAVE_LUA */
3928 idx += INOTIFY_EVENT_SIZE + ev->len;
3931 } else if (disable_auto_reload && inotify_fd != -1) {
3932 inotify_rm_watch(inotify_fd, inotify_config_wd);
3934 inotify_fd = inotify_config_wd = 0;
3936 #endif /* HAVE_SYS_INOTIFY_H */
3939 llua_update_info(&info, update_interval);
3940 #endif /* HAVE_LUA */
3941 g_signal_pending = 0;
3943 clean_up(NULL, NULL);
3945 #ifdef HAVE_SYS_INOTIFY_H
3946 if (inotify_fd != -1) {
3947 inotify_rm_watch(inotify_fd, inotify_config_wd);
3949 inotify_fd = inotify_config_wd = 0;
3951 #endif /* HAVE_SYS_INOTIFY_H */
3955 static void load_config_file_x11(const char *);
3957 void initialisation(int argc, char** argv);
3959 /* reload the config file */
3960 static void reload_config(void)
3962 char *current_config_copy = strdup(current_config);
3963 clean_up(NULL, NULL);
3964 sleep(1); /* slight pause */
3965 current_config = current_config_copy;
3966 initialisation(argc_copy, argv_copy);
3969 void clean_up(void *memtofree1, void* memtofree2)
3973 free_update_callbacks();
3976 if(output_methods & TO_NCURSES) {
3980 conftree_empty(currentconffile);
3981 currentconffile = NULL;
3988 timed_thread_destroy_registered_threads();
3990 if (info.cpu_usage) {
3991 free(info.cpu_usage);
3992 info.cpu_usage = NULL;
3995 if (x_initialised == YES) {
3996 if(window_created == 1) {
3997 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3998 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3999 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4000 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4004 if(x11_stuff.region) {
4005 XDestroyRegion(x11_stuff.region);
4006 x11_stuff.region = NULL;
4008 XCloseDisplay(display);
4010 if(info.x11.desktop.all_names) {
4011 free(info.x11.desktop.all_names);
4012 info.x11.desktop.all_names = NULL;
4014 if (info.x11.desktop.name) {
4015 free(info.x11.desktop.name);
4016 info.x11.desktop.name = NULL;
4020 free(fonts); //in set_default_configurations a font is set but not loaded
4028 free_text_objects(&global_root_object, 0);
4047 free(current_config);
4050 #ifdef TCP_PORT_MONITOR
4051 tcp_portmon_clear();
4060 weather_free_info();
4063 llua_shutdown_hook();
4065 #endif /* HAVE_LUA */
4067 if (output_methods & TO_X)
4075 for (i = 0; i < special_count; i++) {
4076 if (specials[i].type == GRAPH) {
4077 free(specials[i].graph);
4085 clear_diskio_stats();
4086 if(global_cpu != NULL) {
4092 static int string_to_bool(const char *s)
4095 // Assumes an option without a true/false means true
4097 } else if (strcasecmp(s, "yes") == EQUAL) {
4099 } else if (strcasecmp(s, "true") == EQUAL) {
4101 } else if (strcasecmp(s, "1") == EQUAL) {
4108 static enum alignment string_to_alignment(const char *s)
4110 if (strcasecmp(s, "top_left") == EQUAL) {
4112 } else if (strcasecmp(s, "top_right") == EQUAL) {
4114 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4116 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4118 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4119 return BOTTOM_RIGHT;
4120 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4121 return BOTTOM_MIDDLE;
4122 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4124 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4125 return MIDDLE_RIGHT;
4126 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4127 return MIDDLE_MIDDLE;
4128 } else if (strcasecmp(s, "tl") == EQUAL) {
4130 } else if (strcasecmp(s, "tr") == EQUAL) {
4132 } else if (strcasecmp(s, "tm") == EQUAL) {
4134 } else if (strcasecmp(s, "bl") == EQUAL) {
4136 } else if (strcasecmp(s, "br") == EQUAL) {
4137 return BOTTOM_RIGHT;
4138 } else if (strcasecmp(s, "bm") == EQUAL) {
4139 return BOTTOM_MIDDLE;
4140 } else if (strcasecmp(s, "ml") == EQUAL) {
4142 } else if (strcasecmp(s, "mr") == EQUAL) {
4143 return MIDDLE_RIGHT;
4144 } else if (strcasecmp(s, "mm") == EQUAL) {
4145 return MIDDLE_MIDDLE;
4146 } else if (strcasecmp(s, "none") == EQUAL) {
4154 static void set_default_configurations_for_x(void)
4156 default_fg_color = WhitePixel(display, screen);
4157 default_bg_color = BlackPixel(display, screen);
4158 default_out_color = BlackPixel(display, screen);
4159 color0 = default_fg_color;
4160 color1 = default_fg_color;
4161 color2 = default_fg_color;
4162 color3 = default_fg_color;
4163 color4 = default_fg_color;
4164 color5 = default_fg_color;
4165 color6 = default_fg_color;
4166 color7 = default_fg_color;
4167 color8 = default_fg_color;
4168 color9 = default_fg_color;
4169 current_text_color = default_fg_color;
4173 static void set_default_configurations(void)
4180 fork_to_background = 0;
4181 total_run_times = 0;
4182 info.cpu_avg_samples = 2;
4183 info.net_avg_samples = 2;
4184 info.diskio_avg_samples = 2;
4189 format_human_readable = 1;
4199 mpd_env_host = getenv("MPD_HOST");
4200 mpd_env_port = getenv("MPD_PORT");
4202 if (!mpd_env_host || !strlen(mpd_env_host)) {
4203 mpd_set_host("localhost");
4205 /* MPD_HOST environment variable is set */
4206 char *mpd_hostpart = strchr(mpd_env_host, '@');
4207 if (!mpd_hostpart) {
4208 mpd_set_host(mpd_env_host);
4210 /* MPD_HOST contains a password */
4211 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4212 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4214 if (!strlen(mpd_hostpart + 1)) {
4215 mpd_set_host("localhost");
4217 mpd_set_host(mpd_hostpart + 1);
4220 mpd_set_password(mpd_password, 1);
4225 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4226 /* failed to set port from environment variable */
4227 mpd_set_port("6600");
4231 info.xmms2.artist = NULL;
4232 info.xmms2.album = NULL;
4233 info.xmms2.title = NULL;
4234 info.xmms2.genre = NULL;
4235 info.xmms2.comment = NULL;
4236 info.xmms2.url = NULL;
4237 info.xmms2.status = NULL;
4238 info.xmms2.playlist = NULL;
4240 use_spacer = NO_SPACER;
4242 output_methods = TO_X;
4244 output_methods = TO_STDOUT;
4247 show_graph_scale = 0;
4248 show_graph_range = 0;
4251 draw_graph_borders = 1;
4253 set_first_font("6x10");
4261 window.type = TYPE_NORMAL;
4263 strcpy(window.class_name, PACKAGE_NAME);
4264 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4266 use_argb_visual = 0;
4267 own_window_argb_value = 255;
4270 stippled_borders = 0;
4271 window.border_inner_margin = 3;
4272 window.border_outer_margin = 1;
4273 window.border_width = 1;
4274 text_alignment = BOTTOM_LEFT;
4275 info.x11.monitor.number = 1;
4276 info.x11.monitor.current = 0;
4277 info.x11.desktop.current = 1;
4278 info.x11.desktop.number = 1;
4279 info.x11.desktop.nitems = 0;
4280 info.x11.desktop.all_names = NULL;
4281 info.x11.desktop.name = NULL;
4286 free(current_mail_spool);
4290 variable_substitute(MAIL_FILE, buf, 256);
4291 if (buf[0] != '\0') {
4292 current_mail_spool = strndup(buf, text_buffer_size);
4297 set_update_interval(3);
4298 update_interval_bat = NOBATTERY;
4299 info.music_player_interval = 1.0;
4300 stuff_in_uppercase = 0;
4301 info.users.number = 1;
4303 set_times_in_seconds(0);
4305 #ifdef TCP_PORT_MONITOR
4306 /* set default connection limit */
4307 tcp_portmon_set_max_connections(0);
4311 /* returns 1 if you can overwrite or create the file at 'path' */
4312 static _Bool overwrite_works(const char *path)
4316 if (!(filepointer = fopen(path, "w")))
4318 fclose(filepointer);
4322 /* returns 1 if you can append or create the file at 'path' */
4323 static _Bool append_works(const char *path)
4327 if (!(filepointer = fopen(path, "a")))
4329 fclose(filepointer);
4335 /* WARNING, this type not in Xlib spec */
4336 int x11_error_handler(Display *d, XErrorEvent *err)
4337 __attribute__((noreturn));
4338 int x11_error_handler(Display *d, XErrorEvent *err)
4340 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",
4342 (long unsigned)err->display,
4343 (long)err->resourceid,
4353 int x11_ioerror_handler(Display *d)
4354 __attribute__((noreturn));
4355 int x11_ioerror_handler(Display *d)
4357 NORM_ERR("X Error: Display %lx\n",
4364 static void X11_initialisation(void)
4366 if (x_initialised == YES) return;
4367 output_methods |= TO_X;
4369 set_default_configurations_for_x();
4370 x_initialised = YES;
4373 /* WARNING, this type not in Xlib spec */
4374 XSetErrorHandler(&x11_error_handler);
4375 XSetIOErrorHandler(&x11_ioerror_handler);
4379 static char **xargv = 0;
4380 static int xargc = 0;
4382 static void X11_create_window(void)
4384 if (output_methods & TO_X) {
4386 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4387 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4389 #else /* OWN_WINDOW */
4390 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4391 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4393 #endif /* OWN_WINDOW */
4397 update_text_area(); /* to position text/window on screen */
4400 if (own_window && !fixed_pos) {
4401 XMoveWindow(display, window.window, window.x, window.y);
4404 set_transparent_background(window.window, own_window_argb_value);
4412 x11_stuff.region = XCreateRegion();
4414 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4415 NORM_ERR("Xdamage extension unavailable");
4417 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4418 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4419 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4420 #endif /* HAVE_XDAMAGE */
4423 update_text_area(); /* to get initial size of the window */
4426 /* setup lua window globals */
4427 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4428 #endif /* HAVE_LUA */
4432 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4433 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4434 #define CONF2(a) if (strcasecmp(name, a) == 0)
4435 #define CONF(a) else CONF2(a)
4436 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4437 || strcasecmp(name, b) == 0)
4438 #define CONF_CONTINUE 1
4439 #define CONF_BREAK 2
4440 #define CONF_BUFF_SIZE 512
4442 static FILE *open_config_file(const char *f)
4444 #ifdef CONFIG_OUTPUT
4445 if (!strcmp(f, "==builtin==")) {
4446 return conf_cookie_open();
4448 #endif /* CONFIG_OUTPUT */
4449 return fopen(f, "r");
4452 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4456 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4459 remove_comments(buf);
4464 while (*p && isspace((int) *p)) {
4468 return CONF_CONTINUE; /* empty line */
4475 while (*p2 && !isspace((int) *p2)) {
4479 *p2 = '\0'; /* break at name's end */
4486 while (*p && isspace((int) *p)) {
4492 p2 = *value + strlen(*value);
4493 while (isspace((int) *(p2 - 1))) {
4502 char load_config_file(const char *f)
4507 fp = open_config_file(f);
4511 DBGP("reading contents from config file '%s'", f);
4514 char buff[CONF_BUFF_SIZE], *name, *value;
4515 int ret = do_config_step(&line, fp, buff, &name, &value);
4516 if (ret == CONF_BREAK) {
4518 } else if (ret == CONF_CONTINUE) {
4524 /* don't listen if X is already initialised or
4525 * if we already know we don't want it */
4526 if(x_initialised != YES) {
4527 if (string_to_bool(value)) {
4528 output_methods &= TO_X;
4530 output_methods &= ~TO_X;
4531 x_initialised = NEVER;
4536 if (!value || x_initialised == YES) {
4541 disp = strdup(value);
4546 if (window.type == TYPE_DOCK)
4549 #endif /*OWN_WINDOW */
4551 int a = string_to_alignment(value);
4562 CONF("background") {
4563 fork_to_background = string_to_bool(value);
4566 CONF2("background") {
4567 fork_to_background = string_to_bool(value);
4571 CONF("show_graph_scale") {
4572 show_graph_scale = string_to_bool(value);
4574 CONF("show_graph_range") {
4575 show_graph_range = string_to_bool(value);
4577 CONF("border_inner_margin") {
4579 window.border_inner_margin = strtol(value, 0, 0);
4580 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4585 CONF("border_outer_margin") {
4587 window.border_outer_margin = strtol(value, 0, 0);
4588 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4593 CONF("border_width") {
4595 window.border_width = strtol(value, 0, 0);
4596 if (window.border_width < 0) window.border_width = 0;
4602 #define TEMPLATE_CONF(n) \
4603 CONF("template"#n) { \
4604 if (set_template(n, value)) \
4619 parse_global_imap_mail_args(value);
4626 parse_global_pop3_mail_args(value);
4631 CONF("default_bar_size") {
4634 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4641 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4645 CONF("default_graph_size") {
4648 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4655 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4658 CONF("default_gauge_size") {
4661 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4668 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4675 mpd_set_host(value);
4681 if (value && mpd_set_port(value)) {
4685 CONF("mpd_password") {
4687 mpd_set_password(value, 0);
4693 CONF("music_player_interval") {
4695 info.music_player_interval = strtod(value, 0);
4701 CONF("sensor_device") {
4703 sensor_device = strtol(value, 0, 0);
4709 CONF("cpu_avg_samples") {
4711 cpu_avg_samples = strtol(value, 0, 0);
4712 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4715 info.cpu_avg_samples = cpu_avg_samples;
4721 CONF("net_avg_samples") {
4723 net_avg_samples = strtol(value, 0, 0);
4724 if (net_avg_samples < 1 || net_avg_samples > 14) {
4727 info.net_avg_samples = net_avg_samples;
4733 CONF("diskio_avg_samples") {
4735 diskio_avg_samples = strtol(value, 0, 0);
4736 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4739 info.diskio_avg_samples = diskio_avg_samples;
4747 CONF("double_buffer") {
4748 use_xdbe = string_to_bool(value);
4752 CONF("override_utf8_locale") {
4753 utf8_mode = string_to_bool(value);
4755 CONF("draw_borders") {
4756 draw_borders = string_to_bool(value);
4758 CONF("draw_graph_borders") {
4759 draw_graph_borders = string_to_bool(value);
4761 CONF("draw_shades") {
4762 draw_shades = string_to_bool(value);
4764 CONF("draw_outline") {
4765 draw_outline = string_to_bool(value);
4768 CONF("times_in_seconds") {
4769 set_times_in_seconds(string_to_bool(value));
4771 CONF("max_text_width") {
4772 max_text_width = atoi(value);
4774 CONF("out_to_console") {
4775 if(string_to_bool(value)) {
4776 output_methods |= TO_STDOUT;
4778 output_methods &= ~TO_STDOUT;
4781 CONF("extra_newline") {
4782 extra_newline = string_to_bool(value);
4784 CONF("disable_auto_reload") {
4785 disable_auto_reload = string_to_bool(value);
4787 CONF("out_to_stderr") {
4788 if(string_to_bool(value))
4789 output_methods |= TO_STDERR;
4792 CONF("out_to_ncurses") {
4793 if(string_to_bool(value)) {
4796 output_methods |= TO_NCURSES;
4800 CONF("overwrite_file") {
4801 if(overwrite_file) {
4802 free(overwrite_file);
4805 if(overwrite_works(value)) {
4806 overwrite_file = strdup(value);
4807 output_methods |= OVERWRITE_FILE;
4809 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4811 CONF("append_file") {
4816 if(append_works(value)) {
4817 append_file = strdup(value);
4818 output_methods |= APPEND_FILE;
4820 NORM_ERR("append_file won't be able to create/append '%s'", value);
4822 CONF("use_spacer") {
4824 if (strcasecmp(value, "left") == EQUAL) {
4825 use_spacer = LEFT_SPACER;
4826 } else if (strcasecmp(value, "right") == EQUAL) {
4827 use_spacer = RIGHT_SPACER;
4828 } else if (strcasecmp(value, "none") == EQUAL) {
4829 use_spacer = NO_SPACER;
4831 use_spacer = string_to_bool(value);
4832 NORM_ERR("use_spacer should have an argument of left, right, or"
4833 " none. '%s' seems to be some form of '%s', so"
4834 " defaulting to %s.", value,
4835 use_spacer ? "true" : "false",
4836 use_spacer ? "right" : "none");
4838 use_spacer = RIGHT_SPACER;
4840 use_spacer = NO_SPACER;
4844 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4845 use_spacer = RIGHT_SPACER;
4851 use_xft = string_to_bool(value);
4855 set_first_font(value);
4859 if (value && font_count >= 0) {
4860 fonts[0].font_alpha = atof(value) * 65535.0;
4867 if (string_to_bool(value)) {
4868 NORM_ERR("Xft not enabled at compile time");
4872 /* xftfont silently ignored when no Xft */
4875 /* xftalpha is silently ignored when no Xft */
4880 set_first_font(value);
4888 gap_x = atoi(value);
4895 gap_y = atoi(value);
4901 CONF("mail_spool") {
4905 variable_substitute(value, buffer, 256);
4907 if (buffer[0] != '\0') {
4908 if (current_mail_spool) {
4909 free(current_mail_spool);
4911 current_mail_spool = strndup(buffer, text_buffer_size);
4918 CONF("minimum_size") {
4920 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4922 if (sscanf(value, "%d", &minimum_width) != 1) {
4930 CONF("maximum_width") {
4932 if (sscanf(value, "%d", &maximum_width) != 1) {
4940 CONF("no_buffers") {
4941 no_buffers = string_to_bool(value);
4943 CONF("top_name_width") {
4944 if (set_top_name_width(value))
4947 CONF("top_cpu_separate") {
4948 cpu_separate = string_to_bool(value);
4950 CONF("short_units") {
4951 short_units = string_to_bool(value);
4953 CONF("format_human_readable") {
4954 format_human_readable = string_to_bool(value);
4957 CONF("hddtemp_host") {
4958 set_hddtemp_host(value);
4960 CONF("hddtemp_port") {
4961 set_hddtemp_port(value);
4963 #endif /* HDDTEMP */
4964 CONF("pad_percents") {
4965 pad_percents = atoi(value);
4969 CONF("own_window") {
4971 own_window = string_to_bool(value);
4974 CONF("own_window_class") {
4976 memset(window.class_name, 0, sizeof(window.class_name));
4977 strncpy(window.class_name, value,
4978 sizeof(window.class_name) - 1);
4981 CONF("own_window_title") {
4983 memset(window.title, 0, sizeof(window.title));
4984 strncpy(window.title, value, sizeof(window.title) - 1);
4987 CONF("own_window_transparent") {
4989 set_transparent = string_to_bool(value);
4992 CONF("own_window_hints") {
4994 char *p_hint, *p_save;
4995 char delim[] = ", ";
4997 /* tokenize the value into individual hints */
4998 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5000 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5001 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5002 SET_HINT(window.hints, HINT_UNDECORATED);
5003 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5004 SET_HINT(window.hints, HINT_BELOW);
5005 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5006 SET_HINT(window.hints, HINT_ABOVE);
5007 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5008 SET_HINT(window.hints, HINT_STICKY);
5009 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5010 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5011 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5012 SET_HINT(window.hints, HINT_SKIP_PAGER);
5017 p_hint = strtok_r(NULL, delim, &p_save);
5018 } while (p_hint != NULL);
5024 CONF("own_window_type") {
5026 if (strncmp(value, "normal", 6) == EQUAL) {
5027 window.type = TYPE_NORMAL;
5028 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5029 window.type = TYPE_DESKTOP;
5030 } else if (strncmp(value, "dock", 4) == EQUAL) {
5031 window.type = TYPE_DOCK;
5032 text_alignment = TOP_LEFT;
5033 } else if (strncmp(value, "panel", 5) == EQUAL) {
5034 window.type = TYPE_PANEL;
5035 } else if (strncmp(value, "override", 8) == EQUAL) {
5036 window.type = TYPE_OVERRIDE;
5045 CONF("own_window_argb_visual") {
5046 use_argb_visual = string_to_bool(value);
5048 CONF("own_window_argb_value") {
5049 own_window_argb_value = strtol(value, 0, 0);
5050 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5051 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5054 #endif /* USE_ARGB */
5056 CONF("stippled_borders") {
5058 stippled_borders = strtol(value, 0, 0);
5060 stippled_borders = 4;
5064 CONF("imlib_cache_size") {
5066 cimlib_set_cache_size(atoi(value));
5069 CONF("imlib_cache_flush_interval") {
5071 cimlib_set_cache_flush_interval(atoi(value));
5076 CONF("update_interval_on_battery") {
5078 update_interval_bat = strtod(value, 0);
5083 CONF("update_interval") {
5085 set_update_interval(strtod(value, 0));
5089 if (info.music_player_interval == 0) {
5090 // default to update_interval
5091 info.music_player_interval = update_interval;
5094 CONF("total_run_times") {
5096 total_run_times = strtod(value, 0);
5102 stuff_in_uppercase = string_to_bool(value);
5104 CONF("max_specials") {
5106 max_specials = atoi(value);
5111 CONF("max_user_text") {
5113 max_user_text = atoi(value);
5118 CONF("text_buffer_size") {
5120 text_buffer_size = atoi(value);
5121 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5122 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5123 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5131 if (output_methods & TO_X) {
5132 X11_initialisation();
5141 global_text = (char *) malloc(1);
5142 global_text[0] = '\0';
5145 unsigned int l = strlen(global_text);
5147 char buf[CONF_BUFF_SIZE];
5149 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5155 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5163 /* Check for continuation of \\-\n. */
5164 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5165 global_text[l-1] = '\0';
5169 global_text = (char *) realloc(global_text, l + bl + 1);
5170 strcat(global_text, buf);
5172 if (strlen(global_text) > max_user_text) {
5176 global_text_lines = line + 1;
5179 #ifdef TCP_PORT_MONITOR
5180 CONF("max_port_monitor_connections") {
5182 if (!value || (sscanf(value, "%d", &max) != 1)) {
5183 /* an error. use default, warn and continue. */
5184 tcp_portmon_set_max_connections(0);
5186 } else if (tcp_portmon_set_max_connections(max)) {
5187 /* max is < 0, default has been set*/
5192 CONF("if_up_strictness") {
5194 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5195 ifup_strictness = IFUP_UP;
5196 } else if (strcasecmp(value, "up") == EQUAL) {
5197 ifup_strictness = IFUP_UP;
5198 } else if (strcasecmp(value, "link") == EQUAL) {
5199 ifup_strictness = IFUP_LINK;
5200 } else if (strcasecmp(value, "address") == EQUAL) {
5201 ifup_strictness = IFUP_ADDR;
5203 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5204 ifup_strictness = IFUP_UP;
5208 CONF("temperature_unit") {
5210 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5211 } else if (set_temp_output_unit(value)) {
5212 NORM_ERR("temperature_unit: incorrect argument");
5219 char *ptr = strtok(value, " ");
5222 ptr = strtok(NULL, " ");
5229 CONF("lua_draw_hook_pre") {
5231 llua_set_draw_pre_hook(value);
5236 CONF("lua_draw_hook_post") {
5238 llua_set_draw_post_hook(value);
5243 CONF("lua_startup_hook") {
5245 llua_set_startup_hook(value);
5250 CONF("lua_shutdown_hook") {
5252 llua_set_shutdown_hook(value);
5258 #endif /* HAVE_LUA */
5270 CONF("default_color"){}
5271 CONF3("default_shade_color", "default_shadecolor"){}
5272 CONF3("default_outline_color", "default_outlinecolor") {}
5273 CONF("own_window_colour") {}
5276 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5282 if (info.music_player_interval == 0) {
5283 // default to update_interval
5284 info.music_player_interval = update_interval;
5286 if (!global_text) { // didn't supply any text
5287 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5289 if (!output_methods) {
5290 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5292 #if defined(NCURSES)
5294 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5295 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5296 output_methods &= ~TO_NCURSES;
5300 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5301 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5302 output_methods &= ~(TO_STDOUT | TO_STDERR);
5304 #endif /* NCURSES */
5309 static void load_config_file_x11(const char *f)
5314 fp = open_config_file(f);
5318 DBGP("reading contents from config file '%s'", f);
5321 char buff[CONF_BUFF_SIZE], *name, *value;
5322 int ret = do_config_step(&line, fp, buff, &name, &value);
5323 if (ret == CONF_BREAK) {
5325 } else if (ret == CONF_CONTINUE) {
5330 X11_initialisation();
5331 if (x_initialised == YES) {
5333 color0 = get_x11_color(value);
5340 X11_initialisation();
5341 if (x_initialised == YES) {
5343 color1 = get_x11_color(value);
5350 X11_initialisation();
5351 if (x_initialised == YES) {
5353 color2 = get_x11_color(value);
5360 X11_initialisation();
5361 if (x_initialised == YES) {
5363 color3 = get_x11_color(value);
5370 X11_initialisation();
5371 if (x_initialised == YES) {
5373 color4 = get_x11_color(value);
5380 X11_initialisation();
5381 if (x_initialised == YES) {
5383 color5 = get_x11_color(value);
5390 X11_initialisation();
5391 if (x_initialised == YES) {
5393 color6 = get_x11_color(value);
5400 X11_initialisation();
5401 if (x_initialised == YES) {
5403 color7 = get_x11_color(value);
5410 X11_initialisation();
5411 if (x_initialised == YES) {
5413 color8 = get_x11_color(value);
5420 X11_initialisation();
5421 if (x_initialised == YES) {
5423 color9 = get_x11_color(value);
5429 CONF("default_color") {
5430 X11_initialisation();
5431 if (x_initialised == YES) {
5433 default_fg_color = get_x11_color(value);
5439 CONF3("default_shade_color", "default_shadecolor") {
5440 X11_initialisation();
5441 if (x_initialised == YES) {
5443 default_bg_color = get_x11_color(value);
5449 CONF3("default_outline_color", "default_outlinecolor") {
5450 X11_initialisation();
5451 if (x_initialised == YES) {
5453 default_out_color = get_x11_color(value);
5460 CONF("own_window_colour") {
5461 X11_initialisation();
5462 if (x_initialised == YES) {
5464 background_colour = get_x11_color(value);
5466 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5467 "'#' for hex colours");
5473 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5474 if(output_methods & TO_X) {
5475 X11_initialisation();
5484 #undef CONF_CONTINUE
5485 #undef CONF_BUFF_SIZE
5493 static void print_help(const char *prog_name) {
5494 printf("Usage: %s [OPTION]...\n"
5495 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5496 "window. Command line options will override configurations defined in config\n"
5498 " -v, --version version\n"
5499 " -q, --quiet quiet mode\n"
5500 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5501 " -c, --config=FILE config file to load\n"
5502 #ifdef CONFIG_OUTPUT
5503 " -C, --print-config print the builtin default config to stdout\n"
5504 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5506 " -d, --daemonize daemonize, fork to background\n"
5507 " -h, --help help\n"
5509 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5510 " -f, --font=FONT font to use\n"
5511 " -X, --display=DISPLAY X11 display to use\n"
5513 " -o, --own-window create own window to draw\n"
5516 " -b, --double-buffer double buffer (prevents flickering)\n"
5518 " -w, --window-id=WIN_ID window id to draw\n"
5519 " -x X x position\n"
5520 " -y Y y position\n"
5522 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5523 " -u, --interval=SECS update interval\n"
5524 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5525 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5530 /* : means that character before that takes an argument */
5531 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5541 #ifdef CONFIG_OUTPUT
5546 static const struct option longopts[] = {
5547 { "help", 0, NULL, 'h' },
5548 { "version", 0, NULL, 'V' },
5549 { "debug", 0, NULL, 'D' },
5550 { "config", 1, NULL, 'c' },
5551 #ifdef CONFIG_OUTPUT
5552 { "print-config", 0, NULL, 'C' },
5554 { "daemonize", 0, NULL, 'd' },
5556 { "alignment", 1, NULL, 'a' },
5557 { "font", 1, NULL, 'f' },
5558 { "display", 1, NULL, 'X' },
5560 { "own-window", 0, NULL, 'o' },
5563 { "double-buffer", 0, NULL, 'b' },
5565 { "window-id", 1, NULL, 'w' },
5567 { "text", 1, NULL, 't' },
5568 { "interval", 0, NULL, 'u' },
5569 { "pause", 0, NULL, 'p' },
5573 void initialisation(int argc, char **argv) {
5574 struct sigaction act, oact;
5576 set_default_configurations();
5577 load_config_file(current_config);
5578 currentconffile = conftree_add(currentconffile, current_config);
5580 /* init specials array */
5581 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5582 NORM_ERR("failed to create specials array");
5586 if (current_mail_spool == NULL) {
5589 variable_substitute(MAIL_FILE, buf, 256);
5591 if (buf[0] != '\0') {
5592 current_mail_spool = strndup(buf, text_buffer_size);
5597 /* handle other command line arguments */
5599 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5600 || defined(__NetBSD__)
5601 optind = optreset = 1;
5606 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5607 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5608 "kvm_open")) == NULL) {
5609 CRIT_ERR(NULL, NULL, "cannot read kvm");
5614 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5623 fork_to_background = 1;
5626 global_debug_level++;
5630 set_first_font(optarg);
5633 text_alignment = string_to_alignment(optarg);
5652 global_text = strndup(optarg, max_user_text);
5653 convert_escapes(global_text);
5657 update_interval = strtod(optarg, 0);
5658 update_interval_old = update_interval;
5659 if (info.music_player_interval == 0) {
5660 // default to update_interval
5661 info.music_player_interval = update_interval;
5666 total_run_times = strtod(optarg, 0);
5670 gap_x = atoi(optarg);
5674 gap_y = atoi(optarg);
5678 startup_pause = atoi(optarg);
5679 sleep(startup_pause);
5689 if (output_methods & TO_X) {
5690 load_config_file_x11(current_config);
5694 /* generate text and get initial size */
5695 extract_variable_text(global_text);
5702 if (fork_to_background) {
5707 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5714 fprintf(stderr, "\n");
5719 /* parent process */
5720 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5727 start_update_threading();
5729 text_buffer = malloc(max_user_text);
5730 memset(text_buffer, 0, max_user_text);
5731 tmpstring1 = malloc(text_buffer_size);
5732 memset(tmpstring1, 0, text_buffer_size);
5733 tmpstring2 = malloc(text_buffer_size);
5734 memset(tmpstring2, 0, text_buffer_size);
5739 X11_create_window();
5742 llua_setup_info(&info, update_interval);
5743 #endif /* HAVE_LUA */
5748 /* Set signal handlers */
5749 act.sa_handler = signal_handler;
5750 sigemptyset(&act.sa_mask);
5753 act.sa_flags |= SA_RESTART;
5756 if ( sigaction(SIGINT, &act, &oact) < 0
5757 || sigaction(SIGALRM, &act, &oact) < 0
5758 || sigaction(SIGUSR1, &act, &oact) < 0
5759 || sigaction(SIGHUP, &act, &oact) < 0
5760 || sigaction(SIGTERM, &act, &oact) < 0) {
5761 NORM_ERR("error setting signal handler: %s", strerror(errno));
5765 llua_startup_hook();
5766 #endif /* HAVE_LUA */
5769 int main(int argc, char **argv)
5778 g_signal_pending = 0;
5779 max_user_text = MAX_USER_TEXT_DEFAULT;
5781 memset(&info, 0, sizeof(info));
5785 #ifdef TCP_PORT_MONITOR
5786 /* set default connection limit */
5787 tcp_portmon_set_max_connections(0);
5790 /* handle command line parameters that don't change configs */
5792 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5793 || ((s = getenv("LANG")) && *s)) {
5794 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5796 NORM_ERR("malloc failed");
5798 for (x = 0; x < strlen(s); x++) {
5799 temp[x] = tolower(s[x]);
5802 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5808 if (!setlocale(LC_CTYPE, "")) {
5809 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5813 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5824 if (current_config) {
5825 free(current_config);
5827 current_config = strndup(optarg, max_user_text);
5830 freopen("/dev/null", "w", stderr);
5833 print_help(argv[0]);
5835 #ifdef CONFIG_OUTPUT
5842 window.window = strtol(optarg, 0, 0);
5847 disp = strdup(optarg);
5856 /* check if specified config file is valid */
5857 if (current_config) {
5859 if (stat(current_config, &sb) ||
5860 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5861 NORM_ERR("invalid configuration file '%s'\n", current_config);
5862 free(current_config);
5867 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5869 if (!current_config) {
5870 /* load default config file */
5871 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5874 /* Try to use personal config file first */
5875 to_real_path(buf, CONFIG_FILE);
5876 if (buf[0] && (fp = fopen(buf, "r"))) {
5877 current_config = strndup(buf, max_user_text);
5881 /* Try to use system config file if personal config not readable */
5882 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5883 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5887 /* No readable config found */
5888 if (!current_config) {
5889 #ifdef CONFIG_OUTPUT
5890 current_config = strdup("==builtin==");
5891 NORM_ERR("no readable personal or system-wide config file found,"
5892 " using builtin default");
5894 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5895 #endif /* ! CONF_OUTPUT */
5900 /* Load xoap keys, if existing */
5904 #ifdef HAVE_SYS_INOTIFY_H
5905 inotify_fd = inotify_init1(IN_NONBLOCK);
5906 #endif /* HAVE_SYS_INOTIFY_H */
5908 initialisation(argc, argv);
5912 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5920 void alarm_handler(void) {
5922 kill(childpid, SIGTERM);
5926 static void signal_handler(int sig)
5928 /* signal handler is light as a feather, as it should be.
5929 * we will poll g_signal_pending with each loop of conky
5930 * and do any signal processing there, NOT here (except
5931 * SIGALRM because this is caused when conky is hanging) */
5932 if(sig == SIGALRM) {
5935 g_signal_pending = sig;