1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* two strings for internal use */
151 static char *tmpstring1, *tmpstring2;
153 /* variables holding various config settings */
155 int format_human_readable;
162 int top_cpu, top_mem, top_time;
170 static int extra_newline;
171 enum x_initialiser_state x_initialised = NO;
172 static volatile int g_signal_pending;
173 /* Update interval */
174 double update_interval;
175 double update_interval_old;
176 double update_interval_bat;
177 void *global_cpu = NULL;
182 /* prototypes for internally used functions */
183 static void signal_handler(int);
184 static void print_version(void) __attribute__((noreturn));
185 static void reload_config(void);
187 static void print_version(void)
189 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
191 printf("\nCompiled in features:\n\n"
192 "System config file: "SYSTEM_CONFIG_FILE"\n"
193 "Package library path: "PACKAGE_LIBDIR"\n\n"
197 " * Xdamage extension\n"
198 # endif /* HAVE_XDAMAGE */
200 " * XDBE (double buffer extension)\n"
201 # endif /* HAVE_XDBE */
206 "\n Music detection:\n"
209 #endif /* AUDACIOUS */
225 #endif /* HAVE_OPENMP */
232 #ifdef TCP_PORT_MONITOR
234 #endif /* TCP_PORT_MONITOR */
237 #endif /* HAVE_CURL */
242 " * Weather (METAR)\n"
244 " * Weather (XOAP)\n"
249 #endif /* HAVE_IWLIB */
251 " * support for IBM/Lenovo notebooks\n"
261 #endif /* CONFIG_OUTPUT */
266 " * ALSA mixer support\n"
267 #endif /* MIXER_IS_ALSA */
280 #ifdef HAVE_LUA_CAIRO
282 #endif /* HAVE_LUA_CAIRO */
283 #ifdef HAVE_LUA_IMLIB2
286 #endif /* HAVE_LUA */
292 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
297 static void X11_create_window(void);
298 static void X11_initialisation(void);
300 struct _x11_stuff_s {
304 XserverRegion region2, part;
305 int event_base, error_base;
311 static int text_start_x, text_start_y; /* text start position in window */
312 static int text_width, text_height;
328 /* display to connect to */
329 static char *disp = NULL;
333 /* struct that has all info to be shared between
334 * instances of the same text object */
335 struct information info;
337 /* path to config file */
338 char *current_config;
340 /* set to 1 if you want all text to be in uppercase */
341 static unsigned int stuff_in_uppercase;
343 /* Run how many times? */
344 static unsigned long total_run_times;
347 static int fork_to_background;
349 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
351 /* filenames for output */
352 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
353 char *append_file = NULL; FILE *append_fpointer = NULL;
357 static int show_graph_scale;
358 static int show_graph_range;
360 /* Position on the screen */
361 static int text_alignment;
362 static int gap_x, gap_y;
365 static int draw_borders;
366 static int draw_graph_borders;
367 static int stippled_borders;
369 int get_stippled_borders(void)
371 return stippled_borders;
374 static int draw_shades, draw_outline;
376 long default_fg_color, default_bg_color, default_out_color;
378 /* create own window or draw stuff to root? */
379 static int set_transparent = 0;
382 static int own_window = 0;
383 static int background_colour = 0;
385 /* fixed size/pos is set if wm/user changes them */
386 static int fixed_size = 0, fixed_pos = 0;
389 static int minimum_width, minimum_height;
390 static int maximum_width;
395 static int sensor_device;
398 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
401 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
402 unsigned int max_user_text;
404 /* maximum size of individual text buffers, ie $exec buffer size */
405 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
410 /* no buffers in used memory? */
413 /* pad percentages to decimals? */
414 static int pad_percents = 0;
416 static char *global_text = 0;
418 char *get_global_text(void)
423 long global_text_lines;
425 static int total_updates;
426 static int updatereset;
428 void set_updatereset(int i)
433 int get_updatereset(void)
438 int check_contains(char *f, char *s)
441 FILE *where = open_file(f, 0);
446 while (fgets(buf1, 256, where)) {
447 if (strstr(buf1, s)) {
454 NORM_ERR("Could not open the file");
459 #define SECRIT_MULTILINE_CHAR '\x02'
461 static inline int calc_text_width(const char *s)
463 size_t slen = strlen(s);
466 if ((output_methods & TO_X) == 0) {
476 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
477 (const FcChar8 *) s, slen, &gi);
479 XftTextExtents8(display, fonts[selected_font].xftfont,
480 (const FcChar8 *) s, slen, &gi);
486 return XTextWidth(fonts[selected_font].font, s, slen);
491 /* formatted text to render on screen, generated in generate_text(),
492 * drawn in draw_stuff() */
494 static char *text_buffer;
496 /* quite boring functions */
498 static inline void for_each_line(char *b, int f(char *, int))
501 int special_index = 0; /* specials index */
503 for (ps = b, pe = b; *pe; pe++) {
506 special_index = f(ps, special_index);
513 f(ps, special_index);
517 static void convert_escapes(char *buf)
519 char *p = buf, *s = buf;
526 } else if (*s == '\\') {
537 /* Prints anything normally printed with snprintf according to the current value
538 * of use_spacer. Actually slightly more flexible than snprintf, as you can
539 * safely specify the destination buffer as one of your inputs. */
540 int spaced_print(char *buf, int size, const char *format, int width, ...)
549 tempbuf = malloc(size * sizeof(char));
551 // Passes the varargs along to vsnprintf
552 va_start(argp, width);
553 vsnprintf(tempbuf, size, format, argp);
556 switch (use_spacer) {
558 len = snprintf(buf, size, "%s", tempbuf);
561 len = snprintf(buf, size, "%*s", width, tempbuf);
564 len = snprintf(buf, size, "%-*s", width, tempbuf);
571 /* print percentage values
573 * - i.e., unsigned values between 0 and 100
574 * - respect the value of pad_percents */
575 int percent_print(char *buf, int size, unsigned value)
577 return spaced_print(buf, size, "%u", pad_percents, value);
580 /* converts from bytes to human readable format (K, M, G, T)
582 * The algorithm always divides by 1024, as unit-conversion of byte
583 * counts suggests. But for output length determination we need to
584 * compare with 1000 here, as we print in decimal form. */
585 void human_readable(long long num, char *buf, int size)
587 const char **suffix = suffixes;
593 /* Possibly just output as usual, for example for stdout usage */
594 if (!format_human_readable) {
595 spaced_print(buf, size, "%d", 6, round_to_int(num));
606 if (llabs(num) < 1000LL) {
607 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
611 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
619 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
621 * The goal is to always have a significance of 3, by
622 * adjusting the decimal part of the number. Sample output:
626 * so the point of alignment resides between number and unit. The
627 * upside of this is that there is minimal padding necessary, though
628 * there should be a way to make alignment take place at the decimal
629 * dot (then with fixed width decimal part).
631 * Note the repdigits below: when given a precision value, printf()
632 * rounds the float to it, not just cuts off the remaining digits. So
633 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
634 * printed with a precision of 0. Yay. */
636 precision = 0; /* print 100-999 without decimal part */
638 precision = 1; /* print 10-99 with one decimal place */
640 precision = 2; /* print 0-9 with two decimal places */
642 spaced_print(buf, size, format, width, precision, fnum, *suffix);
645 /* global object list root element */
646 static struct text_object global_root_object;
648 static long current_text_color;
650 void set_current_text_color(long colour)
652 current_text_color = colour;
655 long get_current_text_color(void)
657 return current_text_color;
660 //adds newstring to to the tree unless you can already see it when travelling back.
661 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
662 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
663 struct conftree* node;
664 struct conftree* node2;
666 for(node = previous; node != NULL; node = node->back) {
667 if(strcmp(node->string, newstring) == 0) {
671 node = malloc(sizeof(struct conftree));
672 if (previous != NULL) {
673 if(previous->vert_next == NULL) {
674 previous->vert_next = node;
676 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
677 node2->horz_next = node;
680 node->string = strdup(newstring);
681 node->horz_next = NULL;
682 node->vert_next = NULL;
683 node->back = previous;
687 void conftree_empty(struct conftree* tree) {
689 conftree_empty(tree->horz_next);
690 conftree_empty(tree->vert_next);
696 struct conftree *currentconffile;
698 static void extract_variable_text(const char *p)
700 free_text_objects(&global_root_object, 0);
714 extract_variable_text_internal(&global_root_object, p);
717 void parse_conky_vars(struct text_object *root, const char *txt,
718 char *p, int p_max_size, struct information *cur)
720 extract_variable_text_internal(root, txt);
721 generate_text_internal(p, p_max_size, *root, cur);
724 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
725 * multiline objects like $exec work with $align[rc] and friends
727 void substitute_newlines(char *p, long l)
731 while (p && *p && p < s + l) {
733 /* only substitute if it's not the last newline */
734 *p = SECRIT_MULTILINE_CHAR;
740 void generate_text_internal(char *p, int p_max_size,
741 struct text_object root, struct information *cur)
743 struct text_object *obj;
745 int need_to_load_fonts = 0;
748 /* for the OBJ_top* handler */
749 struct process **needed = 0;
752 char buff_in[p_max_size];
754 #endif /* HAVE_ICONV */
758 while (obj && p_max_size > 0) {
759 needed = 0; /* reset for top stuff */
761 /* IFBLOCK jumping algorithm
763 * This is easier as it looks like:
764 * - each IF checks it's condition
765 * - on FALSE: call DO_JUMP
766 * - on TRUE: don't care
767 * - each ELSE calls DO_JUMP unconditionally
768 * - each ENDIF is silently being ignored
771 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
772 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
773 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
774 * regularly, "obj" is being updated to point to obj->next, so object parsing
775 * continues right after the corresponding ELSE or ENDIF. This means that if we
776 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
777 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
778 * jumped, and there is nothing to do.
782 if (obj->ifblock_next) \
783 obj = obj->ifblock_next; \
786 #define OBJ(a) break; case OBJ_##a:
790 NORM_ERR("not implemented obj type %d", obj->type);
792 print_read_tcp(obj, p, p_max_size);
796 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
798 #endif /* !__OpenBSD__ */
802 ok = get_freq(p, p_max_size, "%.0f", 1,
810 ok = get_freq(p, p_max_size, "%'.2f", 1000,
813 /* OpenBSD has no such flag (SUSv2) */
814 ok = get_freq(p, p_max_size, "%.2f", 1000,
816 #endif /* __OpenBSD */
819 #if defined(__linux__)
823 ok = get_voltage(p, p_max_size, "%.0f", 1,
830 ok = get_voltage(p, p_max_size, "%'.3f", 1000,
836 OBJ(wireless_essid) {
837 print_wireless_essid(obj, p, p_max_size);
840 print_wireless_mode(obj, p, p_max_size);
842 OBJ(wireless_bitrate) {
843 print_wireless_bitrate(obj, p, p_max_size);
846 print_wireless_ap(obj, p, p_max_size);
848 OBJ(wireless_link_qual) {
849 print_wireless_link_qual(obj, p, p_max_size);
851 OBJ(wireless_link_qual_max) {
852 print_wireless_link_qual_max(obj, p, p_max_size);
854 OBJ(wireless_link_qual_perc) {
855 print_wireless_link_qual_perc(obj, p, p_max_size);
857 OBJ(wireless_link_bar) {
858 print_wireless_link_bar(obj, p, p_max_size);
860 #endif /* HAVE_IWLIB */
862 #endif /* __linux__ */
866 get_acpi_fan(p, p_max_size);
869 get_acpi_ac_adapter(p, p_max_size);
872 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
875 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
877 OBJ(battery_percent) {
878 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
882 if(output_methods & TO_X) {
883 new_bar(obj, p, get_battery_perct_bar(obj->data.s));
886 new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
889 get_battery_short_status(p, p_max_size, obj->data.s);
891 #endif /* __OpenBSD__ */
894 human_readable(cur->buffers * 1024, p, 255);
897 human_readable(cur->cached * 1024, p, 255);
899 OBJ(cmdline_to_pid) {
900 print_cmdline_to_pid(obj, p, p_max_size);
903 if (obj->data.i > info.cpu_count) {
904 NORM_ERR("obj->data.i %i info.cpu_count %i",
905 obj->data.i, info.cpu_count);
906 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
908 percent_print(p, p_max_size,
909 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
913 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
917 if(output_methods & TO_X) {
918 new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
921 new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
925 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
928 print_loadgraph(obj, p);
932 new_fg(p, obj->data.l);
967 snprintf(p, p_max_size, "%s", VERSION);
969 OBJ(conky_build_date) {
970 snprintf(p, p_max_size, "%s", BUILD_DATE);
972 OBJ(conky_build_arch) {
973 snprintf(p, p_max_size, "%s", BUILD_ARCH);
975 #if defined(__linux__)
977 snprintf(p, p_max_size, "%s",
978 get_disk_protect_queue(obj->data.s));
981 print_i8k_version(obj, p, p_max_size);
984 print_i8k_bios(obj, p, p_max_size);
987 print_i8k_serial(obj, p, p_max_size);
990 print_i8k_cpu_temp(obj, p, p_max_size);
992 OBJ(i8k_left_fan_status) {
993 print_i8k_left_fan_status(obj, p, p_max_size);
995 OBJ(i8k_right_fan_status) {
996 print_i8k_right_fan_status(obj, p, p_max_size);
998 OBJ(i8k_left_fan_rpm) {
999 print_i8k_left_fan_rpm(obj, p, p_max_size);
1001 OBJ(i8k_right_fan_rpm) {
1002 print_i8k_right_fan_rpm(obj, p, p_max_size);
1004 OBJ(i8k_ac_status) {
1005 print_i8k_ac_status(obj, p, p_max_size);
1007 OBJ(i8k_buttons_status) {
1008 print_i8k_buttons_status(obj, p, p_max_size);
1012 get_ibm_acpi_fan(obj, p, p_max_size);
1015 print_ibm_temps(obj, p, p_max_size);
1018 get_ibm_acpi_volume(obj, p, p_max_size);
1020 OBJ(ibm_brightness) {
1021 get_ibm_acpi_brightness(obj, p, p_max_size);
1024 /* information from sony_laptop kernel module
1025 * /sys/devices/platform/sony-laptop */
1026 OBJ(sony_fanspeed) {
1027 get_sony_fanspeed(p, p_max_size);
1030 if (!gateway_exists()) {
1035 print_gateway_iface(p, p_max_size);
1038 print_gateway_ip(p, p_max_size);
1041 snprintf(p, p_max_size, "%d", get_laptop_mode());
1044 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1046 #endif /* __linux__ */
1047 #if (defined(__FreeBSD__) || defined(__linux__))
1049 if (!interface_up(obj)) {
1055 OBJ(obsd_sensors_temp) {
1056 print_obsd_sensors_temp(obj, p, p_max_size);
1058 OBJ(obsd_sensors_fan) {
1059 print_obsd_sensors_fan(obj, p, p_max_size);
1061 OBJ(obsd_sensors_volt) {
1062 print_obsd_sensors_volt(obj, p, p_max_size);
1065 get_obsd_vendor(p, p_max_size);
1068 get_obsd_product(p, p_max_size);
1070 #endif /* __OpenBSD__ */
1073 new_font(p, obj->data.s);
1074 need_to_load_fonts = 1;
1078 print_diskio(obj, p, p_max_size);
1081 print_diskio_write(obj, p, p_max_size);
1084 print_diskio_read(obj, p, p_max_size);
1088 print_diskiograph(obj, p, p_max_size);
1090 OBJ(diskiograph_read) {
1091 print_diskiograph(obj, p, p_max_size);
1093 OBJ(diskiograph_write) {
1094 print_diskiograph(obj, p, p_max_size);
1098 print_downspeed(obj, p, p_max_size);
1101 print_downspeedf(obj, p, p_max_size);
1104 OBJ(downspeedgraph) {
1105 print_downspeedgraph(obj, p, p_max_size);
1109 /* Since we see you, you're if has not jumped.
1110 * Do Ninja jump here: without leaving traces.
1111 * This is to prevent us from stale jumped flags.
1113 obj = obj->ifblock_next;
1117 /* harmless object, just ignore */
1120 print_addr(obj, p, p_max_size);
1122 #if defined(__linux__)
1124 print_addrs(obj, p, p_max_size);
1126 #endif /* __linux__ */
1127 #if defined(IMLIB2) && defined(X11)
1129 /* doesn't actually draw anything, just queues it omp. the
1130 * image will get drawn after the X event loop */
1131 cimlib_add_image(obj->data.s);
1135 evaluate(obj->data.s, p, p_max_size);
1138 print_exec(obj, p, p_max_size);
1141 print_execp(obj, p, p_max_size);
1145 print_execgauge(obj, p, p_max_size);
1149 print_execbar(obj, p, p_max_size);
1153 print_execgraph(obj, p, p_max_size);
1157 print_execibar(obj, p, p_max_size);
1161 print_execigraph(obj, p, p_max_size);
1164 print_execigauge(obj, p, p_max_size);
1168 print_execi(obj, p, p_max_size);
1171 print_execpi(obj, p, p_max_size);
1174 print_texeci(obj, p, p_max_size);
1177 print_imap_unseen(obj, p, p_max_size);
1179 OBJ(imap_messages) {
1180 print_imap_messages(obj, p, p_max_size);
1183 print_pop3_unseen(obj, p, p_max_size);
1186 print_pop3_used(obj, p, p_max_size);
1189 print_fs_bar(obj, 0, p, p_max_size);
1192 print_fs_free(obj, p, p_max_size);
1195 print_fs_perc(obj, 1, p, p_max_size);
1198 print_fs_size(obj, p, p_max_size);
1201 print_fs_type(obj, p, p_max_size);
1204 print_fs_used(obj, p, p_max_size);
1207 print_fs_bar(obj, 1, p, p_max_size);
1210 print_fs_perc(obj, 0, p, p_max_size);
1213 print_loadavg(obj, p, p_max_size);
1216 new_goto(p, obj->data.i);
1223 new_hr(p, obj->data.i);
1227 print_nameserver(obj, p, p_max_size);
1231 print_eve(obj, p, p_max_size);
1236 curl_print(obj, p, p_max_size);
1241 rss_print_info(obj, p, p_max_size);
1246 print_weather(obj, p, p_max_size);
1250 OBJ(weather_forecast) {
1251 print_weather_forecast(obj, p, p_max_size);
1256 print_lua(obj, p, p_max_size);
1259 print_lua_parse(obj, p, p_max_size);
1262 print_lua_bar(obj, p, p_max_size);
1266 print_lua_graph(obj, p, p_max_size);
1269 print_lua_gauge(obj, p, p_max_size);
1272 #endif /* HAVE_LUA */
1278 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1279 snprintf(p, p_max_size, "N/A");
1281 temp_print(p, p_max_size, (double)val,
1282 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1287 new_offset(p, obj->data.i);
1290 new_voffset(p, obj->data.i);
1294 print_sysfs_sensor(obj, p, p_max_size);
1297 print_sysfs_sensor(obj, p, p_max_size);
1300 print_sysfs_sensor(obj, p, p_max_size);
1302 #endif /* __linux__ */
1304 new_alignr(p, obj->data.i);
1307 new_alignc(p, obj->data.i);
1310 char buf[max_user_text];
1311 struct information *tmp_info =
1312 malloc(sizeof(struct information));
1313 memcpy(tmp_info, cur, sizeof(struct information));
1314 generate_text_internal(buf, max_user_text,
1315 *obj->sub, tmp_info);
1317 if (strlen(buf) != 0) {
1323 char expression[max_user_text];
1325 struct information *tmp_info;
1327 tmp_info = malloc(sizeof(struct information));
1328 memcpy(tmp_info, cur, sizeof(struct information));
1329 generate_text_internal(expression, max_user_text,
1330 *obj->sub, tmp_info);
1331 DBGP("parsed arg into '%s'", expression);
1333 val = compare(expression);
1335 NORM_ERR("compare failed for expression '%s'",
1345 spc = strchr(obj->data.s, ' ');
1346 if (!spc && access(obj->data.s, F_OK)) {
1350 if (check_contains(obj->data.s, spc + 1))
1357 && (!check_mount(obj->data.s))) {
1363 if (!get_process_by_name(obj->data.s)) {
1365 if ((obj->data.s) && system(obj->data.s)) {
1370 #if defined(__linux__)
1372 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1376 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1379 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1384 human_readable(cur->mem * 1024, p, 255);
1387 human_readable(cur->memeasyfree * 1024, p, 255);
1390 human_readable(cur->memfree * 1024, p, 255);
1393 human_readable(cur->memmax * 1024, p, 255);
1397 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1401 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1406 if(output_methods & TO_X) {
1407 new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1410 new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1414 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1419 print_mixer(obj, p, p_max_size);
1422 print_mixerl(obj, p, p_max_size);
1425 print_mixerr(obj, p, p_max_size);
1429 print_mixer_bar(obj, p, p_max_size);
1432 print_mixerl_bar(obj, p, p_max_size);
1435 print_mixerr_bar(obj, p, p_max_size);
1438 OBJ(if_mixer_mute) {
1439 if (!check_mixer_muted(obj)) {
1444 #define NOT_IN_X "Not running in X"
1446 if(x_initialised != YES) {
1447 strncpy(p, NOT_IN_X, p_max_size);
1449 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1452 OBJ(monitor_number) {
1453 if(x_initialised != YES) {
1454 strncpy(p, NOT_IN_X, p_max_size);
1456 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1460 if(x_initialised != YES) {
1461 strncpy(p, NOT_IN_X, p_max_size);
1463 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1466 OBJ(desktop_number) {
1467 if(x_initialised != YES) {
1468 strncpy(p, NOT_IN_X, p_max_size);
1470 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1474 if(x_initialised != YES) {
1475 strncpy(p, NOT_IN_X, p_max_size);
1476 }else if(cur->x11.desktop.name != NULL) {
1477 strncpy(p, cur->x11.desktop.name, p_max_size);
1483 char buf[max_user_text];
1485 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1487 print_format_time(obj, p, p_max_size);
1491 print_mails(obj, p, p_max_size);
1494 print_new_mails(obj, p, p_max_size);
1497 print_seen_mails(obj, p, p_max_size);
1500 print_unseen_mails(obj, p, p_max_size);
1502 OBJ(flagged_mails) {
1503 print_flagged_mails(obj, p, p_max_size);
1505 OBJ(unflagged_mails) {
1506 print_unflagged_mails(obj, p, p_max_size);
1508 OBJ(forwarded_mails) {
1509 print_forwarded_mails(obj, p, p_max_size);
1511 OBJ(unforwarded_mails) {
1512 print_unforwarded_mails(obj, p, p_max_size);
1514 OBJ(replied_mails) {
1515 print_replied_mails(obj, p, p_max_size);
1517 OBJ(unreplied_mails) {
1518 print_unreplied_mails(obj, p, p_max_size);
1521 print_draft_mails(obj, p, p_max_size);
1523 OBJ(trashed_mails) {
1524 print_trashed_mails(obj, p, p_max_size);
1527 print_mboxscan(obj, p, p_max_size);
1530 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1533 new_outline(p, obj->data.l);
1536 char buf[max_user_text];
1538 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1540 print_pid_chroot(obj, p, p_max_size);
1543 char buf[max_user_text];
1545 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1547 print_pid_cmdline(obj, p, p_max_size);
1550 char buf[max_user_text];
1552 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1554 print_pid_cwd(obj, p, p_max_size);
1557 char buf[max_user_text];
1559 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1561 print_pid_environ(obj, p, p_max_size);
1563 OBJ(pid_environ_list) {
1564 char buf[max_user_text];
1566 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1568 print_pid_environ_list(obj, p, p_max_size);
1571 char buf[max_user_text];
1573 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1575 print_pid_exe(obj, p, p_max_size);
1578 char buf[max_user_text];
1580 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1582 print_pid_nice(obj, p, p_max_size);
1584 OBJ(pid_openfiles) {
1585 char buf[max_user_text];
1587 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1589 print_pid_openfiles(obj, p, p_max_size);
1592 char buf[max_user_text];
1594 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1596 print_pid_parent(obj, p, p_max_size);
1599 char buf[max_user_text];
1601 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1603 print_pid_priority(obj, p, p_max_size);
1606 char buf[max_user_text];
1608 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1610 print_pid_state(obj, p, p_max_size);
1612 OBJ(pid_state_short) {
1613 char buf[max_user_text];
1615 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1617 print_pid_state_short(obj, p, p_max_size);
1620 char buf[max_user_text];
1622 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1624 print_pid_stderr(obj, p, p_max_size);
1627 char buf[max_user_text];
1629 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1631 print_pid_stdin(obj, p, p_max_size);
1634 char buf[max_user_text];
1636 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1638 print_pid_stdout(obj, p, p_max_size);
1641 char buf[max_user_text];
1643 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1645 print_pid_threads(obj, p, p_max_size);
1647 OBJ(pid_thread_list) {
1648 char buf[max_user_text];
1650 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1652 print_pid_thread_list(obj, p, p_max_size);
1654 OBJ(pid_time_kernelmode) {
1655 char buf[max_user_text];
1657 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1659 print_pid_time_kernelmode(obj, p, p_max_size);
1661 OBJ(pid_time_usermode) {
1662 char buf[max_user_text];
1664 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1666 print_pid_time_usermode(obj, p, p_max_size);
1669 char buf[max_user_text];
1671 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1673 print_pid_time(obj, p, p_max_size);
1676 char buf[max_user_text];
1678 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1680 print_pid_uid(obj, p, p_max_size);
1683 char buf[max_user_text];
1685 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1687 print_pid_euid(obj, p, p_max_size);
1690 char buf[max_user_text];
1692 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1694 print_pid_suid(obj, p, p_max_size);
1697 char buf[max_user_text];
1699 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1701 print_pid_fsuid(obj, p, p_max_size);
1704 char buf[max_user_text];
1706 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1708 print_pid_gid(obj, p, p_max_size);
1711 char buf[max_user_text];
1713 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1715 print_pid_egid(obj, p, p_max_size);
1718 char buf[max_user_text];
1720 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1722 print_pid_sgid(obj, p, p_max_size);
1725 char buf[max_user_text];
1727 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1729 print_pid_fsgid(obj, p, p_max_size);
1732 char buf[max_user_text];
1734 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1736 print_pid_read(obj, p, p_max_size);
1739 char buf[max_user_text];
1741 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1743 print_pid_vmpeak(obj, p, p_max_size);
1746 char buf[max_user_text];
1748 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1750 print_pid_vmsize(obj, p, p_max_size);
1753 char buf[max_user_text];
1755 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1757 print_pid_vmlck(obj, p, p_max_size);
1760 char buf[max_user_text];
1762 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1764 print_pid_vmhwm(obj, p, p_max_size);
1767 char buf[max_user_text];
1769 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1771 print_pid_vmrss(obj, p, p_max_size);
1774 char buf[max_user_text];
1776 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1778 print_pid_vmdata(obj, p, p_max_size);
1781 char buf[max_user_text];
1783 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1785 print_pid_vmstk(obj, p, p_max_size);
1788 char buf[max_user_text];
1790 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1792 print_pid_vmexe(obj, p, p_max_size);
1795 char buf[max_user_text];
1797 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1799 print_pid_vmlib(obj, p, p_max_size);
1802 char buf[max_user_text];
1804 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1806 print_pid_vmpte(obj, p, p_max_size);
1809 char buf[max_user_text];
1811 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1813 print_pid_write(obj, p, p_max_size);
1816 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1818 OBJ(running_processes) {
1819 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1821 OBJ(running_threads) {
1822 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1825 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1828 snprintf(p, p_max_size, "%s", obj->data.s);
1832 new_bg(p, obj->data.l);
1835 new_stippled_hr(obj, p);
1839 human_readable(cur->swap * 1024, p, 255);
1842 human_readable(cur->swapfree * 1024, p, 255);
1845 human_readable(cur->swapmax * 1024, p, 255);
1848 if (cur->swapmax == 0) {
1849 strncpy(p, "No swap", p_max_size);
1851 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1856 if(output_methods & TO_X) {
1857 new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1860 new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1863 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1866 print_time(obj, p, p_max_size);
1869 print_utime(obj, p, p_max_size);
1872 print_tztime(obj, p, p_max_size);
1875 print_totaldown(obj, p, p_max_size);
1878 print_totalup(obj, p, p_max_size);
1881 char buf[max_user_text];
1883 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1885 print_gid_name(obj, p, p_max_size);
1888 char buf[max_user_text];
1890 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1892 print_uid_name(obj, p, p_max_size);
1895 snprintf(p, p_max_size, "%d", total_updates);
1898 if(total_updates % updatereset != obj->data.i - 1) {
1903 print_upspeed(obj, p, p_max_size);
1906 print_upspeedf(obj, p, p_max_size);
1910 print_upspeedgraph(obj, p, p_max_size);
1914 format_seconds_short(p, p_max_size, (int) cur->uptime);
1917 format_seconds(p, p_max_size, (int) cur->uptime);
1921 snprintf(p, p_max_size, "%s", cur->users.names);
1924 snprintf(p, p_max_size, "%s", cur->users.terms);
1927 snprintf(p, p_max_size, "%s", cur->users.times);
1930 update_user_time(obj->data.s);
1931 snprintf(p, p_max_size, "%s", cur->users.ctime);
1934 snprintf(p, p_max_size, "%d", cur->users.number);
1936 #endif /* __linux__ */
1937 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1938 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1942 msg = get_apm_adapter();
1943 snprintf(p, p_max_size, "%s", msg);
1946 OBJ(apm_battery_life) {
1949 msg = get_apm_battery_life();
1950 snprintf(p, p_max_size, "%s", msg);
1953 OBJ(apm_battery_time) {
1956 msg = get_apm_battery_time();
1957 snprintf(p, p_max_size, "%s", msg);
1960 #endif /* __FreeBSD__ __OpenBSD__ */
1964 print_mpd_title(obj, p, p_max_size);
1966 print_mpd_artist(obj, p, p_max_size);
1968 print_mpd_album(obj, p, p_max_size);
1970 print_mpd_random(obj, p, p_max_size);
1972 print_mpd_repeat(obj, p, p_max_size);
1974 print_mpd_track(obj, p, p_max_size);
1976 print_mpd_name(obj, p, p_max_size);
1978 print_mpd_file(obj, p, p_max_size);
1980 print_mpd_vol(obj, p, p_max_size);
1982 print_mpd_bitrate(obj, p, p_max_size);
1984 print_mpd_status(obj, p, p_max_size);
1986 print_mpd_elapsed(obj, p, p_max_size);
1989 print_mpd_length(obj, p, p_max_size);
1992 print_mpd_percent(obj, p, p_max_size);
1995 print_mpd_bar(obj, p, p_max_size);
1998 print_mpd_smart(obj, p, p_max_size);
2000 OBJ(if_mpd_playing) {
2001 if (!mpd_get_info()->is_playing) {
2008 #define MOC_PRINT(t, a) \
2009 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2011 MOC_PRINT(state, "??");
2014 MOC_PRINT(file, "no file");
2017 MOC_PRINT(title, "no title");
2020 MOC_PRINT(artist, "no artist");
2023 MOC_PRINT(song, "no song");
2026 MOC_PRINT(album, "no album");
2028 OBJ(moc_totaltime) {
2029 MOC_PRINT(totaltime, "0:00");
2032 MOC_PRINT(timeleft, "0:00");
2035 MOC_PRINT(curtime, "0:00");
2038 MOC_PRINT(bitrate, "0Kbps");
2041 MOC_PRINT(rate, "0KHz");
2047 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2050 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2053 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2056 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2058 OBJ(xmms2_comment) {
2059 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2062 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2065 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2068 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2070 OBJ(xmms2_tracknr) {
2071 if (cur->xmms2.tracknr != -1) {
2072 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2075 OBJ(xmms2_bitrate) {
2076 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2079 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2082 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2084 OBJ(xmms2_elapsed) {
2085 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2086 (cur->xmms2.elapsed / 1000) % 60);
2088 OBJ(xmms2_duration) {
2089 snprintf(p, p_max_size, "%02d:%02d",
2090 cur->xmms2.duration / 60000,
2091 (cur->xmms2.duration / 1000) % 60);
2093 OBJ(xmms2_percent) {
2094 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2098 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
2101 OBJ(xmms2_playlist) {
2102 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2104 OBJ(xmms2_timesplayed) {
2105 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2108 if (strlen(cur->xmms2.title) < 2
2109 && strlen(cur->xmms2.title) < 2) {
2110 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2112 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2116 OBJ(if_xmms2_connected) {
2117 if (cur->xmms2.conn_state != 1) {
2123 OBJ(audacious_status) {
2124 snprintf(p, p_max_size, "%s",
2125 cur->audacious.items[AUDACIOUS_STATUS]);
2127 OBJ(audacious_title) {
2128 snprintf(p, cur->audacious.max_title_len > 0
2129 ? cur->audacious.max_title_len : p_max_size, "%s",
2130 cur->audacious.items[AUDACIOUS_TITLE]);
2132 OBJ(audacious_length) {
2133 snprintf(p, p_max_size, "%s",
2134 cur->audacious.items[AUDACIOUS_LENGTH]);
2136 OBJ(audacious_length_seconds) {
2137 snprintf(p, p_max_size, "%s",
2138 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2140 OBJ(audacious_position) {
2141 snprintf(p, p_max_size, "%s",
2142 cur->audacious.items[AUDACIOUS_POSITION]);
2144 OBJ(audacious_position_seconds) {
2145 snprintf(p, p_max_size, "%s",
2146 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2148 OBJ(audacious_bitrate) {
2149 snprintf(p, p_max_size, "%s",
2150 cur->audacious.items[AUDACIOUS_BITRATE]);
2152 OBJ(audacious_frequency) {
2153 snprintf(p, p_max_size, "%s",
2154 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2156 OBJ(audacious_channels) {
2157 snprintf(p, p_max_size, "%s",
2158 cur->audacious.items[AUDACIOUS_CHANNELS]);
2160 OBJ(audacious_filename) {
2161 snprintf(p, p_max_size, "%s",
2162 cur->audacious.items[AUDACIOUS_FILENAME]);
2164 OBJ(audacious_playlist_length) {
2165 snprintf(p, p_max_size, "%s",
2166 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2168 OBJ(audacious_playlist_position) {
2169 snprintf(p, p_max_size, "%s",
2170 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2172 OBJ(audacious_main_volume) {
2173 snprintf(p, p_max_size, "%s",
2174 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2177 OBJ(audacious_bar) {
2181 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2182 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2183 new_bar(obj, p, (int) (progress * 255.0f));
2186 #endif /* AUDACIOUS */
2190 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2193 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2196 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2199 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2202 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2205 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2208 /* we have four different types of top (top, top_mem,
2209 * top_time and top_io). To avoid having almost-same code four
2210 * times, we have this special handler. */
2219 print_top(obj, p, p_max_size);
2220 #endif /* __linux__ */
2222 print_tailhead("tail", obj, p, p_max_size);
2225 print_tailhead("head", obj, p, p_max_size);
2228 print_lines(obj, p, p_max_size);
2232 print_words(obj, p, p_max_size);
2234 #ifdef TCP_PORT_MONITOR
2236 tcp_portmon_action(obj, p, p_max_size);
2238 #endif /* TCP_PORT_MONITOR */
2242 do_iconv_start(obj);
2247 #endif /* HAVE_ICONV */
2249 OBJ(entropy_avail) {
2250 print_entropy_avail(obj, p, p_max_size);
2253 print_entropy_perc(obj, p, p_max_size);
2255 OBJ(entropy_poolsize) {
2256 print_entropy_poolsize(obj, p, p_max_size);
2259 print_entropy_bar(obj, p, p_max_size);
2263 print_smapi(obj, p, p_max_size);
2265 OBJ(if_smapi_bat_installed) {
2267 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2268 if(!smapi_bat_installed(idx)) {
2272 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2274 OBJ(smapi_bat_perc) {
2275 print_smapi_bat_perc(obj, p, p_max_size);
2277 OBJ(smapi_bat_temp) {
2278 print_smapi_bat_temp(obj, p, p_max_size);
2280 OBJ(smapi_bat_power) {
2281 print_smapi_bat_power(obj, p, p_max_size);
2284 OBJ(smapi_bat_bar) {
2285 print_smapi_bat_bar(obj, p, p_max_size);
2291 char buf[max_user_text];
2293 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2294 snprintf(p, p_max_size, "%s", buf);
2300 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2301 char buf[max_user_text];
2304 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2305 snprintf(p, p_max_size, "%s", buf);
2306 if(total_updates % 2) {
2307 for(j=0; p[j] != 0; j++) {
2313 char buf[max_user_text];
2315 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2317 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2318 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2319 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2320 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2321 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2322 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2323 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2325 snprintf(p, p_max_size, "%s", buf);
2328 print_scroll(obj, p, p_max_size, cur);
2331 print_combine(obj, p, cur);
2335 print_nvidia_value(obj, display, p, p_max_size);
2340 /* This is just a meta-object to set host:port */
2343 snprintf(p, p_max_size, "%s",
2344 cur->apcupsd.items[APCUPSD_NAME]);
2346 OBJ(apcupsd_model) {
2347 snprintf(p, p_max_size, "%s",
2348 cur->apcupsd.items[APCUPSD_MODEL]);
2350 OBJ(apcupsd_upsmode) {
2351 snprintf(p, p_max_size, "%s",
2352 cur->apcupsd.items[APCUPSD_UPSMODE]);
2354 OBJ(apcupsd_cable) {
2355 snprintf(p, p_max_size, "%s",
2356 cur->apcupsd.items[APCUPSD_CABLE]);
2358 OBJ(apcupsd_status) {
2359 snprintf(p, p_max_size, "%s",
2360 cur->apcupsd.items[APCUPSD_STATUS]);
2362 OBJ(apcupsd_linev) {
2363 snprintf(p, p_max_size, "%s",
2364 cur->apcupsd.items[APCUPSD_LINEV]);
2367 snprintf(p, p_max_size, "%s",
2368 cur->apcupsd.items[APCUPSD_LOAD]);
2370 OBJ(apcupsd_loadbar) {
2373 if(output_methods & TO_X) {
2374 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2375 new_bar(obj, p, (int) progress);
2379 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2380 new_bar_in_shell(obj, p, p_max_size, (int) progress);
2384 OBJ(apcupsd_loadgraph) {
2386 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2387 new_graph(obj, p, (int)progress);
2389 OBJ(apcupsd_loadgauge) {
2391 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2392 new_gauge(obj, p, (int)progress);
2395 OBJ(apcupsd_charge) {
2396 snprintf(p, p_max_size, "%s",
2397 cur->apcupsd.items[APCUPSD_CHARGE]);
2399 OBJ(apcupsd_timeleft) {
2400 snprintf(p, p_max_size, "%s",
2401 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2404 snprintf(p, p_max_size, "%s",
2405 cur->apcupsd.items[APCUPSD_TEMP]);
2407 OBJ(apcupsd_lastxfer) {
2408 snprintf(p, p_max_size, "%s",
2409 cur->apcupsd.items[APCUPSD_LASTXFER]);
2411 #endif /* APCUPSD */
2418 size_t a = strlen(p);
2421 iconv_convert(&a, buff_in, p, p_max_size);
2422 #endif /* HAVE_ICONV */
2423 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2425 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2426 #endif /* HAVE_LUA */
2428 substitute_newlines(p, a - 2);
2437 /* load any new fonts we may have had */
2438 if (need_to_load_fonts) {
2444 void evaluate(const char *text, char *p, int p_max_size)
2446 struct information *tmp_info;
2447 struct text_object subroot;
2449 tmp_info = malloc(sizeof(struct information));
2450 memcpy(tmp_info, &info, sizeof(struct information));
2451 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2452 DBGP("evaluated '%s' to '%s'", text, p);
2454 free_text_objects(&subroot, 1);
2458 double current_update_time, next_update_time, last_update_time;
2460 static void generate_text(void)
2462 struct information *cur = &info;
2469 current_update_time = get_time();
2473 /* add things to the buffer */
2479 generate_text_internal(p, max_user_text, global_root_object, cur);
2481 if (stuff_in_uppercase) {
2484 tmp_p = text_buffer;
2486 *tmp_p = toupper(*tmp_p);
2491 next_update_time += update_interval;
2492 if (next_update_time < get_time()) {
2493 next_update_time = get_time() + update_interval;
2494 } else if (next_update_time > get_time() + update_interval) {
2495 next_update_time = get_time() + update_interval;
2497 last_update_time = current_update_time;
2501 void set_update_interval(double interval)
2503 update_interval = interval;
2504 update_interval_old = interval;
2507 static inline int get_string_width(const char *s)
2509 return *s ? calc_text_width(s) : 0;
2513 static int get_string_width_special(char *s, int special_index)
2523 if ((output_methods & TO_X) == 0)
2526 p = strndup(s, text_buffer_size);
2530 if (*p == SPECIAL_CHAR) {
2531 /* shift everything over by 1 so that the special char
2532 * doesn't mess up the size calculation */
2533 for (i = 0; i < (long)strlen(p); i++) {
2534 *(p + i) = *(p + i + 1);
2536 if (specials[special_index + idx].type == GRAPH
2537 || specials[special_index + idx].type == GAUGE
2538 || specials[special_index + idx].type == BAR) {
2539 width += specials[special_index + idx].width;
2542 } else if (*p == SECRIT_MULTILINE_CHAR) {
2549 if (strlen(final) > 1) {
2550 width += calc_text_width(final);
2556 static int text_size_updater(char *s, int special_index);
2558 int last_font_height;
2559 static void update_text_area(void)
2563 if ((output_methods & TO_X) == 0)
2565 /* update text size if it isn't fixed */
2570 text_width = minimum_width;
2572 last_font_height = font_height();
2573 for_each_line(text_buffer, text_size_updater);
2575 if (text_height < minimum_height) {
2576 text_height = minimum_height;
2578 if (text_width > maximum_width && maximum_width > 0) {
2579 text_width = maximum_width;
2583 /* get text position on workarea */
2584 switch (text_alignment) {
2585 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2589 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2590 y = workarea[3] - text_height - gap_y;
2593 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2594 y = workarea[3] / 2 - text_height / 2 - gap_y;
2597 switch (text_alignment) {
2598 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2602 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2603 x = workarea[2] - text_width - gap_x;
2606 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2607 x = workarea[2] / 2 - text_width / 2 - gap_x;
2611 if (text_alignment == NONE) { // Let the WM manage the window
2618 #endif /* OWN_WINDOW */
2621 if (own_window && !fixed_pos) {
2624 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2625 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2626 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2627 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2631 /* If window size doesn't match to workarea's size,
2632 * then window probably includes panels (gnome).
2633 * Blah, doesn't work on KDE. */
2634 if (workarea[2] != window.width || workarea[3] != window.height) {
2643 /* update lua window globals */
2644 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2645 #endif /* HAVE_LUA */
2650 static int cur_x, cur_y; /* current x and y for drawing */
2652 //draw_mode also without X11 because we only need to print to stdout with FG
2653 static int draw_mode; /* FG, BG or OUTLINE */
2655 static long current_color;
2657 static int text_size_updater(char *s, int special_index)
2662 if ((output_methods & TO_X) == 0)
2664 /* get string widths and skip specials */
2667 if (*p == SPECIAL_CHAR) {
2669 w += get_string_width(s);
2672 if (specials[special_index].type == BAR
2673 || specials[special_index].type == GAUGE
2674 || specials[special_index].type == GRAPH) {
2675 w += specials[special_index].width;
2676 if (specials[special_index].height > last_font_height) {
2677 last_font_height = specials[special_index].height;
2678 last_font_height += font_height();
2680 } else if (specials[special_index].type == OFFSET) {
2681 if (specials[special_index].arg > 0) {
2682 w += specials[special_index].arg;
2684 } else if (specials[special_index].type == VOFFSET) {
2685 last_font_height += specials[special_index].arg;
2686 } else if (specials[special_index].type == GOTO) {
2687 if (specials[special_index].arg > cur_x) {
2688 w = (int) specials[special_index].arg;
2690 } else if (specials[special_index].type == TAB) {
2691 int start = specials[special_index].arg;
2692 int step = specials[special_index].width;
2694 if (!step || step < 0) {
2697 w += step - (cur_x - text_start_x - start) % step;
2698 } else if (specials[special_index].type == FONT) {
2699 selected_font = specials[special_index].font_added;
2700 if (font_height() > last_font_height) {
2701 last_font_height = font_height();
2707 } else if (*p == SECRIT_MULTILINE_CHAR) {
2710 lw = get_string_width(s);
2711 *p = SECRIT_MULTILINE_CHAR;
2713 w = lw > w ? lw : w;
2714 text_height += last_font_height;
2718 w += get_string_width(s);
2719 if (w > text_width) {
2722 if (text_width > maximum_width && maximum_width) {
2723 text_width = maximum_width;
2726 text_height += last_font_height;
2727 last_font_height = font_height();
2728 return special_index;
2732 static inline void set_foreground_color(long c)
2735 if (output_methods & TO_X) {
2737 XSetForeground(display, window.gc, c);
2741 if (output_methods & TO_NCURSES) {
2742 attron(COLOR_PAIR(c));
2744 #endif /* NCURSES */
2749 static void draw_string(const char *s)
2751 int i, i2, pos, width_of_s;
2754 char *s_with_newlines;
2760 width_of_s = get_string_width(s);
2761 s_with_newlines = strdup(s);
2762 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2763 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2764 s_with_newlines[i] = '\n';
2767 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2768 printf("%s\n", s_with_newlines);
2769 if (extra_newline) fputc('\n', stdout);
2770 fflush(stdout); /* output immediately, don't buffer */
2772 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2773 fprintf(stderr, "%s\n", s_with_newlines);
2774 fflush(stderr); /* output immediately, don't buffer */
2776 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2777 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2779 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2780 fprintf(append_fpointer, "%s\n", s_with_newlines);
2783 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2784 printw("%s", s_with_newlines);
2787 free(s_with_newlines);
2788 memset(tmpstring1, 0, text_buffer_size);
2789 memset(tmpstring2, 0, text_buffer_size);
2790 strncpy(tmpstring1, s, text_buffer_size - 1);
2795 if (output_methods & TO_X) {
2796 max = ((text_width - width_of_s) / get_string_width(" "));
2799 /* This code looks for tabs in the text and coverts them to spaces.
2800 * The trick is getting the correct number of spaces, and not going
2801 * over the window's size without forcing the window larger. */
2802 for (i = 0; i < (int) text_buffer_size; i++) {
2803 if (tmpstring1[i] == '\t') {
2805 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2806 /* guard against overrun */
2807 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2812 /* guard against overrun */
2813 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2818 if (output_methods & TO_X) {
2819 if (text_width == maximum_width) {
2820 /* this means the text is probably pushing the limit,
2821 * so we'll chop it */
2822 while (cur_x + get_string_width(tmpstring2) - text_start_x
2823 > maximum_width && strlen(tmpstring2) > 0) {
2824 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2831 if (output_methods & TO_X) {
2837 c.pixel = current_color;
2838 XQueryColor(display, DefaultColormap(display, screen), &c);
2841 c2.color.red = c.red;
2842 c2.color.green = c.green;
2843 c2.color.blue = c.blue;
2844 c2.color.alpha = fonts[selected_font].font_alpha;
2846 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2847 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2849 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2850 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2855 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2858 cur_x += width_of_s;
2861 memcpy(tmpstring1, s, text_buffer_size);
2864 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2872 int last_special_needed = -1;
2873 int orig_special_index = special_index;
2876 if (output_methods & TO_X) {
2877 font_h = font_height();
2878 cur_y += font_ascent();
2880 cur_x = text_start_x;
2884 if (*p == SECRIT_MULTILINE_CHAR) {
2885 /* special newline marker for multiline objects */
2890 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2895 /* draw string before special, unless we're dealing multiline
2897 if (last_special_applied > -1) {
2898 special_index = last_special_applied;
2906 switch (specials[special_index].type) {
2908 case HORIZONTAL_LINE:
2910 int h = specials[special_index].height;
2911 int mid = font_ascent() / 2;
2913 w = text_start_x + text_width - cur_x;
2915 XSetLineAttributes(display, window.gc, h, LineSolid,
2916 CapButt, JoinMiter);
2917 XDrawLine(display, window.drawable, window.gc, cur_x,
2918 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2924 int h = specials[special_index].height;
2925 int tmp_s = specials[special_index].arg;
2926 int mid = font_ascent() / 2;
2927 char ss[2] = { tmp_s, tmp_s };
2929 w = text_start_x + text_width - cur_x - 1;
2930 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2931 CapButt, JoinMiter);
2932 XSetDashes(display, window.gc, 0, ss, 2);
2933 XDrawLine(display, window.drawable, window.gc, cur_x,
2934 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2940 int h, bar_usage, by;
2941 if (cur_x - text_start_x > maximum_width
2942 && maximum_width > 0) {
2945 h = specials[special_index].height;
2946 bar_usage = specials[special_index].arg;
2947 by = cur_y - (font_ascent() / 2) - 1;
2952 w = specials[special_index].width;
2954 w = text_start_x + text_width - cur_x - 1;
2960 XSetLineAttributes(display, window.gc, 1, LineSolid,
2961 CapButt, JoinMiter);
2963 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2965 XFillRectangle(display, window.drawable, window.gc, cur_x,
2966 by, w * bar_usage / 255, h);
2974 case GAUGE: /* new GAUGE */
2977 unsigned long last_colour = current_color;
2979 float angle, px, py;
2983 if (cur_x - text_start_x > maximum_width
2984 && maximum_width > 0) {
2988 h = specials[special_index].height;
2989 by = cur_y - (font_ascent() / 2) - 1;
2994 w = specials[special_index].width;
2996 w = text_start_x + text_width - cur_x - 1;
3002 XSetLineAttributes(display, window.gc, 1, LineSolid,
3003 CapButt, JoinMiter);
3005 XDrawArc(display, window.drawable, window.gc,
3006 cur_x, by, w, h * 2, 0, 180*64);
3009 usage = specials[special_index].arg;
3010 angle = (M_PI)*(float)(usage)/255.;
3011 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3012 py = (float)(by+(h))-(float)(h)*sin(angle);
3014 XDrawLine(display, window.drawable, window.gc,
3015 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3023 set_foreground_color(last_colour);
3031 int h, by, i = 0, j = 0;
3033 unsigned long last_colour = current_color;
3034 unsigned long *tmpcolour = 0;
3035 if (cur_x - text_start_x > maximum_width
3036 && maximum_width > 0) {
3039 h = specials[special_index].height;
3040 by = cur_y - (font_ascent() / 2) - 1;
3045 w = specials[special_index].width;
3047 w = text_start_x + text_width - cur_x - 1;
3052 if (draw_graph_borders) {
3053 XSetLineAttributes(display, window.gc, 1, LineSolid,
3054 CapButt, JoinMiter);
3055 XDrawRectangle(display, window.drawable, window.gc,
3058 XSetLineAttributes(display, window.gc, 1, LineSolid,
3059 CapButt, JoinMiter);
3061 if (specials[special_index].last_colour != 0
3062 || specials[special_index].first_colour != 0) {
3063 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3066 for (i = w - 2; i > -1; i--) {
3067 if (specials[special_index].last_colour != 0
3068 || specials[special_index].first_colour != 0) {
3069 if (specials[special_index].tempgrad) {
3072 (int)((float)(w - 2) - specials[special_index].graph[j] *
3073 (w - 2) / (float)specials[special_index].graph_scale)
3077 (int)((float)(w - 2) - specials[special_index].graph[j] *
3078 (w - 2) / (float)specials[special_index].graph_scale)
3081 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3083 (int)((float)(w - 2) - specials[special_index].graph[j] *
3084 (w - 2) / (float)specials[special_index].graph_scale)
3088 #endif /* DEBUG_lol */
3089 XSetForeground(display, window.gc, tmpcolour[
3090 (int)((float)(w - 2) - specials[special_index].graph[j] *
3091 (w - 2) / (float)specials[special_index].graph_scale)
3094 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3097 /* this is mugfugly, but it works */
3098 XDrawLine(display, window.drawable, window.gc,
3099 cur_x + i + 1, by + h, cur_x + i + 1,
3100 round_to_int((double)by + h - specials[special_index].graph[j] *
3101 (h - 1) / specials[special_index].graph_scale));
3102 if ((w - i) / ((float) (w - 2) /
3103 (specials[special_index].graph_width)) > j
3104 && j < MAX_GRAPH_DEPTH - 3) {
3108 if (tmpcolour) free(tmpcolour);
3113 /* if (draw_mode == BG) {
3114 set_foreground_color(default_bg_color);
3115 } else if (draw_mode == OUTLINE) {
3116 set_foreground_color(default_out_color);
3118 set_foreground_color(default_fg_color);
3120 if (show_graph_range) {
3123 unsigned short int seconds = update_interval * w;
3129 unsigned short int timeunits;
3131 timeunits = seconds / 86400; seconds %= 86400;
3132 if (timeunits > 0) {
3133 asprintf(&tmp_day_str, "%dd", timeunits);
3135 tmp_day_str = strdup("");
3137 timeunits = seconds / 3600; seconds %= 3600;
3138 if (timeunits > 0) {
3139 asprintf(&tmp_hour_str, "%dh", timeunits);
3141 tmp_hour_str = strdup("");
3143 timeunits = seconds / 60; seconds %= 60;
3144 if (timeunits > 0) {
3145 asprintf(&tmp_min_str, "%dm", timeunits);
3147 tmp_min_str = strdup("");
3150 asprintf(&tmp_sec_str, "%ds", seconds);
3152 tmp_sec_str = strdup("");
3154 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3155 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3157 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3159 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3160 cur_y += font_h / 2;
3161 draw_string(tmp_str);
3167 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3171 cur_x += font_ascent() / 2;
3172 cur_y += font_h / 2;
3174 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3176 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3177 draw_string(tmp_str);
3183 set_foreground_color(last_colour);
3189 int old = font_ascent();
3191 cur_y -= font_ascent();
3192 selected_font = specials[special_index].font_added;
3194 if (cur_y + font_ascent() < cur_y + old) {
3197 cur_y += font_ascent();
3199 font_h = font_height();
3204 if (draw_mode == FG) {
3205 set_foreground_color(specials[special_index].arg);
3211 if (draw_mode == BG) {
3212 set_foreground_color(specials[special_index].arg);
3217 if (draw_mode == OUTLINE) {
3218 set_foreground_color(specials[special_index].arg);
3223 w += specials[special_index].arg;
3224 last_special_needed = special_index;
3228 cur_y += specials[special_index].arg;
3232 if (specials[special_index].arg >= 0) {
3233 cur_x = (int) specials[special_index].arg;
3235 last_special_needed = special_index;
3240 int start = specials[special_index].arg;
3241 int step = specials[special_index].width;
3243 if (!step || step < 0) {
3246 w = step - (cur_x - text_start_x - start) % step;
3247 last_special_needed = special_index;
3253 /* TODO: add back in "+ window.border_inner_margin" to the end of
3255 int pos_x = text_start_x + text_width -
3256 get_string_width_special(s, special_index);
3258 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3259 "get_string_width(p) %i gap_x %i "
3260 "specials[special_index].arg %i window.border_inner_margin %i "
3261 "window.border_width %i\n", pos_x, text_start_x, text_width,
3262 cur_x, get_string_width_special(s), gap_x,
3263 specials[special_index].arg, window.border_inner_margin,
3264 window.border_width); */
3265 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3266 cur_x = pos_x - specials[special_index].arg;
3268 last_special_needed = special_index;
3274 int pos_x = (text_width) / 2 - get_string_width_special(s,
3275 special_index) / 2 - (cur_x -
3277 /* int pos_x = text_start_x + text_width / 2 -
3278 get_string_width_special(s) / 2; */
3280 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3281 "get_string_width(p) %i gap_x %i "
3282 "specials[special_index].arg %i\n", pos_x, text_start_x,
3283 text_width, cur_x, get_string_width(s), gap_x,
3284 specials[special_index].arg); */
3285 if (pos_x > specials[special_index].arg) {
3286 w = pos_x - specials[special_index].arg;
3288 last_special_needed = special_index;
3298 if (special_index != last_special_applied) {
3301 special_index = orig_special_index;
3302 last_special_applied = -1;
3313 if (output_methods & TO_NCURSES) {
3316 #endif /* NCURSES */
3318 if (output_methods & TO_X)
3319 cur_y += font_descent();
3321 if (recurse && *recurse) {
3322 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3323 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3325 return special_index;
3328 static int draw_line(char *s, int special_index)
3331 if (output_methods & TO_X) {
3332 return draw_each_line_inner(s, special_index, -1);
3336 if (output_methods & TO_NCURSES) {
3337 return draw_each_line_inner(s, special_index, -1);
3339 #endif /* NCURSES */
3341 UNUSED(special_index);
3345 static void draw_text(void)
3349 llua_draw_pre_hook();
3350 #endif /* HAVE_LUA */
3351 if (output_methods & TO_X) {
3352 cur_y = text_start_y;
3355 if (draw_borders && window.border_width > 0) {
3356 if (stippled_borders) {
3357 char ss[2] = { stippled_borders, stippled_borders };
3358 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3359 CapButt, JoinMiter);
3360 XSetDashes(display, window.gc, 0, ss, 2);
3362 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3363 CapButt, JoinMiter);
3366 XDrawRectangle(display, window.drawable, window.gc,
3367 text_start_x - window.border_inner_margin - window.border_width,
3368 text_start_y - window.border_inner_margin - window.border_width,
3369 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3370 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3378 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3379 attron(COLOR_PAIR(COLOR_WHITE));
3380 #endif /* NCURSES */
3381 for_each_line(text_buffer, draw_line);
3382 #if defined(HAVE_LUA) && defined(X11)
3383 llua_draw_post_hook();
3384 #endif /* HAVE_LUA */
3387 static void draw_stuff(void)
3390 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3392 if (overwrite_file) {
3393 overwrite_fpointer = fopen(overwrite_file, "w");
3394 if(!overwrite_fpointer)
3395 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3398 append_fpointer = fopen(append_file, "a");
3399 if(!append_fpointer)
3400 NORM_ERR("Can't append '%s' anymore", append_file);
3403 if (output_methods & TO_X) {
3405 if (draw_shades && !draw_outline) {
3408 set_foreground_color(default_bg_color);
3419 for (i = -1; i < 2; i++) {
3420 for (j = -1; j < 2; j++) {
3421 if (i == 0 && j == 0) {
3426 set_foreground_color(default_out_color);
3427 draw_mode = OUTLINE;
3435 set_foreground_color(default_fg_color);
3440 #if defined(X11) && defined(HAVE_XDBE)
3441 if (output_methods & TO_X) {
3442 xdbe_swap_buffers();
3444 #endif /* X11 && HAVE_XDBE */
3445 if(overwrite_fpointer) {
3446 fclose(overwrite_fpointer);
3447 overwrite_fpointer = 0;
3449 if(append_fpointer) {
3450 fclose(append_fpointer);
3451 append_fpointer = 0;
3456 static void clear_text(int exposures)
3460 /* The swap action is XdbeBackground, which clears */
3464 if (display && window.window) { // make sure these are !null
3465 /* there is some extra space for borders and outlines */
3466 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3467 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3468 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3469 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3474 static int need_to_update;
3476 /* update_text() generates new text and clears old text area */
3477 static void update_text(void)
3484 if (output_methods & TO_X)
3489 llua_update_info(&info, update_interval);
3490 #endif /* HAVE_LUA */
3493 #ifdef HAVE_SYS_INOTIFY_H
3497 static void main_loop(void)
3500 #ifdef SIGNAL_BLOCKING
3501 sigset_t newmask, oldmask;
3504 #ifdef HAVE_SYS_INOTIFY_H
3505 int inotify_config_wd = -1;
3506 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3507 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3508 char inotify_buff[INOTIFY_BUF_LEN];
3509 #endif /* HAVE_SYS_INOTIFY_H */
3512 #ifdef SIGNAL_BLOCKING
3513 sigemptyset(&newmask);
3514 sigaddset(&newmask, SIGINT);
3515 sigaddset(&newmask, SIGTERM);
3516 sigaddset(&newmask, SIGUSR1);
3519 last_update_time = 0.0;
3520 next_update_time = get_time();
3522 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3523 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3524 char buf[max_user_text];
3526 get_battery_short_status(buf, max_user_text, "BAT0");
3528 update_interval = update_interval_bat;
3530 update_interval = update_interval_old;
3535 #ifdef SIGNAL_BLOCKING
3536 /* block signals. we will inspect for pending signals later */
3537 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3538 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3543 if (output_methods & TO_X) {
3546 /* wait for X event or timeout */
3548 if (!XPending(display)) {
3552 t = next_update_time - get_time();
3556 } else if (t > update_interval) {
3557 t = update_interval;
3560 tv.tv_sec = (long) t;
3561 tv.tv_usec = (long) (t * 1000000) % 1000000;
3563 FD_SET(ConnectionNumber(display), &fdsr);
3565 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3567 if (errno != EINTR) {
3568 NORM_ERR("can't select(): %s", strerror(errno));
3578 if (need_to_update) {
3580 int wx = window.x, wy = window.y;
3590 /* resize window if it isn't right size */
3592 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3593 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3594 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3595 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3596 draw_stuff(); /* redraw everything in our newly sized window */
3597 XResizeWindow(display, window.window, window.width,
3598 window.height); /* resize window */
3599 set_transparent_background(window.window);
3602 xdbe_swap_buffers();
3607 /* update lua window globals */
3608 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3609 #endif /* HAVE_LUA */
3612 /* move window if it isn't in right position */
3613 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3614 XMoveWindow(display, window.window, window.x, window.y);
3619 if (changed && window.type == TYPE_PANEL) {
3622 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3625 switch (text_alignment) {
3652 set_struts(sidenum);
3663 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3664 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3665 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3666 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3667 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3672 /* handle X events */
3673 while (XPending(display)) {
3676 XNextEvent(display, &ev);
3683 r.width = ev.xexpose.width;
3684 r.height = ev.xexpose.height;
3685 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3689 case PropertyNotify:
3691 if ( ev.xproperty.state == PropertyNewValue ) {
3692 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3698 case ReparentNotify:
3699 /* set background to ParentRelative for all parents */
3701 set_transparent_background(window.window);
3705 case ConfigureNotify:
3707 /* if window size isn't what expected, set fixed size */
3708 if (ev.xconfigure.width != window.width
3709 || ev.xconfigure.height != window.height) {
3710 if (window.width != 0 && window.height != 0) {
3714 /* clear old stuff before screwing up
3719 XWindowAttributes attrs;
3720 if (XGetWindowAttributes(display,
3721 window.window, &attrs)) {
3722 window.width = attrs.width;
3723 window.height = attrs.height;
3727 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3728 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3729 if (text_width > maximum_width
3730 && maximum_width > 0) {
3731 text_width = maximum_width;
3735 /* if position isn't what expected, set fixed pos
3736 * total_updates avoids setting fixed_pos when window
3737 * is set to weird locations when started */
3738 /* // this is broken
3739 if (total_updates >= 2 && !fixed_pos
3740 && (window.x != ev.xconfigure.x
3741 || window.y != ev.xconfigure.y)
3742 && (ev.xconfigure.x != 0
3743 || ev.xconfigure.y != 0)) {
3751 /* if an ordinary window with decorations */
3752 if ((window.type == TYPE_NORMAL &&
3753 (!TEST_HINT(window.hints,
3754 HINT_UNDECORATED))) ||
3755 window.type == TYPE_DESKTOP) {
3756 /* allow conky to hold input focus. */
3759 /* forward the click to the desktop window */
3760 XUngrabPointer(display, ev.xbutton.time);
3761 ev.xbutton.window = window.desktop;
3762 ev.xbutton.x = ev.xbutton.x_root;
3763 ev.xbutton.y = ev.xbutton.y_root;
3764 XSendEvent(display, ev.xbutton.window, False,
3765 ButtonPressMask, &ev);
3766 XSetInputFocus(display, ev.xbutton.window,
3767 RevertToParent, ev.xbutton.time);
3774 /* if an ordinary window with decorations */
3775 if ((window.type == TYPE_NORMAL)
3776 && (!TEST_HINT(window.hints,
3777 HINT_UNDECORATED))) {
3778 /* allow conky to hold input focus. */
3781 /* forward the release to the desktop window */
3782 ev.xbutton.window = window.desktop;
3783 ev.xbutton.x = ev.xbutton.x_root;
3784 ev.xbutton.y = ev.xbutton.y_root;
3785 XSendEvent(display, ev.xbutton.window, False,
3786 ButtonReleaseMask, &ev);
3795 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3796 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3798 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3799 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3801 #endif /* HAVE_XDAMAGE */
3807 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3808 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3809 #endif /* HAVE_XDAMAGE */
3811 /* XDBE doesn't seem to provide a way to clear the back buffer
3812 * without interfering with the front buffer, other than passing
3813 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3814 * using XDBE, we need to redraw the text even if it wasn't part of
3815 * the exposed area. OTOH, if we're not going to call draw_stuff at
3816 * all, then no swap happens and we can safely do nothing. */
3818 if (!XEmptyRegion(x11_stuff.region)) {
3823 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3824 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3825 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3826 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3827 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3830 XSetRegion(display, window.gc, x11_stuff.region);
3833 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3837 XDestroyRegion(x11_stuff.region);
3838 x11_stuff.region = XCreateRegion();
3842 t = (next_update_time - get_time()) * 1000000;
3843 if(t > 0) usleep((useconds_t)t);
3847 if(output_methods & TO_NCURSES) {
3856 #ifdef SIGNAL_BLOCKING
3857 /* unblock signals of interest and let handler fly */
3858 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3859 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3863 switch (g_signal_pending) {
3866 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3871 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3874 if (output_methods & TO_X) {
3875 XDestroyRegion(x11_stuff.region);
3876 x11_stuff.region = NULL;
3878 XDamageDestroy(display, x11_stuff.damage);
3879 XFixesDestroyRegion(display, x11_stuff.region2);
3880 XFixesDestroyRegion(display, x11_stuff.part);
3881 #endif /* HAVE_XDAMAGE */
3887 if(overwrite_file) {
3888 free(overwrite_file);
3897 /* Reaching here means someone set a signal
3898 * (SIGXXXX, signal_handler), but didn't write any code
3900 * If you don't want to handle a signal, don't set a handler on
3901 * it in the first place. */
3902 if (g_signal_pending) {
3903 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3907 #ifdef HAVE_SYS_INOTIFY_H
3908 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3909 inotify_config_wd = inotify_add_watch(inotify_fd,
3913 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3914 int len = 0, idx = 0;
3916 struct timeval time_to_wait;
3918 FD_ZERO(&descriptors);
3919 FD_SET(inotify_fd, &descriptors);
3921 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3923 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3924 if (FD_ISSET(inotify_fd, &descriptors)) {
3925 /* process inotify events */
3926 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3927 while (len > 0 && idx < len) {
3928 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3929 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3930 /* current_config should be reloaded */
3931 NORM_ERR("'%s' modified, reloading...", current_config);
3933 if (ev->mask & IN_IGNORED) {
3934 /* for some reason we get IN_IGNORED here
3935 * sometimes, so we need to re-add the watch */
3936 inotify_config_wd = inotify_add_watch(inotify_fd,
3943 llua_inotify_query(ev->wd, ev->mask);
3945 #endif /* HAVE_LUA */
3946 idx += INOTIFY_EVENT_SIZE + ev->len;
3950 #endif /* HAVE_SYS_INOTIFY_H */
3953 llua_update_info(&info, update_interval);
3954 #endif /* HAVE_LUA */
3955 g_signal_pending = 0;
3957 clean_up(NULL, NULL);
3959 #ifdef HAVE_SYS_INOTIFY_H
3960 if (inotify_fd != -1) {
3961 inotify_rm_watch(inotify_fd, inotify_config_wd);
3963 inotify_fd = inotify_config_wd = 0;
3965 #endif /* HAVE_SYS_INOTIFY_H */
3969 static void load_config_file_x11(const char *);
3971 void initialisation(int argc, char** argv);
3973 /* reload the config file */
3974 static void reload_config(void)
3976 char *current_config_copy = strdup(current_config);
3977 clean_up(NULL, NULL);
3978 current_config = current_config_copy;
3979 initialisation(argc_copy, argv_copy);
3982 void clean_up(void *memtofree1, void* memtofree2)
3987 if(output_methods & TO_NCURSES) {
3991 conftree_empty(currentconffile);
3992 currentconffile = NULL;
3999 timed_thread_destroy_registered_threads();
4001 if (info.cpu_usage) {
4002 free(info.cpu_usage);
4003 info.cpu_usage = NULL;
4006 if (x_initialised == YES) {
4007 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4008 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4009 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4010 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4013 if(x11_stuff.region) {
4014 XDestroyRegion(x11_stuff.region);
4015 x11_stuff.region = NULL;
4017 XCloseDisplay(display);
4019 if(info.x11.desktop.all_names) {
4020 free(info.x11.desktop.all_names);
4021 info.x11.desktop.all_names = NULL;
4023 if (info.x11.desktop.name) {
4024 free(info.x11.desktop.name);
4025 info.x11.desktop.name = NULL;
4029 free(fonts); //in set_default_configurations a font is set but not loaded
4035 free_update_callbacks();
4039 free_text_objects(&global_root_object, 0);
4058 free(current_config);
4061 #ifdef TCP_PORT_MONITOR
4062 tcp_portmon_clear();
4071 weather_free_info();
4074 llua_shutdown_hook();
4076 #endif /* HAVE_LUA */
4078 if (output_methods & TO_X)
4086 for (i = 0; i < special_count; i++) {
4087 if (specials[i].type == GRAPH) {
4088 free(specials[i].graph);
4096 clear_diskio_stats();
4097 if(global_cpu != NULL) {
4103 static int string_to_bool(const char *s)
4106 // Assumes an option without a true/false means true
4108 } else if (strcasecmp(s, "yes") == EQUAL) {
4110 } else if (strcasecmp(s, "true") == EQUAL) {
4112 } else if (strcasecmp(s, "1") == EQUAL) {
4119 static enum alignment string_to_alignment(const char *s)
4121 if (strcasecmp(s, "top_left") == EQUAL) {
4123 } else if (strcasecmp(s, "top_right") == EQUAL) {
4125 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4127 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4129 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4130 return BOTTOM_RIGHT;
4131 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4132 return BOTTOM_MIDDLE;
4133 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4135 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4136 return MIDDLE_RIGHT;
4137 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4138 return MIDDLE_MIDDLE;
4139 } else if (strcasecmp(s, "tl") == EQUAL) {
4141 } else if (strcasecmp(s, "tr") == EQUAL) {
4143 } else if (strcasecmp(s, "tm") == EQUAL) {
4145 } else if (strcasecmp(s, "bl") == EQUAL) {
4147 } else if (strcasecmp(s, "br") == EQUAL) {
4148 return BOTTOM_RIGHT;
4149 } else if (strcasecmp(s, "bm") == EQUAL) {
4150 return BOTTOM_MIDDLE;
4151 } else if (strcasecmp(s, "ml") == EQUAL) {
4153 } else if (strcasecmp(s, "mr") == EQUAL) {
4154 return MIDDLE_RIGHT;
4155 } else if (strcasecmp(s, "mm") == EQUAL) {
4156 return MIDDLE_MIDDLE;
4157 } else if (strcasecmp(s, "none") == EQUAL) {
4165 static void set_default_configurations_for_x(void)
4167 default_fg_color = WhitePixel(display, screen);
4168 default_bg_color = BlackPixel(display, screen);
4169 default_out_color = BlackPixel(display, screen);
4170 color0 = default_fg_color;
4171 color1 = default_fg_color;
4172 color2 = default_fg_color;
4173 color3 = default_fg_color;
4174 color4 = default_fg_color;
4175 color5 = default_fg_color;
4176 color6 = default_fg_color;
4177 color7 = default_fg_color;
4178 color8 = default_fg_color;
4179 color9 = default_fg_color;
4180 current_text_color = default_fg_color;
4184 static void set_default_configurations(void)
4191 fork_to_background = 0;
4192 total_run_times = 0;
4193 info.cpu_avg_samples = 2;
4194 info.net_avg_samples = 2;
4195 info.diskio_avg_samples = 2;
4200 format_human_readable = 1;
4210 mpd_env_host = getenv("MPD_HOST");
4211 mpd_env_port = getenv("MPD_PORT");
4213 if (!mpd_env_host || !strlen(mpd_env_host)) {
4214 mpd_set_host("localhost");
4216 /* MPD_HOST environment variable is set */
4217 char *mpd_hostpart = strchr(mpd_env_host, '@');
4218 if (!mpd_hostpart) {
4219 mpd_set_host(mpd_env_host);
4221 /* MPD_HOST contains a password */
4222 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4223 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4225 if (!strlen(mpd_hostpart + 1)) {
4226 mpd_set_host("localhost");
4228 mpd_set_host(mpd_hostpart + 1);
4231 mpd_set_password(mpd_password, 1);
4236 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4237 /* failed to set port from environment variable */
4238 mpd_set_port("6600");
4242 info.xmms2.artist = NULL;
4243 info.xmms2.album = NULL;
4244 info.xmms2.title = NULL;
4245 info.xmms2.genre = NULL;
4246 info.xmms2.comment = NULL;
4247 info.xmms2.url = NULL;
4248 info.xmms2.status = NULL;
4249 info.xmms2.playlist = NULL;
4251 use_spacer = NO_SPACER;
4253 output_methods = TO_X;
4255 output_methods = TO_STDOUT;
4258 show_graph_scale = 0;
4259 show_graph_range = 0;
4262 draw_graph_borders = 1;
4264 set_first_font("6x10");
4272 window.type = TYPE_NORMAL;
4274 strcpy(window.class_name, PACKAGE_NAME);
4275 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4277 stippled_borders = 0;
4278 window.border_inner_margin = 3;
4279 window.border_outer_margin = 1;
4280 window.border_width = 1;
4281 text_alignment = BOTTOM_LEFT;
4282 info.x11.monitor.number = 1;
4283 info.x11.monitor.current = 0;
4284 info.x11.desktop.current = 1;
4285 info.x11.desktop.number = 1;
4286 info.x11.desktop.nitems = 0;
4287 info.x11.desktop.all_names = NULL;
4288 info.x11.desktop.name = NULL;
4293 free(current_mail_spool);
4297 variable_substitute(MAIL_FILE, buf, 256);
4298 if (buf[0] != '\0') {
4299 current_mail_spool = strndup(buf, text_buffer_size);
4304 set_update_interval(3);
4305 update_interval_bat = NOBATTERY;
4306 info.music_player_interval = 1.0;
4307 stuff_in_uppercase = 0;
4308 info.users.number = 1;
4310 set_times_in_seconds(0);
4312 #ifdef TCP_PORT_MONITOR
4313 /* set default connection limit */
4314 tcp_portmon_set_max_connections(0);
4318 /* returns 1 if you can overwrite or create the file at 'path' */
4319 static _Bool overwrite_works(const char *path)
4323 if (!(filepointer = fopen(path, "w")))
4325 fclose(filepointer);
4329 /* returns 1 if you can append or create the file at 'path' */
4330 static _Bool append_works(const char *path)
4334 if (!(filepointer = fopen(path, "a")))
4336 fclose(filepointer);
4342 /* WARNING, this type not in Xlib spec */
4343 int x11_error_handler(Display *d, XErrorEvent *err)
4344 __attribute__((noreturn));
4345 int x11_error_handler(Display *d, XErrorEvent *err)
4347 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",
4349 (long unsigned)err->display,
4350 (long)err->resourceid,
4360 int x11_ioerror_handler(Display *d)
4361 __attribute__((noreturn));
4362 int x11_ioerror_handler(Display *d)
4364 NORM_ERR("X Error: Display %lx\n",
4371 static void X11_initialisation(void)
4373 if (x_initialised == YES) return;
4374 output_methods |= TO_X;
4376 set_default_configurations_for_x();
4377 x_initialised = YES;
4380 /* WARNING, this type not in Xlib spec */
4381 XSetErrorHandler(&x11_error_handler);
4382 XSetIOErrorHandler(&x11_ioerror_handler);
4386 static char **xargv = 0;
4387 static int xargc = 0;
4389 static void X11_create_window(void)
4391 if (output_methods & TO_X) {
4393 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4394 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4396 #else /* OWN_WINDOW */
4397 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4398 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4400 #endif /* OWN_WINDOW */
4404 update_text_area(); /* to position text/window on screen */
4407 if (own_window && !fixed_pos) {
4408 XMoveWindow(display, window.window, window.x, window.y);
4411 set_transparent_background(window.window);
4419 x11_stuff.region = XCreateRegion();
4421 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4422 NORM_ERR("Xdamage extension unavailable");
4424 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4425 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4426 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4427 #endif /* HAVE_XDAMAGE */
4430 update_text_area(); /* to get initial size of the window */
4433 /* setup lua window globals */
4434 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4435 #endif /* HAVE_LUA */
4439 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4440 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4441 #define CONF2(a) if (strcasecmp(name, a) == 0)
4442 #define CONF(a) else CONF2(a)
4443 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4444 || strcasecmp(name, b) == 0)
4445 #define CONF_CONTINUE 1
4446 #define CONF_BREAK 2
4447 #define CONF_BUFF_SIZE 512
4449 static FILE *open_config_file(const char *f)
4451 #ifdef CONFIG_OUTPUT
4452 if (!strcmp(f, "==builtin==")) {
4453 return conf_cookie_open();
4455 #endif /* CONFIG_OUTPUT */
4456 return fopen(f, "r");
4459 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4463 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4466 remove_comments(buf);
4471 while (*p && isspace((int) *p)) {
4475 return CONF_CONTINUE; /* empty line */
4482 while (*p2 && !isspace((int) *p2)) {
4486 *p2 = '\0'; /* break at name's end */
4493 while (*p && isspace((int) *p)) {
4499 p2 = *value + strlen(*value);
4500 while (isspace((int) *(p2 - 1))) {
4509 char load_config_file(const char *f)
4514 fp = open_config_file(f);
4518 DBGP("reading contents from config file '%s'", f);
4521 char buff[CONF_BUFF_SIZE], *name, *value;
4522 int ret = do_config_step(&line, fp, buff, &name, &value);
4523 if (ret == CONF_BREAK) {
4525 } else if (ret == CONF_CONTINUE) {
4531 /* don't listen if X is already initialised or
4532 * if we already know we don't want it */
4533 if(x_initialised != YES) {
4534 if (string_to_bool(value)) {
4535 output_methods &= TO_X;
4537 output_methods &= ~TO_X;
4538 x_initialised = NEVER;
4543 if (!value || x_initialised == YES) {
4548 disp = strdup(value);
4553 if (window.type == TYPE_DOCK)
4556 #endif /*OWN_WINDOW */
4558 int a = string_to_alignment(value);
4569 CONF("background") {
4570 fork_to_background = string_to_bool(value);
4573 CONF2("background") {
4574 fork_to_background = string_to_bool(value);
4578 CONF("show_graph_scale") {
4579 show_graph_scale = string_to_bool(value);
4581 CONF("show_graph_range") {
4582 show_graph_range = string_to_bool(value);
4584 CONF("border_inner_margin") {
4586 window.border_inner_margin = strtol(value, 0, 0);
4587 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4592 CONF("border_outer_margin") {
4594 window.border_outer_margin = strtol(value, 0, 0);
4595 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4600 CONF("border_width") {
4602 window.border_width = strtol(value, 0, 0);
4603 if (window.border_width < 0) window.border_width = 0;
4609 #define TEMPLATE_CONF(n) \
4610 CONF("template"#n) { \
4611 if (set_template(n, value)) \
4626 parse_global_imap_mail_args(value);
4633 parse_global_pop3_mail_args(value);
4638 CONF("default_bar_size") {
4641 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4648 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4652 CONF("default_graph_size") {
4655 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4662 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4665 CONF("default_gauge_size") {
4668 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4675 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4682 mpd_set_host(value);
4688 if (value && mpd_set_port(value)) {
4692 CONF("mpd_password") {
4694 mpd_set_password(value, 0);
4700 CONF("music_player_interval") {
4702 info.music_player_interval = strtod(value, 0);
4708 CONF("sensor_device") {
4710 sensor_device = strtol(value, 0, 0);
4716 CONF("cpu_avg_samples") {
4718 cpu_avg_samples = strtol(value, 0, 0);
4719 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4722 info.cpu_avg_samples = cpu_avg_samples;
4728 CONF("net_avg_samples") {
4730 net_avg_samples = strtol(value, 0, 0);
4731 if (net_avg_samples < 1 || net_avg_samples > 14) {
4734 info.net_avg_samples = net_avg_samples;
4740 CONF("diskio_avg_samples") {
4742 diskio_avg_samples = strtol(value, 0, 0);
4743 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4746 info.diskio_avg_samples = diskio_avg_samples;
4754 CONF("double_buffer") {
4755 use_xdbe = string_to_bool(value);
4759 CONF("override_utf8_locale") {
4760 utf8_mode = string_to_bool(value);
4762 CONF("draw_borders") {
4763 draw_borders = string_to_bool(value);
4765 CONF("draw_graph_borders") {
4766 draw_graph_borders = string_to_bool(value);
4768 CONF("draw_shades") {
4769 draw_shades = string_to_bool(value);
4771 CONF("draw_outline") {
4772 draw_outline = string_to_bool(value);
4775 CONF("times_in_seconds") {
4776 set_times_in_seconds(string_to_bool(value));
4778 CONF("out_to_console") {
4779 if(string_to_bool(value)) {
4780 output_methods |= TO_STDOUT;
4782 output_methods &= ~TO_STDOUT;
4785 CONF("extra_newline") {
4786 extra_newline = string_to_bool(value);
4788 CONF("out_to_stderr") {
4789 if(string_to_bool(value))
4790 output_methods |= TO_STDERR;
4793 CONF("out_to_ncurses") {
4794 if(string_to_bool(value)) {
4797 output_methods |= TO_NCURSES;
4801 CONF("overwrite_file") {
4802 if(overwrite_file) {
4803 free(overwrite_file);
4806 if(overwrite_works(value)) {
4807 overwrite_file = strdup(value);
4808 output_methods |= OVERWRITE_FILE;
4810 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4812 CONF("append_file") {
4817 if(append_works(value)) {
4818 append_file = strdup(value);
4819 output_methods |= APPEND_FILE;
4821 NORM_ERR("append_file won't be able to create/append '%s'", value);
4823 CONF("use_spacer") {
4825 if (strcasecmp(value, "left") == EQUAL) {
4826 use_spacer = LEFT_SPACER;
4827 } else if (strcasecmp(value, "right") == EQUAL) {
4828 use_spacer = RIGHT_SPACER;
4829 } else if (strcasecmp(value, "none") == EQUAL) {
4830 use_spacer = NO_SPACER;
4832 use_spacer = string_to_bool(value);
4833 NORM_ERR("use_spacer should have an argument of left, right, or"
4834 " none. '%s' seems to be some form of '%s', so"
4835 " defaulting to %s.", value,
4836 use_spacer ? "true" : "false",
4837 use_spacer ? "right" : "none");
4839 use_spacer = RIGHT_SPACER;
4841 use_spacer = NO_SPACER;
4845 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4846 use_spacer = RIGHT_SPACER;
4852 use_xft = string_to_bool(value);
4856 set_first_font(value);
4860 if (value && font_count >= 0) {
4861 fonts[0].font_alpha = atof(value) * 65535.0;
4868 if (string_to_bool(value)) {
4869 NORM_ERR("Xft not enabled at compile time");
4873 /* xftfont silently ignored when no Xft */
4876 /* xftalpha is silently ignored when no Xft */
4881 set_first_font(value);
4889 gap_x = atoi(value);
4896 gap_y = atoi(value);
4902 CONF("mail_spool") {
4906 variable_substitute(value, buffer, 256);
4908 if (buffer[0] != '\0') {
4909 if (current_mail_spool) {
4910 free(current_mail_spool);
4912 current_mail_spool = strndup(buffer, text_buffer_size);
4919 CONF("minimum_size") {
4921 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4923 if (sscanf(value, "%d", &minimum_width) != 1) {
4931 CONF("maximum_width") {
4933 if (sscanf(value, "%d", &maximum_width) != 1) {
4941 CONF("no_buffers") {
4942 no_buffers = string_to_bool(value);
4944 CONF("top_name_width") {
4945 if (set_top_name_width(value))
4948 CONF("top_cpu_separate") {
4949 cpu_separate = string_to_bool(value);
4951 CONF("short_units") {
4952 short_units = string_to_bool(value);
4954 CONF("format_human_readable") {
4955 format_human_readable = string_to_bool(value);
4958 CONF("hddtemp_host") {
4959 set_hddtemp_host(value);
4961 CONF("hddtemp_port") {
4962 set_hddtemp_port(value);
4964 #endif /* HDDTEMP */
4965 CONF("pad_percents") {
4966 pad_percents = atoi(value);
4970 CONF("own_window") {
4972 own_window = string_to_bool(value);
4975 CONF("own_window_class") {
4977 memset(window.class_name, 0, sizeof(window.class_name));
4978 strncpy(window.class_name, value,
4979 sizeof(window.class_name) - 1);
4982 CONF("own_window_title") {
4984 memset(window.title, 0, sizeof(window.title));
4985 strncpy(window.title, value, sizeof(window.title) - 1);
4988 CONF("own_window_transparent") {
4990 set_transparent = string_to_bool(value);
4993 CONF("own_window_hints") {
4995 char *p_hint, *p_save;
4996 char delim[] = ", ";
4998 /* tokenize the value into individual hints */
4999 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5001 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5002 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5003 SET_HINT(window.hints, HINT_UNDECORATED);
5004 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5005 SET_HINT(window.hints, HINT_BELOW);
5006 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5007 SET_HINT(window.hints, HINT_ABOVE);
5008 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5009 SET_HINT(window.hints, HINT_STICKY);
5010 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5011 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5012 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5013 SET_HINT(window.hints, HINT_SKIP_PAGER);
5018 p_hint = strtok_r(NULL, delim, &p_save);
5019 } while (p_hint != NULL);
5025 CONF("own_window_type") {
5027 if (strncmp(value, "normal", 6) == EQUAL) {
5028 window.type = TYPE_NORMAL;
5029 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5030 window.type = TYPE_DESKTOP;
5031 } else if (strncmp(value, "dock", 4) == EQUAL) {
5032 window.type = TYPE_DOCK;
5033 text_alignment = TOP_LEFT;
5034 } else if (strncmp(value, "panel", 5) == EQUAL) {
5035 window.type = TYPE_PANEL;
5036 } else if (strncmp(value, "override", 8) == EQUAL) {
5037 window.type = TYPE_OVERRIDE;
5046 CONF("stippled_borders") {
5048 stippled_borders = strtol(value, 0, 0);
5050 stippled_borders = 4;
5054 CONF("imlib_cache_size") {
5056 cimlib_set_cache_size(atoi(value));
5059 CONF("imlib_cache_flush_interval") {
5061 cimlib_set_cache_flush_interval(atoi(value));
5066 CONF("update_interval_on_battery") {
5068 update_interval_bat = strtod(value, 0);
5073 CONF("update_interval") {
5075 set_update_interval(strtod(value, 0));
5079 if (info.music_player_interval == 0) {
5080 // default to update_interval
5081 info.music_player_interval = update_interval;
5084 CONF("total_run_times") {
5086 total_run_times = strtod(value, 0);
5092 stuff_in_uppercase = string_to_bool(value);
5094 CONF("max_specials") {
5096 max_specials = atoi(value);
5101 CONF("max_user_text") {
5103 max_user_text = atoi(value);
5108 CONF("text_buffer_size") {
5110 text_buffer_size = atoi(value);
5111 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5112 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5113 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5121 if (output_methods & TO_X) {
5122 X11_initialisation();
5131 global_text = (char *) malloc(1);
5132 global_text[0] = '\0';
5135 unsigned int l = strlen(global_text);
5137 char buf[CONF_BUFF_SIZE];
5139 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5145 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5153 /* Check for continuation of \\-\n. */
5154 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5155 global_text[l-1] = '\0';
5159 global_text = (char *) realloc(global_text, l + bl + 1);
5160 strcat(global_text, buf);
5162 if (strlen(global_text) > max_user_text) {
5166 global_text_lines = line + 1;
5169 #ifdef TCP_PORT_MONITOR
5170 CONF("max_port_monitor_connections") {
5172 if (!value || (sscanf(value, "%d", &max) != 1)) {
5173 /* an error. use default, warn and continue. */
5174 tcp_portmon_set_max_connections(0);
5176 } else if (tcp_portmon_set_max_connections(max)) {
5177 /* max is < 0, default has been set*/
5182 CONF("if_up_strictness") {
5184 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5185 ifup_strictness = IFUP_UP;
5186 } else if (strcasecmp(value, "up") == EQUAL) {
5187 ifup_strictness = IFUP_UP;
5188 } else if (strcasecmp(value, "link") == EQUAL) {
5189 ifup_strictness = IFUP_LINK;
5190 } else if (strcasecmp(value, "address") == EQUAL) {
5191 ifup_strictness = IFUP_ADDR;
5193 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5194 ifup_strictness = IFUP_UP;
5198 CONF("temperature_unit") {
5200 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5201 } else if (set_temp_output_unit(value)) {
5202 NORM_ERR("temperature_unit: incorrect argument");
5209 char *ptr = strtok(value, " ");
5212 ptr = strtok(NULL, " ");
5219 CONF("lua_draw_hook_pre") {
5221 llua_set_draw_pre_hook(value);
5226 CONF("lua_draw_hook_post") {
5228 llua_set_draw_post_hook(value);
5233 CONF("lua_startup_hook") {
5235 llua_set_startup_hook(value);
5240 CONF("lua_shutdown_hook") {
5242 llua_set_shutdown_hook(value);
5248 #endif /* HAVE_LUA */
5260 CONF("default_color"){}
5261 CONF3("default_shade_color", "default_shadecolor"){}
5262 CONF3("default_outline_color", "default_outlinecolor") {}
5263 CONF("own_window_colour") {}
5266 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5272 if (info.music_player_interval == 0) {
5273 // default to update_interval
5274 info.music_player_interval = update_interval;
5276 if (!global_text) { // didn't supply any text
5277 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5279 if (!output_methods) {
5280 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5282 #if defined(NCURSES)
5284 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5285 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5286 output_methods &= ~TO_NCURSES;
5290 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5291 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5292 output_methods &= ~(TO_STDOUT | TO_STDERR);
5294 #endif /* NCURSES */
5299 static void load_config_file_x11(const char *f)
5304 fp = open_config_file(f);
5308 DBGP("reading contents from config file '%s'", f);
5311 char buff[CONF_BUFF_SIZE], *name, *value;
5312 int ret = do_config_step(&line, fp, buff, &name, &value);
5313 if (ret == CONF_BREAK) {
5315 } else if (ret == CONF_CONTINUE) {
5320 X11_initialisation();
5321 if (x_initialised == YES) {
5323 color0 = get_x11_color(value);
5330 X11_initialisation();
5331 if (x_initialised == YES) {
5333 color1 = get_x11_color(value);
5340 X11_initialisation();
5341 if (x_initialised == YES) {
5343 color2 = get_x11_color(value);
5350 X11_initialisation();
5351 if (x_initialised == YES) {
5353 color3 = get_x11_color(value);
5360 X11_initialisation();
5361 if (x_initialised == YES) {
5363 color4 = get_x11_color(value);
5370 X11_initialisation();
5371 if (x_initialised == YES) {
5373 color5 = get_x11_color(value);
5380 X11_initialisation();
5381 if (x_initialised == YES) {
5383 color6 = get_x11_color(value);
5390 X11_initialisation();
5391 if (x_initialised == YES) {
5393 color7 = get_x11_color(value);
5400 X11_initialisation();
5401 if (x_initialised == YES) {
5403 color8 = get_x11_color(value);
5410 X11_initialisation();
5411 if (x_initialised == YES) {
5413 color9 = get_x11_color(value);
5419 CONF("default_color") {
5420 X11_initialisation();
5421 if (x_initialised == YES) {
5423 default_fg_color = get_x11_color(value);
5429 CONF3("default_shade_color", "default_shadecolor") {
5430 X11_initialisation();
5431 if (x_initialised == YES) {
5433 default_bg_color = get_x11_color(value);
5439 CONF3("default_outline_color", "default_outlinecolor") {
5440 X11_initialisation();
5441 if (x_initialised == YES) {
5443 default_out_color = get_x11_color(value);
5450 CONF("own_window_colour") {
5451 X11_initialisation();
5452 if (x_initialised == YES) {
5454 background_colour = get_x11_color(value);
5456 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5457 "'#' for hex colours");
5463 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5464 if(output_methods & TO_X) {
5465 X11_initialisation();
5474 #undef CONF_CONTINUE
5475 #undef CONF_BUFF_SIZE
5483 static void print_help(const char *prog_name) {
5484 printf("Usage: %s [OPTION]...\n"
5485 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5486 "window. Command line options will override configurations defined in config\n"
5488 " -v, --version version\n"
5489 " -q, --quiet quiet mode\n"
5490 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5491 " -c, --config=FILE config file to load\n"
5492 #ifdef CONFIG_OUTPUT
5493 " -C, --print-config print the builtin default config to stdout\n"
5494 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5496 " -d, --daemonize daemonize, fork to background\n"
5497 " -h, --help help\n"
5499 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5500 " -f, --font=FONT font to use\n"
5501 " -X, --display=DISPLAY X11 display to use\n"
5503 " -o, --own-window create own window to draw\n"
5506 " -b, --double-buffer double buffer (prevents flickering)\n"
5508 " -w, --window-id=WIN_ID window id to draw\n"
5509 " -x X x position\n"
5510 " -y Y y position\n"
5512 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5513 " -u, --interval=SECS update interval\n"
5514 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5515 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5520 /* : means that character before that takes an argument */
5521 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5531 #ifdef CONFIG_OUTPUT
5536 static const struct option longopts[] = {
5537 { "help", 0, NULL, 'h' },
5538 { "version", 0, NULL, 'V' },
5539 { "debug", 0, NULL, 'D' },
5540 { "config", 1, NULL, 'c' },
5541 #ifdef CONFIG_OUTPUT
5542 { "print-config", 0, NULL, 'C' },
5544 { "daemonize", 0, NULL, 'd' },
5546 { "alignment", 1, NULL, 'a' },
5547 { "font", 1, NULL, 'f' },
5548 { "display", 1, NULL, 'X' },
5550 { "own-window", 0, NULL, 'o' },
5553 { "double-buffer", 0, NULL, 'b' },
5555 { "window-id", 1, NULL, 'w' },
5557 { "text", 1, NULL, 't' },
5558 { "interval", 0, NULL, 'u' },
5559 { "pause", 0, NULL, 'p' },
5563 void initialisation(int argc, char **argv) {
5564 struct sigaction act, oact;
5566 set_default_configurations();
5567 load_config_file(current_config);
5568 currentconffile = conftree_add(currentconffile, current_config);
5570 /* init specials array */
5571 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5572 NORM_ERR("failed to create specials array");
5576 if (current_mail_spool == NULL) {
5579 variable_substitute(MAIL_FILE, buf, 256);
5581 if (buf[0] != '\0') {
5582 current_mail_spool = strndup(buf, text_buffer_size);
5587 /* handle other command line arguments */
5589 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5590 || defined(__NetBSD__)
5591 optind = optreset = 1;
5596 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5597 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5598 "kvm_open")) == NULL) {
5599 CRIT_ERR(NULL, NULL, "cannot read kvm");
5604 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5613 fork_to_background = 1;
5616 global_debug_level++;
5620 set_first_font(optarg);
5623 text_alignment = string_to_alignment(optarg);
5642 global_text = strndup(optarg, max_user_text);
5643 convert_escapes(global_text);
5647 update_interval = strtod(optarg, 0);
5648 update_interval_old = update_interval;
5649 if (info.music_player_interval == 0) {
5650 // default to update_interval
5651 info.music_player_interval = update_interval;
5656 total_run_times = strtod(optarg, 0);
5660 gap_x = atoi(optarg);
5664 gap_y = atoi(optarg);
5668 startup_pause = atoi(optarg);
5669 sleep(startup_pause);
5679 if (output_methods & TO_X) {
5680 load_config_file_x11(current_config);
5684 /* generate text and get initial size */
5685 extract_variable_text(global_text);
5692 if (fork_to_background) {
5697 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5704 fprintf(stderr, "\n");
5709 /* parent process */
5710 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5717 start_update_threading();
5719 text_buffer = malloc(max_user_text);
5720 memset(text_buffer, 0, max_user_text);
5721 tmpstring1 = malloc(text_buffer_size);
5722 memset(tmpstring1, 0, text_buffer_size);
5723 tmpstring2 = malloc(text_buffer_size);
5724 memset(tmpstring2, 0, text_buffer_size);
5729 X11_create_window();
5732 llua_setup_info(&info, update_interval);
5733 #endif /* HAVE_LUA */
5738 /* Set signal handlers */
5739 act.sa_handler = signal_handler;
5740 sigemptyset(&act.sa_mask);
5743 act.sa_flags |= SA_RESTART;
5746 if ( sigaction(SIGINT, &act, &oact) < 0
5747 || sigaction(SIGALRM, &act, &oact) < 0
5748 || sigaction(SIGUSR1, &act, &oact) < 0
5749 || sigaction(SIGHUP, &act, &oact) < 0
5750 || sigaction(SIGTERM, &act, &oact) < 0) {
5751 NORM_ERR("error setting signal handler: %s", strerror(errno));
5755 llua_startup_hook();
5756 #endif /* HAVE_LUA */
5759 int main(int argc, char **argv)
5768 g_signal_pending = 0;
5769 max_user_text = MAX_USER_TEXT_DEFAULT;
5771 memset(&info, 0, sizeof(info));
5775 #ifdef TCP_PORT_MONITOR
5776 /* set default connection limit */
5777 tcp_portmon_set_max_connections(0);
5780 /* handle command line parameters that don't change configs */
5782 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5783 || ((s = getenv("LANG")) && *s)) {
5784 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5786 NORM_ERR("malloc failed");
5788 for (x = 0; x < strlen(s); x++) {
5789 temp[x] = tolower(s[x]);
5792 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5798 if (!setlocale(LC_CTYPE, "")) {
5799 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5803 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5814 if (current_config) {
5815 free(current_config);
5817 current_config = strndup(optarg, max_user_text);
5820 freopen("/dev/null", "w", stderr);
5823 print_help(argv[0]);
5825 #ifdef CONFIG_OUTPUT
5832 window.window = strtol(optarg, 0, 0);
5837 disp = strdup(optarg);
5846 /* check if specified config file is valid */
5847 if (current_config) {
5849 if (stat(current_config, &sb) ||
5850 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5851 NORM_ERR("invalid configuration file '%s'\n", current_config);
5852 free(current_config);
5857 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5859 if (!current_config) {
5860 /* load default config file */
5861 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5864 /* Try to use personal config file first */
5865 to_real_path(buf, CONFIG_FILE);
5866 if (buf[0] && (fp = fopen(buf, "r"))) {
5867 current_config = strndup(buf, max_user_text);
5871 /* Try to use system config file if personal config not readable */
5872 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5873 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5877 /* No readable config found */
5878 if (!current_config) {
5879 #ifdef CONFIG_OUTPUT
5880 current_config = strdup("==builtin==");
5881 NORM_ERR("no readable personal or system-wide config file found,"
5882 " using builtin default");
5884 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
5885 #endif /* ! CONF_OUTPUT */
5890 /* Load xoap keys, if existing */
5894 #ifdef HAVE_SYS_INOTIFY_H
5895 inotify_fd = inotify_init1(IN_NONBLOCK);
5896 #endif /* HAVE_SYS_INOTIFY_H */
5898 initialisation(argc, argv);
5902 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5910 void alarm_handler(void) {
5912 kill(childpid, SIGTERM);
5916 static void signal_handler(int sig)
5918 /* signal handler is light as a feather, as it should be.
5919 * we will poll g_signal_pending with each loop of conky
5920 * and do any signal processing there, NOT here (except
5921 * SIGALRM because this is caused when conky is hanging) */
5922 if(sig == SIGALRM) {
5925 g_signal_pending = sig;