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;
171 static int extra_newline;
172 enum x_initialiser_state x_initialised = NO;
173 static volatile int g_signal_pending;
174 /* Update interval */
175 double update_interval;
176 double update_interval_old;
177 double update_interval_bat;
178 void *global_cpu = NULL;
179 unsigned int max_text_width = 0;
184 /* prototypes for internally used functions */
185 static void signal_handler(int);
186 static void print_version(void) __attribute__((noreturn));
187 static void reload_config(void);
189 static void print_version(void)
191 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
193 printf("\nCompiled in features:\n\n"
194 "System config file: "SYSTEM_CONFIG_FILE"\n"
195 "Package library path: "PACKAGE_LIBDIR"\n\n"
199 " * Xdamage extension\n"
200 # endif /* HAVE_XDAMAGE */
202 " * XDBE (double buffer extension)\n"
203 # endif /* HAVE_XDBE */
209 # endif /* USE_ARGB */
211 "\n Music detection:\n"
214 #endif /* AUDACIOUS */
230 #endif /* HAVE_OPENMP */
237 #ifdef TCP_PORT_MONITOR
239 #endif /* TCP_PORT_MONITOR */
242 #endif /* HAVE_CURL */
247 " * Weather (METAR)\n"
249 " * Weather (XOAP)\n"
254 #endif /* HAVE_IWLIB */
256 " * support for IBM/Lenovo notebooks\n"
266 #endif /* CONFIG_OUTPUT */
282 #ifdef HAVE_LUA_CAIRO
284 #endif /* HAVE_LUA_CAIRO */
285 #ifdef HAVE_LUA_IMLIB2
288 #endif /* HAVE_LUA */
294 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
299 static void X11_create_window(void);
300 static void X11_initialisation(void);
302 struct _x11_stuff_s {
306 XserverRegion region2, part;
307 int event_base, error_base;
313 static int text_start_x, text_start_y; /* text start position in window */
314 static int text_width, text_height;
331 /* display to connect to */
332 static char *disp = NULL;
336 /* struct that has all info to be shared between
337 * instances of the same text object */
338 struct information info;
340 /* path to config file */
341 char *current_config;
343 /* set to 1 if you want all text to be in uppercase */
344 static unsigned int stuff_in_uppercase;
346 /* Run how many times? */
347 static unsigned long total_run_times;
350 static int fork_to_background;
352 /* set to 0 after the first time conky is run, so we don't fork again after the
354 static int first_pass = 1;
356 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
358 /* filenames for output */
359 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
360 char *append_file = NULL; FILE *append_fpointer = NULL;
364 static int show_graph_scale;
365 static int show_graph_range;
367 /* Position on the screen */
368 static int text_alignment;
369 static int gap_x, gap_y;
372 static int draw_borders;
373 static int draw_graph_borders;
374 static int stippled_borders;
376 int get_stippled_borders(void)
378 return stippled_borders;
381 static int draw_shades, draw_outline;
383 long default_fg_color, default_bg_color, default_out_color;
385 /* create own window or draw stuff to root? */
386 static int set_transparent = 0;
389 static int own_window = 0;
390 static int background_colour = 0;
392 /* fixed size/pos is set if wm/user changes them */
393 static int fixed_size = 0, fixed_pos = 0;
396 static int minimum_width, minimum_height;
397 static int maximum_width;
402 static int sensor_device;
405 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
408 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
409 unsigned int max_user_text;
411 /* maximum size of individual text buffers, ie $exec buffer size */
412 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
417 /* no buffers in used memory? */
420 /* pad percentages to decimals? */
421 static int pad_percents = 0;
423 static char *global_text = 0;
425 char *get_global_text(void)
430 long global_text_lines;
432 static int total_updates;
433 static int updatereset;
435 void set_updatereset(int i)
440 int get_updatereset(void)
445 int check_contains(char *f, char *s)
448 FILE *where = open_file(f, 0);
453 while (fgets(buf1, 256, where)) {
454 if (strstr(buf1, s)) {
461 NORM_ERR("Could not open the file '%s'", f);
466 #define SECRIT_MULTILINE_CHAR '\x02'
468 int calc_text_width(const char *s)
470 size_t slen = strlen(s);
473 if ((output_methods & TO_X) == 0) {
483 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
484 (const FcChar8 *) s, slen, &gi);
486 XftTextExtents8(display, fonts[selected_font].xftfont,
487 (const FcChar8 *) s, slen, &gi);
493 return XTextWidth(fonts[selected_font].font, s, slen);
498 /* formatted text to render on screen, generated in generate_text(),
499 * drawn in draw_stuff() */
501 static char *text_buffer;
503 /* quite boring functions */
505 static inline void for_each_line(char *b, int f(char *, int))
508 int special_index = 0; /* specials index */
511 for (ps = b, pe = b; *pe; pe++) {
514 special_index = f(ps, special_index);
521 f(ps, special_index);
525 static void convert_escapes(char *buf)
527 char *p = buf, *s = buf;
534 } else if (*s == '\\') {
545 /* Prints anything normally printed with snprintf according to the current value
546 * of use_spacer. Actually slightly more flexible than snprintf, as you can
547 * safely specify the destination buffer as one of your inputs. */
548 int spaced_print(char *buf, int size, const char *format, int width, ...)
557 tempbuf = malloc(size * sizeof(char));
559 // Passes the varargs along to vsnprintf
560 va_start(argp, width);
561 vsnprintf(tempbuf, size, format, argp);
564 switch (use_spacer) {
566 len = snprintf(buf, size, "%s", tempbuf);
569 len = snprintf(buf, size, "%*s", width, tempbuf);
572 len = snprintf(buf, size, "%-*s", width, tempbuf);
579 /* print percentage values
581 * - i.e., unsigned values between 0 and 100
582 * - respect the value of pad_percents */
583 int percent_print(char *buf, int size, unsigned value)
585 return spaced_print(buf, size, "%u", pad_percents, value);
588 /* converts from bytes to human readable format (K, M, G, T)
590 * The algorithm always divides by 1024, as unit-conversion of byte
591 * counts suggests. But for output length determination we need to
592 * compare with 1000 here, as we print in decimal form. */
593 void human_readable(long long num, char *buf, int size)
595 const char **suffix = suffixes;
601 /* Possibly just output as usual, for example for stdout usage */
602 if (!format_human_readable) {
603 spaced_print(buf, size, "%d", 6, round_to_int(num));
614 if (llabs(num) < 1000LL) {
615 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
619 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
627 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
629 * The goal is to always have a significance of 3, by
630 * adjusting the decimal part of the number. Sample output:
634 * so the point of alignment resides between number and unit. The
635 * upside of this is that there is minimal padding necessary, though
636 * there should be a way to make alignment take place at the decimal
637 * dot (then with fixed width decimal part).
639 * Note the repdigits below: when given a precision value, printf()
640 * rounds the float to it, not just cuts off the remaining digits. So
641 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
642 * printed with a precision of 0. Yay. */
644 precision = 0; /* print 100-999 without decimal part */
646 precision = 1; /* print 10-99 with one decimal place */
648 precision = 2; /* print 0-9 with two decimal places */
650 spaced_print(buf, size, format, width, precision, fnum, *suffix);
653 /* global object list root element */
654 static struct text_object global_root_object;
656 static long current_text_color;
658 void set_current_text_color(long colour)
660 current_text_color = colour;
663 long get_current_text_color(void)
665 return current_text_color;
668 //adds newstring to to the tree unless you can already see it when travelling back.
669 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
670 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
671 struct conftree* node;
672 struct conftree* node2;
674 for(node = previous; node != NULL; node = node->back) {
675 if(strcmp(node->string, newstring) == 0) {
679 node = malloc(sizeof(struct conftree));
680 if (previous != NULL) {
681 if(previous->vert_next == NULL) {
682 previous->vert_next = node;
684 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
685 node2->horz_next = node;
688 node->string = strdup(newstring);
689 node->horz_next = NULL;
690 node->vert_next = NULL;
691 node->back = previous;
695 void conftree_empty(struct conftree* tree) {
697 conftree_empty(tree->horz_next);
698 conftree_empty(tree->vert_next);
704 struct conftree *currentconffile;
706 static void extract_variable_text(const char *p)
708 free_text_objects(&global_root_object, 0);
722 extract_variable_text_internal(&global_root_object, p);
725 void parse_conky_vars(struct text_object *root, const char *txt,
726 char *p, int p_max_size, struct information *cur)
728 extract_variable_text_internal(root, txt);
729 generate_text_internal(p, p_max_size, *root, cur);
732 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
733 * multiline objects like $exec work with $align[rc] and friends
735 void substitute_newlines(char *p, long l)
739 while (p && *p && p < s + l) {
741 /* only substitute if it's not the last newline */
742 *p = SECRIT_MULTILINE_CHAR;
748 void generate_text_internal(char *p, int p_max_size,
749 struct text_object root, struct information *cur)
751 struct text_object *obj;
753 int need_to_load_fonts = 0;
756 /* for the OBJ_top* handler */
757 struct process **needed = 0;
760 char buff_in[p_max_size];
762 #endif /* HAVE_ICONV */
768 while (obj && p_max_size > 0) {
769 needed = 0; /* reset for top stuff */
771 /* IFBLOCK jumping algorithm
773 * This is easier as it looks like:
774 * - each IF checks it's condition
775 * - on FALSE: call DO_JUMP
776 * - on TRUE: don't care
777 * - each ELSE calls DO_JUMP unconditionally
778 * - each ENDIF is silently being ignored
781 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
782 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
783 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
784 * regularly, "obj" is being updated to point to obj->next, so object parsing
785 * continues right after the corresponding ELSE or ENDIF. This means that if we
786 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
787 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
788 * jumped, and there is nothing to do.
792 if (obj->ifblock_next) \
793 obj = obj->ifblock_next; \
796 #define OBJ(a) break; case OBJ_##a:
800 NORM_ERR("not implemented obj type %d", obj->type);
802 print_read_tcp(obj, p, p_max_size);
806 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
808 #endif /* !__OpenBSD__ */
812 ok = get_freq(p, p_max_size, "%.0f", 1,
820 ok = get_freq(p, p_max_size, "%'.2f", 1000,
823 /* OpenBSD has no such flag (SUSv2) */
824 ok = get_freq(p, p_max_size, "%.2f", 1000,
826 #endif /* __OpenBSD */
829 #if defined(__linux__)
831 print_voltage_mv(obj, p, p_max_size);
834 print_voltage_v(obj, p, p_max_size);
838 OBJ(wireless_essid) {
839 print_wireless_essid(obj, p, p_max_size);
842 print_wireless_mode(obj, p, p_max_size);
844 OBJ(wireless_bitrate) {
845 print_wireless_bitrate(obj, p, p_max_size);
848 print_wireless_ap(obj, p, p_max_size);
850 OBJ(wireless_link_qual) {
851 print_wireless_link_qual(obj, p, p_max_size);
853 OBJ(wireless_link_qual_max) {
854 print_wireless_link_qual_max(obj, p, p_max_size);
856 OBJ(wireless_link_qual_perc) {
857 print_wireless_link_qual_perc(obj, p, p_max_size);
859 OBJ(wireless_link_bar) {
860 print_wireless_link_bar(obj, p, p_max_size);
862 #endif /* HAVE_IWLIB */
864 #endif /* __linux__ */
868 get_acpi_fan(p, p_max_size);
871 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
874 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
877 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
879 OBJ(battery_percent) {
880 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
883 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
886 get_battery_short_status(p, p_max_size, obj->data.s);
888 #endif /* __OpenBSD__ */
891 human_readable(cur->buffers * 1024, p, 255);
894 human_readable(cur->cached * 1024, p, 255);
896 OBJ(cmdline_to_pid) {
897 print_cmdline_to_pid(obj, p, p_max_size);
900 if (cur->cpu_usage) {
901 if (obj->data.i > info.cpu_count) {
902 NORM_ERR("obj->data.i %i info.cpu_count %i",
903 obj->data.i, info.cpu_count);
904 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
906 percent_print(p, p_max_size,
907 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
911 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
913 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
917 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
920 print_loadgraph(obj, p, p_max_size);
924 new_fg(p, obj->data.l);
958 #if defined(__linux__)
960 snprintf(p, p_max_size, "%s",
961 get_disk_protect_queue(obj->data.s));
964 print_i8k_version(obj, p, p_max_size);
967 print_i8k_bios(obj, p, p_max_size);
970 print_i8k_serial(obj, p, p_max_size);
973 print_i8k_cpu_temp(obj, p, p_max_size);
975 OBJ(i8k_left_fan_status) {
976 print_i8k_left_fan_status(obj, p, p_max_size);
978 OBJ(i8k_right_fan_status) {
979 print_i8k_right_fan_status(obj, p, p_max_size);
981 OBJ(i8k_left_fan_rpm) {
982 print_i8k_left_fan_rpm(obj, p, p_max_size);
984 OBJ(i8k_right_fan_rpm) {
985 print_i8k_right_fan_rpm(obj, p, p_max_size);
988 print_i8k_ac_status(obj, p, p_max_size);
990 OBJ(i8k_buttons_status) {
991 print_i8k_buttons_status(obj, p, p_max_size);
995 get_ibm_acpi_fan(obj, p, p_max_size);
998 print_ibm_temps(obj, p, p_max_size);
1001 get_ibm_acpi_volume(obj, p, p_max_size);
1003 OBJ(ibm_brightness) {
1004 get_ibm_acpi_brightness(obj, p, p_max_size);
1007 /* information from sony_laptop kernel module
1008 * /sys/devices/platform/sony-laptop */
1009 OBJ(sony_fanspeed) {
1010 get_sony_fanspeed(p, p_max_size);
1013 if (!gateway_exists()) {
1018 print_gateway_iface(p, p_max_size);
1021 print_gateway_ip(p, p_max_size);
1024 snprintf(p, p_max_size, "%d", get_laptop_mode());
1027 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1029 #endif /* __linux__ */
1030 #if (defined(__FreeBSD__) || defined(__linux__))
1032 if (!interface_up(obj)) {
1038 OBJ(obsd_sensors_temp) {
1039 print_obsd_sensors_temp(obj, p, p_max_size);
1041 OBJ(obsd_sensors_fan) {
1042 print_obsd_sensors_fan(obj, p, p_max_size);
1044 OBJ(obsd_sensors_volt) {
1045 print_obsd_sensors_volt(obj, p, p_max_size);
1048 get_obsd_vendor(p, p_max_size);
1051 get_obsd_product(p, p_max_size);
1053 #endif /* __OpenBSD__ */
1056 new_font(p, obj->data.s);
1057 need_to_load_fonts = 1;
1061 print_diskio(obj, p, p_max_size);
1064 print_diskio_write(obj, p, p_max_size);
1067 print_diskio_read(obj, p, p_max_size);
1071 print_diskiograph(obj, p, p_max_size);
1073 OBJ(diskiograph_read) {
1074 print_diskiograph_read(obj, p, p_max_size);
1076 OBJ(diskiograph_write) {
1077 print_diskiograph_write(obj, p, p_max_size);
1081 print_downspeed(obj, p, p_max_size);
1084 print_downspeedf(obj, p, p_max_size);
1087 OBJ(downspeedgraph) {
1088 print_downspeedgraph(obj, p, p_max_size);
1092 /* Since we see you, you're if has not jumped.
1093 * Do Ninja jump here: without leaving traces.
1094 * This is to prevent us from stale jumped flags.
1096 obj = obj->ifblock_next;
1100 /* harmless object, just ignore */
1103 print_addr(obj, p, p_max_size);
1105 #if defined(__linux__)
1107 print_addrs(obj, p, p_max_size);
1109 #endif /* __linux__ */
1110 #if defined(IMLIB2) && defined(X11)
1112 /* doesn't actually draw anything, just queues it omp. the
1113 * image will get drawn after the X event loop */
1114 cimlib_add_image(obj->data.s);
1118 evaluate(obj->data.s, p, p_max_size);
1121 print_exec(obj, p, p_max_size);
1124 print_execp(obj, p, p_max_size);
1127 print_execgauge(obj, p, p_max_size);
1130 print_execbar(obj, p, p_max_size);
1134 print_execgraph(obj, p, p_max_size);
1138 print_execibar(obj, p, p_max_size);
1142 print_execigraph(obj, p, p_max_size);
1146 print_execigauge(obj, p, p_max_size);
1149 print_execi(obj, p, p_max_size);
1152 print_execpi(obj, p, p_max_size);
1155 print_texeci(obj, p, p_max_size);
1158 print_imap_unseen(obj, p, p_max_size);
1160 OBJ(imap_messages) {
1161 print_imap_messages(obj, p, p_max_size);
1164 print_pop3_unseen(obj, p, p_max_size);
1167 print_pop3_used(obj, p, p_max_size);
1170 print_fs_bar(obj, 0, p, p_max_size);
1173 print_fs_free(obj, p, p_max_size);
1176 print_fs_perc(obj, 1, p, p_max_size);
1179 print_fs_size(obj, p, p_max_size);
1182 print_fs_type(obj, p, p_max_size);
1185 print_fs_used(obj, p, p_max_size);
1188 print_fs_bar(obj, 1, p, p_max_size);
1191 print_fs_perc(obj, 0, p, p_max_size);
1194 print_loadavg(obj, p, p_max_size);
1197 new_goto(p, obj->data.i);
1204 new_hr(p, obj->data.i);
1208 print_nameserver(obj, p, p_max_size);
1212 print_eve(obj, p, p_max_size);
1217 curl_print(obj, p, p_max_size);
1222 rss_print_info(obj, p, p_max_size);
1227 print_weather(obj, p, p_max_size);
1231 OBJ(weather_forecast) {
1232 print_weather_forecast(obj, p, p_max_size);
1237 print_lua(obj, p, p_max_size);
1240 print_lua_parse(obj, p, p_max_size);
1243 print_lua_bar(obj, p, p_max_size);
1247 print_lua_graph(obj, p, p_max_size);
1251 print_lua_gauge(obj, p, p_max_size);
1253 #endif /* HAVE_LUA */
1259 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1260 snprintf(p, p_max_size, "N/A");
1262 temp_print(p, p_max_size, (double)val,
1263 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1268 new_offset(p, obj->data.i);
1271 new_voffset(p, obj->data.i);
1275 print_sysfs_sensor(obj, p, p_max_size);
1278 print_sysfs_sensor(obj, p, p_max_size);
1281 print_sysfs_sensor(obj, p, p_max_size);
1283 #endif /* __linux__ */
1285 new_alignr(p, obj->data.i);
1288 new_alignc(p, obj->data.i);
1291 char buf[max_user_text];
1292 struct information *tmp_info =
1293 malloc(sizeof(struct information));
1294 memcpy(tmp_info, cur, sizeof(struct information));
1295 generate_text_internal(buf, max_user_text,
1296 *obj->sub, tmp_info);
1298 if (strlen(buf) != 0) {
1304 char expression[max_user_text];
1306 struct information *tmp_info;
1308 tmp_info = malloc(sizeof(struct information));
1309 memcpy(tmp_info, cur, sizeof(struct information));
1310 generate_text_internal(expression, max_user_text,
1311 *obj->sub, tmp_info);
1312 DBGP("parsed arg into '%s'", expression);
1314 val = compare(expression);
1316 NORM_ERR("compare failed for expression '%s'",
1326 spc = strchr(obj->data.s, ' ');
1327 if (!spc && access(obj->data.s, F_OK)) {
1331 if (!check_contains(obj->data.s, spc + 1))
1338 && (!check_mount(obj->data.s))) {
1344 if (!get_process_by_name(obj->data.s)) {
1346 if ((obj->data.s) && system(obj->data.s)) {
1351 #if defined(__linux__)
1353 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1357 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1360 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1365 human_readable(cur->mem * 1024, p, 255);
1368 human_readable(cur->memeasyfree * 1024, p, 255);
1371 human_readable(cur->memfree * 1024, p, 255);
1374 human_readable(cur->memmax * 1024, p, 255);
1378 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1381 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1384 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1388 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1393 print_mixer(obj, p, p_max_size);
1396 print_mixerl(obj, p, p_max_size);
1399 print_mixerr(obj, p, p_max_size);
1402 print_mixer_bar(obj, p, p_max_size);
1405 print_mixerl_bar(obj, p, p_max_size);
1408 print_mixerr_bar(obj, p, p_max_size);
1410 OBJ(if_mixer_mute) {
1411 if (!check_mixer_muted(obj)) {
1416 #define NOT_IN_X "Not running in X"
1418 if(x_initialised != YES) {
1419 strncpy(p, NOT_IN_X, p_max_size);
1421 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1424 OBJ(monitor_number) {
1425 if(x_initialised != YES) {
1426 strncpy(p, NOT_IN_X, p_max_size);
1428 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1432 if(x_initialised != YES) {
1433 strncpy(p, NOT_IN_X, p_max_size);
1435 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1438 OBJ(desktop_number) {
1439 if(x_initialised != YES) {
1440 strncpy(p, NOT_IN_X, p_max_size);
1442 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1446 if(x_initialised != YES) {
1447 strncpy(p, NOT_IN_X, p_max_size);
1448 }else if(cur->x11.desktop.name != NULL) {
1449 strncpy(p, cur->x11.desktop.name, p_max_size);
1455 char buf[max_user_text];
1457 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1459 print_format_time(obj, p, p_max_size);
1463 print_mails(obj, p, p_max_size);
1466 print_new_mails(obj, p, p_max_size);
1469 print_seen_mails(obj, p, p_max_size);
1472 print_unseen_mails(obj, p, p_max_size);
1474 OBJ(flagged_mails) {
1475 print_flagged_mails(obj, p, p_max_size);
1477 OBJ(unflagged_mails) {
1478 print_unflagged_mails(obj, p, p_max_size);
1480 OBJ(forwarded_mails) {
1481 print_forwarded_mails(obj, p, p_max_size);
1483 OBJ(unforwarded_mails) {
1484 print_unforwarded_mails(obj, p, p_max_size);
1486 OBJ(replied_mails) {
1487 print_replied_mails(obj, p, p_max_size);
1489 OBJ(unreplied_mails) {
1490 print_unreplied_mails(obj, p, p_max_size);
1493 print_draft_mails(obj, p, p_max_size);
1495 OBJ(trashed_mails) {
1496 print_trashed_mails(obj, p, p_max_size);
1499 print_mboxscan(obj, p, p_max_size);
1502 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1504 OBJ(nodename_short) {
1506 pos = strstr(cur->uname_s.nodename, ".");
1508 snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1510 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1514 new_outline(p, obj->data.l);
1517 char buf[max_user_text];
1519 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1521 print_pid_chroot(obj, p, p_max_size);
1524 char buf[max_user_text];
1526 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1528 print_pid_cmdline(obj, p, p_max_size);
1531 char buf[max_user_text];
1533 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1535 print_pid_cwd(obj, p, p_max_size);
1538 char buf[max_user_text];
1540 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1542 print_pid_environ(obj, p, p_max_size);
1544 OBJ(pid_environ_list) {
1545 char buf[max_user_text];
1547 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1549 print_pid_environ_list(obj, p, p_max_size);
1552 char buf[max_user_text];
1554 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1556 print_pid_exe(obj, p, p_max_size);
1559 char buf[max_user_text];
1561 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1563 print_pid_nice(obj, p, p_max_size);
1565 OBJ(pid_openfiles) {
1566 char buf[max_user_text];
1568 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1570 print_pid_openfiles(obj, p, p_max_size);
1573 char buf[max_user_text];
1575 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1577 print_pid_parent(obj, p, p_max_size);
1580 char buf[max_user_text];
1582 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1584 print_pid_priority(obj, p, p_max_size);
1587 char buf[max_user_text];
1589 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1591 print_pid_state(obj, p, p_max_size);
1593 OBJ(pid_state_short) {
1594 char buf[max_user_text];
1596 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1598 print_pid_state_short(obj, p, p_max_size);
1601 char buf[max_user_text];
1603 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1605 print_pid_stderr(obj, p, p_max_size);
1608 char buf[max_user_text];
1610 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1612 print_pid_stdin(obj, p, p_max_size);
1615 char buf[max_user_text];
1617 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1619 print_pid_stdout(obj, p, p_max_size);
1622 char buf[max_user_text];
1624 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1626 print_pid_threads(obj, p, p_max_size);
1628 OBJ(pid_thread_list) {
1629 char buf[max_user_text];
1631 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1633 print_pid_thread_list(obj, p, p_max_size);
1635 OBJ(pid_time_kernelmode) {
1636 char buf[max_user_text];
1638 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1640 print_pid_time_kernelmode(obj, p, p_max_size);
1642 OBJ(pid_time_usermode) {
1643 char buf[max_user_text];
1645 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1647 print_pid_time_usermode(obj, p, p_max_size);
1650 char buf[max_user_text];
1652 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1654 print_pid_time(obj, p, p_max_size);
1657 char buf[max_user_text];
1659 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1661 print_pid_uid(obj, p, p_max_size);
1664 char buf[max_user_text];
1666 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1668 print_pid_euid(obj, p, p_max_size);
1671 char buf[max_user_text];
1673 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1675 print_pid_suid(obj, p, p_max_size);
1678 char buf[max_user_text];
1680 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1682 print_pid_fsuid(obj, p, p_max_size);
1685 char buf[max_user_text];
1687 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1689 print_pid_gid(obj, p, p_max_size);
1692 char buf[max_user_text];
1694 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1696 print_pid_egid(obj, p, p_max_size);
1699 char buf[max_user_text];
1701 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1703 print_pid_sgid(obj, p, p_max_size);
1706 char buf[max_user_text];
1708 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1710 print_pid_fsgid(obj, p, p_max_size);
1713 char buf[max_user_text];
1715 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1717 print_pid_read(obj, p, p_max_size);
1720 char buf[max_user_text];
1722 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1724 print_pid_vmpeak(obj, p, p_max_size);
1727 char buf[max_user_text];
1729 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1731 print_pid_vmsize(obj, p, p_max_size);
1734 char buf[max_user_text];
1736 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1738 print_pid_vmlck(obj, p, p_max_size);
1741 char buf[max_user_text];
1743 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1745 print_pid_vmhwm(obj, p, p_max_size);
1748 char buf[max_user_text];
1750 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1752 print_pid_vmrss(obj, p, p_max_size);
1755 char buf[max_user_text];
1757 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1759 print_pid_vmdata(obj, p, p_max_size);
1762 char buf[max_user_text];
1764 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1766 print_pid_vmstk(obj, p, p_max_size);
1769 char buf[max_user_text];
1771 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1773 print_pid_vmexe(obj, p, p_max_size);
1776 char buf[max_user_text];
1778 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1780 print_pid_vmlib(obj, p, p_max_size);
1783 char buf[max_user_text];
1785 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1787 print_pid_vmpte(obj, p, p_max_size);
1790 char buf[max_user_text];
1792 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1794 print_pid_write(obj, p, p_max_size);
1797 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1799 OBJ(running_processes) {
1800 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1802 OBJ(running_threads) {
1803 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1806 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1809 snprintf(p, p_max_size, "%s", obj->data.s);
1813 new_bg(p, obj->data.l);
1816 new_stippled_hr(obj, p);
1820 human_readable(cur->swap * 1024, p, 255);
1823 human_readable(cur->swapfree * 1024, p, 255);
1826 human_readable(cur->swapmax * 1024, p, 255);
1829 if (cur->swapmax == 0) {
1830 strncpy(p, "No swap", p_max_size);
1832 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1836 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1839 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1842 print_time(obj, p, p_max_size);
1845 print_utime(obj, p, p_max_size);
1848 print_tztime(obj, p, p_max_size);
1851 print_totaldown(obj, p, p_max_size);
1854 print_totalup(obj, p, p_max_size);
1857 char buf[max_user_text];
1859 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1861 print_gid_name(obj, p, p_max_size);
1864 char buf[max_user_text];
1866 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1868 print_uid_name(obj, p, p_max_size);
1871 snprintf(p, p_max_size, "%d", total_updates);
1874 if(total_updates % updatereset != obj->data.i - 1) {
1879 print_upspeed(obj, p, p_max_size);
1882 print_upspeedf(obj, p, p_max_size);
1886 print_upspeedgraph(obj, p, p_max_size);
1890 format_seconds_short(p, p_max_size, (int) cur->uptime);
1893 format_seconds(p, p_max_size, (int) cur->uptime);
1897 snprintf(p, p_max_size, "%s", cur->users.names);
1900 snprintf(p, p_max_size, "%s", cur->users.terms);
1903 snprintf(p, p_max_size, "%s", cur->users.times);
1906 update_user_time(obj->data.s);
1907 snprintf(p, p_max_size, "%s", cur->users.ctime);
1910 snprintf(p, p_max_size, "%d", cur->users.number);
1912 #endif /* __linux__ */
1913 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1914 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1918 msg = get_apm_adapter();
1919 snprintf(p, p_max_size, "%s", msg);
1922 OBJ(apm_battery_life) {
1925 msg = get_apm_battery_life();
1926 snprintf(p, p_max_size, "%s", msg);
1929 OBJ(apm_battery_time) {
1932 msg = get_apm_battery_time();
1933 snprintf(p, p_max_size, "%s", msg);
1936 #endif /* __FreeBSD__ __OpenBSD__ */
1940 print_mpd_title(obj, p, p_max_size);
1942 print_mpd_artist(obj, p, p_max_size);
1944 print_mpd_album(obj, p, p_max_size);
1946 print_mpd_random(obj, p, p_max_size);
1948 print_mpd_repeat(obj, p, p_max_size);
1950 print_mpd_track(obj, p, p_max_size);
1952 print_mpd_name(obj, p, p_max_size);
1954 print_mpd_file(obj, p, p_max_size);
1956 print_mpd_vol(obj, p, p_max_size);
1958 print_mpd_bitrate(obj, p, p_max_size);
1960 print_mpd_status(obj, p, p_max_size);
1962 print_mpd_elapsed(obj, p, p_max_size);
1965 print_mpd_length(obj, p, p_max_size);
1968 print_mpd_percent(obj, p, p_max_size);
1971 print_mpd_bar(obj, p, p_max_size);
1974 print_mpd_smart(obj, p, p_max_size);
1976 OBJ(if_mpd_playing) {
1977 if (!mpd_get_info()->is_playing) {
1984 #define MOC_PRINT(t, a) \
1985 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
1987 MOC_PRINT(state, "??");
1990 MOC_PRINT(file, "no file");
1993 MOC_PRINT(title, "no title");
1996 MOC_PRINT(artist, "no artist");
1999 MOC_PRINT(song, "no song");
2002 MOC_PRINT(album, "no album");
2004 OBJ(moc_totaltime) {
2005 MOC_PRINT(totaltime, "0:00");
2008 MOC_PRINT(timeleft, "0:00");
2011 MOC_PRINT(curtime, "0:00");
2014 MOC_PRINT(bitrate, "0Kbps");
2017 MOC_PRINT(rate, "0KHz");
2023 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2026 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2029 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2032 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2034 OBJ(xmms2_comment) {
2035 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2038 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2041 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2044 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2046 OBJ(xmms2_tracknr) {
2047 if (cur->xmms2.tracknr != -1) {
2048 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2051 OBJ(xmms2_bitrate) {
2052 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2055 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2058 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2060 OBJ(xmms2_elapsed) {
2061 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2062 (cur->xmms2.elapsed / 1000) % 60);
2064 OBJ(xmms2_duration) {
2065 snprintf(p, p_max_size, "%02d:%02d",
2066 cur->xmms2.duration / 60000,
2067 (cur->xmms2.duration / 1000) % 60);
2069 OBJ(xmms2_percent) {
2070 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2073 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2075 OBJ(xmms2_playlist) {
2076 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2078 OBJ(xmms2_timesplayed) {
2079 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2082 if (strlen(cur->xmms2.title) < 2
2083 && strlen(cur->xmms2.title) < 2) {
2084 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2086 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2090 OBJ(if_xmms2_connected) {
2091 if (cur->xmms2.conn_state != 1) {
2097 OBJ(audacious_status) {
2098 snprintf(p, p_max_size, "%s",
2099 cur->audacious.items[AUDACIOUS_STATUS]);
2101 OBJ(audacious_title) {
2102 snprintf(p, cur->audacious.max_title_len > 0
2103 ? cur->audacious.max_title_len : p_max_size, "%s",
2104 cur->audacious.items[AUDACIOUS_TITLE]);
2106 OBJ(audacious_length) {
2107 snprintf(p, p_max_size, "%s",
2108 cur->audacious.items[AUDACIOUS_LENGTH]);
2110 OBJ(audacious_length_seconds) {
2111 snprintf(p, p_max_size, "%s",
2112 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2114 OBJ(audacious_position) {
2115 snprintf(p, p_max_size, "%s",
2116 cur->audacious.items[AUDACIOUS_POSITION]);
2118 OBJ(audacious_position_seconds) {
2119 snprintf(p, p_max_size, "%s",
2120 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2122 OBJ(audacious_bitrate) {
2123 snprintf(p, p_max_size, "%s",
2124 cur->audacious.items[AUDACIOUS_BITRATE]);
2126 OBJ(audacious_frequency) {
2127 snprintf(p, p_max_size, "%s",
2128 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2130 OBJ(audacious_channels) {
2131 snprintf(p, p_max_size, "%s",
2132 cur->audacious.items[AUDACIOUS_CHANNELS]);
2134 OBJ(audacious_filename) {
2135 snprintf(p, p_max_size, "%s",
2136 cur->audacious.items[AUDACIOUS_FILENAME]);
2138 OBJ(audacious_playlist_length) {
2139 snprintf(p, p_max_size, "%s",
2140 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2142 OBJ(audacious_playlist_position) {
2143 snprintf(p, p_max_size, "%s",
2144 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2146 OBJ(audacious_main_volume) {
2147 snprintf(p, p_max_size, "%s",
2148 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2150 OBJ(audacious_bar) {
2154 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2155 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2156 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2158 #endif /* AUDACIOUS */
2162 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2165 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2168 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2171 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2174 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2177 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2180 /* we have four different types of top (top, top_mem,
2181 * top_time and top_io). To avoid having almost-same code four
2182 * times, we have this special handler. */
2190 print_top(obj, p, p_max_size);
2192 print_tailhead("tail", obj, p, p_max_size);
2195 print_tailhead("head", obj, p, p_max_size);
2198 print_lines(obj, p, p_max_size);
2202 print_words(obj, p, p_max_size);
2204 #ifdef TCP_PORT_MONITOR
2206 tcp_portmon_action(obj, p, p_max_size);
2208 #endif /* TCP_PORT_MONITOR */
2212 do_iconv_start(obj);
2217 #endif /* HAVE_ICONV */
2219 OBJ(entropy_avail) {
2220 print_entropy_avail(obj, p, p_max_size);
2223 print_entropy_perc(obj, p, p_max_size);
2225 OBJ(entropy_poolsize) {
2226 print_entropy_poolsize(obj, p, p_max_size);
2229 print_entropy_bar(obj, p, p_max_size);
2233 print_smapi(obj, p, p_max_size);
2235 OBJ(if_smapi_bat_installed) {
2237 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2238 if(!smapi_bat_installed(idx)) {
2242 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2244 OBJ(smapi_bat_perc) {
2245 print_smapi_bat_perc(obj, p, p_max_size);
2247 OBJ(smapi_bat_temp) {
2248 print_smapi_bat_temp(obj, p, p_max_size);
2250 OBJ(smapi_bat_power) {
2251 print_smapi_bat_power(obj, p, p_max_size);
2253 OBJ(smapi_bat_bar) {
2254 print_smapi_bat_bar(obj, p, p_max_size);
2259 char buf[max_user_text];
2261 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2262 snprintf(p, p_max_size, "%s", buf);
2268 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2269 char buf[max_user_text];
2272 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2273 snprintf(p, p_max_size, "%s", buf);
2274 if(total_updates % 2) {
2275 for(j=0; p[j] != 0; j++) {
2281 char buf[max_user_text];
2283 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2285 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2286 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2287 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2288 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2289 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2290 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2291 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2293 snprintf(p, p_max_size, "%s", buf);
2296 print_scroll(obj, p, p_max_size, cur);
2299 print_combine(obj, p, cur);
2303 print_nvidia_value(obj, display, p, p_max_size);
2308 /* This is just a meta-object to set host:port */
2311 snprintf(p, p_max_size, "%s",
2312 cur->apcupsd.items[APCUPSD_NAME]);
2314 OBJ(apcupsd_model) {
2315 snprintf(p, p_max_size, "%s",
2316 cur->apcupsd.items[APCUPSD_MODEL]);
2318 OBJ(apcupsd_upsmode) {
2319 snprintf(p, p_max_size, "%s",
2320 cur->apcupsd.items[APCUPSD_UPSMODE]);
2322 OBJ(apcupsd_cable) {
2323 snprintf(p, p_max_size, "%s",
2324 cur->apcupsd.items[APCUPSD_CABLE]);
2326 OBJ(apcupsd_status) {
2327 snprintf(p, p_max_size, "%s",
2328 cur->apcupsd.items[APCUPSD_STATUS]);
2330 OBJ(apcupsd_linev) {
2331 snprintf(p, p_max_size, "%s",
2332 cur->apcupsd.items[APCUPSD_LINEV]);
2335 snprintf(p, p_max_size, "%s",
2336 cur->apcupsd.items[APCUPSD_LOAD]);
2338 OBJ(apcupsd_loadbar) {
2340 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2341 new_bar(obj, p, p_max_size, (int) progress);
2344 OBJ(apcupsd_loadgraph) {
2346 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2347 new_graph(obj, p, p_max_size, (int)progress);
2350 OBJ(apcupsd_loadgauge) {
2352 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2353 new_gauge(obj, p, p_max_size, (int)progress);
2355 OBJ(apcupsd_charge) {
2356 snprintf(p, p_max_size, "%s",
2357 cur->apcupsd.items[APCUPSD_CHARGE]);
2359 OBJ(apcupsd_timeleft) {
2360 snprintf(p, p_max_size, "%s",
2361 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2364 snprintf(p, p_max_size, "%s",
2365 cur->apcupsd.items[APCUPSD_TEMP]);
2367 OBJ(apcupsd_lastxfer) {
2368 snprintf(p, p_max_size, "%s",
2369 cur->apcupsd.items[APCUPSD_LASTXFER]);
2371 #endif /* APCUPSD */
2378 size_t a = strlen(p);
2381 iconv_convert(&a, buff_in, p, p_max_size);
2382 #endif /* HAVE_ICONV */
2383 if (obj->type == OBJ_execp || obj->type == OBJ_execpi || obj->type
2386 || obj->type == OBJ_lua || obj->type == OBJ_lua_parse
2387 #endif /* HAVE_LUA */
2389 substitute_newlines(p, a - 2);
2398 /* load any new fonts we may have had */
2399 if (need_to_load_fonts) {
2405 void evaluate(const char *text, char *p, int p_max_size)
2407 struct information *tmp_info;
2408 struct text_object subroot;
2410 tmp_info = malloc(sizeof(struct information));
2411 memcpy(tmp_info, &info, sizeof(struct information));
2412 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2413 DBGP2("evaluated '%s' to '%s'", text, p);
2415 free_text_objects(&subroot, 1);
2419 double current_update_time, next_update_time, last_update_time;
2421 static void generate_text(void)
2423 struct information *cur = &info;
2425 unsigned int i, j, k;
2431 current_update_time = get_time();
2435 /* add things to the buffer */
2441 generate_text_internal(p, max_user_text, global_root_object, cur);
2442 if(max_text_width > 0) {
2443 for(i = 0, j = 0; p[i] != 0; i++) {
2444 if(p[i] == '\n') j = 0;
2445 else if(j == max_text_width) {
2446 k = i + strlen(p + i) + 1;
2447 if(k < text_buffer_size) {
2454 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2459 if (stuff_in_uppercase) {
2462 tmp_p = text_buffer;
2464 *tmp_p = toupper(*tmp_p);
2469 next_update_time += update_interval;
2470 if (next_update_time < get_time()) {
2471 next_update_time = get_time() + update_interval;
2472 } else if (next_update_time > get_time() + update_interval) {
2473 next_update_time = get_time() + update_interval;
2475 last_update_time = current_update_time;
2479 void set_update_interval(double interval)
2481 update_interval = interval;
2482 update_interval_old = interval;
2485 int get_string_width(const char *s)
2487 return *s ? calc_text_width(s) : 0;
2491 static int get_string_width_special(char *s, int special_index)
2501 if ((output_methods & TO_X) == 0)
2504 p = strndup(s, text_buffer_size);
2508 if (*p == SPECIAL_CHAR) {
2509 /* shift everything over by 1 so that the special char
2510 * doesn't mess up the size calculation */
2511 for (i = 0; i < (long)strlen(p); i++) {
2512 *(p + i) = *(p + i + 1);
2514 if (specials[special_index + idx].type == GRAPH
2515 || specials[special_index + idx].type == GAUGE
2516 || specials[special_index + idx].type == BAR) {
2517 width += specials[special_index + idx].width;
2520 } else if (*p == SECRIT_MULTILINE_CHAR) {
2527 if (strlen(final) > 1) {
2528 width += calc_text_width(final);
2534 static int text_size_updater(char *s, int special_index);
2536 int last_font_height;
2537 static void update_text_area(void)
2541 if ((output_methods & TO_X) == 0)
2543 /* update text size if it isn't fixed */
2548 text_width = minimum_width;
2550 last_font_height = font_height();
2551 for_each_line(text_buffer, text_size_updater);
2553 if (text_height < minimum_height) {
2554 text_height = minimum_height;
2556 if (text_width > maximum_width && maximum_width > 0) {
2557 text_width = maximum_width;
2561 /* get text position on workarea */
2562 switch (text_alignment) {
2563 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2567 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2568 y = workarea[3] - text_height - gap_y;
2571 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2572 y = workarea[3] / 2 - text_height / 2 - gap_y;
2575 switch (text_alignment) {
2576 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2580 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2581 x = workarea[2] - text_width - gap_x;
2584 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2585 x = workarea[2] / 2 - text_width / 2 - gap_x;
2589 if (text_alignment == NONE) { // Let the WM manage the window
2596 #endif /* OWN_WINDOW */
2599 if (own_window && !fixed_pos) {
2602 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2603 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2604 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2605 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2609 /* If window size doesn't match to workarea's size,
2610 * then window probably includes panels (gnome).
2611 * Blah, doesn't work on KDE. */
2612 if (workarea[2] != window.width || workarea[3] != window.height) {
2621 /* update lua window globals */
2622 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2623 #endif /* HAVE_LUA */
2628 static int cur_x, cur_y; /* current x and y for drawing */
2630 //draw_mode also without X11 because we only need to print to stdout with FG
2631 static int draw_mode; /* FG, BG or OUTLINE */
2633 static long current_color;
2635 static int text_size_updater(char *s, int special_index)
2639 int contain_SECRIT_MULTILINE_CHAR = 0;
2642 if ((output_methods & TO_X) == 0)
2644 /* get string widths and skip specials */
2647 if (*p == SPECIAL_CHAR) {
2649 w += get_string_width(s);
2652 if (specials[special_index].type == BAR
2653 || specials[special_index].type == GAUGE
2654 || specials[special_index].type == GRAPH) {
2655 w += specials[special_index].width;
2656 if (specials[special_index].height > last_font_height) {
2657 last_font_height = specials[special_index].height;
2658 last_font_height += font_height();
2660 } else if (specials[special_index].type == OFFSET) {
2661 if (specials[special_index].arg > 0) {
2662 w += specials[special_index].arg;
2664 } else if (specials[special_index].type == VOFFSET) {
2665 last_font_height += specials[special_index].arg;
2666 } else if (specials[special_index].type == GOTO) {
2667 if (specials[special_index].arg > cur_x) {
2668 w = (int) specials[special_index].arg;
2670 } else if (specials[special_index].type == TAB) {
2671 int start = specials[special_index].arg;
2672 int step = specials[special_index].width;
2674 if (!step || step < 0) {
2677 w += step - (cur_x - text_start_x - start) % step;
2678 } else if (specials[special_index].type == FONT) {
2679 selected_font = specials[special_index].font_added;
2680 if (font_height() > last_font_height) {
2681 last_font_height = font_height();
2687 } else if (*p == SECRIT_MULTILINE_CHAR) {
2688 contain_SECRIT_MULTILINE_CHAR = 1;
2690 lw = get_string_width(s);
2691 *p = SECRIT_MULTILINE_CHAR;
2693 w = lw > w ? lw : w;
2694 text_height += last_font_height;
2698 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2699 if (contain_SECRIT_MULTILINE_CHAR) {
2700 lw = get_string_width(s);
2701 w = lw > w ? lw : w;
2703 w += get_string_width(s);
2705 if (w > text_width) {
2708 if (text_width > maximum_width && maximum_width) {
2709 text_width = maximum_width;
2712 text_height += last_font_height;
2713 last_font_height = font_height();
2714 return special_index;
2718 static inline void set_foreground_color(long c)
2721 if (output_methods & TO_X) {
2723 if (have_argb_visual) {
2724 current_color = c | (own_window_argb_value << 24);
2726 #endif /* USE_ARGB */
2730 #endif /* USE_ARGB */
2731 XSetForeground(display, window.gc, current_color);
2735 if (output_methods & TO_NCURSES) {
2736 attron(COLOR_PAIR(c));
2738 #endif /* NCURSES */
2743 static void draw_string(const char *s)
2745 int i, i2, pos, width_of_s;
2748 char *s_with_newlines;
2754 width_of_s = get_string_width(s);
2755 s_with_newlines = strdup(s);
2756 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2757 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2758 s_with_newlines[i] = '\n';
2761 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2762 printf("%s\n", s_with_newlines);
2763 if (extra_newline) fputc('\n', stdout);
2764 fflush(stdout); /* output immediately, don't buffer */
2766 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2767 fprintf(stderr, "%s\n", s_with_newlines);
2768 fflush(stderr); /* output immediately, don't buffer */
2770 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2771 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2773 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2774 fprintf(append_fpointer, "%s\n", s_with_newlines);
2777 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2778 printw("%s", s_with_newlines);
2781 free(s_with_newlines);
2782 memset(tmpstring1, 0, text_buffer_size);
2783 memset(tmpstring2, 0, text_buffer_size);
2784 strncpy(tmpstring1, s, text_buffer_size - 1);
2789 if (output_methods & TO_X) {
2790 max = ((text_width - width_of_s) / get_string_width(" "));
2793 /* This code looks for tabs in the text and coverts them to spaces.
2794 * The trick is getting the correct number of spaces, and not going
2795 * over the window's size without forcing the window larger. */
2796 for (i = 0; i < (int) text_buffer_size; i++) {
2797 if (tmpstring1[i] == '\t') {
2799 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2800 /* guard against overrun */
2801 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2806 /* guard against overrun */
2807 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2812 if (output_methods & TO_X) {
2813 if (text_width == maximum_width) {
2814 /* this means the text is probably pushing the limit,
2815 * so we'll chop it */
2816 while (cur_x + get_string_width(tmpstring2) - text_start_x
2817 > maximum_width && strlen(tmpstring2) > 0) {
2818 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2825 if (output_methods & TO_X) {
2831 c.pixel = current_color;
2832 // query color on custom colormap
2833 XQueryColor(display, window.colourmap, &c);
2836 c2.color.red = c.red;
2837 c2.color.green = c.green;
2838 c2.color.blue = c.blue;
2839 c2.color.alpha = fonts[selected_font].font_alpha;
2841 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2842 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2844 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2845 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2850 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2853 cur_x += width_of_s;
2856 memcpy(tmpstring1, s, text_buffer_size);
2859 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2867 int last_special_needed = -1;
2868 int orig_special_index = special_index;
2871 if (output_methods & TO_X) {
2872 font_h = font_height();
2873 cur_y += font_ascent();
2875 cur_x = text_start_x;
2879 if (*p == SECRIT_MULTILINE_CHAR) {
2880 /* special newline marker for multiline objects */
2885 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2890 /* draw string before special, unless we're dealing multiline
2892 if (last_special_applied > -1) {
2893 special_index = last_special_applied;
2901 switch (specials[special_index].type) {
2903 case HORIZONTAL_LINE:
2905 int h = specials[special_index].height;
2906 int mid = font_ascent() / 2;
2908 w = text_start_x + text_width - cur_x;
2910 XSetLineAttributes(display, window.gc, h, LineSolid,
2911 CapButt, JoinMiter);
2912 XDrawLine(display, window.drawable, window.gc, cur_x,
2913 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2919 int h = specials[special_index].height;
2920 int tmp_s = specials[special_index].arg;
2921 int mid = font_ascent() / 2;
2922 char ss[2] = { tmp_s, tmp_s };
2924 w = text_start_x + text_width - cur_x - 1;
2925 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2926 CapButt, JoinMiter);
2927 XSetDashes(display, window.gc, 0, ss, 2);
2928 XDrawLine(display, window.drawable, window.gc, cur_x,
2929 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2935 int h, bar_usage, by;
2936 if (cur_x - text_start_x > maximum_width
2937 && maximum_width > 0) {
2940 h = specials[special_index].height;
2941 bar_usage = specials[special_index].arg;
2942 by = cur_y - (font_ascent() / 2) - 1;
2947 w = specials[special_index].width;
2949 w = text_start_x + text_width - cur_x - 1;
2955 XSetLineAttributes(display, window.gc, 1, LineSolid,
2956 CapButt, JoinMiter);
2958 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2960 XFillRectangle(display, window.drawable, window.gc, cur_x,
2961 by, w * bar_usage / 255, h);
2969 case GAUGE: /* new GAUGE */
2972 unsigned long last_colour = current_color;
2974 float angle, px, py;
2978 if (cur_x - text_start_x > maximum_width
2979 && maximum_width > 0) {
2983 h = specials[special_index].height;
2984 by = cur_y - (font_ascent() / 2) - 1;
2989 w = specials[special_index].width;
2991 w = text_start_x + text_width - cur_x - 1;
2997 XSetLineAttributes(display, window.gc, 1, LineSolid,
2998 CapButt, JoinMiter);
3000 XDrawArc(display, window.drawable, window.gc,
3001 cur_x, by, w, h * 2, 0, 180*64);
3004 usage = specials[special_index].arg;
3005 angle = (M_PI)*(float)(usage)/255.;
3006 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3007 py = (float)(by+(h))-(float)(h)*sin(angle);
3009 XDrawLine(display, window.drawable, window.gc,
3010 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3018 set_foreground_color(last_colour);
3026 int h, by, i = 0, j = 0;
3028 unsigned long last_colour = current_color;
3029 unsigned long *tmpcolour = 0;
3030 if (cur_x - text_start_x > maximum_width
3031 && maximum_width > 0) {
3034 h = specials[special_index].height;
3035 by = cur_y - (font_ascent() / 2) - 1;
3040 w = specials[special_index].width;
3042 w = text_start_x + text_width - cur_x - 1;
3047 if (draw_graph_borders) {
3048 XSetLineAttributes(display, window.gc, 1, LineSolid,
3049 CapButt, JoinMiter);
3050 XDrawRectangle(display, window.drawable, window.gc,
3053 XSetLineAttributes(display, window.gc, 1, LineSolid,
3054 CapButt, JoinMiter);
3056 if (specials[special_index].last_colour != 0
3057 || specials[special_index].first_colour != 0) {
3058 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3061 for (i = w - 2; i > -1; i--) {
3062 if (specials[special_index].last_colour != 0
3063 || specials[special_index].first_colour != 0) {
3064 if (specials[special_index].tempgrad) {
3067 (int)((float)(w - 2) - specials[special_index].graph[j] *
3068 (w - 2) / (float)specials[special_index].graph_scale)
3072 (int)((float)(w - 2) - specials[special_index].graph[j] *
3073 (w - 2) / (float)specials[special_index].graph_scale)
3076 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3078 (int)((float)(w - 2) - specials[special_index].graph[j] *
3079 (w - 2) / (float)specials[special_index].graph_scale)
3083 #endif /* DEBUG_lol */
3084 XSetForeground(display, window.gc, tmpcolour[
3085 (int)((float)(w - 2) - specials[special_index].graph[j] *
3086 (w - 2) / (float)specials[special_index].graph_scale)
3089 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3092 /* this is mugfugly, but it works */
3093 XDrawLine(display, window.drawable, window.gc,
3094 cur_x + i + 1, by + h, cur_x + i + 1,
3095 round_to_int((double)by + h - specials[special_index].graph[j] *
3096 (h - 1) / specials[special_index].graph_scale));
3097 if ((w - i) / ((float) (w - 2) /
3098 (specials[special_index].graph_width)) > j
3099 && j < MAX_GRAPH_DEPTH - 3) {
3103 if (tmpcolour) free(tmpcolour);
3108 /* if (draw_mode == BG) {
3109 set_foreground_color(default_bg_color);
3110 } else if (draw_mode == OUTLINE) {
3111 set_foreground_color(default_out_color);
3113 set_foreground_color(default_fg_color);
3115 if (show_graph_range) {
3118 unsigned short int seconds = update_interval * w;
3124 unsigned short int timeunits;
3126 timeunits = seconds / 86400; seconds %= 86400;
3127 if (timeunits > 0) {
3128 if (asprintf(&tmp_day_str, "%dd", timeunits) < 0) {
3132 tmp_day_str = strdup("");
3134 timeunits = seconds / 3600; seconds %= 3600;
3135 if (timeunits > 0) {
3136 if (asprintf(&tmp_hour_str, "%dh", timeunits) < 0) {
3140 tmp_hour_str = strdup("");
3142 timeunits = seconds / 60; seconds %= 60;
3143 if (timeunits > 0) {
3144 if (asprintf(&tmp_min_str, "%dm", timeunits) < 0) {
3148 tmp_min_str = strdup("");
3151 if (asprintf(&tmp_sec_str, "%ds", seconds) < 0) {
3155 tmp_sec_str = strdup("");
3157 if (asprintf(&tmp_str, "%s%s%s%s", tmp_day_str,
3158 tmp_hour_str, tmp_min_str, tmp_sec_str) < 0) {
3161 #define FREE(a) if ((a)) free((a));
3162 FREE(tmp_day_str); FREE(tmp_hour_str); FREE(tmp_min_str); FREE(tmp_sec_str);
3164 tmp_str = strdup("Range not possible"); /* should never happen, but better safe then sorry */
3166 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3167 cur_y += font_h / 2;
3168 draw_string(tmp_str);
3175 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3179 cur_x += font_ascent() / 2;
3180 cur_y += font_h / 2;
3182 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3184 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3185 draw_string(tmp_str);
3191 set_foreground_color(last_colour);
3197 int old = font_ascent();
3199 cur_y -= font_ascent();
3200 selected_font = specials[special_index].font_added;
3202 if (cur_y + font_ascent() < cur_y + old) {
3205 cur_y += font_ascent();
3207 font_h = font_height();
3212 if (draw_mode == FG) {
3213 set_foreground_color(specials[special_index].arg);
3219 if (draw_mode == BG) {
3220 set_foreground_color(specials[special_index].arg);
3225 if (draw_mode == OUTLINE) {
3226 set_foreground_color(specials[special_index].arg);
3231 w += specials[special_index].arg;
3232 last_special_needed = special_index;
3236 cur_y += specials[special_index].arg;
3240 if (specials[special_index].arg >= 0) {
3241 cur_x = (int) specials[special_index].arg;
3243 last_special_needed = special_index;
3248 int start = specials[special_index].arg;
3249 int step = specials[special_index].width;
3251 if (!step || step < 0) {
3254 w = step - (cur_x - text_start_x - start) % step;
3255 last_special_needed = special_index;
3261 /* TODO: add back in "+ window.border_inner_margin" to the end of
3263 int pos_x = text_start_x + text_width -
3264 get_string_width_special(s, special_index);
3266 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3267 "get_string_width(p) %i gap_x %i "
3268 "specials[special_index].arg %i window.border_inner_margin %i "
3269 "window.border_width %i\n", pos_x, text_start_x, text_width,
3270 cur_x, get_string_width_special(s), gap_x,
3271 specials[special_index].arg, window.border_inner_margin,
3272 window.border_width); */
3273 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3274 cur_x = pos_x - specials[special_index].arg;
3276 last_special_needed = special_index;
3282 int pos_x = (text_width) / 2 - get_string_width_special(s,
3283 special_index) / 2 - (cur_x -
3285 /* int pos_x = text_start_x + text_width / 2 -
3286 get_string_width_special(s) / 2; */
3288 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3289 "get_string_width(p) %i gap_x %i "
3290 "specials[special_index].arg %i\n", pos_x, text_start_x,
3291 text_width, cur_x, get_string_width(s), gap_x,
3292 specials[special_index].arg); */
3293 if (pos_x > specials[special_index].arg) {
3294 w = pos_x - specials[special_index].arg;
3296 last_special_needed = special_index;
3306 if (special_index != last_special_applied) {
3309 special_index = orig_special_index;
3310 last_special_applied = -1;
3321 if (output_methods & TO_NCURSES) {
3324 #endif /* NCURSES */
3326 if (output_methods & TO_X)
3327 cur_y += font_descent();
3329 if (recurse && *recurse) {
3330 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3331 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3333 return special_index;
3336 static int draw_line(char *s, int special_index)
3339 if (output_methods & TO_X) {
3340 return draw_each_line_inner(s, special_index, -1);
3344 if (output_methods & TO_NCURSES) {
3345 return draw_each_line_inner(s, special_index, -1);
3347 #endif /* NCURSES */
3349 UNUSED(special_index);
3353 static void draw_text(void)
3357 llua_draw_pre_hook();
3358 #endif /* HAVE_LUA */
3359 if (output_methods & TO_X) {
3360 cur_y = text_start_y;
3363 if (draw_borders && window.border_width > 0) {
3364 if (stippled_borders) {
3365 char ss[2] = { stippled_borders, stippled_borders };
3366 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3367 CapButt, JoinMiter);
3368 XSetDashes(display, window.gc, 0, ss, 2);
3370 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3371 CapButt, JoinMiter);
3374 XDrawRectangle(display, window.drawable, window.gc,
3375 text_start_x - window.border_inner_margin - window.border_width,
3376 text_start_y - window.border_inner_margin - window.border_width,
3377 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3378 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3386 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3387 attron(COLOR_PAIR(COLOR_WHITE));
3388 #endif /* NCURSES */
3389 for_each_line(text_buffer, draw_line);
3390 #if defined(HAVE_LUA) && defined(X11)
3391 llua_draw_post_hook();
3392 #endif /* HAVE_LUA */
3395 static void draw_stuff(void)
3398 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3400 if (overwrite_file) {
3401 overwrite_fpointer = fopen(overwrite_file, "w");
3402 if(!overwrite_fpointer)
3403 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3406 append_fpointer = fopen(append_file, "a");
3407 if(!append_fpointer)
3408 NORM_ERR("Can't append '%s' anymore", append_file);
3411 if (output_methods & TO_X) {
3413 if (draw_shades && !draw_outline) {
3416 set_foreground_color(default_bg_color);
3427 for (i = -1; i < 2; i++) {
3428 for (j = -1; j < 2; j++) {
3429 if (i == 0 && j == 0) {
3434 set_foreground_color(default_out_color);
3435 draw_mode = OUTLINE;
3443 set_foreground_color(default_fg_color);
3448 #if defined(X11) && defined(HAVE_XDBE)
3449 if (output_methods & TO_X) {
3450 xdbe_swap_buffers();
3452 #endif /* X11 && HAVE_XDBE */
3453 if(overwrite_fpointer) {
3454 fclose(overwrite_fpointer);
3455 overwrite_fpointer = 0;
3457 if(append_fpointer) {
3458 fclose(append_fpointer);
3459 append_fpointer = 0;
3464 static void clear_text(int exposures)
3468 /* The swap action is XdbeBackground, which clears */
3472 if (display && window.window) { // make sure these are !null
3473 /* there is some extra space for borders and outlines */
3474 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3475 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3476 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3477 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3482 static int need_to_update;
3484 /* update_text() generates new text and clears old text area */
3485 static void update_text(void)
3492 if (output_methods & TO_X)
3497 llua_update_info(&info, update_interval);
3498 #endif /* HAVE_LUA */
3501 #ifdef HAVE_SYS_INOTIFY_H
3505 static void main_loop(void)
3508 #ifdef SIGNAL_BLOCKING
3509 sigset_t newmask, oldmask;
3512 #ifdef HAVE_SYS_INOTIFY_H
3513 int inotify_config_wd = -1;
3514 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3515 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3516 char inotify_buff[INOTIFY_BUF_LEN];
3517 #endif /* HAVE_SYS_INOTIFY_H */
3520 #ifdef SIGNAL_BLOCKING
3521 sigemptyset(&newmask);
3522 sigaddset(&newmask, SIGINT);
3523 sigaddset(&newmask, SIGTERM);
3524 sigaddset(&newmask, SIGUSR1);
3527 last_update_time = 0.0;
3528 next_update_time = get_time();
3530 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3531 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3532 char buf[max_user_text];
3534 get_battery_short_status(buf, max_user_text, "BAT0");
3536 update_interval = update_interval_bat;
3538 update_interval = update_interval_old;
3543 #ifdef SIGNAL_BLOCKING
3544 /* block signals. we will inspect for pending signals later */
3545 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3546 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3551 if (output_methods & TO_X) {
3554 /* wait for X event or timeout */
3556 if (!XPending(display)) {
3560 t = next_update_time - get_time();
3564 } else if (t > update_interval) {
3565 t = update_interval;
3568 tv.tv_sec = (long) t;
3569 tv.tv_usec = (long) (t * 1000000) % 1000000;
3571 FD_SET(ConnectionNumber(display), &fdsr);
3573 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3575 if (errno != EINTR) {
3576 NORM_ERR("can't select(): %s", strerror(errno));
3586 if (need_to_update) {
3588 int wx = window.x, wy = window.y;
3598 /* resize window if it isn't right size */
3600 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3601 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3602 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3603 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3604 draw_stuff(); /* redraw everything in our newly sized window */
3605 XResizeWindow(display, window.window, window.width,
3606 window.height); /* resize window */
3607 set_transparent_background(window.window, own_window_argb_value);
3610 xdbe_swap_buffers();
3615 /* update lua window globals */
3616 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3617 #endif /* HAVE_LUA */
3620 /* move window if it isn't in right position */
3621 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3622 XMoveWindow(display, window.window, window.x, window.y);
3627 if (changed && window.type == TYPE_PANEL) {
3630 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3633 switch (text_alignment) {
3660 set_struts(sidenum);
3671 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3672 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3673 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3674 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3675 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3680 /* handle X events */
3681 while (XPending(display)) {
3684 XNextEvent(display, &ev);
3691 r.width = ev.xexpose.width;
3692 r.height = ev.xexpose.height;
3693 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3697 case PropertyNotify:
3699 if ( ev.xproperty.state == PropertyNewValue ) {
3700 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3706 case ReparentNotify:
3707 /* make background transparent */
3709 set_transparent_background(window.window, own_window_argb_value);
3713 case ConfigureNotify:
3715 /* if window size isn't what expected, set fixed size */
3716 if (ev.xconfigure.width != window.width
3717 || ev.xconfigure.height != window.height) {
3718 if (window.width != 0 && window.height != 0) {
3722 /* clear old stuff before screwing up
3727 XWindowAttributes attrs;
3728 if (XGetWindowAttributes(display,
3729 window.window, &attrs)) {
3730 window.width = attrs.width;
3731 window.height = attrs.height;
3735 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3736 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3737 if (text_width > maximum_width
3738 && maximum_width > 0) {
3739 text_width = maximum_width;
3743 /* if position isn't what expected, set fixed pos
3744 * total_updates avoids setting fixed_pos when window
3745 * is set to weird locations when started */
3746 /* // this is broken
3747 if (total_updates >= 2 && !fixed_pos
3748 && (window.x != ev.xconfigure.x
3749 || window.y != ev.xconfigure.y)
3750 && (ev.xconfigure.x != 0
3751 || ev.xconfigure.y != 0)) {
3759 /* if an ordinary window with decorations */
3760 if ((window.type == TYPE_NORMAL &&
3761 (!TEST_HINT(window.hints,
3762 HINT_UNDECORATED))) ||
3763 window.type == TYPE_DESKTOP) {
3764 /* allow conky to hold input focus. */
3767 /* forward the click to the desktop window */
3768 XUngrabPointer(display, ev.xbutton.time);
3769 ev.xbutton.window = window.desktop;
3770 ev.xbutton.x = ev.xbutton.x_root;
3771 ev.xbutton.y = ev.xbutton.y_root;
3772 XSendEvent(display, ev.xbutton.window, False,
3773 ButtonPressMask, &ev);
3774 XSetInputFocus(display, ev.xbutton.window,
3775 RevertToParent, ev.xbutton.time);
3782 /* if an ordinary window with decorations */
3783 if ((window.type == TYPE_NORMAL)
3784 && (!TEST_HINT(window.hints,
3785 HINT_UNDECORATED))) {
3786 /* allow conky to hold input focus. */
3789 /* forward the release to the desktop window */
3790 ev.xbutton.window = window.desktop;
3791 ev.xbutton.x = ev.xbutton.x_root;
3792 ev.xbutton.y = ev.xbutton.y_root;
3793 XSendEvent(display, ev.xbutton.window, False,
3794 ButtonReleaseMask, &ev);
3803 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3804 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3806 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3807 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3809 #endif /* HAVE_XDAMAGE */
3815 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3816 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3817 #endif /* HAVE_XDAMAGE */
3819 /* XDBE doesn't seem to provide a way to clear the back buffer
3820 * without interfering with the front buffer, other than passing
3821 * XdbeBackground to XdbeSwapBuffers. That means that if we're
3822 * using XDBE, we need to redraw the text even if it wasn't part of
3823 * the exposed area. OTOH, if we're not going to call draw_stuff at
3824 * all, then no swap happens and we can safely do nothing. */
3826 if (!XEmptyRegion(x11_stuff.region)) {
3831 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3832 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3833 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3834 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3835 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3838 XSetRegion(display, window.gc, x11_stuff.region);
3841 XftDrawSetClip(window.xftdraw, x11_stuff.region);
3845 XDestroyRegion(x11_stuff.region);
3846 x11_stuff.region = XCreateRegion();
3850 t = (next_update_time - get_time()) * 1000000;
3851 if(t > 0) usleep((useconds_t)t);
3855 if(output_methods & TO_NCURSES) {
3864 #ifdef SIGNAL_BLOCKING
3865 /* unblock signals of interest and let handler fly */
3866 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
3867 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3871 switch (g_signal_pending) {
3874 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
3879 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
3882 if (output_methods & TO_X) {
3883 XDestroyRegion(x11_stuff.region);
3884 x11_stuff.region = NULL;
3886 XDamageDestroy(display, x11_stuff.damage);
3887 XFixesDestroyRegion(display, x11_stuff.region2);
3888 XFixesDestroyRegion(display, x11_stuff.part);
3889 #endif /* HAVE_XDAMAGE */
3895 if(overwrite_file) {
3896 free(overwrite_file);
3905 /* Reaching here means someone set a signal
3906 * (SIGXXXX, signal_handler), but didn't write any code
3908 * If you don't want to handle a signal, don't set a handler on
3909 * it in the first place. */
3910 if (g_signal_pending) {
3911 NORM_ERR("ignoring signal (%d)", g_signal_pending);
3915 #ifdef HAVE_SYS_INOTIFY_H
3916 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
3917 inotify_config_wd = inotify_add_watch(inotify_fd,
3921 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
3922 int len = 0, idx = 0;
3924 struct timeval time_to_wait;
3926 FD_ZERO(&descriptors);
3927 FD_SET(inotify_fd, &descriptors);
3929 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
3931 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
3932 if (FD_ISSET(inotify_fd, &descriptors)) {
3933 /* process inotify events */
3934 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
3935 while (len > 0 && idx < len) {
3936 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
3937 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
3938 /* current_config should be reloaded */
3939 NORM_ERR("'%s' modified, reloading...", current_config);
3941 if (ev->mask & IN_IGNORED) {
3942 /* for some reason we get IN_IGNORED here
3943 * sometimes, so we need to re-add the watch */
3944 inotify_config_wd = inotify_add_watch(inotify_fd,
3952 llua_inotify_query(ev->wd, ev->mask);
3954 #endif /* HAVE_LUA */
3955 idx += INOTIFY_EVENT_SIZE + ev->len;
3958 } else if (disable_auto_reload && inotify_fd != -1) {
3959 inotify_rm_watch(inotify_fd, inotify_config_wd);
3961 inotify_fd = inotify_config_wd = 0;
3963 #endif /* HAVE_SYS_INOTIFY_H */
3966 llua_update_info(&info, update_interval);
3967 #endif /* HAVE_LUA */
3968 g_signal_pending = 0;
3970 clean_up(current_mail_spool, NULL);
3972 #ifdef HAVE_SYS_INOTIFY_H
3973 if (inotify_fd != -1) {
3974 inotify_rm_watch(inotify_fd, inotify_config_wd);
3976 inotify_fd = inotify_config_wd = 0;
3978 #endif /* HAVE_SYS_INOTIFY_H */
3982 static void load_config_file_x11(const char *);
3984 void initialisation(int argc, char** argv);
3986 /* reload the config file */
3987 static void reload_config(void)
3989 char *current_config_copy = strdup(current_config);
3990 clean_up(NULL, NULL);
3991 sleep(1); /* slight pause */
3992 current_config = current_config_copy;
3993 initialisation(argc_copy, argv_copy);
3997 void clean_up_x11() {
3998 if(window_created == 1) {
3999 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4000 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4001 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4002 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4007 if(x11_stuff.region) {
4008 XDestroyRegion(x11_stuff.region);
4009 x11_stuff.region = NULL;
4012 XCloseDisplay(display);
4015 if(info.x11.desktop.all_names) {
4016 free(info.x11.desktop.all_names);
4017 info.x11.desktop.all_names = NULL;
4019 if (info.x11.desktop.name) {
4020 free(info.x11.desktop.name);
4021 info.x11.desktop.name = NULL;
4027 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4031 if(output_methods & TO_NCURSES) {
4035 conftree_empty(currentconffile);
4036 currentconffile = NULL;
4043 timed_thread_destroy_registered_threads();
4045 if (info.cpu_usage) {
4046 free(info.cpu_usage);
4047 info.cpu_usage = NULL;
4050 if (x_initialised == YES) {
4053 free(fonts); //in set_default_configurations a font is set but not loaded
4061 free_text_objects(&global_root_object, 0);
4080 free(current_config);
4083 #ifdef TCP_PORT_MONITOR
4084 tcp_portmon_clear();
4093 weather_free_info();
4096 llua_shutdown_hook();
4098 #endif /* HAVE_LUA */
4100 if (output_methods & TO_X)
4108 for (i = 0; i < special_count; i++) {
4109 if (specials[i].type == GRAPH) {
4110 free(specials[i].graph);
4118 clear_diskio_stats();
4119 if(global_cpu != NULL) {
4125 void clean_up(void *memtofree1, void* memtofree2)
4127 free_update_callbacks();
4128 clean_up_without_threads(memtofree1, memtofree2);
4131 static int string_to_bool(const char *s)
4134 // Assumes an option without a true/false means true
4136 } else if (strcasecmp(s, "yes") == EQUAL) {
4138 } else if (strcasecmp(s, "true") == EQUAL) {
4140 } else if (strcasecmp(s, "1") == EQUAL) {
4147 static enum alignment string_to_alignment(const char *s)
4149 if (strcasecmp(s, "top_left") == EQUAL) {
4151 } else if (strcasecmp(s, "top_right") == EQUAL) {
4153 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4155 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4157 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4158 return BOTTOM_RIGHT;
4159 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4160 return BOTTOM_MIDDLE;
4161 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4163 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4164 return MIDDLE_RIGHT;
4165 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4166 return MIDDLE_MIDDLE;
4167 } else if (strcasecmp(s, "tl") == EQUAL) {
4169 } else if (strcasecmp(s, "tr") == EQUAL) {
4171 } else if (strcasecmp(s, "tm") == EQUAL) {
4173 } else if (strcasecmp(s, "bl") == EQUAL) {
4175 } else if (strcasecmp(s, "br") == EQUAL) {
4176 return BOTTOM_RIGHT;
4177 } else if (strcasecmp(s, "bm") == EQUAL) {
4178 return BOTTOM_MIDDLE;
4179 } else if (strcasecmp(s, "ml") == EQUAL) {
4181 } else if (strcasecmp(s, "mr") == EQUAL) {
4182 return MIDDLE_RIGHT;
4183 } else if (strcasecmp(s, "mm") == EQUAL) {
4184 return MIDDLE_MIDDLE;
4185 } else if (strcasecmp(s, "none") == EQUAL) {
4188 return ALIGNMENT_ERROR;
4193 static void set_default_configurations_for_x(void)
4195 default_fg_color = WhitePixel(display, screen);
4196 default_bg_color = BlackPixel(display, screen);
4197 default_out_color = BlackPixel(display, screen);
4198 color0 = default_fg_color;
4199 color1 = default_fg_color;
4200 color2 = default_fg_color;
4201 color3 = default_fg_color;
4202 color4 = default_fg_color;
4203 color5 = default_fg_color;
4204 color6 = default_fg_color;
4205 color7 = default_fg_color;
4206 color8 = default_fg_color;
4207 color9 = default_fg_color;
4208 current_text_color = default_fg_color;
4212 static void set_default_configurations(void)
4219 fork_to_background = 0;
4220 total_run_times = 0;
4221 info.cpu_avg_samples = 2;
4222 info.net_avg_samples = 2;
4223 info.diskio_avg_samples = 2;
4228 format_human_readable = 1;
4236 mpd_env_host = getenv("MPD_HOST");
4237 mpd_env_port = getenv("MPD_PORT");
4239 if (!mpd_env_host || !strlen(mpd_env_host)) {
4240 mpd_set_host("localhost");
4242 /* MPD_HOST environment variable is set */
4243 char *mpd_hostpart = strchr(mpd_env_host, '@');
4244 if (!mpd_hostpart) {
4245 mpd_set_host(mpd_env_host);
4247 /* MPD_HOST contains a password */
4248 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4249 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4251 if (!strlen(mpd_hostpart + 1)) {
4252 mpd_set_host("localhost");
4254 mpd_set_host(mpd_hostpart + 1);
4257 mpd_set_password(mpd_password, 1);
4262 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4263 /* failed to set port from environment variable */
4264 mpd_set_port("6600");
4268 info.xmms2.artist = NULL;
4269 info.xmms2.album = NULL;
4270 info.xmms2.title = NULL;
4271 info.xmms2.genre = NULL;
4272 info.xmms2.comment = NULL;
4273 info.xmms2.url = NULL;
4274 info.xmms2.status = NULL;
4275 info.xmms2.playlist = NULL;
4277 use_spacer = NO_SPACER;
4279 output_methods = TO_X;
4281 output_methods = TO_STDOUT;
4287 show_graph_scale = 0;
4288 show_graph_range = 0;
4291 draw_graph_borders = 1;
4293 set_first_font("6x10");
4301 window.type = TYPE_NORMAL;
4303 strcpy(window.class_name, PACKAGE_NAME);
4304 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4306 use_argb_visual = 0;
4307 own_window_argb_value = 255;
4310 stippled_borders = 0;
4311 window.border_inner_margin = 3;
4312 window.border_outer_margin = 1;
4313 window.border_width = 1;
4314 text_alignment = BOTTOM_LEFT;
4315 info.x11.monitor.number = 1;
4316 info.x11.monitor.current = 0;
4317 info.x11.desktop.current = 1;
4318 info.x11.desktop.number = 1;
4319 info.x11.desktop.nitems = 0;
4320 info.x11.desktop.all_names = NULL;
4321 info.x11.desktop.name = NULL;
4326 free(current_mail_spool);
4330 variable_substitute(MAIL_FILE, buf, 256);
4331 if (buf[0] != '\0') {
4332 current_mail_spool = strndup(buf, text_buffer_size);
4337 set_update_interval(3);
4338 update_interval_bat = NOBATTERY;
4339 info.music_player_interval = 1.0;
4340 stuff_in_uppercase = 0;
4341 info.users.number = 1;
4343 set_times_in_seconds(0);
4345 #ifdef TCP_PORT_MONITOR
4346 /* set default connection limit */
4347 tcp_portmon_set_max_connections(0);
4351 /* returns 1 if you can overwrite or create the file at 'path' */
4352 static _Bool overwrite_works(const char *path)
4356 if (!(filepointer = fopen(path, "w")))
4358 fclose(filepointer);
4362 /* returns 1 if you can append or create the file at 'path' */
4363 static _Bool append_works(const char *path)
4367 if (!(filepointer = fopen(path, "a")))
4369 fclose(filepointer);
4375 /* WARNING, this type not in Xlib spec */
4376 int x11_error_handler(Display *d, XErrorEvent *err)
4377 __attribute__((noreturn));
4378 int x11_error_handler(Display *d, XErrorEvent *err)
4380 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",
4382 (long unsigned)err->display,
4383 (long)err->resourceid,
4393 int x11_ioerror_handler(Display *d)
4394 __attribute__((noreturn));
4395 int x11_ioerror_handler(Display *d)
4397 NORM_ERR("X Error: Display %lx\n",
4404 static void X11_initialisation(void)
4406 if (x_initialised == YES) return;
4407 output_methods |= TO_X;
4409 set_default_configurations_for_x();
4410 x_initialised = YES;
4413 /* WARNING, this type not in Xlib spec */
4414 XSetErrorHandler(&x11_error_handler);
4415 XSetIOErrorHandler(&x11_ioerror_handler);
4419 static char **xargv = 0;
4420 static int xargc = 0;
4422 static void X11_create_window(void)
4424 if (output_methods & TO_X) {
4426 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4427 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4429 #else /* OWN_WINDOW */
4430 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4431 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4433 #endif /* OWN_WINDOW */
4437 update_text_area(); /* to position text/window on screen */
4440 if (own_window && !fixed_pos) {
4441 XMoveWindow(display, window.window, window.x, window.y);
4444 set_transparent_background(window.window, own_window_argb_value);
4452 x11_stuff.region = XCreateRegion();
4454 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4455 NORM_ERR("Xdamage extension unavailable");
4457 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4458 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4459 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4460 #endif /* HAVE_XDAMAGE */
4463 update_text_area(); /* to get initial size of the window */
4466 /* setup lua window globals */
4467 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4468 #endif /* HAVE_LUA */
4472 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4473 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4474 #define CONF2(a) if (strcasecmp(name, a) == 0)
4475 #define CONF(a) else CONF2(a)
4476 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4477 || strcasecmp(name, b) == 0)
4478 #define CONF_CONTINUE 1
4479 #define CONF_BREAK 2
4480 #define CONF_BUFF_SIZE 512
4482 static FILE *open_config_file(const char *f)
4484 #ifdef CONFIG_OUTPUT
4485 if (!strcmp(f, "==builtin==")) {
4486 return conf_cookie_open();
4488 #endif /* CONFIG_OUTPUT */
4489 return fopen(f, "r");
4492 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4496 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4499 remove_comments(buf);
4504 while (*p && isspace((int) *p)) {
4508 return CONF_CONTINUE; /* empty line */
4515 while (*p2 && !isspace((int) *p2)) {
4519 *p2 = '\0'; /* break at name's end */
4526 while (*p && isspace((int) *p)) {
4532 p2 = *value + strlen(*value);
4533 while (isspace((int) *(p2 - 1))) {
4543 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4545 if (windowtype == TYPE_DOCK) {
4546 NORM_ERR("alignment is disabled when own_window_type is dock");
4548 #endif /*OWN_WINDOW */
4550 int a = string_to_alignment(value);
4553 if (setbyconffile) {
4555 } else NORM_ERR("'%s' is not a alignment setting", value);
4557 *ltext_alignment = a;
4559 } else if (setbyconffile) {
4565 char load_config_file(const char *f)
4570 fp = open_config_file(f);
4574 DBGP("reading contents from config file '%s'", f);
4577 char buff[CONF_BUFF_SIZE], *name, *value;
4578 int ret = do_config_step(&line, fp, buff, &name, &value);
4579 if (ret == CONF_BREAK) {
4581 } else if (ret == CONF_CONTINUE) {
4587 if (string_to_bool(value)) {
4588 output_methods &= TO_X;
4591 output_methods &= ~TO_X;
4592 x_initialised = NEVER;
4596 if (!value || x_initialised == YES) {
4601 disp = strdup(value);
4605 setalignment(&text_alignment, window.type, value, f, line, 1);
4607 CONF("background") {
4608 fork_to_background = string_to_bool(value);
4611 CONF2("background") {
4612 fork_to_background = string_to_bool(value);
4616 CONF("show_graph_scale") {
4617 show_graph_scale = string_to_bool(value);
4619 CONF("show_graph_range") {
4620 show_graph_range = string_to_bool(value);
4622 CONF("border_inner_margin") {
4624 window.border_inner_margin = strtol(value, 0, 0);
4625 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4630 CONF("border_outer_margin") {
4632 window.border_outer_margin = strtol(value, 0, 0);
4633 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4638 CONF("border_width") {
4640 window.border_width = strtol(value, 0, 0);
4641 if (window.border_width < 1) window.border_width = 1;
4647 #define TEMPLATE_CONF(n) \
4648 CONF("template"#n) { \
4649 if (set_template(n, value)) \
4664 parse_global_imap_mail_args(value);
4671 parse_global_pop3_mail_args(value);
4676 CONF("default_bar_size") {
4679 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4686 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4690 CONF("default_graph_size") {
4693 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4700 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4703 CONF("default_gauge_size") {
4706 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4713 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4720 mpd_set_host(value);
4726 if (value && mpd_set_port(value)) {
4730 CONF("mpd_password") {
4732 mpd_set_password(value, 0);
4738 CONF("music_player_interval") {
4740 info.music_player_interval = strtod(value, 0);
4746 CONF("sensor_device") {
4748 sensor_device = strtol(value, 0, 0);
4754 CONF("cpu_avg_samples") {
4756 cpu_avg_samples = strtol(value, 0, 0);
4757 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4760 info.cpu_avg_samples = cpu_avg_samples;
4766 CONF("net_avg_samples") {
4768 net_avg_samples = strtol(value, 0, 0);
4769 if (net_avg_samples < 1 || net_avg_samples > 14) {
4772 info.net_avg_samples = net_avg_samples;
4778 CONF("diskio_avg_samples") {
4780 diskio_avg_samples = strtol(value, 0, 0);
4781 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4784 info.diskio_avg_samples = diskio_avg_samples;
4792 CONF("double_buffer") {
4793 use_xdbe = string_to_bool(value);
4797 CONF("override_utf8_locale") {
4798 utf8_mode = string_to_bool(value);
4800 CONF("draw_borders") {
4801 draw_borders = string_to_bool(value);
4803 CONF("draw_graph_borders") {
4804 draw_graph_borders = string_to_bool(value);
4806 CONF("draw_shades") {
4807 draw_shades = string_to_bool(value);
4809 CONF("draw_outline") {
4810 draw_outline = string_to_bool(value);
4813 CONF("times_in_seconds") {
4814 set_times_in_seconds(string_to_bool(value));
4816 CONF("max_text_width") {
4817 max_text_width = atoi(value);
4819 CONF("out_to_console") {
4820 if(string_to_bool(value)) {
4821 output_methods |= TO_STDOUT;
4823 output_methods &= ~TO_STDOUT;
4826 CONF("extra_newline") {
4827 extra_newline = string_to_bool(value);
4829 CONF("disable_auto_reload") {
4830 disable_auto_reload = string_to_bool(value);
4832 CONF("out_to_stderr") {
4833 if(string_to_bool(value))
4834 output_methods |= TO_STDERR;
4837 CONF("out_to_ncurses") {
4838 if(string_to_bool(value)) {
4841 output_methods |= TO_NCURSES;
4845 CONF("overwrite_file") {
4846 if(overwrite_file) {
4847 free(overwrite_file);
4850 if(overwrite_works(value)) {
4851 overwrite_file = strdup(value);
4852 output_methods |= OVERWRITE_FILE;
4854 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4856 CONF("append_file") {
4861 if(append_works(value)) {
4862 append_file = strdup(value);
4863 output_methods |= APPEND_FILE;
4865 NORM_ERR("append_file won't be able to create/append '%s'", value);
4867 CONF("use_spacer") {
4869 if (strcasecmp(value, "left") == EQUAL) {
4870 use_spacer = LEFT_SPACER;
4871 } else if (strcasecmp(value, "right") == EQUAL) {
4872 use_spacer = RIGHT_SPACER;
4873 } else if (strcasecmp(value, "none") == EQUAL) {
4874 use_spacer = NO_SPACER;
4876 use_spacer = string_to_bool(value);
4877 NORM_ERR("use_spacer should have an argument of left, right, or"
4878 " none. '%s' seems to be some form of '%s', so"
4879 " defaulting to %s.", value,
4880 use_spacer ? "true" : "false",
4881 use_spacer ? "right" : "none");
4883 use_spacer = RIGHT_SPACER;
4885 use_spacer = NO_SPACER;
4889 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
4890 use_spacer = RIGHT_SPACER;
4896 use_xft = string_to_bool(value);
4900 set_first_font(value);
4904 if (value && font_count >= 0) {
4905 fonts[0].font_alpha = atof(value) * 65535.0;
4912 if (string_to_bool(value)) {
4913 NORM_ERR("Xft not enabled at compile time");
4917 /* xftfont silently ignored when no Xft */
4920 /* xftalpha is silently ignored when no Xft */
4925 set_first_font(value);
4933 gap_x = atoi(value);
4940 gap_y = atoi(value);
4946 CONF("mail_spool") {
4950 variable_substitute(value, buffer, 256);
4952 if (buffer[0] != '\0') {
4953 if (current_mail_spool) {
4954 free(current_mail_spool);
4956 current_mail_spool = strndup(buffer, text_buffer_size);
4963 CONF("minimum_size") {
4965 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
4967 if (sscanf(value, "%d", &minimum_width) != 1) {
4975 CONF("maximum_width") {
4977 if (sscanf(value, "%d", &maximum_width) != 1) {
4985 CONF("no_buffers") {
4986 no_buffers = string_to_bool(value);
4988 CONF("top_name_width") {
4989 if (set_top_name_width(value))
4992 CONF("top_cpu_separate") {
4993 cpu_separate = string_to_bool(value);
4995 CONF("short_units") {
4996 short_units = string_to_bool(value);
4998 CONF("format_human_readable") {
4999 format_human_readable = string_to_bool(value);
5002 CONF("hddtemp_host") {
5003 set_hddtemp_host(value);
5005 CONF("hddtemp_port") {
5006 set_hddtemp_port(value);
5008 #endif /* HDDTEMP */
5009 CONF("pad_percents") {
5010 pad_percents = atoi(value);
5014 CONF("own_window") {
5016 own_window = string_to_bool(value);
5019 CONF("own_window_class") {
5021 memset(window.class_name, 0, sizeof(window.class_name));
5022 strncpy(window.class_name, value,
5023 sizeof(window.class_name) - 1);
5026 CONF("own_window_title") {
5028 memset(window.title, 0, sizeof(window.title));
5029 strncpy(window.title, value, sizeof(window.title) - 1);
5032 CONF("own_window_transparent") {
5034 set_transparent = string_to_bool(value);
5037 CONF("own_window_hints") {
5039 char *p_hint, *p_save;
5040 char delim[] = ", ";
5042 /* tokenize the value into individual hints */
5043 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5045 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5046 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5047 SET_HINT(window.hints, HINT_UNDECORATED);
5048 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5049 SET_HINT(window.hints, HINT_BELOW);
5050 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5051 SET_HINT(window.hints, HINT_ABOVE);
5052 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5053 SET_HINT(window.hints, HINT_STICKY);
5054 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5055 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5056 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5057 SET_HINT(window.hints, HINT_SKIP_PAGER);
5062 p_hint = strtok_r(NULL, delim, &p_save);
5063 } while (p_hint != NULL);
5069 CONF("own_window_type") {
5071 if (strncmp(value, "normal", 6) == EQUAL) {
5072 window.type = TYPE_NORMAL;
5073 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5074 window.type = TYPE_DESKTOP;
5075 } else if (strncmp(value, "dock", 4) == EQUAL) {
5076 window.type = TYPE_DOCK;
5077 text_alignment = TOP_LEFT;
5078 } else if (strncmp(value, "panel", 5) == EQUAL) {
5079 window.type = TYPE_PANEL;
5080 } else if (strncmp(value, "override", 8) == EQUAL) {
5081 window.type = TYPE_OVERRIDE;
5090 CONF("own_window_argb_visual") {
5091 use_argb_visual = string_to_bool(value);
5093 CONF("own_window_argb_value") {
5094 own_window_argb_value = strtol(value, 0, 0);
5095 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5096 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5099 #endif /* USE_ARGB */
5101 CONF("stippled_borders") {
5103 stippled_borders = strtol(value, 0, 0);
5105 stippled_borders = 4;
5109 CONF("imlib_cache_size") {
5111 cimlib_set_cache_size(atoi(value));
5114 CONF("imlib_cache_flush_interval") {
5116 cimlib_set_cache_flush_interval(atoi(value));
5121 CONF("update_interval_on_battery") {
5123 update_interval_bat = strtod(value, 0);
5128 CONF("update_interval") {
5130 set_update_interval(strtod(value, 0));
5134 if (info.music_player_interval == 0) {
5135 // default to update_interval
5136 info.music_player_interval = update_interval;
5139 CONF("total_run_times") {
5141 total_run_times = strtod(value, 0);
5147 stuff_in_uppercase = string_to_bool(value);
5149 CONF("max_specials") {
5151 max_specials = atoi(value);
5156 CONF("max_user_text") {
5158 max_user_text = atoi(value);
5163 CONF("text_buffer_size") {
5165 text_buffer_size = atoi(value);
5166 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5167 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5168 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5176 if (output_methods & TO_X) {
5177 X11_initialisation();
5186 global_text = (char *) malloc(1);
5187 global_text[0] = '\0';
5190 unsigned int l = strlen(global_text);
5192 char buf[CONF_BUFF_SIZE];
5194 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5200 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5208 /* Check for continuation of \\-\n. */
5209 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5210 global_text[l-1] = '\0';
5214 global_text = (char *) realloc(global_text, l + bl + 1);
5215 strcat(global_text, buf);
5217 if (strlen(global_text) > max_user_text) {
5221 global_text_lines = line + 1;
5224 #ifdef TCP_PORT_MONITOR
5225 CONF("max_port_monitor_connections") {
5227 if (!value || (sscanf(value, "%d", &max) != 1)) {
5228 /* an error. use default, warn and continue. */
5229 tcp_portmon_set_max_connections(0);
5231 } else if (tcp_portmon_set_max_connections(max)) {
5232 /* max is < 0, default has been set*/
5237 CONF("if_up_strictness") {
5239 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5240 ifup_strictness = IFUP_UP;
5241 } else if (strcasecmp(value, "up") == EQUAL) {
5242 ifup_strictness = IFUP_UP;
5243 } else if (strcasecmp(value, "link") == EQUAL) {
5244 ifup_strictness = IFUP_LINK;
5245 } else if (strcasecmp(value, "address") == EQUAL) {
5246 ifup_strictness = IFUP_ADDR;
5248 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5249 ifup_strictness = IFUP_UP;
5253 CONF("temperature_unit") {
5255 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5256 } else if (set_temp_output_unit(value)) {
5257 NORM_ERR("temperature_unit: incorrect argument");
5264 char *ptr = strtok(value, " ");
5267 ptr = strtok(NULL, " ");
5274 CONF("lua_draw_hook_pre") {
5276 llua_set_draw_pre_hook(value);
5281 CONF("lua_draw_hook_post") {
5283 llua_set_draw_post_hook(value);
5288 CONF("lua_startup_hook") {
5290 llua_set_startup_hook(value);
5295 CONF("lua_shutdown_hook") {
5297 llua_set_shutdown_hook(value);
5303 #endif /* HAVE_LUA */
5315 CONF("default_color"){}
5316 CONF3("default_shade_color", "default_shadecolor"){}
5317 CONF3("default_outline_color", "default_outlinecolor") {}
5318 CONF("own_window_colour") {}
5321 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5327 if (info.music_player_interval == 0) {
5328 // default to update_interval
5329 info.music_player_interval = update_interval;
5331 if (!global_text) { // didn't supply any text
5332 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5334 if (!output_methods) {
5335 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5337 #if defined(NCURSES)
5339 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5340 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5341 output_methods &= ~TO_NCURSES;
5345 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5346 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5347 output_methods &= ~(TO_STDOUT | TO_STDERR);
5349 #endif /* NCURSES */
5354 static void load_config_file_x11(const char *f)
5359 fp = open_config_file(f);
5363 DBGP("reading contents from config file '%s'", f);
5366 char buff[CONF_BUFF_SIZE], *name, *value;
5367 int ret = do_config_step(&line, fp, buff, &name, &value);
5368 if (ret == CONF_BREAK) {
5370 } else if (ret == CONF_CONTINUE) {
5375 X11_initialisation();
5376 if (x_initialised == YES) {
5378 color0 = get_x11_color(value);
5385 X11_initialisation();
5386 if (x_initialised == YES) {
5388 color1 = get_x11_color(value);
5395 X11_initialisation();
5396 if (x_initialised == YES) {
5398 color2 = get_x11_color(value);
5405 X11_initialisation();
5406 if (x_initialised == YES) {
5408 color3 = get_x11_color(value);
5415 X11_initialisation();
5416 if (x_initialised == YES) {
5418 color4 = get_x11_color(value);
5425 X11_initialisation();
5426 if (x_initialised == YES) {
5428 color5 = get_x11_color(value);
5435 X11_initialisation();
5436 if (x_initialised == YES) {
5438 color6 = get_x11_color(value);
5445 X11_initialisation();
5446 if (x_initialised == YES) {
5448 color7 = get_x11_color(value);
5455 X11_initialisation();
5456 if (x_initialised == YES) {
5458 color8 = get_x11_color(value);
5465 X11_initialisation();
5466 if (x_initialised == YES) {
5468 color9 = get_x11_color(value);
5474 CONF("default_color") {
5475 X11_initialisation();
5476 if (x_initialised == YES) {
5478 default_fg_color = get_x11_color(value);
5484 CONF3("default_shade_color", "default_shadecolor") {
5485 X11_initialisation();
5486 if (x_initialised == YES) {
5488 default_bg_color = get_x11_color(value);
5494 CONF3("default_outline_color", "default_outlinecolor") {
5495 X11_initialisation();
5496 if (x_initialised == YES) {
5498 default_out_color = get_x11_color(value);
5505 CONF("own_window_colour") {
5506 X11_initialisation();
5507 if (x_initialised == YES) {
5509 background_colour = get_x11_color(value);
5511 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5512 "'#' for hex colours");
5518 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5519 if(output_methods & TO_X) {
5520 X11_initialisation();
5529 #undef CONF_CONTINUE
5530 #undef CONF_BUFF_SIZE
5538 static void print_help(const char *prog_name) {
5539 printf("Usage: %s [OPTION]...\n"
5540 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5541 "window. Command line options will override configurations defined in config\n"
5543 " -v, --version version\n"
5544 " -q, --quiet quiet mode\n"
5545 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5546 " -c, --config=FILE config file to load\n"
5547 #ifdef CONFIG_OUTPUT
5548 " -C, --print-config print the builtin default config to stdout\n"
5549 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5551 " -d, --daemonize daemonize, fork to background\n"
5552 " -h, --help help\n"
5554 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5555 " -f, --font=FONT font to use\n"
5556 " -X, --display=DISPLAY X11 display to use\n"
5558 " -o, --own-window create own window to draw\n"
5561 " -b, --double-buffer double buffer (prevents flickering)\n"
5563 " -w, --window-id=WIN_ID window id to draw\n"
5564 " -x X x position\n"
5565 " -y Y y position\n"
5567 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5568 " -u, --interval=SECS update interval\n"
5569 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5570 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5575 /* : means that character before that takes an argument */
5576 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5586 #ifdef CONFIG_OUTPUT
5591 static const struct option longopts[] = {
5592 { "help", 0, NULL, 'h' },
5593 { "version", 0, NULL, 'V' },
5594 { "quiet", 0, NULL, 'q' },
5595 { "debug", 0, NULL, 'D' },
5596 { "config", 1, NULL, 'c' },
5597 #ifdef CONFIG_OUTPUT
5598 { "print-config", 0, NULL, 'C' },
5600 { "daemonize", 0, NULL, 'd' },
5602 { "alignment", 1, NULL, 'a' },
5603 { "font", 1, NULL, 'f' },
5604 { "display", 1, NULL, 'X' },
5606 { "own-window", 0, NULL, 'o' },
5609 { "double-buffer", 0, NULL, 'b' },
5611 { "window-id", 1, NULL, 'w' },
5613 { "text", 1, NULL, 't' },
5614 { "interval", 1, NULL, 'u' },
5615 { "pause", 1, NULL, 'p' },
5619 void set_current_config(void);
5620 void set_current_config(void)
5622 /* check if specified config file is valid */
5623 if (current_config) {
5625 if (stat(current_config, &sb) ||
5626 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5627 NORM_ERR("invalid configuration file '%s'\n", current_config);
5628 free(current_config);
5633 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5635 if (!current_config) {
5636 /* load default config file */
5637 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5640 /* Try to use personal config file first */
5641 to_real_path(buf, CONFIG_FILE);
5642 if (buf[0] && (fp = fopen(buf, "r"))) {
5643 current_config = strndup(buf, max_user_text);
5647 /* Try to use system config file if personal config not readable */
5648 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5649 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5653 /* No readable config found */
5654 if (!current_config) {
5655 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5656 #ifdef BUILD_BUILTIN_CONFIG
5657 current_config = strdup("==builtin==");
5658 NORM_ERR(NOCFGFILEFOUND
5659 ", using builtin default");
5661 CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5662 #endif /* ! CONF_OUTPUT */
5667 void initialisation(int argc, char **argv) {
5668 struct sigaction act, oact;
5670 set_default_configurations();
5671 set_current_config();
5672 load_config_file(current_config);
5673 currentconffile = conftree_add(currentconffile, current_config);
5675 /* init specials array */
5676 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5677 NORM_ERR("failed to create specials array");
5681 if (current_mail_spool == NULL) {
5684 variable_substitute(MAIL_FILE, buf, 256);
5686 if (buf[0] != '\0') {
5687 current_mail_spool = strndup(buf, text_buffer_size);
5692 /* handle other command line arguments */
5694 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5695 || defined(__NetBSD__)
5696 optind = optreset = 1;
5701 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5702 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5703 "kvm_open")) == NULL) {
5704 CRIT_ERR(NULL, NULL, "cannot read kvm");
5709 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5718 fork_to_background = 1;
5721 global_debug_level++;
5725 set_first_font(optarg);
5728 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
5747 global_text = strndup(optarg, max_user_text);
5748 convert_escapes(global_text);
5752 update_interval = strtod(optarg, 0);
5753 update_interval_old = update_interval;
5754 if (info.music_player_interval == 0) {
5755 // default to update_interval
5756 info.music_player_interval = update_interval;
5761 total_run_times = strtod(optarg, 0);
5765 gap_x = atoi(optarg);
5769 gap_y = atoi(optarg);
5774 startup_pause = atoi(optarg);
5775 sleep(startup_pause);
5786 if (output_methods & TO_X) {
5787 load_config_file_x11(current_config);
5791 /* generate text and get initial size */
5792 extract_variable_text(global_text);
5799 if (fork_to_background && first_pass) {
5804 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5811 fprintf(stderr, "\n");
5816 /* parent process */
5817 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5824 start_update_threading();
5826 text_buffer = malloc(max_user_text);
5827 memset(text_buffer, 0, max_user_text);
5828 tmpstring1 = malloc(text_buffer_size);
5829 memset(tmpstring1, 0, text_buffer_size);
5830 tmpstring2 = malloc(text_buffer_size);
5831 memset(tmpstring2, 0, text_buffer_size);
5836 X11_create_window();
5839 llua_setup_info(&info, update_interval);
5840 #endif /* HAVE_LUA */
5845 /* Set signal handlers */
5846 act.sa_handler = signal_handler;
5847 sigemptyset(&act.sa_mask);
5850 act.sa_flags |= SA_RESTART;
5853 if ( sigaction(SIGINT, &act, &oact) < 0
5854 || sigaction(SIGALRM, &act, &oact) < 0
5855 || sigaction(SIGUSR1, &act, &oact) < 0
5856 || sigaction(SIGHUP, &act, &oact) < 0
5857 || sigaction(SIGTERM, &act, &oact) < 0) {
5858 NORM_ERR("error setting signal handler: %s", strerror(errno));
5862 llua_startup_hook();
5863 #endif /* HAVE_LUA */
5866 int main(int argc, char **argv)
5875 g_signal_pending = 0;
5876 max_user_text = MAX_USER_TEXT_DEFAULT;
5878 memset(&info, 0, sizeof(info));
5882 #ifdef TCP_PORT_MONITOR
5883 /* set default connection limit */
5884 tcp_portmon_set_max_connections(0);
5887 /* handle command line parameters that don't change configs */
5889 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5890 || ((s = getenv("LANG")) && *s)) {
5891 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5893 NORM_ERR("malloc failed");
5895 for (x = 0; x < strlen(s); x++) {
5896 temp[x] = tolower(s[x]);
5899 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5905 if (!setlocale(LC_CTYPE, "")) {
5906 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5910 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5921 if (current_config) {
5922 free(current_config);
5924 current_config = strndup(optarg, max_user_text);
5927 if (!freopen("/dev/null", "w", stderr)) {
5928 NORM_ERR("unable to redirect stderr to /dev/null");
5932 print_help(argv[0]);
5934 #ifdef CONFIG_OUTPUT
5941 window.window = strtol(optarg, 0, 0);
5946 disp = strdup(optarg);
5955 set_current_config();
5958 /* Load xoap keys, if existing */
5962 #ifdef HAVE_SYS_INOTIFY_H
5963 inotify_fd = inotify_init();
5964 if(inotify_fd != -1) {
5967 fl = fcntl(inotify_fd, F_GETFL);
5968 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
5970 #endif /* HAVE_SYS_INOTIFY_H */
5972 initialisation(argc, argv);
5974 first_pass = 0; /* don't ever call fork() again */
5978 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5986 void alarm_handler(void) {
5988 kill(childpid, SIGTERM);
5992 static void signal_handler(int sig)
5994 /* signal handler is light as a feather, as it should be.
5995 * we will poll g_signal_pending with each loop of conky
5996 * and do any signal processing there, NOT here (except
5997 * SIGALRM because this is caused when conky is hanging) */
5998 if(sig == SIGALRM) {
6001 g_signal_pending = sig;