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;
170 static int extra_newline;
171 enum x_initialiser_state x_initialised = NO;
172 static volatile int g_signal_pending;
173 /* Update interval */
174 double update_interval;
175 double update_interval_old;
176 double update_interval_bat;
177 void *global_cpu = NULL;
182 /* prototypes for internally used functions */
183 static void signal_handler(int);
184 static void print_version(void) __attribute__((noreturn));
185 static void reload_config(void);
187 static void print_version(void)
189 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
191 printf("\nCompiled in features:\n\n"
192 "System config file: "SYSTEM_CONFIG_FILE"\n"
193 "Package library path: "PACKAGE_LIBDIR"\n\n"
197 " * Xdamage extension\n"
198 # endif /* HAVE_XDAMAGE */
200 " * XDBE (double buffer extension)\n"
201 # endif /* HAVE_XDBE */
206 "\n Music detection:\n"
209 #endif /* AUDACIOUS */
225 #endif /* HAVE_OPENMP */
232 #ifdef TCP_PORT_MONITOR
234 #endif /* TCP_PORT_MONITOR */
237 #endif /* HAVE_CURL */
242 " * Weather (METAR)\n"
244 " * Weather (XOAP)\n"
249 #endif /* HAVE_IWLIB */
251 " * support for IBM/Lenovo notebooks\n"
261 #endif /* CONFIG_OUTPUT */
266 " * ALSA mixer support\n"
267 #endif /* MIXER_IS_ALSA */
280 #ifdef HAVE_LUA_CAIRO
282 #endif /* HAVE_LUA_CAIRO */
283 #ifdef HAVE_LUA_IMLIB2
286 #endif /* HAVE_LUA */
292 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
297 static void X11_create_window(void);
298 static void X11_initialisation(void);
300 struct _x11_stuff_s {
304 XserverRegion region2, part;
305 int event_base, error_base;
311 static int text_start_x, text_start_y; /* text start position in window */
312 static int text_width, text_height;
328 /* display to connect to */
329 static char *disp = NULL;
333 /* struct that has all info to be shared between
334 * instances of the same text object */
335 struct information info;
337 /* path to config file */
338 char *current_config;
340 /* set to 1 if you want all text to be in uppercase */
341 static unsigned int stuff_in_uppercase;
343 /* Run how many times? */
344 static unsigned long total_run_times;
347 static int fork_to_background;
349 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
351 /* filenames for output */
352 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
353 char *append_file = NULL; FILE *append_fpointer = NULL;
357 static int show_graph_scale;
358 static int show_graph_range;
360 /* Position on the screen */
361 static int text_alignment;
362 static int gap_x, gap_y;
365 static int draw_borders;
366 static int draw_graph_borders;
367 static int stippled_borders;
369 int get_stippled_borders(void)
371 return stippled_borders;
374 static int draw_shades, draw_outline;
376 long default_fg_color, default_bg_color, default_out_color;
378 /* create own window or draw stuff to root? */
379 static int set_transparent = 0;
382 static int own_window = 0;
383 static int background_colour = 0;
385 /* fixed size/pos is set if wm/user changes them */
386 static int fixed_size = 0, fixed_pos = 0;
389 static int minimum_width, minimum_height;
390 static int maximum_width;
395 static int sensor_device;
398 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
401 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
402 unsigned int max_user_text;
404 /* maximum size of individual text buffers, ie $exec buffer size */
405 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
410 /* no buffers in used memory? */
413 /* pad percentages to decimals? */
414 static int pad_percents = 0;
416 static char *global_text = 0;
418 char *get_global_text(void)
423 long global_text_lines;
425 static int total_updates;
426 static int updatereset;
428 void set_updatereset(int i)
433 int get_updatereset(void)
438 int check_contains(char *f, char *s)
441 FILE *where = open_file(f, 0);
446 while (fgets(buf1, 256, where)) {
447 if (strstr(buf1, s)) {
454 NORM_ERR("Could not open the file");
459 #define SECRIT_MULTILINE_CHAR '\x02'
461 static inline int calc_text_width(const char *s)
463 size_t slen = strlen(s);
466 if ((output_methods & TO_X) == 0) {
476 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
477 (const FcChar8 *) s, slen, &gi);
479 XftTextExtents8(display, fonts[selected_font].xftfont,
480 (const FcChar8 *) s, slen, &gi);
486 return XTextWidth(fonts[selected_font].font, s, slen);
491 /* formatted text to render on screen, generated in generate_text(),
492 * drawn in draw_stuff() */
494 static char *text_buffer;
496 /* quite boring functions */
498 static inline void for_each_line(char *b, int f(char *, int))
501 int special_index = 0; /* specials index */
503 for (ps = b, pe = b; *pe; pe++) {
506 special_index = f(ps, special_index);
513 f(ps, special_index);
517 static void convert_escapes(char *buf)
519 char *p = buf, *s = buf;
526 } else if (*s == '\\') {
537 /* Prints anything normally printed with snprintf according to the current value
538 * of use_spacer. Actually slightly more flexible than snprintf, as you can
539 * safely specify the destination buffer as one of your inputs. */
540 int spaced_print(char *buf, int size, const char *format, int width, ...)
549 tempbuf = malloc(size * sizeof(char));
551 // Passes the varargs along to vsnprintf
552 va_start(argp, width);
553 vsnprintf(tempbuf, size, format, argp);
556 switch (use_spacer) {
558 len = snprintf(buf, size, "%s", tempbuf);
561 len = snprintf(buf, size, "%*s", width, tempbuf);
564 len = snprintf(buf, size, "%-*s", width, tempbuf);
571 /* print percentage values
573 * - i.e., unsigned values between 0 and 100
574 * - respect the value of pad_percents */
575 int percent_print(char *buf, int size, unsigned value)
577 return spaced_print(buf, size, "%u", pad_percents, value);
580 /* converts from bytes to human readable format (K, M, G, T)
582 * The algorithm always divides by 1024, as unit-conversion of byte
583 * counts suggests. But for output length determination we need to
584 * compare with 1000 here, as we print in decimal form. */
585 void human_readable(long long num, char *buf, int size)
587 const char **suffix = suffixes;
593 /* Possibly just output as usual, for example for stdout usage */
594 if (!format_human_readable) {
595 spaced_print(buf, size, "%d", 6, round_to_int(num));
606 if (llabs(num) < 1000LL) {
607 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
611 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
619 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
621 * The goal is to always have a significance of 3, by
622 * adjusting the decimal part of the number. Sample output:
626 * so the point of alignment resides between number and unit. The
627 * upside of this is that there is minimal padding necessary, though
628 * there should be a way to make alignment take place at the decimal
629 * dot (then with fixed width decimal part).
631 * Note the repdigits below: when given a precision value, printf()
632 * rounds the float to it, not just cuts off the remaining digits. So
633 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
634 * printed with a precision of 0. Yay. */
636 precision = 0; /* print 100-999 without decimal part */
638 precision = 1; /* print 10-99 with one decimal place */
640 precision = 2; /* print 0-9 with two decimal places */
642 spaced_print(buf, size, format, width, precision, fnum, *suffix);
645 /* global object list root element */
646 static struct text_object global_root_object;
648 static long current_text_color;
650 void set_current_text_color(long colour)
652 current_text_color = colour;
655 long get_current_text_color(void)
657 return current_text_color;
660 //adds newstring to to the tree unless you can already see it when travelling back.
661 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
662 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
663 struct conftree* node;
664 struct conftree* node2;
666 for(node = previous; node != NULL; node = node->back) {
667 if(strcmp(node->string, newstring) == 0) {
671 node = malloc(sizeof(struct conftree));
672 if (previous != NULL) {
673 if(previous->vert_next == NULL) {
674 previous->vert_next = node;
676 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
677 node2->horz_next = node;
680 node->string = strdup(newstring);
681 node->horz_next = NULL;
682 node->vert_next = NULL;
683 node->back = previous;
687 void conftree_empty(struct conftree* tree) {
689 conftree_empty(tree->horz_next);
690 conftree_empty(tree->vert_next);
696 struct conftree *currentconffile;
698 static void extract_variable_text(const char *p)
700 free_text_objects(&global_root_object, 0);
714 extract_variable_text_internal(&global_root_object, p);
717 void parse_conky_vars(struct text_object *root, const char *txt,
718 char *p, int p_max_size, struct information *cur)
720 extract_variable_text_internal(root, txt);
721 generate_text_internal(p, p_max_size, *root, cur);
724 static inline void format_media_player_time(char *buf, const int size,
727 int days, hours, minutes;
729 if (times_in_seconds()) {
730 snprintf(buf, size, "%d", seconds);
734 days = seconds / (24 * 60 * 60);
735 seconds %= (24 * 60 * 60);
736 hours = seconds / (60 * 60);
737 seconds %= (60 * 60);
738 minutes = seconds / 60;
742 snprintf(buf, size, "%i days %i:%02i:%02i", days,
743 hours, minutes, seconds);
744 } else if (hours > 0) {
745 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
748 snprintf(buf, size, "%i:%02i", minutes, seconds);
752 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
753 * multiline objects like $exec work with $align[rc] and friends
755 void substitute_newlines(char *p, long l)
759 while (p && *p && p < s + l) {
761 /* only substitute if it's not the last newline */
762 *p = SECRIT_MULTILINE_CHAR;
768 void generate_text_internal(char *p, int p_max_size,
769 struct text_object root, struct information *cur)
771 struct text_object *obj;
773 int need_to_load_fonts = 0;
776 /* for the OBJ_top* handler */
777 struct process **needed = 0;
780 char buff_in[p_max_size];
782 #endif /* HAVE_ICONV */
786 while (obj && p_max_size > 0) {
787 needed = 0; /* reset for top stuff */
789 /* IFBLOCK jumping algorithm
791 * This is easier as it looks like:
792 * - each IF checks it's condition
793 * - on FALSE: call DO_JUMP
794 * - on TRUE: don't care
795 * - each ELSE calls DO_JUMP unconditionally
796 * - each ENDIF is silently being ignored
799 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
800 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
801 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
802 * regularly, "obj" is being updated to point to obj->next, so object parsing
803 * continues right after the corresponding ELSE or ENDIF. This means that if we
804 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
805 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
806 * jumped, and there is nothing to do.
810 if (obj->ifblock_next) \
811 obj = obj->ifblock_next; \
814 #define OBJ(a) break; case OBJ_##a:
818 NORM_ERR("not implemented obj type %d", obj->type);
820 print_read_tcp(obj, p, p_max_size);
824 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
826 #endif /* !__OpenBSD__ */
830 ok = get_freq(p, p_max_size, "%.0f", 1,
838 ok = get_freq(p, p_max_size, "%'.2f", 1000,
841 /* OpenBSD has no such flag (SUSv2) */
842 ok = get_freq(p, p_max_size, "%.2f", 1000,
844 #endif /* __OpenBSD */
847 #if defined(__linux__)
851 ok = get_voltage(p, p_max_size, "%.0f", 1,
858 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
864 OBJ(wireless_essid) {
865 print_wireless_essid(obj, p, p_max_size);
868 print_wireless_mode(obj, p, p_max_size);
870 OBJ(wireless_bitrate) {
871 print_wireless_bitrate(obj, p, p_max_size);
874 print_wireless_ap(obj, p, p_max_size);
876 OBJ(wireless_link_qual) {
877 print_wireless_link_qual(obj, p, p_max_size);
879 OBJ(wireless_link_qual_max) {
880 print_wireless_link_qual_max(obj, p, p_max_size);
882 OBJ(wireless_link_qual_perc) {
883 print_wireless_link_qual_perc(obj, p, p_max_size);
885 OBJ(wireless_link_bar) {
886 print_wireless_link_bar(obj, p, p_max_size);
888 #endif /* HAVE_IWLIB */
890 #endif /* __linux__ */
894 get_acpi_fan(p, p_max_size);
897 get_acpi_ac_adapter(p, p_max_size);
900 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
903 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
905 OBJ(battery_percent) {
906 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
910 if(output_methods & TO_X) {
911 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
914 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
917 get_battery_short_status(p, p_max_size, obj->data.s);
919 #endif /* __OpenBSD__ */
922 human_readable(cur->buffers * 1024, p, 255);
925 human_readable(cur->cached * 1024, p, 255);
927 OBJ(cmdline_to_pid) {
928 print_cmdline_to_pid(obj, p, p_max_size);
931 if (obj->data.i > info.cpu_count) {
932 NORM_ERR("obj->data.i %i info.cpu_count %i",
933 obj->data.i, info.cpu_count);
934 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
936 percent_print(p, p_max_size,
937 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
941 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
945 if(output_methods & TO_X) {
946 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
949 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
953 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
956 print_loadgraph(obj, p);
960 new_fg(p, obj->data.l);
995 snprintf(p, p_max_size, "%s", VERSION);
997 OBJ(conky_build_date) {
998 snprintf(p, p_max_size, "%s", BUILD_DATE);
1000 OBJ(conky_build_arch) {
1001 snprintf(p, p_max_size, "%s", BUILD_ARCH);
1003 #if defined(__linux__)
1005 snprintf(p, p_max_size, "%s",
1006 get_disk_protect_queue(obj->data.s));
1009 print_i8k_version(obj, p, p_max_size);
1012 print_i8k_bios(obj, p, p_max_size);
1015 print_i8k_serial(obj, p, p_max_size);
1018 print_i8k_cpu_temp(obj, p, p_max_size);
1020 OBJ(i8k_left_fan_status) {
1021 print_i8k_left_fan_status(obj, p, p_max_size);
1023 OBJ(i8k_right_fan_status) {
1024 print_i8k_right_fan_status(obj, p, p_max_size);
1026 OBJ(i8k_left_fan_rpm) {
1027 print_i8k_left_fan_rpm(obj, p, p_max_size);
1029 OBJ(i8k_right_fan_rpm) {
1030 print_i8k_right_fan_rpm(obj, p, p_max_size);
1032 OBJ(i8k_ac_status) {
1033 print_i8k_ac_status(obj, p, p_max_size);
1035 OBJ(i8k_buttons_status) {
1036 print_i8k_buttons_status(obj, p, p_max_size);
1040 get_ibm_acpi_fan(obj, p, p_max_size);
1043 print_ibm_temps(obj, p, p_max_size);
1046 get_ibm_acpi_volume(obj, p, p_max_size);
1048 OBJ(ibm_brightness) {
1049 get_ibm_acpi_brightness(obj, p, p_max_size);
1052 /* information from sony_laptop kernel module
1053 * /sys/devices/platform/sony-laptop */
1054 OBJ(sony_fanspeed) {
1055 get_sony_fanspeed(p, p_max_size);
1058 if (!gateway_exists()) {
1063 print_gateway_iface(p, p_max_size);
1066 print_gateway_ip(p, p_max_size);
1069 snprintf(p, p_max_size, "%d", get_laptop_mode());
1072 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1074 #endif /* __linux__ */
1075 #if (defined(__FreeBSD__) || defined(__linux__))
1077 if (!interface_up(obj)) {
1083 OBJ(obsd_sensors_temp) {
1084 print_obsd_sensors_temp(obj, p, p_max_size);
1086 OBJ(obsd_sensors_fan) {
1087 print_obsd_sensors_fan(obj, p, p_max_size);
1089 OBJ(obsd_sensors_volt) {
1090 print_obsd_sensors_volt(obj, p, p_max_size);
1093 get_obsd_vendor(p, p_max_size);
1096 get_obsd_product(p, p_max_size);
1098 #endif /* __OpenBSD__ */
1101 new_font(p, obj->data.s);
1102 need_to_load_fonts = 1;
1106 print_diskio(obj, p, p_max_size);
1109 print_diskio_write(obj, p, p_max_size);
1112 print_diskio_read(obj, p, p_max_size);
1116 print_diskiograph(obj, p, p_max_size);
1118 OBJ(diskiograph_read) {
1119 print_diskiograph(obj, p, p_max_size);
1121 OBJ(diskiograph_write) {
1122 print_diskiograph(obj, p, p_max_size);
1126 print_downspeed(obj, p, p_max_size);
1129 print_downspeedf(obj, p, p_max_size);
1132 OBJ(downspeedgraph) {
1133 print_downspeedgraph(obj, p, p_max_size);
1137 /* Since we see you, you're if has not jumped.
1138 * Do Ninja jump here: without leaving traces.
1139 * This is to prevent us from stale jumped flags.
1141 obj = obj->ifblock_next;
1145 /* harmless object, just ignore */
1148 print_addr(obj, p, p_max_size);
1150 #if defined(__linux__)
1152 print_addrs(obj, p, p_max_size);
1154 #endif /* __linux__ */
1155 #if defined(IMLIB2) && defined(X11)
1157 /* doesn't actually draw anything, just queues it omp. the
1158 * image will get drawn after the X event loop */
1159 cimlib_add_image(obj->data.s);
1163 evaluate(obj->data.s, p, p_max_size);
1166 print_exec(obj, p, p_max_size);
1169 print_execp(obj, p, p_max_size);
1173 print_execgauge(obj, p, p_max_size);
1177 print_execbar(obj, p, p_max_size);
1181 print_execgraph(obj, p, p_max_size);
1185 print_execibar(obj, p, p_max_size);
1189 print_execigraph(obj, p, p_max_size);
1192 print_execigauge(obj, p, p_max_size);
1196 print_execi(obj, p, p_max_size);
1199 print_execpi(obj, p, p_max_size);
1202 print_texeci(obj, p, p_max_size);
1205 print_imap_unseen(obj, p, p_max_size);
1207 OBJ(imap_messages) {
1208 print_imap_messages(obj, p, p_max_size);
1211 print_pop3_unseen(obj, p, p_max_size);
1214 print_pop3_used(obj, p, p_max_size);
1217 print_fs_bar(obj, 0, p, p_max_size);
1220 print_fs_free(obj, p, p_max_size);
1223 print_fs_perc(obj, 1, p, p_max_size);
1226 print_fs_size(obj, p, p_max_size);
1229 print_fs_type(obj, p, p_max_size);
1232 print_fs_used(obj, p, p_max_size);
1235 print_fs_bar(obj, 1, p, p_max_size);
1238 print_fs_perc(obj, 0, p, p_max_size);
1241 print_loadavg(obj, p, p_max_size);
1244 new_goto(p, obj->data.i);
1251 new_hr(p, obj->data.i);
1255 print_nameserver(obj, p, p_max_size);
1259 print_eve(obj, p, p_max_size);
1264 curl_print(obj, p, p_max_size);
1269 rss_print_info(obj, p, p_max_size);
1274 print_weather(obj, p, p_max_size);
1278 OBJ(weather_forecast) {
1279 print_weather_forecast(obj, p, p_max_size);
1284 print_lua(obj, p, p_max_size);
1287 print_lua_parse(obj, p, p_max_size);
1290 print_lua_bar(obj, p, p_max_size);
1294 print_lua_graph(obj, p, p_max_size);
1297 print_lua_gauge(obj, p, p_max_size);
1300 #endif /* HAVE_LUA */
1306 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1307 snprintf(p, p_max_size, "N/A");
1309 temp_print(p, p_max_size, (double)val,
1310 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1315 new_offset(p, obj->data.i);
1318 new_voffset(p, obj->data.i);
1322 print_sysfs_sensor(obj, p, p_max_size);
1325 print_sysfs_sensor(obj, p, p_max_size);
1328 print_sysfs_sensor(obj, p, p_max_size);
1330 #endif /* __linux__ */
1332 new_alignr(p, obj->data.i);
1335 new_alignc(p, obj->data.i);
1338 char buf[max_user_text];
1339 struct information *tmp_info =
1340 malloc(sizeof(struct information));
1341 memcpy(tmp_info, cur, sizeof(struct information));
1342 generate_text_internal(buf, max_user_text,
1343 *obj->sub, tmp_info);
1345 if (strlen(buf) != 0) {
1351 char expression[max_user_text];
1353 struct information *tmp_info;
1355 tmp_info = malloc(sizeof(struct information));
1356 memcpy(tmp_info, cur, sizeof(struct information));
1357 generate_text_internal(expression, max_user_text,
1358 *obj->sub, tmp_info);
1359 DBGP("parsed arg into '%s'", expression);
1361 val = compare(expression);
1363 NORM_ERR("compare failed for expression '%s'",
1373 spc = strchr(obj->data.s, ' ');
1374 if (!spc && access(obj->data.s, F_OK)) {
1378 if (check_contains(obj->data.s, spc + 1))
1385 && (!check_mount(obj->data.s))) {
1391 if (!get_process_by_name(obj->data.s)) {
1393 if ((obj->data.s) && system(obj->data.s)) {
1398 #if defined(__linux__)
1400 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1404 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1407 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1412 human_readable(cur->mem * 1024, p, 255);
1415 human_readable(cur->memeasyfree * 1024, p, 255);
1418 human_readable(cur->memfree * 1024, p, 255);
1421 human_readable(cur->memmax * 1024, p, 255);
1425 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1429 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1434 if(output_methods & TO_X) {
1435 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1438 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1442 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1447 print_mixer(obj, p, p_max_size);
1450 print_mixerl(obj, p, p_max_size);
1453 print_mixerr(obj, p, p_max_size);
1457 print_mixer_bar(obj, p, p_max_size);
1460 print_mixerl_bar(obj, p, p_max_size);
1463 print_mixerr_bar(obj, p, p_max_size);
1466 OBJ(if_mixer_mute) {
1467 if (!check_mixer_muted(obj)) {
1472 #define NOT_IN_X "Not running in X"
1474 if(x_initialised != YES) {
1475 strncpy(p, NOT_IN_X, p_max_size);
1477 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1480 OBJ(monitor_number) {
1481 if(x_initialised != YES) {
1482 strncpy(p, NOT_IN_X, p_max_size);
1484 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1488 if(x_initialised != YES) {
1489 strncpy(p, NOT_IN_X, p_max_size);
1491 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1494 OBJ(desktop_number) {
1495 if(x_initialised != YES) {
1496 strncpy(p, NOT_IN_X, p_max_size);
1498 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1502 if(x_initialised != YES) {
1503 strncpy(p, NOT_IN_X, p_max_size);
1504 }else if(cur->x11.desktop.name != NULL) {
1505 strncpy(p, cur->x11.desktop.name, p_max_size);
1511 char buf[max_user_text];
1513 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1515 print_format_time(obj, p, p_max_size);
1519 print_mails(obj, p, p_max_size);
1522 print_new_mails(obj, p, p_max_size);
1525 print_seen_mails(obj, p, p_max_size);
1528 print_unseen_mails(obj, p, p_max_size);
1530 OBJ(flagged_mails) {
1531 print_flagged_mails(obj, p, p_max_size);
1533 OBJ(unflagged_mails) {
1534 print_unflagged_mails(obj, p, p_max_size);
1536 OBJ(forwarded_mails) {
1537 print_forwarded_mails(obj, p, p_max_size);
1539 OBJ(unforwarded_mails) {
1540 print_unforwarded_mails(obj, p, p_max_size);
1542 OBJ(replied_mails) {
1543 print_replied_mails(obj, p, p_max_size);
1545 OBJ(unreplied_mails) {
1546 print_unreplied_mails(obj, p, p_max_size);
1549 print_draft_mails(obj, p, p_max_size);
1551 OBJ(trashed_mails) {
1552 print_trashed_mails(obj, p, p_max_size);
1555 print_mboxscan(obj, p, p_max_size);
1558 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1561 new_outline(p, obj->data.l);
1564 char buf[max_user_text];
1566 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1568 print_pid_chroot(obj, p, p_max_size);
1571 char buf[max_user_text];
1573 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1575 print_pid_cmdline(obj, p, p_max_size);
1578 char buf[max_user_text];
1580 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1582 print_pid_cwd(obj, p, p_max_size);
1585 char buf[max_user_text];
1587 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1589 print_pid_environ(obj, p, p_max_size);
1591 OBJ(pid_environ_list) {
1592 char buf[max_user_text];
1594 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1596 print_pid_environ_list(obj, p, p_max_size);
1599 char buf[max_user_text];
1601 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1603 print_pid_exe(obj, p, p_max_size);
1606 char buf[max_user_text];
1608 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1610 print_pid_nice(obj, p, p_max_size);
1612 OBJ(pid_openfiles) {
1613 char buf[max_user_text];
1615 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1617 print_pid_openfiles(obj, p, p_max_size);
1620 char buf[max_user_text];
1622 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1624 print_pid_parent(obj, p, p_max_size);
1627 char buf[max_user_text];
1629 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1631 print_pid_priority(obj, p, p_max_size);
1634 char buf[max_user_text];
1636 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1638 print_pid_state(obj, p, p_max_size);
1640 OBJ(pid_state_short) {
1641 char buf[max_user_text];
1643 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1645 print_pid_state_short(obj, p, p_max_size);
1648 char buf[max_user_text];
1650 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1652 print_pid_stderr(obj, p, p_max_size);
1655 char buf[max_user_text];
1657 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1659 print_pid_stdin(obj, p, p_max_size);
1662 char buf[max_user_text];
1664 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1666 print_pid_stdout(obj, p, p_max_size);
1669 char buf[max_user_text];
1671 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1673 print_pid_threads(obj, p, p_max_size);
1675 OBJ(pid_thread_list) {
1676 char buf[max_user_text];
1678 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1680 print_pid_thread_list(obj, p, p_max_size);
1682 OBJ(pid_time_kernelmode) {
1683 char buf[max_user_text];
1685 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1687 print_pid_time_kernelmode(obj, p, p_max_size);
1689 OBJ(pid_time_usermode) {
1690 char buf[max_user_text];
1692 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1694 print_pid_time_usermode(obj, p, p_max_size);
1697 char buf[max_user_text];
1699 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1701 print_pid_time(obj, p, p_max_size);
1704 char buf[max_user_text];
1706 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1708 print_pid_uid(obj, p, p_max_size);
1711 char buf[max_user_text];
1713 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1715 print_pid_euid(obj, p, p_max_size);
1718 char buf[max_user_text];
1720 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1722 print_pid_suid(obj, p, p_max_size);
1725 char buf[max_user_text];
1727 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1729 print_pid_fsuid(obj, p, p_max_size);
1732 char buf[max_user_text];
1734 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1736 print_pid_gid(obj, p, p_max_size);
1739 char buf[max_user_text];
1741 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1743 print_pid_egid(obj, p, p_max_size);
1746 char buf[max_user_text];
1748 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1750 print_pid_sgid(obj, p, p_max_size);
1753 char buf[max_user_text];
1755 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1757 print_pid_fsgid(obj, p, p_max_size);
1760 char buf[max_user_text];
1762 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1764 print_pid_read(obj, p, p_max_size);
1767 char buf[max_user_text];
1769 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1771 print_pid_vmpeak(obj, p, p_max_size);
1774 char buf[max_user_text];
1776 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1778 print_pid_vmsize(obj, p, p_max_size);
1781 char buf[max_user_text];
1783 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1785 print_pid_vmlck(obj, p, p_max_size);
1788 char buf[max_user_text];
1790 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1792 print_pid_vmhwm(obj, p, p_max_size);
1795 char buf[max_user_text];
1797 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1799 print_pid_vmrss(obj, p, p_max_size);
1802 char buf[max_user_text];
1804 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1806 print_pid_vmdata(obj, p, p_max_size);
1809 char buf[max_user_text];
1811 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1813 print_pid_vmstk(obj, p, p_max_size);
1816 char buf[max_user_text];
1818 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1820 print_pid_vmexe(obj, p, p_max_size);
1823 char buf[max_user_text];
1825 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1827 print_pid_vmlib(obj, p, p_max_size);
1830 char buf[max_user_text];
1832 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1834 print_pid_vmpte(obj, p, p_max_size);
1837 char buf[max_user_text];
1839 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1841 print_pid_write(obj, p, p_max_size);
1844 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1846 OBJ(running_processes) {
1847 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1849 OBJ(running_threads) {
1850 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1853 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1856 snprintf(p, p_max_size, "%s", obj->data.s);
1860 new_bg(p, obj->data.l);
1863 new_stippled_hr(obj, p);
1867 human_readable(cur->swap * 1024, p, 255);
1870 human_readable(cur->swapfree * 1024, p, 255);
1873 human_readable(cur->swapmax * 1024, p, 255);
1876 if (cur->swapmax == 0) {
1877 strncpy(p, "No swap", p_max_size);
1879 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1884 if(output_methods & TO_X) {
1885 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1888 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1891 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1894 print_time(obj, p, p_max_size);
1897 print_utime(obj, p, p_max_size);
1900 print_tztime(obj, p, p_max_size);
1903 print_totaldown(obj, p, p_max_size);
1906 print_totalup(obj, p, p_max_size);
1909 char buf[max_user_text];
1911 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1913 print_gid_name(obj, p, p_max_size);
1916 char buf[max_user_text];
1918 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1920 print_uid_name(obj, p, p_max_size);
1923 snprintf(p, p_max_size, "%d", total_updates);
1926 if(total_updates % updatereset != obj->data.i - 1) {
1931 print_upspeed(obj, p, p_max_size);
1934 print_upspeedf(obj, p, p_max_size);
1938 print_upspeedgraph(obj, p, p_max_size);
1942 format_seconds_short(p, p_max_size, (int) cur->uptime);
1945 format_seconds(p, p_max_size, (int) cur->uptime);
1949 snprintf(p, p_max_size, "%s", cur->users.names);
1952 snprintf(p, p_max_size, "%s", cur->users.terms);
1955 snprintf(p, p_max_size, "%s", cur->users.times);
1958 update_user_time(obj->data.s);
1959 snprintf(p, p_max_size, "%s", cur->users.ctime);
1962 snprintf(p, p_max_size, "%d", cur->users.number);
1964 #endif /* __linux__ */
1965 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1966 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1970 msg = get_apm_adapter();
1971 snprintf(p, p_max_size, "%s", msg);
1974 OBJ(apm_battery_life) {
1977 msg = get_apm_battery_life();
1978 snprintf(p, p_max_size, "%s", msg);
1981 OBJ(apm_battery_time) {
1984 msg = get_apm_battery_time();
1985 snprintf(p, p_max_size, "%s", msg);
1988 #endif /* __FreeBSD__ __OpenBSD__ */
1991 #define mpd_printf(fmt, val) \
1992 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1993 #define mpd_sprintf(val) { \
1994 if (!obj->data.i || obj->data.i > p_max_size) \
1995 mpd_printf("%s", val); \
1997 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2002 mpd_sprintf(artist);
2006 mpd_printf("%s", random);
2008 mpd_printf("%s", repeat);
2016 mpd_printf("%d", volume);
2018 mpd_printf("%d", bitrate);
2020 mpd_printf("%s", status);
2022 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2025 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2028 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2032 if(output_methods & TO_X) {
2033 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
2036 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
2039 struct mpd_s *mpd = mpd_get_info();
2040 int len = obj->data.i;
2041 if (len == 0 || len > p_max_size)
2044 memset(p, 0, p_max_size);
2045 if (mpd->artist && *mpd->artist &&
2046 mpd->title && *mpd->title) {
2047 snprintf(p, len, "%s - %s", mpd->artist,
2049 } else if (mpd->title && *mpd->title) {
2050 snprintf(p, len, "%s", mpd->title);
2051 } else if (mpd->artist && *mpd->artist) {
2052 snprintf(p, len, "%s", mpd->artist);
2053 } else if (mpd->file && *mpd->file) {
2054 snprintf(p, len, "%s", mpd->file);
2059 OBJ(if_mpd_playing) {
2060 if (!mpd_get_info()->is_playing) {
2069 #define MOC_PRINT(t, a) \
2070 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2072 MOC_PRINT(state, "??");
2075 MOC_PRINT(file, "no file");
2078 MOC_PRINT(title, "no title");
2081 MOC_PRINT(artist, "no artist");
2084 MOC_PRINT(song, "no song");
2087 MOC_PRINT(album, "no album");
2089 OBJ(moc_totaltime) {
2090 MOC_PRINT(totaltime, "0:00");
2093 MOC_PRINT(timeleft, "0:00");
2096 MOC_PRINT(curtime, "0:00");
2099 MOC_PRINT(bitrate, "0Kbps");
2102 MOC_PRINT(rate, "0KHz");
2108 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2111 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2114 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2117 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2119 OBJ(xmms2_comment) {
2120 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2123 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2126 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2129 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2131 OBJ(xmms2_tracknr) {
2132 if (cur->xmms2.tracknr != -1) {
2133 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2136 OBJ(xmms2_bitrate) {
2137 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2140 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2143 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2145 OBJ(xmms2_elapsed) {
2146 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2147 (cur->xmms2.elapsed / 1000) % 60);
2149 OBJ(xmms2_duration) {
2150 snprintf(p, p_max_size, "%02d:%02d",
2151 cur->xmms2.duration / 60000,
2152 (cur->xmms2.duration / 1000) % 60);
2154 OBJ(xmms2_percent) {
2155 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2159 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
2162 OBJ(xmms2_playlist) {
2163 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2165 OBJ(xmms2_timesplayed) {
2166 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2169 if (strlen(cur->xmms2.title) < 2
2170 && strlen(cur->xmms2.title) < 2) {
2171 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2173 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2177 OBJ(if_xmms2_connected) {
2178 if (cur->xmms2.conn_state != 1) {
2184 OBJ(audacious_status) {
2185 snprintf(p, p_max_size, "%s",
2186 cur->audacious.items[AUDACIOUS_STATUS]);
2188 OBJ(audacious_title) {
2189 snprintf(p, cur->audacious.max_title_len > 0
2190 ? cur->audacious.max_title_len : p_max_size, "%s",
2191 cur->audacious.items[AUDACIOUS_TITLE]);
2193 OBJ(audacious_length) {
2194 snprintf(p, p_max_size, "%s",
2195 cur->audacious.items[AUDACIOUS_LENGTH]);
2197 OBJ(audacious_length_seconds) {
2198 snprintf(p, p_max_size, "%s",
2199 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2201 OBJ(audacious_position) {
2202 snprintf(p, p_max_size, "%s",
2203 cur->audacious.items[AUDACIOUS_POSITION]);
2205 OBJ(audacious_position_seconds) {
2206 snprintf(p, p_max_size, "%s",
2207 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2209 OBJ(audacious_bitrate) {
2210 snprintf(p, p_max_size, "%s",
2211 cur->audacious.items[AUDACIOUS_BITRATE]);
2213 OBJ(audacious_frequency) {
2214 snprintf(p, p_max_size, "%s",
2215 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2217 OBJ(audacious_channels) {
2218 snprintf(p, p_max_size, "%s",
2219 cur->audacious.items[AUDACIOUS_CHANNELS]);
2221 OBJ(audacious_filename) {
2222 snprintf(p, p_max_size, "%s",
2223 cur->audacious.items[AUDACIOUS_FILENAME]);
2225 OBJ(audacious_playlist_length) {
2226 snprintf(p, p_max_size, "%s",
2227 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2229 OBJ(audacious_playlist_position) {
2230 snprintf(p, p_max_size, "%s",
2231 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2233 OBJ(audacious_main_volume) {
2234 snprintf(p, p_max_size, "%s",
2235 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2238 OBJ(audacious_bar) {
2242 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2243 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2244 new_bar(obj, p, (int) (progress * 255.0f));
2247 #endif /* AUDACIOUS */
2251 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2254 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2257 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2260 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2263 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2266 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2269 /* we have four different types of top (top, top_mem,
2270 * top_time and top_io). To avoid having almost-same code four
2271 * times, we have this special handler. */
2280 print_top(obj, p, p_max_size);
2281 #endif /* __linux__ */
2283 print_tailhead("tail", obj, p, p_max_size);
2286 print_tailhead("head", obj, p, p_max_size);
2290 FILE *fp = open_file(obj->data.s, &rep);
2293 /* FIXME: use something more general (see also tail.c, head.c */
2294 #define BUFSZ 0x1000
2299 while(fgets(buf, BUFSZ, fp) != NULL){
2300 for(j = 0; buf[j] != 0; j++) {
2301 if(buf[j] == '\n') {
2306 sprintf(p, "%d", lines);
2309 sprintf(p, "File Unreadable");
2315 FILE *fp = open_file(obj->data.s, &rep);
2320 char inword = FALSE;
2323 while(fgets(buf, BUFSZ, fp) != NULL){
2324 for(j = 0; buf[j] != 0; j++) {
2325 if(!isspace(buf[j])) {
2326 if(inword == FALSE) {
2335 sprintf(p, "%d", words);
2338 sprintf(p, "File Unreadable");
2341 #ifdef TCP_PORT_MONITOR
2343 tcp_portmon_action(obj, p, p_max_size);
2345 #endif /* TCP_PORT_MONITOR */
2349 do_iconv_start(obj);
2354 #endif /* HAVE_ICONV */
2356 OBJ(entropy_avail) {
2357 print_entropy_avail(obj, p, p_max_size);
2360 print_entropy_perc(obj, p, p_max_size);
2362 OBJ(entropy_poolsize) {
2363 print_entropy_poolsize(obj, p, p_max_size);
2366 print_entropy_bar(obj, p, p_max_size);
2370 print_smapi(obj, p, p_max_size);
2372 OBJ(if_smapi_bat_installed) {
2374 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2375 if(!smapi_bat_installed(idx)) {
2379 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2381 OBJ(smapi_bat_perc) {
2382 print_smapi_bat_perc(obj, p, p_max_size);
2384 OBJ(smapi_bat_temp) {
2385 print_smapi_bat_temp(obj, p, p_max_size);
2387 OBJ(smapi_bat_power) {
2388 print_smapi_bat_power(obj, p, p_max_size);
2391 OBJ(smapi_bat_bar) {
2392 print_smapi_bat_bar(obj, p, p_max_size);
2398 char buf[max_user_text];
2400 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2401 snprintf(p, p_max_size, "%s", buf);
2407 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2408 char buf[max_user_text];
2411 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2412 snprintf(p, p_max_size, "%s", buf);
2413 if(total_updates % 2) {
2414 for(j=0; p[j] != 0; j++) {
2420 char buf[max_user_text];
2422 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2424 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2425 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2426 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2427 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2428 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2429 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2430 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2432 snprintf(p, p_max_size, "%s", buf);
2435 print_scroll(obj, p, p_max_size, cur);
2438 print_combine(obj, p, cur);
2442 print_nvidia_value(obj, display, p, p_max_size);
2447 /* This is just a meta-object to set host:port */
2450 snprintf(p, p_max_size, "%s",
2451 cur->apcupsd.items[APCUPSD_NAME]);
2453 OBJ(apcupsd_model) {
2454 snprintf(p, p_max_size, "%s",
2455 cur->apcupsd.items[APCUPSD_MODEL]);
2457 OBJ(apcupsd_upsmode) {
2458 snprintf(p, p_max_size, "%s",
2459 cur->apcupsd.items[APCUPSD_UPSMODE]);
2461 OBJ(apcupsd_cable) {
2462 snprintf(p, p_max_size, "%s",
2463 cur->apcupsd.items[APCUPSD_CABLE]);
2465 OBJ(apcupsd_status) {
2466 snprintf(p, p_max_size, "%s",
2467 cur->apcupsd.items[APCUPSD_STATUS]);
2469 OBJ(apcupsd_linev) {
2470 snprintf(p, p_max_size, "%s",
2471 cur->apcupsd.items[APCUPSD_LINEV]);
2474 snprintf(p, p_max_size, "%s",
2475 cur->apcupsd.items[APCUPSD_LOAD]);
2477 OBJ(apcupsd_loadbar) {
2480 if(output_methods & TO_X) {
2481 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2482 new_bar(obj, p, (int) progress);
2486 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2487 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2491 OBJ(apcupsd_loadgraph) {
2493 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2494 new_graph(obj, p, (int)progress);
2496 OBJ(apcupsd_loadgauge) {
2498 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2499 new_gauge(obj, p, (int)progress);
2502 OBJ(apcupsd_charge) {
2503 snprintf(p, p_max_size, "%s",
2504 cur->apcupsd.items[APCUPSD_CHARGE]);
2506 OBJ(apcupsd_timeleft) {
2507 snprintf(p, p_max_size, "%s",
2508 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2511 snprintf(p, p_max_size, "%s",
2512 cur->apcupsd.items[APCUPSD_TEMP]);
2514 OBJ(apcupsd_lastxfer) {
2515 snprintf(p, p_max_size, "%s",
2516 cur->apcupsd.items[APCUPSD_LASTXFER]);
2518 #endif /* APCUPSD */
2525 size_t a = strlen(p);
2528 iconv_convert(&a, buff_in, p, p_max_size);
2529 #endif /* HAVE_ICONV */
2530 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2532 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2533 #endif /* HAVE_LUA */
2535 substitute_newlines(p, a - 2);
2544 /* load any new fonts we may have had */
2545 if (need_to_load_fonts) {
2551 void evaluate(const char *text, char *p, int p_max_size)
2553 struct information *tmp_info;
2554 struct text_object subroot;
2556 tmp_info = malloc(sizeof(struct information));
2557 memcpy(tmp_info, &info, sizeof(struct information));
2558 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2559 DBGP("evaluated '%s' to '%s'", text, p);
2561 free_text_objects(&subroot, 1);
2565 double current_update_time, next_update_time, last_update_time;
2567 static void generate_text(void)
2569 struct information *cur = &info;
2576 current_update_time = get_time();
2580 /* add things to the buffer */
2586 generate_text_internal(p, max_user_text, global_root_object, cur);
2588 if (stuff_in_uppercase) {
2591 tmp_p = text_buffer;
2593 *tmp_p = toupper(*tmp_p);
2598 next_update_time += update_interval;
2599 if (next_update_time < get_time()) {
2600 next_update_time = get_time() + update_interval;
2601 } else if (next_update_time > get_time() + update_interval) {
2602 next_update_time = get_time() + update_interval;
2604 last_update_time = current_update_time;
2608 void set_update_interval(double interval)
2610 update_interval = interval;
2611 update_interval_old = interval;
2614 static inline int get_string_width(const char *s)
2616 return *s ? calc_text_width(s) : 0;
2620 static int get_string_width_special(char *s, int special_index)
2630 if ((output_methods & TO_X) == 0)
2633 p = strndup(s, text_buffer_size);
2637 if (*p == SPECIAL_CHAR) {
2638 /* shift everything over by 1 so that the special char
2639 * doesn't mess up the size calculation */
2640 for (i = 0; i < (long)strlen(p); i++) {
2641 *(p + i) = *(p + i + 1);
2643 if (specials[special_index + idx].type == GRAPH
2644 || specials[special_index + idx].type == GAUGE
2645 || specials[special_index + idx].type == BAR) {
2646 width += specials[special_index + idx].width;
2649 } else if (*p == SECRIT_MULTILINE_CHAR) {
2656 if (strlen(final) > 1) {
2657 width += calc_text_width(final);
2663 static int text_size_updater(char *s, int special_index);
2665 int last_font_height;
2666 static void update_text_area(void)
2670 if ((output_methods & TO_X) == 0)
2672 /* update text size if it isn't fixed */
2677 text_width = minimum_width;
2679 last_font_height = font_height();
2680 for_each_line(text_buffer, text_size_updater);
2682 if (text_height < minimum_height) {
2683 text_height = minimum_height;
2685 if (text_width > maximum_width && maximum_width > 0) {
2686 text_width = maximum_width;
2690 /* get text position on workarea */
2691 switch (text_alignment) {
2692 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2696 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2697 y = workarea[3] - text_height - gap_y;
2700 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2701 y = workarea[3] / 2 - text_height / 2 - gap_y;
2704 switch (text_alignment) {
2705 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2709 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2710 x = workarea[2] - text_width - gap_x;
2713 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2714 x = workarea[2] / 2 - text_width / 2 - gap_x;
2718 if (text_alignment == NONE) { // Let the WM manage the window
2725 #endif /* OWN_WINDOW */
2728 if (own_window && !fixed_pos) {
2731 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2732 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2733 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2734 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2738 /* If window size doesn't match to workarea's size,
2739 * then window probably includes panels (gnome).
2740 * Blah, doesn't work on KDE. */
2741 if (workarea[2] != window.width || workarea[3] != window.height) {
2750 /* update lua window globals */
2751 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2752 #endif /* HAVE_LUA */
2757 static int cur_x, cur_y; /* current x and y for drawing */
2759 //draw_mode also without X11 because we only need to print to stdout with FG
2760 static int draw_mode; /* FG, BG or OUTLINE */
2762 static long current_color;
2764 static int text_size_updater(char *s, int special_index)
2769 if ((output_methods & TO_X) == 0)
2771 /* get string widths and skip specials */
2774 if (*p == SPECIAL_CHAR) {
2776 w += get_string_width(s);
2779 if (specials[special_index].type == BAR
2780 || specials[special_index].type == GAUGE
2781 || specials[special_index].type == GRAPH) {
2782 w += specials[special_index].width;
2783 if (specials[special_index].height > last_font_height) {
2784 last_font_height = specials[special_index].height;
2785 last_font_height += font_height();
2787 } else if (specials[special_index].type == OFFSET) {
2788 if (specials[special_index].arg > 0) {
2789 w += specials[special_index].arg;
2791 } else if (specials[special_index].type == VOFFSET) {
2792 last_font_height += specials[special_index].arg;
2793 } else if (specials[special_index].type == GOTO) {
2794 if (specials[special_index].arg > cur_x) {
2795 w = (int) specials[special_index].arg;
2797 } else if (specials[special_index].type == TAB) {
2798 int start = specials[special_index].arg;
2799 int step = specials[special_index].width;
2801 if (!step || step < 0) {
2804 w += step - (cur_x - text_start_x - start) % step;
2805 } else if (specials[special_index].type == FONT) {
2806 selected_font = specials[special_index].font_added;
2807 if (font_height() > last_font_height) {
2808 last_font_height = font_height();
2814 } else if (*p == SECRIT_MULTILINE_CHAR) {
2817 lw = get_string_width(s);
2818 *p = SECRIT_MULTILINE_CHAR;
2820 w = lw > w ? lw : w;
2821 text_height += last_font_height;
2825 w += get_string_width(s);
2826 if (w > text_width) {
2829 if (text_width > maximum_width && maximum_width) {
2830 text_width = maximum_width;
2833 text_height += last_font_height;
2834 last_font_height = font_height();
2835 return special_index;
2839 static inline void set_foreground_color(long c)
2842 if (output_methods & TO_X) {
2844 XSetForeground(display, window.gc, c);
2848 if (output_methods & TO_NCURSES) {
2849 attron(COLOR_PAIR(c));
2851 #endif /* NCURSES */
2856 static void draw_string(const char *s)
2858 int i, i2, pos, width_of_s;
2861 char *s_with_newlines;
2867 width_of_s = get_string_width(s);
2868 s_with_newlines = strdup(s);
2869 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2870 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2871 s_with_newlines[i] = '\n';
2874 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2875 printf("%s\n", s_with_newlines);
2876 if (extra_newline) fputc('\n', stdout);
2877 fflush(stdout); /* output immediately, don't buffer */
2879 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2880 fprintf(stderr, "%s\n", s_with_newlines);
2881 fflush(stderr); /* output immediately, don't buffer */
2883 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2884 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2886 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2887 fprintf(append_fpointer, "%s\n", s_with_newlines);
2890 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2891 printw("%s", s_with_newlines);
2894 free(s_with_newlines);
2895 memset(tmpstring1, 0, text_buffer_size);
2896 memset(tmpstring2, 0, text_buffer_size);
2897 strncpy(tmpstring1, s, text_buffer_size - 1);
2902 if (output_methods & TO_X) {
2903 max = ((text_width - width_of_s) / get_string_width(" "));
2906 /* This code looks for tabs in the text and coverts them to spaces.
2907 * The trick is getting the correct number of spaces, and not going
2908 * over the window's size without forcing the window larger. */
2909 for (i = 0; i < (int) text_buffer_size; i++) {
2910 if (tmpstring1[i] == '\t') {
2912 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2913 /* guard against overrun */
2914 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2919 /* guard against overrun */
2920 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2925 if (output_methods & TO_X) {
2926 if (text_width == maximum_width) {
2927 /* this means the text is probably pushing the limit,
2928 * so we'll chop it */
2929 while (cur_x + get_string_width(tmpstring2) - text_start_x
2930 > maximum_width && strlen(tmpstring2) > 0) {
2931 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2938 if (output_methods & TO_X) {
2944 c.pixel = current_color;
2945 XQueryColor(display, DefaultColormap(display, screen), &c);
2948 c2.color.red = c.red;
2949 c2.color.green = c.green;
2950 c2.color.blue = c.blue;
2951 c2.color.alpha = fonts[selected_font].font_alpha;
2953 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2954 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2956 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2957 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2962 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2965 cur_x += width_of_s;
2968 memcpy(tmpstring1, s, text_buffer_size);
2971 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2979 int last_special_needed = -1;
2980 int orig_special_index = special_index;
2983 if (output_methods & TO_X) {
2984 font_h = font_height();
2985 cur_y += font_ascent();
2987 cur_x = text_start_x;
2991 if (*p == SECRIT_MULTILINE_CHAR) {
2992 /* special newline marker for multiline objects */
2997 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3002 /* draw string before special, unless we're dealing multiline
3004 if (last_special_applied > -1) {
3005 special_index = last_special_applied;
3013 switch (specials[special_index].type) {
3015 case HORIZONTAL_LINE:
3017 int h = specials[special_index].height;
3018 int mid = font_ascent() / 2;
3020 w = text_start_x + text_width - cur_x;
3022 XSetLineAttributes(display, window.gc, h, LineSolid,
3023 CapButt, JoinMiter);
3024 XDrawLine(display, window.drawable, window.gc, cur_x,
3025 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3031 int h = specials[special_index].height;
3032 int tmp_s = specials[special_index].arg;
3033 int mid = font_ascent() / 2;
3034 char ss[2] = { tmp_s, tmp_s };
3036 w = text_start_x + text_width - cur_x - 1;
3037 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3038 CapButt, JoinMiter);
3039 XSetDashes(display, window.gc, 0, ss, 2);
3040 XDrawLine(display, window.drawable, window.gc, cur_x,
3041 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3047 int h, bar_usage, by;
3048 if (cur_x - text_start_x > maximum_width
3049 && maximum_width > 0) {
3052 h = specials[special_index].height;
3053 bar_usage = specials[special_index].arg;
3054 by = cur_y - (font_ascent() / 2) - 1;
3059 w = specials[special_index].width;
3061 w = text_start_x + text_width - cur_x - 1;
3067 XSetLineAttributes(display, window.gc, 1, LineSolid,
3068 CapButt, JoinMiter);
3070 XDrawRectangle(display, window.drawable, window.gc, cur_x,
3072 XFillRectangle(display, window.drawable, window.gc, cur_x,
3073 by, w * bar_usage / 255, h);
3081 case GAUGE: /* new GAUGE */
3084 unsigned long last_colour = current_color;
3086 float angle, px, py;
3090 if (cur_x - text_start_x > maximum_width
3091 && maximum_width > 0) {
3095 h = specials[special_index].height;
3096 by = cur_y - (font_ascent() / 2) - 1;
3101 w = specials[special_index].width;
3103 w = text_start_x + text_width - cur_x - 1;
3109 XSetLineAttributes(display, window.gc, 1, LineSolid,
3110 CapButt, JoinMiter);
3112 XDrawArc(display, window.drawable, window.gc,
3113 cur_x, by, w, h * 2, 0, 180*64);
3116 usage = specials[special_index].arg;
3117 angle = (M_PI)*(float)(usage)/255.;
3118 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3119 py = (float)(by+(h))-(float)(h)*sin(angle);
3121 XDrawLine(display, window.drawable, window.gc,
3122 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3130 set_foreground_color(last_colour);
3138 int h, by, i = 0, j = 0;
3140 unsigned long last_colour = current_color;
3141 unsigned long *tmpcolour = 0;
3142 if (cur_x - text_start_x > maximum_width
3143 && maximum_width > 0) {
3146 h = specials[special_index].height;
3147 by = cur_y - (font_ascent() / 2) - 1;
3152 w = specials[special_index].width;
3154 w = text_start_x + text_width - cur_x - 1;
3159 if (draw_graph_borders) {
3160 XSetLineAttributes(display, window.gc, 1, LineSolid,
3161 CapButt, JoinMiter);
3162 XDrawRectangle(display, window.drawable, window.gc,
3165 XSetLineAttributes(display, window.gc, 1, LineSolid,
3166 CapButt, JoinMiter);
3168 if (specials[special_index].last_colour != 0
3169 || specials[special_index].first_colour != 0) {
3170 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3173 for (i = w - 2; i > -1; i--) {
3174 if (specials[special_index].last_colour != 0
3175 || specials[special_index].first_colour != 0) {
3176 if (specials[special_index].tempgrad) {
3179 (int)((float)(w - 2) - specials[special_index].graph[j] *
3180 (w - 2) / (float)specials[special_index].graph_scale)
3184 (int)((float)(w - 2) - specials[special_index].graph[j] *
3185 (w - 2) / (float)specials[special_index].graph_scale)
3188 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3190 (int)((float)(w - 2) - specials[special_index].graph[j] *
3191 (w - 2) / (float)specials[special_index].graph_scale)
3195 #endif /* DEBUG_lol */
3196 XSetForeground(display, window.gc, tmpcolour[
3197 (int)((float)(w - 2) - specials[special_index].graph[j] *
3198 (w - 2) / (float)specials[special_index].graph_scale)
3201 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3204 /* this is mugfugly, but it works */
3205 XDrawLine(display, window.drawable, window.gc,
3206 cur_x + i + 1, by + h, cur_x + i + 1,
3207 round_to_int((double)by + h - specials[special_index].graph[j] *
3208 (h - 1) / specials[special_index].graph_scale));
3209 if ((w - i) / ((float) (w - 2) /
3210 (specials[special_index].graph_width)) > j
3211 && j < MAX_GRAPH_DEPTH - 3) {
3215 if (tmpcolour) free(tmpcolour);
3220 /* if (draw_mode == BG) {
3221 set_foreground_color(default_bg_color);
3222 } else if (draw_mode == OUTLINE) {
3223 set_foreground_color(default_out_color);
3225 set_foreground_color(default_fg_color);
3227 if (show_graph_range) {
3230 unsigned short int seconds = update_interval * w;
3236 unsigned short int timeunits;
3238 timeunits = seconds / 86400; seconds %= 86400;
3239 if (timeunits > 0) {
3240 asprintf(&tmp_day_str, "%dd", timeunits);
3242 tmp_day_str = strdup("");
3244 timeunits = seconds / 3600; seconds %= 3600;
3245 if (timeunits > 0) {
3246 asprintf(&tmp_hour_str, "%dh", timeunits);
3248 tmp_hour_str = strdup("");
3250 timeunits = seconds / 60; seconds %= 60;
3251 if (timeunits > 0) {
3252 asprintf(&tmp_min_str, "%dm", timeunits);
3254 tmp_min_str = strdup("");
3257 asprintf(&tmp_sec_str, "%ds", seconds);
3259 tmp_sec_str = strdup("");
3261 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3262 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3264 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3266 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3267 cur_y += font_h / 2;
3268 draw_string(tmp_str);
3274 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3278 cur_x += font_ascent() / 2;
3279 cur_y += font_h / 2;
3281 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3283 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3284 draw_string(tmp_str);
3290 set_foreground_color(last_colour);
3296 int old = font_ascent();
3298 cur_y -= font_ascent();
3299 selected_font = specials[special_index].font_added;
3301 if (cur_y + font_ascent() < cur_y + old) {
3304 cur_y += font_ascent();
3306 font_h = font_height();
3311 if (draw_mode == FG) {
3312 set_foreground_color(specials[special_index].arg);
3318 if (draw_mode == BG) {
3319 set_foreground_color(specials[special_index].arg);
3324 if (draw_mode == OUTLINE) {
3325 set_foreground_color(specials[special_index].arg);
3330 w += specials[special_index].arg;
3331 last_special_needed = special_index;
3335 cur_y += specials[special_index].arg;
3339 if (specials[special_index].arg >= 0) {
3340 cur_x = (int) specials[special_index].arg;
3342 last_special_needed = special_index;
3347 int start = specials[special_index].arg;
3348 int step = specials[special_index].width;
3350 if (!step || step < 0) {
3353 w = step - (cur_x - text_start_x - start) % step;
3354 last_special_needed = special_index;
3360 /* TODO: add back in "+ window.border_inner_margin" to the end of
3362 int pos_x = text_start_x + text_width -
3363 get_string_width_special(s, special_index);
3365 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3366 "get_string_width(p) %i gap_x %i "
3367 "specials[special_index].arg %i window.border_inner_margin %i "
3368 "window.border_width %i\n", pos_x, text_start_x, text_width,
3369 cur_x, get_string_width_special(s), gap_x,
3370 specials[special_index].arg, window.border_inner_margin,
3371 window.border_width); */
3372 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3373 cur_x = pos_x - specials[special_index].arg;
3375 last_special_needed = special_index;
3381 int pos_x = (text_width) / 2 - get_string_width_special(s,
3382 special_index) / 2 - (cur_x -
3384 /* int pos_x = text_start_x + text_width / 2 -
3385 get_string_width_special(s) / 2; */
3387 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3388 "get_string_width(p) %i gap_x %i "
3389 "specials[special_index].arg %i\n", pos_x, text_start_x,
3390 text_width, cur_x, get_string_width(s), gap_x,
3391 specials[special_index].arg); */
3392 if (pos_x > specials[special_index].arg) {
3393 w = pos_x - specials[special_index].arg;
3395 last_special_needed = special_index;
3405 if (special_index != last_special_applied) {
3408 special_index = orig_special_index;
3409 last_special_applied = -1;
3420 if (output_methods & TO_NCURSES) {
3423 #endif /* NCURSES */
3425 if (output_methods & TO_X)
3426 cur_y += font_descent();
3428 if (recurse && *recurse) {
3429 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3430 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3432 return special_index;
3435 static int draw_line(char *s, int special_index)
3438 if (output_methods & TO_X) {
3439 return draw_each_line_inner(s, special_index, -1);
3443 if (output_methods & TO_NCURSES) {
3444 return draw_each_line_inner(s, special_index, -1);
3446 #endif /* NCURSES */
3448 UNUSED(special_index);
3452 static void draw_text(void)
3456 llua_draw_pre_hook();
3457 #endif /* HAVE_LUA */
3458 if (output_methods & TO_X) {
3459 cur_y = text_start_y;
3462 if (draw_borders && window.border_width > 0) {
3463 if (stippled_borders) {
3464 char ss[2] = { stippled_borders, stippled_borders };
3465 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3466 CapButt, JoinMiter);
3467 XSetDashes(display, window.gc, 0, ss, 2);
3469 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3470 CapButt, JoinMiter);
3473 XDrawRectangle(display, window.drawable, window.gc,
3474 text_start_x - window.border_inner_margin - window.border_width,
3475 text_start_y - window.border_inner_margin - window.border_width,
3476 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3477 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3485 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3486 attron(COLOR_PAIR(COLOR_WHITE));
3487 #endif /* NCURSES */
3488 for_each_line(text_buffer, draw_line);
3489 #if defined(HAVE_LUA) && defined(X11)
3490 llua_draw_post_hook();
3491 #endif /* HAVE_LUA */
3494 static void draw_stuff(void)
3497 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3499 if (overwrite_file) {
3500 overwrite_fpointer = fopen(overwrite_file, "w");
3501 if(!overwrite_fpointer)
3502 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3505 append_fpointer = fopen(append_file, "a");
3506 if(!append_fpointer)
3507 NORM_ERR("Can't append '%s' anymore", append_file);
3510 if (output_methods & TO_X) {
3512 if (draw_shades && !draw_outline) {
3515 set_foreground_color(default_bg_color);
3526 for (i = -1; i < 2; i++) {
3527 for (j = -1; j < 2; j++) {
3528 if (i == 0 && j == 0) {
3533 set_foreground_color(default_out_color);
3534 draw_mode = OUTLINE;
3542 set_foreground_color(default_fg_color);
3547 #if defined(X11) && defined(HAVE_XDBE)
3548 if (output_methods & TO_X) {
3549 xdbe_swap_buffers();
3551 #endif /* X11 && HAVE_XDBE */
3552 if(overwrite_fpointer) {
3553 fclose(overwrite_fpointer);
3554 overwrite_fpointer = 0;
3556 if(append_fpointer) {
3557 fclose(append_fpointer);
3558 append_fpointer = 0;
3563 static void clear_text(int exposures)
3567 /* The swap action is XdbeBackground, which clears */
3571 if (display && window.window) { // make sure these are !null
3572 /* there is some extra space for borders and outlines */
3573 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3574 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3575 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3576 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3581 static int need_to_update;
3583 /* update_text() generates new text and clears old text area */
3584 static void update_text(void)
3591 if (output_methods & TO_X)
3596 llua_update_info(&info, update_interval);
3597 #endif /* HAVE_LUA */
3600 #ifdef HAVE_SYS_INOTIFY_H
3604 static void main_loop(void)
3607 #ifdef SIGNAL_BLOCKING
3608 sigset_t newmask, oldmask;
3611 #ifdef HAVE_SYS_INOTIFY_H
3612 int inotify_config_wd = -1;
3613 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3614 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3615 char inotify_buff[INOTIFY_BUF_LEN];
3616 #endif /* HAVE_SYS_INOTIFY_H */
3619 #ifdef SIGNAL_BLOCKING
3620 sigemptyset(&newmask);
3621 sigaddset(&newmask, SIGINT);
3622 sigaddset(&newmask, SIGTERM);
3623 sigaddset(&newmask, SIGUSR1);
3626 last_update_time = 0.0;
3627 next_update_time = get_time();
3629 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3630 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3631 char buf[max_user_text];
3633 get_battery_short_status(buf, max_user_text, "BAT0");
3635 update_interval = update_interval_bat;
3637 update_interval = update_interval_old;
3642 #ifdef SIGNAL_BLOCKING
3643 /* block signals. we will inspect for pending signals later */
3644 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3645 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3650 if (output_methods & TO_X) {
3653 /* wait for X event or timeout */
3655 if (!XPending(display)) {
3659 t = next_update_time - get_time();
3663 } else if (t > update_interval) {
3664 t = update_interval;
3667 tv.tv_sec = (long) t;
3668 tv.tv_usec = (long) (t * 1000000) % 1000000;
3670 FD_SET(ConnectionNumber(display), &fdsr);
3672 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3674 if (errno != EINTR) {
3675 NORM_ERR("can't select(): %s", strerror(errno));
3685 if (need_to_update) {
3687 int wx = window.x, wy = window.y;
3697 /* resize window if it isn't right size */
3699 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3700 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3701 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3702 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3703 draw_stuff(); /* redraw everything in our newly sized window */
3704 XResizeWindow(display, window.window, window.width,
3705 window.height); /* resize window */
3706 set_transparent_background(window.window);
3709 xdbe_swap_buffers();
3714 /* update lua window globals */
3715 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3716 #endif /* HAVE_LUA */
3719 /* move window if it isn't in right position */
3720 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3721 XMoveWindow(display, window.window, window.x, window.y);
3726 if (changed && window.type == TYPE_PANEL) {
3729 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3732 switch (text_alignment) {
3759 set_struts(sidenum);
3770 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3771 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3772 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3773 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3774 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3779 /* handle X events */
3780 while (XPending(display)) {
3783 XNextEvent(display, &ev);
3790 r.width = ev.xexpose.width;
3791 r.height = ev.xexpose.height;
3792 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3796 case PropertyNotify:
3798 if ( ev.xproperty.state == PropertyNewValue ) {
3799 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3805 case ReparentNotify:
3806 /* set background to ParentRelative for all parents */
3808 set_transparent_background(window.window);
3812 case ConfigureNotify:
3814 /* if window size isn't what expected, set fixed size */
3815 if (ev.xconfigure.width != window.width
3816 || ev.xconfigure.height != window.height) {
3817 if (window.width != 0 && window.height != 0) {
3821 /* clear old stuff before screwing up
3826 XWindowAttributes attrs;
3827 if (XGetWindowAttributes(display,
3828 window.window, &attrs)) {
3829 window.width = attrs.width;
3830 window.height = attrs.height;
3834 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3835 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3836 if (text_width > maximum_width
3837 && maximum_width > 0) {
3838 text_width = maximum_width;
3842 /* if position isn't what expected, set fixed pos
3843 * total_updates avoids setting fixed_pos when window
3844 * is set to weird locations when started */
3845 /* // this is broken
3846 if (total_updates >= 2 && !fixed_pos
3847 && (window.x != ev.xconfigure.x
3848 || window.y != ev.xconfigure.y)
3849 && (ev.xconfigure.x != 0
3850 || ev.xconfigure.y != 0)) {
3858 /* if an ordinary window with decorations */
3859 if ((window.type == TYPE_NORMAL &&
3860 (!TEST_HINT(window.hints,
3861 HINT_UNDECORATED))) ||
3862 window.type == TYPE_DESKTOP) {
3863 /* allow conky to hold input focus. */
3866 /* forward the click to the desktop window */
3867 XUngrabPointer(display, ev.xbutton.time);
3868 ev.xbutton.window = window.desktop;
3869 ev.xbutton.x = ev.xbutton.x_root;
3870 ev.xbutton.y = ev.xbutton.y_root;
3871 XSendEvent(display, ev.xbutton.window, False,
3872 ButtonPressMask, &ev);
3873 XSetInputFocus(display, ev.xbutton.window,
3874 RevertToParent, ev.xbutton.time);
3881 /* if an ordinary window with decorations */
3882 if ((window.type == TYPE_NORMAL)
3883 && (!TEST_HINT(window.hints,
3884 HINT_UNDECORATED))) {
3885 /* allow conky to hold input focus. */
3888 /* forward the release to the desktop window */
3889 ev.xbutton.window = window.desktop;
3890 ev.xbutton.x = ev.xbutton.x_root;
3891 ev.xbutton.y = ev.xbutton.y_root;
3892 XSendEvent(display, ev.xbutton.window, False,
3893 ButtonReleaseMask, &ev);
3902 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3903 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3905 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3906 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3908 #endif /* HAVE_XDAMAGE */
3914 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3915 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3916 #endif /* HAVE_XDAMAGE */
3918 /* XDBE doesn't seem to provide a way to clear the back buffer
3919 * without interfering with the front buffer, other than passing
3920 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3921 * using XDBE, we need to redraw the text even if it wasn't part of
3922 * the exposed area. OTOH, if we're not going to call draw_stuff at
3923 * all, then no swap happens and we can safely do nothing. */
3925 if (!XEmptyRegion(x11_stuff.region)) {
3930 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3931 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3932 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3933 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3934 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3937 XSetRegion(display, window.gc, x11_stuff.region);
3940 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3944 XDestroyRegion(x11_stuff.region);
3945 x11_stuff.region = XCreateRegion();
3949 t = (next_update_time - get_time()) * 1000000;
3950 if(t > 0) usleep((useconds_t)t);
3954 if(output_methods & TO_NCURSES) {
3963 #ifdef SIGNAL_BLOCKING
3964 /* unblock signals of interest and let handler fly */
3965 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3966 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3970 switch (g_signal_pending) {
3973 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3978 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3981 if (output_methods & TO_X) {
3982 XDestroyRegion(x11_stuff.region);
3983 x11_stuff.region = NULL;
3985 XDamageDestroy(display, x11_stuff.damage);
3986 XFixesDestroyRegion(display, x11_stuff.region2);
3987 XFixesDestroyRegion(display, x11_stuff.part);
3988 #endif /* HAVE_XDAMAGE */
3994 if(overwrite_file) {
3995 free(overwrite_file);
4004 /* Reaching here means someone set a signal
4005 * (SIGXXXX, signal_handler), but didn't write any code
4007 * If you don't want to handle a signal, don't set a handler on
4008 * it in the first place. */
4009 if (g_signal_pending) {
4010 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4014 #ifdef HAVE_SYS_INOTIFY_H
4015 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4016 inotify_config_wd = inotify_add_watch(inotify_fd,
4020 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4021 int len = 0, idx = 0;
4023 struct timeval time_to_wait;
4025 FD_ZERO(&descriptors);
4026 FD_SET(inotify_fd, &descriptors);
4028 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4030 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4031 if (FD_ISSET(inotify_fd, &descriptors)) {
4032 /* process inotify events */
4033 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4034 while (len > 0 && idx < len) {
4035 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4036 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4037 /* current_config should be reloaded */
4038 NORM_ERR("'%s' modified, reloading...", current_config);
4040 if (ev->mask & IN_IGNORED) {
4041 /* for some reason we get IN_IGNORED here
4042 * sometimes, so we need to re-add the watch */
4043 inotify_config_wd = inotify_add_watch(inotify_fd,
4050 llua_inotify_query(ev->wd, ev->mask);
4052 #endif /* HAVE_LUA */
4053 idx += INOTIFY_EVENT_SIZE + ev->len;
4057 #endif /* HAVE_SYS_INOTIFY_H */
4060 llua_update_info(&info, update_interval);
4061 #endif /* HAVE_LUA */
4062 g_signal_pending = 0;
4064 clean_up(NULL, NULL);
4066 #ifdef HAVE_SYS_INOTIFY_H
4067 if (inotify_fd != -1) {
4068 inotify_rm_watch(inotify_fd, inotify_config_wd);
4070 inotify_fd = inotify_config_wd = 0;
4072 #endif /* HAVE_SYS_INOTIFY_H */
4076 static void load_config_file_x11(const char *);
4078 void initialisation(int argc, char** argv);
4080 /* reload the config file */
4081 static void reload_config(void)
4083 char *current_config_copy = strdup(current_config);
4084 clean_up(NULL, NULL);
4085 current_config = current_config_copy;
4086 initialisation(argc_copy, argv_copy);
4089 void clean_up(void *memtofree1, void* memtofree2)
4094 if(output_methods & TO_NCURSES) {
4098 conftree_empty(currentconffile);
4099 currentconffile = NULL;
4106 timed_thread_destroy_registered_threads();
4108 if (info.cpu_usage) {
4109 free(info.cpu_usage);
4110 info.cpu_usage = NULL;
4113 if (x_initialised == YES) {
4114 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4115 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4116 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4117 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4120 if(x11_stuff.region) {
4121 XDestroyRegion(x11_stuff.region);
4122 x11_stuff.region = NULL;
4124 XCloseDisplay(display);
4126 if(info.x11.desktop.all_names) {
4127 free(info.x11.desktop.all_names);
4128 info.x11.desktop.all_names = NULL;
4130 if (info.x11.desktop.name) {
4131 free(info.x11.desktop.name);
4132 info.x11.desktop.name = NULL;
4136 free(fonts); //in set_default_configurations a font is set but not loaded
4142 free_update_callbacks();
4146 free_text_objects(&global_root_object, 0);
4165 free(current_config);
4168 #ifdef TCP_PORT_MONITOR
4169 tcp_portmon_clear();
4178 weather_free_info();
4181 llua_shutdown_hook();
4183 #endif /* HAVE_LUA */
4185 if (output_methods & TO_X)
4193 for (i = 0; i < special_count; i++) {
4194 if (specials[i].type == GRAPH) {
4195 free(specials[i].graph);
4203 clear_diskio_stats();
4204 if(global_cpu != NULL) {
4210 static int string_to_bool(const char *s)
4213 // Assumes an option without a true/false means true
4215 } else if (strcasecmp(s, "yes") == EQUAL) {
4217 } else if (strcasecmp(s, "true") == EQUAL) {
4219 } else if (strcasecmp(s, "1") == EQUAL) {
4226 static enum alignment string_to_alignment(const char *s)
4228 if (strcasecmp(s, "top_left") == EQUAL) {
4230 } else if (strcasecmp(s, "top_right") == EQUAL) {
4232 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4234 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4236 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4237 return BOTTOM_RIGHT;
4238 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4239 return BOTTOM_MIDDLE;
4240 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4242 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4243 return MIDDLE_RIGHT;
4244 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4245 return MIDDLE_MIDDLE;
4246 } else if (strcasecmp(s, "tl") == EQUAL) {
4248 } else if (strcasecmp(s, "tr") == EQUAL) {
4250 } else if (strcasecmp(s, "tm") == EQUAL) {
4252 } else if (strcasecmp(s, "bl") == EQUAL) {
4254 } else if (strcasecmp(s, "br") == EQUAL) {
4255 return BOTTOM_RIGHT;
4256 } else if (strcasecmp(s, "bm") == EQUAL) {
4257 return BOTTOM_MIDDLE;
4258 } else if (strcasecmp(s, "ml") == EQUAL) {
4260 } else if (strcasecmp(s, "mr") == EQUAL) {
4261 return MIDDLE_RIGHT;
4262 } else if (strcasecmp(s, "mm") == EQUAL) {
4263 return MIDDLE_MIDDLE;
4264 } else if (strcasecmp(s, "none") == EQUAL) {
4272 static void set_default_configurations_for_x(void)
4274 default_fg_color = WhitePixel(display, screen);
4275 default_bg_color = BlackPixel(display, screen);
4276 default_out_color = BlackPixel(display, screen);
4277 color0 = default_fg_color;
4278 color1 = default_fg_color;
4279 color2 = default_fg_color;
4280 color3 = default_fg_color;
4281 color4 = default_fg_color;
4282 color5 = default_fg_color;
4283 color6 = default_fg_color;
4284 color7 = default_fg_color;
4285 color8 = default_fg_color;
4286 color9 = default_fg_color;
4287 current_text_color = default_fg_color;
4291 static void set_default_configurations(void)
4298 fork_to_background = 0;
4299 total_run_times = 0;
4300 info.cpu_avg_samples = 2;
4301 info.net_avg_samples = 2;
4302 info.diskio_avg_samples = 2;
4307 format_human_readable = 1;
4317 mpd_env_host = getenv("MPD_HOST");
4318 mpd_env_port = getenv("MPD_PORT");
4320 if (!mpd_env_host || !strlen(mpd_env_host)) {
4321 mpd_set_host("localhost");
4323 /* MPD_HOST environment variable is set */
4324 char *mpd_hostpart = strchr(mpd_env_host, '@');
4325 if (!mpd_hostpart) {
4326 mpd_set_host(mpd_env_host);
4328 /* MPD_HOST contains a password */
4329 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4330 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4332 if (!strlen(mpd_hostpart + 1)) {
4333 mpd_set_host("localhost");
4335 mpd_set_host(mpd_hostpart + 1);
4338 mpd_set_password(mpd_password, 1);
4343 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4344 /* failed to set port from environment variable */
4345 mpd_set_port("6600");
4349 info.xmms2.artist = NULL;
4350 info.xmms2.album = NULL;
4351 info.xmms2.title = NULL;
4352 info.xmms2.genre = NULL;
4353 info.xmms2.comment = NULL;
4354 info.xmms2.url = NULL;
4355 info.xmms2.status = NULL;
4356 info.xmms2.playlist = NULL;
4358 use_spacer = NO_SPACER;
4360 output_methods = TO_X;
4362 output_methods = TO_STDOUT;
4365 show_graph_scale = 0;
4366 show_graph_range = 0;
4369 draw_graph_borders = 1;
4371 set_first_font("6x10");
4379 window.type = TYPE_NORMAL;
4381 strcpy(window.class_name, PACKAGE_NAME);
4382 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4384 stippled_borders = 0;
4385 window.border_inner_margin = 3;
4386 window.border_outer_margin = 1;
4387 window.border_width = 1;
4388 text_alignment = BOTTOM_LEFT;
4389 info.x11.monitor.number = 1;
4390 info.x11.monitor.current = 0;
4391 info.x11.desktop.current = 1;
4392 info.x11.desktop.number = 1;
4393 info.x11.desktop.nitems = 0;
4394 info.x11.desktop.all_names = NULL;
4395 info.x11.desktop.name = NULL;
4400 free(current_mail_spool);
4404 variable_substitute(MAIL_FILE, buf, 256);
4405 if (buf[0] != '\0') {
4406 current_mail_spool = strndup(buf, text_buffer_size);
4411 set_update_interval(3);
4412 update_interval_bat = NOBATTERY;
4413 info.music_player_interval = 1.0;
4414 stuff_in_uppercase = 0;
4415 info.users.number = 1;
4417 set_times_in_seconds(0);
4419 #ifdef TCP_PORT_MONITOR
4420 /* set default connection limit */
4421 tcp_portmon_set_max_connections(0);
4425 /* returns 1 if you can overwrite or create the file at 'path' */
4426 static _Bool overwrite_works(const char *path)
4430 if (!(filepointer = fopen(path, "w")))
4432 fclose(filepointer);
4436 /* returns 1 if you can append or create the file at 'path' */
4437 static _Bool append_works(const char *path)
4441 if (!(filepointer = fopen(path, "a")))
4443 fclose(filepointer);
4449 /* WARNING, this type not in Xlib spec */
4450 int x11_error_handler(Display *d, XErrorEvent *err)
4451 __attribute__((noreturn));
4452 int x11_error_handler(Display *d, XErrorEvent *err)
4454 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",
4456 (long unsigned)err->display,
4457 (long)err->resourceid,
4467 int x11_ioerror_handler(Display *d)
4468 __attribute__((noreturn));
4469 int x11_ioerror_handler(Display *d)
4471 NORM_ERR("X Error: Display %lx\n",
4478 static void X11_initialisation(void)
4480 if (x_initialised == YES) return;
4481 output_methods |= TO_X;
4483 set_default_configurations_for_x();
4484 x_initialised = YES;
4487 /* WARNING, this type not in Xlib spec */
4488 XSetErrorHandler(&x11_error_handler);
4489 XSetIOErrorHandler(&x11_ioerror_handler);
4493 static char **xargv = 0;
4494 static int xargc = 0;
4496 static void X11_create_window(void)
4498 if (output_methods & TO_X) {
4500 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4501 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4503 #else /* OWN_WINDOW */
4504 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4505 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4507 #endif /* OWN_WINDOW */
4511 update_text_area(); /* to position text/window on screen */
4514 if (own_window && !fixed_pos) {
4515 XMoveWindow(display, window.window, window.x, window.y);
4518 set_transparent_background(window.window);
4526 x11_stuff.region = XCreateRegion();
4528 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4529 NORM_ERR("Xdamage extension unavailable");
4531 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4532 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4533 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4534 #endif /* HAVE_XDAMAGE */
4537 update_text_area(); /* to get initial size of the window */
4540 /* setup lua window globals */
4541 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4542 #endif /* HAVE_LUA */
4546 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4547 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4548 #define CONF2(a) if (strcasecmp(name, a) == 0)
4549 #define CONF(a) else CONF2(a)
4550 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4551 || strcasecmp(name, b) == 0)
4552 #define CONF_CONTINUE 1
4553 #define CONF_BREAK 2
4554 #define CONF_BUFF_SIZE 512
4556 static FILE *open_config_file(const char *f)
4558 #ifdef CONFIG_OUTPUT
4559 if (!strcmp(f, "==builtin==")) {
4560 return conf_cookie_open();
4562 #endif /* CONFIG_OUTPUT */
4563 return fopen(f, "r");
4566 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4570 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4573 remove_comments(buf);
4578 while (*p && isspace((int) *p)) {
4582 return CONF_CONTINUE; /* empty line */
4589 while (*p2 && !isspace((int) *p2)) {
4593 *p2 = '\0'; /* break at name's end */
4600 while (*p && isspace((int) *p)) {
4606 p2 = *value + strlen(*value);
4607 while (isspace((int) *(p2 - 1))) {
4616 char load_config_file(const char *f)
4621 fp = open_config_file(f);
4625 DBGP("reading contents from config file '%s'", f);
4628 char buff[CONF_BUFF_SIZE], *name, *value;
4629 int ret = do_config_step(&line, fp, buff, &name, &value);
4630 if (ret == CONF_BREAK) {
4632 } else if (ret == CONF_CONTINUE) {
4638 /* don't listen if X is already initialised or
4639 * if we already know we don't want it */
4640 if(x_initialised != YES) {
4641 if (string_to_bool(value)) {
4642 output_methods &= TO_X;
4644 output_methods &= ~TO_X;
4645 x_initialised = NEVER;
4650 if (!value || x_initialised == YES) {
4655 disp = strdup(value);
4660 if (window.type == TYPE_DOCK)
4663 #endif /*OWN_WINDOW */
4665 int a = string_to_alignment(value);
4676 CONF("background") {
4677 fork_to_background = string_to_bool(value);
4680 CONF2("background") {
4681 fork_to_background = string_to_bool(value);
4685 CONF("show_graph_scale") {
4686 show_graph_scale = string_to_bool(value);
4688 CONF("show_graph_range") {
4689 show_graph_range = string_to_bool(value);
4691 CONF("border_inner_margin") {
4693 window.border_inner_margin = strtol(value, 0, 0);
4694 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4699 CONF("border_outer_margin") {
4701 window.border_outer_margin = strtol(value, 0, 0);
4702 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4707 CONF("border_width") {
4709 window.border_width = strtol(value, 0, 0);
4710 if (window.border_width < 0) window.border_width = 0;
4716 #define TEMPLATE_CONF(n) \
4717 CONF("template"#n) { \
4718 if (set_template(n, value)) \
4733 parse_global_imap_mail_args(value);
4740 parse_global_pop3_mail_args(value);
4745 CONF("default_bar_size") {
4748 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4755 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4759 CONF("default_graph_size") {
4762 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4769 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4772 CONF("default_gauge_size") {
4775 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4782 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4789 mpd_set_host(value);
4795 if (value && mpd_set_port(value)) {
4799 CONF("mpd_password") {
4801 mpd_set_password(value, 0);
4807 CONF("music_player_interval") {
4809 info.music_player_interval = strtod(value, 0);
4815 CONF("sensor_device") {
4817 sensor_device = strtol(value, 0, 0);
4823 CONF("cpu_avg_samples") {
4825 cpu_avg_samples = strtol(value, 0, 0);
4826 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4829 info.cpu_avg_samples = cpu_avg_samples;
4835 CONF("net_avg_samples") {
4837 net_avg_samples = strtol(value, 0, 0);
4838 if (net_avg_samples < 1 || net_avg_samples > 14) {
4841 info.net_avg_samples = net_avg_samples;
4847 CONF("diskio_avg_samples") {
4849 diskio_avg_samples = strtol(value, 0, 0);
4850 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4853 info.diskio_avg_samples = diskio_avg_samples;
4861 CONF("double_buffer") {
4862 use_xdbe = string_to_bool(value);
4866 CONF("override_utf8_locale") {
4867 utf8_mode = string_to_bool(value);
4869 CONF("draw_borders") {
4870 draw_borders = string_to_bool(value);
4872 CONF("draw_graph_borders") {
4873 draw_graph_borders = string_to_bool(value);
4875 CONF("draw_shades") {
4876 draw_shades = string_to_bool(value);
4878 CONF("draw_outline") {
4879 draw_outline = string_to_bool(value);
4882 CONF("times_in_seconds") {
4883 set_times_in_seconds(string_to_bool(value));
4885 CONF("out_to_console") {
4886 if(string_to_bool(value)) {
4887 output_methods |= TO_STDOUT;
4889 output_methods &= ~TO_STDOUT;
4892 CONF("extra_newline") {
4893 extra_newline = string_to_bool(value);
4895 CONF("out_to_stderr") {
4896 if(string_to_bool(value))
4897 output_methods |= TO_STDERR;
4900 CONF("out_to_ncurses") {
4901 if(string_to_bool(value)) {
4904 output_methods |= TO_NCURSES;
4908 CONF("overwrite_file") {
4909 if(overwrite_file) {
4910 free(overwrite_file);
4913 if(overwrite_works(value)) {
4914 overwrite_file = strdup(value);
4915 output_methods |= OVERWRITE_FILE;
4917 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4919 CONF("append_file") {
4924 if(append_works(value)) {
4925 append_file = strdup(value);
4926 output_methods |= APPEND_FILE;
4928 NORM_ERR("append_file won't be able to create/append '%s'", value);
4930 CONF("use_spacer") {
4932 if (strcasecmp(value, "left") == EQUAL) {
4933 use_spacer = LEFT_SPACER;
4934 } else if (strcasecmp(value, "right") == EQUAL) {
4935 use_spacer = RIGHT_SPACER;
4936 } else if (strcasecmp(value, "none") == EQUAL) {
4937 use_spacer = NO_SPACER;
4939 use_spacer = string_to_bool(value);
4940 NORM_ERR("use_spacer should have an argument of left, right, or"
4941 " none. '%s' seems to be some form of '%s', so"
4942 " defaulting to %s.", value,
4943 use_spacer ? "true" : "false",
4944 use_spacer ? "right" : "none");
4946 use_spacer = RIGHT_SPACER;
4948 use_spacer = NO_SPACER;
4952 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4953 use_spacer = RIGHT_SPACER;
4959 use_xft = string_to_bool(value);
4963 set_first_font(value);
4967 if (value && font_count >= 0) {
4968 fonts[0].font_alpha = atof(value) * 65535.0;
4975 if (string_to_bool(value)) {
4976 NORM_ERR("Xft not enabled at compile time");
4980 /* xftfont silently ignored when no Xft */
4983 /* xftalpha is silently ignored when no Xft */
4988 set_first_font(value);
4996 gap_x = atoi(value);
5003 gap_y = atoi(value);
5009 CONF("mail_spool") {
5013 variable_substitute(value, buffer, 256);
5015 if (buffer[0] != '\0') {
5016 if (current_mail_spool) {
5017 free(current_mail_spool);
5019 current_mail_spool = strndup(buffer, text_buffer_size);
5026 CONF("minimum_size") {
5028 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5030 if (sscanf(value, "%d", &minimum_width) != 1) {
5038 CONF("maximum_width") {
5040 if (sscanf(value, "%d", &maximum_width) != 1) {
5048 CONF("no_buffers") {
5049 no_buffers = string_to_bool(value);
5051 CONF("top_name_width") {
5052 if (set_top_name_width(value))
5055 CONF("top_cpu_separate") {
5056 cpu_separate = string_to_bool(value);
5058 CONF("short_units") {
5059 short_units = string_to_bool(value);
5061 CONF("format_human_readable") {
5062 format_human_readable = string_to_bool(value);
5065 CONF("hddtemp_host") {
5066 set_hddtemp_host(value);
5068 CONF("hddtemp_port") {
5069 set_hddtemp_port(value);
5071 #endif /* HDDTEMP */
5072 CONF("pad_percents") {
5073 pad_percents = atoi(value);
5077 CONF("own_window") {
5079 own_window = string_to_bool(value);
5082 CONF("own_window_class") {
5084 memset(window.class_name, 0, sizeof(window.class_name));
5085 strncpy(window.class_name, value,
5086 sizeof(window.class_name) - 1);
5089 CONF("own_window_title") {
5091 memset(window.title, 0, sizeof(window.title));
5092 strncpy(window.title, value, sizeof(window.title) - 1);
5095 CONF("own_window_transparent") {
5097 set_transparent = string_to_bool(value);
5100 CONF("own_window_hints") {
5102 char *p_hint, *p_save;
5103 char delim[] = ", ";
5105 /* tokenize the value into individual hints */
5106 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5108 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5109 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5110 SET_HINT(window.hints, HINT_UNDECORATED);
5111 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5112 SET_HINT(window.hints, HINT_BELOW);
5113 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5114 SET_HINT(window.hints, HINT_ABOVE);
5115 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5116 SET_HINT(window.hints, HINT_STICKY);
5117 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5118 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5119 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5120 SET_HINT(window.hints, HINT_SKIP_PAGER);
5125 p_hint = strtok_r(NULL, delim, &p_save);
5126 } while (p_hint != NULL);
5132 CONF("own_window_type") {
5134 if (strncmp(value, "normal", 6) == EQUAL) {
5135 window.type = TYPE_NORMAL;
5136 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5137 window.type = TYPE_DESKTOP;
5138 } else if (strncmp(value, "dock", 4) == EQUAL) {
5139 window.type = TYPE_DOCK;
5140 text_alignment = TOP_LEFT;
5141 } else if (strncmp(value, "panel", 5) == EQUAL) {
5142 window.type = TYPE_PANEL;
5143 } else if (strncmp(value, "override", 8) == EQUAL) {
5144 window.type = TYPE_OVERRIDE;
5153 CONF("stippled_borders") {
5155 stippled_borders = strtol(value, 0, 0);
5157 stippled_borders = 4;
5161 CONF("imlib_cache_size") {
5163 cimlib_set_cache_size(atoi(value));
5166 CONF("imlib_cache_flush_interval") {
5168 cimlib_set_cache_flush_interval(atoi(value));
5173 CONF("update_interval_on_battery") {
5175 update_interval_bat = strtod(value, 0);
5180 CONF("update_interval") {
5182 set_update_interval(strtod(value, 0));
5186 if (info.music_player_interval == 0) {
5187 // default to update_interval
5188 info.music_player_interval = update_interval;
5191 CONF("total_run_times") {
5193 total_run_times = strtod(value, 0);
5199 stuff_in_uppercase = string_to_bool(value);
5201 CONF("max_specials") {
5203 max_specials = atoi(value);
5208 CONF("max_user_text") {
5210 max_user_text = atoi(value);
5215 CONF("text_buffer_size") {
5217 text_buffer_size = atoi(value);
5218 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5219 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5220 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5228 if (output_methods & TO_X) {
5229 X11_initialisation();
5238 global_text = (char *) malloc(1);
5239 global_text[0] = '\0';
5242 unsigned int l = strlen(global_text);
5244 char buf[CONF_BUFF_SIZE];
5246 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5252 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5260 /* Check for continuation of \\-\n. */
5261 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5262 global_text[l-1] = '\0';
5266 global_text = (char *) realloc(global_text, l + bl + 1);
5267 strcat(global_text, buf);
5269 if (strlen(global_text) > max_user_text) {
5273 global_text_lines = line + 1;
5276 #ifdef TCP_PORT_MONITOR
5277 CONF("max_port_monitor_connections") {
5279 if (!value || (sscanf(value, "%d", &max) != 1)) {
5280 /* an error. use default, warn and continue. */
5281 tcp_portmon_set_max_connections(0);
5283 } else if (tcp_portmon_set_max_connections(max)) {
5284 /* max is < 0, default has been set*/
5289 CONF("if_up_strictness") {
5291 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5292 ifup_strictness = IFUP_UP;
5293 } else if (strcasecmp(value, "up") == EQUAL) {
5294 ifup_strictness = IFUP_UP;
5295 } else if (strcasecmp(value, "link") == EQUAL) {
5296 ifup_strictness = IFUP_LINK;
5297 } else if (strcasecmp(value, "address") == EQUAL) {
5298 ifup_strictness = IFUP_ADDR;
5300 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5301 ifup_strictness = IFUP_UP;
5305 CONF("temperature_unit") {
5307 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5308 } else if (set_temp_output_unit(value)) {
5309 NORM_ERR("temperature_unit: incorrect argument");
5316 char *ptr = strtok(value, " ");
5319 ptr = strtok(NULL, " ");
5326 CONF("lua_draw_hook_pre") {
5328 llua_set_draw_pre_hook(value);
5333 CONF("lua_draw_hook_post") {
5335 llua_set_draw_post_hook(value);
5340 CONF("lua_startup_hook") {
5342 llua_set_startup_hook(value);
5347 CONF("lua_shutdown_hook") {
5349 llua_set_shutdown_hook(value);
5355 #endif /* HAVE_LUA */
5367 CONF("default_color"){}
5368 CONF3("default_shade_color", "default_shadecolor"){}
5369 CONF3("default_outline_color", "default_outlinecolor") {}
5370 CONF("own_window_colour") {}
5373 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5379 if (info.music_player_interval == 0) {
5380 // default to update_interval
5381 info.music_player_interval = update_interval;
5383 if (!global_text) { // didn't supply any text
5384 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5386 if (!output_methods) {
5387 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5389 #if defined(NCURSES)
5391 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5392 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5393 output_methods &= ~TO_NCURSES;
5397 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5398 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5399 output_methods &= ~(TO_STDOUT | TO_STDERR);
5401 #endif /* NCURSES */
5406 static void load_config_file_x11(const char *f)
5411 fp = open_config_file(f);
5415 DBGP("reading contents from config file '%s'", f);
5418 char buff[CONF_BUFF_SIZE], *name, *value;
5419 int ret = do_config_step(&line, fp, buff, &name, &value);
5420 if (ret == CONF_BREAK) {
5422 } else if (ret == CONF_CONTINUE) {
5427 X11_initialisation();
5428 if (x_initialised == YES) {
5430 color0 = get_x11_color(value);
5437 X11_initialisation();
5438 if (x_initialised == YES) {
5440 color1 = get_x11_color(value);
5447 X11_initialisation();
5448 if (x_initialised == YES) {
5450 color2 = get_x11_color(value);
5457 X11_initialisation();
5458 if (x_initialised == YES) {
5460 color3 = get_x11_color(value);
5467 X11_initialisation();
5468 if (x_initialised == YES) {
5470 color4 = get_x11_color(value);
5477 X11_initialisation();
5478 if (x_initialised == YES) {
5480 color5 = get_x11_color(value);
5487 X11_initialisation();
5488 if (x_initialised == YES) {
5490 color6 = get_x11_color(value);
5497 X11_initialisation();
5498 if (x_initialised == YES) {
5500 color7 = get_x11_color(value);
5507 X11_initialisation();
5508 if (x_initialised == YES) {
5510 color8 = get_x11_color(value);
5517 X11_initialisation();
5518 if (x_initialised == YES) {
5520 color9 = get_x11_color(value);
5526 CONF("default_color") {
5527 X11_initialisation();
5528 if (x_initialised == YES) {
5530 default_fg_color = get_x11_color(value);
5536 CONF3("default_shade_color", "default_shadecolor") {
5537 X11_initialisation();
5538 if (x_initialised == YES) {
5540 default_bg_color = get_x11_color(value);
5546 CONF3("default_outline_color", "default_outlinecolor") {
5547 X11_initialisation();
5548 if (x_initialised == YES) {
5550 default_out_color = get_x11_color(value);
5557 CONF("own_window_colour") {
5558 X11_initialisation();
5559 if (x_initialised == YES) {
5561 background_colour = get_x11_color(value);
5563 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5564 "'#' for hex colours");
5570 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5571 if(output_methods & TO_X) {
5572 X11_initialisation();
5581 #undef CONF_CONTINUE
5582 #undef CONF_BUFF_SIZE
5590 static void print_help(const char *prog_name) {
5591 printf("Usage: %s [OPTION]...\n"
5592 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5593 "window. Command line options will override configurations defined in config\n"
5595 " -v, --version version\n"
5596 " -q, --quiet quiet mode\n"
5597 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5598 " -c, --config=FILE config file to load\n"
5599 #ifdef CONFIG_OUTPUT
5600 " -C, --print-config print the builtin default config to stdout\n"
5601 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5603 " -d, --daemonize daemonize, fork to background\n"
5604 " -h, --help help\n"
5606 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5607 " -f, --font=FONT font to use\n"
5608 " -X, --display=DISPLAY X11 display to use\n"
5610 " -o, --own-window create own window to draw\n"
5613 " -b, --double-buffer double buffer (prevents flickering)\n"
5615 " -w, --window-id=WIN_ID window id to draw\n"
5616 " -x X x position\n"
5617 " -y Y y position\n"
5619 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5620 " -u, --interval=SECS update interval\n"
5621 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5622 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5627 /* : means that character before that takes an argument */
5628 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5638 #ifdef CONFIG_OUTPUT
5643 static const struct option longopts[] = {
5644 { "help", 0, NULL, 'h' },
5645 { "version", 0, NULL, 'V' },
5646 { "debug", 0, NULL, 'D' },
5647 { "config", 1, NULL, 'c' },
5648 #ifdef CONFIG_OUTPUT
5649 { "print-config", 0, NULL, 'C' },
5651 { "daemonize", 0, NULL, 'd' },
5653 { "alignment", 1, NULL, 'a' },
5654 { "font", 1, NULL, 'f' },
5655 { "display", 1, NULL, 'X' },
5657 { "own-window", 0, NULL, 'o' },
5660 { "double-buffer", 0, NULL, 'b' },
5662 { "window-id", 1, NULL, 'w' },
5664 { "text", 1, NULL, 't' },
5665 { "interval", 0, NULL, 'u' },
5666 { "pause", 0, NULL, 'p' },
5670 void initialisation(int argc, char **argv) {
5671 struct sigaction act, oact;
5673 set_default_configurations();
5674 load_config_file(current_config);
5675 currentconffile = conftree_add(currentconffile, current_config);
5677 /* init specials array */
5678 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5679 NORM_ERR("failed to create specials array");
5683 if (current_mail_spool == NULL) {
5686 variable_substitute(MAIL_FILE, buf, 256);
5688 if (buf[0] != '\0') {
5689 current_mail_spool = strndup(buf, text_buffer_size);
5694 /* handle other command line arguments */
5696 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5697 || defined(__NetBSD__)
5698 optind = optreset = 1;
5703 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5704 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5705 "kvm_open")) == NULL) {
5706 CRIT_ERR(NULL, NULL, "cannot read kvm");
5711 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5720 fork_to_background = 1;
5723 global_debug_level++;
5727 set_first_font(optarg);
5730 text_alignment = string_to_alignment(optarg);
5749 global_text = strndup(optarg, max_user_text);
5750 convert_escapes(global_text);
5754 update_interval = strtod(optarg, 0);
5755 update_interval_old = update_interval;
5756 if (info.music_player_interval == 0) {
5757 // default to update_interval
5758 info.music_player_interval = update_interval;
5763 total_run_times = strtod(optarg, 0);
5767 gap_x = atoi(optarg);
5771 gap_y = atoi(optarg);
5775 startup_pause = atoi(optarg);
5776 sleep(startup_pause);
5786 if (output_methods & TO_X) {
5787 load_config_file_x11(current_config);
5791 /* generate text and get initial size */
5792 extract_variable_text(global_text);
5799 if (fork_to_background) {
5804 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5811 fprintf(stderr, "\n");
5816 /* parent process */
5817 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5824 start_update_threading();
5826 text_buffer = malloc(max_user_text);
5827 memset(text_buffer, 0, max_user_text);
5828 tmpstring1 = malloc(text_buffer_size);
5829 memset(tmpstring1, 0, text_buffer_size);
5830 tmpstring2 = malloc(text_buffer_size);
5831 memset(tmpstring2, 0, text_buffer_size);
5836 X11_create_window();
5839 llua_setup_info(&info, update_interval);
5840 #endif /* HAVE_LUA */
5845 /* Set signal handlers */
5846 act.sa_handler = signal_handler;
5847 sigemptyset(&act.sa_mask);
5850 act.sa_flags |= SA_RESTART;
5853 if ( sigaction(SIGINT, &act, &oact) < 0
5854 || sigaction(SIGALRM, &act, &oact) < 0
5855 || sigaction(SIGUSR1, &act, &oact) < 0
5856 || sigaction(SIGHUP, &act, &oact) < 0
5857 || sigaction(SIGTERM, &act, &oact) < 0) {
5858 NORM_ERR("error setting signal handler: %s", strerror(errno));
5862 llua_startup_hook();
5863 #endif /* HAVE_LUA */
5866 int main(int argc, char **argv)
5875 g_signal_pending = 0;
5876 max_user_text = MAX_USER_TEXT_DEFAULT;
5878 memset(&info, 0, sizeof(info));
5882 #ifdef TCP_PORT_MONITOR
5883 /* set default connection limit */
5884 tcp_portmon_set_max_connections(0);
5887 /* handle command line parameters that don't change configs */
5889 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5890 || ((s = getenv("LANG")) && *s)) {
5891 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5893 NORM_ERR("malloc failed");
5895 for (x = 0; x < strlen(s); x++) {
5896 temp[x] = tolower(s[x]);
5899 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5905 if (!setlocale(LC_CTYPE, "")) {
5906 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5910 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5921 if (current_config) {
5922 free(current_config);
5924 current_config = strndup(optarg, max_user_text);
5927 freopen("/dev/null", "w", stderr);
5930 print_help(argv[0]);
5932 #ifdef CONFIG_OUTPUT
5939 window.window = strtol(optarg, 0, 0);
5944 disp = strdup(optarg);
5953 /* check if specified config file is valid */
5954 if (current_config) {
5956 if (stat(current_config, &sb) ||
5957 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5958 NORM_ERR("invalid configuration file '%s'\n", current_config);
5959 free(current_config);
5964 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5966 if (!current_config) {
5967 /* load default config file */
5968 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5971 /* Try to use personal config file first */
5972 to_real_path(buf, CONFIG_FILE);
5973 if (buf[0] && (fp = fopen(buf, "r"))) {
5974 current_config = strndup(buf, max_user_text);
5978 /* Try to use system config file if personal config not readable */
5979 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5980 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5984 /* No readable config found */
5985 if (!current_config) {
5986 #ifdef CONFIG_OUTPUT
5987 current_config = strdup("==builtin==");
5988 NORM_ERR("no readable personal or system-wide config file found,"
5989 " using builtin default");
5991 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5992 #endif /* ! CONF_OUTPUT */
5997 /* Load xoap keys, if existing */
6001 #ifdef HAVE_SYS_INOTIFY_H
6002 inotify_fd = inotify_init1(IN_NONBLOCK);
6003 #endif /* HAVE_SYS_INOTIFY_H */
6005 initialisation(argc, argv);
6009 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6017 void alarm_handler(void) {
6019 kill(childpid, SIGTERM);
6023 static void signal_handler(int sig)
6025 /* signal handler is light as a feather, as it should be.
6026 * we will poll g_signal_pending with each loop of conky
6027 * and do any signal processing there, NOT here (except
6028 * SIGALRM because this is caused when conky is hanging) */
6029 if(sig == SIGALRM) {
6032 g_signal_pending = sig;