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>
91 #include "iconv_tools.h"
101 #include "read_tcp.h"
103 #include "specials.h"
104 #include "temphelper.h"
105 #include "template.h"
106 #include "tailhead.h"
107 #include "timeinfo.h"
110 /* check for OS and include appropriate headers */
111 #if defined(__linux__)
113 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
115 #elif defined(__OpenBSD__)
119 #if defined(__FreeBSD_kernel__)
123 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
124 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
125 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
126 int apm_getinfo(int fd, apm_info_t aip);
127 char *get_apm_adapter(void);
128 char *get_apm_battery_life(void);
129 char *get_apm_battery_time(void);
133 #include "defconfig.h"
134 #include "conf_cookie.h"
138 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
141 #define MAIL_FILE "$MAIL"
142 #define MAX_IF_BLOCK_DEPTH 5
144 //#define SIGNAL_BLOCKING
145 #undef SIGNAL_BLOCKING
147 /* debugging level, used by logging.h */
148 int global_debug_level = 0;
150 /* disable inotify auto reload feature if desired */
151 int disable_auto_reload = 0;
153 /* two strings for internal use */
154 static char *tmpstring1, *tmpstring2;
156 /* variables holding various config settings */
158 int format_human_readable;
165 int top_cpu, top_mem, top_time;
173 static int extra_newline;
174 enum x_initialiser_state x_initialised = NO;
175 static volatile int g_signal_pending;
176 /* Update interval */
177 double update_interval;
178 double update_interval_old;
179 double update_interval_bat;
180 void *global_cpu = NULL;
181 unsigned int max_text_width = 0;
186 /* prototypes for internally used functions */
187 static void signal_handler(int);
188 static void print_version(void) __attribute__((noreturn));
189 static void reload_config(void);
191 static void print_version(void)
193 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
195 printf("\nCompiled in features:\n\n"
196 "System config file: "SYSTEM_CONFIG_FILE"\n"
197 "Package library path: "PACKAGE_LIBDIR"\n\n"
201 " * Xdamage extension\n"
202 # endif /* HAVE_XDAMAGE */
204 " * XDBE (double buffer extension)\n"
205 # endif /* HAVE_XDBE */
211 # endif /* USE_ARGB */
213 "\n Music detection:\n"
216 #endif /* AUDACIOUS */
232 #endif /* HAVE_OPENMP */
239 #ifdef TCP_PORT_MONITOR
241 #endif /* TCP_PORT_MONITOR */
244 #endif /* HAVE_CURL */
249 " * Weather (METAR)\n"
251 " * Weather (XOAP)\n"
256 #endif /* HAVE_IWLIB */
258 " * support for IBM/Lenovo notebooks\n"
268 #endif /* CONFIG_OUTPUT */
284 #ifdef HAVE_LUA_CAIRO
286 #endif /* HAVE_LUA_CAIRO */
287 #ifdef HAVE_LUA_IMLIB2
290 #endif /* HAVE_LUA */
296 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
301 static void X11_create_window(void);
302 static void X11_initialisation(void);
304 struct _x11_stuff_s {
308 XserverRegion region2, part;
309 int event_base, error_base;
315 static int text_start_x, text_start_y; /* text start position in window */
316 static int text_width, text_height;
333 /* display to connect to */
334 static char *disp = NULL;
338 /* struct that has all info to be shared between
339 * instances of the same text object */
340 struct information info;
342 /* path to config file */
343 char *current_config;
345 /* set to 1 if you want all text to be in uppercase */
346 static unsigned int stuff_in_uppercase;
348 /* Run how many times? */
349 static unsigned long total_run_times;
352 static int fork_to_background;
354 /* set to 0 after the first time conky is run, so we don't fork again after the
356 static int first_pass = 1;
358 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
360 /* filenames for output */
361 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
362 char *append_file = NULL; FILE *append_fpointer = NULL;
366 static int show_graph_scale;
367 static int show_graph_range;
369 /* Position on the screen */
370 static int text_alignment;
371 static int gap_x, gap_y;
374 static int draw_borders;
375 static int draw_graph_borders;
376 static int stippled_borders;
378 int get_stippled_borders(void)
380 return stippled_borders;
383 static int draw_shades, draw_outline;
385 long default_fg_color, default_bg_color, default_out_color;
387 /* create own window or draw stuff to root? */
388 static int set_transparent = 0;
391 static int own_window = 0;
392 static int background_colour = 0;
394 /* fixed size/pos is set if wm/user changes them */
395 static int fixed_size = 0, fixed_pos = 0;
398 static int minimum_width, minimum_height;
399 static int maximum_width;
404 static int sensor_device;
407 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
410 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
411 unsigned int max_user_text;
413 /* maximum size of individual text buffers, ie $exec buffer size */
414 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
419 /* no buffers in used memory? */
422 /* pad percentages to decimals? */
423 static int pad_percents = 0;
425 static char *global_text = 0;
427 char *get_global_text(void)
432 long global_text_lines;
434 static int total_updates;
435 static int updatereset;
437 void set_updatereset(int i)
442 int get_updatereset(void)
447 int check_contains(char *f, char *s)
450 FILE *where = open_file(f, 0);
455 while (fgets(buf1, 256, where)) {
456 if (strstr(buf1, s)) {
463 NORM_ERR("Could not open the file '%s'", f);
468 #define SECRIT_MULTILINE_CHAR '\x02'
470 int calc_text_width(const char *s)
472 size_t slen = strlen(s);
475 if ((output_methods & TO_X) == 0) {
485 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
486 (const FcChar8 *) s, slen, &gi);
488 XftTextExtents8(display, fonts[selected_font].xftfont,
489 (const FcChar8 *) s, slen, &gi);
495 return XTextWidth(fonts[selected_font].font, s, slen);
500 /* formatted text to render on screen, generated in generate_text(),
501 * drawn in draw_stuff() */
503 static char *text_buffer;
505 /* quite boring functions */
507 static inline void for_each_line(char *b, int f(char *, int))
510 int special_index = 0; /* specials index */
513 for (ps = b, pe = b; *pe; pe++) {
516 special_index = f(ps, special_index);
523 f(ps, special_index);
527 static void convert_escapes(char *buf)
529 char *p = buf, *s = buf;
536 } else if (*s == '\\') {
547 /* Prints anything normally printed with snprintf according to the current value
548 * of use_spacer. Actually slightly more flexible than snprintf, as you can
549 * safely specify the destination buffer as one of your inputs. */
550 int spaced_print(char *buf, int size, const char *format, int width, ...)
559 tempbuf = malloc(size * sizeof(char));
561 // Passes the varargs along to vsnprintf
562 va_start(argp, width);
563 vsnprintf(tempbuf, size, format, argp);
566 switch (use_spacer) {
568 len = snprintf(buf, size, "%s", tempbuf);
571 len = snprintf(buf, size, "%*s", width, tempbuf);
574 len = snprintf(buf, size, "%-*s", width, tempbuf);
581 /* print percentage values
583 * - i.e., unsigned values between 0 and 100
584 * - respect the value of pad_percents */
585 int percent_print(char *buf, int size, unsigned value)
587 return spaced_print(buf, size, "%u", pad_percents, value);
590 /* converts from bytes to human readable format (K, M, G, T)
592 * The algorithm always divides by 1024, as unit-conversion of byte
593 * counts suggests. But for output length determination we need to
594 * compare with 1000 here, as we print in decimal form. */
595 void human_readable(long long num, char *buf, int size)
597 const char **suffix = suffixes;
603 /* Possibly just output as usual, for example for stdout usage */
604 if (!format_human_readable) {
605 spaced_print(buf, size, "%d", 6, round_to_int(num));
616 if (llabs(num) < 1000LL) {
617 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
621 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
629 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
631 * The goal is to always have a significance of 3, by
632 * adjusting the decimal part of the number. Sample output:
636 * so the point of alignment resides between number and unit. The
637 * upside of this is that there is minimal padding necessary, though
638 * there should be a way to make alignment take place at the decimal
639 * dot (then with fixed width decimal part).
641 * Note the repdigits below: when given a precision value, printf()
642 * rounds the float to it, not just cuts off the remaining digits. So
643 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
644 * printed with a precision of 0. Yay. */
646 precision = 0; /* print 100-999 without decimal part */
648 precision = 1; /* print 10-99 with one decimal place */
650 precision = 2; /* print 0-9 with two decimal places */
652 spaced_print(buf, size, format, width, precision, fnum, *suffix);
655 /* global object list root element */
656 static struct text_object global_root_object;
658 static long current_text_color;
660 void set_current_text_color(long colour)
662 current_text_color = colour;
665 long get_current_text_color(void)
667 return current_text_color;
670 //adds newstring to to the tree unless you can already see it when travelling back.
671 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
672 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
673 struct conftree* node;
674 struct conftree* node2;
676 for(node = previous; node != NULL; node = node->back) {
677 if(strcmp(node->string, newstring) == 0) {
681 node = malloc(sizeof(struct conftree));
682 if (previous != NULL) {
683 if(previous->vert_next == NULL) {
684 previous->vert_next = node;
686 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
687 node2->horz_next = node;
690 node->string = strdup(newstring);
691 node->horz_next = NULL;
692 node->vert_next = NULL;
693 node->back = previous;
697 void conftree_empty(struct conftree* tree) {
699 conftree_empty(tree->horz_next);
700 conftree_empty(tree->vert_next);
706 struct conftree *currentconffile;
708 static void extract_variable_text(const char *p)
710 free_text_objects(&global_root_object, 0);
724 extract_variable_text_internal(&global_root_object, p);
727 void parse_conky_vars(struct text_object *root, const char *txt,
728 char *p, int p_max_size, struct information *cur)
730 extract_variable_text_internal(root, txt);
731 generate_text_internal(p, p_max_size, *root, cur);
734 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
735 * multiline objects like $exec work with $align[rc] and friends
737 void substitute_newlines(char *p, long l)
741 while (p && *p && p < s + l) {
743 /* only substitute if it's not the last newline */
744 *p = SECRIT_MULTILINE_CHAR;
750 void generate_text_internal(char *p, int p_max_size,
751 struct text_object root, struct information *cur)
753 struct text_object *obj;
755 int need_to_load_fonts = 0;
758 /* for the OBJ_top* handler */
759 struct process **needed = 0;
762 char buff_in[p_max_size];
764 #endif /* HAVE_ICONV */
770 while (obj && p_max_size > 0) {
771 needed = 0; /* reset for top stuff */
773 /* IFBLOCK jumping algorithm
775 * This is easier as it looks like:
776 * - each IF checks it's condition
777 * - on FALSE: call DO_JUMP
778 * - on TRUE: don't care
779 * - each ELSE calls DO_JUMP unconditionally
780 * - each ENDIF is silently being ignored
783 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
784 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
785 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
786 * regularly, "obj" is being updated to point to obj->next, so object parsing
787 * continues right after the corresponding ELSE or ENDIF. This means that if we
788 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
789 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
790 * jumped, and there is nothing to do.
794 if (obj->ifblock_next) \
795 obj = obj->ifblock_next; \
798 #define OBJ(a) break; case OBJ_##a:
802 NORM_ERR("not implemented obj type %d", obj->type);
804 print_read_tcp(obj, p, p_max_size);
808 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
810 #endif /* !__OpenBSD__ */
814 ok = get_freq(p, p_max_size, "%.0f", 1,
822 ok = get_freq(p, p_max_size, "%'.2f", 1000,
825 /* OpenBSD has no such flag (SUSv2) */
826 ok = get_freq(p, p_max_size, "%.2f", 1000,
828 #endif /* __OpenBSD */
831 #if defined(__linux__)
833 print_voltage_mv(obj, p, p_max_size);
836 print_voltage_v(obj, p, p_max_size);
840 OBJ(wireless_essid) {
841 print_wireless_essid(obj, p, p_max_size);
844 print_wireless_mode(obj, p, p_max_size);
846 OBJ(wireless_bitrate) {
847 print_wireless_bitrate(obj, p, p_max_size);
850 print_wireless_ap(obj, p, p_max_size);
852 OBJ(wireless_link_qual) {
853 print_wireless_link_qual(obj, p, p_max_size);
855 OBJ(wireless_link_qual_max) {
856 print_wireless_link_qual_max(obj, p, p_max_size);
858 OBJ(wireless_link_qual_perc) {
859 print_wireless_link_qual_perc(obj, p, p_max_size);
861 OBJ(wireless_link_bar) {
862 print_wireless_link_bar(obj, p, p_max_size);
864 #endif /* HAVE_IWLIB */
866 #endif /* __linux__ */
870 get_acpi_fan(p, p_max_size);
873 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
876 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
879 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
881 OBJ(battery_percent) {
882 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
885 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
888 get_battery_short_status(p, p_max_size, obj->data.s);
890 #endif /* __OpenBSD__ */
893 human_readable(cur->buffers * 1024, p, 255);
896 human_readable(cur->cached * 1024, p, 255);
898 OBJ(cmdline_to_pid) {
899 print_cmdline_to_pid(obj, p, p_max_size);
902 if (cur->cpu_usage) {
903 if (obj->data.i > info.cpu_count) {
904 NORM_ERR("obj->data.i %i info.cpu_count %i",
905 obj->data.i, info.cpu_count);
906 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
908 percent_print(p, p_max_size,
909 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
913 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
915 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
919 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
922 print_loadgraph(obj, p, p_max_size);
926 new_fg(p, obj->data.l);
960 #if defined(__linux__)
962 snprintf(p, p_max_size, "%s",
963 get_disk_protect_queue(obj->data.s));
966 print_i8k_version(obj, p, p_max_size);
969 print_i8k_bios(obj, p, p_max_size);
972 print_i8k_serial(obj, p, p_max_size);
975 print_i8k_cpu_temp(obj, p, p_max_size);
977 OBJ(i8k_left_fan_status) {
978 print_i8k_left_fan_status(obj, p, p_max_size);
980 OBJ(i8k_right_fan_status) {
981 print_i8k_right_fan_status(obj, p, p_max_size);
983 OBJ(i8k_left_fan_rpm) {
984 print_i8k_left_fan_rpm(obj, p, p_max_size);
986 OBJ(i8k_right_fan_rpm) {
987 print_i8k_right_fan_rpm(obj, p, p_max_size);
990 print_i8k_ac_status(obj, p, p_max_size);
992 OBJ(i8k_buttons_status) {
993 print_i8k_buttons_status(obj, p, p_max_size);
997 get_ibm_acpi_fan(obj, p, p_max_size);
1000 print_ibm_temps(obj, p, p_max_size);
1003 get_ibm_acpi_volume(obj, p, p_max_size);
1005 OBJ(ibm_brightness) {
1006 get_ibm_acpi_brightness(obj, p, p_max_size);
1009 /* information from sony_laptop kernel module
1010 * /sys/devices/platform/sony-laptop */
1011 OBJ(sony_fanspeed) {
1012 get_sony_fanspeed(p, p_max_size);
1015 if (!gateway_exists()) {
1020 print_gateway_iface(p, p_max_size);
1023 print_gateway_ip(p, p_max_size);
1026 snprintf(p, p_max_size, "%d", get_laptop_mode());
1029 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1031 #endif /* __linux__ */
1032 #if (defined(__FreeBSD__) || defined(__linux__))
1034 if (!interface_up(obj)) {
1040 OBJ(obsd_sensors_temp) {
1041 print_obsd_sensors_temp(obj, p, p_max_size);
1043 OBJ(obsd_sensors_fan) {
1044 print_obsd_sensors_fan(obj, p, p_max_size);
1046 OBJ(obsd_sensors_volt) {
1047 print_obsd_sensors_volt(obj, p, p_max_size);
1050 get_obsd_vendor(p, p_max_size);
1053 get_obsd_product(p, p_max_size);
1055 #endif /* __OpenBSD__ */
1058 new_font(p, obj->data.s);
1059 need_to_load_fonts = 1;
1063 print_diskio(obj, p, p_max_size);
1066 print_diskio_write(obj, p, p_max_size);
1069 print_diskio_read(obj, p, p_max_size);
1073 print_diskiograph(obj, p, p_max_size);
1075 OBJ(diskiograph_read) {
1076 print_diskiograph_read(obj, p, p_max_size);
1078 OBJ(diskiograph_write) {
1079 print_diskiograph_write(obj, p, p_max_size);
1083 print_downspeed(obj, p, p_max_size);
1086 print_downspeedf(obj, p, p_max_size);
1089 OBJ(downspeedgraph) {
1090 print_downspeedgraph(obj, p, p_max_size);
1094 /* Since we see you, you're if has not jumped.
1095 * Do Ninja jump here: without leaving traces.
1096 * This is to prevent us from stale jumped flags.
1098 obj = obj->ifblock_next;
1102 /* harmless object, just ignore */
1105 print_addr(obj, p, p_max_size);
1107 #if defined(__linux__)
1109 print_addrs(obj, p, p_max_size);
1111 #endif /* __linux__ */
1112 #if defined(IMLIB2) && defined(X11)
1114 /* doesn't actually draw anything, just queues it omp. the
1115 * image will get drawn after the X event loop */
1116 cimlib_add_image(obj->data.s);
1120 evaluate(obj->data.s, p, p_max_size);
1123 print_exec(obj, p, p_max_size);
1126 print_execp(obj, p, p_max_size);
1129 print_execgauge(obj, p, p_max_size);
1132 print_execbar(obj, p, p_max_size);
1136 print_execgraph(obj, p, p_max_size);
1140 print_execibar(obj, p, p_max_size);
1144 print_execigraph(obj, p, p_max_size);
1148 print_execigauge(obj, p, p_max_size);
1151 print_execi(obj, p, p_max_size);
1154 print_execpi(obj, p, p_max_size);
1157 print_texeci(obj, p, p_max_size);
1160 print_imap_unseen(obj, p, p_max_size);
1162 OBJ(imap_messages) {
1163 print_imap_messages(obj, p, p_max_size);
1166 print_pop3_unseen(obj, p, p_max_size);
1169 print_pop3_used(obj, p, p_max_size);
1172 print_fs_bar(obj, 0, p, p_max_size);
1175 print_fs_free(obj, p, p_max_size);
1178 print_fs_perc(obj, 1, p, p_max_size);
1181 print_fs_size(obj, p, p_max_size);
1184 print_fs_type(obj, p, p_max_size);
1187 print_fs_used(obj, p, p_max_size);
1190 print_fs_bar(obj, 1, p, p_max_size);
1193 print_fs_perc(obj, 0, p, p_max_size);
1196 print_loadavg(obj, p, p_max_size);
1199 new_goto(p, obj->data.i);
1206 new_hr(p, obj->data.i);
1210 print_nameserver(obj, p, p_max_size);
1214 print_eve(obj, p, p_max_size);
1219 curl_print(obj, p, p_max_size);
1224 rss_print_info(obj, p, p_max_size);
1229 print_weather(obj, p, p_max_size);
1233 OBJ(weather_forecast) {
1234 print_weather_forecast(obj, p, p_max_size);
1239 print_lua(obj, p, p_max_size);
1242 print_lua_parse(obj, p, p_max_size);
1245 print_lua_bar(obj, p, p_max_size);
1249 print_lua_graph(obj, p, p_max_size);
1253 print_lua_gauge(obj, p, p_max_size);
1255 #endif /* HAVE_LUA */
1261 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1262 snprintf(p, p_max_size, "N/A");
1264 temp_print(p, p_max_size, (double)val,
1265 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1270 new_offset(p, obj->data.i);
1273 new_voffset(p, obj->data.i);
1277 print_sysfs_sensor(obj, p, p_max_size);
1280 print_sysfs_sensor(obj, p, p_max_size);
1283 print_sysfs_sensor(obj, p, p_max_size);
1285 #endif /* __linux__ */
1287 new_alignr(p, obj->data.i);
1290 new_alignc(p, obj->data.i);
1293 char buf[max_user_text];
1294 struct information *tmp_info =
1295 malloc(sizeof(struct information));
1296 memcpy(tmp_info, cur, sizeof(struct information));
1297 generate_text_internal(buf, max_user_text,
1298 *obj->sub, tmp_info);
1300 if (strlen(buf) != 0) {
1306 char expression[max_user_text];
1308 struct information *tmp_info;
1310 tmp_info = malloc(sizeof(struct information));
1311 memcpy(tmp_info, cur, sizeof(struct information));
1312 generate_text_internal(expression, max_user_text,
1313 *obj->sub, tmp_info);
1314 DBGP("parsed arg into '%s'", expression);
1316 val = compare(expression);
1318 NORM_ERR("compare failed for expression '%s'",
1328 spc = strchr(obj->data.s, ' ');
1329 if (!spc && access(obj->data.s, F_OK)) {
1333 if (!check_contains(obj->data.s, spc + 1))
1340 && (!check_mount(obj->data.s))) {
1346 if (!get_process_by_name(obj->data.s)) {
1348 if ((obj->data.s) && system(obj->data.s)) {
1353 #if defined(__linux__)
1355 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1359 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1362 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1367 human_readable(cur->mem * 1024, p, 255);
1370 human_readable(cur->memeasyfree * 1024, p, 255);
1373 human_readable(cur->memfree * 1024, p, 255);
1376 human_readable(cur->memmax * 1024, p, 255);
1380 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1383 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1386 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1390 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1395 print_mixer(obj, p, p_max_size);
1398 print_mixerl(obj, p, p_max_size);
1401 print_mixerr(obj, p, p_max_size);
1404 print_mixer_bar(obj, p, p_max_size);
1407 print_mixerl_bar(obj, p, p_max_size);
1410 print_mixerr_bar(obj, p, p_max_size);
1412 OBJ(if_mixer_mute) {
1413 if (!check_mixer_muted(obj)) {
1418 #define NOT_IN_X "Not running in X"
1420 if(x_initialised != YES) {
1421 strncpy(p, NOT_IN_X, p_max_size);
1423 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1426 OBJ(monitor_number) {
1427 if(x_initialised != YES) {
1428 strncpy(p, NOT_IN_X, p_max_size);
1430 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1434 if(x_initialised != YES) {
1435 strncpy(p, NOT_IN_X, p_max_size);
1437 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1440 OBJ(desktop_number) {
1441 if(x_initialised != YES) {
1442 strncpy(p, NOT_IN_X, p_max_size);
1444 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1448 if(x_initialised != YES) {
1449 strncpy(p, NOT_IN_X, p_max_size);
1450 }else if(cur->x11.desktop.name != NULL) {
1451 strncpy(p, cur->x11.desktop.name, p_max_size);
1457 char buf[max_user_text];
1459 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1461 print_format_time(obj, p, p_max_size);
1465 print_mails(obj, p, p_max_size);
1468 print_new_mails(obj, p, p_max_size);
1471 print_seen_mails(obj, p, p_max_size);
1474 print_unseen_mails(obj, p, p_max_size);
1476 OBJ(flagged_mails) {
1477 print_flagged_mails(obj, p, p_max_size);
1479 OBJ(unflagged_mails) {
1480 print_unflagged_mails(obj, p, p_max_size);
1482 OBJ(forwarded_mails) {
1483 print_forwarded_mails(obj, p, p_max_size);
1485 OBJ(unforwarded_mails) {
1486 print_unforwarded_mails(obj, p, p_max_size);
1488 OBJ(replied_mails) {
1489 print_replied_mails(obj, p, p_max_size);
1491 OBJ(unreplied_mails) {
1492 print_unreplied_mails(obj, p, p_max_size);
1495 print_draft_mails(obj, p, p_max_size);
1497 OBJ(trashed_mails) {
1498 print_trashed_mails(obj, p, p_max_size);
1501 print_mboxscan(obj, p, p_max_size);
1504 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1506 OBJ(nodename_short) {
1508 pos = strstr(cur->uname_s.nodename, ".");
1510 snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1512 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1516 new_outline(p, obj->data.l);
1519 char buf[max_user_text];
1521 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1523 print_pid_chroot(obj, p, p_max_size);
1526 char buf[max_user_text];
1528 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1530 print_pid_cmdline(obj, p, p_max_size);
1533 char buf[max_user_text];
1535 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1537 print_pid_cwd(obj, p, p_max_size);
1540 char buf[max_user_text];
1542 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1544 print_pid_environ(obj, p, p_max_size);
1546 OBJ(pid_environ_list) {
1547 char buf[max_user_text];
1549 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1551 print_pid_environ_list(obj, p, p_max_size);
1554 char buf[max_user_text];
1556 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1558 print_pid_exe(obj, p, p_max_size);
1561 char buf[max_user_text];
1563 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1565 print_pid_nice(obj, p, p_max_size);
1567 OBJ(pid_openfiles) {
1568 char buf[max_user_text];
1570 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1572 print_pid_openfiles(obj, p, p_max_size);
1575 char buf[max_user_text];
1577 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1579 print_pid_parent(obj, p, p_max_size);
1582 char buf[max_user_text];
1584 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1586 print_pid_priority(obj, p, p_max_size);
1589 char buf[max_user_text];
1591 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1593 print_pid_state(obj, p, p_max_size);
1595 OBJ(pid_state_short) {
1596 char buf[max_user_text];
1598 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1600 print_pid_state_short(obj, p, p_max_size);
1603 char buf[max_user_text];
1605 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1607 print_pid_stderr(obj, p, p_max_size);
1610 char buf[max_user_text];
1612 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1614 print_pid_stdin(obj, p, p_max_size);
1617 char buf[max_user_text];
1619 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1621 print_pid_stdout(obj, p, p_max_size);
1624 char buf[max_user_text];
1626 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1628 print_pid_threads(obj, p, p_max_size);
1630 OBJ(pid_thread_list) {
1631 char buf[max_user_text];
1633 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1635 print_pid_thread_list(obj, p, p_max_size);
1637 OBJ(pid_time_kernelmode) {
1638 char buf[max_user_text];
1640 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1642 print_pid_time_kernelmode(obj, p, p_max_size);
1644 OBJ(pid_time_usermode) {
1645 char buf[max_user_text];
1647 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1649 print_pid_time_usermode(obj, p, p_max_size);
1652 char buf[max_user_text];
1654 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1656 print_pid_time(obj, p, p_max_size);
1659 char buf[max_user_text];
1661 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1663 print_pid_uid(obj, p, p_max_size);
1666 char buf[max_user_text];
1668 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1670 print_pid_euid(obj, p, p_max_size);
1673 char buf[max_user_text];
1675 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1677 print_pid_suid(obj, p, p_max_size);
1680 char buf[max_user_text];
1682 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1684 print_pid_fsuid(obj, p, p_max_size);
1687 char buf[max_user_text];
1689 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1691 print_pid_gid(obj, p, p_max_size);
1694 char buf[max_user_text];
1696 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1698 print_pid_egid(obj, p, p_max_size);
1701 char buf[max_user_text];
1703 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1705 print_pid_sgid(obj, p, p_max_size);
1708 char buf[max_user_text];
1710 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1712 print_pid_fsgid(obj, p, p_max_size);
1715 char buf[max_user_text];
1717 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1719 print_pid_read(obj, p, p_max_size);
1722 char buf[max_user_text];
1724 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1726 print_pid_vmpeak(obj, p, p_max_size);
1729 char buf[max_user_text];
1731 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1733 print_pid_vmsize(obj, p, p_max_size);
1736 char buf[max_user_text];
1738 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1740 print_pid_vmlck(obj, p, p_max_size);
1743 char buf[max_user_text];
1745 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1747 print_pid_vmhwm(obj, p, p_max_size);
1750 char buf[max_user_text];
1752 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1754 print_pid_vmrss(obj, p, p_max_size);
1757 char buf[max_user_text];
1759 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1761 print_pid_vmdata(obj, p, p_max_size);
1764 char buf[max_user_text];
1766 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1768 print_pid_vmstk(obj, p, p_max_size);
1771 char buf[max_user_text];
1773 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1775 print_pid_vmexe(obj, p, p_max_size);
1778 char buf[max_user_text];
1780 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1782 print_pid_vmlib(obj, p, p_max_size);
1785 char buf[max_user_text];
1787 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1789 print_pid_vmpte(obj, p, p_max_size);
1792 char buf[max_user_text];
1794 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1796 print_pid_write(obj, p, p_max_size);
1799 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1801 OBJ(running_processes) {
1802 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1804 OBJ(running_threads) {
1805 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1808 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1811 snprintf(p, p_max_size, "%s", obj->data.s);
1815 new_bg(p, obj->data.l);
1818 new_stippled_hr(obj, p);
1822 human_readable(cur->swap * 1024, p, 255);
1825 human_readable(cur->swapfree * 1024, p, 255);
1828 human_readable(cur->swapmax * 1024, p, 255);
1831 if (cur->swapmax == 0) {
1832 strncpy(p, "No swap", p_max_size);
1834 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1838 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1841 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1844 print_time(obj, p, p_max_size);
1847 print_utime(obj, p, p_max_size);
1850 print_tztime(obj, p, p_max_size);
1853 print_totaldown(obj, p, p_max_size);
1856 print_totalup(obj, p, p_max_size);
1859 char buf[max_user_text];
1861 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1863 print_gid_name(obj, p, p_max_size);
1866 char buf[max_user_text];
1868 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1870 print_uid_name(obj, p, p_max_size);
1873 snprintf(p, p_max_size, "%d", total_updates);
1876 if(total_updates % updatereset != obj->data.i - 1) {
1881 print_upspeed(obj, p, p_max_size);
1884 print_upspeedf(obj, p, p_max_size);
1888 print_upspeedgraph(obj, p, p_max_size);
1892 format_seconds_short(p, p_max_size, (int) cur->uptime);
1895 format_seconds(p, p_max_size, (int) cur->uptime);
1899 snprintf(p, p_max_size, "%s", cur->users.names);
1902 snprintf(p, p_max_size, "%s", cur->users.terms);
1905 snprintf(p, p_max_size, "%s", cur->users.times);
1908 update_user_time(obj->data.s);
1909 snprintf(p, p_max_size, "%s", cur->users.ctime);
1912 snprintf(p, p_max_size, "%d", cur->users.number);
1914 #endif /* __linux__ */
1915 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1916 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1920 msg = get_apm_adapter();
1921 snprintf(p, p_max_size, "%s", msg);
1924 OBJ(apm_battery_life) {
1927 msg = get_apm_battery_life();
1928 snprintf(p, p_max_size, "%s", msg);
1931 OBJ(apm_battery_time) {
1934 msg = get_apm_battery_time();
1935 snprintf(p, p_max_size, "%s", msg);
1938 #endif /* __FreeBSD__ __OpenBSD__ */
1942 print_mpd_title(obj, p, p_max_size);
1944 print_mpd_artist(obj, p, p_max_size);
1946 print_mpd_album(obj, p, p_max_size);
1948 print_mpd_random(obj, p, p_max_size);
1950 print_mpd_repeat(obj, p, p_max_size);
1952 print_mpd_track(obj, p, p_max_size);
1954 print_mpd_name(obj, p, p_max_size);
1956 print_mpd_file(obj, p, p_max_size);
1958 print_mpd_vol(obj, p, p_max_size);
1960 print_mpd_bitrate(obj, p, p_max_size);
1962 print_mpd_status(obj, p, p_max_size);
1964 print_mpd_elapsed(obj, p, p_max_size);
1967 print_mpd_length(obj, p, p_max_size);
1970 print_mpd_percent(obj, p, p_max_size);
1973 print_mpd_bar(obj, p, p_max_size);
1976 print_mpd_smart(obj, p, p_max_size);
1978 OBJ(if_mpd_playing) {
1979 if (!mpd_get_info()->is_playing) {
1986 #define MOC_PRINT(t, a) \
1987 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1989 MOC_PRINT(state, "??");
1992 MOC_PRINT(file, "no file");
1995 MOC_PRINT(title, "no title");
1998 MOC_PRINT(artist, "no artist");
2001 MOC_PRINT(song, "no song");
2004 MOC_PRINT(album, "no album");
2006 OBJ(moc_totaltime) {
2007 MOC_PRINT(totaltime, "0:00");
2010 MOC_PRINT(timeleft, "0:00");
2013 MOC_PRINT(curtime, "0:00");
2016 MOC_PRINT(bitrate, "0Kbps");
2019 MOC_PRINT(rate, "0KHz");
2025 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2028 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2031 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2034 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2036 OBJ(xmms2_comment) {
2037 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2040 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2043 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2046 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2048 OBJ(xmms2_tracknr) {
2049 if (cur->xmms2.tracknr != -1) {
2050 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2053 OBJ(xmms2_bitrate) {
2054 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2057 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2060 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2062 OBJ(xmms2_elapsed) {
2063 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2064 (cur->xmms2.elapsed / 1000) % 60);
2066 OBJ(xmms2_duration) {
2067 snprintf(p, p_max_size, "%02d:%02d",
2068 cur->xmms2.duration / 60000,
2069 (cur->xmms2.duration / 1000) % 60);
2071 OBJ(xmms2_percent) {
2072 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2075 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2077 OBJ(xmms2_playlist) {
2078 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2080 OBJ(xmms2_timesplayed) {
2081 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2084 if (strlen(cur->xmms2.title) < 2
2085 && strlen(cur->xmms2.title) < 2) {
2086 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2088 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2092 OBJ(if_xmms2_connected) {
2093 if (cur->xmms2.conn_state != 1) {
2099 OBJ(audacious_status) {
2100 snprintf(p, p_max_size, "%s",
2101 cur->audacious.items[AUDACIOUS_STATUS]);
2103 OBJ(audacious_title) {
2104 snprintf(p, cur->audacious.max_title_len > 0
2105 ? cur->audacious.max_title_len : p_max_size, "%s",
2106 cur->audacious.items[AUDACIOUS_TITLE]);
2108 OBJ(audacious_length) {
2109 snprintf(p, p_max_size, "%s",
2110 cur->audacious.items[AUDACIOUS_LENGTH]);
2112 OBJ(audacious_length_seconds) {
2113 snprintf(p, p_max_size, "%s",
2114 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2116 OBJ(audacious_position) {
2117 snprintf(p, p_max_size, "%s",
2118 cur->audacious.items[AUDACIOUS_POSITION]);
2120 OBJ(audacious_position_seconds) {
2121 snprintf(p, p_max_size, "%s",
2122 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2124 OBJ(audacious_bitrate) {
2125 snprintf(p, p_max_size, "%s",
2126 cur->audacious.items[AUDACIOUS_BITRATE]);
2128 OBJ(audacious_frequency) {
2129 snprintf(p, p_max_size, "%s",
2130 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2132 OBJ(audacious_channels) {
2133 snprintf(p, p_max_size, "%s",
2134 cur->audacious.items[AUDACIOUS_CHANNELS]);
2136 OBJ(audacious_filename) {
2137 snprintf(p, p_max_size, "%s",
2138 cur->audacious.items[AUDACIOUS_FILENAME]);
2140 OBJ(audacious_playlist_length) {
2141 snprintf(p, p_max_size, "%s",
2142 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2144 OBJ(audacious_playlist_position) {
2145 snprintf(p, p_max_size, "%s",
2146 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2148 OBJ(audacious_main_volume) {
2149 snprintf(p, p_max_size, "%s",
2150 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2152 OBJ(audacious_bar) {
2156 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2157 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2158 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2160 #endif /* AUDACIOUS */
2164 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2167 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2170 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2173 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2176 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2179 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2182 /* we have four different types of top (top, top_mem,
2183 * top_time and top_io). To avoid having almost-same code four
2184 * times, we have this special handler. */
2193 print_top(obj, p, p_max_size);
2194 #endif /* __linux__ */
2196 print_tailhead("tail", obj, p, p_max_size);
2199 print_tailhead("head", obj, p, p_max_size);
2202 print_lines(obj, p, p_max_size);
2206 print_words(obj, p, p_max_size);
2208 #ifdef TCP_PORT_MONITOR
2210 tcp_portmon_action(obj, p, p_max_size);
2212 #endif /* TCP_PORT_MONITOR */
2216 do_iconv_start(obj);
2221 #endif /* HAVE_ICONV */
2223 OBJ(entropy_avail) {
2224 print_entropy_avail(obj, p, p_max_size);
2227 print_entropy_perc(obj, p, p_max_size);
2229 OBJ(entropy_poolsize) {
2230 print_entropy_poolsize(obj, p, p_max_size);
2233 print_entropy_bar(obj, p, p_max_size);
2237 print_smapi(obj, p, p_max_size);
2239 OBJ(if_smapi_bat_installed) {
2241 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2242 if(!smapi_bat_installed(idx)) {
2246 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2248 OBJ(smapi_bat_perc) {
2249 print_smapi_bat_perc(obj, p, p_max_size);
2251 OBJ(smapi_bat_temp) {
2252 print_smapi_bat_temp(obj, p, p_max_size);
2254 OBJ(smapi_bat_power) {
2255 print_smapi_bat_power(obj, p, p_max_size);
2257 OBJ(smapi_bat_bar) {
2258 print_smapi_bat_bar(obj, p, p_max_size);
2263 char buf[max_user_text];
2265 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2266 snprintf(p, p_max_size, "%s", buf);
2272 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2273 char buf[max_user_text];
2276 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2277 snprintf(p, p_max_size, "%s", buf);
2278 if(total_updates % 2) {
2279 for(j=0; p[j] != 0; j++) {
2285 char buf[max_user_text];
2287 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2289 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2290 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2291 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2292 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2293 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2294 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2295 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2297 snprintf(p, p_max_size, "%s", buf);
2300 print_scroll(obj, p, p_max_size, cur);
2303 print_combine(obj, p, cur);
2307 print_nvidia_value(obj, display, p, p_max_size);
2312 /* This is just a meta-object to set host:port */
2315 snprintf(p, p_max_size, "%s",
2316 cur->apcupsd.items[APCUPSD_NAME]);
2318 OBJ(apcupsd_model) {
2319 snprintf(p, p_max_size, "%s",
2320 cur->apcupsd.items[APCUPSD_MODEL]);
2322 OBJ(apcupsd_upsmode) {
2323 snprintf(p, p_max_size, "%s",
2324 cur->apcupsd.items[APCUPSD_UPSMODE]);
2326 OBJ(apcupsd_cable) {
2327 snprintf(p, p_max_size, "%s",
2328 cur->apcupsd.items[APCUPSD_CABLE]);
2330 OBJ(apcupsd_status) {
2331 snprintf(p, p_max_size, "%s",
2332 cur->apcupsd.items[APCUPSD_STATUS]);
2334 OBJ(apcupsd_linev) {
2335 snprintf(p, p_max_size, "%s",
2336 cur->apcupsd.items[APCUPSD_LINEV]);
2339 snprintf(p, p_max_size, "%s",
2340 cur->apcupsd.items[APCUPSD_LOAD]);
2342 OBJ(apcupsd_loadbar) {
2344 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2345 new_bar(obj, p, p_max_size, (int) progress);
2348 OBJ(apcupsd_loadgraph) {
2350 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2351 new_graph(obj, p, p_max_size, (int)progress);
2354 OBJ(apcupsd_loadgauge) {
2356 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2357 new_gauge(obj, p, p_max_size, (int)progress);
2359 OBJ(apcupsd_charge) {
2360 snprintf(p, p_max_size, "%s",
2361 cur->apcupsd.items[APCUPSD_CHARGE]);
2363 OBJ(apcupsd_timeleft) {
2364 snprintf(p, p_max_size, "%s",
2365 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2368 snprintf(p, p_max_size, "%s",
2369 cur->apcupsd.items[APCUPSD_TEMP]);
2371 OBJ(apcupsd_lastxfer) {
2372 snprintf(p, p_max_size, "%s",
2373 cur->apcupsd.items[APCUPSD_LASTXFER]);
2375 #endif /* APCUPSD */
2382 size_t a = strlen(p);
2385 iconv_convert(&a, buff_in, p, p_max_size);
2386 #endif /* HAVE_ICONV */
2387 substitute_newlines(p, a - 2);
2395 /* load any new fonts we may have had */
2396 if (need_to_load_fonts) {
2402 void evaluate(const char *text, char *p, int p_max_size)
2404 struct information *tmp_info;
2405 struct text_object subroot;
2407 tmp_info = malloc(sizeof(struct information));
2408 memcpy(tmp_info, &info, sizeof(struct information));
2409 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2410 DBGP2("evaluated '%s' to '%s'", text, p);
2412 free_text_objects(&subroot, 1);
2416 double current_update_time, next_update_time, last_update_time;
2418 static void generate_text(void)
2420 struct information *cur = &info;
2422 unsigned int i, j, k;
2428 current_update_time = get_time();
2432 /* add things to the buffer */
2438 generate_text_internal(p, max_user_text, global_root_object, cur);
2439 if(max_text_width > 0) {
2440 for(i = 0, j = 0; p[i] != 0; i++) {
2441 if(p[i] == '\n') j = 0;
2442 else if(j == max_text_width) {
2443 k = i + strlen(p + i) + 1;
2444 if(k < text_buffer_size) {
2451 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2456 if (stuff_in_uppercase) {
2459 tmp_p = text_buffer;
2461 *tmp_p = toupper(*tmp_p);
2466 next_update_time += update_interval;
2467 if (next_update_time < get_time()) {
2468 next_update_time = get_time() + update_interval;
2469 } else if (next_update_time > get_time() + update_interval) {
2470 next_update_time = get_time() + update_interval;
2472 last_update_time = current_update_time;
2476 void set_update_interval(double interval)
2478 update_interval = interval;
2479 update_interval_old = interval;
2482 int get_string_width(const char *s)
2484 return *s ? calc_text_width(s) : 0;
2488 static int get_string_width_special(char *s, int special_index)
2498 if ((output_methods & TO_X) == 0)
2501 p = strndup(s, text_buffer_size);
2505 if (*p == SPECIAL_CHAR) {
2506 /* shift everything over by 1 so that the special char
2507 * doesn't mess up the size calculation */
2508 for (i = 0; i < (long)strlen(p); i++) {
2509 *(p + i) = *(p + i + 1);
2511 if (specials[special_index + idx].type == GRAPH
2512 || specials[special_index + idx].type == GAUGE
2513 || specials[special_index + idx].type == BAR) {
2514 width += specials[special_index + idx].width;
2517 } else if (*p == SECRIT_MULTILINE_CHAR) {
2524 if (strlen(final) > 1) {
2525 width += calc_text_width(final);
2531 static int text_size_updater(char *s, int special_index);
2533 int last_font_height;
2534 static void update_text_area(void)
2538 if ((output_methods & TO_X) == 0)
2540 /* update text size if it isn't fixed */
2545 text_width = minimum_width;
2547 last_font_height = font_height();
2548 for_each_line(text_buffer, text_size_updater);
2550 if (text_height < minimum_height) {
2551 text_height = minimum_height;
2553 if (text_width > maximum_width && maximum_width > 0) {
2554 text_width = maximum_width;
2558 /* get text position on workarea */
2559 switch (text_alignment) {
2560 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2564 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2565 y = workarea[3] - text_height - gap_y;
2568 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2569 y = workarea[3] / 2 - text_height / 2 - gap_y;
2572 switch (text_alignment) {
2573 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2577 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2578 x = workarea[2] - text_width - gap_x;
2581 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2582 x = workarea[2] / 2 - text_width / 2 - gap_x;
2586 if (text_alignment == NONE) { // Let the WM manage the window
2593 #endif /* OWN_WINDOW */
2596 if (own_window && !fixed_pos) {
2599 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2600 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2601 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2602 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2606 /* If window size doesn't match to workarea's size,
2607 * then window probably includes panels (gnome).
2608 * Blah, doesn't work on KDE. */
2609 if (workarea[2] != window.width || workarea[3] != window.height) {
2618 /* update lua window globals */
2619 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2620 #endif /* HAVE_LUA */
2625 static int cur_x, cur_y; /* current x and y for drawing */
2627 //draw_mode also without X11 because we only need to print to stdout with FG
2628 static int draw_mode; /* FG, BG or OUTLINE */
2630 static long current_color;
2632 static int text_size_updater(char *s, int special_index)
2636 int contain_SECRIT_MULTILINE_CHAR = 0;
2639 if ((output_methods & TO_X) == 0)
2641 /* get string widths and skip specials */
2644 if (*p == SPECIAL_CHAR) {
2646 w += get_string_width(s);
2649 if (specials[special_index].type == BAR
2650 || specials[special_index].type == GAUGE
2651 || specials[special_index].type == GRAPH) {
2652 w += specials[special_index].width;
2653 if (specials[special_index].height > last_font_height) {
2654 last_font_height = specials[special_index].height;
2655 last_font_height += font_height();
2657 } else if (specials[special_index].type == OFFSET) {
2658 if (specials[special_index].arg > 0) {
2659 w += specials[special_index].arg;
2661 } else if (specials[special_index].type == VOFFSET) {
2662 last_font_height += specials[special_index].arg;
2663 } else if (specials[special_index].type == GOTO) {
2664 if (specials[special_index].arg > cur_x) {
2665 w = (int) specials[special_index].arg;
2667 } else if (specials[special_index].type == TAB) {
2668 int start = specials[special_index].arg;
2669 int step = specials[special_index].width;
2671 if (!step || step < 0) {
2674 w += step - (cur_x - text_start_x - start) % step;
2675 } else if (specials[special_index].type == FONT) {
2676 selected_font = specials[special_index].font_added;
2677 if (font_height() > last_font_height) {
2678 last_font_height = font_height();
2684 } else if (*p == SECRIT_MULTILINE_CHAR) {
2685 contain_SECRIT_MULTILINE_CHAR = 1;
2687 lw = get_string_width(s);
2688 *p = SECRIT_MULTILINE_CHAR;
2690 w = lw > w ? lw : w;
2691 text_height += last_font_height;
2695 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2696 if (contain_SECRIT_MULTILINE_CHAR) {
2697 lw = get_string_width(s);
2698 w = lw > w ? lw : w;
2700 w += get_string_width(s);
2702 if (w > text_width) {
2705 if (text_width > maximum_width && maximum_width) {
2706 text_width = maximum_width;
2709 text_height += last_font_height;
2710 last_font_height = font_height();
2711 return special_index;
2715 static inline void set_foreground_color(long c)
2718 if (output_methods & TO_X) {
2720 if (have_argb_visual) {
2721 current_color = c | (own_window_argb_value << 24);
2723 #endif /* USE_ARGB */
2727 #endif /* USE_ARGB */
2728 XSetForeground(display, window.gc, current_color);
2732 if (output_methods & TO_NCURSES) {
2733 attron(COLOR_PAIR(c));
2735 #endif /* NCURSES */
2740 static void draw_string(const char *s)
2742 int i, i2, pos, width_of_s;
2745 char *s_with_newlines;
2751 width_of_s = get_string_width(s);
2752 s_with_newlines = strdup(s);
2753 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2754 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2755 s_with_newlines[i] = '\n';
2758 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2759 printf("%s\n", s_with_newlines);
2760 if (extra_newline) fputc('\n', stdout);
2761 fflush(stdout); /* output immediately, don't buffer */
2763 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2764 fprintf(stderr, "%s\n", s_with_newlines);
2765 fflush(stderr); /* output immediately, don't buffer */
2767 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2768 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2770 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2771 fprintf(append_fpointer, "%s\n", s_with_newlines);
2774 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2775 printw("%s", s_with_newlines);
2778 free(s_with_newlines);
2779 memset(tmpstring1, 0, text_buffer_size);
2780 memset(tmpstring2, 0, text_buffer_size);
2781 strncpy(tmpstring1, s, text_buffer_size - 1);
2786 if (output_methods & TO_X) {
2787 max = ((text_width - width_of_s) / get_string_width(" "));
2790 /* This code looks for tabs in the text and coverts them to spaces.
2791 * The trick is getting the correct number of spaces, and not going
2792 * over the window's size without forcing the window larger. */
2793 for (i = 0; i < (int) text_buffer_size; i++) {
2794 if (tmpstring1[i] == '\t') {
2796 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2797 /* guard against overrun */
2798 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2803 /* guard against overrun */
2804 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2809 if (output_methods & TO_X) {
2810 if (text_width == maximum_width) {
2811 /* this means the text is probably pushing the limit,
2812 * so we'll chop it */
2813 while (cur_x + get_string_width(tmpstring2) - text_start_x
2814 > maximum_width && strlen(tmpstring2) > 0) {
2815 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2822 if (output_methods & TO_X) {
2828 c.pixel = current_color;
2829 // query color on custom colormap
2830 XQueryColor(display, window.colourmap, &c);
2833 c2.color.red = c.red;
2834 c2.color.green = c.green;
2835 c2.color.blue = c.blue;
2836 c2.color.alpha = fonts[selected_font].font_alpha;
2838 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2839 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2841 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2842 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2847 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2850 cur_x += width_of_s;
2853 memcpy(tmpstring1, s, text_buffer_size);
2856 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2864 int last_special_needed = -1;
2865 int orig_special_index = special_index;
2868 if (output_methods & TO_X) {
2869 font_h = font_height();
2870 cur_y += font_ascent();
2872 cur_x = text_start_x;
2876 if (*p == SECRIT_MULTILINE_CHAR) {
2877 /* special newline marker for multiline objects */
2882 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2887 /* draw string before special, unless we're dealing multiline
2889 if (last_special_applied > -1) {
2890 special_index = last_special_applied;
2898 switch (specials[special_index].type) {
2900 case HORIZONTAL_LINE:
2902 int h = specials[special_index].height;
2903 int mid = font_ascent() / 2;
2905 w = text_start_x + text_width - cur_x;
2907 XSetLineAttributes(display, window.gc, h, LineSolid,
2908 CapButt, JoinMiter);
2909 XDrawLine(display, window.drawable, window.gc, cur_x,
2910 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2916 int h = specials[special_index].height;
2917 int tmp_s = specials[special_index].arg;
2918 int mid = font_ascent() / 2;
2919 char ss[2] = { tmp_s, tmp_s };
2921 w = text_start_x + text_width - cur_x - 1;
2922 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2923 CapButt, JoinMiter);
2924 XSetDashes(display, window.gc, 0, ss, 2);
2925 XDrawLine(display, window.drawable, window.gc, cur_x,
2926 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2932 int h, bar_usage, by;
2933 if (cur_x - text_start_x > maximum_width
2934 && maximum_width > 0) {
2937 h = specials[special_index].height;
2938 bar_usage = specials[special_index].arg;
2939 by = cur_y - (font_ascent() / 2) - 1;
2944 w = specials[special_index].width;
2946 w = text_start_x + text_width - cur_x - 1;
2952 XSetLineAttributes(display, window.gc, 1, LineSolid,
2953 CapButt, JoinMiter);
2955 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2957 XFillRectangle(display, window.drawable, window.gc, cur_x,
2958 by, w * bar_usage / 255, h);
2966 case GAUGE: /* new GAUGE */
2969 unsigned long last_colour = current_color;
2971 float angle, px, py;
2975 if (cur_x - text_start_x > maximum_width
2976 && maximum_width > 0) {
2980 h = specials[special_index].height;
2981 by = cur_y - (font_ascent() / 2) - 1;
2986 w = specials[special_index].width;
2988 w = text_start_x + text_width - cur_x - 1;
2994 XSetLineAttributes(display, window.gc, 1, LineSolid,
2995 CapButt, JoinMiter);
2997 XDrawArc(display, window.drawable, window.gc,
2998 cur_x, by, w, h * 2, 0, 180*64);
3001 usage = specials[special_index].arg;
3002 angle = (M_PI)*(float)(usage)/255.;
3003 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3004 py = (float)(by+(h))-(float)(h)*sin(angle);
3006 XDrawLine(display, window.drawable, window.gc,
3007 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3015 set_foreground_color(last_colour);
3023 int h, by, i = 0, j = 0;
3025 unsigned long last_colour = current_color;
3026 unsigned long *tmpcolour = 0;
3027 if (cur_x - text_start_x > maximum_width
3028 && maximum_width > 0) {
3031 h = specials[special_index].height;
3032 by = cur_y - (font_ascent() / 2) - 1;
3037 w = specials[special_index].width;
3039 w = text_start_x + text_width - cur_x - 1;
3044 if (draw_graph_borders) {
3045 XSetLineAttributes(display, window.gc, 1, LineSolid,
3046 CapButt, JoinMiter);
3047 XDrawRectangle(display, window.drawable, window.gc,
3050 XSetLineAttributes(display, window.gc, 1, LineSolid,
3051 CapButt, JoinMiter);
3053 if (specials[special_index].last_colour != 0
3054 || specials[special_index].first_colour != 0) {
3055 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3058 for (i = w - 2; i > -1; i--) {
3059 if (specials[special_index].last_colour != 0
3060 || specials[special_index].first_colour != 0) {
3061 if (specials[special_index].tempgrad) {
3064 (int)((float)(w - 2) - specials[special_index].graph[j] *
3065 (w - 2) / (float)specials[special_index].graph_scale)
3069 (int)((float)(w - 2) - specials[special_index].graph[j] *
3070 (w - 2) / (float)specials[special_index].graph_scale)
3073 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3075 (int)((float)(w - 2) - specials[special_index].graph[j] *
3076 (w - 2) / (float)specials[special_index].graph_scale)
3080 #endif /* DEBUG_lol */
3081 XSetForeground(display, window.gc, tmpcolour[
3082 (int)((float)(w - 2) - specials[special_index].graph[j] *
3083 (w - 2) / (float)specials[special_index].graph_scale)
3086 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3089 /* this is mugfugly, but it works */
3090 XDrawLine(display, window.drawable, window.gc,
3091 cur_x + i + 1, by + h, cur_x + i + 1,
3092 round_to_int((double)by + h - specials[special_index].graph[j] *
3093 (h - 1) / specials[special_index].graph_scale));
3094 if ((w - i) / ((float) (w - 2) /
3095 (specials[special_index].graph_width)) > j
3096 && j < MAX_GRAPH_DEPTH - 3) {
3100 if (tmpcolour) free(tmpcolour);
3105 /* if (draw_mode == BG) {
3106 set_foreground_color(default_bg_color);
3107 } else if (draw_mode == OUTLINE) {
3108 set_foreground_color(default_out_color);
3110 set_foreground_color(default_fg_color);
3112 if (show_graph_range) {
3115 unsigned short int seconds = update_interval * w;
3121 unsigned short int timeunits;
3123 timeunits = seconds / 86400; seconds %= 86400;
3124 if (timeunits > 0) {
3125 if (asprintf(&tmp_day_str, "%dd", timeunits) < 0) {
3129 tmp_day_str = strdup("");
3131 timeunits = seconds / 3600; seconds %= 3600;
3132 if (timeunits > 0) {
3133 if (asprintf(&tmp_hour_str, "%dh", timeunits) < 0) {
3137 tmp_hour_str = strdup("");
3139 timeunits = seconds / 60; seconds %= 60;
3140 if (timeunits > 0) {
3141 if (asprintf(&tmp_min_str, "%dm", timeunits) < 0) {
3145 tmp_min_str = strdup("");
3148 if (asprintf(&tmp_sec_str, "%ds", seconds) < 0) {
3152 tmp_sec_str = strdup("");
3154 if (asprintf(&tmp_str, "%s%s%s%s", tmp_day_str,
3155 tmp_hour_str, tmp_min_str, tmp_sec_str) < 0) {
3158 #define FREE(a) if ((a)) free((a));
3159 FREE(tmp_day_str); FREE(tmp_hour_str); FREE(tmp_min_str); FREE(tmp_sec_str);
3161 tmp_str = strdup("Range not possible"); /* should never happen, but better safe then sorry */
3163 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3164 cur_y += font_h / 2;
3165 draw_string(tmp_str);
3172 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3176 cur_x += font_ascent() / 2;
3177 cur_y += font_h / 2;
3179 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3181 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3182 draw_string(tmp_str);
3188 set_foreground_color(last_colour);
3194 int old = font_ascent();
3196 cur_y -= font_ascent();
3197 selected_font = specials[special_index].font_added;
3199 if (cur_y + font_ascent() < cur_y + old) {
3202 cur_y += font_ascent();
3204 font_h = font_height();
3209 if (draw_mode == FG) {
3210 set_foreground_color(specials[special_index].arg);
3216 if (draw_mode == BG) {
3217 set_foreground_color(specials[special_index].arg);
3222 if (draw_mode == OUTLINE) {
3223 set_foreground_color(specials[special_index].arg);
3228 w += specials[special_index].arg;
3229 last_special_needed = special_index;
3233 cur_y += specials[special_index].arg;
3237 if (specials[special_index].arg >= 0) {
3238 cur_x = (int) specials[special_index].arg;
3240 last_special_needed = special_index;
3245 int start = specials[special_index].arg;
3246 int step = specials[special_index].width;
3248 if (!step || step < 0) {
3251 w = step - (cur_x - text_start_x - start) % step;
3252 last_special_needed = special_index;
3258 /* TODO: add back in "+ window.border_inner_margin" to the end of
3260 int pos_x = text_start_x + text_width -
3261 get_string_width_special(s, special_index);
3263 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3264 "get_string_width(p) %i gap_x %i "
3265 "specials[special_index].arg %i window.border_inner_margin %i "
3266 "window.border_width %i\n", pos_x, text_start_x, text_width,
3267 cur_x, get_string_width_special(s), gap_x,
3268 specials[special_index].arg, window.border_inner_margin,
3269 window.border_width); */
3270 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3271 cur_x = pos_x - specials[special_index].arg;
3273 last_special_needed = special_index;
3279 int pos_x = (text_width) / 2 - get_string_width_special(s,
3280 special_index) / 2 - (cur_x -
3282 /* int pos_x = text_start_x + text_width / 2 -
3283 get_string_width_special(s) / 2; */
3285 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3286 "get_string_width(p) %i gap_x %i "
3287 "specials[special_index].arg %i\n", pos_x, text_start_x,
3288 text_width, cur_x, get_string_width(s), gap_x,
3289 specials[special_index].arg); */
3290 if (pos_x > specials[special_index].arg) {
3291 w = pos_x - specials[special_index].arg;
3293 last_special_needed = special_index;
3303 if (special_index != last_special_applied) {
3306 special_index = orig_special_index;
3307 last_special_applied = -1;
3318 if (output_methods & TO_NCURSES) {
3321 #endif /* NCURSES */
3323 if (output_methods & TO_X)
3324 cur_y += font_descent();
3326 if (recurse && *recurse) {
3327 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3328 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3330 return special_index;
3333 static int draw_line(char *s, int special_index)
3336 if (output_methods & TO_X) {
3337 return draw_each_line_inner(s, special_index, -1);
3341 if (output_methods & TO_NCURSES) {
3342 return draw_each_line_inner(s, special_index, -1);
3344 #endif /* NCURSES */
3346 UNUSED(special_index);
3350 static void draw_text(void)
3354 llua_draw_pre_hook();
3355 #endif /* HAVE_LUA */
3356 if (output_methods & TO_X) {
3357 cur_y = text_start_y;
3360 if (draw_borders && window.border_width > 0) {
3361 if (stippled_borders) {
3362 char ss[2] = { stippled_borders, stippled_borders };
3363 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3364 CapButt, JoinMiter);
3365 XSetDashes(display, window.gc, 0, ss, 2);
3367 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3368 CapButt, JoinMiter);
3371 XDrawRectangle(display, window.drawable, window.gc,
3372 text_start_x - window.border_inner_margin - window.border_width,
3373 text_start_y - window.border_inner_margin - window.border_width,
3374 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3375 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3383 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3384 attron(COLOR_PAIR(COLOR_WHITE));
3385 #endif /* NCURSES */
3386 for_each_line(text_buffer, draw_line);
3387 #if defined(HAVE_LUA) && defined(X11)
3388 llua_draw_post_hook();
3389 #endif /* HAVE_LUA */
3392 static void draw_stuff(void)
3395 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3397 if (overwrite_file) {
3398 overwrite_fpointer = fopen(overwrite_file, "w");
3399 if(!overwrite_fpointer)
3400 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3403 append_fpointer = fopen(append_file, "a");
3404 if(!append_fpointer)
3405 NORM_ERR("Can't append '%s' anymore", append_file);
3408 if (output_methods & TO_X) {
3410 if (draw_shades && !draw_outline) {
3413 set_foreground_color(default_bg_color);
3424 for (i = -1; i < 2; i++) {
3425 for (j = -1; j < 2; j++) {
3426 if (i == 0 && j == 0) {
3431 set_foreground_color(default_out_color);
3432 draw_mode = OUTLINE;
3440 set_foreground_color(default_fg_color);
3445 #if defined(X11) && defined(HAVE_XDBE)
3446 if (output_methods & TO_X) {
3447 xdbe_swap_buffers();
3449 #endif /* X11 && HAVE_XDBE */
3450 if(overwrite_fpointer) {
3451 fclose(overwrite_fpointer);
3452 overwrite_fpointer = 0;
3454 if(append_fpointer) {
3455 fclose(append_fpointer);
3456 append_fpointer = 0;
3461 static void clear_text(int exposures)
3465 /* The swap action is XdbeBackground, which clears */
3469 if (display && window.window) { // make sure these are !null
3470 /* there is some extra space for borders and outlines */
3471 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3472 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3473 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3474 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3479 static int need_to_update;
3481 /* update_text() generates new text and clears old text area */
3482 static void update_text(void)
3489 if (output_methods & TO_X)
3494 llua_update_info(&info, update_interval);
3495 #endif /* HAVE_LUA */
3498 #ifdef HAVE_SYS_INOTIFY_H
3502 static void main_loop(void)
3505 #ifdef SIGNAL_BLOCKING
3506 sigset_t newmask, oldmask;
3509 #ifdef HAVE_SYS_INOTIFY_H
3510 int inotify_config_wd = -1;
3511 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3512 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3513 char inotify_buff[INOTIFY_BUF_LEN];
3514 #endif /* HAVE_SYS_INOTIFY_H */
3517 #ifdef SIGNAL_BLOCKING
3518 sigemptyset(&newmask);
3519 sigaddset(&newmask, SIGINT);
3520 sigaddset(&newmask, SIGTERM);
3521 sigaddset(&newmask, SIGUSR1);
3524 last_update_time = 0.0;
3525 next_update_time = get_time();
3527 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3528 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3529 char buf[max_user_text];
3531 get_battery_short_status(buf, max_user_text, "BAT0");
3533 update_interval = update_interval_bat;
3535 update_interval = update_interval_old;
3540 #ifdef SIGNAL_BLOCKING
3541 /* block signals. we will inspect for pending signals later */
3542 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3543 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3548 if (output_methods & TO_X) {
3551 /* wait for X event or timeout */
3553 if (!XPending(display)) {
3557 t = next_update_time - get_time();
3561 } else if (t > update_interval) {
3562 t = update_interval;
3565 tv.tv_sec = (long) t;
3566 tv.tv_usec = (long) (t * 1000000) % 1000000;
3568 FD_SET(ConnectionNumber(display), &fdsr);
3570 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3572 if (errno != EINTR) {
3573 NORM_ERR("can't select(): %s", strerror(errno));
3583 if (need_to_update) {
3585 int wx = window.x, wy = window.y;
3595 /* resize window if it isn't right size */
3597 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3598 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3599 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3600 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3601 draw_stuff(); /* redraw everything in our newly sized window */
3602 XResizeWindow(display, window.window, window.width,
3603 window.height); /* resize window */
3604 set_transparent_background(window.window, own_window_argb_value);
3607 xdbe_swap_buffers();
3612 /* update lua window globals */
3613 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3614 #endif /* HAVE_LUA */
3617 /* move window if it isn't in right position */
3618 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3619 XMoveWindow(display, window.window, window.x, window.y);
3624 if (changed && window.type == TYPE_PANEL) {
3627 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3630 switch (text_alignment) {
3657 set_struts(sidenum);
3668 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3669 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3670 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3671 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3672 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3677 /* handle X events */
3678 while (XPending(display)) {
3681 XNextEvent(display, &ev);
3688 r.width = ev.xexpose.width;
3689 r.height = ev.xexpose.height;
3690 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3694 case PropertyNotify:
3696 if ( ev.xproperty.state == PropertyNewValue ) {
3697 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3703 case ReparentNotify:
3704 /* make background transparent */
3706 set_transparent_background(window.window, own_window_argb_value);
3710 case ConfigureNotify:
3712 /* if window size isn't what expected, set fixed size */
3713 if (ev.xconfigure.width != window.width
3714 || ev.xconfigure.height != window.height) {
3715 if (window.width != 0 && window.height != 0) {
3719 /* clear old stuff before screwing up
3724 XWindowAttributes attrs;
3725 if (XGetWindowAttributes(display,
3726 window.window, &attrs)) {
3727 window.width = attrs.width;
3728 window.height = attrs.height;
3732 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3733 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3734 if (text_width > maximum_width
3735 && maximum_width > 0) {
3736 text_width = maximum_width;
3740 /* if position isn't what expected, set fixed pos
3741 * total_updates avoids setting fixed_pos when window
3742 * is set to weird locations when started */
3743 /* // this is broken
3744 if (total_updates >= 2 && !fixed_pos
3745 && (window.x != ev.xconfigure.x
3746 || window.y != ev.xconfigure.y)
3747 && (ev.xconfigure.x != 0
3748 || ev.xconfigure.y != 0)) {
3756 /* if an ordinary window with decorations */
3757 if ((window.type == TYPE_NORMAL &&
3758 (!TEST_HINT(window.hints,
3759 HINT_UNDECORATED))) ||
3760 window.type == TYPE_DESKTOP) {
3761 /* allow conky to hold input focus. */
3764 /* forward the click to the desktop window */
3765 XUngrabPointer(display, ev.xbutton.time);
3766 ev.xbutton.window = window.desktop;
3767 ev.xbutton.x = ev.xbutton.x_root;
3768 ev.xbutton.y = ev.xbutton.y_root;
3769 XSendEvent(display, ev.xbutton.window, False,
3770 ButtonPressMask, &ev);
3771 XSetInputFocus(display, ev.xbutton.window,
3772 RevertToParent, ev.xbutton.time);
3779 /* if an ordinary window with decorations */
3780 if ((window.type == TYPE_NORMAL)
3781 && (!TEST_HINT(window.hints,
3782 HINT_UNDECORATED))) {
3783 /* allow conky to hold input focus. */
3786 /* forward the release to the desktop window */
3787 ev.xbutton.window = window.desktop;
3788 ev.xbutton.x = ev.xbutton.x_root;
3789 ev.xbutton.y = ev.xbutton.y_root;
3790 XSendEvent(display, ev.xbutton.window, False,
3791 ButtonReleaseMask, &ev);
3800 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3801 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3803 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3804 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3806 #endif /* HAVE_XDAMAGE */
3812 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3813 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3814 #endif /* HAVE_XDAMAGE */
3816 /* XDBE doesn't seem to provide a way to clear the back buffer
3817 * without interfering with the front buffer, other than passing
3818 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3819 * using XDBE, we need to redraw the text even if it wasn't part of
3820 * the exposed area. OTOH, if we're not going to call draw_stuff at
3821 * all, then no swap happens and we can safely do nothing. */
3823 if (!XEmptyRegion(x11_stuff.region)) {
3828 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3829 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3830 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3831 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3832 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3835 XSetRegion(display, window.gc, x11_stuff.region);
3838 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3842 XDestroyRegion(x11_stuff.region);
3843 x11_stuff.region = XCreateRegion();
3847 t = (next_update_time - get_time()) * 1000000;
3848 if(t > 0) usleep((useconds_t)t);
3852 if(output_methods & TO_NCURSES) {
3861 #ifdef SIGNAL_BLOCKING
3862 /* unblock signals of interest and let handler fly */
3863 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3864 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3868 switch (g_signal_pending) {
3871 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3876 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3879 if (output_methods & TO_X) {
3880 XDestroyRegion(x11_stuff.region);
3881 x11_stuff.region = NULL;
3883 XDamageDestroy(display, x11_stuff.damage);
3884 XFixesDestroyRegion(display, x11_stuff.region2);
3885 XFixesDestroyRegion(display, x11_stuff.part);
3886 #endif /* HAVE_XDAMAGE */
3892 if(overwrite_file) {
3893 free(overwrite_file);
3902 /* Reaching here means someone set a signal
3903 * (SIGXXXX, signal_handler), but didn't write any code
3905 * If you don't want to handle a signal, don't set a handler on
3906 * it in the first place. */
3907 if (g_signal_pending) {
3908 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3912 #ifdef HAVE_SYS_INOTIFY_H
3913 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3914 inotify_config_wd = inotify_add_watch(inotify_fd,
3918 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3919 int len = 0, idx = 0;
3921 struct timeval time_to_wait;
3923 FD_ZERO(&descriptors);
3924 FD_SET(inotify_fd, &descriptors);
3926 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3928 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3929 if (FD_ISSET(inotify_fd, &descriptors)) {
3930 /* process inotify events */
3931 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3932 while (len > 0 && idx < len) {
3933 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3934 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3935 /* current_config should be reloaded */
3936 NORM_ERR("'%s' modified, reloading...", current_config);
3938 if (ev->mask & IN_IGNORED) {
3939 /* for some reason we get IN_IGNORED here
3940 * sometimes, so we need to re-add the watch */
3941 inotify_config_wd = inotify_add_watch(inotify_fd,
3949 llua_inotify_query(ev->wd, ev->mask);
3951 #endif /* HAVE_LUA */
3952 idx += INOTIFY_EVENT_SIZE + ev->len;
3955 } else if (disable_auto_reload && inotify_fd != -1) {
3956 inotify_rm_watch(inotify_fd, inotify_config_wd);
3958 inotify_fd = inotify_config_wd = 0;
3960 #endif /* HAVE_SYS_INOTIFY_H */
3963 llua_update_info(&info, update_interval);
3964 #endif /* HAVE_LUA */
3965 g_signal_pending = 0;
3967 clean_up(current_mail_spool, NULL);
3969 #ifdef HAVE_SYS_INOTIFY_H
3970 if (inotify_fd != -1) {
3971 inotify_rm_watch(inotify_fd, inotify_config_wd);
3973 inotify_fd = inotify_config_wd = 0;
3975 #endif /* HAVE_SYS_INOTIFY_H */
3979 static void load_config_file_x11(const char *);
3981 void initialisation(int argc, char** argv);
3983 /* reload the config file */
3984 static void reload_config(void)
3986 char *current_config_copy = strdup(current_config);
3987 clean_up(NULL, NULL);
3988 sleep(1); /* slight pause */
3989 current_config = current_config_copy;
3990 initialisation(argc_copy, argv_copy);
3994 void clean_up_x11() {
3995 if(window_created == 1) {
3996 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3997 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3998 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3999 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4004 if(x11_stuff.region) {
4005 XDestroyRegion(x11_stuff.region);
4006 x11_stuff.region = NULL;
4009 XCloseDisplay(display);
4012 if(info.x11.desktop.all_names) {
4013 free(info.x11.desktop.all_names);
4014 info.x11.desktop.all_names = NULL;
4016 if (info.x11.desktop.name) {
4017 free(info.x11.desktop.name);
4018 info.x11.desktop.name = NULL;
4024 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4028 if(output_methods & TO_NCURSES) {
4032 conftree_empty(currentconffile);
4033 currentconffile = NULL;
4040 timed_thread_destroy_registered_threads();
4042 if (info.cpu_usage) {
4043 free(info.cpu_usage);
4044 info.cpu_usage = NULL;
4047 if (x_initialised == YES) {
4050 free(fonts); //in set_default_configurations a font is set but not loaded
4058 free_text_objects(&global_root_object, 0);
4077 free(current_config);
4080 #ifdef TCP_PORT_MONITOR
4081 tcp_portmon_clear();
4090 weather_free_info();
4093 llua_shutdown_hook();
4095 #endif /* HAVE_LUA */
4097 if (output_methods & TO_X)
4105 for (i = 0; i < special_count; i++) {
4106 if (specials[i].type == GRAPH) {
4107 free(specials[i].graph);
4115 clear_diskio_stats();
4116 if(global_cpu != NULL) {
4122 void clean_up(void *memtofree1, void* memtofree2)
4124 free_update_callbacks();
4125 clean_up_without_threads(memtofree1, memtofree2);
4128 static int string_to_bool(const char *s)
4131 // Assumes an option without a true/false means true
4133 } else if (strcasecmp(s, "yes") == EQUAL) {
4135 } else if (strcasecmp(s, "true") == EQUAL) {
4137 } else if (strcasecmp(s, "1") == EQUAL) {
4144 static enum alignment string_to_alignment(const char *s)
4146 if (strcasecmp(s, "top_left") == EQUAL) {
4148 } else if (strcasecmp(s, "top_right") == EQUAL) {
4150 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4152 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4154 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4155 return BOTTOM_RIGHT;
4156 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4157 return BOTTOM_MIDDLE;
4158 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4160 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4161 return MIDDLE_RIGHT;
4162 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4163 return MIDDLE_MIDDLE;
4164 } else if (strcasecmp(s, "tl") == EQUAL) {
4166 } else if (strcasecmp(s, "tr") == EQUAL) {
4168 } else if (strcasecmp(s, "tm") == EQUAL) {
4170 } else if (strcasecmp(s, "bl") == EQUAL) {
4172 } else if (strcasecmp(s, "br") == EQUAL) {
4173 return BOTTOM_RIGHT;
4174 } else if (strcasecmp(s, "bm") == EQUAL) {
4175 return BOTTOM_MIDDLE;
4176 } else if (strcasecmp(s, "ml") == EQUAL) {
4178 } else if (strcasecmp(s, "mr") == EQUAL) {
4179 return MIDDLE_RIGHT;
4180 } else if (strcasecmp(s, "mm") == EQUAL) {
4181 return MIDDLE_MIDDLE;
4182 } else if (strcasecmp(s, "none") == EQUAL) {
4185 return ALIGNMENT_ERROR;
4190 static void set_default_configurations_for_x(void)
4192 default_fg_color = WhitePixel(display, screen);
4193 default_bg_color = BlackPixel(display, screen);
4194 default_out_color = BlackPixel(display, screen);
4195 color0 = default_fg_color;
4196 color1 = default_fg_color;
4197 color2 = default_fg_color;
4198 color3 = default_fg_color;
4199 color4 = default_fg_color;
4200 color5 = default_fg_color;
4201 color6 = default_fg_color;
4202 color7 = default_fg_color;
4203 color8 = default_fg_color;
4204 color9 = default_fg_color;
4205 current_text_color = default_fg_color;
4209 static void set_default_configurations(void)
4216 fork_to_background = 0;
4217 total_run_times = 0;
4218 info.cpu_avg_samples = 2;
4219 info.net_avg_samples = 2;
4220 info.diskio_avg_samples = 2;
4225 format_human_readable = 1;
4235 mpd_env_host = getenv("MPD_HOST");
4236 mpd_env_port = getenv("MPD_PORT");
4238 if (!mpd_env_host || !strlen(mpd_env_host)) {
4239 mpd_set_host("localhost");
4241 /* MPD_HOST environment variable is set */
4242 char *mpd_hostpart = strchr(mpd_env_host, '@');
4243 if (!mpd_hostpart) {
4244 mpd_set_host(mpd_env_host);
4246 /* MPD_HOST contains a password */
4247 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4248 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4250 if (!strlen(mpd_hostpart + 1)) {
4251 mpd_set_host("localhost");
4253 mpd_set_host(mpd_hostpart + 1);
4256 mpd_set_password(mpd_password, 1);
4261 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4262 /* failed to set port from environment variable */
4263 mpd_set_port("6600");
4267 info.xmms2.artist = NULL;
4268 info.xmms2.album = NULL;
4269 info.xmms2.title = NULL;
4270 info.xmms2.genre = NULL;
4271 info.xmms2.comment = NULL;
4272 info.xmms2.url = NULL;
4273 info.xmms2.status = NULL;
4274 info.xmms2.playlist = NULL;
4276 use_spacer = NO_SPACER;
4278 output_methods = TO_X;
4280 output_methods = TO_STDOUT;
4286 show_graph_scale = 0;
4287 show_graph_range = 0;
4290 draw_graph_borders = 1;
4292 set_first_font("6x10");
4300 window.type = TYPE_NORMAL;
4302 strcpy(window.class_name, PACKAGE_NAME);
4303 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4305 use_argb_visual = 0;
4306 own_window_argb_value = 255;
4309 stippled_borders = 0;
4310 window.border_inner_margin = 3;
4311 window.border_outer_margin = 1;
4312 window.border_width = 1;
4313 text_alignment = BOTTOM_LEFT;
4314 info.x11.monitor.number = 1;
4315 info.x11.monitor.current = 0;
4316 info.x11.desktop.current = 1;
4317 info.x11.desktop.number = 1;
4318 info.x11.desktop.nitems = 0;
4319 info.x11.desktop.all_names = NULL;
4320 info.x11.desktop.name = NULL;
4325 free(current_mail_spool);
4329 variable_substitute(MAIL_FILE, buf, 256);
4330 if (buf[0] != '\0') {
4331 current_mail_spool = strndup(buf, text_buffer_size);
4336 set_update_interval(3);
4337 update_interval_bat = NOBATTERY;
4338 info.music_player_interval = 1.0;
4339 stuff_in_uppercase = 0;
4340 info.users.number = 1;
4342 set_times_in_seconds(0);
4344 #ifdef TCP_PORT_MONITOR
4345 /* set default connection limit */
4346 tcp_portmon_set_max_connections(0);
4350 /* returns 1 if you can overwrite or create the file at 'path' */
4351 static _Bool overwrite_works(const char *path)
4355 if (!(filepointer = fopen(path, "w")))
4357 fclose(filepointer);
4361 /* returns 1 if you can append or create the file at 'path' */
4362 static _Bool append_works(const char *path)
4366 if (!(filepointer = fopen(path, "a")))
4368 fclose(filepointer);
4374 /* WARNING, this type not in Xlib spec */
4375 int x11_error_handler(Display *d, XErrorEvent *err)
4376 __attribute__((noreturn));
4377 int x11_error_handler(Display *d, XErrorEvent *err)
4379 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",
4381 (long unsigned)err->display,
4382 (long)err->resourceid,
4392 int x11_ioerror_handler(Display *d)
4393 __attribute__((noreturn));
4394 int x11_ioerror_handler(Display *d)
4396 NORM_ERR("X Error: Display %lx\n",
4403 static void X11_initialisation(void)
4405 if (x_initialised == YES) return;
4406 output_methods |= TO_X;
4408 set_default_configurations_for_x();
4409 x_initialised = YES;
4412 /* WARNING, this type not in Xlib spec */
4413 XSetErrorHandler(&x11_error_handler);
4414 XSetIOErrorHandler(&x11_ioerror_handler);
4418 static char **xargv = 0;
4419 static int xargc = 0;
4421 static void X11_create_window(void)
4423 if (output_methods & TO_X) {
4425 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4426 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4428 #else /* OWN_WINDOW */
4429 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4430 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4432 #endif /* OWN_WINDOW */
4436 update_text_area(); /* to position text/window on screen */
4439 if (own_window && !fixed_pos) {
4440 XMoveWindow(display, window.window, window.x, window.y);
4443 set_transparent_background(window.window, own_window_argb_value);
4451 x11_stuff.region = XCreateRegion();
4453 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4454 NORM_ERR("Xdamage extension unavailable");
4456 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4457 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4458 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4459 #endif /* HAVE_XDAMAGE */
4462 update_text_area(); /* to get initial size of the window */
4465 /* setup lua window globals */
4466 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4467 #endif /* HAVE_LUA */
4471 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4472 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4473 #define CONF2(a) if (strcasecmp(name, a) == 0)
4474 #define CONF(a) else CONF2(a)
4475 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4476 || strcasecmp(name, b) == 0)
4477 #define CONF_CONTINUE 1
4478 #define CONF_BREAK 2
4479 #define CONF_BUFF_SIZE 512
4481 static FILE *open_config_file(const char *f)
4483 #ifdef CONFIG_OUTPUT
4484 if (!strcmp(f, "==builtin==")) {
4485 return conf_cookie_open();
4487 #endif /* CONFIG_OUTPUT */
4488 return fopen(f, "r");
4491 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4495 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4498 remove_comments(buf);
4503 while (*p && isspace((int) *p)) {
4507 return CONF_CONTINUE; /* empty line */
4514 while (*p2 && !isspace((int) *p2)) {
4518 *p2 = '\0'; /* break at name's end */
4525 while (*p && isspace((int) *p)) {
4531 p2 = *value + strlen(*value);
4532 while (isspace((int) *(p2 - 1))) {
4542 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4544 if (windowtype == TYPE_DOCK) {
4545 NORM_ERR("alignment is disabled when own_window_type is dock");
4547 #endif /*OWN_WINDOW */
4549 int a = string_to_alignment(value);
4552 if (setbyconffile) {
4554 } else NORM_ERR("'%s' is not a alignment setting", value);
4556 *ltext_alignment = a;
4558 } else if (setbyconffile) {
4564 char load_config_file(const char *f)
4569 fp = open_config_file(f);
4573 DBGP("reading contents from config file '%s'", f);
4576 char buff[CONF_BUFF_SIZE], *name, *value;
4577 int ret = do_config_step(&line, fp, buff, &name, &value);
4578 if (ret == CONF_BREAK) {
4580 } else if (ret == CONF_CONTINUE) {
4586 if (string_to_bool(value)) {
4587 output_methods &= TO_X;
4590 output_methods &= ~TO_X;
4591 x_initialised = NEVER;
4595 if (!value || x_initialised == YES) {
4600 disp = strdup(value);
4604 setalignment(&text_alignment, window.type, value, f, line, 1);
4606 CONF("background") {
4607 fork_to_background = string_to_bool(value);
4610 CONF2("background") {
4611 fork_to_background = string_to_bool(value);
4615 CONF("show_graph_scale") {
4616 show_graph_scale = string_to_bool(value);
4618 CONF("show_graph_range") {
4619 show_graph_range = string_to_bool(value);
4621 CONF("border_inner_margin") {
4623 window.border_inner_margin = strtol(value, 0, 0);
4624 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4629 CONF("border_outer_margin") {
4631 window.border_outer_margin = strtol(value, 0, 0);
4632 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4637 CONF("border_width") {
4639 window.border_width = strtol(value, 0, 0);
4640 if (window.border_width < 1) window.border_width = 1;
4646 #define TEMPLATE_CONF(n) \
4647 CONF("template"#n) { \
4648 if (set_template(n, value)) \
4663 parse_global_imap_mail_args(value);
4670 parse_global_pop3_mail_args(value);
4675 CONF("default_bar_size") {
4678 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4685 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4689 CONF("default_graph_size") {
4692 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4699 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4702 CONF("default_gauge_size") {
4705 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4712 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4719 mpd_set_host(value);
4725 if (value && mpd_set_port(value)) {
4729 CONF("mpd_password") {
4731 mpd_set_password(value, 0);
4737 CONF("music_player_interval") {
4739 info.music_player_interval = strtod(value, 0);
4745 CONF("sensor_device") {
4747 sensor_device = strtol(value, 0, 0);
4753 CONF("cpu_avg_samples") {
4755 cpu_avg_samples = strtol(value, 0, 0);
4756 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4759 info.cpu_avg_samples = cpu_avg_samples;
4765 CONF("net_avg_samples") {
4767 net_avg_samples = strtol(value, 0, 0);
4768 if (net_avg_samples < 1 || net_avg_samples > 14) {
4771 info.net_avg_samples = net_avg_samples;
4777 CONF("diskio_avg_samples") {
4779 diskio_avg_samples = strtol(value, 0, 0);
4780 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4783 info.diskio_avg_samples = diskio_avg_samples;
4791 CONF("double_buffer") {
4792 use_xdbe = string_to_bool(value);
4796 CONF("override_utf8_locale") {
4797 utf8_mode = string_to_bool(value);
4799 CONF("draw_borders") {
4800 draw_borders = string_to_bool(value);
4802 CONF("draw_graph_borders") {
4803 draw_graph_borders = string_to_bool(value);
4805 CONF("draw_shades") {
4806 draw_shades = string_to_bool(value);
4808 CONF("draw_outline") {
4809 draw_outline = string_to_bool(value);
4812 CONF("times_in_seconds") {
4813 set_times_in_seconds(string_to_bool(value));
4815 CONF("max_text_width") {
4816 max_text_width = atoi(value);
4818 CONF("out_to_console") {
4819 if(string_to_bool(value)) {
4820 output_methods |= TO_STDOUT;
4822 output_methods &= ~TO_STDOUT;
4825 CONF("extra_newline") {
4826 extra_newline = string_to_bool(value);
4828 CONF("disable_auto_reload") {
4829 disable_auto_reload = string_to_bool(value);
4831 CONF("out_to_stderr") {
4832 if(string_to_bool(value))
4833 output_methods |= TO_STDERR;
4836 CONF("out_to_ncurses") {
4837 if(string_to_bool(value)) {
4840 output_methods |= TO_NCURSES;
4844 CONF("overwrite_file") {
4845 if(overwrite_file) {
4846 free(overwrite_file);
4849 if(overwrite_works(value)) {
4850 overwrite_file = strdup(value);
4851 output_methods |= OVERWRITE_FILE;
4853 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4855 CONF("append_file") {
4860 if(append_works(value)) {
4861 append_file = strdup(value);
4862 output_methods |= APPEND_FILE;
4864 NORM_ERR("append_file won't be able to create/append '%s'", value);
4866 CONF("use_spacer") {
4868 if (strcasecmp(value, "left") == EQUAL) {
4869 use_spacer = LEFT_SPACER;
4870 } else if (strcasecmp(value, "right") == EQUAL) {
4871 use_spacer = RIGHT_SPACER;
4872 } else if (strcasecmp(value, "none") == EQUAL) {
4873 use_spacer = NO_SPACER;
4875 use_spacer = string_to_bool(value);
4876 NORM_ERR("use_spacer should have an argument of left, right, or"
4877 " none. '%s' seems to be some form of '%s', so"
4878 " defaulting to %s.", value,
4879 use_spacer ? "true" : "false",
4880 use_spacer ? "right" : "none");
4882 use_spacer = RIGHT_SPACER;
4884 use_spacer = NO_SPACER;
4888 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4889 use_spacer = RIGHT_SPACER;
4895 use_xft = string_to_bool(value);
4899 set_first_font(value);
4903 if (value && font_count >= 0) {
4904 fonts[0].font_alpha = atof(value) * 65535.0;
4911 if (string_to_bool(value)) {
4912 NORM_ERR("Xft not enabled at compile time");
4916 /* xftfont silently ignored when no Xft */
4919 /* xftalpha is silently ignored when no Xft */
4924 set_first_font(value);
4932 gap_x = atoi(value);
4939 gap_y = atoi(value);
4945 CONF("mail_spool") {
4949 variable_substitute(value, buffer, 256);
4951 if (buffer[0] != '\0') {
4952 if (current_mail_spool) {
4953 free(current_mail_spool);
4955 current_mail_spool = strndup(buffer, text_buffer_size);
4962 CONF("minimum_size") {
4964 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4966 if (sscanf(value, "%d", &minimum_width) != 1) {
4974 CONF("maximum_width") {
4976 if (sscanf(value, "%d", &maximum_width) != 1) {
4984 CONF("no_buffers") {
4985 no_buffers = string_to_bool(value);
4987 CONF("top_name_width") {
4988 if (set_top_name_width(value))
4991 CONF("top_cpu_separate") {
4992 cpu_separate = string_to_bool(value);
4994 CONF("short_units") {
4995 short_units = string_to_bool(value);
4997 CONF("format_human_readable") {
4998 format_human_readable = string_to_bool(value);
5001 CONF("hddtemp_host") {
5002 set_hddtemp_host(value);
5004 CONF("hddtemp_port") {
5005 set_hddtemp_port(value);
5007 #endif /* HDDTEMP */
5008 CONF("pad_percents") {
5009 pad_percents = atoi(value);
5013 CONF("own_window") {
5015 own_window = string_to_bool(value);
5018 CONF("own_window_class") {
5020 memset(window.class_name, 0, sizeof(window.class_name));
5021 strncpy(window.class_name, value,
5022 sizeof(window.class_name) - 1);
5025 CONF("own_window_title") {
5027 memset(window.title, 0, sizeof(window.title));
5028 strncpy(window.title, value, sizeof(window.title) - 1);
5031 CONF("own_window_transparent") {
5033 set_transparent = string_to_bool(value);
5036 CONF("own_window_hints") {
5038 char *p_hint, *p_save;
5039 char delim[] = ", ";
5041 /* tokenize the value into individual hints */
5042 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5044 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5045 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5046 SET_HINT(window.hints, HINT_UNDECORATED);
5047 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5048 SET_HINT(window.hints, HINT_BELOW);
5049 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5050 SET_HINT(window.hints, HINT_ABOVE);
5051 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5052 SET_HINT(window.hints, HINT_STICKY);
5053 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5054 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5055 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5056 SET_HINT(window.hints, HINT_SKIP_PAGER);
5061 p_hint = strtok_r(NULL, delim, &p_save);
5062 } while (p_hint != NULL);
5068 CONF("own_window_type") {
5070 if (strncmp(value, "normal", 6) == EQUAL) {
5071 window.type = TYPE_NORMAL;
5072 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5073 window.type = TYPE_DESKTOP;
5074 } else if (strncmp(value, "dock", 4) == EQUAL) {
5075 window.type = TYPE_DOCK;
5076 text_alignment = TOP_LEFT;
5077 } else if (strncmp(value, "panel", 5) == EQUAL) {
5078 window.type = TYPE_PANEL;
5079 } else if (strncmp(value, "override", 8) == EQUAL) {
5080 window.type = TYPE_OVERRIDE;
5089 CONF("own_window_argb_visual") {
5090 use_argb_visual = string_to_bool(value);
5092 CONF("own_window_argb_value") {
5093 own_window_argb_value = strtol(value, 0, 0);
5094 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5095 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5098 #endif /* USE_ARGB */
5100 CONF("stippled_borders") {
5102 stippled_borders = strtol(value, 0, 0);
5104 stippled_borders = 4;
5108 CONF("imlib_cache_size") {
5110 cimlib_set_cache_size(atoi(value));
5113 CONF("imlib_cache_flush_interval") {
5115 cimlib_set_cache_flush_interval(atoi(value));
5120 CONF("update_interval_on_battery") {
5122 update_interval_bat = strtod(value, 0);
5127 CONF("update_interval") {
5129 set_update_interval(strtod(value, 0));
5133 if (info.music_player_interval == 0) {
5134 // default to update_interval
5135 info.music_player_interval = update_interval;
5138 CONF("total_run_times") {
5140 total_run_times = strtod(value, 0);
5146 stuff_in_uppercase = string_to_bool(value);
5148 CONF("max_specials") {
5150 max_specials = atoi(value);
5155 CONF("max_user_text") {
5157 max_user_text = atoi(value);
5162 CONF("text_buffer_size") {
5164 text_buffer_size = atoi(value);
5165 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5166 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5167 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5175 if (output_methods & TO_X) {
5176 X11_initialisation();
5185 global_text = (char *) malloc(1);
5186 global_text[0] = '\0';
5189 unsigned int l = strlen(global_text);
5191 char buf[CONF_BUFF_SIZE];
5193 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5199 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5207 /* Check for continuation of \\-\n. */
5208 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5209 global_text[l-1] = '\0';
5213 global_text = (char *) realloc(global_text, l + bl + 1);
5214 strcat(global_text, buf);
5216 if (strlen(global_text) > max_user_text) {
5220 global_text_lines = line + 1;
5223 #ifdef TCP_PORT_MONITOR
5224 CONF("max_port_monitor_connections") {
5226 if (!value || (sscanf(value, "%d", &max) != 1)) {
5227 /* an error. use default, warn and continue. */
5228 tcp_portmon_set_max_connections(0);
5230 } else if (tcp_portmon_set_max_connections(max)) {
5231 /* max is < 0, default has been set*/
5236 CONF("if_up_strictness") {
5238 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5239 ifup_strictness = IFUP_UP;
5240 } else if (strcasecmp(value, "up") == EQUAL) {
5241 ifup_strictness = IFUP_UP;
5242 } else if (strcasecmp(value, "link") == EQUAL) {
5243 ifup_strictness = IFUP_LINK;
5244 } else if (strcasecmp(value, "address") == EQUAL) {
5245 ifup_strictness = IFUP_ADDR;
5247 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5248 ifup_strictness = IFUP_UP;
5252 CONF("temperature_unit") {
5254 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5255 } else if (set_temp_output_unit(value)) {
5256 NORM_ERR("temperature_unit: incorrect argument");
5263 char *ptr = strtok(value, " ");
5266 ptr = strtok(NULL, " ");
5273 CONF("lua_draw_hook_pre") {
5275 llua_set_draw_pre_hook(value);
5280 CONF("lua_draw_hook_post") {
5282 llua_set_draw_post_hook(value);
5287 CONF("lua_startup_hook") {
5289 llua_set_startup_hook(value);
5294 CONF("lua_shutdown_hook") {
5296 llua_set_shutdown_hook(value);
5302 #endif /* HAVE_LUA */
5314 CONF("default_color"){}
5315 CONF3("default_shade_color", "default_shadecolor"){}
5316 CONF3("default_outline_color", "default_outlinecolor") {}
5317 CONF("own_window_colour") {}
5320 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5326 if (info.music_player_interval == 0) {
5327 // default to update_interval
5328 info.music_player_interval = update_interval;
5330 if (!global_text) { // didn't supply any text
5331 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5333 if (!output_methods) {
5334 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5336 #if defined(NCURSES)
5338 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5339 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5340 output_methods &= ~TO_NCURSES;
5344 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5345 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5346 output_methods &= ~(TO_STDOUT | TO_STDERR);
5348 #endif /* NCURSES */
5353 static void load_config_file_x11(const char *f)
5358 fp = open_config_file(f);
5362 DBGP("reading contents from config file '%s'", f);
5365 char buff[CONF_BUFF_SIZE], *name, *value;
5366 int ret = do_config_step(&line, fp, buff, &name, &value);
5367 if (ret == CONF_BREAK) {
5369 } else if (ret == CONF_CONTINUE) {
5374 X11_initialisation();
5375 if (x_initialised == YES) {
5377 color0 = get_x11_color(value);
5384 X11_initialisation();
5385 if (x_initialised == YES) {
5387 color1 = get_x11_color(value);
5394 X11_initialisation();
5395 if (x_initialised == YES) {
5397 color2 = get_x11_color(value);
5404 X11_initialisation();
5405 if (x_initialised == YES) {
5407 color3 = get_x11_color(value);
5414 X11_initialisation();
5415 if (x_initialised == YES) {
5417 color4 = get_x11_color(value);
5424 X11_initialisation();
5425 if (x_initialised == YES) {
5427 color5 = get_x11_color(value);
5434 X11_initialisation();
5435 if (x_initialised == YES) {
5437 color6 = get_x11_color(value);
5444 X11_initialisation();
5445 if (x_initialised == YES) {
5447 color7 = get_x11_color(value);
5454 X11_initialisation();
5455 if (x_initialised == YES) {
5457 color8 = get_x11_color(value);
5464 X11_initialisation();
5465 if (x_initialised == YES) {
5467 color9 = get_x11_color(value);
5473 CONF("default_color") {
5474 X11_initialisation();
5475 if (x_initialised == YES) {
5477 default_fg_color = get_x11_color(value);
5483 CONF3("default_shade_color", "default_shadecolor") {
5484 X11_initialisation();
5485 if (x_initialised == YES) {
5487 default_bg_color = get_x11_color(value);
5493 CONF3("default_outline_color", "default_outlinecolor") {
5494 X11_initialisation();
5495 if (x_initialised == YES) {
5497 default_out_color = get_x11_color(value);
5504 CONF("own_window_colour") {
5505 X11_initialisation();
5506 if (x_initialised == YES) {
5508 background_colour = get_x11_color(value);
5510 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5511 "'#' for hex colours");
5517 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5518 if(output_methods & TO_X) {
5519 X11_initialisation();
5528 #undef CONF_CONTINUE
5529 #undef CONF_BUFF_SIZE
5537 static void print_help(const char *prog_name) {
5538 printf("Usage: %s [OPTION]...\n"
5539 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5540 "window. Command line options will override configurations defined in config\n"
5542 " -v, --version version\n"
5543 " -q, --quiet quiet mode\n"
5544 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5545 " -c, --config=FILE config file to load\n"
5546 #ifdef CONFIG_OUTPUT
5547 " -C, --print-config print the builtin default config to stdout\n"
5548 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5550 " -d, --daemonize daemonize, fork to background\n"
5551 " -h, --help help\n"
5553 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5554 " -f, --font=FONT font to use\n"
5555 " -X, --display=DISPLAY X11 display to use\n"
5557 " -o, --own-window create own window to draw\n"
5560 " -b, --double-buffer double buffer (prevents flickering)\n"
5562 " -w, --window-id=WIN_ID window id to draw\n"
5563 " -x X x position\n"
5564 " -y Y y position\n"
5566 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5567 " -u, --interval=SECS update interval\n"
5568 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5569 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5574 /* : means that character before that takes an argument */
5575 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5585 #ifdef CONFIG_OUTPUT
5590 static const struct option longopts[] = {
5591 { "help", 0, NULL, 'h' },
5592 { "version", 0, NULL, 'V' },
5593 { "quiet", 0, NULL, 'q' },
5594 { "debug", 0, NULL, 'D' },
5595 { "config", 1, NULL, 'c' },
5596 #ifdef CONFIG_OUTPUT
5597 { "print-config", 0, NULL, 'C' },
5599 { "daemonize", 0, NULL, 'd' },
5601 { "alignment", 1, NULL, 'a' },
5602 { "font", 1, NULL, 'f' },
5603 { "display", 1, NULL, 'X' },
5605 { "own-window", 0, NULL, 'o' },
5608 { "double-buffer", 0, NULL, 'b' },
5610 { "window-id", 1, NULL, 'w' },
5612 { "text", 1, NULL, 't' },
5613 { "interval", 1, NULL, 'u' },
5614 { "pause", 1, NULL, 'p' },
5618 void set_current_config(void);
5619 void set_current_config(void)
5621 /* check if specified config file is valid */
5622 if (current_config) {
5624 if (stat(current_config, &sb) ||
5625 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5626 NORM_ERR("invalid configuration file '%s'\n", current_config);
5627 free(current_config);
5632 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5634 if (!current_config) {
5635 /* load default config file */
5636 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5639 /* Try to use personal config file first */
5640 to_real_path(buf, CONFIG_FILE);
5641 if (buf[0] && (fp = fopen(buf, "r"))) {
5642 current_config = strndup(buf, max_user_text);
5646 /* Try to use system config file if personal config not readable */
5647 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5648 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5652 /* No readable config found */
5653 if (!current_config) {
5654 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5655 #ifdef BUILD_BUILTIN_CONFIG
5656 current_config = strdup("==builtin==");
5657 NORM_ERR(NOCFGFILEFOUND
5658 ", using builtin default");
5660 CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5661 #endif /* ! CONF_OUTPUT */
5666 void initialisation(int argc, char **argv) {
5667 struct sigaction act, oact;
5669 set_default_configurations();
5670 set_current_config();
5671 load_config_file(current_config);
5672 currentconffile = conftree_add(currentconffile, current_config);
5674 /* init specials array */
5675 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5676 NORM_ERR("failed to create specials array");
5680 if (current_mail_spool == NULL) {
5683 variable_substitute(MAIL_FILE, buf, 256);
5685 if (buf[0] != '\0') {
5686 current_mail_spool = strndup(buf, text_buffer_size);
5691 /* handle other command line arguments */
5693 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5694 || defined(__NetBSD__)
5695 optind = optreset = 1;
5700 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5701 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5702 "kvm_open")) == NULL) {
5703 CRIT_ERR(NULL, NULL, "cannot read kvm");
5708 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5717 fork_to_background = 1;
5720 global_debug_level++;
5724 set_first_font(optarg);
5727 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
5746 global_text = strndup(optarg, max_user_text);
5747 convert_escapes(global_text);
5751 update_interval = strtod(optarg, 0);
5752 update_interval_old = update_interval;
5753 if (info.music_player_interval == 0) {
5754 // default to update_interval
5755 info.music_player_interval = update_interval;
5760 total_run_times = strtod(optarg, 0);
5764 gap_x = atoi(optarg);
5768 gap_y = atoi(optarg);
5773 startup_pause = atoi(optarg);
5774 sleep(startup_pause);
5785 if (output_methods & TO_X) {
5786 load_config_file_x11(current_config);
5790 /* generate text and get initial size */
5791 extract_variable_text(global_text);
5798 if (fork_to_background && first_pass) {
5803 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5810 fprintf(stderr, "\n");
5815 /* parent process */
5816 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5823 start_update_threading();
5825 text_buffer = malloc(max_user_text);
5826 memset(text_buffer, 0, max_user_text);
5827 tmpstring1 = malloc(text_buffer_size);
5828 memset(tmpstring1, 0, text_buffer_size);
5829 tmpstring2 = malloc(text_buffer_size);
5830 memset(tmpstring2, 0, text_buffer_size);
5835 X11_create_window();
5838 llua_setup_info(&info, update_interval);
5839 #endif /* HAVE_LUA */
5844 /* Set signal handlers */
5845 act.sa_handler = signal_handler;
5846 sigemptyset(&act.sa_mask);
5849 act.sa_flags |= SA_RESTART;
5852 if ( sigaction(SIGINT, &act, &oact) < 0
5853 || sigaction(SIGALRM, &act, &oact) < 0
5854 || sigaction(SIGUSR1, &act, &oact) < 0
5855 || sigaction(SIGHUP, &act, &oact) < 0
5856 || sigaction(SIGTERM, &act, &oact) < 0) {
5857 NORM_ERR("error setting signal handler: %s", strerror(errno));
5861 llua_startup_hook();
5862 #endif /* HAVE_LUA */
5865 int main(int argc, char **argv)
5874 g_signal_pending = 0;
5875 max_user_text = MAX_USER_TEXT_DEFAULT;
5877 memset(&info, 0, sizeof(info));
5881 #ifdef TCP_PORT_MONITOR
5882 /* set default connection limit */
5883 tcp_portmon_set_max_connections(0);
5886 /* handle command line parameters that don't change configs */
5888 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5889 || ((s = getenv("LANG")) && *s)) {
5890 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5892 NORM_ERR("malloc failed");
5894 for (x = 0; x < strlen(s); x++) {
5895 temp[x] = tolower(s[x]);
5898 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5904 if (!setlocale(LC_CTYPE, "")) {
5905 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5909 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5920 if (current_config) {
5921 free(current_config);
5923 current_config = strndup(optarg, max_user_text);
5926 if (!freopen("/dev/null", "w", stderr)) {
5927 NORM_ERR("unable to redirect stderr to /dev/null");
5931 print_help(argv[0]);
5933 #ifdef CONFIG_OUTPUT
5940 window.window = strtol(optarg, 0, 0);
5945 disp = strdup(optarg);
5954 set_current_config();
5957 /* Load xoap keys, if existing */
5961 #ifdef HAVE_SYS_INOTIFY_H
5962 inotify_fd = inotify_init();
5963 if(inotify_fd != -1) {
5966 fl = fcntl(inotify_fd, F_GETFL);
5967 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
5969 #endif /* HAVE_SYS_INOTIFY_H */
5971 initialisation(argc, argv);
5973 first_pass = 0; /* don't ever call fork() again */
5977 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5985 void alarm_handler(void) {
5987 kill(childpid, SIGTERM);
5991 static void signal_handler(int sig)
5993 /* signal handler is light as a feather, as it should be.
5994 * we will poll g_signal_pending with each loop of conky
5995 * and do any signal processing there, NOT here (except
5996 * SIGALRM because this is caused when conky is hanging) */
5997 if(sig == SIGALRM) {
6000 g_signal_pending = sig;