1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
35 #include "timed_thread.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
74 #include <curl/curl.h>
94 #include "iconv_tools.h"
100 #include "net_stat.h"
104 #include "read_tcp.h"
106 #include "specials.h"
107 #include "temphelper.h"
108 #include "template.h"
109 #include "tailhead.h"
110 #include "timeinfo.h"
113 /* check for OS and include appropriate headers */
114 #if defined(__linux__)
116 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
118 #elif defined(__OpenBSD__)
122 #if defined(__FreeBSD_kernel__)
126 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
127 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
128 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
129 int apm_getinfo(int fd, apm_info_t aip);
130 char *get_apm_adapter(void);
131 char *get_apm_battery_life(void);
132 char *get_apm_battery_time(void);
136 #include "defconfig.h"
137 #include "conf_cookie.h"
141 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
144 #define MAIL_FILE "$MAIL"
145 #define MAX_IF_BLOCK_DEPTH 5
147 //#define SIGNAL_BLOCKING
148 #undef SIGNAL_BLOCKING
150 /* debugging level, used by logging.h */
151 int global_debug_level = 0;
153 /* disable inotify auto reload feature if desired */
154 int disable_auto_reload = 0;
156 /* two strings for internal use */
157 static char *tmpstring1, *tmpstring2;
159 /* variables holding various config settings */
161 int format_human_readable;
168 int top_cpu, top_mem, top_time;
174 static int extra_newline;
175 enum x_initialiser_state x_initialised = NO;
176 static volatile int g_signal_pending;
177 /* Update interval */
178 double update_interval;
179 double update_interval_old;
180 double update_interval_bat;
181 int update_heartbeat_battery_skip = 1;
182 int update_skips_when_sleeping = 5;
183 int update_heartbeat_min = 0;
184 int update_heartbeat_max = 0;
185 void *global_cpu = NULL;
186 unsigned int max_text_width = 0;
191 /* prototypes for internally used functions */
192 static void signal_handler(int);
193 static void print_version(void) __attribute__((noreturn));
194 static void reload_config(void);
196 static void print_version(void)
198 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
200 printf("\nCompiled in features:\n\n"
201 "System config file: "SYSTEM_CONFIG_FILE"\n"
202 "Package library path: "PACKAGE_LIBDIR"\n\n"
206 " * Xdamage extension\n"
207 # endif /* HAVE_XDAMAGE */
209 " * XDBE (double buffer extension)\n"
210 # endif /* HAVE_XDBE */
216 # endif /* USE_ARGB */
218 "\n Music detection:\n"
221 #endif /* AUDACIOUS */
237 #endif /* HAVE_OPENMP */
244 #ifdef TCP_PORT_MONITOR
246 #endif /* TCP_PORT_MONITOR */
249 #endif /* HAVE_CURL */
254 " * Weather (METAR)\n"
256 " * Weather (XOAP)\n"
261 #endif /* HAVE_IWLIB */
263 " * support for IBM/Lenovo notebooks\n"
273 #endif /* CONFIG_OUTPUT */
289 #ifdef HAVE_LUA_CAIRO
291 #endif /* HAVE_LUA_CAIRO */
292 #ifdef HAVE_LUA_IMLIB2
295 #endif /* HAVE_LUA */
301 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
306 static void X11_create_window(void);
307 static void X11_initialisation(void);
309 struct _x11_stuff_s {
313 XserverRegion region2, part;
314 int event_base, error_base;
320 static int text_start_x, text_start_y; /* text start position in window */
321 static int text_width, text_height;
338 /* display to connect to */
339 static char *disp = NULL;
343 /* struct that has all info to be shared between
344 * instances of the same text object */
345 struct information info;
347 /* path to config file */
348 char *current_config;
350 /* set to 1 if you want all text to be in uppercase */
351 static unsigned int stuff_in_uppercase;
353 /* Run how many times? */
354 static unsigned long total_run_times;
357 static int fork_to_background;
359 /* set to 0 after the first time conky is run, so we don't fork again after the
361 static int first_pass = 1;
363 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
365 /* filenames for output */
366 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
367 char *append_file = NULL; FILE *append_fpointer = NULL;
371 static int show_graph_scale;
372 static int show_graph_range;
374 /* Position on the screen */
375 static int text_alignment;
376 static int gap_x, gap_y;
379 static int draw_borders;
380 static int draw_graph_borders;
381 static int stippled_borders;
383 int get_stippled_borders(void)
385 return stippled_borders;
388 static int draw_shades, draw_outline;
390 long default_fg_color, default_bg_color, default_out_color;
392 /* create own window or draw stuff to root? */
393 static int set_transparent = 0;
396 static int own_window = 0;
397 static int background_colour = 0;
399 /* fixed size/pos is set if wm/user changes them */
400 static int fixed_size = 0, fixed_pos = 0;
403 static int minimum_width, minimum_height;
404 static int maximum_width;
409 static int sensor_device;
412 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
415 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
416 unsigned int max_user_text;
418 /* maximum size of individual text buffers, ie $exec buffer size */
419 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
424 /* no buffers in used memory? */
427 /* pad percentages to decimals? */
428 static int pad_percents = 0;
430 static char *global_text = 0;
432 char *get_global_text(void)
437 long global_text_lines;
439 static int total_updates;
440 static int updatereset;
442 void set_updatereset(int i)
447 int get_updatereset(void)
452 int check_contains(char *f, char *s)
455 FILE *where = open_file(f, 0);
460 while (fgets(buf1, 256, where)) {
461 if (strstr(buf1, s)) {
468 NORM_ERR("Could not open the file '%s'", f);
473 #define SECRIT_MULTILINE_CHAR '\x02'
475 int calc_text_width(const char *s)
477 size_t slen = strlen(s);
480 if ((output_methods & TO_X) == 0) {
490 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
491 (const FcChar8 *) s, slen, &gi);
493 XftTextExtents8(display, fonts[selected_font].xftfont,
494 (const FcChar8 *) s, slen, &gi);
500 return XTextWidth(fonts[selected_font].font, s, slen);
505 /* formatted text to render on screen, generated in generate_text(),
506 * drawn in draw_stuff() */
508 static char *text_buffer;
510 /* quite boring functions */
512 static inline void for_each_line(char *b, int f(char *, int))
515 int special_index = 0; /* specials index */
518 for (ps = b, pe = b; *pe; pe++) {
521 special_index = f(ps, special_index);
528 f(ps, special_index);
532 static void convert_escapes(char *buf)
534 char *p = buf, *s = buf;
541 } else if (*s == '\\') {
552 /* Prints anything normally printed with snprintf according to the current value
553 * of use_spacer. Actually slightly more flexible than snprintf, as you can
554 * safely specify the destination buffer as one of your inputs. */
555 int spaced_print(char *buf, int size, const char *format, int width, ...)
564 tempbuf = malloc(size * sizeof(char));
566 // Passes the varargs along to vsnprintf
567 va_start(argp, width);
568 vsnprintf(tempbuf, size, format, argp);
571 switch (use_spacer) {
573 len = snprintf(buf, size, "%s", tempbuf);
576 len = snprintf(buf, size, "%*s", width, tempbuf);
579 len = snprintf(buf, size, "%-*s", width, tempbuf);
586 /* print percentage values
588 * - i.e., unsigned values between 0 and 100
589 * - respect the value of pad_percents */
590 int percent_print(char *buf, int size, unsigned value)
592 return spaced_print(buf, size, "%u", pad_percents, value);
595 /* converts from bytes to human readable format (K, M, G, T)
597 * The algorithm always divides by 1024, as unit-conversion of byte
598 * counts suggests. But for output length determination we need to
599 * compare with 1000 here, as we print in decimal form. */
600 void human_readable(long long num, char *buf, int size)
602 const char **suffix = suffixes;
608 /* Possibly just output as usual, for example for stdout usage */
609 if (!format_human_readable) {
610 spaced_print(buf, size, "%d", 6, round_to_int(num));
613 if (short_units || llabs(num) < 1000LL) {
621 if (llabs(num) < 1000LL) {
622 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
626 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
634 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
636 * The goal is to always have a significance of 3, by
637 * adjusting the decimal part of the number. Sample output:
641 * so the point of alignment resides between number and unit. The
642 * upside of this is that there is minimal padding necessary, though
643 * there should be a way to make alignment take place at the decimal
644 * dot (then with fixed width decimal part).
646 * Note the repdigits below: when given a precision value, printf()
647 * rounds the float to it, not just cuts off the remaining digits. So
648 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
649 * printed with a precision of 0. Yay. */
651 precision = 0; /* print 100-999 without decimal part */
653 precision = 1; /* print 10-99 with one decimal place */
655 precision = 2; /* print 0-9 with two decimal places */
657 spaced_print(buf, size, format, width, precision, fnum, *suffix);
660 /* global object list root element */
661 static struct text_object global_root_object;
663 static long current_text_color;
665 void set_current_text_color(long colour)
667 current_text_color = colour;
670 long get_current_text_color(void)
672 return current_text_color;
675 //adds newstring to to the tree unless you can already see it when travelling back.
676 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
677 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
678 struct conftree* node;
679 struct conftree* node2;
681 for(node = previous; node != NULL; node = node->back) {
682 if(strcmp(node->string, newstring) == 0) {
686 node = malloc(sizeof(struct conftree));
687 if (previous != NULL) {
688 if(previous->vert_next == NULL) {
689 previous->vert_next = node;
691 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
692 node2->horz_next = node;
695 node->string = strdup(newstring);
696 node->horz_next = NULL;
697 node->vert_next = NULL;
698 node->back = previous;
702 void conftree_empty(struct conftree* tree) {
704 conftree_empty(tree->horz_next);
705 conftree_empty(tree->vert_next);
711 struct conftree *currentconffile;
713 static void extract_variable_text(const char *p)
715 free_text_objects(&global_root_object, 0);
729 extract_variable_text_internal(&global_root_object, p);
732 void parse_conky_vars(struct text_object *root, const char *txt,
733 char *p, int p_max_size, struct information *cur)
735 extract_variable_text_internal(root, txt);
736 generate_text_internal(p, p_max_size, *root, cur);
739 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
740 * multiline objects like $exec work with $align[rc] and friends
742 void substitute_newlines(char *p, long l)
746 while (p && *p && p < s + l) {
748 /* only substitute if it's not the last newline */
749 *p = SECRIT_MULTILINE_CHAR;
755 void generate_text_internal(char *p, int p_max_size,
756 struct text_object root, struct information *cur)
758 struct text_object *obj;
760 int need_to_load_fonts = 0;
763 /* for the OBJ_top* handler */
764 struct process **needed = 0;
767 char buff_in[p_max_size];
769 #endif /* HAVE_ICONV */
775 while (obj && p_max_size > 0) {
776 needed = 0; /* reset for top stuff */
778 /* IFBLOCK jumping algorithm
780 * This is easier as it looks like:
781 * - each IF checks it's condition
782 * - on FALSE: call DO_JUMP
783 * - on TRUE: don't care
784 * - each ELSE calls DO_JUMP unconditionally
785 * - each ENDIF is silently being ignored
788 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
789 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
790 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
791 * regularly, "obj" is being updated to point to obj->next, so object parsing
792 * continues right after the corresponding ELSE or ENDIF. This means that if we
793 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
794 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
795 * jumped, and there is nothing to do.
799 if (obj->ifblock_next) \
800 obj = obj->ifblock_next; \
803 #define OBJ(a) break; case OBJ_##a:
807 NORM_ERR("not implemented obj type %d", obj->type);
809 print_read_tcp(obj, p, p_max_size);
813 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
815 #endif /* !__OpenBSD__ */
819 ok = get_freq(p, p_max_size, "%.0f", 1,
827 ok = get_freq(p, p_max_size, "%'.2f", 1000,
830 /* OpenBSD has no such flag (SUSv2) */
831 ok = get_freq(p, p_max_size, "%.2f", 1000,
833 #endif /* __OpenBSD */
836 #if defined(__linux__)
838 print_voltage_mv(obj, p, p_max_size);
841 print_voltage_v(obj, p, p_max_size);
845 OBJ(wireless_essid) {
846 print_wireless_essid(obj, p, p_max_size);
849 print_wireless_mode(obj, p, p_max_size);
851 OBJ(wireless_bitrate) {
852 print_wireless_bitrate(obj, p, p_max_size);
855 print_wireless_ap(obj, p, p_max_size);
857 OBJ(wireless_link_qual) {
858 print_wireless_link_qual(obj, p, p_max_size);
860 OBJ(wireless_link_qual_max) {
861 print_wireless_link_qual_max(obj, p, p_max_size);
863 OBJ(wireless_link_qual_perc) {
864 print_wireless_link_qual_perc(obj, p, p_max_size);
866 OBJ(wireless_link_bar) {
867 print_wireless_link_bar(obj, p, p_max_size);
869 #endif /* HAVE_IWLIB */
871 #endif /* __linux__ */
875 get_acpi_fan(p, p_max_size);
878 get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque);
881 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
884 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
887 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_VOLTS);
890 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TEMP);
893 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_RATE);
895 OBJ(battery_percent) {
896 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
899 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
902 get_battery_short_status(p, p_max_size, obj->data.s);
904 OBJ(cell_radio_dbm) {
905 get_dbus_stuff(p, p_max_size, DBUS_CELL_DBM);
907 OBJ(cell_radio_percent) {
908 get_dbus_stuff(p, p_max_size, DBUS_CELL_PERCENT);
910 #endif /* __OpenBSD__ */
913 human_readable(cur->buffers * 1024, p, 255);
916 human_readable(cur->cached * 1024, p, 255);
918 OBJ(cmdline_to_pid) {
919 print_cmdline_to_pid(obj, p, p_max_size);
922 if (cur->cpu_usage) {
923 if (obj->data.i > info.cpu_count) {
924 NORM_ERR("obj->data.i %i info.cpu_count %i",
925 obj->data.i, info.cpu_count);
926 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
928 percent_print(p, p_max_size,
929 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
933 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
935 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
939 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
942 print_loadgraph(obj, p, p_max_size);
946 new_fg(p, obj->data.l);
980 #if defined(__linux__)
982 snprintf(p, p_max_size, "%s",
983 get_disk_protect_queue(obj->data.s));
986 print_i8k_version(obj, p, p_max_size);
989 print_i8k_bios(obj, p, p_max_size);
992 print_i8k_serial(obj, p, p_max_size);
995 print_i8k_cpu_temp(obj, p, p_max_size);
997 OBJ(i8k_left_fan_status) {
998 print_i8k_left_fan_status(obj, p, p_max_size);
1000 OBJ(i8k_right_fan_status) {
1001 print_i8k_right_fan_status(obj, p, p_max_size);
1003 OBJ(i8k_left_fan_rpm) {
1004 print_i8k_left_fan_rpm(obj, p, p_max_size);
1006 OBJ(i8k_right_fan_rpm) {
1007 print_i8k_right_fan_rpm(obj, p, p_max_size);
1009 OBJ(i8k_ac_status) {
1010 print_i8k_ac_status(obj, p, p_max_size);
1012 OBJ(i8k_buttons_status) {
1013 print_i8k_buttons_status(obj, p, p_max_size);
1017 get_ibm_acpi_fan(obj, p, p_max_size);
1020 print_ibm_temps(obj, p, p_max_size);
1023 get_ibm_acpi_volume(obj, p, p_max_size);
1025 OBJ(ibm_brightness) {
1026 get_ibm_acpi_brightness(obj, p, p_max_size);
1029 /* information from sony_laptop kernel module
1030 * /sys/devices/platform/sony-laptop */
1031 OBJ(sony_fanspeed) {
1032 get_sony_fanspeed(p, p_max_size);
1035 if (!gateway_exists()) {
1040 print_gateway_iface(p, p_max_size);
1043 print_gateway_ip(p, p_max_size);
1046 snprintf(p, p_max_size, "%d", get_laptop_mode());
1049 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1051 #endif /* __linux__ */
1052 #if (defined(__FreeBSD__) || defined(__linux__))
1054 if (!interface_up(obj)) {
1060 OBJ(obsd_sensors_temp) {
1061 print_obsd_sensors_temp(obj, p, p_max_size);
1063 OBJ(obsd_sensors_fan) {
1064 print_obsd_sensors_fan(obj, p, p_max_size);
1066 OBJ(obsd_sensors_volt) {
1067 print_obsd_sensors_volt(obj, p, p_max_size);
1070 get_obsd_vendor(p, p_max_size);
1073 get_obsd_product(p, p_max_size);
1075 #endif /* __OpenBSD__ */
1078 new_font(p, obj->data.s);
1079 need_to_load_fonts = 1;
1083 print_diskio(obj, p, p_max_size);
1086 print_diskio_write(obj, p, p_max_size);
1089 print_diskio_read(obj, p, p_max_size);
1093 print_diskiograph(obj, p, p_max_size);
1095 OBJ(diskiograph_read) {
1096 print_diskiograph_read(obj, p, p_max_size);
1098 OBJ(diskiograph_write) {
1099 print_diskiograph_write(obj, p, p_max_size);
1103 print_downspeed(obj, p, p_max_size);
1106 print_downspeedf(obj, p, p_max_size);
1109 OBJ(downspeedgraph) {
1110 print_downspeedgraph(obj, p, p_max_size);
1114 /* Since we see you, you're if has not jumped.
1115 * Do Ninja jump here: without leaving traces.
1116 * This is to prevent us from stale jumped flags.
1118 obj = obj->ifblock_next;
1122 /* harmless object, just ignore */
1125 print_addr(obj, p, p_max_size);
1127 #if defined(__linux__)
1129 print_addrs(obj, p, p_max_size);
1131 #endif /* __linux__ */
1132 #if defined(IMLIB2) && defined(X11)
1134 /* doesn't actually draw anything, just queues it omp. the
1135 * image will get drawn after the X event loop */
1136 cimlib_add_image(obj->data.s);
1140 evaluate(obj->data.s, p, p_max_size);
1143 print_exec(obj, p, p_max_size);
1146 print_execp(obj, p, p_max_size);
1149 print_execgauge(obj, p, p_max_size);
1152 print_execbar(obj, p, p_max_size);
1156 print_execgraph(obj, p, p_max_size);
1160 print_execibar(obj, p, p_max_size);
1164 print_execigraph(obj, p, p_max_size);
1168 print_execigauge(obj, p, p_max_size);
1171 print_execi(obj, p, p_max_size);
1174 print_execpi(obj, p, p_max_size);
1177 print_texeci(obj, p, p_max_size);
1180 print_imap_unseen(obj, p, p_max_size);
1182 OBJ(imap_messages) {
1183 print_imap_messages(obj, p, p_max_size);
1186 print_pop3_unseen(obj, p, p_max_size);
1189 print_pop3_used(obj, p, p_max_size);
1192 print_fs_bar(obj, 0, p, p_max_size);
1195 print_fs_free(obj, p, p_max_size);
1198 print_fs_perc(obj, 1, p, p_max_size);
1201 print_fs_size(obj, p, p_max_size);
1204 print_fs_type(obj, p, p_max_size);
1207 print_fs_used(obj, p, p_max_size);
1210 print_fs_bar(obj, 1, p, p_max_size);
1213 print_fs_perc(obj, 0, p, p_max_size);
1216 print_loadavg(obj, p, p_max_size);
1219 new_goto(p, obj->data.i);
1226 new_hr(p, obj->data.i);
1230 print_nameserver(obj, p, p_max_size);
1234 print_eve(obj, p, p_max_size);
1239 curl_print(obj, p, p_max_size);
1244 rss_print_info(obj, p, p_max_size);
1249 print_weather(obj, p, p_max_size);
1253 OBJ(weather_forecast) {
1254 print_weather_forecast(obj, p, p_max_size);
1259 print_lua(obj, p, p_max_size);
1262 print_lua_parse(obj, p, p_max_size);
1265 print_lua_bar(obj, p, p_max_size);
1269 print_lua_graph(obj, p, p_max_size);
1273 print_lua_gauge(obj, p, p_max_size);
1275 #endif /* HAVE_LUA */
1281 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1282 snprintf(p, p_max_size, "N/A");
1284 temp_print(p, p_max_size, (double)val,
1285 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1290 new_offset(p, obj->data.i);
1293 new_voffset(p, obj->data.i);
1297 print_sysfs_sensor(obj, p, p_max_size);
1300 print_sysfs_sensor(obj, p, p_max_size);
1303 print_sysfs_sensor(obj, p, p_max_size);
1305 #endif /* __linux__ */
1307 new_alignr(p, obj->data.i);
1310 new_alignc(p, obj->data.i);
1313 char buf[max_user_text];
1314 struct information *tmp_info =
1315 malloc(sizeof(struct information));
1316 memcpy(tmp_info, cur, sizeof(struct information));
1317 generate_text_internal(buf, max_user_text,
1318 *obj->sub, tmp_info);
1320 if (strlen(buf) != 0) {
1326 char expression[max_user_text];
1328 struct information *tmp_info;
1330 tmp_info = malloc(sizeof(struct information));
1331 memcpy(tmp_info, cur, sizeof(struct information));
1332 generate_text_internal(expression, max_user_text,
1333 *obj->sub, tmp_info);
1334 DBGP("parsed arg into '%s'", expression);
1336 val = compare(expression);
1338 NORM_ERR("compare failed for expression '%s'",
1348 spc = strchr(obj->data.s, ' ');
1349 if (!spc && access(obj->data.s, F_OK)) {
1353 if (!check_contains(obj->data.s, spc + 1))
1360 && (!check_mount(obj->data.s))) {
1366 if (!get_process_by_name(obj->data.s)) {
1368 if ((obj->data.s) && system(obj->data.s)) {
1373 #if defined(__linux__)
1375 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1379 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1382 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1387 human_readable(cur->mem * 1024, p, 255);
1390 human_readable(cur->memeasyfree * 1024, p, 255);
1393 human_readable(cur->memfree * 1024, p, 255);
1396 human_readable(cur->memmax * 1024, p, 255);
1400 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1403 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1406 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1410 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1415 print_mixer(obj, p, p_max_size);
1418 print_mixerl(obj, p, p_max_size);
1421 print_mixerr(obj, p, p_max_size);
1424 print_mixer_bar(obj, p, p_max_size);
1427 print_mixerl_bar(obj, p, p_max_size);
1430 print_mixerr_bar(obj, p, p_max_size);
1432 OBJ(if_mixer_mute) {
1433 if (!check_mixer_muted(obj)) {
1438 #define NOT_IN_X "Not running in X"
1440 if(x_initialised != YES) {
1441 strncpy(p, NOT_IN_X, p_max_size);
1443 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1446 OBJ(monitor_number) {
1447 if(x_initialised != YES) {
1448 strncpy(p, NOT_IN_X, p_max_size);
1450 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1454 if(x_initialised != YES) {
1455 strncpy(p, NOT_IN_X, p_max_size);
1457 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1460 OBJ(desktop_number) {
1461 if(x_initialised != YES) {
1462 strncpy(p, NOT_IN_X, p_max_size);
1464 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1468 if(x_initialised != YES) {
1469 strncpy(p, NOT_IN_X, p_max_size);
1470 }else if(cur->x11.desktop.name != NULL) {
1471 strncpy(p, cur->x11.desktop.name, p_max_size);
1477 char buf[max_user_text];
1479 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1481 print_format_time(obj, p, p_max_size);
1485 print_mails(obj, p, p_max_size);
1488 print_new_mails(obj, p, p_max_size);
1491 print_seen_mails(obj, p, p_max_size);
1494 print_unseen_mails(obj, p, p_max_size);
1496 OBJ(flagged_mails) {
1497 print_flagged_mails(obj, p, p_max_size);
1499 OBJ(unflagged_mails) {
1500 print_unflagged_mails(obj, p, p_max_size);
1502 OBJ(forwarded_mails) {
1503 print_forwarded_mails(obj, p, p_max_size);
1505 OBJ(unforwarded_mails) {
1506 print_unforwarded_mails(obj, p, p_max_size);
1508 OBJ(replied_mails) {
1509 print_replied_mails(obj, p, p_max_size);
1511 OBJ(unreplied_mails) {
1512 print_unreplied_mails(obj, p, p_max_size);
1515 print_draft_mails(obj, p, p_max_size);
1517 OBJ(trashed_mails) {
1518 print_trashed_mails(obj, p, p_max_size);
1521 print_mboxscan(obj, p, p_max_size);
1524 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1526 OBJ(nodename_short) {
1528 pos = strstr(cur->uname_s.nodename, ".");
1530 snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1532 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1536 new_outline(p, obj->data.l);
1539 char buf[max_user_text];
1541 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1543 print_pid_chroot(obj, p, p_max_size);
1546 char buf[max_user_text];
1548 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1550 print_pid_cmdline(obj, p, p_max_size);
1553 char buf[max_user_text];
1555 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1557 print_pid_cwd(obj, p, p_max_size);
1560 char buf[max_user_text];
1562 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1564 print_pid_environ(obj, p, p_max_size);
1566 OBJ(pid_environ_list) {
1567 char buf[max_user_text];
1569 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1571 print_pid_environ_list(obj, p, p_max_size);
1574 char buf[max_user_text];
1576 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1578 print_pid_exe(obj, p, p_max_size);
1581 char buf[max_user_text];
1583 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1585 print_pid_nice(obj, p, p_max_size);
1587 OBJ(pid_openfiles) {
1588 char buf[max_user_text];
1590 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1592 print_pid_openfiles(obj, p, p_max_size);
1595 char buf[max_user_text];
1597 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1599 print_pid_parent(obj, p, p_max_size);
1602 char buf[max_user_text];
1604 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1606 print_pid_priority(obj, p, p_max_size);
1609 char buf[max_user_text];
1611 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1613 print_pid_state(obj, p, p_max_size);
1615 OBJ(pid_state_short) {
1616 char buf[max_user_text];
1618 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1620 print_pid_state_short(obj, p, p_max_size);
1623 char buf[max_user_text];
1625 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1627 print_pid_stderr(obj, p, p_max_size);
1630 char buf[max_user_text];
1632 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1634 print_pid_stdin(obj, p, p_max_size);
1637 char buf[max_user_text];
1639 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1641 print_pid_stdout(obj, p, p_max_size);
1644 char buf[max_user_text];
1646 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1648 print_pid_threads(obj, p, p_max_size);
1650 OBJ(pid_thread_list) {
1651 char buf[max_user_text];
1653 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1655 print_pid_thread_list(obj, p, p_max_size);
1657 OBJ(pid_time_kernelmode) {
1658 char buf[max_user_text];
1660 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1662 print_pid_time_kernelmode(obj, p, p_max_size);
1664 OBJ(pid_time_usermode) {
1665 char buf[max_user_text];
1667 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1669 print_pid_time_usermode(obj, p, p_max_size);
1672 char buf[max_user_text];
1674 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1676 print_pid_time(obj, p, p_max_size);
1679 char buf[max_user_text];
1681 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1683 print_pid_uid(obj, p, p_max_size);
1686 char buf[max_user_text];
1688 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1690 print_pid_euid(obj, p, p_max_size);
1693 char buf[max_user_text];
1695 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1697 print_pid_suid(obj, p, p_max_size);
1700 char buf[max_user_text];
1702 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1704 print_pid_fsuid(obj, p, p_max_size);
1707 char buf[max_user_text];
1709 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1711 print_pid_gid(obj, p, p_max_size);
1714 char buf[max_user_text];
1716 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1718 print_pid_egid(obj, p, p_max_size);
1721 char buf[max_user_text];
1723 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1725 print_pid_sgid(obj, p, p_max_size);
1728 char buf[max_user_text];
1730 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1732 print_pid_fsgid(obj, p, p_max_size);
1735 char buf[max_user_text];
1737 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1739 print_pid_read(obj, p, p_max_size);
1742 char buf[max_user_text];
1744 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1746 print_pid_vmpeak(obj, p, p_max_size);
1749 char buf[max_user_text];
1751 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1753 print_pid_vmsize(obj, p, p_max_size);
1756 char buf[max_user_text];
1758 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1760 print_pid_vmlck(obj, p, p_max_size);
1763 char buf[max_user_text];
1765 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1767 print_pid_vmhwm(obj, p, p_max_size);
1770 char buf[max_user_text];
1772 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1774 print_pid_vmrss(obj, p, p_max_size);
1777 char buf[max_user_text];
1779 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1781 print_pid_vmdata(obj, p, p_max_size);
1784 char buf[max_user_text];
1786 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1788 print_pid_vmstk(obj, p, p_max_size);
1791 char buf[max_user_text];
1793 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1795 print_pid_vmexe(obj, p, p_max_size);
1798 char buf[max_user_text];
1800 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1802 print_pid_vmlib(obj, p, p_max_size);
1805 char buf[max_user_text];
1807 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1809 print_pid_vmpte(obj, p, p_max_size);
1812 char buf[max_user_text];
1814 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1816 print_pid_write(obj, p, p_max_size);
1819 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1821 OBJ(running_processes) {
1822 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1824 OBJ(running_threads) {
1825 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1828 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1831 snprintf(p, p_max_size, "%s", obj->data.s);
1835 new_bg(p, obj->data.l);
1838 new_stippled_hr(obj, p);
1842 human_readable(cur->swap * 1024, p, 255);
1845 human_readable(cur->swapfree * 1024, p, 255);
1848 human_readable(cur->swapmax * 1024, p, 255);
1851 if (cur->swapmax == 0) {
1852 strncpy(p, "No swap", p_max_size);
1854 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1858 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1861 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1864 print_time(obj, p, p_max_size);
1867 print_utime(obj, p, p_max_size);
1870 print_tztime(obj, p, p_max_size);
1873 print_totaldown(obj, p, p_max_size);
1876 print_totalup(obj, p, p_max_size);
1879 char buf[max_user_text];
1881 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1883 print_gid_name(obj, p, p_max_size);
1886 char buf[max_user_text];
1888 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1890 print_uid_name(obj, p, p_max_size);
1893 snprintf(p, p_max_size, "%d", total_updates);
1896 if(total_updates % updatereset != obj->data.i - 1) {
1901 print_upspeed(obj, p, p_max_size);
1904 print_upspeedf(obj, p, p_max_size);
1908 print_upspeedgraph(obj, p, p_max_size);
1912 format_seconds_short(p, p_max_size, (int) cur->uptime);
1915 format_seconds(p, p_max_size, (int) cur->uptime);
1919 snprintf(p, p_max_size, "%s", cur->users.names);
1922 snprintf(p, p_max_size, "%s", cur->users.terms);
1925 snprintf(p, p_max_size, "%s", cur->users.times);
1928 update_user_time(obj->data.s);
1929 snprintf(p, p_max_size, "%s", cur->users.ctime);
1932 snprintf(p, p_max_size, "%d", cur->users.number);
1934 #endif /* __linux__ */
1935 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1936 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1940 msg = get_apm_adapter();
1941 snprintf(p, p_max_size, "%s", msg);
1944 OBJ(apm_battery_life) {
1947 msg = get_apm_battery_life();
1948 snprintf(p, p_max_size, "%s", msg);
1951 OBJ(apm_battery_time) {
1954 msg = get_apm_battery_time();
1955 snprintf(p, p_max_size, "%s", msg);
1958 #endif /* __FreeBSD__ __OpenBSD__ */
1962 print_mpd_title(obj, p, p_max_size);
1964 print_mpd_artist(obj, p, p_max_size);
1966 print_mpd_album(obj, p, p_max_size);
1968 print_mpd_random(obj, p, p_max_size);
1970 print_mpd_repeat(obj, p, p_max_size);
1972 print_mpd_track(obj, p, p_max_size);
1974 print_mpd_name(obj, p, p_max_size);
1976 print_mpd_file(obj, p, p_max_size);
1978 print_mpd_vol(obj, p, p_max_size);
1980 print_mpd_bitrate(obj, p, p_max_size);
1982 print_mpd_status(obj, p, p_max_size);
1984 print_mpd_elapsed(obj, p, p_max_size);
1987 print_mpd_length(obj, p, p_max_size);
1990 print_mpd_percent(obj, p, p_max_size);
1993 print_mpd_bar(obj, p, p_max_size);
1996 print_mpd_smart(obj, p, p_max_size);
1998 OBJ(if_mpd_playing) {
1999 if (!mpd_get_info()->is_playing) {
2006 #define MOC_PRINT(t, a) \
2007 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2009 MOC_PRINT(state, "??");
2012 MOC_PRINT(file, "no file");
2015 MOC_PRINT(title, "no title");
2018 MOC_PRINT(artist, "no artist");
2021 MOC_PRINT(song, "no song");
2024 MOC_PRINT(album, "no album");
2026 OBJ(moc_totaltime) {
2027 MOC_PRINT(totaltime, "0:00");
2030 MOC_PRINT(timeleft, "0:00");
2033 MOC_PRINT(curtime, "0:00");
2036 MOC_PRINT(bitrate, "0Kbps");
2039 MOC_PRINT(rate, "0KHz");
2045 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2048 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2051 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2054 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2056 OBJ(xmms2_comment) {
2057 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2060 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2063 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2066 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2068 OBJ(xmms2_tracknr) {
2069 if (cur->xmms2.tracknr != -1) {
2070 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2073 OBJ(xmms2_bitrate) {
2074 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2077 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2080 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2082 OBJ(xmms2_elapsed) {
2083 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2084 (cur->xmms2.elapsed / 1000) % 60);
2086 OBJ(xmms2_duration) {
2087 snprintf(p, p_max_size, "%02d:%02d",
2088 cur->xmms2.duration / 60000,
2089 (cur->xmms2.duration / 1000) % 60);
2091 OBJ(xmms2_percent) {
2092 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2096 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2099 OBJ(xmms2_playlist) {
2100 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2102 OBJ(xmms2_timesplayed) {
2103 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2106 if (strlen(cur->xmms2.title) < 2
2107 && strlen(cur->xmms2.title) < 2) {
2108 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2110 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2114 OBJ(if_xmms2_connected) {
2115 if (cur->xmms2.conn_state != 1) {
2121 OBJ(audacious_status) {
2122 snprintf(p, p_max_size, "%s",
2123 cur->audacious.items[AUDACIOUS_STATUS]);
2125 OBJ(audacious_title) {
2126 snprintf(p, cur->audacious.max_title_len > 0
2127 ? cur->audacious.max_title_len : p_max_size, "%s",
2128 cur->audacious.items[AUDACIOUS_TITLE]);
2130 OBJ(audacious_length) {
2131 snprintf(p, p_max_size, "%s",
2132 cur->audacious.items[AUDACIOUS_LENGTH]);
2134 OBJ(audacious_length_seconds) {
2135 snprintf(p, p_max_size, "%s",
2136 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2138 OBJ(audacious_position) {
2139 snprintf(p, p_max_size, "%s",
2140 cur->audacious.items[AUDACIOUS_POSITION]);
2142 OBJ(audacious_position_seconds) {
2143 snprintf(p, p_max_size, "%s",
2144 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2146 OBJ(audacious_bitrate) {
2147 snprintf(p, p_max_size, "%s",
2148 cur->audacious.items[AUDACIOUS_BITRATE]);
2150 OBJ(audacious_frequency) {
2151 snprintf(p, p_max_size, "%s",
2152 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2154 OBJ(audacious_channels) {
2155 snprintf(p, p_max_size, "%s",
2156 cur->audacious.items[AUDACIOUS_CHANNELS]);
2158 OBJ(audacious_filename) {
2159 snprintf(p, p_max_size, "%s",
2160 cur->audacious.items[AUDACIOUS_FILENAME]);
2162 OBJ(audacious_playlist_length) {
2163 snprintf(p, p_max_size, "%s",
2164 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2166 OBJ(audacious_playlist_position) {
2167 snprintf(p, p_max_size, "%s",
2168 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2170 OBJ(audacious_main_volume) {
2171 snprintf(p, p_max_size, "%s",
2172 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2175 OBJ(audacious_bar) {
2179 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2180 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2181 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2184 #endif /* AUDACIOUS */
2188 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2191 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2194 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2197 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2200 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2203 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2206 /* we have four different types of top (top, top_mem,
2207 * top_time and top_io). To avoid having almost-same code four
2208 * times, we have this special handler. */
2216 print_top(obj, p, p_max_size);
2218 print_tailhead("tail", obj, p, p_max_size);
2221 print_tailhead("head", obj, p, p_max_size);
2224 print_lines(obj, p, p_max_size);
2228 print_words(obj, p, p_max_size);
2230 #ifdef TCP_PORT_MONITOR
2232 tcp_portmon_action(obj, p, p_max_size);
2234 #endif /* TCP_PORT_MONITOR */
2238 do_iconv_start(obj);
2243 #endif /* HAVE_ICONV */
2245 OBJ(entropy_avail) {
2246 print_entropy_avail(obj, p, p_max_size);
2249 print_entropy_perc(obj, p, p_max_size);
2251 OBJ(entropy_poolsize) {
2252 print_entropy_poolsize(obj, p, p_max_size);
2255 print_entropy_bar(obj, p, p_max_size);
2259 print_smapi(obj, p, p_max_size);
2261 OBJ(if_smapi_bat_installed) {
2263 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2264 if(!smapi_bat_installed(idx)) {
2268 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2270 OBJ(smapi_bat_perc) {
2271 print_smapi_bat_perc(obj, p, p_max_size);
2273 OBJ(smapi_bat_temp) {
2274 print_smapi_bat_temp(obj, p, p_max_size);
2276 OBJ(smapi_bat_power) {
2277 print_smapi_bat_power(obj, p, p_max_size);
2279 OBJ(smapi_bat_bar) {
2280 print_smapi_bat_bar(obj, p, p_max_size);
2285 char buf[max_user_text];
2287 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2288 snprintf(p, p_max_size, "%s", buf);
2294 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2295 char buf[max_user_text];
2298 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2299 snprintf(p, p_max_size, "%s", buf);
2300 if(total_updates % 2) {
2301 for(j=0; p[j] != 0; j++) {
2306 OBJ(to_bytes) {//commented code is original - not working when input value contains a decimal point
2307 char buf[max_user_text];
2310 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2312 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2313 // if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2314 // if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2315 // else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2316 // else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2317 // else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2318 // else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2320 if(sscanf(buf, "%Lf%s", &bytes, unit) == 2 && strlen(unit) < 16){
2321 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes);
2322 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024);
2323 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024);
2324 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024 * 1024);
2325 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024 * 1024 * 1024);
2327 snprintf(p, p_max_size, "%s", buf);
2330 print_scroll(obj, p, p_max_size, cur);
2333 print_combine(obj, p, cur);
2337 print_nvidia_value(obj, p, p_max_size);
2342 /* This is just a meta-object to set host:port */
2345 snprintf(p, p_max_size, "%s",
2346 cur->apcupsd.items[APCUPSD_NAME]);
2348 OBJ(apcupsd_model) {
2349 snprintf(p, p_max_size, "%s",
2350 cur->apcupsd.items[APCUPSD_MODEL]);
2352 OBJ(apcupsd_upsmode) {
2353 snprintf(p, p_max_size, "%s",
2354 cur->apcupsd.items[APCUPSD_UPSMODE]);
2356 OBJ(apcupsd_cable) {
2357 snprintf(p, p_max_size, "%s",
2358 cur->apcupsd.items[APCUPSD_CABLE]);
2360 OBJ(apcupsd_status) {
2361 snprintf(p, p_max_size, "%s",
2362 cur->apcupsd.items[APCUPSD_STATUS]);
2364 OBJ(apcupsd_linev) {
2365 snprintf(p, p_max_size, "%s",
2366 cur->apcupsd.items[APCUPSD_LINEV]);
2369 snprintf(p, p_max_size, "%s",
2370 cur->apcupsd.items[APCUPSD_LOAD]);
2372 OBJ(apcupsd_loadbar) {
2374 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2375 new_bar(obj, p, p_max_size, (int) progress);
2378 OBJ(apcupsd_loadgraph) {
2380 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2381 new_graph(obj, p, p_max_size, (int)progress);
2384 OBJ(apcupsd_loadgauge) {
2386 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2387 new_gauge(obj, p, p_max_size, (int)progress);
2389 OBJ(apcupsd_charge) {
2390 snprintf(p, p_max_size, "%s",
2391 cur->apcupsd.items[APCUPSD_CHARGE]);
2393 OBJ(apcupsd_timeleft) {
2394 snprintf(p, p_max_size, "%s",
2395 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2398 snprintf(p, p_max_size, "%s",
2399 cur->apcupsd.items[APCUPSD_TEMP]);
2401 OBJ(apcupsd_lastxfer) {
2402 snprintf(p, p_max_size, "%s",
2403 cur->apcupsd.items[APCUPSD_LASTXFER]);
2405 #endif /* APCUPSD */
2412 size_t a = strlen(p);
2415 iconv_convert(&a, buff_in, p, p_max_size);
2416 #endif /* HAVE_ICONV */
2417 if (obj->type == OBJ_execp || obj->type == OBJ_execpi || obj->type
2420 || obj->type == OBJ_lua || obj->type == OBJ_lua_parse
2421 #endif /* HAVE_LUA */
2423 substitute_newlines(p, a - 2);
2432 /* load any new fonts we may have had */
2433 if (need_to_load_fonts) {
2439 void evaluate(const char *text, char *p, int p_max_size)
2441 struct information *tmp_info;
2442 struct text_object subroot;
2444 tmp_info = malloc(sizeof(struct information));
2445 memcpy(tmp_info, &info, sizeof(struct information));
2446 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2447 DBGP2("evaluated '%s' to '%s'", text, p);
2449 free_text_objects(&subroot, 1);
2453 double current_update_time, next_update_time, last_update_time;
2455 static void generate_text(void)
2457 struct information *cur = &info;
2459 unsigned int i, j, k;
2465 current_update_time = get_time();
2469 /* add things to the buffer */
2475 generate_text_internal(p, max_user_text, global_root_object, cur);
2476 if(max_text_width > 0) {
2477 for(i = 0, j = 0; p[i] != 0; i++) {
2478 if(p[i] == '\n') j = 0;
2479 else if(j == max_text_width) {
2480 k = i + strlen(p + i) + 1;
2481 if(k < text_buffer_size) {
2488 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2493 if (stuff_in_uppercase) {
2496 tmp_p = text_buffer;
2498 *tmp_p = toupper(*tmp_p);
2503 next_update_time += update_interval;
2504 if (next_update_time < get_time()) {
2505 next_update_time = get_time() + update_interval;
2506 } else if (next_update_time > get_time() + update_interval) {
2507 next_update_time = get_time() + update_interval;
2509 last_update_time = current_update_time;
2513 void set_update_interval(double interval)
2515 update_interval = interval;
2516 update_interval_old = interval;
2519 int get_string_width(const char *s)
2521 return *s ? calc_text_width(s) : 0;
2525 static int get_string_width_special(char *s, int special_index)
2535 if ((output_methods & TO_X) == 0)
2538 p = strndup(s, text_buffer_size);
2542 if (*p == SPECIAL_CHAR) {
2543 /* shift everything over by 1 so that the special char
2544 * doesn't mess up the size calculation */
2545 for (i = 0; i < (long)strlen(p); i++) {
2546 *(p + i) = *(p + i + 1);
2548 if (specials[special_index + idx].type == GRAPH
2549 || specials[special_index + idx].type == GAUGE
2550 || specials[special_index + idx].type == BAR) {
2551 width += specials[special_index + idx].width;
2554 } else if (*p == SECRIT_MULTILINE_CHAR) {
2561 if (strlen(final) > 1) {
2562 width += calc_text_width(final);
2568 static int text_size_updater(char *s, int special_index);
2570 int last_font_height;
2571 static void update_text_area(void)
2575 if ((output_methods & TO_X) == 0)
2577 /* update text size if it isn't fixed */
2582 text_width = minimum_width;
2584 last_font_height = font_height();
2585 for_each_line(text_buffer, text_size_updater);
2587 if (text_height < minimum_height) {
2588 text_height = minimum_height;
2590 if (text_width > maximum_width && maximum_width > 0) {
2591 text_width = maximum_width;
2595 /* get text position on workarea */
2596 switch (text_alignment) {
2597 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2601 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2602 y = workarea[3] - text_height - gap_y;
2605 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2606 y = workarea[3] / 2 - text_height / 2 - gap_y;
2609 switch (text_alignment) {
2610 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2614 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2615 x = workarea[2] - text_width - gap_x;
2618 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2619 x = workarea[2] / 2 - text_width / 2 - gap_x;
2623 if (text_alignment == NONE) { // Let the WM manage the window
2630 #endif /* OWN_WINDOW */
2633 if (own_window && !fixed_pos) {
2636 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2637 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2638 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2639 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2643 /* If window size doesn't match to workarea's size,
2644 * then window probably includes panels (gnome).
2645 * Blah, doesn't work on KDE. */
2646 if (workarea[2] != window.width || workarea[3] != window.height) {
2655 /* update lua window globals */
2656 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2657 #endif /* HAVE_LUA */
2662 static int cur_x, cur_y; /* current x and y for drawing */
2664 //draw_mode also without X11 because we only need to print to stdout with FG
2665 static int draw_mode; /* FG, BG or OUTLINE */
2667 static long current_color;
2669 static int text_size_updater(char *s, int special_index)
2673 int contain_SECRIT_MULTILINE_CHAR = 0;
2676 if ((output_methods & TO_X) == 0)
2678 /* get string widths and skip specials */
2681 if (*p == SPECIAL_CHAR) {
2683 w += get_string_width(s);
2686 if (specials[special_index].type == BAR
2687 || specials[special_index].type == GAUGE
2688 || specials[special_index].type == GRAPH) {
2689 w += specials[special_index].width;
2690 if (specials[special_index].height > last_font_height) {
2691 last_font_height = specials[special_index].height;
2692 last_font_height += font_height();
2694 } else if (specials[special_index].type == OFFSET) {
2695 if (specials[special_index].arg > 0) {
2696 w += specials[special_index].arg;
2698 } else if (specials[special_index].type == VOFFSET) {
2699 last_font_height += specials[special_index].arg;
2700 } else if (specials[special_index].type == GOTO) {
2701 if (specials[special_index].arg > cur_x) {
2702 w = (int) specials[special_index].arg;
2704 } else if (specials[special_index].type == TAB) {
2705 int start = specials[special_index].arg;
2706 int step = specials[special_index].width;
2708 if (!step || step < 0) {
2711 w += step - (cur_x - text_start_x - start) % step;
2712 } else if (specials[special_index].type == FONT) {
2713 selected_font = specials[special_index].font_added;
2714 if (font_height() > last_font_height) {
2715 last_font_height = font_height();
2721 } else if (*p == SECRIT_MULTILINE_CHAR) {
2722 contain_SECRIT_MULTILINE_CHAR = 1;
2724 lw = get_string_width(s);
2725 *p = SECRIT_MULTILINE_CHAR;
2727 w = lw > w ? lw : w;
2728 text_height += last_font_height;
2732 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2733 if (contain_SECRIT_MULTILINE_CHAR) {
2734 lw = get_string_width(s);
2735 w = lw > w ? lw : w;
2737 w += get_string_width(s);
2739 if (w > text_width) {
2742 if (text_width > maximum_width && maximum_width) {
2743 text_width = maximum_width;
2746 text_height += last_font_height;
2747 last_font_height = font_height();
2748 return special_index;
2752 static inline void set_foreground_color(long c)
2755 if (output_methods & TO_X) {
2757 if (have_argb_visual) {
2758 current_color = c | (own_window_argb_value << 24);
2760 #endif /* USE_ARGB */
2764 #endif /* USE_ARGB */
2765 XSetForeground(display, window.gc, current_color);
2769 if (output_methods & TO_NCURSES) {
2770 attron(COLOR_PAIR(c));
2772 #endif /* NCURSES */
2777 static void draw_string(const char *s)
2779 int i, i2, pos, width_of_s;
2782 char *s_with_newlines;
2788 width_of_s = get_string_width(s);
2789 s_with_newlines = strdup(s);
2790 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2791 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2792 s_with_newlines[i] = '\n';
2795 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2796 printf("%s\n", s_with_newlines);
2797 if (extra_newline) fputc('\n', stdout);
2798 fflush(stdout); /* output immediately, don't buffer */
2800 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2801 fprintf(stderr, "%s\n", s_with_newlines);
2802 fflush(stderr); /* output immediately, don't buffer */
2804 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2805 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2807 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2808 fprintf(append_fpointer, "%s\n", s_with_newlines);
2811 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2812 printw("%s", s_with_newlines);
2815 free(s_with_newlines);
2816 memset(tmpstring1, 0, text_buffer_size);
2817 memset(tmpstring2, 0, text_buffer_size);
2818 strncpy(tmpstring1, s, text_buffer_size - 1);
2823 if (output_methods & TO_X) {
2824 max = ((text_width - width_of_s) / get_string_width(" "));
2827 /* This code looks for tabs in the text and coverts them to spaces.
2828 * The trick is getting the correct number of spaces, and not going
2829 * over the window's size without forcing the window larger. */
2830 for (i = 0; i < (int) text_buffer_size; i++) {
2831 if (tmpstring1[i] == '\t') {
2833 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2834 /* guard against overrun */
2835 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2840 /* guard against overrun */
2841 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2846 if (output_methods & TO_X) {
2847 if (text_width == maximum_width) {
2848 /* this means the text is probably pushing the limit,
2849 * so we'll chop it */
2850 while (cur_x + get_string_width(tmpstring2) - text_start_x
2851 > maximum_width && strlen(tmpstring2) > 0) {
2852 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2859 if (output_methods & TO_X) {
2865 c.pixel = current_color;
2866 // query color on custom colormap
2867 XQueryColor(display, window.colourmap, &c);
2870 c2.color.red = c.red;
2871 c2.color.green = c.green;
2872 c2.color.blue = c.blue;
2873 c2.color.alpha = fonts[selected_font].font_alpha;
2875 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2876 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2878 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2879 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2884 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2887 cur_x += width_of_s;
2890 memcpy(tmpstring1, s, text_buffer_size);
2893 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2901 int last_special_needed = -1;
2902 int orig_special_index = special_index;
2905 if (output_methods & TO_X) {
2906 font_h = font_height();
2907 cur_y += font_ascent();
2909 cur_x = text_start_x;
2913 if (*p == SECRIT_MULTILINE_CHAR) {
2914 /* special newline marker for multiline objects */
2919 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2924 /* draw string before special, unless we're dealing multiline
2926 if (last_special_applied > -1) {
2927 special_index = last_special_applied;
2935 switch (specials[special_index].type) {
2937 case HORIZONTAL_LINE:
2939 int h = specials[special_index].height;
2940 int mid = font_ascent() / 2;
2942 w = text_start_x + text_width - cur_x;
2944 XSetLineAttributes(display, window.gc, h, LineSolid,
2945 CapButt, JoinMiter);
2946 XDrawLine(display, window.drawable, window.gc, cur_x,
2947 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2953 int h = specials[special_index].height;
2954 int tmp_s = specials[special_index].arg;
2955 int mid = font_ascent() / 2;
2956 char ss[2] = { tmp_s, tmp_s };
2958 w = text_start_x + text_width - cur_x - 1;
2959 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2960 CapButt, JoinMiter);
2961 XSetDashes(display, window.gc, 0, ss, 2);
2962 XDrawLine(display, window.drawable, window.gc, cur_x,
2963 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2969 int h, bar_usage, by;
2970 if (cur_x - text_start_x > maximum_width
2971 && maximum_width > 0) {
2974 h = specials[special_index].height;
2975 bar_usage = specials[special_index].arg;
2976 by = cur_y - (font_ascent() / 2) - 1;
2981 w = specials[special_index].width;
2983 w = text_start_x + text_width - cur_x - 1;
2989 XSetLineAttributes(display, window.gc, 1, LineSolid,
2990 CapButt, JoinMiter);
2992 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2994 XFillRectangle(display, window.drawable, window.gc, cur_x,
2995 by, w * bar_usage / 255, h);
3003 case GAUGE: /* new GAUGE */
3006 unsigned long last_colour = current_color;
3008 float angle, px, py;
3012 if (cur_x - text_start_x > maximum_width
3013 && maximum_width > 0) {
3017 h = specials[special_index].height;
3018 by = cur_y - (font_ascent() / 2) - 1;
3023 w = specials[special_index].width;
3025 w = text_start_x + text_width - cur_x - 1;
3031 XSetLineAttributes(display, window.gc, 1, LineSolid,
3032 CapButt, JoinMiter);
3034 XDrawArc(display, window.drawable, window.gc,
3035 cur_x, by, w, h * 2, 0, 180*64);
3038 usage = specials[special_index].arg;
3039 angle = (M_PI)*(float)(usage)/255.;
3040 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3041 py = (float)(by+(h))-(float)(h)*sin(angle);
3043 XDrawLine(display, window.drawable, window.gc,
3044 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3052 set_foreground_color(last_colour);
3060 int h, by, i = 0, j = 0;
3062 unsigned long last_colour = current_color;
3063 unsigned long *tmpcolour = 0;
3064 if (cur_x - text_start_x > maximum_width
3065 && maximum_width > 0) {
3068 h = specials[special_index].height;
3069 by = cur_y - (font_ascent() / 2) - 1;
3074 w = specials[special_index].width;
3076 w = text_start_x + text_width - cur_x - 1;
3081 if (draw_graph_borders) {
3082 XSetLineAttributes(display, window.gc, 1, LineSolid,
3083 CapButt, JoinMiter);
3084 XDrawRectangle(display, window.drawable, window.gc,
3087 XSetLineAttributes(display, window.gc, 1, LineSolid,
3088 CapButt, JoinMiter);
3090 if (specials[special_index].last_colour != 0
3091 || specials[special_index].first_colour != 0) {
3092 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3095 for (i = w - 2; i > -1; i--) {
3096 if (specials[special_index].last_colour != 0
3097 || specials[special_index].first_colour != 0) {
3098 if (specials[special_index].tempgrad) {
3101 (int)((float)(w - 2) - specials[special_index].graph[j] *
3102 (w - 2) / (float)specials[special_index].graph_scale)
3106 (int)((float)(w - 2) - specials[special_index].graph[j] *
3107 (w - 2) / (float)specials[special_index].graph_scale)
3110 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3112 (int)((float)(w - 2) - specials[special_index].graph[j] *
3113 (w - 2) / (float)specials[special_index].graph_scale)
3117 #endif /* DEBUG_lol */
3118 set_foreground_color(tmpcolour[
3119 (int)((float)(w - 2) -
3120 specials[special_index].graph[j]
3122 (float)specials[special_index].graph_scale)
3125 set_foreground_color(tmpcolour[colour_idx++]);
3128 /* this is mugfugly, but it works */
3129 XDrawLine(display, window.drawable, window.gc,
3130 cur_x + i + 1, by + h, cur_x + i + 1,
3131 round_to_int((double)by + h - specials[special_index].graph[j] *
3132 (h - 1) / specials[special_index].graph_scale));
3133 if ((w - i) / ((float) (w - 2) /
3134 (specials[special_index].graph_width)) > j
3135 && j < MAX_GRAPH_DEPTH - 3) {
3139 if (tmpcolour) free(tmpcolour);
3144 /* if (draw_mode == BG) {
3145 set_foreground_color(default_bg_color);
3146 } else if (draw_mode == OUTLINE) {
3147 set_foreground_color(default_out_color);
3149 set_foreground_color(default_fg_color);
3151 if (show_graph_range) {
3154 unsigned short int seconds = update_interval * w;
3160 unsigned short int timeunits;
3162 timeunits = seconds / 86400; seconds %= 86400;
3163 if (timeunits > 0) {
3164 if (asprintf(&tmp_day_str, "%dd", timeunits) < 0) {
3168 tmp_day_str = strdup("");
3170 timeunits = seconds / 3600; seconds %= 3600;
3171 if (timeunits > 0) {
3172 if (asprintf(&tmp_hour_str, "%dh", timeunits) < 0) {
3176 tmp_hour_str = strdup("");
3178 timeunits = seconds / 60; seconds %= 60;
3179 if (timeunits > 0) {
3180 if (asprintf(&tmp_min_str, "%dm", timeunits) < 0) {
3184 tmp_min_str = strdup("");
3187 if (asprintf(&tmp_sec_str, "%ds", seconds) < 0) {
3191 tmp_sec_str = strdup("");
3193 if (asprintf(&tmp_str, "%s%s%s%s", tmp_day_str,
3194 tmp_hour_str, tmp_min_str, tmp_sec_str) < 0) {
3197 #define FREE(a) if ((a)) free((a));
3198 FREE(tmp_day_str); FREE(tmp_hour_str); FREE(tmp_min_str); FREE(tmp_sec_str);
3200 tmp_str = strdup("Range not possible"); /* should never happen, but better safe then sorry */
3202 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3203 cur_y += font_h / 2;
3204 draw_string(tmp_str);
3211 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3215 cur_x += font_ascent() / 2;
3216 cur_y += font_h / 2;
3218 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3220 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3221 draw_string(tmp_str);
3227 set_foreground_color(last_colour);
3233 int old = font_ascent();
3235 cur_y -= font_ascent();
3236 selected_font = specials[special_index].font_added;
3238 if (cur_y + font_ascent() < cur_y + old) {
3241 cur_y += font_ascent();
3243 font_h = font_height();
3248 if (draw_mode == FG) {
3249 set_foreground_color(specials[special_index].arg);
3255 if (draw_mode == BG) {
3256 set_foreground_color(specials[special_index].arg);
3261 if (draw_mode == OUTLINE) {
3262 set_foreground_color(specials[special_index].arg);
3267 w += specials[special_index].arg;
3268 last_special_needed = special_index;
3272 cur_y += specials[special_index].arg;
3276 if (specials[special_index].arg >= 0) {
3277 cur_x = (int) specials[special_index].arg;
3279 last_special_needed = special_index;
3284 int start = specials[special_index].arg;
3285 int step = specials[special_index].width;
3287 if (!step || step < 0) {
3290 w = step - (cur_x - text_start_x - start) % step;
3291 last_special_needed = special_index;
3297 /* TODO: add back in "+ window.border_inner_margin" to the end of
3299 int pos_x = text_start_x + text_width -
3300 get_string_width_special(s, special_index);
3302 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3303 "get_string_width(p) %i gap_x %i "
3304 "specials[special_index].arg %i window.border_inner_margin %i "
3305 "window.border_width %i\n", pos_x, text_start_x, text_width,
3306 cur_x, get_string_width_special(s), gap_x,
3307 specials[special_index].arg, window.border_inner_margin,
3308 window.border_width); */
3309 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3310 cur_x = pos_x - specials[special_index].arg;
3312 last_special_needed = special_index;
3318 int pos_x = (text_width) / 2 - get_string_width_special(s,
3319 special_index) / 2 - (cur_x -
3321 /* int pos_x = text_start_x + text_width / 2 -
3322 get_string_width_special(s) / 2; */
3324 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3325 "get_string_width(p) %i gap_x %i "
3326 "specials[special_index].arg %i\n", pos_x, text_start_x,
3327 text_width, cur_x, get_string_width(s), gap_x,
3328 specials[special_index].arg); */
3329 if (pos_x > specials[special_index].arg) {
3330 w = pos_x - specials[special_index].arg;
3332 last_special_needed = special_index;
3342 if (special_index != last_special_applied) {
3345 special_index = orig_special_index;
3346 last_special_applied = -1;
3357 if (output_methods & TO_NCURSES) {
3360 #endif /* NCURSES */
3362 if (output_methods & TO_X)
3363 cur_y += font_descent();
3365 if (recurse && *recurse) {
3366 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3367 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3369 return special_index;
3372 static int draw_line(char *s, int special_index)
3375 if (output_methods & TO_X) {
3376 return draw_each_line_inner(s, special_index, -1);
3380 if (output_methods & TO_NCURSES) {
3381 return draw_each_line_inner(s, special_index, -1);
3383 #endif /* NCURSES */
3385 UNUSED(special_index);
3389 static void draw_text(void)
3393 llua_draw_pre_hook();
3394 #endif /* HAVE_LUA */
3395 if (output_methods & TO_X) {
3396 cur_y = text_start_y;
3399 if (draw_borders && window.border_width > 0) {
3400 if (stippled_borders) {
3401 char ss[2] = { stippled_borders, stippled_borders };
3402 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3403 CapButt, JoinMiter);
3404 XSetDashes(display, window.gc, 0, ss, 2);
3406 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3407 CapButt, JoinMiter);
3410 XDrawRectangle(display, window.drawable, window.gc,
3411 text_start_x - window.border_inner_margin - window.border_width,
3412 text_start_y - window.border_inner_margin - window.border_width,
3413 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3414 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3422 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3423 attron(COLOR_PAIR(COLOR_WHITE));
3424 #endif /* NCURSES */
3425 for_each_line(text_buffer, draw_line);
3426 #if defined(HAVE_LUA) && defined(X11)
3427 llua_draw_post_hook();
3428 #endif /* HAVE_LUA */
3431 static void draw_stuff(void)
3434 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3436 if (overwrite_file) {
3437 overwrite_fpointer = fopen(overwrite_file, "w");
3438 if(!overwrite_fpointer)
3439 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3442 append_fpointer = fopen(append_file, "a");
3443 if(!append_fpointer)
3444 NORM_ERR("Can't append '%s' anymore", append_file);
3447 if (output_methods & TO_X) {
3449 if (draw_shades && !draw_outline) {
3452 set_foreground_color(default_bg_color);
3463 for (i = -1; i < 2; i++) {
3464 for (j = -1; j < 2; j++) {
3465 if (i == 0 && j == 0) {
3470 set_foreground_color(default_out_color);
3471 draw_mode = OUTLINE;
3479 set_foreground_color(default_fg_color);
3484 #if defined(X11) && defined(HAVE_XDBE)
3485 if (output_methods & TO_X) {
3486 xdbe_swap_buffers();
3488 #endif /* X11 && HAVE_XDBE */
3489 if(overwrite_fpointer) {
3490 fclose(overwrite_fpointer);
3491 overwrite_fpointer = 0;
3493 if(append_fpointer) {
3494 fclose(append_fpointer);
3495 append_fpointer = 0;
3500 static void clear_text(int exposures)
3505 The swap action is XdbeBackground, which clears
3508 if (display && window.back_buffer) { // make sure these are !null
3509 /* there is some extra space for borders and outlines */
3510 XFillRectangle(display, window.back_buffer, window.gc_back,
3511 text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3512 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3513 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3514 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2);
3520 if (display && window.window) { // make sure these are !null
3521 /* there is some extra space for borders and outlines */
3522 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3523 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3524 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3525 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3530 static int need_to_update;
3532 /* update_text() generates new text and clears old text area */
3533 static void update_text(void)
3540 if (output_methods & TO_X)
3545 llua_update_info(&info, update_interval);
3546 #endif /* HAVE_LUA */
3549 #ifdef HAVE_SYS_INOTIFY_H
3553 static void main_loop(void)
3556 #ifdef SIGNAL_BLOCKING
3557 sigset_t newmask, oldmask;
3560 #ifdef HAVE_SYS_INOTIFY_H
3561 int inotify_config_wd = -1;
3562 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3563 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3564 char inotify_buff[INOTIFY_BUF_LEN];
3565 #endif /* HAVE_SYS_INOTIFY_H */
3568 #ifdef SIGNAL_BLOCKING
3569 sigemptyset(&newmask);
3570 sigaddset(&newmask, SIGINT);
3571 sigaddset(&newmask, SIGTERM);
3572 sigaddset(&newmask, SIGUSR1);
3575 last_update_time = 0.0;
3576 next_update_time = get_time();
3581 if (update_heartbeat_min < update_heartbeat_max){
3582 msg = dbus_message_new_method_call("net.appcheck.Proximus", // target service
3583 "/Proximus", // object path
3584 "net.appcheck.Proximus", // interface to call on
3585 "startTimer"); // method name
3587 fprintf(stderr, "Message Null\n");
3590 if (!dbus_message_append_args(msg, DBUS_TYPE_INT32, &update_heartbeat_min, DBUS_TYPE_INT32, &update_heartbeat_max, DBUS_TYPE_INVALID)) {
3591 fprintf(stderr, "Out Of Memory!\n");
3595 dbus_error_init (&err);
3596 reply = dbus_connection_send_with_reply_and_block (conn, msg, 1500, &err);
3598 if (dbus_error_is_set(&err))
3600 fprintf (stderr, "dbus Error %s: %s\n",err.name,err.message);
3605 //fprintf (stderr, "got dbus reply to startTimer\n");
3606 dbus_message_unref (reply);
3609 fprintf (stderr, "no reply to startTimer\n");
3610 update_heartbeat_min = update_heartbeat_max;
3612 dbus_message_unref (msg);
3617 bool sleeping = false; //i assume it's pretty hard to start this with the screen off...
3619 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3620 //fprintf(stderr, "started main_loop_while\n");
3621 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3622 char buf[max_user_text];
3624 get_battery_short_status(buf, max_user_text, "bq27200-0"); //why is this even hardcoded here? oh well.
3626 update_interval = update_interval_bat;
3627 WantSkips = update_heartbeat_battery_skip;
3629 update_interval = update_interval_old;
3633 if (sleeping) WantSkips = update_skips_when_sleeping;
3637 #ifdef SIGNAL_BLOCKING
3638 /* block signals. we will inspect for pending signals later */
3639 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3640 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3645 if (output_methods & TO_X) {
3648 /* wait for X event or timeout */
3650 if (!XPending(display)) {
3654 //should look for /org/freedesktop/Hal/devices/bme com.nokia.bme.signal
3655 if (update_heartbeat_min != update_heartbeat_max) { //wait for signal
3656 // add a rule for which messages we want to see
3657 dbus_bus_add_match(conn,
3658 "type='signal',interface='net.appcheck.Proximus'",
3659 &err); // see signals from the given interface
3660 dbus_bus_add_match(conn2,
3661 "type='signal',interface='com.nokia.mce.signal'",
3662 &err); // see signals from the given interface
3663 dbus_connection_flush(conn);
3664 dbus_connection_flush(conn2);
3665 if (dbus_error_is_set(&err)) {
3666 fprintf(stderr, "Match Error (%s)\n", err.message);
3671 while (exitloop == 0 && info.looped >= 10) { //first 10 frames drawn fast because if using lua + cairo it takes a while to get going
3672 // non blocking read of the next available message
3673 dbus_connection_read_write(conn, 0);
3674 dbus_connection_read_write(conn2, 0);
3675 msg = dbus_connection_pop_message(conn);
3677 // loop again if we haven't read a message
3679 msg = dbus_connection_pop_message(conn2);
3685 // check if the message is a signal from the correct interface and with the correct name
3686 if (dbus_message_is_signal(msg, "net.appcheck.Proximus", "heartbeat")) {
3687 if(WantSkips == 0 || DoneSkips >= WantSkips){
3689 fprintf(stderr, PACKAGE_NAME": woke by heartbeat\n");
3692 fprintf(stderr, PACKAGE_NAME": ignoring this heartbeat\n");
3696 else if (dbus_message_is_signal(msg, "com.nokia.mce.signal", "display_status_ind")){
3697 // read the parameters
3698 if (!dbus_message_iter_init(msg, &args))
3699 fprintf(stderr, "Message has no arguments!\n");
3700 else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
3701 fprintf(stderr, "Argument is not string!\n");
3704 dbus_message_iter_get_basic(&args, &sigvalue);
3705 if(!strcmp(sigvalue, MCE_DISPLAY_ON_STRING)){
3706 printf("wakeup because got display_status_ind signal with value %s\n", sigvalue);
3710 else if(!strcmp(sigvalue, MCE_DISPLAY_OFF_STRING)){
3711 printf("sleeping because got display_status_ind signal with value %s\n", sigvalue);
3712 WantSkips = update_skips_when_sleeping;
3718 //fprintf(stderr, PACKAGE_NAME": got dbus junk\n");
3720 dbus_message_unref(msg);
3722 t = 0;//force legacy timer to skip -- update now or as soon as x is ready
3724 else{ //signal not available, set up for legacy timer
3725 t = next_update_time - get_time();
3729 else if (t > update_interval) {
3730 t = update_interval;
3734 tv.tv_sec = (long) t;
3735 tv.tv_usec = (long) (t * 1000000) % 1000000;
3737 FD_SET(ConnectionNumber(display), &fdsr);
3739 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3741 if (errno != EINTR) {
3742 NORM_ERR("can't select(): %s", strerror(errno));
3752 if (need_to_update) {
3754 int wx = window.x, wy = window.y;
3764 /* resize window if it isn't right size */
3766 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3767 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3768 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3769 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3770 draw_stuff(); /* redraw everything in our newly sized window */
3771 XResizeWindow(display, window.window, window.width,
3772 window.height); /* resize window */
3773 fprintf(stderr, PACKAGE_NAME": resizing window to %d x %d\n",window.width,window.height);
3776 XFreePixmap(display, window.back_buffer);
3777 window.back_buffer = XCreatePixmap(display, window.window,
3778 window.width, window.height, window.depth);
3779 if (window.back_buffer != None)
3780 window.drawable = window.back_buffer;
3782 window.drawable = window.window;
3787 cimlib_init(display, window.drawable, window.visual, window.colourmap);
3792 set_transparent_background(window.window, own_window_argb_value);
3795 xdbe_swap_buffers();
3800 /* update lua window globals */
3801 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3802 #endif /* HAVE_LUA */
3805 /* move window if it isn't in right position */
3806 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3807 XMoveWindow(display, window.window, window.x, window.y);
3812 if (changed && window.type == TYPE_PANEL) {
3815 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3818 switch (text_alignment) {
3845 set_struts(sidenum);
3856 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3857 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3858 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3859 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3860 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3865 /* handle X events */
3866 while (XPending(display)) {
3869 XNextEvent(display, &ev);
3876 r.width = ev.xexpose.width;
3877 r.height = ev.xexpose.height;
3878 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3882 case PropertyNotify:
3884 if ( ev.xproperty.state == PropertyNewValue ) {
3885 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3891 case ReparentNotify:
3892 /* make background transparent */
3894 set_transparent_background(window.window, own_window_argb_value);
3898 case ConfigureNotify:
3900 /* if window size isn't what expected, set fixed size */
3901 if (ev.xconfigure.width != window.width
3902 || ev.xconfigure.height != window.height) {
3903 if (window.width != 0 && window.height != 0) {
3907 /* clear old stuff before screwing up
3912 XWindowAttributes attrs;
3913 if (XGetWindowAttributes(display,
3914 window.window, &attrs)) {
3915 window.width = attrs.width;
3916 window.height = attrs.height;
3917 fprintf(stderr, PACKAGE_NAME": x11? resized our window to %d x %d\n",window.width,window.height);
3921 XFreePixmap(display, window.back_buffer);
3922 window.back_buffer = XCreatePixmap(display, window.window,
3923 window.width, window.height, window.depth);
3924 if (window.back_buffer != None)
3925 window.drawable = window.back_buffer;
3927 window.drawable = window.window;
3932 cimlib_init(display, window.drawable, window.visual, window.colourmap);
3940 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3941 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3942 if (text_width > maximum_width
3943 && maximum_width > 0) {
3944 text_width = maximum_width;
3948 /* if position isn't what expected, set fixed pos
3949 * total_updates avoids setting fixed_pos when window
3950 * is set to weird locations when started */
3951 /* // this is broken
3952 if (total_updates >= 2 && !fixed_pos
3953 && (window.x != ev.xconfigure.x
3954 || window.y != ev.xconfigure.y)
3955 && (ev.xconfigure.x != 0
3956 || ev.xconfigure.y != 0)) {
3964 /* if an ordinary window with decorations */
3965 if ((window.type == TYPE_NORMAL &&
3966 (!TEST_HINT(window.hints,
3967 HINT_UNDECORATED))) ||
3968 window.type == TYPE_DESKTOP) {
3969 /* allow conky to hold input focus. */
3971 } else if(TEST_HINT(window.hints,HINT_FULLSCREEN)) {
3972 //detect top right click
3973 if (ev.xbutton.x > 750 && ev.xbutton.y < 50)
3977 if (ev.xbutton.x <= 50 && ev.xbutton.y <= 50)
3978 { //minimize / goto task switcher
3979 dbus_exit_app_view();
3983 /* forward the click to the desktop window */
3984 XUngrabPointer(display, ev.xbutton.time);
3985 ev.xbutton.window = window.desktop;
3986 ev.xbutton.x = ev.xbutton.x_root;
3987 ev.xbutton.y = ev.xbutton.y_root;
3988 XSendEvent(display, ev.xbutton.window, False,
3989 ButtonPressMask, &ev);
3990 XSetInputFocus(display, ev.xbutton.window,
3991 RevertToParent, ev.xbutton.time);
3998 /* if an ordinary window with decorations */
3999 if ((window.type == TYPE_NORMAL)
4000 && (!TEST_HINT(window.hints,
4001 HINT_UNDECORATED))) {
4002 /* allow conky to hold input focus. */
4005 /* forward the release to the desktop window */
4006 ev.xbutton.window = window.desktop;
4007 ev.xbutton.x = ev.xbutton.x_root;
4008 ev.xbutton.y = ev.xbutton.y_root;
4009 XSendEvent(display, ev.xbutton.window, False,
4010 ButtonReleaseMask, &ev);
4019 if (ev.type == x11_stuff.event_base + XDamageNotify) {
4020 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
4022 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
4023 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
4025 #endif /* HAVE_XDAMAGE */
4031 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
4032 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
4033 #endif /* HAVE_XDAMAGE */
4035 /* XDBE doesn't seem to provide a way to clear the back buffer
4036 * without interfering with the front buffer, other than passing
4037 * XdbeBackground to XdbeSwapBuffers. That means that if we're
4038 * using XDBE, we need to redraw the text even if it wasn't part of
4039 * the exposed area. OTOH, if we're not going to call draw_stuff at
4040 * all, then no swap happens and we can safely do nothing. */
4042 if (!XEmptyRegion(x11_stuff.region)) {
4047 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4048 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4049 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4050 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4051 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4054 XSetRegion(display, window.gc, x11_stuff.region);
4057 XftDrawSetClip(window.xftdraw, x11_stuff.region);
4061 XDestroyRegion(x11_stuff.region);
4062 x11_stuff.region = XCreateRegion();
4066 t = (next_update_time - get_time()) * 1000000;
4067 //fprintf(stderr, PACKAGE_NAME": trying to sleep %d microseconds\n",t);
4068 if(t > 0) usleep((useconds_t)t);
4072 if(output_methods & TO_NCURSES) {
4081 #ifdef SIGNAL_BLOCKING
4082 /* unblock signals of interest and let handler fly */
4083 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4084 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4088 switch (g_signal_pending) {
4091 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4096 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4099 if (output_methods & TO_X) {
4100 XDestroyRegion(x11_stuff.region);
4101 x11_stuff.region = NULL;
4103 XDamageDestroy(display, x11_stuff.damage);
4104 XFixesDestroyRegion(display, x11_stuff.region2);
4105 XFixesDestroyRegion(display, x11_stuff.part);
4106 #endif /* HAVE_XDAMAGE */
4112 if(overwrite_file) {
4113 free(overwrite_file);
4122 /* Reaching here means someone set a signal
4123 * (SIGXXXX, signal_handler), but didn't write any code
4125 * If you don't want to handle a signal, don't set a handler on
4126 * it in the first place. */
4127 if (g_signal_pending) {
4128 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4132 #ifdef HAVE_SYS_INOTIFY_H
4133 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4134 inotify_config_wd = inotify_add_watch(inotify_fd,
4138 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4139 int len = 0, idx = 0;
4141 struct timeval time_to_wait;
4143 FD_ZERO(&descriptors);
4144 FD_SET(inotify_fd, &descriptors);
4146 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4148 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4149 if (FD_ISSET(inotify_fd, &descriptors)) {
4150 /* process inotify events */
4151 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4152 while (len > 0 && idx < len) {
4153 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4154 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4155 /* current_config should be reloaded */
4156 NORM_ERR("'%s' modified, reloading...", current_config);
4158 if (ev->mask & IN_IGNORED) {
4159 /* for some reason we get IN_IGNORED here
4160 * sometimes, so we need to re-add the watch */
4161 inotify_config_wd = inotify_add_watch(inotify_fd,
4169 llua_inotify_query(ev->wd, ev->mask);
4171 #endif /* HAVE_LUA */
4172 idx += INOTIFY_EVENT_SIZE + ev->len;
4175 } else if (disable_auto_reload && inotify_fd != -1) {
4176 inotify_rm_watch(inotify_fd, inotify_config_wd);
4178 inotify_fd = inotify_config_wd = 0;
4180 #endif /* HAVE_SYS_INOTIFY_H */
4183 llua_update_info(&info, update_interval);
4184 #endif /* HAVE_LUA */
4185 g_signal_pending = 0;
4186 }//end of while{} (the real loop)
4187 clean_up(current_mail_spool, NULL);
4189 #ifdef HAVE_SYS_INOTIFY_H
4190 if (inotify_fd != -1) {
4191 inotify_rm_watch(inotify_fd, inotify_config_wd);
4193 inotify_fd = inotify_config_wd = 0;
4195 #endif /* HAVE_SYS_INOTIFY_H */
4196 }//end of main_loop()
4199 static void load_config_file_x11(const char *);
4201 void initialisation(int argc, char** argv);
4203 /* reload the config file */
4204 static void reload_config(void)
4206 char *current_config_copy = strdup(current_config);
4207 clean_up(NULL, NULL);
4208 sleep(1); /* slight pause */
4209 current_config = current_config_copy;
4210 initialisation(argc_copy, argv_copy);
4214 void clean_up_x11(void)
4216 if(window_created == 1) {
4217 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4218 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4219 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4220 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4225 if(x11_stuff.region) {
4226 XDestroyRegion(x11_stuff.region);
4227 x11_stuff.region = NULL;
4230 XCloseDisplay(display);
4233 if(info.x11.desktop.all_names) {
4234 free(info.x11.desktop.all_names);
4235 info.x11.desktop.all_names = NULL;
4237 if (info.x11.desktop.name) {
4238 free(info.x11.desktop.name);
4239 info.x11.desktop.name = NULL;
4245 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4249 if(output_methods & TO_NCURSES) {
4253 conftree_empty(currentconffile);
4254 currentconffile = NULL;
4261 timed_thread_destroy_registered_threads();
4263 if (info.cpu_usage) {
4264 free(info.cpu_usage);
4265 info.cpu_usage = NULL;
4268 if (x_initialised == YES) {
4271 free(fonts); //in set_default_configurations a font is set but not loaded
4276 set_nvidia_display(NULL);
4282 free_text_objects(&global_root_object, 0);
4301 free(current_config);
4304 #ifdef TCP_PORT_MONITOR
4305 tcp_portmon_clear();
4314 weather_free_info();
4317 llua_shutdown_hook();
4319 #endif /* HAVE_LUA */
4321 if (output_methods & TO_X)
4329 for (i = 0; i < special_count; i++) {
4330 if (specials[i].type == GRAPH) {
4331 free(specials[i].graph);
4339 clear_diskio_stats();
4340 if(global_cpu != NULL) {
4346 void clean_up(void *memtofree1, void* memtofree2)
4348 free_update_callbacks();
4349 clean_up_without_threads(memtofree1, memtofree2);
4352 static int string_to_bool(const char *s)
4355 // Assumes an option without a true/false means true
4357 } else if (strcasecmp(s, "yes") == EQUAL) {
4359 } else if (strcasecmp(s, "true") == EQUAL) {
4361 } else if (strcasecmp(s, "1") == EQUAL) {
4368 static enum alignment string_to_alignment(const char *s)
4370 if (strcasecmp(s, "top_left") == EQUAL) {
4372 } else if (strcasecmp(s, "top_right") == EQUAL) {
4374 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4376 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4378 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4379 return BOTTOM_RIGHT;
4380 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4381 return BOTTOM_MIDDLE;
4382 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4384 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4385 return MIDDLE_RIGHT;
4386 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4387 return MIDDLE_MIDDLE;
4388 } else if (strcasecmp(s, "tl") == EQUAL) {
4390 } else if (strcasecmp(s, "tr") == EQUAL) {
4392 } else if (strcasecmp(s, "tm") == EQUAL) {
4394 } else if (strcasecmp(s, "bl") == EQUAL) {
4396 } else if (strcasecmp(s, "br") == EQUAL) {
4397 return BOTTOM_RIGHT;
4398 } else if (strcasecmp(s, "bm") == EQUAL) {
4399 return BOTTOM_MIDDLE;
4400 } else if (strcasecmp(s, "ml") == EQUAL) {
4402 } else if (strcasecmp(s, "mr") == EQUAL) {
4403 return MIDDLE_RIGHT;
4404 } else if (strcasecmp(s, "mm") == EQUAL) {
4405 return MIDDLE_MIDDLE;
4406 } else if (strcasecmp(s, "none") == EQUAL) {
4409 return ALIGNMENT_ERROR;
4414 static void set_default_configurations_for_x(void)
4416 default_fg_color = WhitePixel(display, screen);
4417 default_bg_color = BlackPixel(display, screen);
4418 default_out_color = BlackPixel(display, screen);
4419 color0 = default_fg_color;
4420 color1 = default_fg_color;
4421 color2 = default_fg_color;
4422 color3 = default_fg_color;
4423 color4 = default_fg_color;
4424 color5 = default_fg_color;
4425 color6 = default_fg_color;
4426 color7 = default_fg_color;
4427 color8 = default_fg_color;
4428 color9 = default_fg_color;
4429 current_text_color = default_fg_color;
4433 static void set_default_configurations(void)
4440 fork_to_background = 0;
4441 total_run_times = 0;
4442 info.cpu_avg_samples = 2;
4443 info.net_avg_samples = 2;
4444 info.diskio_avg_samples = 2;
4449 format_human_readable = 1;
4457 mpd_env_host = getenv("MPD_HOST");
4458 mpd_env_port = getenv("MPD_PORT");
4460 if (!mpd_env_host || !strlen(mpd_env_host)) {
4461 mpd_set_host("localhost");
4463 /* MPD_HOST environment variable is set */
4464 char *mpd_hostpart = strchr(mpd_env_host, '@');
4465 if (!mpd_hostpart) {
4466 mpd_set_host(mpd_env_host);
4468 /* MPD_HOST contains a password */
4469 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4470 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4472 if (!strlen(mpd_hostpart + 1)) {
4473 mpd_set_host("localhost");
4475 mpd_set_host(mpd_hostpart + 1);
4478 mpd_set_password(mpd_password, 1);
4483 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4484 /* failed to set port from environment variable */
4485 mpd_set_port("6600");
4489 info.xmms2.artist = NULL;
4490 info.xmms2.album = NULL;
4491 info.xmms2.title = NULL;
4492 info.xmms2.genre = NULL;
4493 info.xmms2.comment = NULL;
4494 info.xmms2.url = NULL;
4495 info.xmms2.status = NULL;
4496 info.xmms2.playlist = NULL;
4498 use_spacer = NO_SPACER;
4500 output_methods = TO_X;
4502 output_methods = TO_STDOUT;
4508 show_graph_scale = 0;
4509 show_graph_range = 0;
4512 draw_graph_borders = 1;
4514 set_first_font("6x10");
4522 window.type = TYPE_NORMAL;
4524 strcpy(window.class_name, PACKAGE_NAME);
4525 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4527 use_argb_visual = 0;
4528 own_window_argb_value = 255;
4531 stippled_borders = 0;
4532 window.border_inner_margin = 3;
4533 window.border_outer_margin = 1;
4534 window.border_width = 1;
4535 text_alignment = BOTTOM_LEFT;
4536 info.x11.monitor.number = 1;
4537 info.x11.monitor.current = 0;
4538 info.x11.desktop.current = 1;
4539 info.x11.desktop.number = 1;
4540 info.x11.desktop.nitems = 0;
4541 info.x11.desktop.all_names = NULL;
4542 info.x11.desktop.name = NULL;
4547 free(current_mail_spool);
4551 variable_substitute(MAIL_FILE, buf, 256);
4552 if (buf[0] != '\0') {
4553 current_mail_spool = strndup(buf, text_buffer_size);
4558 set_update_interval(3);
4559 update_interval_bat = NOBATTERY;
4560 info.music_player_interval = 1.0;
4561 stuff_in_uppercase = 0;
4562 info.users.number = 1;
4564 set_times_in_seconds(0);
4566 #ifdef TCP_PORT_MONITOR
4567 /* set default connection limit */
4568 tcp_portmon_set_max_connections(0);
4572 /* returns 1 if you can overwrite or create the file at 'path' */
4573 static _Bool overwrite_works(const char *path)
4577 if (!(filepointer = fopen(path, "w")))
4579 fclose(filepointer);
4583 /* returns 1 if you can append or create the file at 'path' */
4584 static _Bool append_works(const char *path)
4588 if (!(filepointer = fopen(path, "a")))
4590 fclose(filepointer);
4596 /* WARNING, this type not in Xlib spec */
4597 int x11_error_handler(Display *d, XErrorEvent *err)
4598 __attribute__((noreturn));
4599 int x11_error_handler(Display *d, XErrorEvent *err)
4601 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",
4603 (long unsigned)err->display,
4604 (long)err->resourceid,
4614 int x11_ioerror_handler(Display *d)
4615 __attribute__((noreturn));
4616 int x11_ioerror_handler(Display *d)
4618 NORM_ERR("X Error: Display %lx\n",
4625 static void X11_initialisation(void)
4627 if (x_initialised == YES) return;
4628 output_methods |= TO_X;
4630 set_default_configurations_for_x();
4631 x_initialised = YES;
4634 /* WARNING, this type not in Xlib spec */
4635 XSetErrorHandler(&x11_error_handler);
4636 XSetIOErrorHandler(&x11_ioerror_handler);
4640 static char **xargv = 0;
4641 static int xargc = 0;
4643 static void X11_create_window(void)
4645 if (output_methods & TO_X) {
4647 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4648 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4650 #else /* OWN_WINDOW */
4651 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4652 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4654 #endif /* OWN_WINDOW */
4658 update_text_area(); /* to position text/window on screen */
4661 if (own_window && !fixed_pos) {
4662 XMoveWindow(display, window.window, window.x, window.y);
4665 set_transparent_background(window.window, own_window_argb_value);
4673 x11_stuff.region = XCreateRegion();
4675 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4676 NORM_ERR("Xdamage extension unavailable");
4678 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4679 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4680 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4681 #endif /* HAVE_XDAMAGE */
4684 update_text_area(); /* to get initial size of the window */
4687 /* setup lua window globals */
4688 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4689 #endif /* HAVE_LUA */
4693 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4694 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4695 #define CONF2(a) if (strcasecmp(name, a) == 0)
4696 #define CONF(a) else CONF2(a)
4697 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4698 || strcasecmp(name, b) == 0)
4699 #define CONF_CONTINUE 1
4700 #define CONF_BREAK 2
4701 #define CONF_BUFF_SIZE 512
4703 static FILE *open_config_file(const char *f)
4705 #ifdef CONFIG_OUTPUT
4706 if (!strcmp(f, "==builtin==")) {
4707 return conf_cookie_open();
4709 #endif /* CONFIG_OUTPUT */
4710 return fopen(f, "r");
4713 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4717 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4720 remove_comments(buf);
4725 while (*p && isspace((int) *p)) {
4729 return CONF_CONTINUE; /* empty line */
4736 while (*p2 && !isspace((int) *p2)) {
4740 *p2 = '\0'; /* break at name's end */
4747 while (*p && isspace((int) *p)) {
4753 p2 = *value + strlen(*value);
4754 while (isspace((int) *(p2 - 1))) {
4764 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4766 if (windowtype == TYPE_DOCK) {
4767 NORM_ERR("alignment is disabled when own_window_type is dock");
4769 #endif /*OWN_WINDOW */
4771 int a = string_to_alignment(value);
4774 if (setbyconffile) {
4776 } else NORM_ERR("'%s' is not a alignment setting", value);
4778 *ltext_alignment = a;
4780 } else if (setbyconffile) {
4786 char load_config_file(const char *f)
4791 fp = open_config_file(f);
4795 DBGP("reading contents from config file '%s'", f);
4798 char buff[CONF_BUFF_SIZE], *name, *value;
4799 int ret = do_config_step(&line, fp, buff, &name, &value);
4800 if (ret == CONF_BREAK) {
4802 } else if (ret == CONF_CONTINUE) {
4808 if (string_to_bool(value)) {
4809 output_methods &= TO_X;
4812 output_methods &= ~TO_X;
4813 x_initialised = NEVER;
4817 if (!value || x_initialised == YES) {
4822 disp = strdup(value);
4826 CONF("nvidia_display") {
4828 set_nvidia_display(value);
4832 setalignment(&text_alignment, window.type, value, f, line, 1);
4834 CONF("background") {
4835 fork_to_background = string_to_bool(value);
4838 CONF2("background") {
4839 fork_to_background = string_to_bool(value);
4843 CONF("show_graph_scale") {
4844 show_graph_scale = string_to_bool(value);
4846 CONF("show_graph_range") {
4847 show_graph_range = string_to_bool(value);
4849 CONF("border_inner_margin") {
4851 window.border_inner_margin = strtol(value, 0, 0);
4852 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4857 CONF("border_outer_margin") {
4859 window.border_outer_margin = strtol(value, 0, 0);
4860 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4865 CONF("border_width") {
4867 window.border_width = strtol(value, 0, 0);
4868 if (window.border_width < 1) window.border_width = 1;
4874 #define TEMPLATE_CONF(n) \
4875 CONF("template"#n) { \
4876 if (set_template(n, value)) \
4891 parse_global_imap_mail_args(value);
4898 parse_global_pop3_mail_args(value);
4903 CONF("default_bar_size") {
4906 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4913 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4917 CONF("default_graph_size") {
4920 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4927 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4930 CONF("default_gauge_size") {
4933 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4940 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4947 mpd_set_host(value);
4953 if (value && mpd_set_port(value)) {
4957 CONF("mpd_password") {
4959 mpd_set_password(value, 0);
4965 CONF("music_player_interval") {
4967 info.music_player_interval = strtod(value, 0);
4973 CONF("sensor_device") {
4975 sensor_device = strtol(value, 0, 0);
4981 CONF("cpu_avg_samples") {
4983 cpu_avg_samples = strtol(value, 0, 0);
4984 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4987 info.cpu_avg_samples = cpu_avg_samples;
4993 CONF("net_avg_samples") {
4995 net_avg_samples = strtol(value, 0, 0);
4996 if (net_avg_samples < 1 || net_avg_samples > 14) {
4999 info.net_avg_samples = net_avg_samples;
5005 CONF("diskio_avg_samples") {
5007 diskio_avg_samples = strtol(value, 0, 0);
5008 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
5011 info.diskio_avg_samples = diskio_avg_samples;
5019 CONF("double_buffer") {
5020 use_xdbe = string_to_bool(value);
5024 CONF("override_utf8_locale") {
5025 utf8_mode = string_to_bool(value);
5027 CONF("draw_borders") {
5028 draw_borders = string_to_bool(value);
5030 CONF("draw_graph_borders") {
5031 draw_graph_borders = string_to_bool(value);
5033 CONF("draw_shades") {
5034 draw_shades = string_to_bool(value);
5036 CONF("draw_outline") {
5037 draw_outline = string_to_bool(value);
5040 CONF("times_in_seconds") {
5041 set_times_in_seconds(string_to_bool(value));
5043 CONF("max_text_width") {
5044 max_text_width = atoi(value);
5046 CONF("out_to_console") {
5047 if(string_to_bool(value)) {
5048 output_methods |= TO_STDOUT;
5050 output_methods &= ~TO_STDOUT;
5053 CONF("extra_newline") {
5054 extra_newline = string_to_bool(value);
5056 CONF("disable_auto_reload") {
5057 disable_auto_reload = string_to_bool(value);
5059 CONF("out_to_stderr") {
5060 if(string_to_bool(value))
5061 output_methods |= TO_STDERR;
5064 CONF("out_to_ncurses") {
5065 if(string_to_bool(value)) {
5068 output_methods |= TO_NCURSES;
5072 CONF("overwrite_file") {
5073 if(overwrite_file) {
5074 free(overwrite_file);
5077 if(overwrite_works(value)) {
5078 overwrite_file = strdup(value);
5079 output_methods |= OVERWRITE_FILE;
5081 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
5083 CONF("append_file") {
5088 if(append_works(value)) {
5089 append_file = strdup(value);
5090 output_methods |= APPEND_FILE;
5092 NORM_ERR("append_file won't be able to create/append '%s'", value);
5094 CONF("use_spacer") {
5096 if (strcasecmp(value, "left") == EQUAL) {
5097 use_spacer = LEFT_SPACER;
5098 } else if (strcasecmp(value, "right") == EQUAL) {
5099 use_spacer = RIGHT_SPACER;
5100 } else if (strcasecmp(value, "none") == EQUAL) {
5101 use_spacer = NO_SPACER;
5103 use_spacer = string_to_bool(value);
5104 NORM_ERR("use_spacer should have an argument of left, right, or"
5105 " none. '%s' seems to be some form of '%s', so"
5106 " defaulting to %s.", value,
5107 use_spacer ? "true" : "false",
5108 use_spacer ? "right" : "none");
5110 use_spacer = RIGHT_SPACER;
5112 use_spacer = NO_SPACER;
5116 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5117 use_spacer = RIGHT_SPACER;
5123 use_xft = string_to_bool(value);
5127 set_first_font(value);
5131 if (value && font_count >= 0) {
5132 fonts[0].font_alpha = atof(value) * 65535.0;
5139 if (string_to_bool(value)) {
5140 NORM_ERR("Xft not enabled at compile time");
5144 /* xftfont silently ignored when no Xft */
5147 /* xftalpha is silently ignored when no Xft */
5152 set_first_font(value);
5160 gap_x = atoi(value);
5167 gap_y = atoi(value);
5173 CONF("mail_spool") {
5177 variable_substitute(value, buffer, 256);
5179 if (buffer[0] != '\0') {
5180 if (current_mail_spool) {
5181 free(current_mail_spool);
5183 current_mail_spool = strndup(buffer, text_buffer_size);
5190 CONF("minimum_size") {
5192 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5194 if (sscanf(value, "%d", &minimum_width) != 1) {
5202 CONF("maximum_width") {
5204 if (sscanf(value, "%d", &maximum_width) != 1) {
5212 CONF("no_buffers") {
5213 no_buffers = string_to_bool(value);
5215 CONF("top_name_width") {
5216 if (set_top_name_width(value))
5219 CONF("top_cpu_separate") {
5220 cpu_separate = string_to_bool(value);
5222 CONF("short_units") {
5223 short_units = string_to_bool(value);
5225 CONF("format_human_readable") {
5226 format_human_readable = string_to_bool(value);
5229 CONF("hddtemp_host") {
5230 set_hddtemp_host(value);
5232 CONF("hddtemp_port") {
5233 set_hddtemp_port(value);
5235 #endif /* HDDTEMP */
5236 CONF("pad_percents") {
5237 pad_percents = atoi(value);
5241 CONF("own_window") {
5243 own_window = string_to_bool(value);
5246 CONF("own_window_class") {
5248 memset(window.class_name, 0, sizeof(window.class_name));
5249 strncpy(window.class_name, value,
5250 sizeof(window.class_name) - 1);
5253 CONF("own_window_title") {
5255 memset(window.title, 0, sizeof(window.title));
5256 strncpy(window.title, value, sizeof(window.title) - 1);
5259 CONF("own_window_transparent") {
5261 set_transparent = string_to_bool(value);
5264 CONF("own_window_hints") {
5266 char *p_hint, *p_save;
5267 char delim[] = ", ";
5269 /* tokenize the value into individual hints */
5270 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5272 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5273 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5274 SET_HINT(window.hints, HINT_UNDECORATED);
5275 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5276 SET_HINT(window.hints, HINT_BELOW);
5277 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5278 SET_HINT(window.hints, HINT_ABOVE);
5279 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5280 SET_HINT(window.hints, HINT_STICKY);
5281 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5282 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5283 } else if (strncmp(p_hint, "fullscreen", 10) == EQUAL) {
5284 SET_HINT(window.hints, HINT_FULLSCREEN);
5289 p_hint = strtok_r(NULL, delim, &p_save);
5290 } while (p_hint != NULL);
5296 CONF("own_window_type") {
5298 if (strncmp(value, "normal", 6) == EQUAL) {
5299 window.type = TYPE_NORMAL;
5300 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5301 window.type = TYPE_DESKTOP;
5302 } else if (strncmp(value, "dock", 4) == EQUAL) {
5303 window.type = TYPE_DOCK;
5304 text_alignment = TOP_LEFT;
5305 } else if (strncmp(value, "panel", 5) == EQUAL) {
5306 window.type = TYPE_PANEL;
5307 } else if (strncmp(value, "override", 8) == EQUAL) {
5308 window.type = TYPE_OVERRIDE;
5317 CONF("own_window_argb_visual") {
5318 use_argb_visual = string_to_bool(value);
5320 CONF("own_window_argb_value") {
5321 own_window_argb_value = strtol(value, 0, 0);
5322 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5323 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5326 #endif /* USE_ARGB */
5328 CONF("stippled_borders") {
5330 stippled_borders = strtol(value, 0, 0);
5332 stippled_borders = 4;
5336 CONF("imlib_cache_size") {
5338 cimlib_set_cache_size(atoi(value));
5341 CONF("imlib_cache_flush_interval") {
5343 cimlib_set_cache_flush_interval(atoi(value));
5348 CONF("update_heartbeat_min") {
5350 update_heartbeat_min = strtod(value, 0);
5355 CONF("update_heartbeat_max") {
5357 update_heartbeat_max = strtod(value, 0);
5362 CONF("update_heartbeat_battery_skip") {
5364 update_heartbeat_battery_skip = strtod(value, 0);
5369 CONF("update_skips_when_sleeping") {
5371 update_skips_when_sleeping = strtod(value, 0);
5376 CONF("update_interval_on_battery") {
5378 update_interval_bat = strtod(value, 0);
5383 CONF("update_interval") {
5385 set_update_interval(strtod(value, 0));
5389 if (info.music_player_interval == 0) {
5390 // default to update_interval
5391 info.music_player_interval = update_interval;
5394 CONF("total_run_times") {
5396 total_run_times = strtod(value, 0);
5402 stuff_in_uppercase = string_to_bool(value);
5404 CONF("max_specials") {
5406 max_specials = atoi(value);
5411 CONF("max_user_text") {
5413 max_user_text = atoi(value);
5418 CONF("text_buffer_size") {
5420 text_buffer_size = atoi(value);
5421 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5422 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5423 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5431 if (output_methods & TO_X) {
5432 X11_initialisation();
5441 global_text = (char *) malloc(1);
5442 global_text[0] = '\0';
5445 unsigned int l = strlen(global_text);
5447 char buf[CONF_BUFF_SIZE];
5449 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5455 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5463 /* Check for continuation of \\-\n. */
5464 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5465 global_text[l-1] = '\0';
5469 global_text = (char *) realloc(global_text, l + bl + 1);
5470 strcat(global_text, buf);
5472 if (strlen(global_text) > max_user_text) {
5476 global_text_lines = line + 1;
5479 #ifdef TCP_PORT_MONITOR
5480 CONF("max_port_monitor_connections") {
5482 if (!value || (sscanf(value, "%d", &max) != 1)) {
5483 /* an error. use default, warn and continue. */
5484 tcp_portmon_set_max_connections(0);
5486 } else if (tcp_portmon_set_max_connections(max)) {
5487 /* max is < 0, default has been set*/
5492 CONF("if_up_strictness") {
5494 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5495 ifup_strictness = IFUP_UP;
5496 } else if (strcasecmp(value, "up") == EQUAL) {
5497 ifup_strictness = IFUP_UP;
5498 } else if (strcasecmp(value, "link") == EQUAL) {
5499 ifup_strictness = IFUP_LINK;
5500 } else if (strcasecmp(value, "address") == EQUAL) {
5501 ifup_strictness = IFUP_ADDR;
5503 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5504 ifup_strictness = IFUP_UP;
5508 CONF("temperature_unit") {
5510 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5511 } else if (set_temp_output_unit(value)) {
5512 NORM_ERR("temperature_unit: incorrect argument");
5519 char *ptr = strtok(value, " ");
5522 ptr = strtok(NULL, " ");
5529 CONF("lua_draw_hook_pre") {
5531 llua_set_draw_pre_hook(value);
5536 CONF("lua_draw_hook_post") {
5538 llua_set_draw_post_hook(value);
5543 CONF("lua_startup_hook") {
5545 llua_set_startup_hook(value);
5550 CONF("lua_shutdown_hook") {
5552 llua_set_shutdown_hook(value);
5558 #endif /* HAVE_LUA */
5570 CONF("default_color"){}
5571 CONF3("default_shade_color", "default_shadecolor"){}
5572 CONF3("default_outline_color", "default_outlinecolor") {}
5573 CONF("own_window_colour") {}
5576 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5582 if (info.music_player_interval == 0) {
5583 // default to update_interval
5584 info.music_player_interval = update_interval;
5586 if (!global_text) { // didn't supply any text
5587 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5589 if (!output_methods) {
5590 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5592 #if defined(NCURSES)
5594 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5595 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5596 output_methods &= ~TO_NCURSES;
5600 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5601 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5602 output_methods &= ~(TO_STDOUT | TO_STDERR);
5604 #endif /* NCURSES */
5609 static void load_config_file_x11(const char *f)
5614 fp = open_config_file(f);
5618 DBGP("reading contents from config file '%s'", f);
5621 char buff[CONF_BUFF_SIZE], *name, *value;
5622 int ret = do_config_step(&line, fp, buff, &name, &value);
5623 if (ret == CONF_BREAK) {
5625 } else if (ret == CONF_CONTINUE) {
5630 X11_initialisation();
5631 if (x_initialised == YES) {
5633 color0 = get_x11_color(value);
5640 X11_initialisation();
5641 if (x_initialised == YES) {
5643 color1 = get_x11_color(value);
5650 X11_initialisation();
5651 if (x_initialised == YES) {
5653 color2 = get_x11_color(value);
5660 X11_initialisation();
5661 if (x_initialised == YES) {
5663 color3 = get_x11_color(value);
5670 X11_initialisation();
5671 if (x_initialised == YES) {
5673 color4 = get_x11_color(value);
5680 X11_initialisation();
5681 if (x_initialised == YES) {
5683 color5 = get_x11_color(value);
5690 X11_initialisation();
5691 if (x_initialised == YES) {
5693 color6 = get_x11_color(value);
5700 X11_initialisation();
5701 if (x_initialised == YES) {
5703 color7 = get_x11_color(value);
5710 X11_initialisation();
5711 if (x_initialised == YES) {
5713 color8 = get_x11_color(value);
5720 X11_initialisation();
5721 if (x_initialised == YES) {
5723 color9 = get_x11_color(value);
5729 CONF("default_color") {
5730 X11_initialisation();
5731 if (x_initialised == YES) {
5733 default_fg_color = get_x11_color(value);
5739 CONF3("default_shade_color", "default_shadecolor") {
5740 X11_initialisation();
5741 if (x_initialised == YES) {
5743 default_bg_color = get_x11_color(value);
5749 CONF3("default_outline_color", "default_outlinecolor") {
5750 X11_initialisation();
5751 if (x_initialised == YES) {
5753 default_out_color = get_x11_color(value);
5760 CONF("own_window_colour") {
5761 X11_initialisation();
5762 if (x_initialised == YES) {
5764 background_colour = get_x11_color(value);
5766 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5767 "'#' for hex colours");
5773 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5774 if(output_methods & TO_X) {
5775 X11_initialisation();
5784 #undef CONF_CONTINUE
5785 #undef CONF_BUFF_SIZE
5793 static void print_help(const char *prog_name) {
5794 printf("Usage: %s [OPTION]...\n"
5795 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5796 "window. Command line options will override configurations defined in config\n"
5798 " -v, --version version\n"
5799 " -q, --quiet quiet mode\n"
5800 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5801 " -c, --config=FILE config file to load\n"
5802 #ifdef CONFIG_OUTPUT
5803 " -C, --print-config print the builtin default config to stdout\n"
5804 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5806 " -d, --daemonize daemonize, fork to background\n"
5807 " -h, --help help\n"
5809 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5810 " -f, --font=FONT font to use\n"
5811 " -X, --display=DISPLAY X11 display to use\n"
5813 " -o, --own-window create own window to draw\n"
5816 " -b, --double-buffer double buffer (prevents flickering)\n"
5818 " -w, --window-id=WIN_ID window id to draw\n"
5819 " -x X x position\n"
5820 " -y Y y position\n"
5822 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5823 " -u, --interval=SECS update interval\n"
5824 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5825 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5830 /* : means that character before that takes an argument */
5831 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5841 #ifdef CONFIG_OUTPUT
5846 static const struct option longopts[] = {
5847 { "help", 0, NULL, 'h' },
5848 { "version", 0, NULL, 'V' },
5849 { "quiet", 0, NULL, 'q' },
5850 { "debug", 0, NULL, 'D' },
5851 { "config", 1, NULL, 'c' },
5852 #ifdef CONFIG_OUTPUT
5853 { "print-config", 0, NULL, 'C' },
5855 { "daemonize", 0, NULL, 'd' },
5857 { "alignment", 1, NULL, 'a' },
5858 { "font", 1, NULL, 'f' },
5859 { "display", 1, NULL, 'X' },
5861 { "own-window", 0, NULL, 'o' },
5864 { "double-buffer", 0, NULL, 'b' },
5866 { "window-id", 1, NULL, 'w' },
5868 { "text", 1, NULL, 't' },
5869 { "interval", 1, NULL, 'u' },
5870 { "pause", 1, NULL, 'p' },
5874 void set_current_config(void);
5875 void set_current_config(void)
5877 /* check if specified config file is valid */
5878 if (current_config) {
5880 if (stat(current_config, &sb) ||
5881 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5882 NORM_ERR("invalid configuration file '%s'\n", current_config);
5883 free(current_config);
5888 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5890 if (!current_config) {
5891 /* load default config file */
5892 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5895 /* Try to use personal config file first */
5896 to_real_path(buf, CONFIG_FILE);
5897 if (buf[0] && (fp = fopen(buf, "r"))) {
5898 current_config = strndup(buf, max_user_text);
5902 /* Try to use system config file if personal config not readable */
5903 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5904 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5908 /* No readable config found */
5909 if (!current_config) {
5910 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5911 #ifdef BUILD_BUILTIN_CONFIG
5912 current_config = strdup("==builtin==");
5913 NORM_ERR(NOCFGFILEFOUND
5914 ", using builtin default");
5916 CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5917 #endif /* ! CONF_OUTPUT */
5922 void initialisation(int argc, char **argv) {
5923 struct sigaction act, oact;
5925 set_default_configurations();
5926 set_current_config();
5927 load_config_file(current_config);
5928 currentconffile = conftree_add(currentconffile, current_config);
5930 /* init specials array */
5931 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5932 NORM_ERR("failed to create specials array");
5936 if (current_mail_spool == NULL) {
5939 variable_substitute(MAIL_FILE, buf, 256);
5941 if (buf[0] != '\0') {
5942 current_mail_spool = strndup(buf, text_buffer_size);
5947 /* handle other command line arguments */
5949 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5950 || defined(__NetBSD__)
5951 optind = optreset = 1;
5956 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5957 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5958 "kvm_open")) == NULL) {
5959 CRIT_ERR(NULL, NULL, "cannot read kvm");
5961 pthread_mutex_init(&kvm_proc_mutex, NULL);
5965 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5974 fork_to_background = 1;
5977 global_debug_level++;
5981 set_first_font(optarg);
5984 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
6003 global_text = strndup(optarg, max_user_text);
6004 convert_escapes(global_text);
6008 update_interval = strtod(optarg, 0);
6009 update_interval_old = update_interval;
6010 if (info.music_player_interval == 0) {
6011 // default to update_interval
6012 info.music_player_interval = update_interval;
6017 total_run_times = strtod(optarg, 0);
6021 gap_x = atoi(optarg);
6025 gap_y = atoi(optarg);
6030 startup_pause = atoi(optarg);
6031 sleep(startup_pause);
6042 if (output_methods & TO_X) {
6043 load_config_file_x11(current_config);
6047 /* generate text and get initial size */
6048 extract_variable_text(global_text);
6055 if (fork_to_background && first_pass) {
6060 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
6067 fprintf(stderr, "\n");
6072 /* parent process */
6073 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
6080 start_update_threading();
6082 text_buffer = malloc(max_user_text);
6083 memset(text_buffer, 0, max_user_text);
6084 tmpstring1 = malloc(text_buffer_size);
6085 memset(tmpstring1, 0, text_buffer_size);
6086 tmpstring2 = malloc(text_buffer_size);
6087 memset(tmpstring2, 0, text_buffer_size);
6092 X11_create_window();
6095 llua_setup_info(&info, update_interval);
6096 #endif /* HAVE_LUA */
6101 /* Set signal handlers */
6102 act.sa_handler = signal_handler;
6103 sigemptyset(&act.sa_mask);
6106 act.sa_flags |= SA_RESTART;
6109 if ( sigaction(SIGINT, &act, &oact) < 0
6110 || sigaction(SIGALRM, &act, &oact) < 0
6111 || sigaction(SIGUSR1, &act, &oact) < 0
6112 || sigaction(SIGHUP, &act, &oact) < 0
6113 || sigaction(SIGTERM, &act, &oact) < 0) {
6114 NORM_ERR("error setting signal handler: %s", strerror(errno));
6118 llua_startup_hook();
6119 #endif /* HAVE_LUA */
6122 int main(int argc, char **argv)
6131 g_signal_pending = 0;
6132 max_user_text = MAX_USER_TEXT_DEFAULT;
6134 memset(&info, 0, sizeof(info));
6138 #ifdef TCP_PORT_MONITOR
6139 /* set default connection limit */
6140 tcp_portmon_set_max_connections(0);
6144 if(curl_global_init(CURL_GLOBAL_ALL))
6145 NORM_ERR("curl_global_init() failed, you may not be able to use curl variables");
6148 /* handle command line parameters that don't change configs */
6150 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
6151 || ((s = getenv("LANG")) && *s)) {
6152 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6154 NORM_ERR("malloc failed");
6156 for (x = 0; x < strlen(s); x++) {
6157 temp[x] = tolower(s[x]);
6160 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6166 if (!setlocale(LC_CTYPE, "")) {
6167 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6171 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6182 if (current_config) {
6183 free(current_config);
6185 current_config = strndup(optarg, max_user_text);
6188 if (!freopen("/dev/null", "w", stderr)) {
6189 NORM_ERR("unable to redirect stderr to /dev/null");
6193 print_help(argv[0]);
6195 #ifdef CONFIG_OUTPUT
6202 window.window = strtol(optarg, 0, 0);
6207 disp = strdup(optarg);
6216 set_current_config();
6219 /* Load xoap keys, if existing */
6223 #ifdef HAVE_SYS_INOTIFY_H
6224 inotify_fd = inotify_init();
6225 if(inotify_fd != -1) {
6228 fl = fcntl(inotify_fd, F_GETFL);
6229 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
6231 #endif /* HAVE_SYS_INOTIFY_H */
6233 initialisation(argc, argv);
6235 first_pass = 0; /* don't ever call fork() again */
6237 //dbus code from http://www.matthew.ath.cx/misc/dbus
6238 dbus_error_init(&err);
6239 // connect to the bus
6240 conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
6241 if (dbus_error_is_set(&err)) {
6242 fprintf(stderr, "Connection Error (%s)\n", err.message);
6243 dbus_error_free(&err);
6245 conn2 = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
6246 if (dbus_error_is_set(&err)) {
6247 fprintf(stderr, "Connection Error (%s)\n", err.message);
6248 dbus_error_free(&err);
6250 if (NULL == conn || NULL == conn2) {
6253 // request a name on the bus (we don't use it for now)
6254 ret = dbus_bus_request_name(conn, "net.appcheck.Conky",
6255 DBUS_NAME_FLAG_REPLACE_EXISTING
6257 if (dbus_error_is_set(&err)) {
6258 fprintf(stderr, "Name Error (%s)\n", err.message);
6259 dbus_error_free(&err);
6261 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
6262 //dbus_connection_close(conn);
6263 fprintf(stderr, "some other dbus Name Error\n");
6270 dbus_connection_close(conn);
6271 dbus_connection_close(conn2);
6274 curl_global_cleanup();
6277 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6279 pthread_mutex_destroy(&kvm_proc_mutex);
6286 void alarm_handler(void) {
6288 kill(childpid, SIGTERM);
6292 static void signal_handler(int sig)
6294 /* signal handler is light as a feather, as it should be.
6295 * we will poll g_signal_pending with each loop of conky
6296 * and do any signal processing there, NOT here (except
6297 * SIGALRM because this is caused when conky is hanging) */
6298 if(sig == SIGALRM) {
6301 g_signal_pending = sig;