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>
74 #include <curl/curl.h>
94 #include "iconv_tools.h"
100 #include "net_stat.h"
104 #include "read_tcp.h"
106 #include "specials.h"
107 #include "temphelper.h"
108 #include "template.h"
109 #include "tailhead.h"
110 #include "timeinfo.h"
113 /* check for OS and include appropriate headers */
114 #if defined(__linux__)
116 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
118 #elif defined(__OpenBSD__)
122 #if defined(__FreeBSD_kernel__)
126 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
127 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
128 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
129 int apm_getinfo(int fd, apm_info_t aip);
130 char *get_apm_adapter(void);
131 char *get_apm_battery_life(void);
132 char *get_apm_battery_time(void);
136 #include "defconfig.h"
137 #include "conf_cookie.h"
141 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
144 #define MAIL_FILE "$MAIL"
145 #define MAX_IF_BLOCK_DEPTH 5
147 //#define SIGNAL_BLOCKING
148 #undef SIGNAL_BLOCKING
150 /* debugging level, used by logging.h */
151 int global_debug_level = 0;
153 /* disable inotify auto reload feature if desired */
154 int disable_auto_reload = 0;
156 /* two strings for internal use */
157 static char *tmpstring1, *tmpstring2;
159 /* variables holding various config settings */
161 int format_human_readable;
168 int top_cpu, top_mem, top_time;
174 static int extra_newline;
175 enum x_initialiser_state x_initialised = NO;
176 static volatile int g_signal_pending;
177 /* Update interval */
178 double update_interval;
179 double update_interval_old;
180 double update_interval_bat;
181 void *global_cpu = NULL;
182 unsigned int max_text_width = 0;
187 /* prototypes for internally used functions */
188 static void signal_handler(int);
189 static void print_version(void) __attribute__((noreturn));
190 static void reload_config(void);
192 static void print_version(void)
194 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
196 printf("\nCompiled in features:\n\n"
197 "System config file: "SYSTEM_CONFIG_FILE"\n"
198 "Package library path: "PACKAGE_LIBDIR"\n\n"
202 " * Xdamage extension\n"
203 # endif /* HAVE_XDAMAGE */
205 " * XDBE (double buffer extension)\n"
206 # endif /* HAVE_XDBE */
212 # endif /* USE_ARGB */
214 "\n Music detection:\n"
217 #endif /* AUDACIOUS */
233 #endif /* HAVE_OPENMP */
240 #ifdef TCP_PORT_MONITOR
242 #endif /* TCP_PORT_MONITOR */
245 #endif /* HAVE_CURL */
250 " * Weather (METAR)\n"
252 " * Weather (XOAP)\n"
257 #endif /* HAVE_IWLIB */
259 " * support for IBM/Lenovo notebooks\n"
269 #endif /* CONFIG_OUTPUT */
285 #ifdef HAVE_LUA_CAIRO
287 #endif /* HAVE_LUA_CAIRO */
288 #ifdef HAVE_LUA_IMLIB2
291 #endif /* HAVE_LUA */
297 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
302 static void X11_create_window(void);
303 static void X11_initialisation(void);
305 struct _x11_stuff_s {
309 XserverRegion region2, part;
310 int event_base, error_base;
316 static int text_start_x, text_start_y; /* text start position in window */
317 static int text_width, text_height;
334 /* display to connect to */
335 static char *disp = NULL;
339 /* struct that has all info to be shared between
340 * instances of the same text object */
341 struct information info;
343 /* path to config file */
344 char *current_config;
346 /* set to 1 if you want all text to be in uppercase */
347 static unsigned int stuff_in_uppercase;
349 /* Run how many times? */
350 static unsigned long total_run_times;
353 static int fork_to_background;
355 /* set to 0 after the first time conky is run, so we don't fork again after the
357 static int first_pass = 1;
359 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
361 /* filenames for output */
362 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
363 char *append_file = NULL; FILE *append_fpointer = NULL;
367 static int show_graph_scale;
368 static int show_graph_range;
370 /* Position on the screen */
371 static int text_alignment;
372 static int gap_x, gap_y;
375 static int draw_borders;
376 static int draw_graph_borders;
377 static int stippled_borders;
379 int get_stippled_borders(void)
381 return stippled_borders;
384 static int draw_shades, draw_outline;
386 long default_fg_color, default_bg_color, default_out_color;
388 /* create own window or draw stuff to root? */
389 static int set_transparent = 0;
392 static int own_window = 0;
393 static int background_colour = 0;
395 /* fixed size/pos is set if wm/user changes them */
396 static int fixed_size = 0, fixed_pos = 0;
399 static int minimum_width, minimum_height;
400 static int maximum_width;
405 static int sensor_device;
408 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
411 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
412 unsigned int max_user_text;
414 /* maximum size of individual text buffers, ie $exec buffer size */
415 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
420 /* no buffers in used memory? */
423 /* pad percentages to decimals? */
424 static int pad_percents = 0;
426 static char *global_text = 0;
428 char *get_global_text(void)
433 long global_text_lines;
435 static int total_updates;
436 static int updatereset;
438 void set_updatereset(int i)
443 int get_updatereset(void)
448 int check_contains(char *f, char *s)
451 FILE *where = open_file(f, 0);
456 while (fgets(buf1, 256, where)) {
457 if (strstr(buf1, s)) {
464 NORM_ERR("Could not open the file '%s'", f);
469 #define SECRIT_MULTILINE_CHAR '\x02'
471 int calc_text_width(const char *s)
473 size_t slen = strlen(s);
476 if ((output_methods & TO_X) == 0) {
486 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
487 (const FcChar8 *) s, slen, &gi);
489 XftTextExtents8(display, fonts[selected_font].xftfont,
490 (const FcChar8 *) s, slen, &gi);
496 return XTextWidth(fonts[selected_font].font, s, slen);
501 /* formatted text to render on screen, generated in generate_text(),
502 * drawn in draw_stuff() */
504 static char *text_buffer;
506 /* quite boring functions */
508 static inline void for_each_line(char *b, int f(char *, int))
511 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));
609 if (short_units || llabs(num) < 1000LL) {
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 */
771 while (obj && p_max_size > 0) {
772 needed = 0; /* reset for top stuff */
774 /* IFBLOCK jumping algorithm
776 * This is easier as it looks like:
777 * - each IF checks it's condition
778 * - on FALSE: call DO_JUMP
779 * - on TRUE: don't care
780 * - each ELSE calls DO_JUMP unconditionally
781 * - each ENDIF is silently being ignored
784 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
785 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
786 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
787 * regularly, "obj" is being updated to point to obj->next, so object parsing
788 * continues right after the corresponding ELSE or ENDIF. This means that if we
789 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
790 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
791 * jumped, and there is nothing to do.
795 if (obj->ifblock_next) \
796 obj = obj->ifblock_next; \
799 #define OBJ(a) break; case OBJ_##a:
803 NORM_ERR("not implemented obj type %d", obj->type);
805 print_read_tcp(obj, p, p_max_size);
809 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
811 #endif /* !__OpenBSD__ */
815 ok = get_freq(p, p_max_size, "%.0f", 1,
823 ok = get_freq(p, p_max_size, "%'.2f", 1000,
826 /* OpenBSD has no such flag (SUSv2) */
827 ok = get_freq(p, p_max_size, "%.2f", 1000,
829 #endif /* __OpenBSD */
832 #if defined(__linux__)
834 print_voltage_mv(obj, p, p_max_size);
837 print_voltage_v(obj, p, p_max_size);
841 OBJ(wireless_essid) {
842 print_wireless_essid(obj, p, p_max_size);
845 print_wireless_mode(obj, p, p_max_size);
847 OBJ(wireless_bitrate) {
848 print_wireless_bitrate(obj, p, p_max_size);
851 print_wireless_ap(obj, p, p_max_size);
853 OBJ(wireless_link_qual) {
854 print_wireless_link_qual(obj, p, p_max_size);
856 OBJ(wireless_link_qual_max) {
857 print_wireless_link_qual_max(obj, p, p_max_size);
859 OBJ(wireless_link_qual_perc) {
860 print_wireless_link_qual_perc(obj, p, p_max_size);
862 OBJ(wireless_link_bar) {
863 print_wireless_link_bar(obj, p, p_max_size);
865 #endif /* HAVE_IWLIB */
867 #endif /* __linux__ */
871 get_acpi_fan(p, p_max_size);
874 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
877 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
880 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
882 OBJ(battery_percent) {
883 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
886 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
889 get_battery_short_status(p, p_max_size, obj->data.s);
891 #endif /* __OpenBSD__ */
894 human_readable(cur->buffers * 1024, p, 255);
897 human_readable(cur->cached * 1024, p, 255);
899 OBJ(cmdline_to_pid) {
900 print_cmdline_to_pid(obj, p, p_max_size);
903 if (cur->cpu_usage) {
904 if (obj->data.i > info.cpu_count) {
905 NORM_ERR("obj->data.i %i info.cpu_count %i",
906 obj->data.i, info.cpu_count);
907 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
909 percent_print(p, p_max_size,
910 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
914 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
916 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
920 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
923 print_loadgraph(obj, p, p_max_size);
927 new_fg(p, obj->data.l);
961 #if defined(__linux__)
963 snprintf(p, p_max_size, "%s",
964 get_disk_protect_queue(obj->data.s));
967 print_i8k_version(obj, p, p_max_size);
970 print_i8k_bios(obj, p, p_max_size);
973 print_i8k_serial(obj, p, p_max_size);
976 print_i8k_cpu_temp(obj, p, p_max_size);
978 OBJ(i8k_left_fan_status) {
979 print_i8k_left_fan_status(obj, p, p_max_size);
981 OBJ(i8k_right_fan_status) {
982 print_i8k_right_fan_status(obj, p, p_max_size);
984 OBJ(i8k_left_fan_rpm) {
985 print_i8k_left_fan_rpm(obj, p, p_max_size);
987 OBJ(i8k_right_fan_rpm) {
988 print_i8k_right_fan_rpm(obj, p, p_max_size);
991 print_i8k_ac_status(obj, p, p_max_size);
993 OBJ(i8k_buttons_status) {
994 print_i8k_buttons_status(obj, p, p_max_size);
998 get_ibm_acpi_fan(obj, p, p_max_size);
1001 print_ibm_temps(obj, p, p_max_size);
1004 get_ibm_acpi_volume(obj, p, p_max_size);
1006 OBJ(ibm_brightness) {
1007 get_ibm_acpi_brightness(obj, p, p_max_size);
1010 /* information from sony_laptop kernel module
1011 * /sys/devices/platform/sony-laptop */
1012 OBJ(sony_fanspeed) {
1013 get_sony_fanspeed(p, p_max_size);
1016 if (!gateway_exists()) {
1021 print_gateway_iface(p, p_max_size);
1024 print_gateway_ip(p, p_max_size);
1027 snprintf(p, p_max_size, "%d", get_laptop_mode());
1030 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1032 #endif /* __linux__ */
1033 #if (defined(__FreeBSD__) || defined(__linux__))
1035 if (!interface_up(obj)) {
1041 OBJ(obsd_sensors_temp) {
1042 print_obsd_sensors_temp(obj, p, p_max_size);
1044 OBJ(obsd_sensors_fan) {
1045 print_obsd_sensors_fan(obj, p, p_max_size);
1047 OBJ(obsd_sensors_volt) {
1048 print_obsd_sensors_volt(obj, p, p_max_size);
1051 get_obsd_vendor(p, p_max_size);
1054 get_obsd_product(p, p_max_size);
1056 #endif /* __OpenBSD__ */
1059 new_font(p, obj->data.s);
1060 need_to_load_fonts = 1;
1064 print_diskio(obj, p, p_max_size);
1067 print_diskio_write(obj, p, p_max_size);
1070 print_diskio_read(obj, p, p_max_size);
1074 print_diskiograph(obj, p, p_max_size);
1076 OBJ(diskiograph_read) {
1077 print_diskiograph_read(obj, p, p_max_size);
1079 OBJ(diskiograph_write) {
1080 print_diskiograph_write(obj, p, p_max_size);
1084 print_downspeed(obj, p, p_max_size);
1087 print_downspeedf(obj, p, p_max_size);
1090 OBJ(downspeedgraph) {
1091 print_downspeedgraph(obj, p, p_max_size);
1095 /* Since we see you, you're if has not jumped.
1096 * Do Ninja jump here: without leaving traces.
1097 * This is to prevent us from stale jumped flags.
1099 obj = obj->ifblock_next;
1103 /* harmless object, just ignore */
1106 print_addr(obj, p, p_max_size);
1108 #if defined(__linux__)
1110 print_addrs(obj, p, p_max_size);
1112 #endif /* __linux__ */
1113 #if defined(IMLIB2) && defined(X11)
1115 /* doesn't actually draw anything, just queues it omp. the
1116 * image will get drawn after the X event loop */
1117 cimlib_add_image(obj->data.s);
1121 evaluate(obj->data.s, p, p_max_size);
1124 print_exec(obj, p, p_max_size);
1127 print_execp(obj, p, p_max_size);
1130 print_execgauge(obj, p, p_max_size);
1133 print_execbar(obj, p, p_max_size);
1137 print_execgraph(obj, p, p_max_size);
1141 print_execibar(obj, p, p_max_size);
1145 print_execigraph(obj, p, p_max_size);
1149 print_execigauge(obj, p, p_max_size);
1152 print_execi(obj, p, p_max_size);
1155 print_execpi(obj, p, p_max_size);
1158 print_texeci(obj, p, p_max_size);
1161 print_imap_unseen(obj, p, p_max_size);
1163 OBJ(imap_messages) {
1164 print_imap_messages(obj, p, p_max_size);
1167 print_pop3_unseen(obj, p, p_max_size);
1170 print_pop3_used(obj, p, p_max_size);
1173 print_fs_bar(obj, 0, p, p_max_size);
1176 print_fs_free(obj, p, p_max_size);
1179 print_fs_perc(obj, 1, p, p_max_size);
1182 print_fs_size(obj, p, p_max_size);
1185 print_fs_type(obj, p, p_max_size);
1188 print_fs_used(obj, p, p_max_size);
1191 print_fs_bar(obj, 1, p, p_max_size);
1194 print_fs_perc(obj, 0, p, p_max_size);
1197 print_loadavg(obj, p, p_max_size);
1200 new_goto(p, obj->data.i);
1207 new_hr(p, obj->data.i);
1211 print_nameserver(obj, p, p_max_size);
1215 print_eve(obj, p, p_max_size);
1220 curl_print(obj, p, p_max_size);
1225 rss_print_info(obj, p, p_max_size);
1230 print_weather(obj, p, p_max_size);
1234 OBJ(weather_forecast) {
1235 print_weather_forecast(obj, p, p_max_size);
1240 print_lua(obj, p, p_max_size);
1243 print_lua_parse(obj, p, p_max_size);
1246 print_lua_bar(obj, p, p_max_size);
1250 print_lua_graph(obj, p, p_max_size);
1254 print_lua_gauge(obj, p, p_max_size);
1256 #endif /* HAVE_LUA */
1262 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1263 snprintf(p, p_max_size, "N/A");
1265 temp_print(p, p_max_size, (double)val,
1266 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1271 new_offset(p, obj->data.i);
1274 new_voffset(p, obj->data.i);
1278 print_sysfs_sensor(obj, p, p_max_size);
1281 print_sysfs_sensor(obj, p, p_max_size);
1284 print_sysfs_sensor(obj, p, p_max_size);
1286 #endif /* __linux__ */
1288 new_alignr(p, obj->data.i);
1291 new_alignc(p, obj->data.i);
1294 char buf[max_user_text];
1295 struct information *tmp_info =
1296 malloc(sizeof(struct information));
1297 memcpy(tmp_info, cur, sizeof(struct information));
1298 generate_text_internal(buf, max_user_text,
1299 *obj->sub, tmp_info);
1301 if (strlen(buf) != 0) {
1307 char expression[max_user_text];
1309 struct information *tmp_info;
1311 tmp_info = malloc(sizeof(struct information));
1312 memcpy(tmp_info, cur, sizeof(struct information));
1313 generate_text_internal(expression, max_user_text,
1314 *obj->sub, tmp_info);
1315 DBGP("parsed arg into '%s'", expression);
1317 val = compare(expression);
1319 NORM_ERR("compare failed for expression '%s'",
1329 spc = strchr(obj->data.s, ' ');
1330 if (!spc && access(obj->data.s, F_OK)) {
1334 if (!check_contains(obj->data.s, spc + 1))
1341 && (!check_mount(obj->data.s))) {
1347 if (!get_process_by_name(obj->data.s)) {
1349 if ((obj->data.s) && system(obj->data.s)) {
1354 #if defined(__linux__)
1356 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1360 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1363 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1368 human_readable(cur->mem * 1024, p, 255);
1371 human_readable(cur->memeasyfree * 1024, p, 255);
1374 human_readable(cur->memfree * 1024, p, 255);
1377 human_readable(cur->memmax * 1024, p, 255);
1381 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1384 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1387 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1391 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1396 print_mixer(obj, p, p_max_size);
1399 print_mixerl(obj, p, p_max_size);
1402 print_mixerr(obj, p, p_max_size);
1405 print_mixer_bar(obj, p, p_max_size);
1408 print_mixerl_bar(obj, p, p_max_size);
1411 print_mixerr_bar(obj, p, p_max_size);
1413 OBJ(if_mixer_mute) {
1414 if (!check_mixer_muted(obj)) {
1419 #define NOT_IN_X "Not running in X"
1421 if(x_initialised != YES) {
1422 strncpy(p, NOT_IN_X, p_max_size);
1424 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1427 OBJ(monitor_number) {
1428 if(x_initialised != YES) {
1429 strncpy(p, NOT_IN_X, p_max_size);
1431 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1435 if(x_initialised != YES) {
1436 strncpy(p, NOT_IN_X, p_max_size);
1438 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1441 OBJ(desktop_number) {
1442 if(x_initialised != YES) {
1443 strncpy(p, NOT_IN_X, p_max_size);
1445 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1449 if(x_initialised != YES) {
1450 strncpy(p, NOT_IN_X, p_max_size);
1451 }else if(cur->x11.desktop.name != NULL) {
1452 strncpy(p, cur->x11.desktop.name, p_max_size);
1458 char buf[max_user_text];
1460 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1462 print_format_time(obj, p, p_max_size);
1466 print_mails(obj, p, p_max_size);
1469 print_new_mails(obj, p, p_max_size);
1472 print_seen_mails(obj, p, p_max_size);
1475 print_unseen_mails(obj, p, p_max_size);
1477 OBJ(flagged_mails) {
1478 print_flagged_mails(obj, p, p_max_size);
1480 OBJ(unflagged_mails) {
1481 print_unflagged_mails(obj, p, p_max_size);
1483 OBJ(forwarded_mails) {
1484 print_forwarded_mails(obj, p, p_max_size);
1486 OBJ(unforwarded_mails) {
1487 print_unforwarded_mails(obj, p, p_max_size);
1489 OBJ(replied_mails) {
1490 print_replied_mails(obj, p, p_max_size);
1492 OBJ(unreplied_mails) {
1493 print_unreplied_mails(obj, p, p_max_size);
1496 print_draft_mails(obj, p, p_max_size);
1498 OBJ(trashed_mails) {
1499 print_trashed_mails(obj, p, p_max_size);
1502 print_mboxscan(obj, p, p_max_size);
1505 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1507 OBJ(nodename_short) {
1509 pos = strstr(cur->uname_s.nodename, ".");
1511 snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1513 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1517 new_outline(p, obj->data.l);
1520 char buf[max_user_text];
1522 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1524 print_pid_chroot(obj, p, p_max_size);
1527 char buf[max_user_text];
1529 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1531 print_pid_cmdline(obj, p, p_max_size);
1534 char buf[max_user_text];
1536 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1538 print_pid_cwd(obj, p, p_max_size);
1541 char buf[max_user_text];
1543 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1545 print_pid_environ(obj, p, p_max_size);
1547 OBJ(pid_environ_list) {
1548 char buf[max_user_text];
1550 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1552 print_pid_environ_list(obj, p, p_max_size);
1555 char buf[max_user_text];
1557 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1559 print_pid_exe(obj, p, p_max_size);
1562 char buf[max_user_text];
1564 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1566 print_pid_nice(obj, p, p_max_size);
1568 OBJ(pid_openfiles) {
1569 char buf[max_user_text];
1571 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1573 print_pid_openfiles(obj, p, p_max_size);
1576 char buf[max_user_text];
1578 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1580 print_pid_parent(obj, p, p_max_size);
1583 char buf[max_user_text];
1585 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1587 print_pid_priority(obj, p, p_max_size);
1590 char buf[max_user_text];
1592 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1594 print_pid_state(obj, p, p_max_size);
1596 OBJ(pid_state_short) {
1597 char buf[max_user_text];
1599 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1601 print_pid_state_short(obj, p, p_max_size);
1604 char buf[max_user_text];
1606 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1608 print_pid_stderr(obj, p, p_max_size);
1611 char buf[max_user_text];
1613 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1615 print_pid_stdin(obj, p, p_max_size);
1618 char buf[max_user_text];
1620 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1622 print_pid_stdout(obj, p, p_max_size);
1625 char buf[max_user_text];
1627 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1629 print_pid_threads(obj, p, p_max_size);
1631 OBJ(pid_thread_list) {
1632 char buf[max_user_text];
1634 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1636 print_pid_thread_list(obj, p, p_max_size);
1638 OBJ(pid_time_kernelmode) {
1639 char buf[max_user_text];
1641 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1643 print_pid_time_kernelmode(obj, p, p_max_size);
1645 OBJ(pid_time_usermode) {
1646 char buf[max_user_text];
1648 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1650 print_pid_time_usermode(obj, p, p_max_size);
1653 char buf[max_user_text];
1655 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1657 print_pid_time(obj, p, p_max_size);
1660 char buf[max_user_text];
1662 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1664 print_pid_uid(obj, p, p_max_size);
1667 char buf[max_user_text];
1669 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1671 print_pid_euid(obj, p, p_max_size);
1674 char buf[max_user_text];
1676 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1678 print_pid_suid(obj, p, p_max_size);
1681 char buf[max_user_text];
1683 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1685 print_pid_fsuid(obj, p, p_max_size);
1688 char buf[max_user_text];
1690 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1692 print_pid_gid(obj, p, p_max_size);
1695 char buf[max_user_text];
1697 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1699 print_pid_egid(obj, p, p_max_size);
1702 char buf[max_user_text];
1704 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1706 print_pid_sgid(obj, p, p_max_size);
1709 char buf[max_user_text];
1711 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1713 print_pid_fsgid(obj, p, p_max_size);
1716 char buf[max_user_text];
1718 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1720 print_pid_read(obj, p, p_max_size);
1723 char buf[max_user_text];
1725 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1727 print_pid_vmpeak(obj, p, p_max_size);
1730 char buf[max_user_text];
1732 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1734 print_pid_vmsize(obj, p, p_max_size);
1737 char buf[max_user_text];
1739 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1741 print_pid_vmlck(obj, p, p_max_size);
1744 char buf[max_user_text];
1746 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1748 print_pid_vmhwm(obj, p, p_max_size);
1751 char buf[max_user_text];
1753 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1755 print_pid_vmrss(obj, p, p_max_size);
1758 char buf[max_user_text];
1760 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1762 print_pid_vmdata(obj, p, p_max_size);
1765 char buf[max_user_text];
1767 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1769 print_pid_vmstk(obj, p, p_max_size);
1772 char buf[max_user_text];
1774 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1776 print_pid_vmexe(obj, p, p_max_size);
1779 char buf[max_user_text];
1781 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1783 print_pid_vmlib(obj, p, p_max_size);
1786 char buf[max_user_text];
1788 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1790 print_pid_vmpte(obj, p, p_max_size);
1793 char buf[max_user_text];
1795 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1797 print_pid_write(obj, p, p_max_size);
1800 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1802 OBJ(running_processes) {
1803 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1805 OBJ(running_threads) {
1806 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1809 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1812 snprintf(p, p_max_size, "%s", obj->data.s);
1816 new_bg(p, obj->data.l);
1819 new_stippled_hr(obj, p);
1823 human_readable(cur->swap * 1024, p, 255);
1826 human_readable(cur->swapfree * 1024, p, 255);
1829 human_readable(cur->swapmax * 1024, p, 255);
1832 if (cur->swapmax == 0) {
1833 strncpy(p, "No swap", p_max_size);
1835 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1839 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1842 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1845 print_time(obj, p, p_max_size);
1848 print_utime(obj, p, p_max_size);
1851 print_tztime(obj, p, p_max_size);
1854 print_totaldown(obj, p, p_max_size);
1857 print_totalup(obj, p, p_max_size);
1860 char buf[max_user_text];
1862 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1864 print_gid_name(obj, p, p_max_size);
1867 char buf[max_user_text];
1869 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1871 print_uid_name(obj, p, p_max_size);
1874 snprintf(p, p_max_size, "%d", total_updates);
1877 if(total_updates % updatereset != obj->data.i - 1) {
1882 print_upspeed(obj, p, p_max_size);
1885 print_upspeedf(obj, p, p_max_size);
1889 print_upspeedgraph(obj, p, p_max_size);
1893 format_seconds_short(p, p_max_size, (int) cur->uptime);
1896 format_seconds(p, p_max_size, (int) cur->uptime);
1900 snprintf(p, p_max_size, "%s", cur->users.names);
1903 snprintf(p, p_max_size, "%s", cur->users.terms);
1906 snprintf(p, p_max_size, "%s", cur->users.times);
1909 update_user_time(obj->data.s);
1910 snprintf(p, p_max_size, "%s", cur->users.ctime);
1913 snprintf(p, p_max_size, "%d", cur->users.number);
1915 #endif /* __linux__ */
1916 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1917 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1921 msg = get_apm_adapter();
1922 snprintf(p, p_max_size, "%s", msg);
1925 OBJ(apm_battery_life) {
1928 msg = get_apm_battery_life();
1929 snprintf(p, p_max_size, "%s", msg);
1932 OBJ(apm_battery_time) {
1935 msg = get_apm_battery_time();
1936 snprintf(p, p_max_size, "%s", msg);
1939 #endif /* __FreeBSD__ __OpenBSD__ */
1943 print_mpd_title(obj, p, p_max_size);
1945 print_mpd_artist(obj, p, p_max_size);
1947 print_mpd_album(obj, p, p_max_size);
1949 print_mpd_random(obj, p, p_max_size);
1951 print_mpd_repeat(obj, p, p_max_size);
1953 print_mpd_track(obj, p, p_max_size);
1955 print_mpd_name(obj, p, p_max_size);
1957 print_mpd_file(obj, p, p_max_size);
1959 print_mpd_vol(obj, p, p_max_size);
1961 print_mpd_bitrate(obj, p, p_max_size);
1963 print_mpd_status(obj, p, p_max_size);
1965 print_mpd_elapsed(obj, p, p_max_size);
1968 print_mpd_length(obj, p, p_max_size);
1971 print_mpd_percent(obj, p, p_max_size);
1974 print_mpd_bar(obj, p, p_max_size);
1977 print_mpd_smart(obj, p, p_max_size);
1979 OBJ(if_mpd_playing) {
1980 if (!mpd_get_info()->is_playing) {
1987 #define MOC_PRINT(t, a) \
1988 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1990 MOC_PRINT(state, "??");
1993 MOC_PRINT(file, "no file");
1996 MOC_PRINT(title, "no title");
1999 MOC_PRINT(artist, "no artist");
2002 MOC_PRINT(song, "no song");
2005 MOC_PRINT(album, "no album");
2007 OBJ(moc_totaltime) {
2008 MOC_PRINT(totaltime, "0:00");
2011 MOC_PRINT(timeleft, "0:00");
2014 MOC_PRINT(curtime, "0:00");
2017 MOC_PRINT(bitrate, "0Kbps");
2020 MOC_PRINT(rate, "0KHz");
2026 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2029 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2032 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2035 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2037 OBJ(xmms2_comment) {
2038 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2041 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2044 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2047 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2049 OBJ(xmms2_tracknr) {
2050 if (cur->xmms2.tracknr != -1) {
2051 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2054 OBJ(xmms2_bitrate) {
2055 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2058 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2061 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2063 OBJ(xmms2_elapsed) {
2064 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2065 (cur->xmms2.elapsed / 1000) % 60);
2067 OBJ(xmms2_duration) {
2068 snprintf(p, p_max_size, "%02d:%02d",
2069 cur->xmms2.duration / 60000,
2070 (cur->xmms2.duration / 1000) % 60);
2072 OBJ(xmms2_percent) {
2073 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2076 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2078 OBJ(xmms2_playlist) {
2079 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2081 OBJ(xmms2_timesplayed) {
2082 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2085 if (strlen(cur->xmms2.title) < 2
2086 && strlen(cur->xmms2.title) < 2) {
2087 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2089 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2093 OBJ(if_xmms2_connected) {
2094 if (cur->xmms2.conn_state != 1) {
2100 OBJ(audacious_status) {
2101 snprintf(p, p_max_size, "%s",
2102 cur->audacious.items[AUDACIOUS_STATUS]);
2104 OBJ(audacious_title) {
2105 snprintf(p, cur->audacious.max_title_len > 0
2106 ? cur->audacious.max_title_len : p_max_size, "%s",
2107 cur->audacious.items[AUDACIOUS_TITLE]);
2109 OBJ(audacious_length) {
2110 snprintf(p, p_max_size, "%s",
2111 cur->audacious.items[AUDACIOUS_LENGTH]);
2113 OBJ(audacious_length_seconds) {
2114 snprintf(p, p_max_size, "%s",
2115 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2117 OBJ(audacious_position) {
2118 snprintf(p, p_max_size, "%s",
2119 cur->audacious.items[AUDACIOUS_POSITION]);
2121 OBJ(audacious_position_seconds) {
2122 snprintf(p, p_max_size, "%s",
2123 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2125 OBJ(audacious_bitrate) {
2126 snprintf(p, p_max_size, "%s",
2127 cur->audacious.items[AUDACIOUS_BITRATE]);
2129 OBJ(audacious_frequency) {
2130 snprintf(p, p_max_size, "%s",
2131 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2133 OBJ(audacious_channels) {
2134 snprintf(p, p_max_size, "%s",
2135 cur->audacious.items[AUDACIOUS_CHANNELS]);
2137 OBJ(audacious_filename) {
2138 snprintf(p, p_max_size, "%s",
2139 cur->audacious.items[AUDACIOUS_FILENAME]);
2141 OBJ(audacious_playlist_length) {
2142 snprintf(p, p_max_size, "%s",
2143 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2145 OBJ(audacious_playlist_position) {
2146 snprintf(p, p_max_size, "%s",
2147 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2149 OBJ(audacious_main_volume) {
2150 snprintf(p, p_max_size, "%s",
2151 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2153 OBJ(audacious_bar) {
2157 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2158 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2159 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2161 #endif /* AUDACIOUS */
2165 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2168 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2171 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2174 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2177 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2180 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2183 /* we have four different types of top (top, top_mem,
2184 * top_time and top_io). To avoid having almost-same code four
2185 * times, we have this special handler. */
2193 print_top(obj, p, p_max_size);
2195 print_tailhead("tail", obj, p, p_max_size);
2198 print_tailhead("head", obj, p, p_max_size);
2201 print_lines(obj, p, p_max_size);
2205 print_words(obj, p, p_max_size);
2207 #ifdef TCP_PORT_MONITOR
2209 tcp_portmon_action(obj, p, p_max_size);
2211 #endif /* TCP_PORT_MONITOR */
2215 do_iconv_start(obj);
2220 #endif /* HAVE_ICONV */
2222 OBJ(entropy_avail) {
2223 print_entropy_avail(obj, p, p_max_size);
2226 print_entropy_perc(obj, p, p_max_size);
2228 OBJ(entropy_poolsize) {
2229 print_entropy_poolsize(obj, p, p_max_size);
2232 print_entropy_bar(obj, p, p_max_size);
2236 print_smapi(obj, p, p_max_size);
2238 OBJ(if_smapi_bat_installed) {
2240 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2241 if(!smapi_bat_installed(idx)) {
2245 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2247 OBJ(smapi_bat_perc) {
2248 print_smapi_bat_perc(obj, p, p_max_size);
2250 OBJ(smapi_bat_temp) {
2251 print_smapi_bat_temp(obj, p, p_max_size);
2253 OBJ(smapi_bat_power) {
2254 print_smapi_bat_power(obj, p, p_max_size);
2256 OBJ(smapi_bat_bar) {
2257 print_smapi_bat_bar(obj, p, p_max_size);
2262 char buf[max_user_text];
2264 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2265 snprintf(p, p_max_size, "%s", buf);
2271 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2272 char buf[max_user_text];
2275 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2276 snprintf(p, p_max_size, "%s", buf);
2277 if(total_updates % 2) {
2278 for(j=0; p[j] != 0; j++) {
2284 char buf[max_user_text];
2286 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2288 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2289 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2290 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2291 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2292 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2293 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2294 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2296 snprintf(p, p_max_size, "%s", buf);
2299 print_scroll(obj, p, p_max_size, cur);
2302 print_combine(obj, p, cur);
2306 print_nvidia_value(obj, p, p_max_size);
2311 /* This is just a meta-object to set host:port */
2314 snprintf(p, p_max_size, "%s",
2315 cur->apcupsd.items[APCUPSD_NAME]);
2317 OBJ(apcupsd_model) {
2318 snprintf(p, p_max_size, "%s",
2319 cur->apcupsd.items[APCUPSD_MODEL]);
2321 OBJ(apcupsd_upsmode) {
2322 snprintf(p, p_max_size, "%s",
2323 cur->apcupsd.items[APCUPSD_UPSMODE]);
2325 OBJ(apcupsd_cable) {
2326 snprintf(p, p_max_size, "%s",
2327 cur->apcupsd.items[APCUPSD_CABLE]);
2329 OBJ(apcupsd_status) {
2330 snprintf(p, p_max_size, "%s",
2331 cur->apcupsd.items[APCUPSD_STATUS]);
2333 OBJ(apcupsd_linev) {
2334 snprintf(p, p_max_size, "%s",
2335 cur->apcupsd.items[APCUPSD_LINEV]);
2338 snprintf(p, p_max_size, "%s",
2339 cur->apcupsd.items[APCUPSD_LOAD]);
2341 OBJ(apcupsd_loadbar) {
2343 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2344 new_bar(obj, p, p_max_size, (int) progress);
2347 OBJ(apcupsd_loadgraph) {
2349 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2350 new_graph(obj, p, p_max_size, (int)progress);
2353 OBJ(apcupsd_loadgauge) {
2355 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2356 new_gauge(obj, p, p_max_size, (int)progress);
2358 OBJ(apcupsd_charge) {
2359 snprintf(p, p_max_size, "%s",
2360 cur->apcupsd.items[APCUPSD_CHARGE]);
2362 OBJ(apcupsd_timeleft) {
2363 snprintf(p, p_max_size, "%s",
2364 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2367 snprintf(p, p_max_size, "%s",
2368 cur->apcupsd.items[APCUPSD_TEMP]);
2370 OBJ(apcupsd_lastxfer) {
2371 snprintf(p, p_max_size, "%s",
2372 cur->apcupsd.items[APCUPSD_LASTXFER]);
2374 #endif /* APCUPSD */
2381 size_t a = strlen(p);
2384 iconv_convert(&a, buff_in, p, p_max_size);
2385 #endif /* HAVE_ICONV */
2386 if (obj->type == OBJ_execp || obj->type == OBJ_execpi || obj->type
2389 || obj->type == OBJ_lua || obj->type == OBJ_lua_parse
2390 #endif /* HAVE_LUA */
2392 substitute_newlines(p, a - 2);
2401 /* load any new fonts we may have had */
2402 if (need_to_load_fonts) {
2408 void evaluate(const char *text, char *p, int p_max_size)
2410 struct information *tmp_info;
2411 struct text_object subroot;
2413 tmp_info = malloc(sizeof(struct information));
2414 memcpy(tmp_info, &info, sizeof(struct information));
2415 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2416 DBGP2("evaluated '%s' to '%s'", text, p);
2418 free_text_objects(&subroot, 1);
2422 double current_update_time, next_update_time, last_update_time;
2424 static void generate_text(void)
2426 struct information *cur = &info;
2428 unsigned int i, j, k;
2434 current_update_time = get_time();
2438 /* add things to the buffer */
2444 generate_text_internal(p, max_user_text, global_root_object, cur);
2445 if(max_text_width > 0) {
2446 for(i = 0, j = 0; p[i] != 0; i++) {
2447 if(p[i] == '\n') j = 0;
2448 else if(j == max_text_width) {
2449 k = i + strlen(p + i) + 1;
2450 if(k < text_buffer_size) {
2457 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2462 if (stuff_in_uppercase) {
2465 tmp_p = text_buffer;
2467 *tmp_p = toupper(*tmp_p);
2472 next_update_time += update_interval;
2473 if (next_update_time < get_time()) {
2474 next_update_time = get_time() + update_interval;
2475 } else if (next_update_time > get_time() + update_interval) {
2476 next_update_time = get_time() + update_interval;
2478 last_update_time = current_update_time;
2482 void set_update_interval(double interval)
2484 update_interval = interval;
2485 update_interval_old = interval;
2488 int get_string_width(const char *s)
2490 return *s ? calc_text_width(s) : 0;
2494 static int get_string_width_special(char *s, int special_index)
2504 if ((output_methods & TO_X) == 0)
2507 p = strndup(s, text_buffer_size);
2511 if (*p == SPECIAL_CHAR) {
2512 /* shift everything over by 1 so that the special char
2513 * doesn't mess up the size calculation */
2514 for (i = 0; i < (long)strlen(p); i++) {
2515 *(p + i) = *(p + i + 1);
2517 if (specials[special_index + idx].type == GRAPH
2518 || specials[special_index + idx].type == GAUGE
2519 || specials[special_index + idx].type == BAR) {
2520 width += specials[special_index + idx].width;
2523 } else if (*p == SECRIT_MULTILINE_CHAR) {
2530 if (strlen(final) > 1) {
2531 width += calc_text_width(final);
2537 static int text_size_updater(char *s, int special_index);
2539 int last_font_height;
2540 static void update_text_area(void)
2544 if ((output_methods & TO_X) == 0)
2546 /* update text size if it isn't fixed */
2551 text_width = minimum_width;
2553 last_font_height = font_height();
2554 for_each_line(text_buffer, text_size_updater);
2556 if (text_height < minimum_height) {
2557 text_height = minimum_height;
2559 if (text_width > maximum_width && maximum_width > 0) {
2560 text_width = maximum_width;
2564 /* get text position on workarea */
2565 switch (text_alignment) {
2566 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2570 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2571 y = workarea[3] - text_height - gap_y;
2574 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2575 y = workarea[3] / 2 - text_height / 2 - gap_y;
2578 switch (text_alignment) {
2579 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2583 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2584 x = workarea[2] - text_width - gap_x;
2587 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2588 x = workarea[2] / 2 - text_width / 2 - gap_x;
2592 if (text_alignment == NONE) { // Let the WM manage the window
2599 #endif /* OWN_WINDOW */
2602 if (own_window && !fixed_pos) {
2605 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2606 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2607 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2608 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2612 /* If window size doesn't match to workarea's size,
2613 * then window probably includes panels (gnome).
2614 * Blah, doesn't work on KDE. */
2615 if (workarea[2] != window.width || workarea[3] != window.height) {
2624 /* update lua window globals */
2625 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2626 #endif /* HAVE_LUA */
2631 static int cur_x, cur_y; /* current x and y for drawing */
2633 //draw_mode also without X11 because we only need to print to stdout with FG
2634 static int draw_mode; /* FG, BG or OUTLINE */
2636 static long current_color;
2638 static int text_size_updater(char *s, int special_index)
2642 int contain_SECRIT_MULTILINE_CHAR = 0;
2645 if ((output_methods & TO_X) == 0)
2647 /* get string widths and skip specials */
2650 if (*p == SPECIAL_CHAR) {
2652 w += get_string_width(s);
2655 if (specials[special_index].type == BAR
2656 || specials[special_index].type == GAUGE
2657 || specials[special_index].type == GRAPH) {
2658 w += specials[special_index].width;
2659 if (specials[special_index].height > last_font_height) {
2660 last_font_height = specials[special_index].height;
2661 last_font_height += font_height();
2663 } else if (specials[special_index].type == OFFSET) {
2664 if (specials[special_index].arg > 0) {
2665 w += specials[special_index].arg;
2667 } else if (specials[special_index].type == VOFFSET) {
2668 last_font_height += specials[special_index].arg;
2669 } else if (specials[special_index].type == GOTO) {
2670 if (specials[special_index].arg > cur_x) {
2671 w = (int) specials[special_index].arg;
2673 } else if (specials[special_index].type == TAB) {
2674 int start = specials[special_index].arg;
2675 int step = specials[special_index].width;
2677 if (!step || step < 0) {
2680 w += step - (cur_x - text_start_x - start) % step;
2681 } else if (specials[special_index].type == FONT) {
2682 selected_font = specials[special_index].font_added;
2683 if (font_height() > last_font_height) {
2684 last_font_height = font_height();
2690 } else if (*p == SECRIT_MULTILINE_CHAR) {
2691 contain_SECRIT_MULTILINE_CHAR = 1;
2693 lw = get_string_width(s);
2694 *p = SECRIT_MULTILINE_CHAR;
2696 w = lw > w ? lw : w;
2697 text_height += last_font_height;
2701 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2702 if (contain_SECRIT_MULTILINE_CHAR) {
2703 lw = get_string_width(s);
2704 w = lw > w ? lw : w;
2706 w += get_string_width(s);
2708 if (w > text_width) {
2711 if (text_width > maximum_width && maximum_width) {
2712 text_width = maximum_width;
2715 text_height += last_font_height;
2716 last_font_height = font_height();
2717 return special_index;
2721 static inline void set_foreground_color(long c)
2724 if (output_methods & TO_X) {
2726 if (have_argb_visual) {
2727 current_color = c | (own_window_argb_value << 24);
2729 #endif /* USE_ARGB */
2733 #endif /* USE_ARGB */
2734 XSetForeground(display, window.gc, current_color);
2738 if (output_methods & TO_NCURSES) {
2739 attron(COLOR_PAIR(c));
2741 #endif /* NCURSES */
2746 static void draw_string(const char *s)
2748 int i, i2, pos, width_of_s;
2751 char *s_with_newlines;
2757 width_of_s = get_string_width(s);
2758 s_with_newlines = strdup(s);
2759 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2760 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2761 s_with_newlines[i] = '\n';
2764 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2765 printf("%s\n", s_with_newlines);
2766 if (extra_newline) fputc('\n', stdout);
2767 fflush(stdout); /* output immediately, don't buffer */
2769 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2770 fprintf(stderr, "%s\n", s_with_newlines);
2771 fflush(stderr); /* output immediately, don't buffer */
2773 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2774 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2776 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2777 fprintf(append_fpointer, "%s\n", s_with_newlines);
2780 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2781 printw("%s", s_with_newlines);
2784 free(s_with_newlines);
2785 memset(tmpstring1, 0, text_buffer_size);
2786 memset(tmpstring2, 0, text_buffer_size);
2787 strncpy(tmpstring1, s, text_buffer_size - 1);
2792 if (output_methods & TO_X) {
2793 max = ((text_width - width_of_s) / get_string_width(" "));
2796 /* This code looks for tabs in the text and coverts them to spaces.
2797 * The trick is getting the correct number of spaces, and not going
2798 * over the window's size without forcing the window larger. */
2799 for (i = 0; i < (int) text_buffer_size; i++) {
2800 if (tmpstring1[i] == '\t') {
2802 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2803 /* guard against overrun */
2804 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2809 /* guard against overrun */
2810 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2815 if (output_methods & TO_X) {
2816 if (text_width == maximum_width) {
2817 /* this means the text is probably pushing the limit,
2818 * so we'll chop it */
2819 while (cur_x + get_string_width(tmpstring2) - text_start_x
2820 > maximum_width && strlen(tmpstring2) > 0) {
2821 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2828 if (output_methods & TO_X) {
2834 c.pixel = current_color;
2835 // query color on custom colormap
2836 XQueryColor(display, window.colourmap, &c);
2839 c2.color.red = c.red;
2840 c2.color.green = c.green;
2841 c2.color.blue = c.blue;
2842 c2.color.alpha = fonts[selected_font].font_alpha;
2844 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2845 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2847 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2848 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2853 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2856 cur_x += width_of_s;
2859 memcpy(tmpstring1, s, text_buffer_size);
2862 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2870 int last_special_needed = -1;
2871 int orig_special_index = special_index;
2874 if (output_methods & TO_X) {
2875 font_h = font_height();
2876 cur_y += font_ascent();
2878 cur_x = text_start_x;
2882 if (*p == SECRIT_MULTILINE_CHAR) {
2883 /* special newline marker for multiline objects */
2888 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2893 /* draw string before special, unless we're dealing multiline
2895 if (last_special_applied > -1) {
2896 special_index = last_special_applied;
2904 switch (specials[special_index].type) {
2906 case HORIZONTAL_LINE:
2908 int h = specials[special_index].height;
2909 int mid = font_ascent() / 2;
2911 w = text_start_x + text_width - cur_x;
2913 XSetLineAttributes(display, window.gc, h, LineSolid,
2914 CapButt, JoinMiter);
2915 XDrawLine(display, window.drawable, window.gc, cur_x,
2916 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2922 int h = specials[special_index].height;
2923 int tmp_s = specials[special_index].arg;
2924 int mid = font_ascent() / 2;
2925 char ss[2] = { tmp_s, tmp_s };
2927 w = text_start_x + text_width - cur_x - 1;
2928 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2929 CapButt, JoinMiter);
2930 XSetDashes(display, window.gc, 0, ss, 2);
2931 XDrawLine(display, window.drawable, window.gc, cur_x,
2932 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2938 int h, bar_usage, by;
2939 if (cur_x - text_start_x > maximum_width
2940 && maximum_width > 0) {
2943 h = specials[special_index].height;
2944 bar_usage = specials[special_index].arg;
2945 by = cur_y - (font_ascent() / 2) - 1;
2950 w = specials[special_index].width;
2952 w = text_start_x + text_width - cur_x - 1;
2958 XSetLineAttributes(display, window.gc, 1, LineSolid,
2959 CapButt, JoinMiter);
2961 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2963 XFillRectangle(display, window.drawable, window.gc, cur_x,
2964 by, w * bar_usage / 255, h);
2972 case GAUGE: /* new GAUGE */
2975 unsigned long last_colour = current_color;
2977 float angle, px, py;
2981 if (cur_x - text_start_x > maximum_width
2982 && maximum_width > 0) {
2986 h = specials[special_index].height;
2987 by = cur_y - (font_ascent() / 2) - 1;
2992 w = specials[special_index].width;
2994 w = text_start_x + text_width - cur_x - 1;
3000 XSetLineAttributes(display, window.gc, 1, LineSolid,
3001 CapButt, JoinMiter);
3003 XDrawArc(display, window.drawable, window.gc,
3004 cur_x, by, w, h * 2, 0, 180*64);
3007 usage = specials[special_index].arg;
3008 angle = (M_PI)*(float)(usage)/255.;
3009 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3010 py = (float)(by+(h))-(float)(h)*sin(angle);
3012 XDrawLine(display, window.drawable, window.gc,
3013 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3021 set_foreground_color(last_colour);
3029 int h, by, i = 0, j = 0;
3031 unsigned long last_colour = current_color;
3032 unsigned long *tmpcolour = 0;
3033 if (cur_x - text_start_x > maximum_width
3034 && maximum_width > 0) {
3037 h = specials[special_index].height;
3038 by = cur_y - (font_ascent() / 2) - 1;
3043 w = specials[special_index].width;
3045 w = text_start_x + text_width - cur_x - 1;
3050 if (draw_graph_borders) {
3051 XSetLineAttributes(display, window.gc, 1, LineSolid,
3052 CapButt, JoinMiter);
3053 XDrawRectangle(display, window.drawable, window.gc,
3056 XSetLineAttributes(display, window.gc, 1, LineSolid,
3057 CapButt, JoinMiter);
3059 if (specials[special_index].last_colour != 0
3060 || specials[special_index].first_colour != 0) {
3061 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3064 for (i = w - 2; i > -1; i--) {
3065 if (specials[special_index].last_colour != 0
3066 || specials[special_index].first_colour != 0) {
3067 if (specials[special_index].tempgrad) {
3070 (int)((float)(w - 2) - specials[special_index].graph[j] *
3071 (w - 2) / (float)specials[special_index].graph_scale)
3075 (int)((float)(w - 2) - specials[special_index].graph[j] *
3076 (w - 2) / (float)specials[special_index].graph_scale)
3079 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3081 (int)((float)(w - 2) - specials[special_index].graph[j] *
3082 (w - 2) / (float)specials[special_index].graph_scale)
3086 #endif /* DEBUG_lol */
3087 set_foreground_color(tmpcolour[
3088 (int)((float)(w - 2) -
3089 specials[special_index].graph[j]
3091 (float)specials[special_index].graph_scale)
3094 set_foreground_color(tmpcolour[colour_idx++]);
3097 /* this is mugfugly, but it works */
3098 XDrawLine(display, window.drawable, window.gc,
3099 cur_x + i + 1, by + h, cur_x + i + 1,
3100 round_to_int((double)by + h - specials[special_index].graph[j] *
3101 (h - 1) / specials[special_index].graph_scale));
3102 if ((w - i) / ((float) (w - 2) /
3103 (specials[special_index].graph_width)) > j
3104 && j < MAX_GRAPH_DEPTH - 3) {
3108 if (tmpcolour) free(tmpcolour);
3113 /* if (draw_mode == BG) {
3114 set_foreground_color(default_bg_color);
3115 } else if (draw_mode == OUTLINE) {
3116 set_foreground_color(default_out_color);
3118 set_foreground_color(default_fg_color);
3120 if (show_graph_range) {
3123 unsigned short int seconds = update_interval * w;
3129 unsigned short int timeunits;
3131 timeunits = seconds / 86400; seconds %= 86400;
3132 if (timeunits > 0) {
3133 if (asprintf(&tmp_day_str, "%dd", timeunits) < 0) {
3137 tmp_day_str = strdup("");
3139 timeunits = seconds / 3600; seconds %= 3600;
3140 if (timeunits > 0) {
3141 if (asprintf(&tmp_hour_str, "%dh", timeunits) < 0) {
3145 tmp_hour_str = strdup("");
3147 timeunits = seconds / 60; seconds %= 60;
3148 if (timeunits > 0) {
3149 if (asprintf(&tmp_min_str, "%dm", timeunits) < 0) {
3153 tmp_min_str = strdup("");
3156 if (asprintf(&tmp_sec_str, "%ds", seconds) < 0) {
3160 tmp_sec_str = strdup("");
3162 if (asprintf(&tmp_str, "%s%s%s%s", tmp_day_str,
3163 tmp_hour_str, tmp_min_str, tmp_sec_str) < 0) {
3166 #define FREE(a) if ((a)) free((a));
3167 FREE(tmp_day_str); FREE(tmp_hour_str); FREE(tmp_min_str); FREE(tmp_sec_str);
3169 tmp_str = strdup("Range not possible"); /* should never happen, but better safe then sorry */
3171 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3172 cur_y += font_h / 2;
3173 draw_string(tmp_str);
3180 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3184 cur_x += font_ascent() / 2;
3185 cur_y += font_h / 2;
3187 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3189 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3190 draw_string(tmp_str);
3196 set_foreground_color(last_colour);
3202 int old = font_ascent();
3204 cur_y -= font_ascent();
3205 selected_font = specials[special_index].font_added;
3207 if (cur_y + font_ascent() < cur_y + old) {
3210 cur_y += font_ascent();
3212 font_h = font_height();
3217 if (draw_mode == FG) {
3218 set_foreground_color(specials[special_index].arg);
3224 if (draw_mode == BG) {
3225 set_foreground_color(specials[special_index].arg);
3230 if (draw_mode == OUTLINE) {
3231 set_foreground_color(specials[special_index].arg);
3236 w += specials[special_index].arg;
3237 last_special_needed = special_index;
3241 cur_y += specials[special_index].arg;
3245 if (specials[special_index].arg >= 0) {
3246 cur_x = (int) specials[special_index].arg;
3248 last_special_needed = special_index;
3253 int start = specials[special_index].arg;
3254 int step = specials[special_index].width;
3256 if (!step || step < 0) {
3259 w = step - (cur_x - text_start_x - start) % step;
3260 last_special_needed = special_index;
3266 /* TODO: add back in "+ window.border_inner_margin" to the end of
3268 int pos_x = text_start_x + text_width -
3269 get_string_width_special(s, special_index);
3271 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3272 "get_string_width(p) %i gap_x %i "
3273 "specials[special_index].arg %i window.border_inner_margin %i "
3274 "window.border_width %i\n", pos_x, text_start_x, text_width,
3275 cur_x, get_string_width_special(s), gap_x,
3276 specials[special_index].arg, window.border_inner_margin,
3277 window.border_width); */
3278 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3279 cur_x = pos_x - specials[special_index].arg;
3281 last_special_needed = special_index;
3287 int pos_x = (text_width) / 2 - get_string_width_special(s,
3288 special_index) / 2 - (cur_x -
3290 /* int pos_x = text_start_x + text_width / 2 -
3291 get_string_width_special(s) / 2; */
3293 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3294 "get_string_width(p) %i gap_x %i "
3295 "specials[special_index].arg %i\n", pos_x, text_start_x,
3296 text_width, cur_x, get_string_width(s), gap_x,
3297 specials[special_index].arg); */
3298 if (pos_x > specials[special_index].arg) {
3299 w = pos_x - specials[special_index].arg;
3301 last_special_needed = special_index;
3311 if (special_index != last_special_applied) {
3314 special_index = orig_special_index;
3315 last_special_applied = -1;
3326 if (output_methods & TO_NCURSES) {
3329 #endif /* NCURSES */
3331 if (output_methods & TO_X)
3332 cur_y += font_descent();
3334 if (recurse && *recurse) {
3335 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3336 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3338 return special_index;
3341 static int draw_line(char *s, int special_index)
3344 if (output_methods & TO_X) {
3345 return draw_each_line_inner(s, special_index, -1);
3349 if (output_methods & TO_NCURSES) {
3350 return draw_each_line_inner(s, special_index, -1);
3352 #endif /* NCURSES */
3354 UNUSED(special_index);
3358 static void draw_text(void)
3362 llua_draw_pre_hook();
3363 #endif /* HAVE_LUA */
3364 if (output_methods & TO_X) {
3365 cur_y = text_start_y;
3368 if (draw_borders && window.border_width > 0) {
3369 if (stippled_borders) {
3370 char ss[2] = { stippled_borders, stippled_borders };
3371 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3372 CapButt, JoinMiter);
3373 XSetDashes(display, window.gc, 0, ss, 2);
3375 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3376 CapButt, JoinMiter);
3379 XDrawRectangle(display, window.drawable, window.gc,
3380 text_start_x - window.border_inner_margin - window.border_width,
3381 text_start_y - window.border_inner_margin - window.border_width,
3382 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3383 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3391 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3392 attron(COLOR_PAIR(COLOR_WHITE));
3393 #endif /* NCURSES */
3394 for_each_line(text_buffer, draw_line);
3395 #if defined(HAVE_LUA) && defined(X11)
3396 llua_draw_post_hook();
3397 #endif /* HAVE_LUA */
3400 static void draw_stuff(void)
3403 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3405 if (overwrite_file) {
3406 overwrite_fpointer = fopen(overwrite_file, "w");
3407 if(!overwrite_fpointer)
3408 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3411 append_fpointer = fopen(append_file, "a");
3412 if(!append_fpointer)
3413 NORM_ERR("Can't append '%s' anymore", append_file);
3416 if (output_methods & TO_X) {
3418 if (draw_shades && !draw_outline) {
3421 set_foreground_color(default_bg_color);
3432 for (i = -1; i < 2; i++) {
3433 for (j = -1; j < 2; j++) {
3434 if (i == 0 && j == 0) {
3439 set_foreground_color(default_out_color);
3440 draw_mode = OUTLINE;
3448 set_foreground_color(default_fg_color);
3453 #if defined(X11) && defined(HAVE_XDBE)
3454 if (output_methods & TO_X) {
3455 xdbe_swap_buffers();
3457 #endif /* X11 && HAVE_XDBE */
3458 if(overwrite_fpointer) {
3459 fclose(overwrite_fpointer);
3460 overwrite_fpointer = 0;
3462 if(append_fpointer) {
3463 fclose(append_fpointer);
3464 append_fpointer = 0;
3469 static void clear_text(int exposures)
3473 /* The swap action is XdbeBackground, which clears */
3477 if (display && window.window) { // make sure these are !null
3478 /* there is some extra space for borders and outlines */
3479 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3480 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3481 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3482 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3487 static int need_to_update;
3489 /* update_text() generates new text and clears old text area */
3490 static void update_text(void)
3497 if (output_methods & TO_X)
3502 llua_update_info(&info, update_interval);
3503 #endif /* HAVE_LUA */
3506 #ifdef HAVE_SYS_INOTIFY_H
3510 static void main_loop(void)
3513 #ifdef SIGNAL_BLOCKING
3514 sigset_t newmask, oldmask;
3517 #ifdef HAVE_SYS_INOTIFY_H
3518 int inotify_config_wd = -1;
3519 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3520 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3521 char inotify_buff[INOTIFY_BUF_LEN];
3522 #endif /* HAVE_SYS_INOTIFY_H */
3525 #ifdef SIGNAL_BLOCKING
3526 sigemptyset(&newmask);
3527 sigaddset(&newmask, SIGINT);
3528 sigaddset(&newmask, SIGTERM);
3529 sigaddset(&newmask, SIGUSR1);
3532 last_update_time = 0.0;
3533 next_update_time = get_time();
3535 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3536 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3537 char buf[max_user_text];
3539 get_battery_short_status(buf, max_user_text, "BAT0");
3541 update_interval = update_interval_bat;
3543 update_interval = update_interval_old;
3548 #ifdef SIGNAL_BLOCKING
3549 /* block signals. we will inspect for pending signals later */
3550 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3551 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3556 if (output_methods & TO_X) {
3559 /* wait for X event or timeout */
3561 if (!XPending(display)) {
3565 t = next_update_time - get_time();
3569 } else if (t > update_interval) {
3570 t = update_interval;
3573 tv.tv_sec = (long) t;
3574 tv.tv_usec = (long) (t * 1000000) % 1000000;
3576 FD_SET(ConnectionNumber(display), &fdsr);
3578 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3580 if (errno != EINTR) {
3581 NORM_ERR("can't select(): %s", strerror(errno));
3591 if (need_to_update) {
3593 int wx = window.x, wy = window.y;
3603 /* resize window if it isn't right size */
3605 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3606 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3607 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3608 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3609 draw_stuff(); /* redraw everything in our newly sized window */
3610 XResizeWindow(display, window.window, window.width,
3611 window.height); /* resize window */
3612 set_transparent_background(window.window, own_window_argb_value);
3615 xdbe_swap_buffers();
3620 /* update lua window globals */
3621 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3622 #endif /* HAVE_LUA */
3625 /* move window if it isn't in right position */
3626 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3627 XMoveWindow(display, window.window, window.x, window.y);
3632 if (changed && window.type == TYPE_PANEL) {
3635 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3638 switch (text_alignment) {
3665 set_struts(sidenum);
3676 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3677 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3678 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3679 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3680 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3685 /* handle X events */
3686 while (XPending(display)) {
3689 XNextEvent(display, &ev);
3696 r.width = ev.xexpose.width;
3697 r.height = ev.xexpose.height;
3698 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3702 case PropertyNotify:
3704 if ( ev.xproperty.state == PropertyNewValue ) {
3705 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3711 case ReparentNotify:
3712 /* make background transparent */
3714 set_transparent_background(window.window, own_window_argb_value);
3718 case ConfigureNotify:
3720 /* if window size isn't what expected, set fixed size */
3721 if (ev.xconfigure.width != window.width
3722 || ev.xconfigure.height != window.height) {
3723 if (window.width != 0 && window.height != 0) {
3727 /* clear old stuff before screwing up
3732 XWindowAttributes attrs;
3733 if (XGetWindowAttributes(display,
3734 window.window, &attrs)) {
3735 window.width = attrs.width;
3736 window.height = attrs.height;
3740 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3741 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3742 if (text_width > maximum_width
3743 && maximum_width > 0) {
3744 text_width = maximum_width;
3748 /* if position isn't what expected, set fixed pos
3749 * total_updates avoids setting fixed_pos when window
3750 * is set to weird locations when started */
3751 /* // this is broken
3752 if (total_updates >= 2 && !fixed_pos
3753 && (window.x != ev.xconfigure.x
3754 || window.y != ev.xconfigure.y)
3755 && (ev.xconfigure.x != 0
3756 || ev.xconfigure.y != 0)) {
3764 /* if an ordinary window with decorations */
3765 if ((window.type == TYPE_NORMAL &&
3766 (!TEST_HINT(window.hints,
3767 HINT_UNDECORATED))) ||
3768 window.type == TYPE_DESKTOP) {
3769 /* allow conky to hold input focus. */
3772 /* forward the click to the desktop window */
3773 XUngrabPointer(display, ev.xbutton.time);
3774 ev.xbutton.window = window.desktop;
3775 ev.xbutton.x = ev.xbutton.x_root;
3776 ev.xbutton.y = ev.xbutton.y_root;
3777 XSendEvent(display, ev.xbutton.window, False,
3778 ButtonPressMask, &ev);
3779 XSetInputFocus(display, ev.xbutton.window,
3780 RevertToParent, ev.xbutton.time);
3787 /* if an ordinary window with decorations */
3788 if ((window.type == TYPE_NORMAL)
3789 && (!TEST_HINT(window.hints,
3790 HINT_UNDECORATED))) {
3791 /* allow conky to hold input focus. */
3794 /* forward the release to the desktop window */
3795 ev.xbutton.window = window.desktop;
3796 ev.xbutton.x = ev.xbutton.x_root;
3797 ev.xbutton.y = ev.xbutton.y_root;
3798 XSendEvent(display, ev.xbutton.window, False,
3799 ButtonReleaseMask, &ev);
3808 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3809 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3811 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3812 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3814 #endif /* HAVE_XDAMAGE */
3820 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3821 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3822 #endif /* HAVE_XDAMAGE */
3824 /* XDBE doesn't seem to provide a way to clear the back buffer
3825 * without interfering with the front buffer, other than passing
3826 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3827 * using XDBE, we need to redraw the text even if it wasn't part of
3828 * the exposed area. OTOH, if we're not going to call draw_stuff at
3829 * all, then no swap happens and we can safely do nothing. */
3831 if (!XEmptyRegion(x11_stuff.region)) {
3836 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3837 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3838 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3839 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3840 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3843 XSetRegion(display, window.gc, x11_stuff.region);
3846 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3850 XDestroyRegion(x11_stuff.region);
3851 x11_stuff.region = XCreateRegion();
3855 t = (next_update_time - get_time()) * 1000000;
3856 if(t > 0) usleep((useconds_t)t);
3860 if(output_methods & TO_NCURSES) {
3869 #ifdef SIGNAL_BLOCKING
3870 /* unblock signals of interest and let handler fly */
3871 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3872 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3876 switch (g_signal_pending) {
3879 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3884 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3887 if (output_methods & TO_X) {
3888 XDestroyRegion(x11_stuff.region);
3889 x11_stuff.region = NULL;
3891 XDamageDestroy(display, x11_stuff.damage);
3892 XFixesDestroyRegion(display, x11_stuff.region2);
3893 XFixesDestroyRegion(display, x11_stuff.part);
3894 #endif /* HAVE_XDAMAGE */
3900 if(overwrite_file) {
3901 free(overwrite_file);
3910 /* Reaching here means someone set a signal
3911 * (SIGXXXX, signal_handler), but didn't write any code
3913 * If you don't want to handle a signal, don't set a handler on
3914 * it in the first place. */
3915 if (g_signal_pending) {
3916 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3920 #ifdef HAVE_SYS_INOTIFY_H
3921 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3922 inotify_config_wd = inotify_add_watch(inotify_fd,
3926 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3927 int len = 0, idx = 0;
3929 struct timeval time_to_wait;
3931 FD_ZERO(&descriptors);
3932 FD_SET(inotify_fd, &descriptors);
3934 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3936 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3937 if (FD_ISSET(inotify_fd, &descriptors)) {
3938 /* process inotify events */
3939 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3940 while (len > 0 && idx < len) {
3941 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3942 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3943 /* current_config should be reloaded */
3944 NORM_ERR("'%s' modified, reloading...", current_config);
3946 if (ev->mask & IN_IGNORED) {
3947 /* for some reason we get IN_IGNORED here
3948 * sometimes, so we need to re-add the watch */
3949 inotify_config_wd = inotify_add_watch(inotify_fd,
3957 llua_inotify_query(ev->wd, ev->mask);
3959 #endif /* HAVE_LUA */
3960 idx += INOTIFY_EVENT_SIZE + ev->len;
3963 } else if (disable_auto_reload && inotify_fd != -1) {
3964 inotify_rm_watch(inotify_fd, inotify_config_wd);
3966 inotify_fd = inotify_config_wd = 0;
3968 #endif /* HAVE_SYS_INOTIFY_H */
3971 llua_update_info(&info, update_interval);
3972 #endif /* HAVE_LUA */
3973 g_signal_pending = 0;
3975 clean_up(current_mail_spool, NULL);
3977 #ifdef HAVE_SYS_INOTIFY_H
3978 if (inotify_fd != -1) {
3979 inotify_rm_watch(inotify_fd, inotify_config_wd);
3981 inotify_fd = inotify_config_wd = 0;
3983 #endif /* HAVE_SYS_INOTIFY_H */
3987 static void load_config_file_x11(const char *);
3989 void initialisation(int argc, char** argv);
3991 /* reload the config file */
3992 static void reload_config(void)
3994 char *current_config_copy = strdup(current_config);
3995 clean_up(NULL, NULL);
3996 sleep(1); /* slight pause */
3997 current_config = current_config_copy;
3998 initialisation(argc_copy, argv_copy);
4002 void clean_up_x11(void)
4004 if(window_created == 1) {
4005 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4006 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4007 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4008 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4013 if(x11_stuff.region) {
4014 XDestroyRegion(x11_stuff.region);
4015 x11_stuff.region = NULL;
4018 XCloseDisplay(display);
4021 if(info.x11.desktop.all_names) {
4022 free(info.x11.desktop.all_names);
4023 info.x11.desktop.all_names = NULL;
4025 if (info.x11.desktop.name) {
4026 free(info.x11.desktop.name);
4027 info.x11.desktop.name = NULL;
4033 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4037 if(output_methods & TO_NCURSES) {
4041 conftree_empty(currentconffile);
4042 currentconffile = NULL;
4049 timed_thread_destroy_registered_threads();
4051 if (info.cpu_usage) {
4052 free(info.cpu_usage);
4053 info.cpu_usage = NULL;
4056 if (x_initialised == YES) {
4059 free(fonts); //in set_default_configurations a font is set but not loaded
4064 set_nvidia_display(NULL);
4070 free_text_objects(&global_root_object, 0);
4089 free(current_config);
4092 #ifdef TCP_PORT_MONITOR
4093 tcp_portmon_clear();
4102 weather_free_info();
4105 llua_shutdown_hook();
4107 #endif /* HAVE_LUA */
4109 if (output_methods & TO_X)
4117 for (i = 0; i < special_count; i++) {
4118 if (specials[i].type == GRAPH) {
4119 free(specials[i].graph);
4127 clear_diskio_stats();
4128 if(global_cpu != NULL) {
4134 void clean_up(void *memtofree1, void* memtofree2)
4136 free_update_callbacks();
4137 clean_up_without_threads(memtofree1, memtofree2);
4140 static int string_to_bool(const char *s)
4143 // Assumes an option without a true/false means true
4145 } else if (strcasecmp(s, "yes") == EQUAL) {
4147 } else if (strcasecmp(s, "true") == EQUAL) {
4149 } else if (strcasecmp(s, "1") == EQUAL) {
4156 static enum alignment string_to_alignment(const char *s)
4158 if (strcasecmp(s, "top_left") == EQUAL) {
4160 } else if (strcasecmp(s, "top_right") == EQUAL) {
4162 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4164 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4166 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4167 return BOTTOM_RIGHT;
4168 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4169 return BOTTOM_MIDDLE;
4170 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4172 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4173 return MIDDLE_RIGHT;
4174 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4175 return MIDDLE_MIDDLE;
4176 } else if (strcasecmp(s, "tl") == EQUAL) {
4178 } else if (strcasecmp(s, "tr") == EQUAL) {
4180 } else if (strcasecmp(s, "tm") == EQUAL) {
4182 } else if (strcasecmp(s, "bl") == EQUAL) {
4184 } else if (strcasecmp(s, "br") == EQUAL) {
4185 return BOTTOM_RIGHT;
4186 } else if (strcasecmp(s, "bm") == EQUAL) {
4187 return BOTTOM_MIDDLE;
4188 } else if (strcasecmp(s, "ml") == EQUAL) {
4190 } else if (strcasecmp(s, "mr") == EQUAL) {
4191 return MIDDLE_RIGHT;
4192 } else if (strcasecmp(s, "mm") == EQUAL) {
4193 return MIDDLE_MIDDLE;
4194 } else if (strcasecmp(s, "none") == EQUAL) {
4197 return ALIGNMENT_ERROR;
4202 static void set_default_configurations_for_x(void)
4204 default_fg_color = WhitePixel(display, screen);
4205 default_bg_color = BlackPixel(display, screen);
4206 default_out_color = BlackPixel(display, screen);
4207 color0 = default_fg_color;
4208 color1 = default_fg_color;
4209 color2 = default_fg_color;
4210 color3 = default_fg_color;
4211 color4 = default_fg_color;
4212 color5 = default_fg_color;
4213 color6 = default_fg_color;
4214 color7 = default_fg_color;
4215 color8 = default_fg_color;
4216 color9 = default_fg_color;
4217 current_text_color = default_fg_color;
4221 static void set_default_configurations(void)
4228 fork_to_background = 0;
4229 total_run_times = 0;
4230 info.cpu_avg_samples = 2;
4231 info.net_avg_samples = 2;
4232 info.diskio_avg_samples = 2;
4237 format_human_readable = 1;
4245 mpd_env_host = getenv("MPD_HOST");
4246 mpd_env_port = getenv("MPD_PORT");
4248 if (!mpd_env_host || !strlen(mpd_env_host)) {
4249 mpd_set_host("localhost");
4251 /* MPD_HOST environment variable is set */
4252 char *mpd_hostpart = strchr(mpd_env_host, '@');
4253 if (!mpd_hostpart) {
4254 mpd_set_host(mpd_env_host);
4256 /* MPD_HOST contains a password */
4257 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4258 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4260 if (!strlen(mpd_hostpart + 1)) {
4261 mpd_set_host("localhost");
4263 mpd_set_host(mpd_hostpart + 1);
4266 mpd_set_password(mpd_password, 1);
4271 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4272 /* failed to set port from environment variable */
4273 mpd_set_port("6600");
4277 info.xmms2.artist = NULL;
4278 info.xmms2.album = NULL;
4279 info.xmms2.title = NULL;
4280 info.xmms2.genre = NULL;
4281 info.xmms2.comment = NULL;
4282 info.xmms2.url = NULL;
4283 info.xmms2.status = NULL;
4284 info.xmms2.playlist = NULL;
4286 use_spacer = NO_SPACER;
4288 output_methods = TO_X;
4290 output_methods = TO_STDOUT;
4296 show_graph_scale = 0;
4297 show_graph_range = 0;
4300 draw_graph_borders = 1;
4302 set_first_font("6x10");
4310 window.type = TYPE_NORMAL;
4312 strcpy(window.class_name, PACKAGE_NAME);
4313 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4315 use_argb_visual = 0;
4316 own_window_argb_value = 255;
4319 stippled_borders = 0;
4320 window.border_inner_margin = 3;
4321 window.border_outer_margin = 1;
4322 window.border_width = 1;
4323 text_alignment = BOTTOM_LEFT;
4324 info.x11.monitor.number = 1;
4325 info.x11.monitor.current = 0;
4326 info.x11.desktop.current = 1;
4327 info.x11.desktop.number = 1;
4328 info.x11.desktop.nitems = 0;
4329 info.x11.desktop.all_names = NULL;
4330 info.x11.desktop.name = NULL;
4335 free(current_mail_spool);
4339 variable_substitute(MAIL_FILE, buf, 256);
4340 if (buf[0] != '\0') {
4341 current_mail_spool = strndup(buf, text_buffer_size);
4346 set_update_interval(3);
4347 update_interval_bat = NOBATTERY;
4348 info.music_player_interval = 1.0;
4349 stuff_in_uppercase = 0;
4350 info.users.number = 1;
4352 set_times_in_seconds(0);
4354 #ifdef TCP_PORT_MONITOR
4355 /* set default connection limit */
4356 tcp_portmon_set_max_connections(0);
4360 /* returns 1 if you can overwrite or create the file at 'path' */
4361 static _Bool overwrite_works(const char *path)
4365 if (!(filepointer = fopen(path, "w")))
4367 fclose(filepointer);
4371 /* returns 1 if you can append or create the file at 'path' */
4372 static _Bool append_works(const char *path)
4376 if (!(filepointer = fopen(path, "a")))
4378 fclose(filepointer);
4384 /* WARNING, this type not in Xlib spec */
4385 int x11_error_handler(Display *d, XErrorEvent *err)
4386 __attribute__((noreturn));
4387 int x11_error_handler(Display *d, XErrorEvent *err)
4389 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",
4391 (long unsigned)err->display,
4392 (long)err->resourceid,
4402 int x11_ioerror_handler(Display *d)
4403 __attribute__((noreturn));
4404 int x11_ioerror_handler(Display *d)
4406 NORM_ERR("X Error: Display %lx\n",
4413 static void X11_initialisation(void)
4415 if (x_initialised == YES) return;
4416 output_methods |= TO_X;
4418 set_default_configurations_for_x();
4419 x_initialised = YES;
4422 /* WARNING, this type not in Xlib spec */
4423 XSetErrorHandler(&x11_error_handler);
4424 XSetIOErrorHandler(&x11_ioerror_handler);
4428 static char **xargv = 0;
4429 static int xargc = 0;
4431 static void X11_create_window(void)
4433 if (output_methods & TO_X) {
4435 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4436 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4438 #else /* OWN_WINDOW */
4439 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4440 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4442 #endif /* OWN_WINDOW */
4446 update_text_area(); /* to position text/window on screen */
4449 if (own_window && !fixed_pos) {
4450 XMoveWindow(display, window.window, window.x, window.y);
4453 set_transparent_background(window.window, own_window_argb_value);
4461 x11_stuff.region = XCreateRegion();
4463 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4464 NORM_ERR("Xdamage extension unavailable");
4466 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4467 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4468 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4469 #endif /* HAVE_XDAMAGE */
4472 update_text_area(); /* to get initial size of the window */
4475 /* setup lua window globals */
4476 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4477 #endif /* HAVE_LUA */
4481 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4482 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4483 #define CONF2(a) if (strcasecmp(name, a) == 0)
4484 #define CONF(a) else CONF2(a)
4485 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4486 || strcasecmp(name, b) == 0)
4487 #define CONF_CONTINUE 1
4488 #define CONF_BREAK 2
4489 #define CONF_BUFF_SIZE 512
4491 static FILE *open_config_file(const char *f)
4493 #ifdef CONFIG_OUTPUT
4494 if (!strcmp(f, "==builtin==")) {
4495 return conf_cookie_open();
4497 #endif /* CONFIG_OUTPUT */
4498 return fopen(f, "r");
4501 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4505 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4508 remove_comments(buf);
4513 while (*p && isspace((int) *p)) {
4517 return CONF_CONTINUE; /* empty line */
4524 while (*p2 && !isspace((int) *p2)) {
4528 *p2 = '\0'; /* break at name's end */
4535 while (*p && isspace((int) *p)) {
4541 p2 = *value + strlen(*value);
4542 while (isspace((int) *(p2 - 1))) {
4552 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4554 if (windowtype == TYPE_DOCK) {
4555 NORM_ERR("alignment is disabled when own_window_type is dock");
4557 #endif /*OWN_WINDOW */
4559 int a = string_to_alignment(value);
4562 if (setbyconffile) {
4564 } else NORM_ERR("'%s' is not a alignment setting", value);
4566 *ltext_alignment = a;
4568 } else if (setbyconffile) {
4574 char load_config_file(const char *f)
4579 fp = open_config_file(f);
4583 DBGP("reading contents from config file '%s'", f);
4586 char buff[CONF_BUFF_SIZE], *name, *value;
4587 int ret = do_config_step(&line, fp, buff, &name, &value);
4588 if (ret == CONF_BREAK) {
4590 } else if (ret == CONF_CONTINUE) {
4596 if (string_to_bool(value)) {
4597 output_methods &= TO_X;
4600 output_methods &= ~TO_X;
4601 x_initialised = NEVER;
4605 if (!value || x_initialised == YES) {
4610 disp = strdup(value);
4614 CONF("nvidia_display") {
4616 set_nvidia_display(value);
4620 setalignment(&text_alignment, window.type, value, f, line, 1);
4622 CONF("background") {
4623 fork_to_background = string_to_bool(value);
4626 CONF2("background") {
4627 fork_to_background = string_to_bool(value);
4631 CONF("show_graph_scale") {
4632 show_graph_scale = string_to_bool(value);
4634 CONF("show_graph_range") {
4635 show_graph_range = string_to_bool(value);
4637 CONF("border_inner_margin") {
4639 window.border_inner_margin = strtol(value, 0, 0);
4640 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4645 CONF("border_outer_margin") {
4647 window.border_outer_margin = strtol(value, 0, 0);
4648 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4653 CONF("border_width") {
4655 window.border_width = strtol(value, 0, 0);
4656 if (window.border_width < 1) window.border_width = 1;
4662 #define TEMPLATE_CONF(n) \
4663 CONF("template"#n) { \
4664 if (set_template(n, value)) \
4679 parse_global_imap_mail_args(value);
4686 parse_global_pop3_mail_args(value);
4691 CONF("default_bar_size") {
4694 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4701 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4705 CONF("default_graph_size") {
4708 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4715 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4718 CONF("default_gauge_size") {
4721 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4728 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4735 mpd_set_host(value);
4741 if (value && mpd_set_port(value)) {
4745 CONF("mpd_password") {
4747 mpd_set_password(value, 0);
4753 CONF("music_player_interval") {
4755 info.music_player_interval = strtod(value, 0);
4761 CONF("sensor_device") {
4763 sensor_device = strtol(value, 0, 0);
4769 CONF("cpu_avg_samples") {
4771 cpu_avg_samples = strtol(value, 0, 0);
4772 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4775 info.cpu_avg_samples = cpu_avg_samples;
4781 CONF("net_avg_samples") {
4783 net_avg_samples = strtol(value, 0, 0);
4784 if (net_avg_samples < 1 || net_avg_samples > 14) {
4787 info.net_avg_samples = net_avg_samples;
4793 CONF("diskio_avg_samples") {
4795 diskio_avg_samples = strtol(value, 0, 0);
4796 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4799 info.diskio_avg_samples = diskio_avg_samples;
4807 CONF("double_buffer") {
4808 use_xdbe = string_to_bool(value);
4812 CONF("override_utf8_locale") {
4813 utf8_mode = string_to_bool(value);
4815 CONF("draw_borders") {
4816 draw_borders = string_to_bool(value);
4818 CONF("draw_graph_borders") {
4819 draw_graph_borders = string_to_bool(value);
4821 CONF("draw_shades") {
4822 draw_shades = string_to_bool(value);
4824 CONF("draw_outline") {
4825 draw_outline = string_to_bool(value);
4828 CONF("times_in_seconds") {
4829 set_times_in_seconds(string_to_bool(value));
4831 CONF("max_text_width") {
4832 max_text_width = atoi(value);
4834 CONF("out_to_console") {
4835 if(string_to_bool(value)) {
4836 output_methods |= TO_STDOUT;
4838 output_methods &= ~TO_STDOUT;
4841 CONF("extra_newline") {
4842 extra_newline = string_to_bool(value);
4844 CONF("disable_auto_reload") {
4845 disable_auto_reload = string_to_bool(value);
4847 CONF("out_to_stderr") {
4848 if(string_to_bool(value))
4849 output_methods |= TO_STDERR;
4852 CONF("out_to_ncurses") {
4853 if(string_to_bool(value)) {
4856 output_methods |= TO_NCURSES;
4860 CONF("overwrite_file") {
4861 if(overwrite_file) {
4862 free(overwrite_file);
4865 if(overwrite_works(value)) {
4866 overwrite_file = strdup(value);
4867 output_methods |= OVERWRITE_FILE;
4869 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4871 CONF("append_file") {
4876 if(append_works(value)) {
4877 append_file = strdup(value);
4878 output_methods |= APPEND_FILE;
4880 NORM_ERR("append_file won't be able to create/append '%s'", value);
4882 CONF("use_spacer") {
4884 if (strcasecmp(value, "left") == EQUAL) {
4885 use_spacer = LEFT_SPACER;
4886 } else if (strcasecmp(value, "right") == EQUAL) {
4887 use_spacer = RIGHT_SPACER;
4888 } else if (strcasecmp(value, "none") == EQUAL) {
4889 use_spacer = NO_SPACER;
4891 use_spacer = string_to_bool(value);
4892 NORM_ERR("use_spacer should have an argument of left, right, or"
4893 " none. '%s' seems to be some form of '%s', so"
4894 " defaulting to %s.", value,
4895 use_spacer ? "true" : "false",
4896 use_spacer ? "right" : "none");
4898 use_spacer = RIGHT_SPACER;
4900 use_spacer = NO_SPACER;
4904 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4905 use_spacer = RIGHT_SPACER;
4911 use_xft = string_to_bool(value);
4915 set_first_font(value);
4919 if (value && font_count >= 0) {
4920 fonts[0].font_alpha = atof(value) * 65535.0;
4927 if (string_to_bool(value)) {
4928 NORM_ERR("Xft not enabled at compile time");
4932 /* xftfont silently ignored when no Xft */
4935 /* xftalpha is silently ignored when no Xft */
4940 set_first_font(value);
4948 gap_x = atoi(value);
4955 gap_y = atoi(value);
4961 CONF("mail_spool") {
4965 variable_substitute(value, buffer, 256);
4967 if (buffer[0] != '\0') {
4968 if (current_mail_spool) {
4969 free(current_mail_spool);
4971 current_mail_spool = strndup(buffer, text_buffer_size);
4978 CONF("minimum_size") {
4980 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4982 if (sscanf(value, "%d", &minimum_width) != 1) {
4990 CONF("maximum_width") {
4992 if (sscanf(value, "%d", &maximum_width) != 1) {
5000 CONF("no_buffers") {
5001 no_buffers = string_to_bool(value);
5003 CONF("top_name_width") {
5004 if (set_top_name_width(value))
5007 CONF("top_cpu_separate") {
5008 cpu_separate = string_to_bool(value);
5010 CONF("short_units") {
5011 short_units = string_to_bool(value);
5013 CONF("format_human_readable") {
5014 format_human_readable = string_to_bool(value);
5017 CONF("hddtemp_host") {
5018 set_hddtemp_host(value);
5020 CONF("hddtemp_port") {
5021 set_hddtemp_port(value);
5023 #endif /* HDDTEMP */
5024 CONF("pad_percents") {
5025 pad_percents = atoi(value);
5029 CONF("own_window") {
5031 own_window = string_to_bool(value);
5034 CONF("own_window_class") {
5036 memset(window.class_name, 0, sizeof(window.class_name));
5037 strncpy(window.class_name, value,
5038 sizeof(window.class_name) - 1);
5041 CONF("own_window_title") {
5043 memset(window.title, 0, sizeof(window.title));
5044 strncpy(window.title, value, sizeof(window.title) - 1);
5047 CONF("own_window_transparent") {
5049 set_transparent = string_to_bool(value);
5052 CONF("own_window_hints") {
5054 char *p_hint, *p_save;
5055 char delim[] = ", ";
5057 /* tokenize the value into individual hints */
5058 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5060 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5061 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5062 SET_HINT(window.hints, HINT_UNDECORATED);
5063 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5064 SET_HINT(window.hints, HINT_BELOW);
5065 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5066 SET_HINT(window.hints, HINT_ABOVE);
5067 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5068 SET_HINT(window.hints, HINT_STICKY);
5069 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5070 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5071 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5072 SET_HINT(window.hints, HINT_SKIP_PAGER);
5077 p_hint = strtok_r(NULL, delim, &p_save);
5078 } while (p_hint != NULL);
5084 CONF("own_window_type") {
5086 if (strncmp(value, "normal", 6) == EQUAL) {
5087 window.type = TYPE_NORMAL;
5088 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5089 window.type = TYPE_DESKTOP;
5090 } else if (strncmp(value, "dock", 4) == EQUAL) {
5091 window.type = TYPE_DOCK;
5092 text_alignment = TOP_LEFT;
5093 } else if (strncmp(value, "panel", 5) == EQUAL) {
5094 window.type = TYPE_PANEL;
5095 } else if (strncmp(value, "override", 8) == EQUAL) {
5096 window.type = TYPE_OVERRIDE;
5105 CONF("own_window_argb_visual") {
5106 use_argb_visual = string_to_bool(value);
5108 CONF("own_window_argb_value") {
5109 own_window_argb_value = strtol(value, 0, 0);
5110 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5111 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5114 #endif /* USE_ARGB */
5116 CONF("stippled_borders") {
5118 stippled_borders = strtol(value, 0, 0);
5120 stippled_borders = 4;
5124 CONF("imlib_cache_size") {
5126 cimlib_set_cache_size(atoi(value));
5129 CONF("imlib_cache_flush_interval") {
5131 cimlib_set_cache_flush_interval(atoi(value));
5136 CONF("update_interval_on_battery") {
5138 update_interval_bat = strtod(value, 0);
5143 CONF("update_interval") {
5145 set_update_interval(strtod(value, 0));
5149 if (info.music_player_interval == 0) {
5150 // default to update_interval
5151 info.music_player_interval = update_interval;
5154 CONF("total_run_times") {
5156 total_run_times = strtod(value, 0);
5162 stuff_in_uppercase = string_to_bool(value);
5164 CONF("max_specials") {
5166 max_specials = atoi(value);
5171 CONF("max_user_text") {
5173 max_user_text = atoi(value);
5178 CONF("text_buffer_size") {
5180 text_buffer_size = atoi(value);
5181 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5182 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5183 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5191 if (output_methods & TO_X) {
5192 X11_initialisation();
5201 global_text = (char *) malloc(1);
5202 global_text[0] = '\0';
5205 unsigned int l = strlen(global_text);
5207 char buf[CONF_BUFF_SIZE];
5209 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5215 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5223 /* Check for continuation of \\-\n. */
5224 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5225 global_text[l-1] = '\0';
5229 global_text = (char *) realloc(global_text, l + bl + 1);
5230 strcat(global_text, buf);
5232 if (strlen(global_text) > max_user_text) {
5236 global_text_lines = line + 1;
5239 #ifdef TCP_PORT_MONITOR
5240 CONF("max_port_monitor_connections") {
5242 if (!value || (sscanf(value, "%d", &max) != 1)) {
5243 /* an error. use default, warn and continue. */
5244 tcp_portmon_set_max_connections(0);
5246 } else if (tcp_portmon_set_max_connections(max)) {
5247 /* max is < 0, default has been set*/
5252 CONF("if_up_strictness") {
5254 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5255 ifup_strictness = IFUP_UP;
5256 } else if (strcasecmp(value, "up") == EQUAL) {
5257 ifup_strictness = IFUP_UP;
5258 } else if (strcasecmp(value, "link") == EQUAL) {
5259 ifup_strictness = IFUP_LINK;
5260 } else if (strcasecmp(value, "address") == EQUAL) {
5261 ifup_strictness = IFUP_ADDR;
5263 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5264 ifup_strictness = IFUP_UP;
5268 CONF("temperature_unit") {
5270 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5271 } else if (set_temp_output_unit(value)) {
5272 NORM_ERR("temperature_unit: incorrect argument");
5279 char *ptr = strtok(value, " ");
5282 ptr = strtok(NULL, " ");
5289 CONF("lua_draw_hook_pre") {
5291 llua_set_draw_pre_hook(value);
5296 CONF("lua_draw_hook_post") {
5298 llua_set_draw_post_hook(value);
5303 CONF("lua_startup_hook") {
5305 llua_set_startup_hook(value);
5310 CONF("lua_shutdown_hook") {
5312 llua_set_shutdown_hook(value);
5318 #endif /* HAVE_LUA */
5330 CONF("default_color"){}
5331 CONF3("default_shade_color", "default_shadecolor"){}
5332 CONF3("default_outline_color", "default_outlinecolor") {}
5333 CONF("own_window_colour") {}
5336 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5342 if (info.music_player_interval == 0) {
5343 // default to update_interval
5344 info.music_player_interval = update_interval;
5346 if (!global_text) { // didn't supply any text
5347 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5349 if (!output_methods) {
5350 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5352 #if defined(NCURSES)
5354 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5355 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5356 output_methods &= ~TO_NCURSES;
5360 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5361 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5362 output_methods &= ~(TO_STDOUT | TO_STDERR);
5364 #endif /* NCURSES */
5369 static void load_config_file_x11(const char *f)
5374 fp = open_config_file(f);
5378 DBGP("reading contents from config file '%s'", f);
5381 char buff[CONF_BUFF_SIZE], *name, *value;
5382 int ret = do_config_step(&line, fp, buff, &name, &value);
5383 if (ret == CONF_BREAK) {
5385 } else if (ret == CONF_CONTINUE) {
5390 X11_initialisation();
5391 if (x_initialised == YES) {
5393 color0 = get_x11_color(value);
5400 X11_initialisation();
5401 if (x_initialised == YES) {
5403 color1 = get_x11_color(value);
5410 X11_initialisation();
5411 if (x_initialised == YES) {
5413 color2 = get_x11_color(value);
5420 X11_initialisation();
5421 if (x_initialised == YES) {
5423 color3 = get_x11_color(value);
5430 X11_initialisation();
5431 if (x_initialised == YES) {
5433 color4 = get_x11_color(value);
5440 X11_initialisation();
5441 if (x_initialised == YES) {
5443 color5 = get_x11_color(value);
5450 X11_initialisation();
5451 if (x_initialised == YES) {
5453 color6 = get_x11_color(value);
5460 X11_initialisation();
5461 if (x_initialised == YES) {
5463 color7 = get_x11_color(value);
5470 X11_initialisation();
5471 if (x_initialised == YES) {
5473 color8 = get_x11_color(value);
5480 X11_initialisation();
5481 if (x_initialised == YES) {
5483 color9 = get_x11_color(value);
5489 CONF("default_color") {
5490 X11_initialisation();
5491 if (x_initialised == YES) {
5493 default_fg_color = get_x11_color(value);
5499 CONF3("default_shade_color", "default_shadecolor") {
5500 X11_initialisation();
5501 if (x_initialised == YES) {
5503 default_bg_color = get_x11_color(value);
5509 CONF3("default_outline_color", "default_outlinecolor") {
5510 X11_initialisation();
5511 if (x_initialised == YES) {
5513 default_out_color = get_x11_color(value);
5520 CONF("own_window_colour") {
5521 X11_initialisation();
5522 if (x_initialised == YES) {
5524 background_colour = get_x11_color(value);
5526 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5527 "'#' for hex colours");
5533 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5534 if(output_methods & TO_X) {
5535 X11_initialisation();
5544 #undef CONF_CONTINUE
5545 #undef CONF_BUFF_SIZE
5553 static void print_help(const char *prog_name) {
5554 printf("Usage: %s [OPTION]...\n"
5555 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5556 "window. Command line options will override configurations defined in config\n"
5558 " -v, --version version\n"
5559 " -q, --quiet quiet mode\n"
5560 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5561 " -c, --config=FILE config file to load\n"
5562 #ifdef CONFIG_OUTPUT
5563 " -C, --print-config print the builtin default config to stdout\n"
5564 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5566 " -d, --daemonize daemonize, fork to background\n"
5567 " -h, --help help\n"
5569 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5570 " -f, --font=FONT font to use\n"
5571 " -X, --display=DISPLAY X11 display to use\n"
5573 " -o, --own-window create own window to draw\n"
5576 " -b, --double-buffer double buffer (prevents flickering)\n"
5578 " -w, --window-id=WIN_ID window id to draw\n"
5579 " -x X x position\n"
5580 " -y Y y position\n"
5582 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5583 " -u, --interval=SECS update interval\n"
5584 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5585 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5590 /* : means that character before that takes an argument */
5591 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5601 #ifdef CONFIG_OUTPUT
5606 static const struct option longopts[] = {
5607 { "help", 0, NULL, 'h' },
5608 { "version", 0, NULL, 'V' },
5609 { "quiet", 0, NULL, 'q' },
5610 { "debug", 0, NULL, 'D' },
5611 { "config", 1, NULL, 'c' },
5612 #ifdef CONFIG_OUTPUT
5613 { "print-config", 0, NULL, 'C' },
5615 { "daemonize", 0, NULL, 'd' },
5617 { "alignment", 1, NULL, 'a' },
5618 { "font", 1, NULL, 'f' },
5619 { "display", 1, NULL, 'X' },
5621 { "own-window", 0, NULL, 'o' },
5624 { "double-buffer", 0, NULL, 'b' },
5626 { "window-id", 1, NULL, 'w' },
5628 { "text", 1, NULL, 't' },
5629 { "interval", 1, NULL, 'u' },
5630 { "pause", 1, NULL, 'p' },
5634 void set_current_config(void);
5635 void set_current_config(void)
5637 /* check if specified config file is valid */
5638 if (current_config) {
5640 if (stat(current_config, &sb) ||
5641 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5642 NORM_ERR("invalid configuration file '%s'\n", current_config);
5643 free(current_config);
5648 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5650 if (!current_config) {
5651 /* load default config file */
5652 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5655 /* Try to use personal config file first */
5656 to_real_path(buf, CONFIG_FILE);
5657 if (buf[0] && (fp = fopen(buf, "r"))) {
5658 current_config = strndup(buf, max_user_text);
5662 /* Try to use system config file if personal config not readable */
5663 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5664 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5668 /* No readable config found */
5669 if (!current_config) {
5670 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5671 #ifdef BUILD_BUILTIN_CONFIG
5672 current_config = strdup("==builtin==");
5673 NORM_ERR(NOCFGFILEFOUND
5674 ", using builtin default");
5676 CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5677 #endif /* ! CONF_OUTPUT */
5682 void initialisation(int argc, char **argv) {
5683 struct sigaction act, oact;
5685 set_default_configurations();
5686 set_current_config();
5687 load_config_file(current_config);
5688 currentconffile = conftree_add(currentconffile, current_config);
5690 /* init specials array */
5691 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5692 NORM_ERR("failed to create specials array");
5696 if (current_mail_spool == NULL) {
5699 variable_substitute(MAIL_FILE, buf, 256);
5701 if (buf[0] != '\0') {
5702 current_mail_spool = strndup(buf, text_buffer_size);
5707 /* handle other command line arguments */
5709 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5710 || defined(__NetBSD__)
5711 optind = optreset = 1;
5716 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5717 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5718 "kvm_open")) == NULL) {
5719 CRIT_ERR(NULL, NULL, "cannot read kvm");
5724 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5733 fork_to_background = 1;
5736 global_debug_level++;
5740 set_first_font(optarg);
5743 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
5762 global_text = strndup(optarg, max_user_text);
5763 convert_escapes(global_text);
5767 update_interval = strtod(optarg, 0);
5768 update_interval_old = update_interval;
5769 if (info.music_player_interval == 0) {
5770 // default to update_interval
5771 info.music_player_interval = update_interval;
5776 total_run_times = strtod(optarg, 0);
5780 gap_x = atoi(optarg);
5784 gap_y = atoi(optarg);
5789 startup_pause = atoi(optarg);
5790 sleep(startup_pause);
5801 if (output_methods & TO_X) {
5802 load_config_file_x11(current_config);
5806 /* generate text and get initial size */
5807 extract_variable_text(global_text);
5814 if (fork_to_background && first_pass) {
5819 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5826 fprintf(stderr, "\n");
5831 /* parent process */
5832 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5839 start_update_threading();
5841 text_buffer = malloc(max_user_text);
5842 memset(text_buffer, 0, max_user_text);
5843 tmpstring1 = malloc(text_buffer_size);
5844 memset(tmpstring1, 0, text_buffer_size);
5845 tmpstring2 = malloc(text_buffer_size);
5846 memset(tmpstring2, 0, text_buffer_size);
5851 X11_create_window();
5854 llua_setup_info(&info, update_interval);
5855 #endif /* HAVE_LUA */
5860 /* Set signal handlers */
5861 act.sa_handler = signal_handler;
5862 sigemptyset(&act.sa_mask);
5865 act.sa_flags |= SA_RESTART;
5868 if ( sigaction(SIGINT, &act, &oact) < 0
5869 || sigaction(SIGALRM, &act, &oact) < 0
5870 || sigaction(SIGUSR1, &act, &oact) < 0
5871 || sigaction(SIGHUP, &act, &oact) < 0
5872 || sigaction(SIGTERM, &act, &oact) < 0) {
5873 NORM_ERR("error setting signal handler: %s", strerror(errno));
5877 llua_startup_hook();
5878 #endif /* HAVE_LUA */
5881 int main(int argc, char **argv)
5890 g_signal_pending = 0;
5891 max_user_text = MAX_USER_TEXT_DEFAULT;
5893 memset(&info, 0, sizeof(info));
5897 #ifdef TCP_PORT_MONITOR
5898 /* set default connection limit */
5899 tcp_portmon_set_max_connections(0);
5903 if(curl_global_init(CURL_GLOBAL_ALL))
5904 NORM_ERR("curl_global_init() failed, you may not be able to use curl variables");
5907 /* handle command line parameters that don't change configs */
5909 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5910 || ((s = getenv("LANG")) && *s)) {
5911 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5913 NORM_ERR("malloc failed");
5915 for (x = 0; x < strlen(s); x++) {
5916 temp[x] = tolower(s[x]);
5919 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5925 if (!setlocale(LC_CTYPE, "")) {
5926 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5930 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5941 if (current_config) {
5942 free(current_config);
5944 current_config = strndup(optarg, max_user_text);
5947 if (!freopen("/dev/null", "w", stderr)) {
5948 NORM_ERR("unable to redirect stderr to /dev/null");
5952 print_help(argv[0]);
5954 #ifdef CONFIG_OUTPUT
5961 window.window = strtol(optarg, 0, 0);
5966 disp = strdup(optarg);
5975 set_current_config();
5978 /* Load xoap keys, if existing */
5982 #ifdef HAVE_SYS_INOTIFY_H
5983 inotify_fd = inotify_init();
5984 if(inotify_fd != -1) {
5987 fl = fcntl(inotify_fd, F_GETFL);
5988 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
5990 #endif /* HAVE_SYS_INOTIFY_H */
5992 initialisation(argc, argv);
5994 first_pass = 0; /* don't ever call fork() again */
5999 curl_global_cleanup();
6002 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6010 void alarm_handler(void) {
6012 kill(childpid, SIGTERM);
6016 static void signal_handler(int sig)
6018 /* signal handler is light as a feather, as it should be.
6019 * we will poll g_signal_pending with each loop of conky
6020 * and do any signal processing there, NOT here (except
6021 * SIGALRM because this is caused when conky is hanging) */
6022 if(sig == SIGALRM) {
6025 g_signal_pending = sig;