1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* two strings for internal use */
151 static char *tmpstring1, *tmpstring2;
153 /* variables holding various config settings */
155 int format_human_readable;
162 int top_cpu, top_mem, top_time;
169 static unsigned int top_name_width = 15;
171 char times_in_seconds = 0;
172 static int extra_newline;
173 enum x_initialiser_state x_initialised = NO;
174 static volatile int g_signal_pending;
175 /* Update interval */
176 double update_interval;
177 double update_interval_old;
178 double update_interval_bat;
179 void *global_cpu = NULL;
184 /* prototypes for internally used functions */
185 static void signal_handler(int);
186 static void print_version(void) __attribute__((noreturn));
187 static void reload_config(void);
189 static void print_version(void)
191 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
193 printf("\nCompiled in features:\n\n"
194 "System config file: "SYSTEM_CONFIG_FILE"\n"
195 "Package library path: "PACKAGE_LIBDIR"\n\n"
199 " * Xdamage extension\n"
200 # endif /* HAVE_XDAMAGE */
202 " * XDBE (double buffer extension)\n"
203 # endif /* HAVE_XDBE */
208 "\n Music detection:\n"
211 #endif /* AUDACIOUS */
227 #endif /* HAVE_OPENMP */
234 #ifdef TCP_PORT_MONITOR
236 #endif /* TCP_PORT_MONITOR */
239 #endif /* HAVE_CURL */
244 " * Weather (METAR)\n"
246 " * Weather (XOAP)\n"
251 #endif /* HAVE_IWLIB */
253 " * support for IBM/Lenovo notebooks\n"
263 #endif /* CONFIG_OUTPUT */
268 " * ALSA mixer support\n"
269 #endif /* MIXER_IS_ALSA */
282 #ifdef HAVE_LUA_CAIRO
284 #endif /* HAVE_LUA_CAIRO */
285 #ifdef HAVE_LUA_IMLIB2
288 #endif /* HAVE_LUA */
294 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
299 static void X11_create_window(void);
300 static void X11_initialisation(void);
302 struct _x11_stuff_s {
306 XserverRegion region2, part;
307 int event_base, error_base;
313 static int text_start_x, text_start_y; /* text start position in window */
314 static int text_width, text_height;
330 /* display to connect to */
331 static char *disp = NULL;
335 /* struct that has all info to be shared between
336 * instances of the same text object */
337 struct information info;
339 /* path to config file */
340 char *current_config;
342 /* set to 1 if you want all text to be in uppercase */
343 static unsigned int stuff_in_uppercase;
345 /* Run how many times? */
346 static unsigned long total_run_times;
349 static int fork_to_background;
351 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
353 /* filenames for output */
354 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
355 char *append_file = NULL; FILE *append_fpointer = NULL;
359 static int show_graph_scale;
360 static int show_graph_range;
362 /* Position on the screen */
363 static int text_alignment;
364 static int gap_x, gap_y;
367 static int draw_borders;
368 static int draw_graph_borders;
369 static int stippled_borders;
371 int get_stippled_borders(void)
373 return stippled_borders;
376 static int draw_shades, draw_outline;
378 long default_fg_color, default_bg_color, default_out_color;
380 /* create own window or draw stuff to root? */
381 static int set_transparent = 0;
384 static int own_window = 0;
385 static int background_colour = 0;
387 /* fixed size/pos is set if wm/user changes them */
388 static int fixed_size = 0, fixed_pos = 0;
391 static int minimum_width, minimum_height;
392 static int maximum_width;
397 static int sensor_device;
400 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
403 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
404 unsigned int max_user_text;
406 /* maximum size of individual text buffers, ie $exec buffer size */
407 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
412 /* no buffers in used memory? */
415 /* pad percentages to decimals? */
416 static int pad_percents = 0;
418 static char *global_text = 0;
420 char *get_global_text(void)
425 long global_text_lines;
427 static int total_updates;
428 static int updatereset;
430 void set_updatereset(int i)
435 int get_updatereset(void)
440 int check_contains(char *f, char *s)
443 FILE *where = open_file(f, 0);
448 while (fgets(buf1, 256, where)) {
449 if (strstr(buf1, s)) {
456 NORM_ERR("Could not open the file");
461 #define SECRIT_MULTILINE_CHAR '\x02'
463 static inline int calc_text_width(const char *s)
465 size_t slen = strlen(s);
468 if ((output_methods & TO_X) == 0) {
478 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
479 (const FcChar8 *) s, slen, &gi);
481 XftTextExtents8(display, fonts[selected_font].xftfont,
482 (const FcChar8 *) s, slen, &gi);
488 return XTextWidth(fonts[selected_font].font, s, slen);
493 /* formatted text to render on screen, generated in generate_text(),
494 * drawn in draw_stuff() */
496 static char *text_buffer;
498 /* quite boring functions */
500 static inline void for_each_line(char *b, int f(char *, int))
503 int special_index = 0; /* specials index */
505 for (ps = b, pe = b; *pe; pe++) {
508 special_index = f(ps, special_index);
515 f(ps, special_index);
519 static void convert_escapes(char *buf)
521 char *p = buf, *s = buf;
528 } else if (*s == '\\') {
539 /* Prints anything normally printed with snprintf according to the current value
540 * of use_spacer. Actually slightly more flexible than snprintf, as you can
541 * safely specify the destination buffer as one of your inputs. */
542 int spaced_print(char *buf, int size, const char *format, int width, ...)
551 tempbuf = malloc(size * sizeof(char));
553 // Passes the varargs along to vsnprintf
554 va_start(argp, width);
555 vsnprintf(tempbuf, size, format, argp);
558 switch (use_spacer) {
560 len = snprintf(buf, size, "%s", tempbuf);
563 len = snprintf(buf, size, "%*s", width, tempbuf);
566 len = snprintf(buf, size, "%-*s", width, tempbuf);
573 /* print percentage values
575 * - i.e., unsigned values between 0 and 100
576 * - respect the value of pad_percents */
577 int percent_print(char *buf, int size, unsigned value)
579 return spaced_print(buf, size, "%u", pad_percents, value);
582 /* converts from bytes to human readable format (K, M, G, T)
584 * The algorithm always divides by 1024, as unit-conversion of byte
585 * counts suggests. But for output length determination we need to
586 * compare with 1000 here, as we print in decimal form. */
587 void human_readable(long long num, char *buf, int size)
589 const char **suffix = suffixes;
595 /* Possibly just output as usual, for example for stdout usage */
596 if (!format_human_readable) {
597 spaced_print(buf, size, "%d", 6, round_to_int(num));
608 if (llabs(num) < 1000LL) {
609 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
613 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
621 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
623 * The goal is to always have a significance of 3, by
624 * adjusting the decimal part of the number. Sample output:
628 * so the point of alignment resides between number and unit. The
629 * upside of this is that there is minimal padding necessary, though
630 * there should be a way to make alignment take place at the decimal
631 * dot (then with fixed width decimal part).
633 * Note the repdigits below: when given a precision value, printf()
634 * rounds the float to it, not just cuts off the remaining digits. So
635 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
636 * printed with a precision of 0. Yay. */
638 precision = 0; /* print 100-999 without decimal part */
640 precision = 1; /* print 10-99 with one decimal place */
642 precision = 2; /* print 0-9 with two decimal places */
644 spaced_print(buf, size, format, width, precision, fnum, *suffix);
647 /* global object list root element */
648 static struct text_object global_root_object;
650 static long current_text_color;
652 void set_current_text_color(long colour)
654 current_text_color = colour;
657 long get_current_text_color(void)
659 return current_text_color;
662 //adds newstring to to the tree unless you can already see it when travelling back.
663 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
664 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
665 struct conftree* node;
666 struct conftree* node2;
668 for(node = previous; node != NULL; node = node->back) {
669 if(strcmp(node->string, newstring) == 0) {
673 node = malloc(sizeof(struct conftree));
674 if (previous != NULL) {
675 if(previous->vert_next == NULL) {
676 previous->vert_next = node;
678 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
679 node2->horz_next = node;
682 node->string = strdup(newstring);
683 node->horz_next = NULL;
684 node->vert_next = NULL;
685 node->back = previous;
689 void conftree_empty(struct conftree* tree) {
691 conftree_empty(tree->horz_next);
692 conftree_empty(tree->vert_next);
698 struct conftree *currentconffile;
700 static void extract_variable_text(const char *p)
702 free_text_objects(&global_root_object, 0);
716 extract_variable_text_internal(&global_root_object, p);
719 void parse_conky_vars(struct text_object *root, const char *txt,
720 char *p, int p_max_size, struct information *cur)
722 extract_variable_text_internal(root, txt);
723 generate_text_internal(p, p_max_size, *root, cur);
726 static inline void format_media_player_time(char *buf, const int size,
729 int days, hours, minutes;
731 days = seconds / (24 * 60 * 60);
732 seconds %= (24 * 60 * 60);
733 hours = seconds / (60 * 60);
734 seconds %= (60 * 60);
735 minutes = seconds / 60;
739 snprintf(buf, size, "%i days %i:%02i:%02i", days,
740 hours, minutes, seconds);
741 } else if (hours > 0) {
742 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
745 snprintf(buf, size, "%i:%02i", minutes, seconds);
749 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
750 * multiline objects like $exec work with $align[rc] and friends
752 void substitute_newlines(char *p, long l)
756 while (p && *p && p < s + l) {
758 /* only substitute if it's not the last newline */
759 *p = SECRIT_MULTILINE_CHAR;
765 void generate_text_internal(char *p, int p_max_size,
766 struct text_object root, struct information *cur)
768 struct text_object *obj;
770 int need_to_load_fonts = 0;
773 /* for the OBJ_top* handler */
774 struct process **needed = 0;
777 char buff_in[p_max_size];
779 #endif /* HAVE_ICONV */
783 while (obj && p_max_size > 0) {
784 needed = 0; /* reset for top stuff */
786 /* IFBLOCK jumping algorithm
788 * This is easier as it looks like:
789 * - each IF checks it's condition
790 * - on FALSE: call DO_JUMP
791 * - on TRUE: don't care
792 * - each ELSE calls DO_JUMP unconditionally
793 * - each ENDIF is silently being ignored
796 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
797 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
798 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
799 * regularly, "obj" is being updated to point to obj->next, so object parsing
800 * continues right after the corresponding ELSE or ENDIF. This means that if we
801 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
802 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
803 * jumped, and there is nothing to do.
807 if (obj->ifblock_next) \
808 obj = obj->ifblock_next; \
811 #define OBJ(a) break; case OBJ_##a:
815 NORM_ERR("not implemented obj type %d", obj->type);
817 print_read_tcp(obj, p, p_max_size);
821 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
823 #endif /* !__OpenBSD__ */
827 ok = get_freq(p, p_max_size, "%.0f", 1,
835 ok = get_freq(p, p_max_size, "%'.2f", 1000,
838 /* OpenBSD has no such flag (SUSv2) */
839 ok = get_freq(p, p_max_size, "%.2f", 1000,
841 #endif /* __OpenBSD */
844 #if defined(__linux__)
848 ok = get_voltage(p, p_max_size, "%.0f", 1,
855 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
861 OBJ(wireless_essid) {
862 print_wireless_essid(obj, p, p_max_size);
865 print_wireless_mode(obj, p, p_max_size);
867 OBJ(wireless_bitrate) {
868 print_wireless_bitrate(obj, p, p_max_size);
871 print_wireless_ap(obj, p, p_max_size);
873 OBJ(wireless_link_qual) {
874 print_wireless_link_qual(obj, p, p_max_size);
876 OBJ(wireless_link_qual_max) {
877 print_wireless_link_qual_max(obj, p, p_max_size);
879 OBJ(wireless_link_qual_perc) {
880 print_wireless_link_qual_perc(obj, p, p_max_size);
882 OBJ(wireless_link_bar) {
883 print_wireless_link_bar(obj, p, p_max_size);
885 #endif /* HAVE_IWLIB */
887 #endif /* __linux__ */
891 get_acpi_fan(p, p_max_size);
894 get_acpi_ac_adapter(p, p_max_size);
897 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
900 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
902 OBJ(battery_percent) {
903 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
907 if(output_methods & TO_X) {
908 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
911 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
914 get_battery_short_status(p, p_max_size, obj->data.s);
916 #endif /* __OpenBSD__ */
919 human_readable(cur->buffers * 1024, p, 255);
922 human_readable(cur->cached * 1024, p, 255);
924 OBJ(cmdline_to_pid) {
925 print_cmdline_to_pid(obj, p, p_max_size);
928 if (obj->data.i > info.cpu_count) {
929 NORM_ERR("obj->data.i %i info.cpu_count %i",
930 obj->data.i, info.cpu_count);
931 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
933 percent_print(p, p_max_size,
934 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
938 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
942 if(output_methods & TO_X) {
943 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
946 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
950 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
953 print_loadgraph(obj, p);
957 new_fg(p, obj->data.l);
992 snprintf(p, p_max_size, "%s", VERSION);
994 OBJ(conky_build_date) {
995 snprintf(p, p_max_size, "%s", BUILD_DATE);
997 OBJ(conky_build_arch) {
998 snprintf(p, p_max_size, "%s", BUILD_ARCH);
1000 #if defined(__linux__)
1002 snprintf(p, p_max_size, "%s",
1003 get_disk_protect_queue(obj->data.s));
1006 print_i8k_version(obj, p, p_max_size);
1009 print_i8k_bios(obj, p, p_max_size);
1012 print_i8k_serial(obj, p, p_max_size);
1015 print_i8k_cpu_temp(obj, p, p_max_size);
1017 OBJ(i8k_left_fan_status) {
1018 print_i8k_left_fan_status(obj, p, p_max_size);
1020 OBJ(i8k_right_fan_status) {
1021 print_i8k_right_fan_status(obj, p, p_max_size);
1023 OBJ(i8k_left_fan_rpm) {
1024 print_i8k_left_fan_rpm(obj, p, p_max_size);
1026 OBJ(i8k_right_fan_rpm) {
1027 print_i8k_right_fan_rpm(obj, p, p_max_size);
1029 OBJ(i8k_ac_status) {
1030 print_i8k_ac_status(obj, p, p_max_size);
1032 OBJ(i8k_buttons_status) {
1033 print_i8k_buttons_status(obj, p, p_max_size);
1037 get_ibm_acpi_fan(p, p_max_size);
1040 print_ibm_temps(obj, p, p_max_size);
1043 get_ibm_acpi_volume(p, p_max_size);
1045 OBJ(ibm_brightness) {
1046 get_ibm_acpi_brightness(p, p_max_size);
1049 /* information from sony_laptop kernel module
1050 * /sys/devices/platform/sony-laptop */
1051 OBJ(sony_fanspeed) {
1052 get_sony_fanspeed(p, p_max_size);
1055 if (!gateway_exists()) {
1060 print_gateway_iface(p, p_max_size);
1063 print_gateway_ip(p, p_max_size);
1066 snprintf(p, p_max_size, "%d", get_laptop_mode());
1069 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1071 #endif /* __linux__ */
1072 #if (defined(__FreeBSD__) || defined(__linux__))
1074 if (!interface_up(obj)) {
1080 OBJ(obsd_sensors_temp) {
1081 print_obsd_sensors_temp(obj, p, p_max_size);
1083 OBJ(obsd_sensors_fan) {
1084 print_obsd_sensors_fan(obj, p, p_max_size);
1086 OBJ(obsd_sensors_volt) {
1087 print_obsd_sensors_volt(obj, p, p_max_size);
1090 get_obsd_vendor(p, p_max_size);
1093 get_obsd_product(p, p_max_size);
1095 #endif /* __OpenBSD__ */
1098 new_font(p, obj->data.s);
1099 need_to_load_fonts = 1;
1103 print_diskio(obj, 0, p, p_max_size);
1106 print_diskio(obj, 1, p, p_max_size);
1109 print_diskio(obj, -1, p, p_max_size);
1113 print_diskiograph(obj, 0, p);
1115 OBJ(diskiograph_read) {
1116 print_diskiograph(obj, -1, p);
1118 OBJ(diskiograph_write) {
1119 print_diskiograph(obj, 1, p);
1123 print_downspeed(obj, p, p_max_size);
1126 print_downspeedf(obj, p, p_max_size);
1129 OBJ(downspeedgraph) {
1130 print_downspeedgraph(obj, p);
1134 /* Since we see you, you're if has not jumped.
1135 * Do Ninja jump here: without leaving traces.
1136 * This is to prevent us from stale jumped flags.
1138 obj = obj->ifblock_next;
1142 /* harmless object, just ignore */
1145 print_addr(obj, p, p_max_size);
1147 #if defined(__linux__)
1149 print_addrs(obj, p, p_max_size);
1151 #endif /* __linux__ */
1152 #if defined(IMLIB2) && defined(X11)
1154 /* doesn't actually draw anything, just queues it omp. the
1155 * image will get drawn after the X event loop */
1156 cimlib_add_image(obj->data.s);
1160 evaluate(obj->data.s, p, p_max_size);
1163 print_exec(obj, p, p_max_size);
1166 print_execp(obj, p, p_max_size);
1170 print_execgauge(obj, p, p_max_size);
1174 print_execbar(obj, p, p_max_size);
1178 print_execgraph(obj, p, p_max_size);
1182 print_execibar(obj, p, p_max_size);
1186 print_execigraph(obj, p, p_max_size);
1189 print_execigauge(obj, p, p_max_size);
1193 print_execi(obj, p, p_max_size);
1196 print_execpi(obj, p, p_max_size);
1199 print_texeci(obj, p, p_max_size);
1202 print_imap_unseen(obj, p, p_max_size);
1204 OBJ(imap_messages) {
1205 print_imap_messages(obj, p, p_max_size);
1208 print_pop3_unseen(obj, p, p_max_size);
1211 print_pop3_used(obj, p, p_max_size);
1214 print_fs_bar(obj, 0, p, p_max_size);
1217 print_fs_free(obj, p, p_max_size);
1220 print_fs_perc(obj, 1, p, p_max_size);
1223 print_fs_size(obj, p, p_max_size);
1226 print_fs_type(obj, p, p_max_size);
1229 print_fs_used(obj, p, p_max_size);
1232 print_fs_bar(obj, 1, p, p_max_size);
1235 print_fs_perc(obj, 0, p, p_max_size);
1238 print_loadavg(obj, p, p_max_size);
1241 new_goto(p, obj->data.i);
1248 new_hr(p, obj->data.i);
1252 print_nameserver(obj, p, p_max_size);
1256 print_eve(obj, p, p_max_size);
1261 curl_print(obj, p, p_max_size);
1266 rss_print_info(obj, p, p_max_size);
1271 print_weather(obj, p, p_max_size);
1275 OBJ(weather_forecast) {
1276 print_weather_forecast(obj, p, p_max_size);
1281 print_lua(obj, p, p_max_size);
1284 print_lua_parse(obj, p, p_max_size);
1287 print_lua_bar(obj, p, p_max_size);
1291 print_lua_graph(obj, p, p_max_size);
1294 print_lua_gauge(obj, p, p_max_size);
1297 #endif /* HAVE_LUA */
1303 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1304 snprintf(p, p_max_size, "N/A");
1306 temp_print(p, p_max_size, (double)val,
1307 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1312 new_offset(p, obj->data.i);
1315 new_voffset(p, obj->data.i);
1319 print_sysfs_sensor(obj, p, p_max_size);
1322 print_sysfs_sensor(obj, p, p_max_size);
1325 print_sysfs_sensor(obj, p, p_max_size);
1327 #endif /* __linux__ */
1329 new_alignr(p, obj->data.i);
1332 new_alignc(p, obj->data.i);
1335 char buf[max_user_text];
1336 struct information *tmp_info =
1337 malloc(sizeof(struct information));
1338 memcpy(tmp_info, cur, sizeof(struct information));
1339 generate_text_internal(buf, max_user_text,
1340 *obj->sub, tmp_info);
1342 if (strlen(buf) != 0) {
1348 char expression[max_user_text];
1350 struct information *tmp_info;
1352 tmp_info = malloc(sizeof(struct information));
1353 memcpy(tmp_info, cur, sizeof(struct information));
1354 generate_text_internal(expression, max_user_text,
1355 *obj->sub, tmp_info);
1356 DBGP("parsed arg into '%s'", expression);
1358 val = compare(expression);
1360 NORM_ERR("compare failed for expression '%s'",
1370 spc = strchr(obj->data.s, ' ');
1371 if (!spc && access(obj->data.s, F_OK)) {
1375 if (check_contains(obj->data.s, spc + 1))
1382 && (!check_mount(obj->data.s))) {
1388 if (!get_process_by_name(obj->data.s)) {
1390 if ((obj->data.s) && system(obj->data.s)) {
1395 #if defined(__linux__)
1397 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1401 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1404 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1409 human_readable(cur->mem * 1024, p, 255);
1412 human_readable(cur->memeasyfree * 1024, p, 255);
1415 human_readable(cur->memfree * 1024, p, 255);
1418 human_readable(cur->memmax * 1024, p, 255);
1422 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1426 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1431 if(output_methods & TO_X) {
1432 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1435 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1439 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1444 print_mixer(obj, 0, p, p_max_size);
1447 print_mixer(obj, -1, p, p_max_size);
1450 print_mixer(obj, 1, p, p_max_size);
1454 print_mixer_bar(obj, 0, p);
1457 print_mixer_bar(obj, -1, p);
1460 print_mixer_bar(obj, 1, p);
1463 OBJ(if_mixer_mute) {
1464 if (!check_mixer_muted(obj)) {
1469 #define NOT_IN_X "Not running in X"
1471 if(x_initialised != YES) {
1472 strncpy(p, NOT_IN_X, p_max_size);
1474 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1477 OBJ(monitor_number) {
1478 if(x_initialised != YES) {
1479 strncpy(p, NOT_IN_X, p_max_size);
1481 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1485 if(x_initialised != YES) {
1486 strncpy(p, NOT_IN_X, p_max_size);
1488 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1491 OBJ(desktop_number) {
1492 if(x_initialised != YES) {
1493 strncpy(p, NOT_IN_X, p_max_size);
1495 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1499 if(x_initialised != YES) {
1500 strncpy(p, NOT_IN_X, p_max_size);
1501 }else if(cur->x11.desktop.name != NULL) {
1502 strncpy(p, cur->x11.desktop.name, p_max_size);
1508 char buf[max_user_text];
1510 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1512 if(times_in_seconds) {
1513 print_format_time(obj, p, p_max_size);
1515 NORM_ERR("Enable \"times_in_seconds\" to use $format_time");
1520 print_mails(obj, p, p_max_size);
1523 print_new_mails(obj, p, p_max_size);
1526 print_seen_mails(obj, p, p_max_size);
1529 print_unseen_mails(obj, p, p_max_size);
1531 OBJ(flagged_mails) {
1532 print_flagged_mails(obj, p, p_max_size);
1534 OBJ(unflagged_mails) {
1535 print_unflagged_mails(obj, p, p_max_size);
1537 OBJ(forwarded_mails) {
1538 print_forwarded_mails(obj, p, p_max_size);
1540 OBJ(unforwarded_mails) {
1541 print_unforwarded_mails(obj, p, p_max_size);
1543 OBJ(replied_mails) {
1544 print_replied_mails(obj, p, p_max_size);
1546 OBJ(unreplied_mails) {
1547 print_unreplied_mails(obj, p, p_max_size);
1550 print_draft_mails(obj, p, p_max_size);
1552 OBJ(trashed_mails) {
1553 print_trashed_mails(obj, p, p_max_size);
1556 print_mboxscan(obj, p, p_max_size);
1559 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1562 new_outline(p, obj->data.l);
1565 char buf[max_user_text];
1567 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1569 print_pid_chroot(obj, p, p_max_size);
1572 char buf[max_user_text];
1574 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1576 print_pid_cmdline(obj, p, p_max_size);
1579 char buf[max_user_text];
1581 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1583 print_pid_cwd(obj, p, p_max_size);
1586 char buf[max_user_text];
1588 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1590 print_pid_environ(obj, p, p_max_size);
1592 OBJ(pid_environ_list) {
1593 char buf[max_user_text];
1595 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1597 print_pid_environ_list(obj, p, p_max_size);
1600 char buf[max_user_text];
1602 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1604 print_pid_exe(obj, p, p_max_size);
1607 char buf[max_user_text];
1609 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1611 print_pid_nice(obj, p, p_max_size);
1613 OBJ(pid_openfiles) {
1614 char buf[max_user_text];
1616 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1618 print_pid_openfiles(obj, p, p_max_size);
1621 char buf[max_user_text];
1623 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1625 print_pid_parent(obj, p, p_max_size);
1628 char buf[max_user_text];
1630 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1632 print_pid_priority(obj, p, p_max_size);
1635 char buf[max_user_text];
1637 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1639 print_pid_state(obj, p, p_max_size);
1641 OBJ(pid_state_short) {
1642 char buf[max_user_text];
1644 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1646 print_pid_state_short(obj, p, p_max_size);
1649 char buf[max_user_text];
1651 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1653 print_pid_stderr(obj, p, p_max_size);
1656 char buf[max_user_text];
1658 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1660 print_pid_stdin(obj, p, p_max_size);
1663 char buf[max_user_text];
1665 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1667 print_pid_stdout(obj, p, p_max_size);
1670 char buf[max_user_text];
1672 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1674 print_pid_threads(obj, p, p_max_size);
1676 OBJ(pid_thread_list) {
1677 char buf[max_user_text];
1679 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1681 print_pid_thread_list(obj, p, p_max_size);
1683 OBJ(pid_time_kernelmode) {
1684 char buf[max_user_text];
1686 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1688 print_pid_time_kernelmode(obj, p, p_max_size);
1690 OBJ(pid_time_usermode) {
1691 char buf[max_user_text];
1693 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1695 print_pid_time_usermode(obj, p, p_max_size);
1698 char buf[max_user_text];
1700 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1702 print_pid_time(obj, p, p_max_size);
1705 char buf[max_user_text];
1707 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1709 print_pid_uid(obj, p, p_max_size);
1712 char buf[max_user_text];
1714 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1716 print_pid_euid(obj, p, p_max_size);
1719 char buf[max_user_text];
1721 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1723 print_pid_suid(obj, p, p_max_size);
1726 char buf[max_user_text];
1728 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1730 print_pid_fsuid(obj, p, p_max_size);
1733 char buf[max_user_text];
1735 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1737 print_pid_gid(obj, p, p_max_size);
1740 char buf[max_user_text];
1742 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1744 print_pid_egid(obj, p, p_max_size);
1747 char buf[max_user_text];
1749 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1751 print_pid_sgid(obj, p, p_max_size);
1754 char buf[max_user_text];
1756 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1758 print_pid_fsgid(obj, p, p_max_size);
1761 char buf[max_user_text];
1763 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1765 print_pid_read(obj, p, p_max_size);
1768 char buf[max_user_text];
1770 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1772 print_pid_vmpeak(obj, p, p_max_size);
1775 char buf[max_user_text];
1777 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1779 print_pid_vmsize(obj, p, p_max_size);
1782 char buf[max_user_text];
1784 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1786 print_pid_vmlck(obj, p, p_max_size);
1789 char buf[max_user_text];
1791 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1793 print_pid_vmhwm(obj, p, p_max_size);
1796 char buf[max_user_text];
1798 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1800 print_pid_vmrss(obj, p, p_max_size);
1803 char buf[max_user_text];
1805 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1807 print_pid_vmdata(obj, p, p_max_size);
1810 char buf[max_user_text];
1812 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1814 print_pid_vmstk(obj, p, p_max_size);
1817 char buf[max_user_text];
1819 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1821 print_pid_vmexe(obj, p, p_max_size);
1824 char buf[max_user_text];
1826 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1828 print_pid_vmlib(obj, p, p_max_size);
1831 char buf[max_user_text];
1833 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1835 print_pid_vmpte(obj, p, p_max_size);
1838 char buf[max_user_text];
1840 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1842 print_pid_write(obj, p, p_max_size);
1845 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1847 OBJ(running_processes) {
1848 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1850 OBJ(running_threads) {
1851 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1854 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1857 snprintf(p, p_max_size, "%s", obj->data.s);
1861 new_bg(p, obj->data.l);
1864 new_stippled_hr(obj, p);
1868 human_readable(cur->swap * 1024, p, 255);
1871 human_readable(cur->swapfree * 1024, p, 255);
1874 human_readable(cur->swapmax * 1024, p, 255);
1877 if (cur->swapmax == 0) {
1878 strncpy(p, "No swap", p_max_size);
1880 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1885 if(output_methods & TO_X) {
1886 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1889 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1892 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1895 print_time(obj, p, p_max_size);
1898 print_utime(obj, p, p_max_size);
1901 print_tztime(obj, p, p_max_size);
1904 print_totaldown(obj, p, p_max_size);
1907 print_totalup(obj, p, p_max_size);
1910 char buf[max_user_text];
1912 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1914 print_gid_name(obj, p, p_max_size);
1917 char buf[max_user_text];
1919 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1921 print_uid_name(obj, p, p_max_size);
1924 snprintf(p, p_max_size, "%d", total_updates);
1927 if(total_updates % updatereset != obj->data.i - 1) {
1932 print_upspeed(obj, p, p_max_size);
1935 print_upspeedf(obj, p, p_max_size);
1939 print_upspeedgraph(obj, p);
1943 if(times_in_seconds) {
1944 snprintf(p, p_max_size, "%d", (int) cur->uptime);
1946 format_seconds_short(p, p_max_size, (int) cur->uptime);
1950 if(times_in_seconds) {
1951 snprintf(p, p_max_size, "%d", (int) cur->uptime);
1953 format_seconds(p, p_max_size, (int) cur->uptime);
1958 snprintf(p, p_max_size, "%s", cur->users.names);
1961 snprintf(p, p_max_size, "%s", cur->users.terms);
1964 snprintf(p, p_max_size, "%s", cur->users.times);
1967 update_user_time(obj->data.s, times_in_seconds);
1968 snprintf(p, p_max_size, "%s", cur->users.ctime);
1971 snprintf(p, p_max_size, "%d", cur->users.number);
1973 #endif /* __linux__ */
1974 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1975 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1979 msg = get_apm_adapter();
1980 snprintf(p, p_max_size, "%s", msg);
1983 OBJ(apm_battery_life) {
1986 msg = get_apm_battery_life();
1987 snprintf(p, p_max_size, "%s", msg);
1990 OBJ(apm_battery_time) {
1993 msg = get_apm_battery_time();
1994 snprintf(p, p_max_size, "%s", msg);
1997 #endif /* __FreeBSD__ __OpenBSD__ */
2000 #define mpd_printf(fmt, val) \
2001 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
2002 #define mpd_sprintf(val) { \
2003 if (!obj->data.i || obj->data.i > p_max_size) \
2004 mpd_printf("%s", val); \
2006 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2011 mpd_sprintf(artist);
2015 mpd_printf("%s", random);
2017 mpd_printf("%s", repeat);
2025 mpd_printf("%d", volume);
2027 mpd_printf("%d", bitrate);
2029 mpd_printf("%s", status);
2031 if(times_in_seconds) {
2032 snprintf(p, p_max_size, "%d", mpd_get_info()->elapsed);
2034 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2038 if(times_in_seconds) {
2039 snprintf(p, p_max_size, "%d", mpd_get_info()->length);
2041 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2045 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2049 if(output_methods & TO_X) {
2050 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
2053 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
2056 struct mpd_s *mpd = mpd_get_info();
2057 int len = obj->data.i;
2058 if (len == 0 || len > p_max_size)
2061 memset(p, 0, p_max_size);
2062 if (mpd->artist && *mpd->artist &&
2063 mpd->title && *mpd->title) {
2064 snprintf(p, len, "%s - %s", mpd->artist,
2066 } else if (mpd->title && *mpd->title) {
2067 snprintf(p, len, "%s", mpd->title);
2068 } else if (mpd->artist && *mpd->artist) {
2069 snprintf(p, len, "%s", mpd->artist);
2070 } else if (mpd->file && *mpd->file) {
2071 snprintf(p, len, "%s", mpd->file);
2076 OBJ(if_mpd_playing) {
2077 if (!mpd_get_info()->is_playing) {
2086 #define MOC_PRINT(t, a) \
2087 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2089 MOC_PRINT(state, "??");
2092 MOC_PRINT(file, "no file");
2095 MOC_PRINT(title, "no title");
2098 MOC_PRINT(artist, "no artist");
2101 MOC_PRINT(song, "no song");
2104 MOC_PRINT(album, "no album");
2106 OBJ(moc_totaltime) {
2107 MOC_PRINT(totaltime, "0:00");
2110 MOC_PRINT(timeleft, "0:00");
2113 MOC_PRINT(curtime, "0:00");
2116 MOC_PRINT(bitrate, "0Kbps");
2119 MOC_PRINT(rate, "0KHz");
2125 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2128 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2131 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2134 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2136 OBJ(xmms2_comment) {
2137 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2140 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2143 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2146 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2148 OBJ(xmms2_tracknr) {
2149 if (cur->xmms2.tracknr != -1) {
2150 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2153 OBJ(xmms2_bitrate) {
2154 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2157 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2160 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2162 OBJ(xmms2_elapsed) {
2163 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2164 (cur->xmms2.elapsed / 1000) % 60);
2166 OBJ(xmms2_duration) {
2167 snprintf(p, p_max_size, "%02d:%02d",
2168 cur->xmms2.duration / 60000,
2169 (cur->xmms2.duration / 1000) % 60);
2171 OBJ(xmms2_percent) {
2172 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2176 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
2179 OBJ(xmms2_playlist) {
2180 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2182 OBJ(xmms2_timesplayed) {
2183 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2186 if (strlen(cur->xmms2.title) < 2
2187 && strlen(cur->xmms2.title) < 2) {
2188 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2190 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2194 OBJ(if_xmms2_connected) {
2195 if (cur->xmms2.conn_state != 1) {
2201 OBJ(audacious_status) {
2202 snprintf(p, p_max_size, "%s",
2203 cur->audacious.items[AUDACIOUS_STATUS]);
2205 OBJ(audacious_title) {
2206 snprintf(p, cur->audacious.max_title_len > 0
2207 ? cur->audacious.max_title_len : p_max_size, "%s",
2208 cur->audacious.items[AUDACIOUS_TITLE]);
2210 OBJ(audacious_length) {
2211 snprintf(p, p_max_size, "%s",
2212 cur->audacious.items[AUDACIOUS_LENGTH]);
2214 OBJ(audacious_length_seconds) {
2215 snprintf(p, p_max_size, "%s",
2216 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2218 OBJ(audacious_position) {
2219 snprintf(p, p_max_size, "%s",
2220 cur->audacious.items[AUDACIOUS_POSITION]);
2222 OBJ(audacious_position_seconds) {
2223 snprintf(p, p_max_size, "%s",
2224 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2226 OBJ(audacious_bitrate) {
2227 snprintf(p, p_max_size, "%s",
2228 cur->audacious.items[AUDACIOUS_BITRATE]);
2230 OBJ(audacious_frequency) {
2231 snprintf(p, p_max_size, "%s",
2232 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2234 OBJ(audacious_channels) {
2235 snprintf(p, p_max_size, "%s",
2236 cur->audacious.items[AUDACIOUS_CHANNELS]);
2238 OBJ(audacious_filename) {
2239 snprintf(p, p_max_size, "%s",
2240 cur->audacious.items[AUDACIOUS_FILENAME]);
2242 OBJ(audacious_playlist_length) {
2243 snprintf(p, p_max_size, "%s",
2244 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2246 OBJ(audacious_playlist_position) {
2247 snprintf(p, p_max_size, "%s",
2248 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2250 OBJ(audacious_main_volume) {
2251 snprintf(p, p_max_size, "%s",
2252 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2255 OBJ(audacious_bar) {
2259 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2260 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2261 new_bar(obj, p, (int) (progress * 255.0f));
2264 #endif /* AUDACIOUS */
2268 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2271 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2274 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2277 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2280 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2283 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2286 /* we have four different types of top (top, top_mem,
2287 * top_time and top_io). To avoid having almost-same code four
2288 * times, we have this special handler. */
2297 /* yes, passing top_name_width instead
2298 * of p_max_size is intended here */
2299 print_top(obj, p, top_name_width);
2300 #endif /* __linux__ */
2302 print_tailhead("tail", obj, p, p_max_size);
2305 print_tailhead("head", obj, p, p_max_size);
2309 FILE *fp = open_file(obj->data.s, &rep);
2312 /* FIXME: use something more general (see also tail.c, head.c */
2313 #define BUFSZ 0x1000
2318 while(fgets(buf, BUFSZ, fp) != NULL){
2319 for(j = 0; buf[j] != 0; j++) {
2320 if(buf[j] == '\n') {
2325 sprintf(p, "%d", lines);
2328 sprintf(p, "File Unreadable");
2334 FILE *fp = open_file(obj->data.s, &rep);
2339 char inword = FALSE;
2342 while(fgets(buf, BUFSZ, fp) != NULL){
2343 for(j = 0; buf[j] != 0; j++) {
2344 if(!isspace(buf[j])) {
2345 if(inword == FALSE) {
2354 sprintf(p, "%d", words);
2357 sprintf(p, "File Unreadable");
2360 #ifdef TCP_PORT_MONITOR
2362 tcp_portmon_action(obj, p, p_max_size);
2364 #endif /* TCP_PORT_MONITOR */
2368 do_iconv_start(obj);
2373 #endif /* HAVE_ICONV */
2375 OBJ(entropy_avail) {
2376 print_entropy_avail(obj, p, p_max_size);
2379 print_entropy_perc(obj, p, p_max_size);
2381 OBJ(entropy_poolsize) {
2382 print_entropy_poolsize(obj, p, p_max_size);
2385 print_entropy_bar(obj, p, p_max_size);
2391 s = smapi_get_val(obj->data.s);
2392 snprintf(p, p_max_size, "%s", s);
2396 OBJ(if_smapi_bat_installed) {
2398 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2399 if(!smapi_bat_installed(idx)) {
2403 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2405 OBJ(smapi_bat_perc) {
2407 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2408 val = smapi_bat_installed(idx) ?
2409 smapi_get_bat_int(idx, "remaining_percent") : 0;
2410 percent_print(p, p_max_size, val);
2412 NORM_ERR("argument to smapi_bat_perc must be an integer");
2414 OBJ(smapi_bat_temp) {
2416 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2417 val = smapi_bat_installed(idx) ?
2418 smapi_get_bat_int(idx, "temperature") : 0;
2419 /* temperature is in milli degree celsius */
2420 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2422 NORM_ERR("argument to smapi_bat_temp must be an integer");
2424 OBJ(smapi_bat_power) {
2426 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2427 val = smapi_bat_installed(idx) ?
2428 smapi_get_bat_int(idx, "power_now") : 0;
2429 /* power_now is in mW, set to W with one digit precision */
2430 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2432 NORM_ERR("argument to smapi_bat_power must be an integer");
2435 OBJ(smapi_bat_bar) {
2436 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2437 new_bar(obj, p, (int)
2438 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2446 char buf[max_user_text];
2448 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2449 snprintf(p, p_max_size, "%s", buf);
2455 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2456 char buf[max_user_text];
2459 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2460 snprintf(p, p_max_size, "%s", buf);
2461 if(total_updates % 2) {
2462 for(j=0; p[j] != 0; j++) {
2468 char buf[max_user_text];
2470 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2472 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2473 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2474 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2475 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2476 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2477 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2478 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2480 snprintf(p, p_max_size, "%s", buf);
2483 print_scroll(obj, p, p_max_size, cur);
2486 print_combine(obj, p, cur);
2490 print_nvidia_value(obj, display, p, p_max_size);
2495 /* This is just a meta-object to set host:port */
2498 snprintf(p, p_max_size, "%s",
2499 cur->apcupsd.items[APCUPSD_NAME]);
2501 OBJ(apcupsd_model) {
2502 snprintf(p, p_max_size, "%s",
2503 cur->apcupsd.items[APCUPSD_MODEL]);
2505 OBJ(apcupsd_upsmode) {
2506 snprintf(p, p_max_size, "%s",
2507 cur->apcupsd.items[APCUPSD_UPSMODE]);
2509 OBJ(apcupsd_cable) {
2510 snprintf(p, p_max_size, "%s",
2511 cur->apcupsd.items[APCUPSD_CABLE]);
2513 OBJ(apcupsd_status) {
2514 snprintf(p, p_max_size, "%s",
2515 cur->apcupsd.items[APCUPSD_STATUS]);
2517 OBJ(apcupsd_linev) {
2518 snprintf(p, p_max_size, "%s",
2519 cur->apcupsd.items[APCUPSD_LINEV]);
2522 snprintf(p, p_max_size, "%s",
2523 cur->apcupsd.items[APCUPSD_LOAD]);
2525 OBJ(apcupsd_loadbar) {
2528 if(output_methods & TO_X) {
2529 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2530 new_bar(obj, p, (int) progress);
2534 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2535 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2539 OBJ(apcupsd_loadgraph) {
2541 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2542 new_graph(obj, p, (int)progress);
2544 OBJ(apcupsd_loadgauge) {
2546 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2547 new_gauge(obj, p, (int)progress);
2550 OBJ(apcupsd_charge) {
2551 snprintf(p, p_max_size, "%s",
2552 cur->apcupsd.items[APCUPSD_CHARGE]);
2554 OBJ(apcupsd_timeleft) {
2555 snprintf(p, p_max_size, "%s",
2556 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2559 snprintf(p, p_max_size, "%s",
2560 cur->apcupsd.items[APCUPSD_TEMP]);
2562 OBJ(apcupsd_lastxfer) {
2563 snprintf(p, p_max_size, "%s",
2564 cur->apcupsd.items[APCUPSD_LASTXFER]);
2566 #endif /* APCUPSD */
2573 size_t a = strlen(p);
2576 iconv_convert(&a, buff_in, p, p_max_size);
2577 #endif /* HAVE_ICONV */
2578 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2580 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2581 #endif /* HAVE_LUA */
2583 substitute_newlines(p, a - 2);
2592 /* load any new fonts we may have had */
2593 if (need_to_load_fonts) {
2599 void evaluate(const char *text, char *p, int p_max_size)
2601 struct information *tmp_info;
2602 struct text_object subroot;
2604 tmp_info = malloc(sizeof(struct information));
2605 memcpy(tmp_info, &info, sizeof(struct information));
2606 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2607 DBGP("evaluated '%s' to '%s'", text, p);
2609 free_text_objects(&subroot, 1);
2613 double current_update_time, next_update_time, last_update_time;
2615 static void generate_text(void)
2617 struct information *cur = &info;
2624 current_update_time = get_time();
2628 /* add things to the buffer */
2634 generate_text_internal(p, max_user_text, global_root_object, cur);
2636 if (stuff_in_uppercase) {
2639 tmp_p = text_buffer;
2641 *tmp_p = toupper(*tmp_p);
2646 next_update_time += update_interval;
2647 if (next_update_time < get_time()) {
2648 next_update_time = get_time() + update_interval;
2649 } else if (next_update_time > get_time() + update_interval) {
2650 next_update_time = get_time() + update_interval;
2652 last_update_time = current_update_time;
2656 void set_update_interval(double interval)
2658 update_interval = interval;
2659 update_interval_old = interval;
2662 static inline int get_string_width(const char *s)
2664 return *s ? calc_text_width(s) : 0;
2668 static int get_string_width_special(char *s, int special_index)
2678 if ((output_methods & TO_X) == 0)
2681 p = strndup(s, text_buffer_size);
2685 if (*p == SPECIAL_CHAR) {
2686 /* shift everything over by 1 so that the special char
2687 * doesn't mess up the size calculation */
2688 for (i = 0; i < (long)strlen(p); i++) {
2689 *(p + i) = *(p + i + 1);
2691 if (specials[special_index + idx].type == GRAPH
2692 || specials[special_index + idx].type == GAUGE
2693 || specials[special_index + idx].type == BAR) {
2694 width += specials[special_index + idx].width;
2697 } else if (*p == SECRIT_MULTILINE_CHAR) {
2704 if (strlen(final) > 1) {
2705 width += calc_text_width(final);
2711 static int text_size_updater(char *s, int special_index);
2713 int last_font_height;
2714 static void update_text_area(void)
2718 if ((output_methods & TO_X) == 0)
2720 /* update text size if it isn't fixed */
2725 text_width = minimum_width;
2727 last_font_height = font_height();
2728 for_each_line(text_buffer, text_size_updater);
2730 if (text_height < minimum_height) {
2731 text_height = minimum_height;
2733 if (text_width > maximum_width && maximum_width > 0) {
2734 text_width = maximum_width;
2738 /* get text position on workarea */
2739 switch (text_alignment) {
2740 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2744 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2745 y = workarea[3] - text_height - gap_y;
2748 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2749 y = workarea[3] / 2 - text_height / 2 - gap_y;
2752 switch (text_alignment) {
2753 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2757 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2758 x = workarea[2] - text_width - gap_x;
2761 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2762 x = workarea[2] / 2 - text_width / 2 - gap_x;
2766 if (text_alignment == NONE) { // Let the WM manage the window
2773 #endif /* OWN_WINDOW */
2776 if (own_window && !fixed_pos) {
2779 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2780 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2781 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2782 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2786 /* If window size doesn't match to workarea's size,
2787 * then window probably includes panels (gnome).
2788 * Blah, doesn't work on KDE. */
2789 if (workarea[2] != window.width || workarea[3] != window.height) {
2798 /* update lua window globals */
2799 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2800 #endif /* HAVE_LUA */
2805 static int cur_x, cur_y; /* current x and y for drawing */
2807 //draw_mode also without X11 because we only need to print to stdout with FG
2808 static int draw_mode; /* FG, BG or OUTLINE */
2810 static long current_color;
2812 static int text_size_updater(char *s, int special_index)
2817 if ((output_methods & TO_X) == 0)
2819 /* get string widths and skip specials */
2822 if (*p == SPECIAL_CHAR) {
2824 w += get_string_width(s);
2827 if (specials[special_index].type == BAR
2828 || specials[special_index].type == GAUGE
2829 || specials[special_index].type == GRAPH) {
2830 w += specials[special_index].width;
2831 if (specials[special_index].height > last_font_height) {
2832 last_font_height = specials[special_index].height;
2833 last_font_height += font_height();
2835 } else if (specials[special_index].type == OFFSET) {
2836 if (specials[special_index].arg > 0) {
2837 w += specials[special_index].arg;
2839 } else if (specials[special_index].type == VOFFSET) {
2840 last_font_height += specials[special_index].arg;
2841 } else if (specials[special_index].type == GOTO) {
2842 if (specials[special_index].arg > cur_x) {
2843 w = (int) specials[special_index].arg;
2845 } else if (specials[special_index].type == TAB) {
2846 int start = specials[special_index].arg;
2847 int step = specials[special_index].width;
2849 if (!step || step < 0) {
2852 w += step - (cur_x - text_start_x - start) % step;
2853 } else if (specials[special_index].type == FONT) {
2854 selected_font = specials[special_index].font_added;
2855 if (font_height() > last_font_height) {
2856 last_font_height = font_height();
2862 } else if (*p == SECRIT_MULTILINE_CHAR) {
2865 lw = get_string_width(s);
2866 *p = SECRIT_MULTILINE_CHAR;
2868 w = lw > w ? lw : w;
2869 text_height += last_font_height;
2873 w += get_string_width(s);
2874 if (w > text_width) {
2877 if (text_width > maximum_width && maximum_width) {
2878 text_width = maximum_width;
2881 text_height += last_font_height;
2882 last_font_height = font_height();
2883 return special_index;
2887 static inline void set_foreground_color(long c)
2890 if (output_methods & TO_X) {
2892 XSetForeground(display, window.gc, c);
2896 if (output_methods & TO_NCURSES) {
2897 attron(COLOR_PAIR(c));
2899 #endif /* NCURSES */
2904 static void draw_string(const char *s)
2906 int i, i2, pos, width_of_s;
2909 char *s_with_newlines;
2915 width_of_s = get_string_width(s);
2916 s_with_newlines = strdup(s);
2917 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2918 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2919 s_with_newlines[i] = '\n';
2922 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2923 printf("%s\n", s_with_newlines);
2924 if (extra_newline) fputc('\n', stdout);
2925 fflush(stdout); /* output immediately, don't buffer */
2927 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2928 fprintf(stderr, "%s\n", s_with_newlines);
2929 fflush(stderr); /* output immediately, don't buffer */
2931 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2932 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2934 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2935 fprintf(append_fpointer, "%s\n", s_with_newlines);
2938 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2939 printw("%s", s_with_newlines);
2942 free(s_with_newlines);
2943 memset(tmpstring1, 0, text_buffer_size);
2944 memset(tmpstring2, 0, text_buffer_size);
2945 strncpy(tmpstring1, s, text_buffer_size - 1);
2950 if (output_methods & TO_X) {
2951 max = ((text_width - width_of_s) / get_string_width(" "));
2954 /* This code looks for tabs in the text and coverts them to spaces.
2955 * The trick is getting the correct number of spaces, and not going
2956 * over the window's size without forcing the window larger. */
2957 for (i = 0; i < (int) text_buffer_size; i++) {
2958 if (tmpstring1[i] == '\t') {
2960 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2961 /* guard against overrun */
2962 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2967 /* guard against overrun */
2968 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2973 if (output_methods & TO_X) {
2974 if (text_width == maximum_width) {
2975 /* this means the text is probably pushing the limit,
2976 * so we'll chop it */
2977 while (cur_x + get_string_width(tmpstring2) - text_start_x
2978 > maximum_width && strlen(tmpstring2) > 0) {
2979 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2986 if (output_methods & TO_X) {
2992 c.pixel = current_color;
2993 XQueryColor(display, DefaultColormap(display, screen), &c);
2996 c2.color.red = c.red;
2997 c2.color.green = c.green;
2998 c2.color.blue = c.blue;
2999 c2.color.alpha = fonts[selected_font].font_alpha;
3001 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3002 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3004 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3005 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3010 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
3013 cur_x += width_of_s;
3016 memcpy(tmpstring1, s, text_buffer_size);
3019 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
3027 int last_special_needed = -1;
3028 int orig_special_index = special_index;
3031 if (output_methods & TO_X) {
3032 font_h = font_height();
3033 cur_y += font_ascent();
3035 cur_x = text_start_x;
3039 if (*p == SECRIT_MULTILINE_CHAR) {
3040 /* special newline marker for multiline objects */
3045 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3050 /* draw string before special, unless we're dealing multiline
3052 if (last_special_applied > -1) {
3053 special_index = last_special_applied;
3061 switch (specials[special_index].type) {
3063 case HORIZONTAL_LINE:
3065 int h = specials[special_index].height;
3066 int mid = font_ascent() / 2;
3068 w = text_start_x + text_width - cur_x;
3070 XSetLineAttributes(display, window.gc, h, LineSolid,
3071 CapButt, JoinMiter);
3072 XDrawLine(display, window.drawable, window.gc, cur_x,
3073 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3079 int h = specials[special_index].height;
3080 int tmp_s = specials[special_index].arg;
3081 int mid = font_ascent() / 2;
3082 char ss[2] = { tmp_s, tmp_s };
3084 w = text_start_x + text_width - cur_x - 1;
3085 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3086 CapButt, JoinMiter);
3087 XSetDashes(display, window.gc, 0, ss, 2);
3088 XDrawLine(display, window.drawable, window.gc, cur_x,
3089 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3095 int h, bar_usage, by;
3096 if (cur_x - text_start_x > maximum_width
3097 && maximum_width > 0) {
3100 h = specials[special_index].height;
3101 bar_usage = specials[special_index].arg;
3102 by = cur_y - (font_ascent() / 2) - 1;
3107 w = specials[special_index].width;
3109 w = text_start_x + text_width - cur_x - 1;
3115 XSetLineAttributes(display, window.gc, 1, LineSolid,
3116 CapButt, JoinMiter);
3118 XDrawRectangle(display, window.drawable, window.gc, cur_x,
3120 XFillRectangle(display, window.drawable, window.gc, cur_x,
3121 by, w * bar_usage / 255, h);
3129 case GAUGE: /* new GAUGE */
3132 unsigned long last_colour = current_color;
3134 float angle, px, py;
3138 if (cur_x - text_start_x > maximum_width
3139 && maximum_width > 0) {
3143 h = specials[special_index].height;
3144 by = cur_y - (font_ascent() / 2) - 1;
3149 w = specials[special_index].width;
3151 w = text_start_x + text_width - cur_x - 1;
3157 XSetLineAttributes(display, window.gc, 1, LineSolid,
3158 CapButt, JoinMiter);
3160 XDrawArc(display, window.drawable, window.gc,
3161 cur_x, by, w, h * 2, 0, 180*64);
3164 usage = specials[special_index].arg;
3165 angle = (M_PI)*(float)(usage)/255.;
3166 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3167 py = (float)(by+(h))-(float)(h)*sin(angle);
3169 XDrawLine(display, window.drawable, window.gc,
3170 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3178 set_foreground_color(last_colour);
3186 int h, by, i = 0, j = 0;
3188 unsigned long last_colour = current_color;
3189 unsigned long *tmpcolour = 0;
3190 if (cur_x - text_start_x > maximum_width
3191 && maximum_width > 0) {
3194 h = specials[special_index].height;
3195 by = cur_y - (font_ascent() / 2) - 1;
3200 w = specials[special_index].width;
3202 w = text_start_x + text_width - cur_x - 1;
3207 if (draw_graph_borders) {
3208 XSetLineAttributes(display, window.gc, 1, LineSolid,
3209 CapButt, JoinMiter);
3210 XDrawRectangle(display, window.drawable, window.gc,
3213 XSetLineAttributes(display, window.gc, 1, LineSolid,
3214 CapButt, JoinMiter);
3216 if (specials[special_index].last_colour != 0
3217 || specials[special_index].first_colour != 0) {
3218 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3221 for (i = w - 2; i > -1; i--) {
3222 if (specials[special_index].last_colour != 0
3223 || specials[special_index].first_colour != 0) {
3224 if (specials[special_index].tempgrad) {
3227 (int)((float)(w - 2) - specials[special_index].graph[j] *
3228 (w - 2) / (float)specials[special_index].graph_scale)
3232 (int)((float)(w - 2) - specials[special_index].graph[j] *
3233 (w - 2) / (float)specials[special_index].graph_scale)
3236 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3238 (int)((float)(w - 2) - specials[special_index].graph[j] *
3239 (w - 2) / (float)specials[special_index].graph_scale)
3243 #endif /* DEBUG_lol */
3244 XSetForeground(display, window.gc, tmpcolour[
3245 (int)((float)(w - 2) - specials[special_index].graph[j] *
3246 (w - 2) / (float)specials[special_index].graph_scale)
3249 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3252 /* this is mugfugly, but it works */
3253 XDrawLine(display, window.drawable, window.gc,
3254 cur_x + i + 1, by + h, cur_x + i + 1,
3255 round_to_int((double)by + h - specials[special_index].graph[j] *
3256 (h - 1) / specials[special_index].graph_scale));
3257 if ((w - i) / ((float) (w - 2) /
3258 (specials[special_index].graph_width)) > j
3259 && j < MAX_GRAPH_DEPTH - 3) {
3263 if (tmpcolour) free(tmpcolour);
3268 /* if (draw_mode == BG) {
3269 set_foreground_color(default_bg_color);
3270 } else if (draw_mode == OUTLINE) {
3271 set_foreground_color(default_out_color);
3273 set_foreground_color(default_fg_color);
3275 if (show_graph_range) {
3278 unsigned short int seconds = update_interval * w;
3284 unsigned short int timeunits;
3286 timeunits = seconds / 86400; seconds %= 86400;
3287 if (timeunits > 0) {
3288 asprintf(&tmp_day_str, "%dd", timeunits);
3290 tmp_day_str = strdup("");
3292 timeunits = seconds / 3600; seconds %= 3600;
3293 if (timeunits > 0) {
3294 asprintf(&tmp_hour_str, "%dh", timeunits);
3296 tmp_hour_str = strdup("");
3298 timeunits = seconds / 60; seconds %= 60;
3299 if (timeunits > 0) {
3300 asprintf(&tmp_min_str, "%dm", timeunits);
3302 tmp_min_str = strdup("");
3305 asprintf(&tmp_sec_str, "%ds", seconds);
3307 tmp_sec_str = strdup("");
3309 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3310 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3312 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3314 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3315 cur_y += font_h / 2;
3316 draw_string(tmp_str);
3322 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3326 cur_x += font_ascent() / 2;
3327 cur_y += font_h / 2;
3329 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3331 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3332 draw_string(tmp_str);
3338 set_foreground_color(last_colour);
3344 int old = font_ascent();
3346 cur_y -= font_ascent();
3347 selected_font = specials[special_index].font_added;
3349 if (cur_y + font_ascent() < cur_y + old) {
3352 cur_y += font_ascent();
3354 font_h = font_height();
3359 if (draw_mode == FG) {
3360 set_foreground_color(specials[special_index].arg);
3366 if (draw_mode == BG) {
3367 set_foreground_color(specials[special_index].arg);
3372 if (draw_mode == OUTLINE) {
3373 set_foreground_color(specials[special_index].arg);
3378 w += specials[special_index].arg;
3379 last_special_needed = special_index;
3383 cur_y += specials[special_index].arg;
3387 if (specials[special_index].arg >= 0) {
3388 cur_x = (int) specials[special_index].arg;
3390 last_special_needed = special_index;
3395 int start = specials[special_index].arg;
3396 int step = specials[special_index].width;
3398 if (!step || step < 0) {
3401 w = step - (cur_x - text_start_x - start) % step;
3402 last_special_needed = special_index;
3408 /* TODO: add back in "+ window.border_inner_margin" to the end of
3410 int pos_x = text_start_x + text_width -
3411 get_string_width_special(s, special_index);
3413 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3414 "get_string_width(p) %i gap_x %i "
3415 "specials[special_index].arg %i window.border_inner_margin %i "
3416 "window.border_width %i\n", pos_x, text_start_x, text_width,
3417 cur_x, get_string_width_special(s), gap_x,
3418 specials[special_index].arg, window.border_inner_margin,
3419 window.border_width); */
3420 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3421 cur_x = pos_x - specials[special_index].arg;
3423 last_special_needed = special_index;
3429 int pos_x = (text_width) / 2 - get_string_width_special(s,
3430 special_index) / 2 - (cur_x -
3432 /* int pos_x = text_start_x + text_width / 2 -
3433 get_string_width_special(s) / 2; */
3435 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3436 "get_string_width(p) %i gap_x %i "
3437 "specials[special_index].arg %i\n", pos_x, text_start_x,
3438 text_width, cur_x, get_string_width(s), gap_x,
3439 specials[special_index].arg); */
3440 if (pos_x > specials[special_index].arg) {
3441 w = pos_x - specials[special_index].arg;
3443 last_special_needed = special_index;
3453 if (special_index != last_special_applied) {
3456 special_index = orig_special_index;
3457 last_special_applied = -1;
3468 if (output_methods & TO_NCURSES) {
3471 #endif /* NCURSES */
3473 if (output_methods & TO_X)
3474 cur_y += font_descent();
3476 if (recurse && *recurse) {
3477 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3478 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3480 return special_index;
3483 static int draw_line(char *s, int special_index)
3486 if (output_methods & TO_X) {
3487 return draw_each_line_inner(s, special_index, -1);
3491 if (output_methods & TO_NCURSES) {
3492 return draw_each_line_inner(s, special_index, -1);
3494 #endif /* NCURSES */
3496 UNUSED(special_index);
3500 static void draw_text(void)
3504 llua_draw_pre_hook();
3505 #endif /* HAVE_LUA */
3506 if (output_methods & TO_X) {
3507 cur_y = text_start_y;
3510 if (draw_borders && window.border_width > 0) {
3511 if (stippled_borders) {
3512 char ss[2] = { stippled_borders, stippled_borders };
3513 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3514 CapButt, JoinMiter);
3515 XSetDashes(display, window.gc, 0, ss, 2);
3517 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3518 CapButt, JoinMiter);
3521 XDrawRectangle(display, window.drawable, window.gc,
3522 text_start_x - window.border_inner_margin - window.border_width,
3523 text_start_y - window.border_inner_margin - window.border_width,
3524 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3525 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3533 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3534 attron(COLOR_PAIR(COLOR_WHITE));
3535 #endif /* NCURSES */
3536 for_each_line(text_buffer, draw_line);
3537 #if defined(HAVE_LUA) && defined(X11)
3538 llua_draw_post_hook();
3539 #endif /* HAVE_LUA */
3542 static void draw_stuff(void)
3545 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3547 if (overwrite_file) {
3548 overwrite_fpointer = fopen(overwrite_file, "w");
3549 if(!overwrite_fpointer)
3550 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3553 append_fpointer = fopen(append_file, "a");
3554 if(!append_fpointer)
3555 NORM_ERR("Can't append '%s' anymore", append_file);
3558 if (output_methods & TO_X) {
3560 if (draw_shades && !draw_outline) {
3563 set_foreground_color(default_bg_color);
3574 for (i = -1; i < 2; i++) {
3575 for (j = -1; j < 2; j++) {
3576 if (i == 0 && j == 0) {
3581 set_foreground_color(default_out_color);
3582 draw_mode = OUTLINE;
3590 set_foreground_color(default_fg_color);
3595 #if defined(X11) && defined(HAVE_XDBE)
3596 if (output_methods & TO_X) {
3597 xdbe_swap_buffers();
3599 #endif /* X11 && HAVE_XDBE */
3600 if(overwrite_fpointer) {
3601 fclose(overwrite_fpointer);
3602 overwrite_fpointer = 0;
3604 if(append_fpointer) {
3605 fclose(append_fpointer);
3606 append_fpointer = 0;
3611 static void clear_text(int exposures)
3615 /* The swap action is XdbeBackground, which clears */
3619 if (display && window.window) { // make sure these are !null
3620 /* there is some extra space for borders and outlines */
3621 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3622 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3623 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3624 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3629 static int need_to_update;
3631 /* update_text() generates new text and clears old text area */
3632 static void update_text(void)
3639 if (output_methods & TO_X)
3644 llua_update_info(&info, update_interval);
3645 #endif /* HAVE_LUA */
3648 #ifdef HAVE_SYS_INOTIFY_H
3652 static void main_loop(void)
3655 #ifdef SIGNAL_BLOCKING
3656 sigset_t newmask, oldmask;
3659 #ifdef HAVE_SYS_INOTIFY_H
3660 int inotify_config_wd = -1;
3661 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3662 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3663 char inotify_buff[INOTIFY_BUF_LEN];
3664 #endif /* HAVE_SYS_INOTIFY_H */
3667 #ifdef SIGNAL_BLOCKING
3668 sigemptyset(&newmask);
3669 sigaddset(&newmask, SIGINT);
3670 sigaddset(&newmask, SIGTERM);
3671 sigaddset(&newmask, SIGUSR1);
3674 last_update_time = 0.0;
3675 next_update_time = get_time();
3677 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3678 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3679 char buf[max_user_text];
3681 get_battery_short_status(buf, max_user_text, "BAT0");
3683 update_interval = update_interval_bat;
3685 update_interval = update_interval_old;
3690 #ifdef SIGNAL_BLOCKING
3691 /* block signals. we will inspect for pending signals later */
3692 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3693 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3698 if (output_methods & TO_X) {
3701 /* wait for X event or timeout */
3703 if (!XPending(display)) {
3707 t = next_update_time - get_time();
3711 } else if (t > update_interval) {
3712 t = update_interval;
3715 tv.tv_sec = (long) t;
3716 tv.tv_usec = (long) (t * 1000000) % 1000000;
3718 FD_SET(ConnectionNumber(display), &fdsr);
3720 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3722 if (errno != EINTR) {
3723 NORM_ERR("can't select(): %s", strerror(errno));
3733 if (need_to_update) {
3735 int wx = window.x, wy = window.y;
3745 /* resize window if it isn't right size */
3747 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3748 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3749 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3750 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3751 draw_stuff(); /* redraw everything in our newly sized window */
3752 XResizeWindow(display, window.window, window.width,
3753 window.height); /* resize window */
3754 set_transparent_background(window.window);
3757 xdbe_swap_buffers();
3762 /* update lua window globals */
3763 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3764 #endif /* HAVE_LUA */
3767 /* move window if it isn't in right position */
3768 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3769 XMoveWindow(display, window.window, window.x, window.y);
3774 if (changed && window.type == TYPE_PANEL) {
3777 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3780 switch (text_alignment) {
3807 set_struts(sidenum);
3818 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3819 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3820 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3821 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3822 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3827 /* handle X events */
3828 while (XPending(display)) {
3831 XNextEvent(display, &ev);
3838 r.width = ev.xexpose.width;
3839 r.height = ev.xexpose.height;
3840 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3844 case PropertyNotify:
3846 if ( ev.xproperty.state == PropertyNewValue ) {
3847 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3853 case ReparentNotify:
3854 /* set background to ParentRelative for all parents */
3856 set_transparent_background(window.window);
3860 case ConfigureNotify:
3862 /* if window size isn't what expected, set fixed size */
3863 if (ev.xconfigure.width != window.width
3864 || ev.xconfigure.height != window.height) {
3865 if (window.width != 0 && window.height != 0) {
3869 /* clear old stuff before screwing up
3874 XWindowAttributes attrs;
3875 if (XGetWindowAttributes(display,
3876 window.window, &attrs)) {
3877 window.width = attrs.width;
3878 window.height = attrs.height;
3882 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3883 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3884 if (text_width > maximum_width
3885 && maximum_width > 0) {
3886 text_width = maximum_width;
3890 /* if position isn't what expected, set fixed pos
3891 * total_updates avoids setting fixed_pos when window
3892 * is set to weird locations when started */
3893 /* // this is broken
3894 if (total_updates >= 2 && !fixed_pos
3895 && (window.x != ev.xconfigure.x
3896 || window.y != ev.xconfigure.y)
3897 && (ev.xconfigure.x != 0
3898 || ev.xconfigure.y != 0)) {
3906 /* if an ordinary window with decorations */
3907 if ((window.type == TYPE_NORMAL &&
3908 (!TEST_HINT(window.hints,
3909 HINT_UNDECORATED))) ||
3910 window.type == TYPE_DESKTOP) {
3911 /* allow conky to hold input focus. */
3914 /* forward the click to the desktop window */
3915 XUngrabPointer(display, ev.xbutton.time);
3916 ev.xbutton.window = window.desktop;
3917 ev.xbutton.x = ev.xbutton.x_root;
3918 ev.xbutton.y = ev.xbutton.y_root;
3919 XSendEvent(display, ev.xbutton.window, False,
3920 ButtonPressMask, &ev);
3921 XSetInputFocus(display, ev.xbutton.window,
3922 RevertToParent, ev.xbutton.time);
3929 /* if an ordinary window with decorations */
3930 if ((window.type == TYPE_NORMAL)
3931 && (!TEST_HINT(window.hints,
3932 HINT_UNDECORATED))) {
3933 /* allow conky to hold input focus. */
3936 /* forward the release to the desktop window */
3937 ev.xbutton.window = window.desktop;
3938 ev.xbutton.x = ev.xbutton.x_root;
3939 ev.xbutton.y = ev.xbutton.y_root;
3940 XSendEvent(display, ev.xbutton.window, False,
3941 ButtonReleaseMask, &ev);
3950 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3951 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3953 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3954 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3956 #endif /* HAVE_XDAMAGE */
3962 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3963 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3964 #endif /* HAVE_XDAMAGE */
3966 /* XDBE doesn't seem to provide a way to clear the back buffer
3967 * without interfering with the front buffer, other than passing
3968 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3969 * using XDBE, we need to redraw the text even if it wasn't part of
3970 * the exposed area. OTOH, if we're not going to call draw_stuff at
3971 * all, then no swap happens and we can safely do nothing. */
3973 if (!XEmptyRegion(x11_stuff.region)) {
3978 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3979 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3980 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3981 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3982 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3985 XSetRegion(display, window.gc, x11_stuff.region);
3988 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3992 XDestroyRegion(x11_stuff.region);
3993 x11_stuff.region = XCreateRegion();
3997 t = (next_update_time - get_time()) * 1000000;
3998 if(t > 0) usleep((useconds_t)t);
4002 if(output_methods & TO_NCURSES) {
4011 #ifdef SIGNAL_BLOCKING
4012 /* unblock signals of interest and let handler fly */
4013 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4014 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4018 switch (g_signal_pending) {
4021 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4026 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4029 if (output_methods & TO_X) {
4030 XDestroyRegion(x11_stuff.region);
4031 x11_stuff.region = NULL;
4033 XDamageDestroy(display, x11_stuff.damage);
4034 XFixesDestroyRegion(display, x11_stuff.region2);
4035 XFixesDestroyRegion(display, x11_stuff.part);
4036 #endif /* HAVE_XDAMAGE */
4042 if(overwrite_file) {
4043 free(overwrite_file);
4052 /* Reaching here means someone set a signal
4053 * (SIGXXXX, signal_handler), but didn't write any code
4055 * If you don't want to handle a signal, don't set a handler on
4056 * it in the first place. */
4057 if (g_signal_pending) {
4058 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4062 #ifdef HAVE_SYS_INOTIFY_H
4063 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4064 inotify_config_wd = inotify_add_watch(inotify_fd,
4068 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4069 int len = 0, idx = 0;
4071 struct timeval time_to_wait;
4073 FD_ZERO(&descriptors);
4074 FD_SET(inotify_fd, &descriptors);
4076 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4078 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4079 if (FD_ISSET(inotify_fd, &descriptors)) {
4080 /* process inotify events */
4081 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4082 while (len > 0 && idx < len) {
4083 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4084 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4085 /* current_config should be reloaded */
4086 NORM_ERR("'%s' modified, reloading...", current_config);
4088 if (ev->mask & IN_IGNORED) {
4089 /* for some reason we get IN_IGNORED here
4090 * sometimes, so we need to re-add the watch */
4091 inotify_config_wd = inotify_add_watch(inotify_fd,
4098 llua_inotify_query(ev->wd, ev->mask);
4100 #endif /* HAVE_LUA */
4101 idx += INOTIFY_EVENT_SIZE + ev->len;
4105 #endif /* HAVE_SYS_INOTIFY_H */
4108 llua_update_info(&info, update_interval);
4109 #endif /* HAVE_LUA */
4110 g_signal_pending = 0;
4112 clean_up(NULL, NULL);
4114 #ifdef HAVE_SYS_INOTIFY_H
4115 if (inotify_fd != -1) {
4116 inotify_rm_watch(inotify_fd, inotify_config_wd);
4118 inotify_fd = inotify_config_wd = 0;
4120 #endif /* HAVE_SYS_INOTIFY_H */
4124 static void load_config_file_x11(const char *);
4126 void initialisation(int argc, char** argv);
4128 /* reload the config file */
4129 static void reload_config(void)
4131 char *current_config_copy = strdup(current_config);
4132 clean_up(NULL, NULL);
4133 current_config = current_config_copy;
4134 initialisation(argc_copy, argv_copy);
4137 void clean_up(void *memtofree1, void* memtofree2)
4142 if(output_methods & TO_NCURSES) {
4146 conftree_empty(currentconffile);
4147 currentconffile = NULL;
4154 timed_thread_destroy_registered_threads();
4156 if (info.cpu_usage) {
4157 free(info.cpu_usage);
4158 info.cpu_usage = NULL;
4161 if (x_initialised == YES) {
4162 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4163 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4164 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4165 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4168 if(x11_stuff.region) {
4169 XDestroyRegion(x11_stuff.region);
4170 x11_stuff.region = NULL;
4172 XCloseDisplay(display);
4174 if(info.x11.desktop.all_names) {
4175 free(info.x11.desktop.all_names);
4176 info.x11.desktop.all_names = NULL;
4178 if (info.x11.desktop.name) {
4179 free(info.x11.desktop.name);
4180 info.x11.desktop.name = NULL;
4184 free(fonts); //in set_default_configurations a font is set but not loaded
4190 free_update_callbacks();
4194 free_text_objects(&global_root_object, 0);
4213 free(current_config);
4216 #ifdef TCP_PORT_MONITOR
4217 tcp_portmon_clear();
4226 weather_free_info();
4229 llua_shutdown_hook();
4231 #endif /* HAVE_LUA */
4233 if (output_methods & TO_X)
4241 for (i = 0; i < special_count; i++) {
4242 if (specials[i].type == GRAPH) {
4243 free(specials[i].graph);
4251 clear_diskio_stats();
4252 if(global_cpu != NULL) {
4258 static int string_to_bool(const char *s)
4261 // Assumes an option without a true/false means true
4263 } else if (strcasecmp(s, "yes") == EQUAL) {
4265 } else if (strcasecmp(s, "true") == EQUAL) {
4267 } else if (strcasecmp(s, "1") == EQUAL) {
4274 static enum alignment string_to_alignment(const char *s)
4276 if (strcasecmp(s, "top_left") == EQUAL) {
4278 } else if (strcasecmp(s, "top_right") == EQUAL) {
4280 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4282 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4284 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4285 return BOTTOM_RIGHT;
4286 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4287 return BOTTOM_MIDDLE;
4288 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4290 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4291 return MIDDLE_RIGHT;
4292 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4293 return MIDDLE_MIDDLE;
4294 } else if (strcasecmp(s, "tl") == EQUAL) {
4296 } else if (strcasecmp(s, "tr") == EQUAL) {
4298 } else if (strcasecmp(s, "tm") == EQUAL) {
4300 } else if (strcasecmp(s, "bl") == EQUAL) {
4302 } else if (strcasecmp(s, "br") == EQUAL) {
4303 return BOTTOM_RIGHT;
4304 } else if (strcasecmp(s, "bm") == EQUAL) {
4305 return BOTTOM_MIDDLE;
4306 } else if (strcasecmp(s, "ml") == EQUAL) {
4308 } else if (strcasecmp(s, "mr") == EQUAL) {
4309 return MIDDLE_RIGHT;
4310 } else if (strcasecmp(s, "mm") == EQUAL) {
4311 return MIDDLE_MIDDLE;
4312 } else if (strcasecmp(s, "none") == EQUAL) {
4320 static void set_default_configurations_for_x(void)
4322 default_fg_color = WhitePixel(display, screen);
4323 default_bg_color = BlackPixel(display, screen);
4324 default_out_color = BlackPixel(display, screen);
4325 color0 = default_fg_color;
4326 color1 = default_fg_color;
4327 color2 = default_fg_color;
4328 color3 = default_fg_color;
4329 color4 = default_fg_color;
4330 color5 = default_fg_color;
4331 color6 = default_fg_color;
4332 color7 = default_fg_color;
4333 color8 = default_fg_color;
4334 color9 = default_fg_color;
4335 current_text_color = default_fg_color;
4339 static void set_default_configurations(void)
4346 fork_to_background = 0;
4347 total_run_times = 0;
4348 info.cpu_avg_samples = 2;
4349 info.net_avg_samples = 2;
4350 info.diskio_avg_samples = 2;
4355 format_human_readable = 1;
4365 mpd_env_host = getenv("MPD_HOST");
4366 mpd_env_port = getenv("MPD_PORT");
4368 if (!mpd_env_host || !strlen(mpd_env_host)) {
4369 mpd_set_host("localhost");
4371 /* MPD_HOST environment variable is set */
4372 char *mpd_hostpart = strchr(mpd_env_host, '@');
4373 if (!mpd_hostpart) {
4374 mpd_set_host(mpd_env_host);
4376 /* MPD_HOST contains a password */
4377 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4378 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4380 if (!strlen(mpd_hostpart + 1)) {
4381 mpd_set_host("localhost");
4383 mpd_set_host(mpd_hostpart + 1);
4386 mpd_set_password(mpd_password, 1);
4391 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4392 /* failed to set port from environment variable */
4393 mpd_set_port("6600");
4397 info.xmms2.artist = NULL;
4398 info.xmms2.album = NULL;
4399 info.xmms2.title = NULL;
4400 info.xmms2.genre = NULL;
4401 info.xmms2.comment = NULL;
4402 info.xmms2.url = NULL;
4403 info.xmms2.status = NULL;
4404 info.xmms2.playlist = NULL;
4406 use_spacer = NO_SPACER;
4408 output_methods = TO_X;
4410 output_methods = TO_STDOUT;
4413 show_graph_scale = 0;
4414 show_graph_range = 0;
4417 draw_graph_borders = 1;
4419 set_first_font("6x10");
4427 window.type = TYPE_NORMAL;
4429 strcpy(window.class_name, PACKAGE_NAME);
4430 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4432 stippled_borders = 0;
4433 window.border_inner_margin = 3;
4434 window.border_outer_margin = 1;
4435 window.border_width = 1;
4436 text_alignment = BOTTOM_LEFT;
4437 info.x11.monitor.number = 1;
4438 info.x11.monitor.current = 0;
4439 info.x11.desktop.current = 1;
4440 info.x11.desktop.number = 1;
4441 info.x11.desktop.nitems = 0;
4442 info.x11.desktop.all_names = NULL;
4443 info.x11.desktop.name = NULL;
4448 free(current_mail_spool);
4452 variable_substitute(MAIL_FILE, buf, 256);
4453 if (buf[0] != '\0') {
4454 current_mail_spool = strndup(buf, text_buffer_size);
4459 set_update_interval(3);
4460 update_interval_bat = NOBATTERY;
4461 info.music_player_interval = 1.0;
4462 stuff_in_uppercase = 0;
4463 info.users.number = 1;
4465 #ifdef TCP_PORT_MONITOR
4466 /* set default connection limit */
4467 tcp_portmon_set_max_connections(0);
4471 /* returns 1 if you can overwrite or create the file at 'path' */
4472 static _Bool overwrite_works(const char *path)
4476 if (!(filepointer = fopen(path, "w")))
4478 fclose(filepointer);
4482 /* returns 1 if you can append or create the file at 'path' */
4483 static _Bool append_works(const char *path)
4487 if (!(filepointer = fopen(path, "a")))
4489 fclose(filepointer);
4495 /* WARNING, this type not in Xlib spec */
4496 int x11_error_handler(Display *d, XErrorEvent *err)
4497 __attribute__((noreturn));
4498 int x11_error_handler(Display *d, XErrorEvent *err)
4500 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",
4502 (long unsigned)err->display,
4503 (long)err->resourceid,
4513 int x11_ioerror_handler(Display *d)
4514 __attribute__((noreturn));
4515 int x11_ioerror_handler(Display *d)
4517 NORM_ERR("X Error: Display %lx\n",
4524 static void X11_initialisation(void)
4526 if (x_initialised == YES) return;
4527 output_methods |= TO_X;
4529 set_default_configurations_for_x();
4530 x_initialised = YES;
4533 /* WARNING, this type not in Xlib spec */
4534 XSetErrorHandler(&x11_error_handler);
4535 XSetIOErrorHandler(&x11_ioerror_handler);
4539 static char **xargv = 0;
4540 static int xargc = 0;
4542 static void X11_create_window(void)
4544 if (output_methods & TO_X) {
4546 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4547 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4549 #else /* OWN_WINDOW */
4550 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4551 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4553 #endif /* OWN_WINDOW */
4557 update_text_area(); /* to position text/window on screen */
4560 if (own_window && !fixed_pos) {
4561 XMoveWindow(display, window.window, window.x, window.y);
4564 set_transparent_background(window.window);
4572 x11_stuff.region = XCreateRegion();
4574 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4575 NORM_ERR("Xdamage extension unavailable");
4577 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4578 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4579 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4580 #endif /* HAVE_XDAMAGE */
4583 update_text_area(); /* to get initial size of the window */
4586 /* setup lua window globals */
4587 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4588 #endif /* HAVE_LUA */
4592 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4593 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4594 #define CONF2(a) if (strcasecmp(name, a) == 0)
4595 #define CONF(a) else CONF2(a)
4596 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4597 || strcasecmp(name, b) == 0)
4598 #define CONF_CONTINUE 1
4599 #define CONF_BREAK 2
4600 #define CONF_BUFF_SIZE 512
4602 static FILE *open_config_file(const char *f)
4604 #ifdef CONFIG_OUTPUT
4605 if (!strcmp(f, "==builtin==")) {
4606 return conf_cookie_open();
4608 #endif /* CONFIG_OUTPUT */
4609 return fopen(f, "r");
4612 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4616 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4619 remove_comments(buf);
4624 while (*p && isspace((int) *p)) {
4628 return CONF_CONTINUE; /* empty line */
4635 while (*p2 && !isspace((int) *p2)) {
4639 *p2 = '\0'; /* break at name's end */
4646 while (*p && isspace((int) *p)) {
4652 p2 = *value + strlen(*value);
4653 while (isspace((int) *(p2 - 1))) {
4662 char load_config_file(const char *f)
4667 fp = open_config_file(f);
4671 DBGP("reading contents from config file '%s'", f);
4674 char buff[CONF_BUFF_SIZE], *name, *value;
4675 int ret = do_config_step(&line, fp, buff, &name, &value);
4676 if (ret == CONF_BREAK) {
4678 } else if (ret == CONF_CONTINUE) {
4684 /* don't listen if X is already initialised or
4685 * if we already know we don't want it */
4686 if(x_initialised != YES) {
4687 if (string_to_bool(value)) {
4688 output_methods &= TO_X;
4690 output_methods &= ~TO_X;
4691 x_initialised = NEVER;
4696 if (!value || x_initialised == YES) {
4701 disp = strdup(value);
4706 if (window.type == TYPE_DOCK)
4709 #endif /*OWN_WINDOW */
4711 int a = string_to_alignment(value);
4722 CONF("background") {
4723 fork_to_background = string_to_bool(value);
4726 CONF2("background") {
4727 fork_to_background = string_to_bool(value);
4731 CONF("show_graph_scale") {
4732 show_graph_scale = string_to_bool(value);
4734 CONF("show_graph_range") {
4735 show_graph_range = string_to_bool(value);
4737 CONF("border_inner_margin") {
4739 window.border_inner_margin = strtol(value, 0, 0);
4740 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4745 CONF("border_outer_margin") {
4747 window.border_outer_margin = strtol(value, 0, 0);
4748 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4753 CONF("border_width") {
4755 window.border_width = strtol(value, 0, 0);
4756 if (window.border_width < 0) window.border_width = 0;
4762 #define TEMPLATE_CONF(n) \
4763 CONF("template"#n) { \
4764 if (set_template(n, value)) \
4779 parse_global_imap_mail_args(value);
4786 parse_global_pop3_mail_args(value);
4791 CONF("default_bar_size") {
4794 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4801 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4805 CONF("default_graph_size") {
4808 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4815 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4818 CONF("default_gauge_size") {
4821 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4828 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4835 mpd_set_host(value);
4841 if (value && mpd_set_port(value)) {
4845 CONF("mpd_password") {
4847 mpd_set_password(value, 0);
4853 CONF("music_player_interval") {
4855 info.music_player_interval = strtod(value, 0);
4861 CONF("sensor_device") {
4863 sensor_device = strtol(value, 0, 0);
4869 CONF("cpu_avg_samples") {
4871 cpu_avg_samples = strtol(value, 0, 0);
4872 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4875 info.cpu_avg_samples = cpu_avg_samples;
4881 CONF("net_avg_samples") {
4883 net_avg_samples = strtol(value, 0, 0);
4884 if (net_avg_samples < 1 || net_avg_samples > 14) {
4887 info.net_avg_samples = net_avg_samples;
4893 CONF("diskio_avg_samples") {
4895 diskio_avg_samples = strtol(value, 0, 0);
4896 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4899 info.diskio_avg_samples = diskio_avg_samples;
4907 CONF("double_buffer") {
4908 use_xdbe = string_to_bool(value);
4912 CONF("override_utf8_locale") {
4913 utf8_mode = string_to_bool(value);
4915 CONF("draw_borders") {
4916 draw_borders = string_to_bool(value);
4918 CONF("draw_graph_borders") {
4919 draw_graph_borders = string_to_bool(value);
4921 CONF("draw_shades") {
4922 draw_shades = string_to_bool(value);
4924 CONF("draw_outline") {
4925 draw_outline = string_to_bool(value);
4928 CONF("times_in_seconds") {
4929 times_in_seconds = string_to_bool(value);
4931 CONF("out_to_console") {
4932 if(string_to_bool(value)) {
4933 output_methods |= TO_STDOUT;
4935 output_methods &= ~TO_STDOUT;
4938 CONF("extra_newline") {
4939 extra_newline = string_to_bool(value);
4941 CONF("out_to_stderr") {
4942 if(string_to_bool(value))
4943 output_methods |= TO_STDERR;
4946 CONF("out_to_ncurses") {
4947 if(string_to_bool(value)) {
4950 output_methods |= TO_NCURSES;
4954 CONF("overwrite_file") {
4955 if(overwrite_file) {
4956 free(overwrite_file);
4959 if(overwrite_works(value)) {
4960 overwrite_file = strdup(value);
4961 output_methods |= OVERWRITE_FILE;
4963 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4965 CONF("append_file") {
4970 if(append_works(value)) {
4971 append_file = strdup(value);
4972 output_methods |= APPEND_FILE;
4974 NORM_ERR("append_file won't be able to create/append '%s'", value);
4976 CONF("use_spacer") {
4978 if (strcasecmp(value, "left") == EQUAL) {
4979 use_spacer = LEFT_SPACER;
4980 } else if (strcasecmp(value, "right") == EQUAL) {
4981 use_spacer = RIGHT_SPACER;
4982 } else if (strcasecmp(value, "none") == EQUAL) {
4983 use_spacer = NO_SPACER;
4985 use_spacer = string_to_bool(value);
4986 NORM_ERR("use_spacer should have an argument of left, right, or"
4987 " none. '%s' seems to be some form of '%s', so"
4988 " defaulting to %s.", value,
4989 use_spacer ? "true" : "false",
4990 use_spacer ? "right" : "none");
4992 use_spacer = RIGHT_SPACER;
4994 use_spacer = NO_SPACER;
4998 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4999 use_spacer = RIGHT_SPACER;
5005 use_xft = string_to_bool(value);
5009 set_first_font(value);
5013 if (value && font_count >= 0) {
5014 fonts[0].font_alpha = atof(value) * 65535.0;
5021 if (string_to_bool(value)) {
5022 NORM_ERR("Xft not enabled at compile time");
5026 /* xftfont silently ignored when no Xft */
5029 /* xftalpha is silently ignored when no Xft */
5034 set_first_font(value);
5042 gap_x = atoi(value);
5049 gap_y = atoi(value);
5055 CONF("mail_spool") {
5059 variable_substitute(value, buffer, 256);
5061 if (buffer[0] != '\0') {
5062 if (current_mail_spool) {
5063 free(current_mail_spool);
5065 current_mail_spool = strndup(buffer, text_buffer_size);
5072 CONF("minimum_size") {
5074 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5076 if (sscanf(value, "%d", &minimum_width) != 1) {
5084 CONF("maximum_width") {
5086 if (sscanf(value, "%d", &maximum_width) != 1) {
5094 CONF("no_buffers") {
5095 no_buffers = string_to_bool(value);
5097 CONF("top_name_width") {
5099 if (sscanf(value, "%u", &top_name_width) != 1) {
5105 if (top_name_width >= max_user_text) {
5106 top_name_width = max_user_text - 1;
5109 CONF("top_cpu_separate") {
5110 cpu_separate = string_to_bool(value);
5112 CONF("short_units") {
5113 short_units = string_to_bool(value);
5115 CONF("format_human_readable") {
5116 format_human_readable = string_to_bool(value);
5119 CONF("hddtemp_host") {
5120 set_hddtemp_host(value);
5122 CONF("hddtemp_port") {
5123 set_hddtemp_port(value);
5125 #endif /* HDDTEMP */
5126 CONF("pad_percents") {
5127 pad_percents = atoi(value);
5131 CONF("own_window") {
5133 own_window = string_to_bool(value);
5136 CONF("own_window_class") {
5138 memset(window.class_name, 0, sizeof(window.class_name));
5139 strncpy(window.class_name, value,
5140 sizeof(window.class_name) - 1);
5143 CONF("own_window_title") {
5145 memset(window.title, 0, sizeof(window.title));
5146 strncpy(window.title, value, sizeof(window.title) - 1);
5149 CONF("own_window_transparent") {
5151 set_transparent = string_to_bool(value);
5154 CONF("own_window_hints") {
5156 char *p_hint, *p_save;
5157 char delim[] = ", ";
5159 /* tokenize the value into individual hints */
5160 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5162 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5163 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5164 SET_HINT(window.hints, HINT_UNDECORATED);
5165 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5166 SET_HINT(window.hints, HINT_BELOW);
5167 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5168 SET_HINT(window.hints, HINT_ABOVE);
5169 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5170 SET_HINT(window.hints, HINT_STICKY);
5171 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5172 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5173 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5174 SET_HINT(window.hints, HINT_SKIP_PAGER);
5179 p_hint = strtok_r(NULL, delim, &p_save);
5180 } while (p_hint != NULL);
5186 CONF("own_window_type") {
5188 if (strncmp(value, "normal", 6) == EQUAL) {
5189 window.type = TYPE_NORMAL;
5190 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5191 window.type = TYPE_DESKTOP;
5192 } else if (strncmp(value, "dock", 4) == EQUAL) {
5193 window.type = TYPE_DOCK;
5194 text_alignment = TOP_LEFT;
5195 } else if (strncmp(value, "panel", 5) == EQUAL) {
5196 window.type = TYPE_PANEL;
5197 } else if (strncmp(value, "override", 8) == EQUAL) {
5198 window.type = TYPE_OVERRIDE;
5207 CONF("stippled_borders") {
5209 stippled_borders = strtol(value, 0, 0);
5211 stippled_borders = 4;
5215 CONF("imlib_cache_size") {
5217 cimlib_set_cache_size(atoi(value));
5220 CONF("imlib_cache_flush_interval") {
5222 cimlib_set_cache_flush_interval(atoi(value));
5227 CONF("update_interval_on_battery") {
5229 update_interval_bat = strtod(value, 0);
5234 CONF("update_interval") {
5236 set_update_interval(strtod(value, 0));
5240 if (info.music_player_interval == 0) {
5241 // default to update_interval
5242 info.music_player_interval = update_interval;
5245 CONF("total_run_times") {
5247 total_run_times = strtod(value, 0);
5253 stuff_in_uppercase = string_to_bool(value);
5255 CONF("max_specials") {
5257 max_specials = atoi(value);
5262 CONF("max_user_text") {
5264 max_user_text = atoi(value);
5269 CONF("text_buffer_size") {
5271 text_buffer_size = atoi(value);
5272 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5273 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5274 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5282 if (output_methods & TO_X) {
5283 X11_initialisation();
5292 global_text = (char *) malloc(1);
5293 global_text[0] = '\0';
5296 unsigned int l = strlen(global_text);
5298 char buf[CONF_BUFF_SIZE];
5300 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5306 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5314 /* Check for continuation of \\-\n. */
5315 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5316 global_text[l-1] = '\0';
5320 global_text = (char *) realloc(global_text, l + bl + 1);
5321 strcat(global_text, buf);
5323 if (strlen(global_text) > max_user_text) {
5327 global_text_lines = line + 1;
5330 #ifdef TCP_PORT_MONITOR
5331 CONF("max_port_monitor_connections") {
5333 if (!value || (sscanf(value, "%d", &max) != 1)) {
5334 /* an error. use default, warn and continue. */
5335 tcp_portmon_set_max_connections(0);
5337 } else if (tcp_portmon_set_max_connections(max)) {
5338 /* max is < 0, default has been set*/
5343 CONF("if_up_strictness") {
5345 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5346 ifup_strictness = IFUP_UP;
5347 } else if (strcasecmp(value, "up") == EQUAL) {
5348 ifup_strictness = IFUP_UP;
5349 } else if (strcasecmp(value, "link") == EQUAL) {
5350 ifup_strictness = IFUP_LINK;
5351 } else if (strcasecmp(value, "address") == EQUAL) {
5352 ifup_strictness = IFUP_ADDR;
5354 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5355 ifup_strictness = IFUP_UP;
5359 CONF("temperature_unit") {
5361 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5362 } else if (set_temp_output_unit(value)) {
5363 NORM_ERR("temperature_unit: incorrect argument");
5370 char *ptr = strtok(value, " ");
5373 ptr = strtok(NULL, " ");
5380 CONF("lua_draw_hook_pre") {
5382 llua_set_draw_pre_hook(value);
5387 CONF("lua_draw_hook_post") {
5389 llua_set_draw_post_hook(value);
5394 CONF("lua_startup_hook") {
5396 llua_set_startup_hook(value);
5401 CONF("lua_shutdown_hook") {
5403 llua_set_shutdown_hook(value);
5409 #endif /* HAVE_LUA */
5421 CONF("default_color"){}
5422 CONF3("default_shade_color", "default_shadecolor"){}
5423 CONF3("default_outline_color", "default_outlinecolor") {}
5424 CONF("own_window_colour") {}
5427 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5433 if (info.music_player_interval == 0) {
5434 // default to update_interval
5435 info.music_player_interval = update_interval;
5437 if (!global_text) { // didn't supply any text
5438 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5440 if (!output_methods) {
5441 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5443 #if defined(NCURSES)
5445 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5446 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5447 output_methods &= ~TO_NCURSES;
5451 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5452 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5453 output_methods &= ~(TO_STDOUT | TO_STDERR);
5455 #endif /* NCURSES */
5460 static void load_config_file_x11(const char *f)
5465 fp = open_config_file(f);
5469 DBGP("reading contents from config file '%s'", f);
5472 char buff[CONF_BUFF_SIZE], *name, *value;
5473 int ret = do_config_step(&line, fp, buff, &name, &value);
5474 if (ret == CONF_BREAK) {
5476 } else if (ret == CONF_CONTINUE) {
5481 X11_initialisation();
5482 if (x_initialised == YES) {
5484 color0 = get_x11_color(value);
5491 X11_initialisation();
5492 if (x_initialised == YES) {
5494 color1 = get_x11_color(value);
5501 X11_initialisation();
5502 if (x_initialised == YES) {
5504 color2 = get_x11_color(value);
5511 X11_initialisation();
5512 if (x_initialised == YES) {
5514 color3 = get_x11_color(value);
5521 X11_initialisation();
5522 if (x_initialised == YES) {
5524 color4 = get_x11_color(value);
5531 X11_initialisation();
5532 if (x_initialised == YES) {
5534 color5 = get_x11_color(value);
5541 X11_initialisation();
5542 if (x_initialised == YES) {
5544 color6 = get_x11_color(value);
5551 X11_initialisation();
5552 if (x_initialised == YES) {
5554 color7 = get_x11_color(value);
5561 X11_initialisation();
5562 if (x_initialised == YES) {
5564 color8 = get_x11_color(value);
5571 X11_initialisation();
5572 if (x_initialised == YES) {
5574 color9 = get_x11_color(value);
5580 CONF("default_color") {
5581 X11_initialisation();
5582 if (x_initialised == YES) {
5584 default_fg_color = get_x11_color(value);
5590 CONF3("default_shade_color", "default_shadecolor") {
5591 X11_initialisation();
5592 if (x_initialised == YES) {
5594 default_bg_color = get_x11_color(value);
5600 CONF3("default_outline_color", "default_outlinecolor") {
5601 X11_initialisation();
5602 if (x_initialised == YES) {
5604 default_out_color = get_x11_color(value);
5611 CONF("own_window_colour") {
5612 X11_initialisation();
5613 if (x_initialised == YES) {
5615 background_colour = get_x11_color(value);
5617 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5618 "'#' for hex colours");
5624 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5625 if(output_methods & TO_X) {
5626 X11_initialisation();
5635 #undef CONF_CONTINUE
5636 #undef CONF_BUFF_SIZE
5644 static void print_help(const char *prog_name) {
5645 printf("Usage: %s [OPTION]...\n"
5646 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5647 "window. Command line options will override configurations defined in config\n"
5649 " -v, --version version\n"
5650 " -q, --quiet quiet mode\n"
5651 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5652 " -c, --config=FILE config file to load\n"
5653 #ifdef CONFIG_OUTPUT
5654 " -C, --print-config print the builtin default config to stdout\n"
5655 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5657 " -d, --daemonize daemonize, fork to background\n"
5658 " -h, --help help\n"
5660 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5661 " -f, --font=FONT font to use\n"
5662 " -X, --display=DISPLAY X11 display to use\n"
5664 " -o, --own-window create own window to draw\n"
5667 " -b, --double-buffer double buffer (prevents flickering)\n"
5669 " -w, --window-id=WIN_ID window id to draw\n"
5670 " -x X x position\n"
5671 " -y Y y position\n"
5673 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5674 " -u, --interval=SECS update interval\n"
5675 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5676 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5681 /* : means that character before that takes an argument */
5682 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5692 #ifdef CONFIG_OUTPUT
5697 static const struct option longopts[] = {
5698 { "help", 0, NULL, 'h' },
5699 { "version", 0, NULL, 'V' },
5700 { "debug", 0, NULL, 'D' },
5701 { "config", 1, NULL, 'c' },
5702 #ifdef CONFIG_OUTPUT
5703 { "print-config", 0, NULL, 'C' },
5705 { "daemonize", 0, NULL, 'd' },
5707 { "alignment", 1, NULL, 'a' },
5708 { "font", 1, NULL, 'f' },
5709 { "display", 1, NULL, 'X' },
5711 { "own-window", 0, NULL, 'o' },
5714 { "double-buffer", 0, NULL, 'b' },
5716 { "window-id", 1, NULL, 'w' },
5718 { "text", 1, NULL, 't' },
5719 { "interval", 0, NULL, 'u' },
5720 { "pause", 0, NULL, 'p' },
5724 void initialisation(int argc, char **argv) {
5725 struct sigaction act, oact;
5727 set_default_configurations();
5728 load_config_file(current_config);
5729 currentconffile = conftree_add(currentconffile, current_config);
5731 /* init specials array */
5732 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5733 NORM_ERR("failed to create specials array");
5737 if (current_mail_spool == NULL) {
5740 variable_substitute(MAIL_FILE, buf, 256);
5742 if (buf[0] != '\0') {
5743 current_mail_spool = strndup(buf, text_buffer_size);
5748 /* handle other command line arguments */
5750 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5751 || defined(__NetBSD__)
5752 optind = optreset = 1;
5757 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5758 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5759 "kvm_open")) == NULL) {
5760 CRIT_ERR(NULL, NULL, "cannot read kvm");
5765 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5774 fork_to_background = 1;
5777 global_debug_level++;
5781 set_first_font(optarg);
5784 text_alignment = string_to_alignment(optarg);
5803 global_text = strndup(optarg, max_user_text);
5804 convert_escapes(global_text);
5808 update_interval = strtod(optarg, 0);
5809 update_interval_old = update_interval;
5810 if (info.music_player_interval == 0) {
5811 // default to update_interval
5812 info.music_player_interval = update_interval;
5817 total_run_times = strtod(optarg, 0);
5821 gap_x = atoi(optarg);
5825 gap_y = atoi(optarg);
5829 startup_pause = atoi(optarg);
5830 sleep(startup_pause);
5840 if (output_methods & TO_X) {
5841 load_config_file_x11(current_config);
5845 /* generate text and get initial size */
5846 extract_variable_text(global_text);
5853 if (fork_to_background) {
5858 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5865 fprintf(stderr, "\n");
5870 /* parent process */
5871 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5878 start_update_threading();
5880 text_buffer = malloc(max_user_text);
5881 memset(text_buffer, 0, max_user_text);
5882 tmpstring1 = malloc(text_buffer_size);
5883 memset(tmpstring1, 0, text_buffer_size);
5884 tmpstring2 = malloc(text_buffer_size);
5885 memset(tmpstring2, 0, text_buffer_size);
5890 X11_create_window();
5893 llua_setup_info(&info, update_interval);
5894 #endif /* HAVE_LUA */
5899 /* Set signal handlers */
5900 act.sa_handler = signal_handler;
5901 sigemptyset(&act.sa_mask);
5904 act.sa_flags |= SA_RESTART;
5907 if ( sigaction(SIGINT, &act, &oact) < 0
5908 || sigaction(SIGALRM, &act, &oact) < 0
5909 || sigaction(SIGUSR1, &act, &oact) < 0
5910 || sigaction(SIGHUP, &act, &oact) < 0
5911 || sigaction(SIGTERM, &act, &oact) < 0) {
5912 NORM_ERR("error setting signal handler: %s", strerror(errno));
5916 llua_startup_hook();
5917 #endif /* HAVE_LUA */
5920 int main(int argc, char **argv)
5929 g_signal_pending = 0;
5930 max_user_text = MAX_USER_TEXT_DEFAULT;
5932 memset(&info, 0, sizeof(info));
5936 #ifdef TCP_PORT_MONITOR
5937 /* set default connection limit */
5938 tcp_portmon_set_max_connections(0);
5941 /* handle command line parameters that don't change configs */
5943 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5944 || ((s = getenv("LANG")) && *s)) {
5945 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5947 NORM_ERR("malloc failed");
5949 for (x = 0; x < strlen(s); x++) {
5950 temp[x] = tolower(s[x]);
5953 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5959 if (!setlocale(LC_CTYPE, "")) {
5960 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5964 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5975 if (current_config) {
5976 free(current_config);
5978 current_config = strndup(optarg, max_user_text);
5981 freopen("/dev/null", "w", stderr);
5984 print_help(argv[0]);
5986 #ifdef CONFIG_OUTPUT
5993 window.window = strtol(optarg, 0, 0);
5998 disp = strdup(optarg);
6007 /* check if specified config file is valid */
6008 if (current_config) {
6010 if (stat(current_config, &sb) ||
6011 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
6012 NORM_ERR("invalid configuration file '%s'\n", current_config);
6013 free(current_config);
6018 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
6020 if (!current_config) {
6021 /* load default config file */
6022 char buf[DEFAULT_TEXT_BUFFER_SIZE];
6025 /* Try to use personal config file first */
6026 to_real_path(buf, CONFIG_FILE);
6027 if (buf[0] && (fp = fopen(buf, "r"))) {
6028 current_config = strndup(buf, max_user_text);
6032 /* Try to use system config file if personal config not readable */
6033 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
6034 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
6038 /* No readable config found */
6039 if (!current_config) {
6040 #ifdef CONFIG_OUTPUT
6041 current_config = strdup("==builtin==");
6042 NORM_ERR("no readable personal or system-wide config file found,"
6043 " using builtin default");
6045 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
6046 #endif /* ! CONF_OUTPUT */
6051 /* Load xoap keys, if existing */
6055 #ifdef HAVE_SYS_INOTIFY_H
6056 inotify_fd = inotify_init1(IN_NONBLOCK);
6057 #endif /* HAVE_SYS_INOTIFY_H */
6059 initialisation(argc, argv);
6063 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6071 void alarm_handler(void) {
6073 kill(childpid, SIGTERM);
6077 static void signal_handler(int sig)
6079 /* signal handler is light as a feather, as it should be.
6080 * we will poll g_signal_pending with each loop of conky
6081 * and do any signal processing there, NOT here (except
6082 * SIGALRM because this is caused when conky is hanging) */
6083 if(sig == SIGALRM) {
6086 g_signal_pending = sig;