1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "text_object.h"
44 #include <sys/param.h>
47 #include <X11/Xutil.h>
49 #include <X11/extensions/Xdamage.h>
55 #include <sys/types.h>
57 #include <netinet/in.h>
76 #include "temphelper.h"
80 /* check for OS and include appropriate headers */
81 #if defined(__linux__)
83 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
85 #elif defined(__OpenBSD__)
89 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
90 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
91 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
92 int apm_getinfo(int fd, apm_info_t aip);
93 char *get_apm_adapter(void);
94 char *get_apm_battery_life(void);
95 char *get_apm_battery_time(void);
103 #include "defconfig.h"
104 #ifdef HAVE_FOPENCOOKIE
105 #include "conf_cookie.h"
110 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
113 #define MAIL_FILE "$MAIL"
114 #define MAX_IF_BLOCK_DEPTH 5
116 //#define SIGNAL_BLOCKING
117 #undef SIGNAL_BLOCKING
119 /* debugging level, used by logging.h */
120 int global_debug_level = 0;
122 /* two strings for internal use */
123 static char *tmpstring1, *tmpstring2;
125 /* variables holding various config settings */
133 int top_cpu, top_mem, top_time;
135 enum x_initialiser_state x_initialised = NO;
136 static volatile int g_signal_pending;
137 /* Update interval */
138 double update_interval;
141 /* prototypes for internally used functions */
142 static void signal_handler(int);
143 static void print_version(void) __attribute__((noreturn));
144 static void reload_config(void);
145 static void clean_up(void);
146 static void generate_text_internal(char *, int, struct text_object,
147 struct information *);
148 static int extract_variable_text_internal(struct text_object *,
151 static void print_version(void)
153 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
155 printf("\nCompiled in features:\n\n"
156 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
160 " * Xdamage extension\n"
161 # endif /* HAVE_XDAMAGE */
163 " * XDBE (double buffer extension)\n"
164 # endif /* HAVE_XDBE */
169 "\n Music detection:\n"
172 #endif /* AUDACIOUS */
192 #ifdef TCP_PORT_MONITOR
194 #endif /* TCP_PORT_MONITOR */
200 #endif /* HAVE_IWLIB */
202 " * support for IBM/Lenovo notebooks\n"
212 #endif /* CONFIG_OUTPUT */
218 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
225 static int text_start_x, text_start_y; /* text start position in window */
226 static int text_width, text_height;
243 /* struct that has all info to be shared between
244 * instances of the same text object */
245 struct information info;
247 /* default config file */
248 static char *current_config;
250 /* display to connect to */
251 static char *disp = NULL;
253 /* set to 1 if you want all text to be in uppercase */
254 static unsigned int stuff_in_upper_case;
256 /* Run how many times? */
257 static unsigned long total_run_times;
260 static int fork_to_background;
262 static int cpu_avg_samples, net_avg_samples;
264 /* filenames for output */
265 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
266 char *append_file = NULL; FILE *append_fpointer = NULL;
270 static int show_graph_scale;
271 static int show_graph_range;
273 /* Position on the screen */
274 static int text_alignment;
275 static int gap_x, gap_y;
278 static int draw_borders;
279 static int draw_graph_borders;
280 static int stippled_borders;
282 static int draw_shades, draw_outline;
284 static int border_margin, border_width;
286 static long default_fg_color, default_bg_color, default_out_color;
288 /* create own window or draw stuff to root? */
289 static int set_transparent = 0;
292 static int own_window = 0;
293 static int background_colour = 0;
295 /* fixed size/pos is set if wm/user changes them */
296 static int fixed_size = 0, fixed_pos = 0;
299 static int minimum_width, minimum_height;
300 static int maximum_width;
305 static int sensor_device;
308 static long color0, color1, color2, color3, color4, color5, color6, color7,
311 static char *template[10];
313 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
314 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
316 /* maximum size of individual text buffers, ie $exec buffer size */
317 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
320 #define CODEPAGE_LENGTH 20
322 long iconv_count = 0;
323 char iconv_converting;
324 static iconv_t **iconv_cd = 0;
326 int register_iconv(iconv_t *new_iconv)
328 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
330 CRIT_ERR("Out of memory");
332 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
333 if (!iconv_cd[iconv_count]) {
334 CRIT_ERR("Out of memory");
336 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
341 void free_iconv(void)
346 for (i = 0; i < iconv_count; i++) {
348 iconv_close(*iconv_cd[i]);
362 /* no buffers in used memory? */
365 /* pad percentages to decimals? */
366 static int pad_percents = 0;
368 static char *global_text = 0;
369 long global_text_lines;
371 static int total_updates;
372 static int updatereset;
374 int check_contains(char *f, char *s)
377 FILE *where = fopen(f, "r");
380 char buf1[256], buf2[256];
382 while (fgets(buf1, 256, where)) {
383 sscanf(buf1, "%255s", buf2);
384 if (strstr(buf2, s)) {
391 ERR("Could not open the file");
397 static inline int calc_text_width(const char *s, int l)
399 if ((output_methods & TO_X) == 0)
406 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
407 (const FcChar8 *) s, l, &gi);
409 XftTextExtents8(display, fonts[selected_font].xftfont,
410 (const FcChar8 *) s, l, &gi);
416 return XTextWidth(fonts[selected_font].font, s, l);
421 /* formatted text to render on screen, generated in generate_text(),
422 * drawn in draw_stuff() */
424 static char *text_buffer;
427 static unsigned int special_index; /* used when drawing */
430 /* quite boring functions */
432 static inline void for_each_line(char *b, void f(char *))
436 for (ps = b, pe = b; *pe; pe++) {
450 static void convert_escapes(char *buf)
452 char *p = buf, *s = buf;
459 } else if (*s == '\\') {
470 /* Prints anything normally printed with snprintf according to the current value
471 * of use_spacer. Actually slightly more flexible than snprintf, as you can
472 * safely specify the destination buffer as one of your inputs. */
473 int spaced_print(char *buf, int size, const char *format, int width, ...)
482 tempbuf = malloc(size * sizeof(char));
484 // Passes the varargs along to vsnprintf
485 va_start(argp, width);
486 vsnprintf(tempbuf, size, format, argp);
489 switch (use_spacer) {
491 len = snprintf(buf, size, "%s", tempbuf);
494 len = snprintf(buf, size, "%*s", width, tempbuf);
497 len = snprintf(buf, size, "%-*s", width, tempbuf);
504 /* print percentage values
506 * - i.e., unsigned values between 0 and 100
507 * - respect the value of pad_percents */
508 static int percent_print(char *buf, int size, unsigned value)
510 return spaced_print(buf, size, "%u", pad_percents, value);
513 /* converts from bytes to human readable format (K, M, G, T)
515 * The algorithm always divides by 1024, as unit-conversion of byte
516 * counts suggests. But for output length determination we need to
517 * compare with 1000 here, as we print in decimal form. */
518 static void human_readable(long long num, char *buf, int size)
520 const char **suffix = suffixes;
534 if (llabs(num) < 1000LL) {
535 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
539 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
547 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
549 * The goal is to always have a significance of 3, by
550 * adjusting the decimal part of the number. Sample output:
554 * so the point of alignment resides between number and unit. The
555 * upside of this is that there is minimal padding necessary, though
556 * there should be a way to make alignment take place at the decimal
557 * dot (then with fixed width decimal part). * */
559 precision = 0; /* print 100-999 without decimal part */
561 precision = 1; /* print 10-99 with one decimal place */
563 precision = 2; /* print 0-9 with two decimal place */
565 spaced_print(buf, size, format, width, precision, fnum, *suffix);
568 /* global object list root element */
569 static struct text_object global_root_object;
571 static inline void read_exec(const char *data, char *buf, const int size)
573 FILE *fp = popen(data, "r");
574 int length = fread(buf, 1, size, fp);
578 if (length > 0 && buf[length - 1] == '\n') {
579 buf[length - 1] = '\0';
583 void *threaded_exec(void *) __attribute__((noreturn));
585 void *threaded_exec(void *arg)
588 struct text_object *obj = (struct text_object *)arg;
591 p2 = obj->data.texeci.buffer;
592 timed_thread_lock(obj->data.texeci.p_timed_thread);
593 read_exec(obj->data.texeci.cmd, obj->data.texeci.buffer,
601 timed_thread_unlock(obj->data.texeci.p_timed_thread);
602 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
603 timed_thread_exit(obj->data.texeci.p_timed_thread);
609 static struct text_object *new_text_object_internal(void)
611 struct text_object *obj = malloc(sizeof(struct text_object));
612 memset(obj, 0, sizeof(struct text_object));
616 /* free the list of text objects root points to */
617 static void free_text_objects(struct text_object *root)
619 struct text_object *obj;
625 #define data obj->data
626 for (obj = root->prev; obj; obj = root->prev) {
627 root->prev = obj->prev;
633 #endif /* !__OpenBSD__ */
638 close(data.sysfs.fd);
640 #endif /* __linux__ */
646 free(data.tztime.tz);
647 free(data.tztime.fmt);
650 free(data.mboxscan.args);
651 free(data.mboxscan.output);
656 case OBJ_unseen_mails:
657 case OBJ_flagged_mails:
658 case OBJ_unflagged_mails:
659 case OBJ_forwarded_mails:
660 case OBJ_unforwarded_mails:
661 case OBJ_replied_mails:
662 case OBJ_unreplied_mails:
663 case OBJ_draft_mails:
664 case OBJ_trashed_mails:
665 free(data.local_mail.box);
670 case OBJ_imap_unseen:
671 if (!obj->global_mode) {
675 case OBJ_imap_messages:
676 if (!obj->global_mode) {
683 case OBJ_pop3_unseen:
684 if (!obj->global_mode) {
689 if (!obj->global_mode) {
695 free_text_objects(obj->sub);
698 case OBJ_if_existing:
701 free(data.ifblock.s);
702 free(data.ifblock.str);
705 free(data.tail.logfile);
706 free(data.tail.buffer);
720 case OBJ_iconv_start:
725 case OBJ_disk_protect:
729 free(data.ifblock.s);
730 free(data.ifblock.str);
733 if (info.gw_info.iface) {
734 free(info.gw_info.iface);
735 info.gw_info.iface = 0;
737 if (info.gw_info.ip) {
738 free(info.gw_info.ip);
742 case OBJ_ioscheduler:
747 #if (defined(__FreeBSD__) || defined(__linux__))
749 free(data.ifblock.s);
750 free(data.ifblock.str);
753 case OBJ_xmms2_artist:
754 if (info.xmms2.artist) {
755 free(info.xmms2.artist);
756 info.xmms2.artist = 0;
759 case OBJ_xmms2_album:
760 if (info.xmms2.album) {
761 free(info.xmms2.album);
762 info.xmms2.album = 0;
765 case OBJ_xmms2_title:
766 if (info.xmms2.title) {
767 free(info.xmms2.title);
768 info.xmms2.title = 0;
771 case OBJ_xmms2_genre:
772 if (info.xmms2.genre) {
773 free(info.xmms2.genre);
774 info.xmms2.genre = 0;
777 case OBJ_xmms2_comment:
778 if (info.xmms2.comment) {
779 free(info.xmms2.comment);
780 info.xmms2.comment = 0;
784 if (info.xmms2.url) {
785 free(info.xmms2.url);
790 if (info.xmms2.date) {
791 free(info.xmms2.date);
795 case OBJ_xmms2_status:
796 if (info.xmms2.status) {
797 free(info.xmms2.status);
798 info.xmms2.status = 0;
801 case OBJ_xmms2_playlist:
802 if (info.xmms2.playlist) {
803 free(info.xmms2.playlist);
804 info.xmms2.playlist = 0;
807 case OBJ_xmms2_smart:
808 if (info.xmms2.artist) {
809 free(info.xmms2.artist);
810 info.xmms2.artist = 0;
812 if (info.xmms2.title) {
813 free(info.xmms2.title);
814 info.xmms2.title = 0;
816 if (info.xmms2.url) {
817 free(info.xmms2.url);
824 case OBJ_bmpx_artist:
828 case OBJ_bmpx_bitrate:
838 free(data.rss.action);
847 case OBJ_battery_short:
850 case OBJ_battery_time:
853 #endif /* !__OpenBSD__ */
858 free(data.execi.cmd);
859 free(data.execi.buffer);
862 free(data.texeci.cmd);
863 free(data.texeci.buffer);
871 if (info.first_process) {
872 free_all_processes();
873 info.first_process = NULL;
878 free(data.hddtemp.dev);
879 free(data.hddtemp.addr);
880 if (data.hddtemp.temp)
881 free(data.hddtemp.temp);
884 case OBJ_entropy_avail:
885 case OBJ_entropy_poolsize:
886 case OBJ_entropy_bar:
889 if (info.users.names) {
890 free(info.users.names);
891 info.users.names = 0;
895 if (info.users.terms) {
896 free(info.users.terms);
897 info.users.terms = 0;
901 if (info.users.times) {
902 free(info.users.times);
903 info.users.times = 0;
908 case OBJ_smapi_bat_perc:
909 case OBJ_smapi_bat_temp:
910 case OBJ_smapi_bat_power:
913 case OBJ_if_smapi_bat_installed:
914 free(data.ifblock.s);
915 free(data.ifblock.str);
929 case OBJ_mpd_bitrate:
933 case OBJ_mpd_password:
935 case OBJ_mpd_elapsed:
940 case OBJ_mpd_percent:
942 case OBJ_if_mpd_playing:
953 case OBJ_moc_totaltime:
954 case OBJ_moc_timeleft:
955 case OBJ_moc_curtime:
956 case OBJ_moc_bitrate:
962 free(data.scroll.text);
963 free_text_objects(obj->sub);
972 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
977 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
978 *a = mixer_init(buf1);
979 scan_bar(arg + n, w, h);
981 *a = mixer_init(NULL);
986 /* strip a leading /dev/ if any, following symlinks first
988 * BEWARE: this function returns a pointer to static content
989 * which gets overwritten in consecutive calls. I.e.:
990 * this function is NOT reentrant.
992 static const char *dev_name(const char *path)
994 static char buf[255]; /* should be enough for pathnames */
1000 #define DEV_NAME(x) \
1001 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1002 if ((buflen = readlink(path, buf, 254)) == -1)
1003 return DEV_NAME(path);
1005 return DEV_NAME(buf);
1009 /* construct_text_object() creates a new text_object */
1010 static struct text_object *construct_text_object(const char *s,
1011 const char *arg, long line, char allow_threaded, void **ifblock_opaque)
1013 // struct text_object *obj = new_text_object();
1014 struct text_object *obj = new_text_object_internal();
1018 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1019 obj->type = OBJ_##a; need_mask |= (1 << n); {
1020 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1021 obj->type = OBJ_##a; need_mask |= (1 << n); \
1022 obj_be_ifblock_if(ifblock_opaque, obj); {
1023 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1024 obj->type = OBJ_##a; need_mask |= (1 << n); {
1025 #define END } } else
1029 obj->type = OBJ_color;
1030 obj->data.l = get_x11_color(s);
1034 OBJ(freq, INFO_FREQ)
1037 obj->data.i = open_acpi_temperature(arg);
1038 END OBJ(acpiacadapter, 0)
1039 END OBJ(freq, INFO_FREQ)
1040 #endif /* !__OpenBSD__ */
1042 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1043 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1044 obj->data.cpu_index = 1;
1045 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1046 "Displaying the clock for CPU 1."); */
1048 obj->data.cpu_index = atoi(&arg[0]);
1051 END OBJ(freq_g, INFO_FREQ)
1053 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1054 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1055 obj->data.cpu_index = 1;
1056 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1057 "CPUs! Displaying the clock for CPU 1."); */
1059 obj->data.cpu_index = atoi(&arg[0]);
1062 #if defined(__linux__)
1063 END OBJ(voltage_mv, 0)
1065 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1066 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1067 obj->data.cpu_index = 1;
1068 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1069 "CPUs! Displaying voltage for CPU 1."); */
1071 obj->data.cpu_index = atoi(&arg[0]);
1074 END OBJ(voltage_v, 0)
1076 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1077 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1078 obj->data.cpu_index = 1;
1079 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1080 "CPUs! Displaying voltage for CPU 1."); */
1082 obj->data.cpu_index = atoi(&arg[0]);
1087 END OBJ(wireless_essid, INFO_NET)
1089 obj->data.net = get_net_stat(arg);
1091 CRIT_ERR("wireless_essid: needs an argument");
1093 END OBJ(wireless_mode, INFO_NET)
1095 obj->data.net = get_net_stat(arg);
1097 CRIT_ERR("wireless_mode: needs an argument");
1099 END OBJ(wireless_bitrate, INFO_NET)
1101 obj->data.net = get_net_stat(arg);
1103 CRIT_ERR("wireless_bitrate: needs an argument");
1105 END OBJ(wireless_ap, INFO_NET)
1107 obj->data.net = get_net_stat(arg);
1109 CRIT_ERR("wireless_ap: needs an argument");
1111 END OBJ(wireless_link_qual, INFO_NET)
1113 obj->data.net = get_net_stat(arg);
1115 CRIT_ERR("wireless_link_qual: needs an argument");
1117 END OBJ(wireless_link_qual_max, INFO_NET)
1119 obj->data.net = get_net_stat(arg);
1121 CRIT_ERR("wireless_link_qual_max: needs an argument");
1123 END OBJ(wireless_link_qual_perc, INFO_NET)
1125 obj->data.net = get_net_stat(arg);
1127 CRIT_ERR("wireless_link_qual_perc: needs an argument");
1129 END OBJ(wireless_link_bar, INFO_NET)
1131 arg = scan_bar(arg, &obj->a, &obj->b);
1132 obj->data.net = get_net_stat(arg);
1134 CRIT_ERR("wireless_link_bar: needs an argument");
1136 #endif /* HAVE_IWLIB */
1138 #endif /* __linux__ */
1146 sscanf(arg, "%63s", bat);
1148 strcpy(bat, "BAT0");
1150 obj->data.s = strndup(bat, text_buffer_size);
1151 END OBJ(battery_short, 0)
1155 sscanf(arg, "%63s", bat);
1157 strcpy(bat, "BAT0");
1159 obj->data.s = strndup(bat, text_buffer_size);
1160 END OBJ(battery_time, 0)
1164 sscanf(arg, "%63s", bat);
1166 strcpy(bat, "BAT0");
1168 obj->data.s = strndup(bat, text_buffer_size);
1169 END OBJ(battery_percent, 0)
1173 sscanf(arg, "%63s", bat);
1175 strcpy(bat, "BAT0");
1177 obj->data.s = strndup(bat, text_buffer_size);
1178 END OBJ(battery_bar, 0)
1182 arg = scan_bar(arg, &obj->a, &obj->b);
1183 sscanf(arg, "%63s", bat);
1185 strcpy(bat, "BAT0");
1187 obj->data.s = strndup(bat, text_buffer_size);
1188 #endif /* !__OpenBSD__ */
1190 #if defined(__linux__)
1191 END OBJ(disk_protect, 0)
1193 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1195 CRIT_ERR("disk_protect needs an argument");
1196 END OBJ(i8k_version, INFO_I8K)
1197 END OBJ(i8k_bios, INFO_I8K)
1198 END OBJ(i8k_serial, INFO_I8K)
1199 END OBJ(i8k_cpu_temp, INFO_I8K)
1200 END OBJ(i8k_left_fan_status, INFO_I8K)
1201 END OBJ(i8k_right_fan_status, INFO_I8K)
1202 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1203 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1204 END OBJ(i8k_ac_status, INFO_I8K)
1205 END OBJ(i8k_buttons_status, INFO_I8K)
1208 END OBJ(ibm_temps, 0)
1210 CRIT_ERR("ibm_temps: needs an argument");
1212 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1213 obj->data.sensor = 0;
1214 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1215 "Using 0 (CPU temp sensor).");
1217 obj->data.sensor = atoi(&arg[0]);
1218 END OBJ(ibm_volume, 0)
1219 END OBJ(ibm_brightness, 0)
1221 END OBJ_IF(if_gw, 0)
1222 END OBJ(ioscheduler, 0)
1224 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1227 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1228 END OBJ(laptop_mode, 0)
1229 END OBJ(pb_battery, 0)
1230 if (arg && strcmp(arg, "status") == EQUAL) {
1231 obj->data.i = PB_BATT_STATUS;
1232 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1233 obj->data.i = PB_BATT_PERCENT;
1234 } else if (arg && strcmp(arg, "time") == EQUAL) {
1235 obj->data.i = PB_BATT_TIME;
1237 ERR("pb_battery: needs one argument: status, percent or time");
1242 #endif /* __linux__ */
1243 #if (defined(__FreeBSD__) || defined(__linux__))
1244 END OBJ_IF(if_up, 0)
1246 ERR("if_up needs an argument");
1247 obj->data.ifblock.s = 0;
1249 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1251 #if defined(__OpenBSD__)
1252 END OBJ(obsd_sensors_temp, 0)
1254 CRIT_ERR("obsd_sensors_temp: needs an argument");
1256 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1257 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1258 obj->data.sensor = 0;
1259 ERR("Invalid temperature sensor number!");
1261 obj->data.sensor = atoi(&arg[0]);
1262 END OBJ(obsd_sensors_fan, 0)
1264 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1267 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1268 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1269 obj->data.sensor = 0;
1270 ERR("Invalid fan sensor number!");
1272 obj->data.sensor = atoi(&arg[0]);
1273 END OBJ(obsd_sensors_volt, 0)
1275 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1278 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1279 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1280 obj->data.sensor = 0;
1281 ERR("Invalid voltage sensor number!");
1283 obj->data.sensor = atoi(&arg[0]);
1284 END OBJ(obsd_vendor, 0)
1285 END OBJ(obsd_product, 0)
1286 #endif /* __OpenBSD__ */
1287 END OBJ(buffers, INFO_BUFFERS)
1288 END OBJ(cached, INFO_BUFFERS)
1289 #define SCAN_CPU(__arg, __var) { \
1291 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset)) \
1292 __arg += __offset; \
1296 END OBJ(cpu, INFO_CPU)
1297 SCAN_CPU(arg, obj->data.cpu_index);
1298 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1299 END OBJ(cpugauge, INFO_CPU)
1300 SCAN_CPU(arg, obj->data.cpu_index);
1301 scan_gauge(arg, &obj->a, &obj->b);
1302 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1303 END OBJ(cpubar, INFO_CPU)
1304 SCAN_CPU(arg, obj->data.cpu_index);
1305 scan_bar(arg, &obj->a, &obj->b);
1306 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1307 END OBJ(cpugraph, INFO_CPU)
1308 SCAN_CPU(arg, obj->data.cpu_index);
1309 scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1310 &obj->e, &obj->showaslog);
1311 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1312 END OBJ(loadgraph, INFO_LOADAVG)
1313 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1314 &obj->e, &obj->showaslog);
1318 r = sscanf(arg, "%d", &a);
1320 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1323 END OBJ(diskio, INFO_DISKIO)
1324 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1325 END OBJ(diskio_read, INFO_DISKIO)
1326 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1327 END OBJ(diskio_write, INFO_DISKIO)
1328 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1329 END OBJ(diskiograph, INFO_DISKIO)
1330 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1331 &obj->e, &obj->showaslog);
1333 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1336 END OBJ(diskiograph_read, INFO_DISKIO)
1337 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1338 &obj->e, &obj->showaslog);
1340 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1343 END OBJ(diskiograph_write, INFO_DISKIO)
1344 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1345 &obj->e, &obj->showaslog);
1347 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1352 if (output_methods & TO_X) {
1353 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1357 obj->data.l = color0;
1359 obj->data.l = color1;
1361 obj->data.l = color2;
1363 obj->data.l = color3;
1365 obj->data.l = color4;
1367 obj->data.l = color5;
1369 obj->data.l = color6;
1371 obj->data.l = color7;
1373 obj->data.l = color8;
1375 obj->data.l = color9;
1377 obj->data.s = scan_font(arg);
1378 END OBJ(conky_version, 0)
1379 END OBJ(conky_build_date, 0)
1380 END OBJ(conky_build_arch, 0)
1381 END OBJ(downspeed, INFO_NET)
1383 obj->data.net = get_net_stat(arg);
1385 CRIT_ERR("downspeed needs argument");
1387 END OBJ(downspeedf, INFO_NET)
1389 obj->data.net = get_net_stat(arg);
1391 CRIT_ERR("downspeedf needs argument");
1393 END OBJ(downspeedgraph, INFO_NET)
1394 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1395 &obj->e, &obj->showaslog);
1397 // default to DEFAULTNETDEV
1398 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
1399 obj->data.net = get_net_stat(buf);
1402 obj_be_ifblock_else(ifblock_opaque, obj);
1404 obj_be_ifblock_endif(ifblock_opaque, obj);
1406 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1408 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1411 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1413 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1414 END OBJ(execgauge, 0)
1415 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1417 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1418 END OBJ(execgraph, 0)
1419 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1420 END OBJ(execibar, 0)
1423 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1426 ERR("${execibar <interval> command}");
1427 obj->type = OBJ_text;
1428 snprintf(buf, 256, "${%s}", s);
1429 obj->data.s = strndup(buf, text_buffer_size);
1431 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1433 END OBJ(execigraph, 0)
1436 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1439 ERR("${execigraph <interval> command}");
1440 obj->type = OBJ_text;
1441 snprintf(buf, 256, "${%s}", s);
1442 obj->data.s = strndup(buf, text_buffer_size);
1444 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1449 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1452 ERR("${execi <interval> command}");
1453 obj->type = OBJ_text;
1454 snprintf(buf, 256, "${%s}", s);
1455 obj->data.s = strndup(buf, text_buffer_size);
1457 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1458 obj->data.execi.buffer = malloc(text_buffer_size);
1463 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1466 ERR("${execi <interval> command}");
1467 obj->type = OBJ_text;
1468 snprintf(buf, 256, "${%s}", s);
1469 obj->data.s = strndup(buf, text_buffer_size);
1471 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1472 obj->data.execi.buffer = malloc(text_buffer_size);
1474 END OBJ_THREAD(texeci, 0)
1477 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1480 ERR("${texeci <interval> command}");
1481 obj->type = OBJ_text;
1482 snprintf(buf, 256, "${%s}", s);
1483 obj->data.s = strndup(buf, text_buffer_size);
1485 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1486 obj->data.texeci.buffer = malloc(text_buffer_size);
1488 obj->data.texeci.p_timed_thread = NULL;
1489 END OBJ(pre_exec, 0)
1490 obj->type = OBJ_text;
1494 read_exec(arg, buf, sizeof(buf));
1495 obj->data.s = strndup(buf, text_buffer_size);
1497 obj->data.s = strndup("", text_buffer_size);
1500 END OBJ(fs_bar, INFO_FS)
1501 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1503 while (isspace(*arg)) {
1512 obj->data.fsbar.fs = prepare_fs_stat(arg);
1513 END OBJ(fs_bar_free, INFO_FS)
1514 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1516 while (isspace(*arg)) {
1526 obj->data.fsbar.fs = prepare_fs_stat(arg);
1527 END OBJ(fs_free, INFO_FS)
1531 obj->data.fs = prepare_fs_stat(arg);
1532 END OBJ(fs_used_perc, INFO_FS)
1536 obj->data.fs = prepare_fs_stat(arg);
1537 END OBJ(fs_free_perc, INFO_FS)
1541 obj->data.fs = prepare_fs_stat(arg);
1542 END OBJ(fs_size, INFO_FS)
1546 obj->data.fs = prepare_fs_stat(arg);
1547 END OBJ(fs_type, INFO_FS)
1551 obj->data.fs = prepare_fs_stat(arg);
1552 END OBJ(fs_used, INFO_FS)
1556 obj->data.fs = prepare_fs_stat(arg);
1558 obj->data.i = arg ? atoi(arg) : 1;
1559 END OBJ(nameserver, INFO_DNS)
1560 obj->data.i = arg ? atoi(arg) : 0;
1562 obj->data.i = arg ? atoi(arg) : 1;
1564 obj->data.i = arg ? atoi(arg) : 1;
1568 ERR("goto needs arguments");
1569 obj->type = OBJ_text;
1570 obj->data.s = strndup("${goto}", text_buffer_size);
1574 obj->data.i = atoi(arg);
1580 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1581 sscanf(arg, "%d", &b);
1587 obj->data.pair.a = a;
1588 obj->data.pair.b = b;
1591 END OBJ(i2c, INFO_SYSFS)
1592 char buf1[64], buf2[64];
1596 ERR("i2c needs arguments");
1597 obj->type = OBJ_text;
1598 // obj->data.s = strndup("${i2c}", text_buffer_size);
1602 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1603 /* if scanf couldn't read three values, read type and num and use
1605 sscanf(arg, "%63s %d", buf2, &n);
1606 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
1607 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1608 strncpy(obj->data.sysfs.type, buf2, 63);
1610 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
1611 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1612 strncpy(obj->data.sysfs.type, buf2, 63);
1615 END OBJ(platform, INFO_SYSFS)
1616 char buf1[64], buf2[64];
1620 ERR("platform needs arguments");
1621 obj->type = OBJ_text;
1625 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1626 /* if scanf couldn't read three values, read type and num and use
1628 sscanf(arg, "%63s %d", buf2, &n);
1629 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
1630 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1631 strncpy(obj->data.sysfs.type, buf2, 63);
1633 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
1634 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1635 strncpy(obj->data.sysfs.type, buf2, 63);
1638 END OBJ(hwmon, INFO_SYSFS)
1639 char buf1[64], buf2[64];
1643 ERR("hwmon needs argumanets");
1644 obj->type = OBJ_text;
1648 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1649 /* if scanf couldn't read three values, read type and num and use
1651 sscanf(arg, "%63s %d", buf2, &n);
1652 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
1653 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1654 strncpy(obj->data.sysfs.type, buf2, 63);
1656 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
1657 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1658 strncpy(obj->data.sysfs.type, buf2, 63);
1660 #endif /* !__OpenBSD__ */
1663 /* we have three different types of top (top, top_mem and top_time). To
1664 * avoid having almost-same code three times, we have this special
1666 if (strncmp(s, "top", 3) == EQUAL) {
1670 need_mask |= (1 << INFO_TOP);
1673 obj->type = OBJ_top;
1675 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1676 obj->type = OBJ_top_mem;
1678 } else if (strcmp(&s[3], "_time") == EQUAL) {
1679 obj->type = OBJ_top_time;
1682 ERR("Must be top, top_mem or top_time");
1687 ERR("top needs arguments");
1691 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1692 if (strcmp(buf, "name") == EQUAL) {
1693 obj->data.top.type = TOP_NAME;
1694 } else if (strcmp(buf, "cpu") == EQUAL) {
1695 obj->data.top.type = TOP_CPU;
1696 } else if (strcmp(buf, "pid") == EQUAL) {
1697 obj->data.top.type = TOP_PID;
1698 } else if (strcmp(buf, "mem") == EQUAL) {
1699 obj->data.top.type = TOP_MEM;
1700 } else if (strcmp(buf, "time") == EQUAL) {
1701 obj->data.top.type = TOP_TIME;
1702 } else if (strcmp(buf, "mem_res") == EQUAL) {
1703 obj->data.top.type = TOP_MEM_RES;
1704 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1705 obj->data.top.type = TOP_MEM_VSIZE;
1707 ERR("invalid type arg for top");
1708 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1711 if (n < 1 || n > 10) {
1712 ERR("invalid num arg for top. Must be between 1 and 10.");
1715 obj->data.top.num = n - 1;
1718 ERR("invalid argument count for top");
1721 } else OBJ(addr, INFO_NET)
1723 obj->data.net = get_net_stat(arg);
1725 CRIT_ERR("addr needs argument");
1727 #if defined(__linux__)
1728 END OBJ(addrs, INFO_NET)
1730 obj->data.net = get_net_stat(arg);
1732 CRIT_ERR("addrs needs argument");
1734 #endif /* __linux__ */
1736 if (init_tail_object(obj, arg)) {
1737 obj->type = OBJ_text;
1738 obj->data.s = strndup("${tail}", text_buffer_size);
1741 if (init_head_object(obj, arg)) {
1742 obj->type = OBJ_text;
1743 obj->data.s = strndup("${head}", text_buffer_size);
1747 obj->data.s = strdup(arg);
1749 CRIT_ERR("lines needs a argument");
1753 obj->data.s = strdup(arg);
1755 CRIT_ERR("words needs a argument");
1757 END OBJ(loadavg, INFO_LOADAVG)
1758 int a = 1, b = 2, c = 3, r = 3;
1761 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1762 if (r >= 3 && (c < 1 || c > 3)) {
1765 if (r >= 2 && (b < 1 || b > 3)) {
1768 if (r >= 1 && (a < 1 || a > 3)) {
1772 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1773 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1774 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1775 END OBJ_IF(if_empty, 0)
1777 ERR("if_empty needs an argument");
1778 obj->data.ifblock.s = 0;
1780 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1781 obj->sub = malloc(sizeof(struct text_object));
1782 extract_variable_text_internal(obj->sub,
1783 obj->data.ifblock.s, 0);
1785 END OBJ_IF(if_match, 0)
1787 ERR("if_match needs arguments");
1788 obj->data.ifblock.s = 0;
1790 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1791 obj->sub = malloc(sizeof(struct text_object));
1792 extract_variable_text_internal(obj->sub,
1793 obj->data.ifblock.s, 0);
1795 END OBJ_IF(if_existing, 0)
1797 ERR("if_existing needs an argument or two");
1798 obj->data.ifblock.s = NULL;
1799 obj->data.ifblock.str = NULL;
1801 char buf1[256], buf2[256];
1802 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
1805 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
1806 obj->data.ifblock.str = NULL;
1808 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
1809 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
1812 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
1813 END OBJ_IF(if_mounted, 0)
1815 ERR("if_mounted needs an argument");
1816 obj->data.ifblock.s = 0;
1818 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1820 END OBJ_IF(if_running, 0)
1824 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1825 obj->data.ifblock.s = strndup(buf, text_buffer_size);
1827 ERR("if_running needs an argument");
1828 obj->data.ifblock.s = 0;
1834 char box[256], dst[256];
1838 /* Kapil: Changed from MAIL_FILE to
1839 current_mail_spool since the latter
1840 is a copy of the former if undefined
1841 but the latter should take precedence
1843 strncpy(box, current_mail_spool, sizeof(box));
1845 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1847 strncpy(box, arg, sizeof(box));
1851 variable_substitute(box, dst, sizeof(dst));
1852 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1853 obj->data.local_mail.interval = n1;
1854 END OBJ(new_mails, 0)
1856 char box[256], dst[256];
1860 strncpy(box, current_mail_spool, sizeof(box));
1862 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1864 strncpy(box, arg, sizeof(box));
1868 variable_substitute(box, dst, sizeof(dst));
1869 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1870 obj->data.local_mail.interval = n1;
1871 END OBJ(seen_mails, 0)
1873 char box[256], dst[256];
1877 strncpy(box, current_mail_spool, sizeof(box));
1879 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1881 strncpy(box, arg, sizeof(box));
1885 variable_substitute(box, dst, sizeof(dst));
1886 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1887 obj->data.local_mail.interval = n1;
1888 END OBJ(unseen_mails, 0)
1890 char box[256], dst[256];
1894 strncpy(box, current_mail_spool, sizeof(box));
1896 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1898 strncpy(box, arg, sizeof(box));
1902 variable_substitute(box, dst, sizeof(dst));
1903 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1904 obj->data.local_mail.interval = n1;
1905 END OBJ(flagged_mails, 0)
1907 char box[256], dst[256];
1911 strncpy(box, current_mail_spool, sizeof(box));
1913 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1915 strncpy(box, arg, sizeof(box));
1919 variable_substitute(box, dst, sizeof(dst));
1920 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1921 obj->data.local_mail.interval = n1;
1922 END OBJ(unflagged_mails, 0)
1924 char box[256], dst[256];
1928 strncpy(box, current_mail_spool, sizeof(box));
1930 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1932 strncpy(box, arg, sizeof(box));
1936 variable_substitute(box, dst, sizeof(dst));
1937 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1938 obj->data.local_mail.interval = n1;
1939 END OBJ(forwarded_mails, 0)
1941 char box[256], dst[256];
1945 strncpy(box, current_mail_spool, sizeof(box));
1947 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1949 strncpy(box, arg, sizeof(box));
1953 variable_substitute(box, dst, sizeof(dst));
1954 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1955 obj->data.local_mail.interval = n1;
1956 END OBJ(unforwarded_mails, 0)
1958 char box[256], dst[256];
1962 strncpy(box, current_mail_spool, sizeof(box));
1964 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1966 strncpy(box, arg, sizeof(box));
1970 variable_substitute(box, dst, sizeof(dst));
1971 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1972 obj->data.local_mail.interval = n1;
1973 END OBJ(replied_mails, 0)
1975 char box[256], dst[256];
1979 strncpy(box, current_mail_spool, sizeof(box));
1981 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1983 strncpy(box, arg, sizeof(box));
1987 variable_substitute(box, dst, sizeof(dst));
1988 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1989 obj->data.local_mail.interval = n1;
1990 END OBJ(unreplied_mails, 0)
1992 char box[256], dst[256];
1996 strncpy(box, current_mail_spool, sizeof(box));
1998 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2000 strncpy(box, arg, sizeof(box));
2004 variable_substitute(box, dst, sizeof(dst));
2005 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2006 obj->data.local_mail.interval = n1;
2007 END OBJ(draft_mails, 0)
2009 char box[256], dst[256];
2013 strncpy(box, current_mail_spool, sizeof(box));
2015 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2017 strncpy(box, arg, sizeof(box));
2021 variable_substitute(box, dst, sizeof(dst));
2022 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2023 obj->data.local_mail.interval = n1;
2024 END OBJ(trashed_mails, 0)
2026 char box[256], dst[256];
2030 strncpy(box, current_mail_spool, sizeof(box));
2032 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2034 strncpy(box, arg, sizeof(box));
2038 variable_substitute(box, dst, sizeof(dst));
2039 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2040 obj->data.local_mail.interval = n1;
2041 END OBJ(mboxscan, 0)
2042 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2043 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2044 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2045 obj->data.mboxscan.output[0] = 1;
2046 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2047 END OBJ(mem, INFO_MEM)
2048 END OBJ(memeasyfree, INFO_MEM)
2049 END OBJ(memfree, INFO_MEM)
2050 END OBJ(memmax, INFO_MEM)
2051 END OBJ(memperc, INFO_MEM)
2052 END OBJ(memgauge, INFO_MEM)
2053 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2054 END OBJ(membar, INFO_MEM)
2055 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2056 END OBJ(memgraph, INFO_MEM)
2057 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2058 &obj->e, &obj->showaslog);
2063 END OBJ(mixer, INFO_MIXER)
2064 obj->data.l = mixer_init(arg);
2065 END OBJ(mixerl, INFO_MIXER)
2066 obj->data.l = mixer_init(arg);
2067 END OBJ(mixerr, INFO_MIXER)
2068 obj->data.l = mixer_init(arg);
2069 END OBJ(mixerbar, INFO_MIXER)
2070 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2071 &obj->data.mixerbar.h);
2072 END OBJ(mixerlbar, INFO_MIXER)
2073 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2074 &obj->data.mixerbar.h);
2075 END OBJ(mixerrbar, INFO_MIXER)
2076 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2077 &obj->data.mixerbar.h);
2079 END OBJ(monitor, INFO_X11)
2080 END OBJ(monitor_number, INFO_X11)
2082 END OBJ(nodename, 0)
2083 END OBJ(processes, INFO_PROCS)
2084 END OBJ(running_processes, INFO_RUN_PROCS)
2085 END OBJ(shadecolor, 0)
2087 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2089 END OBJ(outlinecolor, 0)
2091 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2093 END OBJ(stippled_hr, 0)
2095 int a = stippled_borders, b = 1;
2098 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2099 sscanf(arg, "%d", &b);
2105 obj->data.pair.a = a;
2106 obj->data.pair.b = b;
2108 END OBJ(swap, INFO_MEM)
2109 END OBJ(swapmax, INFO_MEM)
2110 END OBJ(swapperc, INFO_MEM)
2111 END OBJ(swapbar, INFO_MEM)
2112 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2115 END OBJ(temp1, INFO_SYSFS)
2116 obj->type = OBJ_i2c;
2117 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 1,
2118 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2119 END OBJ(temp2, INFO_SYSFS)
2120 obj->type = OBJ_i2c;
2121 obj->data.sysfs.fd = open_i2c_sensor(0, "temp", 2,
2122 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2125 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2127 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2129 char buf1[256], buf2[256], *fmt, *tz;
2133 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2143 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2144 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2146 END OBJ(iconv_start, 0)
2147 if (iconv_converting) {
2148 CRIT_ERR("You must stop your last iconv conversion before "
2149 "starting another");
2152 char iconv_from[CODEPAGE_LENGTH];
2153 char iconv_to[CODEPAGE_LENGTH];
2155 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2156 CRIT_ERR("Invalid arguments for iconv_start");
2160 new_iconv = iconv_open(iconv_to, iconv_from);
2161 if (new_iconv == (iconv_t) (-1)) {
2162 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2164 obj->a = register_iconv(&new_iconv);
2165 iconv_converting = 1;
2169 CRIT_ERR("Iconv requires arguments");
2171 END OBJ(iconv_stop, 0)
2172 iconv_converting = 0;
2175 END OBJ(totaldown, INFO_NET)
2177 obj->data.net = get_net_stat(arg);
2179 CRIT_ERR("totaldown needs argument");
2181 END OBJ(totalup, INFO_NET)
2182 obj->data.net = get_net_stat(arg);
2184 obj->data.net = get_net_stat(arg);
2186 CRIT_ERR("totalup needs argument");
2189 END OBJ_IF(if_updatenr, 0)
2190 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2191 if(obj->data.ifblock.i == 0) CRIT_ERR("if_updatenr needs a number above 0 as argument");
2192 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2194 obj->data.i = arg ? atoi(arg) : 0;
2196 obj->data.i = arg ? atoi(arg) : 0;
2197 END OBJ(upspeed, INFO_NET)
2199 obj->data.net = get_net_stat(arg);
2201 CRIT_ERR("upspeed needs argument");
2203 END OBJ(upspeedf, INFO_NET)
2205 obj->data.net = get_net_stat(arg);
2207 CRIT_ERR("upspeedf needs argument");
2210 END OBJ(upspeedgraph, INFO_NET)
2211 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2212 &obj->e, &obj->showaslog);
2214 // default to DEFAULTNETDEV
2215 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
2216 obj->data.net = get_net_stat(buf);
2218 END OBJ(uptime_short, INFO_UPTIME)
2219 END OBJ(uptime, INFO_UPTIME)
2220 END OBJ(user_names, INFO_USERS)
2221 END OBJ(user_times, INFO_USERS)
2222 END OBJ(user_terms, INFO_USERS)
2223 END OBJ(user_number, INFO_USERS)
2224 #if defined(__linux__)
2225 END OBJ(gw_iface, INFO_GW)
2226 END OBJ(gw_ip, INFO_GW)
2227 END OBJ(if_gw, INFO_GW)
2228 #endif /* !__linux__ */
2230 END OBJ(adt746xcpu, 0)
2231 END OBJ(adt746xfan, 0)
2232 #endif /* !__OpenBSD__ */
2233 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2234 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2235 END OBJ(apm_adapter, 0)
2236 END OBJ(apm_battery_life, 0)
2237 END OBJ(apm_battery_time, 0)
2238 #endif /* __FreeBSD__ */
2239 END OBJ_THREAD(imap_unseen, 0)
2242 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2243 obj->global_mode = 0;
2245 obj->global_mode = 1;
2247 END OBJ_THREAD(imap_messages, 0)
2250 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2251 obj->global_mode = 0;
2253 obj->global_mode = 1;
2255 END OBJ_THREAD(pop3_unseen, 0)
2258 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2259 obj->global_mode = 0;
2261 obj->global_mode = 1;
2263 END OBJ_THREAD(pop3_used, 0)
2266 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2267 obj->global_mode = 0;
2269 obj->global_mode = 1;
2274 obj->data.s = strndup(arg, text_buffer_size);
2276 ERR("smapi needs an argument");
2277 END OBJ_IF(if_smapi_bat_installed, 0)
2279 ERR("if_smapi_bat_installed needs an argument");
2280 obj->data.ifblock.s = 0;
2282 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2283 END OBJ(smapi_bat_perc, 0)
2285 obj->data.s = strndup(arg, text_buffer_size);
2287 ERR("smapi_bat_perc needs an argument");
2288 END OBJ(smapi_bat_temp, 0)
2290 obj->data.s = strndup(arg, text_buffer_size);
2292 ERR("smapi_bat_temp needs an argument");
2293 END OBJ(smapi_bat_power, 0)
2295 obj->data.s = strndup(arg, text_buffer_size);
2297 ERR("smapi_bat_power needs an argument");
2298 END OBJ(smapi_bat_bar, 0)
2301 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2302 ERR("first argument to smapi_bat_bar must be an integer value");
2306 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2309 ERR("smapi_bat_bar needs an argument");
2312 #define mpd_set_maxlen(name) \
2315 sscanf(arg, "%d", &i); \
2317 obj->data.i = i + 1; \
2319 ERR(#name ": invalid length argument"); \
2321 END OBJ(mpd_artist, INFO_MPD)
2322 mpd_set_maxlen(mpd_artist);
2324 END OBJ(mpd_title, INFO_MPD)
2325 mpd_set_maxlen(mpd_title);
2327 END OBJ(mpd_random, INFO_MPD) init_mpd();
2328 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2329 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2330 END OBJ(mpd_length, INFO_MPD) init_mpd();
2331 END OBJ(mpd_track, INFO_MPD)
2332 mpd_set_maxlen(mpd_track);
2334 END OBJ(mpd_name, INFO_MPD)
2335 mpd_set_maxlen(mpd_name);
2337 END OBJ(mpd_file, INFO_MPD)
2338 mpd_set_maxlen(mpd_file);
2340 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2341 END OBJ(mpd_album, INFO_MPD)
2342 mpd_set_maxlen(mpd_album);
2344 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2345 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2346 END OBJ(mpd_status, INFO_MPD) init_mpd();
2347 END OBJ(mpd_bar, INFO_MPD)
2348 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2350 END OBJ(mpd_smart, INFO_MPD)
2351 mpd_set_maxlen(mpd_smart);
2353 END OBJ_IF(if_mpd_playing, INFO_MPD)
2355 #undef mpd_set_maxlen
2358 END OBJ(moc_state, INFO_MOC)
2359 END OBJ(moc_file, INFO_MOC)
2360 END OBJ(moc_title, INFO_MOC)
2361 END OBJ(moc_artist, INFO_MOC)
2362 END OBJ(moc_song, INFO_MOC)
2363 END OBJ(moc_album, INFO_MOC)
2364 END OBJ(moc_totaltime, INFO_MOC)
2365 END OBJ(moc_timeleft, INFO_MOC)
2366 END OBJ(moc_curtime, INFO_MOC)
2367 END OBJ(moc_bitrate, INFO_MOC)
2368 END OBJ(moc_rate, INFO_MOC)
2371 END OBJ(xmms2_artist, INFO_XMMS2)
2372 END OBJ(xmms2_album, INFO_XMMS2)
2373 END OBJ(xmms2_title, INFO_XMMS2)
2374 END OBJ(xmms2_genre, INFO_XMMS2)
2375 END OBJ(xmms2_comment, INFO_XMMS2)
2376 END OBJ(xmms2_url, INFO_XMMS2)
2377 END OBJ(xmms2_tracknr, INFO_XMMS2)
2378 END OBJ(xmms2_bitrate, INFO_XMMS2)
2379 END OBJ(xmms2_date, INFO_XMMS2)
2380 END OBJ(xmms2_id, INFO_XMMS2)
2381 END OBJ(xmms2_duration, INFO_XMMS2)
2382 END OBJ(xmms2_elapsed, INFO_XMMS2)
2383 END OBJ(xmms2_size, INFO_XMMS2)
2384 END OBJ(xmms2_status, INFO_XMMS2)
2385 END OBJ(xmms2_percent, INFO_XMMS2)
2386 END OBJ(xmms2_bar, INFO_XMMS2)
2387 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2388 END OBJ(xmms2_smart, INFO_XMMS2)
2389 END OBJ(xmms2_playlist, INFO_XMMS2)
2390 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2391 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2394 END OBJ(audacious_status, INFO_AUDACIOUS)
2395 END OBJ(audacious_title, INFO_AUDACIOUS)
2397 sscanf(arg, "%d", &info.audacious.max_title_len);
2398 if (info.audacious.max_title_len > 0) {
2399 info.audacious.max_title_len++;
2401 CRIT_ERR("audacious_title: invalid length argument");
2404 END OBJ(audacious_length, INFO_AUDACIOUS)
2405 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2406 END OBJ(audacious_position, INFO_AUDACIOUS)
2407 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2408 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2409 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2410 END OBJ(audacious_channels, INFO_AUDACIOUS)
2411 END OBJ(audacious_filename, INFO_AUDACIOUS)
2412 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2413 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2414 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2415 END OBJ(audacious_bar, INFO_AUDACIOUS)
2416 scan_bar(arg, &obj->a, &obj->b);
2419 END OBJ(bmpx_title, INFO_BMPX)
2420 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2421 END OBJ(bmpx_artist, INFO_BMPX)
2422 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2423 END OBJ(bmpx_album, INFO_BMPX)
2424 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2425 END OBJ(bmpx_track, INFO_BMPX)
2426 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2427 END OBJ(bmpx_uri, INFO_BMPX)
2428 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2429 END OBJ(bmpx_bitrate, INFO_BMPX)
2430 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2436 char *userid = (char *) malloc(20 * sizeof(char));
2437 char *apikey = (char *) malloc(64 * sizeof(char));
2438 char *charid = (char *) malloc(20 * sizeof(char));
2440 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2441 obj->data.eve.charid = charid;
2442 obj->data.eve.userid = userid;
2443 obj->data.eve.apikey = apikey;
2447 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
2453 int argc, delay, act_par;
2454 char *uri = (char *) malloc(128 * sizeof(char));
2455 char *action = (char *) malloc(64 * sizeof(char));
2457 argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action,
2459 obj->data.rss.uri = uri;
2460 obj->data.rss.delay = delay;
2461 obj->data.rss.action = action;
2462 obj->data.rss.act_par = act_par;
2466 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
2472 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2473 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2474 ERR("hddtemp needs arguments");
2475 obj->type = OBJ_text;
2476 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2477 obj->data.hddtemp.update_time = 0;
2479 obj->data.hddtemp.temp = NULL;
2481 #ifdef TCP_PORT_MONITOR
2482 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2483 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2485 END OBJ(entropy_avail, INFO_ENTROPY)
2486 END OBJ(entropy_poolsize, INFO_ENTROPY)
2487 END OBJ(entropy_bar, INFO_ENTROPY)
2488 scan_bar(arg, &obj->a, &obj->b);
2492 obj->data.scroll.step = 1;
2493 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2494 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
2496 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2497 obj->data.scroll.start = 0;
2498 obj->sub = malloc(sizeof(struct text_object));
2499 extract_variable_text_internal(obj->sub,
2500 obj->data.scroll.text, 0);
2502 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
2507 CRIT_ERR("nvidia needs an argument\n");
2508 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
2509 CRIT_ERR("nvidia: invalid argument"
2510 " specified: '%s'\n", arg);
2516 ERR("unknown variable %s", s);
2517 obj->type = OBJ_text;
2518 snprintf(buf, 256, "${%s}", s);
2519 obj->data.s = strndup(buf, text_buffer_size);
2526 static struct text_object *create_plain_text(const char *s)
2528 struct text_object *obj;
2530 if (s == NULL || *s == '\0') {
2534 obj = new_text_object_internal();
2536 obj->type = OBJ_text;
2537 obj->data.s = strndup(s, text_buffer_size);
2541 /* backslash_escape - do the actual substitution task for template objects
2543 * The field templates is used for substituting the \N occurences. Set it to
2544 * NULL to leave them as they are.
2546 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
2550 unsigned int dup_idx = 0, dup_len;
2552 dup_len = strlen(src) + 1;
2553 src_dup = malloc(dup_len * sizeof(char));
2561 if (*(p + 1) == '\\') {
2562 src_dup[dup_idx++] = '\\';
2564 } else if (*(p + 1) == ' ') {
2565 src_dup[dup_idx++] = ' ';
2567 } else if (*(p + 1) == 'n') {
2568 src_dup[dup_idx++] = '\n';
2570 } else if (templates) {
2571 unsigned int tmpl_num;
2573 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
2574 (tmpl_num > template_count))
2576 dup_len += strlen(templates[tmpl_num - 1]);
2577 src_dup = realloc(src_dup, dup_len * sizeof(char));
2578 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
2579 dup_idx += strlen(templates[tmpl_num - 1]);
2584 src_dup[dup_idx++] = *p;
2589 src_dup[dup_idx] = '\0';
2590 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
2594 /* handle_template_object - core logic of the template object
2596 * use config variables like this:
2597 * template1 = "$\1\2"
2598 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
2600 * and use them like this:
2601 * ${template1 node name}
2602 * ${template2 root /}
2603 * ${template2 cdrom /mnt/cdrom}
2605 static char *handle_template(const char *tmpl, const char *args)
2607 char *args_dup, *p, *p_old;
2608 char **argsp = NULL;
2609 unsigned int argcnt = 0, template_idx, i;
2612 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
2616 args_dup = strdup(args);
2619 while (*p && (*p == ' ' && *(p - 1) != '\\'))
2621 if (*(p - 1) == '\\')
2624 while (*p && (*p != ' ' || *(p - 1) == '\\'))
2630 argsp = realloc(argsp, ++argcnt * sizeof(char *));
2631 argsp[argcnt - 1] = p_old;
2633 for (i = 0; i < argcnt; i++) {
2635 tmp = backslash_escape(argsp[i], NULL, 0);
2636 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
2640 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
2641 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
2643 for (i = 0; i < argcnt; i++)
2649 static char *find_and_replace_templates(const char *inbuf)
2651 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
2654 outlen = strlen(inbuf) + 1;
2655 o = outbuf = calloc(outlen, sizeof(char));
2656 memset(outbuf, 0, outlen * sizeof(char));
2658 p = indup = strdup(inbuf);
2660 while (*p && *p != '$')
2666 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
2671 if (*(p + 1) == '{') {
2693 tmpl_out = handle_template(templ, args);
2695 outlen += strlen(tmpl_out);
2696 outbuf = realloc(outbuf, outlen * sizeof(char));
2697 strcat (outbuf, tmpl_out);
2699 o = outbuf + strlen(outbuf);
2701 ERR("failed to handle template '%s' with args '%s'", templ, args);
2705 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
2710 static int text_contains_templates(const char *text)
2712 if (strcasestr(text, "${template") != NULL)
2714 if (strcasestr(text, "$template") != NULL)
2719 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
2721 struct text_object *obj;
2722 char *p, *s, *orig_p;
2724 void *ifblock_opaque = NULL;
2728 p = strndup(const_p, max_user_text - 1);
2729 while (text_contains_templates(p)) {
2731 tmp = find_and_replace_templates(p);
2737 if (strcmp(p, const_p)) {
2738 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
2740 DBGP("no templates to replace");
2743 memset(retval, 0, sizeof(struct text_object));
2745 line = global_text_lines;
2753 obj = create_plain_text(s);
2755 append_object(retval, obj);
2766 /* variable is either $foo or ${foo} */
2768 unsigned int brl = 1, brr = 0;
2772 while (*p && brl != brr) {
2787 while (*p && (isalnum((int) *p) || *p == '_')) {
2792 /* copy variable to buffer */
2793 len = (p - s > 255) ? 255 : (p - s);
2794 strncpy(buf, s, len);
2804 /* if variable wasn't found in environment, use some special */
2806 strncpy(buf, var, 255);
2811 if (strchr(buf, ' ')) {
2812 arg = strchr(buf, ' ');
2815 while (isspace((int) *arg)) {
2823 /* lowercase variable name */
2826 *tmp_p = tolower(*tmp_p);
2830 obj = construct_text_object(buf, arg,
2831 line, allow_threaded,
2834 append_object(retval, obj);
2838 obj = create_plain_text("$");
2841 append_object(retval, obj);
2847 obj = create_plain_text(s);
2849 append_object(retval, obj);
2852 if (!ifblock_stack_empty(&ifblock_opaque)) {
2853 ERR("one or more $endif's are missing");
2860 static void extract_variable_text(const char *p)
2862 free_text_objects(&global_root_object);
2876 extract_variable_text_internal(&global_root_object, p, 1);
2879 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
2881 extract_variable_text_internal(root, txt, 0);
2882 generate_text_internal(p, max_user_text, *root, cur);
2886 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
2887 void *thread(void *), const char *text)
2889 if (obj->global_mode && info.mail) {
2890 // this means we use info
2891 if (!info.mail->p_timed_thread) {
2892 info.mail->p_timed_thread =
2893 timed_thread_create(thread,
2894 (void *) info.mail, info.mail->interval * 1000000);
2895 if (!info.mail->p_timed_thread) {
2896 ERR("Error creating %s timed thread", text);
2898 timed_thread_register(info.mail->p_timed_thread,
2899 &info.mail->p_timed_thread);
2900 if (timed_thread_run(info.mail->p_timed_thread)) {
2901 ERR("Error running %s timed thread", text);
2905 } else if (obj->data.mail) {
2906 // this means we use obj
2907 if (!obj->data.mail->p_timed_thread) {
2908 obj->data.mail->p_timed_thread =
2909 timed_thread_create(thread,
2910 (void *) obj->data.mail,
2911 obj->data.mail->interval * 1000000);
2912 if (!obj->data.mail->p_timed_thread) {
2913 ERR("Error creating %s timed thread", text);
2915 timed_thread_register(obj->data.mail->p_timed_thread,
2916 &obj->data.mail->p_timed_thread);
2917 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
2918 ERR("Error running %s timed thread", text);
2921 return obj->data.mail;
2922 } else if (!obj->a) {
2923 // something is wrong, warn once then stop
2924 ERR("Theres a problem with your %s_unseen settings. "
2925 "Check that the global %s settings are defined "
2926 "properly (line %li).", global_text, global_text, obj->line);
2932 char *format_time(unsigned long timeval, const int width)
2935 unsigned long nt; // narrow time, for speed on 32-bit
2936 unsigned cc; // centiseconds
2937 unsigned nn; // multi-purpose whatever
2940 cc = nt % 100; // centiseconds past second
2941 nt /= 100; // total seconds
2942 nn = nt % 60; // seconds past the minute
2943 nt /= 60; // total minutes
2944 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
2946 return strndup(buf, text_buffer_size);
2948 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
2949 return strndup(buf, text_buffer_size);
2951 nn = nt % 60; // minutes past the hour
2952 nt /= 60; // total hours
2953 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
2954 return strndup(buf, text_buffer_size);
2956 nn = nt; // now also hours
2957 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
2958 return strndup(buf, text_buffer_size);
2960 nn /= 24; // now days
2961 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
2962 return strndup(buf, text_buffer_size);
2964 nn /= 7; // now weeks
2965 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
2966 return strndup(buf, text_buffer_size);
2968 // well shoot, this outta' fit...
2969 return strndup("<inf>", text_buffer_size);
2972 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
2973 //string has to end with \0 and it's length should fit in a int
2975 void remove_deleted_chars(char *string){
2977 while(string[i] != 0){
2978 if(string[i] == BACKSPACE){
2980 strcpy( &(string[i-1]), &(string[i+1]) );
2982 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
2987 static inline void format_media_player_time(char *buf, const int size,
2990 int days, hours, minutes;
2992 days = seconds / (24 * 60 * 60);
2993 seconds %= (24 * 60 * 60);
2994 hours = seconds / (60 * 60);
2995 seconds %= (60 * 60);
2996 minutes = seconds / 60;
3000 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3001 hours, minutes, seconds);
3002 } else if (hours > 0) {
3003 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3006 snprintf(buf, size, "%i:%02i", minutes, seconds);
3010 static inline double get_barnum(char *buf)
3022 if (sscanf(buf, "%lf", &barnum) == 0) {
3023 ERR("reading execbar value failed (perhaps it's not the "
3024 "correct format?)");
3027 if (barnum > 100.0 || barnum < 0.0) {
3028 ERR("your execbar value is not between 0 and 100, "
3029 "therefore it will be ignored");
3035 static void generate_text_internal(char *p, int p_max_size,
3036 struct text_object root, struct information *cur)
3038 struct text_object *obj;
3040 /* for the OBJ_top* handler */
3041 struct process **needed = 0;
3044 char buff_in[p_max_size];
3046 iconv_converting = 0;
3050 for (obj = root.next; obj && p_max_size > 0; obj = obj->next) {
3051 needed = 0; // reset for top stuff
3053 /* IFBLOCK jumping algorithm
3055 * This is easier as it looks like:
3056 * - each IF checks it's condition
3057 * - on FALSE: call DO_JUMP
3058 * - on TRUE: don't care
3059 * - each ELSE calls DO_JUMP unconditionally
3060 * - each ENDIF is silently being ignored
3063 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3064 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3065 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3066 * regularly, "obj" is being updated to point to obj->next, so object parsing
3067 * continues right after the corresponding ELSE or ENDIF. This means that if we
3068 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3069 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3070 * jumped, and there is nothing to do.
3074 obj = obj->data.ifblock.next; \
3077 #define OBJ(a) break; case OBJ_##a:
3079 switch (obj->type) {
3081 ERR("not implemented obj type %d", obj->type);
3084 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3086 #endif /* !__OpenBSD__ */
3089 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3090 obj->data.cpu_index);
3096 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3097 obj->data.cpu_index);
3099 /* OpenBSD has no such flag (SUSv2) */
3100 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3101 obj->data.cpu_index);
3105 #if defined(__linux__)
3108 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3109 obj->data.cpu_index);
3114 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3115 obj->data.cpu_index);
3120 OBJ(wireless_essid) {
3121 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3123 OBJ(wireless_mode) {
3124 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3126 OBJ(wireless_bitrate) {
3127 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3130 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3132 OBJ(wireless_link_qual) {
3133 spaced_print(p, p_max_size, "%d", 4,
3134 obj->data.net->link_qual);
3136 OBJ(wireless_link_qual_max) {
3137 spaced_print(p, p_max_size, "%d", 4,
3138 obj->data.net->link_qual_max);
3140 OBJ(wireless_link_qual_perc) {
3141 if (obj->data.net->link_qual_max > 0) {
3142 spaced_print(p, p_max_size, "%.0f", 5,
3143 (double) obj->data.net->link_qual /
3144 obj->data.net->link_qual_max * 100);
3146 spaced_print(p, p_max_size, "unk", 5);
3149 OBJ(wireless_link_bar) {
3150 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3151 obj->data.net->link_qual_max) * 255.0);
3153 #endif /* HAVE_IWLIB */
3155 #endif /* __linux__ */
3159 get_adt746x_cpu(p, p_max_size);
3162 get_adt746x_fan(p, p_max_size);
3165 get_acpi_fan(p, p_max_size);
3167 OBJ(acpiacadapter) {
3168 get_acpi_ac_adapter(p, p_max_size);
3171 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3174 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3176 OBJ(battery_percent) {
3177 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3180 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3182 OBJ(battery_short) {
3183 get_battery_short_status(p, p_max_size, obj->data.s);
3185 #endif /* __OpenBSD__ */
3188 human_readable(cur->buffers * 1024, p, 255);
3191 human_readable(cur->cached * 1024, p, 255);
3194 if (obj->data.cpu_index > info.cpu_count) {
3195 printf("obj->data.cpu_index %i info.cpu_count %i",
3196 obj->data.cpu_index, info.cpu_count);
3197 CRIT_ERR("attempting to use more CPUs than you have!");
3199 percent_print(p, p_max_size,
3200 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3203 new_gauge(p, obj->a, obj->b,
3204 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3206 new_bar(p, obj->a, obj->b,
3207 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3210 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
3211 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3212 100, 1, obj->showaslog);
3215 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3216 obj->e, 1, obj->showaslog);
3219 new_fg(p, obj->data.l);
3251 OBJ(conky_version) {
3252 snprintf(p, p_max_size, "%s", VERSION);
3254 OBJ(conky_build_date) {
3255 snprintf(p, p_max_size, "%s", BUILD_DATE);
3257 OBJ(conky_build_arch) {
3258 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3260 #if defined(__linux__)
3262 snprintf(p, p_max_size, "%s",
3263 get_disk_protect_queue(obj->data.s));
3266 snprintf(p, p_max_size, "%s", i8k.version);
3269 snprintf(p, p_max_size, "%s", i8k.bios);
3272 snprintf(p, p_max_size, "%s", i8k.serial);
3277 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3278 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3280 OBJ(i8k_left_fan_status) {
3281 int left_fan_status;
3283 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3284 if (left_fan_status == 0) {
3285 snprintf(p, p_max_size, "off");
3287 if (left_fan_status == 1) {
3288 snprintf(p, p_max_size, "low");
3290 if (left_fan_status == 2) {
3291 snprintf(p, p_max_size, "high");
3294 OBJ(i8k_right_fan_status) {
3295 int right_fan_status;
3297 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3298 if (right_fan_status == 0) {
3299 snprintf(p, p_max_size, "off");
3301 if (right_fan_status == 1) {
3302 snprintf(p, p_max_size, "low");
3304 if (right_fan_status == 2) {
3305 snprintf(p, p_max_size, "high");
3308 OBJ(i8k_left_fan_rpm) {
3309 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3311 OBJ(i8k_right_fan_rpm) {
3312 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3314 OBJ(i8k_ac_status) {
3317 sscanf(i8k.ac_status, "%d", &ac_status);
3318 if (ac_status == -1) {
3319 snprintf(p, p_max_size, "disabled (read i8k docs)");
3321 if (ac_status == 0) {
3322 snprintf(p, p_max_size, "off");
3324 if (ac_status == 1) {
3325 snprintf(p, p_max_size, "on");
3328 OBJ(i8k_buttons_status) {
3329 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3333 get_ibm_acpi_fan(p, p_max_size);
3336 get_ibm_acpi_temps();
3337 temp_print(p, p_max_size,
3338 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
3341 get_ibm_acpi_volume(p, p_max_size);
3343 OBJ(ibm_brightness) {
3344 get_ibm_acpi_brightness(p, p_max_size);
3348 if (!cur->gw_info.count) {
3353 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
3356 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
3359 snprintf(p, p_max_size, "%d", get_laptop_mode());
3362 get_powerbook_batt_info(p, p_max_size, obj->data.i);
3364 #endif /* __linux__ */
3365 #if (defined(__FreeBSD__) || defined(__linux__))
3367 if ((obj->data.ifblock.s)
3368 && (!interface_up(obj->data.ifblock.s))) {
3374 OBJ(obsd_sensors_temp) {
3375 obsd_sensors.device = sensor_device;
3376 update_obsd_sensors();
3377 temp_print(p, p_max_size,
3378 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
3381 OBJ(obsd_sensors_fan) {
3382 obsd_sensors.device = sensor_device;
3383 update_obsd_sensors();
3384 snprintf(p, p_max_size, "%d",
3385 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
3387 OBJ(obsd_sensors_volt) {
3388 obsd_sensors.device = sensor_device;
3389 update_obsd_sensors();
3390 snprintf(p, p_max_size, "%.2f",
3391 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
3394 get_obsd_vendor(p, p_max_size);
3397 get_obsd_product(p, p_max_size);
3399 #endif /* __OpenBSD__ */
3402 new_font(p, obj->data.s);
3404 /* TODO: move this correction from kB to kB/s elsewhere
3405 * (or get rid of it??) */
3407 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
3411 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
3415 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
3419 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3420 obj->data.diskio->current, obj->e, 1, obj->showaslog);
3422 OBJ(diskiograph_read) {
3423 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3424 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
3426 OBJ(diskiograph_write) {
3427 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3428 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
3431 human_readable(obj->data.net->recv_speed, p, 255);
3434 spaced_print(p, p_max_size, "%.1f", 8,
3435 obj->data.net->recv_speed / 1024.0);
3437 OBJ(downspeedgraph) {
3438 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3439 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
3442 /* Since we see you, you're if has not jumped.
3443 * Do Ninja jump here: without leaving traces.
3444 * This is to prevent us from stale jumped flags.
3446 obj = obj->data.ifblock.next;
3450 /* harmless object, just ignore */
3454 if ((obj->data.net->addr.sa_data[2] & 255) == 0
3455 && (obj->data.net->addr.sa_data[3] & 255) == 0
3456 && (obj->data.net->addr.sa_data[4] & 255) == 0
3457 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
3458 snprintf(p, p_max_size, "No Address");
3460 snprintf(p, p_max_size, "%u.%u.%u.%u",
3461 obj->data.net->addr.sa_data[2] & 255,
3462 obj->data.net->addr.sa_data[3] & 255,
3463 obj->data.net->addr.sa_data[4] & 255,
3464 obj->data.net->addr.sa_data[5] & 255);
3467 #if defined(__linux__)
3469 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
3471 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
3472 strcpy(p, obj->data.net->addrs);
3475 strcpy(p, "0.0.0.0");
3477 #endif /* __linux__ */
3479 #if defined(IMLIB2) && defined(X11)
3484 Imlib_Image image, buffer;
3486 image = imlib_load_image(obj->data.s);
3487 imlib_context_set_image(image);
3491 w = imlib_image_get_width();
3492 h = imlib_image_get_height();
3493 buffer = imlib_create_image(w, h);
3494 imlib_context_set_display(display);
3495 imlib_context_set_drawable(window.drawable);
3496 imlib_context_set_colormap(DefaultColormap(display,
3498 imlib_context_set_visual(DefaultVisual(display,
3500 imlib_context_set_image(buffer);
3501 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
3502 text_start_x, text_start_y, w, h);
3503 imlib_render_image_on_drawable(text_start_x,
3506 imlib_context_set_image(image);
3514 struct information *tmp_info;
3515 struct text_object subroot, subroot2;
3517 tmp_info = malloc(sizeof(struct information));
3518 memcpy(tmp_info, cur, sizeof(struct information));
3519 parse_conky_vars(&subroot, obj->data.s, p, tmp_info);
3520 DBGP("evaluated '%s' to '%s'", obj->data.s, p);
3521 parse_conky_vars(&subroot2, p, p, tmp_info);
3523 free_text_objects(&subroot);
3524 free_text_objects(&subroot2);
3528 read_exec(obj->data.s, p, text_buffer_size);
3529 remove_deleted_chars(p);
3532 struct information *tmp_info;
3533 struct text_object subroot;
3535 read_exec(obj->data.s, p, text_buffer_size);
3537 tmp_info = malloc(sizeof(struct information));
3538 memcpy(tmp_info, cur, sizeof(struct information));
3539 parse_conky_vars(&subroot, p, p, tmp_info);
3541 free_text_objects(&subroot);
3547 read_exec(obj->data.s, p, text_buffer_size);
3548 barnum = get_barnum(p); /*using the same function*/
3550 if (barnum >= 0.0) {
3552 new_bar(p, 0, 6, round_to_int(barnum * 255.0));
3558 read_exec(obj->data.s, p, text_buffer_size);
3559 barnum = get_barnum(p);
3561 if (barnum >= 0.0) {
3563 new_bar(p, 0, 6, round_to_int(barnum * 255.0));
3567 char showaslog = FALSE;
3570 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
3572 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, text_buffer_size);
3573 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
3574 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, text_buffer_size);
3576 read_exec(obj->data.s, p, text_buffer_size);
3578 barnum = get_barnum(p);
3580 if (barnum >= 0.0) {
3581 new_graph(p, 0, 25, obj->c, obj->d, round_to_int(barnum),
3586 if (current_update_time - obj->data.execi.last_update
3587 >= obj->data.execi.interval) {
3590 read_exec(obj->data.execi.cmd, p, text_buffer_size);
3591 barnum = get_barnum(p);
3593 if (barnum >= 0.0) {
3594 obj->f = 255 * barnum / 100.0;
3596 obj->data.execi.last_update = current_update_time;
3598 new_bar(p, 0, 6, round_to_int(obj->f));
3601 if (current_update_time - obj->data.execi.last_update
3602 >= obj->data.execi.interval) {
3605 read_exec(obj->data.execi.cmd, p, text_buffer_size);
3606 barnum = get_barnum(p);
3608 if (barnum >= 0.0) {
3611 obj->data.execi.last_update = current_update_time;
3613 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
3616 if (current_update_time - obj->data.execi.last_update
3617 >= obj->data.execi.interval
3618 && obj->data.execi.interval != 0) {
3619 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
3621 obj->data.execi.last_update = current_update_time;
3623 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
3626 struct text_object subroot;
3627 struct information *tmp_info =
3628 malloc(sizeof(struct information));
3629 memcpy(tmp_info, cur, sizeof(struct information));
3631 if (current_update_time - obj->data.execi.last_update
3632 < obj->data.execi.interval
3633 || obj->data.execi.interval == 0) {
3634 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
3636 char *output = obj->data.execi.buffer;
3637 FILE *fp = popen(obj->data.execi.cmd, "r");
3638 int length = fread(output, 1, text_buffer_size, fp);
3642 output[length] = '\0';
3643 if (length > 0 && output[length - 1] == '\n') {
3644 output[length - 1] = '\0';
3647 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
3648 obj->data.execi.last_update = current_update_time;
3650 free_text_objects(&subroot);
3654 if (!obj->data.texeci.p_timed_thread) {
3655 obj->data.texeci.p_timed_thread =
3656 timed_thread_create(&threaded_exec,
3657 (void *) obj, obj->data.texeci.interval * 1000000);
3658 if (!obj->data.texeci.p_timed_thread) {
3659 ERR("Error creating texeci timed thread");
3661 timed_thread_register(obj->data.texeci.p_timed_thread,
3662 &obj->data.texeci.p_timed_thread);
3663 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
3664 ERR("Error running texeci timed thread");
3667 timed_thread_lock(obj->data.texeci.p_timed_thread);
3668 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
3669 timed_thread_unlock(obj->data.texeci.p_timed_thread);
3671 #endif /* HAVE_POPEN */
3673 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
3675 if (mail && mail->p_timed_thread) {
3676 timed_thread_lock(mail->p_timed_thread);
3677 snprintf(p, p_max_size, "%lu", mail->unseen);
3678 timed_thread_unlock(mail->p_timed_thread);
3681 OBJ(imap_messages) {
3682 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
3684 if (mail && mail->p_timed_thread) {
3685 timed_thread_lock(mail->p_timed_thread);
3686 snprintf(p, p_max_size, "%lu", mail->messages);
3687 timed_thread_unlock(mail->p_timed_thread);
3691 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
3693 if (mail && mail->p_timed_thread) {
3694 timed_thread_lock(mail->p_timed_thread);
3695 snprintf(p, p_max_size, "%lu", mail->unseen);
3696 timed_thread_unlock(mail->p_timed_thread);
3700 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
3702 if (mail && mail->p_timed_thread) {
3703 timed_thread_lock(mail->p_timed_thread);
3704 snprintf(p, p_max_size, "%.1f",
3705 mail->used / 1024.0 / 1024.0);
3706 timed_thread_unlock(mail->p_timed_thread);
3710 if (obj->data.fs != NULL) {
3711 if (obj->data.fs->size == 0) {
3712 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
3714 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
3715 (int) (255 - obj->data.fsbar.fs->avail * 255 /
3716 obj->data.fs->size));
3721 if (obj->data.fs != NULL) {
3722 human_readable( (obj->data.fs->free ? obj->data.fs->free :
3723 obj->data.fs->avail), p, 255);
3727 if (obj->data.fs != NULL) {
3730 if (obj->data.fs->size) {
3731 val = (obj->data.fs->free ? obj->data.fs->free :
3732 obj->data.fs->avail) * 100 /
3736 percent_print(p, p_max_size, val);
3740 if (obj->data.fs != NULL) {
3741 human_readable(obj->data.fs->size, p, 255);
3745 if (obj->data.fs != NULL)
3746 snprintf(p, p_max_size, "%s", obj->data.fs->type);
3749 if (obj->data.fs != NULL) {
3750 human_readable(obj->data.fs->size - (obj->data.fs->free
3751 ? obj->data.fs->free : obj->data.fs->avail), p, 255);
3755 if (obj->data.fs != NULL) {
3756 if (obj->data.fs->size == 0) {
3757 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
3759 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
3760 (int) (obj->data.fsbar.fs->avail * 255 /
3761 obj->data.fs->size));
3766 if (obj->data.fs != NULL) {
3769 if (obj->data.fs->size) {
3770 val = (obj->data.fs->free ? obj->data.fs->free :
3771 obj->data.fs->avail) * 100 /
3775 percent_print(p, p_max_size, 100 - val);
3779 float *v = info.loadavg;
3781 if (obj->data.loadavg[2]) {
3782 snprintf(p, p_max_size, "%.2f %.2f %.2f",
3783 v[obj->data.loadavg[0] - 1],
3784 v[obj->data.loadavg[1] - 1],
3785 v[obj->data.loadavg[2] - 1]);
3786 } else if (obj->data.loadavg[1]) {
3787 snprintf(p, p_max_size, "%.2f %.2f",
3788 v[obj->data.loadavg[0] - 1],
3789 v[obj->data.loadavg[1] - 1]);
3790 } else if (obj->data.loadavg[0]) {
3791 snprintf(p, p_max_size, "%.2f",
3792 v[obj->data.loadavg[0] - 1]);
3796 new_goto(p, obj->data.i);
3799 new_tab(p, obj->data.pair.a, obj->data.pair.b);
3802 new_hr(p, obj->data.i);
3805 if (cur->nameserver_info.nscount > obj->data.i)
3806 snprintf(p, p_max_size, "%s",
3807 cur->nameserver_info.ns_list[obj->data.i]);
3811 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
3812 snprintf(p, p_max_size, "%s", skill);
3817 PRSS *data = get_rss_info(obj->data.rss.uri,
3818 obj->data.rss.delay);
3822 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
3824 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
3826 // remove trailing new line if one exists
3827 if (str[strlen(str) - 1] == '\n') {
3828 str[strlen(str) - 1] = 0;
3830 snprintf(p, p_max_size, "%s", str);
3831 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
3832 if (obj->data.rss.act_par < data->item_count) {
3833 str = data->items[obj->data.rss.act_par].title;
3834 // remove trailing new line if one exists
3835 if (str[strlen(str) - 1] == '\n') {
3836 str[strlen(str) - 1] = 0;
3838 snprintf(p, p_max_size, "%s", str);
3840 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
3841 if (obj->data.rss.act_par < data->item_count) {
3843 data->items[obj->data.rss.act_par].description;
3844 // remove trailing new line if one exists
3845 if (str[strlen(str) - 1] == '\n') {
3846 str[strlen(str) - 1] = 0;
3848 snprintf(p, p_max_size, "%s", str);
3850 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
3851 if (data->item_count > 0) {
3857 if (obj->data.rss.act_par > data->item_count) {
3858 show = data->item_count;
3860 show = obj->data.rss.act_par;
3862 for (itmp = 0; itmp < show; itmp++) {
3863 PRSS_Item *item = &data->items[itmp];
3867 // don't add new line before first item
3869 strncat(p, "\n", p_max_size);
3871 /* remove trailing new line if one exists,
3872 * we have our own */
3873 if (str[strlen(str) - 1] == '\n') {
3874 str[strlen(str) - 1] = 0;
3876 strncat(p, str, p_max_size);
3888 if (obj->data.hddtemp.update_time < current_update_time - 30) {
3889 if (obj->data.hddtemp.temp)
3890 free(obj->data.hddtemp.temp);
3891 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
3892 obj->data.hddtemp.addr, obj->data.hddtemp.port);
3893 obj->data.hddtemp.update_time = current_update_time;
3895 if (!obj->data.hddtemp.temp) {
3896 snprintf(p, p_max_size, "N/A");
3898 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
3899 unit = obj->data.hddtemp.temp[0];
3901 if (*endptr != '\0')
3902 snprintf(p, p_max_size, "N/A");
3903 else if (unit == 'C')
3904 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
3905 else if (unit == 'F')
3906 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
3908 snprintf(p, p_max_size, "N/A");
3913 new_offset(p, obj->data.i);
3916 new_voffset(p, obj->data.i);
3922 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
3923 obj->data.sysfs.devtype, obj->data.sysfs.type);
3925 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
3926 temp_print(p, p_max_size, r, TEMP_CELSIUS);
3927 } else if (r >= 100.0 || r == 0) {
3928 snprintf(p, p_max_size, "%d", (int) r);
3930 snprintf(p, p_max_size, "%.1f", r);
3936 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
3937 obj->data.sysfs.devtype, obj->data.sysfs.type);
3939 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
3940 temp_print(p, p_max_size, r, TEMP_CELSIUS);
3941 } else if (r >= 100.0 || r == 0) {
3942 snprintf(p, p_max_size, "%d", (int) r);
3944 snprintf(p, p_max_size, "%.1f", r);
3950 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
3951 obj->data.sysfs.devtype, obj->data.sysfs.type);
3953 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
3954 temp_print(p, p_max_size, r, TEMP_CELSIUS);
3955 } else if (r >= 100.0 || r == 0) {
3956 snprintf(p, p_max_size, "%d", (int) r);
3958 snprintf(p, p_max_size, "%.1f", r);
3961 #endif /* __linux__ */
3963 new_alignr(p, obj->data.i);
3966 new_alignc(p, obj->data.i);
3969 char buf[max_user_text];
3970 struct information *tmp_info =
3971 malloc(sizeof(struct information));
3972 memcpy(tmp_info, cur, sizeof(struct information));
3973 generate_text_internal(buf, max_user_text,
3974 *obj->sub, tmp_info);
3976 if (strlen(buf) != 0) {
3982 char expression[max_user_text];
3984 struct information *tmp_info;
3986 tmp_info = malloc(sizeof(struct information));
3987 memcpy(tmp_info, cur, sizeof(struct information));
3988 generate_text_internal(expression, max_user_text,
3989 *obj->sub, tmp_info);
3990 DBGP("parsed arg into '%s'", expression);
3992 val = compare(expression);
3994 ERR("compare failed for expression '%s'",
4002 if (obj->data.ifblock.str
4003 && !check_contains(obj->data.ifblock.s,
4004 obj->data.ifblock.str)) {
4006 } else if (obj->data.ifblock.s
4007 && access(obj->data.ifblock.s, F_OK)) {
4012 if ((obj->data.ifblock.s)
4013 && (!check_mount(obj->data.ifblock.s))) {
4018 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4022 #if defined(__linux__)
4024 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4028 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4031 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4036 human_readable(cur->mem * 1024, p, 255);
4039 human_readable(cur->memeasyfree * 1024, p, 255);
4042 human_readable(cur->memfree * 1024, p, 255);
4045 human_readable(cur->memmax * 1024, p, 255);
4049 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4052 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4053 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4056 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4057 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4060 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4061 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4062 100, 1, obj->showaslog);
4067 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4070 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4073 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4076 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4077 mixer_get_avg(obj->data.mixerbar.l) * 255 / 100);
4080 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4081 mixer_get_left(obj->data.mixerbar.l) * 255 / 100);
4084 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4085 mixer_get_right(obj->data.mixerbar.l) * 255 / 100);
4089 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4091 OBJ(monitor_number) {
4092 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4098 update_mail_count(&obj->data.local_mail);
4099 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4102 update_mail_count(&obj->data.local_mail);
4103 snprintf(p, p_max_size, "%d",
4104 obj->data.local_mail.new_mail_count);
4107 update_mail_count(&obj->data.local_mail);
4108 snprintf(p, p_max_size, "%d",
4109 obj->data.local_mail.seen_mail_count);
4112 update_mail_count(&obj->data.local_mail);
4113 snprintf(p, p_max_size, "%d",
4114 obj->data.local_mail.unseen_mail_count);
4116 OBJ(flagged_mails) {
4117 update_mail_count(&obj->data.local_mail);
4118 snprintf(p, p_max_size, "%d",
4119 obj->data.local_mail.flagged_mail_count);
4121 OBJ(unflagged_mails) {
4122 update_mail_count(&obj->data.local_mail);
4123 snprintf(p, p_max_size, "%d",
4124 obj->data.local_mail.unflagged_mail_count);
4126 OBJ(forwarded_mails) {
4127 update_mail_count(&obj->data.local_mail);
4128 snprintf(p, p_max_size, "%d",
4129 obj->data.local_mail.forwarded_mail_count);
4131 OBJ(unforwarded_mails) {
4132 update_mail_count(&obj->data.local_mail);
4133 snprintf(p, p_max_size, "%d",
4134 obj->data.local_mail.unforwarded_mail_count);
4136 OBJ(replied_mails) {
4137 update_mail_count(&obj->data.local_mail);
4138 snprintf(p, p_max_size, "%d",
4139 obj->data.local_mail.replied_mail_count);
4141 OBJ(unreplied_mails) {
4142 update_mail_count(&obj->data.local_mail);
4143 snprintf(p, p_max_size, "%d",
4144 obj->data.local_mail.unreplied_mail_count);
4147 update_mail_count(&obj->data.local_mail);
4148 snprintf(p, p_max_size, "%d",
4149 obj->data.local_mail.draft_mail_count);
4151 OBJ(trashed_mails) {
4152 update_mail_count(&obj->data.local_mail);
4153 snprintf(p, p_max_size, "%d",
4154 obj->data.local_mail.trashed_mail_count);
4157 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
4159 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4162 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
4165 new_outline(p, obj->data.l);
4168 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
4170 OBJ(running_processes) {
4171 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
4174 snprintf(p, p_max_size, "%s", obj->data.s);
4177 new_bg(p, obj->data.l);
4180 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
4183 human_readable(cur->swap * 1024, p, 255);
4186 human_readable(cur->swapmax * 1024, p, 255);
4189 if (cur->swapmax == 0) {
4190 strncpy(p, "No swap", p_max_size);
4192 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
4196 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4197 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
4200 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4203 time_t t = time(NULL);
4204 struct tm *tm = localtime(&t);
4206 setlocale(LC_TIME, "");
4207 strftime(p, p_max_size, obj->data.s, tm);
4210 time_t t = time(NULL);
4211 struct tm *tm = gmtime(&t);
4213 strftime(p, p_max_size, obj->data.s, tm);
4220 if (obj->data.tztime.tz) {
4221 oldTZ = getenv("TZ");
4222 setenv("TZ", obj->data.tztime.tz, 1);
4228 setlocale(LC_TIME, "");
4229 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4231 setenv("TZ", oldTZ, 1);
4236 // Needless to free oldTZ since getenv gives ptr to static data
4239 human_readable(obj->data.net->recv, p, 255);
4242 human_readable(obj->data.net->trans, p, 255);
4245 snprintf(p, p_max_size, "%d", total_updates);
4248 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
4253 human_readable(obj->data.net->trans_speed, p, 255);
4256 spaced_print(p, p_max_size, "%.1f", 8,
4257 obj->data.net->trans_speed / 1024.0);
4260 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4261 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
4264 format_seconds_short(p, p_max_size, (int) cur->uptime);
4267 format_seconds(p, p_max_size, (int) cur->uptime);
4270 snprintf(p, p_max_size, "%s", cur->users.names);
4273 snprintf(p, p_max_size, "%s", cur->users.terms);
4276 snprintf(p, p_max_size, "%s", cur->users.times);
4279 snprintf(p, p_max_size, "%d", cur->users.number);
4281 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
4282 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4286 msg = get_apm_adapter();
4287 snprintf(p, p_max_size, "%s", msg);
4290 OBJ(apm_battery_life) {
4293 msg = get_apm_battery_life();
4294 snprintf(p, p_max_size, "%s", msg);
4297 OBJ(apm_battery_time) {
4300 msg = get_apm_battery_time();
4301 snprintf(p, p_max_size, "%s", msg);
4304 #endif /* __FreeBSD__ __OpenBSD__ */
4307 #define mpd_printf(fmt, val) \
4308 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
4309 #define mpd_sprintf(val) { \
4310 if (!obj->data.i || obj->data.i > p_max_size) \
4311 mpd_printf("%s", val); \
4313 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
4318 mpd_sprintf(artist);
4322 mpd_printf("%s", random);
4324 mpd_printf("%s", repeat);
4332 mpd_printf("%d", volume);
4334 mpd_printf("%d", bitrate);
4336 mpd_printf("%s", status);
4338 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
4341 format_media_player_time(p, p_max_size, mpd_get_info()->length);
4344 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
4347 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4348 (int) (mpd_get_info()->progress * 255.0f));
4351 struct mpd_s *mpd = mpd_get_info();
4352 int len = obj->data.i;
4353 if (len == 0 || len > p_max_size)
4356 memset(p, 0, p_max_size);
4357 if (mpd->artist && *mpd->artist &&
4358 mpd->title && *mpd->title) {
4359 snprintf(p, len, "%s - %s", mpd->artist,
4361 } else if (mpd->title && *mpd->title) {
4362 snprintf(p, len, "%s", mpd->title);
4363 } else if (mpd->artist && *mpd->artist) {
4364 snprintf(p, len, "%s", mpd->artist);
4365 } else if (mpd->file && *mpd->file) {
4366 snprintf(p, len, "%s", mpd->file);
4371 OBJ(if_mpd_playing) {
4372 if (!mpd_get_info()->is_playing) {
4381 #define MOC_PRINT(t, a) \
4382 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
4384 MOC_PRINT(state, "??");
4387 MOC_PRINT(file, "no file");
4390 MOC_PRINT(title, "no title");
4393 MOC_PRINT(artist, "no artist");
4396 MOC_PRINT(song, "no song");
4399 MOC_PRINT(album, "no album");
4401 OBJ(moc_totaltime) {
4402 MOC_PRINT(totaltime, "0:00");
4405 MOC_PRINT(timeleft, "0:00");
4408 MOC_PRINT(curtime, "0:00");
4411 MOC_PRINT(bitrate, "0Kbps");
4414 MOC_PRINT(rate, "0KHz");
4420 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4423 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4426 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4429 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4431 OBJ(xmms2_comment) {
4432 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4435 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4438 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4441 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4443 OBJ(xmms2_tracknr) {
4444 if (cur->xmms2.tracknr != -1) {
4445 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4448 OBJ(xmms2_bitrate) {
4449 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4452 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4455 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4457 OBJ(xmms2_elapsed) {
4458 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
4459 (cur->xmms2.elapsed / 1000) % 60);
4461 OBJ(xmms2_duration) {
4462 snprintf(p, p_max_size, "%02d:%02d",
4463 cur->xmms2.duration / 60000,
4464 (cur->xmms2.duration / 1000) % 60);
4466 OBJ(xmms2_percent) {
4467 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
4470 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4471 (int) (cur->xmms2.progress * 255.0f));
4473 OBJ(xmms2_playlist) {
4474 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
4476 OBJ(xmms2_timesplayed) {
4477 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
4480 if (strlen(cur->xmms2.title) < 2
4481 && strlen(cur->xmms2.title) < 2) {
4482 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4484 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
4488 OBJ(if_xmms2_connected) {
4489 if (cur->xmms2_conn_state != 1) {
4495 OBJ(audacious_status) {
4496 snprintf(p, p_max_size, "%s",
4497 cur->audacious.items[AUDACIOUS_STATUS]);
4499 OBJ(audacious_title) {
4500 snprintf(p, cur->audacious.max_title_len > 0
4501 ? cur->audacious.max_title_len : p_max_size, "%s",
4502 cur->audacious.items[AUDACIOUS_TITLE]);
4504 OBJ(audacious_length) {
4505 snprintf(p, p_max_size, "%s",
4506 cur->audacious.items[AUDACIOUS_LENGTH]);
4508 OBJ(audacious_length_seconds) {
4509 snprintf(p, p_max_size, "%s",
4510 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4512 OBJ(audacious_position) {
4513 snprintf(p, p_max_size, "%s",
4514 cur->audacious.items[AUDACIOUS_POSITION]);
4516 OBJ(audacious_position_seconds) {
4517 snprintf(p, p_max_size, "%s",
4518 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4520 OBJ(audacious_bitrate) {
4521 snprintf(p, p_max_size, "%s",
4522 cur->audacious.items[AUDACIOUS_BITRATE]);
4524 OBJ(audacious_frequency) {
4525 snprintf(p, p_max_size, "%s",
4526 cur->audacious.items[AUDACIOUS_FREQUENCY]);
4528 OBJ(audacious_channels) {
4529 snprintf(p, p_max_size, "%s",
4530 cur->audacious.items[AUDACIOUS_CHANNELS]);
4532 OBJ(audacious_filename) {
4533 snprintf(p, p_max_size, "%s",
4534 cur->audacious.items[AUDACIOUS_FILENAME]);
4536 OBJ(audacious_playlist_length) {
4537 snprintf(p, p_max_size, "%s",
4538 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4540 OBJ(audacious_playlist_position) {
4541 snprintf(p, p_max_size, "%s",
4542 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4544 OBJ(audacious_main_volume) {
4545 snprintf(p, p_max_size, "%s",
4546 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
4548 OBJ(audacious_bar) {
4552 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4553 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4554 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
4560 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4563 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4566 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4569 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4572 snprintf(p, p_max_size, "%i", cur->bmpx.track);
4575 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4578 /* we have three different types of top (top, top_mem
4579 * and top_time). To avoid having almost-same code three
4580 * times, we have this special handler. */
4583 if (!needed) needed = cur->cpu;
4585 if (!needed) needed = cur->memu;
4587 if (!needed) needed = cur->time;
4592 switch (obj->data.top.type) {
4594 snprintf(p, 16, "%-15s",
4595 needed[obj->data.top.num]->name);
4598 snprintf(p, 7, "%6.2f",
4599 needed[obj->data.top.num]->amount);
4602 snprintf(p, 6, "%5i",
4603 needed[obj->data.top.num]->pid);
4606 snprintf(p, 7, "%6.2f",
4607 needed[obj->data.top.num]->totalmem);
4610 timeval = format_time(
4611 needed[obj->data.top.num]->total_cpu_time, 9);
4612 snprintf(p, 10, "%9s", timeval);
4616 human_readable(needed[obj->data.top.num]->rss,
4620 human_readable(needed[obj->data.top.num]->vsize,
4626 print_tail_object(obj, p, p_max_size);
4628 print_head_object(obj, p, p_max_size);
4630 FILE *fp = fopen(obj->data.s,"r");
4633 /* FIXME: use something more general (see also tail.c, head.c */
4634 #define BUFSZ 0x1000
4639 while(fgets(buf, BUFSZ, fp) != NULL){
4640 for(j = 0; buf[j] != 0; j++) {
4641 if(buf[j] == '\n') {
4646 sprintf(p, "%d", lines);
4649 sprintf(p, "File Unreadable");
4654 FILE *fp = fopen(obj->data.s,"r");
4659 char inword = FALSE;
4662 while(fgets(buf, BUFSZ, fp) != NULL){
4663 for(j = 0; buf[j] != 0; j++) {
4664 if(!isspace(buf[j])) {
4665 if(inword == FALSE) {
4674 sprintf(p, "%d", words);
4677 sprintf(p, "File Unreadable");
4680 #ifdef TCP_PORT_MONITOR
4682 tcp_portmon_action(p, p_max_size,
4683 &obj->data.tcp_port_monitor);
4689 iconv_converting = 1;
4690 iconv_selected = obj->a;
4693 iconv_converting = 0;
4698 OBJ(entropy_avail) {
4699 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
4701 OBJ(entropy_poolsize) {
4702 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
4705 double entropy_perc;
4707 entropy_perc = (double) cur->entropy.entropy_avail /
4708 (double) cur->entropy.poolsize;
4709 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
4715 s = smapi_get_val(obj->data.s);
4716 snprintf(p, p_max_size, "%s", s);
4720 OBJ(if_smapi_bat_installed) {
4722 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
4723 if(!smapi_bat_installed(idx)) {
4727 ERR("argument to if_smapi_bat_installed must be an integer");
4729 OBJ(smapi_bat_perc) {
4731 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
4732 val = smapi_bat_installed(idx) ?
4733 smapi_get_bat_int(idx, "remaining_percent") : 0;
4734 percent_print(p, p_max_size, val);
4736 ERR("argument to smapi_bat_perc must be an integer");
4738 OBJ(smapi_bat_temp) {
4740 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
4741 val = smapi_bat_installed(idx) ?
4742 smapi_get_bat_int(idx, "temperature") : 0;
4743 /* temperature is in milli degree celsius */
4744 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
4746 ERR("argument to smapi_bat_temp must be an integer");
4748 OBJ(smapi_bat_power) {
4750 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
4751 val = smapi_bat_installed(idx) ?
4752 smapi_get_bat_int(idx, "power_now") : 0;
4753 /* power_now is in mW, set to W with one digit precision */
4754 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
4756 ERR("argument to smapi_bat_power must be an integer");
4758 OBJ(smapi_bat_bar) {
4759 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
4760 new_bar(p, obj->a, obj->b, (int)
4761 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
4763 new_bar(p, obj->a, obj->b, 0);
4768 char *tmp, buf[max_user_text];
4769 generate_text_internal(buf, max_user_text,
4772 if (strlen(buf) <= obj->data.scroll.show) {
4773 snprintf(p, p_max_size, "%s", buf);
4776 #define LINESEPARATOR '|'
4777 //place all the lines behind each other with LINESEPARATOR between them
4778 for(j = 0; buf[j] != 0; j++) {
4780 buf[j]=LINESEPARATOR;
4783 //scroll the output obj->data.scroll.start places by copying that many chars from
4784 //the front of the string to tmp, scrolling the rest to the front and placing tmp
4785 //at the back of the string
4786 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
4787 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
4788 for(j = obj->data.scroll.start; buf[j] != 0; j++){
4789 buf[j - obj->data.scroll.start] = buf[j];
4791 strcpy(&buf[j - obj->data.scroll.start], tmp);
4793 //only show the requested number of chars
4794 if(obj->data.scroll.show < j) {
4795 buf[obj->data.scroll.show] = 0;
4797 //next time, scroll a place more or reset scrolling if we are at the end
4798 obj->data.scroll.start += obj->data.scroll.step;
4799 if(obj->data.scroll.start >= j){
4800 obj->data.scroll.start = 0;
4802 snprintf(p, p_max_size, "%s", buf);
4806 int value = get_nvidia_value(obj->data.nvidia.type, display);
4808 snprintf(p, p_max_size, "N/A");
4809 else if (obj->data.nvidia.type == NV_TEMP)
4810 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
4811 else if (obj->data.nvidia.print_as_float &&
4812 value > 0 && value < 100)
4813 snprintf(p, p_max_size, "%.1f", (float)value);
4815 snprintf(p, p_max_size, "%d", value);
4825 unsigned int a = strlen(p);
4828 if (a > 0 && iconv_converting && iconv_selected > 0
4829 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
4831 size_t dummy1, dummy2;
4832 char *ptr = buff_in;
4835 dummy1 = dummy2 = a;
4837 strncpy(buff_in, p, p_max_size);
4839 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
4840 while (dummy1 > 0) {
4841 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
4844 ERR("Iconv codeset conversion failed");
4849 /* It is nessecary when we are converting from multibyte to
4850 * singlebyte codepage */
4853 #endif /* HAVE_ICONV */
4860 double current_update_time, next_update_time, last_update_time;
4862 static void generate_text(void)
4864 struct information *cur = &info;
4871 current_update_time = get_time();
4875 /* add things to the buffer */
4881 generate_text_internal(p, max_user_text, global_root_object, cur);
4883 if (stuff_in_upper_case) {
4886 tmp_p = text_buffer;
4888 *tmp_p = toupper(*tmp_p);
4893 next_update_time += update_interval;
4894 if (next_update_time < get_time()) {
4895 next_update_time = get_time() + update_interval;
4896 } else if (next_update_time > get_time() + update_interval) {
4897 next_update_time = get_time() + update_interval;
4899 last_update_time = current_update_time;
4904 static inline int get_string_width(const char *s)
4907 if (output_methods & TO_X) {
4908 return *s ? calc_text_width(s, strlen(s)) : 0;
4914 static inline int get_string_width_special(char *s)
4922 if ((output_methods & TO_X) == 0) {
4924 return (s) ? strlen(s) : 0;
4932 p = strndup(s, text_buffer_size);
4936 if (*p == SPECIAL_CHAR) {
4937 /* shift everything over by 1 so that the special char
4938 * doesn't mess up the size calculation */
4939 for (i = 0; i < strlen(p); i++) {
4940 *(p + i) = *(p + i + 1);
4942 if (specials[special_index + idx].type == GRAPH
4943 || specials[special_index + idx].type == BAR) {
4944 width += specials[special_index + idx].width;
4951 if (strlen(final) > 1) {
4952 width += calc_text_width(final, strlen(final));
4960 static void text_size_updater(char *s);
4962 int last_font_height;
4963 static void update_text_area(void)
4967 if ((output_methods & TO_X) == 0)
4969 /* update text size if it isn't fixed */
4974 text_width = minimum_width;
4977 last_font_height = font_height();
4978 for_each_line(text_buffer, text_size_updater);
4980 if (text_height < minimum_height) {
4981 text_height = minimum_height;
4983 if (text_width > maximum_width && maximum_width > 0) {
4984 text_width = maximum_width;
4988 /* get text position on workarea */
4989 switch (text_alignment) {
4996 x = workarea[2] - text_width - gap_x;
5001 x = workarea[2] / 2 - text_width / 2 - gap_x;
5008 y = workarea[3] - text_height - gap_y;
5012 x = workarea[2] - text_width - gap_x;
5013 y = workarea[3] - text_height - gap_y;
5017 x = workarea[2] / 2 - text_width / 2 - gap_x;
5018 y = workarea[3] - text_height - gap_y;
5023 y = workarea[3] / 2 - text_height / 2 - gap_y;
5027 x = workarea[2] - text_width - gap_x;
5028 y = workarea[3] / 2 - text_height / 2 - gap_y;
5032 case NONE: // Let the WM manage the window
5043 if (own_window && !fixed_pos) {
5046 text_start_x = border_margin + 1;
5047 text_start_y = border_margin + 1;
5048 window.x = x - border_margin - 1;
5049 window.y = y - border_margin - 1;
5053 /* If window size doesn't match to workarea's size,
5054 * then window probably includes panels (gnome).
5055 * Blah, doesn't work on KDE. */
5056 if (workarea[2] != window.width || workarea[3] != window.height) {
5068 static int cur_x, cur_y; /* current x and y for drawing */
5070 //draw_mode also without X11 because we only need to print to stdout with FG
5071 static int draw_mode; /* FG, BG or OUTLINE */
5073 static long current_color;
5075 static void text_size_updater(char *s)
5080 if ((output_methods & TO_X) == 0)
5082 /* get string widths and skip specials */
5085 if (*p == SPECIAL_CHAR) {
5087 w += get_string_width(s);
5090 if (specials[special_index].type == BAR
5091 || specials[special_index].type == GRAPH) {
5092 w += specials[special_index].width;
5093 if (specials[special_index].height > last_font_height) {
5094 last_font_height = specials[special_index].height;
5095 last_font_height += font_ascent();
5097 } else if (specials[special_index].type == OFFSET) {
5098 if (specials[special_index].arg > 0) {
5099 w += specials[special_index].arg;
5101 } else if (specials[special_index].type == VOFFSET) {
5102 last_font_height += specials[special_index].arg;
5103 } else if (specials[special_index].type == GOTO) {
5104 if (specials[special_index].arg > cur_x) {
5105 w = (int) specials[special_index].arg;
5107 } else if (specials[special_index].type == TAB) {
5108 int start = specials[special_index].arg;
5109 int step = specials[special_index].width;
5111 if (!step || step < 0) {
5114 w += step - (cur_x - text_start_x - start) % step;
5115 } else if (specials[special_index].type == FONT) {
5116 selected_font = specials[special_index].font_added;
5117 if (font_height() > last_font_height) {
5118 last_font_height = font_height();
5127 w += get_string_width(s);
5128 if (w > text_width) {
5131 if (text_width > maximum_width && maximum_width) {
5132 text_width = maximum_width;
5135 text_height += last_font_height;
5136 last_font_height = font_height();
5139 static inline void set_foreground_color(long c)
5141 if ((output_methods & TO_X) == 0)
5144 XSetForeground(display, window.gc, c);
5148 static void draw_string(const char *s)
5150 int i, i2, pos, width_of_s;
5158 width_of_s = get_string_width(s);
5159 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
5161 fflush(stdout); /* output immediately, don't buffer */
5163 if ((output_methods & TO_STDERR) && draw_mode == FG) {
5164 fprintf(stderr, "%s\n", s);
5165 fflush(stderr); /* output immediately, don't buffer */
5167 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
5168 fprintf(overwrite_fpointer, "%s\n", s);
5170 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
5171 fprintf(append_fpointer, "%s\n", s);
5173 memset(tmpstring1, 0, text_buffer_size);
5174 memset(tmpstring2, 0, text_buffer_size);
5175 strncpy(tmpstring1, s, text_buffer_size - 1);
5180 if (output_methods & TO_X) {
5181 max = ((text_width - width_of_s) / get_string_width(" "));
5184 /* This code looks for tabs in the text and coverts them to spaces.
5185 * The trick is getting the correct number of spaces, and not going
5186 * over the window's size without forcing the window larger. */
5187 for (i = 0; i < (int) text_buffer_size; i++) {
5188 if (tmpstring1[i] == '\t') {
5190 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
5191 /* guard against overrun */
5192 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
5197 /* guard against overrun */
5198 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
5203 if (output_methods & TO_X) {
5204 if (text_width == maximum_width) {
5205 /* this means the text is probably pushing the limit,
5206 * so we'll chop it */
5207 while (cur_x + get_string_width(tmpstring2) - text_start_x
5208 > maximum_width && strlen(tmpstring2) > 0) {
5209 tmpstring2[strlen(tmpstring2) - 1] = '\0';
5216 if (output_methods & TO_X) {
5222 c.pixel = current_color;
5223 XQueryColor(display, DefaultColormap(display, screen), &c);
5226 c2.color.red = c.red;
5227 c2.color.green = c.green;
5228 c2.color.blue = c.blue;
5229 c2.color.alpha = fonts[selected_font].font_alpha;
5231 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5232 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5234 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5235 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5240 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
5243 cur_x += width_of_s;
5246 memcpy(tmpstring1, s, text_buffer_size);
5249 static void draw_line(char *s)
5258 if ((output_methods & TO_X) == 0) {
5263 cur_x = text_start_x;
5264 cur_y += font_ascent();
5265 font_h = font_height();
5267 /* find specials and draw stuff */
5270 if (*p == SPECIAL_CHAR) {
5273 /* draw string before special */
5280 switch (specials[special_index].type) {
5281 case HORIZONTAL_LINE:
5283 int h = specials[special_index].height;
5284 int mid = font_ascent() / 2;
5286 w = text_start_x + text_width - cur_x;
5288 XSetLineAttributes(display, window.gc, h, LineSolid,
5289 CapButt, JoinMiter);
5290 XDrawLine(display, window.drawable, window.gc, cur_x,
5291 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
5297 int h = specials[special_index].height;
5298 int tmp_s = specials[special_index].arg;
5299 int mid = font_ascent() / 2;
5300 char ss[2] = { tmp_s, tmp_s };
5302 w = text_start_x + text_width - cur_x - 1;
5303 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
5304 CapButt, JoinMiter);
5305 XSetDashes(display, window.gc, 0, ss, 2);
5306 XDrawLine(display, window.drawable, window.gc, cur_x,
5307 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
5313 int h, bar_usage, by;
5314 if (cur_x - text_start_x > maximum_width
5315 && maximum_width > 0) {
5318 h = specials[special_index].height;
5319 bar_usage = specials[special_index].arg;
5320 by = cur_y - (font_ascent() / 2) - 1;
5322 if (h < font_height()) {
5325 w = specials[special_index].width;
5327 w = text_start_x + text_width - cur_x - 1;
5333 XSetLineAttributes(display, window.gc, 1, LineSolid,
5334 CapButt, JoinMiter);
5336 XDrawRectangle(display, window.drawable, window.gc, cur_x,
5338 XFillRectangle(display, window.drawable, window.gc, cur_x,
5339 by, w * bar_usage / 255, h);
5340 if (specials[special_index].height > cur_y_add
5341 && specials[special_index].height > font_h) {
5342 cur_y_add = specials[special_index].height;
5347 case GAUGE: /* new GAUGE */
5350 unsigned long last_colour = current_color;
5354 if (cur_x - text_start_x > maximum_width
5355 && maximum_width > 0) {
5359 h = specials[special_index].height;
5360 by = cur_y - (font_ascent() / 2) - 1;
5362 if (h < font_height()) {
5365 w = specials[special_index].width;
5367 w = text_start_x + text_width - cur_x - 1;
5373 XSetLineAttributes(display, window.gc, 1, LineSolid,
5374 CapButt, JoinMiter);
5376 XDrawArc(display, window.drawable, window.gc,
5377 cur_x, by, w * 2.0, h * 2.0, 0, 180*64);
5380 usage =specials[special_index].arg;
5381 angle = (3.14)*(float)(usage)/255.;
5382 px = (float)(cur_x+w)-(float)(w)*cos(angle);
5383 py = (float)(by+h)-(float)(h)*sin(angle);
5385 XDrawLine(display, window.drawable, window.gc,
5386 cur_x + w, by+h, (int)(px), (int)(py));
5389 if (specials[special_index].height > cur_y_add
5390 && specials[special_index].height > font_h) {
5391 cur_y_add = specials[special_index].height;
5394 set_foreground_color(last_colour);
5402 int h, by, i, j = 0;
5403 int gradient_size = 0;
5404 float gradient_factor = 0;
5405 float gradient_update = 0;
5406 unsigned long last_colour = current_color;
5407 unsigned long tmpcolour = current_color;
5408 if (cur_x - text_start_x > maximum_width
5409 && maximum_width > 0) {
5412 h = specials[special_index].height;
5413 by = cur_y - (font_ascent() / 2) - 1;
5415 if (h < font_height()) {
5418 w = specials[special_index].width;
5420 w = text_start_x + text_width - cur_x - 1;
5425 if (draw_graph_borders) {
5426 XSetLineAttributes(display, window.gc, 1, LineSolid,
5427 CapButt, JoinMiter);
5428 XDrawRectangle(display, window.drawable, window.gc,
5431 XSetLineAttributes(display, window.gc, 1, LineSolid,
5432 CapButt, JoinMiter);
5434 if (specials[special_index].last_colour != 0
5435 || specials[special_index].first_colour != 0) {
5436 tmpcolour = specials[special_index].last_colour;
5438 gradient_max(specials[special_index].last_colour,
5439 specials[special_index].first_colour);
5440 gradient_factor = (float) gradient_size / (w - 2);
5442 for (i = w - 2; i > -1; i--) {
5443 if (specials[special_index].last_colour != 0
5444 || specials[special_index].first_colour != 0) {
5445 XSetForeground(display, window.gc, tmpcolour);
5446 gradient_update += gradient_factor;
5447 while (gradient_update > 0) {
5448 tmpcolour = do_gradient(tmpcolour,
5449 specials[special_index].first_colour);
5453 if ((w - i) / ((float) (w - 2) /
5454 (specials[special_index].graph_width)) > j
5455 && j < MAX_GRAPH_DEPTH - 3) {
5458 /* this is mugfugly, but it works */
5459 XDrawLine(display, window.drawable, window.gc,
5460 cur_x + i + 1, by + h, cur_x + i + 1,
5461 by + h - specials[special_index].graph[j] *
5462 (h - 1) / specials[special_index].graph_scale);
5464 if (specials[special_index].height > cur_y_add
5465 && specials[special_index].height > font_h) {
5466 cur_y_add = specials[special_index].height;
5468 /* if (draw_mode == BG) {
5469 set_foreground_color(default_bg_color);
5470 } else if (draw_mode == OUTLINE) {
5471 set_foreground_color(default_out_color);
5473 set_foreground_color(default_fg_color);
5475 if (show_graph_range) {
5478 unsigned short int seconds = update_interval * w;
5483 unsigned short int timeunits;
5485 timeunits = seconds / 86400; seconds %= 86400;
5486 if( timeunits > 0 ) {
5487 asprintf(&tmp_day_str, "%dd", timeunits);
5489 tmp_day_str = strdup("");
5491 timeunits = seconds / 3600; seconds %= 3600;
5492 if( timeunits > 0 ) {
5493 asprintf(&tmp_hour_str, "%dh", timeunits);
5495 tmp_hour_str = strdup("");
5497 timeunits = seconds / 60; seconds %= 60;
5499 asprintf(&tmp_min_str, "%dm", timeunits);
5501 tmp_min_str = strdup("");
5504 asprintf(&tmp_sec_str, "%ds", seconds);
5506 tmp_sec_str = strdup("");
5508 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
5509 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
5511 asprintf(&tmp_str, "Range not possible"); //should never happen, but better safe then sorry
5513 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
5514 cur_y += font_height() / 2;
5515 draw_string(tmp_str);
5521 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
5524 cur_x += font_ascent() / 2;
5525 cur_y += font_height() / 2;
5527 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
5529 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
5530 draw_string(tmp_str);
5536 set_foreground_color(last_colour);
5542 int old = font_ascent();
5544 cur_y -= font_ascent();
5545 selected_font = specials[special_index].font_added;
5546 if (cur_y + font_ascent() < cur_y + old) {
5549 cur_y += font_ascent();
5555 if (draw_mode == FG) {
5556 set_foreground_color(specials[special_index].arg);
5561 if (draw_mode == BG) {
5562 set_foreground_color(specials[special_index].arg);
5567 if (draw_mode == OUTLINE) {
5568 set_foreground_color(specials[special_index].arg);
5573 w += specials[special_index].arg;
5577 cur_y += specials[special_index].arg;
5581 if (specials[special_index].arg >= 0) {
5582 cur_x = (int) specials[special_index].arg;
5588 int start = specials[special_index].arg;
5589 int step = specials[special_index].width;
5591 if (!step || step < 0) {
5594 w = step - (cur_x - text_start_x - start) % step;
5600 /* TODO: add back in "+ border_margin" to the end of
5602 int pos_x = text_start_x + text_width -
5603 get_string_width_special(s);
5605 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
5606 "get_string_width(p) %i gap_x %i "
5607 "specials[special_index].arg %i border_margin %i "
5608 "border_width %i\n", pos_x, text_start_x, text_width,
5609 cur_x, get_string_width_special(s), gap_x,
5610 specials[special_index].arg, border_margin,
5612 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5613 cur_x = pos_x - specials[special_index].arg;
5620 int pos_x = (text_width) / 2 - get_string_width_special(s) /
5621 2 - (cur_x - text_start_x);
5622 /* int pos_x = text_start_x + text_width / 2 -
5623 get_string_width_special(s) / 2; */
5625 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
5626 "get_string_width(p) %i gap_x %i "
5627 "specials[special_index].arg %i\n", pos_x, text_start_x,
5628 text_width, cur_x, get_string_width(s), gap_x,
5629 specials[special_index].arg); */
5630 if (pos_x > specials[special_index].arg) {
5631 w = pos_x - specials[special_index].arg;
5644 if (cur_y_add > 0) {
5646 cur_y -= font_descent();
5651 cur_y += font_descent();
5655 static void draw_text(void)
5658 if (output_methods & TO_X) {
5659 cur_y = text_start_y;
5662 if (draw_borders && border_width > 0) {
5663 unsigned int b = (border_width + 1) / 2;
5665 if (stippled_borders) {
5666 char ss[2] = { stippled_borders, stippled_borders };
5667 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
5668 CapButt, JoinMiter);
5669 XSetDashes(display, window.gc, 0, ss, 2);
5671 XSetLineAttributes(display, window.gc, border_width, LineSolid,
5672 CapButt, JoinMiter);
5675 XDrawRectangle(display, window.drawable, window.gc,
5676 text_start_x - border_margin + b, text_start_y - border_margin + b,
5677 text_width + border_margin * 2 - 1 - b * 2,
5678 text_height + border_margin * 2 - 1 - b * 2);
5685 for_each_line(text_buffer, draw_line);
5688 static void draw_stuff(void)
5690 if(overwrite_file) {
5691 overwrite_fpointer = fopen(overwrite_file, "w");
5692 if(!overwrite_fpointer)
5693 ERR("Can't overwrite '%s' anymore", overwrite_file);
5696 append_fpointer = fopen(append_file, "a");
5697 if(!append_fpointer)
5698 ERR("Can't append '%s' anymore", append_file);
5701 if (output_methods & TO_X) {
5703 if (draw_shades && !draw_outline) {
5706 set_foreground_color(default_bg_color);
5717 for (i = -1; i < 2; i++) {
5718 for (j = -1; j < 2; j++) {
5719 if (i == 0 && j == 0) {
5724 set_foreground_color(default_out_color);
5725 draw_mode = OUTLINE;
5733 set_foreground_color(default_fg_color);
5739 if (output_methods & TO_X) {
5744 swap.swap_window = window.window;
5745 swap.swap_action = XdbeBackground;
5746 XdbeSwapBuffers(display, &swap, 1);
5751 if(overwrite_fpointer) {
5752 fclose(overwrite_fpointer);
5753 overwrite_fpointer = 0;
5755 if(append_fpointer) {
5756 fclose(append_fpointer);
5757 append_fpointer = 0;
5762 static void clear_text(int exposures)
5766 /* The swap action is XdbeBackground, which clears */
5771 /* there is some extra space for borders and outlines */
5772 XClearArea(display, window.window, text_start_x - border_margin - 1,
5773 text_start_y - border_margin - 1,
5774 text_width + border_margin * 2 + 2,
5775 text_height + border_margin * 2 + 2, exposures ? True : 0);
5780 static int need_to_update;
5782 /* update_text() generates new text and clears old text area */
5783 static void update_text(void)
5787 if (output_methods & TO_X)
5793 static void main_loop(void)
5795 #ifdef SIGNAL_BLOCKING
5796 sigset_t newmask, oldmask;
5803 XserverRegion region2, part;
5804 int event_base, error_base;
5806 if (output_methods & TO_X) {
5807 region = XCreateRegion();
5809 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
5810 ERR("Xdamage extension unavailable");
5812 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5813 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5814 part = XFixesCreateRegionFromWindow(display, window.window, 0);
5815 #endif /* HAVE_XDAMAGE */
5819 #ifdef SIGNAL_BLOCKING
5820 sigemptyset(&newmask);
5821 sigaddset(&newmask, SIGINT);
5822 sigaddset(&newmask, SIGTERM);
5823 sigaddset(&newmask, SIGUSR1);
5826 next_update_time = last_update_time = get_time();
5828 while (total_run_times == 0 || info.looped < total_run_times) {
5831 #ifdef SIGNAL_BLOCKING
5832 /* block signals. we will inspect for pending signals later */
5833 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
5834 CRIT_ERR("unable to sigprocmask()");
5839 if (output_methods & TO_X) {
5842 /* wait for X event or timeout */
5844 if (!XPending(display)) {
5848 t = next_update_time - get_time();
5852 } else if (t > update_interval) {
5853 t = update_interval;
5856 tv.tv_sec = (long) t;
5857 tv.tv_usec = (long) (t * 1000000) % 1000000;
5859 FD_SET(ConnectionNumber(display), &fdsr);
5861 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
5863 if (errno != EINTR) {
5864 ERR("can't select(): %s", strerror(errno));
5874 if (need_to_update) {
5876 int wx = window.x, wy = window.y;
5884 /* resize window if it isn't right size */
5886 && (text_width + border_margin * 2 + 1 != window.width
5887 || text_height + border_margin * 2 + 1 != window.height)) {
5888 window.width = text_width + border_margin * 2 + 1;
5889 window.height = text_height + border_margin * 2 + 1;
5890 XResizeWindow(display, window.window, window.width,
5893 set_transparent_background(window.window);
5897 /* move window if it isn't in right position */
5898 if (!fixed_pos && (window.x != wx || window.y != wy)) {
5899 XMoveWindow(display, window.window, window.x, window.y);
5910 r.x = text_start_x - border_margin;
5911 r.y = text_start_y - border_margin;
5912 r.width = text_width + border_margin * 2;
5913 r.height = text_height + border_margin * 2;
5914 XUnionRectWithRegion(&r, region, region);
5919 /* handle X events */
5920 while (XPending(display)) {
5923 XNextEvent(display, &ev);
5931 r.width = ev.xexpose.width;
5932 r.height = ev.xexpose.height;
5933 XUnionRectWithRegion(&r, region, region);
5938 case ReparentNotify:
5939 /* set background to ParentRelative for all parents */
5941 set_transparent_background(window.window);
5945 case ConfigureNotify:
5947 /* if window size isn't what expected, set fixed size */
5948 if (ev.xconfigure.width != window.width
5949 || ev.xconfigure.height != window.height) {
5950 if (window.width != 0 && window.height != 0) {
5954 /* clear old stuff before screwing up
5959 XWindowAttributes attrs;
5961 if (XGetWindowAttributes(display,
5962 window.window, &attrs)) {
5963 window.width = attrs.width;
5964 window.height = attrs.height;
5968 text_width = window.width - border_margin * 2 - 1;
5969 text_height = window.height - border_margin * 2 - 1;
5970 if (text_width > maximum_width
5971 && maximum_width > 0) {
5972 text_width = maximum_width;
5976 /* if position isn't what expected, set fixed pos
5977 * total_updates avoids setting fixed_pos when window
5978 * is set to weird locations when started */
5979 /* // this is broken
5980 if (total_updates >= 2 && !fixed_pos
5981 && (window.x != ev.xconfigure.x
5982 || window.y != ev.xconfigure.y)
5983 && (ev.xconfigure.x != 0
5984 || ev.xconfigure.y != 0)) {
5993 /* if an ordinary window with decorations */
5994 if ((window.type == TYPE_NORMAL)
5995 && (!TEST_HINT(window.hints,
5996 HINT_UNDECORATED))) {
5997 /* allow conky to hold input focus. */
6000 /* forward the click to the desktop window */
6001 XUngrabPointer(display, ev.xbutton.time);
6002 ev.xbutton.window = window.desktop;
6003 XSendEvent(display, ev.xbutton.window, False,
6004 ButtonPressMask, &ev);
6005 XSetInputFocus(display, ev.xbutton.window,
6006 RevertToParent, ev.xbutton.time);
6013 /* if an ordinary window with decorations */
6014 if ((window.type == TYPE_NORMAL)
6015 && (!TEST_HINT(window.hints,
6016 HINT_UNDECORATED))) {
6017 /* allow conky to hold input focus. */
6020 /* forward the release to the desktop window */
6021 ev.xbutton.window = window.desktop;
6022 XSendEvent(display, ev.xbutton.window, False,
6023 ButtonReleaseMask, &ev);
6032 if (ev.type == event_base + XDamageNotify) {
6033 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
6035 XFixesSetRegion(display, part, &dev->area, 1);
6036 XFixesUnionRegion(display, region2, region2, part);
6038 #endif /* HAVE_XDAMAGE */
6044 XDamageSubtract(display, damage, region2, None);
6045 XFixesSetRegion(display, region2, 0, 0);
6046 #endif /* HAVE_XDAMAGE */
6048 /* XDBE doesn't seem to provide a way to clear the back buffer without
6049 * interfering with the front buffer, other than passing XdbeBackground
6050 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6051 * redraw the text even if it wasn't part of the exposed area. OTOH,
6052 * if we're not going to call draw_stuff at all, then no swap happens
6053 * and we can safely do nothing. */
6055 if (!XEmptyRegion(region)) {
6060 r.x = text_start_x - border_margin;
6061 r.y = text_start_y - border_margin;
6062 r.width = text_width + border_margin * 2;
6063 r.height = text_height + border_margin * 2;
6064 XUnionRectWithRegion(&r, region, region);
6067 XSetRegion(display, window.gc, region);
6070 XftDrawSetClip(window.xftdraw, region);
6074 XDestroyRegion(region);
6075 region = XCreateRegion();
6079 t = (next_update_time - get_time()) * 1000000;
6080 if(t > 0) usleep((useconds_t)t);
6087 #ifdef SIGNAL_BLOCKING
6088 /* unblock signals of interest and let handler fly */
6089 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
6090 CRIT_ERR("unable to sigprocmask()");
6094 switch (g_signal_pending) {
6097 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
6102 ERR("received SIGINT or SIGTERM to terminate. bye!");
6105 if (output_methods & TO_X) {
6106 XDestroyRegion(region);
6109 XDamageDestroy(display, damage);
6110 XFixesDestroyRegion(display, region2);
6111 XFixesDestroyRegion(display, part);
6112 #endif /* HAVE_XDAMAGE */
6115 if(overwrite_file) {
6116 free(overwrite_file);
6123 return; /* return from main_loop */
6126 /* Reaching here means someone set a signal
6127 * (SIGXXXX, signal_handler), but didn't write any code
6129 * If you don't want to handle a signal, don't set a handler on
6130 * it in the first place. */
6131 if (g_signal_pending) {
6132 ERR("ignoring signal (%d)", g_signal_pending);
6136 g_signal_pending = 0;
6139 #if defined(X11) && defined(HAVE_XDAMAGE)
6140 if (output_methods & TO_X) {
6141 XDamageDestroy(display, damage);
6142 XFixesDestroyRegion(display, region2);
6143 XFixesDestroyRegion(display, part);
6144 XDestroyRegion(region);
6147 #endif /* X11 && HAVE_XDAMAGE */
6150 static void load_config_file(const char *);
6151 static void load_config_file_x11(const char *);
6153 /* reload the config file */
6154 static void reload_config(void)
6156 timed_thread_destroy_registered_threads();
6158 if (info.cpu_usage) {
6159 free(info.cpu_usage);
6160 info.cpu_usage = NULL;
6171 #ifdef TCP_PORT_MONITOR
6172 tcp_portmon_clear();
6175 if (current_config) {
6177 load_config_file(current_config);
6178 load_config_file_x11(current_config);
6180 /* re-init specials array */
6181 if ((specials = realloc((void *) specials,
6182 sizeof(struct special_t) * max_specials)) == 0) {
6183 ERR("failed to realloc specials array");
6189 // clear the window first
6190 XClearWindow(display, RootWindow(display, screen));
6194 extract_variable_text(global_text);
6200 tmpstring1 = malloc(text_buffer_size);
6201 memset(tmpstring1, 0, text_buffer_size);
6205 tmpstring2 = malloc(text_buffer_size);
6206 memset(tmpstring2, 0, text_buffer_size);
6210 text_buffer = malloc(max_user_text);
6211 memset(text_buffer, 0, max_user_text);
6216 static void clean_up(void)
6218 timed_thread_destroy_registered_threads();
6220 if (info.cpu_usage) {
6221 free(info.cpu_usage);
6222 info.cpu_usage = NULL;
6225 if (output_methods & TO_X) {
6228 XdbeDeallocateBackBufferName(display, window.back_buffer);
6233 XDestroyWindow(display, window.window);
6234 XClearWindow(display, RootWindow(display, screen));
6239 XClearWindow(display, RootWindow(display, screen));
6244 XFreeGC(display, window.gc);
6249 free_text_objects(&global_root_object);
6268 free(current_config);
6270 #ifdef TCP_PORT_MONITOR
6271 tcp_portmon_clear();
6280 for (i = 0; i < special_count; i++) {
6281 if (specials[i].type == GRAPH) {
6282 free(specials[i].graph);
6289 clear_diskio_stats();
6292 static int string_to_bool(const char *s)
6295 // Assumes an option without a true/false means true
6297 } else if (strcasecmp(s, "yes") == EQUAL) {
6299 } else if (strcasecmp(s, "true") == EQUAL) {
6301 } else if (strcasecmp(s, "1") == EQUAL) {
6308 static enum alignment string_to_alignment(const char *s)
6310 if (strcasecmp(s, "top_left") == EQUAL) {
6312 } else if (strcasecmp(s, "top_right") == EQUAL) {
6314 } else if (strcasecmp(s, "top_middle") == EQUAL) {
6316 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
6318 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
6319 return BOTTOM_RIGHT;
6320 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
6321 return BOTTOM_MIDDLE;
6322 } else if (strcasecmp(s, "middle_left") == EQUAL) {
6324 } else if (strcasecmp(s, "middle_right") == EQUAL) {
6325 return MIDDLE_RIGHT;
6326 } else if (strcasecmp(s, "tl") == EQUAL) {
6328 } else if (strcasecmp(s, "tr") == EQUAL) {
6330 } else if (strcasecmp(s, "tm") == EQUAL) {
6332 } else if (strcasecmp(s, "bl") == EQUAL) {
6334 } else if (strcasecmp(s, "br") == EQUAL) {
6335 return BOTTOM_RIGHT;
6336 } else if (strcasecmp(s, "bm") == EQUAL) {
6337 return BOTTOM_MIDDLE;
6338 } else if (strcasecmp(s, "ml") == EQUAL) {
6340 } else if (strcasecmp(s, "mr") == EQUAL) {
6341 return MIDDLE_RIGHT;
6342 } else if (strcasecmp(s, "none") == EQUAL) {
6350 static void set_default_configurations_for_x(void)
6352 default_fg_color = WhitePixel(display, screen);
6353 default_bg_color = BlackPixel(display, screen);
6354 default_out_color = BlackPixel(display, screen);
6355 color0 = default_fg_color;
6356 color1 = default_fg_color;
6357 color2 = default_fg_color;
6358 color3 = default_fg_color;
6359 color4 = default_fg_color;
6360 color5 = default_fg_color;
6361 color6 = default_fg_color;
6362 color7 = default_fg_color;
6363 color8 = default_fg_color;
6364 color9 = default_fg_color;
6368 static void set_default_configurations(void)
6370 fork_to_background = 0;
6371 total_run_times = 0;
6372 info.cpu_avg_samples = 2;
6373 info.net_avg_samples = 2;
6381 mpd_set_host("localhost");
6382 mpd_set_port("6600");
6385 info.xmms2.artist = NULL;
6386 info.xmms2.album = NULL;
6387 info.xmms2.title = NULL;
6388 info.xmms2.genre = NULL;
6389 info.xmms2.comment = NULL;
6390 info.xmms2.url = NULL;
6391 info.xmms2.status = NULL;
6392 info.xmms2.playlist = NULL;
6394 use_spacer = NO_SPACER;
6396 output_methods = TO_X;
6398 output_methods = TO_STDOUT;
6401 show_graph_scale = 0;
6402 show_graph_range = 0;
6403 template[0] = strdup("");
6404 template[1] = strdup("");
6405 template[2] = strdup("");
6406 template[3] = strdup("");
6407 template[4] = strdup("");
6408 template[5] = strdup("");
6409 template[6] = strdup("");
6410 template[7] = strdup("");
6411 template[8] = strdup("");
6412 template[9] = strdup("");
6415 draw_graph_borders = 1;
6417 set_first_font("6x10");
6425 window.type = TYPE_NORMAL;
6427 strcpy(window.class_name, PACKAGE_NAME);
6429 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
6431 stippled_borders = 0;
6434 text_alignment = BOTTOM_LEFT;
6435 info.x11.monitor.number = 1;
6436 info.x11.monitor.current = 0;
6439 free(current_mail_spool);
6443 variable_substitute(MAIL_FILE, buf, 256);
6444 if (buf[0] != '\0') {
6445 current_mail_spool = strndup(buf, text_buffer_size);
6450 update_interval = 3.0;
6451 info.music_player_interval = 1.0;
6452 stuff_in_upper_case = 0;
6453 info.users.number = 1;
6455 #ifdef TCP_PORT_MONITOR
6456 /* set default connection limit */
6457 tcp_portmon_set_max_connections(0);
6461 /* returns 1 if you can overwrite or create the file at 'path' */
6462 static _Bool overwrite_works(const char *path)
6466 if (!(filepointer = fopen(path, "w")))
6468 fclose(filepointer);
6472 /* returns 1 if you can append or create the file at 'path' */
6473 static _Bool append_works(const char *path)
6477 if (!(filepointer = fopen(path, "a")))
6479 fclose(filepointer);
6484 static void X11_initialisation(void)
6486 if (x_initialised == YES) return;
6487 output_methods |= TO_X;
6489 set_default_configurations_for_x();
6490 x_initialised = YES;
6494 static void load_config_file(const char *f)
6496 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6497 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
6501 set_default_configurations();
6502 #ifdef CONFIG_OUTPUT
6503 if (!strcmp(f, "==builtin==")) {
6504 #ifdef HAVE_FOPENCOOKIE
6505 fp = fopencookie(NULL, "r", conf_cookie);
6508 #endif /* CONFIG_OUTPUT */
6514 DBGP("reading contents from config file '%s'", f);
6517 char buf[256], *p, *p2, *name, *value;
6520 if (fgets(buf, 256, fp) == NULL) {
6526 /* break at comment */
6527 p2 = strchr(p, '#');
6533 while (*p && isspace((int) *p)) {
6537 continue; /* empty line */
6544 while (*p2 && !isspace((int) *p2)) {
6548 *p2 = '\0'; /* break at name's end */
6555 while (*p && isspace((int) *p)) {
6561 p2 = value + strlen(value);
6562 while (isspace((int) *(p2 - 1))) {
6569 #define CONF2(a) if (strcasecmp(name, a) == 0)
6570 #define CONF(a) else CONF2(a)
6571 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
6572 || strcasecmp(name, b) == 0)
6576 /* don't listen if X is already initialised or
6577 * if we already know we don't want it */
6578 if(x_initialised == NO) {
6579 if (string_to_bool(value)) {
6580 output_methods &= TO_X;
6582 output_methods &= ~TO_X;
6583 x_initialised = NEVER;
6588 if (!value || x_initialised == YES) {
6593 disp = strdup(value);
6597 if (window.type == TYPE_DOCK)
6600 int a = string_to_alignment(value);
6611 CONF("background") {
6612 fork_to_background = string_to_bool(value);
6615 CONF2("background") {
6616 fork_to_background = string_to_bool(value);
6620 CONF("show_graph_scale") {
6621 show_graph_scale = string_to_bool(value);
6623 CONF("show_graph_range") {
6624 show_graph_range = string_to_bool(value);
6626 CONF("border_margin") {
6628 border_margin = strtol(value, 0, 0);
6633 CONF("border_width") {
6635 border_width = strtol(value, 0, 0);
6640 #define TEMPLATE_CONF(n) \
6641 CONF("template"#n) { \
6643 free(template[n]); \
6644 template[n] = strdup(value); \
6662 info.mail = parse_mail_args(IMAP_TYPE, value);
6669 info.mail = parse_mail_args(POP3_TYPE, value);
6674 CONF("default_bar_size") {
6677 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
6684 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
6687 CONF("default_graph_size") {
6690 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
6697 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
6700 CONF("default_gauge_size") {
6703 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
6710 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
6716 mpd_set_host(value);
6722 if (value && mpd_set_port(value)) {
6726 CONF("mpd_password") {
6728 mpd_set_password(value);
6734 CONF("music_player_interval") {
6736 info.music_player_interval = strtod(value, 0);
6742 CONF("sensor_device") {
6744 sensor_device = strtol(value, 0, 0);
6750 CONF("cpu_avg_samples") {
6752 cpu_avg_samples = strtol(value, 0, 0);
6753 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
6756 info.cpu_avg_samples = cpu_avg_samples;
6762 CONF("net_avg_samples") {
6764 net_avg_samples = strtol(value, 0, 0);
6765 if (net_avg_samples < 1 || net_avg_samples > 14) {
6768 info.net_avg_samples = net_avg_samples;
6776 CONF("double_buffer") {
6777 use_xdbe = string_to_bool(value);
6781 CONF("override_utf8_locale") {
6782 utf8_mode = string_to_bool(value);
6784 CONF("draw_borders") {
6785 draw_borders = string_to_bool(value);
6787 CONF("draw_graph_borders") {
6788 draw_graph_borders = string_to_bool(value);
6790 CONF("draw_shades") {
6791 draw_shades = string_to_bool(value);
6793 CONF("draw_outline") {
6794 draw_outline = string_to_bool(value);
6797 CONF("out_to_console") {
6798 if(string_to_bool(value))
6799 output_methods |= TO_STDOUT;
6801 CONF("out_to_stderr") {
6802 if(string_to_bool(value))
6803 output_methods |= TO_STDERR;
6805 CONF("overwrite_file") {
6806 if(overwrite_file) {
6807 free(overwrite_file);
6810 if(overwrite_works(value)) {
6811 overwrite_file = strdup(value);
6812 output_methods |= OVERWRITE_FILE;
6814 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
6816 CONF("append_file") {
6821 if(append_works(value)) {
6822 append_file = strdup(value);
6823 output_methods |= APPEND_FILE;
6825 ERR("append_file won't be able to create/append '%s'", value);
6827 CONF("use_spacer") {
6829 if (strcasecmp(value, "left") == EQUAL) {
6830 use_spacer = LEFT_SPACER;
6831 } else if (strcasecmp(value, "right") == EQUAL) {
6832 use_spacer = RIGHT_SPACER;
6833 } else if (strcasecmp(value, "none") == EQUAL) {
6834 use_spacer = NO_SPACER;
6836 use_spacer = string_to_bool(value);
6837 ERR("use_spacer should have an argument of left, right, or"
6838 " none. '%s' seems to be some form of '%s', so"
6839 " defaulting to %s.", value,
6840 use_spacer ? "true" : "false",
6841 use_spacer ? "right" : "none");
6843 use_spacer = RIGHT_SPACER;
6845 use_spacer = NO_SPACER;
6849 ERR("use_spacer should have an argument. Defaulting to right.");
6850 use_spacer = RIGHT_SPACER;
6856 use_xft = string_to_bool(value);
6860 set_first_font(value);
6864 if (value && font_count >= 0) {
6865 fonts[0].font_alpha = atof(value) * 65535.0;
6872 if (string_to_bool(value)) {
6873 ERR("Xft not enabled");
6877 /* xftfont silently ignored when no Xft */
6880 /* xftalpha is silently ignored when no Xft */
6885 set_first_font(value);
6893 gap_x = atoi(value);
6900 gap_y = atoi(value);
6906 CONF("mail_spool") {
6910 variable_substitute(value, buffer, 256);
6912 if (buffer[0] != '\0') {
6913 if (current_mail_spool) {
6914 free(current_mail_spool);
6916 current_mail_spool = strndup(buffer, text_buffer_size);
6923 CONF("minimum_size") {
6925 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
6927 if (sscanf(value, "%d", &minimum_width) != 1) {
6935 CONF("maximum_width") {
6937 if (sscanf(value, "%d", &maximum_width) != 1) {
6945 CONF("no_buffers") {
6946 no_buffers = string_to_bool(value);
6948 CONF("top_cpu_separate") {
6949 cpu_separate = string_to_bool(value);
6951 CONF("short_units") {
6952 short_units = string_to_bool(value);
6954 CONF("pad_percents") {
6955 pad_percents = atoi(value);
6959 CONF("own_window") {
6961 own_window = string_to_bool(value);
6964 CONF("own_window_class") {
6966 memset(window.class_name, 0, sizeof(window.class_name));
6967 strncpy(window.class_name, value,
6968 sizeof(window.class_name) - 1);
6971 CONF("own_window_title") {
6973 memset(window.title, 0, sizeof(window.title));
6974 strncpy(window.title, value, sizeof(window.title) - 1);
6977 CONF("own_window_transparent") {
6979 set_transparent = string_to_bool(value);
6982 CONF("own_window_hints") {
6984 char *p_hint, *p_save;
6985 char delim[] = ", ";
6987 /* tokenize the value into individual hints */
6988 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
6990 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
6991 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
6992 SET_HINT(window.hints, HINT_UNDECORATED);
6993 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
6994 SET_HINT(window.hints, HINT_BELOW);
6995 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
6996 SET_HINT(window.hints, HINT_ABOVE);
6997 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
6998 SET_HINT(window.hints, HINT_STICKY);
6999 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
7000 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
7001 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
7002 SET_HINT(window.hints, HINT_SKIP_PAGER);
7007 p_hint = strtok_r(NULL, delim, &p_save);
7008 } while (p_hint != NULL);
7014 CONF("own_window_type") {
7016 if (strncmp(value, "normal", 6) == EQUAL) {
7017 window.type = TYPE_NORMAL;
7018 } else if (strncmp(value, "desktop", 7) == EQUAL) {
7019 window.type = TYPE_DESKTOP;
7020 } else if (strncmp(value, "dock", 7) == EQUAL) {
7021 window.type = TYPE_DOCK;
7022 text_alignment = TOP_LEFT;
7023 } else if (strncmp(value, "override", 8) == EQUAL) {
7024 window.type = TYPE_OVERRIDE;
7033 CONF("stippled_borders") {
7035 stippled_borders = strtol(value, 0, 0);
7037 stippled_borders = 4;
7042 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
7046 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
7049 CONF("update_interval") {
7051 update_interval = strtod(value, 0);
7055 if (info.music_player_interval == 0) {
7056 // default to update_interval
7057 info.music_player_interval = update_interval;
7060 CONF("total_run_times") {
7062 total_run_times = strtod(value, 0);
7068 stuff_in_upper_case = string_to_bool(value);
7070 CONF("max_specials") {
7072 max_specials = atoi(value);
7077 CONF("max_user_text") {
7079 max_user_text = atoi(value);
7084 CONF("text_buffer_size") {
7086 text_buffer_size = atoi(value);
7087 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
7088 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
7089 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
7101 global_text = (char *) malloc(1);
7102 global_text[0] = '\0';
7105 unsigned int l = strlen(global_text);
7108 if (fgets(buf, 256, fp) == NULL) {
7114 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
7122 /* Check for continuation of \\-\n. */
7123 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
7124 global_text[l-1] = '\0';
7128 global_text = (char *) realloc(global_text, l + bl + 1);
7129 strcat(global_text, buf);
7131 if (strlen(global_text) > max_user_text) {
7136 if (strlen(global_text) < 1) {
7137 CRIT_ERR("no text supplied in configuration; exiting");
7139 global_text_lines = line + 1;
7142 #ifdef TCP_PORT_MONITOR
7143 CONF("max_port_monitor_connections") {
7145 if (!value || (sscanf(value, "%d", &max) != 1)) {
7146 /* an error. use default, warn and continue. */
7147 tcp_portmon_set_max_connections(0);
7149 } else if (tcp_portmon_set_max_connections(max)) {
7150 /* max is < 0, default has been set*/
7155 CONF("if_up_strictness") {
7157 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7158 ifup_strictness = IFUP_UP;
7159 } else if (strcasecmp(value, "up") == EQUAL) {
7160 ifup_strictness = IFUP_UP;
7161 } else if (strcasecmp(value, "link") == EQUAL) {
7162 ifup_strictness = IFUP_LINK;
7163 } else if (strcasecmp(value, "address") == EQUAL) {
7164 ifup_strictness = IFUP_ADDR;
7166 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7167 ifup_strictness = IFUP_UP;
7171 CONF("temperature_unit") {
7173 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
7174 } else if (set_temp_output_unit(value)) {
7175 ERR("temperature_unit: incorrect argument");
7181 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
7182 char *skey = malloc(maxlength);
7183 char *svalue = malloc(maxlength);
7185 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
7186 oldvalue = getenv(skey);
7187 if (oldvalue == NULL) {
7188 setenv(skey, svalue, 0);
7190 //PS: Don't free oldvalue, it's the real envvar, not a copy
7211 CONF("default_color"){}
7212 CONF3("default_shade_color", "default_shadecolor"){}
7213 CONF3("default_outline_color", "default_outlinecolor") {}
7214 CONF("own_window_colour") {}
7217 ERR("%s: %d: no such configuration: '%s'", f, line, name);
7223 if (info.music_player_interval == 0) {
7224 // default to update_interval
7225 info.music_player_interval = update_interval;
7227 if (!global_text) { // didn't supply any text
7228 CRIT_ERR("missing text block in configuration; exiting");
7232 static void load_config_file_x11(const char *f)
7237 #ifdef CONFIG_OUTPUT
7238 if (!strcmp(f, "==builtin==")) {
7239 #ifdef HAVE_FOPENCOOKIE
7240 fp = fopencookie(NULL, "r", conf_cookie);
7243 #endif /* CONFIG_OUTPUT */
7249 DBGP("reading contents from config file '%s'", f);
7252 char buf[256], *p, *p2, *name, *value;
7255 if (fgets(buf, 256, fp) == NULL) {
7261 /* break at comment */
7262 p2 = strchr(p, '#');
7268 while (*p && isspace((int) *p)) {
7272 continue; /* empty line */
7279 while (*p2 && !isspace((int) *p2)) {
7283 *p2 = '\0'; /* break at name's end */
7290 while (*p && isspace((int) *p)) {
7296 p2 = value + strlen(value);
7297 while (isspace((int) *(p2 - 1))) {
7306 X11_initialisation();
7307 if (x_initialised == YES) {
7309 color0 = get_x11_color(value);
7316 X11_initialisation();
7317 if (x_initialised == YES) {
7319 color1 = get_x11_color(value);
7326 X11_initialisation();
7327 if (x_initialised == YES) {
7329 color2 = get_x11_color(value);
7336 X11_initialisation();
7337 if (x_initialised == YES) {
7339 color3 = get_x11_color(value);
7346 X11_initialisation();
7347 if (x_initialised == YES) {
7349 color4 = get_x11_color(value);
7356 X11_initialisation();
7357 if (x_initialised == YES) {
7359 color5 = get_x11_color(value);
7366 X11_initialisation();
7367 if (x_initialised == YES) {
7369 color6 = get_x11_color(value);
7376 X11_initialisation();
7377 if (x_initialised == YES) {
7379 color7 = get_x11_color(value);
7386 X11_initialisation();
7387 if (x_initialised == YES) {
7389 color8 = get_x11_color(value);
7396 X11_initialisation();
7397 if (x_initialised == YES) {
7399 color9 = get_x11_color(value);
7405 CONF("default_color") {
7406 X11_initialisation();
7407 if (x_initialised == YES) {
7409 default_fg_color = get_x11_color(value);
7415 CONF3("default_shade_color", "default_shadecolor") {
7416 X11_initialisation();
7417 if (x_initialised == YES) {
7419 default_bg_color = get_x11_color(value);
7425 CONF3("default_outline_color", "default_outlinecolor") {
7426 X11_initialisation();
7427 if (x_initialised == YES) {
7429 default_out_color = get_x11_color(value);
7436 CONF("own_window_colour") {
7437 X11_initialisation();
7438 if (x_initialised == YES) {
7440 background_colour = get_x11_color(value);
7442 ERR("Invalid colour for own_window_colour (try omitting the "
7443 "'#' for hex colours");
7451 //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
7452 if(output_methods & TO_X) {
7453 X11_initialisation();
7466 static void print_help(const char *prog_name) {
7467 printf("Usage: %s [OPTION]...\n"
7468 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
7469 "window. Command line options will override configurations defined in config\n"
7471 " -v, --version version\n"
7472 " -q, --quiet quiet mode\n"
7473 " -D, --debug increase debugging output\n"
7474 " -c, --config=FILE config file to load\n"
7475 #ifdef CONFIG_OUTPUT
7476 " -C, --print-config print the builtin default config to stdout\n"
7477 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
7479 " -d, --daemonize daemonize, fork to background\n"
7480 " -h, --help help\n"
7482 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
7483 " -f, --font=FONT font to use\n"
7485 " -o, --own-window create own window to draw\n"
7488 " -b, --double-buffer double buffer (prevents flickering)\n"
7490 " -w, --window-id=WIN_ID window id to draw\n"
7491 " -x X x position\n"
7492 " -y Y y position\n"
7494 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
7495 " -u, --interval=SECS update interval\n"
7496 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
7501 /* : means that character before that takes an argument */
7502 static const char *getopt_string = "vVqdDt:u:i:hc:"
7512 #ifdef CONFIG_OUTPUT
7517 static const struct option longopts[] = {
7518 { "help", 0, NULL, 'h' },
7519 { "version", 0, NULL, 'V' },
7520 { "debug", 0, NULL, 'D' },
7521 { "config", 1, NULL, 'c' },
7522 #ifdef CONFIG_OUTPUT
7523 { "print-config", 0, NULL, 'C' },
7525 { "daemonize", 0, NULL, 'd' },
7527 { "alignment", 1, NULL, 'a' },
7528 { "font", 1, NULL, 'f' },
7529 { "display", 1, NULL, 'X' },
7531 { "own-window", 0, NULL, 'o' },
7534 { "double-buffer", 0, NULL, 'b' },
7536 { "window-id", 1, NULL, 'w' },
7538 { "text", 1, NULL, 't' },
7539 { "interval", 0, NULL, 'u' },
7543 int main(int argc, char **argv)
7549 struct sigaction act, oact;
7551 g_signal_pending = 0;
7552 memset(&info, 0, sizeof(info));
7555 #ifdef TCP_PORT_MONITOR
7556 /* set default connection limit */
7557 tcp_portmon_set_max_connections(0);
7560 /* handle command line parameters that don't change configs */
7562 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
7563 || ((s = getenv("LANG")) && *s)) {
7564 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
7566 ERR("malloc failed");
7568 for (x = 0; x < strlen(s); x++) {
7569 temp[x] = tolower(s[x]);
7572 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
7578 if (!setlocale(LC_CTYPE, "")) {
7579 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
7583 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
7594 if (current_config) {
7595 free(current_config);
7597 current_config = strndup(optarg, max_user_text);
7600 freopen("/dev/null", "w", stderr);
7603 print_help(argv[0]);
7605 #ifdef CONFIG_OUTPUT
7612 window.window = strtol(optarg, 0, 0);
7621 /* check if specified config file is valid */
7622 if (current_config) {
7624 if (stat(current_config, &sb) ||
7625 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
7626 ERR("invalid configuration file '%s'\n", current_config);
7627 free(current_config);
7632 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
7634 if (!current_config) {
7635 /* load default config file */
7639 /* Try to use personal config file first */
7640 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
7641 if (buf[0] && (fp = fopen(buf, "r"))) {
7642 current_config = strndup(buf, max_user_text);
7646 /* Try to use system config file if personal config not readable */
7647 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
7648 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
7652 /* No readable config found */
7653 if (!current_config) {
7654 #ifdef CONFIG_OUTPUT
7655 current_config = strdup("==builtin==");
7656 ERR("no readable personal or system-wide config file found,"
7657 " using builtin default");
7659 CRIT_ERR("no readable personal or system-wide config file found");
7660 #endif /* ! CONF_OUTPUT */
7664 load_config_file(current_config);
7666 /* init specials array */
7667 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
7668 ERR("failed to create specials array");
7672 if (current_mail_spool == NULL) {
7675 variable_substitute(MAIL_FILE, buf, 256);
7677 if (buf[0] != '\0') {
7678 current_mail_spool = strndup(buf, text_buffer_size);
7683 /* handle other command line arguments */
7685 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
7686 || defined(__NetBSD__)
7687 optind = optreset = 1;
7692 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
7693 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
7694 "kvm_open")) == NULL) {
7695 CRIT_ERR("cannot read kvm");
7700 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
7708 fork_to_background = 1;
7711 global_debug_level++;
7715 set_first_font(optarg);
7718 text_alignment = string_to_alignment(optarg);
7723 disp = strdup(optarg);
7742 global_text = strndup(optarg, max_user_text);
7743 convert_escapes(global_text);
7747 update_interval = strtod(optarg, 0);
7748 if (info.music_player_interval == 0) {
7749 // default to update_interval
7750 info.music_player_interval = update_interval;
7755 total_run_times = strtod(optarg, 0);
7759 gap_x = atoi(optarg);
7763 gap_y = atoi(optarg);
7774 if (output_methods & TO_X) {
7775 load_config_file_x11(current_config);
7780 /* generate text and get initial size */
7781 extract_variable_text(global_text);
7788 if (fork_to_background) {
7793 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
7800 fprintf(stderr, "\n");
7805 /* parent process */
7806 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
7813 text_buffer = malloc(max_user_text);
7814 memset(text_buffer, 0, max_user_text);
7815 tmpstring1 = malloc(text_buffer_size);
7816 memset(tmpstring1, 0, text_buffer_size);
7817 tmpstring2 = malloc(text_buffer_size);
7818 memset(tmpstring2, 0, text_buffer_size);
7821 if (output_methods & TO_X) {
7823 update_text_area(); /* to get initial size of the window */
7826 init_window(own_window, text_width + border_margin * 2 + 1,
7827 text_height + border_margin * 2 + 1, set_transparent, background_colour,
7829 #else /* OWN_WINDOW */
7830 init_window(0, text_width + border_margin * 2 + 1,
7831 text_height + border_margin * 2 + 1, set_transparent, 0,
7833 #endif /* OWN_WINDOW */
7836 update_text_area(); /* to position text/window on screen */
7839 if (own_window && !fixed_pos) {
7840 XMoveWindow(display, window.window, window.x, window.y);
7843 set_transparent_background(window.window);
7854 /* Set signal handlers */
7855 act.sa_handler = signal_handler;
7856 sigemptyset(&act.sa_mask);
7859 act.sa_flags |= SA_RESTART;
7862 if ( sigaction(SIGINT, &act, &oact) < 0
7863 || sigaction(SIGUSR1, &act, &oact) < 0
7864 || sigaction(SIGHUP, &act, &oact) < 0
7865 || sigaction(SIGTERM, &act, &oact) < 0) {
7866 ERR("error setting signal handler: %s", strerror(errno));
7871 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
7878 static void signal_handler(int sig)
7880 /* signal handler is light as a feather, as it should be.
7881 * we will poll g_signal_pending with each loop of conky
7882 * and do any signal processing there, NOT here. */
7883 g_signal_pending = sig;