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 (NOAA)\n"
217 " * Weather (XOAP)\n"
222 #endif /* HAVE_IWLIB */
224 " * support for IBM/Lenovo notebooks\n"
234 #endif /* CONFIG_OUTPUT */
239 " * ALSA mixer support\n"
240 #endif /* MIXER_IS_ALSA */
250 #ifdef HAVE_LUA_CAIRO
252 #endif /* HAVE_LUA_CAIRO */
253 #ifdef HAVE_LUA_IMLIB2
256 #endif /* HAVE_LUA */
262 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
267 static void X11_create_window(void);
268 static void X11_initialisation(void);
270 struct _x11_stuff_s {
274 XserverRegion region2, part;
275 int event_base, error_base;
281 static int text_start_x, text_start_y; /* text start position in window */
282 static int text_width, text_height;
297 /* display to connect to */
298 static char *disp = NULL;
302 /* struct that has all info to be shared between
303 * instances of the same text object */
304 struct information info;
306 /* path to config file */
307 char *current_config;
309 /* set to 1 if you want all text to be in uppercase */
310 static unsigned int stuff_in_uppercase;
312 /* Run how many times? */
313 static unsigned long total_run_times;
316 static int fork_to_background;
318 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
320 /* filenames for output */
321 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
322 char *append_file = NULL; FILE *append_fpointer = NULL;
324 /* xoap suffix for weather from weather.com */
326 static char *xoap = NULL;
331 static int show_graph_scale;
332 static int show_graph_range;
334 /* Position on the screen */
335 static int text_alignment;
336 static int gap_x, gap_y;
339 static int draw_borders;
340 static int draw_graph_borders;
341 static int stippled_borders;
343 static int draw_shades, draw_outline;
345 static long default_fg_color, default_bg_color, default_out_color;
347 /* create own window or draw stuff to root? */
348 static int set_transparent = 0;
351 static int own_window = 0;
352 static int background_colour = 0;
354 /* fixed size/pos is set if wm/user changes them */
355 static int fixed_size = 0, fixed_pos = 0;
358 static int minimum_width, minimum_height;
359 static int maximum_width;
364 static int sensor_device;
367 static long color0, color1, color2, color3, color4, color5, color6, color7,
370 #define MAX_TEMPLATES 10
371 static char *template[MAX_TEMPLATES];
373 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
374 unsigned int max_user_text;
376 /* maximum size of individual text buffers, ie $exec buffer size */
377 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
380 #define CODEPAGE_LENGTH 20
382 long iconv_count = 0;
383 char iconv_converting;
384 static iconv_t **iconv_cd = 0;
386 int register_iconv(iconv_t *new_iconv)
388 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
390 CRIT_ERR(NULL, NULL, "Out of memory");
392 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
393 if (!iconv_cd[iconv_count]) {
394 CRIT_ERR(NULL, NULL, "Out of memory");
396 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
401 void free_iconv(void)
406 for (i = 0; i < iconv_count; i++) {
408 iconv_close(*iconv_cd[i]);
422 /* no buffers in used memory? */
425 /* pad percentages to decimals? */
426 static int pad_percents = 0;
428 static char *global_text = 0;
429 long global_text_lines;
431 static int total_updates;
432 static int updatereset;
434 int check_contains(char *f, char *s)
437 FILE *where = open_file(f, 0);
442 while (fgets(buf1, 256, where)) {
443 if (strstr(buf1, s)) {
450 ERR("Could not open the file");
457 #define SECRIT_MULTILINE_CHAR '\x02'
459 static inline int calc_text_width(const char *s, int l)
461 if ((output_methods & TO_X) == 0) {
469 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
470 (const FcChar8 *) s, l, &gi);
472 XftTextExtents8(display, fonts[selected_font].xftfont,
473 (const FcChar8 *) s, l, &gi);
479 return XTextWidth(fonts[selected_font].font, s, l);
484 /* formatted text to render on screen, generated in generate_text(),
485 * drawn in draw_stuff() */
487 static char *text_buffer;
489 /* quite boring functions */
491 static inline void for_each_line(char *b, int f(char *, int))
494 int special_index = 0; /* specials index */
496 for (ps = b, pe = b; *pe; pe++) {
499 special_index = f(ps, special_index);
506 f(ps, special_index);
510 static void convert_escapes(char *buf)
512 char *p = buf, *s = buf;
519 } else if (*s == '\\') {
530 /* Prints anything normally printed with snprintf according to the current value
531 * of use_spacer. Actually slightly more flexible than snprintf, as you can
532 * safely specify the destination buffer as one of your inputs. */
533 int spaced_print(char *buf, int size, const char *format, int width, ...)
542 tempbuf = malloc(size * sizeof(char));
544 // Passes the varargs along to vsnprintf
545 va_start(argp, width);
546 vsnprintf(tempbuf, size, format, argp);
549 switch (use_spacer) {
551 len = snprintf(buf, size, "%s", tempbuf);
554 len = snprintf(buf, size, "%*s", width, tempbuf);
557 len = snprintf(buf, size, "%-*s", width, tempbuf);
564 /* print percentage values
566 * - i.e., unsigned values between 0 and 100
567 * - respect the value of pad_percents */
568 static int percent_print(char *buf, int size, unsigned value)
570 return spaced_print(buf, size, "%u", pad_percents, value);
573 /* converts from bytes to human readable format (K, M, G, T)
575 * The algorithm always divides by 1024, as unit-conversion of byte
576 * counts suggests. But for output length determination we need to
577 * compare with 1000 here, as we print in decimal form. */
578 static void human_readable(long long num, char *buf, int size)
580 const char **suffix = suffixes;
586 /* Possibly just output as usual, for example for stdout usage */
587 if (!format_human_readable) {
588 spaced_print(buf, size, "%d", 6, round_to_int(num));
599 if (llabs(num) < 1000LL) {
600 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
604 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
612 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
614 * The goal is to always have a significance of 3, by
615 * adjusting the decimal part of the number. Sample output:
619 * so the point of alignment resides between number and unit. The
620 * upside of this is that there is minimal padding necessary, though
621 * there should be a way to make alignment take place at the decimal
622 * dot (then with fixed width decimal part).
624 * Note the repdigits below: when given a precision value, printf()
625 * rounds the float to it, not just cuts off the remaining digits. So
626 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
627 * printed with a precision of 0. Yay. */
629 precision = 0; /* print 100-999 without decimal part */
631 precision = 1; /* print 10-99 with one decimal place */
633 precision = 2; /* print 0-9 with two decimal places */
635 spaced_print(buf, size, format, width, precision, fnum, *suffix);
638 /* global object list root element */
639 static struct text_object global_root_object;
641 static inline void read_exec(const char *data, char *buf, const int size)
643 FILE *fp = popen(data, "r");
644 int length = fread(buf, 1, size, fp);
648 if (length > 0 && buf[length - 1] == '\n') {
649 buf[length - 1] = '\0';
653 void *threaded_exec(void *) __attribute__((noreturn));
655 void *threaded_exec(void *arg)
658 struct text_object *obj = (struct text_object *)arg;
661 buff = malloc(text_buffer_size);
662 read_exec(obj->data.texeci.cmd, buff,
671 timed_thread_lock(obj->data.texeci.p_timed_thread);
672 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
673 timed_thread_unlock(obj->data.texeci.p_timed_thread);
675 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
676 timed_thread_exit(obj->data.texeci.p_timed_thread);
682 static struct text_object *new_text_object_internal(void)
684 struct text_object *obj = malloc(sizeof(struct text_object));
685 memset(obj, 0, sizeof(struct text_object));
690 * Frees the list of text objects root points to. When internal = 1, it won't
691 * free global objects.
693 static void free_text_objects(struct text_object *root, int internal)
695 struct text_object *obj;
701 #define data obj->data
702 for (obj = root->prev; obj; obj = root->prev) {
703 root->prev = obj->prev;
709 #endif /* !__OpenBSD__ */
714 close(data.sysfs.fd);
716 #endif /* __linux__ */
718 free(data.read_tcp.host);
725 free(data.tztime.tz);
726 free(data.tztime.fmt);
729 free(data.mboxscan.args);
730 free(data.mboxscan.output);
735 case OBJ_unseen_mails:
736 case OBJ_flagged_mails:
737 case OBJ_unflagged_mails:
738 case OBJ_forwarded_mails:
739 case OBJ_unforwarded_mails:
740 case OBJ_replied_mails:
741 case OBJ_unreplied_mails:
742 case OBJ_draft_mails:
743 case OBJ_trashed_mails:
744 free(data.local_mail.box);
746 case OBJ_imap_unseen:
751 case OBJ_imap_messages:
756 case OBJ_pop3_unseen:
768 free_text_objects(obj->sub, 1);
771 case OBJ_if_existing:
774 free(data.ifblock.s);
775 free(data.ifblock.str);
778 free(data.tail.logfile);
779 free(data.tail.buffer);
795 case OBJ_iconv_start:
800 case OBJ_disk_protect:
804 free(data.ifblock.s);
805 free(data.ifblock.str);
808 if (info.gw_info.iface) {
809 free(info.gw_info.iface);
810 info.gw_info.iface = 0;
812 if (info.gw_info.ip) {
813 free(info.gw_info.ip);
817 case OBJ_ioscheduler:
822 #if (defined(__FreeBSD__) || defined(__linux__))
824 free(data.ifblock.s);
825 free(data.ifblock.str);
829 case OBJ_xmms2_artist:
830 if (info.xmms2.artist) {
831 free(info.xmms2.artist);
832 info.xmms2.artist = 0;
835 case OBJ_xmms2_album:
836 if (info.xmms2.album) {
837 free(info.xmms2.album);
838 info.xmms2.album = 0;
841 case OBJ_xmms2_title:
842 if (info.xmms2.title) {
843 free(info.xmms2.title);
844 info.xmms2.title = 0;
847 case OBJ_xmms2_genre:
848 if (info.xmms2.genre) {
849 free(info.xmms2.genre);
850 info.xmms2.genre = 0;
853 case OBJ_xmms2_comment:
854 if (info.xmms2.comment) {
855 free(info.xmms2.comment);
856 info.xmms2.comment = 0;
860 if (info.xmms2.url) {
861 free(info.xmms2.url);
866 if (info.xmms2.date) {
867 free(info.xmms2.date);
871 case OBJ_xmms2_status:
872 if (info.xmms2.status) {
873 free(info.xmms2.status);
874 info.xmms2.status = 0;
877 case OBJ_xmms2_playlist:
878 if (info.xmms2.playlist) {
879 free(info.xmms2.playlist);
880 info.xmms2.playlist = 0;
883 case OBJ_xmms2_smart:
884 if (info.xmms2.artist) {
885 free(info.xmms2.artist);
886 info.xmms2.artist = 0;
888 if (info.xmms2.title) {
889 free(info.xmms2.title);
890 info.xmms2.title = 0;
892 if (info.xmms2.url) {
893 free(info.xmms2.url);
900 case OBJ_bmpx_artist:
904 case OBJ_bmpx_bitrate:
914 free(data.rss.action);
919 free(data.weather.uri);
920 free(data.weather.data_type);
932 #endif /* HAVE_LUA */
939 case OBJ_battery_short:
942 case OBJ_battery_time:
945 #endif /* !__OpenBSD__ */
953 free(data.execi.cmd);
954 free(data.execi.buffer);
957 free(data.texeci.cmd);
958 free(data.texeci.buffer);
969 if (info.first_process && !internal) {
970 free_all_processes();
971 info.first_process = NULL;
973 if (data.top.s) free(data.top.s);
977 free(data.hddtemp.dev);
978 free(data.hddtemp.addr);
979 if (data.hddtemp.temp)
980 free(data.hddtemp.temp);
983 case OBJ_entropy_avail:
984 case OBJ_entropy_perc:
985 case OBJ_entropy_poolsize:
986 case OBJ_entropy_bar:
989 if (info.users.names) {
990 free(info.users.names);
991 info.users.names = 0;
995 if (info.users.terms) {
996 free(info.users.terms);
997 info.users.terms = 0;
1000 case OBJ_user_times:
1001 if (info.users.times) {
1002 free(info.users.times);
1003 info.users.times = 0;
1008 case OBJ_smapi_bat_perc:
1009 case OBJ_smapi_bat_temp:
1010 case OBJ_smapi_bat_power:
1013 case OBJ_if_smapi_bat_installed:
1014 free(data.ifblock.s);
1015 free(data.ifblock.str);
1024 case OBJ_mpd_artist:
1026 case OBJ_mpd_random:
1027 case OBJ_mpd_repeat:
1029 case OBJ_mpd_bitrate:
1030 case OBJ_mpd_status:
1032 case OBJ_mpd_elapsed:
1033 case OBJ_mpd_length:
1037 case OBJ_mpd_percent:
1039 case OBJ_if_mpd_playing:
1047 case OBJ_moc_artist:
1050 case OBJ_moc_totaltime:
1051 case OBJ_moc_timeleft:
1052 case OBJ_moc_curtime:
1053 case OBJ_moc_bitrate:
1060 free_text_objects(obj->sub, 1);
1064 free(data.scroll.text);
1065 free_text_objects(obj->sub, 1);
1069 free(data.combine.left);
1070 free(data.combine.seperation);
1071 free(data.combine.right);
1072 free_text_objects(obj->sub, 1);
1077 case OBJ_apcupsd_name:
1078 case OBJ_apcupsd_model:
1079 case OBJ_apcupsd_upsmode:
1080 case OBJ_apcupsd_cable:
1081 case OBJ_apcupsd_status:
1082 case OBJ_apcupsd_linev:
1083 case OBJ_apcupsd_load:
1084 case OBJ_apcupsd_loadbar:
1086 case OBJ_apcupsd_loadgraph:
1087 case OBJ_apcupsd_loadgauge:
1089 case OBJ_apcupsd_charge:
1090 case OBJ_apcupsd_timeleft:
1091 case OBJ_apcupsd_temp:
1092 case OBJ_apcupsd_lastxfer:
1094 #endif /* APCUPSD */
1097 case OBJ_desktop_number:
1098 case OBJ_desktop_name:
1099 if(info.x11.desktop.name) {
1100 free(info.x11.desktop.name);
1101 info.x11.desktop.name = NULL;
1103 if(info.x11.desktop.all_names) {
1104 free(info.x11.desktop.all_names);
1105 info.x11.desktop.all_names = NULL;
1116 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1121 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1122 *a = mixer_init(buf1);
1123 scan_bar(arg + n, w, h);
1125 *a = mixer_init(NULL);
1126 scan_bar(arg, w, h);
1131 /* strip a leading /dev/ if any, following symlinks first
1133 * BEWARE: this function returns a pointer to static content
1134 * which gets overwritten in consecutive calls. I.e.:
1135 * this function is NOT reentrant.
1137 static const char *dev_name(const char *path)
1139 static char buf[255]; /* should be enough for pathnames */
1145 #define DEV_NAME(x) \
1146 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1147 if ((buflen = readlink(path, buf, 254)) == -1)
1148 return DEV_NAME(path);
1150 return DEV_NAME(buf);
1154 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1159 if (obj->data.top.was_parsed) {
1162 obj->data.top.was_parsed = 1;
1164 if (arg && !obj->data.top.s) {
1165 obj->data.top.s = strndup(arg, text_buffer_size);
1168 need_mask |= (1 << INFO_TOP);
1171 obj->type = OBJ_top;
1173 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1174 obj->type = OBJ_top_mem;
1176 } else if (strcmp(&s[3], "_time") == EQUAL) {
1177 obj->type = OBJ_top_time;
1180 } else if (strcmp(&s[3], "_io") == EQUAL) {
1181 obj->type = OBJ_top_io;
1186 ERR("Must be top, top_mem, top_time or top_io");
1188 ERR("Must be top, top_mem or top_time");
1194 ERR("top needs arguments");
1198 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1199 if (strcmp(buf, "name") == EQUAL) {
1200 obj->data.top.type = TOP_NAME;
1201 } else if (strcmp(buf, "cpu") == EQUAL) {
1202 obj->data.top.type = TOP_CPU;
1203 } else if (strcmp(buf, "pid") == EQUAL) {
1204 obj->data.top.type = TOP_PID;
1205 } else if (strcmp(buf, "mem") == EQUAL) {
1206 obj->data.top.type = TOP_MEM;
1207 } else if (strcmp(buf, "time") == EQUAL) {
1208 obj->data.top.type = TOP_TIME;
1209 } else if (strcmp(buf, "mem_res") == EQUAL) {
1210 obj->data.top.type = TOP_MEM_RES;
1211 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1212 obj->data.top.type = TOP_MEM_VSIZE;
1214 } else if (strcmp(buf, "io_read") == EQUAL) {
1215 obj->data.top.type = TOP_READ_BYTES;
1216 } else if (strcmp(buf, "io_write") == EQUAL) {
1217 obj->data.top.type = TOP_WRITE_BYTES;
1218 } else if (strcmp(buf, "io_perc") == EQUAL) {
1219 obj->data.top.type = TOP_IO_PERC;
1222 ERR("invalid type arg for top");
1224 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1225 "io_read, io_write, io_perc");
1227 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1231 if (n < 1 || n > 10) {
1232 ERR("invalid num arg for top. Must be between 1 and 10.");
1235 obj->data.top.num = n - 1;
1238 ERR("invalid argument count for top");
1244 long current_text_color;
1246 /* construct_text_object() creates a new text_object */
1247 static struct text_object *construct_text_object(const char *s,
1248 const char *arg, long line, char allow_threaded, void **ifblock_opaque, void *free_at_crash)
1250 // struct text_object *obj = new_text_object();
1251 struct text_object *obj = new_text_object_internal();
1255 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1256 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1257 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1258 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1259 obj_be_ifblock_if(ifblock_opaque, obj); {
1260 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1261 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1262 #define END } } else
1264 #define SIZE_DEFAULTS(arg) { \
1265 obj->a = default_##arg##_width; \
1266 obj->b = default_##arg##_height; \
1271 obj->type = OBJ_color;
1272 obj->data.l = get_x11_color(s);
1276 OBJ(freq, INFO_FREQ)
1279 obj->data.i = open_acpi_temperature(arg);
1280 END OBJ(acpiacadapter, 0)
1281 END OBJ(freq, INFO_FREQ)
1282 #endif /* !__OpenBSD__ */
1284 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1285 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1286 obj->data.cpu_index = 1;
1287 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1288 "Displaying the clock for CPU 1."); */
1290 obj->data.cpu_index = atoi(&arg[0]);
1293 END OBJ(freq_g, INFO_FREQ)
1295 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1296 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1297 obj->data.cpu_index = 1;
1298 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1299 "CPUs! Displaying the clock for CPU 1."); */
1301 obj->data.cpu_index = atoi(&arg[0]);
1304 END OBJ(read_tcp, 0)
1306 obj->data.read_tcp.host = malloc(text_buffer_size);
1307 sscanf(arg, "%s", obj->data.read_tcp.host);
1308 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1309 if(obj->data.read_tcp.port == 0) {
1310 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1311 strcpy(obj->data.read_tcp.host,"localhost");
1313 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1314 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1315 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1318 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1320 #if defined(__linux__)
1321 END OBJ(voltage_mv, 0)
1323 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1324 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1325 obj->data.cpu_index = 1;
1326 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1327 "CPUs! Displaying voltage for CPU 1."); */
1329 obj->data.cpu_index = atoi(&arg[0]);
1332 END OBJ(voltage_v, 0)
1334 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1335 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1336 obj->data.cpu_index = 1;
1337 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1338 "CPUs! Displaying voltage for CPU 1."); */
1340 obj->data.cpu_index = atoi(&arg[0]);
1345 END OBJ(wireless_essid, INFO_NET)
1347 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1349 // default to DEFAULTNETDEV
1350 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1351 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1354 END OBJ(wireless_mode, INFO_NET)
1356 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1358 // default to DEFAULTNETDEV
1359 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1360 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1363 END OBJ(wireless_bitrate, INFO_NET)
1365 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1367 // default to DEFAULTNETDEV
1368 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1369 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1372 END OBJ(wireless_ap, INFO_NET)
1374 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1376 // default to DEFAULTNETDEV
1377 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1378 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1381 END OBJ(wireless_link_qual, INFO_NET)
1383 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1385 // default to DEFAULTNETDEV
1386 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1387 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1390 END OBJ(wireless_link_qual_max, INFO_NET)
1392 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1394 // default to DEFAULTNETDEV
1395 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1396 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1399 END OBJ(wireless_link_qual_perc, INFO_NET)
1401 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1403 // default to DEFAULTNETDEV
1404 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1405 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1408 END OBJ(wireless_link_bar, INFO_NET)
1411 arg = scan_bar(arg, &obj->a, &obj->b);
1412 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1414 // default to DEFAULTNETDEV
1415 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1416 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1419 #endif /* HAVE_IWLIB */
1421 #endif /* __linux__ */
1429 sscanf(arg, "%63s", bat);
1431 strcpy(bat, "BAT0");
1433 obj->data.s = strndup(bat, text_buffer_size);
1434 END OBJ(battery_short, 0)
1438 sscanf(arg, "%63s", bat);
1440 strcpy(bat, "BAT0");
1442 obj->data.s = strndup(bat, text_buffer_size);
1443 END OBJ(battery_time, 0)
1447 sscanf(arg, "%63s", bat);
1449 strcpy(bat, "BAT0");
1451 obj->data.s = strndup(bat, text_buffer_size);
1452 END OBJ(battery_percent, 0)
1456 sscanf(arg, "%63s", bat);
1458 strcpy(bat, "BAT0");
1460 obj->data.s = strndup(bat, text_buffer_size);
1461 END OBJ(battery_bar, 0)
1466 arg = scan_bar(arg, &obj->a, &obj->b);
1467 sscanf(arg, "%63s", bat);
1469 strcpy(bat, "BAT0");
1471 obj->data.s = strndup(bat, text_buffer_size);
1472 #endif /* !__OpenBSD__ */
1474 #if defined(__linux__)
1475 END OBJ(disk_protect, 0)
1477 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1479 CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1480 END OBJ(i8k_version, INFO_I8K)
1481 END OBJ(i8k_bios, INFO_I8K)
1482 END OBJ(i8k_serial, INFO_I8K)
1483 END OBJ(i8k_cpu_temp, INFO_I8K)
1484 END OBJ(i8k_left_fan_status, INFO_I8K)
1485 END OBJ(i8k_right_fan_status, INFO_I8K)
1486 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1487 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1488 END OBJ(i8k_ac_status, INFO_I8K)
1489 END OBJ(i8k_buttons_status, INFO_I8K)
1492 END OBJ(ibm_temps, 0)
1494 CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1496 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1497 obj->data.sensor = 0;
1498 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1499 "Using 0 (CPU temp sensor).");
1501 obj->data.sensor = atoi(&arg[0]);
1502 END OBJ(ibm_volume, 0)
1503 END OBJ(ibm_brightness, 0)
1505 /* information from sony_laptop kernel module
1506 * /sys/devices/platform/sony-laptop */
1507 END OBJ(sony_fanspeed, 0)
1508 END OBJ_IF(if_gw, INFO_GW)
1509 END OBJ(ioscheduler, 0)
1511 CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1514 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1515 END OBJ(laptop_mode, 0)
1516 END OBJ(pb_battery, 0)
1517 if (arg && strcmp(arg, "status") == EQUAL) {
1518 obj->data.i = PB_BATT_STATUS;
1519 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1520 obj->data.i = PB_BATT_PERCENT;
1521 } else if (arg && strcmp(arg, "time") == EQUAL) {
1522 obj->data.i = PB_BATT_TIME;
1524 ERR("pb_battery: needs one argument: status, percent or time");
1529 #endif /* __linux__ */
1530 #if (defined(__FreeBSD__) || defined(__linux__))
1531 END OBJ_IF(if_up, 0)
1533 ERR("if_up needs an argument");
1534 obj->data.ifblock.s = 0;
1536 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1539 #if defined(__OpenBSD__)
1540 END OBJ(obsd_sensors_temp, 0)
1542 CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1544 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1545 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1546 obj->data.sensor = 0;
1547 ERR("Invalid temperature sensor number!");
1549 obj->data.sensor = atoi(&arg[0]);
1550 END OBJ(obsd_sensors_fan, 0)
1552 CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1555 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1556 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1557 obj->data.sensor = 0;
1558 ERR("Invalid fan sensor number!");
1560 obj->data.sensor = atoi(&arg[0]);
1561 END OBJ(obsd_sensors_volt, 0)
1563 CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1566 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1567 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1568 obj->data.sensor = 0;
1569 ERR("Invalid voltage sensor number!");
1571 obj->data.sensor = atoi(&arg[0]);
1572 END OBJ(obsd_vendor, 0)
1573 END OBJ(obsd_product, 0)
1574 #endif /* __OpenBSD__ */
1575 END OBJ(buffers, INFO_BUFFERS)
1576 END OBJ(cached, INFO_BUFFERS)
1577 #define SCAN_CPU(__arg, __var) { \
1579 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1580 __arg += __offset; \
1584 END OBJ(cpu, INFO_CPU)
1585 SCAN_CPU(arg, obj->data.cpu_index);
1586 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1588 END OBJ(cpugauge, INFO_CPU)
1589 SIZE_DEFAULTS(gauge);
1590 SCAN_CPU(arg, obj->data.cpu_index);
1591 scan_gauge(arg, &obj->a, &obj->b);
1592 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1594 END OBJ(cpubar, INFO_CPU)
1596 SCAN_CPU(arg, obj->data.cpu_index);
1597 scan_bar(arg, &obj->a, &obj->b);
1598 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1600 END OBJ(cpugraph, INFO_CPU)
1602 SIZE_DEFAULTS(graph);
1603 SCAN_CPU(arg, obj->data.cpu_index);
1604 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1605 &obj->e, &obj->char_a, &obj->char_b);
1606 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1608 END OBJ(loadgraph, INFO_LOADAVG)
1610 SIZE_DEFAULTS(graph);
1611 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1612 &obj->e, &obj->char_a, &obj->char_b);
1616 r = sscanf(arg, "%d", &a);
1618 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1622 END OBJ(diskio, INFO_DISKIO)
1623 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1624 END OBJ(diskio_read, INFO_DISKIO)
1625 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1626 END OBJ(diskio_write, INFO_DISKIO)
1627 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1629 END OBJ(diskiograph, INFO_DISKIO)
1631 SIZE_DEFAULTS(graph);
1632 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1633 &obj->e, &obj->char_a, &obj->char_b);
1635 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1637 END OBJ(diskiograph_read, INFO_DISKIO)
1639 SIZE_DEFAULTS(graph);
1640 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1641 &obj->e, &obj->char_a, &obj->char_b);
1643 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1645 END OBJ(diskiograph_write, INFO_DISKIO)
1647 SIZE_DEFAULTS(graph);
1648 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1649 &obj->e, &obj->char_a, &obj->char_b);
1651 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1656 if (output_methods & TO_X) {
1657 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1658 current_text_color = obj->data.l;
1662 obj->data.l = color0;
1663 current_text_color = obj->data.l;
1665 obj->data.l = color1;
1666 current_text_color = obj->data.l;
1668 obj->data.l = color2;
1669 current_text_color = obj->data.l;
1671 obj->data.l = color3;
1672 current_text_color = obj->data.l;
1674 obj->data.l = color4;
1675 current_text_color = obj->data.l;
1677 obj->data.l = color5;
1678 current_text_color = obj->data.l;
1680 obj->data.l = color6;
1681 current_text_color = obj->data.l;
1683 obj->data.l = color7;
1684 current_text_color = obj->data.l;
1686 obj->data.l = color8;
1687 current_text_color = obj->data.l;
1689 obj->data.l = color9;
1690 current_text_color = obj->data.l;
1693 obj->data.s = scan_font(arg);
1695 END OBJ(conky_version, 0)
1696 END OBJ(conky_build_date, 0)
1697 END OBJ(conky_build_arch, 0)
1698 END OBJ(downspeed, INFO_NET)
1700 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1702 // default to DEFAULTNETDEV
1703 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1704 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1707 END OBJ(downspeedf, INFO_NET)
1709 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1711 // default to DEFAULTNETDEV
1712 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1713 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1717 END OBJ(downspeedgraph, INFO_NET)
1719 SIZE_DEFAULTS(graph);
1720 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1721 &obj->e, &obj->char_a, &obj->char_b);
1723 // default to DEFAULTNETDEV
1724 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1725 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1729 obj_be_ifblock_else(ifblock_opaque, obj);
1731 obj_be_ifblock_endif(ifblock_opaque, obj);
1733 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1735 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1738 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1740 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1743 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1745 END OBJ(execgauge, 0)
1746 SIZE_DEFAULTS(gauge);
1747 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1748 END OBJ(execgraph, 0)
1749 SIZE_DEFAULTS(graph);
1750 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1752 END OBJ(execibar, 0)
1756 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1759 ERR("${execibar <interval> command}");
1760 obj->type = OBJ_text;
1761 snprintf(buf, 256, "${%s}", s);
1762 obj->data.s = strndup(buf, text_buffer_size);
1764 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1767 END OBJ(execigraph, 0)
1769 SIZE_DEFAULTS(graph);
1771 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1774 ERR("${execigraph <interval> command}");
1775 obj->type = OBJ_text;
1776 snprintf(buf, 256, "${%s}", s);
1777 obj->data.s = strndup(buf, text_buffer_size);
1779 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1781 END OBJ(execigauge, 0)
1783 SIZE_DEFAULTS(gauge);
1785 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1788 ERR("${execigauge <interval> command}");
1789 obj->type = OBJ_text;
1790 snprintf(buf, 256, "${%s}", s);
1791 obj->data.s = strndup(buf, text_buffer_size);
1793 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1799 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1802 ERR("${execi <interval> command}");
1803 obj->type = OBJ_text;
1804 snprintf(buf, 256, "${%s}", s);
1805 obj->data.s = strndup(buf, text_buffer_size);
1807 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1808 obj->data.execi.buffer = malloc(text_buffer_size);
1813 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1816 ERR("${execi <interval> command}");
1817 obj->type = OBJ_text;
1818 snprintf(buf, 256, "${%s}", s);
1819 obj->data.s = strndup(buf, text_buffer_size);
1821 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1822 obj->data.execi.buffer = malloc(text_buffer_size);
1824 END OBJ_THREAD(texeci, 0)
1827 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1830 ERR("${texeci <interval> command}");
1831 obj->type = OBJ_text;
1832 snprintf(buf, 256, "${%s}", s);
1833 obj->data.s = strndup(buf, text_buffer_size);
1835 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1836 obj->data.texeci.buffer = malloc(text_buffer_size);
1838 obj->data.texeci.p_timed_thread = NULL;
1839 END OBJ(pre_exec, 0)
1840 obj->type = OBJ_text;
1844 read_exec(arg, buf, sizeof(buf));
1845 obj->data.s = strndup(buf, text_buffer_size);
1847 obj->data.s = strndup("", text_buffer_size);
1850 END OBJ(fs_bar, INFO_FS)
1852 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1854 while (isspace(*arg)) {
1863 obj->data.fsbar.fs = prepare_fs_stat(arg);
1864 END OBJ(fs_bar_free, INFO_FS)
1866 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1868 while (isspace(*arg)) {
1878 obj->data.fsbar.fs = prepare_fs_stat(arg);
1879 END OBJ(fs_free, INFO_FS)
1883 obj->data.fs = prepare_fs_stat(arg);
1884 END OBJ(fs_used_perc, INFO_FS)
1888 obj->data.fs = prepare_fs_stat(arg);
1889 END OBJ(fs_free_perc, INFO_FS)
1893 obj->data.fs = prepare_fs_stat(arg);
1894 END OBJ(fs_size, INFO_FS)
1898 obj->data.fs = prepare_fs_stat(arg);
1899 END OBJ(fs_type, INFO_FS)
1903 obj->data.fs = prepare_fs_stat(arg);
1904 END OBJ(fs_used, INFO_FS)
1908 obj->data.fs = prepare_fs_stat(arg);
1910 obj->data.i = arg ? atoi(arg) : 1;
1911 END OBJ(nameserver, INFO_DNS)
1912 obj->data.i = arg ? atoi(arg) : 0;
1914 obj->data.i = arg ? atoi(arg) : 1;
1916 obj->data.i = arg ? atoi(arg) : 1;
1920 ERR("goto needs arguments");
1921 obj->type = OBJ_text;
1922 obj->data.s = strndup("${goto}", text_buffer_size);
1926 obj->data.i = atoi(arg);
1932 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1933 sscanf(arg, "%d", &b);
1939 obj->data.pair.a = a;
1940 obj->data.pair.b = b;
1943 END OBJ(i2c, INFO_SYSFS)
1944 char buf1[64], buf2[64];
1945 float factor, offset;
1949 ERR("i2c needs arguments");
1950 obj->type = OBJ_text;
1951 // obj->data.s = strndup("${i2c}", text_buffer_size);
1955 #define HWMON_RESET() {\
1960 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1961 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1962 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1963 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1966 ERR("i2c failed to parse arguments");
1967 obj->type = OBJ_text;
1970 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1971 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
1972 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1973 strncpy(obj->data.sysfs.type, buf2, 63);
1974 obj->data.sysfs.factor = factor;
1975 obj->data.sysfs.offset = offset;
1977 END OBJ(platform, INFO_SYSFS)
1978 char buf1[64], buf2[64];
1979 float factor, offset;
1983 ERR("platform needs arguments");
1984 obj->type = OBJ_text;
1988 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1989 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1990 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1991 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1994 ERR("platform failed to parse arguments");
1995 obj->type = OBJ_text;
1998 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1999 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
2000 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2001 strncpy(obj->data.sysfs.type, buf2, 63);
2002 obj->data.sysfs.factor = factor;
2003 obj->data.sysfs.offset = offset;
2005 END OBJ(hwmon, INFO_SYSFS)
2006 char buf1[64], buf2[64];
2007 float factor, offset;
2011 ERR("hwmon needs argumanets");
2012 obj->type = OBJ_text;
2016 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2017 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2018 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2019 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2024 ERR("hwmon failed to parse arguments");
2025 obj->type = OBJ_text;
2028 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2029 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2030 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2031 strncpy(obj->data.sysfs.type, buf2, 63);
2032 obj->data.sysfs.factor = factor;
2033 obj->data.sysfs.offset = offset;
2035 #endif /* !__OpenBSD__ */
2038 /* we have four different types of top (top, top_mem, top_time and top_io). To
2039 * avoid having almost-same code four times, we have this special
2041 if (strncmp(s, "top", 3) == EQUAL) {
2042 if (!parse_top_args(s, arg, obj)) {
2045 } else OBJ(addr, INFO_NET)
2047 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2049 // default to DEFAULTNETDEV
2050 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2051 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2054 #if defined(__linux__)
2055 END OBJ(addrs, INFO_NET)
2057 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2059 // default to DEFAULTNETDEV
2060 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2061 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2064 #endif /* __linux__ */
2066 if (init_tail_object(obj, arg)) {
2067 obj->type = OBJ_text;
2068 obj->data.s = strndup("${tail}", text_buffer_size);
2071 if (init_head_object(obj, arg)) {
2072 obj->type = OBJ_text;
2073 obj->data.s = strndup("${head}", text_buffer_size);
2077 obj->data.s = strndup(arg, text_buffer_size);
2079 CRIT_ERR(obj, free_at_crash, "lines needs a argument");
2083 obj->data.s = strndup(arg, text_buffer_size);
2085 CRIT_ERR(obj, free_at_crash, "words needs a argument");
2087 END OBJ(loadavg, INFO_LOADAVG)
2088 int a = 1, b = 2, c = 3, r = 3;
2091 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2092 if (r >= 3 && (c < 1 || c > 3)) {
2095 if (r >= 2 && (b < 1 || b > 3)) {
2098 if (r >= 1 && (a < 1 || a > 3)) {
2102 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2103 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2104 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2105 END OBJ_IF(if_empty, 0)
2107 ERR("if_empty needs an argument");
2108 obj->data.ifblock.s = 0;
2110 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2111 obj->sub = malloc(sizeof(struct text_object));
2112 extract_variable_text_internal(obj->sub,
2113 obj->data.ifblock.s, 0);
2115 END OBJ_IF(if_match, 0)
2117 ERR("if_match needs arguments");
2118 obj->data.ifblock.s = 0;
2120 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2121 obj->sub = malloc(sizeof(struct text_object));
2122 extract_variable_text_internal(obj->sub,
2123 obj->data.ifblock.s, 0);
2125 END OBJ_IF(if_existing, 0)
2127 ERR("if_existing needs an argument or two");
2128 obj->data.ifblock.s = NULL;
2129 obj->data.ifblock.str = NULL;
2131 char buf1[256], buf2[256];
2132 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2135 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2136 obj->data.ifblock.str = NULL;
2138 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2139 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2142 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2143 END OBJ_IF(if_mounted, 0)
2145 ERR("if_mounted needs an argument");
2146 obj->data.ifblock.s = 0;
2148 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2151 END OBJ_IF(if_running, INFO_TOP)
2153 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2155 END OBJ_IF(if_running, 0)
2159 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2160 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2163 ERR("if_running needs an argument");
2164 obj->data.ifblock.s = 0;
2170 char box[256], dst[256];
2174 /* Kapil: Changed from MAIL_FILE to
2175 current_mail_spool since the latter
2176 is a copy of the former if undefined
2177 but the latter should take precedence
2179 strncpy(box, current_mail_spool, sizeof(box));
2181 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2183 strncpy(box, arg, sizeof(box));
2187 variable_substitute(box, dst, sizeof(dst));
2188 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2189 obj->data.local_mail.interval = n1;
2190 END OBJ(new_mails, 0)
2192 char box[256], dst[256];
2196 strncpy(box, current_mail_spool, sizeof(box));
2198 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2200 strncpy(box, arg, sizeof(box));
2204 variable_substitute(box, dst, sizeof(dst));
2205 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2206 obj->data.local_mail.interval = n1;
2207 END OBJ(seen_mails, 0)
2209 char box[256], dst[256];
2213 strncpy(box, current_mail_spool, sizeof(box));
2215 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2217 strncpy(box, arg, sizeof(box));
2221 variable_substitute(box, dst, sizeof(dst));
2222 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2223 obj->data.local_mail.interval = n1;
2224 END OBJ(unseen_mails, 0)
2226 char box[256], dst[256];
2230 strncpy(box, current_mail_spool, sizeof(box));
2232 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2234 strncpy(box, arg, sizeof(box));
2238 variable_substitute(box, dst, sizeof(dst));
2239 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2240 obj->data.local_mail.interval = n1;
2241 END OBJ(flagged_mails, 0)
2243 char box[256], dst[256];
2247 strncpy(box, current_mail_spool, sizeof(box));
2249 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2251 strncpy(box, arg, sizeof(box));
2255 variable_substitute(box, dst, sizeof(dst));
2256 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2257 obj->data.local_mail.interval = n1;
2258 END OBJ(unflagged_mails, 0)
2260 char box[256], dst[256];
2264 strncpy(box, current_mail_spool, sizeof(box));
2266 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2268 strncpy(box, arg, sizeof(box));
2272 variable_substitute(box, dst, sizeof(dst));
2273 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2274 obj->data.local_mail.interval = n1;
2275 END OBJ(forwarded_mails, 0)
2277 char box[256], dst[256];
2281 strncpy(box, current_mail_spool, sizeof(box));
2283 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2285 strncpy(box, arg, sizeof(box));
2289 variable_substitute(box, dst, sizeof(dst));
2290 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2291 obj->data.local_mail.interval = n1;
2292 END OBJ(unforwarded_mails, 0)
2294 char box[256], dst[256];
2298 strncpy(box, current_mail_spool, sizeof(box));
2300 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2302 strncpy(box, arg, sizeof(box));
2306 variable_substitute(box, dst, sizeof(dst));
2307 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2308 obj->data.local_mail.interval = n1;
2309 END OBJ(replied_mails, 0)
2311 char box[256], dst[256];
2315 strncpy(box, current_mail_spool, sizeof(box));
2317 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2319 strncpy(box, arg, sizeof(box));
2323 variable_substitute(box, dst, sizeof(dst));
2324 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2325 obj->data.local_mail.interval = n1;
2326 END OBJ(unreplied_mails, 0)
2328 char box[256], dst[256];
2332 strncpy(box, current_mail_spool, sizeof(box));
2334 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2336 strncpy(box, arg, sizeof(box));
2340 variable_substitute(box, dst, sizeof(dst));
2341 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2342 obj->data.local_mail.interval = n1;
2343 END OBJ(draft_mails, 0)
2345 char box[256], dst[256];
2349 strncpy(box, current_mail_spool, sizeof(box));
2351 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2353 strncpy(box, arg, sizeof(box));
2357 variable_substitute(box, dst, sizeof(dst));
2358 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2359 obj->data.local_mail.interval = n1;
2360 END OBJ(trashed_mails, 0)
2362 char box[256], dst[256];
2366 strncpy(box, current_mail_spool, sizeof(box));
2368 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2370 strncpy(box, arg, sizeof(box));
2374 variable_substitute(box, dst, sizeof(dst));
2375 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2376 obj->data.local_mail.interval = n1;
2377 END OBJ(mboxscan, 0)
2378 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2379 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2380 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2381 obj->data.mboxscan.output[0] = 1;
2382 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2383 END OBJ(mem, INFO_MEM)
2384 END OBJ(memeasyfree, INFO_MEM)
2385 END OBJ(memfree, INFO_MEM)
2386 END OBJ(memmax, INFO_MEM)
2387 END OBJ(memperc, INFO_MEM)
2389 END OBJ(memgauge, INFO_MEM)
2390 SIZE_DEFAULTS(gauge);
2391 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2393 END OBJ(membar, INFO_MEM)
2395 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2397 END OBJ(memgraph, INFO_MEM)
2399 SIZE_DEFAULTS(graph);
2400 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2401 &obj->e, &obj->char_a, &obj->char_b);
2405 END OBJ(mixer, INFO_MIXER)
2406 obj->data.l = mixer_init(arg);
2407 END OBJ(mixerl, INFO_MIXER)
2408 obj->data.l = mixer_init(arg);
2409 END OBJ(mixerr, INFO_MIXER)
2410 obj->data.l = mixer_init(arg);
2412 END OBJ(mixerbar, INFO_MIXER)
2414 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2415 &obj->data.mixerbar.h);
2416 END OBJ(mixerlbar, INFO_MIXER)
2418 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2419 &obj->data.mixerbar.h);
2420 END OBJ(mixerrbar, INFO_MIXER)
2422 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2423 &obj->data.mixerbar.h);
2425 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2426 obj->data.ifblock.i = mixer_init(arg);
2428 END OBJ(monitor, INFO_X11)
2429 END OBJ(monitor_number, INFO_X11)
2430 END OBJ(desktop, INFO_X11)
2431 END OBJ(desktop_number, INFO_X11)
2432 END OBJ(desktop_name, INFO_X11)
2434 END OBJ(nodename, 0)
2435 END OBJ(processes, INFO_PROCS)
2436 END OBJ(running_processes, INFO_RUN_PROCS)
2437 END OBJ(shadecolor, 0)
2439 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2441 END OBJ(outlinecolor, 0)
2443 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2445 END OBJ(stippled_hr, 0)
2447 int a = stippled_borders, b = 1;
2450 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2451 sscanf(arg, "%d", &b);
2457 obj->data.pair.a = a;
2458 obj->data.pair.b = b;
2460 END OBJ(swap, INFO_MEM)
2461 END OBJ(swapfree, INFO_MEM)
2462 END OBJ(swapmax, INFO_MEM)
2463 END OBJ(swapperc, INFO_MEM)
2464 END OBJ(swapbar, INFO_MEM)
2466 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2469 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2471 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2473 char buf1[256], buf2[256], *fmt, *tz;
2477 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2487 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2488 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2490 END OBJ(iconv_start, 0)
2491 if (iconv_converting) {
2492 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
2493 "starting another");
2496 char iconv_from[CODEPAGE_LENGTH];
2497 char iconv_to[CODEPAGE_LENGTH];
2499 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2500 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
2504 new_iconv = iconv_open(iconv_to, iconv_from);
2505 if (new_iconv == (iconv_t) (-1)) {
2506 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2508 obj->a = register_iconv(&new_iconv);
2509 iconv_converting = 1;
2513 CRIT_ERR(obj, free_at_crash, "Iconv requires arguments");
2515 END OBJ(iconv_stop, 0)
2516 iconv_converting = 0;
2519 END OBJ(totaldown, INFO_NET)
2521 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2523 // default to DEFAULTNETDEV
2524 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2525 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2528 END OBJ(totalup, INFO_NET)
2529 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2531 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2533 // default to DEFAULTNETDEV
2534 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2535 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2539 END OBJ_IF(if_updatenr, 0)
2540 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2541 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
2542 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2544 obj->data.i = arg ? atoi(arg) : 0;
2546 obj->data.i = arg ? atoi(arg) : 0;
2547 END OBJ(upspeed, INFO_NET)
2549 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2551 // default to DEFAULTNETDEV
2552 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2553 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2556 END OBJ(upspeedf, INFO_NET)
2558 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2560 // default to DEFAULTNETDEV
2561 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2562 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2567 END OBJ(upspeedgraph, INFO_NET)
2569 SIZE_DEFAULTS(graph);
2570 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2571 &obj->e, &obj->char_a, &obj->char_b);
2573 // default to DEFAULTNETDEV
2574 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2575 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2578 END OBJ(uptime_short, INFO_UPTIME)
2579 END OBJ(uptime, INFO_UPTIME)
2580 END OBJ(user_names, INFO_USERS)
2581 END OBJ(user_times, INFO_USERS)
2582 END OBJ(user_terms, INFO_USERS)
2583 END OBJ(user_number, INFO_USERS)
2584 #if defined(__linux__)
2585 END OBJ(gw_iface, INFO_GW)
2586 END OBJ(gw_ip, INFO_GW)
2587 #endif /* !__linux__ */
2589 END OBJ(adt746xcpu, 0)
2590 END OBJ(adt746xfan, 0)
2591 #endif /* !__OpenBSD__ */
2592 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2593 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2594 END OBJ(apm_adapter, 0)
2595 END OBJ(apm_battery_life, 0)
2596 END OBJ(apm_battery_time, 0)
2597 #endif /* __FreeBSD__ */
2598 END OBJ_THREAD(imap_unseen, 0)
2601 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2606 END OBJ_THREAD(imap_messages, 0)
2609 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2614 END OBJ_THREAD(pop3_unseen, 0)
2617 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2622 END OBJ_THREAD(pop3_used, 0)
2625 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2633 obj->data.s = strndup(arg, text_buffer_size);
2635 ERR("smapi needs an argument");
2636 END OBJ_IF(if_smapi_bat_installed, 0)
2638 ERR("if_smapi_bat_installed needs an argument");
2639 obj->data.ifblock.s = 0;
2641 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2642 END OBJ(smapi_bat_perc, 0)
2644 obj->data.s = strndup(arg, text_buffer_size);
2646 ERR("smapi_bat_perc needs an argument");
2647 END OBJ(smapi_bat_temp, 0)
2649 obj->data.s = strndup(arg, text_buffer_size);
2651 ERR("smapi_bat_temp needs an argument");
2652 END OBJ(smapi_bat_power, 0)
2654 obj->data.s = strndup(arg, text_buffer_size);
2656 ERR("smapi_bat_power needs an argument");
2658 END OBJ(smapi_bat_bar, 0)
2662 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2663 ERR("first argument to smapi_bat_bar must be an integer value");
2667 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2670 ERR("smapi_bat_bar needs an argument");
2674 #define mpd_set_maxlen(name) \
2677 sscanf(arg, "%d", &i); \
2679 obj->data.i = i + 1; \
2681 ERR(#name ": invalid length argument"); \
2683 END OBJ(mpd_artist, INFO_MPD)
2684 mpd_set_maxlen(mpd_artist);
2686 END OBJ(mpd_title, INFO_MPD)
2687 mpd_set_maxlen(mpd_title);
2689 END OBJ(mpd_random, INFO_MPD) init_mpd();
2690 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2691 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2692 END OBJ(mpd_length, INFO_MPD) init_mpd();
2693 END OBJ(mpd_track, INFO_MPD)
2694 mpd_set_maxlen(mpd_track);
2696 END OBJ(mpd_name, INFO_MPD)
2697 mpd_set_maxlen(mpd_name);
2699 END OBJ(mpd_file, INFO_MPD)
2700 mpd_set_maxlen(mpd_file);
2702 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2703 END OBJ(mpd_album, INFO_MPD)
2704 mpd_set_maxlen(mpd_album);
2706 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2707 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2708 END OBJ(mpd_status, INFO_MPD) init_mpd();
2709 END OBJ(mpd_bar, INFO_MPD)
2711 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2713 END OBJ(mpd_smart, INFO_MPD)
2714 mpd_set_maxlen(mpd_smart);
2716 END OBJ_IF(if_mpd_playing, INFO_MPD)
2718 #undef mpd_set_maxlen
2721 END OBJ(moc_state, INFO_MOC)
2722 END OBJ(moc_file, INFO_MOC)
2723 END OBJ(moc_title, INFO_MOC)
2724 END OBJ(moc_artist, INFO_MOC)
2725 END OBJ(moc_song, INFO_MOC)
2726 END OBJ(moc_album, INFO_MOC)
2727 END OBJ(moc_totaltime, INFO_MOC)
2728 END OBJ(moc_timeleft, INFO_MOC)
2729 END OBJ(moc_curtime, INFO_MOC)
2730 END OBJ(moc_bitrate, INFO_MOC)
2731 END OBJ(moc_rate, INFO_MOC)
2734 END OBJ(xmms2_artist, INFO_XMMS2)
2735 END OBJ(xmms2_album, INFO_XMMS2)
2736 END OBJ(xmms2_title, INFO_XMMS2)
2737 END OBJ(xmms2_genre, INFO_XMMS2)
2738 END OBJ(xmms2_comment, INFO_XMMS2)
2739 END OBJ(xmms2_url, INFO_XMMS2)
2740 END OBJ(xmms2_tracknr, INFO_XMMS2)
2741 END OBJ(xmms2_bitrate, INFO_XMMS2)
2742 END OBJ(xmms2_date, INFO_XMMS2)
2743 END OBJ(xmms2_id, INFO_XMMS2)
2744 END OBJ(xmms2_duration, INFO_XMMS2)
2745 END OBJ(xmms2_elapsed, INFO_XMMS2)
2746 END OBJ(xmms2_size, INFO_XMMS2)
2747 END OBJ(xmms2_status, INFO_XMMS2)
2748 END OBJ(xmms2_percent, INFO_XMMS2)
2750 END OBJ(xmms2_bar, INFO_XMMS2)
2752 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2754 END OBJ(xmms2_smart, INFO_XMMS2)
2755 END OBJ(xmms2_playlist, INFO_XMMS2)
2756 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2757 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2760 END OBJ(audacious_status, INFO_AUDACIOUS)
2761 END OBJ(audacious_title, INFO_AUDACIOUS)
2763 sscanf(arg, "%d", &info.audacious.max_title_len);
2764 if (info.audacious.max_title_len > 0) {
2765 info.audacious.max_title_len++;
2767 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
2770 END OBJ(audacious_length, INFO_AUDACIOUS)
2771 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2772 END OBJ(audacious_position, INFO_AUDACIOUS)
2773 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2774 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2775 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2776 END OBJ(audacious_channels, INFO_AUDACIOUS)
2777 END OBJ(audacious_filename, INFO_AUDACIOUS)
2778 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2779 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2780 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2782 END OBJ(audacious_bar, INFO_AUDACIOUS)
2784 scan_bar(arg, &obj->a, &obj->b);
2788 END OBJ(bmpx_title, INFO_BMPX)
2789 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2790 END OBJ(bmpx_artist, INFO_BMPX)
2791 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2792 END OBJ(bmpx_album, INFO_BMPX)
2793 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2794 END OBJ(bmpx_track, INFO_BMPX)
2795 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2796 END OBJ(bmpx_uri, INFO_BMPX)
2797 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2798 END OBJ(bmpx_bitrate, INFO_BMPX)
2799 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2805 char *userid = (char *) malloc(20 * sizeof(char));
2806 char *apikey = (char *) malloc(64 * sizeof(char));
2807 char *charid = (char *) malloc(20 * sizeof(char));
2809 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2810 obj->data.eve.charid = charid;
2811 obj->data.eve.userid = userid;
2812 obj->data.eve.apikey = apikey;
2816 CRIT_ERR(obj, free_at_crash, "eve needs arguments: <userid> <apikey> <characterid>");
2822 int argc, delay, act_par;
2823 unsigned int nrspaces = 0;
2824 char *uri = (char *) malloc(128 * sizeof(char));
2825 char *action = (char *) malloc(64 * sizeof(char));
2827 argc = sscanf(arg, "%127s %d %63s %d %u", uri, &delay, action,
2828 &act_par, &nrspaces);
2829 obj->data.rss.uri = uri;
2830 obj->data.rss.delay = delay;
2831 obj->data.rss.action = action;
2832 obj->data.rss.act_par = act_par;
2833 obj->data.rss.nrspaces = nrspaces;
2837 CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <delay in minutes> <action> "
2838 "[act_par] [spaces in front]");
2842 END OBJ_THREAD(weather, 0)
2845 char *locID = (char *) malloc(9 * sizeof(char));
2846 char *uri = (char *) malloc(128 * sizeof(char));
2847 char *data_type = (char *) malloc(32 * sizeof(char));
2850 argc = sscanf(arg, "%119s %8s %31s %d", uri, locID, data_type, &interval);
2852 //locID MUST BE upper-case
2855 *tmp_p = toupper(*tmp_p);
2859 //Construct complete uri
2860 if (strstr(uri, "xoap.weather.com")) {
2868 } else if (strstr(uri, "weather.noaa.gov")) {
2870 strcat(uri, ".TXT");
2871 } else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
2872 CRIT_ERR(obj, free_at_crash, \
2873 "could not recognize the weather uri");
2876 obj->data.weather.uri = uri;
2877 obj->data.weather.data_type = data_type;
2879 //Limit the data retrieval interval to half hour min
2880 if (interval < 30) {
2884 //Convert to seconds
2885 obj->data.weather.interval = interval * 60;
2888 DBGP("weather: fetching %s from %s every %d seconds", \
2889 data_type, uri, obj->data.weather.interval);
2891 CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
2897 obj->data.s = strndup(arg, text_buffer_size);
2899 CRIT_ERR(obj, free_at_crash, "lua needs arguments: <function name> [function parameters]");
2901 END OBJ(lua_parse, 0)
2903 obj->data.s = strndup(arg, text_buffer_size);
2905 CRIT_ERR(obj, free_at_crash, "lua_parse needs arguments: <function name> [function parameters]");
2910 arg = scan_bar(arg, &obj->a, &obj->b);
2912 obj->data.s = strndup(arg, text_buffer_size);
2914 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2917 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2920 END OBJ(lua_graph, 0)
2921 SIZE_DEFAULTS(graph);
2924 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2925 &obj->e, &obj->char_a, &obj->char_b);
2929 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2932 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2934 END OBJ(lua_gauge, 0)
2935 SIZE_DEFAULTS(gauge);
2937 arg = scan_gauge(arg, &obj->a, &obj->b);
2939 obj->data.s = strndup(arg, text_buffer_size);
2941 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2944 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2947 #endif /* HAVE_LUA */
2950 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2951 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2952 ERR("hddtemp needs arguments");
2953 obj->type = OBJ_text;
2954 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2955 obj->data.hddtemp.update_time = 0;
2957 obj->data.hddtemp.temp = NULL;
2958 #endif /* HDDTEMP */
2959 #ifdef TCP_PORT_MONITOR
2960 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2961 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2962 #endif /* TCP_PORT_MONITOR */
2963 END OBJ(entropy_avail, INFO_ENTROPY)
2964 END OBJ(entropy_perc, INFO_ENTROPY)
2965 END OBJ(entropy_poolsize, INFO_ENTROPY)
2966 END OBJ(entropy_bar, INFO_ENTROPY)
2968 scan_bar(arg, &obj->a, &obj->b);
2971 obj->sub = malloc(sizeof(struct text_object));
2972 extract_variable_text_internal(obj->sub, arg, 0);
2974 CRIT_ERR(obj, free_at_crash, "blink needs a argument");
2976 END OBJ(to_bytes, 0)
2978 obj->sub = malloc(sizeof(struct text_object));
2979 extract_variable_text_internal(obj->sub, arg, 0);
2981 CRIT_ERR(obj, free_at_crash, "to_bytes needs a argument");
2986 obj->data.scroll.resetcolor = current_text_color;
2987 obj->data.scroll.step = 1;
2988 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2989 sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
2990 if (*(arg + n1 + n2)) {
2993 obj->data.scroll.step = 1;
2995 obj->data.scroll.text = malloc(strlen(arg + n1) + obj->data.scroll.show + 1);
2996 for(n2 = 0; (unsigned int) n2 < obj->data.scroll.show; n2++) {
2997 obj->data.scroll.text[n2] = ' ';
2999 obj->data.scroll.text[n2] = 0;
3000 strcat(obj->data.scroll.text, arg + n1);
3001 obj->data.scroll.start = 0;
3002 obj->sub = malloc(sizeof(struct text_object));
3003 extract_variable_text_internal(obj->sub,
3004 obj->data.scroll.text, 0);
3006 CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
3011 unsigned int indenting = 0; //vars can be used as args for other vars
3014 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
3016 for (i=0; arg[i] != 0 && j < 2; i++) {
3017 if(startvar[j] == -1) {
3021 }else if(endvar[j] == -1) {
3024 }else if(arg[i] == '}') {
3027 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
3033 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
3034 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
3035 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
3036 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
3038 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
3039 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
3041 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
3042 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
3044 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
3045 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
3047 obj->sub = malloc(sizeof(struct text_object));
3048 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
3049 obj->sub->sub = malloc(sizeof(struct text_object));
3050 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
3052 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3055 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3060 CRIT_ERR(obj, free_at_crash, "nvidia needs an argument\n");
3061 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3062 CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
3063 " specified: '%s'\n", arg);
3068 END OBJ(apcupsd, INFO_APCUPSD)
3072 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3073 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3075 info.apcupsd.port = htons(port);
3076 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3079 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3081 END OBJ(apcupsd_name, INFO_APCUPSD)
3082 END OBJ(apcupsd_model, INFO_APCUPSD)
3083 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3084 END OBJ(apcupsd_cable, INFO_APCUPSD)
3085 END OBJ(apcupsd_status, INFO_APCUPSD)
3086 END OBJ(apcupsd_linev, INFO_APCUPSD)
3087 END OBJ(apcupsd_load, INFO_APCUPSD)
3088 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3090 scan_bar(arg, &obj->a, &obj->b);
3092 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3094 SIZE_DEFAULTS(graph);
3095 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3096 &obj->e, &obj->char_a, &obj->char_b);
3098 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3099 SIZE_DEFAULTS(gauge);
3100 scan_gauge(arg, &obj->a, &obj->b);
3102 END OBJ(apcupsd_charge, INFO_APCUPSD)
3103 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3104 END OBJ(apcupsd_temp, INFO_APCUPSD)
3105 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3106 #endif /* APCUPSD */
3110 ERR("unknown variable %s", s);
3111 obj->type = OBJ_text;
3112 snprintf(buf, 256, "${%s}", s);
3113 obj->data.s = strndup(buf, text_buffer_size);
3120 static struct text_object *create_plain_text(const char *s)
3122 struct text_object *obj;
3124 if (s == NULL || *s == '\0') {
3128 obj = new_text_object_internal();
3130 obj->type = OBJ_text;
3131 obj->data.s = strndup(s, text_buffer_size);
3135 /* backslash_escape - do the actual substitution task for template objects
3137 * The field templates is used for substituting the \N occurences. Set it to
3138 * NULL to leave them as they are.
3140 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3144 unsigned int dup_idx = 0, dup_len;
3146 dup_len = strlen(src) + 1;
3147 src_dup = malloc(dup_len * sizeof(char));
3155 if (*(p + 1) == '\\') {
3156 src_dup[dup_idx++] = '\\';
3158 } else if (*(p + 1) == ' ') {
3159 src_dup[dup_idx++] = ' ';
3161 } else if (*(p + 1) == 'n') {
3162 src_dup[dup_idx++] = '\n';
3164 } else if (templates) {
3165 unsigned int tmpl_num;
3167 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3168 (tmpl_num > template_count))
3170 dup_len += strlen(templates[tmpl_num - 1]);
3171 src_dup = realloc(src_dup, dup_len * sizeof(char));
3172 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3173 dup_idx += strlen(templates[tmpl_num - 1]);
3178 src_dup[dup_idx++] = *p;
3183 src_dup[dup_idx] = '\0';
3184 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3188 /* handle_template_object - core logic of the template object
3190 * use config variables like this:
3191 * template1 = "$\1\2"
3192 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3194 * and use them like this:
3195 * ${template1 node name}
3196 * ${template2 root /}
3197 * ${template2 cdrom /mnt/cdrom}
3199 static char *handle_template(const char *tmpl, const char *args)
3201 char *args_dup = NULL;
3203 char **argsp = NULL;
3204 unsigned int argcnt = 0, template_idx, i;
3207 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3208 (template_idx >= MAX_TEMPLATES))
3212 args_dup = strdup(args);
3215 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3217 if (p > args_dup && *(p - 1) == '\\')
3220 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3226 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3227 argsp[argcnt - 1] = p_old;
3229 for (i = 0; i < argcnt; i++) {
3231 tmp = backslash_escape(argsp[i], NULL, 0);
3232 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3237 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3238 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3240 for (i = 0; i < argcnt; i++)
3246 static char *find_and_replace_templates(const char *inbuf)
3248 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3251 outlen = strlen(inbuf) + 1;
3252 o = outbuf = calloc(outlen, sizeof(char));
3253 memset(outbuf, 0, outlen * sizeof(char));
3255 p = indup = strdup(inbuf);
3257 while (*p && *p != '$')
3263 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3268 if (*(p + 1) == '{') {
3271 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3279 while (*p && stack > 0) {
3287 // stack is empty. that means the previous char was }, so we zero it
3290 // we ran into the end of string without finding a closing }, bark
3291 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
3295 while (*p && !isspace(*p))
3299 tmpl_out = handle_template(templ, args);
3301 outlen += strlen(tmpl_out);
3303 outbuf = realloc(outbuf, outlen * sizeof(char));
3304 strcat (outbuf, tmpl_out);
3306 o = outbuf + strlen(outbuf);
3308 ERR("failed to handle template '%s' with args '%s'", templ, args);
3312 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3317 static int text_contains_templates(const char *text)
3319 if (strcasestr(text, "${template") != NULL)
3321 if (strcasestr(text, "$template") != NULL)
3326 /* folds a string over top of itself, like so:
3328 * if start is "blah", and you call it with count = 1, the result will be "lah"
3330 static void strfold(char *start, int count)
3333 for (curplace = start + count; *curplace != 0; curplace++) {
3334 *(curplace - count) = *curplace;
3336 *(curplace - count) = 0;
3340 * - assumes that *string is '#'
3341 * - removes the part from '#' to the end of line ('\n' or '\0')
3342 * - it removes the '\n'
3343 * - copies the last char into 'char *last' argument, which should be a pointer
3344 * to a char rather than a string.
3346 static size_t remove_comment(char *string, char *last)
3349 while (*end != '\0' && *end != '\n') {
3352 if (last) *last = *end;
3353 if (*end == '\n') end++;
3354 strfold(string, end - string);
3355 return end - string;
3358 static size_t remove_comments(char *string)
3362 for (curplace = string; *curplace != 0; curplace++) {
3363 if (*curplace == '\\' && *(curplace + 1) == '#') {
3364 // strcpy can't be used for overlapping strings
3365 strfold(curplace, 1);
3367 } else if (*curplace == '#') {
3368 folded += remove_comment(curplace, 0);
3374 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3376 struct text_object *obj;
3377 char *p, *s, *orig_p;
3379 void *ifblock_opaque = NULL;
3384 p = strndup(const_p, max_user_text - 1);
3385 while (text_contains_templates(p)) {
3387 tmp = find_and_replace_templates(p);
3393 if (strcmp(p, const_p)) {
3394 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3396 DBGP("no templates to replace");
3399 memset(retval, 0, sizeof(struct text_object));
3401 line = global_text_lines;
3409 obj = create_plain_text(s);
3411 append_object(retval, obj);
3421 /* variable is either $foo or ${foo} */
3423 unsigned int brl = 1, brr = 0;
3427 while (*p && brl != brr) {
3442 while (*p && (isalnum((int) *p) || *p == '_')) {
3447 /* copy variable to buffer */
3448 len = (p - s > 255) ? 255 : (p - s);
3449 strncpy(buf, s, len);
3457 /* search for variable in environment */
3461 obj = create_plain_text(var);
3463 append_object(retval, obj);
3468 /* if variable wasn't found in environment, use some special */
3473 if (strchr(buf, ' ')) {
3474 arg = strchr(buf, ' ');
3477 while (isspace((int) *arg)) {
3485 /* lowercase variable name */
3488 *tmp_p = tolower(*tmp_p);
3492 obj = construct_text_object(buf, arg,
3493 line, allow_threaded,
3494 &ifblock_opaque, orig_p);
3496 append_object(retval, obj);
3500 obj = create_plain_text("$");
3503 append_object(retval, obj);
3506 } else if (*p == '\\' && *(p+1) == '#') {
3508 } else if (*p == '#') {
3510 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3511 /* if remove_comment removed a newline, we need to 'back up' with p */
3517 obj = create_plain_text(s);
3519 append_object(retval, obj);
3522 if (!ifblock_stack_empty(&ifblock_opaque)) {
3523 ERR("one or more $endif's are missing");
3530 static void extract_variable_text(const char *p)
3532 free_text_objects(&global_root_object, 0);
3546 extract_variable_text_internal(&global_root_object, p, 1);
3549 void parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3551 extract_variable_text_internal(root, txt, 0);
3552 generate_text_internal(p, max_user_text, *root, cur);
3556 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3557 void *thread(void *), const char *text)
3559 if (obj->char_b && info.mail) {
3560 // this means we use info
3561 if (!info.mail->p_timed_thread) {
3562 info.mail->p_timed_thread =
3563 timed_thread_create(thread,
3564 (void *) info.mail, info.mail->interval * 1000000);
3565 if (!info.mail->p_timed_thread) {
3566 ERR("Error creating %s timed thread", text);
3568 timed_thread_register(info.mail->p_timed_thread,
3569 &info.mail->p_timed_thread);
3570 if (timed_thread_run(info.mail->p_timed_thread)) {
3571 ERR("Error running %s timed thread", text);
3575 } else if (obj->data.mail) {
3576 // this means we use obj
3577 if (!obj->data.mail->p_timed_thread) {
3578 obj->data.mail->p_timed_thread =
3579 timed_thread_create(thread,
3580 (void *) obj->data.mail,
3581 obj->data.mail->interval * 1000000);
3582 if (!obj->data.mail->p_timed_thread) {
3583 ERR("Error creating %s timed thread", text);
3585 timed_thread_register(obj->data.mail->p_timed_thread,
3586 &obj->data.mail->p_timed_thread);
3587 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3588 ERR("Error running %s timed thread", text);
3591 return obj->data.mail;
3592 } else if (!obj->a) {
3593 // something is wrong, warn once then stop
3594 ERR("There's a problem with your mail settings. "
3595 "Check that the global mail settings are properly defined"
3596 " (line %li).", obj->line);
3602 char *format_time(unsigned long timeval, const int width)
3605 unsigned long nt; // narrow time, for speed on 32-bit
3606 unsigned cc; // centiseconds
3607 unsigned nn; // multi-purpose whatever
3610 cc = nt % 100; // centiseconds past second
3611 nt /= 100; // total seconds
3612 nn = nt % 60; // seconds past the minute
3613 nt /= 60; // total minutes
3614 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3616 return strndup(buf, text_buffer_size);
3618 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3619 return strndup(buf, text_buffer_size);
3621 nn = nt % 60; // minutes past the hour
3622 nt /= 60; // total hours
3623 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3624 return strndup(buf, text_buffer_size);
3626 nn = nt; // now also hours
3627 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3628 return strndup(buf, text_buffer_size);
3630 nn /= 24; // now days
3631 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3632 return strndup(buf, text_buffer_size);
3634 nn /= 7; // now weeks
3635 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3636 return strndup(buf, text_buffer_size);
3638 // well shoot, this outta' fit...
3639 return strndup("<inf>", text_buffer_size);
3642 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3643 //string has to end with \0 and it's length should fit in a int
3645 void remove_deleted_chars(char *string){
3647 while(string[i] != 0){
3648 if(string[i] == BACKSPACE){
3650 strcpy( &(string[i-1]), &(string[i+1]) );
3652 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3657 static inline void format_media_player_time(char *buf, const int size,
3660 int days, hours, minutes;
3662 days = seconds / (24 * 60 * 60);
3663 seconds %= (24 * 60 * 60);
3664 hours = seconds / (60 * 60);
3665 seconds %= (60 * 60);
3666 minutes = seconds / 60;
3670 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3671 hours, minutes, seconds);
3672 } else if (hours > 0) {
3673 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3676 snprintf(buf, size, "%i:%02i", minutes, seconds);
3680 static inline double get_barnum(char *buf)
3692 if (sscanf(buf, "%lf", &barnum) == 0) {
3693 ERR("reading exec value failed (perhaps it's not the "
3694 "correct format?)");
3697 if (barnum > 100.0 || barnum < 0.0) {
3698 ERR("your exec value is not between 0 and 100, "
3699 "therefore it will be ignored");
3705 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
3706 * multiline objects like $exec work with $align[rc] and friends
3708 void substitute_newlines(char *p, long l)
3712 while (p && *p && p < s + l) {
3714 /* only substitute if it's not the last newline */
3715 *p = SECRIT_MULTILINE_CHAR;
3721 static void generate_text_internal(char *p, int p_max_size,
3722 struct text_object root, struct information *cur)
3724 struct text_object *obj;
3726 int need_to_load_fonts = 0;
3729 /* for the OBJ_top* handler */
3730 struct process **needed = 0;
3733 char buff_in[p_max_size];
3735 iconv_converting = 0;
3736 #endif /* HAVE_ICONV */
3740 while (obj && p_max_size > 0) {
3741 needed = 0; /* reset for top stuff */
3743 /* IFBLOCK jumping algorithm
3745 * This is easier as it looks like:
3746 * - each IF checks it's condition
3747 * - on FALSE: call DO_JUMP
3748 * - on TRUE: don't care
3749 * - each ELSE calls DO_JUMP unconditionally
3750 * - each ENDIF is silently being ignored
3753 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3754 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3755 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3756 * regularly, "obj" is being updated to point to obj->next, so object parsing
3757 * continues right after the corresponding ELSE or ENDIF. This means that if we
3758 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3759 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3760 * jumped, and there is nothing to do.
3764 obj = obj->data.ifblock.next; \
3767 #define OBJ(a) break; case OBJ_##a:
3769 switch (obj->type) {
3771 ERR("not implemented obj type %d", obj->type);
3774 struct sockaddr_in addr;
3775 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3777 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3779 memset(&addr, 0, sizeof(addr));
3780 addr.sin_family = AF_INET;
3781 addr.sin_port = obj->data.read_tcp.port;
3782 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3783 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3787 FD_SET(sock, &readfds);
3790 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3791 received = recv(sock, p, p_max_size, 0);
3796 ERR("read_tcp: Couldn't create a connection");
3799 ERR("read_tcp: Couldn't create a socket");
3802 ERR("read_tcp: Problem with resolving the hostname");
3807 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3809 #endif /* !__OpenBSD__ */
3812 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3813 obj->data.cpu_index);
3819 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3820 obj->data.cpu_index);
3822 /* OpenBSD has no such flag (SUSv2) */
3823 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3824 obj->data.cpu_index);
3825 #endif /* __OpenBSD */
3828 #if defined(__linux__)
3831 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3832 obj->data.cpu_index);
3837 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3838 obj->data.cpu_index);
3843 OBJ(wireless_essid) {
3844 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3846 OBJ(wireless_mode) {
3847 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3849 OBJ(wireless_bitrate) {
3850 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3853 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3855 OBJ(wireless_link_qual) {
3856 spaced_print(p, p_max_size, "%d", 4,
3857 obj->data.net->link_qual);
3859 OBJ(wireless_link_qual_max) {
3860 spaced_print(p, p_max_size, "%d", 4,
3861 obj->data.net->link_qual_max);
3863 OBJ(wireless_link_qual_perc) {
3864 if (obj->data.net->link_qual_max > 0) {
3865 spaced_print(p, p_max_size, "%.0f", 5,
3866 (double) obj->data.net->link_qual /
3867 obj->data.net->link_qual_max * 100);
3869 spaced_print(p, p_max_size, "unk", 5);
3872 OBJ(wireless_link_bar) {
3874 if(output_methods & TO_X) {
3875 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3876 obj->data.net->link_qual_max) * 255.0);
3879 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3880 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3881 obj->data.net->link_qual_max) * 100.0, obj->a);
3886 #endif /* HAVE_IWLIB */
3888 #endif /* __linux__ */
3892 get_adt746x_cpu(p, p_max_size);
3895 get_adt746x_fan(p, p_max_size);
3898 get_acpi_fan(p, p_max_size);
3900 OBJ(acpiacadapter) {
3901 get_acpi_ac_adapter(p, p_max_size);
3904 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3907 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3909 OBJ(battery_percent) {
3910 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3914 if(output_methods & TO_X) {
3915 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3918 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3919 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3924 OBJ(battery_short) {
3925 get_battery_short_status(p, p_max_size, obj->data.s);
3927 #endif /* __OpenBSD__ */
3930 human_readable(cur->buffers * 1024, p, 255);
3933 human_readable(cur->cached * 1024, p, 255);
3936 if (obj->data.cpu_index > info.cpu_count) {
3937 ERR("obj->data.cpu_index %i info.cpu_count %i",
3938 obj->data.cpu_index, info.cpu_count);
3939 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
3941 percent_print(p, p_max_size,
3942 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3946 new_gauge(p, obj->a, obj->b,
3947 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3951 if(output_methods & TO_X) {
3952 new_bar(p, obj->a, obj->b,
3953 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3956 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3957 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3964 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3965 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3966 100, 1, obj->char_a, obj->char_b);
3969 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3970 obj->e, 1, obj->char_a, obj->char_b);
3973 new_fg(p, obj->data.l);
4006 OBJ(conky_version) {
4007 snprintf(p, p_max_size, "%s", VERSION);
4009 OBJ(conky_build_date) {
4010 snprintf(p, p_max_size, "%s", BUILD_DATE);
4012 OBJ(conky_build_arch) {
4013 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4015 #if defined(__linux__)
4017 snprintf(p, p_max_size, "%s",
4018 get_disk_protect_queue(obj->data.s));
4021 snprintf(p, p_max_size, "%s", i8k.version);
4024 snprintf(p, p_max_size, "%s", i8k.bios);
4027 snprintf(p, p_max_size, "%s", i8k.serial);
4032 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4033 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
4035 OBJ(i8k_left_fan_status) {
4036 int left_fan_status;
4038 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4039 if (left_fan_status == 0) {
4040 snprintf(p, p_max_size, "off");
4042 if (left_fan_status == 1) {
4043 snprintf(p, p_max_size, "low");
4045 if (left_fan_status == 2) {
4046 snprintf(p, p_max_size, "high");
4049 OBJ(i8k_right_fan_status) {
4050 int right_fan_status;
4052 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4053 if (right_fan_status == 0) {
4054 snprintf(p, p_max_size, "off");
4056 if (right_fan_status == 1) {
4057 snprintf(p, p_max_size, "low");
4059 if (right_fan_status == 2) {
4060 snprintf(p, p_max_size, "high");
4063 OBJ(i8k_left_fan_rpm) {
4064 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4066 OBJ(i8k_right_fan_rpm) {
4067 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4069 OBJ(i8k_ac_status) {
4072 sscanf(i8k.ac_status, "%d", &ac_status);
4073 if (ac_status == -1) {
4074 snprintf(p, p_max_size, "disabled (read i8k docs)");
4076 if (ac_status == 0) {
4077 snprintf(p, p_max_size, "off");
4079 if (ac_status == 1) {
4080 snprintf(p, p_max_size, "on");
4083 OBJ(i8k_buttons_status) {
4084 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4088 get_ibm_acpi_fan(p, p_max_size);
4091 get_ibm_acpi_temps();
4092 temp_print(p, p_max_size,
4093 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4096 get_ibm_acpi_volume(p, p_max_size);
4098 OBJ(ibm_brightness) {
4099 get_ibm_acpi_brightness(p, p_max_size);
4102 /* information from sony_laptop kernel module
4103 * /sys/devices/platform/sony-laptop */
4104 OBJ(sony_fanspeed) {
4105 get_sony_fanspeed(p, p_max_size);
4108 if (!cur->gw_info.count) {
4113 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4116 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4119 snprintf(p, p_max_size, "%d", get_laptop_mode());
4122 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4124 #endif /* __linux__ */
4125 #if (defined(__FreeBSD__) || defined(__linux__))
4127 if ((obj->data.ifblock.s)
4128 && (!interface_up(obj->data.ifblock.s))) {
4134 OBJ(obsd_sensors_temp) {
4135 obsd_sensors.device = sensor_device;
4136 update_obsd_sensors();
4137 temp_print(p, p_max_size,
4138 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4141 OBJ(obsd_sensors_fan) {
4142 obsd_sensors.device = sensor_device;
4143 update_obsd_sensors();
4144 snprintf(p, p_max_size, "%d",
4145 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4147 OBJ(obsd_sensors_volt) {
4148 obsd_sensors.device = sensor_device;
4149 update_obsd_sensors();
4150 snprintf(p, p_max_size, "%.2f",
4151 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4154 get_obsd_vendor(p, p_max_size);
4157 get_obsd_product(p, p_max_size);
4159 #endif /* __OpenBSD__ */
4162 new_font(p, obj->data.s);
4163 need_to_load_fonts = 1;
4166 /* TODO: move this correction from kB to kB/s elsewhere
4167 * (or get rid of it??) */
4169 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4173 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4177 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4182 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4183 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4185 OBJ(diskiograph_read) {
4186 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4187 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4189 OBJ(diskiograph_write) {
4190 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4191 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4195 human_readable(obj->data.net->recv_speed, p, 255);
4198 spaced_print(p, p_max_size, "%.1f", 8,
4199 obj->data.net->recv_speed / 1024.0);
4202 OBJ(downspeedgraph) {
4203 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4204 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4208 /* Since we see you, you're if has not jumped.
4209 * Do Ninja jump here: without leaving traces.
4210 * This is to prevent us from stale jumped flags.
4212 obj = obj->data.ifblock.next;
4216 /* harmless object, just ignore */
4220 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4221 && (obj->data.net->addr.sa_data[3] & 255) == 0
4222 && (obj->data.net->addr.sa_data[4] & 255) == 0
4223 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4224 snprintf(p, p_max_size, "No Address");
4226 snprintf(p, p_max_size, "%u.%u.%u.%u",
4227 obj->data.net->addr.sa_data[2] & 255,
4228 obj->data.net->addr.sa_data[3] & 255,
4229 obj->data.net->addr.sa_data[4] & 255,
4230 obj->data.net->addr.sa_data[5] & 255);
4233 #if defined(__linux__)
4235 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
4236 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4237 strcpy(p, obj->data.net->addrs);
4239 strcpy(p, "0.0.0.0");
4242 #endif /* __linux__ */
4243 #if defined(IMLIB2) && defined(X11)
4245 /* doesn't actually draw anything, just queues it omp. the
4246 * image will get drawn after the X event loop */
4247 cimlib_add_image(obj->data.s);
4251 evaluate(obj->data.s, p);
4254 read_exec(obj->data.s, p, text_buffer_size);
4255 remove_deleted_chars(p);
4258 struct information *tmp_info;
4259 struct text_object subroot;
4261 read_exec(obj->data.s, p, text_buffer_size);
4263 tmp_info = malloc(sizeof(struct information));
4264 memcpy(tmp_info, cur, sizeof(struct information));
4265 parse_conky_vars(&subroot, p, p, tmp_info);
4267 free_text_objects(&subroot, 1);
4274 read_exec(obj->data.s, p, text_buffer_size);
4275 barnum = get_barnum(p); /*using the same function*/
4277 if (barnum >= 0.0) {
4279 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4286 read_exec(obj->data.s, p, text_buffer_size);
4287 barnum = get_barnum(p);
4289 if (barnum >= 0.0) {
4291 if(output_methods & TO_X) {
4293 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4296 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4297 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4305 char showaslog = FALSE;
4306 char tempgrad = FALSE;
4308 char *cmd = obj->data.s;
4310 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4312 cmd += strlen(" "TEMPGRAD);
4314 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4316 cmd += strlen(" "LOGGRAPH);
4318 read_exec(cmd, p, text_buffer_size);
4319 barnum = get_barnum(p);
4322 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4323 100, 1, showaslog, tempgrad);
4328 if (current_update_time - obj->data.execi.last_update
4329 >= obj->data.execi.interval) {
4332 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4333 barnum = get_barnum(p);
4335 if (barnum >= 0.0) {
4338 obj->data.execi.last_update = current_update_time;
4341 if(output_methods & TO_X) {
4342 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4345 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4346 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4353 if (current_update_time - obj->data.execi.last_update
4354 >= obj->data.execi.interval) {
4356 char showaslog = FALSE;
4357 char tempgrad = FALSE;
4358 char *cmd = obj->data.execi.cmd;
4360 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4362 cmd += strlen(" "TEMPGRAD);
4364 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4366 cmd += strlen(" "LOGGRAPH);
4368 obj->char_a = showaslog;
4369 obj->char_b = tempgrad;
4370 read_exec(cmd, p, text_buffer_size);
4371 barnum = get_barnum(p);
4373 if (barnum >= 0.0) {
4376 obj->data.execi.last_update = current_update_time;
4378 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4381 if (current_update_time - obj->data.execi.last_update
4382 >= obj->data.execi.interval) {
4385 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4386 barnum = get_barnum(p);
4388 if (barnum >= 0.0) {
4389 obj->f = 255 * barnum / 100.0;
4391 obj->data.execi.last_update = current_update_time;
4393 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4397 if (current_update_time - obj->data.execi.last_update
4398 >= obj->data.execi.interval
4399 && obj->data.execi.interval != 0) {
4400 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4402 obj->data.execi.last_update = current_update_time;
4404 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4407 struct text_object subroot;
4408 struct information *tmp_info =
4409 malloc(sizeof(struct information));
4410 memcpy(tmp_info, cur, sizeof(struct information));
4412 if (current_update_time - obj->data.execi.last_update
4413 < obj->data.execi.interval
4414 || obj->data.execi.interval == 0) {
4415 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4417 char *output = obj->data.execi.buffer;
4418 FILE *fp = popen(obj->data.execi.cmd, "r");
4419 int length = fread(output, 1, text_buffer_size, fp);
4423 output[length] = '\0';
4424 if (length > 0 && output[length - 1] == '\n') {
4425 output[length - 1] = '\0';
4428 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4429 obj->data.execi.last_update = current_update_time;
4431 free_text_objects(&subroot, 1);
4435 if (!obj->data.texeci.p_timed_thread) {
4436 obj->data.texeci.p_timed_thread =
4437 timed_thread_create(&threaded_exec,
4438 (void *) obj, obj->data.texeci.interval * 1000000);
4439 if (!obj->data.texeci.p_timed_thread) {
4440 ERR("Error creating texeci timed thread");
4442 timed_thread_register(obj->data.texeci.p_timed_thread,
4443 &obj->data.texeci.p_timed_thread);
4444 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4445 ERR("Error running texeci timed thread");
4448 timed_thread_lock(obj->data.texeci.p_timed_thread);
4449 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4450 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4453 #endif /* HAVE_POPEN */
4455 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4457 if (mail && mail->p_timed_thread) {
4458 timed_thread_lock(mail->p_timed_thread);
4459 snprintf(p, p_max_size, "%lu", mail->unseen);
4460 timed_thread_unlock(mail->p_timed_thread);
4463 OBJ(imap_messages) {
4464 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4466 if (mail && mail->p_timed_thread) {
4467 timed_thread_lock(mail->p_timed_thread);
4468 snprintf(p, p_max_size, "%lu", mail->messages);
4469 timed_thread_unlock(mail->p_timed_thread);
4473 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4475 if (mail && mail->p_timed_thread) {
4476 timed_thread_lock(mail->p_timed_thread);
4477 snprintf(p, p_max_size, "%lu", mail->unseen);
4478 timed_thread_unlock(mail->p_timed_thread);
4482 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4484 if (mail && mail->p_timed_thread) {
4485 timed_thread_lock(mail->p_timed_thread);
4486 snprintf(p, p_max_size, "%.1f",
4487 mail->used / 1024.0 / 1024.0);
4488 timed_thread_unlock(mail->p_timed_thread);
4492 if (obj->data.fs != NULL) {
4493 if (obj->data.fs->size == 0) {
4495 if(output_methods & TO_X) {
4496 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4499 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4500 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4506 if(output_methods & TO_X) {
4507 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4508 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4509 obj->data.fs->size));
4512 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4513 new_bar_in_shell(p, p_max_size,
4514 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4522 if (obj->data.fs != NULL) {
4523 human_readable(obj->data.fs->avail, p, 255);
4527 if (obj->data.fs != NULL) {
4530 if (obj->data.fs->size) {
4531 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4534 percent_print(p, p_max_size, val);
4538 if (obj->data.fs != NULL) {
4539 human_readable(obj->data.fs->size, p, 255);
4543 if (obj->data.fs != NULL)
4544 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4547 if (obj->data.fs != NULL) {
4548 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4553 if (obj->data.fs != NULL) {
4554 if (obj->data.fs->size == 0) {
4556 if(output_methods & TO_X) {
4557 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4560 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4561 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4567 if(output_methods & TO_X) {
4568 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4569 (int) (obj->data.fsbar.fs->avail * 255 /
4570 obj->data.fs->size));
4573 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4574 new_bar_in_shell(p, p_max_size,
4575 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4583 if (obj->data.fs != NULL) {
4586 if (obj->data.fs->size) {
4587 val = obj->data.fs->free
4592 percent_print(p, p_max_size, 100 - val);
4596 float *v = info.loadavg;
4598 if (obj->data.loadavg[2]) {
4599 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4600 v[obj->data.loadavg[0] - 1],
4601 v[obj->data.loadavg[1] - 1],
4602 v[obj->data.loadavg[2] - 1]);
4603 } else if (obj->data.loadavg[1]) {
4604 snprintf(p, p_max_size, "%.2f %.2f",
4605 v[obj->data.loadavg[0] - 1],
4606 v[obj->data.loadavg[1] - 1]);
4607 } else if (obj->data.loadavg[0]) {
4608 snprintf(p, p_max_size, "%.2f",
4609 v[obj->data.loadavg[0] - 1]);
4613 new_goto(p, obj->data.i);
4616 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4620 new_hr(p, obj->data.i);
4624 if (cur->nameserver_info.nscount > obj->data.i)
4625 snprintf(p, p_max_size, "%s",
4626 cur->nameserver_info.ns_list[obj->data.i]);
4630 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4631 snprintf(p, p_max_size, "%s", skill);
4636 PRSS *data = get_rss_info(obj->data.rss.uri,
4637 obj->data.rss.delay);
4641 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4643 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4645 // remove trailing new line if one exists
4646 if (str[strlen(str) - 1] == '\n') {
4647 str[strlen(str) - 1] = 0;
4649 snprintf(p, p_max_size, "%s", str);
4650 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4651 if (obj->data.rss.act_par < data->item_count) {
4652 str = data->items[obj->data.rss.act_par].title;
4653 // remove trailing new line if one exists
4654 if (str[strlen(str) - 1] == '\n') {
4655 str[strlen(str) - 1] = 0;
4657 snprintf(p, p_max_size, "%s", str);
4659 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4660 if (obj->data.rss.act_par < data->item_count) {
4662 data->items[obj->data.rss.act_par].description;
4663 // remove trailing new line if one exists
4664 if (str[strlen(str) - 1] == '\n') {
4665 str[strlen(str) - 1] = 0;
4667 snprintf(p, p_max_size, "%s", str);
4669 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4670 if (data->item_count > 0) {
4673 //'tmpspaces' is a string with spaces too be placed in front of each title
4674 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4675 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4676 tmpspaces[obj->data.rss.nrspaces]=0;
4680 if (obj->data.rss.act_par > data->item_count) {
4681 show = data->item_count;
4683 show = obj->data.rss.act_par;
4685 for (itmp = 0; itmp < show; itmp++) {
4686 PRSS_Item *item = &data->items[itmp];
4690 // don't add new line before first item
4692 strncat(p, "\n", p_max_size);
4694 /* remove trailing new line if one exists,
4695 * we have our own */
4696 if (str[strlen(str) - 1] == '\n') {
4697 str[strlen(str) - 1] = 0;
4699 strncat(p, tmpspaces, p_max_size);
4700 strncat(p, str, p_max_size);
4711 if( obj->data.weather.uri != NULL ) {
4712 process_weather_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4714 strncpy(p, "either invalid xoap keys file or compiled without xoap support", p_max_size);
4720 char *str = llua_getstring(obj->data.s);
4722 snprintf(p, p_max_size, "%s", str);
4727 char *str = llua_getstring(obj->data.s);
4734 if (llua_getnumber(obj->data.s, &per)) {
4736 if(output_methods & TO_X) {
4737 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4740 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4741 new_bar_in_shell(p, p_max_size, per, obj->a);
4750 if (llua_getnumber(obj->data.s, &per)) {
4751 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4752 per, obj->e, 1, obj->char_a, obj->char_b);
4757 if (llua_getnumber(obj->data.s, &per)) {
4758 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4762 #endif /* HAVE_LUA */
4767 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4768 if (obj->data.hddtemp.temp)
4769 free(obj->data.hddtemp.temp);
4770 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4771 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4772 obj->data.hddtemp.update_time = current_update_time;
4774 if (!obj->data.hddtemp.temp) {
4775 snprintf(p, p_max_size, "N/A");
4777 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4778 unit = obj->data.hddtemp.temp[0];
4780 if (*endptr != '\0')
4781 snprintf(p, p_max_size, "N/A");
4782 else if (unit == 'C')
4783 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4784 else if (unit == 'F')
4785 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4787 snprintf(p, p_max_size, "N/A");
4792 new_offset(p, obj->data.i);
4795 new_voffset(p, obj->data.i);
4801 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4802 obj->data.sysfs.devtype, obj->data.sysfs.type);
4804 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4806 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4807 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4808 } else if (r >= 100.0 || r == 0) {
4809 snprintf(p, p_max_size, "%d", (int) r);
4811 snprintf(p, p_max_size, "%.1f", r);
4817 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4818 obj->data.sysfs.devtype, obj->data.sysfs.type);
4820 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4822 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4823 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4824 } else if (r >= 100.0 || r == 0) {
4825 snprintf(p, p_max_size, "%d", (int) r);
4827 snprintf(p, p_max_size, "%.1f", r);
4833 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4834 obj->data.sysfs.devtype, obj->data.sysfs.type);
4836 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4838 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4839 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4840 } else if (r >= 100.0 || r == 0) {
4841 snprintf(p, p_max_size, "%d", (int) r);
4843 snprintf(p, p_max_size, "%.1f", r);
4846 #endif /* __linux__ */
4848 new_alignr(p, obj->data.i);
4851 new_alignc(p, obj->data.i);
4854 char buf[max_user_text];
4855 struct information *tmp_info =
4856 malloc(sizeof(struct information));
4857 memcpy(tmp_info, cur, sizeof(struct information));
4858 generate_text_internal(buf, max_user_text,
4859 *obj->sub, tmp_info);
4861 if (strlen(buf) != 0) {
4867 char expression[max_user_text];
4869 struct information *tmp_info;
4871 tmp_info = malloc(sizeof(struct information));
4872 memcpy(tmp_info, cur, sizeof(struct information));
4873 generate_text_internal(expression, max_user_text,
4874 *obj->sub, tmp_info);
4875 DBGP("parsed arg into '%s'", expression);
4877 val = compare(expression);
4879 ERR("compare failed for expression '%s'",
4887 if (obj->data.ifblock.str
4888 && !check_contains(obj->data.ifblock.s,
4889 obj->data.ifblock.str)) {
4891 } else if (obj->data.ifblock.s
4892 && access(obj->data.ifblock.s, F_OK)) {
4897 if ((obj->data.ifblock.s)
4898 && (!check_mount(obj->data.ifblock.s))) {
4904 if (!get_process_by_name(obj->data.ifblock.s)) {
4906 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4911 #if defined(__linux__)
4913 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4917 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4920 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4925 human_readable(cur->mem * 1024, p, 255);
4928 human_readable(cur->memeasyfree * 1024, p, 255);
4931 human_readable(cur->memfree * 1024, p, 255);
4934 human_readable(cur->memmax * 1024, p, 255);
4938 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4942 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4943 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4948 if(output_methods & TO_X) {
4949 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4950 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4953 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4954 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4961 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4962 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4963 100, 1, obj->char_a, obj->char_b);
4968 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4971 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4974 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4978 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4979 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4982 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4983 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4986 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4987 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4990 OBJ(if_mixer_mute) {
4991 if (!mixer_is_mute(obj->data.ifblock.i)) {
4996 #define NOT_IN_X "Not running in X"
4998 if(x_initialised != YES) {
4999 strncpy(p, NOT_IN_X, p_max_size);
5001 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5004 OBJ(monitor_number) {
5005 if(x_initialised != YES) {
5006 strncpy(p, NOT_IN_X, p_max_size);
5008 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5012 if(x_initialised != YES) {
5013 strncpy(p, NOT_IN_X, p_max_size);
5015 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
5018 OBJ(desktop_number) {
5019 if(x_initialised != YES) {
5020 strncpy(p, NOT_IN_X, p_max_size);
5022 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
5026 if(x_initialised != YES) {
5027 strncpy(p, NOT_IN_X, p_max_size);
5028 }else if(cur->x11.desktop.name != NULL) {
5029 strncpy(p, cur->x11.desktop.name, p_max_size);
5036 update_mail_count(&obj->data.local_mail);
5037 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5040 update_mail_count(&obj->data.local_mail);
5041 snprintf(p, p_max_size, "%d",
5042 obj->data.local_mail.new_mail_count);
5045 update_mail_count(&obj->data.local_mail);
5046 snprintf(p, p_max_size, "%d",
5047 obj->data.local_mail.seen_mail_count);
5050 update_mail_count(&obj->data.local_mail);
5051 snprintf(p, p_max_size, "%d",
5052 obj->data.local_mail.unseen_mail_count);
5054 OBJ(flagged_mails) {
5055 update_mail_count(&obj->data.local_mail);
5056 snprintf(p, p_max_size, "%d",
5057 obj->data.local_mail.flagged_mail_count);
5059 OBJ(unflagged_mails) {
5060 update_mail_count(&obj->data.local_mail);
5061 snprintf(p, p_max_size, "%d",
5062 obj->data.local_mail.unflagged_mail_count);
5064 OBJ(forwarded_mails) {
5065 update_mail_count(&obj->data.local_mail);
5066 snprintf(p, p_max_size, "%d",
5067 obj->data.local_mail.forwarded_mail_count);
5069 OBJ(unforwarded_mails) {
5070 update_mail_count(&obj->data.local_mail);
5071 snprintf(p, p_max_size, "%d",
5072 obj->data.local_mail.unforwarded_mail_count);
5074 OBJ(replied_mails) {
5075 update_mail_count(&obj->data.local_mail);
5076 snprintf(p, p_max_size, "%d",
5077 obj->data.local_mail.replied_mail_count);
5079 OBJ(unreplied_mails) {
5080 update_mail_count(&obj->data.local_mail);
5081 snprintf(p, p_max_size, "%d",
5082 obj->data.local_mail.unreplied_mail_count);
5085 update_mail_count(&obj->data.local_mail);
5086 snprintf(p, p_max_size, "%d",
5087 obj->data.local_mail.draft_mail_count);
5089 OBJ(trashed_mails) {
5090 update_mail_count(&obj->data.local_mail);
5091 snprintf(p, p_max_size, "%d",
5092 obj->data.local_mail.trashed_mail_count);
5095 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5097 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5100 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5103 new_outline(p, obj->data.l);
5106 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5108 OBJ(running_processes) {
5109 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5112 snprintf(p, p_max_size, "%s", obj->data.s);
5116 new_bg(p, obj->data.l);
5119 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5123 human_readable(cur->swap * 1024, p, 255);
5126 human_readable(cur->swapfree * 1024, p, 255);
5129 human_readable(cur->swapmax * 1024, p, 255);
5132 if (cur->swapmax == 0) {
5133 strncpy(p, "No swap", p_max_size);
5135 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5140 if(output_methods & TO_X) {
5141 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5142 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5145 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5146 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5152 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5155 time_t t = time(NULL);
5156 struct tm *tm = localtime(&t);
5158 setlocale(LC_TIME, "");
5159 strftime(p, p_max_size, obj->data.s, tm);
5162 time_t t = time(NULL);
5163 struct tm *tm = gmtime(&t);
5165 strftime(p, p_max_size, obj->data.s, tm);
5172 if (obj->data.tztime.tz) {
5173 oldTZ = getenv("TZ");
5174 setenv("TZ", obj->data.tztime.tz, 1);
5180 setlocale(LC_TIME, "");
5181 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5183 setenv("TZ", oldTZ, 1);
5188 // Needless to free oldTZ since getenv gives ptr to static data
5191 human_readable(obj->data.net->recv, p, 255);
5194 human_readable(obj->data.net->trans, p, 255);
5197 snprintf(p, p_max_size, "%d", total_updates);
5200 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5205 human_readable(obj->data.net->trans_speed, p, 255);
5208 spaced_print(p, p_max_size, "%.1f", 8,
5209 obj->data.net->trans_speed / 1024.0);
5213 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5214 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5218 format_seconds_short(p, p_max_size, (int) cur->uptime);
5221 format_seconds(p, p_max_size, (int) cur->uptime);
5224 snprintf(p, p_max_size, "%s", cur->users.names);
5227 snprintf(p, p_max_size, "%s", cur->users.terms);
5230 snprintf(p, p_max_size, "%s", cur->users.times);
5233 snprintf(p, p_max_size, "%d", cur->users.number);
5235 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5236 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5240 msg = get_apm_adapter();
5241 snprintf(p, p_max_size, "%s", msg);
5244 OBJ(apm_battery_life) {
5247 msg = get_apm_battery_life();
5248 snprintf(p, p_max_size, "%s", msg);
5251 OBJ(apm_battery_time) {
5254 msg = get_apm_battery_time();
5255 snprintf(p, p_max_size, "%s", msg);
5258 #endif /* __FreeBSD__ __OpenBSD__ */
5261 #define mpd_printf(fmt, val) \
5262 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5263 #define mpd_sprintf(val) { \
5264 if (!obj->data.i || obj->data.i > p_max_size) \
5265 mpd_printf("%s", val); \
5267 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5272 mpd_sprintf(artist);
5276 mpd_printf("%s", random);
5278 mpd_printf("%s", repeat);
5286 mpd_printf("%d", volume);
5288 mpd_printf("%d", bitrate);
5290 mpd_printf("%s", status);
5292 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5295 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5298 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5302 if(output_methods & TO_X) {
5303 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5304 (int) (mpd_get_info()->progress * 255.0f));
5307 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5308 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5314 struct mpd_s *mpd = mpd_get_info();
5315 int len = obj->data.i;
5316 if (len == 0 || len > p_max_size)
5319 memset(p, 0, p_max_size);
5320 if (mpd->artist && *mpd->artist &&
5321 mpd->title && *mpd->title) {
5322 snprintf(p, len, "%s - %s", mpd->artist,
5324 } else if (mpd->title && *mpd->title) {
5325 snprintf(p, len, "%s", mpd->title);
5326 } else if (mpd->artist && *mpd->artist) {
5327 snprintf(p, len, "%s", mpd->artist);
5328 } else if (mpd->file && *mpd->file) {
5329 snprintf(p, len, "%s", mpd->file);
5334 OBJ(if_mpd_playing) {
5335 if (!mpd_get_info()->is_playing) {
5344 #define MOC_PRINT(t, a) \
5345 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5347 MOC_PRINT(state, "??");
5350 MOC_PRINT(file, "no file");
5353 MOC_PRINT(title, "no title");
5356 MOC_PRINT(artist, "no artist");
5359 MOC_PRINT(song, "no song");
5362 MOC_PRINT(album, "no album");
5364 OBJ(moc_totaltime) {
5365 MOC_PRINT(totaltime, "0:00");
5368 MOC_PRINT(timeleft, "0:00");
5371 MOC_PRINT(curtime, "0:00");
5374 MOC_PRINT(bitrate, "0Kbps");
5377 MOC_PRINT(rate, "0KHz");
5383 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5386 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5389 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5392 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5394 OBJ(xmms2_comment) {
5395 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5398 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5401 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5404 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5406 OBJ(xmms2_tracknr) {
5407 if (cur->xmms2.tracknr != -1) {
5408 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5411 OBJ(xmms2_bitrate) {
5412 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5415 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5418 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5420 OBJ(xmms2_elapsed) {
5421 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5422 (cur->xmms2.elapsed / 1000) % 60);
5424 OBJ(xmms2_duration) {
5425 snprintf(p, p_max_size, "%02d:%02d",
5426 cur->xmms2.duration / 60000,
5427 (cur->xmms2.duration / 1000) % 60);
5429 OBJ(xmms2_percent) {
5430 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5434 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5435 (int) (cur->xmms2.progress * 255.0f));
5438 OBJ(xmms2_playlist) {
5439 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5441 OBJ(xmms2_timesplayed) {
5442 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5445 if (strlen(cur->xmms2.title) < 2
5446 && strlen(cur->xmms2.title) < 2) {
5447 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5449 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5453 OBJ(if_xmms2_connected) {
5454 if (cur->xmms2.conn_state != 1) {
5460 OBJ(audacious_status) {
5461 snprintf(p, p_max_size, "%s",
5462 cur->audacious.items[AUDACIOUS_STATUS]);
5464 OBJ(audacious_title) {
5465 snprintf(p, cur->audacious.max_title_len > 0
5466 ? cur->audacious.max_title_len : p_max_size, "%s",
5467 cur->audacious.items[AUDACIOUS_TITLE]);
5469 OBJ(audacious_length) {
5470 snprintf(p, p_max_size, "%s",
5471 cur->audacious.items[AUDACIOUS_LENGTH]);
5473 OBJ(audacious_length_seconds) {
5474 snprintf(p, p_max_size, "%s",
5475 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5477 OBJ(audacious_position) {
5478 snprintf(p, p_max_size, "%s",
5479 cur->audacious.items[AUDACIOUS_POSITION]);
5481 OBJ(audacious_position_seconds) {
5482 snprintf(p, p_max_size, "%s",
5483 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5485 OBJ(audacious_bitrate) {
5486 snprintf(p, p_max_size, "%s",
5487 cur->audacious.items[AUDACIOUS_BITRATE]);
5489 OBJ(audacious_frequency) {
5490 snprintf(p, p_max_size, "%s",
5491 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5493 OBJ(audacious_channels) {
5494 snprintf(p, p_max_size, "%s",
5495 cur->audacious.items[AUDACIOUS_CHANNELS]);
5497 OBJ(audacious_filename) {
5498 snprintf(p, p_max_size, "%s",
5499 cur->audacious.items[AUDACIOUS_FILENAME]);
5501 OBJ(audacious_playlist_length) {
5502 snprintf(p, p_max_size, "%s",
5503 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5505 OBJ(audacious_playlist_position) {
5506 snprintf(p, p_max_size, "%s",
5507 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5509 OBJ(audacious_main_volume) {
5510 snprintf(p, p_max_size, "%s",
5511 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5514 OBJ(audacious_bar) {
5518 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5519 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5520 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5523 #endif /* AUDACIOUS */
5527 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5530 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5533 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5536 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5539 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5542 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5545 /* we have four different types of top (top, top_mem,
5546 * top_time and top_io). To avoid having almost-same code four
5547 * times, we have this special handler. */
5550 parse_top_args("top", obj->data.top.s, obj);
5551 if (!needed) needed = cur->cpu;
5553 parse_top_args("top_mem", obj->data.top.s, obj);
5554 if (!needed) needed = cur->memu;
5556 parse_top_args("top_time", obj->data.top.s, obj);
5557 if (!needed) needed = cur->time;
5560 parse_top_args("top_io", obj->data.top.s, obj);
5561 if (!needed) needed = cur->io;
5564 if (needed[obj->data.top.num]) {
5567 switch (obj->data.top.type) {
5569 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5570 needed[obj->data.top.num]->name);
5573 snprintf(p, 7, "%6.2f",
5574 needed[obj->data.top.num]->amount);
5577 snprintf(p, 6, "%5i",
5578 needed[obj->data.top.num]->pid);
5581 snprintf(p, 7, "%6.2f",
5582 needed[obj->data.top.num]->totalmem);
5585 timeval = format_time(
5586 needed[obj->data.top.num]->total_cpu_time, 9);
5587 snprintf(p, 10, "%9s", timeval);
5591 human_readable(needed[obj->data.top.num]->rss,
5595 human_readable(needed[obj->data.top.num]->vsize,
5599 case TOP_READ_BYTES:
5600 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5603 case TOP_WRITE_BYTES:
5604 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5608 snprintf(p, 7, "%6.2f",
5609 needed[obj->data.top.num]->io_perc);
5615 print_tail_object(obj, p, p_max_size);
5617 print_head_object(obj, p, p_max_size);
5619 FILE *fp = open_file(obj->data.s, &obj->a);
5622 /* FIXME: use something more general (see also tail.c, head.c */
5623 #define BUFSZ 0x1000
5628 while(fgets(buf, BUFSZ, fp) != NULL){
5629 for(j = 0; buf[j] != 0; j++) {
5630 if(buf[j] == '\n') {
5635 sprintf(p, "%d", lines);
5638 sprintf(p, "File Unreadable");
5643 FILE *fp = open_file(obj->data.s, &obj->a);
5648 char inword = FALSE;
5651 while(fgets(buf, BUFSZ, fp) != NULL){
5652 for(j = 0; buf[j] != 0; j++) {
5653 if(!isspace(buf[j])) {
5654 if(inword == FALSE) {
5663 sprintf(p, "%d", words);
5666 sprintf(p, "File Unreadable");
5669 #ifdef TCP_PORT_MONITOR
5671 tcp_portmon_action(p, p_max_size,
5672 &obj->data.tcp_port_monitor);
5674 #endif /* TCP_PORT_MONITOR */
5678 iconv_converting = 1;
5679 iconv_selected = obj->a;
5682 iconv_converting = 0;
5685 #endif /* HAVE_ICONV */
5687 OBJ(entropy_avail) {
5688 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5691 percent_print(p, p_max_size,
5692 cur->entropy.entropy_avail *
5693 100 / cur->entropy.poolsize);
5695 OBJ(entropy_poolsize) {
5696 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5699 double entropy_perc;
5701 entropy_perc = (double) cur->entropy.entropy_avail /
5702 (double) cur->entropy.poolsize;
5704 if(output_methods & TO_X) {
5705 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5708 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5709 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5718 s = smapi_get_val(obj->data.s);
5719 snprintf(p, p_max_size, "%s", s);
5723 OBJ(if_smapi_bat_installed) {
5725 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5726 if(!smapi_bat_installed(idx)) {
5730 ERR("argument to if_smapi_bat_installed must be an integer");
5732 OBJ(smapi_bat_perc) {
5734 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5735 val = smapi_bat_installed(idx) ?
5736 smapi_get_bat_int(idx, "remaining_percent") : 0;
5737 percent_print(p, p_max_size, val);
5739 ERR("argument to smapi_bat_perc must be an integer");
5741 OBJ(smapi_bat_temp) {
5743 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5744 val = smapi_bat_installed(idx) ?
5745 smapi_get_bat_int(idx, "temperature") : 0;
5746 /* temperature is in milli degree celsius */
5747 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5749 ERR("argument to smapi_bat_temp must be an integer");
5751 OBJ(smapi_bat_power) {
5753 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5754 val = smapi_bat_installed(idx) ?
5755 smapi_get_bat_int(idx, "power_now") : 0;
5756 /* power_now is in mW, set to W with one digit precision */
5757 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5759 ERR("argument to smapi_bat_power must be an integer");
5762 OBJ(smapi_bat_bar) {
5763 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5764 new_bar(p, obj->a, obj->b, (int)
5765 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5767 new_bar(p, obj->a, obj->b, 0);
5772 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5773 char buf[max_user_text];
5776 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5777 snprintf(p, p_max_size, "%s", buf);
5778 if(total_updates % 2) {
5779 for(j=0; p[j] != 0; j++) {
5785 char buf[max_user_text];
5787 char unit[16]; // 16 because we can also have long names (like mega-bytes)
5789 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5790 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5791 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5792 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5793 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5794 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5795 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5797 snprintf(p, p_max_size, "%s", buf);
5800 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
5802 char buf[max_user_text];
5803 generate_text_internal(buf, max_user_text,
5805 for(j = 0; buf[j] != 0; j++) {
5807 case '\n': //place all the lines behind each other with LINESEPARATOR between them
5808 #define LINESEPARATOR '|'
5809 buf[j]=LINESEPARATOR;
5816 //no scrolling necessary if the length of the text to scroll is too short
5817 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
5818 snprintf(p, p_max_size, "%s", buf);
5821 //make sure a colorchange at the front is not part of the string we are going to show
5822 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
5823 obj->data.scroll.start++;
5825 //place all chars that should be visible in p, including colorchanges
5826 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
5827 p[j] = *(buf + obj->data.scroll.start + j);
5828 if(p[j] == SPECIAL_CHAR) {
5829 visibcolorchanges++;
5831 //if there is still room fill it with spaces
5834 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
5838 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
5839 for(j = 0; j < obj->data.scroll.start; j++) {
5840 if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
5842 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
5843 for(j = 0; j < frontcolorchanges; j++) {
5844 pwithcolors[j] = SPECIAL_CHAR;
5847 strcat(pwithcolors,p);
5848 strend = strlen(pwithcolors);
5849 //and place the colorchanges not in front or in the visible part behind the visible part
5850 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
5851 pwithcolors[strend + j] = SPECIAL_CHAR;
5853 pwithcolors[strend + j] = 0;
5854 strcpy(p, pwithcolors);
5857 obj->data.scroll.start += obj->data.scroll.step;
5858 if(buf[obj->data.scroll.start] == 0){
5859 obj->data.scroll.start = 0;
5861 //reset color when scroll is finished
5862 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
5865 char buf[2][max_user_text];
5872 struct llrows* next;
5874 struct llrows *ll_rows[2], *current[2];
5875 struct text_object * objsub = obj->sub;
5878 for(i=0; i<2; i++) {
5881 ll_rows[i] = malloc(sizeof(struct llrows));
5882 current[i] = ll_rows[i];
5883 for(j=0; j<i; j++) objsub = objsub->sub;
5884 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5885 for(j=0; buf[i][j] != 0; j++) {
5886 if(buf[i][j] == '\t') buf[i][j] = ' ';
5887 if(buf[i][j] == '\n') {
5889 current[i]->row = strdup(buf[i]+nextstart);
5890 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5891 current[i]->next = malloc(sizeof(struct llrows));
5892 current[i] = current[i]->next;
5897 current[i]->row = strdup(buf[i]+nextstart);
5898 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5899 current[i]->next = NULL;
5900 current[i] = ll_rows[i];
5902 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5904 strcat(p, current[0]->row);
5905 i=strlen(current[0]->row);
5907 while(i < longest) {
5912 strcat(p, obj->data.combine.seperation);
5913 strcat(p, current[1]->row);
5917 #pragma omp parallel for schedule(dynamic,10)
5918 #endif /* HAVE_OPENMP */
5919 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5922 #pragma omp parallel for schedule(dynamic,10)
5923 #endif /* HAVE_OPENMP */
5924 for(i=0; i<2; i++) {
5925 while(ll_rows[i] != NULL) {
5926 current[i]=ll_rows[i];
5927 free(current[i]->row);
5928 ll_rows[i]=current[i]->next;
5935 int value = get_nvidia_value(obj->data.nvidia.type, display);
5937 snprintf(p, p_max_size, "N/A");
5938 else if (obj->data.nvidia.type == NV_TEMP)
5939 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5940 else if (obj->data.nvidia.print_as_float &&
5941 value > 0 && value < 100)
5942 snprintf(p, p_max_size, "%.1f", (float)value);
5944 snprintf(p, p_max_size, "%d", value);
5949 /* This is just a meta-object to set host:port */
5952 snprintf(p, p_max_size, "%s",
5953 cur->apcupsd.items[APCUPSD_NAME]);
5955 OBJ(apcupsd_model) {
5956 snprintf(p, p_max_size, "%s",
5957 cur->apcupsd.items[APCUPSD_MODEL]);
5959 OBJ(apcupsd_upsmode) {
5960 snprintf(p, p_max_size, "%s",
5961 cur->apcupsd.items[APCUPSD_UPSMODE]);
5963 OBJ(apcupsd_cable) {
5964 snprintf(p, p_max_size, "%s",
5965 cur->apcupsd.items[APCUPSD_CABLE]);
5967 OBJ(apcupsd_status) {
5968 snprintf(p, p_max_size, "%s",
5969 cur->apcupsd.items[APCUPSD_STATUS]);
5971 OBJ(apcupsd_linev) {
5972 snprintf(p, p_max_size, "%s",
5973 cur->apcupsd.items[APCUPSD_LINEV]);
5976 snprintf(p, p_max_size, "%s",
5977 cur->apcupsd.items[APCUPSD_LOAD]);
5979 OBJ(apcupsd_loadbar) {
5982 if(output_methods & TO_X) {
5983 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5984 new_bar(p, obj->a, obj->b, (int) progress);
5987 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5988 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5989 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5995 OBJ(apcupsd_loadgraph) {
5997 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5998 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5999 (int)progress, 100, 1, obj->char_a, obj->char_b);
6001 OBJ(apcupsd_loadgauge) {
6003 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
6004 new_gauge(p, obj->a, obj->b,
6008 OBJ(apcupsd_charge) {
6009 snprintf(p, p_max_size, "%s",
6010 cur->apcupsd.items[APCUPSD_CHARGE]);
6012 OBJ(apcupsd_timeleft) {
6013 snprintf(p, p_max_size, "%s",
6014 cur->apcupsd.items[APCUPSD_TIMELEFT]);
6017 snprintf(p, p_max_size, "%s",
6018 cur->apcupsd.items[APCUPSD_TEMP]);
6020 OBJ(apcupsd_lastxfer) {
6021 snprintf(p, p_max_size, "%s",
6022 cur->apcupsd.items[APCUPSD_LASTXFER]);
6024 #endif /* APCUPSD */
6031 unsigned int a = strlen(p);
6034 if (a > 0 && iconv_converting && iconv_selected > 0
6035 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6037 size_t dummy1, dummy2;
6039 const char *ptr = buff_in;
6041 char *ptr = buff_in;
6045 dummy1 = dummy2 = a;
6047 strncpy(buff_in, p, p_max_size);
6049 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6050 while (dummy1 > 0) {
6051 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6054 ERR("Iconv codeset conversion failed");
6059 /* It is nessecary when we are converting from multibyte to
6060 * singlebyte codepage */
6063 #endif /* HAVE_ICONV */
6064 if (obj->type != OBJ_text) {
6065 substitute_newlines(p, a - 2);
6073 /* load any new fonts we may have had */
6074 if (need_to_load_fonts) {
6080 void evaluate(char *text, char *buffer)
6082 struct information *tmp_info;
6083 struct text_object subroot;
6085 tmp_info = malloc(sizeof(struct information));
6086 memcpy(tmp_info, &info, sizeof(struct information));
6087 parse_conky_vars(&subroot, text, buffer, tmp_info);
6088 DBGP("evaluated '%s' to '%s'", text, buffer);
6090 free_text_objects(&subroot, 1);
6094 double current_update_time, next_update_time, last_update_time;
6096 static void generate_text(void)
6098 struct information *cur = &info;
6105 current_update_time = get_time();
6109 /* add things to the buffer */
6115 generate_text_internal(p, max_user_text, global_root_object, cur);
6117 if (stuff_in_uppercase) {
6120 tmp_p = text_buffer;
6122 *tmp_p = toupper(*tmp_p);
6127 next_update_time += update_interval;
6128 if (next_update_time < get_time()) {
6129 next_update_time = get_time() + update_interval;
6130 } else if (next_update_time > get_time() + update_interval) {
6131 next_update_time = get_time() + update_interval;
6133 last_update_time = current_update_time;
6137 static inline int get_string_width(const char *s)
6140 if (output_methods & TO_X) {
6141 return *s ? calc_text_width(s, strlen(s)) : 0;
6147 static int get_string_width_special(char *s, int special_index)
6155 if ((output_methods & TO_X) == 0) {
6157 return (s) ? strlen(s) : 0;
6165 p = strndup(s, text_buffer_size);
6169 if (*p == SPECIAL_CHAR) {
6170 /* shift everything over by 1 so that the special char
6171 * doesn't mess up the size calculation */
6172 for (i = 0; i < (long)strlen(p); i++) {
6173 *(p + i) = *(p + i + 1);
6175 if (specials[special_index + idx].type == GRAPH
6176 || specials[special_index + idx].type == GAUGE
6177 || specials[special_index + idx].type == BAR) {
6178 width += specials[special_index + idx].width;
6181 } else if (*p == SECRIT_MULTILINE_CHAR) {
6188 if (strlen(final) > 1) {
6189 width += calc_text_width(final, strlen(final));
6197 static int text_size_updater(char *s, int special_index);
6199 int last_font_height;
6200 static void update_text_area(void)
6204 if ((output_methods & TO_X) == 0)
6206 /* update text size if it isn't fixed */
6211 text_width = minimum_width;
6213 last_font_height = font_height();
6214 for_each_line(text_buffer, text_size_updater);
6216 if (text_height < minimum_height) {
6217 text_height = minimum_height;
6219 if (text_width > maximum_width && maximum_width > 0) {
6220 text_width = maximum_width;
6224 /* get text position on workarea */
6225 switch (text_alignment) {
6232 x = workarea[2] - text_width - gap_x;
6237 x = workarea[2] / 2 - text_width / 2 - gap_x;
6244 y = workarea[3] - text_height - gap_y;
6248 x = workarea[2] - text_width - gap_x;
6249 y = workarea[3] - text_height - gap_y;
6253 x = workarea[2] / 2 - text_width / 2 - gap_x;
6254 y = workarea[3] - text_height - gap_y;
6259 y = workarea[3] / 2 - text_height / 2 - gap_y;
6263 x = workarea[2] - text_width - gap_x;
6264 y = workarea[3] / 2 - text_height / 2 - gap_y;
6268 case NONE: // Let the WM manage the window
6279 if (own_window && !fixed_pos) {
6282 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6283 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6284 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6285 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6289 /* If window size doesn't match to workarea's size,
6290 * then window probably includes panels (gnome).
6291 * Blah, doesn't work on KDE. */
6292 if (workarea[2] != window.width || workarea[3] != window.height) {
6301 /* update lua window globals */
6302 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6303 #endif /* HAVE_LUA */
6308 static int cur_x, cur_y; /* current x and y for drawing */
6310 //draw_mode also without X11 because we only need to print to stdout with FG
6311 static int draw_mode; /* FG, BG or OUTLINE */
6313 static long current_color;
6315 static int text_size_updater(char *s, int special_index)
6320 if ((output_methods & TO_X) == 0)
6322 /* get string widths and skip specials */
6325 if (*p == SPECIAL_CHAR) {
6327 w += get_string_width(s);
6330 if (specials[special_index].type == BAR
6331 || specials[special_index].type == GAUGE
6332 || specials[special_index].type == GRAPH) {
6333 w += specials[special_index].width;
6334 if (specials[special_index].height > last_font_height) {
6335 last_font_height = specials[special_index].height;
6336 last_font_height += font_height();
6338 } else if (specials[special_index].type == OFFSET) {
6339 if (specials[special_index].arg > 0) {
6340 w += specials[special_index].arg;
6342 } else if (specials[special_index].type == VOFFSET) {
6343 last_font_height += specials[special_index].arg;
6344 } else if (specials[special_index].type == GOTO) {
6345 if (specials[special_index].arg > cur_x) {
6346 w = (int) specials[special_index].arg;
6348 } else if (specials[special_index].type == TAB) {
6349 int start = specials[special_index].arg;
6350 int step = specials[special_index].width;
6352 if (!step || step < 0) {
6355 w += step - (cur_x - text_start_x - start) % step;
6356 } else if (specials[special_index].type == FONT) {
6357 selected_font = specials[special_index].font_added;
6358 if (font_height() > last_font_height) {
6359 last_font_height = font_height();
6365 } else if (*p == SECRIT_MULTILINE_CHAR) {
6368 lw = get_string_width(s);
6369 *p = SECRIT_MULTILINE_CHAR;
6371 w = lw > w ? lw : w;
6372 text_height += last_font_height;
6376 w += get_string_width(s);
6377 if (w > text_width) {
6380 if (text_width > maximum_width && maximum_width) {
6381 text_width = maximum_width;
6384 text_height += last_font_height;
6385 last_font_height = font_height();
6386 return special_index;
6389 static inline void set_foreground_color(long c)
6391 if ((output_methods & TO_X) == 0)
6394 XSetForeground(display, window.gc, c);
6398 static void draw_string(const char *s)
6400 int i, i2, pos, width_of_s;
6408 width_of_s = get_string_width(s);
6409 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6411 fflush(stdout); /* output immediately, don't buffer */
6413 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6414 fprintf(stderr, "%s\n", s);
6415 fflush(stderr); /* output immediately, don't buffer */
6417 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6418 fprintf(overwrite_fpointer, "%s\n", s);
6420 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6421 fprintf(append_fpointer, "%s\n", s);
6423 memset(tmpstring1, 0, text_buffer_size);
6424 memset(tmpstring2, 0, text_buffer_size);
6425 strncpy(tmpstring1, s, text_buffer_size - 1);
6430 if (output_methods & TO_X) {
6431 max = ((text_width - width_of_s) / get_string_width(" "));
6434 /* This code looks for tabs in the text and coverts them to spaces.
6435 * The trick is getting the correct number of spaces, and not going
6436 * over the window's size without forcing the window larger. */
6437 for (i = 0; i < (int) text_buffer_size; i++) {
6438 if (tmpstring1[i] == '\t') {
6440 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6441 /* guard against overrun */
6442 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6447 /* guard against overrun */
6448 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6453 if (output_methods & TO_X) {
6454 if (text_width == maximum_width) {
6455 /* this means the text is probably pushing the limit,
6456 * so we'll chop it */
6457 while (cur_x + get_string_width(tmpstring2) - text_start_x
6458 > maximum_width && strlen(tmpstring2) > 0) {
6459 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6466 if (output_methods & TO_X) {
6472 c.pixel = current_color;
6473 XQueryColor(display, DefaultColormap(display, screen), &c);
6476 c2.color.red = c.red;
6477 c2.color.green = c.green;
6478 c2.color.blue = c.blue;
6479 c2.color.alpha = fonts[selected_font].font_alpha;
6481 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6482 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6484 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6485 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6490 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6493 cur_x += width_of_s;
6496 memcpy(tmpstring1, s, text_buffer_size);
6500 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
6502 int font_h = font_height();
6506 int last_special_needed = -1;
6507 int orig_special_index = special_index;
6509 cur_x = text_start_x;
6510 cur_y += font_ascent();
6513 if (*p == SECRIT_MULTILINE_CHAR) {
6514 /* special newline marker for multiline objects */
6519 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
6522 /* draw string before special, unless we're dealing multiline
6524 if (last_special_applied > -1) {
6525 special_index = last_special_applied;
6533 switch (specials[special_index].type) {
6534 case HORIZONTAL_LINE:
6536 int h = specials[special_index].height;
6537 int mid = font_ascent() / 2;
6539 w = text_start_x + text_width - cur_x;
6541 XSetLineAttributes(display, window.gc, h, LineSolid,
6542 CapButt, JoinMiter);
6543 XDrawLine(display, window.drawable, window.gc, cur_x,
6544 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6550 int h = specials[special_index].height;
6551 int tmp_s = specials[special_index].arg;
6552 int mid = font_ascent() / 2;
6553 char ss[2] = { tmp_s, tmp_s };
6555 w = text_start_x + text_width - cur_x - 1;
6556 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6557 CapButt, JoinMiter);
6558 XSetDashes(display, window.gc, 0, ss, 2);
6559 XDrawLine(display, window.drawable, window.gc, cur_x,
6560 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6566 int h, bar_usage, by;
6567 if (cur_x - text_start_x > maximum_width
6568 && maximum_width > 0) {
6571 h = specials[special_index].height;
6572 bar_usage = specials[special_index].arg;
6573 by = cur_y - (font_ascent() / 2) - 1;
6578 w = specials[special_index].width;
6580 w = text_start_x + text_width - cur_x - 1;
6586 XSetLineAttributes(display, window.gc, 1, LineSolid,
6587 CapButt, JoinMiter);
6589 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6591 XFillRectangle(display, window.drawable, window.gc, cur_x,
6592 by, w * bar_usage / 255, h);
6600 case GAUGE: /* new GAUGE */
6603 unsigned long last_colour = current_color;
6605 float angle, px, py;
6609 if (cur_x - text_start_x > maximum_width
6610 && maximum_width > 0) {
6614 h = specials[special_index].height;
6615 by = cur_y - (font_ascent() / 2) - 1;
6620 w = specials[special_index].width;
6622 w = text_start_x + text_width - cur_x - 1;
6628 XSetLineAttributes(display, window.gc, 1, LineSolid,
6629 CapButt, JoinMiter);
6631 XDrawArc(display, window.drawable, window.gc,
6632 cur_x, by, w, h * 2, 0, 180*64);
6635 usage = specials[special_index].arg;
6636 angle = (M_PI)*(float)(usage)/255.;
6637 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6638 py = (float)(by+(h))-(float)(h)*sin(angle);
6640 XDrawLine(display, window.drawable, window.gc,
6641 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6649 set_foreground_color(last_colour);
6657 int h, by, i = 0, j = 0;
6659 unsigned long last_colour = current_color;
6660 unsigned long *tmpcolour = 0;
6661 if (cur_x - text_start_x > maximum_width
6662 && maximum_width > 0) {
6665 h = specials[special_index].height;
6666 by = cur_y - (font_ascent() / 2) - 1;
6671 w = specials[special_index].width;
6673 w = text_start_x + text_width - cur_x - 1;
6678 if (draw_graph_borders) {
6679 XSetLineAttributes(display, window.gc, 1, LineSolid,
6680 CapButt, JoinMiter);
6681 XDrawRectangle(display, window.drawable, window.gc,
6684 XSetLineAttributes(display, window.gc, 1, LineSolid,
6685 CapButt, JoinMiter);
6687 if (specials[special_index].last_colour != 0
6688 || specials[special_index].first_colour != 0) {
6689 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6692 for (i = w - 2; i > -1; i--) {
6693 if (specials[special_index].last_colour != 0
6694 || specials[special_index].first_colour != 0) {
6695 if (specials[special_index].tempgrad) {
6698 (int)((float)(w - 2) - specials[special_index].graph[j] *
6699 (w - 2) / (float)specials[special_index].graph_scale)
6703 (int)((float)(w - 2) - specials[special_index].graph[j] *
6704 (w - 2) / (float)specials[special_index].graph_scale)
6707 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6709 (int)((float)(w - 2) - specials[special_index].graph[j] *
6710 (w - 2) / (float)specials[special_index].graph_scale)
6714 #endif /* DEBUG_lol */
6715 XSetForeground(display, window.gc, tmpcolour[
6716 (int)((float)(w - 2) - specials[special_index].graph[j] *
6717 (w - 2) / (float)specials[special_index].graph_scale)
6720 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6723 /* this is mugfugly, but it works */
6724 XDrawLine(display, window.drawable, window.gc,
6725 cur_x + i + 1, by + h, cur_x + i + 1,
6726 round_to_int((double)by + h - specials[special_index].graph[j] *
6727 (h - 1) / specials[special_index].graph_scale));
6728 if ((w - i) / ((float) (w - 2) /
6729 (specials[special_index].graph_width)) > j
6730 && j < MAX_GRAPH_DEPTH - 3) {
6734 if (tmpcolour) free(tmpcolour);
6739 /* if (draw_mode == BG) {
6740 set_foreground_color(default_bg_color);
6741 } else if (draw_mode == OUTLINE) {
6742 set_foreground_color(default_out_color);
6744 set_foreground_color(default_fg_color);
6746 if (show_graph_range) {
6749 unsigned short int seconds = update_interval * w;
6755 unsigned short int timeunits;
6757 timeunits = seconds / 86400; seconds %= 86400;
6758 if (timeunits > 0) {
6759 asprintf(&tmp_day_str, "%dd", timeunits);
6761 tmp_day_str = strdup("");
6763 timeunits = seconds / 3600; seconds %= 3600;
6764 if (timeunits > 0) {
6765 asprintf(&tmp_hour_str, "%dh", timeunits);
6767 tmp_hour_str = strdup("");
6769 timeunits = seconds / 60; seconds %= 60;
6770 if (timeunits > 0) {
6771 asprintf(&tmp_min_str, "%dm", timeunits);
6773 tmp_min_str = strdup("");
6776 asprintf(&tmp_sec_str, "%ds", seconds);
6778 tmp_sec_str = strdup("");
6780 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6781 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6783 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6785 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6786 cur_y += font_h / 2;
6787 draw_string(tmp_str);
6793 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6797 cur_x += font_ascent() / 2;
6798 cur_y += font_h / 2;
6800 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6802 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6803 draw_string(tmp_str);
6809 set_foreground_color(last_colour);
6815 int old = font_ascent();
6817 cur_y -= font_ascent();
6818 selected_font = specials[special_index].font_added;
6820 if (cur_y + font_ascent() < cur_y + old) {
6823 cur_y += font_ascent();
6825 font_h = font_height();
6829 if (draw_mode == FG) {
6830 set_foreground_color(specials[special_index].arg);
6835 if (draw_mode == BG) {
6836 set_foreground_color(specials[special_index].arg);
6841 if (draw_mode == OUTLINE) {
6842 set_foreground_color(specials[special_index].arg);
6847 w += specials[special_index].arg;
6848 last_special_needed = special_index;
6852 cur_y += specials[special_index].arg;
6856 if (specials[special_index].arg >= 0) {
6857 cur_x = (int) specials[special_index].arg;
6859 last_special_needed = special_index;
6864 int start = specials[special_index].arg;
6865 int step = specials[special_index].width;
6867 if (!step || step < 0) {
6870 w = step - (cur_x - text_start_x - start) % step;
6871 last_special_needed = special_index;
6877 /* TODO: add back in "+ window.border_inner_margin" to the end of
6879 int pos_x = text_start_x + text_width -
6880 get_string_width_special(s, special_index);
6882 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6883 "get_string_width(p) %i gap_x %i "
6884 "specials[special_index].arg %i window.border_inner_margin %i "
6885 "window.border_width %i\n", pos_x, text_start_x, text_width,
6886 cur_x, get_string_width_special(s), gap_x,
6887 specials[special_index].arg, window.border_inner_margin,
6888 window.border_width); */
6889 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6890 cur_x = pos_x - specials[special_index].arg;
6892 last_special_needed = special_index;
6898 int pos_x = (text_width) / 2 - get_string_width_special(s,
6899 special_index) / 2 - (cur_x -
6901 /* int pos_x = text_start_x + text_width / 2 -
6902 get_string_width_special(s) / 2; */
6904 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6905 "get_string_width(p) %i gap_x %i "
6906 "specials[special_index].arg %i\n", pos_x, text_start_x,
6907 text_width, cur_x, get_string_width(s), gap_x,
6908 specials[special_index].arg); */
6909 if (pos_x > specials[special_index].arg) {
6910 w = pos_x - specials[special_index].arg;
6912 last_special_needed = special_index;
6919 if (special_index != last_special_applied) {
6922 special_index = orig_special_index;
6923 last_special_applied = -1;
6931 cur_y += font_descent();
6932 if (recurse && *recurse) {
6933 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
6934 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
6936 return special_index;
6940 static int draw_line(char *s, int special_index)
6943 if ((output_methods & TO_X) == 0) {
6950 /* find specials and draw stuff */
6951 return draw_each_line_inner(s, special_index, -1);
6955 static void draw_text(void)
6959 llua_draw_pre_hook();
6960 #endif /* HAVE_LUA */
6961 if (output_methods & TO_X) {
6962 cur_y = text_start_y;
6965 if (draw_borders && window.border_width > 0) {
6966 if (stippled_borders) {
6967 char ss[2] = { stippled_borders, stippled_borders };
6968 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6969 CapButt, JoinMiter);
6970 XSetDashes(display, window.gc, 0, ss, 2);
6972 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6973 CapButt, JoinMiter);
6976 XDrawRectangle(display, window.drawable, window.gc,
6977 text_start_x - window.border_inner_margin - window.border_width,
6978 text_start_y - window.border_inner_margin - window.border_width,
6979 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6980 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6987 for_each_line(text_buffer, draw_line);
6988 #if defined(HAVE_LUA) && defined(X11)
6989 llua_draw_post_hook();
6990 #endif /* HAVE_LUA */
6993 static void draw_stuff(void)
6995 if (overwrite_file) {
6996 overwrite_fpointer = fopen(overwrite_file, "w");
6997 if(!overwrite_fpointer)
6998 ERR("Can't overwrite '%s' anymore", overwrite_file);
7001 append_fpointer = fopen(append_file, "a");
7002 if(!append_fpointer)
7003 ERR("Can't append '%s' anymore", append_file);
7006 if (output_methods & TO_X) {
7008 if (draw_shades && !draw_outline) {
7011 set_foreground_color(default_bg_color);
7022 for (i = -1; i < 2; i++) {
7023 for (j = -1; j < 2; j++) {
7024 if (i == 0 && j == 0) {
7029 set_foreground_color(default_out_color);
7030 draw_mode = OUTLINE;
7038 set_foreground_color(default_fg_color);
7044 if (output_methods & TO_X) {
7049 swap.swap_window = window.window;
7050 swap.swap_action = XdbeBackground;
7051 XdbeSwapBuffers(display, &swap, 1);
7056 if(overwrite_fpointer) {
7057 fclose(overwrite_fpointer);
7058 overwrite_fpointer = 0;
7060 if(append_fpointer) {
7061 fclose(append_fpointer);
7062 append_fpointer = 0;
7067 static void clear_text(int exposures)
7071 /* The swap action is XdbeBackground, which clears */
7075 if (display && window.window) { // make sure these are !null
7076 /* there is some extra space for borders and outlines */
7077 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
7078 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
7079 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7080 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
7085 static int need_to_update;
7087 /* update_text() generates new text and clears old text area */
7088 static void update_text(void)
7095 if (output_methods & TO_X)
7101 #ifdef HAVE_SYS_INOTIFY_H
7105 static void main_loop(void)
7108 #ifdef SIGNAL_BLOCKING
7109 sigset_t newmask, oldmask;
7112 #ifdef HAVE_SYS_INOTIFY_H
7113 int inotify_config_wd = -1;
7114 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
7115 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
7116 char inotify_buff[INOTIFY_BUF_LEN];
7117 #endif /* HAVE_SYS_INOTIFY_H */
7120 #ifdef SIGNAL_BLOCKING
7121 sigemptyset(&newmask);
7122 sigaddset(&newmask, SIGINT);
7123 sigaddset(&newmask, SIGTERM);
7124 sigaddset(&newmask, SIGUSR1);
7127 last_update_time = 0.0;
7128 next_update_time = get_time();
7130 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7133 #ifdef SIGNAL_BLOCKING
7134 /* block signals. we will inspect for pending signals later */
7135 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7136 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7141 if (output_methods & TO_X) {
7144 /* wait for X event or timeout */
7146 if (!XPending(display)) {
7150 t = next_update_time - get_time();
7154 } else if (t > update_interval) {
7155 t = update_interval;
7158 tv.tv_sec = (long) t;
7159 tv.tv_usec = (long) (t * 1000000) % 1000000;
7161 FD_SET(ConnectionNumber(display), &fdsr);
7163 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7165 if (errno != EINTR) {
7166 ERR("can't select(): %s", strerror(errno));
7176 if (need_to_update) {
7178 int wx = window.x, wy = window.y;
7188 /* resize window if it isn't right size */
7190 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7191 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7192 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7193 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7194 XResizeWindow(display, window.window, window.width,
7196 set_transparent_background(window.window);
7201 /* move window if it isn't in right position */
7202 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7203 XMoveWindow(display, window.window, window.x, window.y);
7208 if (changed && window.type == TYPE_PANEL) {
7211 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7214 switch (text_alignment) {
7241 set_struts(sidenum);
7252 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7253 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7254 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7255 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7256 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7261 /* handle X events */
7262 while (XPending(display)) {
7265 XNextEvent(display, &ev);
7272 r.width = ev.xexpose.width;
7273 r.height = ev.xexpose.height;
7274 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7278 case PropertyNotify:
7280 if ( ev.xproperty.state == PropertyNewValue ) {
7281 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7287 case ReparentNotify:
7288 /* set background to ParentRelative for all parents */
7290 set_transparent_background(window.window);
7294 case ConfigureNotify:
7296 /* if window size isn't what expected, set fixed size */
7297 if (ev.xconfigure.width != window.width
7298 || ev.xconfigure.height != window.height) {
7299 if (window.width != 0 && window.height != 0) {
7303 /* clear old stuff before screwing up
7308 XWindowAttributes attrs;
7309 if (XGetWindowAttributes(display,
7310 window.window, &attrs)) {
7311 window.width = attrs.width;
7312 window.height = attrs.height;
7316 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7317 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7318 if (text_width > maximum_width
7319 && maximum_width > 0) {
7320 text_width = maximum_width;
7324 /* if position isn't what expected, set fixed pos
7325 * total_updates avoids setting fixed_pos when window
7326 * is set to weird locations when started */
7327 /* // this is broken
7328 if (total_updates >= 2 && !fixed_pos
7329 && (window.x != ev.xconfigure.x
7330 || window.y != ev.xconfigure.y)
7331 && (ev.xconfigure.x != 0
7332 || ev.xconfigure.y != 0)) {
7340 /* if an ordinary window with decorations */
7341 if ((window.type == TYPE_NORMAL)
7342 && (!TEST_HINT(window.hints,
7343 HINT_UNDECORATED))) {
7344 /* allow conky to hold input focus. */
7347 /* forward the click to the desktop window */
7348 XUngrabPointer(display, ev.xbutton.time);
7349 ev.xbutton.window = window.desktop;
7350 ev.xbutton.x = ev.xbutton.x_root;
7351 ev.xbutton.y = ev.xbutton.y_root;
7352 XSendEvent(display, ev.xbutton.window, False,
7353 ButtonPressMask, &ev);
7354 XSetInputFocus(display, ev.xbutton.window,
7355 RevertToParent, ev.xbutton.time);
7362 /* if an ordinary window with decorations */
7363 if ((window.type == TYPE_NORMAL)
7364 && (!TEST_HINT(window.hints,
7365 HINT_UNDECORATED))) {
7366 /* allow conky to hold input focus. */
7369 /* forward the release to the desktop window */
7370 ev.xbutton.window = window.desktop;
7371 ev.xbutton.x = ev.xbutton.x_root;
7372 ev.xbutton.y = ev.xbutton.y_root;
7373 XSendEvent(display, ev.xbutton.window, False,
7374 ButtonReleaseMask, &ev);
7383 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7384 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7386 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7387 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7389 #endif /* HAVE_XDAMAGE */
7395 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7396 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7397 #endif /* HAVE_XDAMAGE */
7399 /* XDBE doesn't seem to provide a way to clear the back buffer without
7400 * interfering with the front buffer, other than passing XdbeBackground
7401 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7402 * redraw the text even if it wasn't part of the exposed area. OTOH,
7403 * if we're not going to call draw_stuff at all, then no swap happens
7404 * and we can safely do nothing. */
7406 if (!XEmptyRegion(x11_stuff.region)) {
7411 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7412 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7413 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7414 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7415 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7418 XSetRegion(display, window.gc, x11_stuff.region);
7421 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7425 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7428 XDestroyRegion(x11_stuff.region);
7429 x11_stuff.region = XCreateRegion();
7433 t = (next_update_time - get_time()) * 1000000;
7434 if(t > 0) usleep((useconds_t)t);
7441 #ifdef SIGNAL_BLOCKING
7442 /* unblock signals of interest and let handler fly */
7443 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7444 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7448 switch (g_signal_pending) {
7451 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7456 ERR("received SIGINT or SIGTERM to terminate. bye!");
7459 if (output_methods & TO_X) {
7460 XDestroyRegion(x11_stuff.region);
7461 x11_stuff.region = NULL;
7463 XDamageDestroy(display, x11_stuff.damage);
7464 XFixesDestroyRegion(display, x11_stuff.region2);
7465 XFixesDestroyRegion(display, x11_stuff.part);
7466 #endif /* HAVE_XDAMAGE */
7472 if(overwrite_file) {
7473 free(overwrite_file);
7482 /* Reaching here means someone set a signal
7483 * (SIGXXXX, signal_handler), but didn't write any code
7485 * If you don't want to handle a signal, don't set a handler on
7486 * it in the first place. */
7487 if (g_signal_pending) {
7488 ERR("ignoring signal (%d)", g_signal_pending);
7492 #ifdef HAVE_SYS_INOTIFY_H
7493 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7494 inotify_config_wd = inotify_add_watch(inotify_fd,
7498 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7499 int len = 0, idx = 0;
7501 struct timeval time_to_wait;
7503 FD_ZERO(&descriptors);
7504 FD_SET(inotify_fd, &descriptors);
7506 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7508 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7509 if (FD_ISSET(inotify_fd, &descriptors)) {
7510 /* process inotify events */
7511 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7512 while (len > 0 && idx < len) {
7513 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7514 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7515 /* current_config should be reloaded */
7516 ERR("'%s' modified, reloading...", current_config);
7518 if (ev->mask & IN_IGNORED) {
7519 /* for some reason we get IN_IGNORED here
7520 * sometimes, so we need to re-add the watch */
7521 inotify_config_wd = inotify_add_watch(inotify_fd,
7528 llua_inotify_query(ev->wd, ev->mask);
7530 #endif /* HAVE_LUA */
7531 idx += INOTIFY_EVENT_SIZE + ev->len;
7535 #endif /* HAVE_SYS_INOTIFY_H */
7537 g_signal_pending = 0;
7539 clean_up(NULL, NULL);
7541 #ifdef HAVE_SYS_INOTIFY_H
7542 if (inotify_fd != -1) {
7543 inotify_rm_watch(inotify_fd, inotify_config_wd);
7545 inotify_fd = inotify_config_wd = 0;
7547 #endif /* HAVE_SYS_INOTIFY_H */
7550 static void load_config_file(const char *);
7552 static void load_config_file_x11(const char *);
7554 void initialisation(int argc, char** argv);
7556 /* reload the config file */
7557 static void reload_config(void)
7559 char *current_config_copy = strdup(current_config);
7560 clean_up(NULL, NULL);
7561 current_config = current_config_copy;
7562 initialisation(argc_copy, argv_copy);
7565 void clean_up(void *memtofree1, void* memtofree2)
7574 timed_thread_destroy_registered_threads();
7576 if (info.cpu_usage) {
7577 free(info.cpu_usage);
7578 info.cpu_usage = NULL;
7581 if (x_initialised == YES) {
7584 if(x11_stuff.region) {
7585 XDestroyRegion(x11_stuff.region);
7586 x11_stuff.region = NULL;
7588 XClearWindow(display, RootWindow(display, screen));
7589 XCloseDisplay(display);
7591 if(info.x11.desktop.all_names) {
7592 free(info.x11.desktop.all_names);
7593 info.x11.desktop.all_names = NULL;
7595 if (info.x11.desktop.name) {
7596 free(info.x11.desktop.name);
7597 info.x11.desktop.name = NULL;
7601 free(fonts); //in set_default_configurations a font is set but not loaded
7607 for (i = 0; i < MAX_TEMPLATES; i++) {
7614 free_text_objects(&global_root_object, 0);
7633 free(current_config);
7636 #ifdef TCP_PORT_MONITOR
7637 tcp_portmon_clear();
7643 free_weather_info();
7647 #endif /* HAVE_LUA */
7650 for (i = 0; i < special_count; i++) {
7651 if (specials[i].type == GRAPH) {
7652 free(specials[i].graph);
7660 clear_diskio_stats();
7661 if(global_cpu != NULL) {
7667 static int string_to_bool(const char *s)
7670 // Assumes an option without a true/false means true
7672 } else if (strcasecmp(s, "yes") == EQUAL) {
7674 } else if (strcasecmp(s, "true") == EQUAL) {
7676 } else if (strcasecmp(s, "1") == EQUAL) {
7683 static enum alignment string_to_alignment(const char *s)
7685 if (strcasecmp(s, "top_left") == EQUAL) {
7687 } else if (strcasecmp(s, "top_right") == EQUAL) {
7689 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7691 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7693 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7694 return BOTTOM_RIGHT;
7695 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7696 return BOTTOM_MIDDLE;
7697 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7699 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7700 return MIDDLE_RIGHT;
7701 } else if (strcasecmp(s, "tl") == EQUAL) {
7703 } else if (strcasecmp(s, "tr") == EQUAL) {
7705 } else if (strcasecmp(s, "tm") == EQUAL) {
7707 } else if (strcasecmp(s, "bl") == EQUAL) {
7709 } else if (strcasecmp(s, "br") == EQUAL) {
7710 return BOTTOM_RIGHT;
7711 } else if (strcasecmp(s, "bm") == EQUAL) {
7712 return BOTTOM_MIDDLE;
7713 } else if (strcasecmp(s, "ml") == EQUAL) {
7715 } else if (strcasecmp(s, "mr") == EQUAL) {
7716 return MIDDLE_RIGHT;
7717 } else if (strcasecmp(s, "none") == EQUAL) {
7725 static void set_default_configurations_for_x(void)
7727 default_fg_color = WhitePixel(display, screen);
7728 default_bg_color = BlackPixel(display, screen);
7729 default_out_color = BlackPixel(display, screen);
7730 color0 = default_fg_color;
7731 color1 = default_fg_color;
7732 color2 = default_fg_color;
7733 color3 = default_fg_color;
7734 color4 = default_fg_color;
7735 color5 = default_fg_color;
7736 color6 = default_fg_color;
7737 color7 = default_fg_color;
7738 color8 = default_fg_color;
7739 color9 = default_fg_color;
7740 current_text_color = default_fg_color;
7744 static void set_default_configurations(void)
7752 fork_to_background = 0;
7753 total_run_times = 0;
7754 info.cpu_avg_samples = 2;
7755 info.net_avg_samples = 2;
7756 info.diskio_avg_samples = 2;
7761 format_human_readable = 1;
7768 mpd_env_host = getenv("MPD_HOST");
7769 mpd_env_port = getenv("MPD_PORT");
7771 if (!mpd_env_host || !strlen(mpd_env_host)) {
7772 mpd_set_host("localhost");
7774 /* MPD_HOST environment variable is set */
7775 char *mpd_hostpart = strchr(mpd_env_host, '@');
7776 if (!mpd_hostpart) {
7777 mpd_set_host(mpd_env_host);
7779 /* MPD_HOST contains a password */
7780 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7781 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7783 if (!strlen(mpd_hostpart + 1)) {
7784 mpd_set_host("localhost");
7786 mpd_set_host(mpd_hostpart + 1);
7789 mpd_set_password(mpd_password, 1);
7794 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7795 /* failed to set port from environment variable */
7796 mpd_set_port("6600");
7800 info.xmms2.artist = NULL;
7801 info.xmms2.album = NULL;
7802 info.xmms2.title = NULL;
7803 info.xmms2.genre = NULL;
7804 info.xmms2.comment = NULL;
7805 info.xmms2.url = NULL;
7806 info.xmms2.status = NULL;
7807 info.xmms2.playlist = NULL;
7809 use_spacer = NO_SPACER;
7811 output_methods = TO_X;
7813 output_methods = TO_STDOUT;
7816 show_graph_scale = 0;
7817 show_graph_range = 0;
7820 draw_graph_borders = 1;
7822 set_first_font("6x10");
7830 window.type = TYPE_NORMAL;
7832 strcpy(window.class_name, PACKAGE_NAME);
7833 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7835 stippled_borders = 0;
7836 window.border_inner_margin = 3;
7837 window.border_outer_margin = 1;
7838 window.border_width = 1;
7839 text_alignment = BOTTOM_LEFT;
7840 info.x11.monitor.number = 1;
7841 info.x11.monitor.current = 0;
7842 info.x11.desktop.current = 1;
7843 info.x11.desktop.number = 1;
7844 info.x11.desktop.nitems = 0;
7845 info.x11.desktop.all_names = NULL;
7846 info.x11.desktop.name = NULL;
7849 for (i = 0; i < MAX_TEMPLATES; i++) {
7852 template[i] = strdup("");
7855 free(current_mail_spool);
7859 variable_substitute(MAIL_FILE, buf, 256);
7860 if (buf[0] != '\0') {
7861 current_mail_spool = strndup(buf, text_buffer_size);
7866 update_interval = 3.0;
7867 info.music_player_interval = 1.0;
7868 stuff_in_uppercase = 0;
7869 info.users.number = 1;
7871 #ifdef TCP_PORT_MONITOR
7872 /* set default connection limit */
7873 tcp_portmon_set_max_connections(0);
7877 /* returns 1 if you can overwrite or create the file at 'path' */
7878 static _Bool overwrite_works(const char *path)
7882 if (!(filepointer = fopen(path, "w")))
7884 fclose(filepointer);
7888 /* returns 1 if you can append or create the file at 'path' */
7889 static _Bool append_works(const char *path)
7893 if (!(filepointer = fopen(path, "a")))
7895 fclose(filepointer);
7901 /* WARNING, this type not in Xlib spec */
7902 int x11_error_handler(Display *d, XErrorEvent *err)
7903 __attribute__((noreturn));
7904 int x11_error_handler(Display *d, XErrorEvent *err)
7906 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7908 (long unsigned)err->display,
7909 (long)err->resourceid,
7919 int x11_ioerror_handler(Display *d)
7920 __attribute__((noreturn));
7921 int x11_ioerror_handler(Display *d)
7923 ERR("X Error: Display %lx\n",
7930 static void X11_initialisation(void)
7932 if (x_initialised == YES) return;
7933 output_methods |= TO_X;
7935 set_default_configurations_for_x();
7936 x_initialised = YES;
7939 /* WARNING, this type not in Xlib spec */
7940 XSetErrorHandler(&x11_error_handler);
7941 XSetIOErrorHandler(&x11_ioerror_handler);
7945 static char **xargv = 0;
7946 static int xargc = 0;
7948 static void X11_create_window(void)
7950 if (output_methods & TO_X) {
7952 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7953 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7955 #else /* OWN_WINDOW */
7956 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7957 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7959 #endif /* OWN_WINDOW */
7963 update_text_area(); /* to position text/window on screen */
7966 if (own_window && !fixed_pos) {
7967 XMoveWindow(display, window.window, window.x, window.y);
7970 set_transparent_background(window.window);
7978 x11_stuff.region = XCreateRegion();
7980 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7981 ERR("Xdamage extension unavailable");
7983 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7984 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7985 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7986 #endif /* HAVE_XDAMAGE */
7989 update_text_area(); /* to get initial size of the window */
7992 /* setup lua window globals */
7993 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
7994 #endif /* HAVE_LUA */
7998 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7999 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
8000 #define CONF2(a) if (strcasecmp(name, a) == 0)
8001 #define CONF(a) else CONF2(a)
8002 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
8003 || strcasecmp(name, b) == 0)
8004 #define CONF_CONTINUE 1
8005 #define CONF_BREAK 2
8006 #define CONF_BUFF_SIZE 512
8008 static FILE *open_config_file(const char *f)
8010 #ifdef CONFIG_OUTPUT
8011 if (!strcmp(f, "==builtin==")) {
8012 return conf_cookie_open();
8014 #endif /* CONFIG_OUTPUT */
8015 return fopen(f, "r");
8018 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
8022 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8025 remove_comments(buf);
8030 while (*p && isspace((int) *p)) {
8034 return CONF_CONTINUE; /* empty line */
8041 while (*p2 && !isspace((int) *p2)) {
8045 *p2 = '\0'; /* break at name's end */
8052 while (*p && isspace((int) *p)) {
8058 p2 = *value + strlen(*value);
8059 while (isspace((int) *(p2 - 1))) {
8068 static void load_config_file(const char *f)
8073 set_default_configurations();
8074 fp = open_config_file(f);
8078 DBGP("reading contents from config file '%s'", f);
8081 char buff[CONF_BUFF_SIZE], *name, *value;
8082 int ret = do_config_step(&line, fp, buff, &name, &value);
8083 if (ret == CONF_BREAK) {
8085 } else if (ret == CONF_CONTINUE) {
8091 /* don't listen if X is already initialised or
8092 * if we already know we don't want it */
8093 if(x_initialised != YES) {
8094 if (string_to_bool(value)) {
8095 output_methods &= TO_X;
8097 output_methods &= ~TO_X;
8098 x_initialised = NEVER;
8103 if (!value || x_initialised == YES) {
8108 disp = strdup(value);
8113 if (window.type == TYPE_DOCK)
8116 #endif /*OWN_WINDOW */
8118 int a = string_to_alignment(value);
8129 CONF("background") {
8130 fork_to_background = string_to_bool(value);
8133 CONF2("background") {
8134 fork_to_background = string_to_bool(value);
8138 CONF("show_graph_scale") {
8139 show_graph_scale = string_to_bool(value);
8141 CONF("show_graph_range") {
8142 show_graph_range = string_to_bool(value);
8144 CONF("border_margin") {
8145 ERR("border_margin is deprecated, please use window.border_inner_margin instead");
8147 window.border_inner_margin = strtol(value, 0, 0);
8148 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8153 CONF("border_inner_margin") {
8155 window.border_inner_margin = strtol(value, 0, 0);
8156 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8161 CONF("border_outer_margin") {
8163 window.border_outer_margin = strtol(value, 0, 0);
8164 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8169 CONF("border_width") {
8171 window.border_width = strtol(value, 0, 0);
8172 if (window.border_width < 0) window.border_width = 0;
8178 #define TEMPLATE_CONF(n) \
8179 CONF("template"#n) { \
8181 free(template[n]); \
8182 template[n] = strdup(value); \
8199 info.mail = parse_mail_args(IMAP_TYPE, value);
8206 info.mail = parse_mail_args(POP3_TYPE, value);
8211 CONF("default_bar_size") {
8214 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8221 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8225 CONF("default_graph_size") {
8228 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8235 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8238 CONF("default_gauge_size") {
8241 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8248 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8255 mpd_set_host(value);
8261 if (value && mpd_set_port(value)) {
8265 CONF("mpd_password") {
8267 mpd_set_password(value, 0);
8273 CONF("music_player_interval") {
8275 info.music_player_interval = strtod(value, 0);
8281 CONF("sensor_device") {
8283 sensor_device = strtol(value, 0, 0);
8289 CONF("cpu_avg_samples") {
8291 cpu_avg_samples = strtol(value, 0, 0);
8292 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8295 info.cpu_avg_samples = cpu_avg_samples;
8301 CONF("net_avg_samples") {
8303 net_avg_samples = strtol(value, 0, 0);
8304 if (net_avg_samples < 1 || net_avg_samples > 14) {
8307 info.net_avg_samples = net_avg_samples;
8313 CONF("diskio_avg_samples") {
8315 diskio_avg_samples = strtol(value, 0, 0);
8316 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8319 info.diskio_avg_samples = diskio_avg_samples;
8327 CONF("double_buffer") {
8328 use_xdbe = string_to_bool(value);
8332 CONF("override_utf8_locale") {
8333 utf8_mode = string_to_bool(value);
8335 CONF("draw_borders") {
8336 draw_borders = string_to_bool(value);
8338 CONF("draw_graph_borders") {
8339 draw_graph_borders = string_to_bool(value);
8341 CONF("draw_shades") {
8342 draw_shades = string_to_bool(value);
8344 CONF("draw_outline") {
8345 draw_outline = string_to_bool(value);
8348 CONF("out_to_console") {
8349 if(string_to_bool(value))
8350 output_methods |= TO_STDOUT;
8352 CONF("out_to_stderr") {
8353 if(string_to_bool(value))
8354 output_methods |= TO_STDERR;
8356 CONF("overwrite_file") {
8357 if(overwrite_file) {
8358 free(overwrite_file);
8361 if(overwrite_works(value)) {
8362 overwrite_file = strdup(value);
8363 output_methods |= OVERWRITE_FILE;
8365 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8367 CONF("append_file") {
8372 if(append_works(value)) {
8373 append_file = strdup(value);
8374 output_methods |= APPEND_FILE;
8376 ERR("append_file won't be able to create/append '%s'", value);
8378 CONF("use_spacer") {
8380 if (strcasecmp(value, "left") == EQUAL) {
8381 use_spacer = LEFT_SPACER;
8382 } else if (strcasecmp(value, "right") == EQUAL) {
8383 use_spacer = RIGHT_SPACER;
8384 } else if (strcasecmp(value, "none") == EQUAL) {
8385 use_spacer = NO_SPACER;
8387 use_spacer = string_to_bool(value);
8388 ERR("use_spacer should have an argument of left, right, or"
8389 " none. '%s' seems to be some form of '%s', so"
8390 " defaulting to %s.", value,
8391 use_spacer ? "true" : "false",
8392 use_spacer ? "right" : "none");
8394 use_spacer = RIGHT_SPACER;
8396 use_spacer = NO_SPACER;
8400 ERR("use_spacer should have an argument. Defaulting to right.");
8401 use_spacer = RIGHT_SPACER;
8407 use_xft = string_to_bool(value);
8411 set_first_font(value);
8415 if (value && font_count >= 0) {
8416 fonts[0].font_alpha = atof(value) * 65535.0;
8423 if (string_to_bool(value)) {
8424 ERR("Xft not enabled at compile time");
8428 /* xftfont silently ignored when no Xft */
8431 /* xftalpha is silently ignored when no Xft */
8436 set_first_font(value);
8444 gap_x = atoi(value);
8451 gap_y = atoi(value);
8457 CONF("mail_spool") {
8461 variable_substitute(value, buffer, 256);
8463 if (buffer[0] != '\0') {
8464 if (current_mail_spool) {
8465 free(current_mail_spool);
8467 current_mail_spool = strndup(buffer, text_buffer_size);
8474 CONF("minimum_size") {
8476 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8478 if (sscanf(value, "%d", &minimum_width) != 1) {
8486 CONF("maximum_width") {
8488 if (sscanf(value, "%d", &maximum_width) != 1) {
8496 CONF("no_buffers") {
8497 no_buffers = string_to_bool(value);
8499 CONF("top_name_width") {
8501 if (sscanf(value, "%u", &top_name_width) != 1) {
8507 if (top_name_width >= max_user_text) {
8508 top_name_width = max_user_text - 1;
8511 CONF("top_cpu_separate") {
8512 cpu_separate = string_to_bool(value);
8514 CONF("short_units") {
8515 short_units = string_to_bool(value);
8517 CONF("format_human_readable") {
8518 format_human_readable = string_to_bool(value);
8520 CONF("pad_percents") {
8521 pad_percents = atoi(value);
8525 CONF("own_window") {
8527 own_window = string_to_bool(value);
8530 CONF("own_window_class") {
8532 memset(window.class_name, 0, sizeof(window.class_name));
8533 strncpy(window.class_name, value,
8534 sizeof(window.class_name) - 1);
8537 CONF("own_window_title") {
8539 memset(window.title, 0, sizeof(window.title));
8540 strncpy(window.title, value, sizeof(window.title) - 1);
8543 CONF("own_window_transparent") {
8545 set_transparent = string_to_bool(value);
8548 CONF("own_window_hints") {
8550 char *p_hint, *p_save;
8551 char delim[] = ", ";
8553 /* tokenize the value into individual hints */
8554 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8556 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8557 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8558 SET_HINT(window.hints, HINT_UNDECORATED);
8559 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8560 SET_HINT(window.hints, HINT_BELOW);
8561 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8562 SET_HINT(window.hints, HINT_ABOVE);
8563 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8564 SET_HINT(window.hints, HINT_STICKY);
8565 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8566 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8567 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8568 SET_HINT(window.hints, HINT_SKIP_PAGER);
8573 p_hint = strtok_r(NULL, delim, &p_save);
8574 } while (p_hint != NULL);
8580 CONF("own_window_type") {
8582 if (strncmp(value, "normal", 6) == EQUAL) {
8583 window.type = TYPE_NORMAL;
8584 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8585 window.type = TYPE_DESKTOP;
8586 } else if (strncmp(value, "dock", 4) == EQUAL) {
8587 window.type = TYPE_DOCK;
8588 text_alignment = TOP_LEFT;
8589 } else if (strncmp(value, "panel", 5) == EQUAL) {
8590 window.type = TYPE_PANEL;
8591 } else if (strncmp(value, "override", 8) == EQUAL) {
8592 window.type = TYPE_OVERRIDE;
8601 CONF("stippled_borders") {
8603 stippled_borders = strtol(value, 0, 0);
8605 stippled_borders = 4;
8609 CONF("imlib_cache_size") {
8611 cimlib_set_cache_size(atoi(value));
8614 CONF("imlib_cache_flush_interval") {
8616 cimlib_set_cache_flush_interval(atoi(value));
8621 CONF("update_interval") {
8623 update_interval = strtod(value, 0);
8627 if (info.music_player_interval == 0) {
8628 // default to update_interval
8629 info.music_player_interval = update_interval;
8632 CONF("total_run_times") {
8634 total_run_times = strtod(value, 0);
8640 stuff_in_uppercase = string_to_bool(value);
8642 CONF("max_specials") {
8644 max_specials = atoi(value);
8649 CONF("max_user_text") {
8651 max_user_text = atoi(value);
8656 CONF("text_buffer_size") {
8658 text_buffer_size = atoi(value);
8659 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8660 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8661 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8669 if (output_methods & TO_X) {
8670 X11_initialisation();
8679 global_text = (char *) malloc(1);
8680 global_text[0] = '\0';
8683 unsigned int l = strlen(global_text);
8685 char buf[CONF_BUFF_SIZE];
8687 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8693 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8701 /* Check for continuation of \\-\n. */
8702 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8703 global_text[l-1] = '\0';
8707 global_text = (char *) realloc(global_text, l + bl + 1);
8708 strcat(global_text, buf);
8710 if (strlen(global_text) > max_user_text) {
8715 if (strlen(global_text) < 1) {
8716 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
8718 global_text_lines = line + 1;
8721 #ifdef TCP_PORT_MONITOR
8722 CONF("max_port_monitor_connections") {
8724 if (!value || (sscanf(value, "%d", &max) != 1)) {
8725 /* an error. use default, warn and continue. */
8726 tcp_portmon_set_max_connections(0);
8728 } else if (tcp_portmon_set_max_connections(max)) {
8729 /* max is < 0, default has been set*/
8734 CONF("if_up_strictness") {
8736 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8737 ifup_strictness = IFUP_UP;
8738 } else if (strcasecmp(value, "up") == EQUAL) {
8739 ifup_strictness = IFUP_UP;
8740 } else if (strcasecmp(value, "link") == EQUAL) {
8741 ifup_strictness = IFUP_LINK;
8742 } else if (strcasecmp(value, "address") == EQUAL) {
8743 ifup_strictness = IFUP_ADDR;
8745 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8746 ifup_strictness = IFUP_UP;
8750 CONF("temperature_unit") {
8752 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8753 } else if (set_temp_output_unit(value)) {
8754 ERR("temperature_unit: incorrect argument");
8760 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8761 char *skey = malloc(maxlength);
8762 char *svalue = malloc(maxlength);
8764 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8765 oldvalue = getenv(skey);
8766 if (oldvalue == NULL) {
8767 setenv(skey, svalue, 0);
8769 //PS: Don't free oldvalue, it's the real envvar, not a copy
8782 char *ptr = strtok(value, " ");
8785 ptr = strtok(NULL, " ");
8792 CONF("lua_draw_hook_pre") {
8794 llua_set_draw_pre_hook(value);
8799 CONF("lua_draw_hook_post") {
8801 llua_set_draw_post_hook(value);
8807 #endif /* HAVE_LUA */
8819 CONF("default_color"){}
8820 CONF3("default_shade_color", "default_shadecolor"){}
8821 CONF3("default_outline_color", "default_outlinecolor") {}
8822 CONF("own_window_colour") {}
8825 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8831 if (info.music_player_interval == 0) {
8832 // default to update_interval
8833 info.music_player_interval = update_interval;
8835 if (!global_text) { // didn't supply any text
8836 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
8841 static void load_config_file_x11(const char *f)
8846 fp = open_config_file(f);
8850 DBGP("reading contents from config file '%s'", f);
8853 char buff[CONF_BUFF_SIZE], *name, *value;
8854 int ret = do_config_step(&line, fp, buff, &name, &value);
8855 if (ret == CONF_BREAK) {
8857 } else if (ret == CONF_CONTINUE) {
8862 X11_initialisation();
8863 if (x_initialised == YES) {
8865 color0 = get_x11_color(value);
8872 X11_initialisation();
8873 if (x_initialised == YES) {
8875 color1 = get_x11_color(value);
8882 X11_initialisation();
8883 if (x_initialised == YES) {
8885 color2 = get_x11_color(value);
8892 X11_initialisation();
8893 if (x_initialised == YES) {
8895 color3 = get_x11_color(value);
8902 X11_initialisation();
8903 if (x_initialised == YES) {
8905 color4 = get_x11_color(value);
8912 X11_initialisation();
8913 if (x_initialised == YES) {
8915 color5 = get_x11_color(value);
8922 X11_initialisation();
8923 if (x_initialised == YES) {
8925 color6 = get_x11_color(value);
8932 X11_initialisation();
8933 if (x_initialised == YES) {
8935 color7 = get_x11_color(value);
8942 X11_initialisation();
8943 if (x_initialised == YES) {
8945 color8 = get_x11_color(value);
8952 X11_initialisation();
8953 if (x_initialised == YES) {
8955 color9 = get_x11_color(value);
8961 CONF("default_color") {
8962 X11_initialisation();
8963 if (x_initialised == YES) {
8965 default_fg_color = get_x11_color(value);
8971 CONF3("default_shade_color", "default_shadecolor") {
8972 X11_initialisation();
8973 if (x_initialised == YES) {
8975 default_bg_color = get_x11_color(value);
8981 CONF3("default_outline_color", "default_outlinecolor") {
8982 X11_initialisation();
8983 if (x_initialised == YES) {
8985 default_out_color = get_x11_color(value);
8992 CONF("own_window_colour") {
8993 X11_initialisation();
8994 if (x_initialised == YES) {
8996 background_colour = get_x11_color(value);
8998 ERR("Invalid colour for own_window_colour (try omitting the "
8999 "'#' for hex colours");
9005 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
9006 if(output_methods & TO_X) {
9007 X11_initialisation();
9016 #undef CONF_CONTINUE
9017 #undef CONF_BUFF_SIZE
9025 #if defined(WEATHER) && defined(XOAP)
9027 * TODO: make the xoap keys file readable from the config file
9028 * make the keys directly readable from the config file
9029 * make the xoap keys file giveable as a command line option
9031 static void load_xoap_keys(void)
9034 char *par = (char *) malloc(11 * sizeof(char));
9035 char *key = (char *) malloc(17 * sizeof(char));
9037 xoap = (char *) malloc(64 * sizeof(char));
9038 to_real_path(xoap, XOAP_FILE);
9039 fp = fopen(xoap, "r");
9041 if( fscanf(fp, "%10s %16s", par, key) == 2 ) {
9042 strcpy(xoap, "?cc=*&link=xoap&prod=xoap&par=");
9044 strcat(xoap, "&key=");
9046 strcat(xoap, "&unit=m");
9059 #endif /* WEATHER && XOAP */
9061 static void print_help(const char *prog_name) {
9062 printf("Usage: %s [OPTION]...\n"
9063 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
9064 "window. Command line options will override configurations defined in config\n"
9066 " -v, --version version\n"
9067 " -q, --quiet quiet mode\n"
9068 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
9069 " -c, --config=FILE config file to load\n"
9070 #ifdef CONFIG_OUTPUT
9071 " -C, --print-config print the builtin default config to stdout\n"
9072 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
9074 " -d, --daemonize daemonize, fork to background\n"
9075 " -h, --help help\n"
9077 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
9078 " -f, --font=FONT font to use\n"
9079 " -X, --display=DISPLAY X11 display to use\n"
9081 " -o, --own-window create own window to draw\n"
9084 " -b, --double-buffer double buffer (prevents flickering)\n"
9086 " -w, --window-id=WIN_ID window id to draw\n"
9087 " -x X x position\n"
9088 " -y Y y position\n"
9090 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
9091 " -u, --interval=SECS update interval\n"
9092 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
9097 /* : means that character before that takes an argument */
9098 static const char *getopt_string = "vVqdDt:u:i:hc:"
9108 #ifdef CONFIG_OUTPUT
9113 static const struct option longopts[] = {
9114 { "help", 0, NULL, 'h' },
9115 { "version", 0, NULL, 'V' },
9116 { "debug", 0, NULL, 'D' },
9117 { "config", 1, NULL, 'c' },
9118 #ifdef CONFIG_OUTPUT
9119 { "print-config", 0, NULL, 'C' },
9121 { "daemonize", 0, NULL, 'd' },
9123 { "alignment", 1, NULL, 'a' },
9124 { "font", 1, NULL, 'f' },
9125 { "display", 1, NULL, 'X' },
9127 { "own-window", 0, NULL, 'o' },
9130 { "double-buffer", 0, NULL, 'b' },
9132 { "window-id", 1, NULL, 'w' },
9134 { "text", 1, NULL, 't' },
9135 { "interval", 0, NULL, 'u' },
9139 void initialisation(int argc, char **argv) {
9140 struct sigaction act, oact;
9142 load_config_file(current_config);
9144 /* init specials array */
9145 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9146 ERR("failed to create specials array");
9150 if (current_mail_spool == NULL) {
9153 variable_substitute(MAIL_FILE, buf, 256);
9155 if (buf[0] != '\0') {
9156 current_mail_spool = strndup(buf, text_buffer_size);
9161 /* handle other command line arguments */
9163 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9164 || defined(__NetBSD__)
9165 optind = optreset = 1;
9170 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9171 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9172 "kvm_open")) == NULL) {
9173 CRIT_ERR(NULL, NULL, "cannot read kvm");
9178 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9186 fork_to_background = 1;
9189 global_debug_level++;
9193 set_first_font(optarg);
9196 text_alignment = string_to_alignment(optarg);
9201 disp = strdup(optarg);
9220 global_text = strndup(optarg, max_user_text);
9221 convert_escapes(global_text);
9225 update_interval = strtod(optarg, 0);
9226 if (info.music_player_interval == 0) {
9227 // default to update_interval
9228 info.music_player_interval = update_interval;
9233 total_run_times = strtod(optarg, 0);
9237 gap_x = atoi(optarg);
9241 gap_y = atoi(optarg);
9252 if (output_methods & TO_X) {
9253 load_config_file_x11(current_config);
9257 /* generate text and get initial size */
9258 extract_variable_text(global_text);
9265 if (fork_to_background) {
9270 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9277 fprintf(stderr, "\n");
9282 /* parent process */
9283 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9290 text_buffer = malloc(max_user_text);
9291 memset(text_buffer, 0, max_user_text);
9292 tmpstring1 = malloc(text_buffer_size);
9293 memset(tmpstring1, 0, text_buffer_size);
9294 tmpstring2 = malloc(text_buffer_size);
9295 memset(tmpstring2, 0, text_buffer_size);
9300 X11_create_window();
9303 /* Set signal handlers */
9304 act.sa_handler = signal_handler;
9305 sigemptyset(&act.sa_mask);
9308 act.sa_flags |= SA_RESTART;
9311 if ( sigaction(SIGINT, &act, &oact) < 0
9312 || sigaction(SIGUSR1, &act, &oact) < 0
9313 || sigaction(SIGHUP, &act, &oact) < 0
9314 || sigaction(SIGTERM, &act, &oact) < 0) {
9315 ERR("error setting signal handler: %s", strerror(errno));
9320 int main(int argc, char **argv)
9329 g_signal_pending = 0;
9330 max_user_text = MAX_USER_TEXT_DEFAULT;
9332 memset(&info, 0, sizeof(info));
9333 memset(template, 0, sizeof(template));
9336 #ifdef TCP_PORT_MONITOR
9337 /* set default connection limit */
9338 tcp_portmon_set_max_connections(0);
9341 /* handle command line parameters that don't change configs */
9343 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9344 || ((s = getenv("LANG")) && *s)) {
9345 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9347 ERR("malloc failed");
9349 for (x = 0; x < strlen(s); x++) {
9350 temp[x] = tolower(s[x]);
9353 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9359 if (!setlocale(LC_CTYPE, "")) {
9360 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9364 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9375 if (current_config) {
9376 free(current_config);
9378 current_config = strndup(optarg, max_user_text);
9381 freopen("/dev/null", "w", stderr);
9384 print_help(argv[0]);
9386 #ifdef CONFIG_OUTPUT
9393 window.window = strtol(optarg, 0, 0);
9402 /* check if specified config file is valid */
9403 if (current_config) {
9405 if (stat(current_config, &sb) ||
9406 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9407 ERR("invalid configuration file '%s'\n", current_config);
9408 free(current_config);
9413 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9415 if (!current_config) {
9416 /* load default config file */
9417 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9420 /* Try to use personal config file first */
9421 to_real_path(buf, CONFIG_FILE);
9422 if (buf[0] && (fp = fopen(buf, "r"))) {
9423 current_config = strndup(buf, max_user_text);
9427 /* Try to use system config file if personal config not readable */
9428 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9429 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9433 /* No readable config found */
9434 if (!current_config) {
9435 #ifdef CONFIG_OUTPUT
9436 current_config = strdup("==builtin==");
9437 ERR("no readable personal or system-wide config file found,"
9438 " using builtin default");
9440 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9441 #endif /* ! CONF_OUTPUT */
9445 #if defined(WEATHER) && defined(XOAP)
9446 /* Load xoap keys, if existing */
9448 #endif /* WEATHER && XOAP */
9450 #ifdef HAVE_SYS_INOTIFY_H
9451 inotify_fd = inotify_init();
9452 #endif /* HAVE_SYS_INOTIFY_H */
9454 initialisation(argc, argv);
9458 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9465 static void signal_handler(int sig)
9467 /* signal handler is light as a feather, as it should be.
9468 * we will poll g_signal_pending with each loop of conky
9469 * and do any signal processing there, NOT here. */
9470 g_signal_pending = sig;