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;
134 static unsigned int top_name_width = 15;
136 enum x_initialiser_state x_initialised = NO;
137 static volatile int g_signal_pending;
138 /* Update interval */
139 double update_interval;
142 /* prototypes for internally used functions */
143 static void signal_handler(int);
144 static void print_version(void) __attribute__((noreturn));
145 static void reload_config(void);
146 static void clean_up(void);
147 static void generate_text_internal(char *, int, struct text_object,
148 struct information *);
149 static int extract_variable_text_internal(struct text_object *,
152 static void print_version(void)
154 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
156 printf("\nCompiled in features:\n\n"
157 "System config file: "SYSTEM_CONFIG_FILE"\n\n"
161 " * Xdamage extension\n"
162 # endif /* HAVE_XDAMAGE */
164 " * XDBE (double buffer extension)\n"
165 # endif /* HAVE_XDBE */
170 "\n Music detection:\n"
173 #endif /* AUDACIOUS */
193 #ifdef TCP_PORT_MONITOR
195 #endif /* TCP_PORT_MONITOR */
201 #endif /* HAVE_IWLIB */
203 " * support for IBM/Lenovo notebooks\n"
213 #endif /* CONFIG_OUTPUT */
215 " * ALSA mixer support\n"
216 #endif /* MIXER_IS_ALSA */
222 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
229 static int text_start_x, text_start_y; /* text start position in window */
230 static int text_width, text_height;
245 /* display to connect to */
246 static char *disp = NULL;
250 /* struct that has all info to be shared between
251 * instances of the same text object */
252 struct information info;
254 /* default config file */
255 static char *current_config;
257 /* set to 1 if you want all text to be in uppercase */
258 static unsigned int stuff_in_upper_case;
260 /* Run how many times? */
261 static unsigned long total_run_times;
264 static int fork_to_background;
266 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
268 /* filenames for output */
269 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
270 char *append_file = NULL; FILE *append_fpointer = NULL;
274 static int show_graph_scale;
275 static int show_graph_range;
277 /* Position on the screen */
278 static int text_alignment;
279 static int gap_x, gap_y;
282 static int draw_borders;
283 static int draw_graph_borders;
284 static int stippled_borders;
286 static int draw_shades, draw_outline;
288 static int border_margin, border_width;
290 static long default_fg_color, default_bg_color, default_out_color;
292 /* create own window or draw stuff to root? */
293 static int set_transparent = 0;
296 static int own_window = 0;
297 static int background_colour = 0;
299 /* fixed size/pos is set if wm/user changes them */
300 static int fixed_size = 0, fixed_pos = 0;
303 static int minimum_width, minimum_height;
304 static int maximum_width;
309 static int sensor_device;
312 static long color0, color1, color2, color3, color4, color5, color6, color7,
315 static char *template[10];
317 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
318 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
320 /* maximum size of individual text buffers, ie $exec buffer size */
321 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
324 #define CODEPAGE_LENGTH 20
326 long iconv_count = 0;
327 char iconv_converting;
328 static iconv_t **iconv_cd = 0;
330 int register_iconv(iconv_t *new_iconv)
332 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
334 CRIT_ERR("Out of memory");
336 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
337 if (!iconv_cd[iconv_count]) {
338 CRIT_ERR("Out of memory");
340 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
345 void free_iconv(void)
350 for (i = 0; i < iconv_count; i++) {
352 iconv_close(*iconv_cd[i]);
366 /* no buffers in used memory? */
369 /* pad percentages to decimals? */
370 static int pad_percents = 0;
372 static char *global_text = 0;
373 long global_text_lines;
375 static int total_updates;
376 static int updatereset;
378 int check_contains(char *f, char *s)
381 FILE *where = fopen(f, "r");
384 char buf1[256], buf2[256];
386 while (fgets(buf1, 256, where)) {
387 sscanf(buf1, "%255s", buf2);
388 if (strstr(buf2, s)) {
395 ERR("Could not open the file");
401 static inline int calc_text_width(const char *s, int l)
403 if ((output_methods & TO_X) == 0)
410 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
411 (const FcChar8 *) s, l, &gi);
413 XftTextExtents8(display, fonts[selected_font].xftfont,
414 (const FcChar8 *) s, l, &gi);
420 return XTextWidth(fonts[selected_font].font, s, l);
425 /* formatted text to render on screen, generated in generate_text(),
426 * drawn in draw_stuff() */
428 static char *text_buffer;
431 static unsigned int special_index; /* used when drawing */
434 /* quite boring functions */
436 static inline void for_each_line(char *b, void f(char *))
440 for (ps = b, pe = b; *pe; pe++) {
454 static void convert_escapes(char *buf)
456 char *p = buf, *s = buf;
463 } else if (*s == '\\') {
474 /* Prints anything normally printed with snprintf according to the current value
475 * of use_spacer. Actually slightly more flexible than snprintf, as you can
476 * safely specify the destination buffer as one of your inputs. */
477 int spaced_print(char *buf, int size, const char *format, int width, ...)
486 tempbuf = malloc(size * sizeof(char));
488 // Passes the varargs along to vsnprintf
489 va_start(argp, width);
490 vsnprintf(tempbuf, size, format, argp);
493 switch (use_spacer) {
495 len = snprintf(buf, size, "%s", tempbuf);
498 len = snprintf(buf, size, "%*s", width, tempbuf);
501 len = snprintf(buf, size, "%-*s", width, tempbuf);
508 /* print percentage values
510 * - i.e., unsigned values between 0 and 100
511 * - respect the value of pad_percents */
512 static int percent_print(char *buf, int size, unsigned value)
514 return spaced_print(buf, size, "%u", pad_percents, value);
517 /* converts from bytes to human readable format (K, M, G, T)
519 * The algorithm always divides by 1024, as unit-conversion of byte
520 * counts suggests. But for output length determination we need to
521 * compare with 1000 here, as we print in decimal form. */
522 static void human_readable(long long num, char *buf, int size)
524 const char **suffix = suffixes;
538 if (llabs(num) < 1000LL) {
539 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
543 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
551 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
553 * The goal is to always have a significance of 3, by
554 * adjusting the decimal part of the number. Sample output:
558 * so the point of alignment resides between number and unit. The
559 * upside of this is that there is minimal padding necessary, though
560 * there should be a way to make alignment take place at the decimal
561 * dot (then with fixed width decimal part). * */
563 precision = 0; /* print 100-999 without decimal part */
565 precision = 1; /* print 10-99 with one decimal place */
567 precision = 2; /* print 0-9 with two decimal place */
569 spaced_print(buf, size, format, width, precision, fnum, *suffix);
572 /* global object list root element */
573 static struct text_object global_root_object;
575 static inline void read_exec(const char *data, char *buf, const int size)
577 FILE *fp = popen(data, "r");
578 int length = fread(buf, 1, size, fp);
582 if (length > 0 && buf[length - 1] == '\n') {
583 buf[length - 1] = '\0';
587 void *threaded_exec(void *) __attribute__((noreturn));
589 void *threaded_exec(void *arg)
592 struct text_object *obj = (struct text_object *)arg;
595 buff = malloc(text_buffer_size);
596 read_exec(obj->data.texeci.cmd, buff,
605 timed_thread_lock(obj->data.texeci.p_timed_thread);
606 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
607 timed_thread_unlock(obj->data.texeci.p_timed_thread);
609 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
610 timed_thread_exit(obj->data.texeci.p_timed_thread);
616 static struct text_object *new_text_object_internal(void)
618 struct text_object *obj = malloc(sizeof(struct text_object));
619 memset(obj, 0, sizeof(struct text_object));
623 /* free the list of text objects root points to */
624 static void free_text_objects(struct text_object *root)
626 struct text_object *obj;
632 #define data obj->data
633 for (obj = root->prev; obj; obj = root->prev) {
634 root->prev = obj->prev;
640 #endif /* !__OpenBSD__ */
645 close(data.sysfs.fd);
647 #endif /* __linux__ */
653 free(data.tztime.tz);
654 free(data.tztime.fmt);
657 free(data.mboxscan.args);
658 free(data.mboxscan.output);
663 case OBJ_unseen_mails:
664 case OBJ_flagged_mails:
665 case OBJ_unflagged_mails:
666 case OBJ_forwarded_mails:
667 case OBJ_unforwarded_mails:
668 case OBJ_replied_mails:
669 case OBJ_unreplied_mails:
670 case OBJ_draft_mails:
671 case OBJ_trashed_mails:
672 free(data.local_mail.box);
674 case OBJ_imap_unseen:
675 if (!obj->global_mode) {
679 case OBJ_imap_messages:
680 if (!obj->global_mode) {
684 case OBJ_pop3_unseen:
685 if (!obj->global_mode) {
690 if (!obj->global_mode) {
696 free_text_objects(obj->sub);
699 case OBJ_if_existing:
702 free(data.ifblock.s);
703 free(data.ifblock.str);
706 free(data.tail.logfile);
707 free(data.tail.buffer);
721 case OBJ_iconv_start:
726 case OBJ_disk_protect:
730 free(data.ifblock.s);
731 free(data.ifblock.str);
734 if (info.gw_info.iface) {
735 free(info.gw_info.iface);
736 info.gw_info.iface = 0;
738 if (info.gw_info.ip) {
739 free(info.gw_info.ip);
743 case OBJ_ioscheduler:
748 #if (defined(__FreeBSD__) || defined(__linux__))
750 free(data.ifblock.s);
751 free(data.ifblock.str);
754 case OBJ_xmms2_artist:
755 if (info.xmms2.artist) {
756 free(info.xmms2.artist);
757 info.xmms2.artist = 0;
760 case OBJ_xmms2_album:
761 if (info.xmms2.album) {
762 free(info.xmms2.album);
763 info.xmms2.album = 0;
766 case OBJ_xmms2_title:
767 if (info.xmms2.title) {
768 free(info.xmms2.title);
769 info.xmms2.title = 0;
772 case OBJ_xmms2_genre:
773 if (info.xmms2.genre) {
774 free(info.xmms2.genre);
775 info.xmms2.genre = 0;
778 case OBJ_xmms2_comment:
779 if (info.xmms2.comment) {
780 free(info.xmms2.comment);
781 info.xmms2.comment = 0;
785 if (info.xmms2.url) {
786 free(info.xmms2.url);
791 if (info.xmms2.date) {
792 free(info.xmms2.date);
796 case OBJ_xmms2_status:
797 if (info.xmms2.status) {
798 free(info.xmms2.status);
799 info.xmms2.status = 0;
802 case OBJ_xmms2_playlist:
803 if (info.xmms2.playlist) {
804 free(info.xmms2.playlist);
805 info.xmms2.playlist = 0;
808 case OBJ_xmms2_smart:
809 if (info.xmms2.artist) {
810 free(info.xmms2.artist);
811 info.xmms2.artist = 0;
813 if (info.xmms2.title) {
814 free(info.xmms2.title);
815 info.xmms2.title = 0;
817 if (info.xmms2.url) {
818 free(info.xmms2.url);
825 case OBJ_bmpx_artist:
829 case OBJ_bmpx_bitrate:
839 free(data.rss.action);
848 case OBJ_battery_short:
851 case OBJ_battery_time:
854 #endif /* !__OpenBSD__ */
860 free(data.execi.cmd);
861 free(data.execi.buffer);
864 free(data.texeci.cmd);
865 free(data.texeci.buffer);
873 if (info.first_process) {
874 free_all_processes();
875 info.first_process = NULL;
880 free(data.hddtemp.dev);
881 free(data.hddtemp.addr);
882 if (data.hddtemp.temp)
883 free(data.hddtemp.temp);
886 case OBJ_entropy_avail:
887 case OBJ_entropy_poolsize:
888 case OBJ_entropy_bar:
891 if (info.users.names) {
892 free(info.users.names);
893 info.users.names = 0;
897 if (info.users.terms) {
898 free(info.users.terms);
899 info.users.terms = 0;
903 if (info.users.times) {
904 free(info.users.times);
905 info.users.times = 0;
910 case OBJ_smapi_bat_perc:
911 case OBJ_smapi_bat_temp:
912 case OBJ_smapi_bat_power:
915 case OBJ_if_smapi_bat_installed:
916 free(data.ifblock.s);
917 free(data.ifblock.str);
931 case OBJ_mpd_bitrate:
934 case OBJ_mpd_elapsed:
939 case OBJ_mpd_percent:
941 case OBJ_if_mpd_playing:
952 case OBJ_moc_totaltime:
953 case OBJ_moc_timeleft:
954 case OBJ_moc_curtime:
955 case OBJ_moc_bitrate:
961 free(data.scroll.text);
962 free_text_objects(obj->sub);
966 free(data.combine.left);
967 free(data.combine.seperation);
968 free(data.combine.right);
969 free_text_objects(obj->sub);
978 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
983 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
984 *a = mixer_init(buf1);
985 scan_bar(arg + n, w, h);
987 *a = mixer_init(NULL);
992 /* strip a leading /dev/ if any, following symlinks first
994 * BEWARE: this function returns a pointer to static content
995 * which gets overwritten in consecutive calls. I.e.:
996 * this function is NOT reentrant.
998 static const char *dev_name(const char *path)
1000 static char buf[255]; /* should be enough for pathnames */
1006 #define DEV_NAME(x) \
1007 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1008 if ((buflen = readlink(path, buf, 254)) == -1)
1009 return DEV_NAME(path);
1011 return DEV_NAME(buf);
1015 /* construct_text_object() creates a new text_object */
1016 static struct text_object *construct_text_object(const char *s,
1017 const char *arg, long line, char allow_threaded, void **ifblock_opaque)
1019 // struct text_object *obj = new_text_object();
1020 struct text_object *obj = new_text_object_internal();
1024 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1025 obj->type = OBJ_##a; need_mask |= (1 << n); {
1026 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1027 obj->type = OBJ_##a; need_mask |= (1 << n); \
1028 obj_be_ifblock_if(ifblock_opaque, obj); {
1029 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1030 obj->type = OBJ_##a; need_mask |= (1 << n); {
1031 #define END } } else
1035 obj->type = OBJ_color;
1036 obj->data.l = get_x11_color(s);
1040 OBJ(freq, INFO_FREQ)
1043 obj->data.i = open_acpi_temperature(arg);
1044 END OBJ(acpiacadapter, 0)
1045 END OBJ(freq, INFO_FREQ)
1046 #endif /* !__OpenBSD__ */
1048 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1049 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1050 obj->data.cpu_index = 1;
1051 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1052 "Displaying the clock for CPU 1."); */
1054 obj->data.cpu_index = atoi(&arg[0]);
1057 END OBJ(freq_g, INFO_FREQ)
1059 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1060 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1061 obj->data.cpu_index = 1;
1062 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1063 "CPUs! Displaying the clock for CPU 1."); */
1065 obj->data.cpu_index = atoi(&arg[0]);
1068 #if defined(__linux__)
1069 END OBJ(voltage_mv, 0)
1071 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1072 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1073 obj->data.cpu_index = 1;
1074 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1075 "CPUs! Displaying voltage for CPU 1."); */
1077 obj->data.cpu_index = atoi(&arg[0]);
1080 END OBJ(voltage_v, 0)
1082 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1083 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1084 obj->data.cpu_index = 1;
1085 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1086 "CPUs! Displaying voltage for CPU 1."); */
1088 obj->data.cpu_index = atoi(&arg[0]);
1093 END OBJ(wireless_essid, INFO_NET)
1095 obj->data.net = get_net_stat(arg);
1097 CRIT_ERR("wireless_essid: needs an argument");
1099 END OBJ(wireless_mode, INFO_NET)
1101 obj->data.net = get_net_stat(arg);
1103 CRIT_ERR("wireless_mode: needs an argument");
1105 END OBJ(wireless_bitrate, INFO_NET)
1107 obj->data.net = get_net_stat(arg);
1109 CRIT_ERR("wireless_bitrate: needs an argument");
1111 END OBJ(wireless_ap, INFO_NET)
1113 obj->data.net = get_net_stat(arg);
1115 CRIT_ERR("wireless_ap: needs an argument");
1117 END OBJ(wireless_link_qual, INFO_NET)
1119 obj->data.net = get_net_stat(arg);
1121 CRIT_ERR("wireless_link_qual: needs an argument");
1123 END OBJ(wireless_link_qual_max, INFO_NET)
1125 obj->data.net = get_net_stat(arg);
1127 CRIT_ERR("wireless_link_qual_max: needs an argument");
1129 END OBJ(wireless_link_qual_perc, INFO_NET)
1131 obj->data.net = get_net_stat(arg);
1133 CRIT_ERR("wireless_link_qual_perc: needs an argument");
1135 END OBJ(wireless_link_bar, INFO_NET)
1137 arg = scan_bar(arg, &obj->a, &obj->b);
1138 obj->data.net = get_net_stat(arg);
1140 CRIT_ERR("wireless_link_bar: needs an argument");
1142 #endif /* HAVE_IWLIB */
1144 #endif /* __linux__ */
1152 sscanf(arg, "%63s", bat);
1154 strcpy(bat, "BAT0");
1156 obj->data.s = strndup(bat, text_buffer_size);
1157 END OBJ(battery_short, 0)
1161 sscanf(arg, "%63s", bat);
1163 strcpy(bat, "BAT0");
1165 obj->data.s = strndup(bat, text_buffer_size);
1166 END OBJ(battery_time, 0)
1170 sscanf(arg, "%63s", bat);
1172 strcpy(bat, "BAT0");
1174 obj->data.s = strndup(bat, text_buffer_size);
1175 END OBJ(battery_percent, 0)
1179 sscanf(arg, "%63s", bat);
1181 strcpy(bat, "BAT0");
1183 obj->data.s = strndup(bat, text_buffer_size);
1184 END OBJ(battery_bar, 0)
1188 arg = scan_bar(arg, &obj->a, &obj->b);
1189 sscanf(arg, "%63s", bat);
1191 strcpy(bat, "BAT0");
1193 obj->data.s = strndup(bat, text_buffer_size);
1194 #endif /* !__OpenBSD__ */
1196 #if defined(__linux__)
1197 END OBJ(disk_protect, 0)
1199 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1201 CRIT_ERR("disk_protect needs an argument");
1202 END OBJ(i8k_version, INFO_I8K)
1203 END OBJ(i8k_bios, INFO_I8K)
1204 END OBJ(i8k_serial, INFO_I8K)
1205 END OBJ(i8k_cpu_temp, INFO_I8K)
1206 END OBJ(i8k_left_fan_status, INFO_I8K)
1207 END OBJ(i8k_right_fan_status, INFO_I8K)
1208 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1209 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1210 END OBJ(i8k_ac_status, INFO_I8K)
1211 END OBJ(i8k_buttons_status, INFO_I8K)
1214 END OBJ(ibm_temps, 0)
1216 CRIT_ERR("ibm_temps: needs an argument");
1218 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1219 obj->data.sensor = 0;
1220 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1221 "Using 0 (CPU temp sensor).");
1223 obj->data.sensor = atoi(&arg[0]);
1224 END OBJ(ibm_volume, 0)
1225 END OBJ(ibm_brightness, 0)
1227 /* information from sony_laptop kernel module
1228 * /sys/devices/platform/sony-laptop */
1229 END OBJ(sony_fanspeed, 0)
1230 END OBJ_IF(if_gw, 0)
1231 END OBJ(ioscheduler, 0)
1233 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1236 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1237 END OBJ(laptop_mode, 0)
1238 END OBJ(pb_battery, 0)
1239 if (arg && strcmp(arg, "status") == EQUAL) {
1240 obj->data.i = PB_BATT_STATUS;
1241 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1242 obj->data.i = PB_BATT_PERCENT;
1243 } else if (arg && strcmp(arg, "time") == EQUAL) {
1244 obj->data.i = PB_BATT_TIME;
1246 ERR("pb_battery: needs one argument: status, percent or time");
1251 #endif /* __linux__ */
1252 #if (defined(__FreeBSD__) || defined(__linux__))
1253 END OBJ_IF(if_up, 0)
1255 ERR("if_up needs an argument");
1256 obj->data.ifblock.s = 0;
1258 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1260 #if defined(__OpenBSD__)
1261 END OBJ(obsd_sensors_temp, 0)
1263 CRIT_ERR("obsd_sensors_temp: needs an argument");
1265 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1266 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1267 obj->data.sensor = 0;
1268 ERR("Invalid temperature sensor number!");
1270 obj->data.sensor = atoi(&arg[0]);
1271 END OBJ(obsd_sensors_fan, 0)
1273 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1276 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1277 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1278 obj->data.sensor = 0;
1279 ERR("Invalid fan sensor number!");
1281 obj->data.sensor = atoi(&arg[0]);
1282 END OBJ(obsd_sensors_volt, 0)
1284 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1287 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1288 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1289 obj->data.sensor = 0;
1290 ERR("Invalid voltage sensor number!");
1292 obj->data.sensor = atoi(&arg[0]);
1293 END OBJ(obsd_vendor, 0)
1294 END OBJ(obsd_product, 0)
1295 #endif /* __OpenBSD__ */
1296 END OBJ(buffers, INFO_BUFFERS)
1297 END OBJ(cached, INFO_BUFFERS)
1298 #define SCAN_CPU(__arg, __var) { \
1300 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) == 2) \
1301 __arg += __offset; \
1305 END OBJ(cpu, INFO_CPU)
1306 SCAN_CPU(arg, obj->data.cpu_index);
1307 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1308 END OBJ(cpugauge, INFO_CPU)
1309 SCAN_CPU(arg, obj->data.cpu_index);
1310 scan_gauge(arg, &obj->a, &obj->b);
1311 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1312 END OBJ(cpubar, INFO_CPU)
1313 SCAN_CPU(arg, obj->data.cpu_index);
1314 scan_bar(arg, &obj->a, &obj->b);
1315 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1316 END OBJ(cpugraph, INFO_CPU)
1317 SCAN_CPU(arg, obj->data.cpu_index);
1318 scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1319 &obj->e, &obj->showaslog);
1320 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1321 END OBJ(loadgraph, INFO_LOADAVG)
1322 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1323 &obj->e, &obj->showaslog);
1327 r = sscanf(arg, "%d", &a);
1329 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1332 END OBJ(diskio, INFO_DISKIO)
1333 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1334 END OBJ(diskio_read, INFO_DISKIO)
1335 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1336 END OBJ(diskio_write, INFO_DISKIO)
1337 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1338 END OBJ(diskiograph, INFO_DISKIO)
1339 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1340 &obj->e, &obj->showaslog);
1342 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1345 END OBJ(diskiograph_read, INFO_DISKIO)
1346 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1347 &obj->e, &obj->showaslog);
1349 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1352 END OBJ(diskiograph_write, INFO_DISKIO)
1353 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1354 &obj->e, &obj->showaslog);
1356 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1361 if (output_methods & TO_X) {
1362 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1366 obj->data.l = color0;
1368 obj->data.l = color1;
1370 obj->data.l = color2;
1372 obj->data.l = color3;
1374 obj->data.l = color4;
1376 obj->data.l = color5;
1378 obj->data.l = color6;
1380 obj->data.l = color7;
1382 obj->data.l = color8;
1384 obj->data.l = color9;
1386 obj->data.s = scan_font(arg);
1387 END OBJ(conky_version, 0)
1388 END OBJ(conky_build_date, 0)
1389 END OBJ(conky_build_arch, 0)
1390 END OBJ(downspeed, INFO_NET)
1392 obj->data.net = get_net_stat(arg);
1394 CRIT_ERR("downspeed needs argument");
1396 END OBJ(downspeedf, INFO_NET)
1398 obj->data.net = get_net_stat(arg);
1400 CRIT_ERR("downspeedf needs argument");
1402 END OBJ(downspeedgraph, INFO_NET)
1403 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1404 &obj->e, &obj->showaslog);
1406 // default to DEFAULTNETDEV
1407 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
1408 obj->data.net = get_net_stat(buf);
1411 obj_be_ifblock_else(ifblock_opaque, obj);
1413 obj_be_ifblock_endif(ifblock_opaque, obj);
1415 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1417 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1420 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1422 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1423 #define SIZE_DEFAULTS(arg) { \
1424 obj->a = default_##arg##_width; \
1425 obj->b = default_##arg##_height; \
1427 END OBJ(execgauge, 0)
1428 SIZE_DEFAULTS(gauge);
1429 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1432 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1433 END OBJ(execgraph, 0)
1434 SIZE_DEFAULTS(graph);
1435 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1436 END OBJ(execibar, 0)
1440 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1443 ERR("${execibar <interval> command}");
1444 obj->type = OBJ_text;
1445 snprintf(buf, 256, "${%s}", s);
1446 obj->data.s = strndup(buf, text_buffer_size);
1448 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1450 END OBJ(execigraph, 0)
1452 SIZE_DEFAULTS(graph);
1454 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1457 ERR("${execigraph <interval> command}");
1458 obj->type = OBJ_text;
1459 snprintf(buf, 256, "${%s}", s);
1460 obj->data.s = strndup(buf, text_buffer_size);
1462 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1464 END OBJ(execigauge, 0)
1466 SIZE_DEFAULTS(gauge);
1468 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1471 ERR("${execigauge <interval> command}");
1472 obj->type = OBJ_text;
1473 snprintf(buf, 256, "${%s}", s);
1474 obj->data.s = strndup(buf, text_buffer_size);
1476 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1481 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1484 ERR("${execi <interval> command}");
1485 obj->type = OBJ_text;
1486 snprintf(buf, 256, "${%s}", s);
1487 obj->data.s = strndup(buf, text_buffer_size);
1489 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1490 obj->data.execi.buffer = malloc(text_buffer_size);
1495 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1498 ERR("${execi <interval> command}");
1499 obj->type = OBJ_text;
1500 snprintf(buf, 256, "${%s}", s);
1501 obj->data.s = strndup(buf, text_buffer_size);
1503 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1504 obj->data.execi.buffer = malloc(text_buffer_size);
1506 END OBJ_THREAD(texeci, 0)
1509 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1512 ERR("${texeci <interval> command}");
1513 obj->type = OBJ_text;
1514 snprintf(buf, 256, "${%s}", s);
1515 obj->data.s = strndup(buf, text_buffer_size);
1517 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1518 obj->data.texeci.buffer = malloc(text_buffer_size);
1520 obj->data.texeci.p_timed_thread = NULL;
1521 END OBJ(pre_exec, 0)
1522 obj->type = OBJ_text;
1526 read_exec(arg, buf, sizeof(buf));
1527 obj->data.s = strndup(buf, text_buffer_size);
1529 obj->data.s = strndup("", text_buffer_size);
1532 END OBJ(fs_bar, INFO_FS)
1533 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1535 while (isspace(*arg)) {
1544 obj->data.fsbar.fs = prepare_fs_stat(arg);
1545 END OBJ(fs_bar_free, INFO_FS)
1546 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1548 while (isspace(*arg)) {
1558 obj->data.fsbar.fs = prepare_fs_stat(arg);
1559 END OBJ(fs_free, INFO_FS)
1563 obj->data.fs = prepare_fs_stat(arg);
1564 END OBJ(fs_used_perc, INFO_FS)
1568 obj->data.fs = prepare_fs_stat(arg);
1569 END OBJ(fs_free_perc, INFO_FS)
1573 obj->data.fs = prepare_fs_stat(arg);
1574 END OBJ(fs_size, INFO_FS)
1578 obj->data.fs = prepare_fs_stat(arg);
1579 END OBJ(fs_type, INFO_FS)
1583 obj->data.fs = prepare_fs_stat(arg);
1584 END OBJ(fs_used, INFO_FS)
1588 obj->data.fs = prepare_fs_stat(arg);
1590 obj->data.i = arg ? atoi(arg) : 1;
1591 END OBJ(nameserver, INFO_DNS)
1592 obj->data.i = arg ? atoi(arg) : 0;
1594 obj->data.i = arg ? atoi(arg) : 1;
1596 obj->data.i = arg ? atoi(arg) : 1;
1600 ERR("goto needs arguments");
1601 obj->type = OBJ_text;
1602 obj->data.s = strndup("${goto}", text_buffer_size);
1606 obj->data.i = atoi(arg);
1612 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1613 sscanf(arg, "%d", &b);
1619 obj->data.pair.a = a;
1620 obj->data.pair.b = b;
1623 END OBJ(i2c, INFO_SYSFS)
1624 char buf1[64], buf2[64];
1628 ERR("i2c needs arguments");
1629 obj->type = OBJ_text;
1630 // obj->data.s = strndup("${i2c}", text_buffer_size);
1634 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1635 /* if scanf couldn't read three values, read type and num and use
1637 sscanf(arg, "%63s %d", buf2, &n);
1638 obj->data.sysfs.fd = open_i2c_sensor(0, buf2, n,
1639 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1640 strncpy(obj->data.sysfs.type, buf2, 63);
1642 obj->data.sysfs.fd = open_i2c_sensor(buf1, buf2, n,
1643 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1644 strncpy(obj->data.sysfs.type, buf2, 63);
1647 END OBJ(platform, INFO_SYSFS)
1648 char buf1[64], buf2[64];
1652 ERR("platform needs arguments");
1653 obj->type = OBJ_text;
1657 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1658 /* if scanf couldn't read three values, read type and num and use
1660 sscanf(arg, "%63s %d", buf2, &n);
1661 obj->data.sysfs.fd = open_platform_sensor(0, buf2, n,
1662 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1663 strncpy(obj->data.sysfs.type, buf2, 63);
1665 obj->data.sysfs.fd = open_platform_sensor(buf1, buf2, n,
1666 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1667 strncpy(obj->data.sysfs.type, buf2, 63);
1670 END OBJ(hwmon, INFO_SYSFS)
1671 char buf1[64], buf2[64];
1675 ERR("hwmon needs argumanets");
1676 obj->type = OBJ_text;
1680 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1681 /* if scanf couldn't read three values, read type and num and use
1683 sscanf(arg, "%63s %d", buf2, &n);
1684 obj->data.sysfs.fd = open_hwmon_sensor(0, buf2, n,
1685 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1686 strncpy(obj->data.sysfs.type, buf2, 63);
1688 obj->data.sysfs.fd = open_hwmon_sensor(buf1, buf2, n,
1689 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1690 strncpy(obj->data.sysfs.type, buf2, 63);
1692 #endif /* !__OpenBSD__ */
1695 /* we have three different types of top (top, top_mem and top_time). To
1696 * avoid having almost-same code three times, we have this special
1698 if (strncmp(s, "top", 3) == EQUAL) {
1702 need_mask |= (1 << INFO_TOP);
1705 obj->type = OBJ_top;
1707 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1708 obj->type = OBJ_top_mem;
1710 } else if (strcmp(&s[3], "_time") == EQUAL) {
1711 obj->type = OBJ_top_time;
1714 ERR("Must be top, top_mem or top_time");
1719 ERR("top needs arguments");
1723 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1724 if (strcmp(buf, "name") == EQUAL) {
1725 obj->data.top.type = TOP_NAME;
1726 } else if (strcmp(buf, "cpu") == EQUAL) {
1727 obj->data.top.type = TOP_CPU;
1728 } else if (strcmp(buf, "pid") == EQUAL) {
1729 obj->data.top.type = TOP_PID;
1730 } else if (strcmp(buf, "mem") == EQUAL) {
1731 obj->data.top.type = TOP_MEM;
1732 } else if (strcmp(buf, "time") == EQUAL) {
1733 obj->data.top.type = TOP_TIME;
1734 } else if (strcmp(buf, "mem_res") == EQUAL) {
1735 obj->data.top.type = TOP_MEM_RES;
1736 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1737 obj->data.top.type = TOP_MEM_VSIZE;
1739 ERR("invalid type arg for top");
1740 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1743 if (n < 1 || n > 10) {
1744 ERR("invalid num arg for top. Must be between 1 and 10.");
1747 obj->data.top.num = n - 1;
1750 ERR("invalid argument count for top");
1753 } else OBJ(addr, INFO_NET)
1755 obj->data.net = get_net_stat(arg);
1757 CRIT_ERR("addr needs argument");
1759 #if defined(__linux__)
1760 END OBJ(addrs, INFO_NET)
1762 obj->data.net = get_net_stat(arg);
1764 CRIT_ERR("addrs needs argument");
1766 #endif /* __linux__ */
1768 if (init_tail_object(obj, arg)) {
1769 obj->type = OBJ_text;
1770 obj->data.s = strndup("${tail}", text_buffer_size);
1773 if (init_head_object(obj, arg)) {
1774 obj->type = OBJ_text;
1775 obj->data.s = strndup("${head}", text_buffer_size);
1779 obj->data.s = strdup(arg);
1781 CRIT_ERR("lines needs a argument");
1785 obj->data.s = strdup(arg);
1787 CRIT_ERR("words needs a argument");
1789 END OBJ(loadavg, INFO_LOADAVG)
1790 int a = 1, b = 2, c = 3, r = 3;
1793 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1794 if (r >= 3 && (c < 1 || c > 3)) {
1797 if (r >= 2 && (b < 1 || b > 3)) {
1800 if (r >= 1 && (a < 1 || a > 3)) {
1804 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1805 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1806 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1807 END OBJ_IF(if_empty, 0)
1809 ERR("if_empty needs an argument");
1810 obj->data.ifblock.s = 0;
1812 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1813 obj->sub = malloc(sizeof(struct text_object));
1814 extract_variable_text_internal(obj->sub,
1815 obj->data.ifblock.s, 0);
1817 END OBJ_IF(if_match, 0)
1819 ERR("if_match needs arguments");
1820 obj->data.ifblock.s = 0;
1822 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1823 obj->sub = malloc(sizeof(struct text_object));
1824 extract_variable_text_internal(obj->sub,
1825 obj->data.ifblock.s, 0);
1827 END OBJ_IF(if_existing, 0)
1829 ERR("if_existing needs an argument or two");
1830 obj->data.ifblock.s = NULL;
1831 obj->data.ifblock.str = NULL;
1833 char buf1[256], buf2[256];
1834 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
1837 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
1838 obj->data.ifblock.str = NULL;
1840 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
1841 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
1844 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
1845 END OBJ_IF(if_mounted, 0)
1847 ERR("if_mounted needs an argument");
1848 obj->data.ifblock.s = 0;
1850 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1852 END OBJ_IF(if_running, 0)
1856 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1857 obj->data.ifblock.s = strndup(buf, text_buffer_size);
1859 ERR("if_running needs an argument");
1860 obj->data.ifblock.s = 0;
1866 char box[256], dst[256];
1870 /* Kapil: Changed from MAIL_FILE to
1871 current_mail_spool since the latter
1872 is a copy of the former if undefined
1873 but the latter should take precedence
1875 strncpy(box, current_mail_spool, sizeof(box));
1877 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1879 strncpy(box, arg, sizeof(box));
1883 variable_substitute(box, dst, sizeof(dst));
1884 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1885 obj->data.local_mail.interval = n1;
1886 END OBJ(new_mails, 0)
1888 char box[256], dst[256];
1892 strncpy(box, current_mail_spool, sizeof(box));
1894 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1896 strncpy(box, arg, sizeof(box));
1900 variable_substitute(box, dst, sizeof(dst));
1901 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1902 obj->data.local_mail.interval = n1;
1903 END OBJ(seen_mails, 0)
1905 char box[256], dst[256];
1909 strncpy(box, current_mail_spool, sizeof(box));
1911 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1913 strncpy(box, arg, sizeof(box));
1917 variable_substitute(box, dst, sizeof(dst));
1918 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1919 obj->data.local_mail.interval = n1;
1920 END OBJ(unseen_mails, 0)
1922 char box[256], dst[256];
1926 strncpy(box, current_mail_spool, sizeof(box));
1928 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1930 strncpy(box, arg, sizeof(box));
1934 variable_substitute(box, dst, sizeof(dst));
1935 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1936 obj->data.local_mail.interval = n1;
1937 END OBJ(flagged_mails, 0)
1939 char box[256], dst[256];
1943 strncpy(box, current_mail_spool, sizeof(box));
1945 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1947 strncpy(box, arg, sizeof(box));
1951 variable_substitute(box, dst, sizeof(dst));
1952 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1953 obj->data.local_mail.interval = n1;
1954 END OBJ(unflagged_mails, 0)
1956 char box[256], dst[256];
1960 strncpy(box, current_mail_spool, sizeof(box));
1962 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1964 strncpy(box, arg, sizeof(box));
1968 variable_substitute(box, dst, sizeof(dst));
1969 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1970 obj->data.local_mail.interval = n1;
1971 END OBJ(forwarded_mails, 0)
1973 char box[256], dst[256];
1977 strncpy(box, current_mail_spool, sizeof(box));
1979 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1981 strncpy(box, arg, sizeof(box));
1985 variable_substitute(box, dst, sizeof(dst));
1986 obj->data.local_mail.box = strndup(dst, text_buffer_size);
1987 obj->data.local_mail.interval = n1;
1988 END OBJ(unforwarded_mails, 0)
1990 char box[256], dst[256];
1994 strncpy(box, current_mail_spool, sizeof(box));
1996 if (sscanf(arg, "%s %f", box, &n1) != 2) {
1998 strncpy(box, arg, sizeof(box));
2002 variable_substitute(box, dst, sizeof(dst));
2003 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2004 obj->data.local_mail.interval = n1;
2005 END OBJ(replied_mails, 0)
2007 char box[256], dst[256];
2011 strncpy(box, current_mail_spool, sizeof(box));
2013 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2015 strncpy(box, arg, sizeof(box));
2019 variable_substitute(box, dst, sizeof(dst));
2020 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2021 obj->data.local_mail.interval = n1;
2022 END OBJ(unreplied_mails, 0)
2024 char box[256], dst[256];
2028 strncpy(box, current_mail_spool, sizeof(box));
2030 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2032 strncpy(box, arg, sizeof(box));
2036 variable_substitute(box, dst, sizeof(dst));
2037 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2038 obj->data.local_mail.interval = n1;
2039 END OBJ(draft_mails, 0)
2041 char box[256], dst[256];
2045 strncpy(box, current_mail_spool, sizeof(box));
2047 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2049 strncpy(box, arg, sizeof(box));
2053 variable_substitute(box, dst, sizeof(dst));
2054 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2055 obj->data.local_mail.interval = n1;
2056 END OBJ(trashed_mails, 0)
2058 char box[256], dst[256];
2062 strncpy(box, current_mail_spool, sizeof(box));
2064 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2066 strncpy(box, arg, sizeof(box));
2070 variable_substitute(box, dst, sizeof(dst));
2071 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2072 obj->data.local_mail.interval = n1;
2073 END OBJ(mboxscan, 0)
2074 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2075 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2076 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2077 obj->data.mboxscan.output[0] = 1;
2078 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2079 END OBJ(mem, INFO_MEM)
2080 END OBJ(memeasyfree, INFO_MEM)
2081 END OBJ(memfree, INFO_MEM)
2082 END OBJ(memmax, INFO_MEM)
2083 END OBJ(memperc, INFO_MEM)
2084 END OBJ(memgauge, INFO_MEM)
2085 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2086 END OBJ(membar, INFO_MEM)
2087 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2088 END OBJ(memgraph, INFO_MEM)
2089 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2090 &obj->e, &obj->showaslog);
2095 END OBJ(mixer, INFO_MIXER)
2096 obj->data.l = mixer_init(arg);
2097 END OBJ(mixerl, INFO_MIXER)
2098 obj->data.l = mixer_init(arg);
2099 END OBJ(mixerr, INFO_MIXER)
2100 obj->data.l = mixer_init(arg);
2101 END OBJ(mixerbar, INFO_MIXER)
2102 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2103 &obj->data.mixerbar.h);
2104 END OBJ(mixerlbar, INFO_MIXER)
2105 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2106 &obj->data.mixerbar.h);
2107 END OBJ(mixerrbar, INFO_MIXER)
2108 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2109 &obj->data.mixerbar.h);
2110 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2111 obj->data.ifblock.i = mixer_init(arg);
2113 END OBJ(monitor, INFO_X11)
2114 END OBJ(monitor_number, INFO_X11)
2116 END OBJ(nodename, 0)
2117 END OBJ(processes, INFO_PROCS)
2118 END OBJ(running_processes, INFO_RUN_PROCS)
2119 END OBJ(shadecolor, 0)
2121 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2123 END OBJ(outlinecolor, 0)
2125 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2127 END OBJ(stippled_hr, 0)
2129 int a = stippled_borders, b = 1;
2132 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2133 sscanf(arg, "%d", &b);
2139 obj->data.pair.a = a;
2140 obj->data.pair.b = b;
2142 END OBJ(swap, INFO_MEM)
2143 END OBJ(swapmax, INFO_MEM)
2144 END OBJ(swapperc, INFO_MEM)
2145 END OBJ(swapbar, INFO_MEM)
2146 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2149 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2151 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2153 char buf1[256], buf2[256], *fmt, *tz;
2157 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2167 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2168 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2170 END OBJ(iconv_start, 0)
2171 if (iconv_converting) {
2172 CRIT_ERR("You must stop your last iconv conversion before "
2173 "starting another");
2176 char iconv_from[CODEPAGE_LENGTH];
2177 char iconv_to[CODEPAGE_LENGTH];
2179 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2180 CRIT_ERR("Invalid arguments for iconv_start");
2184 new_iconv = iconv_open(iconv_to, iconv_from);
2185 if (new_iconv == (iconv_t) (-1)) {
2186 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2188 obj->a = register_iconv(&new_iconv);
2189 iconv_converting = 1;
2193 CRIT_ERR("Iconv requires arguments");
2195 END OBJ(iconv_stop, 0)
2196 iconv_converting = 0;
2199 END OBJ(totaldown, INFO_NET)
2201 obj->data.net = get_net_stat(arg);
2203 CRIT_ERR("totaldown needs argument");
2205 END OBJ(totalup, INFO_NET)
2206 obj->data.net = get_net_stat(arg);
2208 obj->data.net = get_net_stat(arg);
2210 CRIT_ERR("totalup needs argument");
2213 END OBJ_IF(if_updatenr, 0)
2214 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2215 if(obj->data.ifblock.i == 0) CRIT_ERR("if_updatenr needs a number above 0 as argument");
2216 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2218 obj->data.i = arg ? atoi(arg) : 0;
2220 obj->data.i = arg ? atoi(arg) : 0;
2221 END OBJ(upspeed, INFO_NET)
2223 obj->data.net = get_net_stat(arg);
2225 CRIT_ERR("upspeed needs argument");
2227 END OBJ(upspeedf, INFO_NET)
2229 obj->data.net = get_net_stat(arg);
2231 CRIT_ERR("upspeedf needs argument");
2234 END OBJ(upspeedgraph, INFO_NET)
2235 char *buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2236 &obj->e, &obj->showaslog);
2238 // default to DEFAULTNETDEV
2239 buf = strndup(buf ? buf : "DEFAULTNETDEV", text_buffer_size);
2240 obj->data.net = get_net_stat(buf);
2242 END OBJ(uptime_short, INFO_UPTIME)
2243 END OBJ(uptime, INFO_UPTIME)
2244 END OBJ(user_names, INFO_USERS)
2245 END OBJ(user_times, INFO_USERS)
2246 END OBJ(user_terms, INFO_USERS)
2247 END OBJ(user_number, INFO_USERS)
2248 #if defined(__linux__)
2249 END OBJ(gw_iface, INFO_GW)
2250 END OBJ(gw_ip, INFO_GW)
2251 END OBJ(if_gw, INFO_GW)
2252 #endif /* !__linux__ */
2254 END OBJ(adt746xcpu, 0)
2255 END OBJ(adt746xfan, 0)
2256 #endif /* !__OpenBSD__ */
2257 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2258 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2259 END OBJ(apm_adapter, 0)
2260 END OBJ(apm_battery_life, 0)
2261 END OBJ(apm_battery_time, 0)
2262 #endif /* __FreeBSD__ */
2263 END OBJ_THREAD(imap_unseen, 0)
2266 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2267 obj->global_mode = 0;
2269 obj->global_mode = 1;
2271 END OBJ_THREAD(imap_messages, 0)
2274 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2275 obj->global_mode = 0;
2277 obj->global_mode = 1;
2279 END OBJ_THREAD(pop3_unseen, 0)
2282 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2283 obj->global_mode = 0;
2285 obj->global_mode = 1;
2287 END OBJ_THREAD(pop3_used, 0)
2290 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2291 obj->global_mode = 0;
2293 obj->global_mode = 1;
2298 obj->data.s = strndup(arg, text_buffer_size);
2300 ERR("smapi needs an argument");
2301 END OBJ_IF(if_smapi_bat_installed, 0)
2303 ERR("if_smapi_bat_installed needs an argument");
2304 obj->data.ifblock.s = 0;
2306 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2307 END OBJ(smapi_bat_perc, 0)
2309 obj->data.s = strndup(arg, text_buffer_size);
2311 ERR("smapi_bat_perc needs an argument");
2312 END OBJ(smapi_bat_temp, 0)
2314 obj->data.s = strndup(arg, text_buffer_size);
2316 ERR("smapi_bat_temp needs an argument");
2317 END OBJ(smapi_bat_power, 0)
2319 obj->data.s = strndup(arg, text_buffer_size);
2321 ERR("smapi_bat_power needs an argument");
2322 END OBJ(smapi_bat_bar, 0)
2325 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2326 ERR("first argument to smapi_bat_bar must be an integer value");
2330 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2333 ERR("smapi_bat_bar needs an argument");
2336 #define mpd_set_maxlen(name) \
2339 sscanf(arg, "%d", &i); \
2341 obj->data.i = i + 1; \
2343 ERR(#name ": invalid length argument"); \
2345 END OBJ(mpd_artist, INFO_MPD)
2346 mpd_set_maxlen(mpd_artist);
2348 END OBJ(mpd_title, INFO_MPD)
2349 mpd_set_maxlen(mpd_title);
2351 END OBJ(mpd_random, INFO_MPD) init_mpd();
2352 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2353 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2354 END OBJ(mpd_length, INFO_MPD) init_mpd();
2355 END OBJ(mpd_track, INFO_MPD)
2356 mpd_set_maxlen(mpd_track);
2358 END OBJ(mpd_name, INFO_MPD)
2359 mpd_set_maxlen(mpd_name);
2361 END OBJ(mpd_file, INFO_MPD)
2362 mpd_set_maxlen(mpd_file);
2364 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2365 END OBJ(mpd_album, INFO_MPD)
2366 mpd_set_maxlen(mpd_album);
2368 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2369 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2370 END OBJ(mpd_status, INFO_MPD) init_mpd();
2371 END OBJ(mpd_bar, INFO_MPD)
2372 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2374 END OBJ(mpd_smart, INFO_MPD)
2375 mpd_set_maxlen(mpd_smart);
2377 END OBJ_IF(if_mpd_playing, INFO_MPD)
2379 #undef mpd_set_maxlen
2382 END OBJ(moc_state, INFO_MOC)
2383 END OBJ(moc_file, INFO_MOC)
2384 END OBJ(moc_title, INFO_MOC)
2385 END OBJ(moc_artist, INFO_MOC)
2386 END OBJ(moc_song, INFO_MOC)
2387 END OBJ(moc_album, INFO_MOC)
2388 END OBJ(moc_totaltime, INFO_MOC)
2389 END OBJ(moc_timeleft, INFO_MOC)
2390 END OBJ(moc_curtime, INFO_MOC)
2391 END OBJ(moc_bitrate, INFO_MOC)
2392 END OBJ(moc_rate, INFO_MOC)
2395 END OBJ(xmms2_artist, INFO_XMMS2)
2396 END OBJ(xmms2_album, INFO_XMMS2)
2397 END OBJ(xmms2_title, INFO_XMMS2)
2398 END OBJ(xmms2_genre, INFO_XMMS2)
2399 END OBJ(xmms2_comment, INFO_XMMS2)
2400 END OBJ(xmms2_url, INFO_XMMS2)
2401 END OBJ(xmms2_tracknr, INFO_XMMS2)
2402 END OBJ(xmms2_bitrate, INFO_XMMS2)
2403 END OBJ(xmms2_date, INFO_XMMS2)
2404 END OBJ(xmms2_id, INFO_XMMS2)
2405 END OBJ(xmms2_duration, INFO_XMMS2)
2406 END OBJ(xmms2_elapsed, INFO_XMMS2)
2407 END OBJ(xmms2_size, INFO_XMMS2)
2408 END OBJ(xmms2_status, INFO_XMMS2)
2409 END OBJ(xmms2_percent, INFO_XMMS2)
2410 END OBJ(xmms2_bar, INFO_XMMS2)
2411 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2412 END OBJ(xmms2_smart, INFO_XMMS2)
2413 END OBJ(xmms2_playlist, INFO_XMMS2)
2414 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2415 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2418 END OBJ(audacious_status, INFO_AUDACIOUS)
2419 END OBJ(audacious_title, INFO_AUDACIOUS)
2421 sscanf(arg, "%d", &info.audacious.max_title_len);
2422 if (info.audacious.max_title_len > 0) {
2423 info.audacious.max_title_len++;
2425 CRIT_ERR("audacious_title: invalid length argument");
2428 END OBJ(audacious_length, INFO_AUDACIOUS)
2429 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2430 END OBJ(audacious_position, INFO_AUDACIOUS)
2431 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2432 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2433 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2434 END OBJ(audacious_channels, INFO_AUDACIOUS)
2435 END OBJ(audacious_filename, INFO_AUDACIOUS)
2436 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2437 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2438 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2439 END OBJ(audacious_bar, INFO_AUDACIOUS)
2440 scan_bar(arg, &obj->a, &obj->b);
2443 END OBJ(bmpx_title, INFO_BMPX)
2444 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2445 END OBJ(bmpx_artist, INFO_BMPX)
2446 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2447 END OBJ(bmpx_album, INFO_BMPX)
2448 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2449 END OBJ(bmpx_track, INFO_BMPX)
2450 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2451 END OBJ(bmpx_uri, INFO_BMPX)
2452 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2453 END OBJ(bmpx_bitrate, INFO_BMPX)
2454 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2460 char *userid = (char *) malloc(20 * sizeof(char));
2461 char *apikey = (char *) malloc(64 * sizeof(char));
2462 char *charid = (char *) malloc(20 * sizeof(char));
2464 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2465 obj->data.eve.charid = charid;
2466 obj->data.eve.userid = userid;
2467 obj->data.eve.apikey = apikey;
2471 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
2477 int argc, delay, act_par;
2478 unsigned int nrspaces = 0;
2479 char *uri = (char *) malloc(128 * sizeof(char));
2480 char *action = (char *) malloc(64 * sizeof(char));
2482 argc = sscanf(arg, "%127s %d %63s %d %u", uri, &delay, action,
2483 &act_par, &nrspaces);
2484 obj->data.rss.uri = uri;
2485 obj->data.rss.delay = delay;
2486 obj->data.rss.action = action;
2487 obj->data.rss.act_par = act_par;
2488 obj->data.rss.nrspaces = nrspaces;
2492 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
2493 "[act_par] [spaces in front]");
2498 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2499 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2500 ERR("hddtemp needs arguments");
2501 obj->type = OBJ_text;
2502 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2503 obj->data.hddtemp.update_time = 0;
2505 obj->data.hddtemp.temp = NULL;
2507 #ifdef TCP_PORT_MONITOR
2508 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2509 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2511 END OBJ(entropy_avail, INFO_ENTROPY)
2512 END OBJ(entropy_poolsize, INFO_ENTROPY)
2513 END OBJ(entropy_bar, INFO_ENTROPY)
2514 scan_bar(arg, &obj->a, &obj->b);
2518 obj->data.scroll.step = 1;
2519 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2520 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
2522 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2523 obj->data.scroll.start = 0;
2524 obj->sub = malloc(sizeof(struct text_object));
2525 extract_variable_text_internal(obj->sub,
2526 obj->data.scroll.text, 0);
2528 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
2533 unsigned int indenting = 0; //vars can be used as args for other vars
2536 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
2538 for(i=0; arg[i] != 0 && j < 2; i++) {
2539 if(startvar[j] == -1) {
2543 }else if(endvar[j] == -1) {
2546 }else if(arg[i] == '}') {
2549 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
2555 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
2556 obj->data.combine.left=malloc(endvar[0]-startvar[0]+1);
2557 obj->data.combine.seperation=malloc(startvar[1]-endvar[0]+1);
2558 obj->data.combine.right=malloc(endvar[1]-startvar[1]+1);
2559 strncpy(obj->data.combine.left, arg+startvar[0], endvar[0]-startvar[0]); obj->data.combine.left[endvar[0]-startvar[0]]=0;
2560 strncpy(obj->data.combine.seperation, arg+endvar[0], startvar[1]-endvar[0]); obj->data.combine.seperation[startvar[1]-endvar[0]]=0;
2561 strncpy(obj->data.combine.right, arg+startvar[1], endvar[1]-startvar[1]); obj->data.combine.right[endvar[1]-startvar[1]]=0;
2562 obj->sub = malloc(sizeof(struct text_object));
2563 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
2564 obj->sub->sub = malloc(sizeof(struct text_object));
2565 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
2567 CRIT_ERR("combine needs arguments: <text1> <text2>");
2570 CRIT_ERR("combine needs arguments: <text1> <text2>");
2575 CRIT_ERR("nvidia needs an argument\n");
2576 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
2577 CRIT_ERR("nvidia: invalid argument"
2578 " specified: '%s'\n", arg);
2584 ERR("unknown variable %s", s);
2585 obj->type = OBJ_text;
2586 snprintf(buf, 256, "${%s}", s);
2587 obj->data.s = strndup(buf, text_buffer_size);
2594 static struct text_object *create_plain_text(const char *s)
2596 struct text_object *obj;
2598 char *esc = strstr(s, "\\#");
2600 /* remove extra '\' */
2601 strcpy(esc, esc + 1);
2604 if (s == NULL || *s == '\0') {
2608 obj = new_text_object_internal();
2610 obj->type = OBJ_text;
2611 obj->data.s = strndup(s, text_buffer_size);
2615 /* backslash_escape - do the actual substitution task for template objects
2617 * The field templates is used for substituting the \N occurences. Set it to
2618 * NULL to leave them as they are.
2620 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
2624 unsigned int dup_idx = 0, dup_len;
2626 dup_len = strlen(src) + 1;
2627 src_dup = malloc(dup_len * sizeof(char));
2635 if (*(p + 1) == '\\') {
2636 src_dup[dup_idx++] = '\\';
2638 } else if (*(p + 1) == ' ') {
2639 src_dup[dup_idx++] = ' ';
2641 } else if (*(p + 1) == 'n') {
2642 src_dup[dup_idx++] = '\n';
2644 } else if (templates) {
2645 unsigned int tmpl_num;
2647 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
2648 (tmpl_num > template_count))
2650 dup_len += strlen(templates[tmpl_num - 1]);
2651 src_dup = realloc(src_dup, dup_len * sizeof(char));
2652 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
2653 dup_idx += strlen(templates[tmpl_num - 1]);
2658 src_dup[dup_idx++] = *p;
2663 src_dup[dup_idx] = '\0';
2664 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
2668 /* handle_template_object - core logic of the template object
2670 * use config variables like this:
2671 * template1 = "$\1\2"
2672 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
2674 * and use them like this:
2675 * ${template1 node name}
2676 * ${template2 root /}
2677 * ${template2 cdrom /mnt/cdrom}
2679 static char *handle_template(const char *tmpl, const char *args)
2681 char *args_dup, *p, *p_old;
2682 char **argsp = NULL;
2683 unsigned int argcnt = 0, template_idx, i;
2686 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
2690 args_dup = strdup(args);
2693 while (*p && (*p == ' ' && *(p - 1) != '\\'))
2695 if (*(p - 1) == '\\')
2698 while (*p && (*p != ' ' || *(p - 1) == '\\'))
2704 argsp = realloc(argsp, ++argcnt * sizeof(char *));
2705 argsp[argcnt - 1] = p_old;
2707 for (i = 0; i < argcnt; i++) {
2709 tmp = backslash_escape(argsp[i], NULL, 0);
2710 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
2714 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
2715 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
2717 for (i = 0; i < argcnt; i++)
2723 static char *find_and_replace_templates(const char *inbuf)
2725 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
2728 outlen = strlen(inbuf) + 1;
2729 o = outbuf = calloc(outlen, sizeof(char));
2730 memset(outbuf, 0, outlen * sizeof(char));
2732 p = indup = strdup(inbuf);
2734 while (*p && *p != '$')
2740 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
2745 if (*(p + 1) == '{') {
2767 tmpl_out = handle_template(templ, args);
2769 outlen += strlen(tmpl_out);
2770 outbuf = realloc(outbuf, outlen * sizeof(char));
2771 strcat (outbuf, tmpl_out);
2773 o = outbuf + strlen(outbuf);
2775 ERR("failed to handle template '%s' with args '%s'", templ, args);
2779 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
2784 static int text_contains_templates(const char *text)
2786 if (strcasestr(text, "${template") != NULL)
2788 if (strcasestr(text, "$template") != NULL)
2793 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
2795 struct text_object *obj;
2796 char *p, *s, *orig_p;
2798 void *ifblock_opaque = NULL;
2802 p = strndup(const_p, max_user_text - 1);
2803 while (text_contains_templates(p)) {
2805 tmp = find_and_replace_templates(p);
2811 if (strcmp(p, const_p)) {
2812 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
2814 DBGP("no templates to replace");
2817 memset(retval, 0, sizeof(struct text_object));
2819 line = global_text_lines;
2825 /* handle comments within the TEXT area */
2826 if (*p == '#' && (p == orig_p || *(p - 1) != '\\')) {
2827 while (*p && *p != '\n') p++;
2833 obj = create_plain_text(s);
2835 append_object(retval, obj);
2846 /* variable is either $foo or ${foo} */
2848 unsigned int brl = 1, brr = 0;
2852 while (*p && brl != brr) {
2867 while (*p && (isalnum((int) *p) || *p == '_')) {
2872 /* copy variable to buffer */
2873 len = (p - s > 255) ? 255 : (p - s);
2874 strncpy(buf, s, len);
2882 /* search for variable in environment */
2886 obj = create_plain_text(var);
2888 append_object(retval, obj);
2893 /* if variable wasn't found in environment, use some special */
2898 if (strchr(buf, ' ')) {
2899 arg = strchr(buf, ' ');
2902 while (isspace((int) *arg)) {
2910 /* lowercase variable name */
2913 *tmp_p = tolower(*tmp_p);
2917 obj = construct_text_object(buf, arg,
2918 line, allow_threaded,
2921 append_object(retval, obj);
2925 obj = create_plain_text("$");
2928 append_object(retval, obj);
2934 obj = create_plain_text(s);
2936 append_object(retval, obj);
2939 if (!ifblock_stack_empty(&ifblock_opaque)) {
2940 ERR("one or more $endif's are missing");
2947 static void extract_variable_text(const char *p)
2949 free_text_objects(&global_root_object);
2963 extract_variable_text_internal(&global_root_object, p, 1);
2966 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
2968 extract_variable_text_internal(root, txt, 0);
2969 generate_text_internal(p, max_user_text, *root, cur);
2973 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
2974 void *thread(void *), const char *text)
2976 if (obj->global_mode && info.mail) {
2977 // this means we use info
2978 if (!info.mail->p_timed_thread) {
2979 info.mail->p_timed_thread =
2980 timed_thread_create(thread,
2981 (void *) info.mail, info.mail->interval * 1000000);
2982 if (!info.mail->p_timed_thread) {
2983 ERR("Error creating %s timed thread", text);
2985 timed_thread_register(info.mail->p_timed_thread,
2986 &info.mail->p_timed_thread);
2987 if (timed_thread_run(info.mail->p_timed_thread)) {
2988 ERR("Error running %s timed thread", text);
2992 } else if (obj->data.mail) {
2993 // this means we use obj
2994 if (!obj->data.mail->p_timed_thread) {
2995 obj->data.mail->p_timed_thread =
2996 timed_thread_create(thread,
2997 (void *) obj->data.mail,
2998 obj->data.mail->interval * 1000000);
2999 if (!obj->data.mail->p_timed_thread) {
3000 ERR("Error creating %s timed thread", text);
3002 timed_thread_register(obj->data.mail->p_timed_thread,
3003 &obj->data.mail->p_timed_thread);
3004 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3005 ERR("Error running %s timed thread", text);
3008 return obj->data.mail;
3009 } else if (!obj->a) {
3010 // something is wrong, warn once then stop
3011 ERR("There's a problem with your mail settings. "
3012 "Check that the global mail settings are properly defined"
3013 " (line %li).", obj->line);
3019 char *format_time(unsigned long timeval, const int width)
3022 unsigned long nt; // narrow time, for speed on 32-bit
3023 unsigned cc; // centiseconds
3024 unsigned nn; // multi-purpose whatever
3027 cc = nt % 100; // centiseconds past second
3028 nt /= 100; // total seconds
3029 nn = nt % 60; // seconds past the minute
3030 nt /= 60; // total minutes
3031 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3033 return strndup(buf, text_buffer_size);
3035 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3036 return strndup(buf, text_buffer_size);
3038 nn = nt % 60; // minutes past the hour
3039 nt /= 60; // total hours
3040 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3041 return strndup(buf, text_buffer_size);
3043 nn = nt; // now also hours
3044 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3045 return strndup(buf, text_buffer_size);
3047 nn /= 24; // now days
3048 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3049 return strndup(buf, text_buffer_size);
3051 nn /= 7; // now weeks
3052 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3053 return strndup(buf, text_buffer_size);
3055 // well shoot, this outta' fit...
3056 return strndup("<inf>", text_buffer_size);
3059 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3060 //string has to end with \0 and it's length should fit in a int
3062 void remove_deleted_chars(char *string){
3064 while(string[i] != 0){
3065 if(string[i] == BACKSPACE){
3067 strcpy( &(string[i-1]), &(string[i+1]) );
3069 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3074 static inline void format_media_player_time(char *buf, const int size,
3077 int days, hours, minutes;
3079 days = seconds / (24 * 60 * 60);
3080 seconds %= (24 * 60 * 60);
3081 hours = seconds / (60 * 60);
3082 seconds %= (60 * 60);
3083 minutes = seconds / 60;
3087 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3088 hours, minutes, seconds);
3089 } else if (hours > 0) {
3090 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3093 snprintf(buf, size, "%i:%02i", minutes, seconds);
3097 static inline double get_barnum(char *buf)
3109 if (sscanf(buf, "%lf", &barnum) == 0) {
3110 ERR("reading exec value failed (perhaps it's not the "
3111 "correct format?)");
3114 if (barnum > 100.0 || barnum < 0.0) {
3115 ERR("your exec value is not between 0 and 100, "
3116 "therefore it will be ignored");
3122 static void generate_text_internal(char *p, int p_max_size,
3123 struct text_object root, struct information *cur)
3125 struct text_object *obj;
3127 /* for the OBJ_top* handler */
3128 struct process **needed = 0;
3131 char buff_in[p_max_size];
3133 iconv_converting = 0;
3137 for (obj = root.next; obj && p_max_size > 0; obj = obj->next) {
3138 needed = 0; // reset for top stuff
3140 /* IFBLOCK jumping algorithm
3142 * This is easier as it looks like:
3143 * - each IF checks it's condition
3144 * - on FALSE: call DO_JUMP
3145 * - on TRUE: don't care
3146 * - each ELSE calls DO_JUMP unconditionally
3147 * - each ENDIF is silently being ignored
3150 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3151 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3152 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3153 * regularly, "obj" is being updated to point to obj->next, so object parsing
3154 * continues right after the corresponding ELSE or ENDIF. This means that if we
3155 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3156 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3157 * jumped, and there is nothing to do.
3161 obj = obj->data.ifblock.next; \
3164 #define OBJ(a) break; case OBJ_##a:
3166 switch (obj->type) {
3168 ERR("not implemented obj type %d", obj->type);
3171 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3173 #endif /* !__OpenBSD__ */
3176 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3177 obj->data.cpu_index);
3183 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3184 obj->data.cpu_index);
3186 /* OpenBSD has no such flag (SUSv2) */
3187 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3188 obj->data.cpu_index);
3192 #if defined(__linux__)
3195 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3196 obj->data.cpu_index);
3201 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3202 obj->data.cpu_index);
3207 OBJ(wireless_essid) {
3208 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3210 OBJ(wireless_mode) {
3211 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3213 OBJ(wireless_bitrate) {
3214 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3217 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3219 OBJ(wireless_link_qual) {
3220 spaced_print(p, p_max_size, "%d", 4,
3221 obj->data.net->link_qual);
3223 OBJ(wireless_link_qual_max) {
3224 spaced_print(p, p_max_size, "%d", 4,
3225 obj->data.net->link_qual_max);
3227 OBJ(wireless_link_qual_perc) {
3228 if (obj->data.net->link_qual_max > 0) {
3229 spaced_print(p, p_max_size, "%.0f", 5,
3230 (double) obj->data.net->link_qual /
3231 obj->data.net->link_qual_max * 100);
3233 spaced_print(p, p_max_size, "unk", 5);
3236 OBJ(wireless_link_bar) {
3237 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3238 obj->data.net->link_qual_max) * 255.0);
3240 #endif /* HAVE_IWLIB */
3242 #endif /* __linux__ */
3246 get_adt746x_cpu(p, p_max_size);
3249 get_adt746x_fan(p, p_max_size);
3252 get_acpi_fan(p, p_max_size);
3254 OBJ(acpiacadapter) {
3255 get_acpi_ac_adapter(p, p_max_size);
3258 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3261 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3263 OBJ(battery_percent) {
3264 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3267 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3269 OBJ(battery_short) {
3270 get_battery_short_status(p, p_max_size, obj->data.s);
3272 #endif /* __OpenBSD__ */
3275 human_readable(cur->buffers * 1024, p, 255);
3278 human_readable(cur->cached * 1024, p, 255);
3281 if (obj->data.cpu_index > info.cpu_count) {
3282 ERR("obj->data.cpu_index %i info.cpu_count %i",
3283 obj->data.cpu_index, info.cpu_count);
3284 CRIT_ERR("attempting to use more CPUs than you have!");
3286 percent_print(p, p_max_size,
3287 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3290 new_gauge(p, obj->a, obj->b,
3291 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3293 new_bar(p, obj->a, obj->b,
3294 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3297 new_graph(p, obj->a, obj->b, obj->c, obj->d, (unsigned int)
3298 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3299 100, 1, obj->showaslog);
3302 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3303 obj->e, 1, obj->showaslog);
3306 new_fg(p, obj->data.l);
3338 OBJ(conky_version) {
3339 snprintf(p, p_max_size, "%s", VERSION);
3341 OBJ(conky_build_date) {
3342 snprintf(p, p_max_size, "%s", BUILD_DATE);
3344 OBJ(conky_build_arch) {
3345 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3347 #if defined(__linux__)
3349 snprintf(p, p_max_size, "%s",
3350 get_disk_protect_queue(obj->data.s));
3353 snprintf(p, p_max_size, "%s", i8k.version);
3356 snprintf(p, p_max_size, "%s", i8k.bios);
3359 snprintf(p, p_max_size, "%s", i8k.serial);
3364 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3365 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3367 OBJ(i8k_left_fan_status) {
3368 int left_fan_status;
3370 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3371 if (left_fan_status == 0) {
3372 snprintf(p, p_max_size, "off");
3374 if (left_fan_status == 1) {
3375 snprintf(p, p_max_size, "low");
3377 if (left_fan_status == 2) {
3378 snprintf(p, p_max_size, "high");
3381 OBJ(i8k_right_fan_status) {
3382 int right_fan_status;
3384 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3385 if (right_fan_status == 0) {
3386 snprintf(p, p_max_size, "off");
3388 if (right_fan_status == 1) {
3389 snprintf(p, p_max_size, "low");
3391 if (right_fan_status == 2) {
3392 snprintf(p, p_max_size, "high");
3395 OBJ(i8k_left_fan_rpm) {
3396 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3398 OBJ(i8k_right_fan_rpm) {
3399 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3401 OBJ(i8k_ac_status) {
3404 sscanf(i8k.ac_status, "%d", &ac_status);
3405 if (ac_status == -1) {
3406 snprintf(p, p_max_size, "disabled (read i8k docs)");
3408 if (ac_status == 0) {
3409 snprintf(p, p_max_size, "off");
3411 if (ac_status == 1) {
3412 snprintf(p, p_max_size, "on");
3415 OBJ(i8k_buttons_status) {
3416 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3420 get_ibm_acpi_fan(p, p_max_size);
3423 get_ibm_acpi_temps();
3424 temp_print(p, p_max_size,
3425 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
3428 get_ibm_acpi_volume(p, p_max_size);
3430 OBJ(ibm_brightness) {
3431 get_ibm_acpi_brightness(p, p_max_size);
3434 /* information from sony_laptop kernel module
3435 * /sys/devices/platform/sony-laptop */
3436 OBJ(sony_fanspeed) {
3437 get_sony_fanspeed(p, p_max_size);
3440 if (!cur->gw_info.count) {
3445 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
3448 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
3451 snprintf(p, p_max_size, "%d", get_laptop_mode());
3454 get_powerbook_batt_info(p, p_max_size, obj->data.i);
3456 #endif /* __linux__ */
3457 #if (defined(__FreeBSD__) || defined(__linux__))
3459 if ((obj->data.ifblock.s)
3460 && (!interface_up(obj->data.ifblock.s))) {
3466 OBJ(obsd_sensors_temp) {
3467 obsd_sensors.device = sensor_device;
3468 update_obsd_sensors();
3469 temp_print(p, p_max_size,
3470 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
3473 OBJ(obsd_sensors_fan) {
3474 obsd_sensors.device = sensor_device;
3475 update_obsd_sensors();
3476 snprintf(p, p_max_size, "%d",
3477 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
3479 OBJ(obsd_sensors_volt) {
3480 obsd_sensors.device = sensor_device;
3481 update_obsd_sensors();
3482 snprintf(p, p_max_size, "%.2f",
3483 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
3486 get_obsd_vendor(p, p_max_size);
3489 get_obsd_product(p, p_max_size);
3491 #endif /* __OpenBSD__ */
3494 new_font(p, obj->data.s);
3496 /* TODO: move this correction from kB to kB/s elsewhere
3497 * (or get rid of it??) */
3499 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
3503 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
3507 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
3511 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3512 obj->data.diskio->current, obj->e, 1, obj->showaslog);
3514 OBJ(diskiograph_read) {
3515 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3516 obj->data.diskio->current_read, obj->e, 1, obj->showaslog);
3518 OBJ(diskiograph_write) {
3519 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3520 obj->data.diskio->current_write, obj->e, 1, obj->showaslog);
3523 human_readable(obj->data.net->recv_speed, p, 255);
3526 spaced_print(p, p_max_size, "%.1f", 8,
3527 obj->data.net->recv_speed / 1024.0);
3529 OBJ(downspeedgraph) {
3530 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3531 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->showaslog);
3534 /* Since we see you, you're if has not jumped.
3535 * Do Ninja jump here: without leaving traces.
3536 * This is to prevent us from stale jumped flags.
3538 obj = obj->data.ifblock.next;
3542 /* harmless object, just ignore */
3546 if ((obj->data.net->addr.sa_data[2] & 255) == 0
3547 && (obj->data.net->addr.sa_data[3] & 255) == 0
3548 && (obj->data.net->addr.sa_data[4] & 255) == 0
3549 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
3550 snprintf(p, p_max_size, "No Address");
3552 snprintf(p, p_max_size, "%u.%u.%u.%u",
3553 obj->data.net->addr.sa_data[2] & 255,
3554 obj->data.net->addr.sa_data[3] & 255,
3555 obj->data.net->addr.sa_data[4] & 255,
3556 obj->data.net->addr.sa_data[5] & 255);
3559 #if defined(__linux__)
3561 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
3563 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
3564 strcpy(p, obj->data.net->addrs);
3567 strcpy(p, "0.0.0.0");
3569 #endif /* __linux__ */
3571 #if defined(IMLIB2) && defined(X11)
3576 Imlib_Image image, buffer;
3578 image = imlib_load_image(obj->data.s);
3579 imlib_context_set_image(image);
3583 w = imlib_image_get_width();
3584 h = imlib_image_get_height();
3585 buffer = imlib_create_image(w, h);
3586 imlib_context_set_display(display);
3587 imlib_context_set_drawable(window.drawable);
3588 imlib_context_set_colormap(DefaultColormap(display,
3590 imlib_context_set_visual(DefaultVisual(display,
3592 imlib_context_set_image(buffer);
3593 imlib_blend_image_onto_image(image, 0, 0, 0, w, h,
3594 text_start_x, text_start_y, w, h);
3595 imlib_render_image_on_drawable(text_start_x,
3598 imlib_context_set_image(image);
3606 struct information *tmp_info;
3607 struct text_object subroot, subroot2;
3609 tmp_info = malloc(sizeof(struct information));
3610 memcpy(tmp_info, cur, sizeof(struct information));
3611 parse_conky_vars(&subroot, obj->data.s, p, tmp_info);
3612 DBGP("evaluated '%s' to '%s'", obj->data.s, p);
3613 parse_conky_vars(&subroot2, p, p, tmp_info);
3615 free_text_objects(&subroot);
3616 free_text_objects(&subroot2);
3620 read_exec(obj->data.s, p, text_buffer_size);
3621 remove_deleted_chars(p);
3624 struct information *tmp_info;
3625 struct text_object subroot;
3627 read_exec(obj->data.s, p, text_buffer_size);
3629 tmp_info = malloc(sizeof(struct information));
3630 memcpy(tmp_info, cur, sizeof(struct information));
3631 parse_conky_vars(&subroot, p, p, tmp_info);
3633 free_text_objects(&subroot);
3639 read_exec(obj->data.s, p, text_buffer_size);
3640 barnum = get_barnum(p); /*using the same function*/
3642 if (barnum >= 0.0) {
3644 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
3650 read_exec(obj->data.s, p, text_buffer_size);
3651 barnum = get_barnum(p);
3653 if (barnum >= 0.0) {
3655 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
3659 char showaslog = FALSE;
3662 if(strncasecmp(obj->data.s, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
3664 read_exec(obj->data.s + strlen(LOGGRAPH" ") * sizeof(char), p, text_buffer_size);
3665 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
3666 read_exec(obj->data.s + strlen(NORMGRAPH" ") * sizeof(char), p, text_buffer_size);
3668 read_exec(obj->data.s, p, text_buffer_size);
3670 barnum = get_barnum(p);
3672 if (barnum >= 0.0) {
3673 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
3678 if (current_update_time - obj->data.execi.last_update
3679 >= obj->data.execi.interval) {
3682 read_exec(obj->data.execi.cmd, p, text_buffer_size);
3683 barnum = get_barnum(p);
3685 if (barnum >= 0.0) {
3686 obj->f = 255 * barnum / 100.0;
3688 obj->data.execi.last_update = current_update_time;
3690 new_bar(p, obj->a, obj->b, round_to_int(obj->f));
3693 if (current_update_time - obj->data.execi.last_update
3694 >= obj->data.execi.interval) {
3697 read_exec(obj->data.execi.cmd, p, text_buffer_size);
3698 barnum = get_barnum(p);
3700 if (barnum >= 0.0) {
3703 obj->data.execi.last_update = current_update_time;
3705 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, FALSE);
3708 if (current_update_time - obj->data.execi.last_update
3709 >= obj->data.execi.interval) {
3712 read_exec(obj->data.execi.cmd, p, text_buffer_size);
3713 barnum = get_barnum(p);
3715 if (barnum >= 0.0) {
3716 obj->f = 255 * barnum / 100.0;
3718 obj->data.execi.last_update = current_update_time;
3720 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
3723 if (current_update_time - obj->data.execi.last_update
3724 >= obj->data.execi.interval
3725 && obj->data.execi.interval != 0) {
3726 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
3728 obj->data.execi.last_update = current_update_time;
3730 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
3733 struct text_object subroot;
3734 struct information *tmp_info =
3735 malloc(sizeof(struct information));
3736 memcpy(tmp_info, cur, sizeof(struct information));
3738 if (current_update_time - obj->data.execi.last_update
3739 < obj->data.execi.interval
3740 || obj->data.execi.interval == 0) {
3741 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
3743 char *output = obj->data.execi.buffer;
3744 FILE *fp = popen(obj->data.execi.cmd, "r");
3745 int length = fread(output, 1, text_buffer_size, fp);
3749 output[length] = '\0';
3750 if (length > 0 && output[length - 1] == '\n') {
3751 output[length - 1] = '\0';
3754 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
3755 obj->data.execi.last_update = current_update_time;
3757 free_text_objects(&subroot);
3761 if (!obj->data.texeci.p_timed_thread) {
3762 obj->data.texeci.p_timed_thread =
3763 timed_thread_create(&threaded_exec,
3764 (void *) obj, obj->data.texeci.interval * 1000000);
3765 if (!obj->data.texeci.p_timed_thread) {
3766 ERR("Error creating texeci timed thread");
3768 timed_thread_register(obj->data.texeci.p_timed_thread,
3769 &obj->data.texeci.p_timed_thread);
3770 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
3771 ERR("Error running texeci timed thread");
3774 timed_thread_lock(obj->data.texeci.p_timed_thread);
3775 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
3776 timed_thread_unlock(obj->data.texeci.p_timed_thread);
3779 #endif /* HAVE_POPEN */
3781 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
3783 if (mail && mail->p_timed_thread) {
3784 timed_thread_lock(mail->p_timed_thread);
3785 snprintf(p, p_max_size, "%lu", mail->unseen);
3786 timed_thread_unlock(mail->p_timed_thread);
3789 OBJ(imap_messages) {
3790 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
3792 if (mail && mail->p_timed_thread) {
3793 timed_thread_lock(mail->p_timed_thread);
3794 snprintf(p, p_max_size, "%lu", mail->messages);
3795 timed_thread_unlock(mail->p_timed_thread);
3799 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
3801 if (mail && mail->p_timed_thread) {
3802 timed_thread_lock(mail->p_timed_thread);
3803 snprintf(p, p_max_size, "%lu", mail->unseen);
3804 timed_thread_unlock(mail->p_timed_thread);
3808 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
3810 if (mail && mail->p_timed_thread) {
3811 timed_thread_lock(mail->p_timed_thread);
3812 snprintf(p, p_max_size, "%.1f",
3813 mail->used / 1024.0 / 1024.0);
3814 timed_thread_unlock(mail->p_timed_thread);
3818 if (obj->data.fs != NULL) {
3819 if (obj->data.fs->size == 0) {
3820 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
3822 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
3823 (int) (255 - obj->data.fsbar.fs->avail * 255 /
3824 obj->data.fs->size));
3829 if (obj->data.fs != NULL) {
3830 human_readable( (obj->data.fs->free ? obj->data.fs->free :
3831 obj->data.fs->avail), p, 255);
3835 if (obj->data.fs != NULL) {
3838 if (obj->data.fs->size) {
3839 val = (obj->data.fs->free ? obj->data.fs->free :
3840 obj->data.fs->avail) * 100 /
3844 percent_print(p, p_max_size, val);
3848 if (obj->data.fs != NULL) {
3849 human_readable(obj->data.fs->size, p, 255);
3853 if (obj->data.fs != NULL)
3854 snprintf(p, p_max_size, "%s", obj->data.fs->type);
3857 if (obj->data.fs != NULL) {
3858 human_readable(obj->data.fs->size - (obj->data.fs->free
3859 ? obj->data.fs->free : obj->data.fs->avail), p, 255);
3863 if (obj->data.fs != NULL) {
3864 if (obj->data.fs->size == 0) {
3865 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
3867 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
3868 (int) (obj->data.fsbar.fs->avail * 255 /
3869 obj->data.fs->size));
3874 if (obj->data.fs != NULL) {
3877 if (obj->data.fs->size) {
3878 val = (obj->data.fs->free ? obj->data.fs->free :
3879 obj->data.fs->avail) * 100 /
3883 percent_print(p, p_max_size, 100 - val);
3887 float *v = info.loadavg;
3889 if (obj->data.loadavg[2]) {
3890 snprintf(p, p_max_size, "%.2f %.2f %.2f",
3891 v[obj->data.loadavg[0] - 1],
3892 v[obj->data.loadavg[1] - 1],
3893 v[obj->data.loadavg[2] - 1]);
3894 } else if (obj->data.loadavg[1]) {
3895 snprintf(p, p_max_size, "%.2f %.2f",
3896 v[obj->data.loadavg[0] - 1],
3897 v[obj->data.loadavg[1] - 1]);
3898 } else if (obj->data.loadavg[0]) {
3899 snprintf(p, p_max_size, "%.2f",
3900 v[obj->data.loadavg[0] - 1]);
3904 new_goto(p, obj->data.i);
3907 new_tab(p, obj->data.pair.a, obj->data.pair.b);
3910 new_hr(p, obj->data.i);
3913 if (cur->nameserver_info.nscount > obj->data.i)
3914 snprintf(p, p_max_size, "%s",
3915 cur->nameserver_info.ns_list[obj->data.i]);
3919 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
3920 snprintf(p, p_max_size, "%s", skill);
3925 PRSS *data = get_rss_info(obj->data.rss.uri,
3926 obj->data.rss.delay);
3930 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
3932 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
3934 // remove trailing new line if one exists
3935 if (str[strlen(str) - 1] == '\n') {
3936 str[strlen(str) - 1] = 0;
3938 snprintf(p, p_max_size, "%s", str);
3939 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
3940 if (obj->data.rss.act_par < data->item_count) {
3941 str = data->items[obj->data.rss.act_par].title;
3942 // remove trailing new line if one exists
3943 if (str[strlen(str) - 1] == '\n') {
3944 str[strlen(str) - 1] = 0;
3946 snprintf(p, p_max_size, "%s", str);
3948 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
3949 if (obj->data.rss.act_par < data->item_count) {
3951 data->items[obj->data.rss.act_par].description;
3952 // remove trailing new line if one exists
3953 if (str[strlen(str) - 1] == '\n') {
3954 str[strlen(str) - 1] = 0;
3956 snprintf(p, p_max_size, "%s", str);
3958 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
3959 if (data->item_count > 0) {
3962 //'tmpspaces' is a string with spaces too be placed in front of each title
3963 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
3964 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
3965 tmpspaces[obj->data.rss.nrspaces]=0;
3969 if (obj->data.rss.act_par > data->item_count) {
3970 show = data->item_count;
3972 show = obj->data.rss.act_par;
3974 for (itmp = 0; itmp < show; itmp++) {
3975 PRSS_Item *item = &data->items[itmp];
3979 // don't add new line before first item
3981 strncat(p, "\n", p_max_size);
3983 /* remove trailing new line if one exists,
3984 * we have our own */
3985 if (str[strlen(str) - 1] == '\n') {
3986 str[strlen(str) - 1] = 0;
3988 strncat(p, tmpspaces, p_max_size);
3989 strncat(p, str, p_max_size);
4002 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4003 if (obj->data.hddtemp.temp)
4004 free(obj->data.hddtemp.temp);
4005 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4006 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4007 obj->data.hddtemp.update_time = current_update_time;
4009 if (!obj->data.hddtemp.temp) {
4010 snprintf(p, p_max_size, "N/A");
4012 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4013 unit = obj->data.hddtemp.temp[0];
4015 if (*endptr != '\0')
4016 snprintf(p, p_max_size, "N/A");
4017 else if (unit == 'C')
4018 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4019 else if (unit == 'F')
4020 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4022 snprintf(p, p_max_size, "N/A");
4027 new_offset(p, obj->data.i);
4030 new_voffset(p, obj->data.i);
4036 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4037 obj->data.sysfs.devtype, obj->data.sysfs.type);
4039 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4040 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4041 } else if (r >= 100.0 || r == 0) {
4042 snprintf(p, p_max_size, "%d", (int) r);
4044 snprintf(p, p_max_size, "%.1f", r);
4050 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4051 obj->data.sysfs.devtype, obj->data.sysfs.type);
4053 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4054 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4055 } else if (r >= 100.0 || r == 0) {
4056 snprintf(p, p_max_size, "%d", (int) r);
4058 snprintf(p, p_max_size, "%.1f", r);
4064 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4065 obj->data.sysfs.devtype, obj->data.sysfs.type);
4067 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4068 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4069 } else if (r >= 100.0 || r == 0) {
4070 snprintf(p, p_max_size, "%d", (int) r);
4072 snprintf(p, p_max_size, "%.1f", r);
4075 #endif /* __linux__ */
4077 new_alignr(p, obj->data.i);
4080 new_alignc(p, obj->data.i);
4083 char buf[max_user_text];
4084 struct information *tmp_info =
4085 malloc(sizeof(struct information));
4086 memcpy(tmp_info, cur, sizeof(struct information));
4087 generate_text_internal(buf, max_user_text,
4088 *obj->sub, tmp_info);
4090 if (strlen(buf) != 0) {
4096 char expression[max_user_text];
4098 struct information *tmp_info;
4100 tmp_info = malloc(sizeof(struct information));
4101 memcpy(tmp_info, cur, sizeof(struct information));
4102 generate_text_internal(expression, max_user_text,
4103 *obj->sub, tmp_info);
4104 DBGP("parsed arg into '%s'", expression);
4106 val = compare(expression);
4108 ERR("compare failed for expression '%s'",
4116 if (obj->data.ifblock.str
4117 && !check_contains(obj->data.ifblock.s,
4118 obj->data.ifblock.str)) {
4120 } else if (obj->data.ifblock.s
4121 && access(obj->data.ifblock.s, F_OK)) {
4126 if ((obj->data.ifblock.s)
4127 && (!check_mount(obj->data.ifblock.s))) {
4132 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4136 #if defined(__linux__)
4138 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4142 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4145 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4150 human_readable(cur->mem * 1024, p, 255);
4153 human_readable(cur->memeasyfree * 1024, p, 255);
4156 human_readable(cur->memfree * 1024, p, 255);
4159 human_readable(cur->memmax * 1024, p, 255);
4163 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4166 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4167 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4170 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4171 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4174 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4175 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4176 100, 1, obj->showaslog);
4181 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4184 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4187 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4190 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4191 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4194 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4195 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4198 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4199 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4201 OBJ(if_mixer_mute) {
4202 if (!mixer_is_mute(obj->data.ifblock.i)) {
4208 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4210 OBJ(monitor_number) {
4211 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4217 update_mail_count(&obj->data.local_mail);
4218 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4221 update_mail_count(&obj->data.local_mail);
4222 snprintf(p, p_max_size, "%d",
4223 obj->data.local_mail.new_mail_count);
4226 update_mail_count(&obj->data.local_mail);
4227 snprintf(p, p_max_size, "%d",
4228 obj->data.local_mail.seen_mail_count);
4231 update_mail_count(&obj->data.local_mail);
4232 snprintf(p, p_max_size, "%d",
4233 obj->data.local_mail.unseen_mail_count);
4235 OBJ(flagged_mails) {
4236 update_mail_count(&obj->data.local_mail);
4237 snprintf(p, p_max_size, "%d",
4238 obj->data.local_mail.flagged_mail_count);
4240 OBJ(unflagged_mails) {
4241 update_mail_count(&obj->data.local_mail);
4242 snprintf(p, p_max_size, "%d",
4243 obj->data.local_mail.unflagged_mail_count);
4245 OBJ(forwarded_mails) {
4246 update_mail_count(&obj->data.local_mail);
4247 snprintf(p, p_max_size, "%d",
4248 obj->data.local_mail.forwarded_mail_count);
4250 OBJ(unforwarded_mails) {
4251 update_mail_count(&obj->data.local_mail);
4252 snprintf(p, p_max_size, "%d",
4253 obj->data.local_mail.unforwarded_mail_count);
4255 OBJ(replied_mails) {
4256 update_mail_count(&obj->data.local_mail);
4257 snprintf(p, p_max_size, "%d",
4258 obj->data.local_mail.replied_mail_count);
4260 OBJ(unreplied_mails) {
4261 update_mail_count(&obj->data.local_mail);
4262 snprintf(p, p_max_size, "%d",
4263 obj->data.local_mail.unreplied_mail_count);
4266 update_mail_count(&obj->data.local_mail);
4267 snprintf(p, p_max_size, "%d",
4268 obj->data.local_mail.draft_mail_count);
4270 OBJ(trashed_mails) {
4271 update_mail_count(&obj->data.local_mail);
4272 snprintf(p, p_max_size, "%d",
4273 obj->data.local_mail.trashed_mail_count);
4276 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
4278 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4281 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
4284 new_outline(p, obj->data.l);
4287 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
4289 OBJ(running_processes) {
4290 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
4293 snprintf(p, p_max_size, "%s", obj->data.s);
4296 new_bg(p, obj->data.l);
4299 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
4302 human_readable(cur->swap * 1024, p, 255);
4305 human_readable(cur->swapmax * 1024, p, 255);
4308 if (cur->swapmax == 0) {
4309 strncpy(p, "No swap", p_max_size);
4311 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
4315 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4316 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
4319 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4322 time_t t = time(NULL);
4323 struct tm *tm = localtime(&t);
4325 setlocale(LC_TIME, "");
4326 strftime(p, p_max_size, obj->data.s, tm);
4329 time_t t = time(NULL);
4330 struct tm *tm = gmtime(&t);
4332 strftime(p, p_max_size, obj->data.s, tm);
4339 if (obj->data.tztime.tz) {
4340 oldTZ = getenv("TZ");
4341 setenv("TZ", obj->data.tztime.tz, 1);
4347 setlocale(LC_TIME, "");
4348 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4350 setenv("TZ", oldTZ, 1);
4355 // Needless to free oldTZ since getenv gives ptr to static data
4358 human_readable(obj->data.net->recv, p, 255);
4361 human_readable(obj->data.net->trans, p, 255);
4364 snprintf(p, p_max_size, "%d", total_updates);
4367 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
4372 human_readable(obj->data.net->trans_speed, p, 255);
4375 spaced_print(p, p_max_size, "%.1f", 8,
4376 obj->data.net->trans_speed / 1024.0);
4379 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4380 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->showaslog);
4383 format_seconds_short(p, p_max_size, (int) cur->uptime);
4386 format_seconds(p, p_max_size, (int) cur->uptime);
4389 snprintf(p, p_max_size, "%s", cur->users.names);
4392 snprintf(p, p_max_size, "%s", cur->users.terms);
4395 snprintf(p, p_max_size, "%s", cur->users.times);
4398 snprintf(p, p_max_size, "%d", cur->users.number);
4400 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
4401 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4405 msg = get_apm_adapter();
4406 snprintf(p, p_max_size, "%s", msg);
4409 OBJ(apm_battery_life) {
4412 msg = get_apm_battery_life();
4413 snprintf(p, p_max_size, "%s", msg);
4416 OBJ(apm_battery_time) {
4419 msg = get_apm_battery_time();
4420 snprintf(p, p_max_size, "%s", msg);
4423 #endif /* __FreeBSD__ __OpenBSD__ */
4426 #define mpd_printf(fmt, val) \
4427 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
4428 #define mpd_sprintf(val) { \
4429 if (!obj->data.i || obj->data.i > p_max_size) \
4430 mpd_printf("%s", val); \
4432 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
4437 mpd_sprintf(artist);
4441 mpd_printf("%s", random);
4443 mpd_printf("%s", repeat);
4451 mpd_printf("%d", volume);
4453 mpd_printf("%d", bitrate);
4455 mpd_printf("%s", status);
4457 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
4460 format_media_player_time(p, p_max_size, mpd_get_info()->length);
4463 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
4466 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4467 (int) (mpd_get_info()->progress * 255.0f));
4470 struct mpd_s *mpd = mpd_get_info();
4471 int len = obj->data.i;
4472 if (len == 0 || len > p_max_size)
4475 memset(p, 0, p_max_size);
4476 if (mpd->artist && *mpd->artist &&
4477 mpd->title && *mpd->title) {
4478 snprintf(p, len, "%s - %s", mpd->artist,
4480 } else if (mpd->title && *mpd->title) {
4481 snprintf(p, len, "%s", mpd->title);
4482 } else if (mpd->artist && *mpd->artist) {
4483 snprintf(p, len, "%s", mpd->artist);
4484 } else if (mpd->file && *mpd->file) {
4485 snprintf(p, len, "%s", mpd->file);
4490 OBJ(if_mpd_playing) {
4491 if (!mpd_get_info()->is_playing) {
4504 #define MOC_PRINT(t, a) \
4505 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
4507 MOC_PRINT(state, "??");
4510 MOC_PRINT(file, "no file");
4513 MOC_PRINT(title, "no title");
4516 MOC_PRINT(artist, "no artist");
4519 MOC_PRINT(song, "no song");
4522 MOC_PRINT(album, "no album");
4524 OBJ(moc_totaltime) {
4525 MOC_PRINT(totaltime, "0:00");
4528 MOC_PRINT(timeleft, "0:00");
4531 MOC_PRINT(curtime, "0:00");
4534 MOC_PRINT(bitrate, "0Kbps");
4537 MOC_PRINT(rate, "0KHz");
4543 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4546 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4549 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4552 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4554 OBJ(xmms2_comment) {
4555 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4558 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4561 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4564 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4566 OBJ(xmms2_tracknr) {
4567 if (cur->xmms2.tracknr != -1) {
4568 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4571 OBJ(xmms2_bitrate) {
4572 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4575 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4578 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4580 OBJ(xmms2_elapsed) {
4581 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
4582 (cur->xmms2.elapsed / 1000) % 60);
4584 OBJ(xmms2_duration) {
4585 snprintf(p, p_max_size, "%02d:%02d",
4586 cur->xmms2.duration / 60000,
4587 (cur->xmms2.duration / 1000) % 60);
4589 OBJ(xmms2_percent) {
4590 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
4593 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4594 (int) (cur->xmms2.progress * 255.0f));
4596 OBJ(xmms2_playlist) {
4597 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
4599 OBJ(xmms2_timesplayed) {
4600 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
4603 if (strlen(cur->xmms2.title) < 2
4604 && strlen(cur->xmms2.title) < 2) {
4605 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4607 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
4611 OBJ(if_xmms2_connected) {
4612 if (cur->xmms2.conn_state != 1) {
4618 OBJ(audacious_status) {
4619 snprintf(p, p_max_size, "%s",
4620 cur->audacious.items[AUDACIOUS_STATUS]);
4622 OBJ(audacious_title) {
4623 snprintf(p, cur->audacious.max_title_len > 0
4624 ? cur->audacious.max_title_len : p_max_size, "%s",
4625 cur->audacious.items[AUDACIOUS_TITLE]);
4627 OBJ(audacious_length) {
4628 snprintf(p, p_max_size, "%s",
4629 cur->audacious.items[AUDACIOUS_LENGTH]);
4631 OBJ(audacious_length_seconds) {
4632 snprintf(p, p_max_size, "%s",
4633 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4635 OBJ(audacious_position) {
4636 snprintf(p, p_max_size, "%s",
4637 cur->audacious.items[AUDACIOUS_POSITION]);
4639 OBJ(audacious_position_seconds) {
4640 snprintf(p, p_max_size, "%s",
4641 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4643 OBJ(audacious_bitrate) {
4644 snprintf(p, p_max_size, "%s",
4645 cur->audacious.items[AUDACIOUS_BITRATE]);
4647 OBJ(audacious_frequency) {
4648 snprintf(p, p_max_size, "%s",
4649 cur->audacious.items[AUDACIOUS_FREQUENCY]);
4651 OBJ(audacious_channels) {
4652 snprintf(p, p_max_size, "%s",
4653 cur->audacious.items[AUDACIOUS_CHANNELS]);
4655 OBJ(audacious_filename) {
4656 snprintf(p, p_max_size, "%s",
4657 cur->audacious.items[AUDACIOUS_FILENAME]);
4659 OBJ(audacious_playlist_length) {
4660 snprintf(p, p_max_size, "%s",
4661 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4663 OBJ(audacious_playlist_position) {
4664 snprintf(p, p_max_size, "%s",
4665 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4667 OBJ(audacious_main_volume) {
4668 snprintf(p, p_max_size, "%s",
4669 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
4671 OBJ(audacious_bar) {
4675 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4676 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4677 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
4683 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4686 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4689 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4692 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4695 snprintf(p, p_max_size, "%i", cur->bmpx.track);
4698 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4701 /* we have three different types of top (top, top_mem
4702 * and top_time). To avoid having almost-same code three
4703 * times, we have this special handler. */
4706 if (!needed) needed = cur->cpu;
4708 if (!needed) needed = cur->memu;
4710 if (!needed) needed = cur->time;
4715 switch (obj->data.top.type) {
4717 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
4718 needed[obj->data.top.num]->name);
4721 snprintf(p, 7, "%6.2f",
4722 needed[obj->data.top.num]->amount);
4725 snprintf(p, 6, "%5i",
4726 needed[obj->data.top.num]->pid);
4729 snprintf(p, 7, "%6.2f",
4730 needed[obj->data.top.num]->totalmem);
4733 timeval = format_time(
4734 needed[obj->data.top.num]->total_cpu_time, 9);
4735 snprintf(p, 10, "%9s", timeval);
4739 human_readable(needed[obj->data.top.num]->rss,
4743 human_readable(needed[obj->data.top.num]->vsize,
4749 print_tail_object(obj, p, p_max_size);
4751 print_head_object(obj, p, p_max_size);
4753 FILE *fp = fopen(obj->data.s,"r");
4756 /* FIXME: use something more general (see also tail.c, head.c */
4757 #define BUFSZ 0x1000
4762 while(fgets(buf, BUFSZ, fp) != NULL){
4763 for(j = 0; buf[j] != 0; j++) {
4764 if(buf[j] == '\n') {
4769 sprintf(p, "%d", lines);
4772 sprintf(p, "File Unreadable");
4777 FILE *fp = fopen(obj->data.s,"r");
4782 char inword = FALSE;
4785 while(fgets(buf, BUFSZ, fp) != NULL){
4786 for(j = 0; buf[j] != 0; j++) {
4787 if(!isspace(buf[j])) {
4788 if(inword == FALSE) {
4797 sprintf(p, "%d", words);
4800 sprintf(p, "File Unreadable");
4803 #ifdef TCP_PORT_MONITOR
4805 tcp_portmon_action(p, p_max_size,
4806 &obj->data.tcp_port_monitor);
4812 iconv_converting = 1;
4813 iconv_selected = obj->a;
4816 iconv_converting = 0;
4821 OBJ(entropy_avail) {
4822 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
4824 OBJ(entropy_poolsize) {
4825 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
4828 double entropy_perc;
4830 entropy_perc = (double) cur->entropy.entropy_avail /
4831 (double) cur->entropy.poolsize;
4832 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
4838 s = smapi_get_val(obj->data.s);
4839 snprintf(p, p_max_size, "%s", s);
4843 OBJ(if_smapi_bat_installed) {
4845 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
4846 if(!smapi_bat_installed(idx)) {
4850 ERR("argument to if_smapi_bat_installed must be an integer");
4852 OBJ(smapi_bat_perc) {
4854 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
4855 val = smapi_bat_installed(idx) ?
4856 smapi_get_bat_int(idx, "remaining_percent") : 0;
4857 percent_print(p, p_max_size, val);
4859 ERR("argument to smapi_bat_perc must be an integer");
4861 OBJ(smapi_bat_temp) {
4863 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
4864 val = smapi_bat_installed(idx) ?
4865 smapi_get_bat_int(idx, "temperature") : 0;
4866 /* temperature is in milli degree celsius */
4867 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
4869 ERR("argument to smapi_bat_temp must be an integer");
4871 OBJ(smapi_bat_power) {
4873 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
4874 val = smapi_bat_installed(idx) ?
4875 smapi_get_bat_int(idx, "power_now") : 0;
4876 /* power_now is in mW, set to W with one digit precision */
4877 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
4879 ERR("argument to smapi_bat_power must be an integer");
4881 OBJ(smapi_bat_bar) {
4882 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
4883 new_bar(p, obj->a, obj->b, (int)
4884 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
4886 new_bar(p, obj->a, obj->b, 0);
4891 char *tmp, buf[max_user_text];
4892 generate_text_internal(buf, max_user_text,
4895 if (strlen(buf) <= obj->data.scroll.show) {
4896 snprintf(p, p_max_size, "%s", buf);
4899 #define LINESEPARATOR '|'
4900 //place all the lines behind each other with LINESEPARATOR between them
4901 for(j = 0; buf[j] != 0; j++) {
4903 buf[j]=LINESEPARATOR;
4906 //scroll the output obj->data.scroll.start places by copying that many chars from
4907 //the front of the string to tmp, scrolling the rest to the front and placing tmp
4908 //at the back of the string
4909 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
4910 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
4911 for(j = obj->data.scroll.start; buf[j] != 0; j++){
4912 buf[j - obj->data.scroll.start] = buf[j];
4914 strcpy(&buf[j - obj->data.scroll.start], tmp);
4916 //only show the requested number of chars
4917 if(obj->data.scroll.show < j) {
4918 buf[obj->data.scroll.show] = 0;
4920 //next time, scroll a place more or reset scrolling if we are at the end
4921 obj->data.scroll.start += obj->data.scroll.step;
4922 if(obj->data.scroll.start >= j){
4923 obj->data.scroll.start = 0;
4925 snprintf(p, p_max_size, "%s", buf);
4928 char buf[2][max_user_text];
4930 unsigned int longest=0;
4931 unsigned int nextstart;
4932 unsigned int nr_rows[2];
4935 struct llrows* next;
4937 struct llrows *ll_rows[2], *current[2];
4938 struct text_object * objsub = obj->sub;
4941 for(i=0; i<2; i++) {
4944 ll_rows[i] = malloc(sizeof(struct llrows));
4945 current[i] = ll_rows[i];
4946 for(j=0; j<i; j++) objsub = objsub->sub;
4947 generate_text_internal(buf[i], max_user_text, *objsub, cur);
4948 for(j=0; buf[i][j] != 0; j++) {
4949 if(buf[i][j] == '\t') buf[i][j] = ' ';
4950 if(buf[i][j] == '\n') {
4952 current[i]->row = strdup(buf[i]+nextstart);
4953 if(i==0 && strlen(current[i]->row) > longest) longest = strlen(current[i]->row);
4954 current[i]->next = malloc(sizeof(struct llrows));
4955 current[i] = current[i]->next;
4960 current[i]->row = strdup(buf[i]+nextstart);
4961 if(i==0 && strlen(current[i]->row) > longest) longest = strlen(current[i]->row);
4962 current[i]->next = NULL;
4963 current[i] = ll_rows[i];
4965 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
4967 strcat(p, current[0]->row);
4968 i=strlen(current[0]->row);
4970 while(i < longest) {
4975 strcat(p, obj->data.combine.seperation);
4976 strcat(p, current[1]->row);
4979 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
4981 for(i=0; i<2; i++) {
4982 while(ll_rows[i] != NULL) {
4983 current[i]=ll_rows[i];
4984 free(current[i]->row);
4985 ll_rows[i]=current[i]->next;
4992 int value = get_nvidia_value(obj->data.nvidia.type, display);
4994 snprintf(p, p_max_size, "N/A");
4995 else if (obj->data.nvidia.type == NV_TEMP)
4996 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
4997 else if (obj->data.nvidia.print_as_float &&
4998 value > 0 && value < 100)
4999 snprintf(p, p_max_size, "%.1f", (float)value);
5001 snprintf(p, p_max_size, "%d", value);
5011 unsigned int a = strlen(p);
5014 if (a > 0 && iconv_converting && iconv_selected > 0
5015 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5017 size_t dummy1, dummy2;
5018 char *ptr = buff_in;
5021 dummy1 = dummy2 = a;
5023 strncpy(buff_in, p, p_max_size);
5025 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5026 while (dummy1 > 0) {
5027 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5030 ERR("Iconv codeset conversion failed");
5035 /* It is nessecary when we are converting from multibyte to
5036 * singlebyte codepage */
5039 #endif /* HAVE_ICONV */
5046 double current_update_time, next_update_time, last_update_time;
5048 static void generate_text(void)
5050 struct information *cur = &info;
5057 current_update_time = get_time();
5061 /* add things to the buffer */
5067 generate_text_internal(p, max_user_text, global_root_object, cur);
5069 if (stuff_in_upper_case) {
5072 tmp_p = text_buffer;
5074 *tmp_p = toupper(*tmp_p);
5079 next_update_time += update_interval;
5080 if (next_update_time < get_time()) {
5081 next_update_time = get_time() + update_interval;
5082 } else if (next_update_time > get_time() + update_interval) {
5083 next_update_time = get_time() + update_interval;
5085 last_update_time = current_update_time;
5090 static inline int get_string_width(const char *s)
5093 if (output_methods & TO_X) {
5094 return *s ? calc_text_width(s, strlen(s)) : 0;
5100 static inline int get_string_width_special(char *s)
5108 if ((output_methods & TO_X) == 0) {
5110 return (s) ? strlen(s) : 0;
5118 p = strndup(s, text_buffer_size);
5122 if (*p == SPECIAL_CHAR) {
5123 /* shift everything over by 1 so that the special char
5124 * doesn't mess up the size calculation */
5125 for (i = 0; i < (long)strlen(p); i++) {
5126 *(p + i) = *(p + i + 1);
5128 if (specials[special_index + idx].type == GRAPH
5129 || specials[special_index + idx].type == GAUGE
5130 || specials[special_index + idx].type == BAR) {
5131 width += specials[special_index + idx].width;
5138 if (strlen(final) > 1) {
5139 width += calc_text_width(final, strlen(final));
5147 static void text_size_updater(char *s);
5149 int last_font_height;
5150 static void update_text_area(void)
5154 if ((output_methods & TO_X) == 0)
5156 /* update text size if it isn't fixed */
5161 text_width = minimum_width;
5164 last_font_height = font_height();
5165 for_each_line(text_buffer, text_size_updater);
5167 if (text_height < minimum_height) {
5168 text_height = minimum_height;
5170 if (text_width > maximum_width && maximum_width > 0) {
5171 text_width = maximum_width;
5175 /* get text position on workarea */
5176 switch (text_alignment) {
5183 x = workarea[2] - text_width - gap_x;
5188 x = workarea[2] / 2 - text_width / 2 - gap_x;
5195 y = workarea[3] - text_height - gap_y;
5199 x = workarea[2] - text_width - gap_x;
5200 y = workarea[3] - text_height - gap_y;
5204 x = workarea[2] / 2 - text_width / 2 - gap_x;
5205 y = workarea[3] - text_height - gap_y;
5210 y = workarea[3] / 2 - text_height / 2 - gap_y;
5214 x = workarea[2] - text_width - gap_x;
5215 y = workarea[3] / 2 - text_height / 2 - gap_y;
5219 case NONE: // Let the WM manage the window
5230 if (own_window && !fixed_pos) {
5233 text_start_x = border_margin + 1;
5234 text_start_y = border_margin + 1;
5235 window.x = x - border_margin - 1;
5236 window.y = y - border_margin - 1;
5240 /* If window size doesn't match to workarea's size,
5241 * then window probably includes panels (gnome).
5242 * Blah, doesn't work on KDE. */
5243 if (workarea[2] != window.width || workarea[3] != window.height) {
5255 static int cur_x, cur_y; /* current x and y for drawing */
5257 //draw_mode also without X11 because we only need to print to stdout with FG
5258 static int draw_mode; /* FG, BG or OUTLINE */
5260 static long current_color;
5262 static void text_size_updater(char *s)
5267 if ((output_methods & TO_X) == 0)
5269 /* get string widths and skip specials */
5272 if (*p == SPECIAL_CHAR) {
5274 w += get_string_width(s);
5277 if (specials[special_index].type == BAR
5278 || specials[special_index].type == GAUGE
5279 || specials[special_index].type == GRAPH) {
5280 w += specials[special_index].width;
5281 if (specials[special_index].height > last_font_height) {
5282 last_font_height = specials[special_index].height;
5283 last_font_height += font_height();
5285 } else if (specials[special_index].type == OFFSET) {
5286 if (specials[special_index].arg > 0) {
5287 w += specials[special_index].arg;
5289 } else if (specials[special_index].type == VOFFSET) {
5290 last_font_height += specials[special_index].arg;
5291 } else if (specials[special_index].type == GOTO) {
5292 if (specials[special_index].arg > cur_x) {
5293 w = (int) specials[special_index].arg;
5295 } else if (specials[special_index].type == TAB) {
5296 int start = specials[special_index].arg;
5297 int step = specials[special_index].width;
5299 if (!step || step < 0) {
5302 w += step - (cur_x - text_start_x - start) % step;
5303 } else if (specials[special_index].type == FONT) {
5304 selected_font = specials[special_index].font_added;
5305 if (font_height() > last_font_height) {
5306 last_font_height = font_height();
5315 w += get_string_width(s);
5316 if (w > text_width) {
5319 if (text_width > maximum_width && maximum_width) {
5320 text_width = maximum_width;
5323 text_height += last_font_height;
5324 last_font_height = font_height();
5327 static inline void set_foreground_color(long c)
5329 if ((output_methods & TO_X) == 0)
5332 XSetForeground(display, window.gc, c);
5336 static void draw_string(const char *s)
5338 int i, i2, pos, width_of_s;
5346 width_of_s = get_string_width(s);
5347 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
5349 fflush(stdout); /* output immediately, don't buffer */
5351 if ((output_methods & TO_STDERR) && draw_mode == FG) {
5352 fprintf(stderr, "%s\n", s);
5353 fflush(stderr); /* output immediately, don't buffer */
5355 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
5356 fprintf(overwrite_fpointer, "%s\n", s);
5358 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
5359 fprintf(append_fpointer, "%s\n", s);
5361 memset(tmpstring1, 0, text_buffer_size);
5362 memset(tmpstring2, 0, text_buffer_size);
5363 strncpy(tmpstring1, s, text_buffer_size - 1);
5368 if (output_methods & TO_X) {
5369 max = ((text_width - width_of_s) / get_string_width(" "));
5372 /* This code looks for tabs in the text and coverts them to spaces.
5373 * The trick is getting the correct number of spaces, and not going
5374 * over the window's size without forcing the window larger. */
5375 for (i = 0; i < (int) text_buffer_size; i++) {
5376 if (tmpstring1[i] == '\t') {
5378 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
5379 /* guard against overrun */
5380 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
5385 /* guard against overrun */
5386 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
5391 if (output_methods & TO_X) {
5392 if (text_width == maximum_width) {
5393 /* this means the text is probably pushing the limit,
5394 * so we'll chop it */
5395 while (cur_x + get_string_width(tmpstring2) - text_start_x
5396 > maximum_width && strlen(tmpstring2) > 0) {
5397 tmpstring2[strlen(tmpstring2) - 1] = '\0';
5404 if (output_methods & TO_X) {
5410 c.pixel = current_color;
5411 XQueryColor(display, DefaultColormap(display, screen), &c);
5414 c2.color.red = c.red;
5415 c2.color.green = c.green;
5416 c2.color.blue = c.blue;
5417 c2.color.alpha = fonts[selected_font].font_alpha;
5419 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5420 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5422 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5423 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5428 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
5431 cur_x += width_of_s;
5434 memcpy(tmpstring1, s, text_buffer_size);
5437 static void draw_line(char *s)
5446 if ((output_methods & TO_X) == 0) {
5451 cur_x = text_start_x;
5452 cur_y += font_ascent();
5453 font_h = font_height();
5455 /* find specials and draw stuff */
5458 if (*p == SPECIAL_CHAR) {
5461 /* draw string before special */
5468 switch (specials[special_index].type) {
5469 case HORIZONTAL_LINE:
5471 int h = specials[special_index].height;
5472 int mid = font_ascent() / 2;
5474 w = text_start_x + text_width - cur_x;
5476 XSetLineAttributes(display, window.gc, h, LineSolid,
5477 CapButt, JoinMiter);
5478 XDrawLine(display, window.drawable, window.gc, cur_x,
5479 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
5485 int h = specials[special_index].height;
5486 int tmp_s = specials[special_index].arg;
5487 int mid = font_ascent() / 2;
5488 char ss[2] = { tmp_s, tmp_s };
5490 w = text_start_x + text_width - cur_x - 1;
5491 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
5492 CapButt, JoinMiter);
5493 XSetDashes(display, window.gc, 0, ss, 2);
5494 XDrawLine(display, window.drawable, window.gc, cur_x,
5495 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
5501 int h, bar_usage, by;
5502 if (cur_x - text_start_x > maximum_width
5503 && maximum_width > 0) {
5506 h = specials[special_index].height;
5507 bar_usage = specials[special_index].arg;
5508 by = cur_y - (font_ascent() / 2) - 1;
5513 w = specials[special_index].width;
5515 w = text_start_x + text_width - cur_x - 1;
5521 XSetLineAttributes(display, window.gc, 1, LineSolid,
5522 CapButt, JoinMiter);
5524 XDrawRectangle(display, window.drawable, window.gc, cur_x,
5526 XFillRectangle(display, window.drawable, window.gc, cur_x,
5527 by, w * bar_usage / 255, h);
5535 case GAUGE: /* new GAUGE */
5538 unsigned long last_colour = current_color;
5539 float angle, px, py;
5542 if (cur_x - text_start_x > maximum_width
5543 && maximum_width > 0) {
5547 h = specials[special_index].height;
5548 by = cur_y - (font_ascent() / 2) - 1;
5553 w = specials[special_index].width;
5555 w = text_start_x + text_width - cur_x - 1;
5561 XSetLineAttributes(display, window.gc, 1, LineSolid,
5562 CapButt, JoinMiter);
5564 XDrawArc(display, window.drawable, window.gc,
5565 cur_x, by, w, h * 2, 0, 180*64);
5568 usage = specials[special_index].arg;
5569 angle = (M_PI)*(float)(usage)/255.;
5570 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
5571 py = (float)(by+(h))-(float)(h)*sin(angle);
5573 XDrawLine(display, window.drawable, window.gc,
5574 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
5582 set_foreground_color(last_colour);
5590 int h, by, i, j = 0;
5591 int gradient_size = 0;
5592 float gradient_factor = 0;
5593 float gradient_update = 0;
5594 unsigned long last_colour = current_color;
5595 unsigned long tmpcolour = current_color;
5596 if (cur_x - text_start_x > maximum_width
5597 && maximum_width > 0) {
5600 h = specials[special_index].height;
5601 by = cur_y - (font_ascent() / 2) - 1;
5606 w = specials[special_index].width;
5608 w = text_start_x + text_width - cur_x - 1;
5613 if (draw_graph_borders) {
5614 XSetLineAttributes(display, window.gc, 1, LineSolid,
5615 CapButt, JoinMiter);
5616 XDrawRectangle(display, window.drawable, window.gc,
5619 XSetLineAttributes(display, window.gc, 1, LineSolid,
5620 CapButt, JoinMiter);
5622 if (specials[special_index].last_colour != 0
5623 || specials[special_index].first_colour != 0) {
5624 tmpcolour = specials[special_index].last_colour;
5626 gradient_max(specials[special_index].last_colour,
5627 specials[special_index].first_colour);
5628 gradient_factor = (float) gradient_size / (w - 2);
5630 for (i = w - 2; i > -1; i--) {
5631 if (specials[special_index].last_colour != 0
5632 || specials[special_index].first_colour != 0) {
5633 XSetForeground(display, window.gc, tmpcolour);
5634 gradient_update += gradient_factor;
5635 while (gradient_update > 0) {
5636 tmpcolour = do_gradient(tmpcolour,
5637 specials[special_index].first_colour);
5641 if ((w - i) / ((float) (w - 2) /
5642 (specials[special_index].graph_width)) > j
5643 && j < MAX_GRAPH_DEPTH - 3) {
5646 /* this is mugfugly, but it works */
5647 XDrawLine(display, window.drawable, window.gc,
5648 cur_x + i + 1, by + h, cur_x + i + 1,
5649 by + h - specials[special_index].graph[j] *
5650 (h - 1) / specials[special_index].graph_scale);
5656 /* if (draw_mode == BG) {
5657 set_foreground_color(default_bg_color);
5658 } else if (draw_mode == OUTLINE) {
5659 set_foreground_color(default_out_color);
5661 set_foreground_color(default_fg_color);
5663 if (show_graph_range) {
5666 unsigned short int seconds = update_interval * w;
5671 unsigned short int timeunits;
5673 timeunits = seconds / 86400; seconds %= 86400;
5674 if (timeunits > 0) {
5675 asprintf(&tmp_day_str, "%dd", timeunits);
5677 tmp_day_str = strdup("");
5679 timeunits = seconds / 3600; seconds %= 3600;
5680 if (timeunits > 0) {
5681 asprintf(&tmp_hour_str, "%dh", timeunits);
5683 tmp_hour_str = strdup("");
5685 timeunits = seconds / 60; seconds %= 60;
5686 if (timeunits > 0) {
5687 asprintf(&tmp_min_str, "%dm", timeunits);
5689 tmp_min_str = strdup("");
5692 asprintf(&tmp_sec_str, "%ds", seconds);
5694 tmp_sec_str = strdup("");
5696 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
5697 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
5699 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
5701 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
5702 cur_y += font_h / 2;
5703 draw_string(tmp_str);
5709 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
5712 cur_x += font_ascent() / 2;
5713 cur_y += font_h / 2;
5715 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
5717 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
5718 draw_string(tmp_str);
5724 set_foreground_color(last_colour);
5730 int old = font_ascent();
5732 cur_y -= font_ascent();
5733 selected_font = specials[special_index].font_added;
5734 if (cur_y + font_ascent() < cur_y + old) {
5737 cur_y += font_ascent();
5740 font_h = font_height();
5744 if (draw_mode == FG) {
5745 set_foreground_color(specials[special_index].arg);
5750 if (draw_mode == BG) {
5751 set_foreground_color(specials[special_index].arg);
5756 if (draw_mode == OUTLINE) {
5757 set_foreground_color(specials[special_index].arg);
5762 w += specials[special_index].arg;
5766 cur_y += specials[special_index].arg;
5770 if (specials[special_index].arg >= 0) {
5771 cur_x = (int) specials[special_index].arg;
5777 int start = specials[special_index].arg;
5778 int step = specials[special_index].width;
5780 if (!step || step < 0) {
5783 w = step - (cur_x - text_start_x - start) % step;
5789 /* TODO: add back in "+ border_margin" to the end of
5791 int pos_x = text_start_x + text_width -
5792 get_string_width_special(s);
5794 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
5795 "get_string_width(p) %i gap_x %i "
5796 "specials[special_index].arg %i border_margin %i "
5797 "border_width %i\n", pos_x, text_start_x, text_width,
5798 cur_x, get_string_width_special(s), gap_x,
5799 specials[special_index].arg, border_margin,
5801 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5802 cur_x = pos_x - specials[special_index].arg;
5809 int pos_x = (text_width) / 2 - get_string_width_special(s) /
5810 2 - (cur_x - text_start_x);
5811 /* int pos_x = text_start_x + text_width / 2 -
5812 get_string_width_special(s) / 2; */
5814 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
5815 "get_string_width(p) %i gap_x %i "
5816 "specials[special_index].arg %i\n", pos_x, text_start_x,
5817 text_width, cur_x, get_string_width(s), gap_x,
5818 specials[special_index].arg); */
5819 if (pos_x > specials[special_index].arg) {
5820 w = pos_x - specials[special_index].arg;
5834 if (cur_y_add > 0) {
5838 cur_y += font_descent();
5843 static void draw_text(void)
5846 if (output_methods & TO_X) {
5847 cur_y = text_start_y;
5850 if (draw_borders && border_width > 0) {
5851 unsigned int b = (border_width + 1) / 2;
5853 if (stippled_borders) {
5854 char ss[2] = { stippled_borders, stippled_borders };
5855 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
5856 CapButt, JoinMiter);
5857 XSetDashes(display, window.gc, 0, ss, 2);
5859 XSetLineAttributes(display, window.gc, border_width, LineSolid,
5860 CapButt, JoinMiter);
5863 XDrawRectangle(display, window.drawable, window.gc,
5864 text_start_x - border_margin + b, text_start_y - border_margin + b,
5865 text_width + border_margin * 2 - 1 - b * 2,
5866 text_height + border_margin * 2 - 1 - b * 2);
5873 for_each_line(text_buffer, draw_line);
5876 static void draw_stuff(void)
5878 if(overwrite_file) {
5879 overwrite_fpointer = fopen(overwrite_file, "w");
5880 if(!overwrite_fpointer)
5881 ERR("Can't overwrite '%s' anymore", overwrite_file);
5884 append_fpointer = fopen(append_file, "a");
5885 if(!append_fpointer)
5886 ERR("Can't append '%s' anymore", append_file);
5889 if (output_methods & TO_X) {
5891 if (draw_shades && !draw_outline) {
5894 set_foreground_color(default_bg_color);
5905 for (i = -1; i < 2; i++) {
5906 for (j = -1; j < 2; j++) {
5907 if (i == 0 && j == 0) {
5912 set_foreground_color(default_out_color);
5913 draw_mode = OUTLINE;
5921 set_foreground_color(default_fg_color);
5927 if (output_methods & TO_X) {
5932 swap.swap_window = window.window;
5933 swap.swap_action = XdbeBackground;
5934 XdbeSwapBuffers(display, &swap, 1);
5939 if(overwrite_fpointer) {
5940 fclose(overwrite_fpointer);
5941 overwrite_fpointer = 0;
5943 if(append_fpointer) {
5944 fclose(append_fpointer);
5945 append_fpointer = 0;
5950 static void clear_text(int exposures)
5954 /* The swap action is XdbeBackground, which clears */
5959 /* there is some extra space for borders and outlines */
5960 XClearArea(display, window.window, text_start_x - border_margin - 1,
5961 text_start_y - border_margin - 1,
5962 text_width + border_margin * 2 + 2,
5963 text_height + border_margin * 2 + 2, exposures ? True : 0);
5968 static int need_to_update;
5970 /* update_text() generates new text and clears old text area */
5971 static void update_text(void)
5975 if (output_methods & TO_X)
5981 static void main_loop(void)
5983 #ifdef SIGNAL_BLOCKING
5984 sigset_t newmask, oldmask;
5991 XserverRegion region2, part;
5992 int event_base, error_base;
5994 if (output_methods & TO_X) {
5995 region = XCreateRegion();
5997 if (!XDamageQueryExtension(display, &event_base, &error_base)) {
5998 ERR("Xdamage extension unavailable");
6000 damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
6001 region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
6002 part = XFixesCreateRegionFromWindow(display, window.window, 0);
6003 #endif /* HAVE_XDAMAGE */
6007 #ifdef SIGNAL_BLOCKING
6008 sigemptyset(&newmask);
6009 sigaddset(&newmask, SIGINT);
6010 sigaddset(&newmask, SIGTERM);
6011 sigaddset(&newmask, SIGUSR1);
6014 next_update_time = last_update_time = get_time();
6016 while (total_run_times == 0 || info.looped < total_run_times) {
6019 #ifdef SIGNAL_BLOCKING
6020 /* block signals. we will inspect for pending signals later */
6021 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6022 CRIT_ERR("unable to sigprocmask()");
6027 if (output_methods & TO_X) {
6030 /* wait for X event or timeout */
6032 if (!XPending(display)) {
6036 t = next_update_time - get_time();
6040 } else if (t > update_interval) {
6041 t = update_interval;
6044 tv.tv_sec = (long) t;
6045 tv.tv_usec = (long) (t * 1000000) % 1000000;
6047 FD_SET(ConnectionNumber(display), &fdsr);
6049 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
6051 if (errno != EINTR) {
6052 ERR("can't select(): %s", strerror(errno));
6062 if (need_to_update) {
6064 int wx = window.x, wy = window.y;
6072 /* resize window if it isn't right size */
6074 && (text_width + border_margin * 2 + 1 != window.width
6075 || text_height + border_margin * 2 + 1 != window.height)) {
6076 window.width = text_width + border_margin * 2 + 1;
6077 window.height = text_height + border_margin * 2 + 1;
6078 XResizeWindow(display, window.window, window.width,
6081 set_transparent_background(window.window);
6085 /* move window if it isn't in right position */
6086 if (!fixed_pos && (window.x != wx || window.y != wy)) {
6087 XMoveWindow(display, window.window, window.x, window.y);
6098 r.x = text_start_x - border_margin;
6099 r.y = text_start_y - border_margin;
6100 r.width = text_width + border_margin * 2;
6101 r.height = text_height + border_margin * 2;
6102 XUnionRectWithRegion(&r, region, region);
6107 /* handle X events */
6108 while (XPending(display)) {
6111 XNextEvent(display, &ev);
6119 r.width = ev.xexpose.width;
6120 r.height = ev.xexpose.height;
6121 XUnionRectWithRegion(&r, region, region);
6126 case ReparentNotify:
6127 /* set background to ParentRelative for all parents */
6129 set_transparent_background(window.window);
6133 case ConfigureNotify:
6135 /* if window size isn't what expected, set fixed size */
6136 if (ev.xconfigure.width != window.width
6137 || ev.xconfigure.height != window.height) {
6138 if (window.width != 0 && window.height != 0) {
6142 /* clear old stuff before screwing up
6147 XWindowAttributes attrs;
6149 if (XGetWindowAttributes(display,
6150 window.window, &attrs)) {
6151 window.width = attrs.width;
6152 window.height = attrs.height;
6156 text_width = window.width - border_margin * 2 - 1;
6157 text_height = window.height - border_margin * 2 - 1;
6158 if (text_width > maximum_width
6159 && maximum_width > 0) {
6160 text_width = maximum_width;
6164 /* if position isn't what expected, set fixed pos
6165 * total_updates avoids setting fixed_pos when window
6166 * is set to weird locations when started */
6167 /* // this is broken
6168 if (total_updates >= 2 && !fixed_pos
6169 && (window.x != ev.xconfigure.x
6170 || window.y != ev.xconfigure.y)
6171 && (ev.xconfigure.x != 0
6172 || ev.xconfigure.y != 0)) {
6181 /* if an ordinary window with decorations */
6182 if ((window.type == TYPE_NORMAL)
6183 && (!TEST_HINT(window.hints,
6184 HINT_UNDECORATED))) {
6185 /* allow conky to hold input focus. */
6188 /* forward the click to the desktop window */
6189 XUngrabPointer(display, ev.xbutton.time);
6190 ev.xbutton.window = window.desktop;
6191 XSendEvent(display, ev.xbutton.window, False,
6192 ButtonPressMask, &ev);
6193 XSetInputFocus(display, ev.xbutton.window,
6194 RevertToParent, ev.xbutton.time);
6201 /* if an ordinary window with decorations */
6202 if ((window.type == TYPE_NORMAL)
6203 && (!TEST_HINT(window.hints,
6204 HINT_UNDECORATED))) {
6205 /* allow conky to hold input focus. */
6208 /* forward the release to the desktop window */
6209 ev.xbutton.window = window.desktop;
6210 XSendEvent(display, ev.xbutton.window, False,
6211 ButtonReleaseMask, &ev);
6220 if (ev.type == event_base + XDamageNotify) {
6221 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
6223 XFixesSetRegion(display, part, &dev->area, 1);
6224 XFixesUnionRegion(display, region2, region2, part);
6226 #endif /* HAVE_XDAMAGE */
6232 XDamageSubtract(display, damage, region2, None);
6233 XFixesSetRegion(display, region2, 0, 0);
6234 #endif /* HAVE_XDAMAGE */
6236 /* XDBE doesn't seem to provide a way to clear the back buffer without
6237 * interfering with the front buffer, other than passing XdbeBackground
6238 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6239 * redraw the text even if it wasn't part of the exposed area. OTOH,
6240 * if we're not going to call draw_stuff at all, then no swap happens
6241 * and we can safely do nothing. */
6243 if (!XEmptyRegion(region)) {
6248 r.x = text_start_x - border_margin;
6249 r.y = text_start_y - border_margin;
6250 r.width = text_width + border_margin * 2;
6251 r.height = text_height + border_margin * 2;
6252 XUnionRectWithRegion(&r, region, region);
6255 XSetRegion(display, window.gc, region);
6258 XftDrawSetClip(window.xftdraw, region);
6262 XDestroyRegion(region);
6263 region = XCreateRegion();
6267 t = (next_update_time - get_time()) * 1000000;
6268 if(t > 0) usleep((useconds_t)t);
6275 #ifdef SIGNAL_BLOCKING
6276 /* unblock signals of interest and let handler fly */
6277 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
6278 CRIT_ERR("unable to sigprocmask()");
6282 switch (g_signal_pending) {
6285 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
6290 ERR("received SIGINT or SIGTERM to terminate. bye!");
6293 if (output_methods & TO_X) {
6294 XDestroyRegion(region);
6297 XDamageDestroy(display, damage);
6298 XFixesDestroyRegion(display, region2);
6299 XFixesDestroyRegion(display, part);
6300 #endif /* HAVE_XDAMAGE */
6303 if(overwrite_file) {
6304 free(overwrite_file);
6311 return; /* return from main_loop */
6314 /* Reaching here means someone set a signal
6315 * (SIGXXXX, signal_handler), but didn't write any code
6317 * If you don't want to handle a signal, don't set a handler on
6318 * it in the first place. */
6319 if (g_signal_pending) {
6320 ERR("ignoring signal (%d)", g_signal_pending);
6324 g_signal_pending = 0;
6327 #if defined(X11) && defined(HAVE_XDAMAGE)
6328 if (output_methods & TO_X) {
6329 XDamageDestroy(display, damage);
6330 XFixesDestroyRegion(display, region2);
6331 XFixesDestroyRegion(display, part);
6332 XDestroyRegion(region);
6335 #endif /* X11 && HAVE_XDAMAGE */
6338 static void load_config_file(const char *);
6339 static void load_config_file_x11(const char *);
6341 /* reload the config file */
6342 static void reload_config(void)
6344 timed_thread_destroy_registered_threads();
6346 if (info.cpu_usage) {
6347 free(info.cpu_usage);
6348 info.cpu_usage = NULL;
6359 #ifdef TCP_PORT_MONITOR
6360 tcp_portmon_clear();
6363 if (current_config) {
6365 load_config_file(current_config);
6366 load_config_file_x11(current_config);
6368 /* re-init specials array */
6369 if ((specials = realloc((void *) specials,
6370 sizeof(struct special_t) * max_specials)) == 0) {
6371 ERR("failed to realloc specials array");
6377 // clear the window first
6378 XClearWindow(display, RootWindow(display, screen));
6382 extract_variable_text(global_text);
6388 tmpstring1 = malloc(text_buffer_size);
6389 memset(tmpstring1, 0, text_buffer_size);
6393 tmpstring2 = malloc(text_buffer_size);
6394 memset(tmpstring2, 0, text_buffer_size);
6398 text_buffer = malloc(max_user_text);
6399 memset(text_buffer, 0, max_user_text);
6404 static void clean_up(void)
6406 timed_thread_destroy_registered_threads();
6408 if (info.cpu_usage) {
6409 free(info.cpu_usage);
6410 info.cpu_usage = NULL;
6413 if (output_methods & TO_X) {
6416 XdbeDeallocateBackBufferName(display, window.back_buffer);
6421 XDestroyWindow(display, window.window);
6422 XClearWindow(display, RootWindow(display, screen));
6427 XClearWindow(display, RootWindow(display, screen));
6432 XFreeGC(display, window.gc);
6437 free_text_objects(&global_root_object);
6456 free(current_config);
6458 #ifdef TCP_PORT_MONITOR
6459 tcp_portmon_clear();
6468 for (i = 0; i < special_count; i++) {
6469 if (specials[i].type == GRAPH) {
6470 free(specials[i].graph);
6477 clear_diskio_stats();
6480 static int string_to_bool(const char *s)
6483 // Assumes an option without a true/false means true
6485 } else if (strcasecmp(s, "yes") == EQUAL) {
6487 } else if (strcasecmp(s, "true") == EQUAL) {
6489 } else if (strcasecmp(s, "1") == EQUAL) {
6496 static enum alignment string_to_alignment(const char *s)
6498 if (strcasecmp(s, "top_left") == EQUAL) {
6500 } else if (strcasecmp(s, "top_right") == EQUAL) {
6502 } else if (strcasecmp(s, "top_middle") == EQUAL) {
6504 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
6506 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
6507 return BOTTOM_RIGHT;
6508 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
6509 return BOTTOM_MIDDLE;
6510 } else if (strcasecmp(s, "middle_left") == EQUAL) {
6512 } else if (strcasecmp(s, "middle_right") == EQUAL) {
6513 return MIDDLE_RIGHT;
6514 } else if (strcasecmp(s, "tl") == EQUAL) {
6516 } else if (strcasecmp(s, "tr") == EQUAL) {
6518 } else if (strcasecmp(s, "tm") == EQUAL) {
6520 } else if (strcasecmp(s, "bl") == EQUAL) {
6522 } else if (strcasecmp(s, "br") == EQUAL) {
6523 return BOTTOM_RIGHT;
6524 } else if (strcasecmp(s, "bm") == EQUAL) {
6525 return BOTTOM_MIDDLE;
6526 } else if (strcasecmp(s, "ml") == EQUAL) {
6528 } else if (strcasecmp(s, "mr") == EQUAL) {
6529 return MIDDLE_RIGHT;
6530 } else if (strcasecmp(s, "none") == EQUAL) {
6538 static void set_default_configurations_for_x(void)
6540 default_fg_color = WhitePixel(display, screen);
6541 default_bg_color = BlackPixel(display, screen);
6542 default_out_color = BlackPixel(display, screen);
6543 color0 = default_fg_color;
6544 color1 = default_fg_color;
6545 color2 = default_fg_color;
6546 color3 = default_fg_color;
6547 color4 = default_fg_color;
6548 color5 = default_fg_color;
6549 color6 = default_fg_color;
6550 color7 = default_fg_color;
6551 color8 = default_fg_color;
6552 color9 = default_fg_color;
6556 static void set_default_configurations(void)
6559 fork_to_background = 0;
6560 total_run_times = 0;
6561 info.cpu_avg_samples = 2;
6562 info.net_avg_samples = 2;
6563 info.diskio_avg_samples = 2;
6571 mpd_set_host("localhost");
6572 mpd_set_port("6600");
6575 info.xmms2.artist = NULL;
6576 info.xmms2.album = NULL;
6577 info.xmms2.title = NULL;
6578 info.xmms2.genre = NULL;
6579 info.xmms2.comment = NULL;
6580 info.xmms2.url = NULL;
6581 info.xmms2.status = NULL;
6582 info.xmms2.playlist = NULL;
6584 use_spacer = NO_SPACER;
6586 output_methods = TO_X;
6588 output_methods = TO_STDOUT;
6591 show_graph_scale = 0;
6592 show_graph_range = 0;
6593 template[0] = strdup("");
6594 template[1] = strdup("");
6595 template[2] = strdup("");
6596 template[3] = strdup("");
6597 template[4] = strdup("");
6598 template[5] = strdup("");
6599 template[6] = strdup("");
6600 template[7] = strdup("");
6601 template[8] = strdup("");
6602 template[9] = strdup("");
6605 draw_graph_borders = 1;
6607 set_first_font("6x10");
6615 window.type = TYPE_NORMAL;
6617 strcpy(window.class_name, PACKAGE_NAME);
6618 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
6620 stippled_borders = 0;
6623 text_alignment = BOTTOM_LEFT;
6624 info.x11.monitor.number = 1;
6625 info.x11.monitor.current = 0;
6628 free(current_mail_spool);
6632 variable_substitute(MAIL_FILE, buf, 256);
6633 if (buf[0] != '\0') {
6634 current_mail_spool = strndup(buf, text_buffer_size);
6639 update_interval = 3.0;
6640 info.music_player_interval = 1.0;
6641 stuff_in_upper_case = 0;
6642 info.users.number = 1;
6644 #ifdef TCP_PORT_MONITOR
6645 /* set default connection limit */
6646 tcp_portmon_set_max_connections(0);
6650 /* returns 1 if you can overwrite or create the file at 'path' */
6651 static _Bool overwrite_works(const char *path)
6655 if (!(filepointer = fopen(path, "w")))
6657 fclose(filepointer);
6661 /* returns 1 if you can append or create the file at 'path' */
6662 static _Bool append_works(const char *path)
6666 if (!(filepointer = fopen(path, "a")))
6668 fclose(filepointer);
6673 static void X11_initialisation(void)
6675 if (x_initialised == YES) return;
6676 output_methods |= TO_X;
6678 set_default_configurations_for_x();
6679 x_initialised = YES;
6683 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6684 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
6685 #define CONF2(a) if (strcasecmp(name, a) == 0)
6686 #define CONF(a) else CONF2(a)
6687 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
6688 || strcasecmp(name, b) == 0)
6689 #define CONF_CONTINUE 1
6690 #define CONF_BREAK 2
6691 #define CONF_BUFF_SIZE 512
6693 static FILE *open_config_file(const char *f)
6695 #ifdef CONFIG_OUTPUT
6696 if (!strcmp(f, "==builtin==")) {
6697 #ifdef HAVE_FOPENCOOKIE
6698 return fopencookie(NULL, "r", conf_cookie);
6699 #endif /* HAVE_FOPENCOOKIE */
6701 #endif /* CONFIG_OUTPUT */
6702 return fopen(f, "r");
6705 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
6709 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
6715 /* break at comment, unless preceeded by \ */
6716 p2 = strchr(p, '#');
6717 if (p2 && (p2 == p || *(p2 - 1) != '\\')) {
6722 while (*p && isspace((int) *p)) {
6726 return CONF_CONTINUE; /* empty line */
6733 while (*p2 && !isspace((int) *p2)) {
6737 *p2 = '\0'; /* break at name's end */
6744 while (*p && isspace((int) *p)) {
6750 p2 = *value + strlen(*value);
6751 while (isspace((int) *(p2 - 1))) {
6760 static void load_config_file(const char *f)
6765 set_default_configurations();
6766 fp = open_config_file(f);
6770 DBGP("reading contents from config file '%s'", f);
6773 char buff[CONF_BUFF_SIZE], *name, *value;
6774 int ret = do_config_step(&line, fp, buff, &name, &value);
6775 if (ret == CONF_BREAK) {
6777 } else if (ret == CONF_CONTINUE) {
6783 /* don't listen if X is already initialised or
6784 * if we already know we don't want it */
6785 if(x_initialised == NO) {
6786 if (string_to_bool(value)) {
6787 output_methods &= TO_X;
6789 output_methods &= ~TO_X;
6790 x_initialised = NEVER;
6795 if (!value || x_initialised == YES) {
6800 disp = strdup(value);
6804 if (window.type == TYPE_DOCK)
6807 int a = string_to_alignment(value);
6818 CONF("background") {
6819 fork_to_background = string_to_bool(value);
6822 CONF2("background") {
6823 fork_to_background = string_to_bool(value);
6827 CONF("show_graph_scale") {
6828 show_graph_scale = string_to_bool(value);
6830 CONF("show_graph_range") {
6831 show_graph_range = string_to_bool(value);
6833 CONF("border_margin") {
6835 border_margin = strtol(value, 0, 0);
6840 CONF("border_width") {
6842 border_width = strtol(value, 0, 0);
6847 #define TEMPLATE_CONF(n) \
6848 CONF("template"#n) { \
6850 free(template[n]); \
6851 template[n] = strdup(value); \
6869 info.mail = parse_mail_args(IMAP_TYPE, value);
6876 info.mail = parse_mail_args(POP3_TYPE, value);
6881 CONF("default_bar_size") {
6884 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
6891 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
6894 CONF("default_graph_size") {
6897 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
6904 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
6907 CONF("default_gauge_size") {
6910 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
6917 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
6923 mpd_set_host(value);
6929 if (value && mpd_set_port(value)) {
6933 CONF("mpd_password") {
6935 mpd_set_password(value);
6941 CONF("music_player_interval") {
6943 info.music_player_interval = strtod(value, 0);
6949 CONF("sensor_device") {
6951 sensor_device = strtol(value, 0, 0);
6957 CONF("cpu_avg_samples") {
6959 cpu_avg_samples = strtol(value, 0, 0);
6960 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
6963 info.cpu_avg_samples = cpu_avg_samples;
6969 CONF("net_avg_samples") {
6971 net_avg_samples = strtol(value, 0, 0);
6972 if (net_avg_samples < 1 || net_avg_samples > 14) {
6975 info.net_avg_samples = net_avg_samples;
6981 CONF("diskio_avg_samples") {
6983 diskio_avg_samples = strtol(value, 0, 0);
6984 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
6987 info.diskio_avg_samples = diskio_avg_samples;
6995 CONF("double_buffer") {
6996 use_xdbe = string_to_bool(value);
7000 CONF("override_utf8_locale") {
7001 utf8_mode = string_to_bool(value);
7003 CONF("draw_borders") {
7004 draw_borders = string_to_bool(value);
7006 CONF("draw_graph_borders") {
7007 draw_graph_borders = string_to_bool(value);
7009 CONF("draw_shades") {
7010 draw_shades = string_to_bool(value);
7012 CONF("draw_outline") {
7013 draw_outline = string_to_bool(value);
7016 CONF("out_to_console") {
7017 if(string_to_bool(value))
7018 output_methods |= TO_STDOUT;
7020 CONF("out_to_stderr") {
7021 if(string_to_bool(value))
7022 output_methods |= TO_STDERR;
7024 CONF("overwrite_file") {
7025 if(overwrite_file) {
7026 free(overwrite_file);
7029 if(overwrite_works(value)) {
7030 overwrite_file = strdup(value);
7031 output_methods |= OVERWRITE_FILE;
7033 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
7035 CONF("append_file") {
7040 if(append_works(value)) {
7041 append_file = strdup(value);
7042 output_methods |= APPEND_FILE;
7044 ERR("append_file won't be able to create/append '%s'", value);
7046 CONF("use_spacer") {
7048 if (strcasecmp(value, "left") == EQUAL) {
7049 use_spacer = LEFT_SPACER;
7050 } else if (strcasecmp(value, "right") == EQUAL) {
7051 use_spacer = RIGHT_SPACER;
7052 } else if (strcasecmp(value, "none") == EQUAL) {
7053 use_spacer = NO_SPACER;
7055 use_spacer = string_to_bool(value);
7056 ERR("use_spacer should have an argument of left, right, or"
7057 " none. '%s' seems to be some form of '%s', so"
7058 " defaulting to %s.", value,
7059 use_spacer ? "true" : "false",
7060 use_spacer ? "right" : "none");
7062 use_spacer = RIGHT_SPACER;
7064 use_spacer = NO_SPACER;
7068 ERR("use_spacer should have an argument. Defaulting to right.");
7069 use_spacer = RIGHT_SPACER;
7075 use_xft = string_to_bool(value);
7079 set_first_font(value);
7083 if (value && font_count >= 0) {
7084 fonts[0].font_alpha = atof(value) * 65535.0;
7091 if (string_to_bool(value)) {
7092 ERR("Xft not enabled");
7096 /* xftfont silently ignored when no Xft */
7099 /* xftalpha is silently ignored when no Xft */
7104 set_first_font(value);
7112 gap_x = atoi(value);
7119 gap_y = atoi(value);
7125 CONF("mail_spool") {
7129 variable_substitute(value, buffer, 256);
7131 if (buffer[0] != '\0') {
7132 if (current_mail_spool) {
7133 free(current_mail_spool);
7135 current_mail_spool = strndup(buffer, text_buffer_size);
7142 CONF("minimum_size") {
7144 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
7146 if (sscanf(value, "%d", &minimum_width) != 1) {
7154 CONF("maximum_width") {
7156 if (sscanf(value, "%d", &maximum_width) != 1) {
7164 CONF("no_buffers") {
7165 no_buffers = string_to_bool(value);
7167 CONF("top_name_width") {
7169 if (sscanf(value, "%u", &top_name_width) != 1) {
7175 if (top_name_width >= max_user_text) {
7176 top_name_width = max_user_text - 1;
7179 CONF("top_cpu_separate") {
7180 cpu_separate = string_to_bool(value);
7182 CONF("short_units") {
7183 short_units = string_to_bool(value);
7185 CONF("pad_percents") {
7186 pad_percents = atoi(value);
7190 CONF("own_window") {
7192 own_window = string_to_bool(value);
7195 CONF("own_window_class") {
7197 memset(window.class_name, 0, sizeof(window.class_name));
7198 strncpy(window.class_name, value,
7199 sizeof(window.class_name) - 1);
7202 CONF("own_window_title") {
7204 memset(window.title, 0, sizeof(window.title));
7205 strncpy(window.title, value, sizeof(window.title) - 1);
7208 CONF("own_window_transparent") {
7210 set_transparent = string_to_bool(value);
7213 CONF("own_window_hints") {
7215 char *p_hint, *p_save;
7216 char delim[] = ", ";
7218 /* tokenize the value into individual hints */
7219 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
7221 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
7222 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
7223 SET_HINT(window.hints, HINT_UNDECORATED);
7224 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
7225 SET_HINT(window.hints, HINT_BELOW);
7226 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
7227 SET_HINT(window.hints, HINT_ABOVE);
7228 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
7229 SET_HINT(window.hints, HINT_STICKY);
7230 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
7231 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
7232 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
7233 SET_HINT(window.hints, HINT_SKIP_PAGER);
7238 p_hint = strtok_r(NULL, delim, &p_save);
7239 } while (p_hint != NULL);
7245 CONF("own_window_type") {
7247 if (strncmp(value, "normal", 6) == EQUAL) {
7248 window.type = TYPE_NORMAL;
7249 } else if (strncmp(value, "desktop", 7) == EQUAL) {
7250 window.type = TYPE_DESKTOP;
7251 } else if (strncmp(value, "dock", 7) == EQUAL) {
7252 window.type = TYPE_DOCK;
7253 text_alignment = TOP_LEFT;
7254 } else if (strncmp(value, "override", 8) == EQUAL) {
7255 window.type = TYPE_OVERRIDE;
7264 CONF("stippled_borders") {
7266 stippled_borders = strtol(value, 0, 0);
7268 stippled_borders = 4;
7273 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> "
7277 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> "
7280 CONF("update_interval") {
7282 update_interval = strtod(value, 0);
7286 if (info.music_player_interval == 0) {
7287 // default to update_interval
7288 info.music_player_interval = update_interval;
7291 CONF("total_run_times") {
7293 total_run_times = strtod(value, 0);
7299 stuff_in_upper_case = string_to_bool(value);
7301 CONF("max_specials") {
7303 max_specials = atoi(value);
7308 CONF("max_user_text") {
7310 max_user_text = atoi(value);
7315 CONF("text_buffer_size") {
7317 text_buffer_size = atoi(value);
7318 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
7319 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
7320 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
7332 global_text = (char *) malloc(1);
7333 global_text[0] = '\0';
7336 unsigned int l = strlen(global_text);
7338 char buf[CONF_BUFF_SIZE];
7340 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7346 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
7354 /* Check for continuation of \\-\n. */
7355 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
7356 global_text[l-1] = '\0';
7360 global_text = (char *) realloc(global_text, l + bl + 1);
7361 strcat(global_text, buf);
7363 if (strlen(global_text) > max_user_text) {
7368 if (strlen(global_text) < 1) {
7369 CRIT_ERR("no text supplied in configuration; exiting");
7371 global_text_lines = line + 1;
7374 #ifdef TCP_PORT_MONITOR
7375 CONF("max_port_monitor_connections") {
7377 if (!value || (sscanf(value, "%d", &max) != 1)) {
7378 /* an error. use default, warn and continue. */
7379 tcp_portmon_set_max_connections(0);
7381 } else if (tcp_portmon_set_max_connections(max)) {
7382 /* max is < 0, default has been set*/
7387 CONF("if_up_strictness") {
7389 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7390 ifup_strictness = IFUP_UP;
7391 } else if (strcasecmp(value, "up") == EQUAL) {
7392 ifup_strictness = IFUP_UP;
7393 } else if (strcasecmp(value, "link") == EQUAL) {
7394 ifup_strictness = IFUP_LINK;
7395 } else if (strcasecmp(value, "address") == EQUAL) {
7396 ifup_strictness = IFUP_ADDR;
7398 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7399 ifup_strictness = IFUP_UP;
7403 CONF("temperature_unit") {
7405 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
7406 } else if (set_temp_output_unit(value)) {
7407 ERR("temperature_unit: incorrect argument");
7413 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
7414 char *skey = malloc(maxlength);
7415 char *svalue = malloc(maxlength);
7417 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
7418 oldvalue = getenv(skey);
7419 if (oldvalue == NULL) {
7420 setenv(skey, svalue, 0);
7422 //PS: Don't free oldvalue, it's the real envvar, not a copy
7443 CONF("default_color"){}
7444 CONF3("default_shade_color", "default_shadecolor"){}
7445 CONF3("default_outline_color", "default_outlinecolor") {}
7446 CONF("own_window_colour") {}
7449 ERR("%s: %d: no such configuration: '%s'", f, line, name);
7455 if (info.music_player_interval == 0) {
7456 // default to update_interval
7457 info.music_player_interval = update_interval;
7459 if (!global_text) { // didn't supply any text
7460 CRIT_ERR("missing text block in configuration; exiting");
7464 static void load_config_file_x11(const char *f)
7469 fp = open_config_file(f);
7473 DBGP("reading contents from config file '%s'", f);
7476 char buff[CONF_BUFF_SIZE], *name, *value;
7477 int ret = do_config_step(&line, fp, buff, &name, &value);
7478 if (ret == CONF_BREAK) {
7480 } else if (ret == CONF_CONTINUE) {
7486 X11_initialisation();
7487 if (x_initialised == YES) {
7489 color0 = get_x11_color(value);
7496 X11_initialisation();
7497 if (x_initialised == YES) {
7499 color1 = get_x11_color(value);
7506 X11_initialisation();
7507 if (x_initialised == YES) {
7509 color2 = get_x11_color(value);
7516 X11_initialisation();
7517 if (x_initialised == YES) {
7519 color3 = get_x11_color(value);
7526 X11_initialisation();
7527 if (x_initialised == YES) {
7529 color4 = get_x11_color(value);
7536 X11_initialisation();
7537 if (x_initialised == YES) {
7539 color5 = get_x11_color(value);
7546 X11_initialisation();
7547 if (x_initialised == YES) {
7549 color6 = get_x11_color(value);
7556 X11_initialisation();
7557 if (x_initialised == YES) {
7559 color7 = get_x11_color(value);
7566 X11_initialisation();
7567 if (x_initialised == YES) {
7569 color8 = get_x11_color(value);
7576 X11_initialisation();
7577 if (x_initialised == YES) {
7579 color9 = get_x11_color(value);
7585 CONF("default_color") {
7586 X11_initialisation();
7587 if (x_initialised == YES) {
7589 default_fg_color = get_x11_color(value);
7595 CONF3("default_shade_color", "default_shadecolor") {
7596 X11_initialisation();
7597 if (x_initialised == YES) {
7599 default_bg_color = get_x11_color(value);
7605 CONF3("default_outline_color", "default_outlinecolor") {
7606 X11_initialisation();
7607 if (x_initialised == YES) {
7609 default_out_color = get_x11_color(value);
7616 CONF("own_window_colour") {
7617 X11_initialisation();
7618 if (x_initialised == YES) {
7620 background_colour = get_x11_color(value);
7622 ERR("Invalid colour for own_window_colour (try omitting the "
7623 "'#' for hex colours");
7629 //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
7630 if(output_methods & TO_X) {
7631 X11_initialisation();
7641 #undef CONF_CONTINUE
7642 #undef CONF_BUFF_SIZE
7649 static void print_help(const char *prog_name) {
7650 printf("Usage: %s [OPTION]...\n"
7651 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
7652 "window. Command line options will override configurations defined in config\n"
7654 " -v, --version version\n"
7655 " -q, --quiet quiet mode\n"
7656 " -D, --debug increase debugging output\n"
7657 " -c, --config=FILE config file to load\n"
7658 #ifdef CONFIG_OUTPUT
7659 " -C, --print-config print the builtin default config to stdout\n"
7660 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
7662 " -d, --daemonize daemonize, fork to background\n"
7663 " -h, --help help\n"
7665 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
7666 " -f, --font=FONT font to use\n"
7668 " -o, --own-window create own window to draw\n"
7671 " -b, --double-buffer double buffer (prevents flickering)\n"
7673 " -w, --window-id=WIN_ID window id to draw\n"
7674 " -x X x position\n"
7675 " -y Y y position\n"
7677 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
7678 " -u, --interval=SECS update interval\n"
7679 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
7684 /* : means that character before that takes an argument */
7685 static const char *getopt_string = "vVqdDt:u:i:hc:"
7695 #ifdef CONFIG_OUTPUT
7700 static const struct option longopts[] = {
7701 { "help", 0, NULL, 'h' },
7702 { "version", 0, NULL, 'V' },
7703 { "debug", 0, NULL, 'D' },
7704 { "config", 1, NULL, 'c' },
7705 #ifdef CONFIG_OUTPUT
7706 { "print-config", 0, NULL, 'C' },
7708 { "daemonize", 0, NULL, 'd' },
7710 { "alignment", 1, NULL, 'a' },
7711 { "font", 1, NULL, 'f' },
7712 { "display", 1, NULL, 'X' },
7714 { "own-window", 0, NULL, 'o' },
7717 { "double-buffer", 0, NULL, 'b' },
7719 { "window-id", 1, NULL, 'w' },
7721 { "text", 1, NULL, 't' },
7722 { "interval", 0, NULL, 'u' },
7726 int main(int argc, char **argv)
7732 struct sigaction act, oact;
7734 g_signal_pending = 0;
7735 memset(&info, 0, sizeof(info));
7738 #ifdef TCP_PORT_MONITOR
7739 /* set default connection limit */
7740 tcp_portmon_set_max_connections(0);
7743 /* handle command line parameters that don't change configs */
7745 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
7746 || ((s = getenv("LANG")) && *s)) {
7747 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
7749 ERR("malloc failed");
7751 for (x = 0; x < strlen(s); x++) {
7752 temp[x] = tolower(s[x]);
7755 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
7761 if (!setlocale(LC_CTYPE, "")) {
7762 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
7766 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
7777 if (current_config) {
7778 free(current_config);
7780 current_config = strndup(optarg, max_user_text);
7783 freopen("/dev/null", "w", stderr);
7786 print_help(argv[0]);
7788 #ifdef CONFIG_OUTPUT
7795 window.window = strtol(optarg, 0, 0);
7804 /* check if specified config file is valid */
7805 if (current_config) {
7807 if (stat(current_config, &sb) ||
7808 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
7809 ERR("invalid configuration file '%s'\n", current_config);
7810 free(current_config);
7815 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
7817 if (!current_config) {
7818 /* load default config file */
7822 /* Try to use personal config file first */
7823 variable_substitute(CONFIG_FILE, buf, sizeof(buf));
7824 if (buf[0] && (fp = fopen(buf, "r"))) {
7825 current_config = strndup(buf, max_user_text);
7829 /* Try to use system config file if personal config not readable */
7830 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
7831 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
7835 /* No readable config found */
7836 if (!current_config) {
7837 #ifdef CONFIG_OUTPUT
7838 current_config = strdup("==builtin==");
7839 ERR("no readable personal or system-wide config file found,"
7840 " using builtin default");
7842 CRIT_ERR("no readable personal or system-wide config file found");
7843 #endif /* ! CONF_OUTPUT */
7847 load_config_file(current_config);
7849 /* init specials array */
7850 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
7851 ERR("failed to create specials array");
7855 if (current_mail_spool == NULL) {
7858 variable_substitute(MAIL_FILE, buf, 256);
7860 if (buf[0] != '\0') {
7861 current_mail_spool = strndup(buf, text_buffer_size);
7866 /* handle other command line arguments */
7868 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
7869 || defined(__NetBSD__)
7870 optind = optreset = 1;
7875 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
7876 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
7877 "kvm_open")) == NULL) {
7878 CRIT_ERR("cannot read kvm");
7883 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
7891 fork_to_background = 1;
7894 global_debug_level++;
7898 set_first_font(optarg);
7901 text_alignment = string_to_alignment(optarg);
7906 disp = strdup(optarg);
7925 global_text = strndup(optarg, max_user_text);
7926 convert_escapes(global_text);
7930 update_interval = strtod(optarg, 0);
7931 if (info.music_player_interval == 0) {
7932 // default to update_interval
7933 info.music_player_interval = update_interval;
7938 total_run_times = strtod(optarg, 0);
7942 gap_x = atoi(optarg);
7946 gap_y = atoi(optarg);
7957 if (output_methods & TO_X) {
7958 load_config_file_x11(current_config);
7963 /* generate text and get initial size */
7964 extract_variable_text(global_text);
7971 if (fork_to_background) {
7976 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
7983 fprintf(stderr, "\n");
7988 /* parent process */
7989 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
7996 text_buffer = malloc(max_user_text);
7997 memset(text_buffer, 0, max_user_text);
7998 tmpstring1 = malloc(text_buffer_size);
7999 memset(tmpstring1, 0, text_buffer_size);
8000 tmpstring2 = malloc(text_buffer_size);
8001 memset(tmpstring2, 0, text_buffer_size);
8004 if (output_methods & TO_X) {
8006 update_text_area(); /* to get initial size of the window */
8009 init_window(own_window, text_width + border_margin * 2 + 1,
8010 text_height + border_margin * 2 + 1, set_transparent, background_colour,
8012 #else /* OWN_WINDOW */
8013 init_window(0, text_width + border_margin * 2 + 1,
8014 text_height + border_margin * 2 + 1, set_transparent, 0,
8016 #endif /* OWN_WINDOW */
8019 update_text_area(); /* to position text/window on screen */
8022 if (own_window && !fixed_pos) {
8023 XMoveWindow(display, window.window, window.x, window.y);
8026 set_transparent_background(window.window);
8037 /* Set signal handlers */
8038 act.sa_handler = signal_handler;
8039 sigemptyset(&act.sa_mask);
8042 act.sa_flags |= SA_RESTART;
8045 if ( sigaction(SIGINT, &act, &oact) < 0
8046 || sigaction(SIGUSR1, &act, &oact) < 0
8047 || sigaction(SIGHUP, &act, &oact) < 0
8048 || sigaction(SIGTERM, &act, &oact) < 0) {
8049 ERR("error setting signal handler: %s", strerror(errno));
8054 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8061 static void signal_handler(int sig)
8063 /* signal handler is light as a feather, as it should be.
8064 * we will poll g_signal_pending with each loop of conky
8065 * and do any signal processing there, NOT here. */
8066 g_signal_pending = sig;