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 /* set to 0 after the first time conky is run, so we don't fork again after the
358 static int first_pass = 1;
360 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
362 /* filenames for output */
363 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
364 char *append_file = NULL; FILE *append_fpointer = NULL;
368 static int show_graph_scale;
369 static int show_graph_range;
371 /* Position on the screen */
372 static int text_alignment;
373 static int gap_x, gap_y;
376 static int draw_borders;
377 static int draw_graph_borders;
378 static int stippled_borders;
380 int get_stippled_borders(void)
382 return stippled_borders;
385 static int draw_shades, draw_outline;
387 long default_fg_color, default_bg_color, default_out_color;
389 /* create own window or draw stuff to root? */
390 static int set_transparent = 0;
393 static int own_window = 0;
394 static int background_colour = 0;
396 /* fixed size/pos is set if wm/user changes them */
397 static int fixed_size = 0, fixed_pos = 0;
400 static int minimum_width, minimum_height;
401 static int maximum_width;
406 static int sensor_device;
409 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
412 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
413 unsigned int max_user_text;
415 /* maximum size of individual text buffers, ie $exec buffer size */
416 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
421 /* no buffers in used memory? */
424 /* pad percentages to decimals? */
425 static int pad_percents = 0;
427 static char *global_text = 0;
429 char *get_global_text(void)
434 long global_text_lines;
436 static int total_updates;
437 static int updatereset;
439 void set_updatereset(int i)
444 int get_updatereset(void)
449 int check_contains(char *f, char *s)
452 FILE *where = open_file(f, 0);
457 while (fgets(buf1, 256, where)) {
458 if (strstr(buf1, s)) {
465 NORM_ERR("Could not open the file");
470 #define SECRIT_MULTILINE_CHAR '\x02'
472 int calc_text_width(const char *s)
474 size_t slen = strlen(s);
477 if ((output_methods & TO_X) == 0) {
487 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
488 (const FcChar8 *) s, slen, &gi);
490 XftTextExtents8(display, fonts[selected_font].xftfont,
491 (const FcChar8 *) s, slen, &gi);
497 return XTextWidth(fonts[selected_font].font, s, slen);
502 /* formatted text to render on screen, generated in generate_text(),
503 * drawn in draw_stuff() */
505 static char *text_buffer;
507 /* quite boring functions */
509 static inline void for_each_line(char *b, int f(char *, int))
512 int special_index = 0; /* specials index */
514 for (ps = b, pe = b; *pe; pe++) {
517 special_index = f(ps, special_index);
524 f(ps, special_index);
528 static void convert_escapes(char *buf)
530 char *p = buf, *s = buf;
537 } else if (*s == '\\') {
548 /* Prints anything normally printed with snprintf according to the current value
549 * of use_spacer. Actually slightly more flexible than snprintf, as you can
550 * safely specify the destination buffer as one of your inputs. */
551 int spaced_print(char *buf, int size, const char *format, int width, ...)
560 tempbuf = malloc(size * sizeof(char));
562 // Passes the varargs along to vsnprintf
563 va_start(argp, width);
564 vsnprintf(tempbuf, size, format, argp);
567 switch (use_spacer) {
569 len = snprintf(buf, size, "%s", tempbuf);
572 len = snprintf(buf, size, "%*s", width, tempbuf);
575 len = snprintf(buf, size, "%-*s", width, tempbuf);
582 /* print percentage values
584 * - i.e., unsigned values between 0 and 100
585 * - respect the value of pad_percents */
586 int percent_print(char *buf, int size, unsigned value)
588 return spaced_print(buf, size, "%u", pad_percents, value);
591 /* converts from bytes to human readable format (K, M, G, T)
593 * The algorithm always divides by 1024, as unit-conversion of byte
594 * counts suggests. But for output length determination we need to
595 * compare with 1000 here, as we print in decimal form. */
596 void human_readable(long long num, char *buf, int size)
598 const char **suffix = suffixes;
604 /* Possibly just output as usual, for example for stdout usage */
605 if (!format_human_readable) {
606 spaced_print(buf, size, "%d", 6, round_to_int(num));
617 if (llabs(num) < 1000LL) {
618 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
622 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
630 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
632 * The goal is to always have a significance of 3, by
633 * adjusting the decimal part of the number. Sample output:
637 * so the point of alignment resides between number and unit. The
638 * upside of this is that there is minimal padding necessary, though
639 * there should be a way to make alignment take place at the decimal
640 * dot (then with fixed width decimal part).
642 * Note the repdigits below: when given a precision value, printf()
643 * rounds the float to it, not just cuts off the remaining digits. So
644 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
645 * printed with a precision of 0. Yay. */
647 precision = 0; /* print 100-999 without decimal part */
649 precision = 1; /* print 10-99 with one decimal place */
651 precision = 2; /* print 0-9 with two decimal places */
653 spaced_print(buf, size, format, width, precision, fnum, *suffix);
656 /* global object list root element */
657 static struct text_object global_root_object;
659 static long current_text_color;
661 void set_current_text_color(long colour)
663 current_text_color = colour;
666 long get_current_text_color(void)
668 return current_text_color;
671 //adds newstring to to the tree unless you can already see it when travelling back.
672 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
673 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
674 struct conftree* node;
675 struct conftree* node2;
677 for(node = previous; node != NULL; node = node->back) {
678 if(strcmp(node->string, newstring) == 0) {
682 node = malloc(sizeof(struct conftree));
683 if (previous != NULL) {
684 if(previous->vert_next == NULL) {
685 previous->vert_next = node;
687 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
688 node2->horz_next = node;
691 node->string = strdup(newstring);
692 node->horz_next = NULL;
693 node->vert_next = NULL;
694 node->back = previous;
698 void conftree_empty(struct conftree* tree) {
700 conftree_empty(tree->horz_next);
701 conftree_empty(tree->vert_next);
707 struct conftree *currentconffile;
709 static void extract_variable_text(const char *p)
711 free_text_objects(&global_root_object, 0);
725 extract_variable_text_internal(&global_root_object, p);
728 void parse_conky_vars(struct text_object *root, const char *txt,
729 char *p, int p_max_size, struct information *cur)
731 extract_variable_text_internal(root, txt);
732 generate_text_internal(p, p_max_size, *root, cur);
735 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
736 * multiline objects like $exec work with $align[rc] and friends
738 void substitute_newlines(char *p, long l)
742 while (p && *p && p < s + l) {
744 /* only substitute if it's not the last newline */
745 *p = SECRIT_MULTILINE_CHAR;
751 void generate_text_internal(char *p, int p_max_size,
752 struct text_object root, struct information *cur)
754 struct text_object *obj;
756 int need_to_load_fonts = 0;
759 /* for the OBJ_top* handler */
760 struct process **needed = 0;
763 char buff_in[p_max_size];
765 #endif /* HAVE_ICONV */
769 while (obj && p_max_size > 0) {
770 needed = 0; /* reset for top stuff */
772 /* IFBLOCK jumping algorithm
774 * This is easier as it looks like:
775 * - each IF checks it's condition
776 * - on FALSE: call DO_JUMP
777 * - on TRUE: don't care
778 * - each ELSE calls DO_JUMP unconditionally
779 * - each ENDIF is silently being ignored
782 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
783 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
784 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
785 * regularly, "obj" is being updated to point to obj->next, so object parsing
786 * continues right after the corresponding ELSE or ENDIF. This means that if we
787 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
788 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
789 * jumped, and there is nothing to do.
793 if (obj->ifblock_next) \
794 obj = obj->ifblock_next; \
797 #define OBJ(a) break; case OBJ_##a:
801 NORM_ERR("not implemented obj type %d", obj->type);
803 print_read_tcp(obj, p, p_max_size);
807 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
809 #endif /* !__OpenBSD__ */
813 ok = get_freq(p, p_max_size, "%.0f", 1,
821 ok = get_freq(p, p_max_size, "%'.2f", 1000,
824 /* OpenBSD has no such flag (SUSv2) */
825 ok = get_freq(p, p_max_size, "%.2f", 1000,
827 #endif /* __OpenBSD */
830 #if defined(__linux__)
832 print_voltage_mv(obj, p, p_max_size);
835 print_voltage_v(obj, p, p_max_size);
839 OBJ(wireless_essid) {
840 print_wireless_essid(obj, p, p_max_size);
843 print_wireless_mode(obj, p, p_max_size);
845 OBJ(wireless_bitrate) {
846 print_wireless_bitrate(obj, p, p_max_size);
849 print_wireless_ap(obj, p, p_max_size);
851 OBJ(wireless_link_qual) {
852 print_wireless_link_qual(obj, p, p_max_size);
854 OBJ(wireless_link_qual_max) {
855 print_wireless_link_qual_max(obj, p, p_max_size);
857 OBJ(wireless_link_qual_perc) {
858 print_wireless_link_qual_perc(obj, p, p_max_size);
860 OBJ(wireless_link_bar) {
861 print_wireless_link_bar(obj, p, p_max_size);
863 #endif /* HAVE_IWLIB */
865 #endif /* __linux__ */
869 get_acpi_fan(p, p_max_size);
872 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
875 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
878 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
880 OBJ(battery_percent) {
881 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
884 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
887 get_battery_short_status(p, p_max_size, obj->data.s);
889 #endif /* __OpenBSD__ */
892 human_readable(cur->buffers * 1024, p, 255);
895 human_readable(cur->cached * 1024, p, 255);
897 OBJ(cmdline_to_pid) {
898 print_cmdline_to_pid(obj, p, p_max_size);
901 if (cur->cpu_usage) {
902 if (obj->data.i > info.cpu_count) {
903 NORM_ERR("obj->data.i %i info.cpu_count %i",
904 obj->data.i, info.cpu_count);
905 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
907 percent_print(p, p_max_size,
908 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
912 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
914 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
918 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
921 print_loadgraph(obj, p, p_max_size);
925 new_fg(p, obj->data.l);
959 #if defined(__linux__)
961 snprintf(p, p_max_size, "%s",
962 get_disk_protect_queue(obj->data.s));
965 print_i8k_version(obj, p, p_max_size);
968 print_i8k_bios(obj, p, p_max_size);
971 print_i8k_serial(obj, p, p_max_size);
974 print_i8k_cpu_temp(obj, p, p_max_size);
976 OBJ(i8k_left_fan_status) {
977 print_i8k_left_fan_status(obj, p, p_max_size);
979 OBJ(i8k_right_fan_status) {
980 print_i8k_right_fan_status(obj, p, p_max_size);
982 OBJ(i8k_left_fan_rpm) {
983 print_i8k_left_fan_rpm(obj, p, p_max_size);
985 OBJ(i8k_right_fan_rpm) {
986 print_i8k_right_fan_rpm(obj, p, p_max_size);
989 print_i8k_ac_status(obj, p, p_max_size);
991 OBJ(i8k_buttons_status) {
992 print_i8k_buttons_status(obj, p, p_max_size);
996 get_ibm_acpi_fan(obj, p, p_max_size);
999 print_ibm_temps(obj, p, p_max_size);
1002 get_ibm_acpi_volume(obj, p, p_max_size);
1004 OBJ(ibm_brightness) {
1005 get_ibm_acpi_brightness(obj, p, p_max_size);
1008 /* information from sony_laptop kernel module
1009 * /sys/devices/platform/sony-laptop */
1010 OBJ(sony_fanspeed) {
1011 get_sony_fanspeed(p, p_max_size);
1014 if (!gateway_exists()) {
1019 print_gateway_iface(p, p_max_size);
1022 print_gateway_ip(p, p_max_size);
1025 snprintf(p, p_max_size, "%d", get_laptop_mode());
1028 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1030 #endif /* __linux__ */
1031 #if (defined(__FreeBSD__) || defined(__linux__))
1033 if (!interface_up(obj)) {
1039 OBJ(obsd_sensors_temp) {
1040 print_obsd_sensors_temp(obj, p, p_max_size);
1042 OBJ(obsd_sensors_fan) {
1043 print_obsd_sensors_fan(obj, p, p_max_size);
1045 OBJ(obsd_sensors_volt) {
1046 print_obsd_sensors_volt(obj, p, p_max_size);
1049 get_obsd_vendor(p, p_max_size);
1052 get_obsd_product(p, p_max_size);
1054 #endif /* __OpenBSD__ */
1057 new_font(p, obj->data.s);
1058 need_to_load_fonts = 1;
1062 print_diskio(obj, p, p_max_size);
1065 print_diskio_write(obj, p, p_max_size);
1068 print_diskio_read(obj, p, p_max_size);
1072 print_diskiograph(obj, p, p_max_size);
1074 OBJ(diskiograph_read) {
1075 print_diskiograph_read(obj, p, p_max_size);
1077 OBJ(diskiograph_write) {
1078 print_diskiograph_write(obj, p, p_max_size);
1082 print_downspeed(obj, p, p_max_size);
1085 print_downspeedf(obj, p, p_max_size);
1088 OBJ(downspeedgraph) {
1089 print_downspeedgraph(obj, p, p_max_size);
1093 /* Since we see you, you're if has not jumped.
1094 * Do Ninja jump here: without leaving traces.
1095 * This is to prevent us from stale jumped flags.
1097 obj = obj->ifblock_next;
1101 /* harmless object, just ignore */
1104 print_addr(obj, p, p_max_size);
1106 #if defined(__linux__)
1108 print_addrs(obj, p, p_max_size);
1110 #endif /* __linux__ */
1111 #if defined(IMLIB2) && defined(X11)
1113 /* doesn't actually draw anything, just queues it omp. the
1114 * image will get drawn after the X event loop */
1115 cimlib_add_image(obj->data.s);
1119 evaluate(obj->data.s, p, p_max_size);
1122 print_exec(obj, p, p_max_size);
1125 print_execp(obj, p, p_max_size);
1128 print_execgauge(obj, p, p_max_size);
1131 print_execbar(obj, p, p_max_size);
1135 print_execgraph(obj, p, p_max_size);
1139 print_execibar(obj, p, p_max_size);
1143 print_execigraph(obj, p, p_max_size);
1147 print_execigauge(obj, p, p_max_size);
1150 print_execi(obj, p, p_max_size);
1153 print_execpi(obj, p, p_max_size);
1156 print_texeci(obj, p, p_max_size);
1159 print_imap_unseen(obj, p, p_max_size);
1161 OBJ(imap_messages) {
1162 print_imap_messages(obj, p, p_max_size);
1165 print_pop3_unseen(obj, p, p_max_size);
1168 print_pop3_used(obj, p, p_max_size);
1171 print_fs_bar(obj, 0, p, p_max_size);
1174 print_fs_free(obj, p, p_max_size);
1177 print_fs_perc(obj, 1, p, p_max_size);
1180 print_fs_size(obj, p, p_max_size);
1183 print_fs_type(obj, p, p_max_size);
1186 print_fs_used(obj, p, p_max_size);
1189 print_fs_bar(obj, 1, p, p_max_size);
1192 print_fs_perc(obj, 0, p, p_max_size);
1195 print_loadavg(obj, p, p_max_size);
1198 new_goto(p, obj->data.i);
1205 new_hr(p, obj->data.i);
1209 print_nameserver(obj, p, p_max_size);
1213 print_eve(obj, p, p_max_size);
1218 curl_print(obj, p, p_max_size);
1223 rss_print_info(obj, p, p_max_size);
1228 print_weather(obj, p, p_max_size);
1232 OBJ(weather_forecast) {
1233 print_weather_forecast(obj, p, p_max_size);
1238 print_lua(obj, p, p_max_size);
1241 print_lua_parse(obj, p, p_max_size);
1244 print_lua_bar(obj, p, p_max_size);
1248 print_lua_graph(obj, p, p_max_size);
1252 print_lua_gauge(obj, p, p_max_size);
1254 #endif /* HAVE_LUA */
1260 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1261 snprintf(p, p_max_size, "N/A");
1263 temp_print(p, p_max_size, (double)val,
1264 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1269 new_offset(p, obj->data.i);
1272 new_voffset(p, obj->data.i);
1276 print_sysfs_sensor(obj, p, p_max_size);
1279 print_sysfs_sensor(obj, p, p_max_size);
1282 print_sysfs_sensor(obj, p, p_max_size);
1284 #endif /* __linux__ */
1286 new_alignr(p, obj->data.i);
1289 new_alignc(p, obj->data.i);
1292 char buf[max_user_text];
1293 struct information *tmp_info =
1294 malloc(sizeof(struct information));
1295 memcpy(tmp_info, cur, sizeof(struct information));
1296 generate_text_internal(buf, max_user_text,
1297 *obj->sub, tmp_info);
1299 if (strlen(buf) != 0) {
1305 char expression[max_user_text];
1307 struct information *tmp_info;
1309 tmp_info = malloc(sizeof(struct information));
1310 memcpy(tmp_info, cur, sizeof(struct information));
1311 generate_text_internal(expression, max_user_text,
1312 *obj->sub, tmp_info);
1313 DBGP("parsed arg into '%s'", expression);
1315 val = compare(expression);
1317 NORM_ERR("compare failed for expression '%s'",
1327 spc = strchr(obj->data.s, ' ');
1328 if (!spc && access(obj->data.s, F_OK)) {
1332 if (check_contains(obj->data.s, spc + 1))
1339 && (!check_mount(obj->data.s))) {
1345 if (!get_process_by_name(obj->data.s)) {
1347 if ((obj->data.s) && system(obj->data.s)) {
1352 #if defined(__linux__)
1354 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1358 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1361 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1366 human_readable(cur->mem * 1024, p, 255);
1369 human_readable(cur->memeasyfree * 1024, p, 255);
1372 human_readable(cur->memfree * 1024, p, 255);
1375 human_readable(cur->memmax * 1024, p, 255);
1379 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1382 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1385 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1389 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1394 print_mixer(obj, p, p_max_size);
1397 print_mixerl(obj, p, p_max_size);
1400 print_mixerr(obj, p, p_max_size);
1403 print_mixer_bar(obj, p, p_max_size);
1406 print_mixerl_bar(obj, p, p_max_size);
1409 print_mixerr_bar(obj, p, p_max_size);
1411 OBJ(if_mixer_mute) {
1412 if (!check_mixer_muted(obj)) {
1417 #define NOT_IN_X "Not running in X"
1419 if(x_initialised != YES) {
1420 strncpy(p, NOT_IN_X, p_max_size);
1422 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1425 OBJ(monitor_number) {
1426 if(x_initialised != YES) {
1427 strncpy(p, NOT_IN_X, p_max_size);
1429 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1433 if(x_initialised != YES) {
1434 strncpy(p, NOT_IN_X, p_max_size);
1436 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1439 OBJ(desktop_number) {
1440 if(x_initialised != YES) {
1441 strncpy(p, NOT_IN_X, p_max_size);
1443 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1447 if(x_initialised != YES) {
1448 strncpy(p, NOT_IN_X, p_max_size);
1449 }else if(cur->x11.desktop.name != NULL) {
1450 strncpy(p, cur->x11.desktop.name, p_max_size);
1456 char buf[max_user_text];
1458 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1460 print_format_time(obj, p, p_max_size);
1464 print_mails(obj, p, p_max_size);
1467 print_new_mails(obj, p, p_max_size);
1470 print_seen_mails(obj, p, p_max_size);
1473 print_unseen_mails(obj, p, p_max_size);
1475 OBJ(flagged_mails) {
1476 print_flagged_mails(obj, p, p_max_size);
1478 OBJ(unflagged_mails) {
1479 print_unflagged_mails(obj, p, p_max_size);
1481 OBJ(forwarded_mails) {
1482 print_forwarded_mails(obj, p, p_max_size);
1484 OBJ(unforwarded_mails) {
1485 print_unforwarded_mails(obj, p, p_max_size);
1487 OBJ(replied_mails) {
1488 print_replied_mails(obj, p, p_max_size);
1490 OBJ(unreplied_mails) {
1491 print_unreplied_mails(obj, p, p_max_size);
1494 print_draft_mails(obj, p, p_max_size);
1496 OBJ(trashed_mails) {
1497 print_trashed_mails(obj, p, p_max_size);
1500 print_mboxscan(obj, p, p_max_size);
1503 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1506 new_outline(p, obj->data.l);
1509 char buf[max_user_text];
1511 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1513 print_pid_chroot(obj, p, p_max_size);
1516 char buf[max_user_text];
1518 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1520 print_pid_cmdline(obj, p, p_max_size);
1523 char buf[max_user_text];
1525 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1527 print_pid_cwd(obj, p, p_max_size);
1530 char buf[max_user_text];
1532 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1534 print_pid_environ(obj, p, p_max_size);
1536 OBJ(pid_environ_list) {
1537 char buf[max_user_text];
1539 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1541 print_pid_environ_list(obj, p, p_max_size);
1544 char buf[max_user_text];
1546 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1548 print_pid_exe(obj, p, p_max_size);
1551 char buf[max_user_text];
1553 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1555 print_pid_nice(obj, p, p_max_size);
1557 OBJ(pid_openfiles) {
1558 char buf[max_user_text];
1560 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1562 print_pid_openfiles(obj, p, p_max_size);
1565 char buf[max_user_text];
1567 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1569 print_pid_parent(obj, p, p_max_size);
1572 char buf[max_user_text];
1574 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1576 print_pid_priority(obj, p, p_max_size);
1579 char buf[max_user_text];
1581 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1583 print_pid_state(obj, p, p_max_size);
1585 OBJ(pid_state_short) {
1586 char buf[max_user_text];
1588 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1590 print_pid_state_short(obj, p, p_max_size);
1593 char buf[max_user_text];
1595 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1597 print_pid_stderr(obj, p, p_max_size);
1600 char buf[max_user_text];
1602 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1604 print_pid_stdin(obj, p, p_max_size);
1607 char buf[max_user_text];
1609 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1611 print_pid_stdout(obj, p, p_max_size);
1614 char buf[max_user_text];
1616 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1618 print_pid_threads(obj, p, p_max_size);
1620 OBJ(pid_thread_list) {
1621 char buf[max_user_text];
1623 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1625 print_pid_thread_list(obj, p, p_max_size);
1627 OBJ(pid_time_kernelmode) {
1628 char buf[max_user_text];
1630 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1632 print_pid_time_kernelmode(obj, p, p_max_size);
1634 OBJ(pid_time_usermode) {
1635 char buf[max_user_text];
1637 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1639 print_pid_time_usermode(obj, p, p_max_size);
1642 char buf[max_user_text];
1644 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1646 print_pid_time(obj, p, p_max_size);
1649 char buf[max_user_text];
1651 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1653 print_pid_uid(obj, p, p_max_size);
1656 char buf[max_user_text];
1658 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1660 print_pid_euid(obj, p, p_max_size);
1663 char buf[max_user_text];
1665 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1667 print_pid_suid(obj, p, p_max_size);
1670 char buf[max_user_text];
1672 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1674 print_pid_fsuid(obj, p, p_max_size);
1677 char buf[max_user_text];
1679 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1681 print_pid_gid(obj, p, p_max_size);
1684 char buf[max_user_text];
1686 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1688 print_pid_egid(obj, p, p_max_size);
1691 char buf[max_user_text];
1693 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1695 print_pid_sgid(obj, p, p_max_size);
1698 char buf[max_user_text];
1700 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1702 print_pid_fsgid(obj, p, p_max_size);
1705 char buf[max_user_text];
1707 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1709 print_pid_read(obj, p, p_max_size);
1712 char buf[max_user_text];
1714 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1716 print_pid_vmpeak(obj, p, p_max_size);
1719 char buf[max_user_text];
1721 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1723 print_pid_vmsize(obj, p, p_max_size);
1726 char buf[max_user_text];
1728 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1730 print_pid_vmlck(obj, p, p_max_size);
1733 char buf[max_user_text];
1735 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1737 print_pid_vmhwm(obj, p, p_max_size);
1740 char buf[max_user_text];
1742 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1744 print_pid_vmrss(obj, p, p_max_size);
1747 char buf[max_user_text];
1749 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1751 print_pid_vmdata(obj, p, p_max_size);
1754 char buf[max_user_text];
1756 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1758 print_pid_vmstk(obj, p, p_max_size);
1761 char buf[max_user_text];
1763 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1765 print_pid_vmexe(obj, p, p_max_size);
1768 char buf[max_user_text];
1770 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1772 print_pid_vmlib(obj, p, p_max_size);
1775 char buf[max_user_text];
1777 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1779 print_pid_vmpte(obj, p, p_max_size);
1782 char buf[max_user_text];
1784 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1786 print_pid_write(obj, p, p_max_size);
1789 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1791 OBJ(running_processes) {
1792 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1794 OBJ(running_threads) {
1795 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1798 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1801 snprintf(p, p_max_size, "%s", obj->data.s);
1805 new_bg(p, obj->data.l);
1808 new_stippled_hr(obj, p);
1812 human_readable(cur->swap * 1024, p, 255);
1815 human_readable(cur->swapfree * 1024, p, 255);
1818 human_readable(cur->swapmax * 1024, p, 255);
1821 if (cur->swapmax == 0) {
1822 strncpy(p, "No swap", p_max_size);
1824 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1828 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1831 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1834 print_time(obj, p, p_max_size);
1837 print_utime(obj, p, p_max_size);
1840 print_tztime(obj, p, p_max_size);
1843 print_totaldown(obj, p, p_max_size);
1846 print_totalup(obj, p, p_max_size);
1849 char buf[max_user_text];
1851 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1853 print_gid_name(obj, p, p_max_size);
1856 char buf[max_user_text];
1858 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1860 print_uid_name(obj, p, p_max_size);
1863 snprintf(p, p_max_size, "%d", total_updates);
1866 if(total_updates % updatereset != obj->data.i - 1) {
1871 print_upspeed(obj, p, p_max_size);
1874 print_upspeedf(obj, p, p_max_size);
1878 print_upspeedgraph(obj, p, p_max_size);
1882 format_seconds_short(p, p_max_size, (int) cur->uptime);
1885 format_seconds(p, p_max_size, (int) cur->uptime);
1889 snprintf(p, p_max_size, "%s", cur->users.names);
1892 snprintf(p, p_max_size, "%s", cur->users.terms);
1895 snprintf(p, p_max_size, "%s", cur->users.times);
1898 update_user_time(obj->data.s);
1899 snprintf(p, p_max_size, "%s", cur->users.ctime);
1902 snprintf(p, p_max_size, "%d", cur->users.number);
1904 #endif /* __linux__ */
1905 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1906 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1910 msg = get_apm_adapter();
1911 snprintf(p, p_max_size, "%s", msg);
1914 OBJ(apm_battery_life) {
1917 msg = get_apm_battery_life();
1918 snprintf(p, p_max_size, "%s", msg);
1921 OBJ(apm_battery_time) {
1924 msg = get_apm_battery_time();
1925 snprintf(p, p_max_size, "%s", msg);
1928 #endif /* __FreeBSD__ __OpenBSD__ */
1932 print_mpd_title(obj, p, p_max_size);
1934 print_mpd_artist(obj, p, p_max_size);
1936 print_mpd_album(obj, p, p_max_size);
1938 print_mpd_random(obj, p, p_max_size);
1940 print_mpd_repeat(obj, p, p_max_size);
1942 print_mpd_track(obj, p, p_max_size);
1944 print_mpd_name(obj, p, p_max_size);
1946 print_mpd_file(obj, p, p_max_size);
1948 print_mpd_vol(obj, p, p_max_size);
1950 print_mpd_bitrate(obj, p, p_max_size);
1952 print_mpd_status(obj, p, p_max_size);
1954 print_mpd_elapsed(obj, p, p_max_size);
1957 print_mpd_length(obj, p, p_max_size);
1960 print_mpd_percent(obj, p, p_max_size);
1963 print_mpd_bar(obj, p, p_max_size);
1966 print_mpd_smart(obj, p, p_max_size);
1968 OBJ(if_mpd_playing) {
1969 if (!mpd_get_info()->is_playing) {
1976 #define MOC_PRINT(t, a) \
1977 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1979 MOC_PRINT(state, "??");
1982 MOC_PRINT(file, "no file");
1985 MOC_PRINT(title, "no title");
1988 MOC_PRINT(artist, "no artist");
1991 MOC_PRINT(song, "no song");
1994 MOC_PRINT(album, "no album");
1996 OBJ(moc_totaltime) {
1997 MOC_PRINT(totaltime, "0:00");
2000 MOC_PRINT(timeleft, "0:00");
2003 MOC_PRINT(curtime, "0:00");
2006 MOC_PRINT(bitrate, "0Kbps");
2009 MOC_PRINT(rate, "0KHz");
2015 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2018 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2021 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2024 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2026 OBJ(xmms2_comment) {
2027 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2030 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2033 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2036 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2038 OBJ(xmms2_tracknr) {
2039 if (cur->xmms2.tracknr != -1) {
2040 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2043 OBJ(xmms2_bitrate) {
2044 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2047 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2050 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2052 OBJ(xmms2_elapsed) {
2053 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2054 (cur->xmms2.elapsed / 1000) % 60);
2056 OBJ(xmms2_duration) {
2057 snprintf(p, p_max_size, "%02d:%02d",
2058 cur->xmms2.duration / 60000,
2059 (cur->xmms2.duration / 1000) % 60);
2061 OBJ(xmms2_percent) {
2062 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2065 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2067 OBJ(xmms2_playlist) {
2068 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2070 OBJ(xmms2_timesplayed) {
2071 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2074 if (strlen(cur->xmms2.title) < 2
2075 && strlen(cur->xmms2.title) < 2) {
2076 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2078 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2082 OBJ(if_xmms2_connected) {
2083 if (cur->xmms2.conn_state != 1) {
2089 OBJ(audacious_status) {
2090 snprintf(p, p_max_size, "%s",
2091 cur->audacious.items[AUDACIOUS_STATUS]);
2093 OBJ(audacious_title) {
2094 snprintf(p, cur->audacious.max_title_len > 0
2095 ? cur->audacious.max_title_len : p_max_size, "%s",
2096 cur->audacious.items[AUDACIOUS_TITLE]);
2098 OBJ(audacious_length) {
2099 snprintf(p, p_max_size, "%s",
2100 cur->audacious.items[AUDACIOUS_LENGTH]);
2102 OBJ(audacious_length_seconds) {
2103 snprintf(p, p_max_size, "%s",
2104 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2106 OBJ(audacious_position) {
2107 snprintf(p, p_max_size, "%s",
2108 cur->audacious.items[AUDACIOUS_POSITION]);
2110 OBJ(audacious_position_seconds) {
2111 snprintf(p, p_max_size, "%s",
2112 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2114 OBJ(audacious_bitrate) {
2115 snprintf(p, p_max_size, "%s",
2116 cur->audacious.items[AUDACIOUS_BITRATE]);
2118 OBJ(audacious_frequency) {
2119 snprintf(p, p_max_size, "%s",
2120 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2122 OBJ(audacious_channels) {
2123 snprintf(p, p_max_size, "%s",
2124 cur->audacious.items[AUDACIOUS_CHANNELS]);
2126 OBJ(audacious_filename) {
2127 snprintf(p, p_max_size, "%s",
2128 cur->audacious.items[AUDACIOUS_FILENAME]);
2130 OBJ(audacious_playlist_length) {
2131 snprintf(p, p_max_size, "%s",
2132 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2134 OBJ(audacious_playlist_position) {
2135 snprintf(p, p_max_size, "%s",
2136 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2138 OBJ(audacious_main_volume) {
2139 snprintf(p, p_max_size, "%s",
2140 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2142 OBJ(audacious_bar) {
2146 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2147 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2148 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2150 #endif /* AUDACIOUS */
2154 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2157 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2160 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2163 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2166 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2169 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2172 /* we have four different types of top (top, top_mem,
2173 * top_time and top_io). To avoid having almost-same code four
2174 * times, we have this special handler. */
2183 print_top(obj, p, p_max_size);
2184 #endif /* __linux__ */
2186 print_tailhead("tail", obj, p, p_max_size);
2189 print_tailhead("head", obj, p, p_max_size);
2192 print_lines(obj, p, p_max_size);
2196 print_words(obj, p, p_max_size);
2198 #ifdef TCP_PORT_MONITOR
2200 tcp_portmon_action(obj, p, p_max_size);
2202 #endif /* TCP_PORT_MONITOR */
2206 do_iconv_start(obj);
2211 #endif /* HAVE_ICONV */
2213 OBJ(entropy_avail) {
2214 print_entropy_avail(obj, p, p_max_size);
2217 print_entropy_perc(obj, p, p_max_size);
2219 OBJ(entropy_poolsize) {
2220 print_entropy_poolsize(obj, p, p_max_size);
2223 print_entropy_bar(obj, p, p_max_size);
2227 print_smapi(obj, p, p_max_size);
2229 OBJ(if_smapi_bat_installed) {
2231 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2232 if(!smapi_bat_installed(idx)) {
2236 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2238 OBJ(smapi_bat_perc) {
2239 print_smapi_bat_perc(obj, p, p_max_size);
2241 OBJ(smapi_bat_temp) {
2242 print_smapi_bat_temp(obj, p, p_max_size);
2244 OBJ(smapi_bat_power) {
2245 print_smapi_bat_power(obj, p, p_max_size);
2247 OBJ(smapi_bat_bar) {
2248 print_smapi_bat_bar(obj, p, p_max_size);
2253 char buf[max_user_text];
2255 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2256 snprintf(p, p_max_size, "%s", buf);
2262 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2263 char buf[max_user_text];
2266 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2267 snprintf(p, p_max_size, "%s", buf);
2268 if(total_updates % 2) {
2269 for(j=0; p[j] != 0; j++) {
2275 char buf[max_user_text];
2277 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2279 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2280 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2281 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2282 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2283 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2284 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2285 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2287 snprintf(p, p_max_size, "%s", buf);
2290 print_scroll(obj, p, p_max_size, cur);
2293 print_combine(obj, p, cur);
2297 print_nvidia_value(obj, display, p, p_max_size);
2302 /* This is just a meta-object to set host:port */
2305 snprintf(p, p_max_size, "%s",
2306 cur->apcupsd.items[APCUPSD_NAME]);
2308 OBJ(apcupsd_model) {
2309 snprintf(p, p_max_size, "%s",
2310 cur->apcupsd.items[APCUPSD_MODEL]);
2312 OBJ(apcupsd_upsmode) {
2313 snprintf(p, p_max_size, "%s",
2314 cur->apcupsd.items[APCUPSD_UPSMODE]);
2316 OBJ(apcupsd_cable) {
2317 snprintf(p, p_max_size, "%s",
2318 cur->apcupsd.items[APCUPSD_CABLE]);
2320 OBJ(apcupsd_status) {
2321 snprintf(p, p_max_size, "%s",
2322 cur->apcupsd.items[APCUPSD_STATUS]);
2324 OBJ(apcupsd_linev) {
2325 snprintf(p, p_max_size, "%s",
2326 cur->apcupsd.items[APCUPSD_LINEV]);
2329 snprintf(p, p_max_size, "%s",
2330 cur->apcupsd.items[APCUPSD_LOAD]);
2332 OBJ(apcupsd_loadbar) {
2334 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2335 new_bar(obj, p, p_max_size, (int) progress);
2338 OBJ(apcupsd_loadgraph) {
2340 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2341 new_graph(obj, p, p_max_size, (int)progress);
2344 OBJ(apcupsd_loadgauge) {
2346 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2347 new_gauge(obj, p, p_max_size, (int)progress);
2349 OBJ(apcupsd_charge) {
2350 snprintf(p, p_max_size, "%s",
2351 cur->apcupsd.items[APCUPSD_CHARGE]);
2353 OBJ(apcupsd_timeleft) {
2354 snprintf(p, p_max_size, "%s",
2355 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2358 snprintf(p, p_max_size, "%s",
2359 cur->apcupsd.items[APCUPSD_TEMP]);
2361 OBJ(apcupsd_lastxfer) {
2362 snprintf(p, p_max_size, "%s",
2363 cur->apcupsd.items[APCUPSD_LASTXFER]);
2365 #endif /* APCUPSD */
2372 size_t a = strlen(p);
2375 iconv_convert(&a, buff_in, p, p_max_size);
2376 #endif /* HAVE_ICONV */
2377 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2379 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2380 #endif /* HAVE_LUA */
2382 substitute_newlines(p, a - 2);
2391 /* load any new fonts we may have had */
2392 if (need_to_load_fonts) {
2398 void evaluate(const char *text, char *p, int p_max_size)
2400 struct information *tmp_info;
2401 struct text_object subroot;
2403 tmp_info = malloc(sizeof(struct information));
2404 memcpy(tmp_info, &info, sizeof(struct information));
2405 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2406 DBGP2("evaluated '%s' to '%s'", text, p);
2408 free_text_objects(&subroot, 1);
2412 double current_update_time, next_update_time, last_update_time;
2414 static void generate_text(void)
2416 struct information *cur = &info;
2418 unsigned int i, j, k;
2424 current_update_time = get_time();
2428 /* add things to the buffer */
2434 generate_text_internal(p, max_user_text, global_root_object, cur);
2435 if(max_text_width > 0) {
2436 for(i = 0, j = 0; p[i] != 0; i++) {
2437 if(p[i] == '\n') j = 0;
2438 else if(j == max_text_width) {
2439 k = i + strlen(p + i) + 1;
2440 if(k < text_buffer_size) {
2447 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2452 if (stuff_in_uppercase) {
2455 tmp_p = text_buffer;
2457 *tmp_p = toupper(*tmp_p);
2462 next_update_time += update_interval;
2463 if (next_update_time < get_time()) {
2464 next_update_time = get_time() + update_interval;
2465 } else if (next_update_time > get_time() + update_interval) {
2466 next_update_time = get_time() + update_interval;
2468 last_update_time = current_update_time;
2472 void set_update_interval(double interval)
2474 update_interval = interval;
2475 update_interval_old = interval;
2478 int get_string_width(const char *s)
2480 return *s ? calc_text_width(s) : 0;
2484 static int get_string_width_special(char *s, int special_index)
2494 if ((output_methods & TO_X) == 0)
2497 p = strndup(s, text_buffer_size);
2501 if (*p == SPECIAL_CHAR) {
2502 /* shift everything over by 1 so that the special char
2503 * doesn't mess up the size calculation */
2504 for (i = 0; i < (long)strlen(p); i++) {
2505 *(p + i) = *(p + i + 1);
2507 if (specials[special_index + idx].type == GRAPH
2508 || specials[special_index + idx].type == GAUGE
2509 || specials[special_index + idx].type == BAR) {
2510 width += specials[special_index + idx].width;
2513 } else if (*p == SECRIT_MULTILINE_CHAR) {
2520 if (strlen(final) > 1) {
2521 width += calc_text_width(final);
2527 static int text_size_updater(char *s, int special_index);
2529 int last_font_height;
2530 static void update_text_area(void)
2534 if ((output_methods & TO_X) == 0)
2536 /* update text size if it isn't fixed */
2541 text_width = minimum_width;
2543 last_font_height = font_height();
2544 for_each_line(text_buffer, text_size_updater);
2546 if (text_height < minimum_height) {
2547 text_height = minimum_height;
2549 if (text_width > maximum_width && maximum_width > 0) {
2550 text_width = maximum_width;
2554 /* get text position on workarea */
2555 switch (text_alignment) {
2556 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2560 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2561 y = workarea[3] - text_height - gap_y;
2564 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2565 y = workarea[3] / 2 - text_height / 2 - gap_y;
2568 switch (text_alignment) {
2569 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2573 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2574 x = workarea[2] - text_width - gap_x;
2577 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2578 x = workarea[2] / 2 - text_width / 2 - gap_x;
2582 if (text_alignment == NONE) { // Let the WM manage the window
2589 #endif /* OWN_WINDOW */
2592 if (own_window && !fixed_pos) {
2595 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2596 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2597 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2598 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2602 /* If window size doesn't match to workarea's size,
2603 * then window probably includes panels (gnome).
2604 * Blah, doesn't work on KDE. */
2605 if (workarea[2] != window.width || workarea[3] != window.height) {
2614 /* update lua window globals */
2615 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2616 #endif /* HAVE_LUA */
2621 static int cur_x, cur_y; /* current x and y for drawing */
2623 //draw_mode also without X11 because we only need to print to stdout with FG
2624 static int draw_mode; /* FG, BG or OUTLINE */
2626 static long current_color;
2628 static int text_size_updater(char *s, int special_index)
2632 int contain_SECRIT_MULTILINE_CHAR = 0;
2635 if ((output_methods & TO_X) == 0)
2637 /* get string widths and skip specials */
2640 if (*p == SPECIAL_CHAR) {
2642 w += get_string_width(s);
2645 if (specials[special_index].type == BAR
2646 || specials[special_index].type == GAUGE
2647 || specials[special_index].type == GRAPH) {
2648 w += specials[special_index].width;
2649 if (specials[special_index].height > last_font_height) {
2650 last_font_height = specials[special_index].height;
2651 last_font_height += font_height();
2653 } else if (specials[special_index].type == OFFSET) {
2654 if (specials[special_index].arg > 0) {
2655 w += specials[special_index].arg;
2657 } else if (specials[special_index].type == VOFFSET) {
2658 last_font_height += specials[special_index].arg;
2659 } else if (specials[special_index].type == GOTO) {
2660 if (specials[special_index].arg > cur_x) {
2661 w = (int) specials[special_index].arg;
2663 } else if (specials[special_index].type == TAB) {
2664 int start = specials[special_index].arg;
2665 int step = specials[special_index].width;
2667 if (!step || step < 0) {
2670 w += step - (cur_x - text_start_x - start) % step;
2671 } else if (specials[special_index].type == FONT) {
2672 selected_font = specials[special_index].font_added;
2673 if (font_height() > last_font_height) {
2674 last_font_height = font_height();
2680 } else if (*p == SECRIT_MULTILINE_CHAR) {
2681 contain_SECRIT_MULTILINE_CHAR = 1;
2683 lw = get_string_width(s);
2684 *p = SECRIT_MULTILINE_CHAR;
2686 w = lw > w ? lw : w;
2687 text_height += last_font_height;
2691 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2692 if (contain_SECRIT_MULTILINE_CHAR) {
2693 lw = get_string_width(s);
2694 w = lw > w ? lw : w;
2696 w += get_string_width(s);
2698 if (w > text_width) {
2701 if (text_width > maximum_width && maximum_width) {
2702 text_width = maximum_width;
2705 text_height += last_font_height;
2706 last_font_height = font_height();
2707 return special_index;
2711 static inline void set_foreground_color(long c)
2714 if (output_methods & TO_X) {
2716 if (have_argb_visual) {
2717 current_color = c | (own_window_argb_value << 24);
2719 #endif /* USE_ARGB */
2723 #endif /* USE_ARGB */
2724 XSetForeground(display, window.gc, current_color);
2728 if (output_methods & TO_NCURSES) {
2729 attron(COLOR_PAIR(c));
2731 #endif /* NCURSES */
2736 static void draw_string(const char *s)
2738 int i, i2, pos, width_of_s;
2741 char *s_with_newlines;
2747 width_of_s = get_string_width(s);
2748 s_with_newlines = strdup(s);
2749 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2750 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2751 s_with_newlines[i] = '\n';
2754 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2755 printf("%s\n", s_with_newlines);
2756 if (extra_newline) fputc('\n', stdout);
2757 fflush(stdout); /* output immediately, don't buffer */
2759 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2760 fprintf(stderr, "%s\n", s_with_newlines);
2761 fflush(stderr); /* output immediately, don't buffer */
2763 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2764 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2766 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2767 fprintf(append_fpointer, "%s\n", s_with_newlines);
2770 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2771 printw("%s", s_with_newlines);
2774 free(s_with_newlines);
2775 memset(tmpstring1, 0, text_buffer_size);
2776 memset(tmpstring2, 0, text_buffer_size);
2777 strncpy(tmpstring1, s, text_buffer_size - 1);
2782 if (output_methods & TO_X) {
2783 max = ((text_width - width_of_s) / get_string_width(" "));
2786 /* This code looks for tabs in the text and coverts them to spaces.
2787 * The trick is getting the correct number of spaces, and not going
2788 * over the window's size without forcing the window larger. */
2789 for (i = 0; i < (int) text_buffer_size; i++) {
2790 if (tmpstring1[i] == '\t') {
2792 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2793 /* guard against overrun */
2794 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2799 /* guard against overrun */
2800 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2805 if (output_methods & TO_X) {
2806 if (text_width == maximum_width) {
2807 /* this means the text is probably pushing the limit,
2808 * so we'll chop it */
2809 while (cur_x + get_string_width(tmpstring2) - text_start_x
2810 > maximum_width && strlen(tmpstring2) > 0) {
2811 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2818 if (output_methods & TO_X) {
2824 c.pixel = current_color;
2825 // query color on custom colormap
2826 XQueryColor(display, window.colourmap, &c);
2829 c2.color.red = c.red;
2830 c2.color.green = c.green;
2831 c2.color.blue = c.blue;
2832 c2.color.alpha = fonts[selected_font].font_alpha;
2834 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2835 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2837 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2838 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2843 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2846 cur_x += width_of_s;
2849 memcpy(tmpstring1, s, text_buffer_size);
2852 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2860 int last_special_needed = -1;
2861 int orig_special_index = special_index;
2864 if (output_methods & TO_X) {
2865 font_h = font_height();
2866 cur_y += font_ascent();
2868 cur_x = text_start_x;
2872 if (*p == SECRIT_MULTILINE_CHAR) {
2873 /* special newline marker for multiline objects */
2878 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2883 /* draw string before special, unless we're dealing multiline
2885 if (last_special_applied > -1) {
2886 special_index = last_special_applied;
2894 switch (specials[special_index].type) {
2896 case HORIZONTAL_LINE:
2898 int h = specials[special_index].height;
2899 int mid = font_ascent() / 2;
2901 w = text_start_x + text_width - cur_x;
2903 XSetLineAttributes(display, window.gc, h, LineSolid,
2904 CapButt, JoinMiter);
2905 XDrawLine(display, window.drawable, window.gc, cur_x,
2906 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2912 int h = specials[special_index].height;
2913 int tmp_s = specials[special_index].arg;
2914 int mid = font_ascent() / 2;
2915 char ss[2] = { tmp_s, tmp_s };
2917 w = text_start_x + text_width - cur_x - 1;
2918 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2919 CapButt, JoinMiter);
2920 XSetDashes(display, window.gc, 0, ss, 2);
2921 XDrawLine(display, window.drawable, window.gc, cur_x,
2922 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2928 int h, bar_usage, by;
2929 if (cur_x - text_start_x > maximum_width
2930 && maximum_width > 0) {
2933 h = specials[special_index].height;
2934 bar_usage = specials[special_index].arg;
2935 by = cur_y - (font_ascent() / 2) - 1;
2940 w = specials[special_index].width;
2942 w = text_start_x + text_width - cur_x - 1;
2948 XSetLineAttributes(display, window.gc, 1, LineSolid,
2949 CapButt, JoinMiter);
2951 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2953 XFillRectangle(display, window.drawable, window.gc, cur_x,
2954 by, w * bar_usage / 255, h);
2962 case GAUGE: /* new GAUGE */
2965 unsigned long last_colour = current_color;
2967 float angle, px, py;
2971 if (cur_x - text_start_x > maximum_width
2972 && maximum_width > 0) {
2976 h = specials[special_index].height;
2977 by = cur_y - (font_ascent() / 2) - 1;
2982 w = specials[special_index].width;
2984 w = text_start_x + text_width - cur_x - 1;
2990 XSetLineAttributes(display, window.gc, 1, LineSolid,
2991 CapButt, JoinMiter);
2993 XDrawArc(display, window.drawable, window.gc,
2994 cur_x, by, w, h * 2, 0, 180*64);
2997 usage = specials[special_index].arg;
2998 angle = (M_PI)*(float)(usage)/255.;
2999 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3000 py = (float)(by+(h))-(float)(h)*sin(angle);
3002 XDrawLine(display, window.drawable, window.gc,
3003 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3011 set_foreground_color(last_colour);
3019 int h, by, i = 0, j = 0;
3021 unsigned long last_colour = current_color;
3022 unsigned long *tmpcolour = 0;
3023 if (cur_x - text_start_x > maximum_width
3024 && maximum_width > 0) {
3027 h = specials[special_index].height;
3028 by = cur_y - (font_ascent() / 2) - 1;
3033 w = specials[special_index].width;
3035 w = text_start_x + text_width - cur_x - 1;
3040 if (draw_graph_borders) {
3041 XSetLineAttributes(display, window.gc, 1, LineSolid,
3042 CapButt, JoinMiter);
3043 XDrawRectangle(display, window.drawable, window.gc,
3046 XSetLineAttributes(display, window.gc, 1, LineSolid,
3047 CapButt, JoinMiter);
3049 if (specials[special_index].last_colour != 0
3050 || specials[special_index].first_colour != 0) {
3051 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3054 for (i = w - 2; i > -1; i--) {
3055 if (specials[special_index].last_colour != 0
3056 || specials[special_index].first_colour != 0) {
3057 if (specials[special_index].tempgrad) {
3060 (int)((float)(w - 2) - specials[special_index].graph[j] *
3061 (w - 2) / (float)specials[special_index].graph_scale)
3065 (int)((float)(w - 2) - specials[special_index].graph[j] *
3066 (w - 2) / (float)specials[special_index].graph_scale)
3069 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3071 (int)((float)(w - 2) - specials[special_index].graph[j] *
3072 (w - 2) / (float)specials[special_index].graph_scale)
3076 #endif /* DEBUG_lol */
3077 XSetForeground(display, window.gc, tmpcolour[
3078 (int)((float)(w - 2) - specials[special_index].graph[j] *
3079 (w - 2) / (float)specials[special_index].graph_scale)
3082 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3085 /* this is mugfugly, but it works */
3086 XDrawLine(display, window.drawable, window.gc,
3087 cur_x + i + 1, by + h, cur_x + i + 1,
3088 round_to_int((double)by + h - specials[special_index].graph[j] *
3089 (h - 1) / specials[special_index].graph_scale));
3090 if ((w - i) / ((float) (w - 2) /
3091 (specials[special_index].graph_width)) > j
3092 && j < MAX_GRAPH_DEPTH - 3) {
3096 if (tmpcolour) free(tmpcolour);
3101 /* if (draw_mode == BG) {
3102 set_foreground_color(default_bg_color);
3103 } else if (draw_mode == OUTLINE) {
3104 set_foreground_color(default_out_color);
3106 set_foreground_color(default_fg_color);
3108 if (show_graph_range) {
3111 unsigned short int seconds = update_interval * w;
3117 unsigned short int timeunits;
3119 timeunits = seconds / 86400; seconds %= 86400;
3120 if (timeunits > 0) {
3121 asprintf(&tmp_day_str, "%dd", timeunits);
3123 tmp_day_str = strdup("");
3125 timeunits = seconds / 3600; seconds %= 3600;
3126 if (timeunits > 0) {
3127 asprintf(&tmp_hour_str, "%dh", timeunits);
3129 tmp_hour_str = strdup("");
3131 timeunits = seconds / 60; seconds %= 60;
3132 if (timeunits > 0) {
3133 asprintf(&tmp_min_str, "%dm", timeunits);
3135 tmp_min_str = strdup("");
3138 asprintf(&tmp_sec_str, "%ds", seconds);
3140 tmp_sec_str = strdup("");
3142 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3143 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3145 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3147 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3148 cur_y += font_h / 2;
3149 draw_string(tmp_str);
3155 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3159 cur_x += font_ascent() / 2;
3160 cur_y += font_h / 2;
3162 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3164 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3165 draw_string(tmp_str);
3171 set_foreground_color(last_colour);
3177 int old = font_ascent();
3179 cur_y -= font_ascent();
3180 selected_font = specials[special_index].font_added;
3182 if (cur_y + font_ascent() < cur_y + old) {
3185 cur_y += font_ascent();
3187 font_h = font_height();
3192 if (draw_mode == FG) {
3193 set_foreground_color(specials[special_index].arg);
3199 if (draw_mode == BG) {
3200 set_foreground_color(specials[special_index].arg);
3205 if (draw_mode == OUTLINE) {
3206 set_foreground_color(specials[special_index].arg);
3211 w += specials[special_index].arg;
3212 last_special_needed = special_index;
3216 cur_y += specials[special_index].arg;
3220 if (specials[special_index].arg >= 0) {
3221 cur_x = (int) specials[special_index].arg;
3223 last_special_needed = special_index;
3228 int start = specials[special_index].arg;
3229 int step = specials[special_index].width;
3231 if (!step || step < 0) {
3234 w = step - (cur_x - text_start_x - start) % step;
3235 last_special_needed = special_index;
3241 /* TODO: add back in "+ window.border_inner_margin" to the end of
3243 int pos_x = text_start_x + text_width -
3244 get_string_width_special(s, special_index);
3246 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3247 "get_string_width(p) %i gap_x %i "
3248 "specials[special_index].arg %i window.border_inner_margin %i "
3249 "window.border_width %i\n", pos_x, text_start_x, text_width,
3250 cur_x, get_string_width_special(s), gap_x,
3251 specials[special_index].arg, window.border_inner_margin,
3252 window.border_width); */
3253 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3254 cur_x = pos_x - specials[special_index].arg;
3256 last_special_needed = special_index;
3262 int pos_x = (text_width) / 2 - get_string_width_special(s,
3263 special_index) / 2 - (cur_x -
3265 /* int pos_x = text_start_x + text_width / 2 -
3266 get_string_width_special(s) / 2; */
3268 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3269 "get_string_width(p) %i gap_x %i "
3270 "specials[special_index].arg %i\n", pos_x, text_start_x,
3271 text_width, cur_x, get_string_width(s), gap_x,
3272 specials[special_index].arg); */
3273 if (pos_x > specials[special_index].arg) {
3274 w = pos_x - specials[special_index].arg;
3276 last_special_needed = special_index;
3286 if (special_index != last_special_applied) {
3289 special_index = orig_special_index;
3290 last_special_applied = -1;
3301 if (output_methods & TO_NCURSES) {
3304 #endif /* NCURSES */
3306 if (output_methods & TO_X)
3307 cur_y += font_descent();
3309 if (recurse && *recurse) {
3310 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3311 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3313 return special_index;
3316 static int draw_line(char *s, int special_index)
3319 if (output_methods & TO_X) {
3320 return draw_each_line_inner(s, special_index, -1);
3324 if (output_methods & TO_NCURSES) {
3325 return draw_each_line_inner(s, special_index, -1);
3327 #endif /* NCURSES */
3329 UNUSED(special_index);
3333 static void draw_text(void)
3337 llua_draw_pre_hook();
3338 #endif /* HAVE_LUA */
3339 if (output_methods & TO_X) {
3340 cur_y = text_start_y;
3343 if (draw_borders && window.border_width > 0) {
3344 if (stippled_borders) {
3345 char ss[2] = { stippled_borders, stippled_borders };
3346 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3347 CapButt, JoinMiter);
3348 XSetDashes(display, window.gc, 0, ss, 2);
3350 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3351 CapButt, JoinMiter);
3354 XDrawRectangle(display, window.drawable, window.gc,
3355 text_start_x - window.border_inner_margin - window.border_width,
3356 text_start_y - window.border_inner_margin - window.border_width,
3357 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3358 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3366 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3367 attron(COLOR_PAIR(COLOR_WHITE));
3368 #endif /* NCURSES */
3369 for_each_line(text_buffer, draw_line);
3370 #if defined(HAVE_LUA) && defined(X11)
3371 llua_draw_post_hook();
3372 #endif /* HAVE_LUA */
3375 static void draw_stuff(void)
3378 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3380 if (overwrite_file) {
3381 overwrite_fpointer = fopen(overwrite_file, "w");
3382 if(!overwrite_fpointer)
3383 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3386 append_fpointer = fopen(append_file, "a");
3387 if(!append_fpointer)
3388 NORM_ERR("Can't append '%s' anymore", append_file);
3391 if (output_methods & TO_X) {
3393 if (draw_shades && !draw_outline) {
3396 set_foreground_color(default_bg_color);
3407 for (i = -1; i < 2; i++) {
3408 for (j = -1; j < 2; j++) {
3409 if (i == 0 && j == 0) {
3414 set_foreground_color(default_out_color);
3415 draw_mode = OUTLINE;
3423 set_foreground_color(default_fg_color);
3428 #if defined(X11) && defined(HAVE_XDBE)
3429 if (output_methods & TO_X) {
3430 xdbe_swap_buffers();
3432 #endif /* X11 && HAVE_XDBE */
3433 if(overwrite_fpointer) {
3434 fclose(overwrite_fpointer);
3435 overwrite_fpointer = 0;
3437 if(append_fpointer) {
3438 fclose(append_fpointer);
3439 append_fpointer = 0;
3444 static void clear_text(int exposures)
3448 /* The swap action is XdbeBackground, which clears */
3452 if (display && window.window) { // make sure these are !null
3453 /* there is some extra space for borders and outlines */
3454 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3455 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3456 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3457 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3462 static int need_to_update;
3464 /* update_text() generates new text and clears old text area */
3465 static void update_text(void)
3472 if (output_methods & TO_X)
3477 llua_update_info(&info, update_interval);
3478 #endif /* HAVE_LUA */
3481 #ifdef HAVE_SYS_INOTIFY_H
3485 static void main_loop(void)
3488 #ifdef SIGNAL_BLOCKING
3489 sigset_t newmask, oldmask;
3492 #ifdef HAVE_SYS_INOTIFY_H
3493 int inotify_config_wd = -1;
3494 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3495 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3496 char inotify_buff[INOTIFY_BUF_LEN];
3497 #endif /* HAVE_SYS_INOTIFY_H */
3500 #ifdef SIGNAL_BLOCKING
3501 sigemptyset(&newmask);
3502 sigaddset(&newmask, SIGINT);
3503 sigaddset(&newmask, SIGTERM);
3504 sigaddset(&newmask, SIGUSR1);
3507 last_update_time = 0.0;
3508 next_update_time = get_time();
3510 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3511 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3512 char buf[max_user_text];
3514 get_battery_short_status(buf, max_user_text, "BAT0");
3516 update_interval = update_interval_bat;
3518 update_interval = update_interval_old;
3523 #ifdef SIGNAL_BLOCKING
3524 /* block signals. we will inspect for pending signals later */
3525 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3526 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3531 if (output_methods & TO_X) {
3534 /* wait for X event or timeout */
3536 if (!XPending(display)) {
3540 t = next_update_time - get_time();
3544 } else if (t > update_interval) {
3545 t = update_interval;
3548 tv.tv_sec = (long) t;
3549 tv.tv_usec = (long) (t * 1000000) % 1000000;
3551 FD_SET(ConnectionNumber(display), &fdsr);
3553 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3555 if (errno != EINTR) {
3556 NORM_ERR("can't select(): %s", strerror(errno));
3566 if (need_to_update) {
3568 int wx = window.x, wy = window.y;
3578 /* resize window if it isn't right size */
3580 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3581 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3582 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3583 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3584 draw_stuff(); /* redraw everything in our newly sized window */
3585 XResizeWindow(display, window.window, window.width,
3586 window.height); /* resize window */
3587 set_transparent_background(window.window, own_window_argb_value);
3590 xdbe_swap_buffers();
3595 /* update lua window globals */
3596 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3597 #endif /* HAVE_LUA */
3600 /* move window if it isn't in right position */
3601 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3602 XMoveWindow(display, window.window, window.x, window.y);
3607 if (changed && window.type == TYPE_PANEL) {
3610 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3613 switch (text_alignment) {
3640 set_struts(sidenum);
3651 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3652 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3653 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3654 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3655 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3660 /* handle X events */
3661 while (XPending(display)) {
3664 XNextEvent(display, &ev);
3671 r.width = ev.xexpose.width;
3672 r.height = ev.xexpose.height;
3673 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3677 case PropertyNotify:
3679 if ( ev.xproperty.state == PropertyNewValue ) {
3680 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3686 case ReparentNotify:
3687 /* make background transparent */
3689 set_transparent_background(window.window, own_window_argb_value);
3693 case ConfigureNotify:
3695 /* if window size isn't what expected, set fixed size */
3696 if (ev.xconfigure.width != window.width
3697 || ev.xconfigure.height != window.height) {
3698 if (window.width != 0 && window.height != 0) {
3702 /* clear old stuff before screwing up
3707 XWindowAttributes attrs;
3708 if (XGetWindowAttributes(display,
3709 window.window, &attrs)) {
3710 window.width = attrs.width;
3711 window.height = attrs.height;
3715 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3716 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3717 if (text_width > maximum_width
3718 && maximum_width > 0) {
3719 text_width = maximum_width;
3723 /* if position isn't what expected, set fixed pos
3724 * total_updates avoids setting fixed_pos when window
3725 * is set to weird locations when started */
3726 /* // this is broken
3727 if (total_updates >= 2 && !fixed_pos
3728 && (window.x != ev.xconfigure.x
3729 || window.y != ev.xconfigure.y)
3730 && (ev.xconfigure.x != 0
3731 || ev.xconfigure.y != 0)) {
3739 /* if an ordinary window with decorations */
3740 if ((window.type == TYPE_NORMAL &&
3741 (!TEST_HINT(window.hints,
3742 HINT_UNDECORATED))) ||
3743 window.type == TYPE_DESKTOP) {
3744 /* allow conky to hold input focus. */
3747 /* forward the click to the desktop window */
3748 XUngrabPointer(display, ev.xbutton.time);
3749 ev.xbutton.window = window.desktop;
3750 ev.xbutton.x = ev.xbutton.x_root;
3751 ev.xbutton.y = ev.xbutton.y_root;
3752 XSendEvent(display, ev.xbutton.window, False,
3753 ButtonPressMask, &ev);
3754 XSetInputFocus(display, ev.xbutton.window,
3755 RevertToParent, ev.xbutton.time);
3762 /* if an ordinary window with decorations */
3763 if ((window.type == TYPE_NORMAL)
3764 && (!TEST_HINT(window.hints,
3765 HINT_UNDECORATED))) {
3766 /* allow conky to hold input focus. */
3769 /* forward the release to the desktop window */
3770 ev.xbutton.window = window.desktop;
3771 ev.xbutton.x = ev.xbutton.x_root;
3772 ev.xbutton.y = ev.xbutton.y_root;
3773 XSendEvent(display, ev.xbutton.window, False,
3774 ButtonReleaseMask, &ev);
3783 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3784 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3786 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3787 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3789 #endif /* HAVE_XDAMAGE */
3795 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3796 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3797 #endif /* HAVE_XDAMAGE */
3799 /* XDBE doesn't seem to provide a way to clear the back buffer
3800 * without interfering with the front buffer, other than passing
3801 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3802 * using XDBE, we need to redraw the text even if it wasn't part of
3803 * the exposed area. OTOH, if we're not going to call draw_stuff at
3804 * all, then no swap happens and we can safely do nothing. */
3806 if (!XEmptyRegion(x11_stuff.region)) {
3811 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3812 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3813 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3814 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3815 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3818 XSetRegion(display, window.gc, x11_stuff.region);
3821 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3825 XDestroyRegion(x11_stuff.region);
3826 x11_stuff.region = XCreateRegion();
3830 t = (next_update_time - get_time()) * 1000000;
3831 if(t > 0) usleep((useconds_t)t);
3835 if(output_methods & TO_NCURSES) {
3844 #ifdef SIGNAL_BLOCKING
3845 /* unblock signals of interest and let handler fly */
3846 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3847 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3851 switch (g_signal_pending) {
3854 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3859 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3862 if (output_methods & TO_X) {
3863 XDestroyRegion(x11_stuff.region);
3864 x11_stuff.region = NULL;
3866 XDamageDestroy(display, x11_stuff.damage);
3867 XFixesDestroyRegion(display, x11_stuff.region2);
3868 XFixesDestroyRegion(display, x11_stuff.part);
3869 #endif /* HAVE_XDAMAGE */
3875 if(overwrite_file) {
3876 free(overwrite_file);
3885 /* Reaching here means someone set a signal
3886 * (SIGXXXX, signal_handler), but didn't write any code
3888 * If you don't want to handle a signal, don't set a handler on
3889 * it in the first place. */
3890 if (g_signal_pending) {
3891 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3895 #ifdef HAVE_SYS_INOTIFY_H
3896 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3897 inotify_config_wd = inotify_add_watch(inotify_fd,
3901 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3902 int len = 0, idx = 0;
3904 struct timeval time_to_wait;
3906 FD_ZERO(&descriptors);
3907 FD_SET(inotify_fd, &descriptors);
3909 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3911 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3912 if (FD_ISSET(inotify_fd, &descriptors)) {
3913 /* process inotify events */
3914 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3915 while (len > 0 && idx < len) {
3916 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3917 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3918 /* current_config should be reloaded */
3919 NORM_ERR("'%s' modified, reloading...", current_config);
3921 if (ev->mask & IN_IGNORED) {
3922 /* for some reason we get IN_IGNORED here
3923 * sometimes, so we need to re-add the watch */
3924 inotify_config_wd = inotify_add_watch(inotify_fd,
3932 llua_inotify_query(ev->wd, ev->mask);
3934 #endif /* HAVE_LUA */
3935 idx += INOTIFY_EVENT_SIZE + ev->len;
3938 } else if (disable_auto_reload && inotify_fd != -1) {
3939 inotify_rm_watch(inotify_fd, inotify_config_wd);
3941 inotify_fd = inotify_config_wd = 0;
3943 #endif /* HAVE_SYS_INOTIFY_H */
3946 llua_update_info(&info, update_interval);
3947 #endif /* HAVE_LUA */
3948 g_signal_pending = 0;
3950 clean_up(NULL, NULL);
3952 #ifdef HAVE_SYS_INOTIFY_H
3953 if (inotify_fd != -1) {
3954 inotify_rm_watch(inotify_fd, inotify_config_wd);
3956 inotify_fd = inotify_config_wd = 0;
3958 #endif /* HAVE_SYS_INOTIFY_H */
3962 static void load_config_file_x11(const char *);
3964 void initialisation(int argc, char** argv);
3966 /* reload the config file */
3967 static void reload_config(void)
3969 char *current_config_copy = strdup(current_config);
3970 clean_up(NULL, NULL);
3971 sleep(1); /* slight pause */
3972 current_config = current_config_copy;
3973 initialisation(argc_copy, argv_copy);
3976 void clean_up(void *memtofree1, void* memtofree2)
3980 free_update_callbacks();
3983 if(output_methods & TO_NCURSES) {
3987 conftree_empty(currentconffile);
3988 currentconffile = NULL;
3995 timed_thread_destroy_registered_threads();
3997 if (info.cpu_usage) {
3998 free(info.cpu_usage);
3999 info.cpu_usage = NULL;
4002 if (x_initialised == YES) {
4003 if(window_created == 1) {
4004 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4005 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4006 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4007 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4011 if(x11_stuff.region) {
4012 XDestroyRegion(x11_stuff.region);
4013 x11_stuff.region = NULL;
4015 XCloseDisplay(display);
4017 if(info.x11.desktop.all_names) {
4018 free(info.x11.desktop.all_names);
4019 info.x11.desktop.all_names = NULL;
4021 if (info.x11.desktop.name) {
4022 free(info.x11.desktop.name);
4023 info.x11.desktop.name = NULL;
4027 free(fonts); //in set_default_configurations a font is set but not loaded
4035 free_text_objects(&global_root_object, 0);
4054 free(current_config);
4057 #ifdef TCP_PORT_MONITOR
4058 tcp_portmon_clear();
4067 weather_free_info();
4070 llua_shutdown_hook();
4072 #endif /* HAVE_LUA */
4074 if (output_methods & TO_X)
4082 for (i = 0; i < special_count; i++) {
4083 if (specials[i].type == GRAPH) {
4084 free(specials[i].graph);
4092 clear_diskio_stats();
4093 if(global_cpu != NULL) {
4099 static int string_to_bool(const char *s)
4102 // Assumes an option without a true/false means true
4104 } else if (strcasecmp(s, "yes") == EQUAL) {
4106 } else if (strcasecmp(s, "true") == EQUAL) {
4108 } else if (strcasecmp(s, "1") == EQUAL) {
4115 static enum alignment string_to_alignment(const char *s)
4117 if (strcasecmp(s, "top_left") == EQUAL) {
4119 } else if (strcasecmp(s, "top_right") == EQUAL) {
4121 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4123 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4125 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4126 return BOTTOM_RIGHT;
4127 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4128 return BOTTOM_MIDDLE;
4129 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4131 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4132 return MIDDLE_RIGHT;
4133 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4134 return MIDDLE_MIDDLE;
4135 } else if (strcasecmp(s, "tl") == EQUAL) {
4137 } else if (strcasecmp(s, "tr") == EQUAL) {
4139 } else if (strcasecmp(s, "tm") == EQUAL) {
4141 } else if (strcasecmp(s, "bl") == EQUAL) {
4143 } else if (strcasecmp(s, "br") == EQUAL) {
4144 return BOTTOM_RIGHT;
4145 } else if (strcasecmp(s, "bm") == EQUAL) {
4146 return BOTTOM_MIDDLE;
4147 } else if (strcasecmp(s, "ml") == EQUAL) {
4149 } else if (strcasecmp(s, "mr") == EQUAL) {
4150 return MIDDLE_RIGHT;
4151 } else if (strcasecmp(s, "mm") == EQUAL) {
4152 return MIDDLE_MIDDLE;
4153 } else if (strcasecmp(s, "none") == EQUAL) {
4161 static void set_default_configurations_for_x(void)
4163 default_fg_color = WhitePixel(display, screen);
4164 default_bg_color = BlackPixel(display, screen);
4165 default_out_color = BlackPixel(display, screen);
4166 color0 = default_fg_color;
4167 color1 = default_fg_color;
4168 color2 = default_fg_color;
4169 color3 = default_fg_color;
4170 color4 = default_fg_color;
4171 color5 = default_fg_color;
4172 color6 = default_fg_color;
4173 color7 = default_fg_color;
4174 color8 = default_fg_color;
4175 color9 = default_fg_color;
4176 current_text_color = default_fg_color;
4180 static void set_default_configurations(void)
4187 fork_to_background = 0;
4188 total_run_times = 0;
4189 info.cpu_avg_samples = 2;
4190 info.net_avg_samples = 2;
4191 info.diskio_avg_samples = 2;
4196 format_human_readable = 1;
4206 mpd_env_host = getenv("MPD_HOST");
4207 mpd_env_port = getenv("MPD_PORT");
4209 if (!mpd_env_host || !strlen(mpd_env_host)) {
4210 mpd_set_host("localhost");
4212 /* MPD_HOST environment variable is set */
4213 char *mpd_hostpart = strchr(mpd_env_host, '@');
4214 if (!mpd_hostpart) {
4215 mpd_set_host(mpd_env_host);
4217 /* MPD_HOST contains a password */
4218 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4219 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4221 if (!strlen(mpd_hostpart + 1)) {
4222 mpd_set_host("localhost");
4224 mpd_set_host(mpd_hostpart + 1);
4227 mpd_set_password(mpd_password, 1);
4232 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4233 /* failed to set port from environment variable */
4234 mpd_set_port("6600");
4238 info.xmms2.artist = NULL;
4239 info.xmms2.album = NULL;
4240 info.xmms2.title = NULL;
4241 info.xmms2.genre = NULL;
4242 info.xmms2.comment = NULL;
4243 info.xmms2.url = NULL;
4244 info.xmms2.status = NULL;
4245 info.xmms2.playlist = NULL;
4247 use_spacer = NO_SPACER;
4249 output_methods = TO_X;
4251 output_methods = TO_STDOUT;
4254 show_graph_scale = 0;
4255 show_graph_range = 0;
4258 draw_graph_borders = 1;
4260 set_first_font("6x10");
4268 window.type = TYPE_NORMAL;
4270 strcpy(window.class_name, PACKAGE_NAME);
4271 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4273 use_argb_visual = 0;
4274 own_window_argb_value = 255;
4277 stippled_borders = 0;
4278 window.border_inner_margin = 3;
4279 window.border_outer_margin = 1;
4280 window.border_width = 1;
4281 text_alignment = BOTTOM_LEFT;
4282 info.x11.monitor.number = 1;
4283 info.x11.monitor.current = 0;
4284 info.x11.desktop.current = 1;
4285 info.x11.desktop.number = 1;
4286 info.x11.desktop.nitems = 0;
4287 info.x11.desktop.all_names = NULL;
4288 info.x11.desktop.name = NULL;
4293 free(current_mail_spool);
4297 variable_substitute(MAIL_FILE, buf, 256);
4298 if (buf[0] != '\0') {
4299 current_mail_spool = strndup(buf, text_buffer_size);
4304 set_update_interval(3);
4305 update_interval_bat = NOBATTERY;
4306 info.music_player_interval = 1.0;
4307 stuff_in_uppercase = 0;
4308 info.users.number = 1;
4310 set_times_in_seconds(0);
4312 #ifdef TCP_PORT_MONITOR
4313 /* set default connection limit */
4314 tcp_portmon_set_max_connections(0);
4318 /* returns 1 if you can overwrite or create the file at 'path' */
4319 static _Bool overwrite_works(const char *path)
4323 if (!(filepointer = fopen(path, "w")))
4325 fclose(filepointer);
4329 /* returns 1 if you can append or create the file at 'path' */
4330 static _Bool append_works(const char *path)
4334 if (!(filepointer = fopen(path, "a")))
4336 fclose(filepointer);
4342 /* WARNING, this type not in Xlib spec */
4343 int x11_error_handler(Display *d, XErrorEvent *err)
4344 __attribute__((noreturn));
4345 int x11_error_handler(Display *d, XErrorEvent *err)
4347 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",
4349 (long unsigned)err->display,
4350 (long)err->resourceid,
4360 int x11_ioerror_handler(Display *d)
4361 __attribute__((noreturn));
4362 int x11_ioerror_handler(Display *d)
4364 NORM_ERR("X Error: Display %lx\n",
4371 static void X11_initialisation(void)
4373 if (x_initialised == YES) return;
4374 output_methods |= TO_X;
4376 set_default_configurations_for_x();
4377 x_initialised = YES;
4380 /* WARNING, this type not in Xlib spec */
4381 XSetErrorHandler(&x11_error_handler);
4382 XSetIOErrorHandler(&x11_ioerror_handler);
4386 static char **xargv = 0;
4387 static int xargc = 0;
4389 static void X11_create_window(void)
4391 if (output_methods & TO_X) {
4393 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4394 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4396 #else /* OWN_WINDOW */
4397 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4398 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4400 #endif /* OWN_WINDOW */
4404 update_text_area(); /* to position text/window on screen */
4407 if (own_window && !fixed_pos) {
4408 XMoveWindow(display, window.window, window.x, window.y);
4411 set_transparent_background(window.window, own_window_argb_value);
4419 x11_stuff.region = XCreateRegion();
4421 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4422 NORM_ERR("Xdamage extension unavailable");
4424 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4425 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4426 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4427 #endif /* HAVE_XDAMAGE */
4430 update_text_area(); /* to get initial size of the window */
4433 /* setup lua window globals */
4434 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4435 #endif /* HAVE_LUA */
4439 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4440 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4441 #define CONF2(a) if (strcasecmp(name, a) == 0)
4442 #define CONF(a) else CONF2(a)
4443 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4444 || strcasecmp(name, b) == 0)
4445 #define CONF_CONTINUE 1
4446 #define CONF_BREAK 2
4447 #define CONF_BUFF_SIZE 512
4449 static FILE *open_config_file(const char *f)
4451 #ifdef CONFIG_OUTPUT
4452 if (!strcmp(f, "==builtin==")) {
4453 return conf_cookie_open();
4455 #endif /* CONFIG_OUTPUT */
4456 return fopen(f, "r");
4459 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4463 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4466 remove_comments(buf);
4471 while (*p && isspace((int) *p)) {
4475 return CONF_CONTINUE; /* empty line */
4482 while (*p2 && !isspace((int) *p2)) {
4486 *p2 = '\0'; /* break at name's end */
4493 while (*p && isspace((int) *p)) {
4499 p2 = *value + strlen(*value);
4500 while (isspace((int) *(p2 - 1))) {
4509 char load_config_file(const char *f)
4514 fp = open_config_file(f);
4518 DBGP("reading contents from config file '%s'", f);
4521 char buff[CONF_BUFF_SIZE], *name, *value;
4522 int ret = do_config_step(&line, fp, buff, &name, &value);
4523 if (ret == CONF_BREAK) {
4525 } else if (ret == CONF_CONTINUE) {
4531 /* don't listen if X is already initialised or
4532 * if we already know we don't want it */
4533 if(x_initialised != YES) {
4534 if (string_to_bool(value)) {
4535 output_methods &= TO_X;
4537 output_methods &= ~TO_X;
4538 x_initialised = NEVER;
4543 if (!value || x_initialised == YES) {
4548 disp = strdup(value);
4553 if (window.type == TYPE_DOCK)
4556 #endif /*OWN_WINDOW */
4558 int a = string_to_alignment(value);
4569 CONF("background") {
4570 fork_to_background = string_to_bool(value);
4573 CONF2("background") {
4574 fork_to_background = string_to_bool(value);
4578 CONF("show_graph_scale") {
4579 show_graph_scale = string_to_bool(value);
4581 CONF("show_graph_range") {
4582 show_graph_range = string_to_bool(value);
4584 CONF("border_inner_margin") {
4586 window.border_inner_margin = strtol(value, 0, 0);
4587 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4592 CONF("border_outer_margin") {
4594 window.border_outer_margin = strtol(value, 0, 0);
4595 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4600 CONF("border_width") {
4602 window.border_width = strtol(value, 0, 0);
4603 if (window.border_width < 0) window.border_width = 0;
4609 #define TEMPLATE_CONF(n) \
4610 CONF("template"#n) { \
4611 if (set_template(n, value)) \
4626 parse_global_imap_mail_args(value);
4633 parse_global_pop3_mail_args(value);
4638 CONF("default_bar_size") {
4641 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4648 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4652 CONF("default_graph_size") {
4655 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4662 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4665 CONF("default_gauge_size") {
4668 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4675 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4682 mpd_set_host(value);
4688 if (value && mpd_set_port(value)) {
4692 CONF("mpd_password") {
4694 mpd_set_password(value, 0);
4700 CONF("music_player_interval") {
4702 info.music_player_interval = strtod(value, 0);
4708 CONF("sensor_device") {
4710 sensor_device = strtol(value, 0, 0);
4716 CONF("cpu_avg_samples") {
4718 cpu_avg_samples = strtol(value, 0, 0);
4719 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4722 info.cpu_avg_samples = cpu_avg_samples;
4728 CONF("net_avg_samples") {
4730 net_avg_samples = strtol(value, 0, 0);
4731 if (net_avg_samples < 1 || net_avg_samples > 14) {
4734 info.net_avg_samples = net_avg_samples;
4740 CONF("diskio_avg_samples") {
4742 diskio_avg_samples = strtol(value, 0, 0);
4743 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4746 info.diskio_avg_samples = diskio_avg_samples;
4754 CONF("double_buffer") {
4755 use_xdbe = string_to_bool(value);
4759 CONF("override_utf8_locale") {
4760 utf8_mode = string_to_bool(value);
4762 CONF("draw_borders") {
4763 draw_borders = string_to_bool(value);
4765 CONF("draw_graph_borders") {
4766 draw_graph_borders = string_to_bool(value);
4768 CONF("draw_shades") {
4769 draw_shades = string_to_bool(value);
4771 CONF("draw_outline") {
4772 draw_outline = string_to_bool(value);
4775 CONF("times_in_seconds") {
4776 set_times_in_seconds(string_to_bool(value));
4778 CONF("max_text_width") {
4779 max_text_width = atoi(value);
4781 CONF("out_to_console") {
4782 if(string_to_bool(value)) {
4783 output_methods |= TO_STDOUT;
4785 output_methods &= ~TO_STDOUT;
4788 CONF("extra_newline") {
4789 extra_newline = string_to_bool(value);
4791 CONF("disable_auto_reload") {
4792 disable_auto_reload = string_to_bool(value);
4794 CONF("out_to_stderr") {
4795 if(string_to_bool(value))
4796 output_methods |= TO_STDERR;
4799 CONF("out_to_ncurses") {
4800 if(string_to_bool(value)) {
4803 output_methods |= TO_NCURSES;
4807 CONF("overwrite_file") {
4808 if(overwrite_file) {
4809 free(overwrite_file);
4812 if(overwrite_works(value)) {
4813 overwrite_file = strdup(value);
4814 output_methods |= OVERWRITE_FILE;
4816 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4818 CONF("append_file") {
4823 if(append_works(value)) {
4824 append_file = strdup(value);
4825 output_methods |= APPEND_FILE;
4827 NORM_ERR("append_file won't be able to create/append '%s'", value);
4829 CONF("use_spacer") {
4831 if (strcasecmp(value, "left") == EQUAL) {
4832 use_spacer = LEFT_SPACER;
4833 } else if (strcasecmp(value, "right") == EQUAL) {
4834 use_spacer = RIGHT_SPACER;
4835 } else if (strcasecmp(value, "none") == EQUAL) {
4836 use_spacer = NO_SPACER;
4838 use_spacer = string_to_bool(value);
4839 NORM_ERR("use_spacer should have an argument of left, right, or"
4840 " none. '%s' seems to be some form of '%s', so"
4841 " defaulting to %s.", value,
4842 use_spacer ? "true" : "false",
4843 use_spacer ? "right" : "none");
4845 use_spacer = RIGHT_SPACER;
4847 use_spacer = NO_SPACER;
4851 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4852 use_spacer = RIGHT_SPACER;
4858 use_xft = string_to_bool(value);
4862 set_first_font(value);
4866 if (value && font_count >= 0) {
4867 fonts[0].font_alpha = atof(value) * 65535.0;
4874 if (string_to_bool(value)) {
4875 NORM_ERR("Xft not enabled at compile time");
4879 /* xftfont silently ignored when no Xft */
4882 /* xftalpha is silently ignored when no Xft */
4887 set_first_font(value);
4895 gap_x = atoi(value);
4902 gap_y = atoi(value);
4908 CONF("mail_spool") {
4912 variable_substitute(value, buffer, 256);
4914 if (buffer[0] != '\0') {
4915 if (current_mail_spool) {
4916 free(current_mail_spool);
4918 current_mail_spool = strndup(buffer, text_buffer_size);
4925 CONF("minimum_size") {
4927 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4929 if (sscanf(value, "%d", &minimum_width) != 1) {
4937 CONF("maximum_width") {
4939 if (sscanf(value, "%d", &maximum_width) != 1) {
4947 CONF("no_buffers") {
4948 no_buffers = string_to_bool(value);
4950 CONF("top_name_width") {
4951 if (set_top_name_width(value))
4954 CONF("top_cpu_separate") {
4955 cpu_separate = string_to_bool(value);
4957 CONF("short_units") {
4958 short_units = string_to_bool(value);
4960 CONF("format_human_readable") {
4961 format_human_readable = string_to_bool(value);
4964 CONF("hddtemp_host") {
4965 set_hddtemp_host(value);
4967 CONF("hddtemp_port") {
4968 set_hddtemp_port(value);
4970 #endif /* HDDTEMP */
4971 CONF("pad_percents") {
4972 pad_percents = atoi(value);
4976 CONF("own_window") {
4978 own_window = string_to_bool(value);
4981 CONF("own_window_class") {
4983 memset(window.class_name, 0, sizeof(window.class_name));
4984 strncpy(window.class_name, value,
4985 sizeof(window.class_name) - 1);
4988 CONF("own_window_title") {
4990 memset(window.title, 0, sizeof(window.title));
4991 strncpy(window.title, value, sizeof(window.title) - 1);
4994 CONF("own_window_transparent") {
4996 set_transparent = string_to_bool(value);
4999 CONF("own_window_hints") {
5001 char *p_hint, *p_save;
5002 char delim[] = ", ";
5004 /* tokenize the value into individual hints */
5005 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5007 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5008 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5009 SET_HINT(window.hints, HINT_UNDECORATED);
5010 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5011 SET_HINT(window.hints, HINT_BELOW);
5012 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5013 SET_HINT(window.hints, HINT_ABOVE);
5014 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5015 SET_HINT(window.hints, HINT_STICKY);
5016 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5017 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5018 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5019 SET_HINT(window.hints, HINT_SKIP_PAGER);
5024 p_hint = strtok_r(NULL, delim, &p_save);
5025 } while (p_hint != NULL);
5031 CONF("own_window_type") {
5033 if (strncmp(value, "normal", 6) == EQUAL) {
5034 window.type = TYPE_NORMAL;
5035 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5036 window.type = TYPE_DESKTOP;
5037 } else if (strncmp(value, "dock", 4) == EQUAL) {
5038 window.type = TYPE_DOCK;
5039 text_alignment = TOP_LEFT;
5040 } else if (strncmp(value, "panel", 5) == EQUAL) {
5041 window.type = TYPE_PANEL;
5042 } else if (strncmp(value, "override", 8) == EQUAL) {
5043 window.type = TYPE_OVERRIDE;
5052 CONF("own_window_argb_visual") {
5053 use_argb_visual = string_to_bool(value);
5055 CONF("own_window_argb_value") {
5056 own_window_argb_value = strtol(value, 0, 0);
5057 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5058 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5061 #endif /* USE_ARGB */
5063 CONF("stippled_borders") {
5065 stippled_borders = strtol(value, 0, 0);
5067 stippled_borders = 4;
5071 CONF("imlib_cache_size") {
5073 cimlib_set_cache_size(atoi(value));
5076 CONF("imlib_cache_flush_interval") {
5078 cimlib_set_cache_flush_interval(atoi(value));
5083 CONF("update_interval_on_battery") {
5085 update_interval_bat = strtod(value, 0);
5090 CONF("update_interval") {
5092 set_update_interval(strtod(value, 0));
5096 if (info.music_player_interval == 0) {
5097 // default to update_interval
5098 info.music_player_interval = update_interval;
5101 CONF("total_run_times") {
5103 total_run_times = strtod(value, 0);
5109 stuff_in_uppercase = string_to_bool(value);
5111 CONF("max_specials") {
5113 max_specials = atoi(value);
5118 CONF("max_user_text") {
5120 max_user_text = atoi(value);
5125 CONF("text_buffer_size") {
5127 text_buffer_size = atoi(value);
5128 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5129 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5130 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5138 if (output_methods & TO_X) {
5139 X11_initialisation();
5148 global_text = (char *) malloc(1);
5149 global_text[0] = '\0';
5152 unsigned int l = strlen(global_text);
5154 char buf[CONF_BUFF_SIZE];
5156 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5162 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5170 /* Check for continuation of \\-\n. */
5171 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5172 global_text[l-1] = '\0';
5176 global_text = (char *) realloc(global_text, l + bl + 1);
5177 strcat(global_text, buf);
5179 if (strlen(global_text) > max_user_text) {
5183 global_text_lines = line + 1;
5186 #ifdef TCP_PORT_MONITOR
5187 CONF("max_port_monitor_connections") {
5189 if (!value || (sscanf(value, "%d", &max) != 1)) {
5190 /* an error. use default, warn and continue. */
5191 tcp_portmon_set_max_connections(0);
5193 } else if (tcp_portmon_set_max_connections(max)) {
5194 /* max is < 0, default has been set*/
5199 CONF("if_up_strictness") {
5201 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5202 ifup_strictness = IFUP_UP;
5203 } else if (strcasecmp(value, "up") == EQUAL) {
5204 ifup_strictness = IFUP_UP;
5205 } else if (strcasecmp(value, "link") == EQUAL) {
5206 ifup_strictness = IFUP_LINK;
5207 } else if (strcasecmp(value, "address") == EQUAL) {
5208 ifup_strictness = IFUP_ADDR;
5210 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5211 ifup_strictness = IFUP_UP;
5215 CONF("temperature_unit") {
5217 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5218 } else if (set_temp_output_unit(value)) {
5219 NORM_ERR("temperature_unit: incorrect argument");
5226 char *ptr = strtok(value, " ");
5229 ptr = strtok(NULL, " ");
5236 CONF("lua_draw_hook_pre") {
5238 llua_set_draw_pre_hook(value);
5243 CONF("lua_draw_hook_post") {
5245 llua_set_draw_post_hook(value);
5250 CONF("lua_startup_hook") {
5252 llua_set_startup_hook(value);
5257 CONF("lua_shutdown_hook") {
5259 llua_set_shutdown_hook(value);
5265 #endif /* HAVE_LUA */
5277 CONF("default_color"){}
5278 CONF3("default_shade_color", "default_shadecolor"){}
5279 CONF3("default_outline_color", "default_outlinecolor") {}
5280 CONF("own_window_colour") {}
5283 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5289 if (info.music_player_interval == 0) {
5290 // default to update_interval
5291 info.music_player_interval = update_interval;
5293 if (!global_text) { // didn't supply any text
5294 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5296 if (!output_methods) {
5297 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5299 #if defined(NCURSES)
5301 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5302 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5303 output_methods &= ~TO_NCURSES;
5307 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5308 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5309 output_methods &= ~(TO_STDOUT | TO_STDERR);
5311 #endif /* NCURSES */
5316 static void load_config_file_x11(const char *f)
5321 fp = open_config_file(f);
5325 DBGP("reading contents from config file '%s'", f);
5328 char buff[CONF_BUFF_SIZE], *name, *value;
5329 int ret = do_config_step(&line, fp, buff, &name, &value);
5330 if (ret == CONF_BREAK) {
5332 } else if (ret == CONF_CONTINUE) {
5337 X11_initialisation();
5338 if (x_initialised == YES) {
5340 color0 = get_x11_color(value);
5347 X11_initialisation();
5348 if (x_initialised == YES) {
5350 color1 = get_x11_color(value);
5357 X11_initialisation();
5358 if (x_initialised == YES) {
5360 color2 = get_x11_color(value);
5367 X11_initialisation();
5368 if (x_initialised == YES) {
5370 color3 = get_x11_color(value);
5377 X11_initialisation();
5378 if (x_initialised == YES) {
5380 color4 = get_x11_color(value);
5387 X11_initialisation();
5388 if (x_initialised == YES) {
5390 color5 = get_x11_color(value);
5397 X11_initialisation();
5398 if (x_initialised == YES) {
5400 color6 = get_x11_color(value);
5407 X11_initialisation();
5408 if (x_initialised == YES) {
5410 color7 = get_x11_color(value);
5417 X11_initialisation();
5418 if (x_initialised == YES) {
5420 color8 = get_x11_color(value);
5427 X11_initialisation();
5428 if (x_initialised == YES) {
5430 color9 = get_x11_color(value);
5436 CONF("default_color") {
5437 X11_initialisation();
5438 if (x_initialised == YES) {
5440 default_fg_color = get_x11_color(value);
5446 CONF3("default_shade_color", "default_shadecolor") {
5447 X11_initialisation();
5448 if (x_initialised == YES) {
5450 default_bg_color = get_x11_color(value);
5456 CONF3("default_outline_color", "default_outlinecolor") {
5457 X11_initialisation();
5458 if (x_initialised == YES) {
5460 default_out_color = get_x11_color(value);
5467 CONF("own_window_colour") {
5468 X11_initialisation();
5469 if (x_initialised == YES) {
5471 background_colour = get_x11_color(value);
5473 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5474 "'#' for hex colours");
5480 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5481 if(output_methods & TO_X) {
5482 X11_initialisation();
5491 #undef CONF_CONTINUE
5492 #undef CONF_BUFF_SIZE
5500 static void print_help(const char *prog_name) {
5501 printf("Usage: %s [OPTION]...\n"
5502 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5503 "window. Command line options will override configurations defined in config\n"
5505 " -v, --version version\n"
5506 " -q, --quiet quiet mode\n"
5507 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5508 " -c, --config=FILE config file to load\n"
5509 #ifdef CONFIG_OUTPUT
5510 " -C, --print-config print the builtin default config to stdout\n"
5511 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5513 " -d, --daemonize daemonize, fork to background\n"
5514 " -h, --help help\n"
5516 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5517 " -f, --font=FONT font to use\n"
5518 " -X, --display=DISPLAY X11 display to use\n"
5520 " -o, --own-window create own window to draw\n"
5523 " -b, --double-buffer double buffer (prevents flickering)\n"
5525 " -w, --window-id=WIN_ID window id to draw\n"
5526 " -x X x position\n"
5527 " -y Y y position\n"
5529 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5530 " -u, --interval=SECS update interval\n"
5531 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5532 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5537 /* : means that character before that takes an argument */
5538 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5548 #ifdef CONFIG_OUTPUT
5553 static const struct option longopts[] = {
5554 { "help", 0, NULL, 'h' },
5555 { "version", 0, NULL, 'V' },
5556 { "debug", 0, NULL, 'D' },
5557 { "config", 1, NULL, 'c' },
5558 #ifdef CONFIG_OUTPUT
5559 { "print-config", 0, NULL, 'C' },
5561 { "daemonize", 0, NULL, 'd' },
5563 { "alignment", 1, NULL, 'a' },
5564 { "font", 1, NULL, 'f' },
5565 { "display", 1, NULL, 'X' },
5567 { "own-window", 0, NULL, 'o' },
5570 { "double-buffer", 0, NULL, 'b' },
5572 { "window-id", 1, NULL, 'w' },
5574 { "text", 1, NULL, 't' },
5575 { "interval", 0, NULL, 'u' },
5576 { "pause", 0, NULL, 'p' },
5580 void initialisation(int argc, char **argv) {
5581 struct sigaction act, oact;
5583 set_default_configurations();
5584 load_config_file(current_config);
5585 currentconffile = conftree_add(currentconffile, current_config);
5587 /* init specials array */
5588 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5589 NORM_ERR("failed to create specials array");
5593 if (current_mail_spool == NULL) {
5596 variable_substitute(MAIL_FILE, buf, 256);
5598 if (buf[0] != '\0') {
5599 current_mail_spool = strndup(buf, text_buffer_size);
5604 /* handle other command line arguments */
5606 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5607 || defined(__NetBSD__)
5608 optind = optreset = 1;
5613 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5614 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5615 "kvm_open")) == NULL) {
5616 CRIT_ERR(NULL, NULL, "cannot read kvm");
5621 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5630 fork_to_background = 1;
5633 global_debug_level++;
5637 set_first_font(optarg);
5640 text_alignment = string_to_alignment(optarg);
5659 global_text = strndup(optarg, max_user_text);
5660 convert_escapes(global_text);
5664 update_interval = strtod(optarg, 0);
5665 update_interval_old = update_interval;
5666 if (info.music_player_interval == 0) {
5667 // default to update_interval
5668 info.music_player_interval = update_interval;
5673 total_run_times = strtod(optarg, 0);
5677 gap_x = atoi(optarg);
5681 gap_y = atoi(optarg);
5686 startup_pause = atoi(optarg);
5687 sleep(startup_pause);
5698 if (output_methods & TO_X) {
5699 load_config_file_x11(current_config);
5703 /* generate text and get initial size */
5704 extract_variable_text(global_text);
5711 if (fork_to_background && first_pass) {
5716 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5723 fprintf(stderr, "\n");
5728 /* parent process */
5729 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5736 start_update_threading();
5738 text_buffer = malloc(max_user_text);
5739 memset(text_buffer, 0, max_user_text);
5740 tmpstring1 = malloc(text_buffer_size);
5741 memset(tmpstring1, 0, text_buffer_size);
5742 tmpstring2 = malloc(text_buffer_size);
5743 memset(tmpstring2, 0, text_buffer_size);
5748 X11_create_window();
5751 llua_setup_info(&info, update_interval);
5752 #endif /* HAVE_LUA */
5757 /* Set signal handlers */
5758 act.sa_handler = signal_handler;
5759 sigemptyset(&act.sa_mask);
5762 act.sa_flags |= SA_RESTART;
5765 if ( sigaction(SIGINT, &act, &oact) < 0
5766 || sigaction(SIGALRM, &act, &oact) < 0
5767 || sigaction(SIGUSR1, &act, &oact) < 0
5768 || sigaction(SIGHUP, &act, &oact) < 0
5769 || sigaction(SIGTERM, &act, &oact) < 0) {
5770 NORM_ERR("error setting signal handler: %s", strerror(errno));
5774 llua_startup_hook();
5775 #endif /* HAVE_LUA */
5778 int main(int argc, char **argv)
5787 g_signal_pending = 0;
5788 max_user_text = MAX_USER_TEXT_DEFAULT;
5790 memset(&info, 0, sizeof(info));
5794 #ifdef TCP_PORT_MONITOR
5795 /* set default connection limit */
5796 tcp_portmon_set_max_connections(0);
5799 /* handle command line parameters that don't change configs */
5801 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5802 || ((s = getenv("LANG")) && *s)) {
5803 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5805 NORM_ERR("malloc failed");
5807 for (x = 0; x < strlen(s); x++) {
5808 temp[x] = tolower(s[x]);
5811 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5817 if (!setlocale(LC_CTYPE, "")) {
5818 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5822 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5833 if (current_config) {
5834 free(current_config);
5836 current_config = strndup(optarg, max_user_text);
5839 freopen("/dev/null", "w", stderr);
5842 print_help(argv[0]);
5844 #ifdef CONFIG_OUTPUT
5851 window.window = strtol(optarg, 0, 0);
5856 disp = strdup(optarg);
5865 /* check if specified config file is valid */
5866 if (current_config) {
5868 if (stat(current_config, &sb) ||
5869 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5870 NORM_ERR("invalid configuration file '%s'\n", current_config);
5871 free(current_config);
5876 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5878 if (!current_config) {
5879 /* load default config file */
5880 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5883 /* Try to use personal config file first */
5884 to_real_path(buf, CONFIG_FILE);
5885 if (buf[0] && (fp = fopen(buf, "r"))) {
5886 current_config = strndup(buf, max_user_text);
5890 /* Try to use system config file if personal config not readable */
5891 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5892 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5896 /* No readable config found */
5897 if (!current_config) {
5898 #ifdef CONFIG_OUTPUT
5899 current_config = strdup("==builtin==");
5900 NORM_ERR("no readable personal or system-wide config file found,"
5901 " using builtin default");
5903 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5904 #endif /* ! CONF_OUTPUT */
5909 /* Load xoap keys, if existing */
5913 #ifdef HAVE_SYS_INOTIFY_H
5914 inotify_fd = inotify_init1(IN_NONBLOCK);
5915 #endif /* HAVE_SYS_INOTIFY_H */
5917 initialisation(argc, argv);
5919 first_pass = 0; /* don't ever call fork() again */
5923 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5931 void alarm_handler(void) {
5933 kill(childpid, SIGTERM);
5937 static void signal_handler(int sig)
5939 /* signal handler is light as a feather, as it should be.
5940 * we will poll g_signal_pending with each loop of conky
5941 * and do any signal processing there, NOT here (except
5942 * SIGALRM because this is caused when conky is hanging) */
5943 if(sig == SIGALRM) {
5946 g_signal_pending = sig;