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-2010 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>
74 #include <curl/curl.h>
94 #include "iconv_tools.h"
100 #include "net_stat.h"
104 #include "read_tcp.h"
106 #include "specials.h"
107 #include "temphelper.h"
108 #include "template.h"
109 #include "tailhead.h"
110 #include "timeinfo.h"
113 /* check for OS and include appropriate headers */
114 #if defined(__linux__)
116 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
118 #elif defined(__OpenBSD__)
122 #if defined(__FreeBSD_kernel__)
126 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
127 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
128 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
129 int apm_getinfo(int fd, apm_info_t aip);
130 char *get_apm_adapter(void);
131 char *get_apm_battery_life(void);
132 char *get_apm_battery_time(void);
136 #include "defconfig.h"
137 #include "conf_cookie.h"
141 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
144 #define MAIL_FILE "$MAIL"
145 #define MAX_IF_BLOCK_DEPTH 5
147 //#define SIGNAL_BLOCKING
148 #undef SIGNAL_BLOCKING
150 /* debugging level, used by logging.h */
151 int global_debug_level = 0;
153 /* disable inotify auto reload feature if desired */
154 int disable_auto_reload = 0;
156 /* two strings for internal use */
157 static char *tmpstring1, *tmpstring2;
159 /* variables holding various config settings */
161 int format_human_readable;
168 int top_cpu, top_mem, top_time;
174 static int extra_newline;
175 enum x_initialiser_state x_initialised = NO;
176 static volatile int g_signal_pending;
177 /* Update interval */
178 double update_interval;
179 double update_interval_old;
180 double update_interval_bat;
181 void *global_cpu = NULL;
182 unsigned int max_text_width = 0;
187 /* prototypes for internally used functions */
188 static void signal_handler(int);
189 static void print_version(void) __attribute__((noreturn));
190 static void reload_config(void);
192 static void print_version(void)
194 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
196 printf("\nCompiled in features:\n\n"
197 "System config file: "SYSTEM_CONFIG_FILE"\n"
198 "Package library path: "PACKAGE_LIBDIR"\n\n"
202 " * Xdamage extension\n"
203 # endif /* HAVE_XDAMAGE */
205 " * XDBE (double buffer extension)\n"
206 # endif /* HAVE_XDBE */
212 # endif /* USE_ARGB */
214 "\n Music detection:\n"
217 #endif /* AUDACIOUS */
233 #endif /* HAVE_OPENMP */
240 #ifdef TCP_PORT_MONITOR
242 #endif /* TCP_PORT_MONITOR */
245 #endif /* HAVE_CURL */
250 " * Weather (METAR)\n"
252 " * Weather (XOAP)\n"
257 #endif /* HAVE_IWLIB */
259 " * support for IBM/Lenovo notebooks\n"
269 #endif /* CONFIG_OUTPUT */
285 #ifdef HAVE_LUA_CAIRO
287 #endif /* HAVE_LUA_CAIRO */
288 #ifdef HAVE_LUA_IMLIB2
291 #endif /* HAVE_LUA */
297 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
302 static void X11_create_window(void);
303 static void X11_initialisation(void);
305 struct _x11_stuff_s {
309 XserverRegion region2, part;
310 int event_base, error_base;
316 static int text_start_x, text_start_y; /* text start position in window */
317 static int text_width, text_height;
334 /* display to connect to */
335 static char *disp = NULL;
339 /* struct that has all info to be shared between
340 * instances of the same text object */
341 struct information info;
343 /* path to config file */
344 char *current_config;
346 /* set to 1 if you want all text to be in uppercase */
347 static unsigned int stuff_in_uppercase;
349 /* Run how many times? */
350 static unsigned long total_run_times;
353 static int fork_to_background;
355 /* set to 0 after the first time conky is run, so we don't fork again after the
357 static int first_pass = 1;
359 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
361 /* filenames for output */
362 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
363 char *append_file = NULL; FILE *append_fpointer = NULL;
367 static int show_graph_scale;
368 static int show_graph_range;
370 /* Position on the screen */
371 static int text_alignment;
372 static int gap_x, gap_y;
375 static int draw_borders;
376 static int draw_graph_borders;
377 static int stippled_borders;
379 int get_stippled_borders(void)
381 return stippled_borders;
384 static int draw_shades, draw_outline;
386 long default_fg_color, default_bg_color, default_out_color;
388 /* create own window or draw stuff to root? */
389 static int set_transparent = 0;
392 static int own_window = 0;
393 static int background_colour = 0;
395 /* fixed size/pos is set if wm/user changes them */
396 static int fixed_size = 0, fixed_pos = 0;
399 static int minimum_width, minimum_height;
400 static int maximum_width;
405 static int sensor_device;
408 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
411 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
412 unsigned int max_user_text;
414 /* maximum size of individual text buffers, ie $exec buffer size */
415 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
420 /* no buffers in used memory? */
423 /* pad percentages to decimals? */
424 static int pad_percents = 0;
426 static char *global_text = 0;
428 char *get_global_text(void)
433 long global_text_lines;
435 static int total_updates;
436 static int updatereset;
438 void set_updatereset(int i)
443 int get_updatereset(void)
448 int check_contains(char *f, char *s)
451 FILE *where = open_file(f, 0);
456 while (fgets(buf1, 256, where)) {
457 if (strstr(buf1, s)) {
464 NORM_ERR("Could not open the file '%s'", f);
469 #define SECRIT_MULTILINE_CHAR '\x02'
471 int calc_text_width(const char *s)
473 size_t slen = strlen(s);
476 if ((output_methods & TO_X) == 0) {
486 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
487 (const FcChar8 *) s, slen, &gi);
489 XftTextExtents8(display, fonts[selected_font].xftfont,
490 (const FcChar8 *) s, slen, &gi);
496 return XTextWidth(fonts[selected_font].font, s, slen);
501 /* formatted text to render on screen, generated in generate_text(),
502 * drawn in draw_stuff() */
504 static char *text_buffer;
506 /* quite boring functions */
508 static inline void for_each_line(char *b, int f(char *, int))
511 int special_index = 0; /* specials index */
514 for (ps = b, pe = b; *pe; pe++) {
517 special_index = f(ps, special_index);
524 f(ps, special_index);
528 static void convert_escapes(char *buf)
530 char *p = buf, *s = buf;
537 } else if (*s == '\\') {
548 /* Prints anything normally printed with snprintf according to the current value
549 * of use_spacer. Actually slightly more flexible than snprintf, as you can
550 * safely specify the destination buffer as one of your inputs. */
551 int spaced_print(char *buf, int size, const char *format, int width, ...)
560 tempbuf = malloc(size * sizeof(char));
562 // Passes the varargs along to vsnprintf
563 va_start(argp, width);
564 vsnprintf(tempbuf, size, format, argp);
567 switch (use_spacer) {
569 len = snprintf(buf, size, "%s", tempbuf);
572 len = snprintf(buf, size, "%*s", width, tempbuf);
575 len = snprintf(buf, size, "%-*s", width, tempbuf);
582 /* print percentage values
584 * - i.e., unsigned values between 0 and 100
585 * - respect the value of pad_percents */
586 int percent_print(char *buf, int size, unsigned value)
588 return spaced_print(buf, size, "%u", pad_percents, value);
591 /* converts from bytes to human readable format (K, M, G, T)
593 * The algorithm always divides by 1024, as unit-conversion of byte
594 * counts suggests. But for output length determination we need to
595 * compare with 1000 here, as we print in decimal form. */
596 void human_readable(long long num, char *buf, int size)
598 const char **suffix = suffixes;
604 /* Possibly just output as usual, for example for stdout usage */
605 if (!format_human_readable) {
606 spaced_print(buf, size, "%d", 6, round_to_int(num));
609 if (short_units || llabs(num) < 1000LL) {
617 if (llabs(num) < 1000LL) {
618 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
622 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
630 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
632 * The goal is to always have a significance of 3, by
633 * adjusting the decimal part of the number. Sample output:
637 * so the point of alignment resides between number and unit. The
638 * upside of this is that there is minimal padding necessary, though
639 * there should be a way to make alignment take place at the decimal
640 * dot (then with fixed width decimal part).
642 * Note the repdigits below: when given a precision value, printf()
643 * rounds the float to it, not just cuts off the remaining digits. So
644 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
645 * printed with a precision of 0. Yay. */
647 precision = 0; /* print 100-999 without decimal part */
649 precision = 1; /* print 10-99 with one decimal place */
651 precision = 2; /* print 0-9 with two decimal places */
653 spaced_print(buf, size, format, width, precision, fnum, *suffix);
656 /* global object list root element */
657 static struct text_object global_root_object;
659 static long current_text_color;
661 void set_current_text_color(long colour)
663 current_text_color = colour;
666 long get_current_text_color(void)
668 return current_text_color;
671 //adds newstring to to the tree unless you can already see it when travelling back.
672 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
673 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
674 struct conftree* node;
675 struct conftree* node2;
677 for(node = previous; node != NULL; node = node->back) {
678 if(strcmp(node->string, newstring) == 0) {
682 node = malloc(sizeof(struct conftree));
683 if (previous != NULL) {
684 if(previous->vert_next == NULL) {
685 previous->vert_next = node;
687 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
688 node2->horz_next = node;
691 node->string = strdup(newstring);
692 node->horz_next = NULL;
693 node->vert_next = NULL;
694 node->back = previous;
698 void conftree_empty(struct conftree* tree) {
700 conftree_empty(tree->horz_next);
701 conftree_empty(tree->vert_next);
707 struct conftree *currentconffile;
709 static void extract_variable_text(const char *p)
711 free_text_objects(&global_root_object, 0);
725 extract_variable_text_internal(&global_root_object, p);
728 void parse_conky_vars(struct text_object *root, const char *txt,
729 char *p, int p_max_size, struct information *cur)
731 extract_variable_text_internal(root, txt);
732 generate_text_internal(p, p_max_size, *root, cur);
735 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
736 * multiline objects like $exec work with $align[rc] and friends
738 void substitute_newlines(char *p, long l)
742 while (p && *p && p < s + l) {
744 /* only substitute if it's not the last newline */
745 *p = SECRIT_MULTILINE_CHAR;
751 void generate_text_internal(char *p, int p_max_size,
752 struct text_object root, struct information *cur)
754 struct text_object *obj;
756 int need_to_load_fonts = 0;
759 /* for the OBJ_top* handler */
760 struct process **needed = 0;
763 char buff_in[p_max_size];
765 #endif /* HAVE_ICONV */
771 while (obj && p_max_size > 0) {
772 needed = 0; /* reset for top stuff */
774 /* IFBLOCK jumping algorithm
776 * This is easier as it looks like:
777 * - each IF checks it's condition
778 * - on FALSE: call DO_JUMP
779 * - on TRUE: don't care
780 * - each ELSE calls DO_JUMP unconditionally
781 * - each ENDIF is silently being ignored
784 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
785 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
786 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
787 * regularly, "obj" is being updated to point to obj->next, so object parsing
788 * continues right after the corresponding ELSE or ENDIF. This means that if we
789 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
790 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
791 * jumped, and there is nothing to do.
795 if (obj->ifblock_next) \
796 obj = obj->ifblock_next; \
799 #define OBJ(a) break; case OBJ_##a:
803 NORM_ERR("not implemented obj type %d", obj->type);
805 print_read_tcp(obj, p, p_max_size);
809 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
811 #endif /* !__OpenBSD__ */
815 ok = get_freq(p, p_max_size, "%.0f", 1,
823 ok = get_freq(p, p_max_size, "%'.2f", 1000,
826 /* OpenBSD has no such flag (SUSv2) */
827 ok = get_freq(p, p_max_size, "%.2f", 1000,
829 #endif /* __OpenBSD */
832 #if defined(__linux__)
834 print_voltage_mv(obj, p, p_max_size);
837 print_voltage_v(obj, p, p_max_size);
841 OBJ(wireless_essid) {
842 print_wireless_essid(obj, p, p_max_size);
845 print_wireless_mode(obj, p, p_max_size);
847 OBJ(wireless_bitrate) {
848 print_wireless_bitrate(obj, p, p_max_size);
851 print_wireless_ap(obj, p, p_max_size);
853 OBJ(wireless_link_qual) {
854 print_wireless_link_qual(obj, p, p_max_size);
856 OBJ(wireless_link_qual_max) {
857 print_wireless_link_qual_max(obj, p, p_max_size);
859 OBJ(wireless_link_qual_perc) {
860 print_wireless_link_qual_perc(obj, p, p_max_size);
862 OBJ(wireless_link_bar) {
863 print_wireless_link_bar(obj, p, p_max_size);
865 #endif /* HAVE_IWLIB */
867 #endif /* __linux__ */
871 get_acpi_fan(p, p_max_size);
874 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
877 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
880 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
883 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_VOLTS);
886 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TEMP);
888 OBJ(battery_percent) {
889 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
892 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
895 get_battery_short_status(p, p_max_size, obj->data.s);
897 OBJ(cell_radio_dbm) {
898 get_dbus_stuff(p, p_max_size, DBUS_CELL_DBM);
900 OBJ(cell_radio_percent) {
901 get_dbus_stuff(p, p_max_size, DBUS_CELL_PERCENT);
903 #endif /* __OpenBSD__ */
906 human_readable(cur->buffers * 1024, p, 255);
909 human_readable(cur->cached * 1024, p, 255);
911 OBJ(cmdline_to_pid) {
912 print_cmdline_to_pid(obj, p, p_max_size);
915 if (cur->cpu_usage) {
916 if (obj->data.i > info.cpu_count) {
917 NORM_ERR("obj->data.i %i info.cpu_count %i",
918 obj->data.i, info.cpu_count);
919 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
921 percent_print(p, p_max_size,
922 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
926 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
928 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
932 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
935 print_loadgraph(obj, p, p_max_size);
939 new_fg(p, obj->data.l);
973 #if defined(__linux__)
975 snprintf(p, p_max_size, "%s",
976 get_disk_protect_queue(obj->data.s));
979 print_i8k_version(obj, p, p_max_size);
982 print_i8k_bios(obj, p, p_max_size);
985 print_i8k_serial(obj, p, p_max_size);
988 print_i8k_cpu_temp(obj, p, p_max_size);
990 OBJ(i8k_left_fan_status) {
991 print_i8k_left_fan_status(obj, p, p_max_size);
993 OBJ(i8k_right_fan_status) {
994 print_i8k_right_fan_status(obj, p, p_max_size);
996 OBJ(i8k_left_fan_rpm) {
997 print_i8k_left_fan_rpm(obj, p, p_max_size);
999 OBJ(i8k_right_fan_rpm) {
1000 print_i8k_right_fan_rpm(obj, p, p_max_size);
1002 OBJ(i8k_ac_status) {
1003 print_i8k_ac_status(obj, p, p_max_size);
1005 OBJ(i8k_buttons_status) {
1006 print_i8k_buttons_status(obj, p, p_max_size);
1010 get_ibm_acpi_fan(obj, p, p_max_size);
1013 print_ibm_temps(obj, p, p_max_size);
1016 get_ibm_acpi_volume(obj, p, p_max_size);
1018 OBJ(ibm_brightness) {
1019 get_ibm_acpi_brightness(obj, p, p_max_size);
1022 /* information from sony_laptop kernel module
1023 * /sys/devices/platform/sony-laptop */
1024 OBJ(sony_fanspeed) {
1025 get_sony_fanspeed(p, p_max_size);
1028 if (!gateway_exists()) {
1033 print_gateway_iface(p, p_max_size);
1036 print_gateway_ip(p, p_max_size);
1039 snprintf(p, p_max_size, "%d", get_laptop_mode());
1042 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1044 #endif /* __linux__ */
1045 #if (defined(__FreeBSD__) || defined(__linux__))
1047 if (!interface_up(obj)) {
1053 OBJ(obsd_sensors_temp) {
1054 print_obsd_sensors_temp(obj, p, p_max_size);
1056 OBJ(obsd_sensors_fan) {
1057 print_obsd_sensors_fan(obj, p, p_max_size);
1059 OBJ(obsd_sensors_volt) {
1060 print_obsd_sensors_volt(obj, p, p_max_size);
1063 get_obsd_vendor(p, p_max_size);
1066 get_obsd_product(p, p_max_size);
1068 #endif /* __OpenBSD__ */
1071 new_font(p, obj->data.s);
1072 need_to_load_fonts = 1;
1076 print_diskio(obj, p, p_max_size);
1079 print_diskio_write(obj, p, p_max_size);
1082 print_diskio_read(obj, p, p_max_size);
1086 print_diskiograph(obj, p, p_max_size);
1088 OBJ(diskiograph_read) {
1089 print_diskiograph_read(obj, p, p_max_size);
1091 OBJ(diskiograph_write) {
1092 print_diskiograph_write(obj, p, p_max_size);
1096 print_downspeed(obj, p, p_max_size);
1099 print_downspeedf(obj, p, p_max_size);
1102 OBJ(downspeedgraph) {
1103 print_downspeedgraph(obj, p, p_max_size);
1107 /* Since we see you, you're if has not jumped.
1108 * Do Ninja jump here: without leaving traces.
1109 * This is to prevent us from stale jumped flags.
1111 obj = obj->ifblock_next;
1115 /* harmless object, just ignore */
1118 print_addr(obj, p, p_max_size);
1120 #if defined(__linux__)
1122 print_addrs(obj, p, p_max_size);
1124 #endif /* __linux__ */
1125 #if defined(IMLIB2) && defined(X11)
1127 /* doesn't actually draw anything, just queues it omp. the
1128 * image will get drawn after the X event loop */
1129 cimlib_add_image(obj->data.s);
1133 evaluate(obj->data.s, p, p_max_size);
1136 print_exec(obj, p, p_max_size);
1139 print_execp(obj, p, p_max_size);
1142 print_execgauge(obj, p, p_max_size);
1145 print_execbar(obj, p, p_max_size);
1149 print_execgraph(obj, p, p_max_size);
1153 print_execibar(obj, p, p_max_size);
1157 print_execigraph(obj, p, p_max_size);
1161 print_execigauge(obj, p, p_max_size);
1164 print_execi(obj, p, p_max_size);
1167 print_execpi(obj, p, p_max_size);
1170 print_texeci(obj, p, p_max_size);
1173 print_imap_unseen(obj, p, p_max_size);
1175 OBJ(imap_messages) {
1176 print_imap_messages(obj, p, p_max_size);
1179 print_pop3_unseen(obj, p, p_max_size);
1182 print_pop3_used(obj, p, p_max_size);
1185 print_fs_bar(obj, 0, p, p_max_size);
1188 print_fs_free(obj, p, p_max_size);
1191 print_fs_perc(obj, 1, p, p_max_size);
1194 print_fs_size(obj, p, p_max_size);
1197 print_fs_type(obj, p, p_max_size);
1200 print_fs_used(obj, p, p_max_size);
1203 print_fs_bar(obj, 1, p, p_max_size);
1206 print_fs_perc(obj, 0, p, p_max_size);
1209 print_loadavg(obj, p, p_max_size);
1212 new_goto(p, obj->data.i);
1219 new_hr(p, obj->data.i);
1223 print_nameserver(obj, p, p_max_size);
1227 print_eve(obj, p, p_max_size);
1232 curl_print(obj, p, p_max_size);
1237 rss_print_info(obj, p, p_max_size);
1242 print_weather(obj, p, p_max_size);
1246 OBJ(weather_forecast) {
1247 print_weather_forecast(obj, p, p_max_size);
1252 print_lua(obj, p, p_max_size);
1255 print_lua_parse(obj, p, p_max_size);
1258 print_lua_bar(obj, p, p_max_size);
1262 print_lua_graph(obj, p, p_max_size);
1266 print_lua_gauge(obj, p, p_max_size);
1268 #endif /* HAVE_LUA */
1274 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1275 snprintf(p, p_max_size, "N/A");
1277 temp_print(p, p_max_size, (double)val,
1278 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1283 new_offset(p, obj->data.i);
1286 new_voffset(p, obj->data.i);
1290 print_sysfs_sensor(obj, p, p_max_size);
1293 print_sysfs_sensor(obj, p, p_max_size);
1296 print_sysfs_sensor(obj, p, p_max_size);
1298 #endif /* __linux__ */
1300 new_alignr(p, obj->data.i);
1303 new_alignc(p, obj->data.i);
1306 char buf[max_user_text];
1307 struct information *tmp_info =
1308 malloc(sizeof(struct information));
1309 memcpy(tmp_info, cur, sizeof(struct information));
1310 generate_text_internal(buf, max_user_text,
1311 *obj->sub, tmp_info);
1313 if (strlen(buf) != 0) {
1319 char expression[max_user_text];
1321 struct information *tmp_info;
1323 tmp_info = malloc(sizeof(struct information));
1324 memcpy(tmp_info, cur, sizeof(struct information));
1325 generate_text_internal(expression, max_user_text,
1326 *obj->sub, tmp_info);
1327 DBGP("parsed arg into '%s'", expression);
1329 val = compare(expression);
1331 NORM_ERR("compare failed for expression '%s'",
1341 spc = strchr(obj->data.s, ' ');
1342 if (!spc && access(obj->data.s, F_OK)) {
1346 if (!check_contains(obj->data.s, spc + 1))
1353 && (!check_mount(obj->data.s))) {
1359 if (!get_process_by_name(obj->data.s)) {
1361 if ((obj->data.s) && system(obj->data.s)) {
1366 #if defined(__linux__)
1368 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1372 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1375 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1380 human_readable(cur->mem * 1024, p, 255);
1383 human_readable(cur->memeasyfree * 1024, p, 255);
1386 human_readable(cur->memfree * 1024, p, 255);
1389 human_readable(cur->memmax * 1024, p, 255);
1393 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1396 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1399 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1403 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1408 print_mixer(obj, p, p_max_size);
1411 print_mixerl(obj, p, p_max_size);
1414 print_mixerr(obj, p, p_max_size);
1417 print_mixer_bar(obj, p, p_max_size);
1420 print_mixerl_bar(obj, p, p_max_size);
1423 print_mixerr_bar(obj, p, p_max_size);
1425 OBJ(if_mixer_mute) {
1426 if (!check_mixer_muted(obj)) {
1431 #define NOT_IN_X "Not running in X"
1433 if(x_initialised != YES) {
1434 strncpy(p, NOT_IN_X, p_max_size);
1436 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1439 OBJ(monitor_number) {
1440 if(x_initialised != YES) {
1441 strncpy(p, NOT_IN_X, p_max_size);
1443 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1447 if(x_initialised != YES) {
1448 strncpy(p, NOT_IN_X, p_max_size);
1450 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1453 OBJ(desktop_number) {
1454 if(x_initialised != YES) {
1455 strncpy(p, NOT_IN_X, p_max_size);
1457 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1461 if(x_initialised != YES) {
1462 strncpy(p, NOT_IN_X, p_max_size);
1463 }else if(cur->x11.desktop.name != NULL) {
1464 strncpy(p, cur->x11.desktop.name, p_max_size);
1470 char buf[max_user_text];
1472 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1474 print_format_time(obj, p, p_max_size);
1478 print_mails(obj, p, p_max_size);
1481 print_new_mails(obj, p, p_max_size);
1484 print_seen_mails(obj, p, p_max_size);
1487 print_unseen_mails(obj, p, p_max_size);
1489 OBJ(flagged_mails) {
1490 print_flagged_mails(obj, p, p_max_size);
1492 OBJ(unflagged_mails) {
1493 print_unflagged_mails(obj, p, p_max_size);
1495 OBJ(forwarded_mails) {
1496 print_forwarded_mails(obj, p, p_max_size);
1498 OBJ(unforwarded_mails) {
1499 print_unforwarded_mails(obj, p, p_max_size);
1501 OBJ(replied_mails) {
1502 print_replied_mails(obj, p, p_max_size);
1504 OBJ(unreplied_mails) {
1505 print_unreplied_mails(obj, p, p_max_size);
1508 print_draft_mails(obj, p, p_max_size);
1510 OBJ(trashed_mails) {
1511 print_trashed_mails(obj, p, p_max_size);
1514 print_mboxscan(obj, p, p_max_size);
1517 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1519 OBJ(nodename_short) {
1521 pos = strstr(cur->uname_s.nodename, ".");
1523 snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1525 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1529 new_outline(p, obj->data.l);
1532 char buf[max_user_text];
1534 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1536 print_pid_chroot(obj, p, p_max_size);
1539 char buf[max_user_text];
1541 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1543 print_pid_cmdline(obj, p, p_max_size);
1546 char buf[max_user_text];
1548 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1550 print_pid_cwd(obj, p, p_max_size);
1553 char buf[max_user_text];
1555 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1557 print_pid_environ(obj, p, p_max_size);
1559 OBJ(pid_environ_list) {
1560 char buf[max_user_text];
1562 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1564 print_pid_environ_list(obj, p, p_max_size);
1567 char buf[max_user_text];
1569 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1571 print_pid_exe(obj, p, p_max_size);
1574 char buf[max_user_text];
1576 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1578 print_pid_nice(obj, p, p_max_size);
1580 OBJ(pid_openfiles) {
1581 char buf[max_user_text];
1583 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1585 print_pid_openfiles(obj, p, p_max_size);
1588 char buf[max_user_text];
1590 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1592 print_pid_parent(obj, p, p_max_size);
1595 char buf[max_user_text];
1597 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1599 print_pid_priority(obj, p, p_max_size);
1602 char buf[max_user_text];
1604 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1606 print_pid_state(obj, p, p_max_size);
1608 OBJ(pid_state_short) {
1609 char buf[max_user_text];
1611 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1613 print_pid_state_short(obj, p, p_max_size);
1616 char buf[max_user_text];
1618 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1620 print_pid_stderr(obj, p, p_max_size);
1623 char buf[max_user_text];
1625 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1627 print_pid_stdin(obj, p, p_max_size);
1630 char buf[max_user_text];
1632 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1634 print_pid_stdout(obj, p, p_max_size);
1637 char buf[max_user_text];
1639 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1641 print_pid_threads(obj, p, p_max_size);
1643 OBJ(pid_thread_list) {
1644 char buf[max_user_text];
1646 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1648 print_pid_thread_list(obj, p, p_max_size);
1650 OBJ(pid_time_kernelmode) {
1651 char buf[max_user_text];
1653 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1655 print_pid_time_kernelmode(obj, p, p_max_size);
1657 OBJ(pid_time_usermode) {
1658 char buf[max_user_text];
1660 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1662 print_pid_time_usermode(obj, p, p_max_size);
1665 char buf[max_user_text];
1667 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1669 print_pid_time(obj, p, p_max_size);
1672 char buf[max_user_text];
1674 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1676 print_pid_uid(obj, p, p_max_size);
1679 char buf[max_user_text];
1681 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1683 print_pid_euid(obj, p, p_max_size);
1686 char buf[max_user_text];
1688 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1690 print_pid_suid(obj, p, p_max_size);
1693 char buf[max_user_text];
1695 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1697 print_pid_fsuid(obj, p, p_max_size);
1700 char buf[max_user_text];
1702 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1704 print_pid_gid(obj, p, p_max_size);
1707 char buf[max_user_text];
1709 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1711 print_pid_egid(obj, p, p_max_size);
1714 char buf[max_user_text];
1716 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1718 print_pid_sgid(obj, p, p_max_size);
1721 char buf[max_user_text];
1723 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1725 print_pid_fsgid(obj, p, p_max_size);
1728 char buf[max_user_text];
1730 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1732 print_pid_read(obj, p, p_max_size);
1735 char buf[max_user_text];
1737 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1739 print_pid_vmpeak(obj, p, p_max_size);
1742 char buf[max_user_text];
1744 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1746 print_pid_vmsize(obj, p, p_max_size);
1749 char buf[max_user_text];
1751 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1753 print_pid_vmlck(obj, p, p_max_size);
1756 char buf[max_user_text];
1758 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1760 print_pid_vmhwm(obj, p, p_max_size);
1763 char buf[max_user_text];
1765 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1767 print_pid_vmrss(obj, p, p_max_size);
1770 char buf[max_user_text];
1772 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1774 print_pid_vmdata(obj, p, p_max_size);
1777 char buf[max_user_text];
1779 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1781 print_pid_vmstk(obj, p, p_max_size);
1784 char buf[max_user_text];
1786 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1788 print_pid_vmexe(obj, p, p_max_size);
1791 char buf[max_user_text];
1793 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1795 print_pid_vmlib(obj, p, p_max_size);
1798 char buf[max_user_text];
1800 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1802 print_pid_vmpte(obj, p, p_max_size);
1805 char buf[max_user_text];
1807 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1809 print_pid_write(obj, p, p_max_size);
1812 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1814 OBJ(running_processes) {
1815 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1817 OBJ(running_threads) {
1818 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1821 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1824 snprintf(p, p_max_size, "%s", obj->data.s);
1828 new_bg(p, obj->data.l);
1831 new_stippled_hr(obj, p);
1835 human_readable(cur->swap * 1024, p, 255);
1838 human_readable(cur->swapfree * 1024, p, 255);
1841 human_readable(cur->swapmax * 1024, p, 255);
1844 if (cur->swapmax == 0) {
1845 strncpy(p, "No swap", p_max_size);
1847 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1851 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1854 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1857 print_time(obj, p, p_max_size);
1860 print_utime(obj, p, p_max_size);
1863 print_tztime(obj, p, p_max_size);
1866 print_totaldown(obj, p, p_max_size);
1869 print_totalup(obj, p, p_max_size);
1872 char buf[max_user_text];
1874 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1876 print_gid_name(obj, p, p_max_size);
1879 char buf[max_user_text];
1881 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1883 print_uid_name(obj, p, p_max_size);
1886 snprintf(p, p_max_size, "%d", total_updates);
1889 if(total_updates % updatereset != obj->data.i - 1) {
1894 print_upspeed(obj, p, p_max_size);
1897 print_upspeedf(obj, p, p_max_size);
1901 print_upspeedgraph(obj, p, p_max_size);
1905 format_seconds_short(p, p_max_size, (int) cur->uptime);
1908 format_seconds(p, p_max_size, (int) cur->uptime);
1912 snprintf(p, p_max_size, "%s", cur->users.names);
1915 snprintf(p, p_max_size, "%s", cur->users.terms);
1918 snprintf(p, p_max_size, "%s", cur->users.times);
1921 update_user_time(obj->data.s);
1922 snprintf(p, p_max_size, "%s", cur->users.ctime);
1925 snprintf(p, p_max_size, "%d", cur->users.number);
1927 #endif /* __linux__ */
1928 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1929 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1933 msg = get_apm_adapter();
1934 snprintf(p, p_max_size, "%s", msg);
1937 OBJ(apm_battery_life) {
1940 msg = get_apm_battery_life();
1941 snprintf(p, p_max_size, "%s", msg);
1944 OBJ(apm_battery_time) {
1947 msg = get_apm_battery_time();
1948 snprintf(p, p_max_size, "%s", msg);
1951 #endif /* __FreeBSD__ __OpenBSD__ */
1955 print_mpd_title(obj, p, p_max_size);
1957 print_mpd_artist(obj, p, p_max_size);
1959 print_mpd_album(obj, p, p_max_size);
1961 print_mpd_random(obj, p, p_max_size);
1963 print_mpd_repeat(obj, p, p_max_size);
1965 print_mpd_track(obj, p, p_max_size);
1967 print_mpd_name(obj, p, p_max_size);
1969 print_mpd_file(obj, p, p_max_size);
1971 print_mpd_vol(obj, p, p_max_size);
1973 print_mpd_bitrate(obj, p, p_max_size);
1975 print_mpd_status(obj, p, p_max_size);
1977 print_mpd_elapsed(obj, p, p_max_size);
1980 print_mpd_length(obj, p, p_max_size);
1983 print_mpd_percent(obj, p, p_max_size);
1986 print_mpd_bar(obj, p, p_max_size);
1989 print_mpd_smart(obj, p, p_max_size);
1991 OBJ(if_mpd_playing) {
1992 if (!mpd_get_info()->is_playing) {
1999 #define MOC_PRINT(t, a) \
2000 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2002 MOC_PRINT(state, "??");
2005 MOC_PRINT(file, "no file");
2008 MOC_PRINT(title, "no title");
2011 MOC_PRINT(artist, "no artist");
2014 MOC_PRINT(song, "no song");
2017 MOC_PRINT(album, "no album");
2019 OBJ(moc_totaltime) {
2020 MOC_PRINT(totaltime, "0:00");
2023 MOC_PRINT(timeleft, "0:00");
2026 MOC_PRINT(curtime, "0:00");
2029 MOC_PRINT(bitrate, "0Kbps");
2032 MOC_PRINT(rate, "0KHz");
2038 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2041 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2044 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2047 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2049 OBJ(xmms2_comment) {
2050 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2053 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2056 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2059 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2061 OBJ(xmms2_tracknr) {
2062 if (cur->xmms2.tracknr != -1) {
2063 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2066 OBJ(xmms2_bitrate) {
2067 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2070 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2073 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2075 OBJ(xmms2_elapsed) {
2076 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2077 (cur->xmms2.elapsed / 1000) % 60);
2079 OBJ(xmms2_duration) {
2080 snprintf(p, p_max_size, "%02d:%02d",
2081 cur->xmms2.duration / 60000,
2082 (cur->xmms2.duration / 1000) % 60);
2084 OBJ(xmms2_percent) {
2085 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2089 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2092 OBJ(xmms2_playlist) {
2093 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2095 OBJ(xmms2_timesplayed) {
2096 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2099 if (strlen(cur->xmms2.title) < 2
2100 && strlen(cur->xmms2.title) < 2) {
2101 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2103 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2107 OBJ(if_xmms2_connected) {
2108 if (cur->xmms2.conn_state != 1) {
2114 OBJ(audacious_status) {
2115 snprintf(p, p_max_size, "%s",
2116 cur->audacious.items[AUDACIOUS_STATUS]);
2118 OBJ(audacious_title) {
2119 snprintf(p, cur->audacious.max_title_len > 0
2120 ? cur->audacious.max_title_len : p_max_size, "%s",
2121 cur->audacious.items[AUDACIOUS_TITLE]);
2123 OBJ(audacious_length) {
2124 snprintf(p, p_max_size, "%s",
2125 cur->audacious.items[AUDACIOUS_LENGTH]);
2127 OBJ(audacious_length_seconds) {
2128 snprintf(p, p_max_size, "%s",
2129 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2131 OBJ(audacious_position) {
2132 snprintf(p, p_max_size, "%s",
2133 cur->audacious.items[AUDACIOUS_POSITION]);
2135 OBJ(audacious_position_seconds) {
2136 snprintf(p, p_max_size, "%s",
2137 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2139 OBJ(audacious_bitrate) {
2140 snprintf(p, p_max_size, "%s",
2141 cur->audacious.items[AUDACIOUS_BITRATE]);
2143 OBJ(audacious_frequency) {
2144 snprintf(p, p_max_size, "%s",
2145 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2147 OBJ(audacious_channels) {
2148 snprintf(p, p_max_size, "%s",
2149 cur->audacious.items[AUDACIOUS_CHANNELS]);
2151 OBJ(audacious_filename) {
2152 snprintf(p, p_max_size, "%s",
2153 cur->audacious.items[AUDACIOUS_FILENAME]);
2155 OBJ(audacious_playlist_length) {
2156 snprintf(p, p_max_size, "%s",
2157 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2159 OBJ(audacious_playlist_position) {
2160 snprintf(p, p_max_size, "%s",
2161 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2163 OBJ(audacious_main_volume) {
2164 snprintf(p, p_max_size, "%s",
2165 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2168 OBJ(audacious_bar) {
2172 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2173 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2174 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2177 #endif /* AUDACIOUS */
2181 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2184 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2187 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2190 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2193 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2196 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2199 /* we have four different types of top (top, top_mem,
2200 * top_time and top_io). To avoid having almost-same code four
2201 * times, we have this special handler. */
2209 print_top(obj, p, p_max_size);
2211 print_tailhead("tail", obj, p, p_max_size);
2214 print_tailhead("head", obj, p, p_max_size);
2217 print_lines(obj, p, p_max_size);
2221 print_words(obj, p, p_max_size);
2223 #ifdef TCP_PORT_MONITOR
2225 tcp_portmon_action(obj, p, p_max_size);
2227 #endif /* TCP_PORT_MONITOR */
2231 do_iconv_start(obj);
2236 #endif /* HAVE_ICONV */
2238 OBJ(entropy_avail) {
2239 print_entropy_avail(obj, p, p_max_size);
2242 print_entropy_perc(obj, p, p_max_size);
2244 OBJ(entropy_poolsize) {
2245 print_entropy_poolsize(obj, p, p_max_size);
2248 print_entropy_bar(obj, p, p_max_size);
2252 print_smapi(obj, p, p_max_size);
2254 OBJ(if_smapi_bat_installed) {
2256 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2257 if(!smapi_bat_installed(idx)) {
2261 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2263 OBJ(smapi_bat_perc) {
2264 print_smapi_bat_perc(obj, p, p_max_size);
2266 OBJ(smapi_bat_temp) {
2267 print_smapi_bat_temp(obj, p, p_max_size);
2269 OBJ(smapi_bat_power) {
2270 print_smapi_bat_power(obj, p, p_max_size);
2272 OBJ(smapi_bat_bar) {
2273 print_smapi_bat_bar(obj, p, p_max_size);
2278 char buf[max_user_text];
2280 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2281 snprintf(p, p_max_size, "%s", buf);
2287 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2288 char buf[max_user_text];
2291 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2292 snprintf(p, p_max_size, "%s", buf);
2293 if(total_updates % 2) {
2294 for(j=0; p[j] != 0; j++) {
2299 OBJ(to_bytes) {//commented code is original - not working when input value contains a decimal point
2300 char buf[max_user_text];
2303 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2305 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2306 // if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2307 // if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2308 // else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2309 // else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2310 // else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2311 // else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2313 if(sscanf(buf, "%Lf%s", &bytes, unit) == 2 && strlen(unit) < 16){
2314 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes);
2315 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024);
2316 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024);
2317 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024 * 1024);
2318 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024 * 1024 * 1024);
2320 snprintf(p, p_max_size, "%s", buf);
2323 print_scroll(obj, p, p_max_size, cur);
2326 print_combine(obj, p, cur);
2330 print_nvidia_value(obj, p, p_max_size);
2335 /* This is just a meta-object to set host:port */
2338 snprintf(p, p_max_size, "%s",
2339 cur->apcupsd.items[APCUPSD_NAME]);
2341 OBJ(apcupsd_model) {
2342 snprintf(p, p_max_size, "%s",
2343 cur->apcupsd.items[APCUPSD_MODEL]);
2345 OBJ(apcupsd_upsmode) {
2346 snprintf(p, p_max_size, "%s",
2347 cur->apcupsd.items[APCUPSD_UPSMODE]);
2349 OBJ(apcupsd_cable) {
2350 snprintf(p, p_max_size, "%s",
2351 cur->apcupsd.items[APCUPSD_CABLE]);
2353 OBJ(apcupsd_status) {
2354 snprintf(p, p_max_size, "%s",
2355 cur->apcupsd.items[APCUPSD_STATUS]);
2357 OBJ(apcupsd_linev) {
2358 snprintf(p, p_max_size, "%s",
2359 cur->apcupsd.items[APCUPSD_LINEV]);
2362 snprintf(p, p_max_size, "%s",
2363 cur->apcupsd.items[APCUPSD_LOAD]);
2365 OBJ(apcupsd_loadbar) {
2367 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2368 new_bar(obj, p, p_max_size, (int) progress);
2371 OBJ(apcupsd_loadgraph) {
2373 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2374 new_graph(obj, p, p_max_size, (int)progress);
2377 OBJ(apcupsd_loadgauge) {
2379 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2380 new_gauge(obj, p, p_max_size, (int)progress);
2382 OBJ(apcupsd_charge) {
2383 snprintf(p, p_max_size, "%s",
2384 cur->apcupsd.items[APCUPSD_CHARGE]);
2386 OBJ(apcupsd_timeleft) {
2387 snprintf(p, p_max_size, "%s",
2388 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2391 snprintf(p, p_max_size, "%s",
2392 cur->apcupsd.items[APCUPSD_TEMP]);
2394 OBJ(apcupsd_lastxfer) {
2395 snprintf(p, p_max_size, "%s",
2396 cur->apcupsd.items[APCUPSD_LASTXFER]);
2398 #endif /* APCUPSD */
2405 size_t a = strlen(p);
2408 iconv_convert(&a, buff_in, p, p_max_size);
2409 #endif /* HAVE_ICONV */
2410 if (obj->type == OBJ_execp || obj->type == OBJ_execpi || obj->type
2413 || obj->type == OBJ_lua || obj->type == OBJ_lua_parse
2414 #endif /* HAVE_LUA */
2416 substitute_newlines(p, a - 2);
2425 /* load any new fonts we may have had */
2426 if (need_to_load_fonts) {
2432 void evaluate(const char *text, char *p, int p_max_size)
2434 struct information *tmp_info;
2435 struct text_object subroot;
2437 tmp_info = malloc(sizeof(struct information));
2438 memcpy(tmp_info, &info, sizeof(struct information));
2439 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2440 DBGP2("evaluated '%s' to '%s'", text, p);
2442 free_text_objects(&subroot, 1);
2446 double current_update_time, next_update_time, last_update_time;
2448 static void generate_text(void)
2450 struct information *cur = &info;
2452 unsigned int i, j, k;
2458 current_update_time = get_time();
2462 /* add things to the buffer */
2468 generate_text_internal(p, max_user_text, global_root_object, cur);
2469 if(max_text_width > 0) {
2470 for(i = 0, j = 0; p[i] != 0; i++) {
2471 if(p[i] == '\n') j = 0;
2472 else if(j == max_text_width) {
2473 k = i + strlen(p + i) + 1;
2474 if(k < text_buffer_size) {
2481 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2486 if (stuff_in_uppercase) {
2489 tmp_p = text_buffer;
2491 *tmp_p = toupper(*tmp_p);
2496 next_update_time += update_interval;
2497 if (next_update_time < get_time()) {
2498 next_update_time = get_time() + update_interval;
2499 } else if (next_update_time > get_time() + update_interval) {
2500 next_update_time = get_time() + update_interval;
2502 last_update_time = current_update_time;
2506 void set_update_interval(double interval)
2508 update_interval = interval;
2509 update_interval_old = interval;
2512 int get_string_width(const char *s)
2514 return *s ? calc_text_width(s) : 0;
2518 static int get_string_width_special(char *s, int special_index)
2528 if ((output_methods & TO_X) == 0)
2531 p = strndup(s, text_buffer_size);
2535 if (*p == SPECIAL_CHAR) {
2536 /* shift everything over by 1 so that the special char
2537 * doesn't mess up the size calculation */
2538 for (i = 0; i < (long)strlen(p); i++) {
2539 *(p + i) = *(p + i + 1);
2541 if (specials[special_index + idx].type == GRAPH
2542 || specials[special_index + idx].type == GAUGE
2543 || specials[special_index + idx].type == BAR) {
2544 width += specials[special_index + idx].width;
2547 } else if (*p == SECRIT_MULTILINE_CHAR) {
2554 if (strlen(final) > 1) {
2555 width += calc_text_width(final);
2561 static int text_size_updater(char *s, int special_index);
2563 int last_font_height;
2564 static void update_text_area(void)
2568 if ((output_methods & TO_X) == 0)
2570 /* update text size if it isn't fixed */
2575 text_width = minimum_width;
2577 last_font_height = font_height();
2578 for_each_line(text_buffer, text_size_updater);
2580 if (text_height < minimum_height) {
2581 text_height = minimum_height;
2583 if (text_width > maximum_width && maximum_width > 0) {
2584 text_width = maximum_width;
2588 /* get text position on workarea */
2589 switch (text_alignment) {
2590 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2594 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2595 y = workarea[3] - text_height - gap_y;
2598 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2599 y = workarea[3] / 2 - text_height / 2 - gap_y;
2602 switch (text_alignment) {
2603 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2607 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2608 x = workarea[2] - text_width - gap_x;
2611 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2612 x = workarea[2] / 2 - text_width / 2 - gap_x;
2616 if (text_alignment == NONE) { // Let the WM manage the window
2623 #endif /* OWN_WINDOW */
2626 if (own_window && !fixed_pos) {
2629 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2630 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2631 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2632 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2636 /* If window size doesn't match to workarea's size,
2637 * then window probably includes panels (gnome).
2638 * Blah, doesn't work on KDE. */
2639 if (workarea[2] != window.width || workarea[3] != window.height) {
2648 /* update lua window globals */
2649 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2650 #endif /* HAVE_LUA */
2655 static int cur_x, cur_y; /* current x and y for drawing */
2657 //draw_mode also without X11 because we only need to print to stdout with FG
2658 static int draw_mode; /* FG, BG or OUTLINE */
2660 static long current_color;
2662 static int text_size_updater(char *s, int special_index)
2666 int contain_SECRIT_MULTILINE_CHAR = 0;
2669 if ((output_methods & TO_X) == 0)
2671 /* get string widths and skip specials */
2674 if (*p == SPECIAL_CHAR) {
2676 w += get_string_width(s);
2679 if (specials[special_index].type == BAR
2680 || specials[special_index].type == GAUGE
2681 || specials[special_index].type == GRAPH) {
2682 w += specials[special_index].width;
2683 if (specials[special_index].height > last_font_height) {
2684 last_font_height = specials[special_index].height;
2685 last_font_height += font_height();
2687 } else if (specials[special_index].type == OFFSET) {
2688 if (specials[special_index].arg > 0) {
2689 w += specials[special_index].arg;
2691 } else if (specials[special_index].type == VOFFSET) {
2692 last_font_height += specials[special_index].arg;
2693 } else if (specials[special_index].type == GOTO) {
2694 if (specials[special_index].arg > cur_x) {
2695 w = (int) specials[special_index].arg;
2697 } else if (specials[special_index].type == TAB) {
2698 int start = specials[special_index].arg;
2699 int step = specials[special_index].width;
2701 if (!step || step < 0) {
2704 w += step - (cur_x - text_start_x - start) % step;
2705 } else if (specials[special_index].type == FONT) {
2706 selected_font = specials[special_index].font_added;
2707 if (font_height() > last_font_height) {
2708 last_font_height = font_height();
2714 } else if (*p == SECRIT_MULTILINE_CHAR) {
2715 contain_SECRIT_MULTILINE_CHAR = 1;
2717 lw = get_string_width(s);
2718 *p = SECRIT_MULTILINE_CHAR;
2720 w = lw > w ? lw : w;
2721 text_height += last_font_height;
2725 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2726 if (contain_SECRIT_MULTILINE_CHAR) {
2727 lw = get_string_width(s);
2728 w = lw > w ? lw : w;
2730 w += get_string_width(s);
2732 if (w > text_width) {
2735 if (text_width > maximum_width && maximum_width) {
2736 text_width = maximum_width;
2739 text_height += last_font_height;
2740 last_font_height = font_height();
2741 return special_index;
2745 static inline void set_foreground_color(long c)
2748 if (output_methods & TO_X) {
2750 if (have_argb_visual) {
2751 current_color = c | (own_window_argb_value << 24);
2753 #endif /* USE_ARGB */
2757 #endif /* USE_ARGB */
2758 XSetForeground(display, window.gc, current_color);
2762 if (output_methods & TO_NCURSES) {
2763 attron(COLOR_PAIR(c));
2765 #endif /* NCURSES */
2770 static void draw_string(const char *s)
2772 int i, i2, pos, width_of_s;
2775 char *s_with_newlines;
2781 width_of_s = get_string_width(s);
2782 s_with_newlines = strdup(s);
2783 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2784 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2785 s_with_newlines[i] = '\n';
2788 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2789 printf("%s\n", s_with_newlines);
2790 if (extra_newline) fputc('\n', stdout);
2791 fflush(stdout); /* output immediately, don't buffer */
2793 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2794 fprintf(stderr, "%s\n", s_with_newlines);
2795 fflush(stderr); /* output immediately, don't buffer */
2797 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2798 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2800 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2801 fprintf(append_fpointer, "%s\n", s_with_newlines);
2804 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2805 printw("%s", s_with_newlines);
2808 free(s_with_newlines);
2809 memset(tmpstring1, 0, text_buffer_size);
2810 memset(tmpstring2, 0, text_buffer_size);
2811 strncpy(tmpstring1, s, text_buffer_size - 1);
2816 if (output_methods & TO_X) {
2817 max = ((text_width - width_of_s) / get_string_width(" "));
2820 /* This code looks for tabs in the text and coverts them to spaces.
2821 * The trick is getting the correct number of spaces, and not going
2822 * over the window's size without forcing the window larger. */
2823 for (i = 0; i < (int) text_buffer_size; i++) {
2824 if (tmpstring1[i] == '\t') {
2826 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2827 /* guard against overrun */
2828 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2833 /* guard against overrun */
2834 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2839 if (output_methods & TO_X) {
2840 if (text_width == maximum_width) {
2841 /* this means the text is probably pushing the limit,
2842 * so we'll chop it */
2843 while (cur_x + get_string_width(tmpstring2) - text_start_x
2844 > maximum_width && strlen(tmpstring2) > 0) {
2845 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2852 if (output_methods & TO_X) {
2858 c.pixel = current_color;
2859 // query color on custom colormap
2860 XQueryColor(display, window.colourmap, &c);
2863 c2.color.red = c.red;
2864 c2.color.green = c.green;
2865 c2.color.blue = c.blue;
2866 c2.color.alpha = fonts[selected_font].font_alpha;
2868 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2869 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2871 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2872 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2877 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2880 cur_x += width_of_s;
2883 memcpy(tmpstring1, s, text_buffer_size);
2886 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2894 int last_special_needed = -1;
2895 int orig_special_index = special_index;
2898 if (output_methods & TO_X) {
2899 font_h = font_height();
2900 cur_y += font_ascent();
2902 cur_x = text_start_x;
2906 if (*p == SECRIT_MULTILINE_CHAR) {
2907 /* special newline marker for multiline objects */
2912 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2917 /* draw string before special, unless we're dealing multiline
2919 if (last_special_applied > -1) {
2920 special_index = last_special_applied;
2928 switch (specials[special_index].type) {
2930 case HORIZONTAL_LINE:
2932 int h = specials[special_index].height;
2933 int mid = font_ascent() / 2;
2935 w = text_start_x + text_width - cur_x;
2937 XSetLineAttributes(display, window.gc, h, LineSolid,
2938 CapButt, JoinMiter);
2939 XDrawLine(display, window.drawable, window.gc, cur_x,
2940 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2946 int h = specials[special_index].height;
2947 int tmp_s = specials[special_index].arg;
2948 int mid = font_ascent() / 2;
2949 char ss[2] = { tmp_s, tmp_s };
2951 w = text_start_x + text_width - cur_x - 1;
2952 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2953 CapButt, JoinMiter);
2954 XSetDashes(display, window.gc, 0, ss, 2);
2955 XDrawLine(display, window.drawable, window.gc, cur_x,
2956 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2962 int h, bar_usage, by;
2963 if (cur_x - text_start_x > maximum_width
2964 && maximum_width > 0) {
2967 h = specials[special_index].height;
2968 bar_usage = specials[special_index].arg;
2969 by = cur_y - (font_ascent() / 2) - 1;
2974 w = specials[special_index].width;
2976 w = text_start_x + text_width - cur_x - 1;
2982 XSetLineAttributes(display, window.gc, 1, LineSolid,
2983 CapButt, JoinMiter);
2985 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2987 XFillRectangle(display, window.drawable, window.gc, cur_x,
2988 by, w * bar_usage / 255, h);
2996 case GAUGE: /* new GAUGE */
2999 unsigned long last_colour = current_color;
3001 float angle, px, py;
3005 if (cur_x - text_start_x > maximum_width
3006 && maximum_width > 0) {
3010 h = specials[special_index].height;
3011 by = cur_y - (font_ascent() / 2) - 1;
3016 w = specials[special_index].width;
3018 w = text_start_x + text_width - cur_x - 1;
3024 XSetLineAttributes(display, window.gc, 1, LineSolid,
3025 CapButt, JoinMiter);
3027 XDrawArc(display, window.drawable, window.gc,
3028 cur_x, by, w, h * 2, 0, 180*64);
3031 usage = specials[special_index].arg;
3032 angle = (M_PI)*(float)(usage)/255.;
3033 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3034 py = (float)(by+(h))-(float)(h)*sin(angle);
3036 XDrawLine(display, window.drawable, window.gc,
3037 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3045 set_foreground_color(last_colour);
3053 int h, by, i = 0, j = 0;
3055 unsigned long last_colour = current_color;
3056 unsigned long *tmpcolour = 0;
3057 if (cur_x - text_start_x > maximum_width
3058 && maximum_width > 0) {
3061 h = specials[special_index].height;
3062 by = cur_y - (font_ascent() / 2) - 1;
3067 w = specials[special_index].width;
3069 w = text_start_x + text_width - cur_x - 1;
3074 if (draw_graph_borders) {
3075 XSetLineAttributes(display, window.gc, 1, LineSolid,
3076 CapButt, JoinMiter);
3077 XDrawRectangle(display, window.drawable, window.gc,
3080 XSetLineAttributes(display, window.gc, 1, LineSolid,
3081 CapButt, JoinMiter);
3083 if (specials[special_index].last_colour != 0
3084 || specials[special_index].first_colour != 0) {
3085 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3088 for (i = w - 2; i > -1; i--) {
3089 if (specials[special_index].last_colour != 0
3090 || specials[special_index].first_colour != 0) {
3091 if (specials[special_index].tempgrad) {
3094 (int)((float)(w - 2) - specials[special_index].graph[j] *
3095 (w - 2) / (float)specials[special_index].graph_scale)
3099 (int)((float)(w - 2) - specials[special_index].graph[j] *
3100 (w - 2) / (float)specials[special_index].graph_scale)
3103 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3105 (int)((float)(w - 2) - specials[special_index].graph[j] *
3106 (w - 2) / (float)specials[special_index].graph_scale)
3110 #endif /* DEBUG_lol */
3111 set_foreground_color(tmpcolour[
3112 (int)((float)(w - 2) -
3113 specials[special_index].graph[j]
3115 (float)specials[special_index].graph_scale)
3118 set_foreground_color(tmpcolour[colour_idx++]);
3121 /* this is mugfugly, but it works */
3122 XDrawLine(display, window.drawable, window.gc,
3123 cur_x + i + 1, by + h, cur_x + i + 1,
3124 round_to_int((double)by + h - specials[special_index].graph[j] *
3125 (h - 1) / specials[special_index].graph_scale));
3126 if ((w - i) / ((float) (w - 2) /
3127 (specials[special_index].graph_width)) > j
3128 && j < MAX_GRAPH_DEPTH - 3) {
3132 if (tmpcolour) free(tmpcolour);
3137 /* if (draw_mode == BG) {
3138 set_foreground_color(default_bg_color);
3139 } else if (draw_mode == OUTLINE) {
3140 set_foreground_color(default_out_color);
3142 set_foreground_color(default_fg_color);
3144 if (show_graph_range) {
3147 unsigned short int seconds = update_interval * w;
3153 unsigned short int timeunits;
3155 timeunits = seconds / 86400; seconds %= 86400;
3156 if (timeunits > 0) {
3157 if (asprintf(&tmp_day_str, "%dd", timeunits) < 0) {
3161 tmp_day_str = strdup("");
3163 timeunits = seconds / 3600; seconds %= 3600;
3164 if (timeunits > 0) {
3165 if (asprintf(&tmp_hour_str, "%dh", timeunits) < 0) {
3169 tmp_hour_str = strdup("");
3171 timeunits = seconds / 60; seconds %= 60;
3172 if (timeunits > 0) {
3173 if (asprintf(&tmp_min_str, "%dm", timeunits) < 0) {
3177 tmp_min_str = strdup("");
3180 if (asprintf(&tmp_sec_str, "%ds", seconds) < 0) {
3184 tmp_sec_str = strdup("");
3186 if (asprintf(&tmp_str, "%s%s%s%s", tmp_day_str,
3187 tmp_hour_str, tmp_min_str, tmp_sec_str) < 0) {
3190 #define FREE(a) if ((a)) free((a));
3191 FREE(tmp_day_str); FREE(tmp_hour_str); FREE(tmp_min_str); FREE(tmp_sec_str);
3193 tmp_str = strdup("Range not possible"); /* should never happen, but better safe then sorry */
3195 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3196 cur_y += font_h / 2;
3197 draw_string(tmp_str);
3204 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3208 cur_x += font_ascent() / 2;
3209 cur_y += font_h / 2;
3211 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3213 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3214 draw_string(tmp_str);
3220 set_foreground_color(last_colour);
3226 int old = font_ascent();
3228 cur_y -= font_ascent();
3229 selected_font = specials[special_index].font_added;
3231 if (cur_y + font_ascent() < cur_y + old) {
3234 cur_y += font_ascent();
3236 font_h = font_height();
3241 if (draw_mode == FG) {
3242 set_foreground_color(specials[special_index].arg);
3248 if (draw_mode == BG) {
3249 set_foreground_color(specials[special_index].arg);
3254 if (draw_mode == OUTLINE) {
3255 set_foreground_color(specials[special_index].arg);
3260 w += specials[special_index].arg;
3261 last_special_needed = special_index;
3265 cur_y += specials[special_index].arg;
3269 if (specials[special_index].arg >= 0) {
3270 cur_x = (int) specials[special_index].arg;
3272 last_special_needed = special_index;
3277 int start = specials[special_index].arg;
3278 int step = specials[special_index].width;
3280 if (!step || step < 0) {
3283 w = step - (cur_x - text_start_x - start) % step;
3284 last_special_needed = special_index;
3290 /* TODO: add back in "+ window.border_inner_margin" to the end of
3292 int pos_x = text_start_x + text_width -
3293 get_string_width_special(s, special_index);
3295 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3296 "get_string_width(p) %i gap_x %i "
3297 "specials[special_index].arg %i window.border_inner_margin %i "
3298 "window.border_width %i\n", pos_x, text_start_x, text_width,
3299 cur_x, get_string_width_special(s), gap_x,
3300 specials[special_index].arg, window.border_inner_margin,
3301 window.border_width); */
3302 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3303 cur_x = pos_x - specials[special_index].arg;
3305 last_special_needed = special_index;
3311 int pos_x = (text_width) / 2 - get_string_width_special(s,
3312 special_index) / 2 - (cur_x -
3314 /* int pos_x = text_start_x + text_width / 2 -
3315 get_string_width_special(s) / 2; */
3317 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3318 "get_string_width(p) %i gap_x %i "
3319 "specials[special_index].arg %i\n", pos_x, text_start_x,
3320 text_width, cur_x, get_string_width(s), gap_x,
3321 specials[special_index].arg); */
3322 if (pos_x > specials[special_index].arg) {
3323 w = pos_x - specials[special_index].arg;
3325 last_special_needed = special_index;
3335 if (special_index != last_special_applied) {
3338 special_index = orig_special_index;
3339 last_special_applied = -1;
3350 if (output_methods & TO_NCURSES) {
3353 #endif /* NCURSES */
3355 if (output_methods & TO_X)
3356 cur_y += font_descent();
3358 if (recurse && *recurse) {
3359 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3360 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3362 return special_index;
3365 static int draw_line(char *s, int special_index)
3368 if (output_methods & TO_X) {
3369 return draw_each_line_inner(s, special_index, -1);
3373 if (output_methods & TO_NCURSES) {
3374 return draw_each_line_inner(s, special_index, -1);
3376 #endif /* NCURSES */
3378 UNUSED(special_index);
3382 static void draw_text(void)
3386 llua_draw_pre_hook();
3387 #endif /* HAVE_LUA */
3388 if (output_methods & TO_X) {
3389 cur_y = text_start_y;
3392 if (draw_borders && window.border_width > 0) {
3393 if (stippled_borders) {
3394 char ss[2] = { stippled_borders, stippled_borders };
3395 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3396 CapButt, JoinMiter);
3397 XSetDashes(display, window.gc, 0, ss, 2);
3399 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3400 CapButt, JoinMiter);
3403 XDrawRectangle(display, window.drawable, window.gc,
3404 text_start_x - window.border_inner_margin - window.border_width,
3405 text_start_y - window.border_inner_margin - window.border_width,
3406 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3407 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3415 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3416 attron(COLOR_PAIR(COLOR_WHITE));
3417 #endif /* NCURSES */
3418 for_each_line(text_buffer, draw_line);
3419 #if defined(HAVE_LUA) && defined(X11)
3420 llua_draw_post_hook();
3421 #endif /* HAVE_LUA */
3424 static void draw_stuff(void)
3427 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3429 if (overwrite_file) {
3430 overwrite_fpointer = fopen(overwrite_file, "w");
3431 if(!overwrite_fpointer)
3432 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3435 append_fpointer = fopen(append_file, "a");
3436 if(!append_fpointer)
3437 NORM_ERR("Can't append '%s' anymore", append_file);
3440 if (output_methods & TO_X) {
3442 if (draw_shades && !draw_outline) {
3445 set_foreground_color(default_bg_color);
3456 for (i = -1; i < 2; i++) {
3457 for (j = -1; j < 2; j++) {
3458 if (i == 0 && j == 0) {
3463 set_foreground_color(default_out_color);
3464 draw_mode = OUTLINE;
3472 set_foreground_color(default_fg_color);
3477 #if defined(X11) && defined(HAVE_XDBE)
3478 if (output_methods & TO_X) {
3479 xdbe_swap_buffers();
3481 #endif /* X11 && HAVE_XDBE */
3482 if(overwrite_fpointer) {
3483 fclose(overwrite_fpointer);
3484 overwrite_fpointer = 0;
3486 if(append_fpointer) {
3487 fclose(append_fpointer);
3488 append_fpointer = 0;
3493 static void clear_text(int exposures)
3498 The swap action is XdbeBackground, which clears
3501 if (display && window.back_buffer) { // make sure these are !null
3502 /* there is some extra space for borders and outlines */
3503 XFillRectangle(display, window.back_buffer, window.gc_back,
3504 text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3505 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3506 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3507 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2);
3513 if (display && window.window) { // make sure these are !null
3514 /* there is some extra space for borders and outlines */
3515 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3516 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3517 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3518 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3523 static int need_to_update;
3525 /* update_text() generates new text and clears old text area */
3526 static void update_text(void)
3533 if (output_methods & TO_X)
3538 llua_update_info(&info, update_interval);
3539 #endif /* HAVE_LUA */
3542 #ifdef HAVE_SYS_INOTIFY_H
3546 static void main_loop(void)
3549 #ifdef SIGNAL_BLOCKING
3550 sigset_t newmask, oldmask;
3553 #ifdef HAVE_SYS_INOTIFY_H
3554 int inotify_config_wd = -1;
3555 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3556 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3557 char inotify_buff[INOTIFY_BUF_LEN];
3558 #endif /* HAVE_SYS_INOTIFY_H */
3561 #ifdef SIGNAL_BLOCKING
3562 sigemptyset(&newmask);
3563 sigaddset(&newmask, SIGINT);
3564 sigaddset(&newmask, SIGTERM);
3565 sigaddset(&newmask, SIGUSR1);
3568 last_update_time = 0.0;
3569 next_update_time = get_time();
3571 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3572 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3573 char buf[max_user_text];
3575 get_battery_short_status(buf, max_user_text, "bq27200-0"); //why is this even hardcoded here? oh well.
3577 update_interval = update_interval_bat;
3579 update_interval = update_interval_old;
3584 #ifdef SIGNAL_BLOCKING
3585 /* block signals. we will inspect for pending signals later */
3586 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3587 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3592 if (output_methods & TO_X) {
3595 /* wait for X event or timeout */
3597 if (!XPending(display)) {
3601 t = next_update_time - get_time();
3605 } else if (t > update_interval) {
3606 t = update_interval;
3609 tv.tv_sec = (long) t;
3610 tv.tv_usec = (long) (t * 1000000) % 1000000;
3612 FD_SET(ConnectionNumber(display), &fdsr);
3614 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3616 if (errno != EINTR) {
3617 NORM_ERR("can't select(): %s", strerror(errno));
3627 if (need_to_update) {
3629 int wx = window.x, wy = window.y;
3639 /* resize window if it isn't right size */
3641 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3642 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3643 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3644 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3645 draw_stuff(); /* redraw everything in our newly sized window */
3646 XResizeWindow(display, window.window, window.width,
3647 window.height); /* resize window */
3648 fprintf(stderr, PACKAGE_NAME": resizing window to %d x %d\n",window.width,window.height);
3651 XFreePixmap(display, window.back_buffer);
3652 window.back_buffer = XCreatePixmap(display, window.window,
3653 window.width, window.height, window.depth);
3654 if (window.back_buffer != None)
3655 window.drawable = window.back_buffer;
3657 window.drawable = window.window;
3662 cimlib_init(display, window.drawable, window.visual, window.colourmap);
3667 set_transparent_background(window.window, own_window_argb_value);
3670 xdbe_swap_buffers();
3675 /* update lua window globals */
3676 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3677 #endif /* HAVE_LUA */
3680 /* move window if it isn't in right position */
3681 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3682 XMoveWindow(display, window.window, window.x, window.y);
3687 if (changed && window.type == TYPE_PANEL) {
3690 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3693 switch (text_alignment) {
3720 set_struts(sidenum);
3731 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3732 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3733 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3734 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3735 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3740 /* handle X events */
3741 while (XPending(display)) {
3744 XNextEvent(display, &ev);
3751 r.width = ev.xexpose.width;
3752 r.height = ev.xexpose.height;
3753 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3757 case PropertyNotify:
3759 if ( ev.xproperty.state == PropertyNewValue ) {
3760 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3766 case ReparentNotify:
3767 /* make background transparent */
3769 set_transparent_background(window.window, own_window_argb_value);
3773 case ConfigureNotify:
3775 /* if window size isn't what expected, set fixed size */
3776 if (ev.xconfigure.width != window.width
3777 || ev.xconfigure.height != window.height) {
3778 if (window.width != 0 && window.height != 0) {
3782 /* clear old stuff before screwing up
3787 XWindowAttributes attrs;
3788 if (XGetWindowAttributes(display,
3789 window.window, &attrs)) {
3790 window.width = attrs.width;
3791 window.height = attrs.height;
3792 fprintf(stderr, PACKAGE_NAME": x11? resized our window to %d x %d\n",window.width,window.height);
3796 XFreePixmap(display, window.back_buffer);
3797 window.back_buffer = XCreatePixmap(display, window.window,
3798 window.width, window.height, window.depth);
3799 if (window.back_buffer != None)
3800 window.drawable = window.back_buffer;
3802 window.drawable = window.window;
3807 cimlib_init(display, window.drawable, window.visual, window.colourmap);
3815 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3816 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3817 if (text_width > maximum_width
3818 && maximum_width > 0) {
3819 text_width = maximum_width;
3823 /* if position isn't what expected, set fixed pos
3824 * total_updates avoids setting fixed_pos when window
3825 * is set to weird locations when started */
3826 /* // this is broken
3827 if (total_updates >= 2 && !fixed_pos
3828 && (window.x != ev.xconfigure.x
3829 || window.y != ev.xconfigure.y)
3830 && (ev.xconfigure.x != 0
3831 || ev.xconfigure.y != 0)) {
3839 /* if an ordinary window with decorations */
3840 if ((window.type == TYPE_NORMAL &&
3841 (!TEST_HINT(window.hints,
3842 HINT_UNDECORATED))) ||
3843 window.type == TYPE_DESKTOP) {
3844 /* allow conky to hold input focus. */
3846 } else if(TEST_HINT(window.hints,HINT_FULLSCREEN)) {
3847 //detect top right click
3848 if (ev.xbutton.x > 750 && ev.xbutton.y < 50)
3852 if (ev.xbutton.x <= 50 && ev.xbutton.y <= 50)
3853 { //minimize / goto task switcher
3854 dbus_exit_app_view();
3858 /* forward the click to the desktop window */
3859 XUngrabPointer(display, ev.xbutton.time);
3860 ev.xbutton.window = window.desktop;
3861 ev.xbutton.x = ev.xbutton.x_root;
3862 ev.xbutton.y = ev.xbutton.y_root;
3863 XSendEvent(display, ev.xbutton.window, False,
3864 ButtonPressMask, &ev);
3865 XSetInputFocus(display, ev.xbutton.window,
3866 RevertToParent, ev.xbutton.time);
3873 /* if an ordinary window with decorations */
3874 if ((window.type == TYPE_NORMAL)
3875 && (!TEST_HINT(window.hints,
3876 HINT_UNDECORATED))) {
3877 /* allow conky to hold input focus. */
3880 /* forward the release to the desktop window */
3881 ev.xbutton.window = window.desktop;
3882 ev.xbutton.x = ev.xbutton.x_root;
3883 ev.xbutton.y = ev.xbutton.y_root;
3884 XSendEvent(display, ev.xbutton.window, False,
3885 ButtonReleaseMask, &ev);
3894 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3895 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3897 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3898 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3900 #endif /* HAVE_XDAMAGE */
3906 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3907 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3908 #endif /* HAVE_XDAMAGE */
3910 /* XDBE doesn't seem to provide a way to clear the back buffer
3911 * without interfering with the front buffer, other than passing
3912 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3913 * using XDBE, we need to redraw the text even if it wasn't part of
3914 * the exposed area. OTOH, if we're not going to call draw_stuff at
3915 * all, then no swap happens and we can safely do nothing. */
3917 if (!XEmptyRegion(x11_stuff.region)) {
3922 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3923 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3924 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3925 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3926 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3929 XSetRegion(display, window.gc, x11_stuff.region);
3932 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3936 XDestroyRegion(x11_stuff.region);
3937 x11_stuff.region = XCreateRegion();
3941 t = (next_update_time - get_time()) * 1000000;
3942 fprintf(stderr, PACKAGE_NAME": trying to sleep %d microseconds\n",t);
3943 if(t > 0) usleep((useconds_t)t);
3947 if(output_methods & TO_NCURSES) {
3956 #ifdef SIGNAL_BLOCKING
3957 /* unblock signals of interest and let handler fly */
3958 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3959 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3963 switch (g_signal_pending) {
3966 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3971 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3974 if (output_methods & TO_X) {
3975 XDestroyRegion(x11_stuff.region);
3976 x11_stuff.region = NULL;
3978 XDamageDestroy(display, x11_stuff.damage);
3979 XFixesDestroyRegion(display, x11_stuff.region2);
3980 XFixesDestroyRegion(display, x11_stuff.part);
3981 #endif /* HAVE_XDAMAGE */
3987 if(overwrite_file) {
3988 free(overwrite_file);
3997 /* Reaching here means someone set a signal
3998 * (SIGXXXX, signal_handler), but didn't write any code
4000 * If you don't want to handle a signal, don't set a handler on
4001 * it in the first place. */
4002 if (g_signal_pending) {
4003 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4007 #ifdef HAVE_SYS_INOTIFY_H
4008 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4009 inotify_config_wd = inotify_add_watch(inotify_fd,
4013 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4014 int len = 0, idx = 0;
4016 struct timeval time_to_wait;
4018 FD_ZERO(&descriptors);
4019 FD_SET(inotify_fd, &descriptors);
4021 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4023 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4024 if (FD_ISSET(inotify_fd, &descriptors)) {
4025 /* process inotify events */
4026 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4027 while (len > 0 && idx < len) {
4028 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4029 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4030 /* current_config should be reloaded */
4031 NORM_ERR("'%s' modified, reloading...", current_config);
4033 if (ev->mask & IN_IGNORED) {
4034 /* for some reason we get IN_IGNORED here
4035 * sometimes, so we need to re-add the watch */
4036 inotify_config_wd = inotify_add_watch(inotify_fd,
4044 llua_inotify_query(ev->wd, ev->mask);
4046 #endif /* HAVE_LUA */
4047 idx += INOTIFY_EVENT_SIZE + ev->len;
4050 } else if (disable_auto_reload && inotify_fd != -1) {
4051 inotify_rm_watch(inotify_fd, inotify_config_wd);
4053 inotify_fd = inotify_config_wd = 0;
4055 #endif /* HAVE_SYS_INOTIFY_H */
4058 llua_update_info(&info, update_interval);
4059 #endif /* HAVE_LUA */
4060 g_signal_pending = 0;
4062 clean_up(current_mail_spool, NULL);
4064 #ifdef HAVE_SYS_INOTIFY_H
4065 if (inotify_fd != -1) {
4066 inotify_rm_watch(inotify_fd, inotify_config_wd);
4068 inotify_fd = inotify_config_wd = 0;
4070 #endif /* HAVE_SYS_INOTIFY_H */
4074 static void load_config_file_x11(const char *);
4076 void initialisation(int argc, char** argv);
4078 /* reload the config file */
4079 static void reload_config(void)
4081 char *current_config_copy = strdup(current_config);
4082 clean_up(NULL, NULL);
4083 sleep(1); /* slight pause */
4084 current_config = current_config_copy;
4085 initialisation(argc_copy, argv_copy);
4089 void clean_up_x11(void)
4091 if(window_created == 1) {
4092 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4093 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4094 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4095 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4100 if(x11_stuff.region) {
4101 XDestroyRegion(x11_stuff.region);
4102 x11_stuff.region = NULL;
4105 XCloseDisplay(display);
4108 if(info.x11.desktop.all_names) {
4109 free(info.x11.desktop.all_names);
4110 info.x11.desktop.all_names = NULL;
4112 if (info.x11.desktop.name) {
4113 free(info.x11.desktop.name);
4114 info.x11.desktop.name = NULL;
4120 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4124 if(output_methods & TO_NCURSES) {
4128 conftree_empty(currentconffile);
4129 currentconffile = NULL;
4136 timed_thread_destroy_registered_threads();
4138 if (info.cpu_usage) {
4139 free(info.cpu_usage);
4140 info.cpu_usage = NULL;
4143 if (x_initialised == YES) {
4146 free(fonts); //in set_default_configurations a font is set but not loaded
4151 set_nvidia_display(NULL);
4157 free_text_objects(&global_root_object, 0);
4176 free(current_config);
4179 #ifdef TCP_PORT_MONITOR
4180 tcp_portmon_clear();
4189 weather_free_info();
4192 llua_shutdown_hook();
4194 #endif /* HAVE_LUA */
4196 if (output_methods & TO_X)
4204 for (i = 0; i < special_count; i++) {
4205 if (specials[i].type == GRAPH) {
4206 free(specials[i].graph);
4214 clear_diskio_stats();
4215 if(global_cpu != NULL) {
4221 void clean_up(void *memtofree1, void* memtofree2)
4223 free_update_callbacks();
4224 clean_up_without_threads(memtofree1, memtofree2);
4227 static int string_to_bool(const char *s)
4230 // Assumes an option without a true/false means true
4232 } else if (strcasecmp(s, "yes") == EQUAL) {
4234 } else if (strcasecmp(s, "true") == EQUAL) {
4236 } else if (strcasecmp(s, "1") == EQUAL) {
4243 static enum alignment string_to_alignment(const char *s)
4245 if (strcasecmp(s, "top_left") == EQUAL) {
4247 } else if (strcasecmp(s, "top_right") == EQUAL) {
4249 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4251 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4253 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4254 return BOTTOM_RIGHT;
4255 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4256 return BOTTOM_MIDDLE;
4257 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4259 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4260 return MIDDLE_RIGHT;
4261 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4262 return MIDDLE_MIDDLE;
4263 } else if (strcasecmp(s, "tl") == EQUAL) {
4265 } else if (strcasecmp(s, "tr") == EQUAL) {
4267 } else if (strcasecmp(s, "tm") == EQUAL) {
4269 } else if (strcasecmp(s, "bl") == EQUAL) {
4271 } else if (strcasecmp(s, "br") == EQUAL) {
4272 return BOTTOM_RIGHT;
4273 } else if (strcasecmp(s, "bm") == EQUAL) {
4274 return BOTTOM_MIDDLE;
4275 } else if (strcasecmp(s, "ml") == EQUAL) {
4277 } else if (strcasecmp(s, "mr") == EQUAL) {
4278 return MIDDLE_RIGHT;
4279 } else if (strcasecmp(s, "mm") == EQUAL) {
4280 return MIDDLE_MIDDLE;
4281 } else if (strcasecmp(s, "none") == EQUAL) {
4284 return ALIGNMENT_ERROR;
4289 static void set_default_configurations_for_x(void)
4291 default_fg_color = WhitePixel(display, screen);
4292 default_bg_color = BlackPixel(display, screen);
4293 default_out_color = BlackPixel(display, screen);
4294 color0 = default_fg_color;
4295 color1 = default_fg_color;
4296 color2 = default_fg_color;
4297 color3 = default_fg_color;
4298 color4 = default_fg_color;
4299 color5 = default_fg_color;
4300 color6 = default_fg_color;
4301 color7 = default_fg_color;
4302 color8 = default_fg_color;
4303 color9 = default_fg_color;
4304 current_text_color = default_fg_color;
4308 static void set_default_configurations(void)
4315 fork_to_background = 0;
4316 total_run_times = 0;
4317 info.cpu_avg_samples = 2;
4318 info.net_avg_samples = 2;
4319 info.diskio_avg_samples = 2;
4324 format_human_readable = 1;
4332 mpd_env_host = getenv("MPD_HOST");
4333 mpd_env_port = getenv("MPD_PORT");
4335 if (!mpd_env_host || !strlen(mpd_env_host)) {
4336 mpd_set_host("localhost");
4338 /* MPD_HOST environment variable is set */
4339 char *mpd_hostpart = strchr(mpd_env_host, '@');
4340 if (!mpd_hostpart) {
4341 mpd_set_host(mpd_env_host);
4343 /* MPD_HOST contains a password */
4344 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4345 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4347 if (!strlen(mpd_hostpart + 1)) {
4348 mpd_set_host("localhost");
4350 mpd_set_host(mpd_hostpart + 1);
4353 mpd_set_password(mpd_password, 1);
4358 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4359 /* failed to set port from environment variable */
4360 mpd_set_port("6600");
4364 info.xmms2.artist = NULL;
4365 info.xmms2.album = NULL;
4366 info.xmms2.title = NULL;
4367 info.xmms2.genre = NULL;
4368 info.xmms2.comment = NULL;
4369 info.xmms2.url = NULL;
4370 info.xmms2.status = NULL;
4371 info.xmms2.playlist = NULL;
4373 use_spacer = NO_SPACER;
4375 output_methods = TO_X;
4377 output_methods = TO_STDOUT;
4383 show_graph_scale = 0;
4384 show_graph_range = 0;
4387 draw_graph_borders = 1;
4389 set_first_font("6x10");
4397 window.type = TYPE_NORMAL;
4399 strcpy(window.class_name, PACKAGE_NAME);
4400 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4402 use_argb_visual = 0;
4403 own_window_argb_value = 255;
4406 stippled_borders = 0;
4407 window.border_inner_margin = 3;
4408 window.border_outer_margin = 1;
4409 window.border_width = 1;
4410 text_alignment = BOTTOM_LEFT;
4411 info.x11.monitor.number = 1;
4412 info.x11.monitor.current = 0;
4413 info.x11.desktop.current = 1;
4414 info.x11.desktop.number = 1;
4415 info.x11.desktop.nitems = 0;
4416 info.x11.desktop.all_names = NULL;
4417 info.x11.desktop.name = NULL;
4422 free(current_mail_spool);
4426 variable_substitute(MAIL_FILE, buf, 256);
4427 if (buf[0] != '\0') {
4428 current_mail_spool = strndup(buf, text_buffer_size);
4433 set_update_interval(3);
4434 update_interval_bat = NOBATTERY;
4435 info.music_player_interval = 1.0;
4436 stuff_in_uppercase = 0;
4437 info.users.number = 1;
4439 set_times_in_seconds(0);
4441 #ifdef TCP_PORT_MONITOR
4442 /* set default connection limit */
4443 tcp_portmon_set_max_connections(0);
4447 /* returns 1 if you can overwrite or create the file at 'path' */
4448 static _Bool overwrite_works(const char *path)
4452 if (!(filepointer = fopen(path, "w")))
4454 fclose(filepointer);
4458 /* returns 1 if you can append or create the file at 'path' */
4459 static _Bool append_works(const char *path)
4463 if (!(filepointer = fopen(path, "a")))
4465 fclose(filepointer);
4471 /* WARNING, this type not in Xlib spec */
4472 int x11_error_handler(Display *d, XErrorEvent *err)
4473 __attribute__((noreturn));
4474 int x11_error_handler(Display *d, XErrorEvent *err)
4476 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",
4478 (long unsigned)err->display,
4479 (long)err->resourceid,
4489 int x11_ioerror_handler(Display *d)
4490 __attribute__((noreturn));
4491 int x11_ioerror_handler(Display *d)
4493 NORM_ERR("X Error: Display %lx\n",
4500 static void X11_initialisation(void)
4502 if (x_initialised == YES) return;
4503 output_methods |= TO_X;
4505 set_default_configurations_for_x();
4506 x_initialised = YES;
4509 /* WARNING, this type not in Xlib spec */
4510 XSetErrorHandler(&x11_error_handler);
4511 XSetIOErrorHandler(&x11_ioerror_handler);
4515 static char **xargv = 0;
4516 static int xargc = 0;
4518 static void X11_create_window(void)
4520 if (output_methods & TO_X) {
4522 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4523 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4525 #else /* OWN_WINDOW */
4526 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4527 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4529 #endif /* OWN_WINDOW */
4533 update_text_area(); /* to position text/window on screen */
4536 if (own_window && !fixed_pos) {
4537 XMoveWindow(display, window.window, window.x, window.y);
4540 set_transparent_background(window.window, own_window_argb_value);
4548 x11_stuff.region = XCreateRegion();
4550 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4551 NORM_ERR("Xdamage extension unavailable");
4553 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4554 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4555 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4556 #endif /* HAVE_XDAMAGE */
4559 update_text_area(); /* to get initial size of the window */
4562 /* setup lua window globals */
4563 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4564 #endif /* HAVE_LUA */
4568 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4569 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4570 #define CONF2(a) if (strcasecmp(name, a) == 0)
4571 #define CONF(a) else CONF2(a)
4572 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4573 || strcasecmp(name, b) == 0)
4574 #define CONF_CONTINUE 1
4575 #define CONF_BREAK 2
4576 #define CONF_BUFF_SIZE 512
4578 static FILE *open_config_file(const char *f)
4580 #ifdef CONFIG_OUTPUT
4581 if (!strcmp(f, "==builtin==")) {
4582 return conf_cookie_open();
4584 #endif /* CONFIG_OUTPUT */
4585 return fopen(f, "r");
4588 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4592 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4595 remove_comments(buf);
4600 while (*p && isspace((int) *p)) {
4604 return CONF_CONTINUE; /* empty line */
4611 while (*p2 && !isspace((int) *p2)) {
4615 *p2 = '\0'; /* break at name's end */
4622 while (*p && isspace((int) *p)) {
4628 p2 = *value + strlen(*value);
4629 while (isspace((int) *(p2 - 1))) {
4639 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4641 if (windowtype == TYPE_DOCK) {
4642 NORM_ERR("alignment is disabled when own_window_type is dock");
4644 #endif /*OWN_WINDOW */
4646 int a = string_to_alignment(value);
4649 if (setbyconffile) {
4651 } else NORM_ERR("'%s' is not a alignment setting", value);
4653 *ltext_alignment = a;
4655 } else if (setbyconffile) {
4661 char load_config_file(const char *f)
4666 fp = open_config_file(f);
4670 DBGP("reading contents from config file '%s'", f);
4673 char buff[CONF_BUFF_SIZE], *name, *value;
4674 int ret = do_config_step(&line, fp, buff, &name, &value);
4675 if (ret == CONF_BREAK) {
4677 } else if (ret == CONF_CONTINUE) {
4683 if (string_to_bool(value)) {
4684 output_methods &= TO_X;
4687 output_methods &= ~TO_X;
4688 x_initialised = NEVER;
4692 if (!value || x_initialised == YES) {
4697 disp = strdup(value);
4701 CONF("nvidia_display") {
4703 set_nvidia_display(value);
4707 setalignment(&text_alignment, window.type, value, f, line, 1);
4709 CONF("background") {
4710 fork_to_background = string_to_bool(value);
4713 CONF2("background") {
4714 fork_to_background = string_to_bool(value);
4718 CONF("show_graph_scale") {
4719 show_graph_scale = string_to_bool(value);
4721 CONF("show_graph_range") {
4722 show_graph_range = string_to_bool(value);
4724 CONF("border_inner_margin") {
4726 window.border_inner_margin = strtol(value, 0, 0);
4727 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4732 CONF("border_outer_margin") {
4734 window.border_outer_margin = strtol(value, 0, 0);
4735 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4740 CONF("border_width") {
4742 window.border_width = strtol(value, 0, 0);
4743 if (window.border_width < 1) window.border_width = 1;
4749 #define TEMPLATE_CONF(n) \
4750 CONF("template"#n) { \
4751 if (set_template(n, value)) \
4766 parse_global_imap_mail_args(value);
4773 parse_global_pop3_mail_args(value);
4778 CONF("default_bar_size") {
4781 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4788 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4792 CONF("default_graph_size") {
4795 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4802 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4805 CONF("default_gauge_size") {
4808 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4815 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4822 mpd_set_host(value);
4828 if (value && mpd_set_port(value)) {
4832 CONF("mpd_password") {
4834 mpd_set_password(value, 0);
4840 CONF("music_player_interval") {
4842 info.music_player_interval = strtod(value, 0);
4848 CONF("sensor_device") {
4850 sensor_device = strtol(value, 0, 0);
4856 CONF("cpu_avg_samples") {
4858 cpu_avg_samples = strtol(value, 0, 0);
4859 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4862 info.cpu_avg_samples = cpu_avg_samples;
4868 CONF("net_avg_samples") {
4870 net_avg_samples = strtol(value, 0, 0);
4871 if (net_avg_samples < 1 || net_avg_samples > 14) {
4874 info.net_avg_samples = net_avg_samples;
4880 CONF("diskio_avg_samples") {
4882 diskio_avg_samples = strtol(value, 0, 0);
4883 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4886 info.diskio_avg_samples = diskio_avg_samples;
4894 CONF("double_buffer") {
4895 use_xdbe = string_to_bool(value);
4899 CONF("override_utf8_locale") {
4900 utf8_mode = string_to_bool(value);
4902 CONF("draw_borders") {
4903 draw_borders = string_to_bool(value);
4905 CONF("draw_graph_borders") {
4906 draw_graph_borders = string_to_bool(value);
4908 CONF("draw_shades") {
4909 draw_shades = string_to_bool(value);
4911 CONF("draw_outline") {
4912 draw_outline = string_to_bool(value);
4915 CONF("times_in_seconds") {
4916 set_times_in_seconds(string_to_bool(value));
4918 CONF("max_text_width") {
4919 max_text_width = atoi(value);
4921 CONF("out_to_console") {
4922 if(string_to_bool(value)) {
4923 output_methods |= TO_STDOUT;
4925 output_methods &= ~TO_STDOUT;
4928 CONF("extra_newline") {
4929 extra_newline = string_to_bool(value);
4931 CONF("disable_auto_reload") {
4932 disable_auto_reload = string_to_bool(value);
4934 CONF("out_to_stderr") {
4935 if(string_to_bool(value))
4936 output_methods |= TO_STDERR;
4939 CONF("out_to_ncurses") {
4940 if(string_to_bool(value)) {
4943 output_methods |= TO_NCURSES;
4947 CONF("overwrite_file") {
4948 if(overwrite_file) {
4949 free(overwrite_file);
4952 if(overwrite_works(value)) {
4953 overwrite_file = strdup(value);
4954 output_methods |= OVERWRITE_FILE;
4956 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4958 CONF("append_file") {
4963 if(append_works(value)) {
4964 append_file = strdup(value);
4965 output_methods |= APPEND_FILE;
4967 NORM_ERR("append_file won't be able to create/append '%s'", value);
4969 CONF("use_spacer") {
4971 if (strcasecmp(value, "left") == EQUAL) {
4972 use_spacer = LEFT_SPACER;
4973 } else if (strcasecmp(value, "right") == EQUAL) {
4974 use_spacer = RIGHT_SPACER;
4975 } else if (strcasecmp(value, "none") == EQUAL) {
4976 use_spacer = NO_SPACER;
4978 use_spacer = string_to_bool(value);
4979 NORM_ERR("use_spacer should have an argument of left, right, or"
4980 " none. '%s' seems to be some form of '%s', so"
4981 " defaulting to %s.", value,
4982 use_spacer ? "true" : "false",
4983 use_spacer ? "right" : "none");
4985 use_spacer = RIGHT_SPACER;
4987 use_spacer = NO_SPACER;
4991 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4992 use_spacer = RIGHT_SPACER;
4998 use_xft = string_to_bool(value);
5002 set_first_font(value);
5006 if (value && font_count >= 0) {
5007 fonts[0].font_alpha = atof(value) * 65535.0;
5014 if (string_to_bool(value)) {
5015 NORM_ERR("Xft not enabled at compile time");
5019 /* xftfont silently ignored when no Xft */
5022 /* xftalpha is silently ignored when no Xft */
5027 set_first_font(value);
5035 gap_x = atoi(value);
5042 gap_y = atoi(value);
5048 CONF("mail_spool") {
5052 variable_substitute(value, buffer, 256);
5054 if (buffer[0] != '\0') {
5055 if (current_mail_spool) {
5056 free(current_mail_spool);
5058 current_mail_spool = strndup(buffer, text_buffer_size);
5065 CONF("minimum_size") {
5067 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5069 if (sscanf(value, "%d", &minimum_width) != 1) {
5077 CONF("maximum_width") {
5079 if (sscanf(value, "%d", &maximum_width) != 1) {
5087 CONF("no_buffers") {
5088 no_buffers = string_to_bool(value);
5090 CONF("top_name_width") {
5091 if (set_top_name_width(value))
5094 CONF("top_cpu_separate") {
5095 cpu_separate = string_to_bool(value);
5097 CONF("short_units") {
5098 short_units = string_to_bool(value);
5100 CONF("format_human_readable") {
5101 format_human_readable = string_to_bool(value);
5104 CONF("hddtemp_host") {
5105 set_hddtemp_host(value);
5107 CONF("hddtemp_port") {
5108 set_hddtemp_port(value);
5110 #endif /* HDDTEMP */
5111 CONF("pad_percents") {
5112 pad_percents = atoi(value);
5116 CONF("own_window") {
5118 own_window = string_to_bool(value);
5121 CONF("own_window_class") {
5123 memset(window.class_name, 0, sizeof(window.class_name));
5124 strncpy(window.class_name, value,
5125 sizeof(window.class_name) - 1);
5128 CONF("own_window_title") {
5130 memset(window.title, 0, sizeof(window.title));
5131 strncpy(window.title, value, sizeof(window.title) - 1);
5134 CONF("own_window_transparent") {
5136 set_transparent = string_to_bool(value);
5139 CONF("own_window_hints") {
5141 char *p_hint, *p_save;
5142 char delim[] = ", ";
5144 /* tokenize the value into individual hints */
5145 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5147 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5148 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5149 SET_HINT(window.hints, HINT_UNDECORATED);
5150 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5151 SET_HINT(window.hints, HINT_BELOW);
5152 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5153 SET_HINT(window.hints, HINT_ABOVE);
5154 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5155 SET_HINT(window.hints, HINT_STICKY);
5156 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5157 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5158 } else if (strncmp(p_hint, "fullscreen", 10) == EQUAL) {
5159 SET_HINT(window.hints, HINT_FULLSCREEN);
5164 p_hint = strtok_r(NULL, delim, &p_save);
5165 } while (p_hint != NULL);
5171 CONF("own_window_type") {
5173 if (strncmp(value, "normal", 6) == EQUAL) {
5174 window.type = TYPE_NORMAL;
5175 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5176 window.type = TYPE_DESKTOP;
5177 } else if (strncmp(value, "dock", 4) == EQUAL) {
5178 window.type = TYPE_DOCK;
5179 text_alignment = TOP_LEFT;
5180 } else if (strncmp(value, "panel", 5) == EQUAL) {
5181 window.type = TYPE_PANEL;
5182 } else if (strncmp(value, "override", 8) == EQUAL) {
5183 window.type = TYPE_OVERRIDE;
5192 CONF("own_window_argb_visual") {
5193 use_argb_visual = string_to_bool(value);
5195 CONF("own_window_argb_value") {
5196 own_window_argb_value = strtol(value, 0, 0);
5197 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5198 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5201 #endif /* USE_ARGB */
5203 CONF("stippled_borders") {
5205 stippled_borders = strtol(value, 0, 0);
5207 stippled_borders = 4;
5211 CONF("imlib_cache_size") {
5213 cimlib_set_cache_size(atoi(value));
5216 CONF("imlib_cache_flush_interval") {
5218 cimlib_set_cache_flush_interval(atoi(value));
5223 CONF("update_interval_on_battery") {
5225 update_interval_bat = strtod(value, 0);
5230 CONF("update_interval") {
5232 set_update_interval(strtod(value, 0));
5236 if (info.music_player_interval == 0) {
5237 // default to update_interval
5238 info.music_player_interval = update_interval;
5241 CONF("total_run_times") {
5243 total_run_times = strtod(value, 0);
5249 stuff_in_uppercase = string_to_bool(value);
5251 CONF("max_specials") {
5253 max_specials = atoi(value);
5258 CONF("max_user_text") {
5260 max_user_text = atoi(value);
5265 CONF("text_buffer_size") {
5267 text_buffer_size = atoi(value);
5268 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5269 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5270 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5278 if (output_methods & TO_X) {
5279 X11_initialisation();
5288 global_text = (char *) malloc(1);
5289 global_text[0] = '\0';
5292 unsigned int l = strlen(global_text);
5294 char buf[CONF_BUFF_SIZE];
5296 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5302 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5310 /* Check for continuation of \\-\n. */
5311 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5312 global_text[l-1] = '\0';
5316 global_text = (char *) realloc(global_text, l + bl + 1);
5317 strcat(global_text, buf);
5319 if (strlen(global_text) > max_user_text) {
5323 global_text_lines = line + 1;
5326 #ifdef TCP_PORT_MONITOR
5327 CONF("max_port_monitor_connections") {
5329 if (!value || (sscanf(value, "%d", &max) != 1)) {
5330 /* an error. use default, warn and continue. */
5331 tcp_portmon_set_max_connections(0);
5333 } else if (tcp_portmon_set_max_connections(max)) {
5334 /* max is < 0, default has been set*/
5339 CONF("if_up_strictness") {
5341 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5342 ifup_strictness = IFUP_UP;
5343 } else if (strcasecmp(value, "up") == EQUAL) {
5344 ifup_strictness = IFUP_UP;
5345 } else if (strcasecmp(value, "link") == EQUAL) {
5346 ifup_strictness = IFUP_LINK;
5347 } else if (strcasecmp(value, "address") == EQUAL) {
5348 ifup_strictness = IFUP_ADDR;
5350 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5351 ifup_strictness = IFUP_UP;
5355 CONF("temperature_unit") {
5357 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5358 } else if (set_temp_output_unit(value)) {
5359 NORM_ERR("temperature_unit: incorrect argument");
5366 char *ptr = strtok(value, " ");
5369 ptr = strtok(NULL, " ");
5376 CONF("lua_draw_hook_pre") {
5378 llua_set_draw_pre_hook(value);
5383 CONF("lua_draw_hook_post") {
5385 llua_set_draw_post_hook(value);
5390 CONF("lua_startup_hook") {
5392 llua_set_startup_hook(value);
5397 CONF("lua_shutdown_hook") {
5399 llua_set_shutdown_hook(value);
5405 #endif /* HAVE_LUA */
5417 CONF("default_color"){}
5418 CONF3("default_shade_color", "default_shadecolor"){}
5419 CONF3("default_outline_color", "default_outlinecolor") {}
5420 CONF("own_window_colour") {}
5423 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5429 if (info.music_player_interval == 0) {
5430 // default to update_interval
5431 info.music_player_interval = update_interval;
5433 if (!global_text) { // didn't supply any text
5434 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5436 if (!output_methods) {
5437 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5439 #if defined(NCURSES)
5441 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5442 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5443 output_methods &= ~TO_NCURSES;
5447 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5448 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5449 output_methods &= ~(TO_STDOUT | TO_STDERR);
5451 #endif /* NCURSES */
5456 static void load_config_file_x11(const char *f)
5461 fp = open_config_file(f);
5465 DBGP("reading contents from config file '%s'", f);
5468 char buff[CONF_BUFF_SIZE], *name, *value;
5469 int ret = do_config_step(&line, fp, buff, &name, &value);
5470 if (ret == CONF_BREAK) {
5472 } else if (ret == CONF_CONTINUE) {
5477 X11_initialisation();
5478 if (x_initialised == YES) {
5480 color0 = get_x11_color(value);
5487 X11_initialisation();
5488 if (x_initialised == YES) {
5490 color1 = get_x11_color(value);
5497 X11_initialisation();
5498 if (x_initialised == YES) {
5500 color2 = get_x11_color(value);
5507 X11_initialisation();
5508 if (x_initialised == YES) {
5510 color3 = get_x11_color(value);
5517 X11_initialisation();
5518 if (x_initialised == YES) {
5520 color4 = get_x11_color(value);
5527 X11_initialisation();
5528 if (x_initialised == YES) {
5530 color5 = get_x11_color(value);
5537 X11_initialisation();
5538 if (x_initialised == YES) {
5540 color6 = get_x11_color(value);
5547 X11_initialisation();
5548 if (x_initialised == YES) {
5550 color7 = get_x11_color(value);
5557 X11_initialisation();
5558 if (x_initialised == YES) {
5560 color8 = get_x11_color(value);
5567 X11_initialisation();
5568 if (x_initialised == YES) {
5570 color9 = get_x11_color(value);
5576 CONF("default_color") {
5577 X11_initialisation();
5578 if (x_initialised == YES) {
5580 default_fg_color = get_x11_color(value);
5586 CONF3("default_shade_color", "default_shadecolor") {
5587 X11_initialisation();
5588 if (x_initialised == YES) {
5590 default_bg_color = get_x11_color(value);
5596 CONF3("default_outline_color", "default_outlinecolor") {
5597 X11_initialisation();
5598 if (x_initialised == YES) {
5600 default_out_color = get_x11_color(value);
5607 CONF("own_window_colour") {
5608 X11_initialisation();
5609 if (x_initialised == YES) {
5611 background_colour = get_x11_color(value);
5613 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5614 "'#' for hex colours");
5620 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5621 if(output_methods & TO_X) {
5622 X11_initialisation();
5631 #undef CONF_CONTINUE
5632 #undef CONF_BUFF_SIZE
5640 static void print_help(const char *prog_name) {
5641 printf("Usage: %s [OPTION]...\n"
5642 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5643 "window. Command line options will override configurations defined in config\n"
5645 " -v, --version version\n"
5646 " -q, --quiet quiet mode\n"
5647 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5648 " -c, --config=FILE config file to load\n"
5649 #ifdef CONFIG_OUTPUT
5650 " -C, --print-config print the builtin default config to stdout\n"
5651 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5653 " -d, --daemonize daemonize, fork to background\n"
5654 " -h, --help help\n"
5656 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5657 " -f, --font=FONT font to use\n"
5658 " -X, --display=DISPLAY X11 display to use\n"
5660 " -o, --own-window create own window to draw\n"
5663 " -b, --double-buffer double buffer (prevents flickering)\n"
5665 " -w, --window-id=WIN_ID window id to draw\n"
5666 " -x X x position\n"
5667 " -y Y y position\n"
5669 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5670 " -u, --interval=SECS update interval\n"
5671 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5672 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5677 /* : means that character before that takes an argument */
5678 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5688 #ifdef CONFIG_OUTPUT
5693 static const struct option longopts[] = {
5694 { "help", 0, NULL, 'h' },
5695 { "version", 0, NULL, 'V' },
5696 { "quiet", 0, NULL, 'q' },
5697 { "debug", 0, NULL, 'D' },
5698 { "config", 1, NULL, 'c' },
5699 #ifdef CONFIG_OUTPUT
5700 { "print-config", 0, NULL, 'C' },
5702 { "daemonize", 0, NULL, 'd' },
5704 { "alignment", 1, NULL, 'a' },
5705 { "font", 1, NULL, 'f' },
5706 { "display", 1, NULL, 'X' },
5708 { "own-window", 0, NULL, 'o' },
5711 { "double-buffer", 0, NULL, 'b' },
5713 { "window-id", 1, NULL, 'w' },
5715 { "text", 1, NULL, 't' },
5716 { "interval", 1, NULL, 'u' },
5717 { "pause", 1, NULL, 'p' },
5721 void set_current_config(void);
5722 void set_current_config(void)
5724 /* check if specified config file is valid */
5725 if (current_config) {
5727 if (stat(current_config, &sb) ||
5728 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5729 NORM_ERR("invalid configuration file '%s'\n", current_config);
5730 free(current_config);
5735 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5737 if (!current_config) {
5738 /* load default config file */
5739 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5742 /* Try to use personal config file first */
5743 to_real_path(buf, CONFIG_FILE);
5744 if (buf[0] && (fp = fopen(buf, "r"))) {
5745 current_config = strndup(buf, max_user_text);
5749 /* Try to use system config file if personal config not readable */
5750 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5751 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5755 /* No readable config found */
5756 if (!current_config) {
5757 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5758 #ifdef BUILD_BUILTIN_CONFIG
5759 current_config = strdup("==builtin==");
5760 NORM_ERR(NOCFGFILEFOUND
5761 ", using builtin default");
5763 CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5764 #endif /* ! CONF_OUTPUT */
5769 void initialisation(int argc, char **argv) {
5770 struct sigaction act, oact;
5772 set_default_configurations();
5773 set_current_config();
5774 load_config_file(current_config);
5775 currentconffile = conftree_add(currentconffile, current_config);
5777 /* init specials array */
5778 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5779 NORM_ERR("failed to create specials array");
5783 if (current_mail_spool == NULL) {
5786 variable_substitute(MAIL_FILE, buf, 256);
5788 if (buf[0] != '\0') {
5789 current_mail_spool = strndup(buf, text_buffer_size);
5794 /* handle other command line arguments */
5796 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5797 || defined(__NetBSD__)
5798 optind = optreset = 1;
5803 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5804 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5805 "kvm_open")) == NULL) {
5806 CRIT_ERR(NULL, NULL, "cannot read kvm");
5808 pthread_mutex_init(&kvm_proc_mutex, NULL);
5812 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5821 fork_to_background = 1;
5824 global_debug_level++;
5828 set_first_font(optarg);
5831 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
5850 global_text = strndup(optarg, max_user_text);
5851 convert_escapes(global_text);
5855 update_interval = strtod(optarg, 0);
5856 update_interval_old = update_interval;
5857 if (info.music_player_interval == 0) {
5858 // default to update_interval
5859 info.music_player_interval = update_interval;
5864 total_run_times = strtod(optarg, 0);
5868 gap_x = atoi(optarg);
5872 gap_y = atoi(optarg);
5877 startup_pause = atoi(optarg);
5878 sleep(startup_pause);
5889 if (output_methods & TO_X) {
5890 load_config_file_x11(current_config);
5894 /* generate text and get initial size */
5895 extract_variable_text(global_text);
5902 if (fork_to_background && first_pass) {
5907 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5914 fprintf(stderr, "\n");
5919 /* parent process */
5920 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5927 start_update_threading();
5929 text_buffer = malloc(max_user_text);
5930 memset(text_buffer, 0, max_user_text);
5931 tmpstring1 = malloc(text_buffer_size);
5932 memset(tmpstring1, 0, text_buffer_size);
5933 tmpstring2 = malloc(text_buffer_size);
5934 memset(tmpstring2, 0, text_buffer_size);
5939 X11_create_window();
5942 llua_setup_info(&info, update_interval);
5943 #endif /* HAVE_LUA */
5948 /* Set signal handlers */
5949 act.sa_handler = signal_handler;
5950 sigemptyset(&act.sa_mask);
5953 act.sa_flags |= SA_RESTART;
5956 if ( sigaction(SIGINT, &act, &oact) < 0
5957 || sigaction(SIGALRM, &act, &oact) < 0
5958 || sigaction(SIGUSR1, &act, &oact) < 0
5959 || sigaction(SIGHUP, &act, &oact) < 0
5960 || sigaction(SIGTERM, &act, &oact) < 0) {
5961 NORM_ERR("error setting signal handler: %s", strerror(errno));
5965 llua_startup_hook();
5966 #endif /* HAVE_LUA */
5969 int main(int argc, char **argv)
5978 g_signal_pending = 0;
5979 max_user_text = MAX_USER_TEXT_DEFAULT;
5981 memset(&info, 0, sizeof(info));
5985 #ifdef TCP_PORT_MONITOR
5986 /* set default connection limit */
5987 tcp_portmon_set_max_connections(0);
5991 if(curl_global_init(CURL_GLOBAL_ALL))
5992 NORM_ERR("curl_global_init() failed, you may not be able to use curl variables");
5995 /* handle command line parameters that don't change configs */
5997 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5998 || ((s = getenv("LANG")) && *s)) {
5999 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6001 NORM_ERR("malloc failed");
6003 for (x = 0; x < strlen(s); x++) {
6004 temp[x] = tolower(s[x]);
6007 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6013 if (!setlocale(LC_CTYPE, "")) {
6014 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6018 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6029 if (current_config) {
6030 free(current_config);
6032 current_config = strndup(optarg, max_user_text);
6035 if (!freopen("/dev/null", "w", stderr)) {
6036 NORM_ERR("unable to redirect stderr to /dev/null");
6040 print_help(argv[0]);
6042 #ifdef CONFIG_OUTPUT
6049 window.window = strtol(optarg, 0, 0);
6054 disp = strdup(optarg);
6063 set_current_config();
6066 /* Load xoap keys, if existing */
6070 #ifdef HAVE_SYS_INOTIFY_H
6071 inotify_fd = inotify_init();
6072 if(inotify_fd != -1) {
6075 fl = fcntl(inotify_fd, F_GETFL);
6076 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
6078 #endif /* HAVE_SYS_INOTIFY_H */
6080 initialisation(argc, argv);
6082 first_pass = 0; /* don't ever call fork() again */
6087 curl_global_cleanup();
6090 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6092 pthread_mutex_destroy(&kvm_proc_mutex);
6099 void alarm_handler(void) {
6101 kill(childpid, SIGTERM);
6105 static void signal_handler(int sig)
6107 /* signal handler is light as a feather, as it should be.
6108 * we will poll g_signal_pending with each loop of conky
6109 * and do any signal processing there, NOT here (except
6110 * SIGALRM because this is caused when conky is hanging) */
6111 if(sig == SIGALRM) {
6114 g_signal_pending = sig;