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>
45 #ifdef HAVE_SYS_INOTIFY_H
46 #include <sys/inotify.h>
47 #endif /* HAVE_SYS_INOTIFY_H */
50 #include <X11/Xutil.h>
52 #include <X11/extensions/Xdamage.h>
58 #include <sys/types.h>
60 #include <netinet/in.h>
79 #include "temphelper.h"
83 /* check for OS and include appropriate headers */
84 #if defined(__linux__)
86 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
88 #elif defined(__OpenBSD__)
92 #if defined(__FreeBSD_kernel__)
96 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
97 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
98 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
99 int apm_getinfo(int fd, apm_info_t aip);
100 char *get_apm_adapter(void);
101 char *get_apm_battery_life(void);
102 char *get_apm_battery_time(void);
110 #include "defconfig.h"
111 #include "conf_cookie.h"
115 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
118 #define MAIL_FILE "$MAIL"
119 #define MAX_IF_BLOCK_DEPTH 5
121 //#define SIGNAL_BLOCKING
122 #undef SIGNAL_BLOCKING
124 /* debugging level, used by logging.h */
125 int global_debug_level = 0;
127 /* two strings for internal use */
128 static char *tmpstring1, *tmpstring2;
130 /* variables holding various config settings */
132 int format_human_readable;
139 int top_cpu, top_mem, top_time;
143 static unsigned int top_name_width = 15;
145 enum x_initialiser_state x_initialised = NO;
146 static volatile int g_signal_pending;
147 /* Update interval */
148 double update_interval;
149 void *global_cpu = NULL;
154 /* prototypes for internally used functions */
155 static void signal_handler(int);
156 static void print_version(void) __attribute__((noreturn));
157 static void reload_config(void);
158 static void generate_text_internal(char *, int, struct text_object,
159 struct information *);
160 static int extract_variable_text_internal(struct text_object *,
163 static void print_version(void)
165 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
167 printf("\nCompiled in features:\n\n"
168 "System config file: "SYSTEM_CONFIG_FILE"\n"
169 "Package library path: "PACKAGE_LIBDIR"\n\n"
173 " * Xdamage extension\n"
174 # endif /* HAVE_XDAMAGE */
176 " * XDBE (double buffer extension)\n"
177 # endif /* HAVE_XDBE */
182 "\n Music detection:\n"
185 #endif /* AUDACIOUS */
201 #endif /* HAVE_OPENMP */
208 #ifdef TCP_PORT_MONITOR
210 #endif /* TCP_PORT_MONITOR */
213 #endif /* HAVE_CURL */
218 " * Weather (NOAA)\n"
220 " * Weather (XOAP)\n"
225 #endif /* HAVE_IWLIB */
227 " * support for IBM/Lenovo notebooks\n"
237 #endif /* CONFIG_OUTPUT */
242 " * ALSA mixer support\n"
243 #endif /* MIXER_IS_ALSA */
253 #ifdef HAVE_LUA_CAIRO
255 #endif /* HAVE_LUA_CAIRO */
256 #ifdef HAVE_LUA_IMLIB2
259 #endif /* HAVE_LUA */
265 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
270 static void X11_create_window(void);
271 static void X11_initialisation(void);
273 struct _x11_stuff_s {
277 XserverRegion region2, part;
278 int event_base, error_base;
284 static int text_start_x, text_start_y; /* text start position in window */
285 static int text_width, text_height;
300 /* display to connect to */
301 static char *disp = NULL;
305 /* struct that has all info to be shared between
306 * instances of the same text object */
307 struct information info;
309 /* path to config file */
310 char *current_config;
312 /* set to 1 if you want all text to be in uppercase */
313 static unsigned int stuff_in_uppercase;
315 /* Run how many times? */
316 static unsigned long total_run_times;
319 static int fork_to_background;
321 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
323 /* filenames for output */
324 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
325 char *append_file = NULL; FILE *append_fpointer = NULL;
327 /* xoap suffix for weather from weather.com */
329 static char *xoap = NULL;
334 static int show_graph_scale;
335 static int show_graph_range;
337 /* Position on the screen */
338 static int text_alignment;
339 static int gap_x, gap_y;
342 static int draw_borders;
343 static int draw_graph_borders;
344 static int stippled_borders;
346 static int draw_shades, draw_outline;
348 static long default_fg_color, default_bg_color, default_out_color;
350 /* create own window or draw stuff to root? */
351 static int set_transparent = 0;
354 static int own_window = 0;
355 static int background_colour = 0;
357 /* fixed size/pos is set if wm/user changes them */
358 static int fixed_size = 0, fixed_pos = 0;
361 static int minimum_width, minimum_height;
362 static int maximum_width;
367 static int sensor_device;
370 static long color0, color1, color2, color3, color4, color5, color6, color7,
373 #define MAX_TEMPLATES 10
374 static char *template[MAX_TEMPLATES];
376 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
377 unsigned int max_user_text;
379 /* maximum size of individual text buffers, ie $exec buffer size */
380 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
383 #define CODEPAGE_LENGTH 20
385 long iconv_count = 0;
386 char iconv_converting;
387 static iconv_t **iconv_cd = 0;
389 int register_iconv(iconv_t *new_iconv)
391 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
393 CRIT_ERR(NULL, NULL, "Out of memory");
395 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
396 if (!iconv_cd[iconv_count]) {
397 CRIT_ERR(NULL, NULL, "Out of memory");
399 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
404 void free_iconv(void)
409 for (i = 0; i < iconv_count; i++) {
411 iconv_close(*iconv_cd[i]);
425 /* no buffers in used memory? */
428 /* pad percentages to decimals? */
429 static int pad_percents = 0;
431 static char *global_text = 0;
432 long global_text_lines;
434 static int total_updates;
435 static int updatereset;
437 int check_contains(char *f, char *s)
440 FILE *where = open_file(f, 0);
445 while (fgets(buf1, 256, where)) {
446 if (strstr(buf1, s)) {
453 ERR("Could not open the file");
460 #define SECRIT_MULTILINE_CHAR '\x02'
462 static inline int calc_text_width(const char *s, int l)
464 if ((output_methods & TO_X) == 0) {
472 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
473 (const FcChar8 *) s, l, &gi);
475 XftTextExtents8(display, fonts[selected_font].xftfont,
476 (const FcChar8 *) s, l, &gi);
482 return XTextWidth(fonts[selected_font].font, s, l);
487 /* formatted text to render on screen, generated in generate_text(),
488 * drawn in draw_stuff() */
490 static char *text_buffer;
492 /* quite boring functions */
494 static inline void for_each_line(char *b, int f(char *, int))
497 int special_index = 0; /* specials index */
499 for (ps = b, pe = b; *pe; pe++) {
502 special_index = f(ps, special_index);
509 f(ps, special_index);
513 static void convert_escapes(char *buf)
515 char *p = buf, *s = buf;
522 } else if (*s == '\\') {
533 /* Prints anything normally printed with snprintf according to the current value
534 * of use_spacer. Actually slightly more flexible than snprintf, as you can
535 * safely specify the destination buffer as one of your inputs. */
536 int spaced_print(char *buf, int size, const char *format, int width, ...)
545 tempbuf = malloc(size * sizeof(char));
547 // Passes the varargs along to vsnprintf
548 va_start(argp, width);
549 vsnprintf(tempbuf, size, format, argp);
552 switch (use_spacer) {
554 len = snprintf(buf, size, "%s", tempbuf);
557 len = snprintf(buf, size, "%*s", width, tempbuf);
560 len = snprintf(buf, size, "%-*s", width, tempbuf);
567 /* print percentage values
569 * - i.e., unsigned values between 0 and 100
570 * - respect the value of pad_percents */
571 static int percent_print(char *buf, int size, unsigned value)
573 return spaced_print(buf, size, "%u", pad_percents, value);
576 /* converts from bytes to human readable format (K, M, G, T)
578 * The algorithm always divides by 1024, as unit-conversion of byte
579 * counts suggests. But for output length determination we need to
580 * compare with 1000 here, as we print in decimal form. */
581 static void human_readable(long long num, char *buf, int size)
583 const char **suffix = suffixes;
589 /* Possibly just output as usual, for example for stdout usage */
590 if (!format_human_readable) {
591 spaced_print(buf, size, "%d", 6, round_to_int(num));
602 if (llabs(num) < 1000LL) {
603 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
607 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
615 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
617 * The goal is to always have a significance of 3, by
618 * adjusting the decimal part of the number. Sample output:
622 * so the point of alignment resides between number and unit. The
623 * upside of this is that there is minimal padding necessary, though
624 * there should be a way to make alignment take place at the decimal
625 * dot (then with fixed width decimal part).
627 * Note the repdigits below: when given a precision value, printf()
628 * rounds the float to it, not just cuts off the remaining digits. So
629 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
630 * printed with a precision of 0. Yay. */
632 precision = 0; /* print 100-999 without decimal part */
634 precision = 1; /* print 10-99 with one decimal place */
636 precision = 2; /* print 0-9 with two decimal places */
638 spaced_print(buf, size, format, width, precision, fnum, *suffix);
641 /* global object list root element */
642 static struct text_object global_root_object;
644 static inline void read_exec(const char *data, char *buf, const int size)
646 FILE *fp = popen(data, "r");
647 int length = fread(buf, 1, size, fp);
651 if (length > 0 && buf[length - 1] == '\n') {
652 buf[length - 1] = '\0';
656 void *threaded_exec(void *) __attribute__((noreturn));
658 void *threaded_exec(void *arg)
661 struct text_object *obj = (struct text_object *)arg;
664 buff = malloc(text_buffer_size);
665 read_exec(obj->data.texeci.cmd, buff,
674 timed_thread_lock(obj->data.texeci.p_timed_thread);
675 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
676 timed_thread_unlock(obj->data.texeci.p_timed_thread);
678 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
679 timed_thread_exit(obj->data.texeci.p_timed_thread);
685 static struct text_object *new_text_object_internal(void)
687 struct text_object *obj = malloc(sizeof(struct text_object));
688 memset(obj, 0, sizeof(struct text_object));
693 * Frees the list of text objects root points to. When internal = 1, it won't
694 * free global objects.
696 static void free_text_objects(struct text_object *root, int internal)
698 struct text_object *obj;
704 #define data obj->data
705 for (obj = root->prev; obj; obj = root->prev) {
706 root->prev = obj->prev;
712 #endif /* !__OpenBSD__ */
717 close(data.sysfs.fd);
719 #endif /* __linux__ */
721 free(data.read_tcp.host);
728 free(data.tztime.tz);
729 free(data.tztime.fmt);
732 free(data.mboxscan.args);
733 free(data.mboxscan.output);
738 case OBJ_unseen_mails:
739 case OBJ_flagged_mails:
740 case OBJ_unflagged_mails:
741 case OBJ_forwarded_mails:
742 case OBJ_unforwarded_mails:
743 case OBJ_replied_mails:
744 case OBJ_unreplied_mails:
745 case OBJ_draft_mails:
746 case OBJ_trashed_mails:
747 free(data.local_mail.box);
749 case OBJ_imap_unseen:
754 case OBJ_imap_messages:
759 case OBJ_pop3_unseen:
771 free_text_objects(obj->sub, 1);
774 case OBJ_if_existing:
777 free(data.ifblock.s);
778 free(data.ifblock.str);
781 free(data.tail.logfile);
782 free(data.tail.buffer);
798 case OBJ_iconv_start:
803 case OBJ_disk_protect:
807 free(data.ifblock.s);
808 free(data.ifblock.str);
811 if (info.gw_info.iface) {
812 free(info.gw_info.iface);
813 info.gw_info.iface = 0;
815 if (info.gw_info.ip) {
816 free(info.gw_info.ip);
820 case OBJ_ioscheduler:
825 #if (defined(__FreeBSD__) || defined(__linux__))
827 free(data.ifblock.s);
828 free(data.ifblock.str);
832 case OBJ_xmms2_artist:
833 if (info.xmms2.artist) {
834 free(info.xmms2.artist);
835 info.xmms2.artist = 0;
838 case OBJ_xmms2_album:
839 if (info.xmms2.album) {
840 free(info.xmms2.album);
841 info.xmms2.album = 0;
844 case OBJ_xmms2_title:
845 if (info.xmms2.title) {
846 free(info.xmms2.title);
847 info.xmms2.title = 0;
850 case OBJ_xmms2_genre:
851 if (info.xmms2.genre) {
852 free(info.xmms2.genre);
853 info.xmms2.genre = 0;
856 case OBJ_xmms2_comment:
857 if (info.xmms2.comment) {
858 free(info.xmms2.comment);
859 info.xmms2.comment = 0;
863 if (info.xmms2.url) {
864 free(info.xmms2.url);
869 if (info.xmms2.date) {
870 free(info.xmms2.date);
874 case OBJ_xmms2_status:
875 if (info.xmms2.status) {
876 free(info.xmms2.status);
877 info.xmms2.status = 0;
880 case OBJ_xmms2_playlist:
881 if (info.xmms2.playlist) {
882 free(info.xmms2.playlist);
883 info.xmms2.playlist = 0;
886 case OBJ_xmms2_smart:
887 if (info.xmms2.artist) {
888 free(info.xmms2.artist);
889 info.xmms2.artist = 0;
891 if (info.xmms2.title) {
892 free(info.xmms2.title);
893 info.xmms2.title = 0;
895 if (info.xmms2.url) {
896 free(info.xmms2.url);
903 case OBJ_bmpx_artist:
907 case OBJ_bmpx_bitrate:
922 free(data.rss.action);
927 free(data.weather.uri);
928 free(data.weather.data_type);
940 #endif /* HAVE_LUA */
947 case OBJ_battery_short:
950 case OBJ_battery_time:
953 #endif /* !__OpenBSD__ */
961 free(data.execi.cmd);
962 free(data.execi.buffer);
965 if (data.texeci.p_timed_thread) timed_thread_destroy(data.texeci.p_timed_thread, &data.texeci.p_timed_thread);
966 free(data.texeci.cmd);
967 free(data.texeci.buffer);
978 if (info.first_process && !internal) {
979 free_all_processes();
980 info.first_process = NULL;
982 if (data.top.s) free(data.top.s);
986 free(data.hddtemp.dev);
987 free(data.hddtemp.addr);
988 if (data.hddtemp.temp)
989 free(data.hddtemp.temp);
992 case OBJ_entropy_avail:
993 case OBJ_entropy_perc:
994 case OBJ_entropy_poolsize:
995 case OBJ_entropy_bar:
998 if (info.users.names) {
999 free(info.users.names);
1000 info.users.names = 0;
1003 case OBJ_user_terms:
1004 if (info.users.terms) {
1005 free(info.users.terms);
1006 info.users.terms = 0;
1009 case OBJ_user_times:
1010 if (info.users.times) {
1011 free(info.users.times);
1012 info.users.times = 0;
1017 case OBJ_smapi_bat_perc:
1018 case OBJ_smapi_bat_temp:
1019 case OBJ_smapi_bat_power:
1022 case OBJ_if_smapi_bat_installed:
1023 free(data.ifblock.s);
1024 free(data.ifblock.str);
1033 case OBJ_mpd_artist:
1035 case OBJ_mpd_random:
1036 case OBJ_mpd_repeat:
1038 case OBJ_mpd_bitrate:
1039 case OBJ_mpd_status:
1041 case OBJ_mpd_elapsed:
1042 case OBJ_mpd_length:
1046 case OBJ_mpd_percent:
1048 case OBJ_if_mpd_playing:
1056 case OBJ_moc_artist:
1059 case OBJ_moc_totaltime:
1060 case OBJ_moc_timeleft:
1061 case OBJ_moc_curtime:
1062 case OBJ_moc_bitrate:
1069 free_text_objects(obj->sub, 1);
1073 free(data.scroll.text);
1074 free_text_objects(obj->sub, 1);
1078 free(data.combine.left);
1079 free(data.combine.seperation);
1080 free(data.combine.right);
1081 free_text_objects(obj->sub, 1);
1086 case OBJ_apcupsd_name:
1087 case OBJ_apcupsd_model:
1088 case OBJ_apcupsd_upsmode:
1089 case OBJ_apcupsd_cable:
1090 case OBJ_apcupsd_status:
1091 case OBJ_apcupsd_linev:
1092 case OBJ_apcupsd_load:
1093 case OBJ_apcupsd_loadbar:
1095 case OBJ_apcupsd_loadgraph:
1096 case OBJ_apcupsd_loadgauge:
1098 case OBJ_apcupsd_charge:
1099 case OBJ_apcupsd_timeleft:
1100 case OBJ_apcupsd_temp:
1101 case OBJ_apcupsd_lastxfer:
1103 #endif /* APCUPSD */
1106 case OBJ_desktop_number:
1107 case OBJ_desktop_name:
1108 if(info.x11.desktop.name) {
1109 free(info.x11.desktop.name);
1110 info.x11.desktop.name = NULL;
1112 if(info.x11.desktop.all_names) {
1113 free(info.x11.desktop.all_names);
1114 info.x11.desktop.all_names = NULL;
1125 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1130 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1131 *a = mixer_init(buf1);
1132 scan_bar(arg + n, w, h);
1134 *a = mixer_init(NULL);
1135 scan_bar(arg, w, h);
1140 /* strip a leading /dev/ if any, following symlinks first
1142 * BEWARE: this function returns a pointer to static content
1143 * which gets overwritten in consecutive calls. I.e.:
1144 * this function is NOT reentrant.
1146 static const char *dev_name(const char *path)
1148 static char buf[255]; /* should be enough for pathnames */
1154 #define DEV_NAME(x) \
1155 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1156 if ((buflen = readlink(path, buf, 254)) == -1)
1157 return DEV_NAME(path);
1159 return DEV_NAME(buf);
1163 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1168 if (obj->data.top.was_parsed) {
1171 obj->data.top.was_parsed = 1;
1173 if (arg && !obj->data.top.s) {
1174 obj->data.top.s = strndup(arg, text_buffer_size);
1177 need_mask |= (1 << INFO_TOP);
1180 obj->type = OBJ_top;
1182 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1183 obj->type = OBJ_top_mem;
1185 } else if (strcmp(&s[3], "_time") == EQUAL) {
1186 obj->type = OBJ_top_time;
1189 } else if (strcmp(&s[3], "_io") == EQUAL) {
1190 obj->type = OBJ_top_io;
1195 ERR("Must be top, top_mem, top_time or top_io");
1197 ERR("Must be top, top_mem or top_time");
1203 ERR("top needs arguments");
1207 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1208 if (strcmp(buf, "name") == EQUAL) {
1209 obj->data.top.type = TOP_NAME;
1210 } else if (strcmp(buf, "cpu") == EQUAL) {
1211 obj->data.top.type = TOP_CPU;
1212 } else if (strcmp(buf, "pid") == EQUAL) {
1213 obj->data.top.type = TOP_PID;
1214 } else if (strcmp(buf, "mem") == EQUAL) {
1215 obj->data.top.type = TOP_MEM;
1216 } else if (strcmp(buf, "time") == EQUAL) {
1217 obj->data.top.type = TOP_TIME;
1218 } else if (strcmp(buf, "mem_res") == EQUAL) {
1219 obj->data.top.type = TOP_MEM_RES;
1220 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1221 obj->data.top.type = TOP_MEM_VSIZE;
1223 } else if (strcmp(buf, "io_read") == EQUAL) {
1224 obj->data.top.type = TOP_READ_BYTES;
1225 } else if (strcmp(buf, "io_write") == EQUAL) {
1226 obj->data.top.type = TOP_WRITE_BYTES;
1227 } else if (strcmp(buf, "io_perc") == EQUAL) {
1228 obj->data.top.type = TOP_IO_PERC;
1231 ERR("invalid type arg for top");
1233 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1234 "io_read, io_write, io_perc");
1236 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1240 if (n < 1 || n > 10) {
1241 ERR("invalid num arg for top. Must be between 1 and 10.");
1244 obj->data.top.num = n - 1;
1247 ERR("invalid argument count for top");
1253 long current_text_color;
1255 /* construct_text_object() creates a new text_object */
1256 static struct text_object *construct_text_object(const char *s,
1257 const char *arg, long line, void **ifblock_opaque, void *free_at_crash)
1259 // struct text_object *obj = new_text_object();
1260 struct text_object *obj = new_text_object_internal();
1264 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1265 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1266 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1267 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1268 obj_be_ifblock_if(ifblock_opaque, obj); {
1269 #define END } } else
1271 #define SIZE_DEFAULTS(arg) { \
1272 obj->a = default_##arg##_width; \
1273 obj->b = default_##arg##_height; \
1278 obj->type = OBJ_color;
1279 obj->data.l = get_x11_color(s);
1283 OBJ(freq, INFO_FREQ)
1286 obj->data.i = open_acpi_temperature(arg);
1287 END OBJ(acpiacadapter, 0)
1288 END OBJ(freq, INFO_FREQ)
1289 #endif /* !__OpenBSD__ */
1291 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1292 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1293 obj->data.cpu_index = 1;
1294 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1295 "Displaying the clock for CPU 1."); */
1297 obj->data.cpu_index = atoi(&arg[0]);
1300 END OBJ(freq_g, INFO_FREQ)
1302 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1303 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1304 obj->data.cpu_index = 1;
1305 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1306 "CPUs! Displaying the clock for CPU 1."); */
1308 obj->data.cpu_index = atoi(&arg[0]);
1311 END OBJ(read_tcp, 0)
1313 obj->data.read_tcp.host = malloc(text_buffer_size);
1314 sscanf(arg, "%s", obj->data.read_tcp.host);
1315 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1316 if(obj->data.read_tcp.port == 0) {
1317 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1318 strcpy(obj->data.read_tcp.host,"localhost");
1320 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1321 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1322 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1325 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1327 #if defined(__linux__)
1328 END OBJ(voltage_mv, 0)
1330 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1331 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1332 obj->data.cpu_index = 1;
1333 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1334 "CPUs! Displaying voltage for CPU 1."); */
1336 obj->data.cpu_index = atoi(&arg[0]);
1339 END OBJ(voltage_v, 0)
1341 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1342 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1343 obj->data.cpu_index = 1;
1344 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1345 "CPUs! Displaying voltage for CPU 1."); */
1347 obj->data.cpu_index = atoi(&arg[0]);
1352 END OBJ(wireless_essid, INFO_NET)
1354 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1356 // default to DEFAULTNETDEV
1357 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1358 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1361 END OBJ(wireless_mode, INFO_NET)
1363 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1365 // default to DEFAULTNETDEV
1366 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1367 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1370 END OBJ(wireless_bitrate, INFO_NET)
1372 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1374 // default to DEFAULTNETDEV
1375 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1376 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1379 END OBJ(wireless_ap, INFO_NET)
1381 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1383 // default to DEFAULTNETDEV
1384 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1385 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1388 END OBJ(wireless_link_qual, INFO_NET)
1390 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1392 // default to DEFAULTNETDEV
1393 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1394 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1397 END OBJ(wireless_link_qual_max, INFO_NET)
1399 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1401 // default to DEFAULTNETDEV
1402 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1403 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1406 END OBJ(wireless_link_qual_perc, INFO_NET)
1408 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1410 // default to DEFAULTNETDEV
1411 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1412 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1415 END OBJ(wireless_link_bar, INFO_NET)
1418 arg = scan_bar(arg, &obj->a, &obj->b);
1419 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1421 // default to DEFAULTNETDEV
1422 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1423 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1426 #endif /* HAVE_IWLIB */
1428 #endif /* __linux__ */
1436 sscanf(arg, "%63s", bat);
1438 strcpy(bat, "BAT0");
1440 obj->data.s = strndup(bat, text_buffer_size);
1441 END OBJ(battery_short, 0)
1445 sscanf(arg, "%63s", bat);
1447 strcpy(bat, "BAT0");
1449 obj->data.s = strndup(bat, text_buffer_size);
1450 END OBJ(battery_time, 0)
1454 sscanf(arg, "%63s", bat);
1456 strcpy(bat, "BAT0");
1458 obj->data.s = strndup(bat, text_buffer_size);
1459 END OBJ(battery_percent, 0)
1463 sscanf(arg, "%63s", bat);
1465 strcpy(bat, "BAT0");
1467 obj->data.s = strndup(bat, text_buffer_size);
1468 END OBJ(battery_bar, 0)
1473 arg = scan_bar(arg, &obj->a, &obj->b);
1474 sscanf(arg, "%63s", bat);
1476 strcpy(bat, "BAT0");
1478 obj->data.s = strndup(bat, text_buffer_size);
1479 #endif /* !__OpenBSD__ */
1481 #if defined(__linux__)
1482 END OBJ(disk_protect, 0)
1484 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1486 CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1487 END OBJ(i8k_version, INFO_I8K)
1488 END OBJ(i8k_bios, INFO_I8K)
1489 END OBJ(i8k_serial, INFO_I8K)
1490 END OBJ(i8k_cpu_temp, INFO_I8K)
1491 END OBJ(i8k_left_fan_status, INFO_I8K)
1492 END OBJ(i8k_right_fan_status, INFO_I8K)
1493 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1494 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1495 END OBJ(i8k_ac_status, INFO_I8K)
1496 END OBJ(i8k_buttons_status, INFO_I8K)
1499 END OBJ(ibm_temps, 0)
1501 CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1503 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1504 obj->data.sensor = 0;
1505 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1506 "Using 0 (CPU temp sensor).");
1508 obj->data.sensor = atoi(&arg[0]);
1509 END OBJ(ibm_volume, 0)
1510 END OBJ(ibm_brightness, 0)
1512 /* information from sony_laptop kernel module
1513 * /sys/devices/platform/sony-laptop */
1514 END OBJ(sony_fanspeed, 0)
1515 END OBJ_IF(if_gw, INFO_GW)
1516 END OBJ(ioscheduler, 0)
1518 CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1521 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1522 END OBJ(laptop_mode, 0)
1523 END OBJ(pb_battery, 0)
1524 if (arg && strcmp(arg, "status") == EQUAL) {
1525 obj->data.i = PB_BATT_STATUS;
1526 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1527 obj->data.i = PB_BATT_PERCENT;
1528 } else if (arg && strcmp(arg, "time") == EQUAL) {
1529 obj->data.i = PB_BATT_TIME;
1531 ERR("pb_battery: needs one argument: status, percent or time");
1536 #endif /* __linux__ */
1537 #if (defined(__FreeBSD__) || defined(__linux__))
1538 END OBJ_IF(if_up, 0)
1540 ERR("if_up needs an argument");
1541 obj->data.ifblock.s = 0;
1543 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1546 #if defined(__OpenBSD__)
1547 END OBJ(obsd_sensors_temp, 0)
1549 CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1551 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1552 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1553 obj->data.sensor = 0;
1554 ERR("Invalid temperature sensor number!");
1556 obj->data.sensor = atoi(&arg[0]);
1557 END OBJ(obsd_sensors_fan, 0)
1559 CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1562 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1563 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1564 obj->data.sensor = 0;
1565 ERR("Invalid fan sensor number!");
1567 obj->data.sensor = atoi(&arg[0]);
1568 END OBJ(obsd_sensors_volt, 0)
1570 CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1573 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1574 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1575 obj->data.sensor = 0;
1576 ERR("Invalid voltage sensor number!");
1578 obj->data.sensor = atoi(&arg[0]);
1579 END OBJ(obsd_vendor, 0)
1580 END OBJ(obsd_product, 0)
1581 #endif /* __OpenBSD__ */
1582 END OBJ(buffers, INFO_BUFFERS)
1583 END OBJ(cached, INFO_BUFFERS)
1584 #define SCAN_CPU(__arg, __var) { \
1586 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1587 __arg += __offset; \
1591 END OBJ(cpu, INFO_CPU)
1592 SCAN_CPU(arg, obj->data.cpu_index);
1593 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1595 END OBJ(cpugauge, INFO_CPU)
1596 SIZE_DEFAULTS(gauge);
1597 SCAN_CPU(arg, obj->data.cpu_index);
1598 scan_gauge(arg, &obj->a, &obj->b);
1599 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1601 END OBJ(cpubar, INFO_CPU)
1603 SCAN_CPU(arg, obj->data.cpu_index);
1604 scan_bar(arg, &obj->a, &obj->b);
1605 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1607 END OBJ(cpugraph, INFO_CPU)
1609 SIZE_DEFAULTS(graph);
1610 SCAN_CPU(arg, obj->data.cpu_index);
1611 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1612 &obj->e, &obj->char_a, &obj->char_b);
1613 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1615 END OBJ(loadgraph, INFO_LOADAVG)
1617 SIZE_DEFAULTS(graph);
1618 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1619 &obj->e, &obj->char_a, &obj->char_b);
1623 r = sscanf(arg, "%d", &a);
1625 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1629 END OBJ(diskio, INFO_DISKIO)
1630 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1631 END OBJ(diskio_read, INFO_DISKIO)
1632 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1633 END OBJ(diskio_write, INFO_DISKIO)
1634 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1636 END OBJ(diskiograph, INFO_DISKIO)
1638 SIZE_DEFAULTS(graph);
1639 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1640 &obj->e, &obj->char_a, &obj->char_b);
1642 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1644 END OBJ(diskiograph_read, INFO_DISKIO)
1646 SIZE_DEFAULTS(graph);
1647 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1648 &obj->e, &obj->char_a, &obj->char_b);
1650 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1652 END OBJ(diskiograph_write, INFO_DISKIO)
1654 SIZE_DEFAULTS(graph);
1655 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1656 &obj->e, &obj->char_a, &obj->char_b);
1658 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1663 if (output_methods & TO_X) {
1664 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1665 current_text_color = obj->data.l;
1669 obj->data.l = color0;
1670 current_text_color = obj->data.l;
1672 obj->data.l = color1;
1673 current_text_color = obj->data.l;
1675 obj->data.l = color2;
1676 current_text_color = obj->data.l;
1678 obj->data.l = color3;
1679 current_text_color = obj->data.l;
1681 obj->data.l = color4;
1682 current_text_color = obj->data.l;
1684 obj->data.l = color5;
1685 current_text_color = obj->data.l;
1687 obj->data.l = color6;
1688 current_text_color = obj->data.l;
1690 obj->data.l = color7;
1691 current_text_color = obj->data.l;
1693 obj->data.l = color8;
1694 current_text_color = obj->data.l;
1696 obj->data.l = color9;
1697 current_text_color = obj->data.l;
1700 obj->data.s = scan_font(arg);
1702 END OBJ(conky_version, 0)
1703 END OBJ(conky_build_date, 0)
1704 END OBJ(conky_build_arch, 0)
1705 END OBJ(downspeed, INFO_NET)
1707 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1709 // default to DEFAULTNETDEV
1710 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1711 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1714 END OBJ(downspeedf, INFO_NET)
1716 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1718 // default to DEFAULTNETDEV
1719 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1720 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1724 END OBJ(downspeedgraph, INFO_NET)
1726 SIZE_DEFAULTS(graph);
1727 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1728 &obj->e, &obj->char_a, &obj->char_b);
1730 // default to DEFAULTNETDEV
1731 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1732 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1736 obj_be_ifblock_else(ifblock_opaque, obj);
1738 obj_be_ifblock_endif(ifblock_opaque, obj);
1740 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1742 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1745 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1747 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1750 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1752 END OBJ(execgauge, 0)
1753 SIZE_DEFAULTS(gauge);
1754 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1755 END OBJ(execgraph, 0)
1756 SIZE_DEFAULTS(graph);
1757 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1759 END OBJ(execibar, 0)
1763 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1766 ERR("${execibar <interval> command}");
1767 obj->type = OBJ_text;
1768 snprintf(buf, 256, "${%s}", s);
1769 obj->data.s = strndup(buf, text_buffer_size);
1771 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1774 END OBJ(execigraph, 0)
1776 SIZE_DEFAULTS(graph);
1778 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1781 ERR("${execigraph <interval> command}");
1782 obj->type = OBJ_text;
1783 snprintf(buf, 256, "${%s}", s);
1784 obj->data.s = strndup(buf, text_buffer_size);
1786 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1788 END OBJ(execigauge, 0)
1790 SIZE_DEFAULTS(gauge);
1792 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1795 ERR("${execigauge <interval> command}");
1796 obj->type = OBJ_text;
1797 snprintf(buf, 256, "${%s}", s);
1798 obj->data.s = strndup(buf, text_buffer_size);
1800 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1806 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1809 ERR("${execi <interval> command}");
1810 obj->type = OBJ_text;
1811 snprintf(buf, 256, "${%s}", s);
1812 obj->data.s = strndup(buf, text_buffer_size);
1814 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1815 obj->data.execi.buffer = malloc(text_buffer_size);
1820 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1823 ERR("${execi <interval> command}");
1824 obj->type = OBJ_text;
1825 snprintf(buf, 256, "${%s}", s);
1826 obj->data.s = strndup(buf, text_buffer_size);
1828 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1829 obj->data.execi.buffer = malloc(text_buffer_size);
1834 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1837 ERR("${texeci <interval> command}");
1838 obj->type = OBJ_text;
1839 snprintf(buf, 256, "${%s}", s);
1840 obj->data.s = strndup(buf, text_buffer_size);
1842 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1843 obj->data.texeci.buffer = malloc(text_buffer_size);
1845 obj->data.texeci.p_timed_thread = NULL;
1846 END OBJ(pre_exec, 0)
1847 obj->type = OBJ_text;
1851 read_exec(arg, buf, sizeof(buf));
1852 obj->data.s = strndup(buf, text_buffer_size);
1854 obj->data.s = strndup("", text_buffer_size);
1857 END OBJ(fs_bar, INFO_FS)
1859 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1861 while (isspace(*arg)) {
1870 obj->data.fsbar.fs = prepare_fs_stat(arg);
1871 END OBJ(fs_bar_free, INFO_FS)
1873 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1875 while (isspace(*arg)) {
1885 obj->data.fsbar.fs = prepare_fs_stat(arg);
1886 END OBJ(fs_free, INFO_FS)
1890 obj->data.fs = prepare_fs_stat(arg);
1891 END OBJ(fs_used_perc, INFO_FS)
1895 obj->data.fs = prepare_fs_stat(arg);
1896 END OBJ(fs_free_perc, INFO_FS)
1900 obj->data.fs = prepare_fs_stat(arg);
1901 END OBJ(fs_size, INFO_FS)
1905 obj->data.fs = prepare_fs_stat(arg);
1906 END OBJ(fs_type, INFO_FS)
1910 obj->data.fs = prepare_fs_stat(arg);
1911 END OBJ(fs_used, INFO_FS)
1915 obj->data.fs = prepare_fs_stat(arg);
1917 obj->data.i = arg ? atoi(arg) : 1;
1918 END OBJ(nameserver, INFO_DNS)
1919 obj->data.i = arg ? atoi(arg) : 0;
1921 obj->data.i = arg ? atoi(arg) : 1;
1923 obj->data.i = arg ? atoi(arg) : 1;
1927 ERR("goto needs arguments");
1928 obj->type = OBJ_text;
1929 obj->data.s = strndup("${goto}", text_buffer_size);
1933 obj->data.i = atoi(arg);
1939 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1940 sscanf(arg, "%d", &b);
1946 obj->data.pair.a = a;
1947 obj->data.pair.b = b;
1950 END OBJ(i2c, INFO_SYSFS)
1951 char buf1[64], buf2[64];
1952 float factor, offset;
1956 ERR("i2c needs arguments");
1957 obj->type = OBJ_text;
1958 // obj->data.s = strndup("${i2c}", text_buffer_size);
1962 #define HWMON_RESET() {\
1967 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1968 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1969 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1970 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1973 ERR("i2c failed to parse arguments");
1974 obj->type = OBJ_text;
1977 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1978 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
1979 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1980 strncpy(obj->data.sysfs.type, buf2, 63);
1981 obj->data.sysfs.factor = factor;
1982 obj->data.sysfs.offset = offset;
1984 END OBJ(platform, INFO_SYSFS)
1985 char buf1[64], buf2[64];
1986 float factor, offset;
1990 ERR("platform needs arguments");
1991 obj->type = OBJ_text;
1995 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1996 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1997 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1998 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2001 ERR("platform failed to parse arguments");
2002 obj->type = OBJ_text;
2005 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2006 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
2007 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2008 strncpy(obj->data.sysfs.type, buf2, 63);
2009 obj->data.sysfs.factor = factor;
2010 obj->data.sysfs.offset = offset;
2012 END OBJ(hwmon, INFO_SYSFS)
2013 char buf1[64], buf2[64];
2014 float factor, offset;
2018 ERR("hwmon needs argumanets");
2019 obj->type = OBJ_text;
2023 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2024 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2025 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2026 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2031 ERR("hwmon failed to parse arguments");
2032 obj->type = OBJ_text;
2035 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2036 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2037 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2038 strncpy(obj->data.sysfs.type, buf2, 63);
2039 obj->data.sysfs.factor = factor;
2040 obj->data.sysfs.offset = offset;
2042 #endif /* !__OpenBSD__ */
2045 /* we have four different types of top (top, top_mem, top_time and top_io). To
2046 * avoid having almost-same code four times, we have this special
2048 if (strncmp(s, "top", 3) == EQUAL) {
2049 if (!parse_top_args(s, arg, obj)) {
2052 } else OBJ(addr, INFO_NET)
2054 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2056 // default to DEFAULTNETDEV
2057 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2058 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2061 #if defined(__linux__)
2062 END OBJ(addrs, INFO_NET)
2064 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2066 // default to DEFAULTNETDEV
2067 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2068 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2071 #endif /* __linux__ */
2073 if (init_tail_object(obj, arg)) {
2074 obj->type = OBJ_text;
2075 obj->data.s = strndup("${tail}", text_buffer_size);
2078 if (init_head_object(obj, arg)) {
2079 obj->type = OBJ_text;
2080 obj->data.s = strndup("${head}", text_buffer_size);
2084 obj->data.s = strndup(arg, text_buffer_size);
2086 CRIT_ERR(obj, free_at_crash, "lines needs a argument");
2090 obj->data.s = strndup(arg, text_buffer_size);
2092 CRIT_ERR(obj, free_at_crash, "words needs a argument");
2094 END OBJ(loadavg, INFO_LOADAVG)
2095 int a = 1, b = 2, c = 3, r = 3;
2098 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2099 if (r >= 3 && (c < 1 || c > 3)) {
2102 if (r >= 2 && (b < 1 || b > 3)) {
2105 if (r >= 1 && (a < 1 || a > 3)) {
2109 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2110 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2111 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2112 END OBJ_IF(if_empty, 0)
2114 ERR("if_empty needs an argument");
2115 obj->data.ifblock.s = 0;
2117 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2118 obj->sub = malloc(sizeof(struct text_object));
2119 extract_variable_text_internal(obj->sub,
2120 obj->data.ifblock.s);
2122 END OBJ_IF(if_match, 0)
2124 ERR("if_match needs arguments");
2125 obj->data.ifblock.s = 0;
2127 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2128 obj->sub = malloc(sizeof(struct text_object));
2129 extract_variable_text_internal(obj->sub,
2130 obj->data.ifblock.s);
2132 END OBJ_IF(if_existing, 0)
2134 ERR("if_existing needs an argument or two");
2135 obj->data.ifblock.s = NULL;
2136 obj->data.ifblock.str = NULL;
2138 char buf1[256], buf2[256];
2139 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2142 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2143 obj->data.ifblock.str = NULL;
2145 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2146 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2149 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2150 END OBJ_IF(if_mounted, 0)
2152 ERR("if_mounted needs an argument");
2153 obj->data.ifblock.s = 0;
2155 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2158 END OBJ_IF(if_running, INFO_TOP)
2160 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2162 END OBJ_IF(if_running, 0)
2166 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2167 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2170 ERR("if_running needs an argument");
2171 obj->data.ifblock.s = 0;
2177 char box[256], dst[256];
2181 /* Kapil: Changed from MAIL_FILE to
2182 current_mail_spool since the latter
2183 is a copy of the former if undefined
2184 but the latter should take precedence
2186 strncpy(box, current_mail_spool, sizeof(box));
2188 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2190 strncpy(box, arg, sizeof(box));
2194 variable_substitute(box, dst, sizeof(dst));
2195 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2196 obj->data.local_mail.interval = n1;
2197 END OBJ(new_mails, 0)
2199 char box[256], dst[256];
2203 strncpy(box, current_mail_spool, sizeof(box));
2205 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2207 strncpy(box, arg, sizeof(box));
2211 variable_substitute(box, dst, sizeof(dst));
2212 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2213 obj->data.local_mail.interval = n1;
2214 END OBJ(seen_mails, 0)
2216 char box[256], dst[256];
2220 strncpy(box, current_mail_spool, sizeof(box));
2222 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2224 strncpy(box, arg, sizeof(box));
2228 variable_substitute(box, dst, sizeof(dst));
2229 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2230 obj->data.local_mail.interval = n1;
2231 END OBJ(unseen_mails, 0)
2233 char box[256], dst[256];
2237 strncpy(box, current_mail_spool, sizeof(box));
2239 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2241 strncpy(box, arg, sizeof(box));
2245 variable_substitute(box, dst, sizeof(dst));
2246 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2247 obj->data.local_mail.interval = n1;
2248 END OBJ(flagged_mails, 0)
2250 char box[256], dst[256];
2254 strncpy(box, current_mail_spool, sizeof(box));
2256 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2258 strncpy(box, arg, sizeof(box));
2262 variable_substitute(box, dst, sizeof(dst));
2263 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2264 obj->data.local_mail.interval = n1;
2265 END OBJ(unflagged_mails, 0)
2267 char box[256], dst[256];
2271 strncpy(box, current_mail_spool, sizeof(box));
2273 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2275 strncpy(box, arg, sizeof(box));
2279 variable_substitute(box, dst, sizeof(dst));
2280 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2281 obj->data.local_mail.interval = n1;
2282 END OBJ(forwarded_mails, 0)
2284 char box[256], dst[256];
2288 strncpy(box, current_mail_spool, sizeof(box));
2290 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2292 strncpy(box, arg, sizeof(box));
2296 variable_substitute(box, dst, sizeof(dst));
2297 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2298 obj->data.local_mail.interval = n1;
2299 END OBJ(unforwarded_mails, 0)
2301 char box[256], dst[256];
2305 strncpy(box, current_mail_spool, sizeof(box));
2307 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2309 strncpy(box, arg, sizeof(box));
2313 variable_substitute(box, dst, sizeof(dst));
2314 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2315 obj->data.local_mail.interval = n1;
2316 END OBJ(replied_mails, 0)
2318 char box[256], dst[256];
2322 strncpy(box, current_mail_spool, sizeof(box));
2324 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2326 strncpy(box, arg, sizeof(box));
2330 variable_substitute(box, dst, sizeof(dst));
2331 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2332 obj->data.local_mail.interval = n1;
2333 END OBJ(unreplied_mails, 0)
2335 char box[256], dst[256];
2339 strncpy(box, current_mail_spool, sizeof(box));
2341 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2343 strncpy(box, arg, sizeof(box));
2347 variable_substitute(box, dst, sizeof(dst));
2348 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2349 obj->data.local_mail.interval = n1;
2350 END OBJ(draft_mails, 0)
2352 char box[256], dst[256];
2356 strncpy(box, current_mail_spool, sizeof(box));
2358 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2360 strncpy(box, arg, sizeof(box));
2364 variable_substitute(box, dst, sizeof(dst));
2365 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2366 obj->data.local_mail.interval = n1;
2367 END OBJ(trashed_mails, 0)
2369 char box[256], dst[256];
2373 strncpy(box, current_mail_spool, sizeof(box));
2375 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2377 strncpy(box, arg, sizeof(box));
2381 variable_substitute(box, dst, sizeof(dst));
2382 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2383 obj->data.local_mail.interval = n1;
2384 END OBJ(mboxscan, 0)
2385 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2386 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2387 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2388 obj->data.mboxscan.output[0] = 1;
2389 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2390 END OBJ(mem, INFO_MEM)
2391 END OBJ(memeasyfree, INFO_MEM)
2392 END OBJ(memfree, INFO_MEM)
2393 END OBJ(memmax, INFO_MEM)
2394 END OBJ(memperc, INFO_MEM)
2396 END OBJ(memgauge, INFO_MEM)
2397 SIZE_DEFAULTS(gauge);
2398 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2400 END OBJ(membar, INFO_MEM)
2402 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2404 END OBJ(memgraph, INFO_MEM)
2406 SIZE_DEFAULTS(graph);
2407 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2408 &obj->e, &obj->char_a, &obj->char_b);
2412 END OBJ(mixer, INFO_MIXER)
2413 obj->data.l = mixer_init(arg);
2414 END OBJ(mixerl, INFO_MIXER)
2415 obj->data.l = mixer_init(arg);
2416 END OBJ(mixerr, INFO_MIXER)
2417 obj->data.l = mixer_init(arg);
2419 END OBJ(mixerbar, INFO_MIXER)
2421 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2422 &obj->data.mixerbar.h);
2423 END OBJ(mixerlbar, INFO_MIXER)
2425 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2426 &obj->data.mixerbar.h);
2427 END OBJ(mixerrbar, INFO_MIXER)
2429 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2430 &obj->data.mixerbar.h);
2432 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2433 obj->data.ifblock.i = mixer_init(arg);
2435 END OBJ(monitor, INFO_X11)
2436 END OBJ(monitor_number, INFO_X11)
2437 END OBJ(desktop, INFO_X11)
2438 END OBJ(desktop_number, INFO_X11)
2439 END OBJ(desktop_name, INFO_X11)
2441 END OBJ(nodename, 0)
2442 END OBJ(processes, INFO_PROCS)
2443 END OBJ(running_processes, INFO_RUN_PROCS)
2444 END OBJ(shadecolor, 0)
2446 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2448 END OBJ(outlinecolor, 0)
2450 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2452 END OBJ(stippled_hr, 0)
2454 int a = stippled_borders, b = 1;
2457 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2458 sscanf(arg, "%d", &b);
2464 obj->data.pair.a = a;
2465 obj->data.pair.b = b;
2467 END OBJ(swap, INFO_MEM)
2468 END OBJ(swapfree, INFO_MEM)
2469 END OBJ(swapmax, INFO_MEM)
2470 END OBJ(swapperc, INFO_MEM)
2471 END OBJ(swapbar, INFO_MEM)
2473 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2476 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2478 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2480 char buf1[256], buf2[256], *fmt, *tz;
2484 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2494 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2495 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2497 END OBJ(iconv_start, 0)
2498 if (iconv_converting) {
2499 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
2500 "starting another");
2503 char iconv_from[CODEPAGE_LENGTH];
2504 char iconv_to[CODEPAGE_LENGTH];
2506 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2507 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
2511 new_iconv = iconv_open(iconv_to, iconv_from);
2512 if (new_iconv == (iconv_t) (-1)) {
2513 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2515 obj->a = register_iconv(&new_iconv);
2516 iconv_converting = 1;
2520 CRIT_ERR(obj, free_at_crash, "Iconv requires arguments");
2522 END OBJ(iconv_stop, 0)
2523 iconv_converting = 0;
2526 END OBJ(totaldown, INFO_NET)
2528 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2530 // default to DEFAULTNETDEV
2531 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2532 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2535 END OBJ(totalup, INFO_NET)
2536 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2538 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2540 // default to DEFAULTNETDEV
2541 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2542 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2546 END OBJ_IF(if_updatenr, 0)
2547 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2548 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
2549 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2551 obj->data.i = arg ? atoi(arg) : 0;
2553 obj->data.i = arg ? atoi(arg) : 0;
2554 END OBJ(upspeed, INFO_NET)
2556 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2558 // default to DEFAULTNETDEV
2559 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2560 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2563 END OBJ(upspeedf, INFO_NET)
2565 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2567 // default to DEFAULTNETDEV
2568 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2569 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2574 END OBJ(upspeedgraph, INFO_NET)
2576 SIZE_DEFAULTS(graph);
2577 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2578 &obj->e, &obj->char_a, &obj->char_b);
2580 // default to DEFAULTNETDEV
2581 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2582 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2585 END OBJ(uptime_short, INFO_UPTIME)
2586 END OBJ(uptime, INFO_UPTIME)
2587 END OBJ(user_names, INFO_USERS)
2588 END OBJ(user_times, INFO_USERS)
2589 END OBJ(user_terms, INFO_USERS)
2590 END OBJ(user_number, INFO_USERS)
2591 #if defined(__linux__)
2592 END OBJ(gw_iface, INFO_GW)
2593 END OBJ(gw_ip, INFO_GW)
2594 #endif /* !__linux__ */
2596 END OBJ(adt746xcpu, 0)
2597 END OBJ(adt746xfan, 0)
2598 #endif /* !__OpenBSD__ */
2599 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2600 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2601 END OBJ(apm_adapter, 0)
2602 END OBJ(apm_battery_life, 0)
2603 END OBJ(apm_battery_time, 0)
2604 #endif /* __FreeBSD__ */
2605 END OBJ(imap_unseen, 0)
2608 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2613 END OBJ(imap_messages, 0)
2616 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2621 END OBJ(pop3_unseen, 0)
2624 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2629 END OBJ(pop3_used, 0)
2632 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2640 obj->data.s = strndup(arg, text_buffer_size);
2642 ERR("smapi needs an argument");
2643 END OBJ_IF(if_smapi_bat_installed, 0)
2645 ERR("if_smapi_bat_installed needs an argument");
2646 obj->data.ifblock.s = 0;
2648 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2649 END OBJ(smapi_bat_perc, 0)
2651 obj->data.s = strndup(arg, text_buffer_size);
2653 ERR("smapi_bat_perc needs an argument");
2654 END OBJ(smapi_bat_temp, 0)
2656 obj->data.s = strndup(arg, text_buffer_size);
2658 ERR("smapi_bat_temp needs an argument");
2659 END OBJ(smapi_bat_power, 0)
2661 obj->data.s = strndup(arg, text_buffer_size);
2663 ERR("smapi_bat_power needs an argument");
2665 END OBJ(smapi_bat_bar, 0)
2669 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2670 ERR("first argument to smapi_bat_bar must be an integer value");
2674 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2677 ERR("smapi_bat_bar needs an argument");
2681 #define mpd_set_maxlen(name) \
2684 sscanf(arg, "%d", &i); \
2686 obj->data.i = i + 1; \
2688 ERR(#name ": invalid length argument"); \
2690 END OBJ(mpd_artist, INFO_MPD)
2691 mpd_set_maxlen(mpd_artist);
2693 END OBJ(mpd_title, INFO_MPD)
2694 mpd_set_maxlen(mpd_title);
2696 END OBJ(mpd_random, INFO_MPD) init_mpd();
2697 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2698 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2699 END OBJ(mpd_length, INFO_MPD) init_mpd();
2700 END OBJ(mpd_track, INFO_MPD)
2701 mpd_set_maxlen(mpd_track);
2703 END OBJ(mpd_name, INFO_MPD)
2704 mpd_set_maxlen(mpd_name);
2706 END OBJ(mpd_file, INFO_MPD)
2707 mpd_set_maxlen(mpd_file);
2709 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2710 END OBJ(mpd_album, INFO_MPD)
2711 mpd_set_maxlen(mpd_album);
2713 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2714 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2715 END OBJ(mpd_status, INFO_MPD) init_mpd();
2716 END OBJ(mpd_bar, INFO_MPD)
2718 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2720 END OBJ(mpd_smart, INFO_MPD)
2721 mpd_set_maxlen(mpd_smart);
2723 END OBJ_IF(if_mpd_playing, INFO_MPD)
2725 #undef mpd_set_maxlen
2728 END OBJ(moc_state, INFO_MOC)
2729 END OBJ(moc_file, INFO_MOC)
2730 END OBJ(moc_title, INFO_MOC)
2731 END OBJ(moc_artist, INFO_MOC)
2732 END OBJ(moc_song, INFO_MOC)
2733 END OBJ(moc_album, INFO_MOC)
2734 END OBJ(moc_totaltime, INFO_MOC)
2735 END OBJ(moc_timeleft, INFO_MOC)
2736 END OBJ(moc_curtime, INFO_MOC)
2737 END OBJ(moc_bitrate, INFO_MOC)
2738 END OBJ(moc_rate, INFO_MOC)
2741 END OBJ(xmms2_artist, INFO_XMMS2)
2742 END OBJ(xmms2_album, INFO_XMMS2)
2743 END OBJ(xmms2_title, INFO_XMMS2)
2744 END OBJ(xmms2_genre, INFO_XMMS2)
2745 END OBJ(xmms2_comment, INFO_XMMS2)
2746 END OBJ(xmms2_url, INFO_XMMS2)
2747 END OBJ(xmms2_tracknr, INFO_XMMS2)
2748 END OBJ(xmms2_bitrate, INFO_XMMS2)
2749 END OBJ(xmms2_date, INFO_XMMS2)
2750 END OBJ(xmms2_id, INFO_XMMS2)
2751 END OBJ(xmms2_duration, INFO_XMMS2)
2752 END OBJ(xmms2_elapsed, INFO_XMMS2)
2753 END OBJ(xmms2_size, INFO_XMMS2)
2754 END OBJ(xmms2_status, INFO_XMMS2)
2755 END OBJ(xmms2_percent, INFO_XMMS2)
2757 END OBJ(xmms2_bar, INFO_XMMS2)
2759 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2761 END OBJ(xmms2_smart, INFO_XMMS2)
2762 END OBJ(xmms2_playlist, INFO_XMMS2)
2763 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2764 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2767 END OBJ(audacious_status, INFO_AUDACIOUS)
2768 END OBJ(audacious_title, INFO_AUDACIOUS)
2770 sscanf(arg, "%d", &info.audacious.max_title_len);
2771 if (info.audacious.max_title_len > 0) {
2772 info.audacious.max_title_len++;
2774 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
2777 END OBJ(audacious_length, INFO_AUDACIOUS)
2778 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2779 END OBJ(audacious_position, INFO_AUDACIOUS)
2780 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2781 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2782 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2783 END OBJ(audacious_channels, INFO_AUDACIOUS)
2784 END OBJ(audacious_filename, INFO_AUDACIOUS)
2785 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2786 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2787 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2789 END OBJ(audacious_bar, INFO_AUDACIOUS)
2791 scan_bar(arg, &obj->a, &obj->b);
2795 END OBJ(bmpx_title, INFO_BMPX)
2796 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2797 END OBJ(bmpx_artist, INFO_BMPX)
2798 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2799 END OBJ(bmpx_album, INFO_BMPX)
2800 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2801 END OBJ(bmpx_track, INFO_BMPX)
2802 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2803 END OBJ(bmpx_uri, INFO_BMPX)
2804 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2805 END OBJ(bmpx_bitrate, INFO_BMPX)
2806 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2812 char *userid = (char *) malloc(20 * sizeof(char));
2813 char *apikey = (char *) malloc(64 * sizeof(char));
2814 char *charid = (char *) malloc(20 * sizeof(char));
2816 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2817 obj->data.eve.charid = charid;
2818 obj->data.eve.userid = userid;
2819 obj->data.eve.apikey = apikey;
2823 CRIT_ERR(obj, free_at_crash, "eve needs arguments: <userid> <apikey> <characterid>");
2831 char *uri = (char *) malloc(128 * sizeof(char));
2833 argc = sscanf(arg, "%127s %f", uri, &interval);
2834 obj->data.curl.uri = uri;
2835 obj->data.curl.interval = interval > 0 ? interval * 60 : 15*60;
2837 CRIT_ERR(obj, free_at_crash, "curl needs arguments: <uri> <interval in minutes>");
2845 unsigned int nrspaces = 0;
2846 char *uri = (char *) malloc(128 * sizeof(char));
2847 char *action = (char *) malloc(64 * sizeof(char));
2849 argc = sscanf(arg, "%127s %f %63s %d %u", uri, &interval, action,
2850 &act_par, &nrspaces);
2851 obj->data.rss.uri = uri;
2852 obj->data.rss.interval = interval > 0 ? interval * 60 : 15*60;
2853 obj->data.rss.action = action;
2854 obj->data.rss.act_par = act_par;
2855 obj->data.rss.nrspaces = nrspaces;
2857 CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <interval in minutes> <action> "
2858 "[act_par] [spaces in front]");
2865 char *locID = (char *) malloc(9 * sizeof(char));
2866 char *uri = (char *) malloc(128 * sizeof(char));
2867 char *data_type = (char *) malloc(32 * sizeof(char));
2870 argc = sscanf(arg, "%119s %8s %31s %d", uri, locID, data_type, &interval);
2872 /* locID MUST BE upper-case */
2875 *tmp_p = toupper(*tmp_p);
2879 /* Construct complete uri */
2880 if (strstr(uri, "xoap.weather.com")) {
2888 } else if (strstr(uri, "weather.noaa.gov")) {
2890 strcat(uri, ".TXT");
2891 } else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
2892 CRIT_ERR(obj, free_at_crash, \
2893 "could not recognize the weather uri");
2896 obj->data.weather.uri = uri;
2897 obj->data.weather.data_type = data_type;
2899 /* Limit the data retrieval interval to half hour min */
2900 if (interval < 30) {
2904 /* Convert to seconds */
2905 obj->data.weather.interval = interval * 60;
2908 DBGP("weather: fetching %s from %s every %d seconds", \
2909 data_type, uri, obj->data.weather.interval);
2911 CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
2917 obj->data.s = strndup(arg, text_buffer_size);
2919 CRIT_ERR(obj, free_at_crash, "lua needs arguments: <function name> [function parameters]");
2921 END OBJ(lua_parse, 0)
2923 obj->data.s = strndup(arg, text_buffer_size);
2925 CRIT_ERR(obj, free_at_crash, "lua_parse needs arguments: <function name> [function parameters]");
2930 arg = scan_bar(arg, &obj->a, &obj->b);
2932 obj->data.s = strndup(arg, text_buffer_size);
2934 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2937 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2940 END OBJ(lua_graph, 0)
2941 SIZE_DEFAULTS(graph);
2944 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2945 &obj->e, &obj->char_a, &obj->char_b);
2949 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2952 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2954 END OBJ(lua_gauge, 0)
2955 SIZE_DEFAULTS(gauge);
2957 arg = scan_gauge(arg, &obj->a, &obj->b);
2959 obj->data.s = strndup(arg, text_buffer_size);
2961 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2964 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2967 #endif /* HAVE_LUA */
2970 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2971 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2972 ERR("hddtemp needs arguments");
2973 obj->type = OBJ_text;
2974 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2975 obj->data.hddtemp.update_time = 0;
2977 obj->data.hddtemp.temp = NULL;
2978 #endif /* HDDTEMP */
2979 #ifdef TCP_PORT_MONITOR
2980 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2981 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2982 #endif /* TCP_PORT_MONITOR */
2983 END OBJ(entropy_avail, INFO_ENTROPY)
2984 END OBJ(entropy_perc, INFO_ENTROPY)
2985 END OBJ(entropy_poolsize, INFO_ENTROPY)
2986 END OBJ(entropy_bar, INFO_ENTROPY)
2988 scan_bar(arg, &obj->a, &obj->b);
2991 obj->sub = malloc(sizeof(struct text_object));
2992 extract_variable_text_internal(obj->sub, arg);
2994 CRIT_ERR(obj, free_at_crash, "blink needs a argument");
2996 END OBJ(to_bytes, 0)
2998 obj->sub = malloc(sizeof(struct text_object));
2999 extract_variable_text_internal(obj->sub, arg);
3001 CRIT_ERR(obj, free_at_crash, "to_bytes needs a argument");
3006 obj->data.scroll.resetcolor = current_text_color;
3007 obj->data.scroll.step = 1;
3008 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
3009 sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
3010 if (*(arg + n1 + n2)) {
3013 obj->data.scroll.step = 1;
3015 obj->data.scroll.text = malloc(strlen(arg + n1) + obj->data.scroll.show + 1);
3016 for(n2 = 0; (unsigned int) n2 < obj->data.scroll.show; n2++) {
3017 obj->data.scroll.text[n2] = ' ';
3019 obj->data.scroll.text[n2] = 0;
3020 strcat(obj->data.scroll.text, arg + n1);
3021 obj->data.scroll.start = 0;
3022 obj->sub = malloc(sizeof(struct text_object));
3023 extract_variable_text_internal(obj->sub,
3024 obj->data.scroll.text);
3026 CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
3031 unsigned int indenting = 0; //vars can be used as args for other vars
3034 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
3036 for (i=0; arg[i] != 0 && j < 2; i++) {
3037 if(startvar[j] == -1) {
3041 }else if(endvar[j] == -1) {
3044 }else if(arg[i] == '}') {
3047 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
3053 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
3054 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
3055 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
3056 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
3058 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
3059 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
3061 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
3062 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
3064 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
3065 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
3067 obj->sub = malloc(sizeof(struct text_object));
3068 extract_variable_text_internal(obj->sub, obj->data.combine.left);
3069 obj->sub->sub = malloc(sizeof(struct text_object));
3070 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right);
3072 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3075 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3080 CRIT_ERR(obj, free_at_crash, "nvidia needs an argument\n");
3081 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3082 CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
3083 " specified: '%s'\n", arg);
3088 END OBJ(apcupsd, INFO_APCUPSD)
3092 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3093 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3095 info.apcupsd.port = htons(port);
3096 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3099 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3101 END OBJ(apcupsd_name, INFO_APCUPSD)
3102 END OBJ(apcupsd_model, INFO_APCUPSD)
3103 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3104 END OBJ(apcupsd_cable, INFO_APCUPSD)
3105 END OBJ(apcupsd_status, INFO_APCUPSD)
3106 END OBJ(apcupsd_linev, INFO_APCUPSD)
3107 END OBJ(apcupsd_load, INFO_APCUPSD)
3108 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3110 scan_bar(arg, &obj->a, &obj->b);
3112 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3114 SIZE_DEFAULTS(graph);
3115 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3116 &obj->e, &obj->char_a, &obj->char_b);
3118 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3119 SIZE_DEFAULTS(gauge);
3120 scan_gauge(arg, &obj->a, &obj->b);
3122 END OBJ(apcupsd_charge, INFO_APCUPSD)
3123 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3124 END OBJ(apcupsd_temp, INFO_APCUPSD)
3125 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3126 #endif /* APCUPSD */
3130 ERR("unknown variable %s", s);
3131 obj->type = OBJ_text;
3132 snprintf(buf, 256, "${%s}", s);
3133 obj->data.s = strndup(buf, text_buffer_size);
3140 static struct text_object *create_plain_text(const char *s)
3142 struct text_object *obj;
3144 if (s == NULL || *s == '\0') {
3148 obj = new_text_object_internal();
3150 obj->type = OBJ_text;
3151 obj->data.s = strndup(s, text_buffer_size);
3155 /* backslash_escape - do the actual substitution task for template objects
3157 * The field templates is used for substituting the \N occurences. Set it to
3158 * NULL to leave them as they are.
3160 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3164 unsigned int dup_idx = 0, dup_len;
3166 dup_len = strlen(src) + 1;
3167 src_dup = malloc(dup_len * sizeof(char));
3175 if (*(p + 1) == '\\') {
3176 src_dup[dup_idx++] = '\\';
3178 } else if (*(p + 1) == ' ') {
3179 src_dup[dup_idx++] = ' ';
3181 } else if (*(p + 1) == 'n') {
3182 src_dup[dup_idx++] = '\n';
3184 } else if (templates) {
3185 unsigned int tmpl_num;
3187 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3188 (tmpl_num > template_count))
3190 dup_len += strlen(templates[tmpl_num - 1]);
3191 src_dup = realloc(src_dup, dup_len * sizeof(char));
3192 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3193 dup_idx += strlen(templates[tmpl_num - 1]);
3198 src_dup[dup_idx++] = *p;
3203 src_dup[dup_idx] = '\0';
3204 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3208 /* handle_template_object - core logic of the template object
3210 * use config variables like this:
3211 * template1 = "$\1\2"
3212 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3214 * and use them like this:
3215 * ${template1 node name}
3216 * ${template2 root /}
3217 * ${template2 cdrom /mnt/cdrom}
3219 static char *handle_template(const char *tmpl, const char *args)
3221 char *args_dup = NULL;
3223 char **argsp = NULL;
3224 unsigned int argcnt = 0, template_idx, i;
3227 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3228 (template_idx >= MAX_TEMPLATES))
3232 args_dup = strdup(args);
3235 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3237 if (p > args_dup && *(p - 1) == '\\')
3240 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3246 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3247 argsp[argcnt - 1] = p_old;
3249 for (i = 0; i < argcnt; i++) {
3251 tmp = backslash_escape(argsp[i], NULL, 0);
3252 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3257 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3258 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3260 for (i = 0; i < argcnt; i++)
3266 static char *find_and_replace_templates(const char *inbuf)
3268 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3271 outlen = strlen(inbuf) + 1;
3272 o = outbuf = calloc(outlen, sizeof(char));
3273 memset(outbuf, 0, outlen * sizeof(char));
3275 p = indup = strdup(inbuf);
3277 while (*p && *p != '$')
3283 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3288 if (*(p + 1) == '{') {
3291 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3299 while (*p && stack > 0) {
3307 // stack is empty. that means the previous char was }, so we zero it
3310 // we ran into the end of string without finding a closing }, bark
3311 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
3315 while (*p && !isspace(*p))
3319 tmpl_out = handle_template(templ, args);
3321 outlen += strlen(tmpl_out);
3323 outbuf = realloc(outbuf, outlen * sizeof(char));
3324 strcat (outbuf, tmpl_out);
3326 o = outbuf + strlen(outbuf);
3328 ERR("failed to handle template '%s' with args '%s'", templ, args);
3332 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3337 static int text_contains_templates(const char *text)
3339 if (strcasestr(text, "${template") != NULL)
3341 if (strcasestr(text, "$template") != NULL)
3346 /* folds a string over top of itself, like so:
3348 * if start is "blah", and you call it with count = 1, the result will be "lah"
3350 static void strfold(char *start, int count)
3353 for (curplace = start + count; *curplace != 0; curplace++) {
3354 *(curplace - count) = *curplace;
3356 *(curplace - count) = 0;
3360 * - assumes that *string is '#'
3361 * - removes the part from '#' to the end of line ('\n' or '\0')
3362 * - it removes the '\n'
3363 * - copies the last char into 'char *last' argument, which should be a pointer
3364 * to a char rather than a string.
3366 static size_t remove_comment(char *string, char *last)
3369 while (*end != '\0' && *end != '\n') {
3372 if (last) *last = *end;
3373 if (*end == '\n') end++;
3374 strfold(string, end - string);
3375 return end - string;
3378 static size_t remove_comments(char *string)
3382 for (curplace = string; *curplace != 0; curplace++) {
3383 if (*curplace == '\\' && *(curplace + 1) == '#') {
3384 // strcpy can't be used for overlapping strings
3385 strfold(curplace, 1);
3387 } else if (*curplace == '#') {
3388 folded += remove_comment(curplace, 0);
3394 static int extract_variable_text_internal(struct text_object *retval, const char *const_p)
3396 struct text_object *obj;
3397 char *p, *s, *orig_p;
3399 void *ifblock_opaque = NULL;
3404 p = strndup(const_p, max_user_text - 1);
3405 while (text_contains_templates(p)) {
3407 tmp = find_and_replace_templates(p);
3413 if (strcmp(p, const_p)) {
3414 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3416 DBGP("no templates to replace");
3419 memset(retval, 0, sizeof(struct text_object));
3421 line = global_text_lines;
3429 obj = create_plain_text(s);
3431 append_object(retval, obj);
3441 /* variable is either $foo or ${foo} */
3443 unsigned int brl = 1, brr = 0;
3447 while (*p && brl != brr) {
3462 while (*p && (isalnum((int) *p) || *p == '_')) {
3467 /* copy variable to buffer */
3468 len = (p - s > 255) ? 255 : (p - s);
3469 strncpy(buf, s, len);
3477 /* search for variable in environment */
3481 obj = create_plain_text(var);
3483 append_object(retval, obj);
3488 /* if variable wasn't found in environment, use some special */
3493 if (strchr(buf, ' ')) {
3494 arg = strchr(buf, ' ');
3497 while (isspace((int) *arg)) {
3505 /* lowercase variable name */
3508 *tmp_p = tolower(*tmp_p);
3512 obj = construct_text_object(buf, arg,
3513 line, &ifblock_opaque, orig_p);
3515 append_object(retval, obj);
3519 obj = create_plain_text("$");
3522 append_object(retval, obj);
3525 } else if (*p == '\\' && *(p+1) == '#') {
3527 } else if (*p == '#') {
3529 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3530 /* if remove_comment removed a newline, we need to 'back up' with p */
3536 obj = create_plain_text(s);
3538 append_object(retval, obj);
3541 if (!ifblock_stack_empty(&ifblock_opaque)) {
3542 ERR("one or more $endif's are missing");
3549 static void extract_variable_text(const char *p)
3551 free_text_objects(&global_root_object, 0);
3565 extract_variable_text_internal(&global_root_object, p);
3568 void parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3570 extract_variable_text_internal(root, txt);
3571 generate_text_internal(p, max_user_text, *root, cur);
3575 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3576 void *thread(void *), const char *text)
3578 if (obj->char_b && info.mail) {
3579 // this means we use info
3580 if (!info.mail->p_timed_thread) {
3581 info.mail->p_timed_thread =
3582 timed_thread_create(thread,
3583 (void *) info.mail, info.mail->interval * 1000000);
3584 if (!info.mail->p_timed_thread) {
3585 ERR("Error creating %s timed thread", text);
3587 timed_thread_register(info.mail->p_timed_thread,
3588 &info.mail->p_timed_thread);
3589 if (timed_thread_run(info.mail->p_timed_thread)) {
3590 ERR("Error running %s timed thread", text);
3594 } else if (obj->data.mail) {
3595 // this means we use obj
3596 if (!obj->data.mail->p_timed_thread) {
3597 obj->data.mail->p_timed_thread =
3598 timed_thread_create(thread,
3599 (void *) obj->data.mail,
3600 obj->data.mail->interval * 1000000);
3601 if (!obj->data.mail->p_timed_thread) {
3602 ERR("Error creating %s timed thread", text);
3604 timed_thread_register(obj->data.mail->p_timed_thread,
3605 &obj->data.mail->p_timed_thread);
3606 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3607 ERR("Error running %s timed thread", text);
3610 return obj->data.mail;
3611 } else if (!obj->a) {
3612 // something is wrong, warn once then stop
3613 ERR("There's a problem with your mail settings. "
3614 "Check that the global mail settings are properly defined"
3615 " (line %li).", obj->line);
3621 char *format_time(unsigned long timeval, const int width)
3624 unsigned long nt; // narrow time, for speed on 32-bit
3625 unsigned cc; // centiseconds
3626 unsigned nn; // multi-purpose whatever
3629 cc = nt % 100; // centiseconds past second
3630 nt /= 100; // total seconds
3631 nn = nt % 60; // seconds past the minute
3632 nt /= 60; // total minutes
3633 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3635 return strndup(buf, text_buffer_size);
3637 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3638 return strndup(buf, text_buffer_size);
3640 nn = nt % 60; // minutes past the hour
3641 nt /= 60; // total hours
3642 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3643 return strndup(buf, text_buffer_size);
3645 nn = nt; // now also hours
3646 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3647 return strndup(buf, text_buffer_size);
3649 nn /= 24; // now days
3650 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3651 return strndup(buf, text_buffer_size);
3653 nn /= 7; // now weeks
3654 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3655 return strndup(buf, text_buffer_size);
3657 // well shoot, this outta' fit...
3658 return strndup("<inf>", text_buffer_size);
3661 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3662 //string has to end with \0 and it's length should fit in a int
3664 void remove_deleted_chars(char *string){
3666 while(string[i] != 0){
3667 if(string[i] == BACKSPACE){
3669 strcpy( &(string[i-1]), &(string[i+1]) );
3671 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3676 static inline void format_media_player_time(char *buf, const int size,
3679 int days, hours, minutes;
3681 days = seconds / (24 * 60 * 60);
3682 seconds %= (24 * 60 * 60);
3683 hours = seconds / (60 * 60);
3684 seconds %= (60 * 60);
3685 minutes = seconds / 60;
3689 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3690 hours, minutes, seconds);
3691 } else if (hours > 0) {
3692 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3695 snprintf(buf, size, "%i:%02i", minutes, seconds);
3699 static inline double get_barnum(char *buf)
3711 if (sscanf(buf, "%lf", &barnum) == 0) {
3712 ERR("reading exec value failed (perhaps it's not the "
3713 "correct format?)");
3716 if (barnum > 100.0 || barnum < 0.0) {
3717 ERR("your exec value is not between 0 and 100, "
3718 "therefore it will be ignored");
3724 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
3725 * multiline objects like $exec work with $align[rc] and friends
3727 void substitute_newlines(char *p, long l)
3731 while (p && *p && p < s + l) {
3733 /* only substitute if it's not the last newline */
3734 *p = SECRIT_MULTILINE_CHAR;
3740 static void generate_text_internal(char *p, int p_max_size,
3741 struct text_object root, struct information *cur)
3743 struct text_object *obj;
3745 int need_to_load_fonts = 0;
3748 /* for the OBJ_top* handler */
3749 struct process **needed = 0;
3752 char buff_in[p_max_size];
3754 iconv_converting = 0;
3755 #endif /* HAVE_ICONV */
3759 while (obj && p_max_size > 0) {
3760 needed = 0; /* reset for top stuff */
3762 /* IFBLOCK jumping algorithm
3764 * This is easier as it looks like:
3765 * - each IF checks it's condition
3766 * - on FALSE: call DO_JUMP
3767 * - on TRUE: don't care
3768 * - each ELSE calls DO_JUMP unconditionally
3769 * - each ENDIF is silently being ignored
3772 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3773 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3774 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3775 * regularly, "obj" is being updated to point to obj->next, so object parsing
3776 * continues right after the corresponding ELSE or ENDIF. This means that if we
3777 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3778 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3779 * jumped, and there is nothing to do.
3783 obj = obj->data.ifblock.next; \
3786 #define OBJ(a) break; case OBJ_##a:
3788 switch (obj->type) {
3790 ERR("not implemented obj type %d", obj->type);
3793 struct sockaddr_in addr;
3794 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3796 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3798 memset(&addr, 0, sizeof(addr));
3799 addr.sin_family = AF_INET;
3800 addr.sin_port = obj->data.read_tcp.port;
3801 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3802 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3806 FD_SET(sock, &readfds);
3809 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3810 received = recv(sock, p, p_max_size, 0);
3815 ERR("read_tcp: Couldn't create a connection");
3818 ERR("read_tcp: Couldn't create a socket");
3821 ERR("read_tcp: Problem with resolving the hostname");
3826 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3828 #endif /* !__OpenBSD__ */
3831 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3832 obj->data.cpu_index);
3838 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3839 obj->data.cpu_index);
3841 /* OpenBSD has no such flag (SUSv2) */
3842 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3843 obj->data.cpu_index);
3844 #endif /* __OpenBSD */
3847 #if defined(__linux__)
3850 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3851 obj->data.cpu_index);
3856 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3857 obj->data.cpu_index);
3862 OBJ(wireless_essid) {
3863 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3865 OBJ(wireless_mode) {
3866 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3868 OBJ(wireless_bitrate) {
3869 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3872 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3874 OBJ(wireless_link_qual) {
3875 spaced_print(p, p_max_size, "%d", 4,
3876 obj->data.net->link_qual);
3878 OBJ(wireless_link_qual_max) {
3879 spaced_print(p, p_max_size, "%d", 4,
3880 obj->data.net->link_qual_max);
3882 OBJ(wireless_link_qual_perc) {
3883 if (obj->data.net->link_qual_max > 0) {
3884 spaced_print(p, p_max_size, "%.0f", 5,
3885 (double) obj->data.net->link_qual /
3886 obj->data.net->link_qual_max * 100);
3888 spaced_print(p, p_max_size, "unk", 5);
3891 OBJ(wireless_link_bar) {
3893 if(output_methods & TO_X) {
3894 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3895 obj->data.net->link_qual_max) * 255.0);
3898 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3899 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3900 obj->data.net->link_qual_max) * 100.0, obj->a);
3905 #endif /* HAVE_IWLIB */
3907 #endif /* __linux__ */
3911 get_adt746x_cpu(p, p_max_size);
3914 get_adt746x_fan(p, p_max_size);
3917 get_acpi_fan(p, p_max_size);
3919 OBJ(acpiacadapter) {
3920 get_acpi_ac_adapter(p, p_max_size);
3923 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3926 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3928 OBJ(battery_percent) {
3929 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3933 if(output_methods & TO_X) {
3934 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3937 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3938 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3943 OBJ(battery_short) {
3944 get_battery_short_status(p, p_max_size, obj->data.s);
3946 #endif /* __OpenBSD__ */
3949 human_readable(cur->buffers * 1024, p, 255);
3952 human_readable(cur->cached * 1024, p, 255);
3955 if (obj->data.cpu_index > info.cpu_count) {
3956 ERR("obj->data.cpu_index %i info.cpu_count %i",
3957 obj->data.cpu_index, info.cpu_count);
3958 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
3960 percent_print(p, p_max_size,
3961 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3965 new_gauge(p, obj->a, obj->b,
3966 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3970 if(output_methods & TO_X) {
3971 new_bar(p, obj->a, obj->b,
3972 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3975 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3976 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3983 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3984 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3985 100, 1, obj->char_a, obj->char_b);
3988 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3989 obj->e, 1, obj->char_a, obj->char_b);
3992 new_fg(p, obj->data.l);
4025 OBJ(conky_version) {
4026 snprintf(p, p_max_size, "%s", VERSION);
4028 OBJ(conky_build_date) {
4029 snprintf(p, p_max_size, "%s", BUILD_DATE);
4031 OBJ(conky_build_arch) {
4032 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4034 #if defined(__linux__)
4036 snprintf(p, p_max_size, "%s",
4037 get_disk_protect_queue(obj->data.s));
4040 snprintf(p, p_max_size, "%s", i8k.version);
4043 snprintf(p, p_max_size, "%s", i8k.bios);
4046 snprintf(p, p_max_size, "%s", i8k.serial);
4051 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4052 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
4054 OBJ(i8k_left_fan_status) {
4055 int left_fan_status;
4057 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4058 if (left_fan_status == 0) {
4059 snprintf(p, p_max_size, "off");
4061 if (left_fan_status == 1) {
4062 snprintf(p, p_max_size, "low");
4064 if (left_fan_status == 2) {
4065 snprintf(p, p_max_size, "high");
4068 OBJ(i8k_right_fan_status) {
4069 int right_fan_status;
4071 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4072 if (right_fan_status == 0) {
4073 snprintf(p, p_max_size, "off");
4075 if (right_fan_status == 1) {
4076 snprintf(p, p_max_size, "low");
4078 if (right_fan_status == 2) {
4079 snprintf(p, p_max_size, "high");
4082 OBJ(i8k_left_fan_rpm) {
4083 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4085 OBJ(i8k_right_fan_rpm) {
4086 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4088 OBJ(i8k_ac_status) {
4091 sscanf(i8k.ac_status, "%d", &ac_status);
4092 if (ac_status == -1) {
4093 snprintf(p, p_max_size, "disabled (read i8k docs)");
4095 if (ac_status == 0) {
4096 snprintf(p, p_max_size, "off");
4098 if (ac_status == 1) {
4099 snprintf(p, p_max_size, "on");
4102 OBJ(i8k_buttons_status) {
4103 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4107 get_ibm_acpi_fan(p, p_max_size);
4110 get_ibm_acpi_temps();
4111 temp_print(p, p_max_size,
4112 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4115 get_ibm_acpi_volume(p, p_max_size);
4117 OBJ(ibm_brightness) {
4118 get_ibm_acpi_brightness(p, p_max_size);
4121 /* information from sony_laptop kernel module
4122 * /sys/devices/platform/sony-laptop */
4123 OBJ(sony_fanspeed) {
4124 get_sony_fanspeed(p, p_max_size);
4127 if (!cur->gw_info.count) {
4132 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4135 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4138 snprintf(p, p_max_size, "%d", get_laptop_mode());
4141 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4143 #endif /* __linux__ */
4144 #if (defined(__FreeBSD__) || defined(__linux__))
4146 if ((obj->data.ifblock.s)
4147 && (!interface_up(obj->data.ifblock.s))) {
4153 OBJ(obsd_sensors_temp) {
4154 obsd_sensors.device = sensor_device;
4155 update_obsd_sensors();
4156 temp_print(p, p_max_size,
4157 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4160 OBJ(obsd_sensors_fan) {
4161 obsd_sensors.device = sensor_device;
4162 update_obsd_sensors();
4163 snprintf(p, p_max_size, "%d",
4164 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4166 OBJ(obsd_sensors_volt) {
4167 obsd_sensors.device = sensor_device;
4168 update_obsd_sensors();
4169 snprintf(p, p_max_size, "%.2f",
4170 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4173 get_obsd_vendor(p, p_max_size);
4176 get_obsd_product(p, p_max_size);
4178 #endif /* __OpenBSD__ */
4181 new_font(p, obj->data.s);
4182 need_to_load_fonts = 1;
4185 /* TODO: move this correction from kB to kB/s elsewhere
4186 * (or get rid of it??) */
4188 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4192 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4196 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4201 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4202 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4204 OBJ(diskiograph_read) {
4205 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4206 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4208 OBJ(diskiograph_write) {
4209 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4210 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4214 human_readable(obj->data.net->recv_speed, p, 255);
4217 spaced_print(p, p_max_size, "%.1f", 8,
4218 obj->data.net->recv_speed / 1024.0);
4221 OBJ(downspeedgraph) {
4222 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4223 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4227 /* Since we see you, you're if has not jumped.
4228 * Do Ninja jump here: without leaving traces.
4229 * This is to prevent us from stale jumped flags.
4231 obj = obj->data.ifblock.next;
4235 /* harmless object, just ignore */
4239 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4240 && (obj->data.net->addr.sa_data[3] & 255) == 0
4241 && (obj->data.net->addr.sa_data[4] & 255) == 0
4242 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4243 snprintf(p, p_max_size, "No Address");
4245 snprintf(p, p_max_size, "%u.%u.%u.%u",
4246 obj->data.net->addr.sa_data[2] & 255,
4247 obj->data.net->addr.sa_data[3] & 255,
4248 obj->data.net->addr.sa_data[4] & 255,
4249 obj->data.net->addr.sa_data[5] & 255);
4252 #if defined(__linux__)
4254 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
4255 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4256 strcpy(p, obj->data.net->addrs);
4258 strcpy(p, "0.0.0.0");
4261 #endif /* __linux__ */
4262 #if defined(IMLIB2) && defined(X11)
4264 /* doesn't actually draw anything, just queues it omp. the
4265 * image will get drawn after the X event loop */
4266 cimlib_add_image(obj->data.s);
4270 evaluate(obj->data.s, p);
4273 read_exec(obj->data.s, p, text_buffer_size);
4274 remove_deleted_chars(p);
4277 struct information *tmp_info;
4278 struct text_object subroot;
4280 read_exec(obj->data.s, p, text_buffer_size);
4282 tmp_info = malloc(sizeof(struct information));
4283 memcpy(tmp_info, cur, sizeof(struct information));
4284 parse_conky_vars(&subroot, p, p, tmp_info);
4286 free_text_objects(&subroot, 1);
4293 read_exec(obj->data.s, p, text_buffer_size);
4294 barnum = get_barnum(p); /*using the same function*/
4296 if (barnum >= 0.0) {
4298 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4305 read_exec(obj->data.s, p, text_buffer_size);
4306 barnum = get_barnum(p);
4308 if (barnum >= 0.0) {
4310 if(output_methods & TO_X) {
4312 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4315 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4316 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4324 char showaslog = FALSE;
4325 char tempgrad = FALSE;
4327 char *cmd = obj->data.s;
4329 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4331 cmd += strlen(" "TEMPGRAD);
4333 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4335 cmd += strlen(" "LOGGRAPH);
4337 read_exec(cmd, p, text_buffer_size);
4338 barnum = get_barnum(p);
4341 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4342 100, 1, showaslog, tempgrad);
4347 if (current_update_time - obj->data.execi.last_update
4348 >= obj->data.execi.interval) {
4351 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4352 barnum = get_barnum(p);
4354 if (barnum >= 0.0) {
4357 obj->data.execi.last_update = current_update_time;
4360 if(output_methods & TO_X) {
4361 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4364 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4365 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4372 if (current_update_time - obj->data.execi.last_update
4373 >= obj->data.execi.interval) {
4375 char showaslog = FALSE;
4376 char tempgrad = FALSE;
4377 char *cmd = obj->data.execi.cmd;
4379 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4381 cmd += strlen(" "TEMPGRAD);
4383 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4385 cmd += strlen(" "LOGGRAPH);
4387 obj->char_a = showaslog;
4388 obj->char_b = tempgrad;
4389 read_exec(cmd, p, text_buffer_size);
4390 barnum = get_barnum(p);
4392 if (barnum >= 0.0) {
4395 obj->data.execi.last_update = current_update_time;
4397 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4400 if (current_update_time - obj->data.execi.last_update
4401 >= obj->data.execi.interval) {
4404 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4405 barnum = get_barnum(p);
4407 if (barnum >= 0.0) {
4408 obj->f = 255 * barnum / 100.0;
4410 obj->data.execi.last_update = current_update_time;
4412 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4416 if (current_update_time - obj->data.execi.last_update
4417 >= obj->data.execi.interval
4418 && obj->data.execi.interval != 0) {
4419 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4421 obj->data.execi.last_update = current_update_time;
4423 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4426 struct text_object subroot;
4427 struct information *tmp_info =
4428 malloc(sizeof(struct information));
4429 memcpy(tmp_info, cur, sizeof(struct information));
4431 if (current_update_time - obj->data.execi.last_update
4432 < obj->data.execi.interval
4433 || obj->data.execi.interval == 0) {
4434 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4436 char *output = obj->data.execi.buffer;
4437 FILE *fp = popen(obj->data.execi.cmd, "r");
4438 int length = fread(output, 1, text_buffer_size, fp);
4442 output[length] = '\0';
4443 if (length > 0 && output[length - 1] == '\n') {
4444 output[length - 1] = '\0';
4447 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4448 obj->data.execi.last_update = current_update_time;
4450 free_text_objects(&subroot, 1);
4454 if (!obj->data.texeci.p_timed_thread) {
4455 obj->data.texeci.p_timed_thread =
4456 timed_thread_create(&threaded_exec,
4457 (void *) obj, obj->data.texeci.interval * 1000000);
4458 if (!obj->data.texeci.p_timed_thread) {
4459 ERR("Error creating texeci timed thread");
4462 * note that we don't register this thread with the
4463 * timed_thread list, because we destroy it manually
4465 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4466 ERR("Error running texeci timed thread");
4469 timed_thread_lock(obj->data.texeci.p_timed_thread);
4470 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4471 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4474 #endif /* HAVE_POPEN */
4476 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4478 if (mail && mail->p_timed_thread) {
4479 timed_thread_lock(mail->p_timed_thread);
4480 snprintf(p, p_max_size, "%lu", mail->unseen);
4481 timed_thread_unlock(mail->p_timed_thread);
4484 OBJ(imap_messages) {
4485 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4487 if (mail && mail->p_timed_thread) {
4488 timed_thread_lock(mail->p_timed_thread);
4489 snprintf(p, p_max_size, "%lu", mail->messages);
4490 timed_thread_unlock(mail->p_timed_thread);
4494 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4496 if (mail && mail->p_timed_thread) {
4497 timed_thread_lock(mail->p_timed_thread);
4498 snprintf(p, p_max_size, "%lu", mail->unseen);
4499 timed_thread_unlock(mail->p_timed_thread);
4503 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4505 if (mail && mail->p_timed_thread) {
4506 timed_thread_lock(mail->p_timed_thread);
4507 snprintf(p, p_max_size, "%.1f",
4508 mail->used / 1024.0 / 1024.0);
4509 timed_thread_unlock(mail->p_timed_thread);
4513 if (obj->data.fs != NULL) {
4514 if (obj->data.fs->size == 0) {
4516 if(output_methods & TO_X) {
4517 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4520 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4521 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4527 if(output_methods & TO_X) {
4528 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4529 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4530 obj->data.fs->size));
4533 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4534 new_bar_in_shell(p, p_max_size,
4535 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4543 if (obj->data.fs != NULL) {
4544 human_readable(obj->data.fs->avail, p, 255);
4548 if (obj->data.fs != NULL) {
4551 if (obj->data.fs->size) {
4552 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4555 percent_print(p, p_max_size, val);
4559 if (obj->data.fs != NULL) {
4560 human_readable(obj->data.fs->size, p, 255);
4564 if (obj->data.fs != NULL)
4565 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4568 if (obj->data.fs != NULL) {
4569 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4574 if (obj->data.fs != NULL) {
4575 if (obj->data.fs->size == 0) {
4577 if(output_methods & TO_X) {
4578 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4581 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4582 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4588 if(output_methods & TO_X) {
4589 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4590 (int) (obj->data.fsbar.fs->avail * 255 /
4591 obj->data.fs->size));
4594 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4595 new_bar_in_shell(p, p_max_size,
4596 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4604 if (obj->data.fs != NULL) {
4607 if (obj->data.fs->size) {
4608 val = obj->data.fs->free
4613 percent_print(p, p_max_size, 100 - val);
4617 float *v = info.loadavg;
4619 if (obj->data.loadavg[2]) {
4620 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4621 v[obj->data.loadavg[0] - 1],
4622 v[obj->data.loadavg[1] - 1],
4623 v[obj->data.loadavg[2] - 1]);
4624 } else if (obj->data.loadavg[1]) {
4625 snprintf(p, p_max_size, "%.2f %.2f",
4626 v[obj->data.loadavg[0] - 1],
4627 v[obj->data.loadavg[1] - 1]);
4628 } else if (obj->data.loadavg[0]) {
4629 snprintf(p, p_max_size, "%.2f",
4630 v[obj->data.loadavg[0] - 1]);
4634 new_goto(p, obj->data.i);
4637 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4641 new_hr(p, obj->data.i);
4645 if (cur->nameserver_info.nscount > obj->data.i)
4646 snprintf(p, p_max_size, "%s",
4647 cur->nameserver_info.ns_list[obj->data.i]);
4651 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4652 snprintf(p, p_max_size, "%s", skill);
4657 if (obj->data.curl.uri != NULL) {
4658 ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
4660 ERR("error processing Curl data");
4666 if (obj->data.rss.uri != NULL) {
4667 rss_process_info(p, p_max_size, obj->data.rss.uri, obj->data.rss.action, obj->data.rss.act_par, obj->data.rss.interval, obj->data.rss.nrspaces);
4669 ERR("error processing RSS data");
4675 if (obj->data.weather.uri != NULL) {
4676 weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4678 ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
4684 char *str = llua_getstring(obj->data.s);
4686 snprintf(p, p_max_size, "%s", str);
4691 char *str = llua_getstring(obj->data.s);
4698 if (llua_getnumber(obj->data.s, &per)) {
4700 if(output_methods & TO_X) {
4701 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4704 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4705 new_bar_in_shell(p, p_max_size, per, obj->a);
4714 if (llua_getnumber(obj->data.s, &per)) {
4715 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4716 per, obj->e, 1, obj->char_a, obj->char_b);
4721 if (llua_getnumber(obj->data.s, &per)) {
4722 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4726 #endif /* HAVE_LUA */
4731 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4732 if (obj->data.hddtemp.temp)
4733 free(obj->data.hddtemp.temp);
4734 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4735 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4736 obj->data.hddtemp.update_time = current_update_time;
4738 if (!obj->data.hddtemp.temp) {
4739 snprintf(p, p_max_size, "N/A");
4741 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4742 unit = obj->data.hddtemp.temp[0];
4744 if (*endptr != '\0')
4745 snprintf(p, p_max_size, "N/A");
4746 else if (unit == 'C')
4747 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4748 else if (unit == 'F')
4749 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4751 snprintf(p, p_max_size, "N/A");
4756 new_offset(p, obj->data.i);
4759 new_voffset(p, obj->data.i);
4765 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4766 obj->data.sysfs.devtype, obj->data.sysfs.type);
4768 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4770 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4771 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4772 } else if (r >= 100.0 || r == 0) {
4773 snprintf(p, p_max_size, "%d", (int) r);
4775 snprintf(p, p_max_size, "%.1f", r);
4781 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4782 obj->data.sysfs.devtype, obj->data.sysfs.type);
4784 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4786 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4787 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4788 } else if (r >= 100.0 || r == 0) {
4789 snprintf(p, p_max_size, "%d", (int) r);
4791 snprintf(p, p_max_size, "%.1f", r);
4797 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4798 obj->data.sysfs.devtype, obj->data.sysfs.type);
4800 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4802 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4803 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4804 } else if (r >= 100.0 || r == 0) {
4805 snprintf(p, p_max_size, "%d", (int) r);
4807 snprintf(p, p_max_size, "%.1f", r);
4810 #endif /* __linux__ */
4812 new_alignr(p, obj->data.i);
4815 new_alignc(p, obj->data.i);
4818 char buf[max_user_text];
4819 struct information *tmp_info =
4820 malloc(sizeof(struct information));
4821 memcpy(tmp_info, cur, sizeof(struct information));
4822 generate_text_internal(buf, max_user_text,
4823 *obj->sub, tmp_info);
4825 if (strlen(buf) != 0) {
4831 char expression[max_user_text];
4833 struct information *tmp_info;
4835 tmp_info = malloc(sizeof(struct information));
4836 memcpy(tmp_info, cur, sizeof(struct information));
4837 generate_text_internal(expression, max_user_text,
4838 *obj->sub, tmp_info);
4839 DBGP("parsed arg into '%s'", expression);
4841 val = compare(expression);
4843 ERR("compare failed for expression '%s'",
4851 if (obj->data.ifblock.str
4852 && !check_contains(obj->data.ifblock.s,
4853 obj->data.ifblock.str)) {
4855 } else if (obj->data.ifblock.s
4856 && access(obj->data.ifblock.s, F_OK)) {
4861 if ((obj->data.ifblock.s)
4862 && (!check_mount(obj->data.ifblock.s))) {
4868 if (!get_process_by_name(obj->data.ifblock.s)) {
4870 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4875 #if defined(__linux__)
4877 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4881 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4884 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4889 human_readable(cur->mem * 1024, p, 255);
4892 human_readable(cur->memeasyfree * 1024, p, 255);
4895 human_readable(cur->memfree * 1024, p, 255);
4898 human_readable(cur->memmax * 1024, p, 255);
4902 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4906 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4907 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4912 if(output_methods & TO_X) {
4913 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4914 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4917 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4918 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4925 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4926 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4927 100, 1, obj->char_a, obj->char_b);
4932 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4935 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4938 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4942 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4943 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4946 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4947 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4950 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4951 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4954 OBJ(if_mixer_mute) {
4955 if (!mixer_is_mute(obj->data.ifblock.i)) {
4960 #define NOT_IN_X "Not running in X"
4962 if(x_initialised != YES) {
4963 strncpy(p, NOT_IN_X, p_max_size);
4965 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4968 OBJ(monitor_number) {
4969 if(x_initialised != YES) {
4970 strncpy(p, NOT_IN_X, p_max_size);
4972 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4976 if(x_initialised != YES) {
4977 strncpy(p, NOT_IN_X, p_max_size);
4979 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
4982 OBJ(desktop_number) {
4983 if(x_initialised != YES) {
4984 strncpy(p, NOT_IN_X, p_max_size);
4986 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
4990 if(x_initialised != YES) {
4991 strncpy(p, NOT_IN_X, p_max_size);
4992 }else if(cur->x11.desktop.name != NULL) {
4993 strncpy(p, cur->x11.desktop.name, p_max_size);
5000 update_mail_count(&obj->data.local_mail);
5001 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5004 update_mail_count(&obj->data.local_mail);
5005 snprintf(p, p_max_size, "%d",
5006 obj->data.local_mail.new_mail_count);
5009 update_mail_count(&obj->data.local_mail);
5010 snprintf(p, p_max_size, "%d",
5011 obj->data.local_mail.seen_mail_count);
5014 update_mail_count(&obj->data.local_mail);
5015 snprintf(p, p_max_size, "%d",
5016 obj->data.local_mail.unseen_mail_count);
5018 OBJ(flagged_mails) {
5019 update_mail_count(&obj->data.local_mail);
5020 snprintf(p, p_max_size, "%d",
5021 obj->data.local_mail.flagged_mail_count);
5023 OBJ(unflagged_mails) {
5024 update_mail_count(&obj->data.local_mail);
5025 snprintf(p, p_max_size, "%d",
5026 obj->data.local_mail.unflagged_mail_count);
5028 OBJ(forwarded_mails) {
5029 update_mail_count(&obj->data.local_mail);
5030 snprintf(p, p_max_size, "%d",
5031 obj->data.local_mail.forwarded_mail_count);
5033 OBJ(unforwarded_mails) {
5034 update_mail_count(&obj->data.local_mail);
5035 snprintf(p, p_max_size, "%d",
5036 obj->data.local_mail.unforwarded_mail_count);
5038 OBJ(replied_mails) {
5039 update_mail_count(&obj->data.local_mail);
5040 snprintf(p, p_max_size, "%d",
5041 obj->data.local_mail.replied_mail_count);
5043 OBJ(unreplied_mails) {
5044 update_mail_count(&obj->data.local_mail);
5045 snprintf(p, p_max_size, "%d",
5046 obj->data.local_mail.unreplied_mail_count);
5049 update_mail_count(&obj->data.local_mail);
5050 snprintf(p, p_max_size, "%d",
5051 obj->data.local_mail.draft_mail_count);
5053 OBJ(trashed_mails) {
5054 update_mail_count(&obj->data.local_mail);
5055 snprintf(p, p_max_size, "%d",
5056 obj->data.local_mail.trashed_mail_count);
5059 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5061 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5064 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5067 new_outline(p, obj->data.l);
5070 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5072 OBJ(running_processes) {
5073 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5076 snprintf(p, p_max_size, "%s", obj->data.s);
5080 new_bg(p, obj->data.l);
5083 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5087 human_readable(cur->swap * 1024, p, 255);
5090 human_readable(cur->swapfree * 1024, p, 255);
5093 human_readable(cur->swapmax * 1024, p, 255);
5096 if (cur->swapmax == 0) {
5097 strncpy(p, "No swap", p_max_size);
5099 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5104 if(output_methods & TO_X) {
5105 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5106 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5109 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5110 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5116 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5119 time_t t = time(NULL);
5120 struct tm *tm = localtime(&t);
5122 setlocale(LC_TIME, "");
5123 strftime(p, p_max_size, obj->data.s, tm);
5126 time_t t = time(NULL);
5127 struct tm *tm = gmtime(&t);
5129 strftime(p, p_max_size, obj->data.s, tm);
5136 if (obj->data.tztime.tz) {
5137 oldTZ = getenv("TZ");
5138 setenv("TZ", obj->data.tztime.tz, 1);
5144 setlocale(LC_TIME, "");
5145 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5147 setenv("TZ", oldTZ, 1);
5152 // Needless to free oldTZ since getenv gives ptr to static data
5155 human_readable(obj->data.net->recv, p, 255);
5158 human_readable(obj->data.net->trans, p, 255);
5161 snprintf(p, p_max_size, "%d", total_updates);
5164 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5169 human_readable(obj->data.net->trans_speed, p, 255);
5172 spaced_print(p, p_max_size, "%.1f", 8,
5173 obj->data.net->trans_speed / 1024.0);
5177 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5178 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5182 format_seconds_short(p, p_max_size, (int) cur->uptime);
5185 format_seconds(p, p_max_size, (int) cur->uptime);
5188 snprintf(p, p_max_size, "%s", cur->users.names);
5191 snprintf(p, p_max_size, "%s", cur->users.terms);
5194 snprintf(p, p_max_size, "%s", cur->users.times);
5197 snprintf(p, p_max_size, "%d", cur->users.number);
5199 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5200 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5204 msg = get_apm_adapter();
5205 snprintf(p, p_max_size, "%s", msg);
5208 OBJ(apm_battery_life) {
5211 msg = get_apm_battery_life();
5212 snprintf(p, p_max_size, "%s", msg);
5215 OBJ(apm_battery_time) {
5218 msg = get_apm_battery_time();
5219 snprintf(p, p_max_size, "%s", msg);
5222 #endif /* __FreeBSD__ __OpenBSD__ */
5225 #define mpd_printf(fmt, val) \
5226 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5227 #define mpd_sprintf(val) { \
5228 if (!obj->data.i || obj->data.i > p_max_size) \
5229 mpd_printf("%s", val); \
5231 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5236 mpd_sprintf(artist);
5240 mpd_printf("%s", random);
5242 mpd_printf("%s", repeat);
5250 mpd_printf("%d", volume);
5252 mpd_printf("%d", bitrate);
5254 mpd_printf("%s", status);
5256 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5259 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5262 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5266 if(output_methods & TO_X) {
5267 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5268 (int) (mpd_get_info()->progress * 255.0f));
5271 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5272 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5278 struct mpd_s *mpd = mpd_get_info();
5279 int len = obj->data.i;
5280 if (len == 0 || len > p_max_size)
5283 memset(p, 0, p_max_size);
5284 if (mpd->artist && *mpd->artist &&
5285 mpd->title && *mpd->title) {
5286 snprintf(p, len, "%s - %s", mpd->artist,
5288 } else if (mpd->title && *mpd->title) {
5289 snprintf(p, len, "%s", mpd->title);
5290 } else if (mpd->artist && *mpd->artist) {
5291 snprintf(p, len, "%s", mpd->artist);
5292 } else if (mpd->file && *mpd->file) {
5293 snprintf(p, len, "%s", mpd->file);
5298 OBJ(if_mpd_playing) {
5299 if (!mpd_get_info()->is_playing) {
5308 #define MOC_PRINT(t, a) \
5309 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5311 MOC_PRINT(state, "??");
5314 MOC_PRINT(file, "no file");
5317 MOC_PRINT(title, "no title");
5320 MOC_PRINT(artist, "no artist");
5323 MOC_PRINT(song, "no song");
5326 MOC_PRINT(album, "no album");
5328 OBJ(moc_totaltime) {
5329 MOC_PRINT(totaltime, "0:00");
5332 MOC_PRINT(timeleft, "0:00");
5335 MOC_PRINT(curtime, "0:00");
5338 MOC_PRINT(bitrate, "0Kbps");
5341 MOC_PRINT(rate, "0KHz");
5347 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5350 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5353 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5356 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5358 OBJ(xmms2_comment) {
5359 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5362 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5365 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5368 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5370 OBJ(xmms2_tracknr) {
5371 if (cur->xmms2.tracknr != -1) {
5372 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5375 OBJ(xmms2_bitrate) {
5376 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5379 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5382 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5384 OBJ(xmms2_elapsed) {
5385 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5386 (cur->xmms2.elapsed / 1000) % 60);
5388 OBJ(xmms2_duration) {
5389 snprintf(p, p_max_size, "%02d:%02d",
5390 cur->xmms2.duration / 60000,
5391 (cur->xmms2.duration / 1000) % 60);
5393 OBJ(xmms2_percent) {
5394 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5398 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5399 (int) (cur->xmms2.progress * 255.0f));
5402 OBJ(xmms2_playlist) {
5403 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5405 OBJ(xmms2_timesplayed) {
5406 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5409 if (strlen(cur->xmms2.title) < 2
5410 && strlen(cur->xmms2.title) < 2) {
5411 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5413 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5417 OBJ(if_xmms2_connected) {
5418 if (cur->xmms2.conn_state != 1) {
5424 OBJ(audacious_status) {
5425 snprintf(p, p_max_size, "%s",
5426 cur->audacious.items[AUDACIOUS_STATUS]);
5428 OBJ(audacious_title) {
5429 snprintf(p, cur->audacious.max_title_len > 0
5430 ? cur->audacious.max_title_len : p_max_size, "%s",
5431 cur->audacious.items[AUDACIOUS_TITLE]);
5433 OBJ(audacious_length) {
5434 snprintf(p, p_max_size, "%s",
5435 cur->audacious.items[AUDACIOUS_LENGTH]);
5437 OBJ(audacious_length_seconds) {
5438 snprintf(p, p_max_size, "%s",
5439 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5441 OBJ(audacious_position) {
5442 snprintf(p, p_max_size, "%s",
5443 cur->audacious.items[AUDACIOUS_POSITION]);
5445 OBJ(audacious_position_seconds) {
5446 snprintf(p, p_max_size, "%s",
5447 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5449 OBJ(audacious_bitrate) {
5450 snprintf(p, p_max_size, "%s",
5451 cur->audacious.items[AUDACIOUS_BITRATE]);
5453 OBJ(audacious_frequency) {
5454 snprintf(p, p_max_size, "%s",
5455 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5457 OBJ(audacious_channels) {
5458 snprintf(p, p_max_size, "%s",
5459 cur->audacious.items[AUDACIOUS_CHANNELS]);
5461 OBJ(audacious_filename) {
5462 snprintf(p, p_max_size, "%s",
5463 cur->audacious.items[AUDACIOUS_FILENAME]);
5465 OBJ(audacious_playlist_length) {
5466 snprintf(p, p_max_size, "%s",
5467 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5469 OBJ(audacious_playlist_position) {
5470 snprintf(p, p_max_size, "%s",
5471 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5473 OBJ(audacious_main_volume) {
5474 snprintf(p, p_max_size, "%s",
5475 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5478 OBJ(audacious_bar) {
5482 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5483 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5484 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5487 #endif /* AUDACIOUS */
5491 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5494 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5497 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5500 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5503 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5506 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5509 /* we have four different types of top (top, top_mem,
5510 * top_time and top_io). To avoid having almost-same code four
5511 * times, we have this special handler. */
5514 parse_top_args("top", obj->data.top.s, obj);
5515 if (!needed) needed = cur->cpu;
5517 parse_top_args("top_mem", obj->data.top.s, obj);
5518 if (!needed) needed = cur->memu;
5520 parse_top_args("top_time", obj->data.top.s, obj);
5521 if (!needed) needed = cur->time;
5524 parse_top_args("top_io", obj->data.top.s, obj);
5525 if (!needed) needed = cur->io;
5528 if (needed[obj->data.top.num]) {
5531 switch (obj->data.top.type) {
5533 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5534 needed[obj->data.top.num]->name);
5537 snprintf(p, 7, "%6.2f",
5538 needed[obj->data.top.num]->amount);
5541 snprintf(p, 6, "%5i",
5542 needed[obj->data.top.num]->pid);
5545 snprintf(p, 7, "%6.2f",
5546 needed[obj->data.top.num]->totalmem);
5549 timeval = format_time(
5550 needed[obj->data.top.num]->total_cpu_time, 9);
5551 snprintf(p, 10, "%9s", timeval);
5555 human_readable(needed[obj->data.top.num]->rss,
5559 human_readable(needed[obj->data.top.num]->vsize,
5563 case TOP_READ_BYTES:
5564 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5567 case TOP_WRITE_BYTES:
5568 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5572 snprintf(p, 7, "%6.2f",
5573 needed[obj->data.top.num]->io_perc);
5579 print_tail_object(obj, p, p_max_size);
5581 print_head_object(obj, p, p_max_size);
5583 FILE *fp = open_file(obj->data.s, &obj->a);
5586 /* FIXME: use something more general (see also tail.c, head.c */
5587 #define BUFSZ 0x1000
5592 while(fgets(buf, BUFSZ, fp) != NULL){
5593 for(j = 0; buf[j] != 0; j++) {
5594 if(buf[j] == '\n') {
5599 sprintf(p, "%d", lines);
5602 sprintf(p, "File Unreadable");
5607 FILE *fp = open_file(obj->data.s, &obj->a);
5612 char inword = FALSE;
5615 while(fgets(buf, BUFSZ, fp) != NULL){
5616 for(j = 0; buf[j] != 0; j++) {
5617 if(!isspace(buf[j])) {
5618 if(inword == FALSE) {
5627 sprintf(p, "%d", words);
5630 sprintf(p, "File Unreadable");
5633 #ifdef TCP_PORT_MONITOR
5635 tcp_portmon_action(p, p_max_size,
5636 &obj->data.tcp_port_monitor);
5638 #endif /* TCP_PORT_MONITOR */
5642 iconv_converting = 1;
5643 iconv_selected = obj->a;
5646 iconv_converting = 0;
5649 #endif /* HAVE_ICONV */
5651 OBJ(entropy_avail) {
5652 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5655 percent_print(p, p_max_size,
5656 cur->entropy.entropy_avail *
5657 100 / cur->entropy.poolsize);
5659 OBJ(entropy_poolsize) {
5660 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5663 double entropy_perc;
5665 entropy_perc = (double) cur->entropy.entropy_avail /
5666 (double) cur->entropy.poolsize;
5668 if(output_methods & TO_X) {
5669 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5672 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5673 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5682 s = smapi_get_val(obj->data.s);
5683 snprintf(p, p_max_size, "%s", s);
5687 OBJ(if_smapi_bat_installed) {
5689 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5690 if(!smapi_bat_installed(idx)) {
5694 ERR("argument to if_smapi_bat_installed must be an integer");
5696 OBJ(smapi_bat_perc) {
5698 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5699 val = smapi_bat_installed(idx) ?
5700 smapi_get_bat_int(idx, "remaining_percent") : 0;
5701 percent_print(p, p_max_size, val);
5703 ERR("argument to smapi_bat_perc must be an integer");
5705 OBJ(smapi_bat_temp) {
5707 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5708 val = smapi_bat_installed(idx) ?
5709 smapi_get_bat_int(idx, "temperature") : 0;
5710 /* temperature is in milli degree celsius */
5711 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5713 ERR("argument to smapi_bat_temp must be an integer");
5715 OBJ(smapi_bat_power) {
5717 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5718 val = smapi_bat_installed(idx) ?
5719 smapi_get_bat_int(idx, "power_now") : 0;
5720 /* power_now is in mW, set to W with one digit precision */
5721 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5723 ERR("argument to smapi_bat_power must be an integer");
5726 OBJ(smapi_bat_bar) {
5727 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5728 new_bar(p, obj->a, obj->b, (int)
5729 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5731 new_bar(p, obj->a, obj->b, 0);
5736 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5737 char buf[max_user_text];
5740 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5741 snprintf(p, p_max_size, "%s", buf);
5742 if(total_updates % 2) {
5743 for(j=0; p[j] != 0; j++) {
5749 char buf[max_user_text];
5751 char unit[16]; // 16 because we can also have long names (like mega-bytes)
5753 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5754 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5755 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5756 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5757 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5758 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5759 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5761 snprintf(p, p_max_size, "%s", buf);
5764 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
5766 char buf[max_user_text];
5767 generate_text_internal(buf, max_user_text,
5769 for(j = 0; buf[j] != 0; j++) {
5771 case '\n': //place all the lines behind each other with LINESEPARATOR between them
5772 #define LINESEPARATOR '|'
5773 buf[j]=LINESEPARATOR;
5780 //no scrolling necessary if the length of the text to scroll is too short
5781 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
5782 snprintf(p, p_max_size, "%s", buf);
5785 //make sure a colorchange at the front is not part of the string we are going to show
5786 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
5787 obj->data.scroll.start++;
5789 //place all chars that should be visible in p, including colorchanges
5790 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
5791 p[j] = *(buf + obj->data.scroll.start + j);
5792 if(p[j] == SPECIAL_CHAR) {
5793 visibcolorchanges++;
5795 //if there is still room fill it with spaces
5798 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
5802 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
5803 for(j = 0; j < obj->data.scroll.start; j++) {
5804 if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
5806 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
5807 for(j = 0; j < frontcolorchanges; j++) {
5808 pwithcolors[j] = SPECIAL_CHAR;
5811 strcat(pwithcolors,p);
5812 strend = strlen(pwithcolors);
5813 //and place the colorchanges not in front or in the visible part behind the visible part
5814 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
5815 pwithcolors[strend + j] = SPECIAL_CHAR;
5817 pwithcolors[strend + j] = 0;
5818 strcpy(p, pwithcolors);
5821 obj->data.scroll.start += obj->data.scroll.step;
5822 if(buf[obj->data.scroll.start] == 0){
5823 obj->data.scroll.start = 0;
5825 //reset color when scroll is finished
5826 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
5829 char buf[2][max_user_text];
5836 struct llrows* next;
5838 struct llrows *ll_rows[2], *current[2];
5839 struct text_object * objsub = obj->sub;
5842 for(i=0; i<2; i++) {
5845 ll_rows[i] = malloc(sizeof(struct llrows));
5846 current[i] = ll_rows[i];
5847 for(j=0; j<i; j++) objsub = objsub->sub;
5848 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5849 for(j=0; buf[i][j] != 0; j++) {
5850 if(buf[i][j] == '\t') buf[i][j] = ' ';
5851 if(buf[i][j] == '\n') {
5853 current[i]->row = strdup(buf[i]+nextstart);
5854 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5855 current[i]->next = malloc(sizeof(struct llrows));
5856 current[i] = current[i]->next;
5861 current[i]->row = strdup(buf[i]+nextstart);
5862 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5863 current[i]->next = NULL;
5864 current[i] = ll_rows[i];
5866 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5868 strcat(p, current[0]->row);
5869 i=strlen(current[0]->row);
5871 while(i < longest) {
5876 strcat(p, obj->data.combine.seperation);
5877 strcat(p, current[1]->row);
5881 #pragma omp parallel for schedule(dynamic,10)
5882 #endif /* HAVE_OPENMP */
5883 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5886 #pragma omp parallel for schedule(dynamic,10)
5887 #endif /* HAVE_OPENMP */
5888 for(i=0; i<2; i++) {
5889 while(ll_rows[i] != NULL) {
5890 current[i]=ll_rows[i];
5891 free(current[i]->row);
5892 ll_rows[i]=current[i]->next;
5899 int value = get_nvidia_value(obj->data.nvidia.type, display);
5901 snprintf(p, p_max_size, "N/A");
5902 else if (obj->data.nvidia.type == NV_TEMP)
5903 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5904 else if (obj->data.nvidia.print_as_float &&
5905 value > 0 && value < 100)
5906 snprintf(p, p_max_size, "%.1f", (float)value);
5908 snprintf(p, p_max_size, "%d", value);
5913 /* This is just a meta-object to set host:port */
5916 snprintf(p, p_max_size, "%s",
5917 cur->apcupsd.items[APCUPSD_NAME]);
5919 OBJ(apcupsd_model) {
5920 snprintf(p, p_max_size, "%s",
5921 cur->apcupsd.items[APCUPSD_MODEL]);
5923 OBJ(apcupsd_upsmode) {
5924 snprintf(p, p_max_size, "%s",
5925 cur->apcupsd.items[APCUPSD_UPSMODE]);
5927 OBJ(apcupsd_cable) {
5928 snprintf(p, p_max_size, "%s",
5929 cur->apcupsd.items[APCUPSD_CABLE]);
5931 OBJ(apcupsd_status) {
5932 snprintf(p, p_max_size, "%s",
5933 cur->apcupsd.items[APCUPSD_STATUS]);
5935 OBJ(apcupsd_linev) {
5936 snprintf(p, p_max_size, "%s",
5937 cur->apcupsd.items[APCUPSD_LINEV]);
5940 snprintf(p, p_max_size, "%s",
5941 cur->apcupsd.items[APCUPSD_LOAD]);
5943 OBJ(apcupsd_loadbar) {
5946 if(output_methods & TO_X) {
5947 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5948 new_bar(p, obj->a, obj->b, (int) progress);
5951 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5952 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5953 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5959 OBJ(apcupsd_loadgraph) {
5961 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5962 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5963 (int)progress, 100, 1, obj->char_a, obj->char_b);
5965 OBJ(apcupsd_loadgauge) {
5967 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5968 new_gauge(p, obj->a, obj->b,
5972 OBJ(apcupsd_charge) {
5973 snprintf(p, p_max_size, "%s",
5974 cur->apcupsd.items[APCUPSD_CHARGE]);
5976 OBJ(apcupsd_timeleft) {
5977 snprintf(p, p_max_size, "%s",
5978 cur->apcupsd.items[APCUPSD_TIMELEFT]);
5981 snprintf(p, p_max_size, "%s",
5982 cur->apcupsd.items[APCUPSD_TEMP]);
5984 OBJ(apcupsd_lastxfer) {
5985 snprintf(p, p_max_size, "%s",
5986 cur->apcupsd.items[APCUPSD_LASTXFER]);
5988 #endif /* APCUPSD */
5995 unsigned int a = strlen(p);
5998 if (a > 0 && iconv_converting && iconv_selected > 0
5999 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6001 size_t dummy1, dummy2;
6003 const char *ptr = buff_in;
6005 char *ptr = buff_in;
6009 dummy1 = dummy2 = a;
6011 strncpy(buff_in, p, p_max_size);
6013 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6014 while (dummy1 > 0) {
6015 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6018 ERR("Iconv codeset conversion failed");
6023 /* It is nessecary when we are converting from multibyte to
6024 * singlebyte codepage */
6027 #endif /* HAVE_ICONV */
6028 if (obj->type != OBJ_text) {
6029 substitute_newlines(p, a - 2);
6037 /* load any new fonts we may have had */
6038 if (need_to_load_fonts) {
6044 void evaluate(char *text, char *buffer)
6046 struct information *tmp_info;
6047 struct text_object subroot;
6049 tmp_info = malloc(sizeof(struct information));
6050 memcpy(tmp_info, &info, sizeof(struct information));
6051 parse_conky_vars(&subroot, text, buffer, tmp_info);
6052 DBGP("evaluated '%s' to '%s'", text, buffer);
6054 free_text_objects(&subroot, 1);
6058 double current_update_time, next_update_time, last_update_time;
6060 static void generate_text(void)
6062 struct information *cur = &info;
6069 current_update_time = get_time();
6073 /* add things to the buffer */
6079 generate_text_internal(p, max_user_text, global_root_object, cur);
6081 if (stuff_in_uppercase) {
6084 tmp_p = text_buffer;
6086 *tmp_p = toupper(*tmp_p);
6091 next_update_time += update_interval;
6092 if (next_update_time < get_time()) {
6093 next_update_time = get_time() + update_interval;
6094 } else if (next_update_time > get_time() + update_interval) {
6095 next_update_time = get_time() + update_interval;
6097 last_update_time = current_update_time;
6101 static inline int get_string_width(const char *s)
6104 if (output_methods & TO_X) {
6105 return *s ? calc_text_width(s, strlen(s)) : 0;
6111 static int get_string_width_special(char *s, int special_index)
6119 if ((output_methods & TO_X) == 0) {
6121 return (s) ? strlen(s) : 0;
6129 p = strndup(s, text_buffer_size);
6133 if (*p == SPECIAL_CHAR) {
6134 /* shift everything over by 1 so that the special char
6135 * doesn't mess up the size calculation */
6136 for (i = 0; i < (long)strlen(p); i++) {
6137 *(p + i) = *(p + i + 1);
6139 if (specials[special_index + idx].type == GRAPH
6140 || specials[special_index + idx].type == GAUGE
6141 || specials[special_index + idx].type == BAR) {
6142 width += specials[special_index + idx].width;
6145 } else if (*p == SECRIT_MULTILINE_CHAR) {
6152 if (strlen(final) > 1) {
6153 width += calc_text_width(final, strlen(final));
6161 static int text_size_updater(char *s, int special_index);
6163 int last_font_height;
6164 static void update_text_area(void)
6168 if ((output_methods & TO_X) == 0)
6170 /* update text size if it isn't fixed */
6175 text_width = minimum_width;
6177 last_font_height = font_height();
6178 for_each_line(text_buffer, text_size_updater);
6180 if (text_height < minimum_height) {
6181 text_height = minimum_height;
6183 if (text_width > maximum_width && maximum_width > 0) {
6184 text_width = maximum_width;
6188 /* get text position on workarea */
6189 switch (text_alignment) {
6196 x = workarea[2] - text_width - gap_x;
6201 x = workarea[2] / 2 - text_width / 2 - gap_x;
6208 y = workarea[3] - text_height - gap_y;
6212 x = workarea[2] - text_width - gap_x;
6213 y = workarea[3] - text_height - gap_y;
6217 x = workarea[2] / 2 - text_width / 2 - gap_x;
6218 y = workarea[3] - text_height - gap_y;
6223 y = workarea[3] / 2 - text_height / 2 - gap_y;
6227 x = workarea[2] - text_width - gap_x;
6228 y = workarea[3] / 2 - text_height / 2 - gap_y;
6232 case NONE: // Let the WM manage the window
6243 if (own_window && !fixed_pos) {
6246 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6247 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6248 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6249 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6253 /* If window size doesn't match to workarea's size,
6254 * then window probably includes panels (gnome).
6255 * Blah, doesn't work on KDE. */
6256 if (workarea[2] != window.width || workarea[3] != window.height) {
6265 /* update lua window globals */
6266 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6267 #endif /* HAVE_LUA */
6272 static int cur_x, cur_y; /* current x and y for drawing */
6274 //draw_mode also without X11 because we only need to print to stdout with FG
6275 static int draw_mode; /* FG, BG or OUTLINE */
6277 static long current_color;
6279 static int text_size_updater(char *s, int special_index)
6284 if ((output_methods & TO_X) == 0)
6286 /* get string widths and skip specials */
6289 if (*p == SPECIAL_CHAR) {
6291 w += get_string_width(s);
6294 if (specials[special_index].type == BAR
6295 || specials[special_index].type == GAUGE
6296 || specials[special_index].type == GRAPH) {
6297 w += specials[special_index].width;
6298 if (specials[special_index].height > last_font_height) {
6299 last_font_height = specials[special_index].height;
6300 last_font_height += font_height();
6302 } else if (specials[special_index].type == OFFSET) {
6303 if (specials[special_index].arg > 0) {
6304 w += specials[special_index].arg;
6306 } else if (specials[special_index].type == VOFFSET) {
6307 last_font_height += specials[special_index].arg;
6308 } else if (specials[special_index].type == GOTO) {
6309 if (specials[special_index].arg > cur_x) {
6310 w = (int) specials[special_index].arg;
6312 } else if (specials[special_index].type == TAB) {
6313 int start = specials[special_index].arg;
6314 int step = specials[special_index].width;
6316 if (!step || step < 0) {
6319 w += step - (cur_x - text_start_x - start) % step;
6320 } else if (specials[special_index].type == FONT) {
6321 selected_font = specials[special_index].font_added;
6322 if (font_height() > last_font_height) {
6323 last_font_height = font_height();
6329 } else if (*p == SECRIT_MULTILINE_CHAR) {
6332 lw = get_string_width(s);
6333 *p = SECRIT_MULTILINE_CHAR;
6335 w = lw > w ? lw : w;
6336 text_height += last_font_height;
6340 w += get_string_width(s);
6341 if (w > text_width) {
6344 if (text_width > maximum_width && maximum_width) {
6345 text_width = maximum_width;
6348 text_height += last_font_height;
6349 last_font_height = font_height();
6350 return special_index;
6353 static inline void set_foreground_color(long c)
6355 if ((output_methods & TO_X) == 0)
6358 XSetForeground(display, window.gc, c);
6362 static void draw_string(const char *s)
6364 int i, i2, pos, width_of_s;
6372 width_of_s = get_string_width(s);
6373 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6375 fflush(stdout); /* output immediately, don't buffer */
6377 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6378 fprintf(stderr, "%s\n", s);
6379 fflush(stderr); /* output immediately, don't buffer */
6381 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6382 fprintf(overwrite_fpointer, "%s\n", s);
6384 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6385 fprintf(append_fpointer, "%s\n", s);
6387 memset(tmpstring1, 0, text_buffer_size);
6388 memset(tmpstring2, 0, text_buffer_size);
6389 strncpy(tmpstring1, s, text_buffer_size - 1);
6394 if (output_methods & TO_X) {
6395 max = ((text_width - width_of_s) / get_string_width(" "));
6398 /* This code looks for tabs in the text and coverts them to spaces.
6399 * The trick is getting the correct number of spaces, and not going
6400 * over the window's size without forcing the window larger. */
6401 for (i = 0; i < (int) text_buffer_size; i++) {
6402 if (tmpstring1[i] == '\t') {
6404 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6405 /* guard against overrun */
6406 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6411 /* guard against overrun */
6412 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6417 if (output_methods & TO_X) {
6418 if (text_width == maximum_width) {
6419 /* this means the text is probably pushing the limit,
6420 * so we'll chop it */
6421 while (cur_x + get_string_width(tmpstring2) - text_start_x
6422 > maximum_width && strlen(tmpstring2) > 0) {
6423 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6430 if (output_methods & TO_X) {
6436 c.pixel = current_color;
6437 XQueryColor(display, DefaultColormap(display, screen), &c);
6440 c2.color.red = c.red;
6441 c2.color.green = c.green;
6442 c2.color.blue = c.blue;
6443 c2.color.alpha = fonts[selected_font].font_alpha;
6445 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6446 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6448 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6449 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6454 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6457 cur_x += width_of_s;
6460 memcpy(tmpstring1, s, text_buffer_size);
6464 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
6466 int font_h = font_height();
6470 int last_special_needed = -1;
6471 int orig_special_index = special_index;
6473 cur_x = text_start_x;
6474 cur_y += font_ascent();
6477 if (*p == SECRIT_MULTILINE_CHAR) {
6478 /* special newline marker for multiline objects */
6483 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
6486 /* draw string before special, unless we're dealing multiline
6488 if (last_special_applied > -1) {
6489 special_index = last_special_applied;
6497 switch (specials[special_index].type) {
6498 case HORIZONTAL_LINE:
6500 int h = specials[special_index].height;
6501 int mid = font_ascent() / 2;
6503 w = text_start_x + text_width - cur_x;
6505 XSetLineAttributes(display, window.gc, h, LineSolid,
6506 CapButt, JoinMiter);
6507 XDrawLine(display, window.drawable, window.gc, cur_x,
6508 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6514 int h = specials[special_index].height;
6515 int tmp_s = specials[special_index].arg;
6516 int mid = font_ascent() / 2;
6517 char ss[2] = { tmp_s, tmp_s };
6519 w = text_start_x + text_width - cur_x - 1;
6520 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6521 CapButt, JoinMiter);
6522 XSetDashes(display, window.gc, 0, ss, 2);
6523 XDrawLine(display, window.drawable, window.gc, cur_x,
6524 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6530 int h, bar_usage, by;
6531 if (cur_x - text_start_x > maximum_width
6532 && maximum_width > 0) {
6535 h = specials[special_index].height;
6536 bar_usage = specials[special_index].arg;
6537 by = cur_y - (font_ascent() / 2) - 1;
6542 w = specials[special_index].width;
6544 w = text_start_x + text_width - cur_x - 1;
6550 XSetLineAttributes(display, window.gc, 1, LineSolid,
6551 CapButt, JoinMiter);
6553 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6555 XFillRectangle(display, window.drawable, window.gc, cur_x,
6556 by, w * bar_usage / 255, h);
6564 case GAUGE: /* new GAUGE */
6567 unsigned long last_colour = current_color;
6569 float angle, px, py;
6573 if (cur_x - text_start_x > maximum_width
6574 && maximum_width > 0) {
6578 h = specials[special_index].height;
6579 by = cur_y - (font_ascent() / 2) - 1;
6584 w = specials[special_index].width;
6586 w = text_start_x + text_width - cur_x - 1;
6592 XSetLineAttributes(display, window.gc, 1, LineSolid,
6593 CapButt, JoinMiter);
6595 XDrawArc(display, window.drawable, window.gc,
6596 cur_x, by, w, h * 2, 0, 180*64);
6599 usage = specials[special_index].arg;
6600 angle = (M_PI)*(float)(usage)/255.;
6601 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6602 py = (float)(by+(h))-(float)(h)*sin(angle);
6604 XDrawLine(display, window.drawable, window.gc,
6605 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6613 set_foreground_color(last_colour);
6621 int h, by, i = 0, j = 0;
6623 unsigned long last_colour = current_color;
6624 unsigned long *tmpcolour = 0;
6625 if (cur_x - text_start_x > maximum_width
6626 && maximum_width > 0) {
6629 h = specials[special_index].height;
6630 by = cur_y - (font_ascent() / 2) - 1;
6635 w = specials[special_index].width;
6637 w = text_start_x + text_width - cur_x - 1;
6642 if (draw_graph_borders) {
6643 XSetLineAttributes(display, window.gc, 1, LineSolid,
6644 CapButt, JoinMiter);
6645 XDrawRectangle(display, window.drawable, window.gc,
6648 XSetLineAttributes(display, window.gc, 1, LineSolid,
6649 CapButt, JoinMiter);
6651 if (specials[special_index].last_colour != 0
6652 || specials[special_index].first_colour != 0) {
6653 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6656 for (i = w - 2; i > -1; i--) {
6657 if (specials[special_index].last_colour != 0
6658 || specials[special_index].first_colour != 0) {
6659 if (specials[special_index].tempgrad) {
6662 (int)((float)(w - 2) - specials[special_index].graph[j] *
6663 (w - 2) / (float)specials[special_index].graph_scale)
6667 (int)((float)(w - 2) - specials[special_index].graph[j] *
6668 (w - 2) / (float)specials[special_index].graph_scale)
6671 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6673 (int)((float)(w - 2) - specials[special_index].graph[j] *
6674 (w - 2) / (float)specials[special_index].graph_scale)
6678 #endif /* DEBUG_lol */
6679 XSetForeground(display, window.gc, tmpcolour[
6680 (int)((float)(w - 2) - specials[special_index].graph[j] *
6681 (w - 2) / (float)specials[special_index].graph_scale)
6684 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6687 /* this is mugfugly, but it works */
6688 XDrawLine(display, window.drawable, window.gc,
6689 cur_x + i + 1, by + h, cur_x + i + 1,
6690 round_to_int((double)by + h - specials[special_index].graph[j] *
6691 (h - 1) / specials[special_index].graph_scale));
6692 if ((w - i) / ((float) (w - 2) /
6693 (specials[special_index].graph_width)) > j
6694 && j < MAX_GRAPH_DEPTH - 3) {
6698 if (tmpcolour) free(tmpcolour);
6703 /* if (draw_mode == BG) {
6704 set_foreground_color(default_bg_color);
6705 } else if (draw_mode == OUTLINE) {
6706 set_foreground_color(default_out_color);
6708 set_foreground_color(default_fg_color);
6710 if (show_graph_range) {
6713 unsigned short int seconds = update_interval * w;
6719 unsigned short int timeunits;
6721 timeunits = seconds / 86400; seconds %= 86400;
6722 if (timeunits > 0) {
6723 asprintf(&tmp_day_str, "%dd", timeunits);
6725 tmp_day_str = strdup("");
6727 timeunits = seconds / 3600; seconds %= 3600;
6728 if (timeunits > 0) {
6729 asprintf(&tmp_hour_str, "%dh", timeunits);
6731 tmp_hour_str = strdup("");
6733 timeunits = seconds / 60; seconds %= 60;
6734 if (timeunits > 0) {
6735 asprintf(&tmp_min_str, "%dm", timeunits);
6737 tmp_min_str = strdup("");
6740 asprintf(&tmp_sec_str, "%ds", seconds);
6742 tmp_sec_str = strdup("");
6744 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6745 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6747 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6749 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6750 cur_y += font_h / 2;
6751 draw_string(tmp_str);
6757 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6761 cur_x += font_ascent() / 2;
6762 cur_y += font_h / 2;
6764 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6766 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6767 draw_string(tmp_str);
6773 set_foreground_color(last_colour);
6779 int old = font_ascent();
6781 cur_y -= font_ascent();
6782 selected_font = specials[special_index].font_added;
6784 if (cur_y + font_ascent() < cur_y + old) {
6787 cur_y += font_ascent();
6789 font_h = font_height();
6793 if (draw_mode == FG) {
6794 set_foreground_color(specials[special_index].arg);
6799 if (draw_mode == BG) {
6800 set_foreground_color(specials[special_index].arg);
6805 if (draw_mode == OUTLINE) {
6806 set_foreground_color(specials[special_index].arg);
6811 w += specials[special_index].arg;
6812 last_special_needed = special_index;
6816 cur_y += specials[special_index].arg;
6820 if (specials[special_index].arg >= 0) {
6821 cur_x = (int) specials[special_index].arg;
6823 last_special_needed = special_index;
6828 int start = specials[special_index].arg;
6829 int step = specials[special_index].width;
6831 if (!step || step < 0) {
6834 w = step - (cur_x - text_start_x - start) % step;
6835 last_special_needed = special_index;
6841 /* TODO: add back in "+ window.border_inner_margin" to the end of
6843 int pos_x = text_start_x + text_width -
6844 get_string_width_special(s, special_index);
6846 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6847 "get_string_width(p) %i gap_x %i "
6848 "specials[special_index].arg %i window.border_inner_margin %i "
6849 "window.border_width %i\n", pos_x, text_start_x, text_width,
6850 cur_x, get_string_width_special(s), gap_x,
6851 specials[special_index].arg, window.border_inner_margin,
6852 window.border_width); */
6853 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6854 cur_x = pos_x - specials[special_index].arg;
6856 last_special_needed = special_index;
6862 int pos_x = (text_width) / 2 - get_string_width_special(s,
6863 special_index) / 2 - (cur_x -
6865 /* int pos_x = text_start_x + text_width / 2 -
6866 get_string_width_special(s) / 2; */
6868 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6869 "get_string_width(p) %i gap_x %i "
6870 "specials[special_index].arg %i\n", pos_x, text_start_x,
6871 text_width, cur_x, get_string_width(s), gap_x,
6872 specials[special_index].arg); */
6873 if (pos_x > specials[special_index].arg) {
6874 w = pos_x - specials[special_index].arg;
6876 last_special_needed = special_index;
6883 if (special_index != last_special_applied) {
6886 special_index = orig_special_index;
6887 last_special_applied = -1;
6895 cur_y += font_descent();
6896 if (recurse && *recurse) {
6897 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
6898 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
6900 return special_index;
6904 static int draw_line(char *s, int special_index)
6907 if ((output_methods & TO_X) == 0) {
6914 /* find specials and draw stuff */
6915 return draw_each_line_inner(s, special_index, -1);
6919 static void draw_text(void)
6923 llua_draw_pre_hook();
6924 #endif /* HAVE_LUA */
6925 if (output_methods & TO_X) {
6926 cur_y = text_start_y;
6929 if (draw_borders && window.border_width > 0) {
6930 if (stippled_borders) {
6931 char ss[2] = { stippled_borders, stippled_borders };
6932 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6933 CapButt, JoinMiter);
6934 XSetDashes(display, window.gc, 0, ss, 2);
6936 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6937 CapButt, JoinMiter);
6940 XDrawRectangle(display, window.drawable, window.gc,
6941 text_start_x - window.border_inner_margin - window.border_width,
6942 text_start_y - window.border_inner_margin - window.border_width,
6943 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6944 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6951 for_each_line(text_buffer, draw_line);
6952 #if defined(HAVE_LUA) && defined(X11)
6953 llua_draw_post_hook();
6954 #endif /* HAVE_LUA */
6957 static void draw_stuff(void)
6959 if (overwrite_file) {
6960 overwrite_fpointer = fopen(overwrite_file, "w");
6961 if(!overwrite_fpointer)
6962 ERR("Can't overwrite '%s' anymore", overwrite_file);
6965 append_fpointer = fopen(append_file, "a");
6966 if(!append_fpointer)
6967 ERR("Can't append '%s' anymore", append_file);
6970 if (output_methods & TO_X) {
6972 if (draw_shades && !draw_outline) {
6975 set_foreground_color(default_bg_color);
6986 for (i = -1; i < 2; i++) {
6987 for (j = -1; j < 2; j++) {
6988 if (i == 0 && j == 0) {
6993 set_foreground_color(default_out_color);
6994 draw_mode = OUTLINE;
7002 set_foreground_color(default_fg_color);
7008 if (output_methods & TO_X) {
7013 swap.swap_window = window.window;
7014 swap.swap_action = XdbeBackground;
7015 XdbeSwapBuffers(display, &swap, 1);
7020 if(overwrite_fpointer) {
7021 fclose(overwrite_fpointer);
7022 overwrite_fpointer = 0;
7024 if(append_fpointer) {
7025 fclose(append_fpointer);
7026 append_fpointer = 0;
7031 static void clear_text(int exposures)
7035 /* The swap action is XdbeBackground, which clears */
7039 if (display && window.window) { // make sure these are !null
7040 /* there is some extra space for borders and outlines */
7041 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
7042 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
7043 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7044 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
7049 static int need_to_update;
7051 /* update_text() generates new text and clears old text area */
7052 static void update_text(void)
7059 if (output_methods & TO_X)
7065 #ifdef HAVE_SYS_INOTIFY_H
7069 static void main_loop(void)
7072 #ifdef SIGNAL_BLOCKING
7073 sigset_t newmask, oldmask;
7076 #ifdef HAVE_SYS_INOTIFY_H
7077 int inotify_config_wd = -1;
7078 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
7079 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
7080 char inotify_buff[INOTIFY_BUF_LEN];
7081 #endif /* HAVE_SYS_INOTIFY_H */
7084 #ifdef SIGNAL_BLOCKING
7085 sigemptyset(&newmask);
7086 sigaddset(&newmask, SIGINT);
7087 sigaddset(&newmask, SIGTERM);
7088 sigaddset(&newmask, SIGUSR1);
7091 last_update_time = 0.0;
7092 next_update_time = get_time();
7094 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7097 #ifdef SIGNAL_BLOCKING
7098 /* block signals. we will inspect for pending signals later */
7099 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7100 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7105 if (output_methods & TO_X) {
7108 /* wait for X event or timeout */
7110 if (!XPending(display)) {
7114 t = next_update_time - get_time();
7118 } else if (t > update_interval) {
7119 t = update_interval;
7122 tv.tv_sec = (long) t;
7123 tv.tv_usec = (long) (t * 1000000) % 1000000;
7125 FD_SET(ConnectionNumber(display), &fdsr);
7127 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7129 if (errno != EINTR) {
7130 ERR("can't select(): %s", strerror(errno));
7140 if (need_to_update) {
7142 int wx = window.x, wy = window.y;
7152 /* resize window if it isn't right size */
7154 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7155 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7156 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7157 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7158 XResizeWindow(display, window.window, window.width,
7160 set_transparent_background(window.window);
7165 /* move window if it isn't in right position */
7166 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7167 XMoveWindow(display, window.window, window.x, window.y);
7172 if (changed && window.type == TYPE_PANEL) {
7175 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7178 switch (text_alignment) {
7205 set_struts(sidenum);
7216 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7217 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7218 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7219 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7220 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7225 /* handle X events */
7226 while (XPending(display)) {
7229 XNextEvent(display, &ev);
7236 r.width = ev.xexpose.width;
7237 r.height = ev.xexpose.height;
7238 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7242 case PropertyNotify:
7244 if ( ev.xproperty.state == PropertyNewValue ) {
7245 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7251 case ReparentNotify:
7252 /* set background to ParentRelative for all parents */
7254 set_transparent_background(window.window);
7258 case ConfigureNotify:
7260 /* if window size isn't what expected, set fixed size */
7261 if (ev.xconfigure.width != window.width
7262 || ev.xconfigure.height != window.height) {
7263 if (window.width != 0 && window.height != 0) {
7267 /* clear old stuff before screwing up
7272 XWindowAttributes attrs;
7273 if (XGetWindowAttributes(display,
7274 window.window, &attrs)) {
7275 window.width = attrs.width;
7276 window.height = attrs.height;
7280 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7281 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7282 if (text_width > maximum_width
7283 && maximum_width > 0) {
7284 text_width = maximum_width;
7288 /* if position isn't what expected, set fixed pos
7289 * total_updates avoids setting fixed_pos when window
7290 * is set to weird locations when started */
7291 /* // this is broken
7292 if (total_updates >= 2 && !fixed_pos
7293 && (window.x != ev.xconfigure.x
7294 || window.y != ev.xconfigure.y)
7295 && (ev.xconfigure.x != 0
7296 || ev.xconfigure.y != 0)) {
7304 /* if an ordinary window with decorations */
7305 if ((window.type == TYPE_NORMAL)
7306 && (!TEST_HINT(window.hints,
7307 HINT_UNDECORATED))) {
7308 /* allow conky to hold input focus. */
7311 /* forward the click to the desktop window */
7312 XUngrabPointer(display, ev.xbutton.time);
7313 ev.xbutton.window = window.desktop;
7314 ev.xbutton.x = ev.xbutton.x_root;
7315 ev.xbutton.y = ev.xbutton.y_root;
7316 XSendEvent(display, ev.xbutton.window, False,
7317 ButtonPressMask, &ev);
7318 XSetInputFocus(display, ev.xbutton.window,
7319 RevertToParent, ev.xbutton.time);
7326 /* if an ordinary window with decorations */
7327 if ((window.type == TYPE_NORMAL)
7328 && (!TEST_HINT(window.hints,
7329 HINT_UNDECORATED))) {
7330 /* allow conky to hold input focus. */
7333 /* forward the release to the desktop window */
7334 ev.xbutton.window = window.desktop;
7335 ev.xbutton.x = ev.xbutton.x_root;
7336 ev.xbutton.y = ev.xbutton.y_root;
7337 XSendEvent(display, ev.xbutton.window, False,
7338 ButtonReleaseMask, &ev);
7347 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7348 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7350 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7351 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7353 #endif /* HAVE_XDAMAGE */
7359 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7360 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7361 #endif /* HAVE_XDAMAGE */
7363 /* XDBE doesn't seem to provide a way to clear the back buffer without
7364 * interfering with the front buffer, other than passing XdbeBackground
7365 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7366 * redraw the text even if it wasn't part of the exposed area. OTOH,
7367 * if we're not going to call draw_stuff at all, then no swap happens
7368 * and we can safely do nothing. */
7370 if (!XEmptyRegion(x11_stuff.region)) {
7375 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7376 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7377 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7378 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7379 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7382 XSetRegion(display, window.gc, x11_stuff.region);
7385 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7389 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7392 XDestroyRegion(x11_stuff.region);
7393 x11_stuff.region = XCreateRegion();
7397 t = (next_update_time - get_time()) * 1000000;
7398 if(t > 0) usleep((useconds_t)t);
7405 #ifdef SIGNAL_BLOCKING
7406 /* unblock signals of interest and let handler fly */
7407 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7408 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7412 switch (g_signal_pending) {
7415 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7420 ERR("received SIGINT or SIGTERM to terminate. bye!");
7423 if (output_methods & TO_X) {
7424 XDestroyRegion(x11_stuff.region);
7425 x11_stuff.region = NULL;
7427 XDamageDestroy(display, x11_stuff.damage);
7428 XFixesDestroyRegion(display, x11_stuff.region2);
7429 XFixesDestroyRegion(display, x11_stuff.part);
7430 #endif /* HAVE_XDAMAGE */
7436 if(overwrite_file) {
7437 free(overwrite_file);
7446 /* Reaching here means someone set a signal
7447 * (SIGXXXX, signal_handler), but didn't write any code
7449 * If you don't want to handle a signal, don't set a handler on
7450 * it in the first place. */
7451 if (g_signal_pending) {
7452 ERR("ignoring signal (%d)", g_signal_pending);
7456 #ifdef HAVE_SYS_INOTIFY_H
7457 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7458 inotify_config_wd = inotify_add_watch(inotify_fd,
7462 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7463 int len = 0, idx = 0;
7465 struct timeval time_to_wait;
7467 FD_ZERO(&descriptors);
7468 FD_SET(inotify_fd, &descriptors);
7470 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7472 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7473 if (FD_ISSET(inotify_fd, &descriptors)) {
7474 /* process inotify events */
7475 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7476 while (len > 0 && idx < len) {
7477 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7478 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7479 /* current_config should be reloaded */
7480 ERR("'%s' modified, reloading...", current_config);
7482 if (ev->mask & IN_IGNORED) {
7483 /* for some reason we get IN_IGNORED here
7484 * sometimes, so we need to re-add the watch */
7485 inotify_config_wd = inotify_add_watch(inotify_fd,
7492 llua_inotify_query(ev->wd, ev->mask);
7494 #endif /* HAVE_LUA */
7495 idx += INOTIFY_EVENT_SIZE + ev->len;
7499 #endif /* HAVE_SYS_INOTIFY_H */
7501 g_signal_pending = 0;
7503 clean_up(NULL, NULL);
7505 #ifdef HAVE_SYS_INOTIFY_H
7506 if (inotify_fd != -1) {
7507 inotify_rm_watch(inotify_fd, inotify_config_wd);
7509 inotify_fd = inotify_config_wd = 0;
7511 #endif /* HAVE_SYS_INOTIFY_H */
7514 static void load_config_file(const char *);
7516 static void load_config_file_x11(const char *);
7518 void initialisation(int argc, char** argv);
7520 /* reload the config file */
7521 static void reload_config(void)
7523 char *current_config_copy = strdup(current_config);
7524 clean_up(NULL, NULL);
7525 current_config = current_config_copy;
7526 initialisation(argc_copy, argv_copy);
7529 void clean_up(void *memtofree1, void* memtofree2)
7538 timed_thread_destroy_registered_threads();
7540 if (info.cpu_usage) {
7541 free(info.cpu_usage);
7542 info.cpu_usage = NULL;
7545 if (x_initialised == YES) {
7548 if(x11_stuff.region) {
7549 XDestroyRegion(x11_stuff.region);
7550 x11_stuff.region = NULL;
7552 XClearWindow(display, RootWindow(display, screen));
7553 XCloseDisplay(display);
7555 if(info.x11.desktop.all_names) {
7556 free(info.x11.desktop.all_names);
7557 info.x11.desktop.all_names = NULL;
7559 if (info.x11.desktop.name) {
7560 free(info.x11.desktop.name);
7561 info.x11.desktop.name = NULL;
7565 free(fonts); //in set_default_configurations a font is set but not loaded
7571 for (i = 0; i < MAX_TEMPLATES; i++) {
7578 free_text_objects(&global_root_object, 0);
7597 free(current_config);
7600 #ifdef TCP_PORT_MONITOR
7601 tcp_portmon_clear();
7610 weather_free_info();
7614 #endif /* HAVE_LUA */
7617 for (i = 0; i < special_count; i++) {
7618 if (specials[i].type == GRAPH) {
7619 free(specials[i].graph);
7627 clear_diskio_stats();
7628 if(global_cpu != NULL) {
7634 static int string_to_bool(const char *s)
7637 // Assumes an option without a true/false means true
7639 } else if (strcasecmp(s, "yes") == EQUAL) {
7641 } else if (strcasecmp(s, "true") == EQUAL) {
7643 } else if (strcasecmp(s, "1") == EQUAL) {
7650 static enum alignment string_to_alignment(const char *s)
7652 if (strcasecmp(s, "top_left") == EQUAL) {
7654 } else if (strcasecmp(s, "top_right") == EQUAL) {
7656 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7658 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7660 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7661 return BOTTOM_RIGHT;
7662 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7663 return BOTTOM_MIDDLE;
7664 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7666 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7667 return MIDDLE_RIGHT;
7668 } else if (strcasecmp(s, "tl") == EQUAL) {
7670 } else if (strcasecmp(s, "tr") == EQUAL) {
7672 } else if (strcasecmp(s, "tm") == EQUAL) {
7674 } else if (strcasecmp(s, "bl") == EQUAL) {
7676 } else if (strcasecmp(s, "br") == EQUAL) {
7677 return BOTTOM_RIGHT;
7678 } else if (strcasecmp(s, "bm") == EQUAL) {
7679 return BOTTOM_MIDDLE;
7680 } else if (strcasecmp(s, "ml") == EQUAL) {
7682 } else if (strcasecmp(s, "mr") == EQUAL) {
7683 return MIDDLE_RIGHT;
7684 } else if (strcasecmp(s, "none") == EQUAL) {
7692 static void set_default_configurations_for_x(void)
7694 default_fg_color = WhitePixel(display, screen);
7695 default_bg_color = BlackPixel(display, screen);
7696 default_out_color = BlackPixel(display, screen);
7697 color0 = default_fg_color;
7698 color1 = default_fg_color;
7699 color2 = default_fg_color;
7700 color3 = default_fg_color;
7701 color4 = default_fg_color;
7702 color5 = default_fg_color;
7703 color6 = default_fg_color;
7704 color7 = default_fg_color;
7705 color8 = default_fg_color;
7706 color9 = default_fg_color;
7707 current_text_color = default_fg_color;
7711 static void set_default_configurations(void)
7719 fork_to_background = 0;
7720 total_run_times = 0;
7721 info.cpu_avg_samples = 2;
7722 info.net_avg_samples = 2;
7723 info.diskio_avg_samples = 2;
7728 format_human_readable = 1;
7735 mpd_env_host = getenv("MPD_HOST");
7736 mpd_env_port = getenv("MPD_PORT");
7738 if (!mpd_env_host || !strlen(mpd_env_host)) {
7739 mpd_set_host("localhost");
7741 /* MPD_HOST environment variable is set */
7742 char *mpd_hostpart = strchr(mpd_env_host, '@');
7743 if (!mpd_hostpart) {
7744 mpd_set_host(mpd_env_host);
7746 /* MPD_HOST contains a password */
7747 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7748 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7750 if (!strlen(mpd_hostpart + 1)) {
7751 mpd_set_host("localhost");
7753 mpd_set_host(mpd_hostpart + 1);
7756 mpd_set_password(mpd_password, 1);
7761 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7762 /* failed to set port from environment variable */
7763 mpd_set_port("6600");
7767 info.xmms2.artist = NULL;
7768 info.xmms2.album = NULL;
7769 info.xmms2.title = NULL;
7770 info.xmms2.genre = NULL;
7771 info.xmms2.comment = NULL;
7772 info.xmms2.url = NULL;
7773 info.xmms2.status = NULL;
7774 info.xmms2.playlist = NULL;
7776 use_spacer = NO_SPACER;
7778 output_methods = TO_X;
7780 output_methods = TO_STDOUT;
7783 show_graph_scale = 0;
7784 show_graph_range = 0;
7787 draw_graph_borders = 1;
7789 set_first_font("6x10");
7797 window.type = TYPE_NORMAL;
7799 strcpy(window.class_name, PACKAGE_NAME);
7800 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7802 stippled_borders = 0;
7803 window.border_inner_margin = 3;
7804 window.border_outer_margin = 1;
7805 window.border_width = 1;
7806 text_alignment = BOTTOM_LEFT;
7807 info.x11.monitor.number = 1;
7808 info.x11.monitor.current = 0;
7809 info.x11.desktop.current = 1;
7810 info.x11.desktop.number = 1;
7811 info.x11.desktop.nitems = 0;
7812 info.x11.desktop.all_names = NULL;
7813 info.x11.desktop.name = NULL;
7816 for (i = 0; i < MAX_TEMPLATES; i++) {
7819 template[i] = strdup("");
7822 free(current_mail_spool);
7826 variable_substitute(MAIL_FILE, buf, 256);
7827 if (buf[0] != '\0') {
7828 current_mail_spool = strndup(buf, text_buffer_size);
7833 update_interval = 3.0;
7834 info.music_player_interval = 1.0;
7835 stuff_in_uppercase = 0;
7836 info.users.number = 1;
7838 #ifdef TCP_PORT_MONITOR
7839 /* set default connection limit */
7840 tcp_portmon_set_max_connections(0);
7844 /* returns 1 if you can overwrite or create the file at 'path' */
7845 static _Bool overwrite_works(const char *path)
7849 if (!(filepointer = fopen(path, "w")))
7851 fclose(filepointer);
7855 /* returns 1 if you can append or create the file at 'path' */
7856 static _Bool append_works(const char *path)
7860 if (!(filepointer = fopen(path, "a")))
7862 fclose(filepointer);
7868 /* WARNING, this type not in Xlib spec */
7869 int x11_error_handler(Display *d, XErrorEvent *err)
7870 __attribute__((noreturn));
7871 int x11_error_handler(Display *d, XErrorEvent *err)
7873 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7875 (long unsigned)err->display,
7876 (long)err->resourceid,
7886 int x11_ioerror_handler(Display *d)
7887 __attribute__((noreturn));
7888 int x11_ioerror_handler(Display *d)
7890 ERR("X Error: Display %lx\n",
7897 static void X11_initialisation(void)
7899 if (x_initialised == YES) return;
7900 output_methods |= TO_X;
7902 set_default_configurations_for_x();
7903 x_initialised = YES;
7906 /* WARNING, this type not in Xlib spec */
7907 XSetErrorHandler(&x11_error_handler);
7908 XSetIOErrorHandler(&x11_ioerror_handler);
7912 static char **xargv = 0;
7913 static int xargc = 0;
7915 static void X11_create_window(void)
7917 if (output_methods & TO_X) {
7919 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7920 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7922 #else /* OWN_WINDOW */
7923 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7924 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7926 #endif /* OWN_WINDOW */
7930 update_text_area(); /* to position text/window on screen */
7933 if (own_window && !fixed_pos) {
7934 XMoveWindow(display, window.window, window.x, window.y);
7937 set_transparent_background(window.window);
7945 x11_stuff.region = XCreateRegion();
7947 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7948 ERR("Xdamage extension unavailable");
7950 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7951 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7952 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7953 #endif /* HAVE_XDAMAGE */
7956 update_text_area(); /* to get initial size of the window */
7959 /* setup lua window globals */
7960 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
7961 #endif /* HAVE_LUA */
7965 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7966 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7967 #define CONF2(a) if (strcasecmp(name, a) == 0)
7968 #define CONF(a) else CONF2(a)
7969 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7970 || strcasecmp(name, b) == 0)
7971 #define CONF_CONTINUE 1
7972 #define CONF_BREAK 2
7973 #define CONF_BUFF_SIZE 512
7975 static FILE *open_config_file(const char *f)
7977 #ifdef CONFIG_OUTPUT
7978 if (!strcmp(f, "==builtin==")) {
7979 return conf_cookie_open();
7981 #endif /* CONFIG_OUTPUT */
7982 return fopen(f, "r");
7985 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7989 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7992 remove_comments(buf);
7997 while (*p && isspace((int) *p)) {
8001 return CONF_CONTINUE; /* empty line */
8008 while (*p2 && !isspace((int) *p2)) {
8012 *p2 = '\0'; /* break at name's end */
8019 while (*p && isspace((int) *p)) {
8025 p2 = *value + strlen(*value);
8026 while (isspace((int) *(p2 - 1))) {
8035 static void load_config_file(const char *f)
8040 set_default_configurations();
8041 fp = open_config_file(f);
8045 DBGP("reading contents from config file '%s'", f);
8048 char buff[CONF_BUFF_SIZE], *name, *value;
8049 int ret = do_config_step(&line, fp, buff, &name, &value);
8050 if (ret == CONF_BREAK) {
8052 } else if (ret == CONF_CONTINUE) {
8058 /* don't listen if X is already initialised or
8059 * if we already know we don't want it */
8060 if(x_initialised != YES) {
8061 if (string_to_bool(value)) {
8062 output_methods &= TO_X;
8064 output_methods &= ~TO_X;
8065 x_initialised = NEVER;
8070 if (!value || x_initialised == YES) {
8075 disp = strdup(value);
8080 if (window.type == TYPE_DOCK)
8083 #endif /*OWN_WINDOW */
8085 int a = string_to_alignment(value);
8096 CONF("background") {
8097 fork_to_background = string_to_bool(value);
8100 CONF2("background") {
8101 fork_to_background = string_to_bool(value);
8105 CONF("show_graph_scale") {
8106 show_graph_scale = string_to_bool(value);
8108 CONF("show_graph_range") {
8109 show_graph_range = string_to_bool(value);
8111 CONF("border_margin") {
8112 ERR("border_margin is deprecated, please use window.border_inner_margin instead");
8114 window.border_inner_margin = strtol(value, 0, 0);
8115 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8120 CONF("border_inner_margin") {
8122 window.border_inner_margin = strtol(value, 0, 0);
8123 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8128 CONF("border_outer_margin") {
8130 window.border_outer_margin = strtol(value, 0, 0);
8131 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8136 CONF("border_width") {
8138 window.border_width = strtol(value, 0, 0);
8139 if (window.border_width < 0) window.border_width = 0;
8145 #define TEMPLATE_CONF(n) \
8146 CONF("template"#n) { \
8148 free(template[n]); \
8149 template[n] = strdup(value); \
8166 info.mail = parse_mail_args(IMAP_TYPE, value);
8173 info.mail = parse_mail_args(POP3_TYPE, value);
8178 CONF("default_bar_size") {
8181 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8188 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8192 CONF("default_graph_size") {
8195 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8202 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8205 CONF("default_gauge_size") {
8208 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8215 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8222 mpd_set_host(value);
8228 if (value && mpd_set_port(value)) {
8232 CONF("mpd_password") {
8234 mpd_set_password(value, 0);
8240 CONF("music_player_interval") {
8242 info.music_player_interval = strtod(value, 0);
8248 CONF("sensor_device") {
8250 sensor_device = strtol(value, 0, 0);
8256 CONF("cpu_avg_samples") {
8258 cpu_avg_samples = strtol(value, 0, 0);
8259 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8262 info.cpu_avg_samples = cpu_avg_samples;
8268 CONF("net_avg_samples") {
8270 net_avg_samples = strtol(value, 0, 0);
8271 if (net_avg_samples < 1 || net_avg_samples > 14) {
8274 info.net_avg_samples = net_avg_samples;
8280 CONF("diskio_avg_samples") {
8282 diskio_avg_samples = strtol(value, 0, 0);
8283 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8286 info.diskio_avg_samples = diskio_avg_samples;
8294 CONF("double_buffer") {
8295 use_xdbe = string_to_bool(value);
8299 CONF("override_utf8_locale") {
8300 utf8_mode = string_to_bool(value);
8302 CONF("draw_borders") {
8303 draw_borders = string_to_bool(value);
8305 CONF("draw_graph_borders") {
8306 draw_graph_borders = string_to_bool(value);
8308 CONF("draw_shades") {
8309 draw_shades = string_to_bool(value);
8311 CONF("draw_outline") {
8312 draw_outline = string_to_bool(value);
8315 CONF("out_to_console") {
8316 if(string_to_bool(value))
8317 output_methods |= TO_STDOUT;
8319 CONF("out_to_stderr") {
8320 if(string_to_bool(value))
8321 output_methods |= TO_STDERR;
8323 CONF("overwrite_file") {
8324 if(overwrite_file) {
8325 free(overwrite_file);
8328 if(overwrite_works(value)) {
8329 overwrite_file = strdup(value);
8330 output_methods |= OVERWRITE_FILE;
8332 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8334 CONF("append_file") {
8339 if(append_works(value)) {
8340 append_file = strdup(value);
8341 output_methods |= APPEND_FILE;
8343 ERR("append_file won't be able to create/append '%s'", value);
8345 CONF("use_spacer") {
8347 if (strcasecmp(value, "left") == EQUAL) {
8348 use_spacer = LEFT_SPACER;
8349 } else if (strcasecmp(value, "right") == EQUAL) {
8350 use_spacer = RIGHT_SPACER;
8351 } else if (strcasecmp(value, "none") == EQUAL) {
8352 use_spacer = NO_SPACER;
8354 use_spacer = string_to_bool(value);
8355 ERR("use_spacer should have an argument of left, right, or"
8356 " none. '%s' seems to be some form of '%s', so"
8357 " defaulting to %s.", value,
8358 use_spacer ? "true" : "false",
8359 use_spacer ? "right" : "none");
8361 use_spacer = RIGHT_SPACER;
8363 use_spacer = NO_SPACER;
8367 ERR("use_spacer should have an argument. Defaulting to right.");
8368 use_spacer = RIGHT_SPACER;
8374 use_xft = string_to_bool(value);
8378 set_first_font(value);
8382 if (value && font_count >= 0) {
8383 fonts[0].font_alpha = atof(value) * 65535.0;
8390 if (string_to_bool(value)) {
8391 ERR("Xft not enabled at compile time");
8395 /* xftfont silently ignored when no Xft */
8398 /* xftalpha is silently ignored when no Xft */
8403 set_first_font(value);
8411 gap_x = atoi(value);
8418 gap_y = atoi(value);
8424 CONF("mail_spool") {
8428 variable_substitute(value, buffer, 256);
8430 if (buffer[0] != '\0') {
8431 if (current_mail_spool) {
8432 free(current_mail_spool);
8434 current_mail_spool = strndup(buffer, text_buffer_size);
8441 CONF("minimum_size") {
8443 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8445 if (sscanf(value, "%d", &minimum_width) != 1) {
8453 CONF("maximum_width") {
8455 if (sscanf(value, "%d", &maximum_width) != 1) {
8463 CONF("no_buffers") {
8464 no_buffers = string_to_bool(value);
8466 CONF("top_name_width") {
8468 if (sscanf(value, "%u", &top_name_width) != 1) {
8474 if (top_name_width >= max_user_text) {
8475 top_name_width = max_user_text - 1;
8478 CONF("top_cpu_separate") {
8479 cpu_separate = string_to_bool(value);
8481 CONF("short_units") {
8482 short_units = string_to_bool(value);
8484 CONF("format_human_readable") {
8485 format_human_readable = string_to_bool(value);
8487 CONF("pad_percents") {
8488 pad_percents = atoi(value);
8492 CONF("own_window") {
8494 own_window = string_to_bool(value);
8497 CONF("own_window_class") {
8499 memset(window.class_name, 0, sizeof(window.class_name));
8500 strncpy(window.class_name, value,
8501 sizeof(window.class_name) - 1);
8504 CONF("own_window_title") {
8506 memset(window.title, 0, sizeof(window.title));
8507 strncpy(window.title, value, sizeof(window.title) - 1);
8510 CONF("own_window_transparent") {
8512 set_transparent = string_to_bool(value);
8515 CONF("own_window_hints") {
8517 char *p_hint, *p_save;
8518 char delim[] = ", ";
8520 /* tokenize the value into individual hints */
8521 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8523 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8524 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8525 SET_HINT(window.hints, HINT_UNDECORATED);
8526 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8527 SET_HINT(window.hints, HINT_BELOW);
8528 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8529 SET_HINT(window.hints, HINT_ABOVE);
8530 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8531 SET_HINT(window.hints, HINT_STICKY);
8532 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8533 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8534 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8535 SET_HINT(window.hints, HINT_SKIP_PAGER);
8540 p_hint = strtok_r(NULL, delim, &p_save);
8541 } while (p_hint != NULL);
8547 CONF("own_window_type") {
8549 if (strncmp(value, "normal", 6) == EQUAL) {
8550 window.type = TYPE_NORMAL;
8551 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8552 window.type = TYPE_DESKTOP;
8553 } else if (strncmp(value, "dock", 4) == EQUAL) {
8554 window.type = TYPE_DOCK;
8555 text_alignment = TOP_LEFT;
8556 } else if (strncmp(value, "panel", 5) == EQUAL) {
8557 window.type = TYPE_PANEL;
8558 } else if (strncmp(value, "override", 8) == EQUAL) {
8559 window.type = TYPE_OVERRIDE;
8568 CONF("stippled_borders") {
8570 stippled_borders = strtol(value, 0, 0);
8572 stippled_borders = 4;
8576 CONF("imlib_cache_size") {
8578 cimlib_set_cache_size(atoi(value));
8581 CONF("imlib_cache_flush_interval") {
8583 cimlib_set_cache_flush_interval(atoi(value));
8588 CONF("update_interval") {
8590 update_interval = strtod(value, 0);
8594 if (info.music_player_interval == 0) {
8595 // default to update_interval
8596 info.music_player_interval = update_interval;
8599 CONF("total_run_times") {
8601 total_run_times = strtod(value, 0);
8607 stuff_in_uppercase = string_to_bool(value);
8609 CONF("max_specials") {
8611 max_specials = atoi(value);
8616 CONF("max_user_text") {
8618 max_user_text = atoi(value);
8623 CONF("text_buffer_size") {
8625 text_buffer_size = atoi(value);
8626 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8627 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8628 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8636 if (output_methods & TO_X) {
8637 X11_initialisation();
8646 global_text = (char *) malloc(1);
8647 global_text[0] = '\0';
8650 unsigned int l = strlen(global_text);
8652 char buf[CONF_BUFF_SIZE];
8654 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8660 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8668 /* Check for continuation of \\-\n. */
8669 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8670 global_text[l-1] = '\0';
8674 global_text = (char *) realloc(global_text, l + bl + 1);
8675 strcat(global_text, buf);
8677 if (strlen(global_text) > max_user_text) {
8682 if (strlen(global_text) < 1) {
8683 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
8685 global_text_lines = line + 1;
8688 #ifdef TCP_PORT_MONITOR
8689 CONF("max_port_monitor_connections") {
8691 if (!value || (sscanf(value, "%d", &max) != 1)) {
8692 /* an error. use default, warn and continue. */
8693 tcp_portmon_set_max_connections(0);
8695 } else if (tcp_portmon_set_max_connections(max)) {
8696 /* max is < 0, default has been set*/
8701 CONF("if_up_strictness") {
8703 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8704 ifup_strictness = IFUP_UP;
8705 } else if (strcasecmp(value, "up") == EQUAL) {
8706 ifup_strictness = IFUP_UP;
8707 } else if (strcasecmp(value, "link") == EQUAL) {
8708 ifup_strictness = IFUP_LINK;
8709 } else if (strcasecmp(value, "address") == EQUAL) {
8710 ifup_strictness = IFUP_ADDR;
8712 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8713 ifup_strictness = IFUP_UP;
8717 CONF("temperature_unit") {
8719 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8720 } else if (set_temp_output_unit(value)) {
8721 ERR("temperature_unit: incorrect argument");
8727 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8728 char *skey = malloc(maxlength);
8729 char *svalue = malloc(maxlength);
8731 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8732 oldvalue = getenv(skey);
8733 if (oldvalue == NULL) {
8734 setenv(skey, svalue, 0);
8736 //PS: Don't free oldvalue, it's the real envvar, not a copy
8749 char *ptr = strtok(value, " ");
8752 ptr = strtok(NULL, " ");
8759 CONF("lua_draw_hook_pre") {
8761 llua_set_draw_pre_hook(value);
8766 CONF("lua_draw_hook_post") {
8768 llua_set_draw_post_hook(value);
8774 #endif /* HAVE_LUA */
8786 CONF("default_color"){}
8787 CONF3("default_shade_color", "default_shadecolor"){}
8788 CONF3("default_outline_color", "default_outlinecolor") {}
8789 CONF("own_window_colour") {}
8792 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8798 if (info.music_player_interval == 0) {
8799 // default to update_interval
8800 info.music_player_interval = update_interval;
8802 if (!global_text) { // didn't supply any text
8803 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
8808 static void load_config_file_x11(const char *f)
8813 fp = open_config_file(f);
8817 DBGP("reading contents from config file '%s'", f);
8820 char buff[CONF_BUFF_SIZE], *name, *value;
8821 int ret = do_config_step(&line, fp, buff, &name, &value);
8822 if (ret == CONF_BREAK) {
8824 } else if (ret == CONF_CONTINUE) {
8829 X11_initialisation();
8830 if (x_initialised == YES) {
8832 color0 = get_x11_color(value);
8839 X11_initialisation();
8840 if (x_initialised == YES) {
8842 color1 = get_x11_color(value);
8849 X11_initialisation();
8850 if (x_initialised == YES) {
8852 color2 = get_x11_color(value);
8859 X11_initialisation();
8860 if (x_initialised == YES) {
8862 color3 = get_x11_color(value);
8869 X11_initialisation();
8870 if (x_initialised == YES) {
8872 color4 = get_x11_color(value);
8879 X11_initialisation();
8880 if (x_initialised == YES) {
8882 color5 = get_x11_color(value);
8889 X11_initialisation();
8890 if (x_initialised == YES) {
8892 color6 = get_x11_color(value);
8899 X11_initialisation();
8900 if (x_initialised == YES) {
8902 color7 = get_x11_color(value);
8909 X11_initialisation();
8910 if (x_initialised == YES) {
8912 color8 = get_x11_color(value);
8919 X11_initialisation();
8920 if (x_initialised == YES) {
8922 color9 = get_x11_color(value);
8928 CONF("default_color") {
8929 X11_initialisation();
8930 if (x_initialised == YES) {
8932 default_fg_color = get_x11_color(value);
8938 CONF3("default_shade_color", "default_shadecolor") {
8939 X11_initialisation();
8940 if (x_initialised == YES) {
8942 default_bg_color = get_x11_color(value);
8948 CONF3("default_outline_color", "default_outlinecolor") {
8949 X11_initialisation();
8950 if (x_initialised == YES) {
8952 default_out_color = get_x11_color(value);
8959 CONF("own_window_colour") {
8960 X11_initialisation();
8961 if (x_initialised == YES) {
8963 background_colour = get_x11_color(value);
8965 ERR("Invalid colour for own_window_colour (try omitting the "
8966 "'#' for hex colours");
8972 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
8973 if(output_methods & TO_X) {
8974 X11_initialisation();
8983 #undef CONF_CONTINUE
8984 #undef CONF_BUFF_SIZE
8992 #if defined(WEATHER) && defined(XOAP)
8994 * TODO: make the xoap keys file readable from the config file
8995 * make the keys directly readable from the config file
8996 * make the xoap keys file giveable as a command line option
8998 static void load_xoap_keys(void)
9001 char *par = (char *) malloc(11 * sizeof(char));
9002 char *key = (char *) malloc(17 * sizeof(char));
9004 xoap = (char *) malloc(64 * sizeof(char));
9005 to_real_path(xoap, XOAP_FILE);
9006 fp = fopen(xoap, "r");
9008 if( fscanf(fp, "%10s %16s", par, key) == 2 ) {
9009 strcpy(xoap, "?cc=*&link=xoap&prod=xoap&par=");
9011 strcat(xoap, "&key=");
9013 strcat(xoap, "&unit=m");
9026 #endif /* WEATHER && XOAP */
9028 static void print_help(const char *prog_name) {
9029 printf("Usage: %s [OPTION]...\n"
9030 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
9031 "window. Command line options will override configurations defined in config\n"
9033 " -v, --version version\n"
9034 " -q, --quiet quiet mode\n"
9035 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
9036 " -c, --config=FILE config file to load\n"
9037 #ifdef CONFIG_OUTPUT
9038 " -C, --print-config print the builtin default config to stdout\n"
9039 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
9041 " -d, --daemonize daemonize, fork to background\n"
9042 " -h, --help help\n"
9044 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
9045 " -f, --font=FONT font to use\n"
9046 " -X, --display=DISPLAY X11 display to use\n"
9048 " -o, --own-window create own window to draw\n"
9051 " -b, --double-buffer double buffer (prevents flickering)\n"
9053 " -w, --window-id=WIN_ID window id to draw\n"
9054 " -x X x position\n"
9055 " -y Y y position\n"
9057 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
9058 " -u, --interval=SECS update interval\n"
9059 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
9064 /* : means that character before that takes an argument */
9065 static const char *getopt_string = "vVqdDt:u:i:hc:"
9075 #ifdef CONFIG_OUTPUT
9080 static const struct option longopts[] = {
9081 { "help", 0, NULL, 'h' },
9082 { "version", 0, NULL, 'V' },
9083 { "debug", 0, NULL, 'D' },
9084 { "config", 1, NULL, 'c' },
9085 #ifdef CONFIG_OUTPUT
9086 { "print-config", 0, NULL, 'C' },
9088 { "daemonize", 0, NULL, 'd' },
9090 { "alignment", 1, NULL, 'a' },
9091 { "font", 1, NULL, 'f' },
9092 { "display", 1, NULL, 'X' },
9094 { "own-window", 0, NULL, 'o' },
9097 { "double-buffer", 0, NULL, 'b' },
9099 { "window-id", 1, NULL, 'w' },
9101 { "text", 1, NULL, 't' },
9102 { "interval", 0, NULL, 'u' },
9106 void initialisation(int argc, char **argv) {
9107 struct sigaction act, oact;
9109 load_config_file(current_config);
9111 /* init specials array */
9112 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9113 ERR("failed to create specials array");
9117 if (current_mail_spool == NULL) {
9120 variable_substitute(MAIL_FILE, buf, 256);
9122 if (buf[0] != '\0') {
9123 current_mail_spool = strndup(buf, text_buffer_size);
9128 /* handle other command line arguments */
9130 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9131 || defined(__NetBSD__)
9132 optind = optreset = 1;
9137 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9138 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9139 "kvm_open")) == NULL) {
9140 CRIT_ERR(NULL, NULL, "cannot read kvm");
9145 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9153 fork_to_background = 1;
9156 global_debug_level++;
9160 set_first_font(optarg);
9163 text_alignment = string_to_alignment(optarg);
9168 disp = strdup(optarg);
9187 global_text = strndup(optarg, max_user_text);
9188 convert_escapes(global_text);
9192 update_interval = strtod(optarg, 0);
9193 if (info.music_player_interval == 0) {
9194 // default to update_interval
9195 info.music_player_interval = update_interval;
9200 total_run_times = strtod(optarg, 0);
9204 gap_x = atoi(optarg);
9208 gap_y = atoi(optarg);
9219 if (output_methods & TO_X) {
9220 load_config_file_x11(current_config);
9224 /* generate text and get initial size */
9225 extract_variable_text(global_text);
9232 if (fork_to_background) {
9237 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9244 fprintf(stderr, "\n");
9249 /* parent process */
9250 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9257 text_buffer = malloc(max_user_text);
9258 memset(text_buffer, 0, max_user_text);
9259 tmpstring1 = malloc(text_buffer_size);
9260 memset(tmpstring1, 0, text_buffer_size);
9261 tmpstring2 = malloc(text_buffer_size);
9262 memset(tmpstring2, 0, text_buffer_size);
9267 X11_create_window();
9270 /* Set signal handlers */
9271 act.sa_handler = signal_handler;
9272 sigemptyset(&act.sa_mask);
9275 act.sa_flags |= SA_RESTART;
9278 if ( sigaction(SIGINT, &act, &oact) < 0
9279 || sigaction(SIGUSR1, &act, &oact) < 0
9280 || sigaction(SIGHUP, &act, &oact) < 0
9281 || sigaction(SIGTERM, &act, &oact) < 0) {
9282 ERR("error setting signal handler: %s", strerror(errno));
9287 int main(int argc, char **argv)
9296 g_signal_pending = 0;
9297 max_user_text = MAX_USER_TEXT_DEFAULT;
9299 memset(&info, 0, sizeof(info));
9300 memset(template, 0, sizeof(template));
9303 #ifdef TCP_PORT_MONITOR
9304 /* set default connection limit */
9305 tcp_portmon_set_max_connections(0);
9308 /* handle command line parameters that don't change configs */
9310 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9311 || ((s = getenv("LANG")) && *s)) {
9312 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9314 ERR("malloc failed");
9316 for (x = 0; x < strlen(s); x++) {
9317 temp[x] = tolower(s[x]);
9320 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9326 if (!setlocale(LC_CTYPE, "")) {
9327 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9331 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9342 if (current_config) {
9343 free(current_config);
9345 current_config = strndup(optarg, max_user_text);
9348 freopen("/dev/null", "w", stderr);
9351 print_help(argv[0]);
9353 #ifdef CONFIG_OUTPUT
9360 window.window = strtol(optarg, 0, 0);
9369 /* check if specified config file is valid */
9370 if (current_config) {
9372 if (stat(current_config, &sb) ||
9373 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9374 ERR("invalid configuration file '%s'\n", current_config);
9375 free(current_config);
9380 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9382 if (!current_config) {
9383 /* load default config file */
9384 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9387 /* Try to use personal config file first */
9388 to_real_path(buf, CONFIG_FILE);
9389 if (buf[0] && (fp = fopen(buf, "r"))) {
9390 current_config = strndup(buf, max_user_text);
9394 /* Try to use system config file if personal config not readable */
9395 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9396 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9400 /* No readable config found */
9401 if (!current_config) {
9402 #ifdef CONFIG_OUTPUT
9403 current_config = strdup("==builtin==");
9404 ERR("no readable personal or system-wide config file found,"
9405 " using builtin default");
9407 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9408 #endif /* ! CONF_OUTPUT */
9412 #if defined(WEATHER) && defined(XOAP)
9413 /* Load xoap keys, if existing */
9415 #endif /* WEATHER && XOAP */
9417 #ifdef HAVE_SYS_INOTIFY_H
9418 inotify_fd = inotify_init();
9419 #endif /* HAVE_SYS_INOTIFY_H */
9421 initialisation(argc, argv);
9425 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9432 static void signal_handler(int sig)
9434 /* signal handler is light as a feather, as it should be.
9435 * we will poll g_signal_pending with each loop of conky
9436 * and do any signal processing there, NOT here. */
9437 g_signal_pending = sig;