1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* disable inotify auto reload feature if desired */
151 int disable_auto_reload = 0;
153 /* two strings for internal use */
154 static char *tmpstring1, *tmpstring2;
156 /* variables holding various config settings */
158 int format_human_readable;
165 int top_cpu, top_mem, top_time;
173 static int extra_newline;
174 enum x_initialiser_state x_initialised = NO;
175 static volatile int g_signal_pending;
176 /* Update interval */
177 double update_interval;
178 double update_interval_old;
179 double update_interval_bat;
180 void *global_cpu = NULL;
181 unsigned int max_text_width = 0;
186 /* prototypes for internally used functions */
187 static void signal_handler(int);
188 static void print_version(void) __attribute__((noreturn));
189 static void reload_config(void);
191 static void print_version(void)
193 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
195 printf("\nCompiled in features:\n\n"
196 "System config file: "SYSTEM_CONFIG_FILE"\n"
197 "Package library path: "PACKAGE_LIBDIR"\n\n"
201 " * Xdamage extension\n"
202 # endif /* HAVE_XDAMAGE */
204 " * XDBE (double buffer extension)\n"
205 # endif /* HAVE_XDBE */
211 # endif /* USE_ARGB */
213 "\n Music detection:\n"
216 #endif /* AUDACIOUS */
232 #endif /* HAVE_OPENMP */
239 #ifdef TCP_PORT_MONITOR
241 #endif /* TCP_PORT_MONITOR */
244 #endif /* HAVE_CURL */
249 " * Weather (METAR)\n"
251 " * Weather (XOAP)\n"
256 #endif /* HAVE_IWLIB */
258 " * support for IBM/Lenovo notebooks\n"
268 #endif /* CONFIG_OUTPUT */
273 " * ALSA mixer support\n"
274 #endif /* MIXER_IS_ALSA */
287 #ifdef HAVE_LUA_CAIRO
289 #endif /* HAVE_LUA_CAIRO */
290 #ifdef HAVE_LUA_IMLIB2
293 #endif /* HAVE_LUA */
299 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
304 static void X11_create_window(void);
305 static void X11_initialisation(void);
307 struct _x11_stuff_s {
311 XserverRegion region2, part;
312 int event_base, error_base;
318 static int text_start_x, text_start_y; /* text start position in window */
319 static int text_width, text_height;
335 /* display to connect to */
336 static char *disp = NULL;
340 /* struct that has all info to be shared between
341 * instances of the same text object */
342 struct information info;
344 /* path to config file */
345 char *current_config;
347 /* set to 1 if you want all text to be in uppercase */
348 static unsigned int stuff_in_uppercase;
350 /* Run how many times? */
351 static unsigned long total_run_times;
354 static int fork_to_background;
356 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
358 /* filenames for output */
359 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
360 char *append_file = NULL; FILE *append_fpointer = NULL;
364 static int show_graph_scale;
365 static int show_graph_range;
367 /* Position on the screen */
368 static int text_alignment;
369 static int gap_x, gap_y;
372 static int draw_borders;
373 static int draw_graph_borders;
374 static int stippled_borders;
376 int get_stippled_borders(void)
378 return stippled_borders;
381 static int draw_shades, draw_outline;
383 long default_fg_color, default_bg_color, default_out_color;
385 /* create own window or draw stuff to root? */
386 static int set_transparent = 0;
389 static int own_window = 0;
390 static int background_colour = 0;
392 /* fixed size/pos is set if wm/user changes them */
393 static int fixed_size = 0, fixed_pos = 0;
396 static int minimum_width, minimum_height;
397 static int maximum_width;
402 static int sensor_device;
405 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
408 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
409 unsigned int max_user_text;
411 /* maximum size of individual text buffers, ie $exec buffer size */
412 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
417 /* no buffers in used memory? */
420 /* pad percentages to decimals? */
421 static int pad_percents = 0;
423 static char *global_text = 0;
425 char *get_global_text(void)
430 long global_text_lines;
432 static int total_updates;
433 static int updatereset;
435 void set_updatereset(int i)
440 int get_updatereset(void)
445 int check_contains(char *f, char *s)
448 FILE *where = open_file(f, 0);
453 while (fgets(buf1, 256, where)) {
454 if (strstr(buf1, s)) {
461 NORM_ERR("Could not open the file");
466 #define SECRIT_MULTILINE_CHAR '\x02'
468 int calc_text_width(const char *s)
470 size_t slen = strlen(s);
473 if ((output_methods & TO_X) == 0) {
483 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
484 (const FcChar8 *) s, slen, &gi);
486 XftTextExtents8(display, fonts[selected_font].xftfont,
487 (const FcChar8 *) s, slen, &gi);
493 return XTextWidth(fonts[selected_font].font, s, slen);
498 /* formatted text to render on screen, generated in generate_text(),
499 * drawn in draw_stuff() */
501 static char *text_buffer;
503 /* quite boring functions */
505 static inline void for_each_line(char *b, int f(char *, int))
508 int special_index = 0; /* specials index */
510 for (ps = b, pe = b; *pe; pe++) {
513 special_index = f(ps, special_index);
520 f(ps, special_index);
524 static void convert_escapes(char *buf)
526 char *p = buf, *s = buf;
533 } else if (*s == '\\') {
544 /* Prints anything normally printed with snprintf according to the current value
545 * of use_spacer. Actually slightly more flexible than snprintf, as you can
546 * safely specify the destination buffer as one of your inputs. */
547 int spaced_print(char *buf, int size, const char *format, int width, ...)
556 tempbuf = malloc(size * sizeof(char));
558 // Passes the varargs along to vsnprintf
559 va_start(argp, width);
560 vsnprintf(tempbuf, size, format, argp);
563 switch (use_spacer) {
565 len = snprintf(buf, size, "%s", tempbuf);
568 len = snprintf(buf, size, "%*s", width, tempbuf);
571 len = snprintf(buf, size, "%-*s", width, tempbuf);
578 /* print percentage values
580 * - i.e., unsigned values between 0 and 100
581 * - respect the value of pad_percents */
582 int percent_print(char *buf, int size, unsigned value)
584 return spaced_print(buf, size, "%u", pad_percents, value);
587 /* converts from bytes to human readable format (K, M, G, T)
589 * The algorithm always divides by 1024, as unit-conversion of byte
590 * counts suggests. But for output length determination we need to
591 * compare with 1000 here, as we print in decimal form. */
592 void human_readable(long long num, char *buf, int size)
594 const char **suffix = suffixes;
600 /* Possibly just output as usual, for example for stdout usage */
601 if (!format_human_readable) {
602 spaced_print(buf, size, "%d", 6, round_to_int(num));
613 if (llabs(num) < 1000LL) {
614 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
618 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
626 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
628 * The goal is to always have a significance of 3, by
629 * adjusting the decimal part of the number. Sample output:
633 * so the point of alignment resides between number and unit. The
634 * upside of this is that there is minimal padding necessary, though
635 * there should be a way to make alignment take place at the decimal
636 * dot (then with fixed width decimal part).
638 * Note the repdigits below: when given a precision value, printf()
639 * rounds the float to it, not just cuts off the remaining digits. So
640 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
641 * printed with a precision of 0. Yay. */
643 precision = 0; /* print 100-999 without decimal part */
645 precision = 1; /* print 10-99 with one decimal place */
647 precision = 2; /* print 0-9 with two decimal places */
649 spaced_print(buf, size, format, width, precision, fnum, *suffix);
652 /* global object list root element */
653 static struct text_object global_root_object;
655 static long current_text_color;
657 void set_current_text_color(long colour)
659 current_text_color = colour;
662 long get_current_text_color(void)
664 return current_text_color;
667 //adds newstring to to the tree unless you can already see it when travelling back.
668 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
669 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
670 struct conftree* node;
671 struct conftree* node2;
673 for(node = previous; node != NULL; node = node->back) {
674 if(strcmp(node->string, newstring) == 0) {
678 node = malloc(sizeof(struct conftree));
679 if (previous != NULL) {
680 if(previous->vert_next == NULL) {
681 previous->vert_next = node;
683 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
684 node2->horz_next = node;
687 node->string = strdup(newstring);
688 node->horz_next = NULL;
689 node->vert_next = NULL;
690 node->back = previous;
694 void conftree_empty(struct conftree* tree) {
696 conftree_empty(tree->horz_next);
697 conftree_empty(tree->vert_next);
703 struct conftree *currentconffile;
705 static void extract_variable_text(const char *p)
707 free_text_objects(&global_root_object, 0);
721 extract_variable_text_internal(&global_root_object, p);
724 void parse_conky_vars(struct text_object *root, const char *txt,
725 char *p, int p_max_size, struct information *cur)
727 extract_variable_text_internal(root, txt);
728 generate_text_internal(p, p_max_size, *root, cur);
731 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
732 * multiline objects like $exec work with $align[rc] and friends
734 void substitute_newlines(char *p, long l)
738 while (p && *p && p < s + l) {
740 /* only substitute if it's not the last newline */
741 *p = SECRIT_MULTILINE_CHAR;
747 void generate_text_internal(char *p, int p_max_size,
748 struct text_object root, struct information *cur)
750 struct text_object *obj;
752 int need_to_load_fonts = 0;
755 /* for the OBJ_top* handler */
756 struct process **needed = 0;
759 char buff_in[p_max_size];
761 #endif /* HAVE_ICONV */
765 while (obj && p_max_size > 0) {
766 needed = 0; /* reset for top stuff */
768 /* IFBLOCK jumping algorithm
770 * This is easier as it looks like:
771 * - each IF checks it's condition
772 * - on FALSE: call DO_JUMP
773 * - on TRUE: don't care
774 * - each ELSE calls DO_JUMP unconditionally
775 * - each ENDIF is silently being ignored
778 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
779 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
780 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
781 * regularly, "obj" is being updated to point to obj->next, so object parsing
782 * continues right after the corresponding ELSE or ENDIF. This means that if we
783 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
784 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
785 * jumped, and there is nothing to do.
789 if (obj->ifblock_next) \
790 obj = obj->ifblock_next; \
793 #define OBJ(a) break; case OBJ_##a:
797 NORM_ERR("not implemented obj type %d", obj->type);
799 print_read_tcp(obj, p, p_max_size);
803 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
805 #endif /* !__OpenBSD__ */
809 ok = get_freq(p, p_max_size, "%.0f", 1,
817 ok = get_freq(p, p_max_size, "%'.2f", 1000,
820 /* OpenBSD has no such flag (SUSv2) */
821 ok = get_freq(p, p_max_size, "%.2f", 1000,
823 #endif /* __OpenBSD */
826 #if defined(__linux__)
828 print_voltage_mv(obj, p, p_max_size);
831 print_voltage_v(obj, p, p_max_size);
835 OBJ(wireless_essid) {
836 print_wireless_essid(obj, p, p_max_size);
839 print_wireless_mode(obj, p, p_max_size);
841 OBJ(wireless_bitrate) {
842 print_wireless_bitrate(obj, p, p_max_size);
845 print_wireless_ap(obj, p, p_max_size);
847 OBJ(wireless_link_qual) {
848 print_wireless_link_qual(obj, p, p_max_size);
850 OBJ(wireless_link_qual_max) {
851 print_wireless_link_qual_max(obj, p, p_max_size);
853 OBJ(wireless_link_qual_perc) {
854 print_wireless_link_qual_perc(obj, p, p_max_size);
856 OBJ(wireless_link_bar) {
857 print_wireless_link_bar(obj, p, p_max_size);
859 #endif /* HAVE_IWLIB */
861 #endif /* __linux__ */
865 get_acpi_fan(p, p_max_size);
868 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
871 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
874 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
876 OBJ(battery_percent) {
877 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
880 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
883 get_battery_short_status(p, p_max_size, obj->data.s);
885 #endif /* __OpenBSD__ */
888 human_readable(cur->buffers * 1024, p, 255);
891 human_readable(cur->cached * 1024, p, 255);
893 OBJ(cmdline_to_pid) {
894 print_cmdline_to_pid(obj, p, p_max_size);
897 if (obj->data.i > info.cpu_count) {
898 NORM_ERR("obj->data.i %i info.cpu_count %i",
899 obj->data.i, info.cpu_count);
900 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
902 if (cur->cpu_usage) {
903 percent_print(p, p_max_size,
904 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
908 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
910 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
914 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
917 print_loadgraph(obj, p, p_max_size);
921 new_fg(p, obj->data.l);
955 #if defined(__linux__)
957 snprintf(p, p_max_size, "%s",
958 get_disk_protect_queue(obj->data.s));
961 print_i8k_version(obj, p, p_max_size);
964 print_i8k_bios(obj, p, p_max_size);
967 print_i8k_serial(obj, p, p_max_size);
970 print_i8k_cpu_temp(obj, p, p_max_size);
972 OBJ(i8k_left_fan_status) {
973 print_i8k_left_fan_status(obj, p, p_max_size);
975 OBJ(i8k_right_fan_status) {
976 print_i8k_right_fan_status(obj, p, p_max_size);
978 OBJ(i8k_left_fan_rpm) {
979 print_i8k_left_fan_rpm(obj, p, p_max_size);
981 OBJ(i8k_right_fan_rpm) {
982 print_i8k_right_fan_rpm(obj, p, p_max_size);
985 print_i8k_ac_status(obj, p, p_max_size);
987 OBJ(i8k_buttons_status) {
988 print_i8k_buttons_status(obj, p, p_max_size);
992 get_ibm_acpi_fan(obj, p, p_max_size);
995 print_ibm_temps(obj, p, p_max_size);
998 get_ibm_acpi_volume(obj, p, p_max_size);
1000 OBJ(ibm_brightness) {
1001 get_ibm_acpi_brightness(obj, p, p_max_size);
1004 /* information from sony_laptop kernel module
1005 * /sys/devices/platform/sony-laptop */
1006 OBJ(sony_fanspeed) {
1007 get_sony_fanspeed(p, p_max_size);
1010 if (!gateway_exists()) {
1015 print_gateway_iface(p, p_max_size);
1018 print_gateway_ip(p, p_max_size);
1021 snprintf(p, p_max_size, "%d", get_laptop_mode());
1024 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1026 #endif /* __linux__ */
1027 #if (defined(__FreeBSD__) || defined(__linux__))
1029 if (!interface_up(obj)) {
1035 OBJ(obsd_sensors_temp) {
1036 print_obsd_sensors_temp(obj, p, p_max_size);
1038 OBJ(obsd_sensors_fan) {
1039 print_obsd_sensors_fan(obj, p, p_max_size);
1041 OBJ(obsd_sensors_volt) {
1042 print_obsd_sensors_volt(obj, p, p_max_size);
1045 get_obsd_vendor(p, p_max_size);
1048 get_obsd_product(p, p_max_size);
1050 #endif /* __OpenBSD__ */
1053 new_font(p, obj->data.s);
1054 need_to_load_fonts = 1;
1058 print_diskio(obj, p, p_max_size);
1061 print_diskio_write(obj, p, p_max_size);
1064 print_diskio_read(obj, p, p_max_size);
1068 print_diskiograph(obj, p, p_max_size);
1070 OBJ(diskiograph_read) {
1071 print_diskiograph(obj, p, p_max_size);
1073 OBJ(diskiograph_write) {
1074 print_diskiograph(obj, p, p_max_size);
1078 print_downspeed(obj, p, p_max_size);
1081 print_downspeedf(obj, p, p_max_size);
1084 OBJ(downspeedgraph) {
1085 print_downspeedgraph(obj, p, p_max_size);
1089 /* Since we see you, you're if has not jumped.
1090 * Do Ninja jump here: without leaving traces.
1091 * This is to prevent us from stale jumped flags.
1093 obj = obj->ifblock_next;
1097 /* harmless object, just ignore */
1100 print_addr(obj, p, p_max_size);
1102 #if defined(__linux__)
1104 print_addrs(obj, p, p_max_size);
1106 #endif /* __linux__ */
1107 #if defined(IMLIB2) && defined(X11)
1109 /* doesn't actually draw anything, just queues it omp. the
1110 * image will get drawn after the X event loop */
1111 cimlib_add_image(obj->data.s);
1115 evaluate(obj->data.s, p, p_max_size);
1118 print_exec(obj, p, p_max_size);
1121 print_execp(obj, p, p_max_size);
1124 print_execgauge(obj, p, p_max_size);
1127 print_execbar(obj, p, p_max_size);
1131 print_execgraph(obj, p, p_max_size);
1135 print_execibar(obj, p, p_max_size);
1139 print_execigraph(obj, p, p_max_size);
1143 print_execigauge(obj, p, p_max_size);
1146 print_execi(obj, p, p_max_size);
1149 print_execpi(obj, p, p_max_size);
1152 print_texeci(obj, p, p_max_size);
1155 print_imap_unseen(obj, p, p_max_size);
1157 OBJ(imap_messages) {
1158 print_imap_messages(obj, p, p_max_size);
1161 print_pop3_unseen(obj, p, p_max_size);
1164 print_pop3_used(obj, p, p_max_size);
1167 print_fs_bar(obj, 0, p, p_max_size);
1170 print_fs_free(obj, p, p_max_size);
1173 print_fs_perc(obj, 1, p, p_max_size);
1176 print_fs_size(obj, p, p_max_size);
1179 print_fs_type(obj, p, p_max_size);
1182 print_fs_used(obj, p, p_max_size);
1185 print_fs_bar(obj, 1, p, p_max_size);
1188 print_fs_perc(obj, 0, p, p_max_size);
1191 print_loadavg(obj, p, p_max_size);
1194 new_goto(p, obj->data.i);
1201 new_hr(p, obj->data.i);
1205 print_nameserver(obj, p, p_max_size);
1209 print_eve(obj, p, p_max_size);
1214 curl_print(obj, p, p_max_size);
1219 rss_print_info(obj, p, p_max_size);
1224 print_weather(obj, p, p_max_size);
1228 OBJ(weather_forecast) {
1229 print_weather_forecast(obj, p, p_max_size);
1234 print_lua(obj, p, p_max_size);
1237 print_lua_parse(obj, p, p_max_size);
1240 print_lua_bar(obj, p, p_max_size);
1244 print_lua_graph(obj, p, p_max_size);
1248 print_lua_gauge(obj, p, p_max_size);
1250 #endif /* HAVE_LUA */
1256 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1257 snprintf(p, p_max_size, "N/A");
1259 temp_print(p, p_max_size, (double)val,
1260 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1265 new_offset(p, obj->data.i);
1268 new_voffset(p, obj->data.i);
1272 print_sysfs_sensor(obj, p, p_max_size);
1275 print_sysfs_sensor(obj, p, p_max_size);
1278 print_sysfs_sensor(obj, p, p_max_size);
1280 #endif /* __linux__ */
1282 new_alignr(p, obj->data.i);
1285 new_alignc(p, obj->data.i);
1288 char buf[max_user_text];
1289 struct information *tmp_info =
1290 malloc(sizeof(struct information));
1291 memcpy(tmp_info, cur, sizeof(struct information));
1292 generate_text_internal(buf, max_user_text,
1293 *obj->sub, tmp_info);
1295 if (strlen(buf) != 0) {
1301 char expression[max_user_text];
1303 struct information *tmp_info;
1305 tmp_info = malloc(sizeof(struct information));
1306 memcpy(tmp_info, cur, sizeof(struct information));
1307 generate_text_internal(expression, max_user_text,
1308 *obj->sub, tmp_info);
1309 DBGP("parsed arg into '%s'", expression);
1311 val = compare(expression);
1313 NORM_ERR("compare failed for expression '%s'",
1323 spc = strchr(obj->data.s, ' ');
1324 if (!spc && access(obj->data.s, F_OK)) {
1328 if (check_contains(obj->data.s, spc + 1))
1335 && (!check_mount(obj->data.s))) {
1341 if (!get_process_by_name(obj->data.s)) {
1343 if ((obj->data.s) && system(obj->data.s)) {
1348 #if defined(__linux__)
1350 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1354 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1357 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1362 human_readable(cur->mem * 1024, p, 255);
1365 human_readable(cur->memeasyfree * 1024, p, 255);
1368 human_readable(cur->memfree * 1024, p, 255);
1371 human_readable(cur->memmax * 1024, p, 255);
1375 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1378 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1381 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1385 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1390 print_mixer(obj, p, p_max_size);
1393 print_mixerl(obj, p, p_max_size);
1396 print_mixerr(obj, p, p_max_size);
1399 print_mixer_bar(obj, p, p_max_size);
1402 print_mixerl_bar(obj, p, p_max_size);
1405 print_mixerr_bar(obj, p, p_max_size);
1407 OBJ(if_mixer_mute) {
1408 if (!check_mixer_muted(obj)) {
1413 #define NOT_IN_X "Not running in X"
1415 if(x_initialised != YES) {
1416 strncpy(p, NOT_IN_X, p_max_size);
1418 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1421 OBJ(monitor_number) {
1422 if(x_initialised != YES) {
1423 strncpy(p, NOT_IN_X, p_max_size);
1425 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1429 if(x_initialised != YES) {
1430 strncpy(p, NOT_IN_X, p_max_size);
1432 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1435 OBJ(desktop_number) {
1436 if(x_initialised != YES) {
1437 strncpy(p, NOT_IN_X, p_max_size);
1439 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1443 if(x_initialised != YES) {
1444 strncpy(p, NOT_IN_X, p_max_size);
1445 }else if(cur->x11.desktop.name != NULL) {
1446 strncpy(p, cur->x11.desktop.name, p_max_size);
1452 char buf[max_user_text];
1454 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1456 print_format_time(obj, p, p_max_size);
1460 print_mails(obj, p, p_max_size);
1463 print_new_mails(obj, p, p_max_size);
1466 print_seen_mails(obj, p, p_max_size);
1469 print_unseen_mails(obj, p, p_max_size);
1471 OBJ(flagged_mails) {
1472 print_flagged_mails(obj, p, p_max_size);
1474 OBJ(unflagged_mails) {
1475 print_unflagged_mails(obj, p, p_max_size);
1477 OBJ(forwarded_mails) {
1478 print_forwarded_mails(obj, p, p_max_size);
1480 OBJ(unforwarded_mails) {
1481 print_unforwarded_mails(obj, p, p_max_size);
1483 OBJ(replied_mails) {
1484 print_replied_mails(obj, p, p_max_size);
1486 OBJ(unreplied_mails) {
1487 print_unreplied_mails(obj, p, p_max_size);
1490 print_draft_mails(obj, p, p_max_size);
1492 OBJ(trashed_mails) {
1493 print_trashed_mails(obj, p, p_max_size);
1496 print_mboxscan(obj, p, p_max_size);
1499 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1502 new_outline(p, obj->data.l);
1505 char buf[max_user_text];
1507 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1509 print_pid_chroot(obj, p, p_max_size);
1512 char buf[max_user_text];
1514 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1516 print_pid_cmdline(obj, p, p_max_size);
1519 char buf[max_user_text];
1521 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1523 print_pid_cwd(obj, p, p_max_size);
1526 char buf[max_user_text];
1528 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1530 print_pid_environ(obj, p, p_max_size);
1532 OBJ(pid_environ_list) {
1533 char buf[max_user_text];
1535 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1537 print_pid_environ_list(obj, p, p_max_size);
1540 char buf[max_user_text];
1542 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1544 print_pid_exe(obj, p, p_max_size);
1547 char buf[max_user_text];
1549 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1551 print_pid_nice(obj, p, p_max_size);
1553 OBJ(pid_openfiles) {
1554 char buf[max_user_text];
1556 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1558 print_pid_openfiles(obj, p, p_max_size);
1561 char buf[max_user_text];
1563 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1565 print_pid_parent(obj, p, p_max_size);
1568 char buf[max_user_text];
1570 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1572 print_pid_priority(obj, p, p_max_size);
1575 char buf[max_user_text];
1577 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1579 print_pid_state(obj, p, p_max_size);
1581 OBJ(pid_state_short) {
1582 char buf[max_user_text];
1584 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1586 print_pid_state_short(obj, p, p_max_size);
1589 char buf[max_user_text];
1591 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1593 print_pid_stderr(obj, p, p_max_size);
1596 char buf[max_user_text];
1598 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1600 print_pid_stdin(obj, p, p_max_size);
1603 char buf[max_user_text];
1605 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1607 print_pid_stdout(obj, p, p_max_size);
1610 char buf[max_user_text];
1612 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1614 print_pid_threads(obj, p, p_max_size);
1616 OBJ(pid_thread_list) {
1617 char buf[max_user_text];
1619 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1621 print_pid_thread_list(obj, p, p_max_size);
1623 OBJ(pid_time_kernelmode) {
1624 char buf[max_user_text];
1626 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1628 print_pid_time_kernelmode(obj, p, p_max_size);
1630 OBJ(pid_time_usermode) {
1631 char buf[max_user_text];
1633 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1635 print_pid_time_usermode(obj, p, p_max_size);
1638 char buf[max_user_text];
1640 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1642 print_pid_time(obj, p, p_max_size);
1645 char buf[max_user_text];
1647 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1649 print_pid_uid(obj, p, p_max_size);
1652 char buf[max_user_text];
1654 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1656 print_pid_euid(obj, p, p_max_size);
1659 char buf[max_user_text];
1661 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1663 print_pid_suid(obj, p, p_max_size);
1666 char buf[max_user_text];
1668 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1670 print_pid_fsuid(obj, p, p_max_size);
1673 char buf[max_user_text];
1675 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1677 print_pid_gid(obj, p, p_max_size);
1680 char buf[max_user_text];
1682 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1684 print_pid_egid(obj, p, p_max_size);
1687 char buf[max_user_text];
1689 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1691 print_pid_sgid(obj, p, p_max_size);
1694 char buf[max_user_text];
1696 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1698 print_pid_fsgid(obj, p, p_max_size);
1701 char buf[max_user_text];
1703 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1705 print_pid_read(obj, p, p_max_size);
1708 char buf[max_user_text];
1710 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1712 print_pid_vmpeak(obj, p, p_max_size);
1715 char buf[max_user_text];
1717 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1719 print_pid_vmsize(obj, p, p_max_size);
1722 char buf[max_user_text];
1724 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1726 print_pid_vmlck(obj, p, p_max_size);
1729 char buf[max_user_text];
1731 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1733 print_pid_vmhwm(obj, p, p_max_size);
1736 char buf[max_user_text];
1738 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1740 print_pid_vmrss(obj, p, p_max_size);
1743 char buf[max_user_text];
1745 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1747 print_pid_vmdata(obj, p, p_max_size);
1750 char buf[max_user_text];
1752 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1754 print_pid_vmstk(obj, p, p_max_size);
1757 char buf[max_user_text];
1759 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1761 print_pid_vmexe(obj, p, p_max_size);
1764 char buf[max_user_text];
1766 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1768 print_pid_vmlib(obj, p, p_max_size);
1771 char buf[max_user_text];
1773 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1775 print_pid_vmpte(obj, p, p_max_size);
1778 char buf[max_user_text];
1780 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1782 print_pid_write(obj, p, p_max_size);
1785 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1787 OBJ(running_processes) {
1788 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1790 OBJ(running_threads) {
1791 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1794 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1797 snprintf(p, p_max_size, "%s", obj->data.s);
1801 new_bg(p, obj->data.l);
1804 new_stippled_hr(obj, p);
1808 human_readable(cur->swap * 1024, p, 255);
1811 human_readable(cur->swapfree * 1024, p, 255);
1814 human_readable(cur->swapmax * 1024, p, 255);
1817 if (cur->swapmax == 0) {
1818 strncpy(p, "No swap", p_max_size);
1820 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1824 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1827 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1830 print_time(obj, p, p_max_size);
1833 print_utime(obj, p, p_max_size);
1836 print_tztime(obj, p, p_max_size);
1839 print_totaldown(obj, p, p_max_size);
1842 print_totalup(obj, p, p_max_size);
1845 char buf[max_user_text];
1847 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1849 print_gid_name(obj, p, p_max_size);
1852 char buf[max_user_text];
1854 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1856 print_uid_name(obj, p, p_max_size);
1859 snprintf(p, p_max_size, "%d", total_updates);
1862 if(total_updates % updatereset != obj->data.i - 1) {
1867 print_upspeed(obj, p, p_max_size);
1870 print_upspeedf(obj, p, p_max_size);
1874 print_upspeedgraph(obj, p, p_max_size);
1878 format_seconds_short(p, p_max_size, (int) cur->uptime);
1881 format_seconds(p, p_max_size, (int) cur->uptime);
1885 snprintf(p, p_max_size, "%s", cur->users.names);
1888 snprintf(p, p_max_size, "%s", cur->users.terms);
1891 snprintf(p, p_max_size, "%s", cur->users.times);
1894 update_user_time(obj->data.s);
1895 snprintf(p, p_max_size, "%s", cur->users.ctime);
1898 snprintf(p, p_max_size, "%d", cur->users.number);
1900 #endif /* __linux__ */
1901 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1902 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1906 msg = get_apm_adapter();
1907 snprintf(p, p_max_size, "%s", msg);
1910 OBJ(apm_battery_life) {
1913 msg = get_apm_battery_life();
1914 snprintf(p, p_max_size, "%s", msg);
1917 OBJ(apm_battery_time) {
1920 msg = get_apm_battery_time();
1921 snprintf(p, p_max_size, "%s", msg);
1924 #endif /* __FreeBSD__ __OpenBSD__ */
1928 print_mpd_title(obj, p, p_max_size);
1930 print_mpd_artist(obj, p, p_max_size);
1932 print_mpd_album(obj, p, p_max_size);
1934 print_mpd_random(obj, p, p_max_size);
1936 print_mpd_repeat(obj, p, p_max_size);
1938 print_mpd_track(obj, p, p_max_size);
1940 print_mpd_name(obj, p, p_max_size);
1942 print_mpd_file(obj, p, p_max_size);
1944 print_mpd_vol(obj, p, p_max_size);
1946 print_mpd_bitrate(obj, p, p_max_size);
1948 print_mpd_status(obj, p, p_max_size);
1950 print_mpd_elapsed(obj, p, p_max_size);
1953 print_mpd_length(obj, p, p_max_size);
1956 print_mpd_percent(obj, p, p_max_size);
1959 print_mpd_bar(obj, p, p_max_size);
1962 print_mpd_smart(obj, p, p_max_size);
1964 OBJ(if_mpd_playing) {
1965 if (!mpd_get_info()->is_playing) {
1972 #define MOC_PRINT(t, a) \
1973 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1975 MOC_PRINT(state, "??");
1978 MOC_PRINT(file, "no file");
1981 MOC_PRINT(title, "no title");
1984 MOC_PRINT(artist, "no artist");
1987 MOC_PRINT(song, "no song");
1990 MOC_PRINT(album, "no album");
1992 OBJ(moc_totaltime) {
1993 MOC_PRINT(totaltime, "0:00");
1996 MOC_PRINT(timeleft, "0:00");
1999 MOC_PRINT(curtime, "0:00");
2002 MOC_PRINT(bitrate, "0Kbps");
2005 MOC_PRINT(rate, "0KHz");
2011 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2014 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2017 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2020 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2022 OBJ(xmms2_comment) {
2023 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2026 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2029 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2032 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2034 OBJ(xmms2_tracknr) {
2035 if (cur->xmms2.tracknr != -1) {
2036 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2039 OBJ(xmms2_bitrate) {
2040 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2043 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2046 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2048 OBJ(xmms2_elapsed) {
2049 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2050 (cur->xmms2.elapsed / 1000) % 60);
2052 OBJ(xmms2_duration) {
2053 snprintf(p, p_max_size, "%02d:%02d",
2054 cur->xmms2.duration / 60000,
2055 (cur->xmms2.duration / 1000) % 60);
2057 OBJ(xmms2_percent) {
2058 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2061 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2063 OBJ(xmms2_playlist) {
2064 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2066 OBJ(xmms2_timesplayed) {
2067 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2070 if (strlen(cur->xmms2.title) < 2
2071 && strlen(cur->xmms2.title) < 2) {
2072 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2074 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2078 OBJ(if_xmms2_connected) {
2079 if (cur->xmms2.conn_state != 1) {
2085 OBJ(audacious_status) {
2086 snprintf(p, p_max_size, "%s",
2087 cur->audacious.items[AUDACIOUS_STATUS]);
2089 OBJ(audacious_title) {
2090 snprintf(p, cur->audacious.max_title_len > 0
2091 ? cur->audacious.max_title_len : p_max_size, "%s",
2092 cur->audacious.items[AUDACIOUS_TITLE]);
2094 OBJ(audacious_length) {
2095 snprintf(p, p_max_size, "%s",
2096 cur->audacious.items[AUDACIOUS_LENGTH]);
2098 OBJ(audacious_length_seconds) {
2099 snprintf(p, p_max_size, "%s",
2100 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2102 OBJ(audacious_position) {
2103 snprintf(p, p_max_size, "%s",
2104 cur->audacious.items[AUDACIOUS_POSITION]);
2106 OBJ(audacious_position_seconds) {
2107 snprintf(p, p_max_size, "%s",
2108 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2110 OBJ(audacious_bitrate) {
2111 snprintf(p, p_max_size, "%s",
2112 cur->audacious.items[AUDACIOUS_BITRATE]);
2114 OBJ(audacious_frequency) {
2115 snprintf(p, p_max_size, "%s",
2116 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2118 OBJ(audacious_channels) {
2119 snprintf(p, p_max_size, "%s",
2120 cur->audacious.items[AUDACIOUS_CHANNELS]);
2122 OBJ(audacious_filename) {
2123 snprintf(p, p_max_size, "%s",
2124 cur->audacious.items[AUDACIOUS_FILENAME]);
2126 OBJ(audacious_playlist_length) {
2127 snprintf(p, p_max_size, "%s",
2128 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2130 OBJ(audacious_playlist_position) {
2131 snprintf(p, p_max_size, "%s",
2132 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2134 OBJ(audacious_main_volume) {
2135 snprintf(p, p_max_size, "%s",
2136 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2138 OBJ(audacious_bar) {
2142 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2143 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2144 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2146 #endif /* AUDACIOUS */
2150 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2153 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2156 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2159 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2162 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2165 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2168 /* we have four different types of top (top, top_mem,
2169 * top_time and top_io). To avoid having almost-same code four
2170 * times, we have this special handler. */
2179 print_top(obj, p, p_max_size);
2180 #endif /* __linux__ */
2182 print_tailhead("tail", obj, p, p_max_size);
2185 print_tailhead("head", obj, p, p_max_size);
2188 print_lines(obj, p, p_max_size);
2192 print_words(obj, p, p_max_size);
2194 #ifdef TCP_PORT_MONITOR
2196 tcp_portmon_action(obj, p, p_max_size);
2198 #endif /* TCP_PORT_MONITOR */
2202 do_iconv_start(obj);
2207 #endif /* HAVE_ICONV */
2209 OBJ(entropy_avail) {
2210 print_entropy_avail(obj, p, p_max_size);
2213 print_entropy_perc(obj, p, p_max_size);
2215 OBJ(entropy_poolsize) {
2216 print_entropy_poolsize(obj, p, p_max_size);
2219 print_entropy_bar(obj, p, p_max_size);
2223 print_smapi(obj, p, p_max_size);
2225 OBJ(if_smapi_bat_installed) {
2227 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2228 if(!smapi_bat_installed(idx)) {
2232 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2234 OBJ(smapi_bat_perc) {
2235 print_smapi_bat_perc(obj, p, p_max_size);
2237 OBJ(smapi_bat_temp) {
2238 print_smapi_bat_temp(obj, p, p_max_size);
2240 OBJ(smapi_bat_power) {
2241 print_smapi_bat_power(obj, p, p_max_size);
2243 OBJ(smapi_bat_bar) {
2244 print_smapi_bat_bar(obj, p, p_max_size);
2249 char buf[max_user_text];
2251 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2252 snprintf(p, p_max_size, "%s", buf);
2258 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2259 char buf[max_user_text];
2262 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2263 snprintf(p, p_max_size, "%s", buf);
2264 if(total_updates % 2) {
2265 for(j=0; p[j] != 0; j++) {
2271 char buf[max_user_text];
2273 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2275 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2276 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2277 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2278 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2279 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2280 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2281 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2283 snprintf(p, p_max_size, "%s", buf);
2286 print_scroll(obj, p, p_max_size, cur);
2289 print_combine(obj, p, cur);
2293 print_nvidia_value(obj, display, p, p_max_size);
2298 /* This is just a meta-object to set host:port */
2301 snprintf(p, p_max_size, "%s",
2302 cur->apcupsd.items[APCUPSD_NAME]);
2304 OBJ(apcupsd_model) {
2305 snprintf(p, p_max_size, "%s",
2306 cur->apcupsd.items[APCUPSD_MODEL]);
2308 OBJ(apcupsd_upsmode) {
2309 snprintf(p, p_max_size, "%s",
2310 cur->apcupsd.items[APCUPSD_UPSMODE]);
2312 OBJ(apcupsd_cable) {
2313 snprintf(p, p_max_size, "%s",
2314 cur->apcupsd.items[APCUPSD_CABLE]);
2316 OBJ(apcupsd_status) {
2317 snprintf(p, p_max_size, "%s",
2318 cur->apcupsd.items[APCUPSD_STATUS]);
2320 OBJ(apcupsd_linev) {
2321 snprintf(p, p_max_size, "%s",
2322 cur->apcupsd.items[APCUPSD_LINEV]);
2325 snprintf(p, p_max_size, "%s",
2326 cur->apcupsd.items[APCUPSD_LOAD]);
2328 OBJ(apcupsd_loadbar) {
2330 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2331 new_bar(obj, p, p_max_size, (int) progress);
2334 OBJ(apcupsd_loadgraph) {
2336 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2337 new_graph(obj, p, p_max_size, (int)progress);
2340 OBJ(apcupsd_loadgauge) {
2342 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2343 new_gauge(obj, p, p_max_size, (int)progress);
2345 OBJ(apcupsd_charge) {
2346 snprintf(p, p_max_size, "%s",
2347 cur->apcupsd.items[APCUPSD_CHARGE]);
2349 OBJ(apcupsd_timeleft) {
2350 snprintf(p, p_max_size, "%s",
2351 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2354 snprintf(p, p_max_size, "%s",
2355 cur->apcupsd.items[APCUPSD_TEMP]);
2357 OBJ(apcupsd_lastxfer) {
2358 snprintf(p, p_max_size, "%s",
2359 cur->apcupsd.items[APCUPSD_LASTXFER]);
2361 #endif /* APCUPSD */
2368 size_t a = strlen(p);
2371 iconv_convert(&a, buff_in, p, p_max_size);
2372 #endif /* HAVE_ICONV */
2373 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2375 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2376 #endif /* HAVE_LUA */
2378 substitute_newlines(p, a - 2);
2387 /* load any new fonts we may have had */
2388 if (need_to_load_fonts) {
2394 void evaluate(const char *text, char *p, int p_max_size)
2396 struct information *tmp_info;
2397 struct text_object subroot;
2399 tmp_info = malloc(sizeof(struct information));
2400 memcpy(tmp_info, &info, sizeof(struct information));
2401 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2402 DBGP2("evaluated '%s' to '%s'", text, p);
2404 free_text_objects(&subroot, 1);
2408 double current_update_time, next_update_time, last_update_time;
2410 static void generate_text(void)
2412 struct information *cur = &info;
2414 unsigned int i, j, k;
2420 current_update_time = get_time();
2424 /* add things to the buffer */
2430 generate_text_internal(p, max_user_text, global_root_object, cur);
2431 if(max_text_width > 0) {
2432 for(i = 0, j = 0; p[i] != 0; i++) {
2433 if(p[i] == '\n') j = 0;
2434 else if(j == max_text_width) {
2435 k = i + strlen(p + i) + 1;
2436 if(k < text_buffer_size) {
2443 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2448 if (stuff_in_uppercase) {
2451 tmp_p = text_buffer;
2453 *tmp_p = toupper(*tmp_p);
2458 next_update_time += update_interval;
2459 if (next_update_time < get_time()) {
2460 next_update_time = get_time() + update_interval;
2461 } else if (next_update_time > get_time() + update_interval) {
2462 next_update_time = get_time() + update_interval;
2464 last_update_time = current_update_time;
2468 void set_update_interval(double interval)
2470 update_interval = interval;
2471 update_interval_old = interval;
2474 int get_string_width(const char *s)
2476 return *s ? calc_text_width(s) : 0;
2480 static int get_string_width_special(char *s, int special_index)
2490 if ((output_methods & TO_X) == 0)
2493 p = strndup(s, text_buffer_size);
2497 if (*p == SPECIAL_CHAR) {
2498 /* shift everything over by 1 so that the special char
2499 * doesn't mess up the size calculation */
2500 for (i = 0; i < (long)strlen(p); i++) {
2501 *(p + i) = *(p + i + 1);
2503 if (specials[special_index + idx].type == GRAPH
2504 || specials[special_index + idx].type == GAUGE
2505 || specials[special_index + idx].type == BAR) {
2506 width += specials[special_index + idx].width;
2509 } else if (*p == SECRIT_MULTILINE_CHAR) {
2516 if (strlen(final) > 1) {
2517 width += calc_text_width(final);
2523 static int text_size_updater(char *s, int special_index);
2525 int last_font_height;
2526 static void update_text_area(void)
2530 if ((output_methods & TO_X) == 0)
2532 /* update text size if it isn't fixed */
2537 text_width = minimum_width;
2539 last_font_height = font_height();
2540 for_each_line(text_buffer, text_size_updater);
2542 if (text_height < minimum_height) {
2543 text_height = minimum_height;
2545 if (text_width > maximum_width && maximum_width > 0) {
2546 text_width = maximum_width;
2550 /* get text position on workarea */
2551 switch (text_alignment) {
2552 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2556 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2557 y = workarea[3] - text_height - gap_y;
2560 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2561 y = workarea[3] / 2 - text_height / 2 - gap_y;
2564 switch (text_alignment) {
2565 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2569 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2570 x = workarea[2] - text_width - gap_x;
2573 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2574 x = workarea[2] / 2 - text_width / 2 - gap_x;
2578 if (text_alignment == NONE) { // Let the WM manage the window
2585 #endif /* OWN_WINDOW */
2588 if (own_window && !fixed_pos) {
2591 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2592 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2593 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2594 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2598 /* If window size doesn't match to workarea's size,
2599 * then window probably includes panels (gnome).
2600 * Blah, doesn't work on KDE. */
2601 if (workarea[2] != window.width || workarea[3] != window.height) {
2610 /* update lua window globals */
2611 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2612 #endif /* HAVE_LUA */
2617 static int cur_x, cur_y; /* current x and y for drawing */
2619 //draw_mode also without X11 because we only need to print to stdout with FG
2620 static int draw_mode; /* FG, BG or OUTLINE */
2622 static long current_color;
2624 static int text_size_updater(char *s, int special_index)
2628 int contain_SECRIT_MULTILINE_CHAR = 0;
2631 if ((output_methods & TO_X) == 0)
2633 /* get string widths and skip specials */
2636 if (*p == SPECIAL_CHAR) {
2638 w += get_string_width(s);
2641 if (specials[special_index].type == BAR
2642 || specials[special_index].type == GAUGE
2643 || specials[special_index].type == GRAPH) {
2644 w += specials[special_index].width;
2645 if (specials[special_index].height > last_font_height) {
2646 last_font_height = specials[special_index].height;
2647 last_font_height += font_height();
2649 } else if (specials[special_index].type == OFFSET) {
2650 if (specials[special_index].arg > 0) {
2651 w += specials[special_index].arg;
2653 } else if (specials[special_index].type == VOFFSET) {
2654 last_font_height += specials[special_index].arg;
2655 } else if (specials[special_index].type == GOTO) {
2656 if (specials[special_index].arg > cur_x) {
2657 w = (int) specials[special_index].arg;
2659 } else if (specials[special_index].type == TAB) {
2660 int start = specials[special_index].arg;
2661 int step = specials[special_index].width;
2663 if (!step || step < 0) {
2666 w += step - (cur_x - text_start_x - start) % step;
2667 } else if (specials[special_index].type == FONT) {
2668 selected_font = specials[special_index].font_added;
2669 if (font_height() > last_font_height) {
2670 last_font_height = font_height();
2676 } else if (*p == SECRIT_MULTILINE_CHAR) {
2677 contain_SECRIT_MULTILINE_CHAR = 1;
2679 lw = get_string_width(s);
2680 *p = SECRIT_MULTILINE_CHAR;
2682 w = lw > w ? lw : w;
2683 text_height += last_font_height;
2687 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2688 if (contain_SECRIT_MULTILINE_CHAR) {
2689 lw = get_string_width(s);
2690 w = lw > w ? lw : w;
2692 w += get_string_width(s);
2694 if (w > text_width) {
2697 if (text_width > maximum_width && maximum_width) {
2698 text_width = maximum_width;
2701 text_height += last_font_height;
2702 last_font_height = font_height();
2703 return special_index;
2707 static inline void set_foreground_color(long c)
2710 if (output_methods & TO_X) {
2712 if (have_argb_visual) {
2713 current_color = c | (own_window_argb_value << 24);
2715 #endif /* USE_ARGB */
2719 #endif /* USE_ARGB */
2720 XSetForeground(display, window.gc, current_color);
2724 if (output_methods & TO_NCURSES) {
2725 attron(COLOR_PAIR(c));
2727 #endif /* NCURSES */
2732 static void draw_string(const char *s)
2734 int i, i2, pos, width_of_s;
2737 char *s_with_newlines;
2743 width_of_s = get_string_width(s);
2744 s_with_newlines = strdup(s);
2745 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2746 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2747 s_with_newlines[i] = '\n';
2750 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2751 printf("%s\n", s_with_newlines);
2752 if (extra_newline) fputc('\n', stdout);
2753 fflush(stdout); /* output immediately, don't buffer */
2755 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2756 fprintf(stderr, "%s\n", s_with_newlines);
2757 fflush(stderr); /* output immediately, don't buffer */
2759 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2760 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2762 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2763 fprintf(append_fpointer, "%s\n", s_with_newlines);
2766 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2767 printw("%s", s_with_newlines);
2770 free(s_with_newlines);
2771 memset(tmpstring1, 0, text_buffer_size);
2772 memset(tmpstring2, 0, text_buffer_size);
2773 strncpy(tmpstring1, s, text_buffer_size - 1);
2778 if (output_methods & TO_X) {
2779 max = ((text_width - width_of_s) / get_string_width(" "));
2782 /* This code looks for tabs in the text and coverts them to spaces.
2783 * The trick is getting the correct number of spaces, and not going
2784 * over the window's size without forcing the window larger. */
2785 for (i = 0; i < (int) text_buffer_size; i++) {
2786 if (tmpstring1[i] == '\t') {
2788 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2789 /* guard against overrun */
2790 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2795 /* guard against overrun */
2796 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2801 if (output_methods & TO_X) {
2802 if (text_width == maximum_width) {
2803 /* this means the text is probably pushing the limit,
2804 * so we'll chop it */
2805 while (cur_x + get_string_width(tmpstring2) - text_start_x
2806 > maximum_width && strlen(tmpstring2) > 0) {
2807 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2814 if (output_methods & TO_X) {
2820 c.pixel = current_color;
2821 // query color on custom colormap
2822 XQueryColor(display, window.colourmap, &c);
2825 c2.color.red = c.red;
2826 c2.color.green = c.green;
2827 c2.color.blue = c.blue;
2828 c2.color.alpha = fonts[selected_font].font_alpha;
2830 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2831 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2833 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2834 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2839 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2842 cur_x += width_of_s;
2845 memcpy(tmpstring1, s, text_buffer_size);
2848 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2856 int last_special_needed = -1;
2857 int orig_special_index = special_index;
2860 if (output_methods & TO_X) {
2861 font_h = font_height();
2862 cur_y += font_ascent();
2864 cur_x = text_start_x;
2868 if (*p == SECRIT_MULTILINE_CHAR) {
2869 /* special newline marker for multiline objects */
2874 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2879 /* draw string before special, unless we're dealing multiline
2881 if (last_special_applied > -1) {
2882 special_index = last_special_applied;
2890 switch (specials[special_index].type) {
2892 case HORIZONTAL_LINE:
2894 int h = specials[special_index].height;
2895 int mid = font_ascent() / 2;
2897 w = text_start_x + text_width - cur_x;
2899 XSetLineAttributes(display, window.gc, h, LineSolid,
2900 CapButt, JoinMiter);
2901 XDrawLine(display, window.drawable, window.gc, cur_x,
2902 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2908 int h = specials[special_index].height;
2909 int tmp_s = specials[special_index].arg;
2910 int mid = font_ascent() / 2;
2911 char ss[2] = { tmp_s, tmp_s };
2913 w = text_start_x + text_width - cur_x - 1;
2914 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2915 CapButt, JoinMiter);
2916 XSetDashes(display, window.gc, 0, ss, 2);
2917 XDrawLine(display, window.drawable, window.gc, cur_x,
2918 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2924 int h, bar_usage, by;
2925 if (cur_x - text_start_x > maximum_width
2926 && maximum_width > 0) {
2929 h = specials[special_index].height;
2930 bar_usage = specials[special_index].arg;
2931 by = cur_y - (font_ascent() / 2) - 1;
2936 w = specials[special_index].width;
2938 w = text_start_x + text_width - cur_x - 1;
2944 XSetLineAttributes(display, window.gc, 1, LineSolid,
2945 CapButt, JoinMiter);
2947 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2949 XFillRectangle(display, window.drawable, window.gc, cur_x,
2950 by, w * bar_usage / 255, h);
2958 case GAUGE: /* new GAUGE */
2961 unsigned long last_colour = current_color;
2963 float angle, px, py;
2967 if (cur_x - text_start_x > maximum_width
2968 && maximum_width > 0) {
2972 h = specials[special_index].height;
2973 by = cur_y - (font_ascent() / 2) - 1;
2978 w = specials[special_index].width;
2980 w = text_start_x + text_width - cur_x - 1;
2986 XSetLineAttributes(display, window.gc, 1, LineSolid,
2987 CapButt, JoinMiter);
2989 XDrawArc(display, window.drawable, window.gc,
2990 cur_x, by, w, h * 2, 0, 180*64);
2993 usage = specials[special_index].arg;
2994 angle = (M_PI)*(float)(usage)/255.;
2995 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2996 py = (float)(by+(h))-(float)(h)*sin(angle);
2998 XDrawLine(display, window.drawable, window.gc,
2999 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3007 set_foreground_color(last_colour);
3015 int h, by, i = 0, j = 0;
3017 unsigned long last_colour = current_color;
3018 unsigned long *tmpcolour = 0;
3019 if (cur_x - text_start_x > maximum_width
3020 && maximum_width > 0) {
3023 h = specials[special_index].height;
3024 by = cur_y - (font_ascent() / 2) - 1;
3029 w = specials[special_index].width;
3031 w = text_start_x + text_width - cur_x - 1;
3036 if (draw_graph_borders) {
3037 XSetLineAttributes(display, window.gc, 1, LineSolid,
3038 CapButt, JoinMiter);
3039 XDrawRectangle(display, window.drawable, window.gc,
3042 XSetLineAttributes(display, window.gc, 1, LineSolid,
3043 CapButt, JoinMiter);
3045 if (specials[special_index].last_colour != 0
3046 || specials[special_index].first_colour != 0) {
3047 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3050 for (i = w - 2; i > -1; i--) {
3051 if (specials[special_index].last_colour != 0
3052 || specials[special_index].first_colour != 0) {
3053 if (specials[special_index].tempgrad) {
3056 (int)((float)(w - 2) - specials[special_index].graph[j] *
3057 (w - 2) / (float)specials[special_index].graph_scale)
3061 (int)((float)(w - 2) - specials[special_index].graph[j] *
3062 (w - 2) / (float)specials[special_index].graph_scale)
3065 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3067 (int)((float)(w - 2) - specials[special_index].graph[j] *
3068 (w - 2) / (float)specials[special_index].graph_scale)
3072 #endif /* DEBUG_lol */
3073 XSetForeground(display, window.gc, tmpcolour[
3074 (int)((float)(w - 2) - specials[special_index].graph[j] *
3075 (w - 2) / (float)specials[special_index].graph_scale)
3078 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3081 /* this is mugfugly, but it works */
3082 XDrawLine(display, window.drawable, window.gc,
3083 cur_x + i + 1, by + h, cur_x + i + 1,
3084 round_to_int((double)by + h - specials[special_index].graph[j] *
3085 (h - 1) / specials[special_index].graph_scale));
3086 if ((w - i) / ((float) (w - 2) /
3087 (specials[special_index].graph_width)) > j
3088 && j < MAX_GRAPH_DEPTH - 3) {
3092 if (tmpcolour) free(tmpcolour);
3097 /* if (draw_mode == BG) {
3098 set_foreground_color(default_bg_color);
3099 } else if (draw_mode == OUTLINE) {
3100 set_foreground_color(default_out_color);
3102 set_foreground_color(default_fg_color);
3104 if (show_graph_range) {
3107 unsigned short int seconds = update_interval * w;
3113 unsigned short int timeunits;
3115 timeunits = seconds / 86400; seconds %= 86400;
3116 if (timeunits > 0) {
3117 asprintf(&tmp_day_str, "%dd", timeunits);
3119 tmp_day_str = strdup("");
3121 timeunits = seconds / 3600; seconds %= 3600;
3122 if (timeunits > 0) {
3123 asprintf(&tmp_hour_str, "%dh", timeunits);
3125 tmp_hour_str = strdup("");
3127 timeunits = seconds / 60; seconds %= 60;
3128 if (timeunits > 0) {
3129 asprintf(&tmp_min_str, "%dm", timeunits);
3131 tmp_min_str = strdup("");
3134 asprintf(&tmp_sec_str, "%ds", seconds);
3136 tmp_sec_str = strdup("");
3138 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3139 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3141 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3143 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3144 cur_y += font_h / 2;
3145 draw_string(tmp_str);
3151 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3155 cur_x += font_ascent() / 2;
3156 cur_y += font_h / 2;
3158 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3160 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3161 draw_string(tmp_str);
3167 set_foreground_color(last_colour);
3173 int old = font_ascent();
3175 cur_y -= font_ascent();
3176 selected_font = specials[special_index].font_added;
3178 if (cur_y + font_ascent() < cur_y + old) {
3181 cur_y += font_ascent();
3183 font_h = font_height();
3188 if (draw_mode == FG) {
3189 set_foreground_color(specials[special_index].arg);
3195 if (draw_mode == BG) {
3196 set_foreground_color(specials[special_index].arg);
3201 if (draw_mode == OUTLINE) {
3202 set_foreground_color(specials[special_index].arg);
3207 w += specials[special_index].arg;
3208 last_special_needed = special_index;
3212 cur_y += specials[special_index].arg;
3216 if (specials[special_index].arg >= 0) {
3217 cur_x = (int) specials[special_index].arg;
3219 last_special_needed = special_index;
3224 int start = specials[special_index].arg;
3225 int step = specials[special_index].width;
3227 if (!step || step < 0) {
3230 w = step - (cur_x - text_start_x - start) % step;
3231 last_special_needed = special_index;
3237 /* TODO: add back in "+ window.border_inner_margin" to the end of
3239 int pos_x = text_start_x + text_width -
3240 get_string_width_special(s, special_index);
3242 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3243 "get_string_width(p) %i gap_x %i "
3244 "specials[special_index].arg %i window.border_inner_margin %i "
3245 "window.border_width %i\n", pos_x, text_start_x, text_width,
3246 cur_x, get_string_width_special(s), gap_x,
3247 specials[special_index].arg, window.border_inner_margin,
3248 window.border_width); */
3249 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3250 cur_x = pos_x - specials[special_index].arg;
3252 last_special_needed = special_index;
3258 int pos_x = (text_width) / 2 - get_string_width_special(s,
3259 special_index) / 2 - (cur_x -
3261 /* int pos_x = text_start_x + text_width / 2 -
3262 get_string_width_special(s) / 2; */
3264 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3265 "get_string_width(p) %i gap_x %i "
3266 "specials[special_index].arg %i\n", pos_x, text_start_x,
3267 text_width, cur_x, get_string_width(s), gap_x,
3268 specials[special_index].arg); */
3269 if (pos_x > specials[special_index].arg) {
3270 w = pos_x - specials[special_index].arg;
3272 last_special_needed = special_index;
3282 if (special_index != last_special_applied) {
3285 special_index = orig_special_index;
3286 last_special_applied = -1;
3297 if (output_methods & TO_NCURSES) {
3300 #endif /* NCURSES */
3302 if (output_methods & TO_X)
3303 cur_y += font_descent();
3305 if (recurse && *recurse) {
3306 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3307 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3309 return special_index;
3312 static int draw_line(char *s, int special_index)
3315 if (output_methods & TO_X) {
3316 return draw_each_line_inner(s, special_index, -1);
3320 if (output_methods & TO_NCURSES) {
3321 return draw_each_line_inner(s, special_index, -1);
3323 #endif /* NCURSES */
3325 UNUSED(special_index);
3329 static void draw_text(void)
3333 llua_draw_pre_hook();
3334 #endif /* HAVE_LUA */
3335 if (output_methods & TO_X) {
3336 cur_y = text_start_y;
3339 if (draw_borders && window.border_width > 0) {
3340 if (stippled_borders) {
3341 char ss[2] = { stippled_borders, stippled_borders };
3342 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3343 CapButt, JoinMiter);
3344 XSetDashes(display, window.gc, 0, ss, 2);
3346 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3347 CapButt, JoinMiter);
3350 XDrawRectangle(display, window.drawable, window.gc,
3351 text_start_x - window.border_inner_margin - window.border_width,
3352 text_start_y - window.border_inner_margin - window.border_width,
3353 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3354 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3362 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3363 attron(COLOR_PAIR(COLOR_WHITE));
3364 #endif /* NCURSES */
3365 for_each_line(text_buffer, draw_line);
3366 #if defined(HAVE_LUA) && defined(X11)
3367 llua_draw_post_hook();
3368 #endif /* HAVE_LUA */
3371 static void draw_stuff(void)
3374 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3376 if (overwrite_file) {
3377 overwrite_fpointer = fopen(overwrite_file, "w");
3378 if(!overwrite_fpointer)
3379 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3382 append_fpointer = fopen(append_file, "a");
3383 if(!append_fpointer)
3384 NORM_ERR("Can't append '%s' anymore", append_file);
3387 if (output_methods & TO_X) {
3389 if (draw_shades && !draw_outline) {
3392 set_foreground_color(default_bg_color);
3403 for (i = -1; i < 2; i++) {
3404 for (j = -1; j < 2; j++) {
3405 if (i == 0 && j == 0) {
3410 set_foreground_color(default_out_color);
3411 draw_mode = OUTLINE;
3419 set_foreground_color(default_fg_color);
3424 #if defined(X11) && defined(HAVE_XDBE)
3425 if (output_methods & TO_X) {
3426 xdbe_swap_buffers();
3428 #endif /* X11 && HAVE_XDBE */
3429 if(overwrite_fpointer) {
3430 fclose(overwrite_fpointer);
3431 overwrite_fpointer = 0;
3433 if(append_fpointer) {
3434 fclose(append_fpointer);
3435 append_fpointer = 0;
3440 static void clear_text(int exposures)
3444 /* The swap action is XdbeBackground, which clears */
3448 if (display && window.window) { // make sure these are !null
3449 /* there is some extra space for borders and outlines */
3450 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3451 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3452 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3453 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3458 static int need_to_update;
3460 /* update_text() generates new text and clears old text area */
3461 static void update_text(void)
3468 if (output_methods & TO_X)
3473 llua_update_info(&info, update_interval);
3474 #endif /* HAVE_LUA */
3477 #ifdef HAVE_SYS_INOTIFY_H
3481 static void main_loop(void)
3484 #ifdef SIGNAL_BLOCKING
3485 sigset_t newmask, oldmask;
3488 #ifdef HAVE_SYS_INOTIFY_H
3489 int inotify_config_wd = -1;
3490 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3491 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3492 char inotify_buff[INOTIFY_BUF_LEN];
3493 #endif /* HAVE_SYS_INOTIFY_H */
3496 #ifdef SIGNAL_BLOCKING
3497 sigemptyset(&newmask);
3498 sigaddset(&newmask, SIGINT);
3499 sigaddset(&newmask, SIGTERM);
3500 sigaddset(&newmask, SIGUSR1);
3503 last_update_time = 0.0;
3504 next_update_time = get_time();
3506 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3507 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3508 char buf[max_user_text];
3510 get_battery_short_status(buf, max_user_text, "BAT0");
3512 update_interval = update_interval_bat;
3514 update_interval = update_interval_old;
3519 #ifdef SIGNAL_BLOCKING
3520 /* block signals. we will inspect for pending signals later */
3521 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3522 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3527 if (output_methods & TO_X) {
3530 /* wait for X event or timeout */
3532 if (!XPending(display)) {
3536 t = next_update_time - get_time();
3540 } else if (t > update_interval) {
3541 t = update_interval;
3544 tv.tv_sec = (long) t;
3545 tv.tv_usec = (long) (t * 1000000) % 1000000;
3547 FD_SET(ConnectionNumber(display), &fdsr);
3549 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3551 if (errno != EINTR) {
3552 NORM_ERR("can't select(): %s", strerror(errno));
3562 if (need_to_update) {
3564 int wx = window.x, wy = window.y;
3574 /* resize window if it isn't right size */
3576 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3577 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3578 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3579 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3580 draw_stuff(); /* redraw everything in our newly sized window */
3581 XResizeWindow(display, window.window, window.width,
3582 window.height); /* resize window */
3583 set_transparent_background(window.window, own_window_argb_value);
3586 xdbe_swap_buffers();
3591 /* update lua window globals */
3592 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3593 #endif /* HAVE_LUA */
3596 /* move window if it isn't in right position */
3597 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3598 XMoveWindow(display, window.window, window.x, window.y);
3603 if (changed && window.type == TYPE_PANEL) {
3606 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3609 switch (text_alignment) {
3636 set_struts(sidenum);
3647 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3648 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3649 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3650 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3651 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3656 /* handle X events */
3657 while (XPending(display)) {
3660 XNextEvent(display, &ev);
3667 r.width = ev.xexpose.width;
3668 r.height = ev.xexpose.height;
3669 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3673 case PropertyNotify:
3675 if ( ev.xproperty.state == PropertyNewValue ) {
3676 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3682 case ReparentNotify:
3683 /* make background transparent */
3685 set_transparent_background(window.window, own_window_argb_value);
3689 case ConfigureNotify:
3691 /* if window size isn't what expected, set fixed size */
3692 if (ev.xconfigure.width != window.width
3693 || ev.xconfigure.height != window.height) {
3694 if (window.width != 0 && window.height != 0) {
3698 /* clear old stuff before screwing up
3703 XWindowAttributes attrs;
3704 if (XGetWindowAttributes(display,
3705 window.window, &attrs)) {
3706 window.width = attrs.width;
3707 window.height = attrs.height;
3711 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3712 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3713 if (text_width > maximum_width
3714 && maximum_width > 0) {
3715 text_width = maximum_width;
3719 /* if position isn't what expected, set fixed pos
3720 * total_updates avoids setting fixed_pos when window
3721 * is set to weird locations when started */
3722 /* // this is broken
3723 if (total_updates >= 2 && !fixed_pos
3724 && (window.x != ev.xconfigure.x
3725 || window.y != ev.xconfigure.y)
3726 && (ev.xconfigure.x != 0
3727 || ev.xconfigure.y != 0)) {
3735 /* if an ordinary window with decorations */
3736 if ((window.type == TYPE_NORMAL &&
3737 (!TEST_HINT(window.hints,
3738 HINT_UNDECORATED))) ||
3739 window.type == TYPE_DESKTOP) {
3740 /* allow conky to hold input focus. */
3743 /* forward the click to the desktop window */
3744 XUngrabPointer(display, ev.xbutton.time);
3745 ev.xbutton.window = window.desktop;
3746 ev.xbutton.x = ev.xbutton.x_root;
3747 ev.xbutton.y = ev.xbutton.y_root;
3748 XSendEvent(display, ev.xbutton.window, False,
3749 ButtonPressMask, &ev);
3750 XSetInputFocus(display, ev.xbutton.window,
3751 RevertToParent, ev.xbutton.time);
3758 /* if an ordinary window with decorations */
3759 if ((window.type == TYPE_NORMAL)
3760 && (!TEST_HINT(window.hints,
3761 HINT_UNDECORATED))) {
3762 /* allow conky to hold input focus. */
3765 /* forward the release to the desktop window */
3766 ev.xbutton.window = window.desktop;
3767 ev.xbutton.x = ev.xbutton.x_root;
3768 ev.xbutton.y = ev.xbutton.y_root;
3769 XSendEvent(display, ev.xbutton.window, False,
3770 ButtonReleaseMask, &ev);
3779 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3780 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3782 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3783 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3785 #endif /* HAVE_XDAMAGE */
3791 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3792 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3793 #endif /* HAVE_XDAMAGE */
3795 /* XDBE doesn't seem to provide a way to clear the back buffer
3796 * without interfering with the front buffer, other than passing
3797 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3798 * using XDBE, we need to redraw the text even if it wasn't part of
3799 * the exposed area. OTOH, if we're not going to call draw_stuff at
3800 * all, then no swap happens and we can safely do nothing. */
3802 if (!XEmptyRegion(x11_stuff.region)) {
3807 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3808 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3809 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3810 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3811 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3814 XSetRegion(display, window.gc, x11_stuff.region);
3817 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3821 XDestroyRegion(x11_stuff.region);
3822 x11_stuff.region = XCreateRegion();
3826 t = (next_update_time - get_time()) * 1000000;
3827 if(t > 0) usleep((useconds_t)t);
3831 if(output_methods & TO_NCURSES) {
3840 #ifdef SIGNAL_BLOCKING
3841 /* unblock signals of interest and let handler fly */
3842 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3843 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3847 switch (g_signal_pending) {
3850 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3855 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3858 if (output_methods & TO_X) {
3859 XDestroyRegion(x11_stuff.region);
3860 x11_stuff.region = NULL;
3862 XDamageDestroy(display, x11_stuff.damage);
3863 XFixesDestroyRegion(display, x11_stuff.region2);
3864 XFixesDestroyRegion(display, x11_stuff.part);
3865 #endif /* HAVE_XDAMAGE */
3871 if(overwrite_file) {
3872 free(overwrite_file);
3881 /* Reaching here means someone set a signal
3882 * (SIGXXXX, signal_handler), but didn't write any code
3884 * If you don't want to handle a signal, don't set a handler on
3885 * it in the first place. */
3886 if (g_signal_pending) {
3887 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3891 #ifdef HAVE_SYS_INOTIFY_H
3892 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3893 inotify_config_wd = inotify_add_watch(inotify_fd,
3897 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3898 int len = 0, idx = 0;
3900 struct timeval time_to_wait;
3902 FD_ZERO(&descriptors);
3903 FD_SET(inotify_fd, &descriptors);
3905 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3907 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3908 if (FD_ISSET(inotify_fd, &descriptors)) {
3909 /* process inotify events */
3910 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3911 while (len > 0 && idx < len) {
3912 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3913 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3914 /* current_config should be reloaded */
3915 NORM_ERR("'%s' modified, reloading...", current_config);
3917 if (ev->mask & IN_IGNORED) {
3918 /* for some reason we get IN_IGNORED here
3919 * sometimes, so we need to re-add the watch */
3920 inotify_config_wd = inotify_add_watch(inotify_fd,
3928 llua_inotify_query(ev->wd, ev->mask);
3930 #endif /* HAVE_LUA */
3931 idx += INOTIFY_EVENT_SIZE + ev->len;
3934 } else if (disable_auto_reload && inotify_fd != -1) {
3935 inotify_rm_watch(inotify_fd, inotify_config_wd);
3937 inotify_fd = inotify_config_wd = 0;
3939 #endif /* HAVE_SYS_INOTIFY_H */
3942 llua_update_info(&info, update_interval);
3943 #endif /* HAVE_LUA */
3944 g_signal_pending = 0;
3946 clean_up(NULL, NULL);
3948 #ifdef HAVE_SYS_INOTIFY_H
3949 if (inotify_fd != -1) {
3950 inotify_rm_watch(inotify_fd, inotify_config_wd);
3952 inotify_fd = inotify_config_wd = 0;
3954 #endif /* HAVE_SYS_INOTIFY_H */
3958 static void load_config_file_x11(const char *);
3960 void initialisation(int argc, char** argv);
3962 /* reload the config file */
3963 static void reload_config(void)
3965 char *current_config_copy = strdup(current_config);
3966 clean_up(NULL, NULL);
3967 sleep(1); /* slight pause */
3968 current_config = current_config_copy;
3969 initialisation(argc_copy, argv_copy);
3972 void clean_up(void *memtofree1, void* memtofree2)
3976 free_update_callbacks();
3979 if(output_methods & TO_NCURSES) {
3983 conftree_empty(currentconffile);
3984 currentconffile = NULL;
3991 timed_thread_destroy_registered_threads();
3993 if (info.cpu_usage) {
3994 free(info.cpu_usage);
3995 info.cpu_usage = NULL;
3998 if (x_initialised == YES) {
3999 if(window_created == 1) {
4000 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4001 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4002 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4003 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4007 if(x11_stuff.region) {
4008 XDestroyRegion(x11_stuff.region);
4009 x11_stuff.region = NULL;
4011 XCloseDisplay(display);
4013 if(info.x11.desktop.all_names) {
4014 free(info.x11.desktop.all_names);
4015 info.x11.desktop.all_names = NULL;
4017 if (info.x11.desktop.name) {
4018 free(info.x11.desktop.name);
4019 info.x11.desktop.name = NULL;
4023 free(fonts); //in set_default_configurations a font is set but not loaded
4031 free_text_objects(&global_root_object, 0);
4050 free(current_config);
4053 #ifdef TCP_PORT_MONITOR
4054 tcp_portmon_clear();
4063 weather_free_info();
4066 llua_shutdown_hook();
4068 #endif /* HAVE_LUA */
4070 if (output_methods & TO_X)
4078 for (i = 0; i < special_count; i++) {
4079 if (specials[i].type == GRAPH) {
4080 free(specials[i].graph);
4088 clear_diskio_stats();
4089 if(global_cpu != NULL) {
4095 static int string_to_bool(const char *s)
4098 // Assumes an option without a true/false means true
4100 } else if (strcasecmp(s, "yes") == EQUAL) {
4102 } else if (strcasecmp(s, "true") == EQUAL) {
4104 } else if (strcasecmp(s, "1") == EQUAL) {
4111 static enum alignment string_to_alignment(const char *s)
4113 if (strcasecmp(s, "top_left") == EQUAL) {
4115 } else if (strcasecmp(s, "top_right") == EQUAL) {
4117 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4119 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4121 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4122 return BOTTOM_RIGHT;
4123 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4124 return BOTTOM_MIDDLE;
4125 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4127 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4128 return MIDDLE_RIGHT;
4129 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4130 return MIDDLE_MIDDLE;
4131 } else if (strcasecmp(s, "tl") == EQUAL) {
4133 } else if (strcasecmp(s, "tr") == EQUAL) {
4135 } else if (strcasecmp(s, "tm") == EQUAL) {
4137 } else if (strcasecmp(s, "bl") == EQUAL) {
4139 } else if (strcasecmp(s, "br") == EQUAL) {
4140 return BOTTOM_RIGHT;
4141 } else if (strcasecmp(s, "bm") == EQUAL) {
4142 return BOTTOM_MIDDLE;
4143 } else if (strcasecmp(s, "ml") == EQUAL) {
4145 } else if (strcasecmp(s, "mr") == EQUAL) {
4146 return MIDDLE_RIGHT;
4147 } else if (strcasecmp(s, "mm") == EQUAL) {
4148 return MIDDLE_MIDDLE;
4149 } else if (strcasecmp(s, "none") == EQUAL) {
4157 static void set_default_configurations_for_x(void)
4159 default_fg_color = WhitePixel(display, screen);
4160 default_bg_color = BlackPixel(display, screen);
4161 default_out_color = BlackPixel(display, screen);
4162 color0 = default_fg_color;
4163 color1 = default_fg_color;
4164 color2 = default_fg_color;
4165 color3 = default_fg_color;
4166 color4 = default_fg_color;
4167 color5 = default_fg_color;
4168 color6 = default_fg_color;
4169 color7 = default_fg_color;
4170 color8 = default_fg_color;
4171 color9 = default_fg_color;
4172 current_text_color = default_fg_color;
4176 static void set_default_configurations(void)
4183 fork_to_background = 0;
4184 total_run_times = 0;
4185 info.cpu_avg_samples = 2;
4186 info.net_avg_samples = 2;
4187 info.diskio_avg_samples = 2;
4192 format_human_readable = 1;
4202 mpd_env_host = getenv("MPD_HOST");
4203 mpd_env_port = getenv("MPD_PORT");
4205 if (!mpd_env_host || !strlen(mpd_env_host)) {
4206 mpd_set_host("localhost");
4208 /* MPD_HOST environment variable is set */
4209 char *mpd_hostpart = strchr(mpd_env_host, '@');
4210 if (!mpd_hostpart) {
4211 mpd_set_host(mpd_env_host);
4213 /* MPD_HOST contains a password */
4214 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4215 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4217 if (!strlen(mpd_hostpart + 1)) {
4218 mpd_set_host("localhost");
4220 mpd_set_host(mpd_hostpart + 1);
4223 mpd_set_password(mpd_password, 1);
4228 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4229 /* failed to set port from environment variable */
4230 mpd_set_port("6600");
4234 info.xmms2.artist = NULL;
4235 info.xmms2.album = NULL;
4236 info.xmms2.title = NULL;
4237 info.xmms2.genre = NULL;
4238 info.xmms2.comment = NULL;
4239 info.xmms2.url = NULL;
4240 info.xmms2.status = NULL;
4241 info.xmms2.playlist = NULL;
4243 use_spacer = NO_SPACER;
4245 output_methods = TO_X;
4247 output_methods = TO_STDOUT;
4250 show_graph_scale = 0;
4251 show_graph_range = 0;
4254 draw_graph_borders = 1;
4256 set_first_font("6x10");
4264 window.type = TYPE_NORMAL;
4266 strcpy(window.class_name, PACKAGE_NAME);
4267 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4269 use_argb_visual = 0;
4270 own_window_argb_value = 255;
4273 stippled_borders = 0;
4274 window.border_inner_margin = 3;
4275 window.border_outer_margin = 1;
4276 window.border_width = 1;
4277 text_alignment = BOTTOM_LEFT;
4278 info.x11.monitor.number = 1;
4279 info.x11.monitor.current = 0;
4280 info.x11.desktop.current = 1;
4281 info.x11.desktop.number = 1;
4282 info.x11.desktop.nitems = 0;
4283 info.x11.desktop.all_names = NULL;
4284 info.x11.desktop.name = NULL;
4289 free(current_mail_spool);
4293 variable_substitute(MAIL_FILE, buf, 256);
4294 if (buf[0] != '\0') {
4295 current_mail_spool = strndup(buf, text_buffer_size);
4300 set_update_interval(3);
4301 update_interval_bat = NOBATTERY;
4302 info.music_player_interval = 1.0;
4303 stuff_in_uppercase = 0;
4304 info.users.number = 1;
4306 set_times_in_seconds(0);
4308 #ifdef TCP_PORT_MONITOR
4309 /* set default connection limit */
4310 tcp_portmon_set_max_connections(0);
4314 /* returns 1 if you can overwrite or create the file at 'path' */
4315 static _Bool overwrite_works(const char *path)
4319 if (!(filepointer = fopen(path, "w")))
4321 fclose(filepointer);
4325 /* returns 1 if you can append or create the file at 'path' */
4326 static _Bool append_works(const char *path)
4330 if (!(filepointer = fopen(path, "a")))
4332 fclose(filepointer);
4338 /* WARNING, this type not in Xlib spec */
4339 int x11_error_handler(Display *d, XErrorEvent *err)
4340 __attribute__((noreturn));
4341 int x11_error_handler(Display *d, XErrorEvent *err)
4343 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",
4345 (long unsigned)err->display,
4346 (long)err->resourceid,
4356 int x11_ioerror_handler(Display *d)
4357 __attribute__((noreturn));
4358 int x11_ioerror_handler(Display *d)
4360 NORM_ERR("X Error: Display %lx\n",
4367 static void X11_initialisation(void)
4369 if (x_initialised == YES) return;
4370 output_methods |= TO_X;
4372 set_default_configurations_for_x();
4373 x_initialised = YES;
4376 /* WARNING, this type not in Xlib spec */
4377 XSetErrorHandler(&x11_error_handler);
4378 XSetIOErrorHandler(&x11_ioerror_handler);
4382 static char **xargv = 0;
4383 static int xargc = 0;
4385 static void X11_create_window(void)
4387 if (output_methods & TO_X) {
4389 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4390 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4392 #else /* OWN_WINDOW */
4393 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4394 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4396 #endif /* OWN_WINDOW */
4400 update_text_area(); /* to position text/window on screen */
4403 if (own_window && !fixed_pos) {
4404 XMoveWindow(display, window.window, window.x, window.y);
4407 set_transparent_background(window.window, own_window_argb_value);
4415 x11_stuff.region = XCreateRegion();
4417 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4418 NORM_ERR("Xdamage extension unavailable");
4420 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4421 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4422 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4423 #endif /* HAVE_XDAMAGE */
4426 update_text_area(); /* to get initial size of the window */
4429 /* setup lua window globals */
4430 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4431 #endif /* HAVE_LUA */
4435 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4436 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4437 #define CONF2(a) if (strcasecmp(name, a) == 0)
4438 #define CONF(a) else CONF2(a)
4439 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4440 || strcasecmp(name, b) == 0)
4441 #define CONF_CONTINUE 1
4442 #define CONF_BREAK 2
4443 #define CONF_BUFF_SIZE 512
4445 static FILE *open_config_file(const char *f)
4447 #ifdef CONFIG_OUTPUT
4448 if (!strcmp(f, "==builtin==")) {
4449 return conf_cookie_open();
4451 #endif /* CONFIG_OUTPUT */
4452 return fopen(f, "r");
4455 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4459 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4462 remove_comments(buf);
4467 while (*p && isspace((int) *p)) {
4471 return CONF_CONTINUE; /* empty line */
4478 while (*p2 && !isspace((int) *p2)) {
4482 *p2 = '\0'; /* break at name's end */
4489 while (*p && isspace((int) *p)) {
4495 p2 = *value + strlen(*value);
4496 while (isspace((int) *(p2 - 1))) {
4505 char load_config_file(const char *f)
4510 fp = open_config_file(f);
4514 DBGP("reading contents from config file '%s'", f);
4517 char buff[CONF_BUFF_SIZE], *name, *value;
4518 int ret = do_config_step(&line, fp, buff, &name, &value);
4519 if (ret == CONF_BREAK) {
4521 } else if (ret == CONF_CONTINUE) {
4527 /* don't listen if X is already initialised or
4528 * if we already know we don't want it */
4529 if(x_initialised != YES) {
4530 if (string_to_bool(value)) {
4531 output_methods &= TO_X;
4533 output_methods &= ~TO_X;
4534 x_initialised = NEVER;
4539 if (!value || x_initialised == YES) {
4544 disp = strdup(value);
4549 if (window.type == TYPE_DOCK)
4552 #endif /*OWN_WINDOW */
4554 int a = string_to_alignment(value);
4565 CONF("background") {
4566 fork_to_background = string_to_bool(value);
4569 CONF2("background") {
4570 fork_to_background = string_to_bool(value);
4574 CONF("show_graph_scale") {
4575 show_graph_scale = string_to_bool(value);
4577 CONF("show_graph_range") {
4578 show_graph_range = string_to_bool(value);
4580 CONF("border_inner_margin") {
4582 window.border_inner_margin = strtol(value, 0, 0);
4583 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4588 CONF("border_outer_margin") {
4590 window.border_outer_margin = strtol(value, 0, 0);
4591 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4596 CONF("border_width") {
4598 window.border_width = strtol(value, 0, 0);
4599 if (window.border_width < 0) window.border_width = 0;
4605 #define TEMPLATE_CONF(n) \
4606 CONF("template"#n) { \
4607 if (set_template(n, value)) \
4622 parse_global_imap_mail_args(value);
4629 parse_global_pop3_mail_args(value);
4634 CONF("default_bar_size") {
4637 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4644 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4648 CONF("default_graph_size") {
4651 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4658 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4661 CONF("default_gauge_size") {
4664 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4671 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4678 mpd_set_host(value);
4684 if (value && mpd_set_port(value)) {
4688 CONF("mpd_password") {
4690 mpd_set_password(value, 0);
4696 CONF("music_player_interval") {
4698 info.music_player_interval = strtod(value, 0);
4704 CONF("sensor_device") {
4706 sensor_device = strtol(value, 0, 0);
4712 CONF("cpu_avg_samples") {
4714 cpu_avg_samples = strtol(value, 0, 0);
4715 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4718 info.cpu_avg_samples = cpu_avg_samples;
4724 CONF("net_avg_samples") {
4726 net_avg_samples = strtol(value, 0, 0);
4727 if (net_avg_samples < 1 || net_avg_samples > 14) {
4730 info.net_avg_samples = net_avg_samples;
4736 CONF("diskio_avg_samples") {
4738 diskio_avg_samples = strtol(value, 0, 0);
4739 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4742 info.diskio_avg_samples = diskio_avg_samples;
4750 CONF("double_buffer") {
4751 use_xdbe = string_to_bool(value);
4755 CONF("override_utf8_locale") {
4756 utf8_mode = string_to_bool(value);
4758 CONF("draw_borders") {
4759 draw_borders = string_to_bool(value);
4761 CONF("draw_graph_borders") {
4762 draw_graph_borders = string_to_bool(value);
4764 CONF("draw_shades") {
4765 draw_shades = string_to_bool(value);
4767 CONF("draw_outline") {
4768 draw_outline = string_to_bool(value);
4771 CONF("times_in_seconds") {
4772 set_times_in_seconds(string_to_bool(value));
4774 CONF("max_text_width") {
4775 max_text_width = atoi(value);
4777 CONF("out_to_console") {
4778 if(string_to_bool(value)) {
4779 output_methods |= TO_STDOUT;
4781 output_methods &= ~TO_STDOUT;
4784 CONF("extra_newline") {
4785 extra_newline = string_to_bool(value);
4787 CONF("disable_auto_reload") {
4788 disable_auto_reload = string_to_bool(value);
4790 CONF("out_to_stderr") {
4791 if(string_to_bool(value))
4792 output_methods |= TO_STDERR;
4795 CONF("out_to_ncurses") {
4796 if(string_to_bool(value)) {
4799 output_methods |= TO_NCURSES;
4803 CONF("overwrite_file") {
4804 if(overwrite_file) {
4805 free(overwrite_file);
4808 if(overwrite_works(value)) {
4809 overwrite_file = strdup(value);
4810 output_methods |= OVERWRITE_FILE;
4812 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4814 CONF("append_file") {
4819 if(append_works(value)) {
4820 append_file = strdup(value);
4821 output_methods |= APPEND_FILE;
4823 NORM_ERR("append_file won't be able to create/append '%s'", value);
4825 CONF("use_spacer") {
4827 if (strcasecmp(value, "left") == EQUAL) {
4828 use_spacer = LEFT_SPACER;
4829 } else if (strcasecmp(value, "right") == EQUAL) {
4830 use_spacer = RIGHT_SPACER;
4831 } else if (strcasecmp(value, "none") == EQUAL) {
4832 use_spacer = NO_SPACER;
4834 use_spacer = string_to_bool(value);
4835 NORM_ERR("use_spacer should have an argument of left, right, or"
4836 " none. '%s' seems to be some form of '%s', so"
4837 " defaulting to %s.", value,
4838 use_spacer ? "true" : "false",
4839 use_spacer ? "right" : "none");
4841 use_spacer = RIGHT_SPACER;
4843 use_spacer = NO_SPACER;
4847 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4848 use_spacer = RIGHT_SPACER;
4854 use_xft = string_to_bool(value);
4858 set_first_font(value);
4862 if (value && font_count >= 0) {
4863 fonts[0].font_alpha = atof(value) * 65535.0;
4870 if (string_to_bool(value)) {
4871 NORM_ERR("Xft not enabled at compile time");
4875 /* xftfont silently ignored when no Xft */
4878 /* xftalpha is silently ignored when no Xft */
4883 set_first_font(value);
4891 gap_x = atoi(value);
4898 gap_y = atoi(value);
4904 CONF("mail_spool") {
4908 variable_substitute(value, buffer, 256);
4910 if (buffer[0] != '\0') {
4911 if (current_mail_spool) {
4912 free(current_mail_spool);
4914 current_mail_spool = strndup(buffer, text_buffer_size);
4921 CONF("minimum_size") {
4923 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4925 if (sscanf(value, "%d", &minimum_width) != 1) {
4933 CONF("maximum_width") {
4935 if (sscanf(value, "%d", &maximum_width) != 1) {
4943 CONF("no_buffers") {
4944 no_buffers = string_to_bool(value);
4946 CONF("top_name_width") {
4947 if (set_top_name_width(value))
4950 CONF("top_cpu_separate") {
4951 cpu_separate = string_to_bool(value);
4953 CONF("short_units") {
4954 short_units = string_to_bool(value);
4956 CONF("format_human_readable") {
4957 format_human_readable = string_to_bool(value);
4960 CONF("hddtemp_host") {
4961 set_hddtemp_host(value);
4963 CONF("hddtemp_port") {
4964 set_hddtemp_port(value);
4966 #endif /* HDDTEMP */
4967 CONF("pad_percents") {
4968 pad_percents = atoi(value);
4972 CONF("own_window") {
4974 own_window = string_to_bool(value);
4977 CONF("own_window_class") {
4979 memset(window.class_name, 0, sizeof(window.class_name));
4980 strncpy(window.class_name, value,
4981 sizeof(window.class_name) - 1);
4984 CONF("own_window_title") {
4986 memset(window.title, 0, sizeof(window.title));
4987 strncpy(window.title, value, sizeof(window.title) - 1);
4990 CONF("own_window_transparent") {
4992 set_transparent = string_to_bool(value);
4995 CONF("own_window_hints") {
4997 char *p_hint, *p_save;
4998 char delim[] = ", ";
5000 /* tokenize the value into individual hints */
5001 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5003 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5004 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5005 SET_HINT(window.hints, HINT_UNDECORATED);
5006 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5007 SET_HINT(window.hints, HINT_BELOW);
5008 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5009 SET_HINT(window.hints, HINT_ABOVE);
5010 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5011 SET_HINT(window.hints, HINT_STICKY);
5012 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5013 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5014 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5015 SET_HINT(window.hints, HINT_SKIP_PAGER);
5020 p_hint = strtok_r(NULL, delim, &p_save);
5021 } while (p_hint != NULL);
5027 CONF("own_window_type") {
5029 if (strncmp(value, "normal", 6) == EQUAL) {
5030 window.type = TYPE_NORMAL;
5031 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5032 window.type = TYPE_DESKTOP;
5033 } else if (strncmp(value, "dock", 4) == EQUAL) {
5034 window.type = TYPE_DOCK;
5035 text_alignment = TOP_LEFT;
5036 } else if (strncmp(value, "panel", 5) == EQUAL) {
5037 window.type = TYPE_PANEL;
5038 } else if (strncmp(value, "override", 8) == EQUAL) {
5039 window.type = TYPE_OVERRIDE;
5048 CONF("own_window_argb_visual") {
5049 use_argb_visual = string_to_bool(value);
5051 CONF("own_window_argb_value") {
5052 own_window_argb_value = strtol(value, 0, 0);
5053 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5054 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5057 #endif /* USE_ARGB */
5059 CONF("stippled_borders") {
5061 stippled_borders = strtol(value, 0, 0);
5063 stippled_borders = 4;
5067 CONF("imlib_cache_size") {
5069 cimlib_set_cache_size(atoi(value));
5072 CONF("imlib_cache_flush_interval") {
5074 cimlib_set_cache_flush_interval(atoi(value));
5079 CONF("update_interval_on_battery") {
5081 update_interval_bat = strtod(value, 0);
5086 CONF("update_interval") {
5088 set_update_interval(strtod(value, 0));
5092 if (info.music_player_interval == 0) {
5093 // default to update_interval
5094 info.music_player_interval = update_interval;
5097 CONF("total_run_times") {
5099 total_run_times = strtod(value, 0);
5105 stuff_in_uppercase = string_to_bool(value);
5107 CONF("max_specials") {
5109 max_specials = atoi(value);
5114 CONF("max_user_text") {
5116 max_user_text = atoi(value);
5121 CONF("text_buffer_size") {
5123 text_buffer_size = atoi(value);
5124 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5125 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5126 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5134 if (output_methods & TO_X) {
5135 X11_initialisation();
5144 global_text = (char *) malloc(1);
5145 global_text[0] = '\0';
5148 unsigned int l = strlen(global_text);
5150 char buf[CONF_BUFF_SIZE];
5152 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5158 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5166 /* Check for continuation of \\-\n. */
5167 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5168 global_text[l-1] = '\0';
5172 global_text = (char *) realloc(global_text, l + bl + 1);
5173 strcat(global_text, buf);
5175 if (strlen(global_text) > max_user_text) {
5179 global_text_lines = line + 1;
5182 #ifdef TCP_PORT_MONITOR
5183 CONF("max_port_monitor_connections") {
5185 if (!value || (sscanf(value, "%d", &max) != 1)) {
5186 /* an error. use default, warn and continue. */
5187 tcp_portmon_set_max_connections(0);
5189 } else if (tcp_portmon_set_max_connections(max)) {
5190 /* max is < 0, default has been set*/
5195 CONF("if_up_strictness") {
5197 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5198 ifup_strictness = IFUP_UP;
5199 } else if (strcasecmp(value, "up") == EQUAL) {
5200 ifup_strictness = IFUP_UP;
5201 } else if (strcasecmp(value, "link") == EQUAL) {
5202 ifup_strictness = IFUP_LINK;
5203 } else if (strcasecmp(value, "address") == EQUAL) {
5204 ifup_strictness = IFUP_ADDR;
5206 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5207 ifup_strictness = IFUP_UP;
5211 CONF("temperature_unit") {
5213 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5214 } else if (set_temp_output_unit(value)) {
5215 NORM_ERR("temperature_unit: incorrect argument");
5222 char *ptr = strtok(value, " ");
5225 ptr = strtok(NULL, " ");
5232 CONF("lua_draw_hook_pre") {
5234 llua_set_draw_pre_hook(value);
5239 CONF("lua_draw_hook_post") {
5241 llua_set_draw_post_hook(value);
5246 CONF("lua_startup_hook") {
5248 llua_set_startup_hook(value);
5253 CONF("lua_shutdown_hook") {
5255 llua_set_shutdown_hook(value);
5261 #endif /* HAVE_LUA */
5273 CONF("default_color"){}
5274 CONF3("default_shade_color", "default_shadecolor"){}
5275 CONF3("default_outline_color", "default_outlinecolor") {}
5276 CONF("own_window_colour") {}
5279 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5285 if (info.music_player_interval == 0) {
5286 // default to update_interval
5287 info.music_player_interval = update_interval;
5289 if (!global_text) { // didn't supply any text
5290 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5292 if (!output_methods) {
5293 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5295 #if defined(NCURSES)
5297 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5298 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5299 output_methods &= ~TO_NCURSES;
5303 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5304 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5305 output_methods &= ~(TO_STDOUT | TO_STDERR);
5307 #endif /* NCURSES */
5312 static void load_config_file_x11(const char *f)
5317 fp = open_config_file(f);
5321 DBGP("reading contents from config file '%s'", f);
5324 char buff[CONF_BUFF_SIZE], *name, *value;
5325 int ret = do_config_step(&line, fp, buff, &name, &value);
5326 if (ret == CONF_BREAK) {
5328 } else if (ret == CONF_CONTINUE) {
5333 X11_initialisation();
5334 if (x_initialised == YES) {
5336 color0 = get_x11_color(value);
5343 X11_initialisation();
5344 if (x_initialised == YES) {
5346 color1 = get_x11_color(value);
5353 X11_initialisation();
5354 if (x_initialised == YES) {
5356 color2 = get_x11_color(value);
5363 X11_initialisation();
5364 if (x_initialised == YES) {
5366 color3 = get_x11_color(value);
5373 X11_initialisation();
5374 if (x_initialised == YES) {
5376 color4 = get_x11_color(value);
5383 X11_initialisation();
5384 if (x_initialised == YES) {
5386 color5 = get_x11_color(value);
5393 X11_initialisation();
5394 if (x_initialised == YES) {
5396 color6 = get_x11_color(value);
5403 X11_initialisation();
5404 if (x_initialised == YES) {
5406 color7 = get_x11_color(value);
5413 X11_initialisation();
5414 if (x_initialised == YES) {
5416 color8 = get_x11_color(value);
5423 X11_initialisation();
5424 if (x_initialised == YES) {
5426 color9 = get_x11_color(value);
5432 CONF("default_color") {
5433 X11_initialisation();
5434 if (x_initialised == YES) {
5436 default_fg_color = get_x11_color(value);
5442 CONF3("default_shade_color", "default_shadecolor") {
5443 X11_initialisation();
5444 if (x_initialised == YES) {
5446 default_bg_color = get_x11_color(value);
5452 CONF3("default_outline_color", "default_outlinecolor") {
5453 X11_initialisation();
5454 if (x_initialised == YES) {
5456 default_out_color = get_x11_color(value);
5463 CONF("own_window_colour") {
5464 X11_initialisation();
5465 if (x_initialised == YES) {
5467 background_colour = get_x11_color(value);
5469 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5470 "'#' for hex colours");
5476 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5477 if(output_methods & TO_X) {
5478 X11_initialisation();
5487 #undef CONF_CONTINUE
5488 #undef CONF_BUFF_SIZE
5496 static void print_help(const char *prog_name) {
5497 printf("Usage: %s [OPTION]...\n"
5498 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5499 "window. Command line options will override configurations defined in config\n"
5501 " -v, --version version\n"
5502 " -q, --quiet quiet mode\n"
5503 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5504 " -c, --config=FILE config file to load\n"
5505 #ifdef CONFIG_OUTPUT
5506 " -C, --print-config print the builtin default config to stdout\n"
5507 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5509 " -d, --daemonize daemonize, fork to background\n"
5510 " -h, --help help\n"
5512 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5513 " -f, --font=FONT font to use\n"
5514 " -X, --display=DISPLAY X11 display to use\n"
5516 " -o, --own-window create own window to draw\n"
5519 " -b, --double-buffer double buffer (prevents flickering)\n"
5521 " -w, --window-id=WIN_ID window id to draw\n"
5522 " -x X x position\n"
5523 " -y Y y position\n"
5525 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5526 " -u, --interval=SECS update interval\n"
5527 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5528 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5533 /* : means that character before that takes an argument */
5534 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5544 #ifdef CONFIG_OUTPUT
5549 static const struct option longopts[] = {
5550 { "help", 0, NULL, 'h' },
5551 { "version", 0, NULL, 'V' },
5552 { "debug", 0, NULL, 'D' },
5553 { "config", 1, NULL, 'c' },
5554 #ifdef CONFIG_OUTPUT
5555 { "print-config", 0, NULL, 'C' },
5557 { "daemonize", 0, NULL, 'd' },
5559 { "alignment", 1, NULL, 'a' },
5560 { "font", 1, NULL, 'f' },
5561 { "display", 1, NULL, 'X' },
5563 { "own-window", 0, NULL, 'o' },
5566 { "double-buffer", 0, NULL, 'b' },
5568 { "window-id", 1, NULL, 'w' },
5570 { "text", 1, NULL, 't' },
5571 { "interval", 0, NULL, 'u' },
5572 { "pause", 0, NULL, 'p' },
5576 void initialisation(int argc, char **argv) {
5577 struct sigaction act, oact;
5579 set_default_configurations();
5580 load_config_file(current_config);
5581 currentconffile = conftree_add(currentconffile, current_config);
5583 /* init specials array */
5584 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5585 NORM_ERR("failed to create specials array");
5589 if (current_mail_spool == NULL) {
5592 variable_substitute(MAIL_FILE, buf, 256);
5594 if (buf[0] != '\0') {
5595 current_mail_spool = strndup(buf, text_buffer_size);
5600 /* handle other command line arguments */
5602 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5603 || defined(__NetBSD__)
5604 optind = optreset = 1;
5609 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5610 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5611 "kvm_open")) == NULL) {
5612 CRIT_ERR(NULL, NULL, "cannot read kvm");
5617 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5626 fork_to_background = 1;
5629 global_debug_level++;
5633 set_first_font(optarg);
5636 text_alignment = string_to_alignment(optarg);
5655 global_text = strndup(optarg, max_user_text);
5656 convert_escapes(global_text);
5660 update_interval = strtod(optarg, 0);
5661 update_interval_old = update_interval;
5662 if (info.music_player_interval == 0) {
5663 // default to update_interval
5664 info.music_player_interval = update_interval;
5669 total_run_times = strtod(optarg, 0);
5673 gap_x = atoi(optarg);
5677 gap_y = atoi(optarg);
5681 startup_pause = atoi(optarg);
5682 sleep(startup_pause);
5692 if (output_methods & TO_X) {
5693 load_config_file_x11(current_config);
5697 /* generate text and get initial size */
5698 extract_variable_text(global_text);
5705 if (fork_to_background) {
5710 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5717 fprintf(stderr, "\n");
5722 /* parent process */
5723 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5730 start_update_threading();
5732 text_buffer = malloc(max_user_text);
5733 memset(text_buffer, 0, max_user_text);
5734 tmpstring1 = malloc(text_buffer_size);
5735 memset(tmpstring1, 0, text_buffer_size);
5736 tmpstring2 = malloc(text_buffer_size);
5737 memset(tmpstring2, 0, text_buffer_size);
5742 X11_create_window();
5745 llua_setup_info(&info, update_interval);
5746 #endif /* HAVE_LUA */
5751 /* Set signal handlers */
5752 act.sa_handler = signal_handler;
5753 sigemptyset(&act.sa_mask);
5756 act.sa_flags |= SA_RESTART;
5759 if ( sigaction(SIGINT, &act, &oact) < 0
5760 || sigaction(SIGALRM, &act, &oact) < 0
5761 || sigaction(SIGUSR1, &act, &oact) < 0
5762 || sigaction(SIGHUP, &act, &oact) < 0
5763 || sigaction(SIGTERM, &act, &oact) < 0) {
5764 NORM_ERR("error setting signal handler: %s", strerror(errno));
5768 llua_startup_hook();
5769 #endif /* HAVE_LUA */
5772 int main(int argc, char **argv)
5781 g_signal_pending = 0;
5782 max_user_text = MAX_USER_TEXT_DEFAULT;
5784 memset(&info, 0, sizeof(info));
5788 #ifdef TCP_PORT_MONITOR
5789 /* set default connection limit */
5790 tcp_portmon_set_max_connections(0);
5793 /* handle command line parameters that don't change configs */
5795 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5796 || ((s = getenv("LANG")) && *s)) {
5797 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5799 NORM_ERR("malloc failed");
5801 for (x = 0; x < strlen(s); x++) {
5802 temp[x] = tolower(s[x]);
5805 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5811 if (!setlocale(LC_CTYPE, "")) {
5812 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5816 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5827 if (current_config) {
5828 free(current_config);
5830 current_config = strndup(optarg, max_user_text);
5833 freopen("/dev/null", "w", stderr);
5836 print_help(argv[0]);
5838 #ifdef CONFIG_OUTPUT
5845 window.window = strtol(optarg, 0, 0);
5850 disp = strdup(optarg);
5859 /* check if specified config file is valid */
5860 if (current_config) {
5862 if (stat(current_config, &sb) ||
5863 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5864 NORM_ERR("invalid configuration file '%s'\n", current_config);
5865 free(current_config);
5870 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5872 if (!current_config) {
5873 /* load default config file */
5874 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5877 /* Try to use personal config file first */
5878 to_real_path(buf, CONFIG_FILE);
5879 if (buf[0] && (fp = fopen(buf, "r"))) {
5880 current_config = strndup(buf, max_user_text);
5884 /* Try to use system config file if personal config not readable */
5885 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5886 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5890 /* No readable config found */
5891 if (!current_config) {
5892 #ifdef CONFIG_OUTPUT
5893 current_config = strdup("==builtin==");
5894 NORM_ERR("no readable personal or system-wide config file found,"
5895 " using builtin default");
5897 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5898 #endif /* ! CONF_OUTPUT */
5903 /* Load xoap keys, if existing */
5907 #ifdef HAVE_SYS_INOTIFY_H
5908 inotify_fd = inotify_init1(IN_NONBLOCK);
5909 #endif /* HAVE_SYS_INOTIFY_H */
5911 initialisation(argc, argv);
5915 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5923 void alarm_handler(void) {
5925 kill(childpid, SIGTERM);
5929 static void signal_handler(int sig)
5931 /* signal handler is light as a feather, as it should be.
5932 * we will poll g_signal_pending with each loop of conky
5933 * and do any signal processing there, NOT here (except
5934 * SIGALRM because this is caused when conky is hanging) */
5935 if(sig == SIGALRM) {
5938 g_signal_pending = sig;