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);
892 OBJ(battery_percent) {
893 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
896 new_bar(obj, p, p_max_size, get_battery_perct_bar(obj->data.s));
899 get_battery_short_status(p, p_max_size, obj->data.s);
901 OBJ(cell_radio_dbm) {
902 get_dbus_stuff(p, p_max_size, DBUS_CELL_DBM);
904 OBJ(cell_radio_percent) {
905 get_dbus_stuff(p, p_max_size, DBUS_CELL_PERCENT);
907 #endif /* __OpenBSD__ */
910 human_readable(cur->buffers * 1024, p, 255);
913 human_readable(cur->cached * 1024, p, 255);
915 OBJ(cmdline_to_pid) {
916 print_cmdline_to_pid(obj, p, p_max_size);
919 if (cur->cpu_usage) {
920 if (obj->data.i > info.cpu_count) {
921 NORM_ERR("obj->data.i %i info.cpu_count %i",
922 obj->data.i, info.cpu_count);
923 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
925 percent_print(p, p_max_size,
926 round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
930 new_gauge(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
932 new_bar(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
936 new_graph(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
939 print_loadgraph(obj, p, p_max_size);
943 new_fg(p, obj->data.l);
977 #if defined(__linux__)
979 snprintf(p, p_max_size, "%s",
980 get_disk_protect_queue(obj->data.s));
983 print_i8k_version(obj, p, p_max_size);
986 print_i8k_bios(obj, p, p_max_size);
989 print_i8k_serial(obj, p, p_max_size);
992 print_i8k_cpu_temp(obj, p, p_max_size);
994 OBJ(i8k_left_fan_status) {
995 print_i8k_left_fan_status(obj, p, p_max_size);
997 OBJ(i8k_right_fan_status) {
998 print_i8k_right_fan_status(obj, p, p_max_size);
1000 OBJ(i8k_left_fan_rpm) {
1001 print_i8k_left_fan_rpm(obj, p, p_max_size);
1003 OBJ(i8k_right_fan_rpm) {
1004 print_i8k_right_fan_rpm(obj, p, p_max_size);
1006 OBJ(i8k_ac_status) {
1007 print_i8k_ac_status(obj, p, p_max_size);
1009 OBJ(i8k_buttons_status) {
1010 print_i8k_buttons_status(obj, p, p_max_size);
1014 get_ibm_acpi_fan(obj, p, p_max_size);
1017 print_ibm_temps(obj, p, p_max_size);
1020 get_ibm_acpi_volume(obj, p, p_max_size);
1022 OBJ(ibm_brightness) {
1023 get_ibm_acpi_brightness(obj, p, p_max_size);
1026 /* information from sony_laptop kernel module
1027 * /sys/devices/platform/sony-laptop */
1028 OBJ(sony_fanspeed) {
1029 get_sony_fanspeed(p, p_max_size);
1032 if (!gateway_exists()) {
1037 print_gateway_iface(p, p_max_size);
1040 print_gateway_ip(p, p_max_size);
1043 snprintf(p, p_max_size, "%d", get_laptop_mode());
1046 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1048 #endif /* __linux__ */
1049 #if (defined(__FreeBSD__) || defined(__linux__))
1051 if (!interface_up(obj)) {
1057 OBJ(obsd_sensors_temp) {
1058 print_obsd_sensors_temp(obj, p, p_max_size);
1060 OBJ(obsd_sensors_fan) {
1061 print_obsd_sensors_fan(obj, p, p_max_size);
1063 OBJ(obsd_sensors_volt) {
1064 print_obsd_sensors_volt(obj, p, p_max_size);
1067 get_obsd_vendor(p, p_max_size);
1070 get_obsd_product(p, p_max_size);
1072 #endif /* __OpenBSD__ */
1075 new_font(p, obj->data.s);
1076 need_to_load_fonts = 1;
1080 print_diskio(obj, p, p_max_size);
1083 print_diskio_write(obj, p, p_max_size);
1086 print_diskio_read(obj, p, p_max_size);
1090 print_diskiograph(obj, p, p_max_size);
1092 OBJ(diskiograph_read) {
1093 print_diskiograph_read(obj, p, p_max_size);
1095 OBJ(diskiograph_write) {
1096 print_diskiograph_write(obj, p, p_max_size);
1100 print_downspeed(obj, p, p_max_size);
1103 print_downspeedf(obj, p, p_max_size);
1106 OBJ(downspeedgraph) {
1107 print_downspeedgraph(obj, p, p_max_size);
1111 /* Since we see you, you're if has not jumped.
1112 * Do Ninja jump here: without leaving traces.
1113 * This is to prevent us from stale jumped flags.
1115 obj = obj->ifblock_next;
1119 /* harmless object, just ignore */
1122 print_addr(obj, p, p_max_size);
1124 #if defined(__linux__)
1126 print_addrs(obj, p, p_max_size);
1128 #endif /* __linux__ */
1129 #if defined(IMLIB2) && defined(X11)
1131 /* doesn't actually draw anything, just queues it omp. the
1132 * image will get drawn after the X event loop */
1133 cimlib_add_image(obj->data.s);
1137 evaluate(obj->data.s, p, p_max_size);
1140 print_exec(obj, p, p_max_size);
1143 print_execp(obj, p, p_max_size);
1146 print_execgauge(obj, p, p_max_size);
1149 print_execbar(obj, p, p_max_size);
1153 print_execgraph(obj, p, p_max_size);
1157 print_execibar(obj, p, p_max_size);
1161 print_execigraph(obj, p, p_max_size);
1165 print_execigauge(obj, p, p_max_size);
1168 print_execi(obj, p, p_max_size);
1171 print_execpi(obj, p, p_max_size);
1174 print_texeci(obj, p, p_max_size);
1177 print_imap_unseen(obj, p, p_max_size);
1179 OBJ(imap_messages) {
1180 print_imap_messages(obj, p, p_max_size);
1183 print_pop3_unseen(obj, p, p_max_size);
1186 print_pop3_used(obj, p, p_max_size);
1189 print_fs_bar(obj, 0, p, p_max_size);
1192 print_fs_free(obj, p, p_max_size);
1195 print_fs_perc(obj, 1, p, p_max_size);
1198 print_fs_size(obj, p, p_max_size);
1201 print_fs_type(obj, p, p_max_size);
1204 print_fs_used(obj, p, p_max_size);
1207 print_fs_bar(obj, 1, p, p_max_size);
1210 print_fs_perc(obj, 0, p, p_max_size);
1213 print_loadavg(obj, p, p_max_size);
1216 new_goto(p, obj->data.i);
1223 new_hr(p, obj->data.i);
1227 print_nameserver(obj, p, p_max_size);
1231 print_eve(obj, p, p_max_size);
1236 curl_print(obj, p, p_max_size);
1241 rss_print_info(obj, p, p_max_size);
1246 print_weather(obj, p, p_max_size);
1250 OBJ(weather_forecast) {
1251 print_weather_forecast(obj, p, p_max_size);
1256 print_lua(obj, p, p_max_size);
1259 print_lua_parse(obj, p, p_max_size);
1262 print_lua_bar(obj, p, p_max_size);
1266 print_lua_graph(obj, p, p_max_size);
1270 print_lua_gauge(obj, p, p_max_size);
1272 #endif /* HAVE_LUA */
1278 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1279 snprintf(p, p_max_size, "N/A");
1281 temp_print(p, p_max_size, (double)val,
1282 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1287 new_offset(p, obj->data.i);
1290 new_voffset(p, obj->data.i);
1294 print_sysfs_sensor(obj, p, p_max_size);
1297 print_sysfs_sensor(obj, p, p_max_size);
1300 print_sysfs_sensor(obj, p, p_max_size);
1302 #endif /* __linux__ */
1304 new_alignr(p, obj->data.i);
1307 new_alignc(p, obj->data.i);
1310 char buf[max_user_text];
1311 struct information *tmp_info =
1312 malloc(sizeof(struct information));
1313 memcpy(tmp_info, cur, sizeof(struct information));
1314 generate_text_internal(buf, max_user_text,
1315 *obj->sub, tmp_info);
1317 if (strlen(buf) != 0) {
1323 char expression[max_user_text];
1325 struct information *tmp_info;
1327 tmp_info = malloc(sizeof(struct information));
1328 memcpy(tmp_info, cur, sizeof(struct information));
1329 generate_text_internal(expression, max_user_text,
1330 *obj->sub, tmp_info);
1331 DBGP("parsed arg into '%s'", expression);
1333 val = compare(expression);
1335 NORM_ERR("compare failed for expression '%s'",
1345 spc = strchr(obj->data.s, ' ');
1346 if (!spc && access(obj->data.s, F_OK)) {
1350 if (!check_contains(obj->data.s, spc + 1))
1357 && (!check_mount(obj->data.s))) {
1363 if (!get_process_by_name(obj->data.s)) {
1365 if ((obj->data.s) && system(obj->data.s)) {
1370 #if defined(__linux__)
1372 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1376 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1379 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1384 human_readable(cur->mem * 1024, p, 255);
1387 human_readable(cur->memeasyfree * 1024, p, 255);
1390 human_readable(cur->memfree * 1024, p, 255);
1393 human_readable(cur->memmax * 1024, p, 255);
1397 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1400 new_gauge(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1403 new_bar(obj, p, p_max_size, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1407 new_graph(obj, p, p_max_size, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1412 print_mixer(obj, p, p_max_size);
1415 print_mixerl(obj, p, p_max_size);
1418 print_mixerr(obj, p, p_max_size);
1421 print_mixer_bar(obj, p, p_max_size);
1424 print_mixerl_bar(obj, p, p_max_size);
1427 print_mixerr_bar(obj, p, p_max_size);
1429 OBJ(if_mixer_mute) {
1430 if (!check_mixer_muted(obj)) {
1435 #define NOT_IN_X "Not running in X"
1437 if(x_initialised != YES) {
1438 strncpy(p, NOT_IN_X, p_max_size);
1440 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1443 OBJ(monitor_number) {
1444 if(x_initialised != YES) {
1445 strncpy(p, NOT_IN_X, p_max_size);
1447 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1451 if(x_initialised != YES) {
1452 strncpy(p, NOT_IN_X, p_max_size);
1454 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1457 OBJ(desktop_number) {
1458 if(x_initialised != YES) {
1459 strncpy(p, NOT_IN_X, p_max_size);
1461 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1465 if(x_initialised != YES) {
1466 strncpy(p, NOT_IN_X, p_max_size);
1467 }else if(cur->x11.desktop.name != NULL) {
1468 strncpy(p, cur->x11.desktop.name, p_max_size);
1474 char buf[max_user_text];
1476 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1478 print_format_time(obj, p, p_max_size);
1482 print_mails(obj, p, p_max_size);
1485 print_new_mails(obj, p, p_max_size);
1488 print_seen_mails(obj, p, p_max_size);
1491 print_unseen_mails(obj, p, p_max_size);
1493 OBJ(flagged_mails) {
1494 print_flagged_mails(obj, p, p_max_size);
1496 OBJ(unflagged_mails) {
1497 print_unflagged_mails(obj, p, p_max_size);
1499 OBJ(forwarded_mails) {
1500 print_forwarded_mails(obj, p, p_max_size);
1502 OBJ(unforwarded_mails) {
1503 print_unforwarded_mails(obj, p, p_max_size);
1505 OBJ(replied_mails) {
1506 print_replied_mails(obj, p, p_max_size);
1508 OBJ(unreplied_mails) {
1509 print_unreplied_mails(obj, p, p_max_size);
1512 print_draft_mails(obj, p, p_max_size);
1514 OBJ(trashed_mails) {
1515 print_trashed_mails(obj, p, p_max_size);
1518 print_mboxscan(obj, p, p_max_size);
1521 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1523 OBJ(nodename_short) {
1525 pos = strstr(cur->uname_s.nodename, ".");
1527 snprintf(p, MIN(pos-cur->uname_s.nodename+1, p_max_size), "%s", cur->uname_s.nodename);
1529 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1533 new_outline(p, obj->data.l);
1536 char buf[max_user_text];
1538 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1540 print_pid_chroot(obj, p, p_max_size);
1543 char buf[max_user_text];
1545 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1547 print_pid_cmdline(obj, p, p_max_size);
1550 char buf[max_user_text];
1552 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1554 print_pid_cwd(obj, p, p_max_size);
1557 char buf[max_user_text];
1559 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1561 print_pid_environ(obj, p, p_max_size);
1563 OBJ(pid_environ_list) {
1564 char buf[max_user_text];
1566 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1568 print_pid_environ_list(obj, p, p_max_size);
1571 char buf[max_user_text];
1573 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1575 print_pid_exe(obj, p, p_max_size);
1578 char buf[max_user_text];
1580 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1582 print_pid_nice(obj, p, p_max_size);
1584 OBJ(pid_openfiles) {
1585 char buf[max_user_text];
1587 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1589 print_pid_openfiles(obj, p, p_max_size);
1592 char buf[max_user_text];
1594 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1596 print_pid_parent(obj, p, p_max_size);
1599 char buf[max_user_text];
1601 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1603 print_pid_priority(obj, p, p_max_size);
1606 char buf[max_user_text];
1608 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1610 print_pid_state(obj, p, p_max_size);
1612 OBJ(pid_state_short) {
1613 char buf[max_user_text];
1615 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1617 print_pid_state_short(obj, p, p_max_size);
1620 char buf[max_user_text];
1622 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1624 print_pid_stderr(obj, p, p_max_size);
1627 char buf[max_user_text];
1629 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1631 print_pid_stdin(obj, p, p_max_size);
1634 char buf[max_user_text];
1636 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1638 print_pid_stdout(obj, p, p_max_size);
1641 char buf[max_user_text];
1643 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1645 print_pid_threads(obj, p, p_max_size);
1647 OBJ(pid_thread_list) {
1648 char buf[max_user_text];
1650 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1652 print_pid_thread_list(obj, p, p_max_size);
1654 OBJ(pid_time_kernelmode) {
1655 char buf[max_user_text];
1657 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1659 print_pid_time_kernelmode(obj, p, p_max_size);
1661 OBJ(pid_time_usermode) {
1662 char buf[max_user_text];
1664 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1666 print_pid_time_usermode(obj, p, p_max_size);
1669 char buf[max_user_text];
1671 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1673 print_pid_time(obj, p, p_max_size);
1676 char buf[max_user_text];
1678 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1680 print_pid_uid(obj, p, p_max_size);
1683 char buf[max_user_text];
1685 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1687 print_pid_euid(obj, p, p_max_size);
1690 char buf[max_user_text];
1692 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1694 print_pid_suid(obj, p, p_max_size);
1697 char buf[max_user_text];
1699 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1701 print_pid_fsuid(obj, p, p_max_size);
1704 char buf[max_user_text];
1706 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1708 print_pid_gid(obj, p, p_max_size);
1711 char buf[max_user_text];
1713 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1715 print_pid_egid(obj, p, p_max_size);
1718 char buf[max_user_text];
1720 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1722 print_pid_sgid(obj, p, p_max_size);
1725 char buf[max_user_text];
1727 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1729 print_pid_fsgid(obj, p, p_max_size);
1732 char buf[max_user_text];
1734 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1736 print_pid_read(obj, p, p_max_size);
1739 char buf[max_user_text];
1741 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1743 print_pid_vmpeak(obj, p, p_max_size);
1746 char buf[max_user_text];
1748 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1750 print_pid_vmsize(obj, p, p_max_size);
1753 char buf[max_user_text];
1755 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1757 print_pid_vmlck(obj, p, p_max_size);
1760 char buf[max_user_text];
1762 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1764 print_pid_vmhwm(obj, p, p_max_size);
1767 char buf[max_user_text];
1769 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1771 print_pid_vmrss(obj, p, p_max_size);
1774 char buf[max_user_text];
1776 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1778 print_pid_vmdata(obj, p, p_max_size);
1781 char buf[max_user_text];
1783 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1785 print_pid_vmstk(obj, p, p_max_size);
1788 char buf[max_user_text];
1790 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1792 print_pid_vmexe(obj, p, p_max_size);
1795 char buf[max_user_text];
1797 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1799 print_pid_vmlib(obj, p, p_max_size);
1802 char buf[max_user_text];
1804 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1806 print_pid_vmpte(obj, p, p_max_size);
1809 char buf[max_user_text];
1811 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1813 print_pid_write(obj, p, p_max_size);
1816 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1818 OBJ(running_processes) {
1819 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1821 OBJ(running_threads) {
1822 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1825 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1828 snprintf(p, p_max_size, "%s", obj->data.s);
1832 new_bg(p, obj->data.l);
1835 new_stippled_hr(obj, p);
1839 human_readable(cur->swap * 1024, p, 255);
1842 human_readable(cur->swapfree * 1024, p, 255);
1845 human_readable(cur->swapmax * 1024, p, 255);
1848 if (cur->swapmax == 0) {
1849 strncpy(p, "No swap", p_max_size);
1851 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1855 new_bar(obj, p, p_max_size, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1858 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1861 print_time(obj, p, p_max_size);
1864 print_utime(obj, p, p_max_size);
1867 print_tztime(obj, p, p_max_size);
1870 print_totaldown(obj, p, p_max_size);
1873 print_totalup(obj, p, p_max_size);
1876 char buf[max_user_text];
1878 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1880 print_gid_name(obj, p, p_max_size);
1883 char buf[max_user_text];
1885 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1887 print_uid_name(obj, p, p_max_size);
1890 snprintf(p, p_max_size, "%d", total_updates);
1893 if(total_updates % updatereset != obj->data.i - 1) {
1898 print_upspeed(obj, p, p_max_size);
1901 print_upspeedf(obj, p, p_max_size);
1905 print_upspeedgraph(obj, p, p_max_size);
1909 format_seconds_short(p, p_max_size, (int) cur->uptime);
1912 format_seconds(p, p_max_size, (int) cur->uptime);
1916 snprintf(p, p_max_size, "%s", cur->users.names);
1919 snprintf(p, p_max_size, "%s", cur->users.terms);
1922 snprintf(p, p_max_size, "%s", cur->users.times);
1925 update_user_time(obj->data.s);
1926 snprintf(p, p_max_size, "%s", cur->users.ctime);
1929 snprintf(p, p_max_size, "%d", cur->users.number);
1931 #endif /* __linux__ */
1932 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1933 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1937 msg = get_apm_adapter();
1938 snprintf(p, p_max_size, "%s", msg);
1941 OBJ(apm_battery_life) {
1944 msg = get_apm_battery_life();
1945 snprintf(p, p_max_size, "%s", msg);
1948 OBJ(apm_battery_time) {
1951 msg = get_apm_battery_time();
1952 snprintf(p, p_max_size, "%s", msg);
1955 #endif /* __FreeBSD__ __OpenBSD__ */
1959 print_mpd_title(obj, p, p_max_size);
1961 print_mpd_artist(obj, p, p_max_size);
1963 print_mpd_album(obj, p, p_max_size);
1965 print_mpd_random(obj, p, p_max_size);
1967 print_mpd_repeat(obj, p, p_max_size);
1969 print_mpd_track(obj, p, p_max_size);
1971 print_mpd_name(obj, p, p_max_size);
1973 print_mpd_file(obj, p, p_max_size);
1975 print_mpd_vol(obj, p, p_max_size);
1977 print_mpd_bitrate(obj, p, p_max_size);
1979 print_mpd_status(obj, p, p_max_size);
1981 print_mpd_elapsed(obj, p, p_max_size);
1984 print_mpd_length(obj, p, p_max_size);
1987 print_mpd_percent(obj, p, p_max_size);
1990 print_mpd_bar(obj, p, p_max_size);
1993 print_mpd_smart(obj, p, p_max_size);
1995 OBJ(if_mpd_playing) {
1996 if (!mpd_get_info()->is_playing) {
2003 #define MOC_PRINT(t, a) \
2004 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2006 MOC_PRINT(state, "??");
2009 MOC_PRINT(file, "no file");
2012 MOC_PRINT(title, "no title");
2015 MOC_PRINT(artist, "no artist");
2018 MOC_PRINT(song, "no song");
2021 MOC_PRINT(album, "no album");
2023 OBJ(moc_totaltime) {
2024 MOC_PRINT(totaltime, "0:00");
2027 MOC_PRINT(timeleft, "0:00");
2030 MOC_PRINT(curtime, "0:00");
2033 MOC_PRINT(bitrate, "0Kbps");
2036 MOC_PRINT(rate, "0KHz");
2042 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2045 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2048 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2051 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2053 OBJ(xmms2_comment) {
2054 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2057 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2060 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2063 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2065 OBJ(xmms2_tracknr) {
2066 if (cur->xmms2.tracknr != -1) {
2067 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2070 OBJ(xmms2_bitrate) {
2071 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2074 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2077 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2079 OBJ(xmms2_elapsed) {
2080 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2081 (cur->xmms2.elapsed / 1000) % 60);
2083 OBJ(xmms2_duration) {
2084 snprintf(p, p_max_size, "%02d:%02d",
2085 cur->xmms2.duration / 60000,
2086 (cur->xmms2.duration / 1000) % 60);
2088 OBJ(xmms2_percent) {
2089 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2093 new_bar(obj, p, p_max_size, (int) (cur->xmms2.progress * 255.0f));
2096 OBJ(xmms2_playlist) {
2097 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2099 OBJ(xmms2_timesplayed) {
2100 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2103 if (strlen(cur->xmms2.title) < 2
2104 && strlen(cur->xmms2.title) < 2) {
2105 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2107 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2111 OBJ(if_xmms2_connected) {
2112 if (cur->xmms2.conn_state != 1) {
2118 OBJ(audacious_status) {
2119 snprintf(p, p_max_size, "%s",
2120 cur->audacious.items[AUDACIOUS_STATUS]);
2122 OBJ(audacious_title) {
2123 snprintf(p, cur->audacious.max_title_len > 0
2124 ? cur->audacious.max_title_len : p_max_size, "%s",
2125 cur->audacious.items[AUDACIOUS_TITLE]);
2127 OBJ(audacious_length) {
2128 snprintf(p, p_max_size, "%s",
2129 cur->audacious.items[AUDACIOUS_LENGTH]);
2131 OBJ(audacious_length_seconds) {
2132 snprintf(p, p_max_size, "%s",
2133 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2135 OBJ(audacious_position) {
2136 snprintf(p, p_max_size, "%s",
2137 cur->audacious.items[AUDACIOUS_POSITION]);
2139 OBJ(audacious_position_seconds) {
2140 snprintf(p, p_max_size, "%s",
2141 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2143 OBJ(audacious_bitrate) {
2144 snprintf(p, p_max_size, "%s",
2145 cur->audacious.items[AUDACIOUS_BITRATE]);
2147 OBJ(audacious_frequency) {
2148 snprintf(p, p_max_size, "%s",
2149 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2151 OBJ(audacious_channels) {
2152 snprintf(p, p_max_size, "%s",
2153 cur->audacious.items[AUDACIOUS_CHANNELS]);
2155 OBJ(audacious_filename) {
2156 snprintf(p, p_max_size, "%s",
2157 cur->audacious.items[AUDACIOUS_FILENAME]);
2159 OBJ(audacious_playlist_length) {
2160 snprintf(p, p_max_size, "%s",
2161 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2163 OBJ(audacious_playlist_position) {
2164 snprintf(p, p_max_size, "%s",
2165 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2167 OBJ(audacious_main_volume) {
2168 snprintf(p, p_max_size, "%s",
2169 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2172 OBJ(audacious_bar) {
2176 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2177 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2178 new_bar(obj, p, p_max_size, (int) (progress * 255.0f));
2181 #endif /* AUDACIOUS */
2185 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2188 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2191 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2194 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2197 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2200 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2203 /* we have four different types of top (top, top_mem,
2204 * top_time and top_io). To avoid having almost-same code four
2205 * times, we have this special handler. */
2213 print_top(obj, p, p_max_size);
2215 print_tailhead("tail", obj, p, p_max_size);
2218 print_tailhead("head", obj, p, p_max_size);
2221 print_lines(obj, p, p_max_size);
2225 print_words(obj, p, p_max_size);
2227 #ifdef TCP_PORT_MONITOR
2229 tcp_portmon_action(obj, p, p_max_size);
2231 #endif /* TCP_PORT_MONITOR */
2235 do_iconv_start(obj);
2240 #endif /* HAVE_ICONV */
2242 OBJ(entropy_avail) {
2243 print_entropy_avail(obj, p, p_max_size);
2246 print_entropy_perc(obj, p, p_max_size);
2248 OBJ(entropy_poolsize) {
2249 print_entropy_poolsize(obj, p, p_max_size);
2252 print_entropy_bar(obj, p, p_max_size);
2256 print_smapi(obj, p, p_max_size);
2258 OBJ(if_smapi_bat_installed) {
2260 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2261 if(!smapi_bat_installed(idx)) {
2265 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2267 OBJ(smapi_bat_perc) {
2268 print_smapi_bat_perc(obj, p, p_max_size);
2270 OBJ(smapi_bat_temp) {
2271 print_smapi_bat_temp(obj, p, p_max_size);
2273 OBJ(smapi_bat_power) {
2274 print_smapi_bat_power(obj, p, p_max_size);
2276 OBJ(smapi_bat_bar) {
2277 print_smapi_bat_bar(obj, p, p_max_size);
2282 char buf[max_user_text];
2284 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2285 snprintf(p, p_max_size, "%s", buf);
2291 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2292 char buf[max_user_text];
2295 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2296 snprintf(p, p_max_size, "%s", buf);
2297 if(total_updates % 2) {
2298 for(j=0; p[j] != 0; j++) {
2303 OBJ(to_bytes) {//commented code is original - not working when input value contains a decimal point
2304 char buf[max_user_text];
2307 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2309 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2310 // if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2311 // if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2312 // else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2313 // else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2314 // else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2315 // else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2317 if(sscanf(buf, "%Lf%s", &bytes, unit) == 2 && strlen(unit) < 16){
2318 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes);
2319 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024);
2320 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024);
2321 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024 * 1024);
2322 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%Lf", bytes * 1024 * 1024 * 1024 * 1024);
2324 snprintf(p, p_max_size, "%s", buf);
2327 print_scroll(obj, p, p_max_size, cur);
2330 print_combine(obj, p, cur);
2334 print_nvidia_value(obj, p, p_max_size);
2339 /* This is just a meta-object to set host:port */
2342 snprintf(p, p_max_size, "%s",
2343 cur->apcupsd.items[APCUPSD_NAME]);
2345 OBJ(apcupsd_model) {
2346 snprintf(p, p_max_size, "%s",
2347 cur->apcupsd.items[APCUPSD_MODEL]);
2349 OBJ(apcupsd_upsmode) {
2350 snprintf(p, p_max_size, "%s",
2351 cur->apcupsd.items[APCUPSD_UPSMODE]);
2353 OBJ(apcupsd_cable) {
2354 snprintf(p, p_max_size, "%s",
2355 cur->apcupsd.items[APCUPSD_CABLE]);
2357 OBJ(apcupsd_status) {
2358 snprintf(p, p_max_size, "%s",
2359 cur->apcupsd.items[APCUPSD_STATUS]);
2361 OBJ(apcupsd_linev) {
2362 snprintf(p, p_max_size, "%s",
2363 cur->apcupsd.items[APCUPSD_LINEV]);
2366 snprintf(p, p_max_size, "%s",
2367 cur->apcupsd.items[APCUPSD_LOAD]);
2369 OBJ(apcupsd_loadbar) {
2371 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2372 new_bar(obj, p, p_max_size, (int) progress);
2375 OBJ(apcupsd_loadgraph) {
2377 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2378 new_graph(obj, p, p_max_size, (int)progress);
2381 OBJ(apcupsd_loadgauge) {
2383 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2384 new_gauge(obj, p, p_max_size, (int)progress);
2386 OBJ(apcupsd_charge) {
2387 snprintf(p, p_max_size, "%s",
2388 cur->apcupsd.items[APCUPSD_CHARGE]);
2390 OBJ(apcupsd_timeleft) {
2391 snprintf(p, p_max_size, "%s",
2392 cur->apcupsd.items[APCUPSD_TIMELEFT]);
2395 snprintf(p, p_max_size, "%s",
2396 cur->apcupsd.items[APCUPSD_TEMP]);
2398 OBJ(apcupsd_lastxfer) {
2399 snprintf(p, p_max_size, "%s",
2400 cur->apcupsd.items[APCUPSD_LASTXFER]);
2402 #endif /* APCUPSD */
2409 size_t a = strlen(p);
2412 iconv_convert(&a, buff_in, p, p_max_size);
2413 #endif /* HAVE_ICONV */
2414 if (obj->type == OBJ_execp || obj->type == OBJ_execpi || obj->type
2417 || obj->type == OBJ_lua || obj->type == OBJ_lua_parse
2418 #endif /* HAVE_LUA */
2420 substitute_newlines(p, a - 2);
2429 /* load any new fonts we may have had */
2430 if (need_to_load_fonts) {
2436 void evaluate(const char *text, char *p, int p_max_size)
2438 struct information *tmp_info;
2439 struct text_object subroot;
2441 tmp_info = malloc(sizeof(struct information));
2442 memcpy(tmp_info, &info, sizeof(struct information));
2443 parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2444 DBGP2("evaluated '%s' to '%s'", text, p);
2446 free_text_objects(&subroot, 1);
2450 double current_update_time, next_update_time, last_update_time;
2452 static void generate_text(void)
2454 struct information *cur = &info;
2456 unsigned int i, j, k;
2462 current_update_time = get_time();
2466 /* add things to the buffer */
2472 generate_text_internal(p, max_user_text, global_root_object, cur);
2473 if(max_text_width > 0) {
2474 for(i = 0, j = 0; p[i] != 0; i++) {
2475 if(p[i] == '\n') j = 0;
2476 else if(j == max_text_width) {
2477 k = i + strlen(p + i) + 1;
2478 if(k < text_buffer_size) {
2485 } else NORM_ERR("The end of the text_buffer is reached, increase \"text_buffer_size\"");
2490 if (stuff_in_uppercase) {
2493 tmp_p = text_buffer;
2495 *tmp_p = toupper(*tmp_p);
2500 next_update_time += update_interval;
2501 if (next_update_time < get_time()) {
2502 next_update_time = get_time() + update_interval;
2503 } else if (next_update_time > get_time() + update_interval) {
2504 next_update_time = get_time() + update_interval;
2506 last_update_time = current_update_time;
2510 void set_update_interval(double interval)
2512 update_interval = interval;
2513 update_interval_old = interval;
2516 int get_string_width(const char *s)
2518 return *s ? calc_text_width(s) : 0;
2522 static int get_string_width_special(char *s, int special_index)
2532 if ((output_methods & TO_X) == 0)
2535 p = strndup(s, text_buffer_size);
2539 if (*p == SPECIAL_CHAR) {
2540 /* shift everything over by 1 so that the special char
2541 * doesn't mess up the size calculation */
2542 for (i = 0; i < (long)strlen(p); i++) {
2543 *(p + i) = *(p + i + 1);
2545 if (specials[special_index + idx].type == GRAPH
2546 || specials[special_index + idx].type == GAUGE
2547 || specials[special_index + idx].type == BAR) {
2548 width += specials[special_index + idx].width;
2551 } else if (*p == SECRIT_MULTILINE_CHAR) {
2558 if (strlen(final) > 1) {
2559 width += calc_text_width(final);
2565 static int text_size_updater(char *s, int special_index);
2567 int last_font_height;
2568 static void update_text_area(void)
2572 if ((output_methods & TO_X) == 0)
2574 /* update text size if it isn't fixed */
2579 text_width = minimum_width;
2581 last_font_height = font_height();
2582 for_each_line(text_buffer, text_size_updater);
2584 if (text_height < minimum_height) {
2585 text_height = minimum_height;
2587 if (text_width > maximum_width && maximum_width > 0) {
2588 text_width = maximum_width;
2592 /* get text position on workarea */
2593 switch (text_alignment) {
2594 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2598 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2599 y = workarea[3] - text_height - gap_y;
2602 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2603 y = workarea[3] / 2 - text_height / 2 - gap_y;
2606 switch (text_alignment) {
2607 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2611 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2612 x = workarea[2] - text_width - gap_x;
2615 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2616 x = workarea[2] / 2 - text_width / 2 - gap_x;
2620 if (text_alignment == NONE) { // Let the WM manage the window
2627 #endif /* OWN_WINDOW */
2630 if (own_window && !fixed_pos) {
2633 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2634 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2635 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2636 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2640 /* If window size doesn't match to workarea's size,
2641 * then window probably includes panels (gnome).
2642 * Blah, doesn't work on KDE. */
2643 if (workarea[2] != window.width || workarea[3] != window.height) {
2652 /* update lua window globals */
2653 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2654 #endif /* HAVE_LUA */
2659 static int cur_x, cur_y; /* current x and y for drawing */
2661 //draw_mode also without X11 because we only need to print to stdout with FG
2662 static int draw_mode; /* FG, BG or OUTLINE */
2664 static long current_color;
2666 static int text_size_updater(char *s, int special_index)
2670 int contain_SECRIT_MULTILINE_CHAR = 0;
2673 if ((output_methods & TO_X) == 0)
2675 /* get string widths and skip specials */
2678 if (*p == SPECIAL_CHAR) {
2680 w += get_string_width(s);
2683 if (specials[special_index].type == BAR
2684 || specials[special_index].type == GAUGE
2685 || specials[special_index].type == GRAPH) {
2686 w += specials[special_index].width;
2687 if (specials[special_index].height > last_font_height) {
2688 last_font_height = specials[special_index].height;
2689 last_font_height += font_height();
2691 } else if (specials[special_index].type == OFFSET) {
2692 if (specials[special_index].arg > 0) {
2693 w += specials[special_index].arg;
2695 } else if (specials[special_index].type == VOFFSET) {
2696 last_font_height += specials[special_index].arg;
2697 } else if (specials[special_index].type == GOTO) {
2698 if (specials[special_index].arg > cur_x) {
2699 w = (int) specials[special_index].arg;
2701 } else if (specials[special_index].type == TAB) {
2702 int start = specials[special_index].arg;
2703 int step = specials[special_index].width;
2705 if (!step || step < 0) {
2708 w += step - (cur_x - text_start_x - start) % step;
2709 } else if (specials[special_index].type == FONT) {
2710 selected_font = specials[special_index].font_added;
2711 if (font_height() > last_font_height) {
2712 last_font_height = font_height();
2718 } else if (*p == SECRIT_MULTILINE_CHAR) {
2719 contain_SECRIT_MULTILINE_CHAR = 1;
2721 lw = get_string_width(s);
2722 *p = SECRIT_MULTILINE_CHAR;
2724 w = lw > w ? lw : w;
2725 text_height += last_font_height;
2729 /* Check also last substring if string contains SECRIT_MULTILINE_CHAR */
2730 if (contain_SECRIT_MULTILINE_CHAR) {
2731 lw = get_string_width(s);
2732 w = lw > w ? lw : w;
2734 w += get_string_width(s);
2736 if (w > text_width) {
2739 if (text_width > maximum_width && maximum_width) {
2740 text_width = maximum_width;
2743 text_height += last_font_height;
2744 last_font_height = font_height();
2745 return special_index;
2749 static inline void set_foreground_color(long c)
2752 if (output_methods & TO_X) {
2754 if (have_argb_visual) {
2755 current_color = c | (own_window_argb_value << 24);
2757 #endif /* USE_ARGB */
2761 #endif /* USE_ARGB */
2762 XSetForeground(display, window.gc, current_color);
2766 if (output_methods & TO_NCURSES) {
2767 attron(COLOR_PAIR(c));
2769 #endif /* NCURSES */
2774 static void draw_string(const char *s)
2776 int i, i2, pos, width_of_s;
2779 char *s_with_newlines;
2785 width_of_s = get_string_width(s);
2786 s_with_newlines = strdup(s);
2787 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2788 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2789 s_with_newlines[i] = '\n';
2792 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2793 printf("%s\n", s_with_newlines);
2794 if (extra_newline) fputc('\n', stdout);
2795 fflush(stdout); /* output immediately, don't buffer */
2797 if ((output_methods & TO_STDERR) && draw_mode == FG) {
2798 fprintf(stderr, "%s\n", s_with_newlines);
2799 fflush(stderr); /* output immediately, don't buffer */
2801 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2802 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2804 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2805 fprintf(append_fpointer, "%s\n", s_with_newlines);
2808 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2809 printw("%s", s_with_newlines);
2812 free(s_with_newlines);
2813 memset(tmpstring1, 0, text_buffer_size);
2814 memset(tmpstring2, 0, text_buffer_size);
2815 strncpy(tmpstring1, s, text_buffer_size - 1);
2820 if (output_methods & TO_X) {
2821 max = ((text_width - width_of_s) / get_string_width(" "));
2824 /* This code looks for tabs in the text and coverts them to spaces.
2825 * The trick is getting the correct number of spaces, and not going
2826 * over the window's size without forcing the window larger. */
2827 for (i = 0; i < (int) text_buffer_size; i++) {
2828 if (tmpstring1[i] == '\t') {
2830 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2831 /* guard against overrun */
2832 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2837 /* guard against overrun */
2838 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2843 if (output_methods & TO_X) {
2844 if (text_width == maximum_width) {
2845 /* this means the text is probably pushing the limit,
2846 * so we'll chop it */
2847 while (cur_x + get_string_width(tmpstring2) - text_start_x
2848 > maximum_width && strlen(tmpstring2) > 0) {
2849 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2856 if (output_methods & TO_X) {
2862 c.pixel = current_color;
2863 // query color on custom colormap
2864 XQueryColor(display, window.colourmap, &c);
2867 c2.color.red = c.red;
2868 c2.color.green = c.green;
2869 c2.color.blue = c.blue;
2870 c2.color.alpha = fonts[selected_font].font_alpha;
2872 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2873 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2875 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2876 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
2881 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
2884 cur_x += width_of_s;
2887 memcpy(tmpstring1, s, text_buffer_size);
2890 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
2898 int last_special_needed = -1;
2899 int orig_special_index = special_index;
2902 if (output_methods & TO_X) {
2903 font_h = font_height();
2904 cur_y += font_ascent();
2906 cur_x = text_start_x;
2910 if (*p == SECRIT_MULTILINE_CHAR) {
2911 /* special newline marker for multiline objects */
2916 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
2921 /* draw string before special, unless we're dealing multiline
2923 if (last_special_applied > -1) {
2924 special_index = last_special_applied;
2932 switch (specials[special_index].type) {
2934 case HORIZONTAL_LINE:
2936 int h = specials[special_index].height;
2937 int mid = font_ascent() / 2;
2939 w = text_start_x + text_width - cur_x;
2941 XSetLineAttributes(display, window.gc, h, LineSolid,
2942 CapButt, JoinMiter);
2943 XDrawLine(display, window.drawable, window.gc, cur_x,
2944 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2950 int h = specials[special_index].height;
2951 int tmp_s = specials[special_index].arg;
2952 int mid = font_ascent() / 2;
2953 char ss[2] = { tmp_s, tmp_s };
2955 w = text_start_x + text_width - cur_x - 1;
2956 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
2957 CapButt, JoinMiter);
2958 XSetDashes(display, window.gc, 0, ss, 2);
2959 XDrawLine(display, window.drawable, window.gc, cur_x,
2960 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
2966 int h, bar_usage, by;
2967 if (cur_x - text_start_x > maximum_width
2968 && maximum_width > 0) {
2971 h = specials[special_index].height;
2972 bar_usage = specials[special_index].arg;
2973 by = cur_y - (font_ascent() / 2) - 1;
2978 w = specials[special_index].width;
2980 w = text_start_x + text_width - cur_x - 1;
2986 XSetLineAttributes(display, window.gc, 1, LineSolid,
2987 CapButt, JoinMiter);
2989 XDrawRectangle(display, window.drawable, window.gc, cur_x,
2991 XFillRectangle(display, window.drawable, window.gc, cur_x,
2992 by, w * bar_usage / 255, h);
3000 case GAUGE: /* new GAUGE */
3003 unsigned long last_colour = current_color;
3005 float angle, px, py;
3009 if (cur_x - text_start_x > maximum_width
3010 && maximum_width > 0) {
3014 h = specials[special_index].height;
3015 by = cur_y - (font_ascent() / 2) - 1;
3020 w = specials[special_index].width;
3022 w = text_start_x + text_width - cur_x - 1;
3028 XSetLineAttributes(display, window.gc, 1, LineSolid,
3029 CapButt, JoinMiter);
3031 XDrawArc(display, window.drawable, window.gc,
3032 cur_x, by, w, h * 2, 0, 180*64);
3035 usage = specials[special_index].arg;
3036 angle = (M_PI)*(float)(usage)/255.;
3037 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3038 py = (float)(by+(h))-(float)(h)*sin(angle);
3040 XDrawLine(display, window.drawable, window.gc,
3041 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3049 set_foreground_color(last_colour);
3057 int h, by, i = 0, j = 0;
3059 unsigned long last_colour = current_color;
3060 unsigned long *tmpcolour = 0;
3061 if (cur_x - text_start_x > maximum_width
3062 && maximum_width > 0) {
3065 h = specials[special_index].height;
3066 by = cur_y - (font_ascent() / 2) - 1;
3071 w = specials[special_index].width;
3073 w = text_start_x + text_width - cur_x - 1;
3078 if (draw_graph_borders) {
3079 XSetLineAttributes(display, window.gc, 1, LineSolid,
3080 CapButt, JoinMiter);
3081 XDrawRectangle(display, window.drawable, window.gc,
3084 XSetLineAttributes(display, window.gc, 1, LineSolid,
3085 CapButt, JoinMiter);
3087 if (specials[special_index].last_colour != 0
3088 || specials[special_index].first_colour != 0) {
3089 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3092 for (i = w - 2; i > -1; i--) {
3093 if (specials[special_index].last_colour != 0
3094 || specials[special_index].first_colour != 0) {
3095 if (specials[special_index].tempgrad) {
3098 (int)((float)(w - 2) - specials[special_index].graph[j] *
3099 (w - 2) / (float)specials[special_index].graph_scale)
3103 (int)((float)(w - 2) - specials[special_index].graph[j] *
3104 (w - 2) / (float)specials[special_index].graph_scale)
3107 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3109 (int)((float)(w - 2) - specials[special_index].graph[j] *
3110 (w - 2) / (float)specials[special_index].graph_scale)
3114 #endif /* DEBUG_lol */
3115 set_foreground_color(tmpcolour[
3116 (int)((float)(w - 2) -
3117 specials[special_index].graph[j]
3119 (float)specials[special_index].graph_scale)
3122 set_foreground_color(tmpcolour[colour_idx++]);
3125 /* this is mugfugly, but it works */
3126 XDrawLine(display, window.drawable, window.gc,
3127 cur_x + i + 1, by + h, cur_x + i + 1,
3128 round_to_int((double)by + h - specials[special_index].graph[j] *
3129 (h - 1) / specials[special_index].graph_scale));
3130 if ((w - i) / ((float) (w - 2) /
3131 (specials[special_index].graph_width)) > j
3132 && j < MAX_GRAPH_DEPTH - 3) {
3136 if (tmpcolour) free(tmpcolour);
3141 /* if (draw_mode == BG) {
3142 set_foreground_color(default_bg_color);
3143 } else if (draw_mode == OUTLINE) {
3144 set_foreground_color(default_out_color);
3146 set_foreground_color(default_fg_color);
3148 if (show_graph_range) {
3151 unsigned short int seconds = update_interval * w;
3157 unsigned short int timeunits;
3159 timeunits = seconds / 86400; seconds %= 86400;
3160 if (timeunits > 0) {
3161 if (asprintf(&tmp_day_str, "%dd", timeunits) < 0) {
3165 tmp_day_str = strdup("");
3167 timeunits = seconds / 3600; seconds %= 3600;
3168 if (timeunits > 0) {
3169 if (asprintf(&tmp_hour_str, "%dh", timeunits) < 0) {
3173 tmp_hour_str = strdup("");
3175 timeunits = seconds / 60; seconds %= 60;
3176 if (timeunits > 0) {
3177 if (asprintf(&tmp_min_str, "%dm", timeunits) < 0) {
3181 tmp_min_str = strdup("");
3184 if (asprintf(&tmp_sec_str, "%ds", seconds) < 0) {
3188 tmp_sec_str = strdup("");
3190 if (asprintf(&tmp_str, "%s%s%s%s", tmp_day_str,
3191 tmp_hour_str, tmp_min_str, tmp_sec_str) < 0) {
3194 #define FREE(a) if ((a)) free((a));
3195 FREE(tmp_day_str); FREE(tmp_hour_str); FREE(tmp_min_str); FREE(tmp_sec_str);
3197 tmp_str = strdup("Range not possible"); /* should never happen, but better safe then sorry */
3199 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3200 cur_y += font_h / 2;
3201 draw_string(tmp_str);
3208 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3212 cur_x += font_ascent() / 2;
3213 cur_y += font_h / 2;
3215 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3217 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3218 draw_string(tmp_str);
3224 set_foreground_color(last_colour);
3230 int old = font_ascent();
3232 cur_y -= font_ascent();
3233 selected_font = specials[special_index].font_added;
3235 if (cur_y + font_ascent() < cur_y + old) {
3238 cur_y += font_ascent();
3240 font_h = font_height();
3245 if (draw_mode == FG) {
3246 set_foreground_color(specials[special_index].arg);
3252 if (draw_mode == BG) {
3253 set_foreground_color(specials[special_index].arg);
3258 if (draw_mode == OUTLINE) {
3259 set_foreground_color(specials[special_index].arg);
3264 w += specials[special_index].arg;
3265 last_special_needed = special_index;
3269 cur_y += specials[special_index].arg;
3273 if (specials[special_index].arg >= 0) {
3274 cur_x = (int) specials[special_index].arg;
3276 last_special_needed = special_index;
3281 int start = specials[special_index].arg;
3282 int step = specials[special_index].width;
3284 if (!step || step < 0) {
3287 w = step - (cur_x - text_start_x - start) % step;
3288 last_special_needed = special_index;
3294 /* TODO: add back in "+ window.border_inner_margin" to the end of
3296 int pos_x = text_start_x + text_width -
3297 get_string_width_special(s, special_index);
3299 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3300 "get_string_width(p) %i gap_x %i "
3301 "specials[special_index].arg %i window.border_inner_margin %i "
3302 "window.border_width %i\n", pos_x, text_start_x, text_width,
3303 cur_x, get_string_width_special(s), gap_x,
3304 specials[special_index].arg, window.border_inner_margin,
3305 window.border_width); */
3306 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3307 cur_x = pos_x - specials[special_index].arg;
3309 last_special_needed = special_index;
3315 int pos_x = (text_width) / 2 - get_string_width_special(s,
3316 special_index) / 2 - (cur_x -
3318 /* int pos_x = text_start_x + text_width / 2 -
3319 get_string_width_special(s) / 2; */
3321 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3322 "get_string_width(p) %i gap_x %i "
3323 "specials[special_index].arg %i\n", pos_x, text_start_x,
3324 text_width, cur_x, get_string_width(s), gap_x,
3325 specials[special_index].arg); */
3326 if (pos_x > specials[special_index].arg) {
3327 w = pos_x - specials[special_index].arg;
3329 last_special_needed = special_index;
3339 if (special_index != last_special_applied) {
3342 special_index = orig_special_index;
3343 last_special_applied = -1;
3354 if (output_methods & TO_NCURSES) {
3357 #endif /* NCURSES */
3359 if (output_methods & TO_X)
3360 cur_y += font_descent();
3362 if (recurse && *recurse) {
3363 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3364 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3366 return special_index;
3369 static int draw_line(char *s, int special_index)
3372 if (output_methods & TO_X) {
3373 return draw_each_line_inner(s, special_index, -1);
3377 if (output_methods & TO_NCURSES) {
3378 return draw_each_line_inner(s, special_index, -1);
3380 #endif /* NCURSES */
3382 UNUSED(special_index);
3386 static void draw_text(void)
3390 llua_draw_pre_hook();
3391 #endif /* HAVE_LUA */
3392 if (output_methods & TO_X) {
3393 cur_y = text_start_y;
3396 if (draw_borders && window.border_width > 0) {
3397 if (stippled_borders) {
3398 char ss[2] = { stippled_borders, stippled_borders };
3399 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3400 CapButt, JoinMiter);
3401 XSetDashes(display, window.gc, 0, ss, 2);
3403 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3404 CapButt, JoinMiter);
3407 XDrawRectangle(display, window.drawable, window.gc,
3408 text_start_x - window.border_inner_margin - window.border_width,
3409 text_start_y - window.border_inner_margin - window.border_width,
3410 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3411 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3419 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3420 attron(COLOR_PAIR(COLOR_WHITE));
3421 #endif /* NCURSES */
3422 for_each_line(text_buffer, draw_line);
3423 #if defined(HAVE_LUA) && defined(X11)
3424 llua_draw_post_hook();
3425 #endif /* HAVE_LUA */
3428 static void draw_stuff(void)
3431 cimlib_render(text_start_x, text_start_y, window.width, window.height);
3433 if (overwrite_file) {
3434 overwrite_fpointer = fopen(overwrite_file, "w");
3435 if(!overwrite_fpointer)
3436 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3439 append_fpointer = fopen(append_file, "a");
3440 if(!append_fpointer)
3441 NORM_ERR("Can't append '%s' anymore", append_file);
3444 if (output_methods & TO_X) {
3446 if (draw_shades && !draw_outline) {
3449 set_foreground_color(default_bg_color);
3460 for (i = -1; i < 2; i++) {
3461 for (j = -1; j < 2; j++) {
3462 if (i == 0 && j == 0) {
3467 set_foreground_color(default_out_color);
3468 draw_mode = OUTLINE;
3476 set_foreground_color(default_fg_color);
3481 #if defined(X11) && defined(HAVE_XDBE)
3482 if (output_methods & TO_X) {
3483 xdbe_swap_buffers();
3485 #endif /* X11 && HAVE_XDBE */
3486 if(overwrite_fpointer) {
3487 fclose(overwrite_fpointer);
3488 overwrite_fpointer = 0;
3490 if(append_fpointer) {
3491 fclose(append_fpointer);
3492 append_fpointer = 0;
3497 static void clear_text(int exposures)
3502 The swap action is XdbeBackground, which clears
3505 if (display && window.back_buffer) { // make sure these are !null
3506 /* there is some extra space for borders and outlines */
3507 XFillRectangle(display, window.back_buffer, window.gc_back,
3508 text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3509 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3510 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3511 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2);
3517 if (display && window.window) { // make sure these are !null
3518 /* there is some extra space for borders and outlines */
3519 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3520 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3521 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3522 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3527 static int need_to_update;
3529 /* update_text() generates new text and clears old text area */
3530 static void update_text(void)
3537 if (output_methods & TO_X)
3542 llua_update_info(&info, update_interval);
3543 #endif /* HAVE_LUA */
3546 #ifdef HAVE_SYS_INOTIFY_H
3550 static void main_loop(void)
3553 #ifdef SIGNAL_BLOCKING
3554 sigset_t newmask, oldmask;
3557 #ifdef HAVE_SYS_INOTIFY_H
3558 int inotify_config_wd = -1;
3559 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
3560 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
3561 char inotify_buff[INOTIFY_BUF_LEN];
3562 #endif /* HAVE_SYS_INOTIFY_H */
3565 #ifdef SIGNAL_BLOCKING
3566 sigemptyset(&newmask);
3567 sigaddset(&newmask, SIGINT);
3568 sigaddset(&newmask, SIGTERM);
3569 sigaddset(&newmask, SIGUSR1);
3572 last_update_time = 0.0;
3573 next_update_time = get_time();
3578 if (update_heartbeat_min < update_heartbeat_max){
3579 msg = dbus_message_new_method_call("net.appcheck.Proximus", // target service
3580 "/Proximus", // object path
3581 "net.appcheck.Proximus", // interface to call on
3582 "startTimer"); // method name
3584 fprintf(stderr, "Message Null\n");
3587 if (!dbus_message_append_args(msg, DBUS_TYPE_INT32, &update_heartbeat_min, DBUS_TYPE_INT32, &update_heartbeat_max, DBUS_TYPE_INVALID)) {
3588 fprintf(stderr, "Out Of Memory!\n");
3592 dbus_error_init (&err);
3593 reply = dbus_connection_send_with_reply_and_block (conn, msg, 1500, &err);
3595 if (dbus_error_is_set(&err))
3597 fprintf (stderr, "dbus Error %s: %s\n",err.name,err.message);
3602 //fprintf (stderr, "got dbus reply to startTimer\n");
3603 dbus_message_unref (reply);
3606 fprintf (stderr, "no reply to startTimer\n");
3607 update_heartbeat_min = update_heartbeat_max;
3609 dbus_message_unref (msg);
3614 bool sleeping = false; //i assume it's pretty hard to start this with the screen off...
3616 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3617 //fprintf(stderr, "started main_loop_while\n");
3618 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3619 char buf[max_user_text];
3621 get_battery_short_status(buf, max_user_text, "bq27200-0"); //why is this even hardcoded here? oh well.
3623 update_interval = update_interval_bat;
3624 WantSkips = update_heartbeat_battery_skip;
3626 update_interval = update_interval_old;
3630 if (sleeping) WantSkips = update_skips_when_sleeping;
3634 #ifdef SIGNAL_BLOCKING
3635 /* block signals. we will inspect for pending signals later */
3636 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3637 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3642 if (output_methods & TO_X) {
3645 /* wait for X event or timeout */
3647 if (!XPending(display)) {
3651 if (update_heartbeat_min != update_heartbeat_max) { //wait for signal
3652 // add a rule for which messages we want to see
3653 dbus_bus_add_match(conn,
3654 "type='signal',interface='net.appcheck.Proximus'",
3655 &err); // see signals from the given interface
3656 dbus_bus_add_match(conn2,
3657 "type='signal',interface='com.nokia.mce.signal'",
3658 &err); // see signals from the given interface
3659 dbus_connection_flush(conn);
3660 dbus_connection_flush(conn2);
3661 if (dbus_error_is_set(&err)) {
3662 fprintf(stderr, "Match Error (%s)\n", err.message);
3667 while (exitloop == 0 && info.looped >= 10) { //first 10 frames drawn fast because if using lua + cairo it takes a while to get going
3668 // non blocking read of the next available message
3669 dbus_connection_read_write(conn, 0);
3670 dbus_connection_read_write(conn2, 0);
3671 msg = dbus_connection_pop_message(conn);
3673 // loop again if we haven't read a message
3675 msg = dbus_connection_pop_message(conn2);
3681 // check if the message is a signal from the correct interface and with the correct name
3682 if (dbus_message_is_signal(msg, "net.appcheck.Proximus", "heartbeat")) {
3683 if(WantSkips == 0 || DoneSkips >= WantSkips){
3685 fprintf(stderr, PACKAGE_NAME": woke by heartbeat\n");
3688 fprintf(stderr, PACKAGE_NAME": ignoring this heartbeat\n");
3692 else if (dbus_message_is_signal(msg, "com.nokia.mce.signal", "display_status_ind")){
3693 // read the parameters
3694 if (!dbus_message_iter_init(msg, &args))
3695 fprintf(stderr, "Message has no arguments!\n");
3696 else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
3697 fprintf(stderr, "Argument is not string!\n");
3700 dbus_message_iter_get_basic(&args, &sigvalue);
3701 if(!strcmp(sigvalue, MCE_DISPLAY_ON_STRING)){
3702 printf("wakeup because got display_status_ind signal with value %s\n", sigvalue);
3706 else if(!strcmp(sigvalue, MCE_DISPLAY_OFF_STRING)){
3707 printf("sleeping because got display_status_ind signal with value %s\n", sigvalue);
3708 WantSkips = update_skips_when_sleeping;
3714 //fprintf(stderr, PACKAGE_NAME": got dbus junk\n");
3716 dbus_message_unref(msg);
3718 t = 0;//force legacy timer to skip -- update now or as soon as x is ready
3720 else{ //signal not available, set up for legacy timer
3721 t = next_update_time - get_time();
3725 else if (t > update_interval) {
3726 t = update_interval;
3730 tv.tv_sec = (long) t;
3731 tv.tv_usec = (long) (t * 1000000) % 1000000;
3733 FD_SET(ConnectionNumber(display), &fdsr);
3735 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3737 if (errno != EINTR) {
3738 NORM_ERR("can't select(): %s", strerror(errno));
3748 if (need_to_update) {
3750 int wx = window.x, wy = window.y;
3760 /* resize window if it isn't right size */
3762 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3763 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3764 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3765 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3766 draw_stuff(); /* redraw everything in our newly sized window */
3767 XResizeWindow(display, window.window, window.width,
3768 window.height); /* resize window */
3769 fprintf(stderr, PACKAGE_NAME": resizing window to %d x %d\n",window.width,window.height);
3772 XFreePixmap(display, window.back_buffer);
3773 window.back_buffer = XCreatePixmap(display, window.window,
3774 window.width, window.height, window.depth);
3775 if (window.back_buffer != None)
3776 window.drawable = window.back_buffer;
3778 window.drawable = window.window;
3783 cimlib_init(display, window.drawable, window.visual, window.colourmap);
3788 set_transparent_background(window.window, own_window_argb_value);
3791 xdbe_swap_buffers();
3796 /* update lua window globals */
3797 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3798 #endif /* HAVE_LUA */
3801 /* move window if it isn't in right position */
3802 if (!fixed_pos && (window.x != wx || window.y != wy)) {
3803 XMoveWindow(display, window.window, window.x, window.y);
3808 if (changed && window.type == TYPE_PANEL) {
3811 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3814 switch (text_alignment) {
3841 set_struts(sidenum);
3852 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3853 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3854 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3855 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3856 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3861 /* handle X events */
3862 while (XPending(display)) {
3865 XNextEvent(display, &ev);
3872 r.width = ev.xexpose.width;
3873 r.height = ev.xexpose.height;
3874 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3878 case PropertyNotify:
3880 if ( ev.xproperty.state == PropertyNewValue ) {
3881 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3887 case ReparentNotify:
3888 /* make background transparent */
3890 set_transparent_background(window.window, own_window_argb_value);
3894 case ConfigureNotify:
3896 /* if window size isn't what expected, set fixed size */
3897 if (ev.xconfigure.width != window.width
3898 || ev.xconfigure.height != window.height) {
3899 if (window.width != 0 && window.height != 0) {
3903 /* clear old stuff before screwing up
3908 XWindowAttributes attrs;
3909 if (XGetWindowAttributes(display,
3910 window.window, &attrs)) {
3911 window.width = attrs.width;
3912 window.height = attrs.height;
3913 fprintf(stderr, PACKAGE_NAME": x11? resized our window to %d x %d\n",window.width,window.height);
3917 XFreePixmap(display, window.back_buffer);
3918 window.back_buffer = XCreatePixmap(display, window.window,
3919 window.width, window.height, window.depth);
3920 if (window.back_buffer != None)
3921 window.drawable = window.back_buffer;
3923 window.drawable = window.window;
3928 cimlib_init(display, window.drawable, window.visual, window.colourmap);
3936 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3937 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3938 if (text_width > maximum_width
3939 && maximum_width > 0) {
3940 text_width = maximum_width;
3944 /* if position isn't what expected, set fixed pos
3945 * total_updates avoids setting fixed_pos when window
3946 * is set to weird locations when started */
3947 /* // this is broken
3948 if (total_updates >= 2 && !fixed_pos
3949 && (window.x != ev.xconfigure.x
3950 || window.y != ev.xconfigure.y)
3951 && (ev.xconfigure.x != 0
3952 || ev.xconfigure.y != 0)) {
3960 /* if an ordinary window with decorations */
3961 if ((window.type == TYPE_NORMAL &&
3962 (!TEST_HINT(window.hints,
3963 HINT_UNDECORATED))) ||
3964 window.type == TYPE_DESKTOP) {
3965 /* allow conky to hold input focus. */
3967 } else if(TEST_HINT(window.hints,HINT_FULLSCREEN)) {
3968 //detect top right click
3969 if (ev.xbutton.x > 750 && ev.xbutton.y < 50)
3973 if (ev.xbutton.x <= 50 && ev.xbutton.y <= 50)
3974 { //minimize / goto task switcher
3975 dbus_exit_app_view();
3979 /* forward the click to the desktop window */
3980 XUngrabPointer(display, ev.xbutton.time);
3981 ev.xbutton.window = window.desktop;
3982 ev.xbutton.x = ev.xbutton.x_root;
3983 ev.xbutton.y = ev.xbutton.y_root;
3984 XSendEvent(display, ev.xbutton.window, False,
3985 ButtonPressMask, &ev);
3986 XSetInputFocus(display, ev.xbutton.window,
3987 RevertToParent, ev.xbutton.time);
3994 /* if an ordinary window with decorations */
3995 if ((window.type == TYPE_NORMAL)
3996 && (!TEST_HINT(window.hints,
3997 HINT_UNDECORATED))) {
3998 /* allow conky to hold input focus. */
4001 /* forward the release to the desktop window */
4002 ev.xbutton.window = window.desktop;
4003 ev.xbutton.x = ev.xbutton.x_root;
4004 ev.xbutton.y = ev.xbutton.y_root;
4005 XSendEvent(display, ev.xbutton.window, False,
4006 ButtonReleaseMask, &ev);
4015 if (ev.type == x11_stuff.event_base + XDamageNotify) {
4016 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
4018 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
4019 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
4021 #endif /* HAVE_XDAMAGE */
4027 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
4028 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
4029 #endif /* HAVE_XDAMAGE */
4031 /* XDBE doesn't seem to provide a way to clear the back buffer
4032 * without interfering with the front buffer, other than passing
4033 * XdbeBackground to XdbeSwapBuffers. That means that if we're
4034 * using XDBE, we need to redraw the text even if it wasn't part of
4035 * the exposed area. OTOH, if we're not going to call draw_stuff at
4036 * all, then no swap happens and we can safely do nothing. */
4038 if (!XEmptyRegion(x11_stuff.region)) {
4043 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4044 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4045 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4046 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4047 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4050 XSetRegion(display, window.gc, x11_stuff.region);
4053 XftDrawSetClip(window.xftdraw, x11_stuff.region);
4057 XDestroyRegion(x11_stuff.region);
4058 x11_stuff.region = XCreateRegion();
4062 t = (next_update_time - get_time()) * 1000000;
4063 //fprintf(stderr, PACKAGE_NAME": trying to sleep %d microseconds\n",t);
4064 if(t > 0) usleep((useconds_t)t);
4068 if(output_methods & TO_NCURSES) {
4077 #ifdef SIGNAL_BLOCKING
4078 /* unblock signals of interest and let handler fly */
4079 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4080 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4084 switch (g_signal_pending) {
4087 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4092 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4095 if (output_methods & TO_X) {
4096 XDestroyRegion(x11_stuff.region);
4097 x11_stuff.region = NULL;
4099 XDamageDestroy(display, x11_stuff.damage);
4100 XFixesDestroyRegion(display, x11_stuff.region2);
4101 XFixesDestroyRegion(display, x11_stuff.part);
4102 #endif /* HAVE_XDAMAGE */
4108 if(overwrite_file) {
4109 free(overwrite_file);
4118 /* Reaching here means someone set a signal
4119 * (SIGXXXX, signal_handler), but didn't write any code
4121 * If you don't want to handle a signal, don't set a handler on
4122 * it in the first place. */
4123 if (g_signal_pending) {
4124 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4128 #ifdef HAVE_SYS_INOTIFY_H
4129 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4130 inotify_config_wd = inotify_add_watch(inotify_fd,
4134 if (!disable_auto_reload && inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4135 int len = 0, idx = 0;
4137 struct timeval time_to_wait;
4139 FD_ZERO(&descriptors);
4140 FD_SET(inotify_fd, &descriptors);
4142 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4144 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4145 if (FD_ISSET(inotify_fd, &descriptors)) {
4146 /* process inotify events */
4147 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4148 while (len > 0 && idx < len) {
4149 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4150 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4151 /* current_config should be reloaded */
4152 NORM_ERR("'%s' modified, reloading...", current_config);
4154 if (ev->mask & IN_IGNORED) {
4155 /* for some reason we get IN_IGNORED here
4156 * sometimes, so we need to re-add the watch */
4157 inotify_config_wd = inotify_add_watch(inotify_fd,
4165 llua_inotify_query(ev->wd, ev->mask);
4167 #endif /* HAVE_LUA */
4168 idx += INOTIFY_EVENT_SIZE + ev->len;
4171 } else if (disable_auto_reload && inotify_fd != -1) {
4172 inotify_rm_watch(inotify_fd, inotify_config_wd);
4174 inotify_fd = inotify_config_wd = 0;
4176 #endif /* HAVE_SYS_INOTIFY_H */
4179 llua_update_info(&info, update_interval);
4180 #endif /* HAVE_LUA */
4181 g_signal_pending = 0;
4182 }//end of while{} (the real loop)
4183 clean_up(current_mail_spool, NULL);
4185 #ifdef HAVE_SYS_INOTIFY_H
4186 if (inotify_fd != -1) {
4187 inotify_rm_watch(inotify_fd, inotify_config_wd);
4189 inotify_fd = inotify_config_wd = 0;
4191 #endif /* HAVE_SYS_INOTIFY_H */
4192 }//end of main_loop()
4195 static void load_config_file_x11(const char *);
4197 void initialisation(int argc, char** argv);
4199 /* reload the config file */
4200 static void reload_config(void)
4202 char *current_config_copy = strdup(current_config);
4203 clean_up(NULL, NULL);
4204 sleep(1); /* slight pause */
4205 current_config = current_config_copy;
4206 initialisation(argc_copy, argv_copy);
4210 void clean_up_x11(void)
4212 if(window_created == 1) {
4213 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4214 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4215 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4216 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4221 if(x11_stuff.region) {
4222 XDestroyRegion(x11_stuff.region);
4223 x11_stuff.region = NULL;
4226 XCloseDisplay(display);
4229 if(info.x11.desktop.all_names) {
4230 free(info.x11.desktop.all_names);
4231 info.x11.desktop.all_names = NULL;
4233 if (info.x11.desktop.name) {
4234 free(info.x11.desktop.name);
4235 info.x11.desktop.name = NULL;
4241 void clean_up_without_threads(void *memtofree1, void* memtofree2) {
4245 if(output_methods & TO_NCURSES) {
4249 conftree_empty(currentconffile);
4250 currentconffile = NULL;
4257 timed_thread_destroy_registered_threads();
4259 if (info.cpu_usage) {
4260 free(info.cpu_usage);
4261 info.cpu_usage = NULL;
4264 if (x_initialised == YES) {
4267 free(fonts); //in set_default_configurations a font is set but not loaded
4272 set_nvidia_display(NULL);
4278 free_text_objects(&global_root_object, 0);
4297 free(current_config);
4300 #ifdef TCP_PORT_MONITOR
4301 tcp_portmon_clear();
4310 weather_free_info();
4313 llua_shutdown_hook();
4315 #endif /* HAVE_LUA */
4317 if (output_methods & TO_X)
4325 for (i = 0; i < special_count; i++) {
4326 if (specials[i].type == GRAPH) {
4327 free(specials[i].graph);
4335 clear_diskio_stats();
4336 if(global_cpu != NULL) {
4342 void clean_up(void *memtofree1, void* memtofree2)
4344 free_update_callbacks();
4345 clean_up_without_threads(memtofree1, memtofree2);
4348 static int string_to_bool(const char *s)
4351 // Assumes an option without a true/false means true
4353 } else if (strcasecmp(s, "yes") == EQUAL) {
4355 } else if (strcasecmp(s, "true") == EQUAL) {
4357 } else if (strcasecmp(s, "1") == EQUAL) {
4364 static enum alignment string_to_alignment(const char *s)
4366 if (strcasecmp(s, "top_left") == EQUAL) {
4368 } else if (strcasecmp(s, "top_right") == EQUAL) {
4370 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4372 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4374 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4375 return BOTTOM_RIGHT;
4376 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4377 return BOTTOM_MIDDLE;
4378 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4380 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4381 return MIDDLE_RIGHT;
4382 } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4383 return MIDDLE_MIDDLE;
4384 } else if (strcasecmp(s, "tl") == EQUAL) {
4386 } else if (strcasecmp(s, "tr") == EQUAL) {
4388 } else if (strcasecmp(s, "tm") == EQUAL) {
4390 } else if (strcasecmp(s, "bl") == EQUAL) {
4392 } else if (strcasecmp(s, "br") == EQUAL) {
4393 return BOTTOM_RIGHT;
4394 } else if (strcasecmp(s, "bm") == EQUAL) {
4395 return BOTTOM_MIDDLE;
4396 } else if (strcasecmp(s, "ml") == EQUAL) {
4398 } else if (strcasecmp(s, "mr") == EQUAL) {
4399 return MIDDLE_RIGHT;
4400 } else if (strcasecmp(s, "mm") == EQUAL) {
4401 return MIDDLE_MIDDLE;
4402 } else if (strcasecmp(s, "none") == EQUAL) {
4405 return ALIGNMENT_ERROR;
4410 static void set_default_configurations_for_x(void)
4412 default_fg_color = WhitePixel(display, screen);
4413 default_bg_color = BlackPixel(display, screen);
4414 default_out_color = BlackPixel(display, screen);
4415 color0 = default_fg_color;
4416 color1 = default_fg_color;
4417 color2 = default_fg_color;
4418 color3 = default_fg_color;
4419 color4 = default_fg_color;
4420 color5 = default_fg_color;
4421 color6 = default_fg_color;
4422 color7 = default_fg_color;
4423 color8 = default_fg_color;
4424 color9 = default_fg_color;
4425 current_text_color = default_fg_color;
4429 static void set_default_configurations(void)
4436 fork_to_background = 0;
4437 total_run_times = 0;
4438 info.cpu_avg_samples = 2;
4439 info.net_avg_samples = 2;
4440 info.diskio_avg_samples = 2;
4445 format_human_readable = 1;
4453 mpd_env_host = getenv("MPD_HOST");
4454 mpd_env_port = getenv("MPD_PORT");
4456 if (!mpd_env_host || !strlen(mpd_env_host)) {
4457 mpd_set_host("localhost");
4459 /* MPD_HOST environment variable is set */
4460 char *mpd_hostpart = strchr(mpd_env_host, '@');
4461 if (!mpd_hostpart) {
4462 mpd_set_host(mpd_env_host);
4464 /* MPD_HOST contains a password */
4465 char mpd_password[mpd_hostpart - mpd_env_host + 1];
4466 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4468 if (!strlen(mpd_hostpart + 1)) {
4469 mpd_set_host("localhost");
4471 mpd_set_host(mpd_hostpart + 1);
4474 mpd_set_password(mpd_password, 1);
4479 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4480 /* failed to set port from environment variable */
4481 mpd_set_port("6600");
4485 info.xmms2.artist = NULL;
4486 info.xmms2.album = NULL;
4487 info.xmms2.title = NULL;
4488 info.xmms2.genre = NULL;
4489 info.xmms2.comment = NULL;
4490 info.xmms2.url = NULL;
4491 info.xmms2.status = NULL;
4492 info.xmms2.playlist = NULL;
4494 use_spacer = NO_SPACER;
4496 output_methods = TO_X;
4498 output_methods = TO_STDOUT;
4504 show_graph_scale = 0;
4505 show_graph_range = 0;
4508 draw_graph_borders = 1;
4510 set_first_font("6x10");
4518 window.type = TYPE_NORMAL;
4520 strcpy(window.class_name, PACKAGE_NAME);
4521 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4523 use_argb_visual = 0;
4524 own_window_argb_value = 255;
4527 stippled_borders = 0;
4528 window.border_inner_margin = 3;
4529 window.border_outer_margin = 1;
4530 window.border_width = 1;
4531 text_alignment = BOTTOM_LEFT;
4532 info.x11.monitor.number = 1;
4533 info.x11.monitor.current = 0;
4534 info.x11.desktop.current = 1;
4535 info.x11.desktop.number = 1;
4536 info.x11.desktop.nitems = 0;
4537 info.x11.desktop.all_names = NULL;
4538 info.x11.desktop.name = NULL;
4543 free(current_mail_spool);
4547 variable_substitute(MAIL_FILE, buf, 256);
4548 if (buf[0] != '\0') {
4549 current_mail_spool = strndup(buf, text_buffer_size);
4554 set_update_interval(3);
4555 update_interval_bat = NOBATTERY;
4556 info.music_player_interval = 1.0;
4557 stuff_in_uppercase = 0;
4558 info.users.number = 1;
4560 set_times_in_seconds(0);
4562 #ifdef TCP_PORT_MONITOR
4563 /* set default connection limit */
4564 tcp_portmon_set_max_connections(0);
4568 /* returns 1 if you can overwrite or create the file at 'path' */
4569 static _Bool overwrite_works(const char *path)
4573 if (!(filepointer = fopen(path, "w")))
4575 fclose(filepointer);
4579 /* returns 1 if you can append or create the file at 'path' */
4580 static _Bool append_works(const char *path)
4584 if (!(filepointer = fopen(path, "a")))
4586 fclose(filepointer);
4592 /* WARNING, this type not in Xlib spec */
4593 int x11_error_handler(Display *d, XErrorEvent *err)
4594 __attribute__((noreturn));
4595 int x11_error_handler(Display *d, XErrorEvent *err)
4597 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",
4599 (long unsigned)err->display,
4600 (long)err->resourceid,
4610 int x11_ioerror_handler(Display *d)
4611 __attribute__((noreturn));
4612 int x11_ioerror_handler(Display *d)
4614 NORM_ERR("X Error: Display %lx\n",
4621 static void X11_initialisation(void)
4623 if (x_initialised == YES) return;
4624 output_methods |= TO_X;
4626 set_default_configurations_for_x();
4627 x_initialised = YES;
4630 /* WARNING, this type not in Xlib spec */
4631 XSetErrorHandler(&x11_error_handler);
4632 XSetIOErrorHandler(&x11_ioerror_handler);
4636 static char **xargv = 0;
4637 static int xargc = 0;
4639 static void X11_create_window(void)
4641 if (output_methods & TO_X) {
4643 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4644 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4646 #else /* OWN_WINDOW */
4647 init_window(0, 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, 0,
4650 #endif /* OWN_WINDOW */
4654 update_text_area(); /* to position text/window on screen */
4657 if (own_window && !fixed_pos) {
4658 XMoveWindow(display, window.window, window.x, window.y);
4661 set_transparent_background(window.window, own_window_argb_value);
4669 x11_stuff.region = XCreateRegion();
4671 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4672 NORM_ERR("Xdamage extension unavailable");
4674 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4675 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4676 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4677 #endif /* HAVE_XDAMAGE */
4680 update_text_area(); /* to get initial size of the window */
4683 /* setup lua window globals */
4684 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4685 #endif /* HAVE_LUA */
4689 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4690 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4691 #define CONF2(a) if (strcasecmp(name, a) == 0)
4692 #define CONF(a) else CONF2(a)
4693 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4694 || strcasecmp(name, b) == 0)
4695 #define CONF_CONTINUE 1
4696 #define CONF_BREAK 2
4697 #define CONF_BUFF_SIZE 512
4699 static FILE *open_config_file(const char *f)
4701 #ifdef CONFIG_OUTPUT
4702 if (!strcmp(f, "==builtin==")) {
4703 return conf_cookie_open();
4705 #endif /* CONFIG_OUTPUT */
4706 return fopen(f, "r");
4709 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4713 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4716 remove_comments(buf);
4721 while (*p && isspace((int) *p)) {
4725 return CONF_CONTINUE; /* empty line */
4732 while (*p2 && !isspace((int) *p2)) {
4736 *p2 = '\0'; /* break at name's end */
4743 while (*p && isspace((int) *p)) {
4749 p2 = *value + strlen(*value);
4750 while (isspace((int) *(p2 - 1))) {
4760 void setalignment(int* ltext_alignment, unsigned int windowtype, const char* value, const char *f, int line, char setbyconffile) {
4762 if (windowtype == TYPE_DOCK) {
4763 NORM_ERR("alignment is disabled when own_window_type is dock");
4765 #endif /*OWN_WINDOW */
4767 int a = string_to_alignment(value);
4770 if (setbyconffile) {
4772 } else NORM_ERR("'%s' is not a alignment setting", value);
4774 *ltext_alignment = a;
4776 } else if (setbyconffile) {
4782 char load_config_file(const char *f)
4787 fp = open_config_file(f);
4791 DBGP("reading contents from config file '%s'", f);
4794 char buff[CONF_BUFF_SIZE], *name, *value;
4795 int ret = do_config_step(&line, fp, buff, &name, &value);
4796 if (ret == CONF_BREAK) {
4798 } else if (ret == CONF_CONTINUE) {
4804 if (string_to_bool(value)) {
4805 output_methods &= TO_X;
4808 output_methods &= ~TO_X;
4809 x_initialised = NEVER;
4813 if (!value || x_initialised == YES) {
4818 disp = strdup(value);
4822 CONF("nvidia_display") {
4824 set_nvidia_display(value);
4828 setalignment(&text_alignment, window.type, value, f, line, 1);
4830 CONF("background") {
4831 fork_to_background = string_to_bool(value);
4834 CONF2("background") {
4835 fork_to_background = string_to_bool(value);
4839 CONF("show_graph_scale") {
4840 show_graph_scale = string_to_bool(value);
4842 CONF("show_graph_range") {
4843 show_graph_range = string_to_bool(value);
4845 CONF("border_inner_margin") {
4847 window.border_inner_margin = strtol(value, 0, 0);
4848 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4853 CONF("border_outer_margin") {
4855 window.border_outer_margin = strtol(value, 0, 0);
4856 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4861 CONF("border_width") {
4863 window.border_width = strtol(value, 0, 0);
4864 if (window.border_width < 1) window.border_width = 1;
4870 #define TEMPLATE_CONF(n) \
4871 CONF("template"#n) { \
4872 if (set_template(n, value)) \
4887 parse_global_imap_mail_args(value);
4894 parse_global_pop3_mail_args(value);
4899 CONF("default_bar_size") {
4902 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4909 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4913 CONF("default_graph_size") {
4916 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4923 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4926 CONF("default_gauge_size") {
4929 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4936 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4943 mpd_set_host(value);
4949 if (value && mpd_set_port(value)) {
4953 CONF("mpd_password") {
4955 mpd_set_password(value, 0);
4961 CONF("music_player_interval") {
4963 info.music_player_interval = strtod(value, 0);
4969 CONF("sensor_device") {
4971 sensor_device = strtol(value, 0, 0);
4977 CONF("cpu_avg_samples") {
4979 cpu_avg_samples = strtol(value, 0, 0);
4980 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4983 info.cpu_avg_samples = cpu_avg_samples;
4989 CONF("net_avg_samples") {
4991 net_avg_samples = strtol(value, 0, 0);
4992 if (net_avg_samples < 1 || net_avg_samples > 14) {
4995 info.net_avg_samples = net_avg_samples;
5001 CONF("diskio_avg_samples") {
5003 diskio_avg_samples = strtol(value, 0, 0);
5004 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
5007 info.diskio_avg_samples = diskio_avg_samples;
5015 CONF("double_buffer") {
5016 use_xdbe = string_to_bool(value);
5020 CONF("override_utf8_locale") {
5021 utf8_mode = string_to_bool(value);
5023 CONF("draw_borders") {
5024 draw_borders = string_to_bool(value);
5026 CONF("draw_graph_borders") {
5027 draw_graph_borders = string_to_bool(value);
5029 CONF("draw_shades") {
5030 draw_shades = string_to_bool(value);
5032 CONF("draw_outline") {
5033 draw_outline = string_to_bool(value);
5036 CONF("times_in_seconds") {
5037 set_times_in_seconds(string_to_bool(value));
5039 CONF("max_text_width") {
5040 max_text_width = atoi(value);
5042 CONF("out_to_console") {
5043 if(string_to_bool(value)) {
5044 output_methods |= TO_STDOUT;
5046 output_methods &= ~TO_STDOUT;
5049 CONF("extra_newline") {
5050 extra_newline = string_to_bool(value);
5052 CONF("disable_auto_reload") {
5053 disable_auto_reload = string_to_bool(value);
5055 CONF("out_to_stderr") {
5056 if(string_to_bool(value))
5057 output_methods |= TO_STDERR;
5060 CONF("out_to_ncurses") {
5061 if(string_to_bool(value)) {
5064 output_methods |= TO_NCURSES;
5068 CONF("overwrite_file") {
5069 if(overwrite_file) {
5070 free(overwrite_file);
5073 if(overwrite_works(value)) {
5074 overwrite_file = strdup(value);
5075 output_methods |= OVERWRITE_FILE;
5077 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
5079 CONF("append_file") {
5084 if(append_works(value)) {
5085 append_file = strdup(value);
5086 output_methods |= APPEND_FILE;
5088 NORM_ERR("append_file won't be able to create/append '%s'", value);
5090 CONF("use_spacer") {
5092 if (strcasecmp(value, "left") == EQUAL) {
5093 use_spacer = LEFT_SPACER;
5094 } else if (strcasecmp(value, "right") == EQUAL) {
5095 use_spacer = RIGHT_SPACER;
5096 } else if (strcasecmp(value, "none") == EQUAL) {
5097 use_spacer = NO_SPACER;
5099 use_spacer = string_to_bool(value);
5100 NORM_ERR("use_spacer should have an argument of left, right, or"
5101 " none. '%s' seems to be some form of '%s', so"
5102 " defaulting to %s.", value,
5103 use_spacer ? "true" : "false",
5104 use_spacer ? "right" : "none");
5106 use_spacer = RIGHT_SPACER;
5108 use_spacer = NO_SPACER;
5112 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5113 use_spacer = RIGHT_SPACER;
5119 use_xft = string_to_bool(value);
5123 set_first_font(value);
5127 if (value && font_count >= 0) {
5128 fonts[0].font_alpha = atof(value) * 65535.0;
5135 if (string_to_bool(value)) {
5136 NORM_ERR("Xft not enabled at compile time");
5140 /* xftfont silently ignored when no Xft */
5143 /* xftalpha is silently ignored when no Xft */
5148 set_first_font(value);
5156 gap_x = atoi(value);
5163 gap_y = atoi(value);
5169 CONF("mail_spool") {
5173 variable_substitute(value, buffer, 256);
5175 if (buffer[0] != '\0') {
5176 if (current_mail_spool) {
5177 free(current_mail_spool);
5179 current_mail_spool = strndup(buffer, text_buffer_size);
5186 CONF("minimum_size") {
5188 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5190 if (sscanf(value, "%d", &minimum_width) != 1) {
5198 CONF("maximum_width") {
5200 if (sscanf(value, "%d", &maximum_width) != 1) {
5208 CONF("no_buffers") {
5209 no_buffers = string_to_bool(value);
5211 CONF("top_name_width") {
5212 if (set_top_name_width(value))
5215 CONF("top_cpu_separate") {
5216 cpu_separate = string_to_bool(value);
5218 CONF("short_units") {
5219 short_units = string_to_bool(value);
5221 CONF("format_human_readable") {
5222 format_human_readable = string_to_bool(value);
5225 CONF("hddtemp_host") {
5226 set_hddtemp_host(value);
5228 CONF("hddtemp_port") {
5229 set_hddtemp_port(value);
5231 #endif /* HDDTEMP */
5232 CONF("pad_percents") {
5233 pad_percents = atoi(value);
5237 CONF("own_window") {
5239 own_window = string_to_bool(value);
5242 CONF("own_window_class") {
5244 memset(window.class_name, 0, sizeof(window.class_name));
5245 strncpy(window.class_name, value,
5246 sizeof(window.class_name) - 1);
5249 CONF("own_window_title") {
5251 memset(window.title, 0, sizeof(window.title));
5252 strncpy(window.title, value, sizeof(window.title) - 1);
5255 CONF("own_window_transparent") {
5257 set_transparent = string_to_bool(value);
5260 CONF("own_window_hints") {
5262 char *p_hint, *p_save;
5263 char delim[] = ", ";
5265 /* tokenize the value into individual hints */
5266 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5268 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5269 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5270 SET_HINT(window.hints, HINT_UNDECORATED);
5271 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5272 SET_HINT(window.hints, HINT_BELOW);
5273 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5274 SET_HINT(window.hints, HINT_ABOVE);
5275 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5276 SET_HINT(window.hints, HINT_STICKY);
5277 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5278 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5279 } else if (strncmp(p_hint, "fullscreen", 10) == EQUAL) {
5280 SET_HINT(window.hints, HINT_FULLSCREEN);
5285 p_hint = strtok_r(NULL, delim, &p_save);
5286 } while (p_hint != NULL);
5292 CONF("own_window_type") {
5294 if (strncmp(value, "normal", 6) == EQUAL) {
5295 window.type = TYPE_NORMAL;
5296 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5297 window.type = TYPE_DESKTOP;
5298 } else if (strncmp(value, "dock", 4) == EQUAL) {
5299 window.type = TYPE_DOCK;
5300 text_alignment = TOP_LEFT;
5301 } else if (strncmp(value, "panel", 5) == EQUAL) {
5302 window.type = TYPE_PANEL;
5303 } else if (strncmp(value, "override", 8) == EQUAL) {
5304 window.type = TYPE_OVERRIDE;
5313 CONF("own_window_argb_visual") {
5314 use_argb_visual = string_to_bool(value);
5316 CONF("own_window_argb_value") {
5317 own_window_argb_value = strtol(value, 0, 0);
5318 if (own_window_argb_value > 255 || own_window_argb_value < 0) {
5319 CONF_ERR2("own_window_argb_value must be <= 255 and >= 0");
5322 #endif /* USE_ARGB */
5324 CONF("stippled_borders") {
5326 stippled_borders = strtol(value, 0, 0);
5328 stippled_borders = 4;
5332 CONF("imlib_cache_size") {
5334 cimlib_set_cache_size(atoi(value));
5337 CONF("imlib_cache_flush_interval") {
5339 cimlib_set_cache_flush_interval(atoi(value));
5344 CONF("update_heartbeat_min") {
5346 update_heartbeat_min = strtod(value, 0);
5351 CONF("update_heartbeat_max") {
5353 update_heartbeat_max = strtod(value, 0);
5358 CONF("update_heartbeat_battery_skip") {
5360 update_heartbeat_battery_skip = strtod(value, 0);
5365 CONF("update_skips_when_sleeping") {
5367 update_skips_when_sleeping = strtod(value, 0);
5372 CONF("update_interval_on_battery") {
5374 update_interval_bat = strtod(value, 0);
5379 CONF("update_interval") {
5381 set_update_interval(strtod(value, 0));
5385 if (info.music_player_interval == 0) {
5386 // default to update_interval
5387 info.music_player_interval = update_interval;
5390 CONF("total_run_times") {
5392 total_run_times = strtod(value, 0);
5398 stuff_in_uppercase = string_to_bool(value);
5400 CONF("max_specials") {
5402 max_specials = atoi(value);
5407 CONF("max_user_text") {
5409 max_user_text = atoi(value);
5414 CONF("text_buffer_size") {
5416 text_buffer_size = atoi(value);
5417 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5418 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5419 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5427 if (output_methods & TO_X) {
5428 X11_initialisation();
5437 global_text = (char *) malloc(1);
5438 global_text[0] = '\0';
5441 unsigned int l = strlen(global_text);
5443 char buf[CONF_BUFF_SIZE];
5445 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5451 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5459 /* Check for continuation of \\-\n. */
5460 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5461 global_text[l-1] = '\0';
5465 global_text = (char *) realloc(global_text, l + bl + 1);
5466 strcat(global_text, buf);
5468 if (strlen(global_text) > max_user_text) {
5472 global_text_lines = line + 1;
5475 #ifdef TCP_PORT_MONITOR
5476 CONF("max_port_monitor_connections") {
5478 if (!value || (sscanf(value, "%d", &max) != 1)) {
5479 /* an error. use default, warn and continue. */
5480 tcp_portmon_set_max_connections(0);
5482 } else if (tcp_portmon_set_max_connections(max)) {
5483 /* max is < 0, default has been set*/
5488 CONF("if_up_strictness") {
5490 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5491 ifup_strictness = IFUP_UP;
5492 } else if (strcasecmp(value, "up") == EQUAL) {
5493 ifup_strictness = IFUP_UP;
5494 } else if (strcasecmp(value, "link") == EQUAL) {
5495 ifup_strictness = IFUP_LINK;
5496 } else if (strcasecmp(value, "address") == EQUAL) {
5497 ifup_strictness = IFUP_ADDR;
5499 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5500 ifup_strictness = IFUP_UP;
5504 CONF("temperature_unit") {
5506 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5507 } else if (set_temp_output_unit(value)) {
5508 NORM_ERR("temperature_unit: incorrect argument");
5515 char *ptr = strtok(value, " ");
5518 ptr = strtok(NULL, " ");
5525 CONF("lua_draw_hook_pre") {
5527 llua_set_draw_pre_hook(value);
5532 CONF("lua_draw_hook_post") {
5534 llua_set_draw_post_hook(value);
5539 CONF("lua_startup_hook") {
5541 llua_set_startup_hook(value);
5546 CONF("lua_shutdown_hook") {
5548 llua_set_shutdown_hook(value);
5554 #endif /* HAVE_LUA */
5566 CONF("default_color"){}
5567 CONF3("default_shade_color", "default_shadecolor"){}
5568 CONF3("default_outline_color", "default_outlinecolor") {}
5569 CONF("own_window_colour") {}
5572 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5578 if (info.music_player_interval == 0) {
5579 // default to update_interval
5580 info.music_player_interval = update_interval;
5582 if (!global_text) { // didn't supply any text
5583 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5585 if (!output_methods) {
5586 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5588 #if defined(NCURSES)
5590 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5591 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5592 output_methods &= ~TO_NCURSES;
5596 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5597 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5598 output_methods &= ~(TO_STDOUT | TO_STDERR);
5600 #endif /* NCURSES */
5605 static void load_config_file_x11(const char *f)
5610 fp = open_config_file(f);
5614 DBGP("reading contents from config file '%s'", f);
5617 char buff[CONF_BUFF_SIZE], *name, *value;
5618 int ret = do_config_step(&line, fp, buff, &name, &value);
5619 if (ret == CONF_BREAK) {
5621 } else if (ret == CONF_CONTINUE) {
5626 X11_initialisation();
5627 if (x_initialised == YES) {
5629 color0 = get_x11_color(value);
5636 X11_initialisation();
5637 if (x_initialised == YES) {
5639 color1 = get_x11_color(value);
5646 X11_initialisation();
5647 if (x_initialised == YES) {
5649 color2 = get_x11_color(value);
5656 X11_initialisation();
5657 if (x_initialised == YES) {
5659 color3 = get_x11_color(value);
5666 X11_initialisation();
5667 if (x_initialised == YES) {
5669 color4 = get_x11_color(value);
5676 X11_initialisation();
5677 if (x_initialised == YES) {
5679 color5 = get_x11_color(value);
5686 X11_initialisation();
5687 if (x_initialised == YES) {
5689 color6 = get_x11_color(value);
5696 X11_initialisation();
5697 if (x_initialised == YES) {
5699 color7 = get_x11_color(value);
5706 X11_initialisation();
5707 if (x_initialised == YES) {
5709 color8 = get_x11_color(value);
5716 X11_initialisation();
5717 if (x_initialised == YES) {
5719 color9 = get_x11_color(value);
5725 CONF("default_color") {
5726 X11_initialisation();
5727 if (x_initialised == YES) {
5729 default_fg_color = get_x11_color(value);
5735 CONF3("default_shade_color", "default_shadecolor") {
5736 X11_initialisation();
5737 if (x_initialised == YES) {
5739 default_bg_color = get_x11_color(value);
5745 CONF3("default_outline_color", "default_outlinecolor") {
5746 X11_initialisation();
5747 if (x_initialised == YES) {
5749 default_out_color = get_x11_color(value);
5756 CONF("own_window_colour") {
5757 X11_initialisation();
5758 if (x_initialised == YES) {
5760 background_colour = get_x11_color(value);
5762 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5763 "'#' for hex colours");
5769 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5770 if(output_methods & TO_X) {
5771 X11_initialisation();
5780 #undef CONF_CONTINUE
5781 #undef CONF_BUFF_SIZE
5789 static void print_help(const char *prog_name) {
5790 printf("Usage: %s [OPTION]...\n"
5791 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5792 "window. Command line options will override configurations defined in config\n"
5794 " -v, --version version\n"
5795 " -q, --quiet quiet mode\n"
5796 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
5797 " -c, --config=FILE config file to load\n"
5798 #ifdef CONFIG_OUTPUT
5799 " -C, --print-config print the builtin default config to stdout\n"
5800 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5802 " -d, --daemonize daemonize, fork to background\n"
5803 " -h, --help help\n"
5805 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5806 " -f, --font=FONT font to use\n"
5807 " -X, --display=DISPLAY X11 display to use\n"
5809 " -o, --own-window create own window to draw\n"
5812 " -b, --double-buffer double buffer (prevents flickering)\n"
5814 " -w, --window-id=WIN_ID window id to draw\n"
5815 " -x X x position\n"
5816 " -y Y y position\n"
5818 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
5819 " -u, --interval=SECS update interval\n"
5820 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n"
5821 " -p, --pause=SECS pause for SECS seconds at startup before doing anything\n",
5826 /* : means that character before that takes an argument */
5827 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5837 #ifdef CONFIG_OUTPUT
5842 static const struct option longopts[] = {
5843 { "help", 0, NULL, 'h' },
5844 { "version", 0, NULL, 'V' },
5845 { "quiet", 0, NULL, 'q' },
5846 { "debug", 0, NULL, 'D' },
5847 { "config", 1, NULL, 'c' },
5848 #ifdef CONFIG_OUTPUT
5849 { "print-config", 0, NULL, 'C' },
5851 { "daemonize", 0, NULL, 'd' },
5853 { "alignment", 1, NULL, 'a' },
5854 { "font", 1, NULL, 'f' },
5855 { "display", 1, NULL, 'X' },
5857 { "own-window", 0, NULL, 'o' },
5860 { "double-buffer", 0, NULL, 'b' },
5862 { "window-id", 1, NULL, 'w' },
5864 { "text", 1, NULL, 't' },
5865 { "interval", 1, NULL, 'u' },
5866 { "pause", 1, NULL, 'p' },
5870 void set_current_config(void);
5871 void set_current_config(void)
5873 /* check if specified config file is valid */
5874 if (current_config) {
5876 if (stat(current_config, &sb) ||
5877 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
5878 NORM_ERR("invalid configuration file '%s'\n", current_config);
5879 free(current_config);
5884 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
5886 if (!current_config) {
5887 /* load default config file */
5888 char buf[DEFAULT_TEXT_BUFFER_SIZE];
5891 /* Try to use personal config file first */
5892 to_real_path(buf, CONFIG_FILE);
5893 if (buf[0] && (fp = fopen(buf, "r"))) {
5894 current_config = strndup(buf, max_user_text);
5898 /* Try to use system config file if personal config not readable */
5899 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
5900 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
5904 /* No readable config found */
5905 if (!current_config) {
5906 #define NOCFGFILEFOUND "no readable personal or system-wide config file found"
5907 #ifdef BUILD_BUILTIN_CONFIG
5908 current_config = strdup("==builtin==");
5909 NORM_ERR(NOCFGFILEFOUND
5910 ", using builtin default");
5912 CRIT_ERR(NULL, NULL, NOCFGFILEFOUND);
5913 #endif /* ! CONF_OUTPUT */
5918 void initialisation(int argc, char **argv) {
5919 struct sigaction act, oact;
5921 set_default_configurations();
5922 set_current_config();
5923 load_config_file(current_config);
5924 currentconffile = conftree_add(currentconffile, current_config);
5926 /* init specials array */
5927 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5928 NORM_ERR("failed to create specials array");
5932 if (current_mail_spool == NULL) {
5935 variable_substitute(MAIL_FILE, buf, 256);
5937 if (buf[0] != '\0') {
5938 current_mail_spool = strndup(buf, text_buffer_size);
5943 /* handle other command line arguments */
5945 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5946 || defined(__NetBSD__)
5947 optind = optreset = 1;
5952 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5953 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5954 "kvm_open")) == NULL) {
5955 CRIT_ERR(NULL, NULL, "cannot read kvm");
5957 pthread_mutex_init(&kvm_proc_mutex, NULL);
5961 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5970 fork_to_background = 1;
5973 global_debug_level++;
5977 set_first_font(optarg);
5980 setalignment(&text_alignment, window.type, optarg, NULL, 0, 0);
5999 global_text = strndup(optarg, max_user_text);
6000 convert_escapes(global_text);
6004 update_interval = strtod(optarg, 0);
6005 update_interval_old = update_interval;
6006 if (info.music_player_interval == 0) {
6007 // default to update_interval
6008 info.music_player_interval = update_interval;
6013 total_run_times = strtod(optarg, 0);
6017 gap_x = atoi(optarg);
6021 gap_y = atoi(optarg);
6026 startup_pause = atoi(optarg);
6027 sleep(startup_pause);
6038 if (output_methods & TO_X) {
6039 load_config_file_x11(current_config);
6043 /* generate text and get initial size */
6044 extract_variable_text(global_text);
6051 if (fork_to_background && first_pass) {
6056 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
6063 fprintf(stderr, "\n");
6068 /* parent process */
6069 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
6076 start_update_threading();
6078 text_buffer = malloc(max_user_text);
6079 memset(text_buffer, 0, max_user_text);
6080 tmpstring1 = malloc(text_buffer_size);
6081 memset(tmpstring1, 0, text_buffer_size);
6082 tmpstring2 = malloc(text_buffer_size);
6083 memset(tmpstring2, 0, text_buffer_size);
6088 X11_create_window();
6091 llua_setup_info(&info, update_interval);
6092 #endif /* HAVE_LUA */
6097 /* Set signal handlers */
6098 act.sa_handler = signal_handler;
6099 sigemptyset(&act.sa_mask);
6102 act.sa_flags |= SA_RESTART;
6105 if ( sigaction(SIGINT, &act, &oact) < 0
6106 || sigaction(SIGALRM, &act, &oact) < 0
6107 || sigaction(SIGUSR1, &act, &oact) < 0
6108 || sigaction(SIGHUP, &act, &oact) < 0
6109 || sigaction(SIGTERM, &act, &oact) < 0) {
6110 NORM_ERR("error setting signal handler: %s", strerror(errno));
6114 llua_startup_hook();
6115 #endif /* HAVE_LUA */
6118 int main(int argc, char **argv)
6127 g_signal_pending = 0;
6128 max_user_text = MAX_USER_TEXT_DEFAULT;
6130 memset(&info, 0, sizeof(info));
6134 #ifdef TCP_PORT_MONITOR
6135 /* set default connection limit */
6136 tcp_portmon_set_max_connections(0);
6140 if(curl_global_init(CURL_GLOBAL_ALL))
6141 NORM_ERR("curl_global_init() failed, you may not be able to use curl variables");
6144 /* handle command line parameters that don't change configs */
6146 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
6147 || ((s = getenv("LANG")) && *s)) {
6148 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6150 NORM_ERR("malloc failed");
6152 for (x = 0; x < strlen(s); x++) {
6153 temp[x] = tolower(s[x]);
6156 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6162 if (!setlocale(LC_CTYPE, "")) {
6163 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6167 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6178 if (current_config) {
6179 free(current_config);
6181 current_config = strndup(optarg, max_user_text);
6184 if (!freopen("/dev/null", "w", stderr)) {
6185 NORM_ERR("unable to redirect stderr to /dev/null");
6189 print_help(argv[0]);
6191 #ifdef CONFIG_OUTPUT
6198 window.window = strtol(optarg, 0, 0);
6203 disp = strdup(optarg);
6212 set_current_config();
6215 /* Load xoap keys, if existing */
6219 #ifdef HAVE_SYS_INOTIFY_H
6220 inotify_fd = inotify_init();
6221 if(inotify_fd != -1) {
6224 fl = fcntl(inotify_fd, F_GETFL);
6225 fcntl(inotify_fd, F_SETFL, fl | O_NONBLOCK);
6227 #endif /* HAVE_SYS_INOTIFY_H */
6229 initialisation(argc, argv);
6231 first_pass = 0; /* don't ever call fork() again */
6233 //dbus code from http://www.matthew.ath.cx/misc/dbus
6234 dbus_error_init(&err);
6235 // connect to the bus
6236 conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
6237 if (dbus_error_is_set(&err)) {
6238 fprintf(stderr, "Connection Error (%s)\n", err.message);
6239 dbus_error_free(&err);
6241 conn2 = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
6242 if (dbus_error_is_set(&err)) {
6243 fprintf(stderr, "Connection Error (%s)\n", err.message);
6244 dbus_error_free(&err);
6246 if (NULL == conn || NULL == conn2) {
6249 // request a name on the bus (we don't use it for now)
6250 ret = dbus_bus_request_name(conn, "net.appcheck.Conky",
6251 DBUS_NAME_FLAG_REPLACE_EXISTING
6253 if (dbus_error_is_set(&err)) {
6254 fprintf(stderr, "Name Error (%s)\n", err.message);
6255 dbus_error_free(&err);
6257 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
6258 //dbus_connection_close(conn);
6259 fprintf(stderr, "some other dbus Name Error\n");
6266 dbus_connection_close(conn);
6267 dbus_connection_close(conn2);
6270 curl_global_cleanup();
6273 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6275 pthread_mutex_destroy(&kvm_proc_mutex);
6282 void alarm_handler(void) {
6284 kill(childpid, SIGTERM);
6288 static void signal_handler(int sig)
6290 /* signal handler is light as a feather, as it should be.
6291 * we will poll g_signal_pending with each loop of conky
6292 * and do any signal processing there, NOT here (except
6293 * SIGALRM because this is caused when conky is hanging) */
6294 if(sig == SIGALRM) {
6297 g_signal_pending = sig;