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>
87 #include "iconv_tools.h"
100 #include "temphelper.h"
101 #include "template.h"
102 #include "tailhead.h"
103 #include "timeinfo.h"
106 /* check for OS and include appropriate headers */
107 #if defined(__linux__)
109 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
111 #elif defined(__OpenBSD__)
115 #if defined(__FreeBSD_kernel__)
119 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
120 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
121 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
122 int apm_getinfo(int fd, apm_info_t aip);
123 char *get_apm_adapter(void);
124 char *get_apm_battery_life(void);
125 char *get_apm_battery_time(void);
129 #include "defconfig.h"
130 #include "conf_cookie.h"
134 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
137 #define MAIL_FILE "$MAIL"
138 #define MAX_IF_BLOCK_DEPTH 5
140 //#define SIGNAL_BLOCKING
141 #undef SIGNAL_BLOCKING
143 /* debugging level, used by logging.h */
144 int global_debug_level = 0;
146 /* two strings for internal use */
147 static char *tmpstring1, *tmpstring2;
149 /* variables holding various config settings */
151 int format_human_readable;
158 int top_cpu, top_mem, top_time;
165 static unsigned int top_name_width = 15;
167 static int extra_newline;
168 enum x_initialiser_state x_initialised = NO;
169 static volatile int g_signal_pending;
170 /* Update interval */
171 double update_interval;
172 double update_interval_old;
173 double update_interval_bat;
174 void *global_cpu = NULL;
179 /* prototypes for internally used functions */
180 static void signal_handler(int);
181 static void print_version(void) __attribute__((noreturn));
182 static void reload_config(void);
184 static void print_version(void)
186 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
188 printf("\nCompiled in features:\n\n"
189 "System config file: "SYSTEM_CONFIG_FILE"\n"
190 "Package library path: "PACKAGE_LIBDIR"\n\n"
194 " * Xdamage extension\n"
195 # endif /* HAVE_XDAMAGE */
197 " * XDBE (double buffer extension)\n"
198 # endif /* HAVE_XDBE */
203 "\n Music detection:\n"
206 #endif /* AUDACIOUS */
222 #endif /* HAVE_OPENMP */
229 #ifdef TCP_PORT_MONITOR
231 #endif /* TCP_PORT_MONITOR */
234 #endif /* HAVE_CURL */
239 " * Weather (METAR)\n"
241 " * Weather (XOAP)\n"
246 #endif /* HAVE_IWLIB */
248 " * support for IBM/Lenovo notebooks\n"
258 #endif /* CONFIG_OUTPUT */
263 " * ALSA mixer support\n"
264 #endif /* MIXER_IS_ALSA */
277 #ifdef HAVE_LUA_CAIRO
279 #endif /* HAVE_LUA_CAIRO */
280 #ifdef HAVE_LUA_IMLIB2
283 #endif /* HAVE_LUA */
289 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
294 static void X11_create_window(void);
295 static void X11_initialisation(void);
297 struct _x11_stuff_s {
301 XserverRegion region2, part;
302 int event_base, error_base;
308 static int text_start_x, text_start_y; /* text start position in window */
309 static int text_width, text_height;
324 /* display to connect to */
325 static char *disp = NULL;
329 /* struct that has all info to be shared between
330 * instances of the same text object */
331 struct information info;
333 /* path to config file */
334 char *current_config;
336 /* set to 1 if you want all text to be in uppercase */
337 static unsigned int stuff_in_uppercase;
339 /* Run how many times? */
340 static unsigned long total_run_times;
343 static int fork_to_background;
345 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
347 /* filenames for output */
348 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
349 char *append_file = NULL; FILE *append_fpointer = NULL;
353 static int show_graph_scale;
354 static int show_graph_range;
356 /* Position on the screen */
357 static int text_alignment;
358 static int gap_x, gap_y;
361 static int draw_borders;
362 static int draw_graph_borders;
363 static int stippled_borders;
365 int get_stippled_borders(void)
367 return stippled_borders;
370 static int draw_shades, draw_outline;
372 long default_fg_color, default_bg_color, default_out_color;
374 /* create own window or draw stuff to root? */
375 static int set_transparent = 0;
378 static int own_window = 0;
379 static int background_colour = 0;
381 /* fixed size/pos is set if wm/user changes them */
382 static int fixed_size = 0, fixed_pos = 0;
385 static int minimum_width, minimum_height;
386 static int maximum_width;
391 static int sensor_device;
394 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
397 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
398 unsigned int max_user_text;
400 /* maximum size of individual text buffers, ie $exec buffer size */
401 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
406 /* no buffers in used memory? */
409 /* pad percentages to decimals? */
410 static int pad_percents = 0;
412 static char *global_text = 0;
414 char *get_global_text(void)
419 long global_text_lines;
421 static int total_updates;
422 static int updatereset;
424 void set_updatereset(int i)
429 int get_updatereset(void)
434 int check_contains(char *f, char *s)
437 FILE *where = open_file(f, 0);
442 while (fgets(buf1, 256, where)) {
443 if (strstr(buf1, s)) {
450 NORM_ERR("Could not open the file");
455 #define SECRIT_MULTILINE_CHAR '\x02'
457 static inline int calc_text_width(const char *s)
459 size_t slen = strlen(s);
462 if ((output_methods & TO_X) == 0) {
472 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
473 (const FcChar8 *) s, slen, &gi);
475 XftTextExtents8(display, fonts[selected_font].xftfont,
476 (const FcChar8 *) s, slen, &gi);
482 return XTextWidth(fonts[selected_font].font, s, slen);
487 /* formatted text to render on screen, generated in generate_text(),
488 * drawn in draw_stuff() */
490 static char *text_buffer;
492 /* quite boring functions */
494 static inline void for_each_line(char *b, int f(char *, int))
497 int special_index = 0; /* specials index */
499 for (ps = b, pe = b; *pe; pe++) {
502 special_index = f(ps, special_index);
509 f(ps, special_index);
513 static void convert_escapes(char *buf)
515 char *p = buf, *s = buf;
522 } else if (*s == '\\') {
533 /* Prints anything normally printed with snprintf according to the current value
534 * of use_spacer. Actually slightly more flexible than snprintf, as you can
535 * safely specify the destination buffer as one of your inputs. */
536 int spaced_print(char *buf, int size, const char *format, int width, ...)
545 tempbuf = malloc(size * sizeof(char));
547 // Passes the varargs along to vsnprintf
548 va_start(argp, width);
549 vsnprintf(tempbuf, size, format, argp);
552 switch (use_spacer) {
554 len = snprintf(buf, size, "%s", tempbuf);
557 len = snprintf(buf, size, "%*s", width, tempbuf);
560 len = snprintf(buf, size, "%-*s", width, tempbuf);
567 /* print percentage values
569 * - i.e., unsigned values between 0 and 100
570 * - respect the value of pad_percents */
571 int percent_print(char *buf, int size, unsigned value)
573 return spaced_print(buf, size, "%u", pad_percents, value);
576 /* converts from bytes to human readable format (K, M, G, T)
578 * The algorithm always divides by 1024, as unit-conversion of byte
579 * counts suggests. But for output length determination we need to
580 * compare with 1000 here, as we print in decimal form. */
581 void human_readable(long long num, char *buf, int size)
583 const char **suffix = suffixes;
589 /* Possibly just output as usual, for example for stdout usage */
590 if (!format_human_readable) {
591 spaced_print(buf, size, "%d", 6, round_to_int(num));
602 if (llabs(num) < 1000LL) {
603 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
607 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
615 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
617 * The goal is to always have a significance of 3, by
618 * adjusting the decimal part of the number. Sample output:
622 * so the point of alignment resides between number and unit. The
623 * upside of this is that there is minimal padding necessary, though
624 * there should be a way to make alignment take place at the decimal
625 * dot (then with fixed width decimal part).
627 * Note the repdigits below: when given a precision value, printf()
628 * rounds the float to it, not just cuts off the remaining digits. So
629 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
630 * printed with a precision of 0. Yay. */
632 precision = 0; /* print 100-999 without decimal part */
634 precision = 1; /* print 10-99 with one decimal place */
636 precision = 2; /* print 0-9 with two decimal places */
638 spaced_print(buf, size, format, width, precision, fnum, *suffix);
641 /* global object list root element */
642 static struct text_object global_root_object;
644 static long current_text_color;
646 void set_current_text_color(long colour)
648 current_text_color = colour;
651 long get_current_text_color(void)
653 return current_text_color;
656 //adds newstring to to the tree unless you can already see it when travelling back.
657 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
658 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
659 struct conftree* node;
660 struct conftree* node2;
662 for(node = previous; node != NULL; node = node->back) {
663 if(strcmp(node->string, newstring) == 0) {
667 node = malloc(sizeof(struct conftree));
668 if (previous != NULL) {
669 if(previous->vert_next == NULL) {
670 previous->vert_next = node;
672 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
673 node2->horz_next = node;
676 node->string = strdup(newstring);
677 node->horz_next = NULL;
678 node->vert_next = NULL;
679 node->back = previous;
683 void conftree_empty(struct conftree* tree) {
685 conftree_empty(tree->horz_next);
686 conftree_empty(tree->vert_next);
692 struct conftree *currentconffile;
694 static void extract_variable_text(const char *p)
696 free_text_objects(&global_root_object, 0);
710 extract_variable_text_internal(&global_root_object, p);
713 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
715 extract_variable_text_internal(root, txt);
716 generate_text_internal(p, max_user_text, *root, cur);
719 static inline void format_media_player_time(char *buf, const int size,
722 int days, hours, minutes;
724 days = seconds / (24 * 60 * 60);
725 seconds %= (24 * 60 * 60);
726 hours = seconds / (60 * 60);
727 seconds %= (60 * 60);
728 minutes = seconds / 60;
732 snprintf(buf, size, "%i days %i:%02i:%02i", days,
733 hours, minutes, seconds);
734 } else if (hours > 0) {
735 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
738 snprintf(buf, size, "%i:%02i", minutes, seconds);
742 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
743 * multiline objects like $exec work with $align[rc] and friends
745 void substitute_newlines(char *p, long l)
749 while (p && *p && p < s + l) {
751 /* only substitute if it's not the last newline */
752 *p = SECRIT_MULTILINE_CHAR;
758 void generate_text_internal(char *p, int p_max_size,
759 struct text_object root, struct information *cur)
761 struct text_object *obj;
763 int need_to_load_fonts = 0;
766 /* for the OBJ_top* handler */
767 struct process **needed = 0;
770 char buff_in[p_max_size];
772 #endif /* HAVE_ICONV */
776 while (obj && p_max_size > 0) {
777 needed = 0; /* reset for top stuff */
779 /* IFBLOCK jumping algorithm
781 * This is easier as it looks like:
782 * - each IF checks it's condition
783 * - on FALSE: call DO_JUMP
784 * - on TRUE: don't care
785 * - each ELSE calls DO_JUMP unconditionally
786 * - each ENDIF is silently being ignored
789 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
790 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
791 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
792 * regularly, "obj" is being updated to point to obj->next, so object parsing
793 * continues right after the corresponding ELSE or ENDIF. This means that if we
794 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
795 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
796 * jumped, and there is nothing to do.
803 #define OBJ(a) break; case OBJ_##a:
807 NORM_ERR("not implemented obj type %d", obj->type);
809 print_read_tcp(obj, p, p_max_size);
813 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
815 #endif /* !__OpenBSD__ */
819 ok = get_freq(p, p_max_size, "%.0f", 1,
827 ok = get_freq(p, p_max_size, "%'.2f", 1000,
830 /* OpenBSD has no such flag (SUSv2) */
831 ok = get_freq(p, p_max_size, "%.2f", 1000,
833 #endif /* __OpenBSD */
836 #if defined(__linux__)
840 ok = get_voltage(p, p_max_size, "%.0f", 1,
847 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
853 OBJ(wireless_essid) {
854 print_wireless_essid(obj, p, p_max_size);
857 print_wireless_mode(obj, p, p_max_size);
859 OBJ(wireless_bitrate) {
860 print_wireless_bitrate(obj, p, p_max_size);
863 print_wireless_ap(obj, p, p_max_size);
865 OBJ(wireless_link_qual) {
866 print_wireless_link_qual(obj, p, p_max_size);
868 OBJ(wireless_link_qual_max) {
869 print_wireless_link_qual_max(obj, p, p_max_size);
871 OBJ(wireless_link_qual_perc) {
872 print_wireless_link_qual_perc(obj, p, p_max_size);
874 OBJ(wireless_link_bar) {
875 print_wireless_link_bar(obj, p, p_max_size);
877 #endif /* HAVE_IWLIB */
879 #endif /* __linux__ */
883 get_adt746x_cpu(p, p_max_size);
886 get_adt746x_fan(p, p_max_size);
889 get_acpi_fan(p, p_max_size);
892 get_acpi_ac_adapter(p, p_max_size);
895 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
898 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
900 OBJ(battery_percent) {
901 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
905 if(output_methods & TO_X) {
906 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
909 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
912 get_battery_short_status(p, p_max_size, obj->data.s);
914 #endif /* __OpenBSD__ */
917 human_readable(cur->buffers * 1024, p, 255);
920 human_readable(cur->cached * 1024, p, 255);
923 if (obj->data.i > info.cpu_count) {
924 NORM_ERR("obj->data.i %i info.cpu_count %i",
925 obj->data.i, info.cpu_count);
926 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
928 percent_print(p, p_max_size,
929 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
933 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
937 if(output_methods & TO_X) {
938 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
941 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
945 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
948 print_loadgraph(obj, p);
952 new_fg(p, obj->data.l);
987 snprintf(p, p_max_size, "%s", VERSION);
989 OBJ(conky_build_date) {
990 snprintf(p, p_max_size, "%s", BUILD_DATE);
992 OBJ(conky_build_arch) {
993 snprintf(p, p_max_size, "%s", BUILD_ARCH);
995 #if defined(__linux__)
997 snprintf(p, p_max_size, "%s",
998 get_disk_protect_queue(obj->data.s));
1001 snprintf(p, p_max_size, "%s", i8k.version);
1004 snprintf(p, p_max_size, "%s", i8k.bios);
1007 snprintf(p, p_max_size, "%s", i8k.serial);
1012 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1013 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1015 OBJ(i8k_left_fan_status) {
1016 int left_fan_status;
1018 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1019 if (left_fan_status == 0) {
1020 snprintf(p, p_max_size, "off");
1022 if (left_fan_status == 1) {
1023 snprintf(p, p_max_size, "low");
1025 if (left_fan_status == 2) {
1026 snprintf(p, p_max_size, "high");
1029 OBJ(i8k_right_fan_status) {
1030 int right_fan_status;
1032 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1033 if (right_fan_status == 0) {
1034 snprintf(p, p_max_size, "off");
1036 if (right_fan_status == 1) {
1037 snprintf(p, p_max_size, "low");
1039 if (right_fan_status == 2) {
1040 snprintf(p, p_max_size, "high");
1043 OBJ(i8k_left_fan_rpm) {
1044 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1046 OBJ(i8k_right_fan_rpm) {
1047 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1049 OBJ(i8k_ac_status) {
1052 sscanf(i8k.ac_status, "%d", &ac_status);
1053 if (ac_status == -1) {
1054 snprintf(p, p_max_size, "disabled (read i8k docs)");
1056 if (ac_status == 0) {
1057 snprintf(p, p_max_size, "off");
1059 if (ac_status == 1) {
1060 snprintf(p, p_max_size, "on");
1063 OBJ(i8k_buttons_status) {
1064 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1068 get_ibm_acpi_fan(p, p_max_size);
1071 print_ibm_temps(obj, p, p_max_size);
1074 get_ibm_acpi_volume(p, p_max_size);
1076 OBJ(ibm_brightness) {
1077 get_ibm_acpi_brightness(p, p_max_size);
1080 /* information from sony_laptop kernel module
1081 * /sys/devices/platform/sony-laptop */
1082 OBJ(sony_fanspeed) {
1083 get_sony_fanspeed(p, p_max_size);
1086 if (!gateway_exists()) {
1091 print_gateway_iface(p, p_max_size);
1094 print_gateway_ip(p, p_max_size);
1097 snprintf(p, p_max_size, "%d", get_laptop_mode());
1100 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1102 #endif /* __linux__ */
1103 #if (defined(__FreeBSD__) || defined(__linux__))
1105 if (!interface_up(obj)) {
1111 OBJ(obsd_sensors_temp) {
1112 print_obsd_sensors_temp(obj, p, p_max_size);
1114 OBJ(obsd_sensors_fan) {
1115 print_obsd_sensors_fan(obj, p, p_max_size);
1117 OBJ(obsd_sensors_volt) {
1118 print_obsd_sensors_volt(obj, p, p_max_size);
1121 get_obsd_vendor(p, p_max_size);
1124 get_obsd_product(p, p_max_size);
1126 #endif /* __OpenBSD__ */
1129 new_font(p, obj->data.s);
1130 need_to_load_fonts = 1;
1134 print_diskio(obj, 0, p, p_max_size);
1137 print_diskio(obj, 1, p, p_max_size);
1140 print_diskio(obj, -1, p, p_max_size);
1144 print_diskiograph(obj, 0, p);
1146 OBJ(diskiograph_read) {
1147 print_diskiograph(obj, -1, p);
1149 OBJ(diskiograph_write) {
1150 print_diskiograph(obj, 1, p);
1154 print_downspeed(obj, p, p_max_size);
1157 print_downspeedf(obj, p, p_max_size);
1160 OBJ(downspeedgraph) {
1161 print_downspeedgraph(obj, p);
1165 /* Since we see you, you're if has not jumped.
1166 * Do Ninja jump here: without leaving traces.
1167 * This is to prevent us from stale jumped flags.
1173 /* harmless object, just ignore */
1176 print_addr(obj, p, p_max_size);
1178 #if defined(__linux__)
1180 print_addrs(obj, p, p_max_size);
1182 #endif /* __linux__ */
1183 #if defined(IMLIB2) && defined(X11)
1185 /* doesn't actually draw anything, just queues it omp. the
1186 * image will get drawn after the X event loop */
1187 cimlib_add_image(obj->data.s);
1191 evaluate(obj->data.s, p);
1194 print_exec(obj, p, p_max_size);
1197 print_execp(obj, p, p_max_size);
1201 print_execgauge(obj, p, p_max_size);
1205 print_execbar(obj, p, p_max_size);
1209 print_execgraph(obj, p, p_max_size);
1213 print_execibar(obj, p, p_max_size);
1217 print_execigraph(obj, p, p_max_size);
1220 print_execigauge(obj, p, p_max_size);
1224 print_execi(obj, p, p_max_size);
1227 print_execpi(obj, p);
1230 print_texeci(obj, p, p_max_size);
1233 print_imap_unseen(obj, p, p_max_size);
1235 OBJ(imap_messages) {
1236 print_imap_messages(obj, p, p_max_size);
1239 print_pop3_unseen(obj, p, p_max_size);
1242 print_pop3_used(obj, p, p_max_size);
1245 print_fs_bar(obj, 0, p, p_max_size);
1248 print_fs_free(obj, p, p_max_size);
1251 print_fs_perc(obj, 1, p, p_max_size);
1254 print_fs_size(obj, p, p_max_size);
1257 print_fs_type(obj, p, p_max_size);
1260 print_fs_used(obj, p, p_max_size);
1263 print_fs_bar(obj, 1, p, p_max_size);
1266 print_fs_perc(obj, 0, p, p_max_size);
1269 print_loadavg(obj, p, p_max_size);
1272 new_goto(p, obj->data.i);
1279 new_hr(p, obj->data.i);
1283 print_nameserver(obj, p, p_max_size);
1287 print_eve(obj, p, p_max_size);
1292 curl_print(obj, p, p_max_size);
1297 rss_print_info(obj, p, p_max_size);
1302 print_weather(obj, p, p_max_size);
1306 OBJ(weather_forecast) {
1307 print_weather_forecast(obj, p, p_max_size);
1312 char *str = llua_getstring(obj->data.s);
1314 snprintf(p, p_max_size, "%s", str);
1319 char *str = llua_getstring(obj->data.s);
1327 if (llua_getnumber(obj->data.s, &per)) {
1329 if(output_methods & TO_X) {
1330 new_bar(obj, p, (per/100.0 * 255));
1333 new_bar_in_shell(obj, p, p_max_size, per);
1339 if (llua_getnumber(obj->data.s, &per)) {
1340 new_graph(obj, p, per);
1345 if (llua_getnumber(obj->data.s, &per)) {
1346 new_gauge(obj, p, (per/100.0 * 255));
1350 #endif /* HAVE_LUA */
1356 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1357 snprintf(p, p_max_size, "N/A");
1359 temp_print(p, p_max_size, (double)val,
1360 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1365 new_offset(p, obj->data.i);
1368 new_voffset(p, obj->data.i);
1372 print_sysfs_sensor(obj, p, p_max_size);
1375 print_sysfs_sensor(obj, p, p_max_size);
1378 print_sysfs_sensor(obj, p, p_max_size);
1380 #endif /* __linux__ */
1382 new_alignr(p, obj->data.i);
1385 new_alignc(p, obj->data.i);
1388 char buf[max_user_text];
1389 struct information *tmp_info =
1390 malloc(sizeof(struct information));
1391 memcpy(tmp_info, cur, sizeof(struct information));
1392 generate_text_internal(buf, max_user_text,
1393 *obj->sub, tmp_info);
1395 if (strlen(buf) != 0) {
1401 char expression[max_user_text];
1403 struct information *tmp_info;
1405 tmp_info = malloc(sizeof(struct information));
1406 memcpy(tmp_info, cur, sizeof(struct information));
1407 generate_text_internal(expression, max_user_text,
1408 *obj->sub, tmp_info);
1409 DBGP("parsed arg into '%s'", expression);
1411 val = compare(expression);
1413 NORM_ERR("compare failed for expression '%s'",
1423 spc = strchr(obj->data.s, ' ');
1424 if (!spc && access(obj->data.s, F_OK)) {
1428 if (check_contains(obj->data.s, spc + 1))
1435 && (!check_mount(obj->data.s))) {
1441 if (!get_process_by_name(obj->data.s)) {
1443 if ((obj->data.s) && system(obj->data.s)) {
1448 #if defined(__linux__)
1450 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1454 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1457 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1462 human_readable(cur->mem * 1024, p, 255);
1465 human_readable(cur->memeasyfree * 1024, p, 255);
1468 human_readable(cur->memfree * 1024, p, 255);
1471 human_readable(cur->memmax * 1024, p, 255);
1475 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1479 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1484 if(output_methods & TO_X) {
1485 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1488 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1492 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1497 print_mixer(obj, 0, p, p_max_size);
1500 print_mixer(obj, -1, p, p_max_size);
1503 print_mixer(obj, 1, p, p_max_size);
1507 print_mixer_bar(obj, 0, p);
1510 print_mixer_bar(obj, -1, p);
1513 print_mixer_bar(obj, 1, p);
1516 OBJ(if_mixer_mute) {
1517 if (!check_mixer_muted(obj)) {
1522 #define NOT_IN_X "Not running in X"
1524 if(x_initialised != YES) {
1525 strncpy(p, NOT_IN_X, p_max_size);
1527 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1530 OBJ(monitor_number) {
1531 if(x_initialised != YES) {
1532 strncpy(p, NOT_IN_X, p_max_size);
1534 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1538 if(x_initialised != YES) {
1539 strncpy(p, NOT_IN_X, p_max_size);
1541 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1544 OBJ(desktop_number) {
1545 if(x_initialised != YES) {
1546 strncpy(p, NOT_IN_X, p_max_size);
1548 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1552 if(x_initialised != YES) {
1553 strncpy(p, NOT_IN_X, p_max_size);
1554 }else if(cur->x11.desktop.name != NULL) {
1555 strncpy(p, cur->x11.desktop.name, p_max_size);
1562 print_mails(obj, p, p_max_size);
1565 print_new_mails(obj, p, p_max_size);
1568 print_seen_mails(obj, p, p_max_size);
1571 print_unseen_mails(obj, p, p_max_size);
1573 OBJ(flagged_mails) {
1574 print_flagged_mails(obj, p, p_max_size);
1576 OBJ(unflagged_mails) {
1577 print_unflagged_mails(obj, p, p_max_size);
1579 OBJ(forwarded_mails) {
1580 print_forwarded_mails(obj, p, p_max_size);
1582 OBJ(unforwarded_mails) {
1583 print_unforwarded_mails(obj, p, p_max_size);
1585 OBJ(replied_mails) {
1586 print_replied_mails(obj, p, p_max_size);
1588 OBJ(unreplied_mails) {
1589 print_unreplied_mails(obj, p, p_max_size);
1592 print_draft_mails(obj, p, p_max_size);
1594 OBJ(trashed_mails) {
1595 print_trashed_mails(obj, p, p_max_size);
1598 print_mboxscan(obj, p, p_max_size);
1601 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1604 new_outline(p, obj->data.l);
1607 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1609 OBJ(running_processes) {
1610 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1613 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1616 snprintf(p, p_max_size, "%s", obj->data.s);
1620 new_bg(p, obj->data.l);
1623 new_stippled_hr(obj, p);
1627 human_readable(cur->swap * 1024, p, 255);
1630 human_readable(cur->swapfree * 1024, p, 255);
1633 human_readable(cur->swapmax * 1024, p, 255);
1636 if (cur->swapmax == 0) {
1637 strncpy(p, "No swap", p_max_size);
1639 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1644 if(output_methods & TO_X) {
1645 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1648 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1651 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1654 print_time(obj, p, p_max_size);
1657 print_utime(obj, p, p_max_size);
1660 print_tztime(obj, p, p_max_size);
1663 print_totaldown(obj, p, p_max_size);
1666 print_totalup(obj, p, p_max_size);
1669 snprintf(p, p_max_size, "%d", total_updates);
1672 if(total_updates % updatereset != obj->data.i - 1) {
1677 print_upspeed(obj, p, p_max_size);
1680 print_upspeedf(obj, p, p_max_size);
1684 print_upspeedgraph(obj, p);
1688 format_seconds_short(p, p_max_size, (int) cur->uptime);
1691 format_seconds(p, p_max_size, (int) cur->uptime);
1694 snprintf(p, p_max_size, "%s", cur->users.names);
1697 snprintf(p, p_max_size, "%s", cur->users.terms);
1700 snprintf(p, p_max_size, "%s", cur->users.times);
1703 snprintf(p, p_max_size, "%d", cur->users.number);
1705 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1706 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1710 msg = get_apm_adapter();
1711 snprintf(p, p_max_size, "%s", msg);
1714 OBJ(apm_battery_life) {
1717 msg = get_apm_battery_life();
1718 snprintf(p, p_max_size, "%s", msg);
1721 OBJ(apm_battery_time) {
1724 msg = get_apm_battery_time();
1725 snprintf(p, p_max_size, "%s", msg);
1728 #endif /* __FreeBSD__ __OpenBSD__ */
1731 #define mpd_printf(fmt, val) \
1732 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1733 #define mpd_sprintf(val) { \
1734 if (!obj->data.i || obj->data.i > p_max_size) \
1735 mpd_printf("%s", val); \
1737 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
1742 mpd_sprintf(artist);
1746 mpd_printf("%s", random);
1748 mpd_printf("%s", repeat);
1756 mpd_printf("%d", volume);
1758 mpd_printf("%d", bitrate);
1760 mpd_printf("%s", status);
1762 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
1765 format_media_player_time(p, p_max_size, mpd_get_info()->length);
1768 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
1772 if(output_methods & TO_X) {
1773 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
1776 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
1779 struct mpd_s *mpd = mpd_get_info();
1780 int len = obj->data.i;
1781 if (len == 0 || len > p_max_size)
1784 memset(p, 0, p_max_size);
1785 if (mpd->artist && *mpd->artist &&
1786 mpd->title && *mpd->title) {
1787 snprintf(p, len, "%s - %s", mpd->artist,
1789 } else if (mpd->title && *mpd->title) {
1790 snprintf(p, len, "%s", mpd->title);
1791 } else if (mpd->artist && *mpd->artist) {
1792 snprintf(p, len, "%s", mpd->artist);
1793 } else if (mpd->file && *mpd->file) {
1794 snprintf(p, len, "%s", mpd->file);
1799 OBJ(if_mpd_playing) {
1800 if (!mpd_get_info()->is_playing) {
1809 #define MOC_PRINT(t, a) \
1810 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1812 MOC_PRINT(state, "??");
1815 MOC_PRINT(file, "no file");
1818 MOC_PRINT(title, "no title");
1821 MOC_PRINT(artist, "no artist");
1824 MOC_PRINT(song, "no song");
1827 MOC_PRINT(album, "no album");
1829 OBJ(moc_totaltime) {
1830 MOC_PRINT(totaltime, "0:00");
1833 MOC_PRINT(timeleft, "0:00");
1836 MOC_PRINT(curtime, "0:00");
1839 MOC_PRINT(bitrate, "0Kbps");
1842 MOC_PRINT(rate, "0KHz");
1848 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
1851 snprintf(p, p_max_size, "%s", cur->xmms2.album);
1854 snprintf(p, p_max_size, "%s", cur->xmms2.title);
1857 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
1859 OBJ(xmms2_comment) {
1860 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
1863 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1866 snprintf(p, p_max_size, "%s", cur->xmms2.status);
1869 snprintf(p, p_max_size, "%s", cur->xmms2.date);
1871 OBJ(xmms2_tracknr) {
1872 if (cur->xmms2.tracknr != -1) {
1873 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
1876 OBJ(xmms2_bitrate) {
1877 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
1880 snprintf(p, p_max_size, "%u", cur->xmms2.id);
1883 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
1885 OBJ(xmms2_elapsed) {
1886 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
1887 (cur->xmms2.elapsed / 1000) % 60);
1889 OBJ(xmms2_duration) {
1890 snprintf(p, p_max_size, "%02d:%02d",
1891 cur->xmms2.duration / 60000,
1892 (cur->xmms2.duration / 1000) % 60);
1894 OBJ(xmms2_percent) {
1895 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
1899 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
1902 OBJ(xmms2_playlist) {
1903 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
1905 OBJ(xmms2_timesplayed) {
1906 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
1909 if (strlen(cur->xmms2.title) < 2
1910 && strlen(cur->xmms2.title) < 2) {
1911 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1913 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
1917 OBJ(if_xmms2_connected) {
1918 if (cur->xmms2.conn_state != 1) {
1924 OBJ(audacious_status) {
1925 snprintf(p, p_max_size, "%s",
1926 cur->audacious.items[AUDACIOUS_STATUS]);
1928 OBJ(audacious_title) {
1929 snprintf(p, cur->audacious.max_title_len > 0
1930 ? cur->audacious.max_title_len : p_max_size, "%s",
1931 cur->audacious.items[AUDACIOUS_TITLE]);
1933 OBJ(audacious_length) {
1934 snprintf(p, p_max_size, "%s",
1935 cur->audacious.items[AUDACIOUS_LENGTH]);
1937 OBJ(audacious_length_seconds) {
1938 snprintf(p, p_max_size, "%s",
1939 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1941 OBJ(audacious_position) {
1942 snprintf(p, p_max_size, "%s",
1943 cur->audacious.items[AUDACIOUS_POSITION]);
1945 OBJ(audacious_position_seconds) {
1946 snprintf(p, p_max_size, "%s",
1947 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
1949 OBJ(audacious_bitrate) {
1950 snprintf(p, p_max_size, "%s",
1951 cur->audacious.items[AUDACIOUS_BITRATE]);
1953 OBJ(audacious_frequency) {
1954 snprintf(p, p_max_size, "%s",
1955 cur->audacious.items[AUDACIOUS_FREQUENCY]);
1957 OBJ(audacious_channels) {
1958 snprintf(p, p_max_size, "%s",
1959 cur->audacious.items[AUDACIOUS_CHANNELS]);
1961 OBJ(audacious_filename) {
1962 snprintf(p, p_max_size, "%s",
1963 cur->audacious.items[AUDACIOUS_FILENAME]);
1965 OBJ(audacious_playlist_length) {
1966 snprintf(p, p_max_size, "%s",
1967 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
1969 OBJ(audacious_playlist_position) {
1970 snprintf(p, p_max_size, "%s",
1971 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
1973 OBJ(audacious_main_volume) {
1974 snprintf(p, p_max_size, "%s",
1975 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
1978 OBJ(audacious_bar) {
1982 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
1983 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1984 new_bar(obj, p, (int) (progress * 255.0f));
1987 #endif /* AUDACIOUS */
1991 snprintf(p, p_max_size, "%s", cur->bmpx.title);
1994 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
1997 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2000 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2003 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2006 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2009 /* we have four different types of top (top, top_mem,
2010 * top_time and top_io). To avoid having almost-same code four
2011 * times, we have this special handler. */
2019 /* yes, passing top_name_width instead
2020 * of p_max_size is intended here */
2021 print_top(obj, p, top_name_width);
2023 print_tailhead("tail", obj, p, p_max_size);
2026 print_tailhead("head", obj, p, p_max_size);
2030 FILE *fp = open_file(obj->data.s, &rep);
2033 /* FIXME: use something more general (see also tail.c, head.c */
2034 #define BUFSZ 0x1000
2039 while(fgets(buf, BUFSZ, fp) != NULL){
2040 for(j = 0; buf[j] != 0; j++) {
2041 if(buf[j] == '\n') {
2046 sprintf(p, "%d", lines);
2049 sprintf(p, "File Unreadable");
2055 FILE *fp = open_file(obj->data.s, &rep);
2060 char inword = FALSE;
2063 while(fgets(buf, BUFSZ, fp) != NULL){
2064 for(j = 0; buf[j] != 0; j++) {
2065 if(!isspace(buf[j])) {
2066 if(inword == FALSE) {
2075 sprintf(p, "%d", words);
2078 sprintf(p, "File Unreadable");
2081 #ifdef TCP_PORT_MONITOR
2083 tcp_portmon_action(obj, p, p_max_size);
2085 #endif /* TCP_PORT_MONITOR */
2089 do_iconv_start(obj);
2094 #endif /* HAVE_ICONV */
2096 OBJ(entropy_avail) {
2097 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2100 percent_print(p, p_max_size,
2101 cur->entropy.entropy_avail *
2102 100 / cur->entropy.poolsize);
2104 OBJ(entropy_poolsize) {
2105 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2108 double entropy_perc;
2110 entropy_perc = (double) cur->entropy.entropy_avail /
2111 (double) cur->entropy.poolsize;
2113 if(output_methods & TO_X) {
2114 new_bar(obj, p, (int) (entropy_perc * 255.0f));
2117 new_bar_in_shell(obj, p, p_max_size, (int) (entropy_perc * 100.0f));
2123 s = smapi_get_val(obj->data.s);
2124 snprintf(p, p_max_size, "%s", s);
2128 OBJ(if_smapi_bat_installed) {
2130 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2131 if(!smapi_bat_installed(idx)) {
2135 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2137 OBJ(smapi_bat_perc) {
2139 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2140 val = smapi_bat_installed(idx) ?
2141 smapi_get_bat_int(idx, "remaining_percent") : 0;
2142 percent_print(p, p_max_size, val);
2144 NORM_ERR("argument to smapi_bat_perc must be an integer");
2146 OBJ(smapi_bat_temp) {
2148 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2149 val = smapi_bat_installed(idx) ?
2150 smapi_get_bat_int(idx, "temperature") : 0;
2151 /* temperature is in milli degree celsius */
2152 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2154 NORM_ERR("argument to smapi_bat_temp must be an integer");
2156 OBJ(smapi_bat_power) {
2158 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2159 val = smapi_bat_installed(idx) ?
2160 smapi_get_bat_int(idx, "power_now") : 0;
2161 /* power_now is in mW, set to W with one digit precision */
2162 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2164 NORM_ERR("argument to smapi_bat_power must be an integer");
2167 OBJ(smapi_bat_bar) {
2168 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2169 new_bar(obj, p, (int)
2170 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2178 char buf[max_user_text];
2180 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2181 snprintf(p, p_max_size, "%s", buf);
2187 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2188 char buf[max_user_text];
2191 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2192 snprintf(p, p_max_size, "%s", buf);
2193 if(total_updates % 2) {
2194 for(j=0; p[j] != 0; j++) {
2200 char buf[max_user_text];
2202 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2204 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2205 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2206 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2207 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2208 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2209 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2210 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2212 snprintf(p, p_max_size, "%s", buf);
2215 print_scroll(obj, p, p_max_size, cur);
2218 print_combine(obj, p, cur);
2222 print_nvidia_value(obj, display, p, p_max_size);
2227 /* This is just a meta-object to set host:port */
2230 snprintf(p, p_max_size, "%s",
2231 cur->apcupsd.items[APCUPSD_NAME]);
2233 OBJ(apcupsd_model) {
2234 snprintf(p, p_max_size, "%s",
2235 cur->apcupsd.items[APCUPSD_MODEL]);
2237 OBJ(apcupsd_upsmode) {
2238 snprintf(p, p_max_size, "%s",
2239 cur->apcupsd.items[APCUPSD_UPSMODE]);
2241 OBJ(apcupsd_cable) {
2242 snprintf(p, p_max_size, "%s",
2243 cur->apcupsd.items[APCUPSD_CABLE]);
2245 OBJ(apcupsd_status) {
2246 snprintf(p, p_max_size, "%s",
2247 cur->apcupsd.items[APCUPSD_STATUS]);
2249 OBJ(apcupsd_linev) {
2250 snprintf(p, p_max_size, "%s",
2251 cur->apcupsd.items[APCUPSD_LINEV]);
2254 snprintf(p, p_max_size, "%s",
2255 cur->apcupsd.items[APCUPSD_LOAD]);
2257 OBJ(apcupsd_loadbar) {
2260 if(output_methods & TO_X) {
2261 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2262 new_bar(obj, p, (int) progress);
2266 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2267 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2271 OBJ(apcupsd_loadgraph) {
2273 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2274 new_graph(obj, p, (int)progress);
2276 OBJ(apcupsd_loadgauge) {
2278 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2279 new_gauge(obj, p, (int)progress);
2282 OBJ(apcupsd_charge) {
2283 snprintf(p, p_max_size, "%s",
2284 cur->apcupsd.items[APCUPSD_CHARGE]);
2286 OBJ(apcupsd_timeleft) {
2287 snprintf(p, p_max_size, "%s",
2288 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2291 snprintf(p, p_max_size, "%s",
2292 cur->apcupsd.items[APCUPSD_TEMP]);
2294 OBJ(apcupsd_lastxfer) {
2295 snprintf(p, p_max_size, "%s",
2296 cur->apcupsd.items[APCUPSD_LASTXFER]);
2298 #endif /* APCUPSD */
2305 size_t a = strlen(p);
2308 iconv_convert(&a, buff_in, p, p_max_size);
2309 #endif /* HAVE_ICONV */
2310 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2312 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2313 #endif /* HAVE_LUA */
2315 substitute_newlines(p, a - 2);
2324 /* load any new fonts we may have had */
2325 if (need_to_load_fonts) {
2331 void evaluate(const char *text, char *buffer)
2333 struct information *tmp_info;
2334 struct text_object subroot;
2336 tmp_info = malloc(sizeof(struct information));
2337 memcpy(tmp_info, &info, sizeof(struct information));
2338 parse_conky_vars(&subroot, text, buffer, tmp_info);
2339 DBGP("evaluated '%s' to '%s'", text, buffer);
2341 free_text_objects(&subroot, 1);
2345 double current_update_time, next_update_time, last_update_time;
2347 static void generate_text(void)
2349 struct information *cur = &info;
2356 current_update_time = get_time();
2360 /* add things to the buffer */
2366 generate_text_internal(p, max_user_text, global_root_object, cur);
2368 if (stuff_in_uppercase) {
2371 tmp_p = text_buffer;
2373 *tmp_p = toupper(*tmp_p);
2378 next_update_time += update_interval;
2379 if (next_update_time < get_time()) {
2380 next_update_time = get_time() + update_interval;
2381 } else if (next_update_time > get_time() + update_interval) {
2382 next_update_time = get_time() + update_interval;
2384 last_update_time = current_update_time;
2388 void set_update_interval(double interval)
2390 update_interval = interval;
2391 update_interval_old = interval;
2394 static inline int get_string_width(const char *s)
2396 return *s ? calc_text_width(s) : 0;
2400 static int get_string_width_special(char *s, int special_index)
2410 if ((output_methods & TO_X) == 0)
2413 p = strndup(s, text_buffer_size);
2417 if (*p == SPECIAL_CHAR) {
2418 /* shift everything over by 1 so that the special char
2419 * doesn't mess up the size calculation */
2420 for (i = 0; i < (long)strlen(p); i++) {
2421 *(p + i) = *(p + i + 1);
2423 if (specials[special_index + idx].type == GRAPH
2424 || specials[special_index + idx].type == GAUGE
2425 || specials[special_index + idx].type == BAR) {
2426 width += specials[special_index + idx].width;
2429 } else if (*p == SECRIT_MULTILINE_CHAR) {
2436 if (strlen(final) > 1) {
2437 width += calc_text_width(final);
2443 static int text_size_updater(char *s, int special_index);
2445 int last_font_height;
2446 static void update_text_area(void)
2450 if ((output_methods & TO_X) == 0)
2452 /* update text size if it isn't fixed */
2457 text_width = minimum_width;
2459 last_font_height = font_height();
2460 for_each_line(text_buffer, text_size_updater);
2462 if (text_height < minimum_height) {
2463 text_height = minimum_height;
2465 if (text_width > maximum_width && maximum_width > 0) {
2466 text_width = maximum_width;
2470 /* get text position on workarea */
2471 switch (text_alignment) {
2478 x = workarea[2] - text_width - gap_x;
2483 x = workarea[2] / 2 - text_width / 2 - gap_x;
2490 y = workarea[3] - text_height - gap_y;
2494 x = workarea[2] - text_width - gap_x;
2495 y = workarea[3] - text_height - gap_y;
2499 x = workarea[2] / 2 - text_width / 2 - gap_x;
2500 y = workarea[3] - text_height - gap_y;
2505 y = workarea[3] / 2 - text_height / 2 - gap_y;
2509 x = workarea[2] - text_width - gap_x;
2510 y = workarea[3] / 2 - text_height / 2 - gap_y;
2514 case NONE: // Let the WM manage the window
2525 if (own_window && !fixed_pos) {
2528 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2529 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2530 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2531 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2535 /* If window size doesn't match to workarea's size,
2536 * then window probably includes panels (gnome).
2537 * Blah, doesn't work on KDE. */
2538 if (workarea[2] != window.width || workarea[3] != window.height) {
2547 /* update lua window globals */
2548 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2549 #endif /* HAVE_LUA */
2554 static int cur_x, cur_y; /* current x and y for drawing */
2556 //draw_mode also without X11 because we only need to print to stdout with FG
2557 static int draw_mode; /* FG, BG or OUTLINE */
2559 static long current_color;
2561 static int text_size_updater(char *s, int special_index)
2566 if ((output_methods & TO_X) == 0)
2568 /* get string widths and skip specials */
2571 if (*p == SPECIAL_CHAR) {
2573 w += get_string_width(s);
2576 if (specials[special_index].type == BAR
2577 || specials[special_index].type == GAUGE
2578 || specials[special_index].type == GRAPH) {
2579 w += specials[special_index].width;
2580 if (specials[special_index].height > last_font_height) {
2581 last_font_height = specials[special_index].height;
2582 last_font_height += font_height();
2584 } else if (specials[special_index].type == OFFSET) {
2585 if (specials[special_index].arg > 0) {
2586 w += specials[special_index].arg;
2588 } else if (specials[special_index].type == VOFFSET) {
2589 last_font_height += specials[special_index].arg;
2590 } else if (specials[special_index].type == GOTO) {
2591 if (specials[special_index].arg > cur_x) {
2592 w = (int) specials[special_index].arg;
2594 } else if (specials[special_index].type == TAB) {
2595 int start = specials[special_index].arg;
2596 int step = specials[special_index].width;
2598 if (!step || step < 0) {
2601 w += step - (cur_x - text_start_x - start) % step;
2602 } else if (specials[special_index].type == FONT) {
2603 selected_font = specials[special_index].font_added;
2604 if (font_height() > last_font_height) {
2605 last_font_height = font_height();
2611 } else if (*p == SECRIT_MULTILINE_CHAR) {
2614 lw = get_string_width(s);
2615 *p = SECRIT_MULTILINE_CHAR;
2617 w = lw > w ? lw : w;
2618 text_height += last_font_height;
2622 w += get_string_width(s);
2623 if (w > text_width) {
2626 if (text_width > maximum_width && maximum_width) {
2627 text_width = maximum_width;
2630 text_height += last_font_height;
2631 last_font_height = font_height();
2632 return special_index;
2636 static inline void set_foreground_color(long c)
2639 if (output_methods & TO_X) {
2641 XSetForeground(display, window.gc, c);
2645 if (output_methods & TO_NCURSES) {
2646 attron(COLOR_PAIR(c));
2648 #endif /* NCURSES */
2653 static void draw_string(const char *s)
2655 int i, i2, pos, width_of_s;
2658 char *s_with_newlines;
2664 width_of_s = get_string_width(s);
2665 s_with_newlines = strdup(s);
2666 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2667 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2668 s_with_newlines[i] = '\n';
2671 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2672 printf("%s\n", s_with_newlines);
2673 if (extra_newline) fputc('\n', stdout);
2674 fflush(stdout); /* output immediately, don't buffer */
2676 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2677 fprintf(stderr, "%s\n", s_with_newlines);
2678 fflush(stderr); /* output immediately, don't buffer */
2680 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2681 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2683 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2684 fprintf(append_fpointer, "%s\n", s_with_newlines);
2687 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2688 printw("%s", s_with_newlines);
2691 free(s_with_newlines);
2692 memset(tmpstring1, 0, text_buffer_size);
2693 memset(tmpstring2, 0, text_buffer_size);
2694 strncpy(tmpstring1, s, text_buffer_size - 1);
2699 if (output_methods & TO_X) {
2700 max = ((text_width - width_of_s) / get_string_width(" "));
2703 /* This code looks for tabs in the text and coverts them to spaces.
2704 * The trick is getting the correct number of spaces, and not going
2705 * over the window's size without forcing the window larger. */
2706 for (i = 0; i < (int) text_buffer_size; i++) {
2707 if (tmpstring1[i] == '\t') {
2709 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2710 /* guard against overrun */
2711 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2716 /* guard against overrun */
2717 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2722 if (output_methods & TO_X) {
2723 if (text_width == maximum_width) {
2724 /* this means the text is probably pushing the limit,
2725 * so we'll chop it */
2726 while (cur_x + get_string_width(tmpstring2) - text_start_x
2727 > maximum_width && strlen(tmpstring2) > 0) {
2728 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2735 if (output_methods & TO_X) {
2741 c.pixel = current_color;
2742 XQueryColor(display, DefaultColormap(display, screen), &c);
2745 c2.color.red = c.red;
2746 c2.color.green = c.green;
2747 c2.color.blue = c.blue;
2748 c2.color.alpha = fonts[selected_font].font_alpha;
2750 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2751 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2753 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2754 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2759 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2762 cur_x += width_of_s;
2765 memcpy(tmpstring1, s, text_buffer_size);
2768 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2776 int last_special_needed = -1;
2777 int orig_special_index = special_index;
2780 if (output_methods & TO_X) {
2781 font_h = font_height();
2782 cur_y += font_ascent();
2784 cur_x = text_start_x;
2788 if (*p == SECRIT_MULTILINE_CHAR) {
2789 /* special newline marker for multiline objects */
2794 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2799 /* draw string before special, unless we're dealing multiline
2801 if (last_special_applied > -1) {
2802 special_index = last_special_applied;
2810 switch (specials[special_index].type) {
2812 case HORIZONTAL_LINE:
2814 int h = specials[special_index].height;
2815 int mid = font_ascent() / 2;
2817 w = text_start_x + text_width - cur_x;
2819 XSetLineAttributes(display, window.gc, h, LineSolid,
2820 CapButt, JoinMiter);
2821 XDrawLine(display, window.drawable, window.gc, cur_x,
2822 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2828 int h = specials[special_index].height;
2829 int tmp_s = specials[special_index].arg;
2830 int mid = font_ascent() / 2;
2831 char ss[2] = { tmp_s, tmp_s };
2833 w = text_start_x + text_width - cur_x - 1;
2834 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2835 CapButt, JoinMiter);
2836 XSetDashes(display, window.gc, 0, ss, 2);
2837 XDrawLine(display, window.drawable, window.gc, cur_x,
2838 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2844 int h, bar_usage, by;
2845 if (cur_x - text_start_x > maximum_width
2846 && maximum_width > 0) {
2849 h = specials[special_index].height;
2850 bar_usage = specials[special_index].arg;
2851 by = cur_y - (font_ascent() / 2) - 1;
2856 w = specials[special_index].width;
2858 w = text_start_x + text_width - cur_x - 1;
2864 XSetLineAttributes(display, window.gc, 1, LineSolid,
2865 CapButt, JoinMiter);
2867 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2869 XFillRectangle(display, window.drawable, window.gc, cur_x,
2870 by, w * bar_usage / 255, h);
2878 case GAUGE: /* new GAUGE */
2881 unsigned long last_colour = current_color;
2883 float angle, px, py;
2887 if (cur_x - text_start_x > maximum_width
2888 && maximum_width > 0) {
2892 h = specials[special_index].height;
2893 by = cur_y - (font_ascent() / 2) - 1;
2898 w = specials[special_index].width;
2900 w = text_start_x + text_width - cur_x - 1;
2906 XSetLineAttributes(display, window.gc, 1, LineSolid,
2907 CapButt, JoinMiter);
2909 XDrawArc(display, window.drawable, window.gc,
2910 cur_x, by, w, h * 2, 0, 180*64);
2913 usage = specials[special_index].arg;
2914 angle = (M_PI)*(float)(usage)/255.;
2915 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2916 py = (float)(by+(h))-(float)(h)*sin(angle);
2918 XDrawLine(display, window.drawable, window.gc,
2919 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2927 set_foreground_color(last_colour);
2935 int h, by, i = 0, j = 0;
2937 unsigned long last_colour = current_color;
2938 unsigned long *tmpcolour = 0;
2939 if (cur_x - text_start_x > maximum_width
2940 && maximum_width > 0) {
2943 h = specials[special_index].height;
2944 by = cur_y - (font_ascent() / 2) - 1;
2949 w = specials[special_index].width;
2951 w = text_start_x + text_width - cur_x - 1;
2956 if (draw_graph_borders) {
2957 XSetLineAttributes(display, window.gc, 1, LineSolid,
2958 CapButt, JoinMiter);
2959 XDrawRectangle(display, window.drawable, window.gc,
2962 XSetLineAttributes(display, window.gc, 1, LineSolid,
2963 CapButt, JoinMiter);
2965 if (specials[special_index].last_colour != 0
2966 || specials[special_index].first_colour != 0) {
2967 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
2970 for (i = w - 2; i > -1; i--) {
2971 if (specials[special_index].last_colour != 0
2972 || specials[special_index].first_colour != 0) {
2973 if (specials[special_index].tempgrad) {
2976 (int)((float)(w - 2) - specials[special_index].graph[j] *
2977 (w - 2) / (float)specials[special_index].graph_scale)
2981 (int)((float)(w - 2) - specials[special_index].graph[j] *
2982 (w - 2) / (float)specials[special_index].graph_scale)
2985 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
2987 (int)((float)(w - 2) - specials[special_index].graph[j] *
2988 (w - 2) / (float)specials[special_index].graph_scale)
2992 #endif /* DEBUG_lol */
2993 XSetForeground(display, window.gc, tmpcolour[
2994 (int)((float)(w - 2) - specials[special_index].graph[j] *
2995 (w - 2) / (float)specials[special_index].graph_scale)
2998 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3001 /* this is mugfugly, but it works */
3002 XDrawLine(display, window.drawable, window.gc,
3003 cur_x + i + 1, by + h, cur_x + i + 1,
3004 round_to_int((double)by + h - specials[special_index].graph[j] *
3005 (h - 1) / specials[special_index].graph_scale));
3006 if ((w - i) / ((float) (w - 2) /
3007 (specials[special_index].graph_width)) > j
3008 && j < MAX_GRAPH_DEPTH - 3) {
3012 if (tmpcolour) free(tmpcolour);
3017 /* if (draw_mode == BG) {
3018 set_foreground_color(default_bg_color);
3019 } else if (draw_mode == OUTLINE) {
3020 set_foreground_color(default_out_color);
3022 set_foreground_color(default_fg_color);
3024 if (show_graph_range) {
3027 unsigned short int seconds = update_interval * w;
3033 unsigned short int timeunits;
3035 timeunits = seconds / 86400; seconds %= 86400;
3036 if (timeunits > 0) {
3037 asprintf(&tmp_day_str, "%dd", timeunits);
3039 tmp_day_str = strdup("");
3041 timeunits = seconds / 3600; seconds %= 3600;
3042 if (timeunits > 0) {
3043 asprintf(&tmp_hour_str, "%dh", timeunits);
3045 tmp_hour_str = strdup("");
3047 timeunits = seconds / 60; seconds %= 60;
3048 if (timeunits > 0) {
3049 asprintf(&tmp_min_str, "%dm", timeunits);
3051 tmp_min_str = strdup("");
3054 asprintf(&tmp_sec_str, "%ds", seconds);
3056 tmp_sec_str = strdup("");
3058 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3059 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3061 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3063 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3064 cur_y += font_h / 2;
3065 draw_string(tmp_str);
3071 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3075 cur_x += font_ascent() / 2;
3076 cur_y += font_h / 2;
3078 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3080 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3081 draw_string(tmp_str);
3087 set_foreground_color(last_colour);
3093 int old = font_ascent();
3095 cur_y -= font_ascent();
3096 selected_font = specials[special_index].font_added;
3098 if (cur_y + font_ascent() < cur_y + old) {
3101 cur_y += font_ascent();
3103 font_h = font_height();
3108 if (draw_mode == FG) {
3109 set_foreground_color(specials[special_index].arg);
3115 if (draw_mode == BG) {
3116 set_foreground_color(specials[special_index].arg);
3121 if (draw_mode == OUTLINE) {
3122 set_foreground_color(specials[special_index].arg);
3127 w += specials[special_index].arg;
3128 last_special_needed = special_index;
3132 cur_y += specials[special_index].arg;
3136 if (specials[special_index].arg >= 0) {
3137 cur_x = (int) specials[special_index].arg;
3139 last_special_needed = special_index;
3144 int start = specials[special_index].arg;
3145 int step = specials[special_index].width;
3147 if (!step || step < 0) {
3150 w = step - (cur_x - text_start_x - start) % step;
3151 last_special_needed = special_index;
3157 /* TODO: add back in "+ window.border_inner_margin" to the end of
3159 int pos_x = text_start_x + text_width -
3160 get_string_width_special(s, special_index);
3162 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3163 "get_string_width(p) %i gap_x %i "
3164 "specials[special_index].arg %i window.border_inner_margin %i "
3165 "window.border_width %i\n", pos_x, text_start_x, text_width,
3166 cur_x, get_string_width_special(s), gap_x,
3167 specials[special_index].arg, window.border_inner_margin,
3168 window.border_width); */
3169 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3170 cur_x = pos_x - specials[special_index].arg;
3172 last_special_needed = special_index;
3178 int pos_x = (text_width) / 2 - get_string_width_special(s,
3179 special_index) / 2 - (cur_x -
3181 /* int pos_x = text_start_x + text_width / 2 -
3182 get_string_width_special(s) / 2; */
3184 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3185 "get_string_width(p) %i gap_x %i "
3186 "specials[special_index].arg %i\n", pos_x, text_start_x,
3187 text_width, cur_x, get_string_width(s), gap_x,
3188 specials[special_index].arg); */
3189 if (pos_x > specials[special_index].arg) {
3190 w = pos_x - specials[special_index].arg;
3192 last_special_needed = special_index;
3202 if (special_index != last_special_applied) {
3205 special_index = orig_special_index;
3206 last_special_applied = -1;
3217 if (output_methods & TO_NCURSES) {
3220 #endif /* NCURSES */
3222 if (output_methods & TO_X)
3223 cur_y += font_descent();
3225 if (recurse && *recurse) {
3226 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3227 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3229 return special_index;
3232 static int draw_line(char *s, int special_index)
3235 if (output_methods & TO_X) {
3236 return draw_each_line_inner(s, special_index, -1);
3240 if (output_methods & TO_NCURSES) {
3241 return draw_each_line_inner(s, special_index, -1);
3243 #endif /* NCURSES */
3245 UNUSED(special_index);
3249 static void draw_text(void)
3253 llua_draw_pre_hook();
3254 #endif /* HAVE_LUA */
3255 if (output_methods & TO_X) {
3256 cur_y = text_start_y;
3259 if (draw_borders && window.border_width > 0) {
3260 if (stippled_borders) {
3261 char ss[2] = { stippled_borders, stippled_borders };
3262 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3263 CapButt, JoinMiter);
3264 XSetDashes(display, window.gc, 0, ss, 2);
3266 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3267 CapButt, JoinMiter);
3270 XDrawRectangle(display, window.drawable, window.gc,
3271 text_start_x - window.border_inner_margin - window.border_width,
3272 text_start_y - window.border_inner_margin - window.border_width,
3273 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3274 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3282 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3283 attron(COLOR_PAIR(COLOR_WHITE));
3284 #endif /* NCURSES */
3285 for_each_line(text_buffer, draw_line);
3286 #if defined(HAVE_LUA) && defined(X11)
3287 llua_draw_post_hook();
3288 #endif /* HAVE_LUA */
3291 static void draw_stuff(void)
3294 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3296 if (overwrite_file) {
3297 overwrite_fpointer = fopen(overwrite_file, "w");
3298 if(!overwrite_fpointer)
3299 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3302 append_fpointer = fopen(append_file, "a");
3303 if(!append_fpointer)
3304 NORM_ERR("Can't append '%s' anymore", append_file);
3307 if (output_methods & TO_X) {
3309 if (draw_shades && !draw_outline) {
3312 set_foreground_color(default_bg_color);
3323 for (i = -1; i < 2; i++) {
3324 for (j = -1; j < 2; j++) {
3325 if (i == 0 && j == 0) {
3330 set_foreground_color(default_out_color);
3331 draw_mode = OUTLINE;
3339 set_foreground_color(default_fg_color);
3344 #if defined(X11) && defined(HAVE_XDBE)
3345 if (output_methods & TO_X) {
3346 xdbe_swap_buffers();
3348 #endif /* X11 && HAVE_XDBE */
3349 if(overwrite_fpointer) {
3350 fclose(overwrite_fpointer);
3351 overwrite_fpointer = 0;
3353 if(append_fpointer) {
3354 fclose(append_fpointer);
3355 append_fpointer = 0;
3360 static void clear_text(int exposures)
3364 /* The swap action is XdbeBackground, which clears */
3368 if (display && window.window) { // make sure these are !null
3369 /* there is some extra space for borders and outlines */
3370 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3371 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3372 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3373 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3378 static int need_to_update;
3380 /* update_text() generates new text and clears old text area */
3381 static void update_text(void)
3388 if (output_methods & TO_X)
3393 llua_update_info(&info, update_interval);
3394 #endif /* HAVE_LUA */
3397 #ifdef HAVE_SYS_INOTIFY_H
3401 static void main_loop(void)
3404 #ifdef SIGNAL_BLOCKING
3405 sigset_t newmask, oldmask;
3408 #ifdef HAVE_SYS_INOTIFY_H
3409 int inotify_config_wd = -1;
3410 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3411 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3412 char inotify_buff[INOTIFY_BUF_LEN];
3413 #endif /* HAVE_SYS_INOTIFY_H */
3416 #ifdef SIGNAL_BLOCKING
3417 sigemptyset(&newmask);
3418 sigaddset(&newmask, SIGINT);
3419 sigaddset(&newmask, SIGTERM);
3420 sigaddset(&newmask, SIGUSR1);
3423 last_update_time = 0.0;
3424 next_update_time = get_time();
3426 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3427 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3428 char buf[max_user_text];
3430 get_battery_short_status(buf, max_user_text, "BAT0");
3432 update_interval = update_interval_bat;
3434 update_interval = update_interval_old;
3439 #ifdef SIGNAL_BLOCKING
3440 /* block signals. we will inspect for pending signals later */
3441 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3442 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3447 if (output_methods & TO_X) {
3450 /* wait for X event or timeout */
3452 if (!XPending(display)) {
3456 t = next_update_time - get_time();
3460 } else if (t > update_interval) {
3461 t = update_interval;
3464 tv.tv_sec = (long) t;
3465 tv.tv_usec = (long) (t * 1000000) % 1000000;
3467 FD_SET(ConnectionNumber(display), &fdsr);
3469 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3471 if (errno != EINTR) {
3472 NORM_ERR("can't select(): %s", strerror(errno));
3482 if (need_to_update) {
3484 int wx = window.x, wy = window.y;
3494 /* resize window if it isn't right size */
3496 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3497 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3498 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3499 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3500 draw_stuff(); /* redraw everything in our newly sized window */
3501 XResizeWindow(display, window.window, window.width,
3502 window.height); /* resize window */
3503 set_transparent_background(window.window);
3506 xdbe_swap_buffers();
3511 /* update lua window globals */
3512 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3513 #endif /* HAVE_LUA */
3516 /* move window if it isn't in right position */
3517 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3518 XMoveWindow(display, window.window, window.x, window.y);
3523 if (changed && window.type == TYPE_PANEL) {
3526 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3529 switch (text_alignment) {
3556 set_struts(sidenum);
3567 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3568 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3569 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3570 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3571 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3576 /* handle X events */
3577 while (XPending(display)) {
3580 XNextEvent(display, &ev);
3587 r.width = ev.xexpose.width;
3588 r.height = ev.xexpose.height;
3589 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3593 case PropertyNotify:
3595 if ( ev.xproperty.state == PropertyNewValue ) {
3596 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3602 case ReparentNotify:
3603 /* set background to ParentRelative for all parents */
3605 set_transparent_background(window.window);
3609 case ConfigureNotify:
3611 /* if window size isn't what expected, set fixed size */
3612 if (ev.xconfigure.width != window.width
3613 || ev.xconfigure.height != window.height) {
3614 if (window.width != 0 && window.height != 0) {
3618 /* clear old stuff before screwing up
3623 XWindowAttributes attrs;
3624 if (XGetWindowAttributes(display,
3625 window.window, &attrs)) {
3626 window.width = attrs.width;
3627 window.height = attrs.height;
3631 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3632 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3633 if (text_width > maximum_width
3634 && maximum_width > 0) {
3635 text_width = maximum_width;
3639 /* if position isn't what expected, set fixed pos
3640 * total_updates avoids setting fixed_pos when window
3641 * is set to weird locations when started */
3642 /* // this is broken
3643 if (total_updates >= 2 && !fixed_pos
3644 && (window.x != ev.xconfigure.x
3645 || window.y != ev.xconfigure.y)
3646 && (ev.xconfigure.x != 0
3647 || ev.xconfigure.y != 0)) {
3655 /* if an ordinary window with decorations */
3656 if ((window.type == TYPE_NORMAL &&
3657 (!TEST_HINT(window.hints,
3658 HINT_UNDECORATED))) ||
3659 window.type == TYPE_DESKTOP) {
3660 /* allow conky to hold input focus. */
3663 /* forward the click to the desktop window */
3664 XUngrabPointer(display, ev.xbutton.time);
3665 ev.xbutton.window = window.desktop;
3666 ev.xbutton.x = ev.xbutton.x_root;
3667 ev.xbutton.y = ev.xbutton.y_root;
3668 XSendEvent(display, ev.xbutton.window, False,
3669 ButtonPressMask, &ev);
3670 XSetInputFocus(display, ev.xbutton.window,
3671 RevertToParent, ev.xbutton.time);
3678 /* if an ordinary window with decorations */
3679 if ((window.type == TYPE_NORMAL)
3680 && (!TEST_HINT(window.hints,
3681 HINT_UNDECORATED))) {
3682 /* allow conky to hold input focus. */
3685 /* forward the release to the desktop window */
3686 ev.xbutton.window = window.desktop;
3687 ev.xbutton.x = ev.xbutton.x_root;
3688 ev.xbutton.y = ev.xbutton.y_root;
3689 XSendEvent(display, ev.xbutton.window, False,
3690 ButtonReleaseMask, &ev);
3699 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3700 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3702 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3703 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3705 #endif /* HAVE_XDAMAGE */
3711 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3712 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3713 #endif /* HAVE_XDAMAGE */
3715 /* XDBE doesn't seem to provide a way to clear the back buffer
3716 * without interfering with the front buffer, other than passing
3717 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3718 * using XDBE, we need to redraw the text even if it wasn't part of
3719 * the exposed area. OTOH, if we're not going to call draw_stuff at
3720 * all, then no swap happens and we can safely do nothing. */
3722 if (!XEmptyRegion(x11_stuff.region)) {
3727 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3728 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3729 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3730 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3731 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3734 XSetRegion(display, window.gc, x11_stuff.region);
3737 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3741 XDestroyRegion(x11_stuff.region);
3742 x11_stuff.region = XCreateRegion();
3746 t = (next_update_time - get_time()) * 1000000;
3747 if(t > 0) usleep((useconds_t)t);
3751 if(output_methods & TO_NCURSES) {
3760 #ifdef SIGNAL_BLOCKING
3761 /* unblock signals of interest and let handler fly */
3762 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3763 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3767 switch (g_signal_pending) {
3770 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3775 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3778 if (output_methods & TO_X) {
3779 XDestroyRegion(x11_stuff.region);
3780 x11_stuff.region = NULL;
3782 XDamageDestroy(display, x11_stuff.damage);
3783 XFixesDestroyRegion(display, x11_stuff.region2);
3784 XFixesDestroyRegion(display, x11_stuff.part);
3785 #endif /* HAVE_XDAMAGE */
3791 if(overwrite_file) {
3792 free(overwrite_file);
3801 /* Reaching here means someone set a signal
3802 * (SIGXXXX, signal_handler), but didn't write any code
3804 * If you don't want to handle a signal, don't set a handler on
3805 * it in the first place. */
3806 if (g_signal_pending) {
3807 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3811 #ifdef HAVE_SYS_INOTIFY_H
3812 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3813 inotify_config_wd = inotify_add_watch(inotify_fd,
3817 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3818 int len = 0, idx = 0;
3820 struct timeval time_to_wait;
3822 FD_ZERO(&descriptors);
3823 FD_SET(inotify_fd, &descriptors);
3825 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3827 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3828 if (FD_ISSET(inotify_fd, &descriptors)) {
3829 /* process inotify events */
3830 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3831 while (len > 0 && idx < len) {
3832 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3833 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3834 /* current_config should be reloaded */
3835 NORM_ERR("'%s' modified, reloading...", current_config);
3837 if (ev->mask & IN_IGNORED) {
3838 /* for some reason we get IN_IGNORED here
3839 * sometimes, so we need to re-add the watch */
3840 inotify_config_wd = inotify_add_watch(inotify_fd,
3847 llua_inotify_query(ev->wd, ev->mask);
3849 #endif /* HAVE_LUA */
3850 idx += INOTIFY_EVENT_SIZE + ev->len;
3854 #endif /* HAVE_SYS_INOTIFY_H */
3857 llua_update_info(&info, update_interval);
3858 #endif /* HAVE_LUA */
3859 g_signal_pending = 0;
3861 clean_up(NULL, NULL);
3863 #ifdef HAVE_SYS_INOTIFY_H
3864 if (inotify_fd != -1) {
3865 inotify_rm_watch(inotify_fd, inotify_config_wd);
3867 inotify_fd = inotify_config_wd = 0;
3869 #endif /* HAVE_SYS_INOTIFY_H */
3873 static void load_config_file_x11(const char *);
3875 void initialisation(int argc, char** argv);
3877 /* reload the config file */
3878 static void reload_config(void)
3880 char *current_config_copy = strdup(current_config);
3881 clean_up(NULL, NULL);
3882 current_config = current_config_copy;
3883 initialisation(argc_copy, argv_copy);
3886 void clean_up(void *memtofree1, void* memtofree2)
3891 if(output_methods & TO_NCURSES) {
3895 conftree_empty(currentconffile);
3896 currentconffile = NULL;
3903 timed_thread_destroy_registered_threads();
3905 if (info.cpu_usage) {
3906 free(info.cpu_usage);
3907 info.cpu_usage = NULL;
3910 if (x_initialised == YES) {
3911 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3912 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3913 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3914 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3917 if(x11_stuff.region) {
3918 XDestroyRegion(x11_stuff.region);
3919 x11_stuff.region = NULL;
3921 XCloseDisplay(display);
3923 if(info.x11.desktop.all_names) {
3924 free(info.x11.desktop.all_names);
3925 info.x11.desktop.all_names = NULL;
3927 if (info.x11.desktop.name) {
3928 free(info.x11.desktop.name);
3929 info.x11.desktop.name = NULL;
3933 free(fonts); //in set_default_configurations a font is set but not loaded
3939 free_update_callbacks();
3943 free_text_objects(&global_root_object, 0);
3962 free(current_config);
3965 #ifdef TCP_PORT_MONITOR
3966 tcp_portmon_clear();
3975 weather_free_info();
3978 llua_shutdown_hook();
3980 #endif /* HAVE_LUA */
3982 if (output_methods & TO_X)
3990 for (i = 0; i < special_count; i++) {
3991 if (specials[i].type == GRAPH) {
3992 free(specials[i].graph);
4000 clear_diskio_stats();
4001 if(global_cpu != NULL) {
4007 static int string_to_bool(const char *s)
4010 // Assumes an option without a true/false means true
4012 } else if (strcasecmp(s, "yes") == EQUAL) {
4014 } else if (strcasecmp(s, "true") == EQUAL) {
4016 } else if (strcasecmp(s, "1") == EQUAL) {
4023 static enum alignment string_to_alignment(const char *s)
4025 if (strcasecmp(s, "top_left") == EQUAL) {
4027 } else if (strcasecmp(s, "top_right") == EQUAL) {
4029 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4031 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4033 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4034 return BOTTOM_RIGHT;
4035 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4036 return BOTTOM_MIDDLE;
4037 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4039 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4040 return MIDDLE_RIGHT;
4041 } else if (strcasecmp(s, "tl") == EQUAL) {
4043 } else if (strcasecmp(s, "tr") == EQUAL) {
4045 } else if (strcasecmp(s, "tm") == EQUAL) {
4047 } else if (strcasecmp(s, "bl") == EQUAL) {
4049 } else if (strcasecmp(s, "br") == EQUAL) {
4050 return BOTTOM_RIGHT;
4051 } else if (strcasecmp(s, "bm") == EQUAL) {
4052 return BOTTOM_MIDDLE;
4053 } else if (strcasecmp(s, "ml") == EQUAL) {
4055 } else if (strcasecmp(s, "mr") == EQUAL) {
4056 return MIDDLE_RIGHT;
4057 } else if (strcasecmp(s, "none") == EQUAL) {
4065 static void set_default_configurations_for_x(void)
4067 default_fg_color = WhitePixel(display, screen);
4068 default_bg_color = BlackPixel(display, screen);
4069 default_out_color = BlackPixel(display, screen);
4070 color0 = default_fg_color;
4071 color1 = default_fg_color;
4072 color2 = default_fg_color;
4073 color3 = default_fg_color;
4074 color4 = default_fg_color;
4075 color5 = default_fg_color;
4076 color6 = default_fg_color;
4077 color7 = default_fg_color;
4078 color8 = default_fg_color;
4079 color9 = default_fg_color;
4080 current_text_color = default_fg_color;
4084 static void set_default_configurations(void)
4091 fork_to_background = 0;
4092 total_run_times = 0;
4093 info.cpu_avg_samples = 2;
4094 info.net_avg_samples = 2;
4095 info.diskio_avg_samples = 2;
4100 format_human_readable = 1;
4110 mpd_env_host = getenv("MPD_HOST");
4111 mpd_env_port = getenv("MPD_PORT");
4113 if (!mpd_env_host || !strlen(mpd_env_host)) {
4114 mpd_set_host("localhost");
4116 /* MPD_HOST environment variable is set */
4117 char *mpd_hostpart = strchr(mpd_env_host, '@');
4118 if (!mpd_hostpart) {
4119 mpd_set_host(mpd_env_host);
4121 /* MPD_HOST contains a password */
4122 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4123 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4125 if (!strlen(mpd_hostpart + 1)) {
4126 mpd_set_host("localhost");
4128 mpd_set_host(mpd_hostpart + 1);
4131 mpd_set_password(mpd_password, 1);
4136 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4137 /* failed to set port from environment variable */
4138 mpd_set_port("6600");
4142 info.xmms2.artist = NULL;
4143 info.xmms2.album = NULL;
4144 info.xmms2.title = NULL;
4145 info.xmms2.genre = NULL;
4146 info.xmms2.comment = NULL;
4147 info.xmms2.url = NULL;
4148 info.xmms2.status = NULL;
4149 info.xmms2.playlist = NULL;
4151 use_spacer = NO_SPACER;
4153 output_methods = TO_X;
4155 output_methods = TO_STDOUT;
4158 show_graph_scale = 0;
4159 show_graph_range = 0;
4162 draw_graph_borders = 1;
4164 set_first_font("6x10");
4172 window.type = TYPE_NORMAL;
4174 strcpy(window.class_name, PACKAGE_NAME);
4175 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4177 stippled_borders = 0;
4178 window.border_inner_margin = 3;
4179 window.border_outer_margin = 1;
4180 window.border_width = 1;
4181 text_alignment = BOTTOM_LEFT;
4182 info.x11.monitor.number = 1;
4183 info.x11.monitor.current = 0;
4184 info.x11.desktop.current = 1;
4185 info.x11.desktop.number = 1;
4186 info.x11.desktop.nitems = 0;
4187 info.x11.desktop.all_names = NULL;
4188 info.x11.desktop.name = NULL;
4193 free(current_mail_spool);
4197 variable_substitute(MAIL_FILE, buf, 256);
4198 if (buf[0] != '\0') {
4199 current_mail_spool = strndup(buf, text_buffer_size);
4204 set_update_interval(3);
4205 update_interval_bat = NOBATTERY;
4206 info.music_player_interval = 1.0;
4207 stuff_in_uppercase = 0;
4208 info.users.number = 1;
4210 #ifdef TCP_PORT_MONITOR
4211 /* set default connection limit */
4212 tcp_portmon_set_max_connections(0);
4216 /* returns 1 if you can overwrite or create the file at 'path' */
4217 static _Bool overwrite_works(const char *path)
4221 if (!(filepointer = fopen(path, "w")))
4223 fclose(filepointer);
4227 /* returns 1 if you can append or create the file at 'path' */
4228 static _Bool append_works(const char *path)
4232 if (!(filepointer = fopen(path, "a")))
4234 fclose(filepointer);
4240 /* WARNING, this type not in Xlib spec */
4241 int x11_error_handler(Display *d, XErrorEvent *err)
4242 __attribute__((noreturn));
4243 int x11_error_handler(Display *d, XErrorEvent *err)
4245 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",
4247 (long unsigned)err->display,
4248 (long)err->resourceid,
4258 int x11_ioerror_handler(Display *d)
4259 __attribute__((noreturn));
4260 int x11_ioerror_handler(Display *d)
4262 NORM_ERR("X Error: Display %lx\n",
4269 static void X11_initialisation(void)
4271 if (x_initialised == YES) return;
4272 output_methods |= TO_X;
4274 set_default_configurations_for_x();
4275 x_initialised = YES;
4278 /* WARNING, this type not in Xlib spec */
4279 XSetErrorHandler(&x11_error_handler);
4280 XSetIOErrorHandler(&x11_ioerror_handler);
4284 static char **xargv = 0;
4285 static int xargc = 0;
4287 static void X11_create_window(void)
4289 if (output_methods & TO_X) {
4291 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4292 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4294 #else /* OWN_WINDOW */
4295 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4296 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4298 #endif /* OWN_WINDOW */
4302 update_text_area(); /* to position text/window on screen */
4305 if (own_window && !fixed_pos) {
4306 XMoveWindow(display, window.window, window.x, window.y);
4309 set_transparent_background(window.window);
4317 x11_stuff.region = XCreateRegion();
4319 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4320 NORM_ERR("Xdamage extension unavailable");
4322 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4323 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4324 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4325 #endif /* HAVE_XDAMAGE */
4328 update_text_area(); /* to get initial size of the window */
4331 /* setup lua window globals */
4332 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4333 #endif /* HAVE_LUA */
4337 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4338 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4339 #define CONF2(a) if (strcasecmp(name, a) == 0)
4340 #define CONF(a) else CONF2(a)
4341 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4342 || strcasecmp(name, b) == 0)
4343 #define CONF_CONTINUE 1
4344 #define CONF_BREAK 2
4345 #define CONF_BUFF_SIZE 512
4347 static FILE *open_config_file(const char *f)
4349 #ifdef CONFIG_OUTPUT
4350 if (!strcmp(f, "==builtin==")) {
4351 return conf_cookie_open();
4353 #endif /* CONFIG_OUTPUT */
4354 return fopen(f, "r");
4357 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4361 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4364 remove_comments(buf);
4369 while (*p && isspace((int) *p)) {
4373 return CONF_CONTINUE; /* empty line */
4380 while (*p2 && !isspace((int) *p2)) {
4384 *p2 = '\0'; /* break at name's end */
4391 while (*p && isspace((int) *p)) {
4397 p2 = *value + strlen(*value);
4398 while (isspace((int) *(p2 - 1))) {
4407 char load_config_file(const char *f)
4412 fp = open_config_file(f);
4416 DBGP("reading contents from config file '%s'", f);
4419 char buff[CONF_BUFF_SIZE], *name, *value;
4420 int ret = do_config_step(&line, fp, buff, &name, &value);
4421 if (ret == CONF_BREAK) {
4423 } else if (ret == CONF_CONTINUE) {
4429 /* don't listen if X is already initialised or
4430 * if we already know we don't want it */
4431 if(x_initialised != YES) {
4432 if (string_to_bool(value)) {
4433 output_methods &= TO_X;
4435 output_methods &= ~TO_X;
4436 x_initialised = NEVER;
4441 if (!value || x_initialised == YES) {
4446 disp = strdup(value);
4451 if (window.type == TYPE_DOCK)
4454 #endif /*OWN_WINDOW */
4456 int a = string_to_alignment(value);
4467 CONF("background") {
4468 fork_to_background = string_to_bool(value);
4471 CONF2("background") {
4472 fork_to_background = string_to_bool(value);
4476 CONF("show_graph_scale") {
4477 show_graph_scale = string_to_bool(value);
4479 CONF("show_graph_range") {
4480 show_graph_range = string_to_bool(value);
4482 CONF("border_inner_margin") {
4484 window.border_inner_margin = strtol(value, 0, 0);
4485 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4490 CONF("border_outer_margin") {
4492 window.border_outer_margin = strtol(value, 0, 0);
4493 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4498 CONF("border_width") {
4500 window.border_width = strtol(value, 0, 0);
4501 if (window.border_width < 0) window.border_width = 0;
4507 #define TEMPLATE_CONF(n) \
4508 CONF("template"#n) { \
4509 if (set_template(n, value)) \
4524 parse_global_imap_mail_args(value);
4531 parse_global_pop3_mail_args(value);
4536 CONF("default_bar_size") {
4539 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4546 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4550 CONF("default_graph_size") {
4553 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4560 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4563 CONF("default_gauge_size") {
4566 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4573 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4580 mpd_set_host(value);
4586 if (value && mpd_set_port(value)) {
4590 CONF("mpd_password") {
4592 mpd_set_password(value, 0);
4598 CONF("music_player_interval") {
4600 info.music_player_interval = strtod(value, 0);
4606 CONF("sensor_device") {
4608 sensor_device = strtol(value, 0, 0);
4614 CONF("cpu_avg_samples") {
4616 cpu_avg_samples = strtol(value, 0, 0);
4617 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4620 info.cpu_avg_samples = cpu_avg_samples;
4626 CONF("net_avg_samples") {
4628 net_avg_samples = strtol(value, 0, 0);
4629 if (net_avg_samples < 1 || net_avg_samples > 14) {
4632 info.net_avg_samples = net_avg_samples;
4638 CONF("diskio_avg_samples") {
4640 diskio_avg_samples = strtol(value, 0, 0);
4641 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4644 info.diskio_avg_samples = diskio_avg_samples;
4652 CONF("double_buffer") {
4653 use_xdbe = string_to_bool(value);
4657 CONF("override_utf8_locale") {
4658 utf8_mode = string_to_bool(value);
4660 CONF("draw_borders") {
4661 draw_borders = string_to_bool(value);
4663 CONF("draw_graph_borders") {
4664 draw_graph_borders = string_to_bool(value);
4666 CONF("draw_shades") {
4667 draw_shades = string_to_bool(value);
4669 CONF("draw_outline") {
4670 draw_outline = string_to_bool(value);
4673 CONF("out_to_console") {
4674 if(string_to_bool(value)) {
4675 output_methods |= TO_STDOUT;
4677 output_methods &= ~TO_STDOUT;
4680 CONF("extra_newline") {
4681 extra_newline = string_to_bool(value);
4683 CONF("out_to_stderr") {
4684 if(string_to_bool(value))
4685 output_methods |= TO_STDERR;
4688 CONF("out_to_ncurses") {
4689 if(string_to_bool(value)) {
4692 output_methods |= TO_NCURSES;
4696 CONF("overwrite_file") {
4697 if(overwrite_file) {
4698 free(overwrite_file);
4701 if(overwrite_works(value)) {
4702 overwrite_file = strdup(value);
4703 output_methods |= OVERWRITE_FILE;
4705 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4707 CONF("append_file") {
4712 if(append_works(value)) {
4713 append_file = strdup(value);
4714 output_methods |= APPEND_FILE;
4716 NORM_ERR("append_file won't be able to create/append '%s'", value);
4718 CONF("use_spacer") {
4720 if (strcasecmp(value, "left") == EQUAL) {
4721 use_spacer = LEFT_SPACER;
4722 } else if (strcasecmp(value, "right") == EQUAL) {
4723 use_spacer = RIGHT_SPACER;
4724 } else if (strcasecmp(value, "none") == EQUAL) {
4725 use_spacer = NO_SPACER;
4727 use_spacer = string_to_bool(value);
4728 NORM_ERR("use_spacer should have an argument of left, right, or"
4729 " none. '%s' seems to be some form of '%s', so"
4730 " defaulting to %s.", value,
4731 use_spacer ? "true" : "false",
4732 use_spacer ? "right" : "none");
4734 use_spacer = RIGHT_SPACER;
4736 use_spacer = NO_SPACER;
4740 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4741 use_spacer = RIGHT_SPACER;
4747 use_xft = string_to_bool(value);
4751 set_first_font(value);
4755 if (value && font_count >= 0) {
4756 fonts[0].font_alpha = atof(value) * 65535.0;
4763 if (string_to_bool(value)) {
4764 NORM_ERR("Xft not enabled at compile time");
4768 /* xftfont silently ignored when no Xft */
4771 /* xftalpha is silently ignored when no Xft */
4776 set_first_font(value);
4784 gap_x = atoi(value);
4791 gap_y = atoi(value);
4797 CONF("mail_spool") {
4801 variable_substitute(value, buffer, 256);
4803 if (buffer[0] != '\0') {
4804 if (current_mail_spool) {
4805 free(current_mail_spool);
4807 current_mail_spool = strndup(buffer, text_buffer_size);
4814 CONF("minimum_size") {
4816 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4818 if (sscanf(value, "%d", &minimum_width) != 1) {
4826 CONF("maximum_width") {
4828 if (sscanf(value, "%d", &maximum_width) != 1) {
4836 CONF("no_buffers") {
4837 no_buffers = string_to_bool(value);
4839 CONF("top_name_width") {
4841 if (sscanf(value, "%u", &top_name_width) != 1) {
4847 if (top_name_width >= max_user_text) {
4848 top_name_width = max_user_text - 1;
4851 CONF("top_cpu_separate") {
4852 cpu_separate = string_to_bool(value);
4854 CONF("short_units") {
4855 short_units = string_to_bool(value);
4857 CONF("format_human_readable") {
4858 format_human_readable = string_to_bool(value);
4861 CONF("hddtemp_host") {
4862 set_hddtemp_host(value);
4864 CONF("hddtemp_port") {
4865 set_hddtemp_port(value);
4867 #endif /* HDDTEMP */
4868 CONF("pad_percents") {
4869 pad_percents = atoi(value);
4873 CONF("own_window") {
4875 own_window = string_to_bool(value);
4878 CONF("own_window_class") {
4880 memset(window.class_name, 0, sizeof(window.class_name));
4881 strncpy(window.class_name, value,
4882 sizeof(window.class_name) - 1);
4885 CONF("own_window_title") {
4887 memset(window.title, 0, sizeof(window.title));
4888 strncpy(window.title, value, sizeof(window.title) - 1);
4891 CONF("own_window_transparent") {
4893 set_transparent = string_to_bool(value);
4896 CONF("own_window_hints") {
4898 char *p_hint, *p_save;
4899 char delim[] = ", ";
4901 /* tokenize the value into individual hints */
4902 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4904 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4905 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4906 SET_HINT(window.hints, HINT_UNDECORATED);
4907 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4908 SET_HINT(window.hints, HINT_BELOW);
4909 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4910 SET_HINT(window.hints, HINT_ABOVE);
4911 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4912 SET_HINT(window.hints, HINT_STICKY);
4913 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4914 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4915 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4916 SET_HINT(window.hints, HINT_SKIP_PAGER);
4921 p_hint = strtok_r(NULL, delim, &p_save);
4922 } while (p_hint != NULL);
4928 CONF("own_window_type") {
4930 if (strncmp(value, "normal", 6) == EQUAL) {
4931 window.type = TYPE_NORMAL;
4932 } else if (strncmp(value, "desktop", 7) == EQUAL) {
4933 window.type = TYPE_DESKTOP;
4934 } else if (strncmp(value, "dock", 4) == EQUAL) {
4935 window.type = TYPE_DOCK;
4936 text_alignment = TOP_LEFT;
4937 } else if (strncmp(value, "panel", 5) == EQUAL) {
4938 window.type = TYPE_PANEL;
4939 } else if (strncmp(value, "override", 8) == EQUAL) {
4940 window.type = TYPE_OVERRIDE;
4949 CONF("stippled_borders") {
4951 stippled_borders = strtol(value, 0, 0);
4953 stippled_borders = 4;
4957 CONF("imlib_cache_size") {
4959 cimlib_set_cache_size(atoi(value));
4962 CONF("imlib_cache_flush_interval") {
4964 cimlib_set_cache_flush_interval(atoi(value));
4969 CONF("update_interval_on_battery") {
4971 update_interval_bat = strtod(value, 0);
4976 CONF("update_interval") {
4978 set_update_interval(strtod(value, 0));
4982 if (info.music_player_interval == 0) {
4983 // default to update_interval
4984 info.music_player_interval = update_interval;
4987 CONF("total_run_times") {
4989 total_run_times = strtod(value, 0);
4995 stuff_in_uppercase = string_to_bool(value);
4997 CONF("max_specials") {
4999 max_specials = atoi(value);
5004 CONF("max_user_text") {
5006 max_user_text = atoi(value);
5011 CONF("text_buffer_size") {
5013 text_buffer_size = atoi(value);
5014 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5015 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5016 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5024 if (output_methods & TO_X) {
5025 X11_initialisation();
5034 global_text = (char *) malloc(1);
5035 global_text[0] = '\0';
5038 unsigned int l = strlen(global_text);
5040 char buf[CONF_BUFF_SIZE];
5042 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5048 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5056 /* Check for continuation of \\-\n. */
5057 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5058 global_text[l-1] = '\0';
5062 global_text = (char *) realloc(global_text, l + bl + 1);
5063 strcat(global_text, buf);
5065 if (strlen(global_text) > max_user_text) {
5069 global_text_lines = line + 1;
5072 #ifdef TCP_PORT_MONITOR
5073 CONF("max_port_monitor_connections") {
5075 if (!value || (sscanf(value, "%d", &max) != 1)) {
5076 /* an error. use default, warn and continue. */
5077 tcp_portmon_set_max_connections(0);
5079 } else if (tcp_portmon_set_max_connections(max)) {
5080 /* max is < 0, default has been set*/
5085 CONF("if_up_strictness") {
5087 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5088 ifup_strictness = IFUP_UP;
5089 } else if (strcasecmp(value, "up") == EQUAL) {
5090 ifup_strictness = IFUP_UP;
5091 } else if (strcasecmp(value, "link") == EQUAL) {
5092 ifup_strictness = IFUP_LINK;
5093 } else if (strcasecmp(value, "address") == EQUAL) {
5094 ifup_strictness = IFUP_ADDR;
5096 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5097 ifup_strictness = IFUP_UP;
5101 CONF("temperature_unit") {
5103 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5104 } else if (set_temp_output_unit(value)) {
5105 NORM_ERR("temperature_unit: incorrect argument");
5112 char *ptr = strtok(value, " ");
5115 ptr = strtok(NULL, " ");
5122 CONF("lua_draw_hook_pre") {
5124 llua_set_draw_pre_hook(value);
5129 CONF("lua_draw_hook_post") {
5131 llua_set_draw_post_hook(value);
5136 CONF("lua_startup_hook") {
5138 llua_set_startup_hook(value);
5143 CONF("lua_shutdown_hook") {
5145 llua_set_shutdown_hook(value);
5151 #endif /* HAVE_LUA */
5163 CONF("default_color"){}
5164 CONF3("default_shade_color", "default_shadecolor"){}
5165 CONF3("default_outline_color", "default_outlinecolor") {}
5166 CONF("own_window_colour") {}
5169 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5175 if (info.music_player_interval == 0) {
5176 // default to update_interval
5177 info.music_player_interval = update_interval;
5179 if (!global_text) { // didn't supply any text
5180 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5182 if (!output_methods) {
5183 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5185 #if defined(NCURSES)
5187 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5188 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5189 output_methods &= ~TO_NCURSES;
5193 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5194 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5195 output_methods &= ~(TO_STDOUT | TO_STDERR);
5197 #endif /* NCURSES */
5202 static void load_config_file_x11(const char *f)
5207 fp = open_config_file(f);
5211 DBGP("reading contents from config file '%s'", f);
5214 char buff[CONF_BUFF_SIZE], *name, *value;
5215 int ret = do_config_step(&line, fp, buff, &name, &value);
5216 if (ret == CONF_BREAK) {
5218 } else if (ret == CONF_CONTINUE) {
5223 X11_initialisation();
5224 if (x_initialised == YES) {
5226 color0 = get_x11_color(value);
5233 X11_initialisation();
5234 if (x_initialised == YES) {
5236 color1 = get_x11_color(value);
5243 X11_initialisation();
5244 if (x_initialised == YES) {
5246 color2 = get_x11_color(value);
5253 X11_initialisation();
5254 if (x_initialised == YES) {
5256 color3 = get_x11_color(value);
5263 X11_initialisation();
5264 if (x_initialised == YES) {
5266 color4 = get_x11_color(value);
5273 X11_initialisation();
5274 if (x_initialised == YES) {
5276 color5 = get_x11_color(value);
5283 X11_initialisation();
5284 if (x_initialised == YES) {
5286 color6 = get_x11_color(value);
5293 X11_initialisation();
5294 if (x_initialised == YES) {
5296 color7 = get_x11_color(value);
5303 X11_initialisation();
5304 if (x_initialised == YES) {
5306 color8 = get_x11_color(value);
5313 X11_initialisation();
5314 if (x_initialised == YES) {
5316 color9 = get_x11_color(value);
5322 CONF("default_color") {
5323 X11_initialisation();
5324 if (x_initialised == YES) {
5326 default_fg_color = get_x11_color(value);
5332 CONF3("default_shade_color", "default_shadecolor") {
5333 X11_initialisation();
5334 if (x_initialised == YES) {
5336 default_bg_color = get_x11_color(value);
5342 CONF3("default_outline_color", "default_outlinecolor") {
5343 X11_initialisation();
5344 if (x_initialised == YES) {
5346 default_out_color = get_x11_color(value);
5353 CONF("own_window_colour") {
5354 X11_initialisation();
5355 if (x_initialised == YES) {
5357 background_colour = get_x11_color(value);
5359 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5360 "'#' for hex colours");
5366 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5367 if(output_methods & TO_X) {
5368 X11_initialisation();
5377 #undef CONF_CONTINUE
5378 #undef CONF_BUFF_SIZE
5386 static void print_help(const char *prog_name) {
5387 printf("Usage: %s [OPTION]...\n"
5388 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5389 "window. Command line options will override configurations defined in config\n"
5391 " -v, --version version\n"
5392 " -q, --quiet quiet mode\n"
5393 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5394 " -c, --config=FILE config file to load\n"
5395 #ifdef CONFIG_OUTPUT
5396 " -C, --print-config print the builtin default config to stdout\n"
5397 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5399 " -d, --daemonize daemonize, fork to background\n"
5400 " -h, --help help\n"
5402 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5403 " -f, --font=FONT font to use\n"
5404 " -X, --display=DISPLAY X11 display to use\n"
5406 " -o, --own-window create own window to draw\n"
5409 " -b, --double-buffer double buffer (prevents flickering)\n"
5411 " -w, --window-id=WIN_ID window id to draw\n"
5412 " -x X x position\n"
5413 " -y Y y position\n"
5415 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5416 " -u, --interval=SECS update interval\n"
5417 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5418 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5423 /* : means that character before that takes an argument */
5424 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5434 #ifdef CONFIG_OUTPUT
5439 static const struct option longopts[] = {
5440 { "help", 0, NULL, 'h' },
5441 { "version", 0, NULL, 'V' },
5442 { "debug", 0, NULL, 'D' },
5443 { "config", 1, NULL, 'c' },
5444 #ifdef CONFIG_OUTPUT
5445 { "print-config", 0, NULL, 'C' },
5447 { "daemonize", 0, NULL, 'd' },
5449 { "alignment", 1, NULL, 'a' },
5450 { "font", 1, NULL, 'f' },
5451 { "display", 1, NULL, 'X' },
5453 { "own-window", 0, NULL, 'o' },
5456 { "double-buffer", 0, NULL, 'b' },
5458 { "window-id", 1, NULL, 'w' },
5460 { "text", 1, NULL, 't' },
5461 { "interval", 0, NULL, 'u' },
5462 { "pause", 0, NULL, 'p' },
5466 void initialisation(int argc, char **argv) {
5467 struct sigaction act, oact;
5469 set_default_configurations();
5470 load_config_file(current_config);
5471 currentconffile = conftree_add(currentconffile, current_config);
5473 /* init specials array */
5474 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5475 NORM_ERR("failed to create specials array");
5479 if (current_mail_spool == NULL) {
5482 variable_substitute(MAIL_FILE, buf, 256);
5484 if (buf[0] != '\0') {
5485 current_mail_spool = strndup(buf, text_buffer_size);
5490 /* handle other command line arguments */
5492 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5493 || defined(__NetBSD__)
5494 optind = optreset = 1;
5499 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5500 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5501 "kvm_open")) == NULL) {
5502 CRIT_ERR(NULL, NULL, "cannot read kvm");
5507 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5516 fork_to_background = 1;
5519 global_debug_level++;
5523 set_first_font(optarg);
5526 text_alignment = string_to_alignment(optarg);
5545 global_text = strndup(optarg, max_user_text);
5546 convert_escapes(global_text);
5550 update_interval = strtod(optarg, 0);
5551 update_interval_old = update_interval;
5552 if (info.music_player_interval == 0) {
5553 // default to update_interval
5554 info.music_player_interval = update_interval;
5559 total_run_times = strtod(optarg, 0);
5563 gap_x = atoi(optarg);
5567 gap_y = atoi(optarg);
5571 startup_pause = atoi(optarg);
5572 sleep(startup_pause);
5582 if (output_methods & TO_X) {
5583 load_config_file_x11(current_config);
5587 /* generate text and get initial size */
5588 extract_variable_text(global_text);
5595 if (fork_to_background) {
5600 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5607 fprintf(stderr, "\n");
5612 /* parent process */
5613 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5620 start_update_threading();
5622 text_buffer = malloc(max_user_text);
5623 memset(text_buffer, 0, max_user_text);
5624 tmpstring1 = malloc(text_buffer_size);
5625 memset(tmpstring1, 0, text_buffer_size);
5626 tmpstring2 = malloc(text_buffer_size);
5627 memset(tmpstring2, 0, text_buffer_size);
5632 X11_create_window();
5635 llua_setup_info(&info, update_interval);
5636 #endif /* HAVE_LUA */
5641 /* Set signal handlers */
5642 act.sa_handler = signal_handler;
5643 sigemptyset(&act.sa_mask);
5646 act.sa_flags |= SA_RESTART;
5649 if ( sigaction(SIGINT, &act, &oact) < 0
5650 || sigaction(SIGALRM, &act, &oact) < 0
5651 || sigaction(SIGUSR1, &act, &oact) < 0
5652 || sigaction(SIGHUP, &act, &oact) < 0
5653 || sigaction(SIGTERM, &act, &oact) < 0) {
5654 NORM_ERR("error setting signal handler: %s", strerror(errno));
5658 llua_startup_hook();
5659 #endif /* HAVE_LUA */
5662 int main(int argc, char **argv)
5671 g_signal_pending = 0;
5672 max_user_text = MAX_USER_TEXT_DEFAULT;
5674 memset(&info, 0, sizeof(info));
5678 #ifdef TCP_PORT_MONITOR
5679 /* set default connection limit */
5680 tcp_portmon_set_max_connections(0);
5683 /* handle command line parameters that don't change configs */
5685 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5686 || ((s = getenv("LANG")) && *s)) {
5687 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5689 NORM_ERR("malloc failed");
5691 for (x = 0; x < strlen(s); x++) {
5692 temp[x] = tolower(s[x]);
5695 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5701 if (!setlocale(LC_CTYPE, "")) {
5702 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5706 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5717 if (current_config) {
5718 free(current_config);
5720 current_config = strndup(optarg, max_user_text);
5723 freopen("/dev/null", "w", stderr);
5726 print_help(argv[0]);
5728 #ifdef CONFIG_OUTPUT
5735 window.window = strtol(optarg, 0, 0);
5740 disp = strdup(optarg);
5749 /* check if specified config file is valid */
5750 if (current_config) {
5752 if (stat(current_config, &sb) ||
5753 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5754 NORM_ERR("invalid configuration file '%s'\n", current_config);
5755 free(current_config);
5760 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5762 if (!current_config) {
5763 /* load default config file */
5764 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5767 /* Try to use personal config file first */
5768 to_real_path(buf, CONFIG_FILE);
5769 if (buf[0] && (fp = fopen(buf, "r"))) {
5770 current_config = strndup(buf, max_user_text);
5774 /* Try to use system config file if personal config not readable */
5775 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5776 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5780 /* No readable config found */
5781 if (!current_config) {
5782 #ifdef CONFIG_OUTPUT
5783 current_config = strdup("==builtin==");
5784 NORM_ERR("no readable personal or system-wide config file found,"
5785 " using builtin default");
5787 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5788 #endif /* ! CONF_OUTPUT */
5793 /* Load xoap keys, if existing */
5797 #ifdef HAVE_SYS_INOTIFY_H
5798 inotify_fd = inotify_init1(IN_NONBLOCK);
5799 #endif /* HAVE_SYS_INOTIFY_H */
5801 initialisation(argc, argv);
5805 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5813 void alarm_handler(void) {
5815 kill(childpid, SIGTERM);
5819 static void signal_handler(int sig)
5821 /* signal handler is light as a feather, as it should be.
5822 * we will poll g_signal_pending with each loop of conky
5823 * and do any signal processing there, NOT here (except
5824 * SIGALRM because this is caused when conky is hanging) */
5825 if(sig == SIGALRM) {
5828 g_signal_pending = sig;