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 */
274 #ifdef HAVE_LUA_CAIRO
276 #endif /* HAVE_LUA_CAIRO */
277 #ifdef HAVE_LUA_IMLIB2
280 #endif /* HAVE_LUA */
286 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
291 static void X11_create_window(void);
292 static void X11_initialisation(void);
294 struct _x11_stuff_s {
298 XserverRegion region2, part;
299 int event_base, error_base;
305 static int text_start_x, text_start_y; /* text start position in window */
306 static int text_width, text_height;
321 /* display to connect to */
322 static char *disp = NULL;
326 /* struct that has all info to be shared between
327 * instances of the same text object */
328 struct information info;
330 /* path to config file */
331 char *current_config;
333 /* set to 1 if you want all text to be in uppercase */
334 static unsigned int stuff_in_uppercase;
336 /* Run how many times? */
337 static unsigned long total_run_times;
340 static int fork_to_background;
342 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
344 /* filenames for output */
345 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
346 char *append_file = NULL; FILE *append_fpointer = NULL;
350 static int show_graph_scale;
351 static int show_graph_range;
353 /* Position on the screen */
354 static int text_alignment;
355 static int gap_x, gap_y;
358 static int draw_borders;
359 static int draw_graph_borders;
360 static int stippled_borders;
362 int get_stippled_borders(void)
364 return stippled_borders;
367 static int draw_shades, draw_outline;
369 long default_fg_color, default_bg_color, default_out_color;
371 /* create own window or draw stuff to root? */
372 static int set_transparent = 0;
375 static int own_window = 0;
376 static int background_colour = 0;
378 /* fixed size/pos is set if wm/user changes them */
379 static int fixed_size = 0, fixed_pos = 0;
382 static int minimum_width, minimum_height;
383 static int maximum_width;
388 static int sensor_device;
391 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
394 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
395 unsigned int max_user_text;
397 /* maximum size of individual text buffers, ie $exec buffer size */
398 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
403 /* no buffers in used memory? */
406 /* pad percentages to decimals? */
407 static int pad_percents = 0;
409 static char *global_text = 0;
411 char *get_global_text(void)
416 long global_text_lines;
418 static int total_updates;
419 static int updatereset;
421 void set_updatereset(int i)
426 int get_updatereset(void)
431 int check_contains(char *f, char *s)
434 FILE *where = open_file(f, 0);
439 while (fgets(buf1, 256, where)) {
440 if (strstr(buf1, s)) {
447 NORM_ERR("Could not open the file");
452 #define SECRIT_MULTILINE_CHAR '\x02'
456 static inline int calc_text_width(const char *s)
458 size_t slen = strlen(s);
460 if ((output_methods & TO_X) == 0) {
468 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
469 (const FcChar8 *) s, slen, &gi);
471 XftTextExtents8(display, fonts[selected_font].xftfont,
472 (const FcChar8 *) s, slen, &gi);
478 return XTextWidth(fonts[selected_font].font, s, slen);
483 /* formatted text to render on screen, generated in generate_text(),
484 * drawn in draw_stuff() */
486 static char *text_buffer;
488 /* quite boring functions */
490 static inline void for_each_line(char *b, int f(char *, int))
493 int special_index = 0; /* specials index */
495 for (ps = b, pe = b; *pe; pe++) {
498 special_index = f(ps, special_index);
505 f(ps, special_index);
509 static void convert_escapes(char *buf)
511 char *p = buf, *s = buf;
518 } else if (*s == '\\') {
529 /* Prints anything normally printed with snprintf according to the current value
530 * of use_spacer. Actually slightly more flexible than snprintf, as you can
531 * safely specify the destination buffer as one of your inputs. */
532 int spaced_print(char *buf, int size, const char *format, int width, ...)
541 tempbuf = malloc(size * sizeof(char));
543 // Passes the varargs along to vsnprintf
544 va_start(argp, width);
545 vsnprintf(tempbuf, size, format, argp);
548 switch (use_spacer) {
550 len = snprintf(buf, size, "%s", tempbuf);
553 len = snprintf(buf, size, "%*s", width, tempbuf);
556 len = snprintf(buf, size, "%-*s", width, tempbuf);
563 /* print percentage values
565 * - i.e., unsigned values between 0 and 100
566 * - respect the value of pad_percents */
567 int percent_print(char *buf, int size, unsigned value)
569 return spaced_print(buf, size, "%u", pad_percents, value);
572 /* converts from bytes to human readable format (K, M, G, T)
574 * The algorithm always divides by 1024, as unit-conversion of byte
575 * counts suggests. But for output length determination we need to
576 * compare with 1000 here, as we print in decimal form. */
577 void human_readable(long long num, char *buf, int size)
579 const char **suffix = suffixes;
585 /* Possibly just output as usual, for example for stdout usage */
586 if (!format_human_readable) {
587 spaced_print(buf, size, "%d", 6, round_to_int(num));
598 if (llabs(num) < 1000LL) {
599 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
603 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
611 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
613 * The goal is to always have a significance of 3, by
614 * adjusting the decimal part of the number. Sample output:
618 * so the point of alignment resides between number and unit. The
619 * upside of this is that there is minimal padding necessary, though
620 * there should be a way to make alignment take place at the decimal
621 * dot (then with fixed width decimal part).
623 * Note the repdigits below: when given a precision value, printf()
624 * rounds the float to it, not just cuts off the remaining digits. So
625 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
626 * printed with a precision of 0. Yay. */
628 precision = 0; /* print 100-999 without decimal part */
630 precision = 1; /* print 10-99 with one decimal place */
632 precision = 2; /* print 0-9 with two decimal places */
634 spaced_print(buf, size, format, width, precision, fnum, *suffix);
637 /* global object list root element */
638 static struct text_object global_root_object;
640 static long current_text_color;
642 void set_current_text_color(long colour)
644 current_text_color = colour;
647 long get_current_text_color(void)
649 return current_text_color;
652 //adds newstring to to the tree unless you can already see it when travelling back.
653 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
654 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
655 struct conftree* node;
656 struct conftree* node2;
658 for(node = previous; node != NULL; node = node->back) {
659 if(strcmp(node->string, newstring) == 0) {
663 node = malloc(sizeof(struct conftree));
664 if (previous != NULL) {
665 if(previous->vert_next == NULL) {
666 previous->vert_next = node;
668 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
669 node2->horz_next = node;
672 node->string = strdup(newstring);
673 node->horz_next = NULL;
674 node->vert_next = NULL;
675 node->back = previous;
679 void conftree_empty(struct conftree* tree) {
681 conftree_empty(tree->horz_next);
682 conftree_empty(tree->vert_next);
688 struct conftree *currentconffile;
690 static void extract_variable_text(const char *p)
692 free_text_objects(&global_root_object, 0);
706 extract_variable_text_internal(&global_root_object, p);
709 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
711 extract_variable_text_internal(root, txt);
712 generate_text_internal(p, max_user_text, *root, cur);
715 static inline void format_media_player_time(char *buf, const int size,
718 int days, hours, minutes;
720 days = seconds / (24 * 60 * 60);
721 seconds %= (24 * 60 * 60);
722 hours = seconds / (60 * 60);
723 seconds %= (60 * 60);
724 minutes = seconds / 60;
728 snprintf(buf, size, "%i days %i:%02i:%02i", days,
729 hours, minutes, seconds);
730 } else if (hours > 0) {
731 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
734 snprintf(buf, size, "%i:%02i", minutes, seconds);
738 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
739 * multiline objects like $exec work with $align[rc] and friends
741 void substitute_newlines(char *p, long l)
745 while (p && *p && p < s + l) {
747 /* only substitute if it's not the last newline */
748 *p = SECRIT_MULTILINE_CHAR;
754 void generate_text_internal(char *p, int p_max_size,
755 struct text_object root, struct information *cur)
757 struct text_object *obj;
759 int need_to_load_fonts = 0;
762 /* for the OBJ_top* handler */
763 struct process **needed = 0;
766 char buff_in[p_max_size];
768 #endif /* HAVE_ICONV */
772 while (obj && p_max_size > 0) {
773 needed = 0; /* reset for top stuff */
775 /* IFBLOCK jumping algorithm
777 * This is easier as it looks like:
778 * - each IF checks it's condition
779 * - on FALSE: call DO_JUMP
780 * - on TRUE: don't care
781 * - each ELSE calls DO_JUMP unconditionally
782 * - each ENDIF is silently being ignored
785 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
786 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
787 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
788 * regularly, "obj" is being updated to point to obj->next, so object parsing
789 * continues right after the corresponding ELSE or ENDIF. This means that if we
790 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
791 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
792 * jumped, and there is nothing to do.
796 obj = obj->data.ifblock.next; \
799 #define OBJ(a) break; case OBJ_##a:
803 NORM_ERR("not implemented obj type %d", obj->type);
805 print_read_tcp(obj, p, p_max_size);
809 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
811 #endif /* !__OpenBSD__ */
815 ok = get_freq(p, p_max_size, "%.0f", 1,
823 ok = get_freq(p, p_max_size, "%'.2f", 1000,
826 /* OpenBSD has no such flag (SUSv2) */
827 ok = get_freq(p, p_max_size, "%.2f", 1000,
829 #endif /* __OpenBSD */
832 #if defined(__linux__)
836 ok = get_voltage(p, p_max_size, "%.0f", 1,
843 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
849 OBJ(wireless_essid) {
850 print_wireless_essid(obj, p, p_max_size);
853 print_wireless_mode(obj, p, p_max_size);
855 OBJ(wireless_bitrate) {
856 print_wireless_bitrate(obj, p, p_max_size);
859 print_wireless_ap(obj, p, p_max_size);
861 OBJ(wireless_link_qual) {
862 print_wireless_link_qual(obj, p, p_max_size);
864 OBJ(wireless_link_qual_max) {
865 print_wireless_link_qual_max(obj, p, p_max_size);
867 OBJ(wireless_link_qual_perc) {
868 print_wireless_link_qual_perc(obj, p, p_max_size);
870 OBJ(wireless_link_bar) {
871 print_wireless_link_bar(obj, p, p_max_size);
873 #endif /* HAVE_IWLIB */
875 #endif /* __linux__ */
879 get_adt746x_cpu(p, p_max_size);
882 get_adt746x_fan(p, p_max_size);
885 get_acpi_fan(p, p_max_size);
888 get_acpi_ac_adapter(p, p_max_size);
891 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
894 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
896 OBJ(battery_percent) {
897 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
901 if(output_methods & TO_X) {
902 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
905 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
908 get_battery_short_status(p, p_max_size, obj->data.s);
910 #endif /* __OpenBSD__ */
913 human_readable(cur->buffers * 1024, p, 255);
916 human_readable(cur->cached * 1024, p, 255);
919 if (obj->data.i > info.cpu_count) {
920 NORM_ERR("obj->data.i %i info.cpu_count %i",
921 obj->data.i, info.cpu_count);
922 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
924 percent_print(p, p_max_size,
925 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
929 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
933 if(output_methods & TO_X) {
934 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
937 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
941 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
944 print_loadgraph(obj, p);
948 new_fg(p, obj->data.l);
983 snprintf(p, p_max_size, "%s", VERSION);
985 OBJ(conky_build_date) {
986 snprintf(p, p_max_size, "%s", BUILD_DATE);
988 OBJ(conky_build_arch) {
989 snprintf(p, p_max_size, "%s", BUILD_ARCH);
991 #if defined(__linux__)
993 snprintf(p, p_max_size, "%s",
994 get_disk_protect_queue(obj->data.s));
997 snprintf(p, p_max_size, "%s", i8k.version);
1000 snprintf(p, p_max_size, "%s", i8k.bios);
1003 snprintf(p, p_max_size, "%s", i8k.serial);
1008 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1009 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1011 OBJ(i8k_left_fan_status) {
1012 int left_fan_status;
1014 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1015 if (left_fan_status == 0) {
1016 snprintf(p, p_max_size, "off");
1018 if (left_fan_status == 1) {
1019 snprintf(p, p_max_size, "low");
1021 if (left_fan_status == 2) {
1022 snprintf(p, p_max_size, "high");
1025 OBJ(i8k_right_fan_status) {
1026 int right_fan_status;
1028 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1029 if (right_fan_status == 0) {
1030 snprintf(p, p_max_size, "off");
1032 if (right_fan_status == 1) {
1033 snprintf(p, p_max_size, "low");
1035 if (right_fan_status == 2) {
1036 snprintf(p, p_max_size, "high");
1039 OBJ(i8k_left_fan_rpm) {
1040 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1042 OBJ(i8k_right_fan_rpm) {
1043 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1045 OBJ(i8k_ac_status) {
1048 sscanf(i8k.ac_status, "%d", &ac_status);
1049 if (ac_status == -1) {
1050 snprintf(p, p_max_size, "disabled (read i8k docs)");
1052 if (ac_status == 0) {
1053 snprintf(p, p_max_size, "off");
1055 if (ac_status == 1) {
1056 snprintf(p, p_max_size, "on");
1059 OBJ(i8k_buttons_status) {
1060 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1064 get_ibm_acpi_fan(p, p_max_size);
1067 print_ibm_temps(obj, p, p_max_size);
1070 get_ibm_acpi_volume(p, p_max_size);
1072 OBJ(ibm_brightness) {
1073 get_ibm_acpi_brightness(p, p_max_size);
1076 /* information from sony_laptop kernel module
1077 * /sys/devices/platform/sony-laptop */
1078 OBJ(sony_fanspeed) {
1079 get_sony_fanspeed(p, p_max_size);
1082 if (!gateway_exists()) {
1087 print_gateway_iface(p, p_max_size);
1090 print_gateway_ip(p, p_max_size);
1093 snprintf(p, p_max_size, "%d", get_laptop_mode());
1096 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1098 #endif /* __linux__ */
1099 #if (defined(__FreeBSD__) || defined(__linux__))
1101 if (!interface_up(obj)) {
1107 OBJ(obsd_sensors_temp) {
1108 print_obsd_sensors_temp(obj, p, p_max_size);
1110 OBJ(obsd_sensors_fan) {
1111 print_obsd_sensors_fan(obj, p, p_max_size);
1113 OBJ(obsd_sensors_volt) {
1114 print_obsd_sensors_volt(obj, p, p_max_size);
1117 get_obsd_vendor(p, p_max_size);
1120 get_obsd_product(p, p_max_size);
1122 #endif /* __OpenBSD__ */
1125 new_font(p, obj->data.s);
1126 need_to_load_fonts = 1;
1130 print_diskio(obj, 0, p, p_max_size);
1133 print_diskio(obj, 1, p, p_max_size);
1136 print_diskio(obj, -1, p, p_max_size);
1140 print_diskiograph(obj, 0, p);
1142 OBJ(diskiograph_read) {
1143 print_diskiograph(obj, -1, p);
1145 OBJ(diskiograph_write) {
1146 print_diskiograph(obj, 1, p);
1150 print_downspeed(obj, p, p_max_size);
1153 print_downspeedf(obj, p, p_max_size);
1156 OBJ(downspeedgraph) {
1157 print_downspeedgraph(obj, p);
1161 /* Since we see you, you're if has not jumped.
1162 * Do Ninja jump here: without leaving traces.
1163 * This is to prevent us from stale jumped flags.
1165 obj = obj->data.ifblock.next;
1169 /* harmless object, just ignore */
1172 print_addr(obj, p, p_max_size);
1174 #if defined(__linux__)
1176 print_addrs(obj, p, p_max_size);
1178 #endif /* __linux__ */
1179 #if defined(IMLIB2) && defined(X11)
1181 /* doesn't actually draw anything, just queues it omp. the
1182 * image will get drawn after the X event loop */
1183 cimlib_add_image(obj->data.s);
1187 evaluate(obj->data.s, p);
1190 print_exec(obj, p, p_max_size);
1193 print_execp(obj, p, p_max_size);
1197 print_execgauge(obj, p, p_max_size);
1201 print_execbar(obj, p, p_max_size);
1205 print_execgraph(obj, p, p_max_size);
1209 print_execibar(obj, p, p_max_size);
1213 print_execigraph(obj, p, p_max_size);
1216 print_execigauge(obj, p, p_max_size);
1220 print_execi(obj, p, p_max_size);
1223 print_execpi(obj, p);
1226 print_texeci(obj, p, p_max_size);
1229 print_imap_unseen(obj, p, p_max_size);
1231 OBJ(imap_messages) {
1232 print_imap_messages(obj, p, p_max_size);
1235 print_pop3_unseen(obj, p, p_max_size);
1238 print_pop3_used(obj, p, p_max_size);
1241 print_fs_bar(obj, 0, p, p_max_size);
1244 print_fs_free(obj, p, p_max_size);
1247 print_fs_perc(obj, 1, p, p_max_size);
1250 print_fs_size(obj, p, p_max_size);
1253 print_fs_type(obj, p, p_max_size);
1256 print_fs_used(obj, p, p_max_size);
1259 print_fs_bar(obj, 1, p, p_max_size);
1262 print_fs_perc(obj, 0, p, p_max_size);
1265 print_loadavg(obj, p, p_max_size);
1268 new_goto(p, obj->data.i);
1275 new_hr(p, obj->data.i);
1279 print_nameserver(obj, p, p_max_size);
1283 print_eve(obj, p, p_max_size);
1288 curl_print(obj, p, p_max_size);
1293 rss_print_info(obj, p, p_max_size);
1298 print_weather(obj, p, p_max_size);
1302 OBJ(weather_forecast) {
1303 print_weather_forecast(obj, p, p_max_size);
1308 char *str = llua_getstring(obj->data.s);
1310 snprintf(p, p_max_size, "%s", str);
1315 char *str = llua_getstring(obj->data.s);
1323 if (llua_getnumber(obj->data.s, &per)) {
1325 if(output_methods & TO_X) {
1326 new_bar(obj, p, (per/100.0 * 255));
1329 new_bar_in_shell(obj, p, p_max_size, per);
1335 if (llua_getnumber(obj->data.s, &per)) {
1336 new_graph(obj, p, per);
1341 if (llua_getnumber(obj->data.s, &per)) {
1342 new_gauge(obj, p, (per/100.0 * 255));
1346 #endif /* HAVE_LUA */
1352 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1353 snprintf(p, p_max_size, "N/A");
1355 temp_print(p, p_max_size, (double)val,
1356 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1361 new_offset(p, obj->data.i);
1364 new_voffset(p, obj->data.i);
1368 print_sysfs_sensor(obj, p, p_max_size);
1371 print_sysfs_sensor(obj, p, p_max_size);
1374 print_sysfs_sensor(obj, p, p_max_size);
1376 #endif /* __linux__ */
1378 new_alignr(p, obj->data.i);
1381 new_alignc(p, obj->data.i);
1384 char buf[max_user_text];
1385 struct information *tmp_info =
1386 malloc(sizeof(struct information));
1387 memcpy(tmp_info, cur, sizeof(struct information));
1388 generate_text_internal(buf, max_user_text,
1389 *obj->sub, tmp_info);
1391 if (strlen(buf) != 0) {
1397 char expression[max_user_text];
1399 struct information *tmp_info;
1401 tmp_info = malloc(sizeof(struct information));
1402 memcpy(tmp_info, cur, sizeof(struct information));
1403 generate_text_internal(expression, max_user_text,
1404 *obj->sub, tmp_info);
1405 DBGP("parsed arg into '%s'", expression);
1407 val = compare(expression);
1409 NORM_ERR("compare failed for expression '%s'",
1417 if (obj->data.ifblock.str
1418 && !check_contains(obj->data.ifblock.s,
1419 obj->data.ifblock.str)) {
1421 } else if (obj->data.ifblock.s
1422 && access(obj->data.ifblock.s, F_OK)) {
1427 if ((obj->data.ifblock.s)
1428 && (!check_mount(obj->data.ifblock.s))) {
1434 if (!get_process_by_name(obj->data.ifblock.s)) {
1436 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
1441 #if defined(__linux__)
1443 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1447 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1450 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1455 human_readable(cur->mem * 1024, p, 255);
1458 human_readable(cur->memeasyfree * 1024, p, 255);
1461 human_readable(cur->memfree * 1024, p, 255);
1464 human_readable(cur->memmax * 1024, p, 255);
1468 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1472 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1477 if(output_methods & TO_X) {
1478 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1481 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1485 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1490 print_mixer(obj, 0, p, p_max_size);
1493 print_mixer(obj, -1, p, p_max_size);
1496 print_mixer(obj, 1, p, p_max_size);
1500 print_mixer_bar(obj, 0, p);
1503 print_mixer_bar(obj, -1, p);
1506 print_mixer_bar(obj, 1, p);
1509 OBJ(if_mixer_mute) {
1510 if (!check_mixer_muted(obj)) {
1515 #define NOT_IN_X "Not running in X"
1517 if(x_initialised != YES) {
1518 strncpy(p, NOT_IN_X, p_max_size);
1520 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1523 OBJ(monitor_number) {
1524 if(x_initialised != YES) {
1525 strncpy(p, NOT_IN_X, p_max_size);
1527 snprintf(p, p_max_size, "%d", cur->x11.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.desktop.current);
1537 OBJ(desktop_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.number);
1545 if(x_initialised != YES) {
1546 strncpy(p, NOT_IN_X, p_max_size);
1547 }else if(cur->x11.desktop.name != NULL) {
1548 strncpy(p, cur->x11.desktop.name, p_max_size);
1555 print_mails(obj, p, p_max_size);
1558 print_new_mails(obj, p, p_max_size);
1561 print_seen_mails(obj, p, p_max_size);
1564 print_unseen_mails(obj, p, p_max_size);
1566 OBJ(flagged_mails) {
1567 print_flagged_mails(obj, p, p_max_size);
1569 OBJ(unflagged_mails) {
1570 print_unflagged_mails(obj, p, p_max_size);
1572 OBJ(forwarded_mails) {
1573 print_forwarded_mails(obj, p, p_max_size);
1575 OBJ(unforwarded_mails) {
1576 print_unforwarded_mails(obj, p, p_max_size);
1578 OBJ(replied_mails) {
1579 print_replied_mails(obj, p, p_max_size);
1581 OBJ(unreplied_mails) {
1582 print_unreplied_mails(obj, p, p_max_size);
1585 print_draft_mails(obj, p, p_max_size);
1587 OBJ(trashed_mails) {
1588 print_trashed_mails(obj, p, p_max_size);
1591 print_mboxscan(obj, p, p_max_size);
1594 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1597 new_outline(p, obj->data.l);
1600 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1602 OBJ(running_processes) {
1603 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1606 snprintf(p, p_max_size, "%s", obj->data.s);
1610 new_bg(p, obj->data.l);
1613 new_stippled_hr(obj, p);
1617 human_readable(cur->swap * 1024, p, 255);
1620 human_readable(cur->swapfree * 1024, p, 255);
1623 human_readable(cur->swapmax * 1024, p, 255);
1626 if (cur->swapmax == 0) {
1627 strncpy(p, "No swap", p_max_size);
1629 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1634 if(output_methods & TO_X) {
1635 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1638 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1641 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1644 print_time(obj, p, p_max_size);
1647 print_utime(obj, p, p_max_size);
1650 print_tztime(obj, p, p_max_size);
1653 print_totaldown(obj, p, p_max_size);
1656 print_totalup(obj, p, p_max_size);
1659 snprintf(p, p_max_size, "%d", total_updates);
1662 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
1667 print_upspeed(obj, p, p_max_size);
1670 print_upspeedf(obj, p, p_max_size);
1674 print_upspeedgraph(obj, p);
1678 format_seconds_short(p, p_max_size, (int) cur->uptime);
1681 format_seconds(p, p_max_size, (int) cur->uptime);
1684 snprintf(p, p_max_size, "%s", cur->users.names);
1687 snprintf(p, p_max_size, "%s", cur->users.terms);
1690 snprintf(p, p_max_size, "%s", cur->users.times);
1693 snprintf(p, p_max_size, "%d", cur->users.number);
1695 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1696 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1700 msg = get_apm_adapter();
1701 snprintf(p, p_max_size, "%s", msg);
1704 OBJ(apm_battery_life) {
1707 msg = get_apm_battery_life();
1708 snprintf(p, p_max_size, "%s", msg);
1711 OBJ(apm_battery_time) {
1714 msg = get_apm_battery_time();
1715 snprintf(p, p_max_size, "%s", msg);
1718 #endif /* __FreeBSD__ __OpenBSD__ */
1721 #define mpd_printf(fmt, val) \
1722 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
1723 #define mpd_sprintf(val) { \
1724 if (!obj->data.i || obj->data.i > p_max_size) \
1725 mpd_printf("%s", val); \
1727 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
1732 mpd_sprintf(artist);
1736 mpd_printf("%s", random);
1738 mpd_printf("%s", repeat);
1746 mpd_printf("%d", volume);
1748 mpd_printf("%d", bitrate);
1750 mpd_printf("%s", status);
1752 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
1755 format_media_player_time(p, p_max_size, mpd_get_info()->length);
1758 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
1762 if(output_methods & TO_X) {
1763 new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
1766 new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
1769 struct mpd_s *mpd = mpd_get_info();
1770 int len = obj->data.i;
1771 if (len == 0 || len > p_max_size)
1774 memset(p, 0, p_max_size);
1775 if (mpd->artist && *mpd->artist &&
1776 mpd->title && *mpd->title) {
1777 snprintf(p, len, "%s - %s", mpd->artist,
1779 } else if (mpd->title && *mpd->title) {
1780 snprintf(p, len, "%s", mpd->title);
1781 } else if (mpd->artist && *mpd->artist) {
1782 snprintf(p, len, "%s", mpd->artist);
1783 } else if (mpd->file && *mpd->file) {
1784 snprintf(p, len, "%s", mpd->file);
1789 OBJ(if_mpd_playing) {
1790 if (!mpd_get_info()->is_playing) {
1799 #define MOC_PRINT(t, a) \
1800 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1802 MOC_PRINT(state, "??");
1805 MOC_PRINT(file, "no file");
1808 MOC_PRINT(title, "no title");
1811 MOC_PRINT(artist, "no artist");
1814 MOC_PRINT(song, "no song");
1817 MOC_PRINT(album, "no album");
1819 OBJ(moc_totaltime) {
1820 MOC_PRINT(totaltime, "0:00");
1823 MOC_PRINT(timeleft, "0:00");
1826 MOC_PRINT(curtime, "0:00");
1829 MOC_PRINT(bitrate, "0Kbps");
1832 MOC_PRINT(rate, "0KHz");
1838 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
1841 snprintf(p, p_max_size, "%s", cur->xmms2.album);
1844 snprintf(p, p_max_size, "%s", cur->xmms2.title);
1847 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
1849 OBJ(xmms2_comment) {
1850 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
1853 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1856 snprintf(p, p_max_size, "%s", cur->xmms2.status);
1859 snprintf(p, p_max_size, "%s", cur->xmms2.date);
1861 OBJ(xmms2_tracknr) {
1862 if (cur->xmms2.tracknr != -1) {
1863 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
1866 OBJ(xmms2_bitrate) {
1867 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
1870 snprintf(p, p_max_size, "%u", cur->xmms2.id);
1873 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
1875 OBJ(xmms2_elapsed) {
1876 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
1877 (cur->xmms2.elapsed / 1000) % 60);
1879 OBJ(xmms2_duration) {
1880 snprintf(p, p_max_size, "%02d:%02d",
1881 cur->xmms2.duration / 60000,
1882 (cur->xmms2.duration / 1000) % 60);
1884 OBJ(xmms2_percent) {
1885 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
1889 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
1892 OBJ(xmms2_playlist) {
1893 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
1895 OBJ(xmms2_timesplayed) {
1896 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
1899 if (strlen(cur->xmms2.title) < 2
1900 && strlen(cur->xmms2.title) < 2) {
1901 snprintf(p, p_max_size, "%s", cur->xmms2.url);
1903 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
1907 OBJ(if_xmms2_connected) {
1908 if (cur->xmms2.conn_state != 1) {
1914 OBJ(audacious_status) {
1915 snprintf(p, p_max_size, "%s",
1916 cur->audacious.items[AUDACIOUS_STATUS]);
1918 OBJ(audacious_title) {
1919 snprintf(p, cur->audacious.max_title_len > 0
1920 ? cur->audacious.max_title_len : p_max_size, "%s",
1921 cur->audacious.items[AUDACIOUS_TITLE]);
1923 OBJ(audacious_length) {
1924 snprintf(p, p_max_size, "%s",
1925 cur->audacious.items[AUDACIOUS_LENGTH]);
1927 OBJ(audacious_length_seconds) {
1928 snprintf(p, p_max_size, "%s",
1929 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1931 OBJ(audacious_position) {
1932 snprintf(p, p_max_size, "%s",
1933 cur->audacious.items[AUDACIOUS_POSITION]);
1935 OBJ(audacious_position_seconds) {
1936 snprintf(p, p_max_size, "%s",
1937 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
1939 OBJ(audacious_bitrate) {
1940 snprintf(p, p_max_size, "%s",
1941 cur->audacious.items[AUDACIOUS_BITRATE]);
1943 OBJ(audacious_frequency) {
1944 snprintf(p, p_max_size, "%s",
1945 cur->audacious.items[AUDACIOUS_FREQUENCY]);
1947 OBJ(audacious_channels) {
1948 snprintf(p, p_max_size, "%s",
1949 cur->audacious.items[AUDACIOUS_CHANNELS]);
1951 OBJ(audacious_filename) {
1952 snprintf(p, p_max_size, "%s",
1953 cur->audacious.items[AUDACIOUS_FILENAME]);
1955 OBJ(audacious_playlist_length) {
1956 snprintf(p, p_max_size, "%s",
1957 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
1959 OBJ(audacious_playlist_position) {
1960 snprintf(p, p_max_size, "%s",
1961 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
1963 OBJ(audacious_main_volume) {
1964 snprintf(p, p_max_size, "%s",
1965 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
1968 OBJ(audacious_bar) {
1972 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
1973 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
1974 new_bar(obj, p, (int) (progress * 255.0f));
1977 #endif /* AUDACIOUS */
1981 snprintf(p, p_max_size, "%s", cur->bmpx.title);
1984 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
1987 snprintf(p, p_max_size, "%s", cur->bmpx.album);
1990 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
1993 snprintf(p, p_max_size, "%i", cur->bmpx.track);
1996 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
1999 /* we have four different types of top (top, top_mem,
2000 * top_time and top_io). To avoid having almost-same code four
2001 * times, we have this special handler. */
2009 /* yes, passing top_name_width instead
2010 * of p_max_size is intended here */
2011 print_top(obj, p, top_name_width);
2013 print_tailhead("tail", obj, p, p_max_size);
2016 print_tailhead("head", obj, p, p_max_size);
2020 FILE *fp = open_file(obj->data.s, &rep);
2023 /* FIXME: use something more general (see also tail.c, head.c */
2024 #define BUFSZ 0x1000
2029 while(fgets(buf, BUFSZ, fp) != NULL){
2030 for(j = 0; buf[j] != 0; j++) {
2031 if(buf[j] == '\n') {
2036 sprintf(p, "%d", lines);
2039 sprintf(p, "File Unreadable");
2045 FILE *fp = open_file(obj->data.s, &rep);
2050 char inword = FALSE;
2053 while(fgets(buf, BUFSZ, fp) != NULL){
2054 for(j = 0; buf[j] != 0; j++) {
2055 if(!isspace(buf[j])) {
2056 if(inword == FALSE) {
2065 sprintf(p, "%d", words);
2068 sprintf(p, "File Unreadable");
2071 #ifdef TCP_PORT_MONITOR
2073 tcp_portmon_action(obj, p, p_max_size);
2075 #endif /* TCP_PORT_MONITOR */
2079 do_iconv_start(obj);
2084 #endif /* HAVE_ICONV */
2086 OBJ(entropy_avail) {
2087 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2090 percent_print(p, p_max_size,
2091 cur->entropy.entropy_avail *
2092 100 / cur->entropy.poolsize);
2094 OBJ(entropy_poolsize) {
2095 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2098 double entropy_perc;
2100 entropy_perc = (double) cur->entropy.entropy_avail /
2101 (double) cur->entropy.poolsize;
2103 if(output_methods & TO_X) {
2104 new_bar(obj, p, (int) (entropy_perc * 255.0f));
2107 new_bar_in_shell(obj, p, p_max_size, (int) (entropy_perc * 100.0f));
2113 s = smapi_get_val(obj->data.s);
2114 snprintf(p, p_max_size, "%s", s);
2118 OBJ(if_smapi_bat_installed) {
2120 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
2121 if(!smapi_bat_installed(idx)) {
2125 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2127 OBJ(smapi_bat_perc) {
2129 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2130 val = smapi_bat_installed(idx) ?
2131 smapi_get_bat_int(idx, "remaining_percent") : 0;
2132 percent_print(p, p_max_size, val);
2134 NORM_ERR("argument to smapi_bat_perc must be an integer");
2136 OBJ(smapi_bat_temp) {
2138 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2139 val = smapi_bat_installed(idx) ?
2140 smapi_get_bat_int(idx, "temperature") : 0;
2141 /* temperature is in milli degree celsius */
2142 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2144 NORM_ERR("argument to smapi_bat_temp must be an integer");
2146 OBJ(smapi_bat_power) {
2148 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2149 val = smapi_bat_installed(idx) ?
2150 smapi_get_bat_int(idx, "power_now") : 0;
2151 /* power_now is in mW, set to W with one digit precision */
2152 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2154 NORM_ERR("argument to smapi_bat_power must be an integer");
2157 OBJ(smapi_bat_bar) {
2158 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2159 new_bar(obj, p, (int)
2160 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2168 char buf[max_user_text];
2170 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2171 snprintf(p, p_max_size, "%s", buf);
2177 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2178 char buf[max_user_text];
2181 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2182 snprintf(p, p_max_size, "%s", buf);
2183 if(total_updates % 2) {
2184 for(j=0; p[j] != 0; j++) {
2190 char buf[max_user_text];
2192 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2194 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2195 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2196 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2197 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2198 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2199 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2200 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2202 snprintf(p, p_max_size, "%s", buf);
2205 print_scroll(obj, p, p_max_size, cur);
2208 print_combine(obj, p, cur);
2212 print_nvidia_value(obj, display, p, p_max_size);
2217 /* This is just a meta-object to set host:port */
2220 snprintf(p, p_max_size, "%s",
2221 cur->apcupsd.items[APCUPSD_NAME]);
2223 OBJ(apcupsd_model) {
2224 snprintf(p, p_max_size, "%s",
2225 cur->apcupsd.items[APCUPSD_MODEL]);
2227 OBJ(apcupsd_upsmode) {
2228 snprintf(p, p_max_size, "%s",
2229 cur->apcupsd.items[APCUPSD_UPSMODE]);
2231 OBJ(apcupsd_cable) {
2232 snprintf(p, p_max_size, "%s",
2233 cur->apcupsd.items[APCUPSD_CABLE]);
2235 OBJ(apcupsd_status) {
2236 snprintf(p, p_max_size, "%s",
2237 cur->apcupsd.items[APCUPSD_STATUS]);
2239 OBJ(apcupsd_linev) {
2240 snprintf(p, p_max_size, "%s",
2241 cur->apcupsd.items[APCUPSD_LINEV]);
2244 snprintf(p, p_max_size, "%s",
2245 cur->apcupsd.items[APCUPSD_LOAD]);
2247 OBJ(apcupsd_loadbar) {
2250 if(output_methods & TO_X) {
2251 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2252 new_bar(obj, p, (int) progress);
2256 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2257 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2261 OBJ(apcupsd_loadgraph) {
2263 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2264 new_graph(obj, p, (int)progress);
2266 OBJ(apcupsd_loadgauge) {
2268 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2269 new_gauge(obj, p, (int)progress);
2272 OBJ(apcupsd_charge) {
2273 snprintf(p, p_max_size, "%s",
2274 cur->apcupsd.items[APCUPSD_CHARGE]);
2276 OBJ(apcupsd_timeleft) {
2277 snprintf(p, p_max_size, "%s",
2278 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2281 snprintf(p, p_max_size, "%s",
2282 cur->apcupsd.items[APCUPSD_TEMP]);
2284 OBJ(apcupsd_lastxfer) {
2285 snprintf(p, p_max_size, "%s",
2286 cur->apcupsd.items[APCUPSD_LASTXFER]);
2288 #endif /* APCUPSD */
2295 size_t a = strlen(p);
2298 iconv_convert(&a, buff_in, p, p_max_size);
2299 #endif /* HAVE_ICONV */
2300 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2301 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse) {
2302 substitute_newlines(p, a - 2);
2311 /* load any new fonts we may have had */
2312 if (need_to_load_fonts) {
2318 void evaluate(const char *text, char *buffer)
2320 struct information *tmp_info;
2321 struct text_object subroot;
2323 tmp_info = malloc(sizeof(struct information));
2324 memcpy(tmp_info, &info, sizeof(struct information));
2325 parse_conky_vars(&subroot, text, buffer, tmp_info);
2326 DBGP("evaluated '%s' to '%s'", text, buffer);
2328 free_text_objects(&subroot, 1);
2332 double current_update_time, next_update_time, last_update_time;
2334 static void generate_text(void)
2336 struct information *cur = &info;
2343 current_update_time = get_time();
2347 /* add things to the buffer */
2353 generate_text_internal(p, max_user_text, global_root_object, cur);
2355 if (stuff_in_uppercase) {
2358 tmp_p = text_buffer;
2360 *tmp_p = toupper(*tmp_p);
2365 next_update_time += update_interval;
2366 if (next_update_time < get_time()) {
2367 next_update_time = get_time() + update_interval;
2368 } else if (next_update_time > get_time() + update_interval) {
2369 next_update_time = get_time() + update_interval;
2371 last_update_time = current_update_time;
2375 void set_update_interval(double interval)
2377 update_interval = interval;
2378 update_interval_old = interval;
2381 static inline int get_string_width(const char *s)
2383 return *s ? calc_text_width(s) : 0;
2387 static int get_string_width_special(char *s, int special_index)
2397 if ((output_methods & TO_X) == 0)
2400 p = strndup(s, text_buffer_size);
2404 if (*p == SPECIAL_CHAR) {
2405 /* shift everything over by 1 so that the special char
2406 * doesn't mess up the size calculation */
2407 for (i = 0; i < (long)strlen(p); i++) {
2408 *(p + i) = *(p + i + 1);
2410 if (specials[special_index + idx].type == GRAPH
2411 || specials[special_index + idx].type == GAUGE
2412 || specials[special_index + idx].type == BAR) {
2413 width += specials[special_index + idx].width;
2416 } else if (*p == SECRIT_MULTILINE_CHAR) {
2423 if (strlen(final) > 1) {
2424 width += calc_text_width(final);
2430 static int text_size_updater(char *s, int special_index);
2432 int last_font_height;
2433 static void update_text_area(void)
2437 if ((output_methods & TO_X) == 0)
2439 /* update text size if it isn't fixed */
2444 text_width = minimum_width;
2446 last_font_height = font_height();
2447 for_each_line(text_buffer, text_size_updater);
2449 if (text_height < minimum_height) {
2450 text_height = minimum_height;
2452 if (text_width > maximum_width && maximum_width > 0) {
2453 text_width = maximum_width;
2457 /* get text position on workarea */
2458 switch (text_alignment) {
2465 x = workarea[2] - text_width - gap_x;
2470 x = workarea[2] / 2 - text_width / 2 - gap_x;
2477 y = workarea[3] - text_height - gap_y;
2481 x = workarea[2] - text_width - gap_x;
2482 y = workarea[3] - text_height - gap_y;
2486 x = workarea[2] / 2 - text_width / 2 - gap_x;
2487 y = workarea[3] - text_height - gap_y;
2492 y = workarea[3] / 2 - text_height / 2 - gap_y;
2496 x = workarea[2] - text_width - gap_x;
2497 y = workarea[3] / 2 - text_height / 2 - gap_y;
2501 case NONE: // Let the WM manage the window
2512 if (own_window && !fixed_pos) {
2515 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2516 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2517 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2518 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2522 /* If window size doesn't match to workarea's size,
2523 * then window probably includes panels (gnome).
2524 * Blah, doesn't work on KDE. */
2525 if (workarea[2] != window.width || workarea[3] != window.height) {
2534 /* update lua window globals */
2535 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2536 #endif /* HAVE_LUA */
2541 static int cur_x, cur_y; /* current x and y for drawing */
2543 //draw_mode also without X11 because we only need to print to stdout with FG
2544 static int draw_mode; /* FG, BG or OUTLINE */
2546 static long current_color;
2548 static int text_size_updater(char *s, int special_index)
2553 if ((output_methods & TO_X) == 0)
2555 /* get string widths and skip specials */
2558 if (*p == SPECIAL_CHAR) {
2560 w += get_string_width(s);
2563 if (specials[special_index].type == BAR
2564 || specials[special_index].type == GAUGE
2565 || specials[special_index].type == GRAPH) {
2566 w += specials[special_index].width;
2567 if (specials[special_index].height > last_font_height) {
2568 last_font_height = specials[special_index].height;
2569 last_font_height += font_height();
2571 } else if (specials[special_index].type == OFFSET) {
2572 if (specials[special_index].arg > 0) {
2573 w += specials[special_index].arg;
2575 } else if (specials[special_index].type == VOFFSET) {
2576 last_font_height += specials[special_index].arg;
2577 } else if (specials[special_index].type == GOTO) {
2578 if (specials[special_index].arg > cur_x) {
2579 w = (int) specials[special_index].arg;
2581 } else if (specials[special_index].type == TAB) {
2582 int start = specials[special_index].arg;
2583 int step = specials[special_index].width;
2585 if (!step || step < 0) {
2588 w += step - (cur_x - text_start_x - start) % step;
2589 } else if (specials[special_index].type == FONT) {
2590 selected_font = specials[special_index].font_added;
2591 if (font_height() > last_font_height) {
2592 last_font_height = font_height();
2598 } else if (*p == SECRIT_MULTILINE_CHAR) {
2601 lw = get_string_width(s);
2602 *p = SECRIT_MULTILINE_CHAR;
2604 w = lw > w ? lw : w;
2605 text_height += last_font_height;
2609 w += get_string_width(s);
2610 if (w > text_width) {
2613 if (text_width > maximum_width && maximum_width) {
2614 text_width = maximum_width;
2617 text_height += last_font_height;
2618 last_font_height = font_height();
2619 return special_index;
2623 static inline void set_foreground_color(long c)
2626 if (output_methods & TO_X) {
2628 XSetForeground(display, window.gc, c);
2632 if (output_methods & TO_NCURSES) {
2633 attron(COLOR_PAIR(c));
2635 #endif /* NCURSES */
2640 static void draw_string(const char *s)
2642 int i, i2, pos, width_of_s;
2645 char *s_with_newlines;
2651 width_of_s = get_string_width(s);
2652 s_with_newlines = strdup(s);
2653 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2654 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2655 s_with_newlines[i] = '\n';
2658 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2659 printf("%s\n", s_with_newlines);
2660 if (extra_newline) fputc('\n', stdout);
2661 fflush(stdout); /* output immediately, don't buffer */
2663 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2664 fprintf(stderr, "%s\n", s_with_newlines);
2665 fflush(stderr); /* output immediately, don't buffer */
2667 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2668 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2670 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2671 fprintf(append_fpointer, "%s\n", s_with_newlines);
2674 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2675 printw("%s", s_with_newlines);
2678 free(s_with_newlines);
2679 memset(tmpstring1, 0, text_buffer_size);
2680 memset(tmpstring2, 0, text_buffer_size);
2681 strncpy(tmpstring1, s, text_buffer_size - 1);
2686 if (output_methods & TO_X) {
2687 max = ((text_width - width_of_s) / get_string_width(" "));
2690 /* This code looks for tabs in the text and coverts them to spaces.
2691 * The trick is getting the correct number of spaces, and not going
2692 * over the window's size without forcing the window larger. */
2693 for (i = 0; i < (int) text_buffer_size; i++) {
2694 if (tmpstring1[i] == '\t') {
2696 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2697 /* guard against overrun */
2698 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2703 /* guard against overrun */
2704 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2709 if (output_methods & TO_X) {
2710 if (text_width == maximum_width) {
2711 /* this means the text is probably pushing the limit,
2712 * so we'll chop it */
2713 while (cur_x + get_string_width(tmpstring2) - text_start_x
2714 > maximum_width && strlen(tmpstring2) > 0) {
2715 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2722 if (output_methods & TO_X) {
2728 c.pixel = current_color;
2729 XQueryColor(display, DefaultColormap(display, screen), &c);
2732 c2.color.red = c.red;
2733 c2.color.green = c.green;
2734 c2.color.blue = c.blue;
2735 c2.color.alpha = fonts[selected_font].font_alpha;
2737 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2738 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2740 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2741 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2746 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2749 cur_x += width_of_s;
2752 memcpy(tmpstring1, s, text_buffer_size);
2755 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2763 int last_special_needed = -1;
2764 int orig_special_index = special_index;
2767 if (output_methods & TO_X) {
2768 font_h = font_height();
2769 cur_y += font_ascent();
2771 cur_x = text_start_x;
2775 if (*p == SECRIT_MULTILINE_CHAR) {
2776 /* special newline marker for multiline objects */
2781 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2786 /* draw string before special, unless we're dealing multiline
2788 if (last_special_applied > -1) {
2789 special_index = last_special_applied;
2797 switch (specials[special_index].type) {
2799 case HORIZONTAL_LINE:
2801 int h = specials[special_index].height;
2802 int mid = font_ascent() / 2;
2804 w = text_start_x + text_width - cur_x;
2806 XSetLineAttributes(display, window.gc, h, LineSolid,
2807 CapButt, JoinMiter);
2808 XDrawLine(display, window.drawable, window.gc, cur_x,
2809 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2815 int h = specials[special_index].height;
2816 int tmp_s = specials[special_index].arg;
2817 int mid = font_ascent() / 2;
2818 char ss[2] = { tmp_s, tmp_s };
2820 w = text_start_x + text_width - cur_x - 1;
2821 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2822 CapButt, JoinMiter);
2823 XSetDashes(display, window.gc, 0, ss, 2);
2824 XDrawLine(display, window.drawable, window.gc, cur_x,
2825 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2831 int h, bar_usage, by;
2832 if (cur_x - text_start_x > maximum_width
2833 && maximum_width > 0) {
2836 h = specials[special_index].height;
2837 bar_usage = specials[special_index].arg;
2838 by = cur_y - (font_ascent() / 2) - 1;
2843 w = specials[special_index].width;
2845 w = text_start_x + text_width - cur_x - 1;
2851 XSetLineAttributes(display, window.gc, 1, LineSolid,
2852 CapButt, JoinMiter);
2854 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2856 XFillRectangle(display, window.drawable, window.gc, cur_x,
2857 by, w * bar_usage / 255, h);
2865 case GAUGE: /* new GAUGE */
2868 unsigned long last_colour = current_color;
2870 float angle, px, py;
2874 if (cur_x - text_start_x > maximum_width
2875 && maximum_width > 0) {
2879 h = specials[special_index].height;
2880 by = cur_y - (font_ascent() / 2) - 1;
2885 w = specials[special_index].width;
2887 w = text_start_x + text_width - cur_x - 1;
2893 XSetLineAttributes(display, window.gc, 1, LineSolid,
2894 CapButt, JoinMiter);
2896 XDrawArc(display, window.drawable, window.gc,
2897 cur_x, by, w, h * 2, 0, 180*64);
2900 usage = specials[special_index].arg;
2901 angle = (M_PI)*(float)(usage)/255.;
2902 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
2903 py = (float)(by+(h))-(float)(h)*sin(angle);
2905 XDrawLine(display, window.drawable, window.gc,
2906 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
2914 set_foreground_color(last_colour);
2922 int h, by, i = 0, j = 0;
2924 unsigned long last_colour = current_color;
2925 unsigned long *tmpcolour = 0;
2926 if (cur_x - text_start_x > maximum_width
2927 && maximum_width > 0) {
2930 h = specials[special_index].height;
2931 by = cur_y - (font_ascent() / 2) - 1;
2936 w = specials[special_index].width;
2938 w = text_start_x + text_width - cur_x - 1;
2943 if (draw_graph_borders) {
2944 XSetLineAttributes(display, window.gc, 1, LineSolid,
2945 CapButt, JoinMiter);
2946 XDrawRectangle(display, window.drawable, window.gc,
2949 XSetLineAttributes(display, window.gc, 1, LineSolid,
2950 CapButt, JoinMiter);
2952 if (specials[special_index].last_colour != 0
2953 || specials[special_index].first_colour != 0) {
2954 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
2957 for (i = w - 2; i > -1; i--) {
2958 if (specials[special_index].last_colour != 0
2959 || specials[special_index].first_colour != 0) {
2960 if (specials[special_index].tempgrad) {
2963 (int)((float)(w - 2) - specials[special_index].graph[j] *
2964 (w - 2) / (float)specials[special_index].graph_scale)
2968 (int)((float)(w - 2) - specials[special_index].graph[j] *
2969 (w - 2) / (float)specials[special_index].graph_scale)
2972 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
2974 (int)((float)(w - 2) - specials[special_index].graph[j] *
2975 (w - 2) / (float)specials[special_index].graph_scale)
2979 #endif /* DEBUG_lol */
2980 XSetForeground(display, window.gc, tmpcolour[
2981 (int)((float)(w - 2) - specials[special_index].graph[j] *
2982 (w - 2) / (float)specials[special_index].graph_scale)
2985 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
2988 /* this is mugfugly, but it works */
2989 XDrawLine(display, window.drawable, window.gc,
2990 cur_x + i + 1, by + h, cur_x + i + 1,
2991 round_to_int((double)by + h - specials[special_index].graph[j] *
2992 (h - 1) / specials[special_index].graph_scale));
2993 if ((w - i) / ((float) (w - 2) /
2994 (specials[special_index].graph_width)) > j
2995 && j < MAX_GRAPH_DEPTH - 3) {
2999 if (tmpcolour) free(tmpcolour);
3004 /* if (draw_mode == BG) {
3005 set_foreground_color(default_bg_color);
3006 } else if (draw_mode == OUTLINE) {
3007 set_foreground_color(default_out_color);
3009 set_foreground_color(default_fg_color);
3011 if (show_graph_range) {
3014 unsigned short int seconds = update_interval * w;
3020 unsigned short int timeunits;
3022 timeunits = seconds / 86400; seconds %= 86400;
3023 if (timeunits > 0) {
3024 asprintf(&tmp_day_str, "%dd", timeunits);
3026 tmp_day_str = strdup("");
3028 timeunits = seconds / 3600; seconds %= 3600;
3029 if (timeunits > 0) {
3030 asprintf(&tmp_hour_str, "%dh", timeunits);
3032 tmp_hour_str = strdup("");
3034 timeunits = seconds / 60; seconds %= 60;
3035 if (timeunits > 0) {
3036 asprintf(&tmp_min_str, "%dm", timeunits);
3038 tmp_min_str = strdup("");
3041 asprintf(&tmp_sec_str, "%ds", seconds);
3043 tmp_sec_str = strdup("");
3045 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3046 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3048 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3050 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3051 cur_y += font_h / 2;
3052 draw_string(tmp_str);
3058 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3062 cur_x += font_ascent() / 2;
3063 cur_y += font_h / 2;
3065 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3067 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3068 draw_string(tmp_str);
3074 set_foreground_color(last_colour);
3080 int old = font_ascent();
3082 cur_y -= font_ascent();
3083 selected_font = specials[special_index].font_added;
3085 if (cur_y + font_ascent() < cur_y + old) {
3088 cur_y += font_ascent();
3090 font_h = font_height();
3095 if (draw_mode == FG) {
3096 set_foreground_color(specials[special_index].arg);
3102 if (draw_mode == BG) {
3103 set_foreground_color(specials[special_index].arg);
3108 if (draw_mode == OUTLINE) {
3109 set_foreground_color(specials[special_index].arg);
3114 w += specials[special_index].arg;
3115 last_special_needed = special_index;
3119 cur_y += specials[special_index].arg;
3123 if (specials[special_index].arg >= 0) {
3124 cur_x = (int) specials[special_index].arg;
3126 last_special_needed = special_index;
3131 int start = specials[special_index].arg;
3132 int step = specials[special_index].width;
3134 if (!step || step < 0) {
3137 w = step - (cur_x - text_start_x - start) % step;
3138 last_special_needed = special_index;
3144 /* TODO: add back in "+ window.border_inner_margin" to the end of
3146 int pos_x = text_start_x + text_width -
3147 get_string_width_special(s, special_index);
3149 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3150 "get_string_width(p) %i gap_x %i "
3151 "specials[special_index].arg %i window.border_inner_margin %i "
3152 "window.border_width %i\n", pos_x, text_start_x, text_width,
3153 cur_x, get_string_width_special(s), gap_x,
3154 specials[special_index].arg, window.border_inner_margin,
3155 window.border_width); */
3156 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3157 cur_x = pos_x - specials[special_index].arg;
3159 last_special_needed = special_index;
3165 int pos_x = (text_width) / 2 - get_string_width_special(s,
3166 special_index) / 2 - (cur_x -
3168 /* int pos_x = text_start_x + text_width / 2 -
3169 get_string_width_special(s) / 2; */
3171 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3172 "get_string_width(p) %i gap_x %i "
3173 "specials[special_index].arg %i\n", pos_x, text_start_x,
3174 text_width, cur_x, get_string_width(s), gap_x,
3175 specials[special_index].arg); */
3176 if (pos_x > specials[special_index].arg) {
3177 w = pos_x - specials[special_index].arg;
3179 last_special_needed = special_index;
3189 if (special_index != last_special_applied) {
3192 special_index = orig_special_index;
3193 last_special_applied = -1;
3204 if (output_methods & TO_NCURSES) {
3207 #endif /* NCURSES */
3209 if (output_methods & TO_X)
3210 cur_y += font_descent();
3212 if (recurse && *recurse) {
3213 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3214 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3216 return special_index;
3219 static int draw_line(char *s, int special_index)
3222 if (output_methods & TO_X) {
3223 return draw_each_line_inner(s, special_index, -1);
3227 if (output_methods & TO_NCURSES) {
3228 return draw_each_line_inner(s, special_index, -1);
3230 #endif /* NCURSES */
3232 UNUSED(special_index);
3236 static void draw_text(void)
3240 llua_draw_pre_hook();
3241 #endif /* HAVE_LUA */
3242 if (output_methods & TO_X) {
3243 cur_y = text_start_y;
3246 if (draw_borders && window.border_width > 0) {
3247 if (stippled_borders) {
3248 char ss[2] = { stippled_borders, stippled_borders };
3249 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3250 CapButt, JoinMiter);
3251 XSetDashes(display, window.gc, 0, ss, 2);
3253 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3254 CapButt, JoinMiter);
3257 XDrawRectangle(display, window.drawable, window.gc,
3258 text_start_x - window.border_inner_margin - window.border_width,
3259 text_start_y - window.border_inner_margin - window.border_width,
3260 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3261 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3269 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3270 attron(COLOR_PAIR(COLOR_WHITE));
3271 #endif /* NCURSES */
3272 for_each_line(text_buffer, draw_line);
3273 #if defined(HAVE_LUA) && defined(X11)
3274 llua_draw_post_hook();
3275 #endif /* HAVE_LUA */
3278 static void draw_stuff(void)
3281 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3283 if (overwrite_file) {
3284 overwrite_fpointer = fopen(overwrite_file, "w");
3285 if(!overwrite_fpointer)
3286 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3289 append_fpointer = fopen(append_file, "a");
3290 if(!append_fpointer)
3291 NORM_ERR("Can't append '%s' anymore", append_file);
3294 if (output_methods & TO_X) {
3296 if (draw_shades && !draw_outline) {
3299 set_foreground_color(default_bg_color);
3310 for (i = -1; i < 2; i++) {
3311 for (j = -1; j < 2; j++) {
3312 if (i == 0 && j == 0) {
3317 set_foreground_color(default_out_color);
3318 draw_mode = OUTLINE;
3326 set_foreground_color(default_fg_color);
3331 #if defined(X11) && defined(HAVE_XDBE)
3332 if (output_methods & TO_X) {
3333 xdbe_swap_buffers();
3335 #endif /* X11 && HAVE_XDBE */
3336 if(overwrite_fpointer) {
3337 fclose(overwrite_fpointer);
3338 overwrite_fpointer = 0;
3340 if(append_fpointer) {
3341 fclose(append_fpointer);
3342 append_fpointer = 0;
3347 static void clear_text(int exposures)
3351 /* The swap action is XdbeBackground, which clears */
3355 if (display && window.window) { // make sure these are !null
3356 /* there is some extra space for borders and outlines */
3357 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3358 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3359 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3360 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3365 static int need_to_update;
3367 /* update_text() generates new text and clears old text area */
3368 static void update_text(void)
3375 if (output_methods & TO_X)
3380 llua_update_info(&info, update_interval);
3381 #endif /* HAVE_LUA */
3384 #ifdef HAVE_SYS_INOTIFY_H
3388 static void main_loop(void)
3391 #ifdef SIGNAL_BLOCKING
3392 sigset_t newmask, oldmask;
3395 #ifdef HAVE_SYS_INOTIFY_H
3396 int inotify_config_wd = -1;
3397 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3398 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3399 char inotify_buff[INOTIFY_BUF_LEN];
3400 #endif /* HAVE_SYS_INOTIFY_H */
3403 #ifdef SIGNAL_BLOCKING
3404 sigemptyset(&newmask);
3405 sigaddset(&newmask, SIGINT);
3406 sigaddset(&newmask, SIGTERM);
3407 sigaddset(&newmask, SIGUSR1);
3410 last_update_time = 0.0;
3411 next_update_time = get_time();
3413 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3414 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3415 char buf[max_user_text];
3417 get_battery_short_status(buf, max_user_text, "BAT0");
3419 update_interval = update_interval_bat;
3421 update_interval = update_interval_old;
3426 #ifdef SIGNAL_BLOCKING
3427 /* block signals. we will inspect for pending signals later */
3428 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3429 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3434 if (output_methods & TO_X) {
3437 /* wait for X event or timeout */
3439 if (!XPending(display)) {
3443 t = next_update_time - get_time();
3447 } else if (t > update_interval) {
3448 t = update_interval;
3451 tv.tv_sec = (long) t;
3452 tv.tv_usec = (long) (t * 1000000) % 1000000;
3454 FD_SET(ConnectionNumber(display), &fdsr);
3456 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3458 if (errno != EINTR) {
3459 NORM_ERR("can't select(): %s", strerror(errno));
3469 if (need_to_update) {
3471 int wx = window.x, wy = window.y;
3481 /* resize window if it isn't right size */
3483 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3484 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3485 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3486 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3487 draw_stuff(); /* redraw everything in our newly sized window */
3488 XResizeWindow(display, window.window, window.width,
3489 window.height); /* resize window */
3490 set_transparent_background(window.window);
3493 xdbe_swap_buffers();
3498 /* update lua window globals */
3499 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3500 #endif /* HAVE_LUA */
3503 /* move window if it isn't in right position */
3504 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3505 XMoveWindow(display, window.window, window.x, window.y);
3510 if (changed && window.type == TYPE_PANEL) {
3513 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3516 switch (text_alignment) {
3543 set_struts(sidenum);
3554 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3555 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3556 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3557 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3558 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3563 /* handle X events */
3564 while (XPending(display)) {
3567 XNextEvent(display, &ev);
3574 r.width = ev.xexpose.width;
3575 r.height = ev.xexpose.height;
3576 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3580 case PropertyNotify:
3582 if ( ev.xproperty.state == PropertyNewValue ) {
3583 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3589 case ReparentNotify:
3590 /* set background to ParentRelative for all parents */
3592 set_transparent_background(window.window);
3596 case ConfigureNotify:
3598 /* if window size isn't what expected, set fixed size */
3599 if (ev.xconfigure.width != window.width
3600 || ev.xconfigure.height != window.height) {
3601 if (window.width != 0 && window.height != 0) {
3605 /* clear old stuff before screwing up
3610 XWindowAttributes attrs;
3611 if (XGetWindowAttributes(display,
3612 window.window, &attrs)) {
3613 window.width = attrs.width;
3614 window.height = attrs.height;
3618 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3619 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3620 if (text_width > maximum_width
3621 && maximum_width > 0) {
3622 text_width = maximum_width;
3626 /* if position isn't what expected, set fixed pos
3627 * total_updates avoids setting fixed_pos when window
3628 * is set to weird locations when started */
3629 /* // this is broken
3630 if (total_updates >= 2 && !fixed_pos
3631 && (window.x != ev.xconfigure.x
3632 || window.y != ev.xconfigure.y)
3633 && (ev.xconfigure.x != 0
3634 || ev.xconfigure.y != 0)) {
3642 /* if an ordinary window with decorations */
3643 if ((window.type == TYPE_NORMAL &&
3644 (!TEST_HINT(window.hints,
3645 HINT_UNDECORATED))) ||
3646 window.type == TYPE_DESKTOP) {
3647 /* allow conky to hold input focus. */
3650 /* forward the click to the desktop window */
3651 XUngrabPointer(display, ev.xbutton.time);
3652 ev.xbutton.window = window.desktop;
3653 ev.xbutton.x = ev.xbutton.x_root;
3654 ev.xbutton.y = ev.xbutton.y_root;
3655 XSendEvent(display, ev.xbutton.window, False,
3656 ButtonPressMask, &ev);
3657 XSetInputFocus(display, ev.xbutton.window,
3658 RevertToParent, ev.xbutton.time);
3665 /* if an ordinary window with decorations */
3666 if ((window.type == TYPE_NORMAL)
3667 && (!TEST_HINT(window.hints,
3668 HINT_UNDECORATED))) {
3669 /* allow conky to hold input focus. */
3672 /* forward the release to the desktop window */
3673 ev.xbutton.window = window.desktop;
3674 ev.xbutton.x = ev.xbutton.x_root;
3675 ev.xbutton.y = ev.xbutton.y_root;
3676 XSendEvent(display, ev.xbutton.window, False,
3677 ButtonReleaseMask, &ev);
3686 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3687 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3689 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3690 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3692 #endif /* HAVE_XDAMAGE */
3698 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3699 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3700 #endif /* HAVE_XDAMAGE */
3702 /* XDBE doesn't seem to provide a way to clear the back buffer
3703 * without interfering with the front buffer, other than passing
3704 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3705 * using XDBE, we need to redraw the text even if it wasn't part of
3706 * the exposed area. OTOH, if we're not going to call draw_stuff at
3707 * all, then no swap happens and we can safely do nothing. */
3709 if (!XEmptyRegion(x11_stuff.region)) {
3714 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3715 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3716 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3717 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3718 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3721 XSetRegion(display, window.gc, x11_stuff.region);
3724 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3728 XDestroyRegion(x11_stuff.region);
3729 x11_stuff.region = XCreateRegion();
3733 t = (next_update_time - get_time()) * 1000000;
3734 if(t > 0) usleep((useconds_t)t);
3738 if(output_methods & TO_NCURSES) {
3747 #ifdef SIGNAL_BLOCKING
3748 /* unblock signals of interest and let handler fly */
3749 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3750 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3754 switch (g_signal_pending) {
3757 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3762 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3765 if (output_methods & TO_X) {
3766 XDestroyRegion(x11_stuff.region);
3767 x11_stuff.region = NULL;
3769 XDamageDestroy(display, x11_stuff.damage);
3770 XFixesDestroyRegion(display, x11_stuff.region2);
3771 XFixesDestroyRegion(display, x11_stuff.part);
3772 #endif /* HAVE_XDAMAGE */
3778 if(overwrite_file) {
3779 free(overwrite_file);
3788 /* Reaching here means someone set a signal
3789 * (SIGXXXX, signal_handler), but didn't write any code
3791 * If you don't want to handle a signal, don't set a handler on
3792 * it in the first place. */
3793 if (g_signal_pending) {
3794 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3798 #ifdef HAVE_SYS_INOTIFY_H
3799 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3800 inotify_config_wd = inotify_add_watch(inotify_fd,
3804 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3805 int len = 0, idx = 0;
3807 struct timeval time_to_wait;
3809 FD_ZERO(&descriptors);
3810 FD_SET(inotify_fd, &descriptors);
3812 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3814 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3815 if (FD_ISSET(inotify_fd, &descriptors)) {
3816 /* process inotify events */
3817 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3818 while (len > 0 && idx < len) {
3819 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3820 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3821 /* current_config should be reloaded */
3822 NORM_ERR("'%s' modified, reloading...", current_config);
3824 if (ev->mask & IN_IGNORED) {
3825 /* for some reason we get IN_IGNORED here
3826 * sometimes, so we need to re-add the watch */
3827 inotify_config_wd = inotify_add_watch(inotify_fd,
3834 llua_inotify_query(ev->wd, ev->mask);
3836 #endif /* HAVE_LUA */
3837 idx += INOTIFY_EVENT_SIZE + ev->len;
3841 #endif /* HAVE_SYS_INOTIFY_H */
3844 llua_update_info(&info, update_interval);
3845 #endif /* HAVE_LUA */
3846 g_signal_pending = 0;
3848 clean_up(NULL, NULL);
3850 #ifdef HAVE_SYS_INOTIFY_H
3851 if (inotify_fd != -1) {
3852 inotify_rm_watch(inotify_fd, inotify_config_wd);
3854 inotify_fd = inotify_config_wd = 0;
3856 #endif /* HAVE_SYS_INOTIFY_H */
3860 static void load_config_file_x11(const char *);
3862 void initialisation(int argc, char** argv);
3864 /* reload the config file */
3865 static void reload_config(void)
3867 char *current_config_copy = strdup(current_config);
3868 clean_up(NULL, NULL);
3869 current_config = current_config_copy;
3870 initialisation(argc_copy, argv_copy);
3873 void clean_up(void *memtofree1, void* memtofree2)
3878 if(output_methods & TO_NCURSES) {
3882 conftree_empty(currentconffile);
3883 currentconffile = NULL;
3890 timed_thread_destroy_registered_threads();
3892 if (info.cpu_usage) {
3893 free(info.cpu_usage);
3894 info.cpu_usage = NULL;
3897 if (x_initialised == YES) {
3898 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3899 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3900 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3901 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
3904 if(x11_stuff.region) {
3905 XDestroyRegion(x11_stuff.region);
3906 x11_stuff.region = NULL;
3908 XCloseDisplay(display);
3910 if(info.x11.desktop.all_names) {
3911 free(info.x11.desktop.all_names);
3912 info.x11.desktop.all_names = NULL;
3914 if (info.x11.desktop.name) {
3915 free(info.x11.desktop.name);
3916 info.x11.desktop.name = NULL;
3920 free(fonts); //in set_default_configurations a font is set but not loaded
3926 free_update_callbacks();
3930 free_text_objects(&global_root_object, 0);
3949 free(current_config);
3952 #ifdef TCP_PORT_MONITOR
3953 tcp_portmon_clear();
3962 weather_free_info();
3965 llua_shutdown_hook();
3967 #endif /* HAVE_LUA */
3969 if (output_methods & TO_X)
3977 for (i = 0; i < special_count; i++) {
3978 if (specials[i].type == GRAPH) {
3979 free(specials[i].graph);
3987 clear_diskio_stats();
3988 if(global_cpu != NULL) {
3994 static int string_to_bool(const char *s)
3997 // Assumes an option without a true/false means true
3999 } else if (strcasecmp(s, "yes") == EQUAL) {
4001 } else if (strcasecmp(s, "true") == EQUAL) {
4003 } else if (strcasecmp(s, "1") == EQUAL) {
4010 static enum alignment string_to_alignment(const char *s)
4012 if (strcasecmp(s, "top_left") == EQUAL) {
4014 } else if (strcasecmp(s, "top_right") == EQUAL) {
4016 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4018 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4020 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4021 return BOTTOM_RIGHT;
4022 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4023 return BOTTOM_MIDDLE;
4024 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4026 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4027 return MIDDLE_RIGHT;
4028 } else if (strcasecmp(s, "tl") == EQUAL) {
4030 } else if (strcasecmp(s, "tr") == EQUAL) {
4032 } else if (strcasecmp(s, "tm") == EQUAL) {
4034 } else if (strcasecmp(s, "bl") == EQUAL) {
4036 } else if (strcasecmp(s, "br") == EQUAL) {
4037 return BOTTOM_RIGHT;
4038 } else if (strcasecmp(s, "bm") == EQUAL) {
4039 return BOTTOM_MIDDLE;
4040 } else if (strcasecmp(s, "ml") == EQUAL) {
4042 } else if (strcasecmp(s, "mr") == EQUAL) {
4043 return MIDDLE_RIGHT;
4044 } else if (strcasecmp(s, "none") == EQUAL) {
4052 static void set_default_configurations_for_x(void)
4054 default_fg_color = WhitePixel(display, screen);
4055 default_bg_color = BlackPixel(display, screen);
4056 default_out_color = BlackPixel(display, screen);
4057 color0 = default_fg_color;
4058 color1 = default_fg_color;
4059 color2 = default_fg_color;
4060 color3 = default_fg_color;
4061 color4 = default_fg_color;
4062 color5 = default_fg_color;
4063 color6 = default_fg_color;
4064 color7 = default_fg_color;
4065 color8 = default_fg_color;
4066 color9 = default_fg_color;
4067 current_text_color = default_fg_color;
4071 static void set_default_configurations(void)
4078 fork_to_background = 0;
4079 total_run_times = 0;
4080 info.cpu_avg_samples = 2;
4081 info.net_avg_samples = 2;
4082 info.diskio_avg_samples = 2;
4087 format_human_readable = 1;
4097 mpd_env_host = getenv("MPD_HOST");
4098 mpd_env_port = getenv("MPD_PORT");
4100 if (!mpd_env_host || !strlen(mpd_env_host)) {
4101 mpd_set_host("localhost");
4103 /* MPD_HOST environment variable is set */
4104 char *mpd_hostpart = strchr(mpd_env_host, '@');
4105 if (!mpd_hostpart) {
4106 mpd_set_host(mpd_env_host);
4108 /* MPD_HOST contains a password */
4109 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4110 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4112 if (!strlen(mpd_hostpart + 1)) {
4113 mpd_set_host("localhost");
4115 mpd_set_host(mpd_hostpart + 1);
4118 mpd_set_password(mpd_password, 1);
4123 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4124 /* failed to set port from environment variable */
4125 mpd_set_port("6600");
4129 info.xmms2.artist = NULL;
4130 info.xmms2.album = NULL;
4131 info.xmms2.title = NULL;
4132 info.xmms2.genre = NULL;
4133 info.xmms2.comment = NULL;
4134 info.xmms2.url = NULL;
4135 info.xmms2.status = NULL;
4136 info.xmms2.playlist = NULL;
4138 use_spacer = NO_SPACER;
4140 output_methods = TO_X;
4142 output_methods = TO_STDOUT;
4145 show_graph_scale = 0;
4146 show_graph_range = 0;
4149 draw_graph_borders = 1;
4151 set_first_font("6x10");
4159 window.type = TYPE_NORMAL;
4161 strcpy(window.class_name, PACKAGE_NAME);
4162 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4164 stippled_borders = 0;
4165 window.border_inner_margin = 3;
4166 window.border_outer_margin = 1;
4167 window.border_width = 1;
4168 text_alignment = BOTTOM_LEFT;
4169 info.x11.monitor.number = 1;
4170 info.x11.monitor.current = 0;
4171 info.x11.desktop.current = 1;
4172 info.x11.desktop.number = 1;
4173 info.x11.desktop.nitems = 0;
4174 info.x11.desktop.all_names = NULL;
4175 info.x11.desktop.name = NULL;
4180 free(current_mail_spool);
4184 variable_substitute(MAIL_FILE, buf, 256);
4185 if (buf[0] != '\0') {
4186 current_mail_spool = strndup(buf, text_buffer_size);
4191 set_update_interval(3);
4192 update_interval_bat = NOBATTERY;
4193 info.music_player_interval = 1.0;
4194 stuff_in_uppercase = 0;
4195 info.users.number = 1;
4197 #ifdef TCP_PORT_MONITOR
4198 /* set default connection limit */
4199 tcp_portmon_set_max_connections(0);
4203 /* returns 1 if you can overwrite or create the file at 'path' */
4204 static _Bool overwrite_works(const char *path)
4208 if (!(filepointer = fopen(path, "w")))
4210 fclose(filepointer);
4214 /* returns 1 if you can append or create the file at 'path' */
4215 static _Bool append_works(const char *path)
4219 if (!(filepointer = fopen(path, "a")))
4221 fclose(filepointer);
4227 /* WARNING, this type not in Xlib spec */
4228 int x11_error_handler(Display *d, XErrorEvent *err)
4229 __attribute__((noreturn));
4230 int x11_error_handler(Display *d, XErrorEvent *err)
4232 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",
4234 (long unsigned)err->display,
4235 (long)err->resourceid,
4245 int x11_ioerror_handler(Display *d)
4246 __attribute__((noreturn));
4247 int x11_ioerror_handler(Display *d)
4249 NORM_ERR("X Error: Display %lx\n",
4256 static void X11_initialisation(void)
4258 if (x_initialised == YES) return;
4259 output_methods |= TO_X;
4261 set_default_configurations_for_x();
4262 x_initialised = YES;
4265 /* WARNING, this type not in Xlib spec */
4266 XSetErrorHandler(&x11_error_handler);
4267 XSetIOErrorHandler(&x11_ioerror_handler);
4271 static char **xargv = 0;
4272 static int xargc = 0;
4274 static void X11_create_window(void)
4276 if (output_methods & TO_X) {
4278 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4279 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4281 #else /* OWN_WINDOW */
4282 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4283 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4285 #endif /* OWN_WINDOW */
4289 update_text_area(); /* to position text/window on screen */
4292 if (own_window && !fixed_pos) {
4293 XMoveWindow(display, window.window, window.x, window.y);
4296 set_transparent_background(window.window);
4304 x11_stuff.region = XCreateRegion();
4306 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4307 NORM_ERR("Xdamage extension unavailable");
4309 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4310 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4311 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4312 #endif /* HAVE_XDAMAGE */
4315 update_text_area(); /* to get initial size of the window */
4318 /* setup lua window globals */
4319 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4320 #endif /* HAVE_LUA */
4324 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4325 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4326 #define CONF2(a) if (strcasecmp(name, a) == 0)
4327 #define CONF(a) else CONF2(a)
4328 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4329 || strcasecmp(name, b) == 0)
4330 #define CONF_CONTINUE 1
4331 #define CONF_BREAK 2
4332 #define CONF_BUFF_SIZE 512
4334 static FILE *open_config_file(const char *f)
4336 #ifdef CONFIG_OUTPUT
4337 if (!strcmp(f, "==builtin==")) {
4338 return conf_cookie_open();
4340 #endif /* CONFIG_OUTPUT */
4341 return fopen(f, "r");
4344 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4348 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4351 remove_comments(buf);
4356 while (*p && isspace((int) *p)) {
4360 return CONF_CONTINUE; /* empty line */
4367 while (*p2 && !isspace((int) *p2)) {
4371 *p2 = '\0'; /* break at name's end */
4378 while (*p && isspace((int) *p)) {
4384 p2 = *value + strlen(*value);
4385 while (isspace((int) *(p2 - 1))) {
4394 char load_config_file(const char *f)
4399 fp = open_config_file(f);
4403 DBGP("reading contents from config file '%s'", f);
4406 char buff[CONF_BUFF_SIZE], *name, *value;
4407 int ret = do_config_step(&line, fp, buff, &name, &value);
4408 if (ret == CONF_BREAK) {
4410 } else if (ret == CONF_CONTINUE) {
4416 /* don't listen if X is already initialised or
4417 * if we already know we don't want it */
4418 if(x_initialised != YES) {
4419 if (string_to_bool(value)) {
4420 output_methods &= TO_X;
4422 output_methods &= ~TO_X;
4423 x_initialised = NEVER;
4428 if (!value || x_initialised == YES) {
4433 disp = strdup(value);
4438 if (window.type == TYPE_DOCK)
4441 #endif /*OWN_WINDOW */
4443 int a = string_to_alignment(value);
4454 CONF("background") {
4455 fork_to_background = string_to_bool(value);
4458 CONF2("background") {
4459 fork_to_background = string_to_bool(value);
4463 CONF("show_graph_scale") {
4464 show_graph_scale = string_to_bool(value);
4466 CONF("show_graph_range") {
4467 show_graph_range = string_to_bool(value);
4469 CONF("border_inner_margin") {
4471 window.border_inner_margin = strtol(value, 0, 0);
4472 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4477 CONF("border_outer_margin") {
4479 window.border_outer_margin = strtol(value, 0, 0);
4480 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4485 CONF("border_width") {
4487 window.border_width = strtol(value, 0, 0);
4488 if (window.border_width < 0) window.border_width = 0;
4494 #define TEMPLATE_CONF(n) \
4495 CONF("template"#n) { \
4496 if (set_template(n, value)) \
4511 parse_global_imap_mail_args(value);
4518 parse_global_pop3_mail_args(value);
4523 CONF("default_bar_size") {
4526 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4533 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4537 CONF("default_graph_size") {
4540 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4547 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4550 CONF("default_gauge_size") {
4553 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4560 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4567 mpd_set_host(value);
4573 if (value && mpd_set_port(value)) {
4577 CONF("mpd_password") {
4579 mpd_set_password(value, 0);
4585 CONF("music_player_interval") {
4587 info.music_player_interval = strtod(value, 0);
4593 CONF("sensor_device") {
4595 sensor_device = strtol(value, 0, 0);
4601 CONF("cpu_avg_samples") {
4603 cpu_avg_samples = strtol(value, 0, 0);
4604 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4607 info.cpu_avg_samples = cpu_avg_samples;
4613 CONF("net_avg_samples") {
4615 net_avg_samples = strtol(value, 0, 0);
4616 if (net_avg_samples < 1 || net_avg_samples > 14) {
4619 info.net_avg_samples = net_avg_samples;
4625 CONF("diskio_avg_samples") {
4627 diskio_avg_samples = strtol(value, 0, 0);
4628 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4631 info.diskio_avg_samples = diskio_avg_samples;
4639 CONF("double_buffer") {
4640 use_xdbe = string_to_bool(value);
4644 CONF("override_utf8_locale") {
4645 utf8_mode = string_to_bool(value);
4647 CONF("draw_borders") {
4648 draw_borders = string_to_bool(value);
4650 CONF("draw_graph_borders") {
4651 draw_graph_borders = string_to_bool(value);
4653 CONF("draw_shades") {
4654 draw_shades = string_to_bool(value);
4656 CONF("draw_outline") {
4657 draw_outline = string_to_bool(value);
4660 CONF("out_to_console") {
4661 if(string_to_bool(value)) {
4662 output_methods |= TO_STDOUT;
4664 output_methods &= ~TO_STDOUT;
4667 CONF("extra_newline") {
4668 extra_newline = string_to_bool(value);
4670 CONF("out_to_stderr") {
4671 if(string_to_bool(value))
4672 output_methods |= TO_STDERR;
4675 CONF("out_to_ncurses") {
4676 if(string_to_bool(value)) {
4679 output_methods |= TO_NCURSES;
4683 CONF("overwrite_file") {
4684 if(overwrite_file) {
4685 free(overwrite_file);
4688 if(overwrite_works(value)) {
4689 overwrite_file = strdup(value);
4690 output_methods |= OVERWRITE_FILE;
4692 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4694 CONF("append_file") {
4699 if(append_works(value)) {
4700 append_file = strdup(value);
4701 output_methods |= APPEND_FILE;
4703 NORM_ERR("append_file won't be able to create/append '%s'", value);
4705 CONF("use_spacer") {
4707 if (strcasecmp(value, "left") == EQUAL) {
4708 use_spacer = LEFT_SPACER;
4709 } else if (strcasecmp(value, "right") == EQUAL) {
4710 use_spacer = RIGHT_SPACER;
4711 } else if (strcasecmp(value, "none") == EQUAL) {
4712 use_spacer = NO_SPACER;
4714 use_spacer = string_to_bool(value);
4715 NORM_ERR("use_spacer should have an argument of left, right, or"
4716 " none. '%s' seems to be some form of '%s', so"
4717 " defaulting to %s.", value,
4718 use_spacer ? "true" : "false",
4719 use_spacer ? "right" : "none");
4721 use_spacer = RIGHT_SPACER;
4723 use_spacer = NO_SPACER;
4727 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4728 use_spacer = RIGHT_SPACER;
4734 use_xft = string_to_bool(value);
4738 set_first_font(value);
4742 if (value && font_count >= 0) {
4743 fonts[0].font_alpha = atof(value) * 65535.0;
4750 if (string_to_bool(value)) {
4751 NORM_ERR("Xft not enabled at compile time");
4755 /* xftfont silently ignored when no Xft */
4758 /* xftalpha is silently ignored when no Xft */
4763 set_first_font(value);
4771 gap_x = atoi(value);
4778 gap_y = atoi(value);
4784 CONF("mail_spool") {
4788 variable_substitute(value, buffer, 256);
4790 if (buffer[0] != '\0') {
4791 if (current_mail_spool) {
4792 free(current_mail_spool);
4794 current_mail_spool = strndup(buffer, text_buffer_size);
4801 CONF("minimum_size") {
4803 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4805 if (sscanf(value, "%d", &minimum_width) != 1) {
4813 CONF("maximum_width") {
4815 if (sscanf(value, "%d", &maximum_width) != 1) {
4823 CONF("no_buffers") {
4824 no_buffers = string_to_bool(value);
4826 CONF("top_name_width") {
4828 if (sscanf(value, "%u", &top_name_width) != 1) {
4834 if (top_name_width >= max_user_text) {
4835 top_name_width = max_user_text - 1;
4838 CONF("top_cpu_separate") {
4839 cpu_separate = string_to_bool(value);
4841 CONF("short_units") {
4842 short_units = string_to_bool(value);
4844 CONF("format_human_readable") {
4845 format_human_readable = string_to_bool(value);
4848 CONF("hddtemp_host") {
4849 set_hddtemp_host(value);
4851 CONF("hddtemp_port") {
4852 set_hddtemp_port(value);
4854 #endif /* HDDTEMP */
4855 CONF("pad_percents") {
4856 pad_percents = atoi(value);
4860 CONF("own_window") {
4862 own_window = string_to_bool(value);
4865 CONF("own_window_class") {
4867 memset(window.class_name, 0, sizeof(window.class_name));
4868 strncpy(window.class_name, value,
4869 sizeof(window.class_name) - 1);
4872 CONF("own_window_title") {
4874 memset(window.title, 0, sizeof(window.title));
4875 strncpy(window.title, value, sizeof(window.title) - 1);
4878 CONF("own_window_transparent") {
4880 set_transparent = string_to_bool(value);
4883 CONF("own_window_hints") {
4885 char *p_hint, *p_save;
4886 char delim[] = ", ";
4888 /* tokenize the value into individual hints */
4889 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
4891 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
4892 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
4893 SET_HINT(window.hints, HINT_UNDECORATED);
4894 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
4895 SET_HINT(window.hints, HINT_BELOW);
4896 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
4897 SET_HINT(window.hints, HINT_ABOVE);
4898 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
4899 SET_HINT(window.hints, HINT_STICKY);
4900 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
4901 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
4902 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
4903 SET_HINT(window.hints, HINT_SKIP_PAGER);
4908 p_hint = strtok_r(NULL, delim, &p_save);
4909 } while (p_hint != NULL);
4915 CONF("own_window_type") {
4917 if (strncmp(value, "normal", 6) == EQUAL) {
4918 window.type = TYPE_NORMAL;
4919 } else if (strncmp(value, "desktop", 7) == EQUAL) {
4920 window.type = TYPE_DESKTOP;
4921 } else if (strncmp(value, "dock", 4) == EQUAL) {
4922 window.type = TYPE_DOCK;
4923 text_alignment = TOP_LEFT;
4924 } else if (strncmp(value, "panel", 5) == EQUAL) {
4925 window.type = TYPE_PANEL;
4926 } else if (strncmp(value, "override", 8) == EQUAL) {
4927 window.type = TYPE_OVERRIDE;
4936 CONF("stippled_borders") {
4938 stippled_borders = strtol(value, 0, 0);
4940 stippled_borders = 4;
4944 CONF("imlib_cache_size") {
4946 cimlib_set_cache_size(atoi(value));
4949 CONF("imlib_cache_flush_interval") {
4951 cimlib_set_cache_flush_interval(atoi(value));
4956 CONF("update_interval_on_battery") {
4958 update_interval_bat = strtod(value, 0);
4963 CONF("update_interval") {
4965 set_update_interval(strtod(value, 0));
4969 if (info.music_player_interval == 0) {
4970 // default to update_interval
4971 info.music_player_interval = update_interval;
4974 CONF("total_run_times") {
4976 total_run_times = strtod(value, 0);
4982 stuff_in_uppercase = string_to_bool(value);
4984 CONF("max_specials") {
4986 max_specials = atoi(value);
4991 CONF("max_user_text") {
4993 max_user_text = atoi(value);
4998 CONF("text_buffer_size") {
5000 text_buffer_size = atoi(value);
5001 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5002 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5003 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5011 if (output_methods & TO_X) {
5012 X11_initialisation();
5021 global_text = (char *) malloc(1);
5022 global_text[0] = '\0';
5025 unsigned int l = strlen(global_text);
5027 char buf[CONF_BUFF_SIZE];
5029 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5035 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5043 /* Check for continuation of \\-\n. */
5044 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5045 global_text[l-1] = '\0';
5049 global_text = (char *) realloc(global_text, l + bl + 1);
5050 strcat(global_text, buf);
5052 if (strlen(global_text) > max_user_text) {
5056 global_text_lines = line + 1;
5059 #ifdef TCP_PORT_MONITOR
5060 CONF("max_port_monitor_connections") {
5062 if (!value || (sscanf(value, "%d", &max) != 1)) {
5063 /* an error. use default, warn and continue. */
5064 tcp_portmon_set_max_connections(0);
5066 } else if (tcp_portmon_set_max_connections(max)) {
5067 /* max is < 0, default has been set*/
5072 CONF("if_up_strictness") {
5074 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5075 ifup_strictness = IFUP_UP;
5076 } else if (strcasecmp(value, "up") == EQUAL) {
5077 ifup_strictness = IFUP_UP;
5078 } else if (strcasecmp(value, "link") == EQUAL) {
5079 ifup_strictness = IFUP_LINK;
5080 } else if (strcasecmp(value, "address") == EQUAL) {
5081 ifup_strictness = IFUP_ADDR;
5083 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5084 ifup_strictness = IFUP_UP;
5088 CONF("temperature_unit") {
5090 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5091 } else if (set_temp_output_unit(value)) {
5092 NORM_ERR("temperature_unit: incorrect argument");
5099 char *ptr = strtok(value, " ");
5102 ptr = strtok(NULL, " ");
5109 CONF("lua_draw_hook_pre") {
5111 llua_set_draw_pre_hook(value);
5116 CONF("lua_draw_hook_post") {
5118 llua_set_draw_post_hook(value);
5123 CONF("lua_startup_hook") {
5125 llua_set_startup_hook(value);
5130 CONF("lua_shutdown_hook") {
5132 llua_set_shutdown_hook(value);
5138 #endif /* HAVE_LUA */
5150 CONF("default_color"){}
5151 CONF3("default_shade_color", "default_shadecolor"){}
5152 CONF3("default_outline_color", "default_outlinecolor") {}
5153 CONF("own_window_colour") {}
5156 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5162 if (info.music_player_interval == 0) {
5163 // default to update_interval
5164 info.music_player_interval = update_interval;
5166 if (!global_text) { // didn't supply any text
5167 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5169 if (!output_methods) {
5170 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5172 #if defined(NCURSES)
5174 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5175 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5176 output_methods &= ~TO_NCURSES;
5180 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5181 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5182 output_methods &= ~(TO_STDOUT | TO_STDERR);
5184 #endif /* NCURSES */
5189 static void load_config_file_x11(const char *f)
5194 fp = open_config_file(f);
5198 DBGP("reading contents from config file '%s'", f);
5201 char buff[CONF_BUFF_SIZE], *name, *value;
5202 int ret = do_config_step(&line, fp, buff, &name, &value);
5203 if (ret == CONF_BREAK) {
5205 } else if (ret == CONF_CONTINUE) {
5210 X11_initialisation();
5211 if (x_initialised == YES) {
5213 color0 = get_x11_color(value);
5220 X11_initialisation();
5221 if (x_initialised == YES) {
5223 color1 = get_x11_color(value);
5230 X11_initialisation();
5231 if (x_initialised == YES) {
5233 color2 = get_x11_color(value);
5240 X11_initialisation();
5241 if (x_initialised == YES) {
5243 color3 = get_x11_color(value);
5250 X11_initialisation();
5251 if (x_initialised == YES) {
5253 color4 = get_x11_color(value);
5260 X11_initialisation();
5261 if (x_initialised == YES) {
5263 color5 = get_x11_color(value);
5270 X11_initialisation();
5271 if (x_initialised == YES) {
5273 color6 = get_x11_color(value);
5280 X11_initialisation();
5281 if (x_initialised == YES) {
5283 color7 = get_x11_color(value);
5290 X11_initialisation();
5291 if (x_initialised == YES) {
5293 color8 = get_x11_color(value);
5300 X11_initialisation();
5301 if (x_initialised == YES) {
5303 color9 = get_x11_color(value);
5309 CONF("default_color") {
5310 X11_initialisation();
5311 if (x_initialised == YES) {
5313 default_fg_color = get_x11_color(value);
5319 CONF3("default_shade_color", "default_shadecolor") {
5320 X11_initialisation();
5321 if (x_initialised == YES) {
5323 default_bg_color = get_x11_color(value);
5329 CONF3("default_outline_color", "default_outlinecolor") {
5330 X11_initialisation();
5331 if (x_initialised == YES) {
5333 default_out_color = get_x11_color(value);
5340 CONF("own_window_colour") {
5341 X11_initialisation();
5342 if (x_initialised == YES) {
5344 background_colour = get_x11_color(value);
5346 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5347 "'#' for hex colours");
5353 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5354 if(output_methods & TO_X) {
5355 X11_initialisation();
5364 #undef CONF_CONTINUE
5365 #undef CONF_BUFF_SIZE
5373 static void print_help(const char *prog_name) {
5374 printf("Usage: %s [OPTION]...\n"
5375 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5376 "window. Command line options will override configurations defined in config\n"
5378 " -v, --version version\n"
5379 " -q, --quiet quiet mode\n"
5380 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5381 " -c, --config=FILE config file to load\n"
5382 #ifdef CONFIG_OUTPUT
5383 " -C, --print-config print the builtin default config to stdout\n"
5384 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5386 " -d, --daemonize daemonize, fork to background\n"
5387 " -h, --help help\n"
5389 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5390 " -f, --font=FONT font to use\n"
5391 " -X, --display=DISPLAY X11 display to use\n"
5393 " -o, --own-window create own window to draw\n"
5396 " -b, --double-buffer double buffer (prevents flickering)\n"
5398 " -w, --window-id=WIN_ID window id to draw\n"
5399 " -x X x position\n"
5400 " -y Y y position\n"
5402 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5403 " -u, --interval=SECS update interval\n"
5404 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5405 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5410 /* : means that character before that takes an argument */
5411 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5421 #ifdef CONFIG_OUTPUT
5426 static const struct option longopts[] = {
5427 { "help", 0, NULL, 'h' },
5428 { "version", 0, NULL, 'V' },
5429 { "debug", 0, NULL, 'D' },
5430 { "config", 1, NULL, 'c' },
5431 #ifdef CONFIG_OUTPUT
5432 { "print-config", 0, NULL, 'C' },
5434 { "daemonize", 0, NULL, 'd' },
5436 { "alignment", 1, NULL, 'a' },
5437 { "font", 1, NULL, 'f' },
5438 { "display", 1, NULL, 'X' },
5440 { "own-window", 0, NULL, 'o' },
5443 { "double-buffer", 0, NULL, 'b' },
5445 { "window-id", 1, NULL, 'w' },
5447 { "text", 1, NULL, 't' },
5448 { "interval", 0, NULL, 'u' },
5449 { "pause", 0, NULL, 'p' },
5453 void initialisation(int argc, char **argv) {
5454 struct sigaction act, oact;
5456 set_default_configurations();
5457 load_config_file(current_config);
5458 currentconffile = conftree_add(currentconffile, current_config);
5460 /* init specials array */
5461 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5462 NORM_ERR("failed to create specials array");
5466 if (current_mail_spool == NULL) {
5469 variable_substitute(MAIL_FILE, buf, 256);
5471 if (buf[0] != '\0') {
5472 current_mail_spool = strndup(buf, text_buffer_size);
5477 /* handle other command line arguments */
5479 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5480 || defined(__NetBSD__)
5481 optind = optreset = 1;
5486 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5487 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5488 "kvm_open")) == NULL) {
5489 CRIT_ERR(NULL, NULL, "cannot read kvm");
5494 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5503 fork_to_background = 1;
5506 global_debug_level++;
5510 set_first_font(optarg);
5513 text_alignment = string_to_alignment(optarg);
5532 global_text = strndup(optarg, max_user_text);
5533 convert_escapes(global_text);
5537 update_interval = strtod(optarg, 0);
5538 update_interval_old = update_interval;
5539 if (info.music_player_interval == 0) {
5540 // default to update_interval
5541 info.music_player_interval = update_interval;
5546 total_run_times = strtod(optarg, 0);
5550 gap_x = atoi(optarg);
5554 gap_y = atoi(optarg);
5558 startup_pause = atoi(optarg);
5559 sleep(startup_pause);
5569 if (output_methods & TO_X) {
5570 load_config_file_x11(current_config);
5574 /* generate text and get initial size */
5575 extract_variable_text(global_text);
5582 if (fork_to_background) {
5587 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5594 fprintf(stderr, "\n");
5599 /* parent process */
5600 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5607 start_update_threading();
5609 text_buffer = malloc(max_user_text);
5610 memset(text_buffer, 0, max_user_text);
5611 tmpstring1 = malloc(text_buffer_size);
5612 memset(tmpstring1, 0, text_buffer_size);
5613 tmpstring2 = malloc(text_buffer_size);
5614 memset(tmpstring2, 0, text_buffer_size);
5619 X11_create_window();
5622 llua_setup_info(&info, update_interval);
5623 #endif /* HAVE_LUA */
5628 /* Set signal handlers */
5629 act.sa_handler = signal_handler;
5630 sigemptyset(&act.sa_mask);
5633 act.sa_flags |= SA_RESTART;
5636 if ( sigaction(SIGINT, &act, &oact) < 0
5637 || sigaction(SIGALRM, &act, &oact) < 0
5638 || sigaction(SIGUSR1, &act, &oact) < 0
5639 || sigaction(SIGHUP, &act, &oact) < 0
5640 || sigaction(SIGTERM, &act, &oact) < 0) {
5641 NORM_ERR("error setting signal handler: %s", strerror(errno));
5645 llua_startup_hook();
5646 #endif /* HAVE_LUA */
5649 int main(int argc, char **argv)
5658 g_signal_pending = 0;
5659 max_user_text = MAX_USER_TEXT_DEFAULT;
5661 memset(&info, 0, sizeof(info));
5665 #ifdef TCP_PORT_MONITOR
5666 /* set default connection limit */
5667 tcp_portmon_set_max_connections(0);
5670 /* handle command line parameters that don't change configs */
5672 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5673 || ((s = getenv("LANG")) && *s)) {
5674 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5676 NORM_ERR("malloc failed");
5678 for (x = 0; x < strlen(s); x++) {
5679 temp[x] = tolower(s[x]);
5682 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5688 if (!setlocale(LC_CTYPE, "")) {
5689 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5693 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5704 if (current_config) {
5705 free(current_config);
5707 current_config = strndup(optarg, max_user_text);
5710 freopen("/dev/null", "w", stderr);
5713 print_help(argv[0]);
5715 #ifdef CONFIG_OUTPUT
5722 window.window = strtol(optarg, 0, 0);
5727 disp = strdup(optarg);
5736 /* check if specified config file is valid */
5737 if (current_config) {
5739 if (stat(current_config, &sb) ||
5740 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5741 NORM_ERR("invalid configuration file '%s'\n", current_config);
5742 free(current_config);
5747 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5749 if (!current_config) {
5750 /* load default config file */
5751 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5754 /* Try to use personal config file first */
5755 to_real_path(buf, CONFIG_FILE);
5756 if (buf[0] && (fp = fopen(buf, "r"))) {
5757 current_config = strndup(buf, max_user_text);
5761 /* Try to use system config file if personal config not readable */
5762 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5763 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5767 /* No readable config found */
5768 if (!current_config) {
5769 #ifdef CONFIG_OUTPUT
5770 current_config = strdup("==builtin==");
5771 NORM_ERR("no readable personal or system-wide config file found,"
5772 " using builtin default");
5774 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5775 #endif /* ! CONF_OUTPUT */
5780 /* Load xoap keys, if existing */
5784 #ifdef HAVE_SYS_INOTIFY_H
5785 inotify_fd = inotify_init1(IN_NONBLOCK);
5786 #endif /* HAVE_SYS_INOTIFY_H */
5788 initialisation(argc, argv);
5792 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5800 void alarm_handler(void) {
5802 kill(childpid, SIGTERM);
5806 static void signal_handler(int sig)
5808 /* signal handler is light as a feather, as it should be.
5809 * we will poll g_signal_pending with each loop of conky
5810 * and do any signal processing there, NOT here (except
5811 * SIGALRM because this is caused when conky is hanging) */
5812 if(sig == SIGALRM) {
5815 g_signal_pending = sig;