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 */
215 " * Weather (METAR)\n"
219 #endif /* HAVE_IWLIB */
221 " * support for IBM/Lenovo notebooks\n"
231 #endif /* CONFIG_OUTPUT */
236 " * ALSA mixer support\n"
237 #endif /* MIXER_IS_ALSA */
247 #ifdef HAVE_LUA_CAIRO
249 #endif /* HAVE_LUA_CAIRO */
250 #ifdef HAVE_LUA_IMLIB2
253 #endif /* HAVE_LUA */
259 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
264 static void X11_create_window(void);
265 static void X11_initialisation(void);
267 struct _x11_stuff_s {
271 XserverRegion region2, part;
272 int event_base, error_base;
278 static int text_start_x, text_start_y; /* text start position in window */
279 static int text_width, text_height;
294 /* display to connect to */
295 static char *disp = NULL;
299 /* struct that has all info to be shared between
300 * instances of the same text object */
301 struct information info;
303 /* path to config file */
304 char *current_config;
306 /* set to 1 if you want all text to be in uppercase */
307 static unsigned int stuff_in_uppercase;
309 /* Run how many times? */
310 static unsigned long total_run_times;
313 static int fork_to_background;
315 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
317 /* filenames for output */
318 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
319 char *append_file = NULL; FILE *append_fpointer = NULL;
321 /* xoap suffix for weather from weather.com */
323 static char *xoap = NULL;
328 static int show_graph_scale;
329 static int show_graph_range;
331 /* Position on the screen */
332 static int text_alignment;
333 static int gap_x, gap_y;
336 static int draw_borders;
337 static int draw_graph_borders;
338 static int stippled_borders;
340 static int draw_shades, draw_outline;
342 static long default_fg_color, default_bg_color, default_out_color;
344 /* create own window or draw stuff to root? */
345 static int set_transparent = 0;
348 static int own_window = 0;
349 static int background_colour = 0;
351 /* fixed size/pos is set if wm/user changes them */
352 static int fixed_size = 0, fixed_pos = 0;
355 static int minimum_width, minimum_height;
356 static int maximum_width;
361 static int sensor_device;
364 static long color0, color1, color2, color3, color4, color5, color6, color7,
367 #define MAX_TEMPLATES 10
368 static char *template[MAX_TEMPLATES];
370 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
371 unsigned int max_user_text;
373 /* maximum size of individual text buffers, ie $exec buffer size */
374 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
377 #define CODEPAGE_LENGTH 20
379 long iconv_count = 0;
380 char iconv_converting;
381 static iconv_t **iconv_cd = 0;
383 int register_iconv(iconv_t *new_iconv)
385 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
387 CRIT_ERR(NULL, NULL, "Out of memory");
389 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
390 if (!iconv_cd[iconv_count]) {
391 CRIT_ERR(NULL, NULL, "Out of memory");
393 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
398 void free_iconv(void)
403 for (i = 0; i < iconv_count; i++) {
405 iconv_close(*iconv_cd[i]);
419 /* no buffers in used memory? */
422 /* pad percentages to decimals? */
423 static int pad_percents = 0;
425 static char *global_text = 0;
426 long global_text_lines;
428 static int total_updates;
429 static int updatereset;
431 int check_contains(char *f, char *s)
434 FILE *where = open_file(f, 0);
439 while (fgets(buf1, 256, where)) {
440 if (strstr(buf1, s)) {
447 ERR("Could not open the file");
454 #define SECRIT_MULTILINE_CHAR '\x02'
456 static inline int calc_text_width(const char *s, int l)
458 if ((output_methods & TO_X) == 0) {
466 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
467 (const FcChar8 *) s, l, &gi);
469 XftTextExtents8(display, fonts[selected_font].xftfont,
470 (const FcChar8 *) s, l, &gi);
476 return XTextWidth(fonts[selected_font].font, s, l);
481 /* formatted text to render on screen, generated in generate_text(),
482 * drawn in draw_stuff() */
484 static char *text_buffer;
486 /* quite boring functions */
488 static inline void for_each_line(char *b, int f(char *, int))
491 int special_index = 0; /* specials index */
493 for (ps = b, pe = b; *pe; pe++) {
496 special_index = f(ps, special_index);
503 f(ps, special_index);
507 static void convert_escapes(char *buf)
509 char *p = buf, *s = buf;
516 } else if (*s == '\\') {
527 /* Prints anything normally printed with snprintf according to the current value
528 * of use_spacer. Actually slightly more flexible than snprintf, as you can
529 * safely specify the destination buffer as one of your inputs. */
530 int spaced_print(char *buf, int size, const char *format, int width, ...)
539 tempbuf = malloc(size * sizeof(char));
541 // Passes the varargs along to vsnprintf
542 va_start(argp, width);
543 vsnprintf(tempbuf, size, format, argp);
546 switch (use_spacer) {
548 len = snprintf(buf, size, "%s", tempbuf);
551 len = snprintf(buf, size, "%*s", width, tempbuf);
554 len = snprintf(buf, size, "%-*s", width, tempbuf);
561 /* print percentage values
563 * - i.e., unsigned values between 0 and 100
564 * - respect the value of pad_percents */
565 static int percent_print(char *buf, int size, unsigned value)
567 return spaced_print(buf, size, "%u", pad_percents, value);
570 /* converts from bytes to human readable format (K, M, G, T)
572 * The algorithm always divides by 1024, as unit-conversion of byte
573 * counts suggests. But for output length determination we need to
574 * compare with 1000 here, as we print in decimal form. */
575 static void human_readable(long long num, char *buf, int size)
577 const char **suffix = suffixes;
583 /* Possibly just output as usual, for example for stdout usage */
584 if (!format_human_readable) {
585 spaced_print(buf, size, "%d", 6, round_to_int(num));
596 if (llabs(num) < 1000LL) {
597 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
601 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
609 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
611 * The goal is to always have a significance of 3, by
612 * adjusting the decimal part of the number. Sample output:
616 * so the point of alignment resides between number and unit. The
617 * upside of this is that there is minimal padding necessary, though
618 * there should be a way to make alignment take place at the decimal
619 * dot (then with fixed width decimal part).
621 * Note the repdigits below: when given a precision value, printf()
622 * rounds the float to it, not just cuts off the remaining digits. So
623 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
624 * printed with a precision of 0. Yay. */
626 precision = 0; /* print 100-999 without decimal part */
628 precision = 1; /* print 10-99 with one decimal place */
630 precision = 2; /* print 0-9 with two decimal places */
632 spaced_print(buf, size, format, width, precision, fnum, *suffix);
635 /* global object list root element */
636 static struct text_object global_root_object;
638 static inline void read_exec(const char *data, char *buf, const int size)
640 FILE *fp = popen(data, "r");
641 int length = fread(buf, 1, size, fp);
645 if (length > 0 && buf[length - 1] == '\n') {
646 buf[length - 1] = '\0';
650 void *threaded_exec(void *) __attribute__((noreturn));
652 void *threaded_exec(void *arg)
655 struct text_object *obj = (struct text_object *)arg;
658 buff = malloc(text_buffer_size);
659 read_exec(obj->data.texeci.cmd, buff,
668 timed_thread_lock(obj->data.texeci.p_timed_thread);
669 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
670 timed_thread_unlock(obj->data.texeci.p_timed_thread);
672 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
673 timed_thread_exit(obj->data.texeci.p_timed_thread);
679 static struct text_object *new_text_object_internal(void)
681 struct text_object *obj = malloc(sizeof(struct text_object));
682 memset(obj, 0, sizeof(struct text_object));
687 * Frees the list of text objects root points to. When internal = 1, it won't
688 * free global objects.
690 static void free_text_objects(struct text_object *root, int internal)
692 struct text_object *obj;
698 #define data obj->data
699 for (obj = root->prev; obj; obj = root->prev) {
700 root->prev = obj->prev;
706 #endif /* !__OpenBSD__ */
711 close(data.sysfs.fd);
713 #endif /* __linux__ */
715 free(data.read_tcp.host);
722 free(data.tztime.tz);
723 free(data.tztime.fmt);
726 free(data.mboxscan.args);
727 free(data.mboxscan.output);
732 case OBJ_unseen_mails:
733 case OBJ_flagged_mails:
734 case OBJ_unflagged_mails:
735 case OBJ_forwarded_mails:
736 case OBJ_unforwarded_mails:
737 case OBJ_replied_mails:
738 case OBJ_unreplied_mails:
739 case OBJ_draft_mails:
740 case OBJ_trashed_mails:
741 free(data.local_mail.box);
743 case OBJ_imap_unseen:
748 case OBJ_imap_messages:
753 case OBJ_pop3_unseen:
765 free_text_objects(obj->sub, 1);
768 case OBJ_if_existing:
771 free(data.ifblock.s);
772 free(data.ifblock.str);
775 free(data.tail.logfile);
776 free(data.tail.buffer);
792 case OBJ_iconv_start:
797 case OBJ_disk_protect:
801 free(data.ifblock.s);
802 free(data.ifblock.str);
805 if (info.gw_info.iface) {
806 free(info.gw_info.iface);
807 info.gw_info.iface = 0;
809 if (info.gw_info.ip) {
810 free(info.gw_info.ip);
814 case OBJ_ioscheduler:
819 #if (defined(__FreeBSD__) || defined(__linux__))
821 free(data.ifblock.s);
822 free(data.ifblock.str);
826 case OBJ_xmms2_artist:
827 if (info.xmms2.artist) {
828 free(info.xmms2.artist);
829 info.xmms2.artist = 0;
832 case OBJ_xmms2_album:
833 if (info.xmms2.album) {
834 free(info.xmms2.album);
835 info.xmms2.album = 0;
838 case OBJ_xmms2_title:
839 if (info.xmms2.title) {
840 free(info.xmms2.title);
841 info.xmms2.title = 0;
844 case OBJ_xmms2_genre:
845 if (info.xmms2.genre) {
846 free(info.xmms2.genre);
847 info.xmms2.genre = 0;
850 case OBJ_xmms2_comment:
851 if (info.xmms2.comment) {
852 free(info.xmms2.comment);
853 info.xmms2.comment = 0;
857 if (info.xmms2.url) {
858 free(info.xmms2.url);
863 if (info.xmms2.date) {
864 free(info.xmms2.date);
868 case OBJ_xmms2_status:
869 if (info.xmms2.status) {
870 free(info.xmms2.status);
871 info.xmms2.status = 0;
874 case OBJ_xmms2_playlist:
875 if (info.xmms2.playlist) {
876 free(info.xmms2.playlist);
877 info.xmms2.playlist = 0;
880 case OBJ_xmms2_smart:
881 if (info.xmms2.artist) {
882 free(info.xmms2.artist);
883 info.xmms2.artist = 0;
885 if (info.xmms2.title) {
886 free(info.xmms2.title);
887 info.xmms2.title = 0;
889 if (info.xmms2.url) {
890 free(info.xmms2.url);
897 case OBJ_bmpx_artist:
901 case OBJ_bmpx_bitrate:
911 free(data.rss.action);
916 free(data.weather.uri);
917 free(data.weather.data_type);
929 #endif /* HAVE_LUA */
936 case OBJ_battery_short:
939 case OBJ_battery_time:
942 #endif /* !__OpenBSD__ */
950 free(data.execi.cmd);
951 free(data.execi.buffer);
954 free(data.texeci.cmd);
955 free(data.texeci.buffer);
966 if (info.first_process && !internal) {
967 free_all_processes();
968 info.first_process = NULL;
970 if (data.top.s) free(data.top.s);
974 free(data.hddtemp.dev);
975 free(data.hddtemp.addr);
976 if (data.hddtemp.temp)
977 free(data.hddtemp.temp);
980 case OBJ_entropy_avail:
981 case OBJ_entropy_perc:
982 case OBJ_entropy_poolsize:
983 case OBJ_entropy_bar:
986 if (info.users.names) {
987 free(info.users.names);
988 info.users.names = 0;
992 if (info.users.terms) {
993 free(info.users.terms);
994 info.users.terms = 0;
998 if (info.users.times) {
999 free(info.users.times);
1000 info.users.times = 0;
1005 case OBJ_smapi_bat_perc:
1006 case OBJ_smapi_bat_temp:
1007 case OBJ_smapi_bat_power:
1010 case OBJ_if_smapi_bat_installed:
1011 free(data.ifblock.s);
1012 free(data.ifblock.str);
1021 case OBJ_mpd_artist:
1023 case OBJ_mpd_random:
1024 case OBJ_mpd_repeat:
1026 case OBJ_mpd_bitrate:
1027 case OBJ_mpd_status:
1029 case OBJ_mpd_elapsed:
1030 case OBJ_mpd_length:
1034 case OBJ_mpd_percent:
1036 case OBJ_if_mpd_playing:
1044 case OBJ_moc_artist:
1047 case OBJ_moc_totaltime:
1048 case OBJ_moc_timeleft:
1049 case OBJ_moc_curtime:
1050 case OBJ_moc_bitrate:
1057 free_text_objects(obj->sub, 1);
1061 free(data.scroll.text);
1062 free_text_objects(obj->sub, 1);
1066 free(data.combine.left);
1067 free(data.combine.seperation);
1068 free(data.combine.right);
1069 free_text_objects(obj->sub, 1);
1074 case OBJ_apcupsd_name:
1075 case OBJ_apcupsd_model:
1076 case OBJ_apcupsd_upsmode:
1077 case OBJ_apcupsd_cable:
1078 case OBJ_apcupsd_status:
1079 case OBJ_apcupsd_linev:
1080 case OBJ_apcupsd_load:
1081 case OBJ_apcupsd_loadbar:
1083 case OBJ_apcupsd_loadgraph:
1084 case OBJ_apcupsd_loadgauge:
1086 case OBJ_apcupsd_charge:
1087 case OBJ_apcupsd_timeleft:
1088 case OBJ_apcupsd_temp:
1089 case OBJ_apcupsd_lastxfer:
1091 #endif /* APCUPSD */
1094 case OBJ_desktop_number:
1095 case OBJ_desktop_name:
1096 if(info.x11.desktop.name) {
1097 free(info.x11.desktop.name);
1098 info.x11.desktop.name = NULL;
1100 if(info.x11.desktop.all_names) {
1101 free(info.x11.desktop.all_names);
1102 info.x11.desktop.all_names = NULL;
1113 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1118 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1119 *a = mixer_init(buf1);
1120 scan_bar(arg + n, w, h);
1122 *a = mixer_init(NULL);
1123 scan_bar(arg, w, h);
1128 /* strip a leading /dev/ if any, following symlinks first
1130 * BEWARE: this function returns a pointer to static content
1131 * which gets overwritten in consecutive calls. I.e.:
1132 * this function is NOT reentrant.
1134 static const char *dev_name(const char *path)
1136 static char buf[255]; /* should be enough for pathnames */
1142 #define DEV_NAME(x) \
1143 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1144 if ((buflen = readlink(path, buf, 254)) == -1)
1145 return DEV_NAME(path);
1147 return DEV_NAME(buf);
1151 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1156 if (obj->data.top.was_parsed) {
1159 obj->data.top.was_parsed = 1;
1161 if (arg && !obj->data.top.s) {
1162 obj->data.top.s = strndup(arg, text_buffer_size);
1165 need_mask |= (1 << INFO_TOP);
1168 obj->type = OBJ_top;
1170 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1171 obj->type = OBJ_top_mem;
1173 } else if (strcmp(&s[3], "_time") == EQUAL) {
1174 obj->type = OBJ_top_time;
1177 } else if (strcmp(&s[3], "_io") == EQUAL) {
1178 obj->type = OBJ_top_io;
1183 ERR("Must be top, top_mem, top_time or top_io");
1185 ERR("Must be top, top_mem or top_time");
1191 ERR("top needs arguments");
1195 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1196 if (strcmp(buf, "name") == EQUAL) {
1197 obj->data.top.type = TOP_NAME;
1198 } else if (strcmp(buf, "cpu") == EQUAL) {
1199 obj->data.top.type = TOP_CPU;
1200 } else if (strcmp(buf, "pid") == EQUAL) {
1201 obj->data.top.type = TOP_PID;
1202 } else if (strcmp(buf, "mem") == EQUAL) {
1203 obj->data.top.type = TOP_MEM;
1204 } else if (strcmp(buf, "time") == EQUAL) {
1205 obj->data.top.type = TOP_TIME;
1206 } else if (strcmp(buf, "mem_res") == EQUAL) {
1207 obj->data.top.type = TOP_MEM_RES;
1208 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1209 obj->data.top.type = TOP_MEM_VSIZE;
1211 } else if (strcmp(buf, "io_read") == EQUAL) {
1212 obj->data.top.type = TOP_READ_BYTES;
1213 } else if (strcmp(buf, "io_write") == EQUAL) {
1214 obj->data.top.type = TOP_WRITE_BYTES;
1215 } else if (strcmp(buf, "io_perc") == EQUAL) {
1216 obj->data.top.type = TOP_IO_PERC;
1219 ERR("invalid type arg for top");
1221 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1222 "io_read, io_write, io_perc");
1224 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1228 if (n < 1 || n > 10) {
1229 ERR("invalid num arg for top. Must be between 1 and 10.");
1232 obj->data.top.num = n - 1;
1235 ERR("invalid argument count for top");
1241 /* construct_text_object() creates a new text_object */
1242 static struct text_object *construct_text_object(const char *s,
1243 const char *arg, long line, char allow_threaded, void **ifblock_opaque, void *free_at_crash)
1245 // struct text_object *obj = new_text_object();
1246 struct text_object *obj = new_text_object_internal();
1250 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1251 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1252 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1253 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1254 obj_be_ifblock_if(ifblock_opaque, obj); {
1255 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1256 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1257 #define END } } else
1259 #define SIZE_DEFAULTS(arg) { \
1260 obj->a = default_##arg##_width; \
1261 obj->b = default_##arg##_height; \
1266 obj->type = OBJ_color;
1267 obj->data.l = get_x11_color(s);
1271 OBJ(freq, INFO_FREQ)
1274 obj->data.i = open_acpi_temperature(arg);
1275 END OBJ(acpiacadapter, 0)
1276 END OBJ(freq, INFO_FREQ)
1277 #endif /* !__OpenBSD__ */
1279 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1280 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1281 obj->data.cpu_index = 1;
1282 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1283 "Displaying the clock for CPU 1."); */
1285 obj->data.cpu_index = atoi(&arg[0]);
1288 END OBJ(freq_g, INFO_FREQ)
1290 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1291 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1292 obj->data.cpu_index = 1;
1293 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1294 "CPUs! Displaying the clock for CPU 1."); */
1296 obj->data.cpu_index = atoi(&arg[0]);
1299 END OBJ(read_tcp, 0)
1301 obj->data.read_tcp.host = malloc(text_buffer_size);
1302 sscanf(arg, "%s", obj->data.read_tcp.host);
1303 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1304 if(obj->data.read_tcp.port == 0) {
1305 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1306 strcpy(obj->data.read_tcp.host,"localhost");
1308 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1309 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1310 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1313 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1315 #if defined(__linux__)
1316 END OBJ(voltage_mv, 0)
1318 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1319 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1320 obj->data.cpu_index = 1;
1321 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1322 "CPUs! Displaying voltage for CPU 1."); */
1324 obj->data.cpu_index = atoi(&arg[0]);
1327 END OBJ(voltage_v, 0)
1329 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1330 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1331 obj->data.cpu_index = 1;
1332 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1333 "CPUs! Displaying voltage for CPU 1."); */
1335 obj->data.cpu_index = atoi(&arg[0]);
1340 END OBJ(wireless_essid, INFO_NET)
1342 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1344 // default to DEFAULTNETDEV
1345 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1346 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1349 END OBJ(wireless_mode, INFO_NET)
1351 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1353 // default to DEFAULTNETDEV
1354 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1355 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1358 END OBJ(wireless_bitrate, INFO_NET)
1360 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1362 // default to DEFAULTNETDEV
1363 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1364 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1367 END OBJ(wireless_ap, INFO_NET)
1369 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1371 // default to DEFAULTNETDEV
1372 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1373 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1376 END OBJ(wireless_link_qual, INFO_NET)
1378 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1380 // default to DEFAULTNETDEV
1381 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1382 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1385 END OBJ(wireless_link_qual_max, INFO_NET)
1387 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1389 // default to DEFAULTNETDEV
1390 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1391 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1394 END OBJ(wireless_link_qual_perc, INFO_NET)
1396 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1398 // default to DEFAULTNETDEV
1399 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1400 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1403 END OBJ(wireless_link_bar, INFO_NET)
1406 arg = scan_bar(arg, &obj->a, &obj->b);
1407 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1409 // default to DEFAULTNETDEV
1410 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1411 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1414 #endif /* HAVE_IWLIB */
1416 #endif /* __linux__ */
1424 sscanf(arg, "%63s", bat);
1426 strcpy(bat, "BAT0");
1428 obj->data.s = strndup(bat, text_buffer_size);
1429 END OBJ(battery_short, 0)
1433 sscanf(arg, "%63s", bat);
1435 strcpy(bat, "BAT0");
1437 obj->data.s = strndup(bat, text_buffer_size);
1438 END OBJ(battery_time, 0)
1442 sscanf(arg, "%63s", bat);
1444 strcpy(bat, "BAT0");
1446 obj->data.s = strndup(bat, text_buffer_size);
1447 END OBJ(battery_percent, 0)
1451 sscanf(arg, "%63s", bat);
1453 strcpy(bat, "BAT0");
1455 obj->data.s = strndup(bat, text_buffer_size);
1456 END OBJ(battery_bar, 0)
1461 arg = scan_bar(arg, &obj->a, &obj->b);
1462 sscanf(arg, "%63s", bat);
1464 strcpy(bat, "BAT0");
1466 obj->data.s = strndup(bat, text_buffer_size);
1467 #endif /* !__OpenBSD__ */
1469 #if defined(__linux__)
1470 END OBJ(disk_protect, 0)
1472 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1474 CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1475 END OBJ(i8k_version, INFO_I8K)
1476 END OBJ(i8k_bios, INFO_I8K)
1477 END OBJ(i8k_serial, INFO_I8K)
1478 END OBJ(i8k_cpu_temp, INFO_I8K)
1479 END OBJ(i8k_left_fan_status, INFO_I8K)
1480 END OBJ(i8k_right_fan_status, INFO_I8K)
1481 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1482 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1483 END OBJ(i8k_ac_status, INFO_I8K)
1484 END OBJ(i8k_buttons_status, INFO_I8K)
1487 END OBJ(ibm_temps, 0)
1489 CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1491 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1492 obj->data.sensor = 0;
1493 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1494 "Using 0 (CPU temp sensor).");
1496 obj->data.sensor = atoi(&arg[0]);
1497 END OBJ(ibm_volume, 0)
1498 END OBJ(ibm_brightness, 0)
1500 /* information from sony_laptop kernel module
1501 * /sys/devices/platform/sony-laptop */
1502 END OBJ(sony_fanspeed, 0)
1503 END OBJ_IF(if_gw, INFO_GW)
1504 END OBJ(ioscheduler, 0)
1506 CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1509 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1510 END OBJ(laptop_mode, 0)
1511 END OBJ(pb_battery, 0)
1512 if (arg && strcmp(arg, "status") == EQUAL) {
1513 obj->data.i = PB_BATT_STATUS;
1514 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1515 obj->data.i = PB_BATT_PERCENT;
1516 } else if (arg && strcmp(arg, "time") == EQUAL) {
1517 obj->data.i = PB_BATT_TIME;
1519 ERR("pb_battery: needs one argument: status, percent or time");
1524 #endif /* __linux__ */
1525 #if (defined(__FreeBSD__) || defined(__linux__))
1526 END OBJ_IF(if_up, 0)
1528 ERR("if_up needs an argument");
1529 obj->data.ifblock.s = 0;
1531 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1534 #if defined(__OpenBSD__)
1535 END OBJ(obsd_sensors_temp, 0)
1537 CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1539 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1540 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1541 obj->data.sensor = 0;
1542 ERR("Invalid temperature sensor number!");
1544 obj->data.sensor = atoi(&arg[0]);
1545 END OBJ(obsd_sensors_fan, 0)
1547 CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1550 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1551 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1552 obj->data.sensor = 0;
1553 ERR("Invalid fan sensor number!");
1555 obj->data.sensor = atoi(&arg[0]);
1556 END OBJ(obsd_sensors_volt, 0)
1558 CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1561 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1562 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1563 obj->data.sensor = 0;
1564 ERR("Invalid voltage sensor number!");
1566 obj->data.sensor = atoi(&arg[0]);
1567 END OBJ(obsd_vendor, 0)
1568 END OBJ(obsd_product, 0)
1569 #endif /* __OpenBSD__ */
1570 END OBJ(buffers, INFO_BUFFERS)
1571 END OBJ(cached, INFO_BUFFERS)
1572 #define SCAN_CPU(__arg, __var) { \
1574 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1575 __arg += __offset; \
1579 END OBJ(cpu, INFO_CPU)
1580 SCAN_CPU(arg, obj->data.cpu_index);
1581 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1583 END OBJ(cpugauge, INFO_CPU)
1584 SIZE_DEFAULTS(gauge);
1585 SCAN_CPU(arg, obj->data.cpu_index);
1586 scan_gauge(arg, &obj->a, &obj->b);
1587 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1589 END OBJ(cpubar, INFO_CPU)
1591 SCAN_CPU(arg, obj->data.cpu_index);
1592 scan_bar(arg, &obj->a, &obj->b);
1593 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1595 END OBJ(cpugraph, INFO_CPU)
1597 SIZE_DEFAULTS(graph);
1598 SCAN_CPU(arg, obj->data.cpu_index);
1599 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1600 &obj->e, &obj->char_a, &obj->char_b);
1601 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1603 END OBJ(loadgraph, INFO_LOADAVG)
1605 SIZE_DEFAULTS(graph);
1606 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1607 &obj->e, &obj->char_a, &obj->char_b);
1611 r = sscanf(arg, "%d", &a);
1613 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1617 END OBJ(diskio, INFO_DISKIO)
1618 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1619 END OBJ(diskio_read, INFO_DISKIO)
1620 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1621 END OBJ(diskio_write, INFO_DISKIO)
1622 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1624 END OBJ(diskiograph, INFO_DISKIO)
1626 SIZE_DEFAULTS(graph);
1627 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1628 &obj->e, &obj->char_a, &obj->char_b);
1630 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1632 END OBJ(diskiograph_read, INFO_DISKIO)
1634 SIZE_DEFAULTS(graph);
1635 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1636 &obj->e, &obj->char_a, &obj->char_b);
1638 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1640 END OBJ(diskiograph_write, INFO_DISKIO)
1642 SIZE_DEFAULTS(graph);
1643 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1644 &obj->e, &obj->char_a, &obj->char_b);
1646 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1651 if (output_methods & TO_X) {
1652 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1656 obj->data.l = color0;
1658 obj->data.l = color1;
1660 obj->data.l = color2;
1662 obj->data.l = color3;
1664 obj->data.l = color4;
1666 obj->data.l = color5;
1668 obj->data.l = color6;
1670 obj->data.l = color7;
1672 obj->data.l = color8;
1674 obj->data.l = color9;
1677 obj->data.s = scan_font(arg);
1679 END OBJ(conky_version, 0)
1680 END OBJ(conky_build_date, 0)
1681 END OBJ(conky_build_arch, 0)
1682 END OBJ(downspeed, INFO_NET)
1684 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1686 // default to DEFAULTNETDEV
1687 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1688 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1691 END OBJ(downspeedf, INFO_NET)
1693 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1695 // default to DEFAULTNETDEV
1696 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1697 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1701 END OBJ(downspeedgraph, INFO_NET)
1703 SIZE_DEFAULTS(graph);
1704 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1705 &obj->e, &obj->char_a, &obj->char_b);
1707 // default to DEFAULTNETDEV
1708 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1709 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1713 obj_be_ifblock_else(ifblock_opaque, obj);
1715 obj_be_ifblock_endif(ifblock_opaque, obj);
1717 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1719 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1722 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1724 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1727 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1729 END OBJ(execgauge, 0)
1730 SIZE_DEFAULTS(gauge);
1731 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1732 END OBJ(execgraph, 0)
1733 SIZE_DEFAULTS(graph);
1734 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1736 END OBJ(execibar, 0)
1740 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1743 ERR("${execibar <interval> command}");
1744 obj->type = OBJ_text;
1745 snprintf(buf, 256, "${%s}", s);
1746 obj->data.s = strndup(buf, text_buffer_size);
1748 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1751 END OBJ(execigraph, 0)
1753 SIZE_DEFAULTS(graph);
1755 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1758 ERR("${execigraph <interval> command}");
1759 obj->type = OBJ_text;
1760 snprintf(buf, 256, "${%s}", s);
1761 obj->data.s = strndup(buf, text_buffer_size);
1763 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1765 END OBJ(execigauge, 0)
1767 SIZE_DEFAULTS(gauge);
1769 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1772 ERR("${execigauge <interval> command}");
1773 obj->type = OBJ_text;
1774 snprintf(buf, 256, "${%s}", s);
1775 obj->data.s = strndup(buf, text_buffer_size);
1777 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1783 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1786 ERR("${execi <interval> command}");
1787 obj->type = OBJ_text;
1788 snprintf(buf, 256, "${%s}", s);
1789 obj->data.s = strndup(buf, text_buffer_size);
1791 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1792 obj->data.execi.buffer = malloc(text_buffer_size);
1797 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1800 ERR("${execi <interval> command}");
1801 obj->type = OBJ_text;
1802 snprintf(buf, 256, "${%s}", s);
1803 obj->data.s = strndup(buf, text_buffer_size);
1805 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1806 obj->data.execi.buffer = malloc(text_buffer_size);
1808 END OBJ_THREAD(texeci, 0)
1811 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1814 ERR("${texeci <interval> command}");
1815 obj->type = OBJ_text;
1816 snprintf(buf, 256, "${%s}", s);
1817 obj->data.s = strndup(buf, text_buffer_size);
1819 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1820 obj->data.texeci.buffer = malloc(text_buffer_size);
1822 obj->data.texeci.p_timed_thread = NULL;
1823 END OBJ(pre_exec, 0)
1824 obj->type = OBJ_text;
1828 read_exec(arg, buf, sizeof(buf));
1829 obj->data.s = strndup(buf, text_buffer_size);
1831 obj->data.s = strndup("", text_buffer_size);
1834 END OBJ(fs_bar, INFO_FS)
1836 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1838 while (isspace(*arg)) {
1847 obj->data.fsbar.fs = prepare_fs_stat(arg);
1848 END OBJ(fs_bar_free, INFO_FS)
1850 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1852 while (isspace(*arg)) {
1862 obj->data.fsbar.fs = prepare_fs_stat(arg);
1863 END OBJ(fs_free, INFO_FS)
1867 obj->data.fs = prepare_fs_stat(arg);
1868 END OBJ(fs_used_perc, INFO_FS)
1872 obj->data.fs = prepare_fs_stat(arg);
1873 END OBJ(fs_free_perc, INFO_FS)
1877 obj->data.fs = prepare_fs_stat(arg);
1878 END OBJ(fs_size, INFO_FS)
1882 obj->data.fs = prepare_fs_stat(arg);
1883 END OBJ(fs_type, INFO_FS)
1887 obj->data.fs = prepare_fs_stat(arg);
1888 END OBJ(fs_used, INFO_FS)
1892 obj->data.fs = prepare_fs_stat(arg);
1894 obj->data.i = arg ? atoi(arg) : 1;
1895 END OBJ(nameserver, INFO_DNS)
1896 obj->data.i = arg ? atoi(arg) : 0;
1898 obj->data.i = arg ? atoi(arg) : 1;
1900 obj->data.i = arg ? atoi(arg) : 1;
1904 ERR("goto needs arguments");
1905 obj->type = OBJ_text;
1906 obj->data.s = strndup("${goto}", text_buffer_size);
1910 obj->data.i = atoi(arg);
1916 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1917 sscanf(arg, "%d", &b);
1923 obj->data.pair.a = a;
1924 obj->data.pair.b = b;
1927 END OBJ(i2c, INFO_SYSFS)
1928 char buf1[64], buf2[64];
1929 float factor, offset;
1933 ERR("i2c needs arguments");
1934 obj->type = OBJ_text;
1935 // obj->data.s = strndup("${i2c}", text_buffer_size);
1939 #define HWMON_RESET() {\
1944 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1945 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1946 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1947 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1950 ERR("i2c failed to parse arguments");
1951 obj->type = OBJ_text;
1954 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1955 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
1956 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1957 strncpy(obj->data.sysfs.type, buf2, 63);
1958 obj->data.sysfs.factor = factor;
1959 obj->data.sysfs.offset = offset;
1961 END OBJ(platform, INFO_SYSFS)
1962 char buf1[64], buf2[64];
1963 float factor, offset;
1967 ERR("platform needs arguments");
1968 obj->type = OBJ_text;
1972 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1973 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1974 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1975 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1978 ERR("platform failed to parse arguments");
1979 obj->type = OBJ_text;
1982 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1983 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
1984 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1985 strncpy(obj->data.sysfs.type, buf2, 63);
1986 obj->data.sysfs.factor = factor;
1987 obj->data.sysfs.offset = offset;
1989 END OBJ(hwmon, INFO_SYSFS)
1990 char buf1[64], buf2[64];
1991 float factor, offset;
1995 ERR("hwmon needs argumanets");
1996 obj->type = OBJ_text;
2000 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2001 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2002 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2003 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2008 ERR("hwmon failed to parse arguments");
2009 obj->type = OBJ_text;
2012 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2013 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2014 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2015 strncpy(obj->data.sysfs.type, buf2, 63);
2016 obj->data.sysfs.factor = factor;
2017 obj->data.sysfs.offset = offset;
2019 #endif /* !__OpenBSD__ */
2022 /* we have four different types of top (top, top_mem, top_time and top_io). To
2023 * avoid having almost-same code four times, we have this special
2025 if (strncmp(s, "top", 3) == EQUAL) {
2026 if (!parse_top_args(s, arg, obj)) {
2029 } else OBJ(addr, INFO_NET)
2031 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2033 // default to DEFAULTNETDEV
2034 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2035 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2038 #if defined(__linux__)
2039 END OBJ(addrs, INFO_NET)
2041 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2043 // default to DEFAULTNETDEV
2044 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2045 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2048 #endif /* __linux__ */
2050 if (init_tail_object(obj, arg)) {
2051 obj->type = OBJ_text;
2052 obj->data.s = strndup("${tail}", text_buffer_size);
2055 if (init_head_object(obj, arg)) {
2056 obj->type = OBJ_text;
2057 obj->data.s = strndup("${head}", text_buffer_size);
2061 obj->data.s = strndup(arg, text_buffer_size);
2063 CRIT_ERR(obj, free_at_crash, "lines needs a argument");
2067 obj->data.s = strndup(arg, text_buffer_size);
2069 CRIT_ERR(obj, free_at_crash, "words needs a argument");
2071 END OBJ(loadavg, INFO_LOADAVG)
2072 int a = 1, b = 2, c = 3, r = 3;
2075 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2076 if (r >= 3 && (c < 1 || c > 3)) {
2079 if (r >= 2 && (b < 1 || b > 3)) {
2082 if (r >= 1 && (a < 1 || a > 3)) {
2086 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2087 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2088 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2089 END OBJ_IF(if_empty, 0)
2091 ERR("if_empty needs an argument");
2092 obj->data.ifblock.s = 0;
2094 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2095 obj->sub = malloc(sizeof(struct text_object));
2096 extract_variable_text_internal(obj->sub,
2097 obj->data.ifblock.s, 0);
2099 END OBJ_IF(if_match, 0)
2101 ERR("if_match needs arguments");
2102 obj->data.ifblock.s = 0;
2104 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2105 obj->sub = malloc(sizeof(struct text_object));
2106 extract_variable_text_internal(obj->sub,
2107 obj->data.ifblock.s, 0);
2109 END OBJ_IF(if_existing, 0)
2111 ERR("if_existing needs an argument or two");
2112 obj->data.ifblock.s = NULL;
2113 obj->data.ifblock.str = NULL;
2115 char buf1[256], buf2[256];
2116 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2119 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2120 obj->data.ifblock.str = NULL;
2122 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2123 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2126 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2127 END OBJ_IF(if_mounted, 0)
2129 ERR("if_mounted needs an argument");
2130 obj->data.ifblock.s = 0;
2132 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2135 END OBJ_IF(if_running, INFO_TOP)
2137 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2139 END OBJ_IF(if_running, 0)
2143 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2144 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2147 ERR("if_running needs an argument");
2148 obj->data.ifblock.s = 0;
2154 char box[256], dst[256];
2158 /* Kapil: Changed from MAIL_FILE to
2159 current_mail_spool since the latter
2160 is a copy of the former if undefined
2161 but the latter should take precedence
2163 strncpy(box, current_mail_spool, sizeof(box));
2165 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2167 strncpy(box, arg, sizeof(box));
2171 variable_substitute(box, dst, sizeof(dst));
2172 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2173 obj->data.local_mail.interval = n1;
2174 END OBJ(new_mails, 0)
2176 char box[256], dst[256];
2180 strncpy(box, current_mail_spool, sizeof(box));
2182 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2184 strncpy(box, arg, sizeof(box));
2188 variable_substitute(box, dst, sizeof(dst));
2189 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2190 obj->data.local_mail.interval = n1;
2191 END OBJ(seen_mails, 0)
2193 char box[256], dst[256];
2197 strncpy(box, current_mail_spool, sizeof(box));
2199 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2201 strncpy(box, arg, sizeof(box));
2205 variable_substitute(box, dst, sizeof(dst));
2206 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2207 obj->data.local_mail.interval = n1;
2208 END OBJ(unseen_mails, 0)
2210 char box[256], dst[256];
2214 strncpy(box, current_mail_spool, sizeof(box));
2216 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2218 strncpy(box, arg, sizeof(box));
2222 variable_substitute(box, dst, sizeof(dst));
2223 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2224 obj->data.local_mail.interval = n1;
2225 END OBJ(flagged_mails, 0)
2227 char box[256], dst[256];
2231 strncpy(box, current_mail_spool, sizeof(box));
2233 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2235 strncpy(box, arg, sizeof(box));
2239 variable_substitute(box, dst, sizeof(dst));
2240 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2241 obj->data.local_mail.interval = n1;
2242 END OBJ(unflagged_mails, 0)
2244 char box[256], dst[256];
2248 strncpy(box, current_mail_spool, sizeof(box));
2250 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2252 strncpy(box, arg, sizeof(box));
2256 variable_substitute(box, dst, sizeof(dst));
2257 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2258 obj->data.local_mail.interval = n1;
2259 END OBJ(forwarded_mails, 0)
2261 char box[256], dst[256];
2265 strncpy(box, current_mail_spool, sizeof(box));
2267 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2269 strncpy(box, arg, sizeof(box));
2273 variable_substitute(box, dst, sizeof(dst));
2274 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2275 obj->data.local_mail.interval = n1;
2276 END OBJ(unforwarded_mails, 0)
2278 char box[256], dst[256];
2282 strncpy(box, current_mail_spool, sizeof(box));
2284 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2286 strncpy(box, arg, sizeof(box));
2290 variable_substitute(box, dst, sizeof(dst));
2291 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2292 obj->data.local_mail.interval = n1;
2293 END OBJ(replied_mails, 0)
2295 char box[256], dst[256];
2299 strncpy(box, current_mail_spool, sizeof(box));
2301 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2303 strncpy(box, arg, sizeof(box));
2307 variable_substitute(box, dst, sizeof(dst));
2308 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2309 obj->data.local_mail.interval = n1;
2310 END OBJ(unreplied_mails, 0)
2312 char box[256], dst[256];
2316 strncpy(box, current_mail_spool, sizeof(box));
2318 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2320 strncpy(box, arg, sizeof(box));
2324 variable_substitute(box, dst, sizeof(dst));
2325 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2326 obj->data.local_mail.interval = n1;
2327 END OBJ(draft_mails, 0)
2329 char box[256], dst[256];
2333 strncpy(box, current_mail_spool, sizeof(box));
2335 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2337 strncpy(box, arg, sizeof(box));
2341 variable_substitute(box, dst, sizeof(dst));
2342 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2343 obj->data.local_mail.interval = n1;
2344 END OBJ(trashed_mails, 0)
2346 char box[256], dst[256];
2350 strncpy(box, current_mail_spool, sizeof(box));
2352 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2354 strncpy(box, arg, sizeof(box));
2358 variable_substitute(box, dst, sizeof(dst));
2359 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2360 obj->data.local_mail.interval = n1;
2361 END OBJ(mboxscan, 0)
2362 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2363 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2364 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2365 obj->data.mboxscan.output[0] = 1;
2366 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2367 END OBJ(mem, INFO_MEM)
2368 END OBJ(memeasyfree, INFO_MEM)
2369 END OBJ(memfree, INFO_MEM)
2370 END OBJ(memmax, INFO_MEM)
2371 END OBJ(memperc, INFO_MEM)
2373 END OBJ(memgauge, INFO_MEM)
2374 SIZE_DEFAULTS(gauge);
2375 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2377 END OBJ(membar, INFO_MEM)
2379 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2381 END OBJ(memgraph, INFO_MEM)
2383 SIZE_DEFAULTS(graph);
2384 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2385 &obj->e, &obj->char_a, &obj->char_b);
2389 END OBJ(mixer, INFO_MIXER)
2390 obj->data.l = mixer_init(arg);
2391 END OBJ(mixerl, INFO_MIXER)
2392 obj->data.l = mixer_init(arg);
2393 END OBJ(mixerr, INFO_MIXER)
2394 obj->data.l = mixer_init(arg);
2396 END OBJ(mixerbar, INFO_MIXER)
2398 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2399 &obj->data.mixerbar.h);
2400 END OBJ(mixerlbar, INFO_MIXER)
2402 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2403 &obj->data.mixerbar.h);
2404 END OBJ(mixerrbar, INFO_MIXER)
2406 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2407 &obj->data.mixerbar.h);
2409 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2410 obj->data.ifblock.i = mixer_init(arg);
2412 END OBJ(monitor, INFO_X11)
2413 END OBJ(monitor_number, INFO_X11)
2414 END OBJ(desktop, INFO_X11)
2415 END OBJ(desktop_number, INFO_X11)
2416 END OBJ(desktop_name, INFO_X11)
2418 END OBJ(nodename, 0)
2419 END OBJ(processes, INFO_PROCS)
2420 END OBJ(running_processes, INFO_RUN_PROCS)
2421 END OBJ(shadecolor, 0)
2423 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2425 END OBJ(outlinecolor, 0)
2427 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2429 END OBJ(stippled_hr, 0)
2431 int a = stippled_borders, b = 1;
2434 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2435 sscanf(arg, "%d", &b);
2441 obj->data.pair.a = a;
2442 obj->data.pair.b = b;
2444 END OBJ(swap, INFO_MEM)
2445 END OBJ(swapfree, INFO_MEM)
2446 END OBJ(swapmax, INFO_MEM)
2447 END OBJ(swapperc, INFO_MEM)
2448 END OBJ(swapbar, INFO_MEM)
2450 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2453 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2455 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2457 char buf1[256], buf2[256], *fmt, *tz;
2461 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2471 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2472 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2474 END OBJ(iconv_start, 0)
2475 if (iconv_converting) {
2476 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
2477 "starting another");
2480 char iconv_from[CODEPAGE_LENGTH];
2481 char iconv_to[CODEPAGE_LENGTH];
2483 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2484 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
2488 new_iconv = iconv_open(iconv_to, iconv_from);
2489 if (new_iconv == (iconv_t) (-1)) {
2490 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2492 obj->a = register_iconv(&new_iconv);
2493 iconv_converting = 1;
2497 CRIT_ERR(obj, free_at_crash, "Iconv requires arguments");
2499 END OBJ(iconv_stop, 0)
2500 iconv_converting = 0;
2503 END OBJ(totaldown, INFO_NET)
2505 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2507 // default to DEFAULTNETDEV
2508 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2509 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2512 END OBJ(totalup, INFO_NET)
2513 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2515 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2517 // default to DEFAULTNETDEV
2518 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2519 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2523 END OBJ_IF(if_updatenr, 0)
2524 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2525 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
2526 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2528 obj->data.i = arg ? atoi(arg) : 0;
2530 obj->data.i = arg ? atoi(arg) : 0;
2531 END OBJ(upspeed, INFO_NET)
2533 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2535 // default to DEFAULTNETDEV
2536 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2537 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2540 END OBJ(upspeedf, INFO_NET)
2542 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2544 // default to DEFAULTNETDEV
2545 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2546 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2551 END OBJ(upspeedgraph, INFO_NET)
2553 SIZE_DEFAULTS(graph);
2554 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2555 &obj->e, &obj->char_a, &obj->char_b);
2557 // default to DEFAULTNETDEV
2558 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2559 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2562 END OBJ(uptime_short, INFO_UPTIME)
2563 END OBJ(uptime, INFO_UPTIME)
2564 END OBJ(user_names, INFO_USERS)
2565 END OBJ(user_times, INFO_USERS)
2566 END OBJ(user_terms, INFO_USERS)
2567 END OBJ(user_number, INFO_USERS)
2568 #if defined(__linux__)
2569 END OBJ(gw_iface, INFO_GW)
2570 END OBJ(gw_ip, INFO_GW)
2571 #endif /* !__linux__ */
2573 END OBJ(adt746xcpu, 0)
2574 END OBJ(adt746xfan, 0)
2575 #endif /* !__OpenBSD__ */
2576 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2577 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2578 END OBJ(apm_adapter, 0)
2579 END OBJ(apm_battery_life, 0)
2580 END OBJ(apm_battery_time, 0)
2581 #endif /* __FreeBSD__ */
2582 END OBJ_THREAD(imap_unseen, 0)
2585 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2590 END OBJ_THREAD(imap_messages, 0)
2593 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2598 END OBJ_THREAD(pop3_unseen, 0)
2601 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2606 END OBJ_THREAD(pop3_used, 0)
2609 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2617 obj->data.s = strndup(arg, text_buffer_size);
2619 ERR("smapi needs an argument");
2620 END OBJ_IF(if_smapi_bat_installed, 0)
2622 ERR("if_smapi_bat_installed needs an argument");
2623 obj->data.ifblock.s = 0;
2625 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2626 END OBJ(smapi_bat_perc, 0)
2628 obj->data.s = strndup(arg, text_buffer_size);
2630 ERR("smapi_bat_perc needs an argument");
2631 END OBJ(smapi_bat_temp, 0)
2633 obj->data.s = strndup(arg, text_buffer_size);
2635 ERR("smapi_bat_temp needs an argument");
2636 END OBJ(smapi_bat_power, 0)
2638 obj->data.s = strndup(arg, text_buffer_size);
2640 ERR("smapi_bat_power needs an argument");
2642 END OBJ(smapi_bat_bar, 0)
2646 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2647 ERR("first argument to smapi_bat_bar must be an integer value");
2651 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2654 ERR("smapi_bat_bar needs an argument");
2658 #define mpd_set_maxlen(name) \
2661 sscanf(arg, "%d", &i); \
2663 obj->data.i = i + 1; \
2665 ERR(#name ": invalid length argument"); \
2667 END OBJ(mpd_artist, INFO_MPD)
2668 mpd_set_maxlen(mpd_artist);
2670 END OBJ(mpd_title, INFO_MPD)
2671 mpd_set_maxlen(mpd_title);
2673 END OBJ(mpd_random, INFO_MPD) init_mpd();
2674 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2675 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2676 END OBJ(mpd_length, INFO_MPD) init_mpd();
2677 END OBJ(mpd_track, INFO_MPD)
2678 mpd_set_maxlen(mpd_track);
2680 END OBJ(mpd_name, INFO_MPD)
2681 mpd_set_maxlen(mpd_name);
2683 END OBJ(mpd_file, INFO_MPD)
2684 mpd_set_maxlen(mpd_file);
2686 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2687 END OBJ(mpd_album, INFO_MPD)
2688 mpd_set_maxlen(mpd_album);
2690 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2691 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2692 END OBJ(mpd_status, INFO_MPD) init_mpd();
2693 END OBJ(mpd_bar, INFO_MPD)
2695 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2697 END OBJ(mpd_smart, INFO_MPD)
2698 mpd_set_maxlen(mpd_smart);
2700 END OBJ_IF(if_mpd_playing, INFO_MPD)
2702 #undef mpd_set_maxlen
2705 END OBJ(moc_state, INFO_MOC)
2706 END OBJ(moc_file, INFO_MOC)
2707 END OBJ(moc_title, INFO_MOC)
2708 END OBJ(moc_artist, INFO_MOC)
2709 END OBJ(moc_song, INFO_MOC)
2710 END OBJ(moc_album, INFO_MOC)
2711 END OBJ(moc_totaltime, INFO_MOC)
2712 END OBJ(moc_timeleft, INFO_MOC)
2713 END OBJ(moc_curtime, INFO_MOC)
2714 END OBJ(moc_bitrate, INFO_MOC)
2715 END OBJ(moc_rate, INFO_MOC)
2718 END OBJ(xmms2_artist, INFO_XMMS2)
2719 END OBJ(xmms2_album, INFO_XMMS2)
2720 END OBJ(xmms2_title, INFO_XMMS2)
2721 END OBJ(xmms2_genre, INFO_XMMS2)
2722 END OBJ(xmms2_comment, INFO_XMMS2)
2723 END OBJ(xmms2_url, INFO_XMMS2)
2724 END OBJ(xmms2_tracknr, INFO_XMMS2)
2725 END OBJ(xmms2_bitrate, INFO_XMMS2)
2726 END OBJ(xmms2_date, INFO_XMMS2)
2727 END OBJ(xmms2_id, INFO_XMMS2)
2728 END OBJ(xmms2_duration, INFO_XMMS2)
2729 END OBJ(xmms2_elapsed, INFO_XMMS2)
2730 END OBJ(xmms2_size, INFO_XMMS2)
2731 END OBJ(xmms2_status, INFO_XMMS2)
2732 END OBJ(xmms2_percent, INFO_XMMS2)
2734 END OBJ(xmms2_bar, INFO_XMMS2)
2736 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2738 END OBJ(xmms2_smart, INFO_XMMS2)
2739 END OBJ(xmms2_playlist, INFO_XMMS2)
2740 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2741 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2744 END OBJ(audacious_status, INFO_AUDACIOUS)
2745 END OBJ(audacious_title, INFO_AUDACIOUS)
2747 sscanf(arg, "%d", &info.audacious.max_title_len);
2748 if (info.audacious.max_title_len > 0) {
2749 info.audacious.max_title_len++;
2751 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
2754 END OBJ(audacious_length, INFO_AUDACIOUS)
2755 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2756 END OBJ(audacious_position, INFO_AUDACIOUS)
2757 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2758 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2759 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2760 END OBJ(audacious_channels, INFO_AUDACIOUS)
2761 END OBJ(audacious_filename, INFO_AUDACIOUS)
2762 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2763 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2764 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2766 END OBJ(audacious_bar, INFO_AUDACIOUS)
2768 scan_bar(arg, &obj->a, &obj->b);
2772 END OBJ(bmpx_title, INFO_BMPX)
2773 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2774 END OBJ(bmpx_artist, INFO_BMPX)
2775 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2776 END OBJ(bmpx_album, INFO_BMPX)
2777 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2778 END OBJ(bmpx_track, INFO_BMPX)
2779 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2780 END OBJ(bmpx_uri, INFO_BMPX)
2781 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2782 END OBJ(bmpx_bitrate, INFO_BMPX)
2783 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2789 char *userid = (char *) malloc(20 * sizeof(char));
2790 char *apikey = (char *) malloc(64 * sizeof(char));
2791 char *charid = (char *) malloc(20 * sizeof(char));
2793 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2794 obj->data.eve.charid = charid;
2795 obj->data.eve.userid = userid;
2796 obj->data.eve.apikey = apikey;
2800 CRIT_ERR(obj, free_at_crash, "eve needs arguments: <userid> <apikey> <characterid>");
2806 int argc, delay, act_par;
2807 unsigned int nrspaces = 0;
2808 char *uri = (char *) malloc(128 * sizeof(char));
2809 char *action = (char *) malloc(64 * sizeof(char));
2811 argc = sscanf(arg, "%127s %d %63s %d %u", uri, &delay, action,
2812 &act_par, &nrspaces);
2813 obj->data.rss.uri = uri;
2814 obj->data.rss.delay = delay;
2815 obj->data.rss.action = action;
2816 obj->data.rss.act_par = act_par;
2817 obj->data.rss.nrspaces = nrspaces;
2821 CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <delay in minutes> <action> "
2822 "[act_par] [spaces in front]");
2826 END OBJ_THREAD(weather, 0)
2829 char *locID = (char *) malloc(9 * sizeof(char));
2830 char *uri = (char *) malloc(128 * sizeof(char));
2831 char *data_type = (char *) malloc(32 * sizeof(char));
2834 argc = sscanf(arg, "%119s %8s %31s %d", uri, locID, data_type, &interval);
2836 //locID MUST BE upper-case
2839 *tmp_p = toupper(*tmp_p);
2843 //Construct complete uri
2844 if (strstr(uri, "xoap.weather.com")) {
2852 } else if (strstr(uri, "weather.noaa.gov")) {
2854 strcat(uri, ".TXT");
2855 } else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
2856 CRIT_ERR(obj, free_at_crash, \
2857 "could not recognize the weather uri");
2860 obj->data.weather.uri = uri;
2861 obj->data.weather.data_type = data_type;
2863 //Limit the data retrieval interval to half hour min
2864 if (interval < 30) {
2868 //Convert to seconds
2869 obj->data.weather.interval = interval * 60;
2872 DBGP("weather: fetching %s from %s every %d seconds", \
2873 data_type, uri, obj->data.weather.interval);
2875 CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
2881 obj->data.s = strndup(arg, text_buffer_size);
2883 CRIT_ERR(obj, free_at_crash, "lua needs arguments: <function name> [function parameters]");
2885 END OBJ(lua_parse, 0)
2887 obj->data.s = strndup(arg, text_buffer_size);
2889 CRIT_ERR(obj, free_at_crash, "lua_parse needs arguments: <function name> [function parameters]");
2894 arg = scan_bar(arg, &obj->a, &obj->b);
2896 obj->data.s = strndup(arg, text_buffer_size);
2898 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2901 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2904 END OBJ(lua_graph, 0)
2905 SIZE_DEFAULTS(graph);
2908 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2909 &obj->e, &obj->char_a, &obj->char_b);
2913 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2916 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2918 END OBJ(lua_gauge, 0)
2919 SIZE_DEFAULTS(gauge);
2921 arg = scan_gauge(arg, &obj->a, &obj->b);
2923 obj->data.s = strndup(arg, text_buffer_size);
2925 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2928 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2931 #endif /* HAVE_LUA */
2934 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2935 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2936 ERR("hddtemp needs arguments");
2937 obj->type = OBJ_text;
2938 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2939 obj->data.hddtemp.update_time = 0;
2941 obj->data.hddtemp.temp = NULL;
2942 #endif /* HDDTEMP */
2943 #ifdef TCP_PORT_MONITOR
2944 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2945 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2946 #endif /* TCP_PORT_MONITOR */
2947 END OBJ(entropy_avail, INFO_ENTROPY)
2948 END OBJ(entropy_perc, INFO_ENTROPY)
2949 END OBJ(entropy_poolsize, INFO_ENTROPY)
2950 END OBJ(entropy_bar, INFO_ENTROPY)
2952 scan_bar(arg, &obj->a, &obj->b);
2955 obj->sub = malloc(sizeof(struct text_object));
2956 extract_variable_text_internal(obj->sub, arg, 0);
2958 CRIT_ERR(obj, free_at_crash, "blink needs a argument");
2960 END OBJ(to_bytes, 0)
2962 obj->sub = malloc(sizeof(struct text_object));
2963 extract_variable_text_internal(obj->sub, arg, 0);
2965 CRIT_ERR(obj, free_at_crash, "to_bytes needs a argument");
2970 obj->data.scroll.step = 1;
2971 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2972 sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
2973 if (*(arg + n1 + n2)) {
2976 obj->data.scroll.step = 1;
2978 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2979 obj->data.scroll.start = 0;
2980 obj->sub = malloc(sizeof(struct text_object));
2981 extract_variable_text_internal(obj->sub,
2982 obj->data.scroll.text, 0);
2984 CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
2989 unsigned int indenting = 0; //vars can be used as args for other vars
2992 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
2994 for (i=0; arg[i] != 0 && j < 2; i++) {
2995 if(startvar[j] == -1) {
2999 }else if(endvar[j] == -1) {
3002 }else if(arg[i] == '}') {
3005 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
3011 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
3012 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
3013 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
3014 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
3016 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
3017 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
3019 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
3020 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
3022 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
3023 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
3025 obj->sub = malloc(sizeof(struct text_object));
3026 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
3027 obj->sub->sub = malloc(sizeof(struct text_object));
3028 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
3030 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3033 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3038 CRIT_ERR(obj, free_at_crash, "nvidia needs an argument\n");
3039 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3040 CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
3041 " specified: '%s'\n", arg);
3046 END OBJ(apcupsd, INFO_APCUPSD)
3050 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3051 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3053 info.apcupsd.port = htons(port);
3054 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3057 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3059 END OBJ(apcupsd_name, INFO_APCUPSD)
3060 END OBJ(apcupsd_model, INFO_APCUPSD)
3061 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3062 END OBJ(apcupsd_cable, INFO_APCUPSD)
3063 END OBJ(apcupsd_status, INFO_APCUPSD)
3064 END OBJ(apcupsd_linev, INFO_APCUPSD)
3065 END OBJ(apcupsd_load, INFO_APCUPSD)
3066 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3068 scan_bar(arg, &obj->a, &obj->b);
3070 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3072 SIZE_DEFAULTS(graph);
3073 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3074 &obj->e, &obj->char_a, &obj->char_b);
3076 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3077 SIZE_DEFAULTS(gauge);
3078 scan_gauge(arg, &obj->a, &obj->b);
3080 END OBJ(apcupsd_charge, INFO_APCUPSD)
3081 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3082 END OBJ(apcupsd_temp, INFO_APCUPSD)
3083 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3084 #endif /* APCUPSD */
3088 ERR("unknown variable %s", s);
3089 obj->type = OBJ_text;
3090 snprintf(buf, 256, "${%s}", s);
3091 obj->data.s = strndup(buf, text_buffer_size);
3098 static struct text_object *create_plain_text(const char *s)
3100 struct text_object *obj;
3102 if (s == NULL || *s == '\0') {
3106 obj = new_text_object_internal();
3108 obj->type = OBJ_text;
3109 obj->data.s = strndup(s, text_buffer_size);
3113 /* backslash_escape - do the actual substitution task for template objects
3115 * The field templates is used for substituting the \N occurences. Set it to
3116 * NULL to leave them as they are.
3118 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3122 unsigned int dup_idx = 0, dup_len;
3124 dup_len = strlen(src) + 1;
3125 src_dup = malloc(dup_len * sizeof(char));
3133 if (*(p + 1) == '\\') {
3134 src_dup[dup_idx++] = '\\';
3136 } else if (*(p + 1) == ' ') {
3137 src_dup[dup_idx++] = ' ';
3139 } else if (*(p + 1) == 'n') {
3140 src_dup[dup_idx++] = '\n';
3142 } else if (templates) {
3143 unsigned int tmpl_num;
3145 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3146 (tmpl_num > template_count))
3148 dup_len += strlen(templates[tmpl_num - 1]);
3149 src_dup = realloc(src_dup, dup_len * sizeof(char));
3150 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3151 dup_idx += strlen(templates[tmpl_num - 1]);
3156 src_dup[dup_idx++] = *p;
3161 src_dup[dup_idx] = '\0';
3162 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3166 /* handle_template_object - core logic of the template object
3168 * use config variables like this:
3169 * template1 = "$\1\2"
3170 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3172 * and use them like this:
3173 * ${template1 node name}
3174 * ${template2 root /}
3175 * ${template2 cdrom /mnt/cdrom}
3177 static char *handle_template(const char *tmpl, const char *args)
3179 char *args_dup = NULL;
3181 char **argsp = NULL;
3182 unsigned int argcnt = 0, template_idx, i;
3185 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3186 (template_idx >= MAX_TEMPLATES))
3190 args_dup = strdup(args);
3193 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3195 if (p > args_dup && *(p - 1) == '\\')
3198 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3204 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3205 argsp[argcnt - 1] = p_old;
3207 for (i = 0; i < argcnt; i++) {
3209 tmp = backslash_escape(argsp[i], NULL, 0);
3210 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3215 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3216 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3218 for (i = 0; i < argcnt; i++)
3224 static char *find_and_replace_templates(const char *inbuf)
3226 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3229 outlen = strlen(inbuf) + 1;
3230 o = outbuf = calloc(outlen, sizeof(char));
3231 memset(outbuf, 0, outlen * sizeof(char));
3233 p = indup = strdup(inbuf);
3235 while (*p && *p != '$')
3241 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3246 if (*(p + 1) == '{') {
3249 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3257 while (*p && stack > 0) {
3265 // stack is empty. that means the previous char was }, so we zero it
3268 // we ran into the end of string without finding a closing }, bark
3269 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
3273 while (*p && !isspace(*p))
3277 tmpl_out = handle_template(templ, args);
3279 outlen += strlen(tmpl_out);
3281 outbuf = realloc(outbuf, outlen * sizeof(char));
3282 strcat (outbuf, tmpl_out);
3284 o = outbuf + strlen(outbuf);
3286 ERR("failed to handle template '%s' with args '%s'", templ, args);
3290 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3295 static int text_contains_templates(const char *text)
3297 if (strcasestr(text, "${template") != NULL)
3299 if (strcasestr(text, "$template") != NULL)
3304 /* folds a string over top of itself, like so:
3306 * if start is "blah", and you call it with count = 1, the result will be "lah"
3308 static void strfold(char *start, int count)
3311 for (curplace = start + count; *curplace != 0; curplace++) {
3312 *(curplace - count) = *curplace;
3314 *(curplace - count) = 0;
3318 * - assumes that *string is '#'
3319 * - removes the part from '#' to the end of line ('\n' or '\0')
3320 * - it removes the '\n'
3321 * - copies the last char into 'char *last' argument, which should be a pointer
3322 * to a char rather than a string.
3324 static size_t remove_comment(char *string, char *last)
3327 while (*end != '\0' && *end != '\n') {
3330 if (last) *last = *end;
3331 if (*end == '\n') end++;
3332 strfold(string, end - string);
3333 return end - string;
3336 static size_t remove_comments(char *string)
3340 for (curplace = string; *curplace != 0; curplace++) {
3341 if (*curplace == '\\' && *(curplace + 1) == '#') {
3342 // strcpy can't be used for overlapping strings
3343 strfold(curplace, 1);
3345 } else if (*curplace == '#') {
3346 folded += remove_comment(curplace, 0);
3352 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3354 struct text_object *obj;
3355 char *p, *s, *orig_p;
3357 void *ifblock_opaque = NULL;
3362 p = strndup(const_p, max_user_text - 1);
3363 while (text_contains_templates(p)) {
3365 tmp = find_and_replace_templates(p);
3371 if (strcmp(p, const_p)) {
3372 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3374 DBGP("no templates to replace");
3377 memset(retval, 0, sizeof(struct text_object));
3379 line = global_text_lines;
3387 obj = create_plain_text(s);
3389 append_object(retval, obj);
3399 /* variable is either $foo or ${foo} */
3401 unsigned int brl = 1, brr = 0;
3405 while (*p && brl != brr) {
3420 while (*p && (isalnum((int) *p) || *p == '_')) {
3425 /* copy variable to buffer */
3426 len = (p - s > 255) ? 255 : (p - s);
3427 strncpy(buf, s, len);
3435 /* search for variable in environment */
3439 obj = create_plain_text(var);
3441 append_object(retval, obj);
3446 /* if variable wasn't found in environment, use some special */
3451 if (strchr(buf, ' ')) {
3452 arg = strchr(buf, ' ');
3455 while (isspace((int) *arg)) {
3463 /* lowercase variable name */
3466 *tmp_p = tolower(*tmp_p);
3470 obj = construct_text_object(buf, arg,
3471 line, allow_threaded,
3472 &ifblock_opaque, orig_p);
3474 append_object(retval, obj);
3478 obj = create_plain_text("$");
3481 append_object(retval, obj);
3484 } else if (*p == '\\' && *(p+1) == '#') {
3486 } else if (*p == '#') {
3488 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3489 /* if remove_comment removed a newline, we need to 'back up' with p */
3495 obj = create_plain_text(s);
3497 append_object(retval, obj);
3500 if (!ifblock_stack_empty(&ifblock_opaque)) {
3501 ERR("one or more $endif's are missing");
3508 static void extract_variable_text(const char *p)
3510 free_text_objects(&global_root_object, 0);
3524 extract_variable_text_internal(&global_root_object, p, 1);
3527 void parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3529 extract_variable_text_internal(root, txt, 0);
3530 generate_text_internal(p, max_user_text, *root, cur);
3534 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3535 void *thread(void *), const char *text)
3537 if (obj->char_b && info.mail) {
3538 // this means we use info
3539 if (!info.mail->p_timed_thread) {
3540 info.mail->p_timed_thread =
3541 timed_thread_create(thread,
3542 (void *) info.mail, info.mail->interval * 1000000);
3543 if (!info.mail->p_timed_thread) {
3544 ERR("Error creating %s timed thread", text);
3546 timed_thread_register(info.mail->p_timed_thread,
3547 &info.mail->p_timed_thread);
3548 if (timed_thread_run(info.mail->p_timed_thread)) {
3549 ERR("Error running %s timed thread", text);
3553 } else if (obj->data.mail) {
3554 // this means we use obj
3555 if (!obj->data.mail->p_timed_thread) {
3556 obj->data.mail->p_timed_thread =
3557 timed_thread_create(thread,
3558 (void *) obj->data.mail,
3559 obj->data.mail->interval * 1000000);
3560 if (!obj->data.mail->p_timed_thread) {
3561 ERR("Error creating %s timed thread", text);
3563 timed_thread_register(obj->data.mail->p_timed_thread,
3564 &obj->data.mail->p_timed_thread);
3565 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3566 ERR("Error running %s timed thread", text);
3569 return obj->data.mail;
3570 } else if (!obj->a) {
3571 // something is wrong, warn once then stop
3572 ERR("There's a problem with your mail settings. "
3573 "Check that the global mail settings are properly defined"
3574 " (line %li).", obj->line);
3580 char *format_time(unsigned long timeval, const int width)
3583 unsigned long nt; // narrow time, for speed on 32-bit
3584 unsigned cc; // centiseconds
3585 unsigned nn; // multi-purpose whatever
3588 cc = nt % 100; // centiseconds past second
3589 nt /= 100; // total seconds
3590 nn = nt % 60; // seconds past the minute
3591 nt /= 60; // total minutes
3592 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3594 return strndup(buf, text_buffer_size);
3596 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3597 return strndup(buf, text_buffer_size);
3599 nn = nt % 60; // minutes past the hour
3600 nt /= 60; // total hours
3601 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3602 return strndup(buf, text_buffer_size);
3604 nn = nt; // now also hours
3605 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3606 return strndup(buf, text_buffer_size);
3608 nn /= 24; // now days
3609 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3610 return strndup(buf, text_buffer_size);
3612 nn /= 7; // now weeks
3613 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3614 return strndup(buf, text_buffer_size);
3616 // well shoot, this outta' fit...
3617 return strndup("<inf>", text_buffer_size);
3620 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3621 //string has to end with \0 and it's length should fit in a int
3623 void remove_deleted_chars(char *string){
3625 while(string[i] != 0){
3626 if(string[i] == BACKSPACE){
3628 strcpy( &(string[i-1]), &(string[i+1]) );
3630 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3635 static inline void format_media_player_time(char *buf, const int size,
3638 int days, hours, minutes;
3640 days = seconds / (24 * 60 * 60);
3641 seconds %= (24 * 60 * 60);
3642 hours = seconds / (60 * 60);
3643 seconds %= (60 * 60);
3644 minutes = seconds / 60;
3648 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3649 hours, minutes, seconds);
3650 } else if (hours > 0) {
3651 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3654 snprintf(buf, size, "%i:%02i", minutes, seconds);
3658 static inline double get_barnum(char *buf)
3670 if (sscanf(buf, "%lf", &barnum) == 0) {
3671 ERR("reading exec value failed (perhaps it's not the "
3672 "correct format?)");
3675 if (barnum > 100.0 || barnum < 0.0) {
3676 ERR("your exec value is not between 0 and 100, "
3677 "therefore it will be ignored");
3683 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
3684 * multiline objects like $exec work with $align[rc] and friends
3686 void substitute_newlines(char *p, long l)
3690 while (p && *p && p < s + l) {
3692 /* only substitute if it's not the last newline */
3693 *p = SECRIT_MULTILINE_CHAR;
3699 static void generate_text_internal(char *p, int p_max_size,
3700 struct text_object root, struct information *cur)
3702 struct text_object *obj;
3704 int need_to_load_fonts = 0;
3707 /* for the OBJ_top* handler */
3708 struct process **needed = 0;
3711 char buff_in[p_max_size];
3713 iconv_converting = 0;
3714 #endif /* HAVE_ICONV */
3718 while (obj && p_max_size > 0) {
3719 needed = 0; /* reset for top stuff */
3721 /* IFBLOCK jumping algorithm
3723 * This is easier as it looks like:
3724 * - each IF checks it's condition
3725 * - on FALSE: call DO_JUMP
3726 * - on TRUE: don't care
3727 * - each ELSE calls DO_JUMP unconditionally
3728 * - each ENDIF is silently being ignored
3731 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3732 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3733 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3734 * regularly, "obj" is being updated to point to obj->next, so object parsing
3735 * continues right after the corresponding ELSE or ENDIF. This means that if we
3736 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3737 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3738 * jumped, and there is nothing to do.
3742 obj = obj->data.ifblock.next; \
3745 #define OBJ(a) break; case OBJ_##a:
3747 switch (obj->type) {
3749 ERR("not implemented obj type %d", obj->type);
3752 struct sockaddr_in addr;
3753 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3755 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3757 memset(&addr, 0, sizeof(addr));
3758 addr.sin_family = AF_INET;
3759 addr.sin_port = obj->data.read_tcp.port;
3760 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3761 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3765 FD_SET(sock, &readfds);
3768 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3769 received = recv(sock, p, p_max_size, 0);
3774 ERR("read_tcp: Couldn't create a connection");
3777 ERR("read_tcp: Couldn't create a socket");
3780 ERR("read_tcp: Problem with resolving the hostname");
3785 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3787 #endif /* !__OpenBSD__ */
3790 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3791 obj->data.cpu_index);
3797 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3798 obj->data.cpu_index);
3800 /* OpenBSD has no such flag (SUSv2) */
3801 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3802 obj->data.cpu_index);
3803 #endif /* __OpenBSD */
3806 #if defined(__linux__)
3809 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3810 obj->data.cpu_index);
3815 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3816 obj->data.cpu_index);
3821 OBJ(wireless_essid) {
3822 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3824 OBJ(wireless_mode) {
3825 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3827 OBJ(wireless_bitrate) {
3828 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3831 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3833 OBJ(wireless_link_qual) {
3834 spaced_print(p, p_max_size, "%d", 4,
3835 obj->data.net->link_qual);
3837 OBJ(wireless_link_qual_max) {
3838 spaced_print(p, p_max_size, "%d", 4,
3839 obj->data.net->link_qual_max);
3841 OBJ(wireless_link_qual_perc) {
3842 if (obj->data.net->link_qual_max > 0) {
3843 spaced_print(p, p_max_size, "%.0f", 5,
3844 (double) obj->data.net->link_qual /
3845 obj->data.net->link_qual_max * 100);
3847 spaced_print(p, p_max_size, "unk", 5);
3850 OBJ(wireless_link_bar) {
3852 if(output_methods & TO_X) {
3853 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3854 obj->data.net->link_qual_max) * 255.0);
3857 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3858 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3859 obj->data.net->link_qual_max) * 100.0, obj->a);
3864 #endif /* HAVE_IWLIB */
3866 #endif /* __linux__ */
3870 get_adt746x_cpu(p, p_max_size);
3873 get_adt746x_fan(p, p_max_size);
3876 get_acpi_fan(p, p_max_size);
3878 OBJ(acpiacadapter) {
3879 get_acpi_ac_adapter(p, p_max_size);
3882 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3885 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3887 OBJ(battery_percent) {
3888 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3892 if(output_methods & TO_X) {
3893 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3896 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3897 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3902 OBJ(battery_short) {
3903 get_battery_short_status(p, p_max_size, obj->data.s);
3905 #endif /* __OpenBSD__ */
3908 human_readable(cur->buffers * 1024, p, 255);
3911 human_readable(cur->cached * 1024, p, 255);
3914 if (obj->data.cpu_index > info.cpu_count) {
3915 ERR("obj->data.cpu_index %i info.cpu_count %i",
3916 obj->data.cpu_index, info.cpu_count);
3917 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
3919 percent_print(p, p_max_size,
3920 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3924 new_gauge(p, obj->a, obj->b,
3925 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3929 if(output_methods & TO_X) {
3930 new_bar(p, obj->a, obj->b,
3931 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3934 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3935 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3942 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3943 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3944 100, 1, obj->char_a, obj->char_b);
3947 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3948 obj->e, 1, obj->char_a, obj->char_b);
3951 new_fg(p, obj->data.l);
3984 OBJ(conky_version) {
3985 snprintf(p, p_max_size, "%s", VERSION);
3987 OBJ(conky_build_date) {
3988 snprintf(p, p_max_size, "%s", BUILD_DATE);
3990 OBJ(conky_build_arch) {
3991 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3993 #if defined(__linux__)
3995 snprintf(p, p_max_size, "%s",
3996 get_disk_protect_queue(obj->data.s));
3999 snprintf(p, p_max_size, "%s", i8k.version);
4002 snprintf(p, p_max_size, "%s", i8k.bios);
4005 snprintf(p, p_max_size, "%s", i8k.serial);
4010 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4011 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
4013 OBJ(i8k_left_fan_status) {
4014 int left_fan_status;
4016 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4017 if (left_fan_status == 0) {
4018 snprintf(p, p_max_size, "off");
4020 if (left_fan_status == 1) {
4021 snprintf(p, p_max_size, "low");
4023 if (left_fan_status == 2) {
4024 snprintf(p, p_max_size, "high");
4027 OBJ(i8k_right_fan_status) {
4028 int right_fan_status;
4030 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4031 if (right_fan_status == 0) {
4032 snprintf(p, p_max_size, "off");
4034 if (right_fan_status == 1) {
4035 snprintf(p, p_max_size, "low");
4037 if (right_fan_status == 2) {
4038 snprintf(p, p_max_size, "high");
4041 OBJ(i8k_left_fan_rpm) {
4042 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4044 OBJ(i8k_right_fan_rpm) {
4045 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4047 OBJ(i8k_ac_status) {
4050 sscanf(i8k.ac_status, "%d", &ac_status);
4051 if (ac_status == -1) {
4052 snprintf(p, p_max_size, "disabled (read i8k docs)");
4054 if (ac_status == 0) {
4055 snprintf(p, p_max_size, "off");
4057 if (ac_status == 1) {
4058 snprintf(p, p_max_size, "on");
4061 OBJ(i8k_buttons_status) {
4062 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4066 get_ibm_acpi_fan(p, p_max_size);
4069 get_ibm_acpi_temps();
4070 temp_print(p, p_max_size,
4071 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4074 get_ibm_acpi_volume(p, p_max_size);
4076 OBJ(ibm_brightness) {
4077 get_ibm_acpi_brightness(p, p_max_size);
4080 /* information from sony_laptop kernel module
4081 * /sys/devices/platform/sony-laptop */
4082 OBJ(sony_fanspeed) {
4083 get_sony_fanspeed(p, p_max_size);
4086 if (!cur->gw_info.count) {
4091 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4094 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4097 snprintf(p, p_max_size, "%d", get_laptop_mode());
4100 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4102 #endif /* __linux__ */
4103 #if (defined(__FreeBSD__) || defined(__linux__))
4105 if ((obj->data.ifblock.s)
4106 && (!interface_up(obj->data.ifblock.s))) {
4112 OBJ(obsd_sensors_temp) {
4113 obsd_sensors.device = sensor_device;
4114 update_obsd_sensors();
4115 temp_print(p, p_max_size,
4116 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4119 OBJ(obsd_sensors_fan) {
4120 obsd_sensors.device = sensor_device;
4121 update_obsd_sensors();
4122 snprintf(p, p_max_size, "%d",
4123 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4125 OBJ(obsd_sensors_volt) {
4126 obsd_sensors.device = sensor_device;
4127 update_obsd_sensors();
4128 snprintf(p, p_max_size, "%.2f",
4129 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4132 get_obsd_vendor(p, p_max_size);
4135 get_obsd_product(p, p_max_size);
4137 #endif /* __OpenBSD__ */
4140 new_font(p, obj->data.s);
4141 need_to_load_fonts = 1;
4144 /* TODO: move this correction from kB to kB/s elsewhere
4145 * (or get rid of it??) */
4147 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4151 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4155 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4160 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4161 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4163 OBJ(diskiograph_read) {
4164 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4165 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4167 OBJ(diskiograph_write) {
4168 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4169 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4173 human_readable(obj->data.net->recv_speed, p, 255);
4176 spaced_print(p, p_max_size, "%.1f", 8,
4177 obj->data.net->recv_speed / 1024.0);
4180 OBJ(downspeedgraph) {
4181 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4182 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4186 /* Since we see you, you're if has not jumped.
4187 * Do Ninja jump here: without leaving traces.
4188 * This is to prevent us from stale jumped flags.
4190 obj = obj->data.ifblock.next;
4194 /* harmless object, just ignore */
4198 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4199 && (obj->data.net->addr.sa_data[3] & 255) == 0
4200 && (obj->data.net->addr.sa_data[4] & 255) == 0
4201 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4202 snprintf(p, p_max_size, "No Address");
4204 snprintf(p, p_max_size, "%u.%u.%u.%u",
4205 obj->data.net->addr.sa_data[2] & 255,
4206 obj->data.net->addr.sa_data[3] & 255,
4207 obj->data.net->addr.sa_data[4] & 255,
4208 obj->data.net->addr.sa_data[5] & 255);
4211 #if defined(__linux__)
4213 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
4214 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4215 strcpy(p, obj->data.net->addrs);
4217 strcpy(p, "0.0.0.0");
4220 #endif /* __linux__ */
4221 #if defined(IMLIB2) && defined(X11)
4223 /* doesn't actually draw anything, just queues it omp. the
4224 * image will get drawn after the X event loop */
4225 cimlib_add_image(obj->data.s);
4229 evaluate(obj->data.s, p);
4232 read_exec(obj->data.s, p, text_buffer_size);
4233 remove_deleted_chars(p);
4236 struct information *tmp_info;
4237 struct text_object subroot;
4239 read_exec(obj->data.s, p, text_buffer_size);
4241 tmp_info = malloc(sizeof(struct information));
4242 memcpy(tmp_info, cur, sizeof(struct information));
4243 parse_conky_vars(&subroot, p, p, tmp_info);
4245 free_text_objects(&subroot, 1);
4252 read_exec(obj->data.s, p, text_buffer_size);
4253 barnum = get_barnum(p); /*using the same function*/
4255 if (barnum >= 0.0) {
4257 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4264 read_exec(obj->data.s, p, text_buffer_size);
4265 barnum = get_barnum(p);
4267 if (barnum >= 0.0) {
4269 if(output_methods & TO_X) {
4271 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4274 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4275 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4283 char showaslog = FALSE;
4284 char tempgrad = FALSE;
4286 char *cmd = obj->data.s;
4288 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4290 cmd += strlen(" "TEMPGRAD);
4292 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4294 cmd += strlen(" "LOGGRAPH);
4296 read_exec(cmd, p, text_buffer_size);
4297 barnum = get_barnum(p);
4300 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4301 100, 1, showaslog, tempgrad);
4306 if (current_update_time - obj->data.execi.last_update
4307 >= obj->data.execi.interval) {
4310 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4311 barnum = get_barnum(p);
4313 if (barnum >= 0.0) {
4316 obj->data.execi.last_update = current_update_time;
4319 if(output_methods & TO_X) {
4320 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4323 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4324 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4331 if (current_update_time - obj->data.execi.last_update
4332 >= obj->data.execi.interval) {
4334 char showaslog = FALSE;
4335 char tempgrad = FALSE;
4336 char *cmd = obj->data.execi.cmd;
4338 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4340 cmd += strlen(" "TEMPGRAD);
4342 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4344 cmd += strlen(" "LOGGRAPH);
4346 obj->char_a = showaslog;
4347 obj->char_b = tempgrad;
4348 read_exec(cmd, p, text_buffer_size);
4349 barnum = get_barnum(p);
4351 if (barnum >= 0.0) {
4354 obj->data.execi.last_update = current_update_time;
4356 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4359 if (current_update_time - obj->data.execi.last_update
4360 >= obj->data.execi.interval) {
4363 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4364 barnum = get_barnum(p);
4366 if (barnum >= 0.0) {
4367 obj->f = 255 * barnum / 100.0;
4369 obj->data.execi.last_update = current_update_time;
4371 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4375 if (current_update_time - obj->data.execi.last_update
4376 >= obj->data.execi.interval
4377 && obj->data.execi.interval != 0) {
4378 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4380 obj->data.execi.last_update = current_update_time;
4382 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4385 struct text_object subroot;
4386 struct information *tmp_info =
4387 malloc(sizeof(struct information));
4388 memcpy(tmp_info, cur, sizeof(struct information));
4390 if (current_update_time - obj->data.execi.last_update
4391 < obj->data.execi.interval
4392 || obj->data.execi.interval == 0) {
4393 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4395 char *output = obj->data.execi.buffer;
4396 FILE *fp = popen(obj->data.execi.cmd, "r");
4397 int length = fread(output, 1, text_buffer_size, fp);
4401 output[length] = '\0';
4402 if (length > 0 && output[length - 1] == '\n') {
4403 output[length - 1] = '\0';
4406 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4407 obj->data.execi.last_update = current_update_time;
4409 free_text_objects(&subroot, 1);
4413 if (!obj->data.texeci.p_timed_thread) {
4414 obj->data.texeci.p_timed_thread =
4415 timed_thread_create(&threaded_exec,
4416 (void *) obj, obj->data.texeci.interval * 1000000);
4417 if (!obj->data.texeci.p_timed_thread) {
4418 ERR("Error creating texeci timed thread");
4420 timed_thread_register(obj->data.texeci.p_timed_thread,
4421 &obj->data.texeci.p_timed_thread);
4422 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4423 ERR("Error running texeci timed thread");
4426 timed_thread_lock(obj->data.texeci.p_timed_thread);
4427 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4428 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4431 #endif /* HAVE_POPEN */
4433 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4435 if (mail && mail->p_timed_thread) {
4436 timed_thread_lock(mail->p_timed_thread);
4437 snprintf(p, p_max_size, "%lu", mail->unseen);
4438 timed_thread_unlock(mail->p_timed_thread);
4441 OBJ(imap_messages) {
4442 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4444 if (mail && mail->p_timed_thread) {
4445 timed_thread_lock(mail->p_timed_thread);
4446 snprintf(p, p_max_size, "%lu", mail->messages);
4447 timed_thread_unlock(mail->p_timed_thread);
4451 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4453 if (mail && mail->p_timed_thread) {
4454 timed_thread_lock(mail->p_timed_thread);
4455 snprintf(p, p_max_size, "%lu", mail->unseen);
4456 timed_thread_unlock(mail->p_timed_thread);
4460 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4462 if (mail && mail->p_timed_thread) {
4463 timed_thread_lock(mail->p_timed_thread);
4464 snprintf(p, p_max_size, "%.1f",
4465 mail->used / 1024.0 / 1024.0);
4466 timed_thread_unlock(mail->p_timed_thread);
4470 if (obj->data.fs != NULL) {
4471 if (obj->data.fs->size == 0) {
4473 if(output_methods & TO_X) {
4474 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4477 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4478 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4484 if(output_methods & TO_X) {
4485 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4486 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4487 obj->data.fs->size));
4490 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4491 new_bar_in_shell(p, p_max_size,
4492 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4500 if (obj->data.fs != NULL) {
4501 human_readable(obj->data.fs->avail, p, 255);
4505 if (obj->data.fs != NULL) {
4508 if (obj->data.fs->size) {
4509 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4512 percent_print(p, p_max_size, val);
4516 if (obj->data.fs != NULL) {
4517 human_readable(obj->data.fs->size, p, 255);
4521 if (obj->data.fs != NULL)
4522 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4525 if (obj->data.fs != NULL) {
4526 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4531 if (obj->data.fs != NULL) {
4532 if (obj->data.fs->size == 0) {
4534 if(output_methods & TO_X) {
4535 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4538 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4539 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4545 if(output_methods & TO_X) {
4546 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4547 (int) (obj->data.fsbar.fs->avail * 255 /
4548 obj->data.fs->size));
4551 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4552 new_bar_in_shell(p, p_max_size,
4553 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4561 if (obj->data.fs != NULL) {
4564 if (obj->data.fs->size) {
4565 val = obj->data.fs->free
4570 percent_print(p, p_max_size, 100 - val);
4574 float *v = info.loadavg;
4576 if (obj->data.loadavg[2]) {
4577 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4578 v[obj->data.loadavg[0] - 1],
4579 v[obj->data.loadavg[1] - 1],
4580 v[obj->data.loadavg[2] - 1]);
4581 } else if (obj->data.loadavg[1]) {
4582 snprintf(p, p_max_size, "%.2f %.2f",
4583 v[obj->data.loadavg[0] - 1],
4584 v[obj->data.loadavg[1] - 1]);
4585 } else if (obj->data.loadavg[0]) {
4586 snprintf(p, p_max_size, "%.2f",
4587 v[obj->data.loadavg[0] - 1]);
4591 new_goto(p, obj->data.i);
4594 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4598 new_hr(p, obj->data.i);
4602 if (cur->nameserver_info.nscount > obj->data.i)
4603 snprintf(p, p_max_size, "%s",
4604 cur->nameserver_info.ns_list[obj->data.i]);
4608 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4609 snprintf(p, p_max_size, "%s", skill);
4614 PRSS *data = get_rss_info(obj->data.rss.uri,
4615 obj->data.rss.delay);
4619 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4621 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4623 // remove trailing new line if one exists
4624 if (str[strlen(str) - 1] == '\n') {
4625 str[strlen(str) - 1] = 0;
4627 snprintf(p, p_max_size, "%s", str);
4628 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4629 if (obj->data.rss.act_par < data->item_count) {
4630 str = data->items[obj->data.rss.act_par].title;
4631 // remove trailing new line if one exists
4632 if (str[strlen(str) - 1] == '\n') {
4633 str[strlen(str) - 1] = 0;
4635 snprintf(p, p_max_size, "%s", str);
4637 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4638 if (obj->data.rss.act_par < data->item_count) {
4640 data->items[obj->data.rss.act_par].description;
4641 // remove trailing new line if one exists
4642 if (str[strlen(str) - 1] == '\n') {
4643 str[strlen(str) - 1] = 0;
4645 snprintf(p, p_max_size, "%s", str);
4647 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4648 if (data->item_count > 0) {
4651 //'tmpspaces' is a string with spaces too be placed in front of each title
4652 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4653 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4654 tmpspaces[obj->data.rss.nrspaces]=0;
4658 if (obj->data.rss.act_par > data->item_count) {
4659 show = data->item_count;
4661 show = obj->data.rss.act_par;
4663 for (itmp = 0; itmp < show; itmp++) {
4664 PRSS_Item *item = &data->items[itmp];
4668 // don't add new line before first item
4670 strncat(p, "\n", p_max_size);
4672 /* remove trailing new line if one exists,
4673 * we have our own */
4674 if (str[strlen(str) - 1] == '\n') {
4675 str[strlen(str) - 1] = 0;
4677 strncat(p, tmpspaces, p_max_size);
4678 strncat(p, str, p_max_size);
4689 if( obj->data.weather.uri != NULL ) {
4690 process_weather_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4692 strncpy(p, "either invalid xoap keys file or compiled without xoap support", p_max_size);
4698 char *str = llua_getstring(obj->data.s);
4700 snprintf(p, p_max_size, "%s", str);
4705 char *str = llua_getstring(obj->data.s);
4712 if (llua_getnumber(obj->data.s, &per)) {
4714 if(output_methods & TO_X) {
4715 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4718 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4719 new_bar_in_shell(p, p_max_size, per, obj->a);
4728 if (llua_getnumber(obj->data.s, &per)) {
4729 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4730 per, obj->e, 1, obj->char_a, obj->char_b);
4735 if (llua_getnumber(obj->data.s, &per)) {
4736 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4740 #endif /* HAVE_LUA */
4745 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4746 if (obj->data.hddtemp.temp)
4747 free(obj->data.hddtemp.temp);
4748 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4749 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4750 obj->data.hddtemp.update_time = current_update_time;
4752 if (!obj->data.hddtemp.temp) {
4753 snprintf(p, p_max_size, "N/A");
4755 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4756 unit = obj->data.hddtemp.temp[0];
4758 if (*endptr != '\0')
4759 snprintf(p, p_max_size, "N/A");
4760 else if (unit == 'C')
4761 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4762 else if (unit == 'F')
4763 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4765 snprintf(p, p_max_size, "N/A");
4770 new_offset(p, obj->data.i);
4773 new_voffset(p, obj->data.i);
4779 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4780 obj->data.sysfs.devtype, obj->data.sysfs.type);
4782 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4784 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4785 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4786 } else if (r >= 100.0 || r == 0) {
4787 snprintf(p, p_max_size, "%d", (int) r);
4789 snprintf(p, p_max_size, "%.1f", r);
4795 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4796 obj->data.sysfs.devtype, obj->data.sysfs.type);
4798 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4800 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4801 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4802 } else if (r >= 100.0 || r == 0) {
4803 snprintf(p, p_max_size, "%d", (int) r);
4805 snprintf(p, p_max_size, "%.1f", r);
4811 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4812 obj->data.sysfs.devtype, obj->data.sysfs.type);
4814 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4816 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4817 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4818 } else if (r >= 100.0 || r == 0) {
4819 snprintf(p, p_max_size, "%d", (int) r);
4821 snprintf(p, p_max_size, "%.1f", r);
4824 #endif /* __linux__ */
4826 new_alignr(p, obj->data.i);
4829 new_alignc(p, obj->data.i);
4832 char buf[max_user_text];
4833 struct information *tmp_info =
4834 malloc(sizeof(struct information));
4835 memcpy(tmp_info, cur, sizeof(struct information));
4836 generate_text_internal(buf, max_user_text,
4837 *obj->sub, tmp_info);
4839 if (strlen(buf) != 0) {
4845 char expression[max_user_text];
4847 struct information *tmp_info;
4849 tmp_info = malloc(sizeof(struct information));
4850 memcpy(tmp_info, cur, sizeof(struct information));
4851 generate_text_internal(expression, max_user_text,
4852 *obj->sub, tmp_info);
4853 DBGP("parsed arg into '%s'", expression);
4855 val = compare(expression);
4857 ERR("compare failed for expression '%s'",
4865 if (obj->data.ifblock.str
4866 && !check_contains(obj->data.ifblock.s,
4867 obj->data.ifblock.str)) {
4869 } else if (obj->data.ifblock.s
4870 && access(obj->data.ifblock.s, F_OK)) {
4875 if ((obj->data.ifblock.s)
4876 && (!check_mount(obj->data.ifblock.s))) {
4882 if (!get_process_by_name(obj->data.ifblock.s)) {
4884 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4889 #if defined(__linux__)
4891 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4895 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4898 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4903 human_readable(cur->mem * 1024, p, 255);
4906 human_readable(cur->memeasyfree * 1024, p, 255);
4909 human_readable(cur->memfree * 1024, p, 255);
4912 human_readable(cur->memmax * 1024, p, 255);
4916 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4920 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4921 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4926 if(output_methods & TO_X) {
4927 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4928 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4931 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4932 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4939 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4940 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4941 100, 1, obj->char_a, obj->char_b);
4946 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4949 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4952 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4956 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4957 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4960 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4961 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4964 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4965 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4968 OBJ(if_mixer_mute) {
4969 if (!mixer_is_mute(obj->data.ifblock.i)) {
4974 #define NOT_IN_X "Not running in X"
4976 if(x_initialised != YES) {
4977 strncpy(p, NOT_IN_X, p_max_size);
4979 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4982 OBJ(monitor_number) {
4983 if(x_initialised != YES) {
4984 strncpy(p, NOT_IN_X, p_max_size);
4986 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4990 if(x_initialised != YES) {
4991 strncpy(p, NOT_IN_X, p_max_size);
4993 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
4996 OBJ(desktop_number) {
4997 if(x_initialised != YES) {
4998 strncpy(p, NOT_IN_X, p_max_size);
5000 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
5004 if(x_initialised != YES) {
5005 strncpy(p, NOT_IN_X, p_max_size);
5006 }else if(cur->x11.desktop.name != NULL) {
5007 strncpy(p, cur->x11.desktop.name, p_max_size);
5014 update_mail_count(&obj->data.local_mail);
5015 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5018 update_mail_count(&obj->data.local_mail);
5019 snprintf(p, p_max_size, "%d",
5020 obj->data.local_mail.new_mail_count);
5023 update_mail_count(&obj->data.local_mail);
5024 snprintf(p, p_max_size, "%d",
5025 obj->data.local_mail.seen_mail_count);
5028 update_mail_count(&obj->data.local_mail);
5029 snprintf(p, p_max_size, "%d",
5030 obj->data.local_mail.unseen_mail_count);
5032 OBJ(flagged_mails) {
5033 update_mail_count(&obj->data.local_mail);
5034 snprintf(p, p_max_size, "%d",
5035 obj->data.local_mail.flagged_mail_count);
5037 OBJ(unflagged_mails) {
5038 update_mail_count(&obj->data.local_mail);
5039 snprintf(p, p_max_size, "%d",
5040 obj->data.local_mail.unflagged_mail_count);
5042 OBJ(forwarded_mails) {
5043 update_mail_count(&obj->data.local_mail);
5044 snprintf(p, p_max_size, "%d",
5045 obj->data.local_mail.forwarded_mail_count);
5047 OBJ(unforwarded_mails) {
5048 update_mail_count(&obj->data.local_mail);
5049 snprintf(p, p_max_size, "%d",
5050 obj->data.local_mail.unforwarded_mail_count);
5052 OBJ(replied_mails) {
5053 update_mail_count(&obj->data.local_mail);
5054 snprintf(p, p_max_size, "%d",
5055 obj->data.local_mail.replied_mail_count);
5057 OBJ(unreplied_mails) {
5058 update_mail_count(&obj->data.local_mail);
5059 snprintf(p, p_max_size, "%d",
5060 obj->data.local_mail.unreplied_mail_count);
5063 update_mail_count(&obj->data.local_mail);
5064 snprintf(p, p_max_size, "%d",
5065 obj->data.local_mail.draft_mail_count);
5067 OBJ(trashed_mails) {
5068 update_mail_count(&obj->data.local_mail);
5069 snprintf(p, p_max_size, "%d",
5070 obj->data.local_mail.trashed_mail_count);
5073 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5075 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5078 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5081 new_outline(p, obj->data.l);
5084 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5086 OBJ(running_processes) {
5087 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5090 snprintf(p, p_max_size, "%s", obj->data.s);
5094 new_bg(p, obj->data.l);
5097 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5101 human_readable(cur->swap * 1024, p, 255);
5104 human_readable(cur->swapfree * 1024, p, 255);
5107 human_readable(cur->swapmax * 1024, p, 255);
5110 if (cur->swapmax == 0) {
5111 strncpy(p, "No swap", p_max_size);
5113 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5118 if(output_methods & TO_X) {
5119 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5120 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5123 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5124 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5130 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5133 time_t t = time(NULL);
5134 struct tm *tm = localtime(&t);
5136 setlocale(LC_TIME, "");
5137 strftime(p, p_max_size, obj->data.s, tm);
5140 time_t t = time(NULL);
5141 struct tm *tm = gmtime(&t);
5143 strftime(p, p_max_size, obj->data.s, tm);
5150 if (obj->data.tztime.tz) {
5151 oldTZ = getenv("TZ");
5152 setenv("TZ", obj->data.tztime.tz, 1);
5158 setlocale(LC_TIME, "");
5159 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5161 setenv("TZ", oldTZ, 1);
5166 // Needless to free oldTZ since getenv gives ptr to static data
5169 human_readable(obj->data.net->recv, p, 255);
5172 human_readable(obj->data.net->trans, p, 255);
5175 snprintf(p, p_max_size, "%d", total_updates);
5178 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5183 human_readable(obj->data.net->trans_speed, p, 255);
5186 spaced_print(p, p_max_size, "%.1f", 8,
5187 obj->data.net->trans_speed / 1024.0);
5191 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5192 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5196 format_seconds_short(p, p_max_size, (int) cur->uptime);
5199 format_seconds(p, p_max_size, (int) cur->uptime);
5202 snprintf(p, p_max_size, "%s", cur->users.names);
5205 snprintf(p, p_max_size, "%s", cur->users.terms);
5208 snprintf(p, p_max_size, "%s", cur->users.times);
5211 snprintf(p, p_max_size, "%d", cur->users.number);
5213 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5214 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5218 msg = get_apm_adapter();
5219 snprintf(p, p_max_size, "%s", msg);
5222 OBJ(apm_battery_life) {
5225 msg = get_apm_battery_life();
5226 snprintf(p, p_max_size, "%s", msg);
5229 OBJ(apm_battery_time) {
5232 msg = get_apm_battery_time();
5233 snprintf(p, p_max_size, "%s", msg);
5236 #endif /* __FreeBSD__ __OpenBSD__ */
5239 #define mpd_printf(fmt, val) \
5240 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5241 #define mpd_sprintf(val) { \
5242 if (!obj->data.i || obj->data.i > p_max_size) \
5243 mpd_printf("%s", val); \
5245 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5250 mpd_sprintf(artist);
5254 mpd_printf("%s", random);
5256 mpd_printf("%s", repeat);
5264 mpd_printf("%d", volume);
5266 mpd_printf("%d", bitrate);
5268 mpd_printf("%s", status);
5270 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5273 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5276 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5280 if(output_methods & TO_X) {
5281 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5282 (int) (mpd_get_info()->progress * 255.0f));
5285 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5286 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5292 struct mpd_s *mpd = mpd_get_info();
5293 int len = obj->data.i;
5294 if (len == 0 || len > p_max_size)
5297 memset(p, 0, p_max_size);
5298 if (mpd->artist && *mpd->artist &&
5299 mpd->title && *mpd->title) {
5300 snprintf(p, len, "%s - %s", mpd->artist,
5302 } else if (mpd->title && *mpd->title) {
5303 snprintf(p, len, "%s", mpd->title);
5304 } else if (mpd->artist && *mpd->artist) {
5305 snprintf(p, len, "%s", mpd->artist);
5306 } else if (mpd->file && *mpd->file) {
5307 snprintf(p, len, "%s", mpd->file);
5312 OBJ(if_mpd_playing) {
5313 if (!mpd_get_info()->is_playing) {
5322 #define MOC_PRINT(t, a) \
5323 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5325 MOC_PRINT(state, "??");
5328 MOC_PRINT(file, "no file");
5331 MOC_PRINT(title, "no title");
5334 MOC_PRINT(artist, "no artist");
5337 MOC_PRINT(song, "no song");
5340 MOC_PRINT(album, "no album");
5342 OBJ(moc_totaltime) {
5343 MOC_PRINT(totaltime, "0:00");
5346 MOC_PRINT(timeleft, "0:00");
5349 MOC_PRINT(curtime, "0:00");
5352 MOC_PRINT(bitrate, "0Kbps");
5355 MOC_PRINT(rate, "0KHz");
5361 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5364 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5367 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5370 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5372 OBJ(xmms2_comment) {
5373 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5376 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5379 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5382 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5384 OBJ(xmms2_tracknr) {
5385 if (cur->xmms2.tracknr != -1) {
5386 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5389 OBJ(xmms2_bitrate) {
5390 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5393 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5396 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5398 OBJ(xmms2_elapsed) {
5399 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5400 (cur->xmms2.elapsed / 1000) % 60);
5402 OBJ(xmms2_duration) {
5403 snprintf(p, p_max_size, "%02d:%02d",
5404 cur->xmms2.duration / 60000,
5405 (cur->xmms2.duration / 1000) % 60);
5407 OBJ(xmms2_percent) {
5408 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5412 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5413 (int) (cur->xmms2.progress * 255.0f));
5416 OBJ(xmms2_playlist) {
5417 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5419 OBJ(xmms2_timesplayed) {
5420 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5423 if (strlen(cur->xmms2.title) < 2
5424 && strlen(cur->xmms2.title) < 2) {
5425 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5427 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5431 OBJ(if_xmms2_connected) {
5432 if (cur->xmms2.conn_state != 1) {
5438 OBJ(audacious_status) {
5439 snprintf(p, p_max_size, "%s",
5440 cur->audacious.items[AUDACIOUS_STATUS]);
5442 OBJ(audacious_title) {
5443 snprintf(p, cur->audacious.max_title_len > 0
5444 ? cur->audacious.max_title_len : p_max_size, "%s",
5445 cur->audacious.items[AUDACIOUS_TITLE]);
5447 OBJ(audacious_length) {
5448 snprintf(p, p_max_size, "%s",
5449 cur->audacious.items[AUDACIOUS_LENGTH]);
5451 OBJ(audacious_length_seconds) {
5452 snprintf(p, p_max_size, "%s",
5453 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5455 OBJ(audacious_position) {
5456 snprintf(p, p_max_size, "%s",
5457 cur->audacious.items[AUDACIOUS_POSITION]);
5459 OBJ(audacious_position_seconds) {
5460 snprintf(p, p_max_size, "%s",
5461 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5463 OBJ(audacious_bitrate) {
5464 snprintf(p, p_max_size, "%s",
5465 cur->audacious.items[AUDACIOUS_BITRATE]);
5467 OBJ(audacious_frequency) {
5468 snprintf(p, p_max_size, "%s",
5469 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5471 OBJ(audacious_channels) {
5472 snprintf(p, p_max_size, "%s",
5473 cur->audacious.items[AUDACIOUS_CHANNELS]);
5475 OBJ(audacious_filename) {
5476 snprintf(p, p_max_size, "%s",
5477 cur->audacious.items[AUDACIOUS_FILENAME]);
5479 OBJ(audacious_playlist_length) {
5480 snprintf(p, p_max_size, "%s",
5481 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5483 OBJ(audacious_playlist_position) {
5484 snprintf(p, p_max_size, "%s",
5485 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5487 OBJ(audacious_main_volume) {
5488 snprintf(p, p_max_size, "%s",
5489 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5492 OBJ(audacious_bar) {
5496 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5497 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5498 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5501 #endif /* AUDACIOUS */
5505 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5508 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5511 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5514 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5517 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5520 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5523 /* we have four different types of top (top, top_mem,
5524 * top_time and top_io). To avoid having almost-same code four
5525 * times, we have this special handler. */
5528 parse_top_args("top", obj->data.top.s, obj);
5529 if (!needed) needed = cur->cpu;
5531 parse_top_args("top_mem", obj->data.top.s, obj);
5532 if (!needed) needed = cur->memu;
5534 parse_top_args("top_time", obj->data.top.s, obj);
5535 if (!needed) needed = cur->time;
5538 parse_top_args("top_io", obj->data.top.s, obj);
5539 if (!needed) needed = cur->io;
5542 if (needed[obj->data.top.num]) {
5545 switch (obj->data.top.type) {
5547 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5548 needed[obj->data.top.num]->name);
5551 snprintf(p, 7, "%6.2f",
5552 needed[obj->data.top.num]->amount);
5555 snprintf(p, 6, "%5i",
5556 needed[obj->data.top.num]->pid);
5559 snprintf(p, 7, "%6.2f",
5560 needed[obj->data.top.num]->totalmem);
5563 timeval = format_time(
5564 needed[obj->data.top.num]->total_cpu_time, 9);
5565 snprintf(p, 10, "%9s", timeval);
5569 human_readable(needed[obj->data.top.num]->rss,
5573 human_readable(needed[obj->data.top.num]->vsize,
5577 case TOP_READ_BYTES:
5578 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5581 case TOP_WRITE_BYTES:
5582 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5586 snprintf(p, 7, "%6.2f",
5587 needed[obj->data.top.num]->io_perc);
5593 print_tail_object(obj, p, p_max_size);
5595 print_head_object(obj, p, p_max_size);
5597 FILE *fp = open_file(obj->data.s, &obj->a);
5600 /* FIXME: use something more general (see also tail.c, head.c */
5601 #define BUFSZ 0x1000
5606 while(fgets(buf, BUFSZ, fp) != NULL){
5607 for(j = 0; buf[j] != 0; j++) {
5608 if(buf[j] == '\n') {
5613 sprintf(p, "%d", lines);
5616 sprintf(p, "File Unreadable");
5621 FILE *fp = open_file(obj->data.s, &obj->a);
5626 char inword = FALSE;
5629 while(fgets(buf, BUFSZ, fp) != NULL){
5630 for(j = 0; buf[j] != 0; j++) {
5631 if(!isspace(buf[j])) {
5632 if(inword == FALSE) {
5641 sprintf(p, "%d", words);
5644 sprintf(p, "File Unreadable");
5647 #ifdef TCP_PORT_MONITOR
5649 tcp_portmon_action(p, p_max_size,
5650 &obj->data.tcp_port_monitor);
5652 #endif /* TCP_PORT_MONITOR */
5656 iconv_converting = 1;
5657 iconv_selected = obj->a;
5660 iconv_converting = 0;
5663 #endif /* HAVE_ICONV */
5665 OBJ(entropy_avail) {
5666 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5669 percent_print(p, p_max_size,
5670 cur->entropy.entropy_avail *
5671 100 / cur->entropy.poolsize);
5673 OBJ(entropy_poolsize) {
5674 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5677 double entropy_perc;
5679 entropy_perc = (double) cur->entropy.entropy_avail /
5680 (double) cur->entropy.poolsize;
5682 if(output_methods & TO_X) {
5683 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5686 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5687 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5696 s = smapi_get_val(obj->data.s);
5697 snprintf(p, p_max_size, "%s", s);
5701 OBJ(if_smapi_bat_installed) {
5703 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5704 if(!smapi_bat_installed(idx)) {
5708 ERR("argument to if_smapi_bat_installed must be an integer");
5710 OBJ(smapi_bat_perc) {
5712 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5713 val = smapi_bat_installed(idx) ?
5714 smapi_get_bat_int(idx, "remaining_percent") : 0;
5715 percent_print(p, p_max_size, val);
5717 ERR("argument to smapi_bat_perc must be an integer");
5719 OBJ(smapi_bat_temp) {
5721 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5722 val = smapi_bat_installed(idx) ?
5723 smapi_get_bat_int(idx, "temperature") : 0;
5724 /* temperature is in milli degree celsius */
5725 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5727 ERR("argument to smapi_bat_temp must be an integer");
5729 OBJ(smapi_bat_power) {
5731 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5732 val = smapi_bat_installed(idx) ?
5733 smapi_get_bat_int(idx, "power_now") : 0;
5734 /* power_now is in mW, set to W with one digit precision */
5735 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5737 ERR("argument to smapi_bat_power must be an integer");
5740 OBJ(smapi_bat_bar) {
5741 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5742 new_bar(p, obj->a, obj->b, (int)
5743 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5745 new_bar(p, obj->a, obj->b, 0);
5750 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5751 char buf[max_user_text];
5754 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5755 snprintf(p, p_max_size, "%s", buf);
5756 if(total_updates % 2) {
5757 for(j=0; p[j] != 0; j++) {
5763 char buf[max_user_text];
5765 char unit[16]; // 16 because we can also have long names (like mega-bytes)
5767 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5768 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5769 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5770 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5771 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5772 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5773 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5775 snprintf(p, p_max_size, "%s", buf);
5779 char *tmp, buf[max_user_text];
5780 generate_text_internal(buf, max_user_text,
5783 if (strlen(buf) <= obj->data.scroll.show) {
5784 snprintf(p, p_max_size, "%s", buf);
5787 #define LINESEPARATOR '|'
5788 //place all the lines behind each other with LINESEPARATOR between them
5789 for(j = 0; buf[j] != 0; j++) {
5791 buf[j]=LINESEPARATOR;
5794 //scroll the output obj->data.scroll.start places by copying that many chars from
5795 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5796 //at the back of the string
5797 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5798 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5799 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5800 buf[j - obj->data.scroll.start] = buf[j];
5802 strcpy(&buf[j - obj->data.scroll.start], tmp);
5804 //only show the requested number of chars
5805 if(obj->data.scroll.show < j) {
5806 buf[obj->data.scroll.show] = 0;
5808 //next time, scroll a place more or reset scrolling if we are at the end
5809 obj->data.scroll.start += obj->data.scroll.step;
5810 if(obj->data.scroll.start >= j){
5811 obj->data.scroll.start = 0;
5813 snprintf(p, p_max_size, "%s", buf);
5816 char buf[2][max_user_text];
5823 struct llrows* next;
5825 struct llrows *ll_rows[2], *current[2];
5826 struct text_object * objsub = obj->sub;
5829 for(i=0; i<2; i++) {
5832 ll_rows[i] = malloc(sizeof(struct llrows));
5833 current[i] = ll_rows[i];
5834 for(j=0; j<i; j++) objsub = objsub->sub;
5835 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5836 for(j=0; buf[i][j] != 0; j++) {
5837 if(buf[i][j] == '\t') buf[i][j] = ' ';
5838 if(buf[i][j] == '\n') {
5840 current[i]->row = strdup(buf[i]+nextstart);
5841 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5842 current[i]->next = malloc(sizeof(struct llrows));
5843 current[i] = current[i]->next;
5848 current[i]->row = strdup(buf[i]+nextstart);
5849 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5850 current[i]->next = NULL;
5851 current[i] = ll_rows[i];
5853 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5855 strcat(p, current[0]->row);
5856 i=strlen(current[0]->row);
5858 while(i < longest) {
5863 strcat(p, obj->data.combine.seperation);
5864 strcat(p, current[1]->row);
5868 #pragma omp parallel for schedule(dynamic,10)
5869 #endif /* HAVE_OPENMP */
5870 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5873 #pragma omp parallel for schedule(dynamic,10)
5874 #endif /* HAVE_OPENMP */
5875 for(i=0; i<2; i++) {
5876 while(ll_rows[i] != NULL) {
5877 current[i]=ll_rows[i];
5878 free(current[i]->row);
5879 ll_rows[i]=current[i]->next;
5886 int value = get_nvidia_value(obj->data.nvidia.type, display);
5888 snprintf(p, p_max_size, "N/A");
5889 else if (obj->data.nvidia.type == NV_TEMP)
5890 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5891 else if (obj->data.nvidia.print_as_float &&
5892 value > 0 && value < 100)
5893 snprintf(p, p_max_size, "%.1f", (float)value);
5895 snprintf(p, p_max_size, "%d", value);
5900 /* This is just a meta-object to set host:port */
5903 snprintf(p, p_max_size, "%s",
5904 cur->apcupsd.items[APCUPSD_NAME]);
5906 OBJ(apcupsd_model) {
5907 snprintf(p, p_max_size, "%s",
5908 cur->apcupsd.items[APCUPSD_MODEL]);
5910 OBJ(apcupsd_upsmode) {
5911 snprintf(p, p_max_size, "%s",
5912 cur->apcupsd.items[APCUPSD_UPSMODE]);
5914 OBJ(apcupsd_cable) {
5915 snprintf(p, p_max_size, "%s",
5916 cur->apcupsd.items[APCUPSD_CABLE]);
5918 OBJ(apcupsd_status) {
5919 snprintf(p, p_max_size, "%s",
5920 cur->apcupsd.items[APCUPSD_STATUS]);
5922 OBJ(apcupsd_linev) {
5923 snprintf(p, p_max_size, "%s",
5924 cur->apcupsd.items[APCUPSD_LINEV]);
5927 snprintf(p, p_max_size, "%s",
5928 cur->apcupsd.items[APCUPSD_LOAD]);
5930 OBJ(apcupsd_loadbar) {
5933 if(output_methods & TO_X) {
5934 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5935 new_bar(p, obj->a, obj->b, (int) progress);
5938 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5939 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5940 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5946 OBJ(apcupsd_loadgraph) {
5948 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5949 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5950 (int)progress, 100, 1, obj->char_a, obj->char_b);
5952 OBJ(apcupsd_loadgauge) {
5954 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5955 new_gauge(p, obj->a, obj->b,
5959 OBJ(apcupsd_charge) {
5960 snprintf(p, p_max_size, "%s",
5961 cur->apcupsd.items[APCUPSD_CHARGE]);
5963 OBJ(apcupsd_timeleft) {
5964 snprintf(p, p_max_size, "%s",
5965 cur->apcupsd.items[APCUPSD_TIMELEFT]);
5968 snprintf(p, p_max_size, "%s",
5969 cur->apcupsd.items[APCUPSD_TEMP]);
5971 OBJ(apcupsd_lastxfer) {
5972 snprintf(p, p_max_size, "%s",
5973 cur->apcupsd.items[APCUPSD_LASTXFER]);
5975 #endif /* APCUPSD */
5982 unsigned int a = strlen(p);
5985 if (a > 0 && iconv_converting && iconv_selected > 0
5986 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5988 size_t dummy1, dummy2;
5990 const char *ptr = buff_in;
5992 char *ptr = buff_in;
5996 dummy1 = dummy2 = a;
5998 strncpy(buff_in, p, p_max_size);
6000 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6001 while (dummy1 > 0) {
6002 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6005 ERR("Iconv codeset conversion failed");
6010 /* It is nessecary when we are converting from multibyte to
6011 * singlebyte codepage */
6014 #endif /* HAVE_ICONV */
6015 if (obj->type != OBJ_text) {
6016 substitute_newlines(p, a - 2);
6024 /* load any new fonts we may have had */
6025 if (need_to_load_fonts) {
6031 void evaluate(char *text, char *buffer)
6033 struct information *tmp_info;
6034 struct text_object subroot;
6036 tmp_info = malloc(sizeof(struct information));
6037 memcpy(tmp_info, &info, sizeof(struct information));
6038 parse_conky_vars(&subroot, text, buffer, tmp_info);
6039 DBGP("evaluated '%s' to '%s'", text, buffer);
6041 free_text_objects(&subroot, 1);
6045 double current_update_time, next_update_time, last_update_time;
6047 static void generate_text(void)
6049 struct information *cur = &info;
6056 current_update_time = get_time();
6060 /* add things to the buffer */
6066 generate_text_internal(p, max_user_text, global_root_object, cur);
6068 if (stuff_in_uppercase) {
6071 tmp_p = text_buffer;
6073 *tmp_p = toupper(*tmp_p);
6078 next_update_time += update_interval;
6079 if (next_update_time < get_time()) {
6080 next_update_time = get_time() + update_interval;
6081 } else if (next_update_time > get_time() + update_interval) {
6082 next_update_time = get_time() + update_interval;
6084 last_update_time = current_update_time;
6088 static inline int get_string_width(const char *s)
6091 if (output_methods & TO_X) {
6092 return *s ? calc_text_width(s, strlen(s)) : 0;
6098 static int get_string_width_special(char *s, int special_index)
6106 if ((output_methods & TO_X) == 0) {
6108 return (s) ? strlen(s) : 0;
6116 p = strndup(s, text_buffer_size);
6120 if (*p == SPECIAL_CHAR) {
6121 /* shift everything over by 1 so that the special char
6122 * doesn't mess up the size calculation */
6123 for (i = 0; i < (long)strlen(p); i++) {
6124 *(p + i) = *(p + i + 1);
6126 if (specials[special_index + idx].type == GRAPH
6127 || specials[special_index + idx].type == GAUGE
6128 || specials[special_index + idx].type == BAR) {
6129 width += specials[special_index + idx].width;
6132 } else if (*p == SECRIT_MULTILINE_CHAR) {
6139 if (strlen(final) > 1) {
6140 width += calc_text_width(final, strlen(final));
6148 static int text_size_updater(char *s, int special_index);
6150 int last_font_height;
6151 static void update_text_area(void)
6155 if ((output_methods & TO_X) == 0)
6157 /* update text size if it isn't fixed */
6162 text_width = minimum_width;
6164 last_font_height = font_height();
6165 for_each_line(text_buffer, text_size_updater);
6167 if (text_height < minimum_height) {
6168 text_height = minimum_height;
6170 if (text_width > maximum_width && maximum_width > 0) {
6171 text_width = maximum_width;
6175 /* get text position on workarea */
6176 switch (text_alignment) {
6183 x = workarea[2] - text_width - gap_x;
6188 x = workarea[2] / 2 - text_width / 2 - gap_x;
6195 y = workarea[3] - text_height - gap_y;
6199 x = workarea[2] - text_width - gap_x;
6200 y = workarea[3] - text_height - gap_y;
6204 x = workarea[2] / 2 - text_width / 2 - gap_x;
6205 y = workarea[3] - text_height - gap_y;
6210 y = workarea[3] / 2 - text_height / 2 - gap_y;
6214 x = workarea[2] - text_width - gap_x;
6215 y = workarea[3] / 2 - text_height / 2 - gap_y;
6219 case NONE: // Let the WM manage the window
6230 if (own_window && !fixed_pos) {
6233 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6234 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6235 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6236 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6240 /* If window size doesn't match to workarea's size,
6241 * then window probably includes panels (gnome).
6242 * Blah, doesn't work on KDE. */
6243 if (workarea[2] != window.width || workarea[3] != window.height) {
6252 /* update lua window globals */
6253 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6254 #endif /* HAVE_LUA */
6259 static int cur_x, cur_y; /* current x and y for drawing */
6261 //draw_mode also without X11 because we only need to print to stdout with FG
6262 static int draw_mode; /* FG, BG or OUTLINE */
6264 static long current_color;
6266 static int text_size_updater(char *s, int special_index)
6271 if ((output_methods & TO_X) == 0)
6273 /* get string widths and skip specials */
6276 if (*p == SPECIAL_CHAR) {
6278 w += get_string_width(s);
6281 if (specials[special_index].type == BAR
6282 || specials[special_index].type == GAUGE
6283 || specials[special_index].type == GRAPH) {
6284 w += specials[special_index].width;
6285 if (specials[special_index].height > last_font_height) {
6286 last_font_height = specials[special_index].height;
6287 last_font_height += font_height();
6289 } else if (specials[special_index].type == OFFSET) {
6290 if (specials[special_index].arg > 0) {
6291 w += specials[special_index].arg;
6293 } else if (specials[special_index].type == VOFFSET) {
6294 last_font_height += specials[special_index].arg;
6295 } else if (specials[special_index].type == GOTO) {
6296 if (specials[special_index].arg > cur_x) {
6297 w = (int) specials[special_index].arg;
6299 } else if (specials[special_index].type == TAB) {
6300 int start = specials[special_index].arg;
6301 int step = specials[special_index].width;
6303 if (!step || step < 0) {
6306 w += step - (cur_x - text_start_x - start) % step;
6307 } else if (specials[special_index].type == FONT) {
6308 selected_font = specials[special_index].font_added;
6309 if (font_height() > last_font_height) {
6310 last_font_height = font_height();
6316 } else if (*p == SECRIT_MULTILINE_CHAR) {
6319 lw = get_string_width(s);
6320 *p = SECRIT_MULTILINE_CHAR;
6322 w = lw > w ? lw : w;
6323 text_height += last_font_height;
6327 w += get_string_width(s);
6328 if (w > text_width) {
6331 if (text_width > maximum_width && maximum_width) {
6332 text_width = maximum_width;
6335 text_height += last_font_height;
6336 last_font_height = font_height();
6337 return special_index;
6340 static inline void set_foreground_color(long c)
6342 if ((output_methods & TO_X) == 0)
6345 XSetForeground(display, window.gc, c);
6349 static void draw_string(const char *s)
6351 int i, i2, pos, width_of_s;
6359 width_of_s = get_string_width(s);
6360 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6362 fflush(stdout); /* output immediately, don't buffer */
6364 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6365 fprintf(stderr, "%s\n", s);
6366 fflush(stderr); /* output immediately, don't buffer */
6368 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6369 fprintf(overwrite_fpointer, "%s\n", s);
6371 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6372 fprintf(append_fpointer, "%s\n", s);
6374 memset(tmpstring1, 0, text_buffer_size);
6375 memset(tmpstring2, 0, text_buffer_size);
6376 strncpy(tmpstring1, s, text_buffer_size - 1);
6381 if (output_methods & TO_X) {
6382 max = ((text_width - width_of_s) / get_string_width(" "));
6385 /* This code looks for tabs in the text and coverts them to spaces.
6386 * The trick is getting the correct number of spaces, and not going
6387 * over the window's size without forcing the window larger. */
6388 for (i = 0; i < (int) text_buffer_size; i++) {
6389 if (tmpstring1[i] == '\t') {
6391 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6392 /* guard against overrun */
6393 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6398 /* guard against overrun */
6399 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6404 if (output_methods & TO_X) {
6405 if (text_width == maximum_width) {
6406 /* this means the text is probably pushing the limit,
6407 * so we'll chop it */
6408 while (cur_x + get_string_width(tmpstring2) - text_start_x
6409 > maximum_width && strlen(tmpstring2) > 0) {
6410 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6417 if (output_methods & TO_X) {
6423 c.pixel = current_color;
6424 XQueryColor(display, DefaultColormap(display, screen), &c);
6427 c2.color.red = c.red;
6428 c2.color.green = c.green;
6429 c2.color.blue = c.blue;
6430 c2.color.alpha = fonts[selected_font].font_alpha;
6432 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6433 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6435 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6436 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6441 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6444 cur_x += width_of_s;
6447 memcpy(tmpstring1, s, text_buffer_size);
6451 int draw_each_line_inner(char *s, int special_index, const int last_special_applied)
6453 int font_h = font_height();
6457 int last_special_needed = -1;
6459 cur_x = text_start_x;
6460 cur_y += font_ascent();
6463 if (*p == SECRIT_MULTILINE_CHAR) {
6464 /* special newline marker for multiline objects */
6469 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
6472 /* draw string before special, unless we're dealing multiline
6474 if (last_special_applied > -1) {
6475 special_index = last_special_applied;
6483 switch (specials[special_index].type) {
6484 case HORIZONTAL_LINE:
6486 int h = specials[special_index].height;
6487 int mid = font_ascent() / 2;
6489 w = text_start_x + text_width - cur_x;
6491 XSetLineAttributes(display, window.gc, h, LineSolid,
6492 CapButt, JoinMiter);
6493 XDrawLine(display, window.drawable, window.gc, cur_x,
6494 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6500 int h = specials[special_index].height;
6501 int tmp_s = specials[special_index].arg;
6502 int mid = font_ascent() / 2;
6503 char ss[2] = { tmp_s, tmp_s };
6505 w = text_start_x + text_width - cur_x - 1;
6506 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6507 CapButt, JoinMiter);
6508 XSetDashes(display, window.gc, 0, ss, 2);
6509 XDrawLine(display, window.drawable, window.gc, cur_x,
6510 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6516 int h, bar_usage, by;
6517 if (cur_x - text_start_x > maximum_width
6518 && maximum_width > 0) {
6521 h = specials[special_index].height;
6522 bar_usage = specials[special_index].arg;
6523 by = cur_y - (font_ascent() / 2) - 1;
6528 w = specials[special_index].width;
6530 w = text_start_x + text_width - cur_x - 1;
6536 XSetLineAttributes(display, window.gc, 1, LineSolid,
6537 CapButt, JoinMiter);
6539 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6541 XFillRectangle(display, window.drawable, window.gc, cur_x,
6542 by, w * bar_usage / 255, h);
6550 case GAUGE: /* new GAUGE */
6553 unsigned long last_colour = current_color;
6555 float angle, px, py;
6559 if (cur_x - text_start_x > maximum_width
6560 && maximum_width > 0) {
6564 h = specials[special_index].height;
6565 by = cur_y - (font_ascent() / 2) - 1;
6570 w = specials[special_index].width;
6572 w = text_start_x + text_width - cur_x - 1;
6578 XSetLineAttributes(display, window.gc, 1, LineSolid,
6579 CapButt, JoinMiter);
6581 XDrawArc(display, window.drawable, window.gc,
6582 cur_x, by, w, h * 2, 0, 180*64);
6585 usage = specials[special_index].arg;
6586 angle = (M_PI)*(float)(usage)/255.;
6587 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6588 py = (float)(by+(h))-(float)(h)*sin(angle);
6590 XDrawLine(display, window.drawable, window.gc,
6591 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6599 set_foreground_color(last_colour);
6607 int h, by, i = 0, j = 0;
6609 unsigned long last_colour = current_color;
6610 unsigned long *tmpcolour = 0;
6611 if (cur_x - text_start_x > maximum_width
6612 && maximum_width > 0) {
6615 h = specials[special_index].height;
6616 by = cur_y - (font_ascent() / 2) - 1;
6621 w = specials[special_index].width;
6623 w = text_start_x + text_width - cur_x - 1;
6628 if (draw_graph_borders) {
6629 XSetLineAttributes(display, window.gc, 1, LineSolid,
6630 CapButt, JoinMiter);
6631 XDrawRectangle(display, window.drawable, window.gc,
6634 XSetLineAttributes(display, window.gc, 1, LineSolid,
6635 CapButt, JoinMiter);
6637 if (specials[special_index].last_colour != 0
6638 || specials[special_index].first_colour != 0) {
6639 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6642 for (i = w - 2; i > -1; i--) {
6643 if (specials[special_index].last_colour != 0
6644 || specials[special_index].first_colour != 0) {
6645 if (specials[special_index].tempgrad) {
6648 (int)((float)(w - 2) - specials[special_index].graph[j] *
6649 (w - 2) / (float)specials[special_index].graph_scale)
6653 (int)((float)(w - 2) - specials[special_index].graph[j] *
6654 (w - 2) / (float)specials[special_index].graph_scale)
6657 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6659 (int)((float)(w - 2) - specials[special_index].graph[j] *
6660 (w - 2) / (float)specials[special_index].graph_scale)
6664 #endif /* DEBUG_lol */
6665 XSetForeground(display, window.gc, tmpcolour[
6666 (int)((float)(w - 2) - specials[special_index].graph[j] *
6667 (w - 2) / (float)specials[special_index].graph_scale)
6670 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6673 /* this is mugfugly, but it works */
6674 XDrawLine(display, window.drawable, window.gc,
6675 cur_x + i + 1, by + h, cur_x + i + 1,
6676 round_to_int((double)by + h - specials[special_index].graph[j] *
6677 (h - 1) / specials[special_index].graph_scale));
6678 if ((w - i) / ((float) (w - 2) /
6679 (specials[special_index].graph_width)) > j
6680 && j < MAX_GRAPH_DEPTH - 3) {
6684 if (tmpcolour) free(tmpcolour);
6689 /* if (draw_mode == BG) {
6690 set_foreground_color(default_bg_color);
6691 } else if (draw_mode == OUTLINE) {
6692 set_foreground_color(default_out_color);
6694 set_foreground_color(default_fg_color);
6696 if (show_graph_range) {
6699 unsigned short int seconds = update_interval * w;
6705 unsigned short int timeunits;
6707 timeunits = seconds / 86400; seconds %= 86400;
6708 if (timeunits > 0) {
6709 asprintf(&tmp_day_str, "%dd", timeunits);
6711 tmp_day_str = strdup("");
6713 timeunits = seconds / 3600; seconds %= 3600;
6714 if (timeunits > 0) {
6715 asprintf(&tmp_hour_str, "%dh", timeunits);
6717 tmp_hour_str = strdup("");
6719 timeunits = seconds / 60; seconds %= 60;
6720 if (timeunits > 0) {
6721 asprintf(&tmp_min_str, "%dm", timeunits);
6723 tmp_min_str = strdup("");
6726 asprintf(&tmp_sec_str, "%ds", seconds);
6728 tmp_sec_str = strdup("");
6730 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6731 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6733 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6735 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6736 cur_y += font_h / 2;
6737 draw_string(tmp_str);
6743 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6747 cur_x += font_ascent() / 2;
6748 cur_y += font_h / 2;
6750 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6752 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6753 draw_string(tmp_str);
6759 set_foreground_color(last_colour);
6765 int old = font_ascent();
6767 cur_y -= font_ascent();
6768 selected_font = specials[special_index].font_added;
6770 if (cur_y + font_ascent() < cur_y + old) {
6773 cur_y += font_ascent();
6775 font_h = font_height();
6779 if (draw_mode == FG) {
6780 set_foreground_color(specials[special_index].arg);
6785 if (draw_mode == BG) {
6786 set_foreground_color(specials[special_index].arg);
6791 if (draw_mode == OUTLINE) {
6792 set_foreground_color(specials[special_index].arg);
6797 w += specials[special_index].arg;
6798 last_special_needed = special_index;
6802 cur_y += specials[special_index].arg;
6806 if (specials[special_index].arg >= 0) {
6807 cur_x = (int) specials[special_index].arg;
6809 last_special_needed = special_index;
6814 int start = specials[special_index].arg;
6815 int step = specials[special_index].width;
6817 if (!step || step < 0) {
6820 w = step - (cur_x - text_start_x - start) % step;
6821 last_special_needed = special_index;
6827 /* TODO: add back in "+ window.border_inner_margin" to the end of
6829 int pos_x = text_start_x + text_width -
6830 get_string_width_special(s, special_index);
6832 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6833 "get_string_width(p) %i gap_x %i "
6834 "specials[special_index].arg %i window.border_inner_margin %i "
6835 "window.border_width %i\n", pos_x, text_start_x, text_width,
6836 cur_x, get_string_width_special(s), gap_x,
6837 specials[special_index].arg, window.border_inner_margin,
6838 window.border_width); */
6839 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6840 cur_x = pos_x - specials[special_index].arg;
6842 last_special_needed = special_index;
6848 int pos_x = (text_width) / 2 - get_string_width_special(s,
6849 special_index) / 2 - (cur_x -
6851 /* int pos_x = text_start_x + text_width / 2 -
6852 get_string_width_special(s) / 2; */
6854 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6855 "get_string_width(p) %i gap_x %i "
6856 "specials[special_index].arg %i\n", pos_x, text_start_x,
6857 text_width, cur_x, get_string_width(s), gap_x,
6858 specials[special_index].arg); */
6859 if (pos_x > specials[special_index].arg) {
6860 w = pos_x - specials[special_index].arg;
6862 last_special_needed = special_index;
6869 if (special_index != last_special_applied) special_index++;
6876 cur_y += font_descent();
6877 if (recurse && *recurse) {
6878 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
6879 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
6881 return special_index;
6885 static int draw_line(char *s, int special_index)
6888 if ((output_methods & TO_X) == 0) {
6895 /* find specials and draw stuff */
6896 return draw_each_line_inner(s, special_index, -1);
6900 static void draw_text(void)
6904 llua_draw_pre_hook();
6905 #endif /* HAVE_LUA */
6906 if (output_methods & TO_X) {
6907 cur_y = text_start_y;
6910 if (draw_borders && window.border_width > 0) {
6911 if (stippled_borders) {
6912 char ss[2] = { stippled_borders, stippled_borders };
6913 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6914 CapButt, JoinMiter);
6915 XSetDashes(display, window.gc, 0, ss, 2);
6917 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6918 CapButt, JoinMiter);
6921 XDrawRectangle(display, window.drawable, window.gc,
6922 text_start_x - window.border_inner_margin - window.border_width,
6923 text_start_y - window.border_inner_margin - window.border_width,
6924 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6925 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6932 for_each_line(text_buffer, draw_line);
6933 #if defined(HAVE_LUA) && defined(X11)
6934 llua_draw_post_hook();
6935 #endif /* HAVE_LUA */
6938 static void draw_stuff(void)
6940 if (overwrite_file) {
6941 overwrite_fpointer = fopen(overwrite_file, "w");
6942 if(!overwrite_fpointer)
6943 ERR("Can't overwrite '%s' anymore", overwrite_file);
6946 append_fpointer = fopen(append_file, "a");
6947 if(!append_fpointer)
6948 ERR("Can't append '%s' anymore", append_file);
6951 if (output_methods & TO_X) {
6953 if (draw_shades && !draw_outline) {
6956 set_foreground_color(default_bg_color);
6967 for (i = -1; i < 2; i++) {
6968 for (j = -1; j < 2; j++) {
6969 if (i == 0 && j == 0) {
6974 set_foreground_color(default_out_color);
6975 draw_mode = OUTLINE;
6983 set_foreground_color(default_fg_color);
6989 if (output_methods & TO_X) {
6994 swap.swap_window = window.window;
6995 swap.swap_action = XdbeBackground;
6996 XdbeSwapBuffers(display, &swap, 1);
7001 if(overwrite_fpointer) {
7002 fclose(overwrite_fpointer);
7003 overwrite_fpointer = 0;
7005 if(append_fpointer) {
7006 fclose(append_fpointer);
7007 append_fpointer = 0;
7012 static void clear_text(int exposures)
7016 /* The swap action is XdbeBackground, which clears */
7020 if (display && window.window) { // make sure these are !null
7021 /* there is some extra space for borders and outlines */
7022 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
7023 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
7024 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7025 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
7030 static int need_to_update;
7032 /* update_text() generates new text and clears old text area */
7033 static void update_text(void)
7040 if (output_methods & TO_X)
7046 #ifdef HAVE_SYS_INOTIFY_H
7050 static void main_loop(void)
7053 #ifdef SIGNAL_BLOCKING
7054 sigset_t newmask, oldmask;
7057 #ifdef HAVE_SYS_INOTIFY_H
7058 int inotify_config_wd = -1;
7059 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
7060 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
7061 char inotify_buff[INOTIFY_BUF_LEN];
7062 #endif /* HAVE_SYS_INOTIFY_H */
7065 #ifdef SIGNAL_BLOCKING
7066 sigemptyset(&newmask);
7067 sigaddset(&newmask, SIGINT);
7068 sigaddset(&newmask, SIGTERM);
7069 sigaddset(&newmask, SIGUSR1);
7072 last_update_time = 0.0;
7073 next_update_time = get_time();
7075 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7078 #ifdef SIGNAL_BLOCKING
7079 /* block signals. we will inspect for pending signals later */
7080 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7081 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7086 if (output_methods & TO_X) {
7089 /* wait for X event or timeout */
7091 if (!XPending(display)) {
7095 t = next_update_time - get_time();
7099 } else if (t > update_interval) {
7100 t = update_interval;
7103 tv.tv_sec = (long) t;
7104 tv.tv_usec = (long) (t * 1000000) % 1000000;
7106 FD_SET(ConnectionNumber(display), &fdsr);
7108 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7110 if (errno != EINTR) {
7111 ERR("can't select(): %s", strerror(errno));
7121 if (need_to_update) {
7123 int wx = window.x, wy = window.y;
7133 /* resize window if it isn't right size */
7135 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7136 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7137 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7138 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7139 XResizeWindow(display, window.window, window.width,
7141 set_transparent_background(window.window);
7146 /* move window if it isn't in right position */
7147 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7148 XMoveWindow(display, window.window, window.x, window.y);
7153 if (changed && window.type == TYPE_PANEL) {
7156 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7159 switch (text_alignment) {
7186 set_struts(sidenum);
7197 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7198 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7199 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7200 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7201 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7206 /* handle X events */
7207 while (XPending(display)) {
7210 XNextEvent(display, &ev);
7217 r.width = ev.xexpose.width;
7218 r.height = ev.xexpose.height;
7219 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7223 case PropertyNotify:
7225 if ( ev.xproperty.state == PropertyNewValue ) {
7226 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7232 case ReparentNotify:
7233 /* set background to ParentRelative for all parents */
7235 set_transparent_background(window.window);
7239 case ConfigureNotify:
7241 /* if window size isn't what expected, set fixed size */
7242 if (ev.xconfigure.width != window.width
7243 || ev.xconfigure.height != window.height) {
7244 if (window.width != 0 && window.height != 0) {
7248 /* clear old stuff before screwing up
7253 XWindowAttributes attrs;
7254 if (XGetWindowAttributes(display,
7255 window.window, &attrs)) {
7256 window.width = attrs.width;
7257 window.height = attrs.height;
7261 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7262 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7263 if (text_width > maximum_width
7264 && maximum_width > 0) {
7265 text_width = maximum_width;
7269 /* if position isn't what expected, set fixed pos
7270 * total_updates avoids setting fixed_pos when window
7271 * is set to weird locations when started */
7272 /* // this is broken
7273 if (total_updates >= 2 && !fixed_pos
7274 && (window.x != ev.xconfigure.x
7275 || window.y != ev.xconfigure.y)
7276 && (ev.xconfigure.x != 0
7277 || ev.xconfigure.y != 0)) {
7285 /* if an ordinary window with decorations */
7286 if ((window.type == TYPE_NORMAL)
7287 && (!TEST_HINT(window.hints,
7288 HINT_UNDECORATED))) {
7289 /* allow conky to hold input focus. */
7292 /* forward the click to the desktop window */
7293 XUngrabPointer(display, ev.xbutton.time);
7294 ev.xbutton.window = window.desktop;
7295 ev.xbutton.x = ev.xbutton.x_root;
7296 ev.xbutton.y = ev.xbutton.y_root;
7297 XSendEvent(display, ev.xbutton.window, False,
7298 ButtonPressMask, &ev);
7299 XSetInputFocus(display, ev.xbutton.window,
7300 RevertToParent, ev.xbutton.time);
7307 /* if an ordinary window with decorations */
7308 if ((window.type == TYPE_NORMAL)
7309 && (!TEST_HINT(window.hints,
7310 HINT_UNDECORATED))) {
7311 /* allow conky to hold input focus. */
7314 /* forward the release to the desktop window */
7315 ev.xbutton.window = window.desktop;
7316 ev.xbutton.x = ev.xbutton.x_root;
7317 ev.xbutton.y = ev.xbutton.y_root;
7318 XSendEvent(display, ev.xbutton.window, False,
7319 ButtonReleaseMask, &ev);
7328 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7329 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7331 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7332 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7334 #endif /* HAVE_XDAMAGE */
7340 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7341 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7342 #endif /* HAVE_XDAMAGE */
7344 /* XDBE doesn't seem to provide a way to clear the back buffer without
7345 * interfering with the front buffer, other than passing XdbeBackground
7346 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7347 * redraw the text even if it wasn't part of the exposed area. OTOH,
7348 * if we're not going to call draw_stuff at all, then no swap happens
7349 * and we can safely do nothing. */
7351 if (!XEmptyRegion(x11_stuff.region)) {
7356 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7357 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7358 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7359 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7360 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7363 XSetRegion(display, window.gc, x11_stuff.region);
7366 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7370 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7373 XDestroyRegion(x11_stuff.region);
7374 x11_stuff.region = XCreateRegion();
7378 t = (next_update_time - get_time()) * 1000000;
7379 if(t > 0) usleep((useconds_t)t);
7386 #ifdef SIGNAL_BLOCKING
7387 /* unblock signals of interest and let handler fly */
7388 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7389 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7393 switch (g_signal_pending) {
7396 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7401 ERR("received SIGINT or SIGTERM to terminate. bye!");
7404 if (output_methods & TO_X) {
7405 XDestroyRegion(x11_stuff.region);
7406 x11_stuff.region = NULL;
7408 XDamageDestroy(display, x11_stuff.damage);
7409 XFixesDestroyRegion(display, x11_stuff.region2);
7410 XFixesDestroyRegion(display, x11_stuff.part);
7411 #endif /* HAVE_XDAMAGE */
7417 if(overwrite_file) {
7418 free(overwrite_file);
7427 /* Reaching here means someone set a signal
7428 * (SIGXXXX, signal_handler), but didn't write any code
7430 * If you don't want to handle a signal, don't set a handler on
7431 * it in the first place. */
7432 if (g_signal_pending) {
7433 ERR("ignoring signal (%d)", g_signal_pending);
7437 #ifdef HAVE_SYS_INOTIFY_H
7438 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7439 inotify_config_wd = inotify_add_watch(inotify_fd,
7443 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7444 int len = 0, idx = 0;
7446 struct timeval time_to_wait;
7448 FD_ZERO(&descriptors);
7449 FD_SET(inotify_fd, &descriptors);
7451 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7453 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7454 if (FD_ISSET(inotify_fd, &descriptors)) {
7455 /* process inotify events */
7456 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7457 while (len > 0 && idx < len) {
7458 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7459 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7460 /* current_config should be reloaded */
7461 ERR("'%s' modified, reloading...", current_config);
7463 if (ev->mask & IN_IGNORED) {
7464 /* for some reason we get IN_IGNORED here
7465 * sometimes, so we need to re-add the watch */
7466 inotify_config_wd = inotify_add_watch(inotify_fd,
7473 llua_inotify_query(ev->wd, ev->mask);
7475 #endif /* HAVE_LUA */
7476 idx += INOTIFY_EVENT_SIZE + ev->len;
7480 #endif /* HAVE_SYS_INOTIFY_H */
7482 g_signal_pending = 0;
7484 clean_up(NULL, NULL);
7486 #ifdef HAVE_SYS_INOTIFY_H
7487 if (inotify_fd != -1) {
7488 inotify_rm_watch(inotify_fd, inotify_config_wd);
7490 inotify_fd = inotify_config_wd = 0;
7492 #endif /* HAVE_SYS_INOTIFY_H */
7495 static void load_config_file(const char *);
7497 static void load_config_file_x11(const char *);
7499 void initialisation(int argc, char** argv);
7501 /* reload the config file */
7502 static void reload_config(void)
7504 char *current_config_copy = strdup(current_config);
7505 clean_up(NULL, NULL);
7506 current_config = current_config_copy;
7507 initialisation(argc_copy, argv_copy);
7510 void clean_up(void *memtofree1, void* memtofree2)
7519 timed_thread_destroy_registered_threads();
7521 if (info.cpu_usage) {
7522 free(info.cpu_usage);
7523 info.cpu_usage = NULL;
7526 if (x_initialised == YES) {
7529 if(x11_stuff.region) {
7530 XDestroyRegion(x11_stuff.region);
7531 x11_stuff.region = NULL;
7533 XClearWindow(display, RootWindow(display, screen));
7534 XCloseDisplay(display);
7536 if(info.x11.desktop.all_names) {
7537 free(info.x11.desktop.all_names);
7538 info.x11.desktop.all_names = NULL;
7540 if (info.x11.desktop.name) {
7541 free(info.x11.desktop.name);
7542 info.x11.desktop.name = NULL;
7546 free(fonts); //in set_default_configurations a font is set but not loaded
7552 for (i = 0; i < MAX_TEMPLATES; i++) {
7559 free_text_objects(&global_root_object, 0);
7578 free(current_config);
7581 #ifdef TCP_PORT_MONITOR
7582 tcp_portmon_clear();
7588 free_weather_info();
7592 #endif /* HAVE_LUA */
7595 for (i = 0; i < special_count; i++) {
7596 if (specials[i].type == GRAPH) {
7597 free(specials[i].graph);
7605 clear_diskio_stats();
7606 if(global_cpu != NULL) {
7612 static int string_to_bool(const char *s)
7615 // Assumes an option without a true/false means true
7617 } else if (strcasecmp(s, "yes") == EQUAL) {
7619 } else if (strcasecmp(s, "true") == EQUAL) {
7621 } else if (strcasecmp(s, "1") == EQUAL) {
7628 static enum alignment string_to_alignment(const char *s)
7630 if (strcasecmp(s, "top_left") == EQUAL) {
7632 } else if (strcasecmp(s, "top_right") == EQUAL) {
7634 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7636 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7638 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7639 return BOTTOM_RIGHT;
7640 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7641 return BOTTOM_MIDDLE;
7642 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7644 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7645 return MIDDLE_RIGHT;
7646 } else if (strcasecmp(s, "tl") == EQUAL) {
7648 } else if (strcasecmp(s, "tr") == EQUAL) {
7650 } else if (strcasecmp(s, "tm") == EQUAL) {
7652 } else if (strcasecmp(s, "bl") == EQUAL) {
7654 } else if (strcasecmp(s, "br") == EQUAL) {
7655 return BOTTOM_RIGHT;
7656 } else if (strcasecmp(s, "bm") == EQUAL) {
7657 return BOTTOM_MIDDLE;
7658 } else if (strcasecmp(s, "ml") == EQUAL) {
7660 } else if (strcasecmp(s, "mr") == EQUAL) {
7661 return MIDDLE_RIGHT;
7662 } else if (strcasecmp(s, "none") == EQUAL) {
7670 static void set_default_configurations_for_x(void)
7672 default_fg_color = WhitePixel(display, screen);
7673 default_bg_color = BlackPixel(display, screen);
7674 default_out_color = BlackPixel(display, screen);
7675 color0 = default_fg_color;
7676 color1 = default_fg_color;
7677 color2 = default_fg_color;
7678 color3 = default_fg_color;
7679 color4 = default_fg_color;
7680 color5 = default_fg_color;
7681 color6 = default_fg_color;
7682 color7 = default_fg_color;
7683 color8 = default_fg_color;
7684 color9 = default_fg_color;
7688 static void set_default_configurations(void)
7696 fork_to_background = 0;
7697 total_run_times = 0;
7698 info.cpu_avg_samples = 2;
7699 info.net_avg_samples = 2;
7700 info.diskio_avg_samples = 2;
7705 format_human_readable = 1;
7712 mpd_env_host = getenv("MPD_HOST");
7713 mpd_env_port = getenv("MPD_PORT");
7715 if (!mpd_env_host || !strlen(mpd_env_host)) {
7716 mpd_set_host("localhost");
7718 /* MPD_HOST environment variable is set */
7719 char *mpd_hostpart = strchr(mpd_env_host, '@');
7720 if (!mpd_hostpart) {
7721 mpd_set_host(mpd_env_host);
7723 /* MPD_HOST contains a password */
7724 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7725 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7727 if (!strlen(mpd_hostpart + 1)) {
7728 mpd_set_host("localhost");
7730 mpd_set_host(mpd_hostpart + 1);
7733 mpd_set_password(mpd_password, 1);
7738 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7739 /* failed to set port from environment variable */
7740 mpd_set_port("6600");
7744 info.xmms2.artist = NULL;
7745 info.xmms2.album = NULL;
7746 info.xmms2.title = NULL;
7747 info.xmms2.genre = NULL;
7748 info.xmms2.comment = NULL;
7749 info.xmms2.url = NULL;
7750 info.xmms2.status = NULL;
7751 info.xmms2.playlist = NULL;
7753 use_spacer = NO_SPACER;
7755 output_methods = TO_X;
7757 output_methods = TO_STDOUT;
7760 show_graph_scale = 0;
7761 show_graph_range = 0;
7764 draw_graph_borders = 1;
7766 set_first_font("6x10");
7774 window.type = TYPE_NORMAL;
7776 strcpy(window.class_name, PACKAGE_NAME);
7777 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7779 stippled_borders = 0;
7780 window.border_inner_margin = 3;
7781 window.border_outer_margin = 1;
7782 window.border_width = 1;
7783 text_alignment = BOTTOM_LEFT;
7784 info.x11.monitor.number = 1;
7785 info.x11.monitor.current = 0;
7786 info.x11.desktop.current = 1;
7787 info.x11.desktop.number = 1;
7788 info.x11.desktop.nitems = 0;
7789 info.x11.desktop.all_names = NULL;
7790 info.x11.desktop.name = NULL;
7793 for (i = 0; i < MAX_TEMPLATES; i++) {
7796 template[i] = strdup("");
7799 free(current_mail_spool);
7803 variable_substitute(MAIL_FILE, buf, 256);
7804 if (buf[0] != '\0') {
7805 current_mail_spool = strndup(buf, text_buffer_size);
7810 update_interval = 3.0;
7811 info.music_player_interval = 1.0;
7812 stuff_in_uppercase = 0;
7813 info.users.number = 1;
7815 #ifdef TCP_PORT_MONITOR
7816 /* set default connection limit */
7817 tcp_portmon_set_max_connections(0);
7821 /* returns 1 if you can overwrite or create the file at 'path' */
7822 static _Bool overwrite_works(const char *path)
7826 if (!(filepointer = fopen(path, "w")))
7828 fclose(filepointer);
7832 /* returns 1 if you can append or create the file at 'path' */
7833 static _Bool append_works(const char *path)
7837 if (!(filepointer = fopen(path, "a")))
7839 fclose(filepointer);
7845 /* WARNING, this type not in Xlib spec */
7846 int x11_error_handler(Display *d, XErrorEvent *err)
7847 __attribute__((noreturn));
7848 int x11_error_handler(Display *d, XErrorEvent *err)
7850 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7852 (long unsigned)err->display,
7853 (long)err->resourceid,
7863 int x11_ioerror_handler(Display *d)
7864 __attribute__((noreturn));
7865 int x11_ioerror_handler(Display *d)
7867 ERR("X Error: Display %lx\n",
7874 static void X11_initialisation(void)
7876 if (x_initialised == YES) return;
7877 output_methods |= TO_X;
7879 set_default_configurations_for_x();
7880 x_initialised = YES;
7883 /* WARNING, this type not in Xlib spec */
7884 XSetErrorHandler(&x11_error_handler);
7885 XSetIOErrorHandler(&x11_ioerror_handler);
7889 static char **xargv = 0;
7890 static int xargc = 0;
7892 static void X11_create_window(void)
7894 if (output_methods & TO_X) {
7896 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7897 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7899 #else /* OWN_WINDOW */
7900 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7901 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7903 #endif /* OWN_WINDOW */
7907 update_text_area(); /* to position text/window on screen */
7910 if (own_window && !fixed_pos) {
7911 XMoveWindow(display, window.window, window.x, window.y);
7914 set_transparent_background(window.window);
7922 x11_stuff.region = XCreateRegion();
7924 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7925 ERR("Xdamage extension unavailable");
7927 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7928 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7929 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7930 #endif /* HAVE_XDAMAGE */
7933 update_text_area(); /* to get initial size of the window */
7936 /* setup lua window globals */
7937 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
7938 #endif /* HAVE_LUA */
7942 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7943 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7944 #define CONF2(a) if (strcasecmp(name, a) == 0)
7945 #define CONF(a) else CONF2(a)
7946 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7947 || strcasecmp(name, b) == 0)
7948 #define CONF_CONTINUE 1
7949 #define CONF_BREAK 2
7950 #define CONF_BUFF_SIZE 512
7952 static FILE *open_config_file(const char *f)
7954 #ifdef CONFIG_OUTPUT
7955 if (!strcmp(f, "==builtin==")) {
7956 return conf_cookie_open();
7958 #endif /* CONFIG_OUTPUT */
7959 return fopen(f, "r");
7962 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7966 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7969 remove_comments(buf);
7974 while (*p && isspace((int) *p)) {
7978 return CONF_CONTINUE; /* empty line */
7985 while (*p2 && !isspace((int) *p2)) {
7989 *p2 = '\0'; /* break at name's end */
7996 while (*p && isspace((int) *p)) {
8002 p2 = *value + strlen(*value);
8003 while (isspace((int) *(p2 - 1))) {
8012 static void load_config_file(const char *f)
8017 set_default_configurations();
8018 fp = open_config_file(f);
8022 DBGP("reading contents from config file '%s'", f);
8025 char buff[CONF_BUFF_SIZE], *name, *value;
8026 int ret = do_config_step(&line, fp, buff, &name, &value);
8027 if (ret == CONF_BREAK) {
8029 } else if (ret == CONF_CONTINUE) {
8035 /* don't listen if X is already initialised or
8036 * if we already know we don't want it */
8037 if(x_initialised != YES) {
8038 if (string_to_bool(value)) {
8039 output_methods &= TO_X;
8041 output_methods &= ~TO_X;
8042 x_initialised = NEVER;
8047 if (!value || x_initialised == YES) {
8052 disp = strdup(value);
8057 if (window.type == TYPE_DOCK)
8060 #endif /*OWN_WINDOW */
8062 int a = string_to_alignment(value);
8073 CONF("background") {
8074 fork_to_background = string_to_bool(value);
8077 CONF2("background") {
8078 fork_to_background = string_to_bool(value);
8082 CONF("show_graph_scale") {
8083 show_graph_scale = string_to_bool(value);
8085 CONF("show_graph_range") {
8086 show_graph_range = string_to_bool(value);
8088 CONF("border_margin") {
8089 ERR("border_margin is deprecated, please use window.border_inner_margin instead");
8091 window.border_inner_margin = strtol(value, 0, 0);
8092 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8097 CONF("border_inner_margin") {
8099 window.border_inner_margin = strtol(value, 0, 0);
8100 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8105 CONF("border_outer_margin") {
8107 window.border_outer_margin = strtol(value, 0, 0);
8108 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8113 CONF("border_width") {
8115 window.border_width = strtol(value, 0, 0);
8116 if (window.border_width < 0) window.border_width = 0;
8122 #define TEMPLATE_CONF(n) \
8123 CONF("template"#n) { \
8125 free(template[n]); \
8126 template[n] = strdup(value); \
8143 info.mail = parse_mail_args(IMAP_TYPE, value);
8150 info.mail = parse_mail_args(POP3_TYPE, value);
8155 CONF("default_bar_size") {
8158 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8165 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8169 CONF("default_graph_size") {
8172 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8179 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8182 CONF("default_gauge_size") {
8185 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8192 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8199 mpd_set_host(value);
8205 if (value && mpd_set_port(value)) {
8209 CONF("mpd_password") {
8211 mpd_set_password(value, 0);
8217 CONF("music_player_interval") {
8219 info.music_player_interval = strtod(value, 0);
8225 CONF("sensor_device") {
8227 sensor_device = strtol(value, 0, 0);
8233 CONF("cpu_avg_samples") {
8235 cpu_avg_samples = strtol(value, 0, 0);
8236 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8239 info.cpu_avg_samples = cpu_avg_samples;
8245 CONF("net_avg_samples") {
8247 net_avg_samples = strtol(value, 0, 0);
8248 if (net_avg_samples < 1 || net_avg_samples > 14) {
8251 info.net_avg_samples = net_avg_samples;
8257 CONF("diskio_avg_samples") {
8259 diskio_avg_samples = strtol(value, 0, 0);
8260 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8263 info.diskio_avg_samples = diskio_avg_samples;
8271 CONF("double_buffer") {
8272 use_xdbe = string_to_bool(value);
8276 CONF("override_utf8_locale") {
8277 utf8_mode = string_to_bool(value);
8279 CONF("draw_borders") {
8280 draw_borders = string_to_bool(value);
8282 CONF("draw_graph_borders") {
8283 draw_graph_borders = string_to_bool(value);
8285 CONF("draw_shades") {
8286 draw_shades = string_to_bool(value);
8288 CONF("draw_outline") {
8289 draw_outline = string_to_bool(value);
8292 CONF("out_to_console") {
8293 if(string_to_bool(value))
8294 output_methods |= TO_STDOUT;
8296 CONF("out_to_stderr") {
8297 if(string_to_bool(value))
8298 output_methods |= TO_STDERR;
8300 CONF("overwrite_file") {
8301 if(overwrite_file) {
8302 free(overwrite_file);
8305 if(overwrite_works(value)) {
8306 overwrite_file = strdup(value);
8307 output_methods |= OVERWRITE_FILE;
8309 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8311 CONF("append_file") {
8316 if(append_works(value)) {
8317 append_file = strdup(value);
8318 output_methods |= APPEND_FILE;
8320 ERR("append_file won't be able to create/append '%s'", value);
8322 CONF("use_spacer") {
8324 if (strcasecmp(value, "left") == EQUAL) {
8325 use_spacer = LEFT_SPACER;
8326 } else if (strcasecmp(value, "right") == EQUAL) {
8327 use_spacer = RIGHT_SPACER;
8328 } else if (strcasecmp(value, "none") == EQUAL) {
8329 use_spacer = NO_SPACER;
8331 use_spacer = string_to_bool(value);
8332 ERR("use_spacer should have an argument of left, right, or"
8333 " none. '%s' seems to be some form of '%s', so"
8334 " defaulting to %s.", value,
8335 use_spacer ? "true" : "false",
8336 use_spacer ? "right" : "none");
8338 use_spacer = RIGHT_SPACER;
8340 use_spacer = NO_SPACER;
8344 ERR("use_spacer should have an argument. Defaulting to right.");
8345 use_spacer = RIGHT_SPACER;
8351 use_xft = string_to_bool(value);
8355 set_first_font(value);
8359 if (value && font_count >= 0) {
8360 fonts[0].font_alpha = atof(value) * 65535.0;
8367 if (string_to_bool(value)) {
8368 ERR("Xft not enabled at compile time");
8372 /* xftfont silently ignored when no Xft */
8375 /* xftalpha is silently ignored when no Xft */
8380 set_first_font(value);
8388 gap_x = atoi(value);
8395 gap_y = atoi(value);
8401 CONF("mail_spool") {
8405 variable_substitute(value, buffer, 256);
8407 if (buffer[0] != '\0') {
8408 if (current_mail_spool) {
8409 free(current_mail_spool);
8411 current_mail_spool = strndup(buffer, text_buffer_size);
8418 CONF("minimum_size") {
8420 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8422 if (sscanf(value, "%d", &minimum_width) != 1) {
8430 CONF("maximum_width") {
8432 if (sscanf(value, "%d", &maximum_width) != 1) {
8440 CONF("no_buffers") {
8441 no_buffers = string_to_bool(value);
8443 CONF("top_name_width") {
8445 if (sscanf(value, "%u", &top_name_width) != 1) {
8451 if (top_name_width >= max_user_text) {
8452 top_name_width = max_user_text - 1;
8455 CONF("top_cpu_separate") {
8456 cpu_separate = string_to_bool(value);
8458 CONF("short_units") {
8459 short_units = string_to_bool(value);
8461 CONF("format_human_readable") {
8462 format_human_readable = string_to_bool(value);
8464 CONF("pad_percents") {
8465 pad_percents = atoi(value);
8469 CONF("own_window") {
8471 own_window = string_to_bool(value);
8474 CONF("own_window_class") {
8476 memset(window.class_name, 0, sizeof(window.class_name));
8477 strncpy(window.class_name, value,
8478 sizeof(window.class_name) - 1);
8481 CONF("own_window_title") {
8483 memset(window.title, 0, sizeof(window.title));
8484 strncpy(window.title, value, sizeof(window.title) - 1);
8487 CONF("own_window_transparent") {
8489 set_transparent = string_to_bool(value);
8492 CONF("own_window_hints") {
8494 char *p_hint, *p_save;
8495 char delim[] = ", ";
8497 /* tokenize the value into individual hints */
8498 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8500 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8501 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8502 SET_HINT(window.hints, HINT_UNDECORATED);
8503 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8504 SET_HINT(window.hints, HINT_BELOW);
8505 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8506 SET_HINT(window.hints, HINT_ABOVE);
8507 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8508 SET_HINT(window.hints, HINT_STICKY);
8509 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8510 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8511 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8512 SET_HINT(window.hints, HINT_SKIP_PAGER);
8517 p_hint = strtok_r(NULL, delim, &p_save);
8518 } while (p_hint != NULL);
8524 CONF("own_window_type") {
8526 if (strncmp(value, "normal", 6) == EQUAL) {
8527 window.type = TYPE_NORMAL;
8528 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8529 window.type = TYPE_DESKTOP;
8530 } else if (strncmp(value, "dock", 4) == EQUAL) {
8531 window.type = TYPE_DOCK;
8532 text_alignment = TOP_LEFT;
8533 } else if (strncmp(value, "panel", 5) == EQUAL) {
8534 window.type = TYPE_PANEL;
8535 } else if (strncmp(value, "override", 8) == EQUAL) {
8536 window.type = TYPE_OVERRIDE;
8545 CONF("stippled_borders") {
8547 stippled_borders = strtol(value, 0, 0);
8549 stippled_borders = 4;
8553 CONF("imlib_cache_size") {
8555 cimlib_set_cache_size(atoi(value));
8558 CONF("imlib_cache_flush_interval") {
8560 cimlib_set_cache_flush_interval(atoi(value));
8565 CONF("update_interval") {
8567 update_interval = strtod(value, 0);
8571 if (info.music_player_interval == 0) {
8572 // default to update_interval
8573 info.music_player_interval = update_interval;
8576 CONF("total_run_times") {
8578 total_run_times = strtod(value, 0);
8584 stuff_in_uppercase = string_to_bool(value);
8586 CONF("max_specials") {
8588 max_specials = atoi(value);
8593 CONF("max_user_text") {
8595 max_user_text = atoi(value);
8600 CONF("text_buffer_size") {
8602 text_buffer_size = atoi(value);
8603 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8604 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8605 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8613 if (output_methods & TO_X) {
8614 X11_initialisation();
8623 global_text = (char *) malloc(1);
8624 global_text[0] = '\0';
8627 unsigned int l = strlen(global_text);
8629 char buf[CONF_BUFF_SIZE];
8631 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8637 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8645 /* Check for continuation of \\-\n. */
8646 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8647 global_text[l-1] = '\0';
8651 global_text = (char *) realloc(global_text, l + bl + 1);
8652 strcat(global_text, buf);
8654 if (strlen(global_text) > max_user_text) {
8659 if (strlen(global_text) < 1) {
8660 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
8662 global_text_lines = line + 1;
8665 #ifdef TCP_PORT_MONITOR
8666 CONF("max_port_monitor_connections") {
8668 if (!value || (sscanf(value, "%d", &max) != 1)) {
8669 /* an error. use default, warn and continue. */
8670 tcp_portmon_set_max_connections(0);
8672 } else if (tcp_portmon_set_max_connections(max)) {
8673 /* max is < 0, default has been set*/
8678 CONF("if_up_strictness") {
8680 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8681 ifup_strictness = IFUP_UP;
8682 } else if (strcasecmp(value, "up") == EQUAL) {
8683 ifup_strictness = IFUP_UP;
8684 } else if (strcasecmp(value, "link") == EQUAL) {
8685 ifup_strictness = IFUP_LINK;
8686 } else if (strcasecmp(value, "address") == EQUAL) {
8687 ifup_strictness = IFUP_ADDR;
8689 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8690 ifup_strictness = IFUP_UP;
8694 CONF("temperature_unit") {
8696 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8697 } else if (set_temp_output_unit(value)) {
8698 ERR("temperature_unit: incorrect argument");
8704 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8705 char *skey = malloc(maxlength);
8706 char *svalue = malloc(maxlength);
8708 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8709 oldvalue = getenv(skey);
8710 if (oldvalue == NULL) {
8711 setenv(skey, svalue, 0);
8713 //PS: Don't free oldvalue, it's the real envvar, not a copy
8726 char *ptr = strtok(value, " ");
8729 ptr = strtok(NULL, " ");
8736 CONF("lua_draw_hook_pre") {
8738 llua_set_draw_pre_hook(value);
8743 CONF("lua_draw_hook_post") {
8745 llua_set_draw_post_hook(value);
8751 #endif /* HAVE_LUA */
8763 CONF("default_color"){}
8764 CONF3("default_shade_color", "default_shadecolor"){}
8765 CONF3("default_outline_color", "default_outlinecolor") {}
8766 CONF("own_window_colour") {}
8769 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8775 if (info.music_player_interval == 0) {
8776 // default to update_interval
8777 info.music_player_interval = update_interval;
8779 if (!global_text) { // didn't supply any text
8780 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
8785 static void load_config_file_x11(const char *f)
8790 fp = open_config_file(f);
8794 DBGP("reading contents from config file '%s'", f);
8797 char buff[CONF_BUFF_SIZE], *name, *value;
8798 int ret = do_config_step(&line, fp, buff, &name, &value);
8799 if (ret == CONF_BREAK) {
8801 } else if (ret == CONF_CONTINUE) {
8806 X11_initialisation();
8807 if (x_initialised == YES) {
8809 color0 = get_x11_color(value);
8816 X11_initialisation();
8817 if (x_initialised == YES) {
8819 color1 = get_x11_color(value);
8826 X11_initialisation();
8827 if (x_initialised == YES) {
8829 color2 = get_x11_color(value);
8836 X11_initialisation();
8837 if (x_initialised == YES) {
8839 color3 = get_x11_color(value);
8846 X11_initialisation();
8847 if (x_initialised == YES) {
8849 color4 = get_x11_color(value);
8856 X11_initialisation();
8857 if (x_initialised == YES) {
8859 color5 = get_x11_color(value);
8866 X11_initialisation();
8867 if (x_initialised == YES) {
8869 color6 = get_x11_color(value);
8876 X11_initialisation();
8877 if (x_initialised == YES) {
8879 color7 = get_x11_color(value);
8886 X11_initialisation();
8887 if (x_initialised == YES) {
8889 color8 = get_x11_color(value);
8896 X11_initialisation();
8897 if (x_initialised == YES) {
8899 color9 = get_x11_color(value);
8905 CONF("default_color") {
8906 X11_initialisation();
8907 if (x_initialised == YES) {
8909 default_fg_color = get_x11_color(value);
8915 CONF3("default_shade_color", "default_shadecolor") {
8916 X11_initialisation();
8917 if (x_initialised == YES) {
8919 default_bg_color = get_x11_color(value);
8925 CONF3("default_outline_color", "default_outlinecolor") {
8926 X11_initialisation();
8927 if (x_initialised == YES) {
8929 default_out_color = get_x11_color(value);
8936 CONF("own_window_colour") {
8937 X11_initialisation();
8938 if (x_initialised == YES) {
8940 background_colour = get_x11_color(value);
8942 ERR("Invalid colour for own_window_colour (try omitting the "
8943 "'#' for hex colours");
8949 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
8950 if(output_methods & TO_X) {
8951 X11_initialisation();
8960 #undef CONF_CONTINUE
8961 #undef CONF_BUFF_SIZE
8969 #if defined(WEATHER) && defined(XOAP)
8971 * TODO: make the xoap keys file readable from the config file
8972 * make the keys directly readable from the config file
8973 * make the xoap keys file giveable as a command line option
8975 static void load_xoap_keys(void)
8978 char *par = (char *) malloc(11 * sizeof(char));
8979 char *key = (char *) malloc(17 * sizeof(char));
8981 xoap = (char *) malloc(64 * sizeof(char));
8982 to_real_path(xoap, XOAP_FILE);
8983 fp = fopen(xoap, "r");
8985 if( fscanf(fp, "%10s %16s", par, key) == 2 ) {
8986 strcpy(xoap, "?cc=*&link=xoap&prod=xoap&par=");
8988 strcat(xoap, "&key=");
8990 strcat(xoap, "&unit=m");
9003 #endif /* WEATHER && XOAP */
9005 static void print_help(const char *prog_name) {
9006 printf("Usage: %s [OPTION]...\n"
9007 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
9008 "window. Command line options will override configurations defined in config\n"
9010 " -v, --version version\n"
9011 " -q, --quiet quiet mode\n"
9012 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
9013 " -c, --config=FILE config file to load\n"
9014 #ifdef CONFIG_OUTPUT
9015 " -C, --print-config print the builtin default config to stdout\n"
9016 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
9018 " -d, --daemonize daemonize, fork to background\n"
9019 " -h, --help help\n"
9021 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
9022 " -f, --font=FONT font to use\n"
9023 " -X, --display=DISPLAY X11 display to use\n"
9025 " -o, --own-window create own window to draw\n"
9028 " -b, --double-buffer double buffer (prevents flickering)\n"
9030 " -w, --window-id=WIN_ID window id to draw\n"
9031 " -x X x position\n"
9032 " -y Y y position\n"
9034 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
9035 " -u, --interval=SECS update interval\n"
9036 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
9041 /* : means that character before that takes an argument */
9042 static const char *getopt_string = "vVqdDt:u:i:hc:"
9052 #ifdef CONFIG_OUTPUT
9057 static const struct option longopts[] = {
9058 { "help", 0, NULL, 'h' },
9059 { "version", 0, NULL, 'V' },
9060 { "debug", 0, NULL, 'D' },
9061 { "config", 1, NULL, 'c' },
9062 #ifdef CONFIG_OUTPUT
9063 { "print-config", 0, NULL, 'C' },
9065 { "daemonize", 0, NULL, 'd' },
9067 { "alignment", 1, NULL, 'a' },
9068 { "font", 1, NULL, 'f' },
9069 { "display", 1, NULL, 'X' },
9071 { "own-window", 0, NULL, 'o' },
9074 { "double-buffer", 0, NULL, 'b' },
9076 { "window-id", 1, NULL, 'w' },
9078 { "text", 1, NULL, 't' },
9079 { "interval", 0, NULL, 'u' },
9083 void initialisation(int argc, char **argv) {
9084 struct sigaction act, oact;
9086 load_config_file(current_config);
9088 /* init specials array */
9089 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9090 ERR("failed to create specials array");
9094 if (current_mail_spool == NULL) {
9097 variable_substitute(MAIL_FILE, buf, 256);
9099 if (buf[0] != '\0') {
9100 current_mail_spool = strndup(buf, text_buffer_size);
9105 /* handle other command line arguments */
9107 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9108 || defined(__NetBSD__)
9109 optind = optreset = 1;
9114 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9115 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9116 "kvm_open")) == NULL) {
9117 CRIT_ERR(NULL, NULL, "cannot read kvm");
9122 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9130 fork_to_background = 1;
9133 global_debug_level++;
9137 set_first_font(optarg);
9140 text_alignment = string_to_alignment(optarg);
9145 disp = strdup(optarg);
9164 global_text = strndup(optarg, max_user_text);
9165 convert_escapes(global_text);
9169 update_interval = strtod(optarg, 0);
9170 if (info.music_player_interval == 0) {
9171 // default to update_interval
9172 info.music_player_interval = update_interval;
9177 total_run_times = strtod(optarg, 0);
9181 gap_x = atoi(optarg);
9185 gap_y = atoi(optarg);
9196 if (output_methods & TO_X) {
9197 load_config_file_x11(current_config);
9201 /* generate text and get initial size */
9202 extract_variable_text(global_text);
9209 if (fork_to_background) {
9214 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9221 fprintf(stderr, "\n");
9226 /* parent process */
9227 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9234 text_buffer = malloc(max_user_text);
9235 memset(text_buffer, 0, max_user_text);
9236 tmpstring1 = malloc(text_buffer_size);
9237 memset(tmpstring1, 0, text_buffer_size);
9238 tmpstring2 = malloc(text_buffer_size);
9239 memset(tmpstring2, 0, text_buffer_size);
9244 X11_create_window();
9247 /* Set signal handlers */
9248 act.sa_handler = signal_handler;
9249 sigemptyset(&act.sa_mask);
9252 act.sa_flags |= SA_RESTART;
9255 if ( sigaction(SIGINT, &act, &oact) < 0
9256 || sigaction(SIGUSR1, &act, &oact) < 0
9257 || sigaction(SIGHUP, &act, &oact) < 0
9258 || sigaction(SIGTERM, &act, &oact) < 0) {
9259 ERR("error setting signal handler: %s", strerror(errno));
9264 int main(int argc, char **argv)
9273 g_signal_pending = 0;
9274 max_user_text = MAX_USER_TEXT_DEFAULT;
9276 memset(&info, 0, sizeof(info));
9277 memset(template, 0, sizeof(template));
9280 #ifdef TCP_PORT_MONITOR
9281 /* set default connection limit */
9282 tcp_portmon_set_max_connections(0);
9285 /* handle command line parameters that don't change configs */
9287 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9288 || ((s = getenv("LANG")) && *s)) {
9289 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9291 ERR("malloc failed");
9293 for (x = 0; x < strlen(s); x++) {
9294 temp[x] = tolower(s[x]);
9297 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9303 if (!setlocale(LC_CTYPE, "")) {
9304 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9308 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9319 if (current_config) {
9320 free(current_config);
9322 current_config = strndup(optarg, max_user_text);
9325 freopen("/dev/null", "w", stderr);
9328 print_help(argv[0]);
9330 #ifdef CONFIG_OUTPUT
9337 window.window = strtol(optarg, 0, 0);
9346 /* check if specified config file is valid */
9347 if (current_config) {
9349 if (stat(current_config, &sb) ||
9350 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9351 ERR("invalid configuration file '%s'\n", current_config);
9352 free(current_config);
9357 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9359 if (!current_config) {
9360 /* load default config file */
9361 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9364 /* Try to use personal config file first */
9365 to_real_path(buf, CONFIG_FILE);
9366 if (buf[0] && (fp = fopen(buf, "r"))) {
9367 current_config = strndup(buf, max_user_text);
9371 /* Try to use system config file if personal config not readable */
9372 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9373 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9377 /* No readable config found */
9378 if (!current_config) {
9379 #ifdef CONFIG_OUTPUT
9380 current_config = strdup("==builtin==");
9381 ERR("no readable personal or system-wide config file found,"
9382 " using builtin default");
9384 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9385 #endif /* ! CONF_OUTPUT */
9389 #if defined(WEATHER) && defined(XOAP)
9390 /* Load xoap keys, if existing */
9392 #endif /* WEATHER && XOAP */
9394 #ifdef HAVE_SYS_INOTIFY_H
9395 inotify_fd = inotify_init();
9396 #endif /* HAVE_SYS_INOTIFY_H */
9398 initialisation(argc, argv);
9402 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9409 static void signal_handler(int sig)
9411 /* signal handler is light as a feather, as it should be.
9412 * we will poll g_signal_pending with each loop of conky
9413 * and do any signal processing there, NOT here. */
9414 g_signal_pending = sig;