1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "text_object.h"
44 #include <sys/param.h>
45 #ifdef HAVE_SYS_INOTIFY_H
46 #include <sys/inotify.h>
47 #endif /* HAVE_SYS_INOTIFY_H */
50 #include <X11/Xutil.h>
52 #include <X11/extensions/Xdamage.h>
58 #include <sys/types.h>
60 #include <netinet/in.h>
79 #include "temphelper.h"
83 /* check for OS and include appropriate headers */
84 #if defined(__linux__)
86 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
88 #elif defined(__OpenBSD__)
92 #if defined(__FreeBSD_kernel__)
96 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
97 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
98 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
99 int apm_getinfo(int fd, apm_info_t aip);
100 char *get_apm_adapter(void);
101 char *get_apm_battery_life(void);
102 char *get_apm_battery_time(void);
110 #include "defconfig.h"
111 #include "conf_cookie.h"
115 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
118 #define MAIL_FILE "$MAIL"
119 #define MAX_IF_BLOCK_DEPTH 5
121 //#define SIGNAL_BLOCKING
122 #undef SIGNAL_BLOCKING
124 /* debugging level, used by logging.h */
125 int global_debug_level = 0;
127 /* two strings for internal use */
128 static char *tmpstring1, *tmpstring2;
130 /* variables holding various config settings */
132 int format_human_readable;
139 int top_cpu, top_mem, top_time;
143 static unsigned int top_name_width = 15;
145 enum x_initialiser_state x_initialised = NO;
146 static volatile int g_signal_pending;
147 /* Update interval */
148 double update_interval;
149 void *global_cpu = NULL;
154 /* prototypes for internally used functions */
155 static void signal_handler(int);
156 static void print_version(void) __attribute__((noreturn));
157 static void reload_config(void);
158 static void generate_text_internal(char *, int, struct text_object,
159 struct information *);
160 static int extract_variable_text_internal(struct text_object *,
163 static void print_version(void)
165 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
167 printf("\nCompiled in features:\n\n"
168 "System config file: "SYSTEM_CONFIG_FILE"\n"
169 "Package library path: "PACKAGE_LIBDIR"\n\n"
173 " * Xdamage extension\n"
174 # endif /* HAVE_XDAMAGE */
176 " * XDBE (double buffer extension)\n"
177 # endif /* HAVE_XDBE */
182 "\n Music detection:\n"
185 #endif /* AUDACIOUS */
201 #endif /* HAVE_OPENMP */
208 #ifdef TCP_PORT_MONITOR
210 #endif /* TCP_PORT_MONITOR */
215 " * Weather (METAR)\n"
219 #endif /* HAVE_IWLIB */
221 " * support for IBM/Lenovo notebooks\n"
231 #endif /* CONFIG_OUTPUT */
236 " * ALSA mixer support\n"
237 #endif /* MIXER_IS_ALSA */
247 #ifdef HAVE_LUA_CAIRO
249 #endif /* HAVE_LUA_CAIRO */
250 #ifdef HAVE_LUA_IMLIB2
253 #endif /* HAVE_LUA */
259 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
264 static void X11_create_window(void);
265 static void X11_initialisation(void);
267 struct _x11_stuff_s {
271 XserverRegion region2, part;
272 int event_base, error_base;
278 static int text_start_x, text_start_y; /* text start position in window */
279 static int text_width, text_height;
294 /* display to connect to */
295 static char *disp = NULL;
299 /* struct that has all info to be shared between
300 * instances of the same text object */
301 struct information info;
303 /* path to config file */
304 char *current_config;
306 /* set to 1 if you want all text to be in uppercase */
307 static unsigned int stuff_in_upper_case;
309 /* Run how many times? */
310 static unsigned long total_run_times;
313 static int fork_to_background;
315 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
317 /* filenames for output */
318 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
319 char *append_file = NULL; FILE *append_fpointer = NULL;
321 /* xoap suffix for weather from weather.com */
323 static char *xoap = NULL;
328 static int show_graph_scale;
329 static int show_graph_range;
331 /* Position on the screen */
332 static int text_alignment;
333 static int gap_x, gap_y;
336 static int draw_borders;
337 static int draw_graph_borders;
338 static int stippled_borders;
340 static int draw_shades, draw_outline;
342 static long default_fg_color, default_bg_color, default_out_color;
344 /* create own window or draw stuff to root? */
345 static int set_transparent = 0;
348 static int own_window = 0;
349 static int background_colour = 0;
351 /* fixed size/pos is set if wm/user changes them */
352 static int fixed_size = 0, fixed_pos = 0;
355 static int minimum_width, minimum_height;
356 static int maximum_width;
361 static int sensor_device;
364 static long color0, color1, color2, color3, color4, color5, color6, color7,
367 #define MAX_TEMPLATES 10
368 static char *template[MAX_TEMPLATES];
370 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
371 unsigned int max_user_text;
373 /* maximum size of individual text buffers, ie $exec buffer size */
374 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
377 #define CODEPAGE_LENGTH 20
379 long iconv_count = 0;
380 char iconv_converting;
381 static iconv_t **iconv_cd = 0;
383 int register_iconv(iconv_t *new_iconv)
385 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
387 CRIT_ERR(NULL, NULL, "Out of memory");
389 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
390 if (!iconv_cd[iconv_count]) {
391 CRIT_ERR(NULL, NULL, "Out of memory");
393 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
398 void free_iconv(void)
403 for (i = 0; i < iconv_count; i++) {
405 iconv_close(*iconv_cd[i]);
419 /* no buffers in used memory? */
422 /* pad percentages to decimals? */
423 static int pad_percents = 0;
425 static char *global_text = 0;
426 long global_text_lines;
428 static int total_updates;
429 static int updatereset;
431 int check_contains(char *f, char *s)
434 FILE *where = open_file(f, 0);
439 while (fgets(buf1, 256, where)) {
440 if (strstr(buf1, s)) {
447 ERR("Could not open the file");
453 static inline int calc_text_width(const char *s, int l)
455 if ((output_methods & TO_X) == 0)
462 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
463 (const FcChar8 *) s, l, &gi);
465 XftTextExtents8(display, fonts[selected_font].xftfont,
466 (const FcChar8 *) s, l, &gi);
472 return XTextWidth(fonts[selected_font].font, s, l);
477 /* formatted text to render on screen, generated in generate_text(),
478 * drawn in draw_stuff() */
480 static char *text_buffer;
483 static unsigned int special_index; /* used when drawing */
486 /* quite boring functions */
488 static inline void for_each_line(char *b, void f(char *))
492 for (ps = b, pe = b; *pe; pe++) {
506 static void convert_escapes(char *buf)
508 char *p = buf, *s = buf;
515 } else if (*s == '\\') {
526 /* Prints anything normally printed with snprintf according to the current value
527 * of use_spacer. Actually slightly more flexible than snprintf, as you can
528 * safely specify the destination buffer as one of your inputs. */
529 int spaced_print(char *buf, int size, const char *format, int width, ...)
538 tempbuf = malloc(size * sizeof(char));
540 // Passes the varargs along to vsnprintf
541 va_start(argp, width);
542 vsnprintf(tempbuf, size, format, argp);
545 switch (use_spacer) {
547 len = snprintf(buf, size, "%s", tempbuf);
550 len = snprintf(buf, size, "%*s", width, tempbuf);
553 len = snprintf(buf, size, "%-*s", width, tempbuf);
560 /* print percentage values
562 * - i.e., unsigned values between 0 and 100
563 * - respect the value of pad_percents */
564 static int percent_print(char *buf, int size, unsigned value)
566 return spaced_print(buf, size, "%u", pad_percents, value);
569 /* converts from bytes to human readable format (K, M, G, T)
571 * The algorithm always divides by 1024, as unit-conversion of byte
572 * counts suggests. But for output length determination we need to
573 * compare with 1000 here, as we print in decimal form. */
574 static void human_readable(long long num, char *buf, int size)
576 const char **suffix = suffixes;
582 /* Possibly just output as usual, for example for stdout usage */
583 if (!format_human_readable) {
584 spaced_print(buf, size, "%d", 6, round_to_int(num));
595 if (llabs(num) < 1000LL) {
596 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
600 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
608 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
610 * The goal is to always have a significance of 3, by
611 * adjusting the decimal part of the number. Sample output:
615 * so the point of alignment resides between number and unit. The
616 * upside of this is that there is minimal padding necessary, though
617 * there should be a way to make alignment take place at the decimal
618 * dot (then with fixed width decimal part).
620 * Note the repdigits below: when given a precision value, printf()
621 * rounds the float to it, not just cuts off the remaining digits. So
622 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
623 * printed with a precision of 0. Yay. */
625 precision = 0; /* print 100-999 without decimal part */
627 precision = 1; /* print 10-99 with one decimal place */
629 precision = 2; /* print 0-9 with two decimal places */
631 spaced_print(buf, size, format, width, precision, fnum, *suffix);
634 /* global object list root element */
635 static struct text_object global_root_object;
637 static inline void read_exec(const char *data, char *buf, const int size)
639 FILE *fp = popen(data, "r");
640 int length = fread(buf, 1, size, fp);
644 if (length > 0 && buf[length - 1] == '\n') {
645 buf[length - 1] = '\0';
649 void *threaded_exec(void *) __attribute__((noreturn));
651 void *threaded_exec(void *arg)
654 struct text_object *obj = (struct text_object *)arg;
657 buff = malloc(text_buffer_size);
658 read_exec(obj->data.texeci.cmd, buff,
667 timed_thread_lock(obj->data.texeci.p_timed_thread);
668 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
669 timed_thread_unlock(obj->data.texeci.p_timed_thread);
671 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
672 timed_thread_exit(obj->data.texeci.p_timed_thread);
678 static struct text_object *new_text_object_internal(void)
680 struct text_object *obj = malloc(sizeof(struct text_object));
681 memset(obj, 0, sizeof(struct text_object));
686 * Frees the list of text objects root points to. When internal = 1, it won't
687 * free global objects.
689 static void free_text_objects(struct text_object *root, int internal)
691 struct text_object *obj;
697 #define data obj->data
698 for (obj = root->prev; obj; obj = root->prev) {
699 root->prev = obj->prev;
705 #endif /* !__OpenBSD__ */
710 close(data.sysfs.fd);
712 #endif /* __linux__ */
714 free(data.read_tcp.host);
721 free(data.tztime.tz);
722 free(data.tztime.fmt);
725 free(data.mboxscan.args);
726 free(data.mboxscan.output);
731 case OBJ_unseen_mails:
732 case OBJ_flagged_mails:
733 case OBJ_unflagged_mails:
734 case OBJ_forwarded_mails:
735 case OBJ_unforwarded_mails:
736 case OBJ_replied_mails:
737 case OBJ_unreplied_mails:
738 case OBJ_draft_mails:
739 case OBJ_trashed_mails:
740 free(data.local_mail.box);
742 case OBJ_imap_unseen:
747 case OBJ_imap_messages:
752 case OBJ_pop3_unseen:
764 free_text_objects(obj->sub, 1);
767 case OBJ_if_existing:
770 free(data.ifblock.s);
771 free(data.ifblock.str);
774 free(data.tail.logfile);
775 free(data.tail.buffer);
791 case OBJ_iconv_start:
796 case OBJ_disk_protect:
800 free(data.ifblock.s);
801 free(data.ifblock.str);
804 if (info.gw_info.iface) {
805 free(info.gw_info.iface);
806 info.gw_info.iface = 0;
808 if (info.gw_info.ip) {
809 free(info.gw_info.ip);
813 case OBJ_ioscheduler:
818 #if (defined(__FreeBSD__) || defined(__linux__))
820 free(data.ifblock.s);
821 free(data.ifblock.str);
825 case OBJ_xmms2_artist:
826 if (info.xmms2.artist) {
827 free(info.xmms2.artist);
828 info.xmms2.artist = 0;
831 case OBJ_xmms2_album:
832 if (info.xmms2.album) {
833 free(info.xmms2.album);
834 info.xmms2.album = 0;
837 case OBJ_xmms2_title:
838 if (info.xmms2.title) {
839 free(info.xmms2.title);
840 info.xmms2.title = 0;
843 case OBJ_xmms2_genre:
844 if (info.xmms2.genre) {
845 free(info.xmms2.genre);
846 info.xmms2.genre = 0;
849 case OBJ_xmms2_comment:
850 if (info.xmms2.comment) {
851 free(info.xmms2.comment);
852 info.xmms2.comment = 0;
856 if (info.xmms2.url) {
857 free(info.xmms2.url);
862 if (info.xmms2.date) {
863 free(info.xmms2.date);
867 case OBJ_xmms2_status:
868 if (info.xmms2.status) {
869 free(info.xmms2.status);
870 info.xmms2.status = 0;
873 case OBJ_xmms2_playlist:
874 if (info.xmms2.playlist) {
875 free(info.xmms2.playlist);
876 info.xmms2.playlist = 0;
879 case OBJ_xmms2_smart:
880 if (info.xmms2.artist) {
881 free(info.xmms2.artist);
882 info.xmms2.artist = 0;
884 if (info.xmms2.title) {
885 free(info.xmms2.title);
886 info.xmms2.title = 0;
888 if (info.xmms2.url) {
889 free(info.xmms2.url);
896 case OBJ_bmpx_artist:
900 case OBJ_bmpx_bitrate:
910 free(data.rss.action);
915 free(data.weather.uri);
916 free(data.weather.data_type);
928 #endif /* HAVE_LUA */
935 case OBJ_battery_short:
938 case OBJ_battery_time:
941 #endif /* !__OpenBSD__ */
949 free(data.execi.cmd);
950 free(data.execi.buffer);
953 free(data.texeci.cmd);
954 free(data.texeci.buffer);
965 if (info.first_process && !internal) {
966 free_all_processes();
967 info.first_process = NULL;
969 if (data.top.s) free(data.top.s);
973 free(data.hddtemp.dev);
974 free(data.hddtemp.addr);
975 if (data.hddtemp.temp)
976 free(data.hddtemp.temp);
979 case OBJ_entropy_avail:
980 case OBJ_entropy_perc:
981 case OBJ_entropy_poolsize:
982 case OBJ_entropy_bar:
985 if (info.users.names) {
986 free(info.users.names);
987 info.users.names = 0;
991 if (info.users.terms) {
992 free(info.users.terms);
993 info.users.terms = 0;
997 if (info.users.times) {
998 free(info.users.times);
999 info.users.times = 0;
1004 case OBJ_smapi_bat_perc:
1005 case OBJ_smapi_bat_temp:
1006 case OBJ_smapi_bat_power:
1009 case OBJ_if_smapi_bat_installed:
1010 free(data.ifblock.s);
1011 free(data.ifblock.str);
1020 case OBJ_mpd_artist:
1022 case OBJ_mpd_random:
1023 case OBJ_mpd_repeat:
1025 case OBJ_mpd_bitrate:
1026 case OBJ_mpd_status:
1028 case OBJ_mpd_elapsed:
1029 case OBJ_mpd_length:
1033 case OBJ_mpd_percent:
1035 case OBJ_if_mpd_playing:
1043 case OBJ_moc_artist:
1046 case OBJ_moc_totaltime:
1047 case OBJ_moc_timeleft:
1048 case OBJ_moc_curtime:
1049 case OBJ_moc_bitrate:
1056 free_text_objects(obj->sub, 1);
1060 free(data.scroll.text);
1061 free_text_objects(obj->sub, 1);
1065 free(data.combine.left);
1066 free(data.combine.seperation);
1067 free(data.combine.right);
1068 free_text_objects(obj->sub, 1);
1073 case OBJ_apcupsd_name:
1074 case OBJ_apcupsd_model:
1075 case OBJ_apcupsd_upsmode:
1076 case OBJ_apcupsd_cable:
1077 case OBJ_apcupsd_status:
1078 case OBJ_apcupsd_linev:
1079 case OBJ_apcupsd_load:
1080 case OBJ_apcupsd_loadbar:
1082 case OBJ_apcupsd_loadgraph:
1083 case OBJ_apcupsd_loadgauge:
1085 case OBJ_apcupsd_charge:
1086 case OBJ_apcupsd_timeleft:
1087 case OBJ_apcupsd_temp:
1088 case OBJ_apcupsd_lastxfer:
1090 #endif /* APCUPSD */
1093 case OBJ_desktop_number:
1094 case OBJ_desktop_name:
1095 if(info.x11.desktop.name) {
1096 free(info.x11.desktop.name);
1097 info.x11.desktop.name = NULL;
1099 if(info.x11.desktop.all_names) {
1100 free(info.x11.desktop.all_names);
1101 info.x11.desktop.all_names = NULL;
1112 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1117 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1118 *a = mixer_init(buf1);
1119 scan_bar(arg + n, w, h);
1121 *a = mixer_init(NULL);
1122 scan_bar(arg, w, h);
1127 /* strip a leading /dev/ if any, following symlinks first
1129 * BEWARE: this function returns a pointer to static content
1130 * which gets overwritten in consecutive calls. I.e.:
1131 * this function is NOT reentrant.
1133 static const char *dev_name(const char *path)
1135 static char buf[255]; /* should be enough for pathnames */
1141 #define DEV_NAME(x) \
1142 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1143 if ((buflen = readlink(path, buf, 254)) == -1)
1144 return DEV_NAME(path);
1146 return DEV_NAME(buf);
1150 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1155 if (obj->data.top.was_parsed) {
1158 obj->data.top.was_parsed = 1;
1160 if (arg && !obj->data.top.s) {
1161 obj->data.top.s = strndup(arg, text_buffer_size);
1164 need_mask |= (1 << INFO_TOP);
1167 obj->type = OBJ_top;
1169 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1170 obj->type = OBJ_top_mem;
1172 } else if (strcmp(&s[3], "_time") == EQUAL) {
1173 obj->type = OBJ_top_time;
1176 } else if (strcmp(&s[3], "_io") == EQUAL) {
1177 obj->type = OBJ_top_io;
1182 ERR("Must be top, top_mem, top_time or top_io");
1184 ERR("Must be top, top_mem or top_time");
1190 ERR("top needs arguments");
1194 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1195 if (strcmp(buf, "name") == EQUAL) {
1196 obj->data.top.type = TOP_NAME;
1197 } else if (strcmp(buf, "cpu") == EQUAL) {
1198 obj->data.top.type = TOP_CPU;
1199 } else if (strcmp(buf, "pid") == EQUAL) {
1200 obj->data.top.type = TOP_PID;
1201 } else if (strcmp(buf, "mem") == EQUAL) {
1202 obj->data.top.type = TOP_MEM;
1203 } else if (strcmp(buf, "time") == EQUAL) {
1204 obj->data.top.type = TOP_TIME;
1205 } else if (strcmp(buf, "mem_res") == EQUAL) {
1206 obj->data.top.type = TOP_MEM_RES;
1207 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1208 obj->data.top.type = TOP_MEM_VSIZE;
1210 } else if (strcmp(buf, "io_read") == EQUAL) {
1211 obj->data.top.type = TOP_READ_BYTES;
1212 } else if (strcmp(buf, "io_write") == EQUAL) {
1213 obj->data.top.type = TOP_WRITE_BYTES;
1214 } else if (strcmp(buf, "io_perc") == EQUAL) {
1215 obj->data.top.type = TOP_IO_PERC;
1218 ERR("invalid type arg for top");
1220 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1221 "io_read, io_write, io_perc");
1223 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1227 if (n < 1 || n > 10) {
1228 ERR("invalid num arg for top. Must be between 1 and 10.");
1231 obj->data.top.num = n - 1;
1234 ERR("invalid argument count for top");
1240 /* construct_text_object() creates a new text_object */
1241 static struct text_object *construct_text_object(const char *s,
1242 const char *arg, long line, char allow_threaded, void **ifblock_opaque, void *free_at_crash)
1244 // struct text_object *obj = new_text_object();
1245 struct text_object *obj = new_text_object_internal();
1249 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1250 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1251 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1252 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1253 obj_be_ifblock_if(ifblock_opaque, obj); {
1254 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1255 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1256 #define END } } else
1258 #define SIZE_DEFAULTS(arg) { \
1259 obj->a = default_##arg##_width; \
1260 obj->b = default_##arg##_height; \
1265 obj->type = OBJ_color;
1266 obj->data.l = get_x11_color(s);
1270 OBJ(freq, INFO_FREQ)
1273 obj->data.i = open_acpi_temperature(arg);
1274 END OBJ(acpiacadapter, 0)
1275 END OBJ(freq, INFO_FREQ)
1276 #endif /* !__OpenBSD__ */
1278 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1279 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1280 obj->data.cpu_index = 1;
1281 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1282 "Displaying the clock for CPU 1."); */
1284 obj->data.cpu_index = atoi(&arg[0]);
1287 END OBJ(freq_g, INFO_FREQ)
1289 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1290 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1291 obj->data.cpu_index = 1;
1292 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1293 "CPUs! Displaying the clock for CPU 1."); */
1295 obj->data.cpu_index = atoi(&arg[0]);
1298 END OBJ(read_tcp, 0)
1300 obj->data.read_tcp.host = malloc(text_buffer_size);
1301 sscanf(arg, "%s", obj->data.read_tcp.host);
1302 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1303 if(obj->data.read_tcp.port == 0) {
1304 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1305 strcpy(obj->data.read_tcp.host,"localhost");
1307 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1308 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1309 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1312 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1314 #if defined(__linux__)
1315 END OBJ(voltage_mv, 0)
1317 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1318 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1319 obj->data.cpu_index = 1;
1320 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1321 "CPUs! Displaying voltage for CPU 1."); */
1323 obj->data.cpu_index = atoi(&arg[0]);
1326 END OBJ(voltage_v, 0)
1328 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1329 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1330 obj->data.cpu_index = 1;
1331 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1332 "CPUs! Displaying voltage for CPU 1."); */
1334 obj->data.cpu_index = atoi(&arg[0]);
1339 END OBJ(wireless_essid, INFO_NET)
1341 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1343 // default to DEFAULTNETDEV
1344 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1345 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1348 END OBJ(wireless_mode, INFO_NET)
1350 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1352 // default to DEFAULTNETDEV
1353 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1354 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1357 END OBJ(wireless_bitrate, INFO_NET)
1359 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1361 // default to DEFAULTNETDEV
1362 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1363 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1366 END OBJ(wireless_ap, INFO_NET)
1368 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1370 // default to DEFAULTNETDEV
1371 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1372 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1375 END OBJ(wireless_link_qual, INFO_NET)
1377 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1379 // default to DEFAULTNETDEV
1380 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1381 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1384 END OBJ(wireless_link_qual_max, INFO_NET)
1386 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1388 // default to DEFAULTNETDEV
1389 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1390 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1393 END OBJ(wireless_link_qual_perc, INFO_NET)
1395 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1397 // default to DEFAULTNETDEV
1398 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1399 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1402 END OBJ(wireless_link_bar, INFO_NET)
1405 arg = scan_bar(arg, &obj->a, &obj->b);
1406 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1408 // default to DEFAULTNETDEV
1409 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1410 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1413 #endif /* HAVE_IWLIB */
1415 #endif /* __linux__ */
1423 sscanf(arg, "%63s", bat);
1425 strcpy(bat, "BAT0");
1427 obj->data.s = strndup(bat, text_buffer_size);
1428 END OBJ(battery_short, 0)
1432 sscanf(arg, "%63s", bat);
1434 strcpy(bat, "BAT0");
1436 obj->data.s = strndup(bat, text_buffer_size);
1437 END OBJ(battery_time, 0)
1441 sscanf(arg, "%63s", bat);
1443 strcpy(bat, "BAT0");
1445 obj->data.s = strndup(bat, text_buffer_size);
1446 END OBJ(battery_percent, 0)
1450 sscanf(arg, "%63s", bat);
1452 strcpy(bat, "BAT0");
1454 obj->data.s = strndup(bat, text_buffer_size);
1455 END OBJ(battery_bar, 0)
1460 arg = scan_bar(arg, &obj->a, &obj->b);
1461 sscanf(arg, "%63s", bat);
1463 strcpy(bat, "BAT0");
1465 obj->data.s = strndup(bat, text_buffer_size);
1466 #endif /* !__OpenBSD__ */
1468 #if defined(__linux__)
1469 END OBJ(disk_protect, 0)
1471 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1473 CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1474 END OBJ(i8k_version, INFO_I8K)
1475 END OBJ(i8k_bios, INFO_I8K)
1476 END OBJ(i8k_serial, INFO_I8K)
1477 END OBJ(i8k_cpu_temp, INFO_I8K)
1478 END OBJ(i8k_left_fan_status, INFO_I8K)
1479 END OBJ(i8k_right_fan_status, INFO_I8K)
1480 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1481 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1482 END OBJ(i8k_ac_status, INFO_I8K)
1483 END OBJ(i8k_buttons_status, INFO_I8K)
1486 END OBJ(ibm_temps, 0)
1488 CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1490 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1491 obj->data.sensor = 0;
1492 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1493 "Using 0 (CPU temp sensor).");
1495 obj->data.sensor = atoi(&arg[0]);
1496 END OBJ(ibm_volume, 0)
1497 END OBJ(ibm_brightness, 0)
1499 /* information from sony_laptop kernel module
1500 * /sys/devices/platform/sony-laptop */
1501 END OBJ(sony_fanspeed, 0)
1502 END OBJ_IF(if_gw, INFO_GW)
1503 END OBJ(ioscheduler, 0)
1505 CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1508 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1509 END OBJ(laptop_mode, 0)
1510 END OBJ(pb_battery, 0)
1511 if (arg && strcmp(arg, "status") == EQUAL) {
1512 obj->data.i = PB_BATT_STATUS;
1513 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1514 obj->data.i = PB_BATT_PERCENT;
1515 } else if (arg && strcmp(arg, "time") == EQUAL) {
1516 obj->data.i = PB_BATT_TIME;
1518 ERR("pb_battery: needs one argument: status, percent or time");
1523 #endif /* __linux__ */
1524 #if (defined(__FreeBSD__) || defined(__linux__))
1525 END OBJ_IF(if_up, 0)
1527 ERR("if_up needs an argument");
1528 obj->data.ifblock.s = 0;
1530 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1533 #if defined(__OpenBSD__)
1534 END OBJ(obsd_sensors_temp, 0)
1536 CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1538 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1539 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1540 obj->data.sensor = 0;
1541 ERR("Invalid temperature sensor number!");
1543 obj->data.sensor = atoi(&arg[0]);
1544 END OBJ(obsd_sensors_fan, 0)
1546 CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1549 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1550 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1551 obj->data.sensor = 0;
1552 ERR("Invalid fan sensor number!");
1554 obj->data.sensor = atoi(&arg[0]);
1555 END OBJ(obsd_sensors_volt, 0)
1557 CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1560 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1561 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1562 obj->data.sensor = 0;
1563 ERR("Invalid voltage sensor number!");
1565 obj->data.sensor = atoi(&arg[0]);
1566 END OBJ(obsd_vendor, 0)
1567 END OBJ(obsd_product, 0)
1568 #endif /* __OpenBSD__ */
1569 END OBJ(buffers, INFO_BUFFERS)
1570 END OBJ(cached, INFO_BUFFERS)
1571 #define SCAN_CPU(__arg, __var) { \
1573 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1574 __arg += __offset; \
1578 END OBJ(cpu, INFO_CPU)
1579 SCAN_CPU(arg, obj->data.cpu_index);
1580 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1582 END OBJ(cpugauge, INFO_CPU)
1583 SIZE_DEFAULTS(gauge);
1584 SCAN_CPU(arg, obj->data.cpu_index);
1585 scan_gauge(arg, &obj->a, &obj->b);
1586 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1588 END OBJ(cpubar, INFO_CPU)
1590 SCAN_CPU(arg, obj->data.cpu_index);
1591 scan_bar(arg, &obj->a, &obj->b);
1592 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1594 END OBJ(cpugraph, INFO_CPU)
1596 SIZE_DEFAULTS(graph);
1597 SCAN_CPU(arg, obj->data.cpu_index);
1598 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1599 &obj->e, &obj->char_a, &obj->char_b);
1600 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1602 END OBJ(loadgraph, INFO_LOADAVG)
1604 SIZE_DEFAULTS(graph);
1605 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1606 &obj->e, &obj->char_a, &obj->char_b);
1610 r = sscanf(arg, "%d", &a);
1612 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1616 END OBJ(diskio, INFO_DISKIO)
1617 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1618 END OBJ(diskio_read, INFO_DISKIO)
1619 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1620 END OBJ(diskio_write, INFO_DISKIO)
1621 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1623 END OBJ(diskiograph, INFO_DISKIO)
1625 SIZE_DEFAULTS(graph);
1626 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1627 &obj->e, &obj->char_a, &obj->char_b);
1629 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1631 END OBJ(diskiograph_read, INFO_DISKIO)
1633 SIZE_DEFAULTS(graph);
1634 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1635 &obj->e, &obj->char_a, &obj->char_b);
1637 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1639 END OBJ(diskiograph_write, INFO_DISKIO)
1641 SIZE_DEFAULTS(graph);
1642 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1643 &obj->e, &obj->char_a, &obj->char_b);
1645 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1650 if (output_methods & TO_X) {
1651 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1655 obj->data.l = color0;
1657 obj->data.l = color1;
1659 obj->data.l = color2;
1661 obj->data.l = color3;
1663 obj->data.l = color4;
1665 obj->data.l = color5;
1667 obj->data.l = color6;
1669 obj->data.l = color7;
1671 obj->data.l = color8;
1673 obj->data.l = color9;
1676 obj->data.s = scan_font(arg);
1678 END OBJ(conky_version, 0)
1679 END OBJ(conky_build_date, 0)
1680 END OBJ(conky_build_arch, 0)
1681 END OBJ(downspeed, INFO_NET)
1683 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1685 // default to DEFAULTNETDEV
1686 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1687 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1690 END OBJ(downspeedf, INFO_NET)
1692 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1694 // default to DEFAULTNETDEV
1695 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1696 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1700 END OBJ(downspeedgraph, INFO_NET)
1702 SIZE_DEFAULTS(graph);
1703 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1704 &obj->e, &obj->char_a, &obj->char_b);
1706 // default to DEFAULTNETDEV
1707 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1708 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1712 obj_be_ifblock_else(ifblock_opaque, obj);
1714 obj_be_ifblock_endif(ifblock_opaque, obj);
1716 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1718 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1721 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1723 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1726 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1728 END OBJ(execgauge, 0)
1729 SIZE_DEFAULTS(gauge);
1730 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1731 END OBJ(execgraph, 0)
1732 SIZE_DEFAULTS(graph);
1733 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1735 END OBJ(execibar, 0)
1739 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1742 ERR("${execibar <interval> command}");
1743 obj->type = OBJ_text;
1744 snprintf(buf, 256, "${%s}", s);
1745 obj->data.s = strndup(buf, text_buffer_size);
1747 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1750 END OBJ(execigraph, 0)
1752 SIZE_DEFAULTS(graph);
1754 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1757 ERR("${execigraph <interval> command}");
1758 obj->type = OBJ_text;
1759 snprintf(buf, 256, "${%s}", s);
1760 obj->data.s = strndup(buf, text_buffer_size);
1762 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1764 END OBJ(execigauge, 0)
1766 SIZE_DEFAULTS(gauge);
1768 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1771 ERR("${execigauge <interval> command}");
1772 obj->type = OBJ_text;
1773 snprintf(buf, 256, "${%s}", s);
1774 obj->data.s = strndup(buf, text_buffer_size);
1776 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1782 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1785 ERR("${execi <interval> command}");
1786 obj->type = OBJ_text;
1787 snprintf(buf, 256, "${%s}", s);
1788 obj->data.s = strndup(buf, text_buffer_size);
1790 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1791 obj->data.execi.buffer = malloc(text_buffer_size);
1796 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1799 ERR("${execi <interval> command}");
1800 obj->type = OBJ_text;
1801 snprintf(buf, 256, "${%s}", s);
1802 obj->data.s = strndup(buf, text_buffer_size);
1804 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1805 obj->data.execi.buffer = malloc(text_buffer_size);
1807 END OBJ_THREAD(texeci, 0)
1810 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1813 ERR("${texeci <interval> command}");
1814 obj->type = OBJ_text;
1815 snprintf(buf, 256, "${%s}", s);
1816 obj->data.s = strndup(buf, text_buffer_size);
1818 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1819 obj->data.texeci.buffer = malloc(text_buffer_size);
1821 obj->data.texeci.p_timed_thread = NULL;
1822 END OBJ(pre_exec, 0)
1823 obj->type = OBJ_text;
1827 read_exec(arg, buf, sizeof(buf));
1828 obj->data.s = strndup(buf, text_buffer_size);
1830 obj->data.s = strndup("", text_buffer_size);
1833 END OBJ(fs_bar, INFO_FS)
1835 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1837 while (isspace(*arg)) {
1846 obj->data.fsbar.fs = prepare_fs_stat(arg);
1847 END OBJ(fs_bar_free, INFO_FS)
1849 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1851 while (isspace(*arg)) {
1861 obj->data.fsbar.fs = prepare_fs_stat(arg);
1862 END OBJ(fs_free, INFO_FS)
1866 obj->data.fs = prepare_fs_stat(arg);
1867 END OBJ(fs_used_perc, INFO_FS)
1871 obj->data.fs = prepare_fs_stat(arg);
1872 END OBJ(fs_free_perc, INFO_FS)
1876 obj->data.fs = prepare_fs_stat(arg);
1877 END OBJ(fs_size, INFO_FS)
1881 obj->data.fs = prepare_fs_stat(arg);
1882 END OBJ(fs_type, INFO_FS)
1886 obj->data.fs = prepare_fs_stat(arg);
1887 END OBJ(fs_used, INFO_FS)
1891 obj->data.fs = prepare_fs_stat(arg);
1893 obj->data.i = arg ? atoi(arg) : 1;
1894 END OBJ(nameserver, INFO_DNS)
1895 obj->data.i = arg ? atoi(arg) : 0;
1897 obj->data.i = arg ? atoi(arg) : 1;
1899 obj->data.i = arg ? atoi(arg) : 1;
1903 ERR("goto needs arguments");
1904 obj->type = OBJ_text;
1905 obj->data.s = strndup("${goto}", text_buffer_size);
1909 obj->data.i = atoi(arg);
1915 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1916 sscanf(arg, "%d", &b);
1922 obj->data.pair.a = a;
1923 obj->data.pair.b = b;
1926 END OBJ(i2c, INFO_SYSFS)
1927 char buf1[64], buf2[64];
1928 float factor, offset;
1932 ERR("i2c needs arguments");
1933 obj->type = OBJ_text;
1934 // obj->data.s = strndup("${i2c}", text_buffer_size);
1938 #define HWMON_RESET() {\
1943 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1944 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1945 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1946 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1949 ERR("i2c failed to parse arguments");
1950 obj->type = OBJ_text;
1953 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1954 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
1955 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1956 strncpy(obj->data.sysfs.type, buf2, 63);
1957 obj->data.sysfs.factor = factor;
1958 obj->data.sysfs.offset = offset;
1960 END OBJ(platform, INFO_SYSFS)
1961 char buf1[64], buf2[64];
1962 float factor, offset;
1966 ERR("platform needs arguments");
1967 obj->type = OBJ_text;
1971 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1972 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1973 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1974 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1977 ERR("platform failed to parse arguments");
1978 obj->type = OBJ_text;
1981 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1982 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
1983 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1984 strncpy(obj->data.sysfs.type, buf2, 63);
1985 obj->data.sysfs.factor = factor;
1986 obj->data.sysfs.offset = offset;
1988 END OBJ(hwmon, INFO_SYSFS)
1989 char buf1[64], buf2[64];
1990 float factor, offset;
1994 ERR("hwmon needs argumanets");
1995 obj->type = OBJ_text;
1999 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2000 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2001 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2002 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2007 ERR("hwmon failed to parse arguments");
2008 obj->type = OBJ_text;
2011 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2012 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2013 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2014 strncpy(obj->data.sysfs.type, buf2, 63);
2015 obj->data.sysfs.factor = factor;
2016 obj->data.sysfs.offset = offset;
2018 #endif /* !__OpenBSD__ */
2021 /* we have four different types of top (top, top_mem, top_time and top_io). To
2022 * avoid having almost-same code four times, we have this special
2024 if (strncmp(s, "top", 3) == EQUAL) {
2025 if (!parse_top_args(s, arg, obj)) {
2028 } else OBJ(addr, INFO_NET)
2030 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2032 // default to DEFAULTNETDEV
2033 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2034 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2037 #if defined(__linux__)
2038 END OBJ(addrs, INFO_NET)
2040 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2042 // default to DEFAULTNETDEV
2043 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2044 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2047 #endif /* __linux__ */
2049 if (init_tail_object(obj, arg)) {
2050 obj->type = OBJ_text;
2051 obj->data.s = strndup("${tail}", text_buffer_size);
2054 if (init_head_object(obj, arg)) {
2055 obj->type = OBJ_text;
2056 obj->data.s = strndup("${head}", text_buffer_size);
2060 obj->data.s = strndup(arg, text_buffer_size);
2062 CRIT_ERR(obj, free_at_crash, "lines needs a argument");
2066 obj->data.s = strndup(arg, text_buffer_size);
2068 CRIT_ERR(obj, free_at_crash, "words needs a argument");
2070 END OBJ(loadavg, INFO_LOADAVG)
2071 int a = 1, b = 2, c = 3, r = 3;
2074 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2075 if (r >= 3 && (c < 1 || c > 3)) {
2078 if (r >= 2 && (b < 1 || b > 3)) {
2081 if (r >= 1 && (a < 1 || a > 3)) {
2085 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2086 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2087 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2088 END OBJ_IF(if_empty, 0)
2090 ERR("if_empty needs an argument");
2091 obj->data.ifblock.s = 0;
2093 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2094 obj->sub = malloc(sizeof(struct text_object));
2095 extract_variable_text_internal(obj->sub,
2096 obj->data.ifblock.s, 0);
2098 END OBJ_IF(if_match, 0)
2100 ERR("if_match needs arguments");
2101 obj->data.ifblock.s = 0;
2103 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2104 obj->sub = malloc(sizeof(struct text_object));
2105 extract_variable_text_internal(obj->sub,
2106 obj->data.ifblock.s, 0);
2108 END OBJ_IF(if_existing, 0)
2110 ERR("if_existing needs an argument or two");
2111 obj->data.ifblock.s = NULL;
2112 obj->data.ifblock.str = NULL;
2114 char buf1[256], buf2[256];
2115 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2118 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2119 obj->data.ifblock.str = NULL;
2121 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2122 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2125 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2126 END OBJ_IF(if_mounted, 0)
2128 ERR("if_mounted needs an argument");
2129 obj->data.ifblock.s = 0;
2131 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2134 END OBJ_IF(if_running, INFO_TOP)
2136 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2138 END OBJ_IF(if_running, 0)
2142 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2143 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2146 ERR("if_running needs an argument");
2147 obj->data.ifblock.s = 0;
2153 char box[256], dst[256];
2157 /* Kapil: Changed from MAIL_FILE to
2158 current_mail_spool since the latter
2159 is a copy of the former if undefined
2160 but the latter should take precedence
2162 strncpy(box, current_mail_spool, sizeof(box));
2164 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2166 strncpy(box, arg, sizeof(box));
2170 variable_substitute(box, dst, sizeof(dst));
2171 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2172 obj->data.local_mail.interval = n1;
2173 END OBJ(new_mails, 0)
2175 char box[256], dst[256];
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(seen_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(unseen_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(flagged_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(unflagged_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(forwarded_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(unforwarded_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(replied_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(unreplied_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(draft_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(trashed_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(mboxscan, 0)
2361 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2362 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2363 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2364 obj->data.mboxscan.output[0] = 1;
2365 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2366 END OBJ(mem, INFO_MEM)
2367 END OBJ(memeasyfree, INFO_MEM)
2368 END OBJ(memfree, INFO_MEM)
2369 END OBJ(memmax, INFO_MEM)
2370 END OBJ(memperc, INFO_MEM)
2372 END OBJ(memgauge, INFO_MEM)
2373 SIZE_DEFAULTS(gauge);
2374 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2376 END OBJ(membar, INFO_MEM)
2378 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2380 END OBJ(memgraph, INFO_MEM)
2382 SIZE_DEFAULTS(graph);
2383 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2384 &obj->e, &obj->char_a, &obj->char_b);
2388 END OBJ(mixer, INFO_MIXER)
2389 obj->data.l = mixer_init(arg);
2390 END OBJ(mixerl, INFO_MIXER)
2391 obj->data.l = mixer_init(arg);
2392 END OBJ(mixerr, INFO_MIXER)
2393 obj->data.l = mixer_init(arg);
2395 END OBJ(mixerbar, INFO_MIXER)
2397 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2398 &obj->data.mixerbar.h);
2399 END OBJ(mixerlbar, INFO_MIXER)
2401 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2402 &obj->data.mixerbar.h);
2403 END OBJ(mixerrbar, INFO_MIXER)
2405 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2406 &obj->data.mixerbar.h);
2408 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2409 obj->data.ifblock.i = mixer_init(arg);
2411 END OBJ(monitor, INFO_X11)
2412 END OBJ(monitor_number, INFO_X11)
2413 END OBJ(desktop, INFO_X11)
2414 END OBJ(desktop_number, INFO_X11)
2415 END OBJ(desktop_name, INFO_X11)
2417 END OBJ(nodename, 0)
2418 END OBJ(processes, INFO_PROCS)
2419 END OBJ(running_processes, INFO_RUN_PROCS)
2420 END OBJ(shadecolor, 0)
2422 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2424 END OBJ(outlinecolor, 0)
2426 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2428 END OBJ(stippled_hr, 0)
2430 int a = stippled_borders, b = 1;
2433 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2434 sscanf(arg, "%d", &b);
2440 obj->data.pair.a = a;
2441 obj->data.pair.b = b;
2443 END OBJ(swap, INFO_MEM)
2444 END OBJ(swapfree, INFO_MEM)
2445 END OBJ(swapmax, INFO_MEM)
2446 END OBJ(swapperc, INFO_MEM)
2447 END OBJ(swapbar, INFO_MEM)
2449 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2452 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2454 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2456 char buf1[256], buf2[256], *fmt, *tz;
2460 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2470 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2471 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2473 END OBJ(iconv_start, 0)
2474 if (iconv_converting) {
2475 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
2476 "starting another");
2479 char iconv_from[CODEPAGE_LENGTH];
2480 char iconv_to[CODEPAGE_LENGTH];
2482 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2483 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
2487 new_iconv = iconv_open(iconv_to, iconv_from);
2488 if (new_iconv == (iconv_t) (-1)) {
2489 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2491 obj->a = register_iconv(&new_iconv);
2492 iconv_converting = 1;
2496 CRIT_ERR(obj, free_at_crash, "Iconv requires arguments");
2498 END OBJ(iconv_stop, 0)
2499 iconv_converting = 0;
2502 END OBJ(totaldown, INFO_NET)
2504 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2506 // default to DEFAULTNETDEV
2507 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2508 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2511 END OBJ(totalup, INFO_NET)
2512 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2514 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2516 // default to DEFAULTNETDEV
2517 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2518 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2522 END OBJ_IF(if_updatenr, 0)
2523 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2524 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
2525 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2527 obj->data.i = arg ? atoi(arg) : 0;
2529 obj->data.i = arg ? atoi(arg) : 0;
2530 END OBJ(upspeed, INFO_NET)
2532 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2534 // default to DEFAULTNETDEV
2535 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2536 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2539 END OBJ(upspeedf, INFO_NET)
2541 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2543 // default to DEFAULTNETDEV
2544 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2545 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2550 END OBJ(upspeedgraph, INFO_NET)
2552 SIZE_DEFAULTS(graph);
2553 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2554 &obj->e, &obj->char_a, &obj->char_b);
2556 // default to DEFAULTNETDEV
2557 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2558 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2561 END OBJ(uptime_short, INFO_UPTIME)
2562 END OBJ(uptime, INFO_UPTIME)
2563 END OBJ(user_names, INFO_USERS)
2564 END OBJ(user_times, INFO_USERS)
2565 END OBJ(user_terms, INFO_USERS)
2566 END OBJ(user_number, INFO_USERS)
2567 #if defined(__linux__)
2568 END OBJ(gw_iface, INFO_GW)
2569 END OBJ(gw_ip, INFO_GW)
2570 #endif /* !__linux__ */
2572 END OBJ(adt746xcpu, 0)
2573 END OBJ(adt746xfan, 0)
2574 #endif /* !__OpenBSD__ */
2575 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2576 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2577 END OBJ(apm_adapter, 0)
2578 END OBJ(apm_battery_life, 0)
2579 END OBJ(apm_battery_time, 0)
2580 #endif /* __FreeBSD__ */
2581 END OBJ_THREAD(imap_unseen, 0)
2584 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2589 END OBJ_THREAD(imap_messages, 0)
2592 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2597 END OBJ_THREAD(pop3_unseen, 0)
2600 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2605 END OBJ_THREAD(pop3_used, 0)
2608 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2616 obj->data.s = strndup(arg, text_buffer_size);
2618 ERR("smapi needs an argument");
2619 END OBJ_IF(if_smapi_bat_installed, 0)
2621 ERR("if_smapi_bat_installed needs an argument");
2622 obj->data.ifblock.s = 0;
2624 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2625 END OBJ(smapi_bat_perc, 0)
2627 obj->data.s = strndup(arg, text_buffer_size);
2629 ERR("smapi_bat_perc needs an argument");
2630 END OBJ(smapi_bat_temp, 0)
2632 obj->data.s = strndup(arg, text_buffer_size);
2634 ERR("smapi_bat_temp needs an argument");
2635 END OBJ(smapi_bat_power, 0)
2637 obj->data.s = strndup(arg, text_buffer_size);
2639 ERR("smapi_bat_power needs an argument");
2641 END OBJ(smapi_bat_bar, 0)
2645 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2646 ERR("first argument to smapi_bat_bar must be an integer value");
2650 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2653 ERR("smapi_bat_bar needs an argument");
2657 #define mpd_set_maxlen(name) \
2660 sscanf(arg, "%d", &i); \
2662 obj->data.i = i + 1; \
2664 ERR(#name ": invalid length argument"); \
2666 END OBJ(mpd_artist, INFO_MPD)
2667 mpd_set_maxlen(mpd_artist);
2669 END OBJ(mpd_title, INFO_MPD)
2670 mpd_set_maxlen(mpd_title);
2672 END OBJ(mpd_random, INFO_MPD) init_mpd();
2673 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2674 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2675 END OBJ(mpd_length, INFO_MPD) init_mpd();
2676 END OBJ(mpd_track, INFO_MPD)
2677 mpd_set_maxlen(mpd_track);
2679 END OBJ(mpd_name, INFO_MPD)
2680 mpd_set_maxlen(mpd_name);
2682 END OBJ(mpd_file, INFO_MPD)
2683 mpd_set_maxlen(mpd_file);
2685 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2686 END OBJ(mpd_album, INFO_MPD)
2687 mpd_set_maxlen(mpd_album);
2689 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2690 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2691 END OBJ(mpd_status, INFO_MPD) init_mpd();
2692 END OBJ(mpd_bar, INFO_MPD)
2694 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2696 END OBJ(mpd_smart, INFO_MPD)
2697 mpd_set_maxlen(mpd_smart);
2699 END OBJ_IF(if_mpd_playing, INFO_MPD)
2701 #undef mpd_set_maxlen
2704 END OBJ(moc_state, INFO_MOC)
2705 END OBJ(moc_file, INFO_MOC)
2706 END OBJ(moc_title, INFO_MOC)
2707 END OBJ(moc_artist, INFO_MOC)
2708 END OBJ(moc_song, INFO_MOC)
2709 END OBJ(moc_album, INFO_MOC)
2710 END OBJ(moc_totaltime, INFO_MOC)
2711 END OBJ(moc_timeleft, INFO_MOC)
2712 END OBJ(moc_curtime, INFO_MOC)
2713 END OBJ(moc_bitrate, INFO_MOC)
2714 END OBJ(moc_rate, INFO_MOC)
2717 END OBJ(xmms2_artist, INFO_XMMS2)
2718 END OBJ(xmms2_album, INFO_XMMS2)
2719 END OBJ(xmms2_title, INFO_XMMS2)
2720 END OBJ(xmms2_genre, INFO_XMMS2)
2721 END OBJ(xmms2_comment, INFO_XMMS2)
2722 END OBJ(xmms2_url, INFO_XMMS2)
2723 END OBJ(xmms2_tracknr, INFO_XMMS2)
2724 END OBJ(xmms2_bitrate, INFO_XMMS2)
2725 END OBJ(xmms2_date, INFO_XMMS2)
2726 END OBJ(xmms2_id, INFO_XMMS2)
2727 END OBJ(xmms2_duration, INFO_XMMS2)
2728 END OBJ(xmms2_elapsed, INFO_XMMS2)
2729 END OBJ(xmms2_size, INFO_XMMS2)
2730 END OBJ(xmms2_status, INFO_XMMS2)
2731 END OBJ(xmms2_percent, INFO_XMMS2)
2733 END OBJ(xmms2_bar, INFO_XMMS2)
2735 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2737 END OBJ(xmms2_smart, INFO_XMMS2)
2738 END OBJ(xmms2_playlist, INFO_XMMS2)
2739 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2740 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2743 END OBJ(audacious_status, INFO_AUDACIOUS)
2744 END OBJ(audacious_title, INFO_AUDACIOUS)
2746 sscanf(arg, "%d", &info.audacious.max_title_len);
2747 if (info.audacious.max_title_len > 0) {
2748 info.audacious.max_title_len++;
2750 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
2753 END OBJ(audacious_length, INFO_AUDACIOUS)
2754 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2755 END OBJ(audacious_position, INFO_AUDACIOUS)
2756 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2757 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2758 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2759 END OBJ(audacious_channels, INFO_AUDACIOUS)
2760 END OBJ(audacious_filename, INFO_AUDACIOUS)
2761 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2762 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2763 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2765 END OBJ(audacious_bar, INFO_AUDACIOUS)
2767 scan_bar(arg, &obj->a, &obj->b);
2771 END OBJ(bmpx_title, INFO_BMPX)
2772 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2773 END OBJ(bmpx_artist, INFO_BMPX)
2774 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2775 END OBJ(bmpx_album, INFO_BMPX)
2776 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2777 END OBJ(bmpx_track, INFO_BMPX)
2778 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2779 END OBJ(bmpx_uri, INFO_BMPX)
2780 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2781 END OBJ(bmpx_bitrate, INFO_BMPX)
2782 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2788 char *userid = (char *) malloc(20 * sizeof(char));
2789 char *apikey = (char *) malloc(64 * sizeof(char));
2790 char *charid = (char *) malloc(20 * sizeof(char));
2792 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2793 obj->data.eve.charid = charid;
2794 obj->data.eve.userid = userid;
2795 obj->data.eve.apikey = apikey;
2799 CRIT_ERR(obj, free_at_crash, "eve needs arguments: <userid> <apikey> <characterid>");
2805 int argc, delay, act_par;
2806 unsigned int nrspaces = 0;
2807 char *uri = (char *) malloc(128 * sizeof(char));
2808 char *action = (char *) malloc(64 * sizeof(char));
2810 argc = sscanf(arg, "%127s %d %63s %d %u", uri, &delay, action,
2811 &act_par, &nrspaces);
2812 obj->data.rss.uri = uri;
2813 obj->data.rss.delay = delay;
2814 obj->data.rss.action = action;
2815 obj->data.rss.act_par = act_par;
2816 obj->data.rss.nrspaces = nrspaces;
2820 CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <delay in minutes> <action> "
2821 "[act_par] [spaces in front]");
2825 END OBJ_THREAD(weather, 0)
2828 char *locID = (char *) malloc(9 * sizeof(char));
2829 char *uri = (char *) malloc(128 * sizeof(char));
2830 char *data_type = (char *) malloc(32 * sizeof(char));
2833 argc = sscanf(arg, "%119s %8s %31s %d", uri, locID, data_type, &interval);
2835 //locID MUST BE upper-case
2838 *tmp_p = toupper(*tmp_p);
2842 //Construct complete uri
2843 if (strstr(uri, "xoap.weather.com")) {
2851 } else if (strstr(uri, "weather.noaa.gov")) {
2853 strcat(uri, ".TXT");
2854 } else if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
2855 CRIT_ERR(obj, free_at_crash, \
2856 "could not recognize the weather uri");
2859 obj->data.weather.uri = uri;
2860 obj->data.weather.data_type = data_type;
2862 //Limit the data retrieval interval to half hour min
2863 if (interval < 30) {
2867 //Convert to seconds
2868 obj->data.weather.interval = interval * 60;
2871 DBGP("weather: fetching %s from %s every %d seconds", \
2872 data_type, uri, obj->data.weather.interval);
2874 CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
2880 obj->data.s = strndup(arg, text_buffer_size);
2882 CRIT_ERR(obj, free_at_crash, "lua needs arguments: <function name> [function parameters]");
2884 END OBJ(lua_parse, 0)
2886 obj->data.s = strndup(arg, text_buffer_size);
2888 CRIT_ERR(obj, free_at_crash, "lua_parse needs arguments: <function name> [function parameters]");
2893 arg = scan_bar(arg, &obj->a, &obj->b);
2895 obj->data.s = strndup(arg, text_buffer_size);
2897 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2900 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2903 END OBJ(lua_graph, 0)
2904 SIZE_DEFAULTS(graph);
2907 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2908 &obj->e, &obj->char_a, &obj->char_b);
2912 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2915 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2917 END OBJ(lua_gauge, 0)
2918 SIZE_DEFAULTS(gauge);
2920 arg = scan_gauge(arg, &obj->a, &obj->b);
2922 obj->data.s = strndup(arg, text_buffer_size);
2924 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2927 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2930 #endif /* HAVE_LUA */
2933 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2934 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2935 ERR("hddtemp needs arguments");
2936 obj->type = OBJ_text;
2937 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2938 obj->data.hddtemp.update_time = 0;
2940 obj->data.hddtemp.temp = NULL;
2941 #endif /* HDDTEMP */
2942 #ifdef TCP_PORT_MONITOR
2943 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2944 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2945 #endif /* TCP_PORT_MONITOR */
2946 END OBJ(entropy_avail, INFO_ENTROPY)
2947 END OBJ(entropy_perc, INFO_ENTROPY)
2948 END OBJ(entropy_poolsize, INFO_ENTROPY)
2949 END OBJ(entropy_bar, INFO_ENTROPY)
2951 scan_bar(arg, &obj->a, &obj->b);
2954 obj->sub = malloc(sizeof(struct text_object));
2955 extract_variable_text_internal(obj->sub, arg, 0);
2957 CRIT_ERR(obj, free_at_crash, "blink needs a argument");
2959 END OBJ(to_bytes, 0)
2961 obj->sub = malloc(sizeof(struct text_object));
2962 extract_variable_text_internal(obj->sub, arg, 0);
2964 CRIT_ERR(obj, free_at_crash, "to_bytes needs a argument");
2969 obj->data.scroll.step = 1;
2970 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2971 sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
2972 if(*(arg + n1 + n2)) {
2975 obj->data.scroll.step = 1;
2977 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2978 obj->data.scroll.start = 0;
2979 obj->sub = malloc(sizeof(struct text_object));
2980 extract_variable_text_internal(obj->sub,
2981 obj->data.scroll.text, 0);
2983 CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
2988 unsigned int indenting = 0; //vars can be used as args for other vars
2991 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
2993 for (i=0; arg[i] != 0 && j < 2; i++) {
2994 if(startvar[j] == -1) {
2998 }else if(endvar[j] == -1) {
3001 }else if(arg[i] == '}') {
3004 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
3010 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
3011 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
3012 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
3013 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
3015 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
3016 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
3018 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
3019 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
3021 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
3022 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
3024 obj->sub = malloc(sizeof(struct text_object));
3025 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
3026 obj->sub->sub = malloc(sizeof(struct text_object));
3027 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
3029 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3032 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3037 CRIT_ERR(obj, free_at_crash, "nvidia needs an argument\n");
3038 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3039 CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
3040 " specified: '%s'\n", arg);
3045 END OBJ(apcupsd, INFO_APCUPSD)
3049 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3050 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3052 info.apcupsd.port = htons(port);
3053 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3056 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3058 END OBJ(apcupsd_name, INFO_APCUPSD)
3059 END OBJ(apcupsd_model, INFO_APCUPSD)
3060 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3061 END OBJ(apcupsd_cable, INFO_APCUPSD)
3062 END OBJ(apcupsd_status, INFO_APCUPSD)
3063 END OBJ(apcupsd_linev, INFO_APCUPSD)
3064 END OBJ(apcupsd_load, INFO_APCUPSD)
3065 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3067 scan_bar(arg, &obj->a, &obj->b);
3069 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3071 SIZE_DEFAULTS(graph);
3072 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3073 &obj->e, &obj->char_a, &obj->char_b);
3075 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3076 SIZE_DEFAULTS(gauge);
3077 scan_gauge(arg, &obj->a, &obj->b);
3079 END OBJ(apcupsd_charge, INFO_APCUPSD)
3080 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3081 END OBJ(apcupsd_temp, INFO_APCUPSD)
3082 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3083 #endif /* APCUPSD */
3087 ERR("unknown variable %s", s);
3088 obj->type = OBJ_text;
3089 snprintf(buf, 256, "${%s}", s);
3090 obj->data.s = strndup(buf, text_buffer_size);
3097 static struct text_object *create_plain_text(const char *s)
3099 struct text_object *obj;
3101 if (s == NULL || *s == '\0') {
3105 obj = new_text_object_internal();
3107 obj->type = OBJ_text;
3108 obj->data.s = strndup(s, text_buffer_size);
3112 /* backslash_escape - do the actual substitution task for template objects
3114 * The field templates is used for substituting the \N occurences. Set it to
3115 * NULL to leave them as they are.
3117 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3121 unsigned int dup_idx = 0, dup_len;
3123 dup_len = strlen(src) + 1;
3124 src_dup = malloc(dup_len * sizeof(char));
3132 if (*(p + 1) == '\\') {
3133 src_dup[dup_idx++] = '\\';
3135 } else if (*(p + 1) == ' ') {
3136 src_dup[dup_idx++] = ' ';
3138 } else if (*(p + 1) == 'n') {
3139 src_dup[dup_idx++] = '\n';
3141 } else if (templates) {
3142 unsigned int tmpl_num;
3144 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3145 (tmpl_num > template_count))
3147 dup_len += strlen(templates[tmpl_num - 1]);
3148 src_dup = realloc(src_dup, dup_len * sizeof(char));
3149 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3150 dup_idx += strlen(templates[tmpl_num - 1]);
3155 src_dup[dup_idx++] = *p;
3160 src_dup[dup_idx] = '\0';
3161 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3165 /* handle_template_object - core logic of the template object
3167 * use config variables like this:
3168 * template1 = "$\1\2"
3169 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3171 * and use them like this:
3172 * ${template1 node name}
3173 * ${template2 root /}
3174 * ${template2 cdrom /mnt/cdrom}
3176 static char *handle_template(const char *tmpl, const char *args)
3178 char *args_dup = NULL;
3180 char **argsp = NULL;
3181 unsigned int argcnt = 0, template_idx, i;
3184 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3185 (template_idx >= MAX_TEMPLATES))
3189 args_dup = strdup(args);
3192 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3194 if (p > args_dup && *(p - 1) == '\\')
3197 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3203 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3204 argsp[argcnt - 1] = p_old;
3206 for (i = 0; i < argcnt; i++) {
3208 tmp = backslash_escape(argsp[i], NULL, 0);
3209 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3214 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3215 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3217 for (i = 0; i < argcnt; i++)
3223 static char *find_and_replace_templates(const char *inbuf)
3225 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3228 outlen = strlen(inbuf) + 1;
3229 o = outbuf = calloc(outlen, sizeof(char));
3230 memset(outbuf, 0, outlen * sizeof(char));
3232 p = indup = strdup(inbuf);
3234 while (*p && *p != '$')
3240 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3245 if (*(p + 1) == '{') {
3248 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3256 while (*p && stack > 0) {
3264 // stack is empty. that means the previous char was }, so we zero it
3267 // we ran into the end of string without finding a closing }, bark
3268 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
3272 while (*p && !isspace(*p))
3276 tmpl_out = handle_template(templ, args);
3278 outlen += strlen(tmpl_out);
3280 outbuf = realloc(outbuf, outlen * sizeof(char));
3281 strcat (outbuf, tmpl_out);
3283 o = outbuf + strlen(outbuf);
3285 ERR("failed to handle template '%s' with args '%s'", templ, args);
3289 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3294 static int text_contains_templates(const char *text)
3296 if (strcasestr(text, "${template") != NULL)
3298 if (strcasestr(text, "$template") != NULL)
3303 /* folds a string over top of itself, like so:
3305 * if start is "blah", and you call it with count = 1, the result will be "lah"
3307 static void strfold(char *start, int count)
3310 for (curplace = start + count; *curplace != 0; curplace++) {
3311 *(curplace - count) = *curplace;
3313 *(curplace - count) = 0;
3317 * - assumes that *string is '#'
3318 * - removes the part from '#' to the end of line ('\n' or '\0')
3319 * - it removes the '\n'
3320 * - copies the last char into 'char *last' argument, which should be a pointer
3321 * to a char rather than a string.
3323 static size_t remove_comment(char *string, char *last)
3326 while (*end != '\0' && *end != '\n') {
3329 if (last) *last = *end;
3330 if (*end == '\n') end++;
3331 strfold(string, end - string);
3332 return end - string;
3335 static size_t remove_comments(char *string)
3339 for (curplace = string; *curplace != 0; curplace++) {
3340 if (*curplace == '\\' && *(curplace + 1) == '#') {
3341 // strcpy can't be used for overlapping strings
3342 strfold(curplace, 1);
3344 } else if (*curplace == '#') {
3345 folded += remove_comment(curplace, 0);
3351 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3353 struct text_object *obj;
3354 char *p, *s, *orig_p;
3356 void *ifblock_opaque = NULL;
3361 p = strndup(const_p, max_user_text - 1);
3362 while (text_contains_templates(p)) {
3364 tmp = find_and_replace_templates(p);
3370 if (strcmp(p, const_p)) {
3371 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3373 DBGP("no templates to replace");
3376 memset(retval, 0, sizeof(struct text_object));
3378 line = global_text_lines;
3386 obj = create_plain_text(s);
3388 append_object(retval, obj);
3398 /* variable is either $foo or ${foo} */
3400 unsigned int brl = 1, brr = 0;
3404 while (*p && brl != brr) {
3419 while (*p && (isalnum((int) *p) || *p == '_')) {
3424 /* copy variable to buffer */
3425 len = (p - s > 255) ? 255 : (p - s);
3426 strncpy(buf, s, len);
3434 /* search for variable in environment */
3438 obj = create_plain_text(var);
3440 append_object(retval, obj);
3445 /* if variable wasn't found in environment, use some special */
3450 if (strchr(buf, ' ')) {
3451 arg = strchr(buf, ' ');
3454 while (isspace((int) *arg)) {
3462 /* lowercase variable name */
3465 *tmp_p = tolower(*tmp_p);
3469 obj = construct_text_object(buf, arg,
3470 line, allow_threaded,
3471 &ifblock_opaque, orig_p);
3473 append_object(retval, obj);
3477 obj = create_plain_text("$");
3480 append_object(retval, obj);
3483 } else if (*p == '\\' && *(p+1) == '#') {
3485 } else if (*p == '#') {
3487 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3488 /* if remove_comment removed a newline, we need to 'back up' with p */
3494 obj = create_plain_text(s);
3496 append_object(retval, obj);
3499 if (!ifblock_stack_empty(&ifblock_opaque)) {
3500 ERR("one or more $endif's are missing");
3507 static void extract_variable_text(const char *p)
3509 free_text_objects(&global_root_object, 0);
3523 extract_variable_text_internal(&global_root_object, p, 1);
3526 void parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3528 extract_variable_text_internal(root, txt, 0);
3529 generate_text_internal(p, max_user_text, *root, cur);
3533 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3534 void *thread(void *), const char *text)
3536 if (obj->char_b && info.mail) {
3537 // this means we use info
3538 if (!info.mail->p_timed_thread) {
3539 info.mail->p_timed_thread =
3540 timed_thread_create(thread,
3541 (void *) info.mail, info.mail->interval * 1000000);
3542 if (!info.mail->p_timed_thread) {
3543 ERR("Error creating %s timed thread", text);
3545 timed_thread_register(info.mail->p_timed_thread,
3546 &info.mail->p_timed_thread);
3547 if (timed_thread_run(info.mail->p_timed_thread)) {
3548 ERR("Error running %s timed thread", text);
3552 } else if (obj->data.mail) {
3553 // this means we use obj
3554 if (!obj->data.mail->p_timed_thread) {
3555 obj->data.mail->p_timed_thread =
3556 timed_thread_create(thread,
3557 (void *) obj->data.mail,
3558 obj->data.mail->interval * 1000000);
3559 if (!obj->data.mail->p_timed_thread) {
3560 ERR("Error creating %s timed thread", text);
3562 timed_thread_register(obj->data.mail->p_timed_thread,
3563 &obj->data.mail->p_timed_thread);
3564 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3565 ERR("Error running %s timed thread", text);
3568 return obj->data.mail;
3569 } else if (!obj->a) {
3570 // something is wrong, warn once then stop
3571 ERR("There's a problem with your mail settings. "
3572 "Check that the global mail settings are properly defined"
3573 " (line %li).", obj->line);
3579 char *format_time(unsigned long timeval, const int width)
3582 unsigned long nt; // narrow time, for speed on 32-bit
3583 unsigned cc; // centiseconds
3584 unsigned nn; // multi-purpose whatever
3587 cc = nt % 100; // centiseconds past second
3588 nt /= 100; // total seconds
3589 nn = nt % 60; // seconds past the minute
3590 nt /= 60; // total minutes
3591 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3593 return strndup(buf, text_buffer_size);
3595 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3596 return strndup(buf, text_buffer_size);
3598 nn = nt % 60; // minutes past the hour
3599 nt /= 60; // total hours
3600 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3601 return strndup(buf, text_buffer_size);
3603 nn = nt; // now also hours
3604 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3605 return strndup(buf, text_buffer_size);
3607 nn /= 24; // now days
3608 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3609 return strndup(buf, text_buffer_size);
3611 nn /= 7; // now weeks
3612 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3613 return strndup(buf, text_buffer_size);
3615 // well shoot, this outta' fit...
3616 return strndup("<inf>", text_buffer_size);
3619 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3620 //string has to end with \0 and it's length should fit in a int
3622 void remove_deleted_chars(char *string){
3624 while(string[i] != 0){
3625 if(string[i] == BACKSPACE){
3627 strcpy( &(string[i-1]), &(string[i+1]) );
3629 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3634 static inline void format_media_player_time(char *buf, const int size,
3637 int days, hours, minutes;
3639 days = seconds / (24 * 60 * 60);
3640 seconds %= (24 * 60 * 60);
3641 hours = seconds / (60 * 60);
3642 seconds %= (60 * 60);
3643 minutes = seconds / 60;
3647 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3648 hours, minutes, seconds);
3649 } else if (hours > 0) {
3650 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3653 snprintf(buf, size, "%i:%02i", minutes, seconds);
3657 static inline double get_barnum(char *buf)
3669 if (sscanf(buf, "%lf", &barnum) == 0) {
3670 ERR("reading exec value failed (perhaps it's not the "
3671 "correct format?)");
3674 if (barnum > 100.0 || barnum < 0.0) {
3675 ERR("your exec value is not between 0 and 100, "
3676 "therefore it will be ignored");
3682 static void generate_text_internal(char *p, int p_max_size,
3683 struct text_object root, struct information *cur)
3685 struct text_object *obj;
3687 int need_to_load_fonts = 0;
3690 /* for the OBJ_top* handler */
3691 struct process **needed = 0;
3694 char buff_in[p_max_size];
3696 iconv_converting = 0;
3697 #endif /* HAVE_ICONV */
3701 while (obj && p_max_size > 0) {
3702 needed = 0; // reset for top stuff
3704 /* IFBLOCK jumping algorithm
3706 * This is easier as it looks like:
3707 * - each IF checks it's condition
3708 * - on FALSE: call DO_JUMP
3709 * - on TRUE: don't care
3710 * - each ELSE calls DO_JUMP unconditionally
3711 * - each ENDIF is silently being ignored
3714 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3715 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3716 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3717 * regularly, "obj" is being updated to point to obj->next, so object parsing
3718 * continues right after the corresponding ELSE or ENDIF. This means that if we
3719 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3720 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3721 * jumped, and there is nothing to do.
3725 obj = obj->data.ifblock.next; \
3728 #define OBJ(a) break; case OBJ_##a:
3730 switch (obj->type) {
3732 ERR("not implemented obj type %d", obj->type);
3735 struct sockaddr_in addr;
3736 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3738 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3740 memset(&addr, 0, sizeof(addr));
3741 addr.sin_family = AF_INET;
3742 addr.sin_port = obj->data.read_tcp.port;
3743 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3744 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3748 FD_SET(sock, &readfds);
3751 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3752 received = recv(sock, p, p_max_size, 0);
3757 ERR("read_tcp: Couldn't create a connection");
3760 ERR("read_tcp: Couldn't create a socket");
3763 ERR("read_tcp: Problem with resolving the hostname");
3768 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3770 #endif /* !__OpenBSD__ */
3773 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3774 obj->data.cpu_index);
3780 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3781 obj->data.cpu_index);
3783 /* OpenBSD has no such flag (SUSv2) */
3784 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3785 obj->data.cpu_index);
3786 #endif /* __OpenBSD */
3789 #if defined(__linux__)
3792 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3793 obj->data.cpu_index);
3798 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3799 obj->data.cpu_index);
3804 OBJ(wireless_essid) {
3805 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3807 OBJ(wireless_mode) {
3808 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3810 OBJ(wireless_bitrate) {
3811 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3814 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3816 OBJ(wireless_link_qual) {
3817 spaced_print(p, p_max_size, "%d", 4,
3818 obj->data.net->link_qual);
3820 OBJ(wireless_link_qual_max) {
3821 spaced_print(p, p_max_size, "%d", 4,
3822 obj->data.net->link_qual_max);
3824 OBJ(wireless_link_qual_perc) {
3825 if (obj->data.net->link_qual_max > 0) {
3826 spaced_print(p, p_max_size, "%.0f", 5,
3827 (double) obj->data.net->link_qual /
3828 obj->data.net->link_qual_max * 100);
3830 spaced_print(p, p_max_size, "unk", 5);
3833 OBJ(wireless_link_bar) {
3835 if(output_methods & TO_X) {
3836 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3837 obj->data.net->link_qual_max) * 255.0);
3840 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3841 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3842 obj->data.net->link_qual_max) * 100.0, obj->a);
3847 #endif /* HAVE_IWLIB */
3849 #endif /* __linux__ */
3853 get_adt746x_cpu(p, p_max_size);
3856 get_adt746x_fan(p, p_max_size);
3859 get_acpi_fan(p, p_max_size);
3861 OBJ(acpiacadapter) {
3862 get_acpi_ac_adapter(p, p_max_size);
3865 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3868 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3870 OBJ(battery_percent) {
3871 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3875 if(output_methods & TO_X) {
3876 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3879 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3880 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3885 OBJ(battery_short) {
3886 get_battery_short_status(p, p_max_size, obj->data.s);
3888 #endif /* __OpenBSD__ */
3891 human_readable(cur->buffers * 1024, p, 255);
3894 human_readable(cur->cached * 1024, p, 255);
3897 if (obj->data.cpu_index > info.cpu_count) {
3898 ERR("obj->data.cpu_index %i info.cpu_count %i",
3899 obj->data.cpu_index, info.cpu_count);
3900 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
3902 percent_print(p, p_max_size,
3903 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3907 new_gauge(p, obj->a, obj->b,
3908 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3912 if(output_methods & TO_X) {
3913 new_bar(p, obj->a, obj->b,
3914 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3917 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3918 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3925 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3926 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3927 100, 1, obj->char_a, obj->char_b);
3930 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3931 obj->e, 1, obj->char_a, obj->char_b);
3934 new_fg(p, obj->data.l);
3967 OBJ(conky_version) {
3968 snprintf(p, p_max_size, "%s", VERSION);
3970 OBJ(conky_build_date) {
3971 snprintf(p, p_max_size, "%s", BUILD_DATE);
3973 OBJ(conky_build_arch) {
3974 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3976 #if defined(__linux__)
3978 snprintf(p, p_max_size, "%s",
3979 get_disk_protect_queue(obj->data.s));
3982 snprintf(p, p_max_size, "%s", i8k.version);
3985 snprintf(p, p_max_size, "%s", i8k.bios);
3988 snprintf(p, p_max_size, "%s", i8k.serial);
3993 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3994 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3996 OBJ(i8k_left_fan_status) {
3997 int left_fan_status;
3999 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4000 if (left_fan_status == 0) {
4001 snprintf(p, p_max_size, "off");
4003 if (left_fan_status == 1) {
4004 snprintf(p, p_max_size, "low");
4006 if (left_fan_status == 2) {
4007 snprintf(p, p_max_size, "high");
4010 OBJ(i8k_right_fan_status) {
4011 int right_fan_status;
4013 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4014 if (right_fan_status == 0) {
4015 snprintf(p, p_max_size, "off");
4017 if (right_fan_status == 1) {
4018 snprintf(p, p_max_size, "low");
4020 if (right_fan_status == 2) {
4021 snprintf(p, p_max_size, "high");
4024 OBJ(i8k_left_fan_rpm) {
4025 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4027 OBJ(i8k_right_fan_rpm) {
4028 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4030 OBJ(i8k_ac_status) {
4033 sscanf(i8k.ac_status, "%d", &ac_status);
4034 if (ac_status == -1) {
4035 snprintf(p, p_max_size, "disabled (read i8k docs)");
4037 if (ac_status == 0) {
4038 snprintf(p, p_max_size, "off");
4040 if (ac_status == 1) {
4041 snprintf(p, p_max_size, "on");
4044 OBJ(i8k_buttons_status) {
4045 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4049 get_ibm_acpi_fan(p, p_max_size);
4052 get_ibm_acpi_temps();
4053 temp_print(p, p_max_size,
4054 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4057 get_ibm_acpi_volume(p, p_max_size);
4059 OBJ(ibm_brightness) {
4060 get_ibm_acpi_brightness(p, p_max_size);
4063 /* information from sony_laptop kernel module
4064 * /sys/devices/platform/sony-laptop */
4065 OBJ(sony_fanspeed) {
4066 get_sony_fanspeed(p, p_max_size);
4069 if (!cur->gw_info.count) {
4074 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4077 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4080 snprintf(p, p_max_size, "%d", get_laptop_mode());
4083 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4085 #endif /* __linux__ */
4086 #if (defined(__FreeBSD__) || defined(__linux__))
4088 if ((obj->data.ifblock.s)
4089 && (!interface_up(obj->data.ifblock.s))) {
4095 OBJ(obsd_sensors_temp) {
4096 obsd_sensors.device = sensor_device;
4097 update_obsd_sensors();
4098 temp_print(p, p_max_size,
4099 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4102 OBJ(obsd_sensors_fan) {
4103 obsd_sensors.device = sensor_device;
4104 update_obsd_sensors();
4105 snprintf(p, p_max_size, "%d",
4106 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4108 OBJ(obsd_sensors_volt) {
4109 obsd_sensors.device = sensor_device;
4110 update_obsd_sensors();
4111 snprintf(p, p_max_size, "%.2f",
4112 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4115 get_obsd_vendor(p, p_max_size);
4118 get_obsd_product(p, p_max_size);
4120 #endif /* __OpenBSD__ */
4123 new_font(p, obj->data.s);
4124 need_to_load_fonts = 1;
4127 /* TODO: move this correction from kB to kB/s elsewhere
4128 * (or get rid of it??) */
4130 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4134 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4138 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4143 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4144 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4146 OBJ(diskiograph_read) {
4147 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4148 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4150 OBJ(diskiograph_write) {
4151 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4152 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4156 human_readable(obj->data.net->recv_speed, p, 255);
4159 spaced_print(p, p_max_size, "%.1f", 8,
4160 obj->data.net->recv_speed / 1024.0);
4163 OBJ(downspeedgraph) {
4164 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4165 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4169 /* Since we see you, you're if has not jumped.
4170 * Do Ninja jump here: without leaving traces.
4171 * This is to prevent us from stale jumped flags.
4173 obj = obj->data.ifblock.next;
4177 /* harmless object, just ignore */
4181 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4182 && (obj->data.net->addr.sa_data[3] & 255) == 0
4183 && (obj->data.net->addr.sa_data[4] & 255) == 0
4184 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4185 snprintf(p, p_max_size, "No Address");
4187 snprintf(p, p_max_size, "%u.%u.%u.%u",
4188 obj->data.net->addr.sa_data[2] & 255,
4189 obj->data.net->addr.sa_data[3] & 255,
4190 obj->data.net->addr.sa_data[4] & 255,
4191 obj->data.net->addr.sa_data[5] & 255);
4194 #if defined(__linux__)
4196 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4198 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4199 strcpy(p, obj->data.net->addrs);
4202 strcpy(p, "0.0.0.0");
4204 #endif /* __linux__ */
4205 #if defined(IMLIB2) && defined(X11)
4207 /* doesn't actually draw anything, just queues it omp. the
4208 * image will get drawn after the X event loop */
4209 cimlib_add_image(obj->data.s);
4213 evaluate(obj->data.s, p);
4216 read_exec(obj->data.s, p, text_buffer_size);
4217 remove_deleted_chars(p);
4220 struct information *tmp_info;
4221 struct text_object subroot;
4223 read_exec(obj->data.s, p, text_buffer_size);
4225 tmp_info = malloc(sizeof(struct information));
4226 memcpy(tmp_info, cur, sizeof(struct information));
4227 parse_conky_vars(&subroot, p, p, tmp_info);
4229 free_text_objects(&subroot, 1);
4236 read_exec(obj->data.s, p, text_buffer_size);
4237 barnum = get_barnum(p); /*using the same function*/
4239 if (barnum >= 0.0) {
4241 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4248 read_exec(obj->data.s, p, text_buffer_size);
4249 barnum = get_barnum(p);
4251 if (barnum >= 0.0) {
4253 if(output_methods & TO_X) {
4255 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4258 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4259 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4267 char showaslog = FALSE;
4268 char tempgrad = FALSE;
4270 char *cmd = obj->data.s;
4272 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4274 cmd += strlen(" "TEMPGRAD);
4276 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4278 cmd += strlen(" "LOGGRAPH);
4280 read_exec(cmd, p, text_buffer_size);
4281 barnum = get_barnum(p);
4284 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4285 100, 1, showaslog, tempgrad);
4290 if (current_update_time - obj->data.execi.last_update
4291 >= obj->data.execi.interval) {
4294 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4295 barnum = get_barnum(p);
4297 if (barnum >= 0.0) {
4300 obj->data.execi.last_update = current_update_time;
4303 if(output_methods & TO_X) {
4304 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4307 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4308 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4315 if (current_update_time - obj->data.execi.last_update
4316 >= obj->data.execi.interval) {
4318 char showaslog = FALSE;
4319 char tempgrad = FALSE;
4320 char *cmd = obj->data.execi.cmd;
4322 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4324 cmd += strlen(" "TEMPGRAD);
4326 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4328 cmd += strlen(" "LOGGRAPH);
4330 obj->char_a = showaslog;
4331 obj->char_b = tempgrad;
4332 read_exec(cmd, p, text_buffer_size);
4333 barnum = get_barnum(p);
4335 if (barnum >= 0.0) {
4338 obj->data.execi.last_update = current_update_time;
4340 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4343 if (current_update_time - obj->data.execi.last_update
4344 >= obj->data.execi.interval) {
4347 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4348 barnum = get_barnum(p);
4350 if (barnum >= 0.0) {
4351 obj->f = 255 * barnum / 100.0;
4353 obj->data.execi.last_update = current_update_time;
4355 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4359 if (current_update_time - obj->data.execi.last_update
4360 >= obj->data.execi.interval
4361 && obj->data.execi.interval != 0) {
4362 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4364 obj->data.execi.last_update = current_update_time;
4366 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4369 struct text_object subroot;
4370 struct information *tmp_info =
4371 malloc(sizeof(struct information));
4372 memcpy(tmp_info, cur, sizeof(struct information));
4374 if (current_update_time - obj->data.execi.last_update
4375 < obj->data.execi.interval
4376 || obj->data.execi.interval == 0) {
4377 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4379 char *output = obj->data.execi.buffer;
4380 FILE *fp = popen(obj->data.execi.cmd, "r");
4381 int length = fread(output, 1, text_buffer_size, fp);
4385 output[length] = '\0';
4386 if (length > 0 && output[length - 1] == '\n') {
4387 output[length - 1] = '\0';
4390 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4391 obj->data.execi.last_update = current_update_time;
4393 free_text_objects(&subroot, 1);
4397 if (!obj->data.texeci.p_timed_thread) {
4398 obj->data.texeci.p_timed_thread =
4399 timed_thread_create(&threaded_exec,
4400 (void *) obj, obj->data.texeci.interval * 1000000);
4401 if (!obj->data.texeci.p_timed_thread) {
4402 ERR("Error creating texeci timed thread");
4404 timed_thread_register(obj->data.texeci.p_timed_thread,
4405 &obj->data.texeci.p_timed_thread);
4406 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4407 ERR("Error running texeci timed thread");
4410 timed_thread_lock(obj->data.texeci.p_timed_thread);
4411 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4412 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4415 #endif /* HAVE_POPEN */
4417 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4419 if (mail && mail->p_timed_thread) {
4420 timed_thread_lock(mail->p_timed_thread);
4421 snprintf(p, p_max_size, "%lu", mail->unseen);
4422 timed_thread_unlock(mail->p_timed_thread);
4425 OBJ(imap_messages) {
4426 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4428 if (mail && mail->p_timed_thread) {
4429 timed_thread_lock(mail->p_timed_thread);
4430 snprintf(p, p_max_size, "%lu", mail->messages);
4431 timed_thread_unlock(mail->p_timed_thread);
4435 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4437 if (mail && mail->p_timed_thread) {
4438 timed_thread_lock(mail->p_timed_thread);
4439 snprintf(p, p_max_size, "%lu", mail->unseen);
4440 timed_thread_unlock(mail->p_timed_thread);
4444 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4446 if (mail && mail->p_timed_thread) {
4447 timed_thread_lock(mail->p_timed_thread);
4448 snprintf(p, p_max_size, "%.1f",
4449 mail->used / 1024.0 / 1024.0);
4450 timed_thread_unlock(mail->p_timed_thread);
4454 if (obj->data.fs != NULL) {
4455 if (obj->data.fs->size == 0) {
4457 if(output_methods & TO_X) {
4458 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4461 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4462 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4468 if(output_methods & TO_X) {
4469 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4470 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4471 obj->data.fs->size));
4474 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4475 new_bar_in_shell(p, p_max_size,
4476 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4484 if (obj->data.fs != NULL) {
4485 human_readable(obj->data.fs->avail, p, 255);
4489 if (obj->data.fs != NULL) {
4492 if (obj->data.fs->size) {
4493 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4496 percent_print(p, p_max_size, val);
4500 if (obj->data.fs != NULL) {
4501 human_readable(obj->data.fs->size, p, 255);
4505 if (obj->data.fs != NULL)
4506 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4509 if (obj->data.fs != NULL) {
4510 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4515 if (obj->data.fs != NULL) {
4516 if (obj->data.fs->size == 0) {
4518 if(output_methods & TO_X) {
4519 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4522 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4523 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4529 if(output_methods & TO_X) {
4530 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4531 (int) (obj->data.fsbar.fs->avail * 255 /
4532 obj->data.fs->size));
4535 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4536 new_bar_in_shell(p, p_max_size,
4537 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4545 if (obj->data.fs != NULL) {
4548 if (obj->data.fs->size) {
4549 val = obj->data.fs->free
4554 percent_print(p, p_max_size, 100 - val);
4558 float *v = info.loadavg;
4560 if (obj->data.loadavg[2]) {
4561 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4562 v[obj->data.loadavg[0] - 1],
4563 v[obj->data.loadavg[1] - 1],
4564 v[obj->data.loadavg[2] - 1]);
4565 } else if (obj->data.loadavg[1]) {
4566 snprintf(p, p_max_size, "%.2f %.2f",
4567 v[obj->data.loadavg[0] - 1],
4568 v[obj->data.loadavg[1] - 1]);
4569 } else if (obj->data.loadavg[0]) {
4570 snprintf(p, p_max_size, "%.2f",
4571 v[obj->data.loadavg[0] - 1]);
4575 new_goto(p, obj->data.i);
4578 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4582 new_hr(p, obj->data.i);
4586 if (cur->nameserver_info.nscount > obj->data.i)
4587 snprintf(p, p_max_size, "%s",
4588 cur->nameserver_info.ns_list[obj->data.i]);
4592 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4593 snprintf(p, p_max_size, "%s", skill);
4598 PRSS *data = get_rss_info(obj->data.rss.uri,
4599 obj->data.rss.delay);
4603 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4605 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4607 // remove trailing new line if one exists
4608 if (str[strlen(str) - 1] == '\n') {
4609 str[strlen(str) - 1] = 0;
4611 snprintf(p, p_max_size, "%s", str);
4612 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4613 if (obj->data.rss.act_par < data->item_count) {
4614 str = data->items[obj->data.rss.act_par].title;
4615 // remove trailing new line if one exists
4616 if (str[strlen(str) - 1] == '\n') {
4617 str[strlen(str) - 1] = 0;
4619 snprintf(p, p_max_size, "%s", str);
4621 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4622 if (obj->data.rss.act_par < data->item_count) {
4624 data->items[obj->data.rss.act_par].description;
4625 // remove trailing new line if one exists
4626 if (str[strlen(str) - 1] == '\n') {
4627 str[strlen(str) - 1] = 0;
4629 snprintf(p, p_max_size, "%s", str);
4631 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4632 if (data->item_count > 0) {
4635 //'tmpspaces' is a string with spaces too be placed in front of each title
4636 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4637 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4638 tmpspaces[obj->data.rss.nrspaces]=0;
4642 if (obj->data.rss.act_par > data->item_count) {
4643 show = data->item_count;
4645 show = obj->data.rss.act_par;
4647 for (itmp = 0; itmp < show; itmp++) {
4648 PRSS_Item *item = &data->items[itmp];
4652 // don't add new line before first item
4654 strncat(p, "\n", p_max_size);
4656 /* remove trailing new line if one exists,
4657 * we have our own */
4658 if (str[strlen(str) - 1] == '\n') {
4659 str[strlen(str) - 1] = 0;
4661 strncat(p, tmpspaces, p_max_size);
4662 strncat(p, str, p_max_size);
4673 if( obj->data.weather.uri != NULL ) {
4674 process_weather_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4676 strncpy(p, "invalid xoap keys file", p_max_size);
4682 char *str = llua_getstring(obj->data.s);
4684 snprintf(p, p_max_size, "%s", str);
4689 char *str = llua_getstring(obj->data.s);
4696 if (llua_getnumber(obj->data.s, &per)) {
4698 if(output_methods & TO_X) {
4699 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4702 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4703 new_bar_in_shell(p, p_max_size, per, obj->a);
4712 if (llua_getnumber(obj->data.s, &per)) {
4713 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4714 per, obj->e, 1, obj->char_a, obj->char_b);
4719 if (llua_getnumber(obj->data.s, &per)) {
4720 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4724 #endif /* HAVE_LUA */
4729 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4730 if (obj->data.hddtemp.temp)
4731 free(obj->data.hddtemp.temp);
4732 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4733 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4734 obj->data.hddtemp.update_time = current_update_time;
4736 if (!obj->data.hddtemp.temp) {
4737 snprintf(p, p_max_size, "N/A");
4739 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4740 unit = obj->data.hddtemp.temp[0];
4742 if (*endptr != '\0')
4743 snprintf(p, p_max_size, "N/A");
4744 else if (unit == 'C')
4745 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4746 else if (unit == 'F')
4747 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4749 snprintf(p, p_max_size, "N/A");
4754 new_offset(p, obj->data.i);
4757 new_voffset(p, obj->data.i);
4763 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4764 obj->data.sysfs.devtype, obj->data.sysfs.type);
4766 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4768 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4769 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4770 } else if (r >= 100.0 || r == 0) {
4771 snprintf(p, p_max_size, "%d", (int) r);
4773 snprintf(p, p_max_size, "%.1f", r);
4779 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4780 obj->data.sysfs.devtype, obj->data.sysfs.type);
4782 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4784 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4785 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4786 } else if (r >= 100.0 || r == 0) {
4787 snprintf(p, p_max_size, "%d", (int) r);
4789 snprintf(p, p_max_size, "%.1f", r);
4795 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4796 obj->data.sysfs.devtype, obj->data.sysfs.type);
4798 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4800 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4801 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4802 } else if (r >= 100.0 || r == 0) {
4803 snprintf(p, p_max_size, "%d", (int) r);
4805 snprintf(p, p_max_size, "%.1f", r);
4808 #endif /* __linux__ */
4810 new_alignr(p, obj->data.i);
4813 new_alignc(p, obj->data.i);
4816 char buf[max_user_text];
4817 struct information *tmp_info =
4818 malloc(sizeof(struct information));
4819 memcpy(tmp_info, cur, sizeof(struct information));
4820 generate_text_internal(buf, max_user_text,
4821 *obj->sub, tmp_info);
4823 if (strlen(buf) != 0) {
4829 char expression[max_user_text];
4831 struct information *tmp_info;
4833 tmp_info = malloc(sizeof(struct information));
4834 memcpy(tmp_info, cur, sizeof(struct information));
4835 generate_text_internal(expression, max_user_text,
4836 *obj->sub, tmp_info);
4837 DBGP("parsed arg into '%s'", expression);
4839 val = compare(expression);
4841 ERR("compare failed for expression '%s'",
4849 if (obj->data.ifblock.str
4850 && !check_contains(obj->data.ifblock.s,
4851 obj->data.ifblock.str)) {
4853 } else if (obj->data.ifblock.s
4854 && access(obj->data.ifblock.s, F_OK)) {
4859 if ((obj->data.ifblock.s)
4860 && (!check_mount(obj->data.ifblock.s))) {
4866 if (!get_process_by_name(obj->data.ifblock.s)) {
4868 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4873 #if defined(__linux__)
4875 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4879 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4882 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4887 human_readable(cur->mem * 1024, p, 255);
4890 human_readable(cur->memeasyfree * 1024, p, 255);
4893 human_readable(cur->memfree * 1024, p, 255);
4896 human_readable(cur->memmax * 1024, p, 255);
4900 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4904 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4905 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4910 if(output_methods & TO_X) {
4911 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4912 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4915 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4916 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4923 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4924 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4925 100, 1, obj->char_a, obj->char_b);
4930 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4933 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4936 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4940 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4941 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4944 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4945 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4948 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4949 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4952 OBJ(if_mixer_mute) {
4953 if (!mixer_is_mute(obj->data.ifblock.i)) {
4958 #define NOT_IN_X "Not running in X"
4960 if(x_initialised != YES) {
4961 strncpy(p, NOT_IN_X, p_max_size);
4963 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4966 OBJ(monitor_number) {
4967 if(x_initialised != YES) {
4968 strncpy(p, NOT_IN_X, p_max_size);
4970 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4974 if(x_initialised != YES) {
4975 strncpy(p, NOT_IN_X, p_max_size);
4977 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
4980 OBJ(desktop_number) {
4981 if(x_initialised != YES) {
4982 strncpy(p, NOT_IN_X, p_max_size);
4984 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
4988 if(x_initialised != YES) {
4989 strncpy(p, NOT_IN_X, p_max_size);
4990 }else if(cur->x11.desktop.name != NULL) {
4991 strncpy(p, cur->x11.desktop.name, p_max_size);
4998 update_mail_count(&obj->data.local_mail);
4999 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5002 update_mail_count(&obj->data.local_mail);
5003 snprintf(p, p_max_size, "%d",
5004 obj->data.local_mail.new_mail_count);
5007 update_mail_count(&obj->data.local_mail);
5008 snprintf(p, p_max_size, "%d",
5009 obj->data.local_mail.seen_mail_count);
5012 update_mail_count(&obj->data.local_mail);
5013 snprintf(p, p_max_size, "%d",
5014 obj->data.local_mail.unseen_mail_count);
5016 OBJ(flagged_mails) {
5017 update_mail_count(&obj->data.local_mail);
5018 snprintf(p, p_max_size, "%d",
5019 obj->data.local_mail.flagged_mail_count);
5021 OBJ(unflagged_mails) {
5022 update_mail_count(&obj->data.local_mail);
5023 snprintf(p, p_max_size, "%d",
5024 obj->data.local_mail.unflagged_mail_count);
5026 OBJ(forwarded_mails) {
5027 update_mail_count(&obj->data.local_mail);
5028 snprintf(p, p_max_size, "%d",
5029 obj->data.local_mail.forwarded_mail_count);
5031 OBJ(unforwarded_mails) {
5032 update_mail_count(&obj->data.local_mail);
5033 snprintf(p, p_max_size, "%d",
5034 obj->data.local_mail.unforwarded_mail_count);
5036 OBJ(replied_mails) {
5037 update_mail_count(&obj->data.local_mail);
5038 snprintf(p, p_max_size, "%d",
5039 obj->data.local_mail.replied_mail_count);
5041 OBJ(unreplied_mails) {
5042 update_mail_count(&obj->data.local_mail);
5043 snprintf(p, p_max_size, "%d",
5044 obj->data.local_mail.unreplied_mail_count);
5047 update_mail_count(&obj->data.local_mail);
5048 snprintf(p, p_max_size, "%d",
5049 obj->data.local_mail.draft_mail_count);
5051 OBJ(trashed_mails) {
5052 update_mail_count(&obj->data.local_mail);
5053 snprintf(p, p_max_size, "%d",
5054 obj->data.local_mail.trashed_mail_count);
5057 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5059 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5062 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5065 new_outline(p, obj->data.l);
5068 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5070 OBJ(running_processes) {
5071 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5074 snprintf(p, p_max_size, "%s", obj->data.s);
5078 new_bg(p, obj->data.l);
5081 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5085 human_readable(cur->swap * 1024, p, 255);
5088 human_readable(cur->swapfree * 1024, p, 255);
5091 human_readable(cur->swapmax * 1024, p, 255);
5094 if (cur->swapmax == 0) {
5095 strncpy(p, "No swap", p_max_size);
5097 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5102 if(output_methods & TO_X) {
5103 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5104 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5107 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5108 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5114 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5117 time_t t = time(NULL);
5118 struct tm *tm = localtime(&t);
5120 setlocale(LC_TIME, "");
5121 strftime(p, p_max_size, obj->data.s, tm);
5124 time_t t = time(NULL);
5125 struct tm *tm = gmtime(&t);
5127 strftime(p, p_max_size, obj->data.s, tm);
5134 if (obj->data.tztime.tz) {
5135 oldTZ = getenv("TZ");
5136 setenv("TZ", obj->data.tztime.tz, 1);
5142 setlocale(LC_TIME, "");
5143 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5145 setenv("TZ", oldTZ, 1);
5150 // Needless to free oldTZ since getenv gives ptr to static data
5153 human_readable(obj->data.net->recv, p, 255);
5156 human_readable(obj->data.net->trans, p, 255);
5159 snprintf(p, p_max_size, "%d", total_updates);
5162 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5167 human_readable(obj->data.net->trans_speed, p, 255);
5170 spaced_print(p, p_max_size, "%.1f", 8,
5171 obj->data.net->trans_speed / 1024.0);
5175 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5176 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5180 format_seconds_short(p, p_max_size, (int) cur->uptime);
5183 format_seconds(p, p_max_size, (int) cur->uptime);
5186 snprintf(p, p_max_size, "%s", cur->users.names);
5189 snprintf(p, p_max_size, "%s", cur->users.terms);
5192 snprintf(p, p_max_size, "%s", cur->users.times);
5195 snprintf(p, p_max_size, "%d", cur->users.number);
5197 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5198 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5202 msg = get_apm_adapter();
5203 snprintf(p, p_max_size, "%s", msg);
5206 OBJ(apm_battery_life) {
5209 msg = get_apm_battery_life();
5210 snprintf(p, p_max_size, "%s", msg);
5213 OBJ(apm_battery_time) {
5216 msg = get_apm_battery_time();
5217 snprintf(p, p_max_size, "%s", msg);
5220 #endif /* __FreeBSD__ __OpenBSD__ */
5223 #define mpd_printf(fmt, val) \
5224 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5225 #define mpd_sprintf(val) { \
5226 if (!obj->data.i || obj->data.i > p_max_size) \
5227 mpd_printf("%s", val); \
5229 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5234 mpd_sprintf(artist);
5238 mpd_printf("%s", random);
5240 mpd_printf("%s", repeat);
5248 mpd_printf("%d", volume);
5250 mpd_printf("%d", bitrate);
5252 mpd_printf("%s", status);
5254 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5257 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5260 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5264 if(output_methods & TO_X) {
5265 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5266 (int) (mpd_get_info()->progress * 255.0f));
5269 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5270 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5276 struct mpd_s *mpd = mpd_get_info();
5277 int len = obj->data.i;
5278 if (len == 0 || len > p_max_size)
5281 memset(p, 0, p_max_size);
5282 if (mpd->artist && *mpd->artist &&
5283 mpd->title && *mpd->title) {
5284 snprintf(p, len, "%s - %s", mpd->artist,
5286 } else if (mpd->title && *mpd->title) {
5287 snprintf(p, len, "%s", mpd->title);
5288 } else if (mpd->artist && *mpd->artist) {
5289 snprintf(p, len, "%s", mpd->artist);
5290 } else if (mpd->file && *mpd->file) {
5291 snprintf(p, len, "%s", mpd->file);
5296 OBJ(if_mpd_playing) {
5297 if (!mpd_get_info()->is_playing) {
5306 #define MOC_PRINT(t, a) \
5307 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5309 MOC_PRINT(state, "??");
5312 MOC_PRINT(file, "no file");
5315 MOC_PRINT(title, "no title");
5318 MOC_PRINT(artist, "no artist");
5321 MOC_PRINT(song, "no song");
5324 MOC_PRINT(album, "no album");
5326 OBJ(moc_totaltime) {
5327 MOC_PRINT(totaltime, "0:00");
5330 MOC_PRINT(timeleft, "0:00");
5333 MOC_PRINT(curtime, "0:00");
5336 MOC_PRINT(bitrate, "0Kbps");
5339 MOC_PRINT(rate, "0KHz");
5345 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5348 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5351 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5354 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5356 OBJ(xmms2_comment) {
5357 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5360 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5363 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5366 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5368 OBJ(xmms2_tracknr) {
5369 if (cur->xmms2.tracknr != -1) {
5370 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5373 OBJ(xmms2_bitrate) {
5374 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5377 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5380 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5382 OBJ(xmms2_elapsed) {
5383 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5384 (cur->xmms2.elapsed / 1000) % 60);
5386 OBJ(xmms2_duration) {
5387 snprintf(p, p_max_size, "%02d:%02d",
5388 cur->xmms2.duration / 60000,
5389 (cur->xmms2.duration / 1000) % 60);
5391 OBJ(xmms2_percent) {
5392 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5396 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5397 (int) (cur->xmms2.progress * 255.0f));
5400 OBJ(xmms2_playlist) {
5401 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5403 OBJ(xmms2_timesplayed) {
5404 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5407 if (strlen(cur->xmms2.title) < 2
5408 && strlen(cur->xmms2.title) < 2) {
5409 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5411 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5415 OBJ(if_xmms2_connected) {
5416 if (cur->xmms2.conn_state != 1) {
5422 OBJ(audacious_status) {
5423 snprintf(p, p_max_size, "%s",
5424 cur->audacious.items[AUDACIOUS_STATUS]);
5426 OBJ(audacious_title) {
5427 snprintf(p, cur->audacious.max_title_len > 0
5428 ? cur->audacious.max_title_len : p_max_size, "%s",
5429 cur->audacious.items[AUDACIOUS_TITLE]);
5431 OBJ(audacious_length) {
5432 snprintf(p, p_max_size, "%s",
5433 cur->audacious.items[AUDACIOUS_LENGTH]);
5435 OBJ(audacious_length_seconds) {
5436 snprintf(p, p_max_size, "%s",
5437 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5439 OBJ(audacious_position) {
5440 snprintf(p, p_max_size, "%s",
5441 cur->audacious.items[AUDACIOUS_POSITION]);
5443 OBJ(audacious_position_seconds) {
5444 snprintf(p, p_max_size, "%s",
5445 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5447 OBJ(audacious_bitrate) {
5448 snprintf(p, p_max_size, "%s",
5449 cur->audacious.items[AUDACIOUS_BITRATE]);
5451 OBJ(audacious_frequency) {
5452 snprintf(p, p_max_size, "%s",
5453 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5455 OBJ(audacious_channels) {
5456 snprintf(p, p_max_size, "%s",
5457 cur->audacious.items[AUDACIOUS_CHANNELS]);
5459 OBJ(audacious_filename) {
5460 snprintf(p, p_max_size, "%s",
5461 cur->audacious.items[AUDACIOUS_FILENAME]);
5463 OBJ(audacious_playlist_length) {
5464 snprintf(p, p_max_size, "%s",
5465 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5467 OBJ(audacious_playlist_position) {
5468 snprintf(p, p_max_size, "%s",
5469 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5471 OBJ(audacious_main_volume) {
5472 snprintf(p, p_max_size, "%s",
5473 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5476 OBJ(audacious_bar) {
5480 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5481 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5482 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5485 #endif /* AUDACIOUS */
5489 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5492 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5495 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5498 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5501 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5504 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5507 /* we have four different types of top (top, top_mem,
5508 * top_time and top_io). To avoid having almost-same code four
5509 * times, we have this special handler. */
5512 parse_top_args("top", obj->data.top.s, obj);
5513 if (!needed) needed = cur->cpu;
5515 parse_top_args("top_mem", obj->data.top.s, obj);
5516 if (!needed) needed = cur->memu;
5518 parse_top_args("top_time", obj->data.top.s, obj);
5519 if (!needed) needed = cur->time;
5522 parse_top_args("top_io", obj->data.top.s, obj);
5523 if (!needed) needed = cur->io;
5526 if (needed[obj->data.top.num]) {
5529 switch (obj->data.top.type) {
5531 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5532 needed[obj->data.top.num]->name);
5535 snprintf(p, 7, "%6.2f",
5536 needed[obj->data.top.num]->amount);
5539 snprintf(p, 6, "%5i",
5540 needed[obj->data.top.num]->pid);
5543 snprintf(p, 7, "%6.2f",
5544 needed[obj->data.top.num]->totalmem);
5547 timeval = format_time(
5548 needed[obj->data.top.num]->total_cpu_time, 9);
5549 snprintf(p, 10, "%9s", timeval);
5553 human_readable(needed[obj->data.top.num]->rss,
5557 human_readable(needed[obj->data.top.num]->vsize,
5561 case TOP_READ_BYTES:
5562 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5565 case TOP_WRITE_BYTES:
5566 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5570 snprintf(p, 7, "%6.2f",
5571 needed[obj->data.top.num]->io_perc);
5577 print_tail_object(obj, p, p_max_size);
5579 print_head_object(obj, p, p_max_size);
5581 FILE *fp = open_file(obj->data.s, &obj->a);
5584 /* FIXME: use something more general (see also tail.c, head.c */
5585 #define BUFSZ 0x1000
5590 while(fgets(buf, BUFSZ, fp) != NULL){
5591 for(j = 0; buf[j] != 0; j++) {
5592 if(buf[j] == '\n') {
5597 sprintf(p, "%d", lines);
5600 sprintf(p, "File Unreadable");
5605 FILE *fp = open_file(obj->data.s, &obj->a);
5610 char inword = FALSE;
5613 while(fgets(buf, BUFSZ, fp) != NULL){
5614 for(j = 0; buf[j] != 0; j++) {
5615 if(!isspace(buf[j])) {
5616 if(inword == FALSE) {
5625 sprintf(p, "%d", words);
5628 sprintf(p, "File Unreadable");
5631 #ifdef TCP_PORT_MONITOR
5633 tcp_portmon_action(p, p_max_size,
5634 &obj->data.tcp_port_monitor);
5636 #endif /* TCP_PORT_MONITOR */
5640 iconv_converting = 1;
5641 iconv_selected = obj->a;
5644 iconv_converting = 0;
5647 #endif /* HAVE_ICONV */
5649 OBJ(entropy_avail) {
5650 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5653 percent_print(p, p_max_size,
5654 cur->entropy.entropy_avail *
5655 100 / cur->entropy.poolsize);
5657 OBJ(entropy_poolsize) {
5658 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5661 double entropy_perc;
5663 entropy_perc = (double) cur->entropy.entropy_avail /
5664 (double) cur->entropy.poolsize;
5666 if(output_methods & TO_X) {
5667 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5670 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5671 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5680 s = smapi_get_val(obj->data.s);
5681 snprintf(p, p_max_size, "%s", s);
5685 OBJ(if_smapi_bat_installed) {
5687 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5688 if(!smapi_bat_installed(idx)) {
5692 ERR("argument to if_smapi_bat_installed must be an integer");
5694 OBJ(smapi_bat_perc) {
5696 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5697 val = smapi_bat_installed(idx) ?
5698 smapi_get_bat_int(idx, "remaining_percent") : 0;
5699 percent_print(p, p_max_size, val);
5701 ERR("argument to smapi_bat_perc must be an integer");
5703 OBJ(smapi_bat_temp) {
5705 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5706 val = smapi_bat_installed(idx) ?
5707 smapi_get_bat_int(idx, "temperature") : 0;
5708 /* temperature is in milli degree celsius */
5709 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5711 ERR("argument to smapi_bat_temp must be an integer");
5713 OBJ(smapi_bat_power) {
5715 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5716 val = smapi_bat_installed(idx) ?
5717 smapi_get_bat_int(idx, "power_now") : 0;
5718 /* power_now is in mW, set to W with one digit precision */
5719 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5721 ERR("argument to smapi_bat_power must be an integer");
5724 OBJ(smapi_bat_bar) {
5725 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5726 new_bar(p, obj->a, obj->b, (int)
5727 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5729 new_bar(p, obj->a, obj->b, 0);
5734 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5735 char buf[max_user_text];
5738 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5739 snprintf(p, p_max_size, "%s", buf);
5740 if(total_updates % 2) {
5741 for(j=0; p[j] != 0; j++) {
5747 char buf[max_user_text];
5749 char unit[16]; //16 because we can also have long names (like mega-bytes)
5751 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5752 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5753 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5754 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5755 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5756 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5757 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5759 snprintf(p, p_max_size, "%s", buf);
5763 char *tmp, buf[max_user_text];
5764 generate_text_internal(buf, max_user_text,
5767 if (strlen(buf) <= obj->data.scroll.show) {
5768 snprintf(p, p_max_size, "%s", buf);
5771 #define LINESEPARATOR '|'
5772 //place all the lines behind each other with LINESEPARATOR between them
5773 for(j = 0; buf[j] != 0; j++) {
5775 buf[j]=LINESEPARATOR;
5778 //scroll the output obj->data.scroll.start places by copying that many chars from
5779 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5780 //at the back of the string
5781 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5782 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5783 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5784 buf[j - obj->data.scroll.start] = buf[j];
5786 strcpy(&buf[j - obj->data.scroll.start], tmp);
5788 //only show the requested number of chars
5789 if(obj->data.scroll.show < j) {
5790 buf[obj->data.scroll.show] = 0;
5792 //next time, scroll a place more or reset scrolling if we are at the end
5793 obj->data.scroll.start += obj->data.scroll.step;
5794 if(obj->data.scroll.start >= j){
5795 obj->data.scroll.start = 0;
5797 snprintf(p, p_max_size, "%s", buf);
5800 char buf[2][max_user_text];
5807 struct llrows* next;
5809 struct llrows *ll_rows[2], *current[2];
5810 struct text_object * objsub = obj->sub;
5813 for(i=0; i<2; i++) {
5816 ll_rows[i] = malloc(sizeof(struct llrows));
5817 current[i] = ll_rows[i];
5818 for(j=0; j<i; j++) objsub = objsub->sub;
5819 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5820 for(j=0; buf[i][j] != 0; j++) {
5821 if(buf[i][j] == '\t') buf[i][j] = ' ';
5822 if(buf[i][j] == '\n') {
5824 current[i]->row = strdup(buf[i]+nextstart);
5825 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5826 current[i]->next = malloc(sizeof(struct llrows));
5827 current[i] = current[i]->next;
5832 current[i]->row = strdup(buf[i]+nextstart);
5833 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5834 current[i]->next = NULL;
5835 current[i] = ll_rows[i];
5837 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5839 strcat(p, current[0]->row);
5840 i=strlen(current[0]->row);
5842 while(i < longest) {
5847 strcat(p, obj->data.combine.seperation);
5848 strcat(p, current[1]->row);
5852 #pragma omp parallel for
5853 #endif /* HAVE_OPENMP */
5854 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5857 #pragma omp parallel for
5858 #endif /* HAVE_OPENMP */
5859 for(i=0; i<2; i++) {
5860 while(ll_rows[i] != NULL) {
5861 current[i]=ll_rows[i];
5862 free(current[i]->row);
5863 ll_rows[i]=current[i]->next;
5870 int value = get_nvidia_value(obj->data.nvidia.type, display);
5872 snprintf(p, p_max_size, "N/A");
5873 else if (obj->data.nvidia.type == NV_TEMP)
5874 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5875 else if (obj->data.nvidia.print_as_float &&
5876 value > 0 && value < 100)
5877 snprintf(p, p_max_size, "%.1f", (float)value);
5879 snprintf(p, p_max_size, "%d", value);
5884 /* This is just a meta-object to set host:port */
5887 snprintf(p, p_max_size, "%s",
5888 cur->apcupsd.items[APCUPSD_NAME]);
5890 OBJ(apcupsd_model) {
5891 snprintf(p, p_max_size, "%s",
5892 cur->apcupsd.items[APCUPSD_MODEL]);
5894 OBJ(apcupsd_upsmode) {
5895 snprintf(p, p_max_size, "%s",
5896 cur->apcupsd.items[APCUPSD_UPSMODE]);
5898 OBJ(apcupsd_cable) {
5899 snprintf(p, p_max_size, "%s",
5900 cur->apcupsd.items[APCUPSD_CABLE]);
5902 OBJ(apcupsd_status) {
5903 snprintf(p, p_max_size, "%s",
5904 cur->apcupsd.items[APCUPSD_STATUS]);
5906 OBJ(apcupsd_linev) {
5907 snprintf(p, p_max_size, "%s",
5908 cur->apcupsd.items[APCUPSD_LINEV]);
5911 snprintf(p, p_max_size, "%s",
5912 cur->apcupsd.items[APCUPSD_LOAD]);
5914 OBJ(apcupsd_loadbar) {
5917 if(output_methods & TO_X) {
5918 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5919 new_bar(p, obj->a, obj->b, (int) progress);
5922 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5923 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5924 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5930 OBJ(apcupsd_loadgraph) {
5932 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5933 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5934 (int)progress, 100, 1, obj->char_a, obj->char_b);
5936 OBJ(apcupsd_loadgauge) {
5938 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5939 new_gauge(p, obj->a, obj->b,
5943 OBJ(apcupsd_charge) {
5944 snprintf(p, p_max_size, "%s",
5945 cur->apcupsd.items[APCUPSD_CHARGE]);
5947 OBJ(apcupsd_timeleft) {
5948 snprintf(p, p_max_size, "%s",
5949 cur->apcupsd.items[APCUPSD_TIMELEFT]);
5952 snprintf(p, p_max_size, "%s",
5953 cur->apcupsd.items[APCUPSD_TEMP]);
5955 OBJ(apcupsd_lastxfer) {
5956 snprintf(p, p_max_size, "%s",
5957 cur->apcupsd.items[APCUPSD_LASTXFER]);
5959 #endif /* APCUPSD */
5966 unsigned int a = strlen(p);
5969 if (a > 0 && iconv_converting && iconv_selected > 0
5970 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5972 size_t dummy1, dummy2;
5974 const char *ptr = buff_in;
5976 char *ptr = buff_in;
5980 dummy1 = dummy2 = a;
5982 strncpy(buff_in, p, p_max_size);
5984 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5985 while (dummy1 > 0) {
5986 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5989 ERR("Iconv codeset conversion failed");
5994 /* It is nessecary when we are converting from multibyte to
5995 * singlebyte codepage */
5998 #endif /* HAVE_ICONV */
6005 /* load any new fonts we may have had */
6006 if (need_to_load_fonts) {
6012 void evaluate(char *text, char *buffer)
6014 struct information *tmp_info;
6015 struct text_object subroot;
6017 tmp_info = malloc(sizeof(struct information));
6018 memcpy(tmp_info, &info, sizeof(struct information));
6019 parse_conky_vars(&subroot, text, buffer, tmp_info);
6020 DBGP("evaluated '%s' to '%s'", text, buffer);
6022 free_text_objects(&subroot, 1);
6026 double current_update_time, next_update_time, last_update_time;
6028 static void generate_text(void)
6030 struct information *cur = &info;
6037 current_update_time = get_time();
6041 /* add things to the buffer */
6047 generate_text_internal(p, max_user_text, global_root_object, cur);
6049 if (stuff_in_upper_case) {
6052 tmp_p = text_buffer;
6054 *tmp_p = toupper(*tmp_p);
6059 next_update_time += update_interval;
6060 if (next_update_time < get_time()) {
6061 next_update_time = get_time() + update_interval;
6062 } else if (next_update_time > get_time() + update_interval) {
6063 next_update_time = get_time() + update_interval;
6065 last_update_time = current_update_time;
6070 static inline int get_string_width(const char *s)
6073 if (output_methods & TO_X) {
6074 return *s ? calc_text_width(s, strlen(s)) : 0;
6080 static inline int get_string_width_special(char *s)
6088 if ((output_methods & TO_X) == 0) {
6090 return (s) ? strlen(s) : 0;
6098 p = strndup(s, text_buffer_size);
6102 if (*p == SPECIAL_CHAR) {
6103 /* shift everything over by 1 so that the special char
6104 * doesn't mess up the size calculation */
6105 for (i = 0; i < (long)strlen(p); i++) {
6106 *(p + i) = *(p + i + 1);
6108 if (specials[special_index + idx].type == GRAPH
6109 || specials[special_index + idx].type == GAUGE
6110 || specials[special_index + idx].type == BAR) {
6111 width += specials[special_index + idx].width;
6118 if (strlen(final) > 1) {
6119 width += calc_text_width(final, strlen(final));
6127 static void text_size_updater(char *s);
6129 int last_font_height;
6130 static void update_text_area(void)
6134 if ((output_methods & TO_X) == 0)
6136 /* update text size if it isn't fixed */
6141 text_width = minimum_width;
6144 last_font_height = font_height();
6145 for_each_line(text_buffer, text_size_updater);
6147 if (text_height < minimum_height) {
6148 text_height = minimum_height;
6150 if (text_width > maximum_width && maximum_width > 0) {
6151 text_width = maximum_width;
6155 /* get text position on workarea */
6156 switch (text_alignment) {
6163 x = workarea[2] - text_width - gap_x;
6168 x = workarea[2] / 2 - text_width / 2 - gap_x;
6175 y = workarea[3] - text_height - gap_y;
6179 x = workarea[2] - text_width - gap_x;
6180 y = workarea[3] - text_height - gap_y;
6184 x = workarea[2] / 2 - text_width / 2 - gap_x;
6185 y = workarea[3] - text_height - gap_y;
6190 y = workarea[3] / 2 - text_height / 2 - gap_y;
6194 x = workarea[2] - text_width - gap_x;
6195 y = workarea[3] / 2 - text_height / 2 - gap_y;
6199 case NONE: // Let the WM manage the window
6210 if (own_window && !fixed_pos) {
6213 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6214 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6215 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6216 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6220 /* If window size doesn't match to workarea's size,
6221 * then window probably includes panels (gnome).
6222 * Blah, doesn't work on KDE. */
6223 if (workarea[2] != window.width || workarea[3] != window.height) {
6232 /* update lua window globals */
6233 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6234 #endif /* HAVE_LUA */
6239 static int cur_x, cur_y; /* current x and y for drawing */
6241 //draw_mode also without X11 because we only need to print to stdout with FG
6242 static int draw_mode; /* FG, BG or OUTLINE */
6244 static long current_color;
6246 static void text_size_updater(char *s)
6251 if ((output_methods & TO_X) == 0)
6253 /* get string widths and skip specials */
6256 if (*p == SPECIAL_CHAR) {
6258 w += get_string_width(s);
6261 if (specials[special_index].type == BAR
6262 || specials[special_index].type == GAUGE
6263 || specials[special_index].type == GRAPH) {
6264 w += specials[special_index].width;
6265 if (specials[special_index].height > last_font_height) {
6266 last_font_height = specials[special_index].height;
6267 last_font_height += font_height();
6269 } else if (specials[special_index].type == OFFSET) {
6270 if (specials[special_index].arg > 0) {
6271 w += specials[special_index].arg;
6273 } else if (specials[special_index].type == VOFFSET) {
6274 last_font_height += specials[special_index].arg;
6275 } else if (specials[special_index].type == GOTO) {
6276 if (specials[special_index].arg > cur_x) {
6277 w = (int) specials[special_index].arg;
6279 } else if (specials[special_index].type == TAB) {
6280 int start = specials[special_index].arg;
6281 int step = specials[special_index].width;
6283 if (!step || step < 0) {
6286 w += step - (cur_x - text_start_x - start) % step;
6287 } else if (specials[special_index].type == FONT) {
6288 selected_font = specials[special_index].font_added;
6289 if (font_height() > last_font_height) {
6290 last_font_height = font_height();
6299 w += get_string_width(s);
6300 if (w > text_width) {
6303 if (text_width > maximum_width && maximum_width) {
6304 text_width = maximum_width;
6307 text_height += last_font_height;
6308 last_font_height = font_height();
6311 static inline void set_foreground_color(long c)
6313 if ((output_methods & TO_X) == 0)
6316 XSetForeground(display, window.gc, c);
6320 static void draw_string(const char *s)
6322 int i, i2, pos, width_of_s;
6330 width_of_s = get_string_width(s);
6331 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6333 fflush(stdout); /* output immediately, don't buffer */
6335 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6336 fprintf(stderr, "%s\n", s);
6337 fflush(stderr); /* output immediately, don't buffer */
6339 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6340 fprintf(overwrite_fpointer, "%s\n", s);
6342 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6343 fprintf(append_fpointer, "%s\n", s);
6345 memset(tmpstring1, 0, text_buffer_size);
6346 memset(tmpstring2, 0, text_buffer_size);
6347 strncpy(tmpstring1, s, text_buffer_size - 1);
6352 if (output_methods & TO_X) {
6353 max = ((text_width - width_of_s) / get_string_width(" "));
6356 /* This code looks for tabs in the text and coverts them to spaces.
6357 * The trick is getting the correct number of spaces, and not going
6358 * over the window's size without forcing the window larger. */
6359 for (i = 0; i < (int) text_buffer_size; i++) {
6360 if (tmpstring1[i] == '\t') {
6362 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6363 /* guard against overrun */
6364 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6369 /* guard against overrun */
6370 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6375 if (output_methods & TO_X) {
6376 if (text_width == maximum_width) {
6377 /* this means the text is probably pushing the limit,
6378 * so we'll chop it */
6379 while (cur_x + get_string_width(tmpstring2) - text_start_x
6380 > maximum_width && strlen(tmpstring2) > 0) {
6381 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6388 if (output_methods & TO_X) {
6394 c.pixel = current_color;
6395 XQueryColor(display, DefaultColormap(display, screen), &c);
6398 c2.color.red = c.red;
6399 c2.color.green = c.green;
6400 c2.color.blue = c.blue;
6401 c2.color.alpha = fonts[selected_font].font_alpha;
6403 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6404 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6406 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6407 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6412 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6415 cur_x += width_of_s;
6418 memcpy(tmpstring1, s, text_buffer_size);
6421 static void draw_line(char *s)
6429 if ((output_methods & TO_X) == 0) {
6435 cur_x = text_start_x;
6436 cur_y += font_ascent();
6437 font_h = font_height();
6439 /* find specials and draw stuff */
6442 if (*p == SPECIAL_CHAR) {
6445 /* draw string before special */
6452 switch (specials[special_index].type) {
6453 case HORIZONTAL_LINE:
6455 int h = specials[special_index].height;
6456 int mid = font_ascent() / 2;
6458 w = text_start_x + text_width - cur_x;
6460 XSetLineAttributes(display, window.gc, h, LineSolid,
6461 CapButt, JoinMiter);
6462 XDrawLine(display, window.drawable, window.gc, cur_x,
6463 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6469 int h = specials[special_index].height;
6470 int tmp_s = specials[special_index].arg;
6471 int mid = font_ascent() / 2;
6472 char ss[2] = { tmp_s, tmp_s };
6474 w = text_start_x + text_width - cur_x - 1;
6475 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6476 CapButt, JoinMiter);
6477 XSetDashes(display, window.gc, 0, ss, 2);
6478 XDrawLine(display, window.drawable, window.gc, cur_x,
6479 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6485 int h, bar_usage, by;
6486 if (cur_x - text_start_x > maximum_width
6487 && maximum_width > 0) {
6490 h = specials[special_index].height;
6491 bar_usage = specials[special_index].arg;
6492 by = cur_y - (font_ascent() / 2) - 1;
6497 w = specials[special_index].width;
6499 w = text_start_x + text_width - cur_x - 1;
6505 XSetLineAttributes(display, window.gc, 1, LineSolid,
6506 CapButt, JoinMiter);
6508 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6510 XFillRectangle(display, window.drawable, window.gc, cur_x,
6511 by, w * bar_usage / 255, h);
6519 case GAUGE: /* new GAUGE */
6522 unsigned long last_colour = current_color;
6524 float angle, px, py;
6528 if (cur_x - text_start_x > maximum_width
6529 && maximum_width > 0) {
6533 h = specials[special_index].height;
6534 by = cur_y - (font_ascent() / 2) - 1;
6539 w = specials[special_index].width;
6541 w = text_start_x + text_width - cur_x - 1;
6547 XSetLineAttributes(display, window.gc, 1, LineSolid,
6548 CapButt, JoinMiter);
6550 XDrawArc(display, window.drawable, window.gc,
6551 cur_x, by, w, h * 2, 0, 180*64);
6554 usage = specials[special_index].arg;
6555 angle = (M_PI)*(float)(usage)/255.;
6556 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6557 py = (float)(by+(h))-(float)(h)*sin(angle);
6559 XDrawLine(display, window.drawable, window.gc,
6560 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6568 set_foreground_color(last_colour);
6576 int h, by, i = 0, j = 0;
6578 unsigned long last_colour = current_color;
6579 unsigned long *tmpcolour = 0;
6580 if (cur_x - text_start_x > maximum_width
6581 && maximum_width > 0) {
6584 h = specials[special_index].height;
6585 by = cur_y - (font_ascent() / 2) - 1;
6590 w = specials[special_index].width;
6592 w = text_start_x + text_width - cur_x - 1;
6597 if (draw_graph_borders) {
6598 XSetLineAttributes(display, window.gc, 1, LineSolid,
6599 CapButt, JoinMiter);
6600 XDrawRectangle(display, window.drawable, window.gc,
6603 XSetLineAttributes(display, window.gc, 1, LineSolid,
6604 CapButt, JoinMiter);
6606 if (specials[special_index].last_colour != 0
6607 || specials[special_index].first_colour != 0) {
6608 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6611 for (i = w - 2; i > -1; i--) {
6612 if (specials[special_index].last_colour != 0
6613 || specials[special_index].first_colour != 0) {
6614 if (specials[special_index].tempgrad) {
6617 (int)((float)(w - 2) - specials[special_index].graph[j] *
6618 (w - 2) / (float)specials[special_index].graph_scale)
6622 (int)((float)(w - 2) - specials[special_index].graph[j] *
6623 (w - 2) / (float)specials[special_index].graph_scale)
6626 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6628 (int)((float)(w - 2) - specials[special_index].graph[j] *
6629 (w - 2) / (float)specials[special_index].graph_scale)
6633 #endif /* DEBUG_lol */
6634 XSetForeground(display, window.gc, tmpcolour[
6635 (int)((float)(w - 2) - specials[special_index].graph[j] *
6636 (w - 2) / (float)specials[special_index].graph_scale)
6639 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6642 /* this is mugfugly, but it works */
6643 XDrawLine(display, window.drawable, window.gc,
6644 cur_x + i + 1, by + h, cur_x + i + 1,
6645 round_to_int((double)by + h - specials[special_index].graph[j] *
6646 (h - 1) / specials[special_index].graph_scale));
6647 if ((w - i) / ((float) (w - 2) /
6648 (specials[special_index].graph_width)) > j
6649 && j < MAX_GRAPH_DEPTH - 3) {
6653 if (tmpcolour) free(tmpcolour);
6658 /* if (draw_mode == BG) {
6659 set_foreground_color(default_bg_color);
6660 } else if (draw_mode == OUTLINE) {
6661 set_foreground_color(default_out_color);
6663 set_foreground_color(default_fg_color);
6665 if (show_graph_range) {
6668 unsigned short int seconds = update_interval * w;
6673 unsigned short int timeunits;
6675 timeunits = seconds / 86400; seconds %= 86400;
6676 if (timeunits > 0) {
6677 asprintf(&tmp_day_str, "%dd", timeunits);
6679 tmp_day_str = strdup("");
6681 timeunits = seconds / 3600; seconds %= 3600;
6682 if (timeunits > 0) {
6683 asprintf(&tmp_hour_str, "%dh", timeunits);
6685 tmp_hour_str = strdup("");
6687 timeunits = seconds / 60; seconds %= 60;
6688 if (timeunits > 0) {
6689 asprintf(&tmp_min_str, "%dm", timeunits);
6691 tmp_min_str = strdup("");
6694 asprintf(&tmp_sec_str, "%ds", seconds);
6696 tmp_sec_str = strdup("");
6698 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6699 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6701 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6703 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6704 cur_y += font_h / 2;
6705 draw_string(tmp_str);
6711 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6714 cur_x += font_ascent() / 2;
6715 cur_y += font_h / 2;
6717 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6719 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6720 draw_string(tmp_str);
6726 set_foreground_color(last_colour);
6732 int old = font_ascent();
6734 cur_y -= font_ascent();
6735 selected_font = specials[special_index].font_added;
6737 if (cur_y + font_ascent() < cur_y + old) {
6740 cur_y += font_ascent();
6742 font_h = font_height();
6746 if (draw_mode == FG) {
6747 set_foreground_color(specials[special_index].arg);
6752 if (draw_mode == BG) {
6753 set_foreground_color(specials[special_index].arg);
6758 if (draw_mode == OUTLINE) {
6759 set_foreground_color(specials[special_index].arg);
6764 w += specials[special_index].arg;
6768 cur_y += specials[special_index].arg;
6772 if (specials[special_index].arg >= 0) {
6773 cur_x = (int) specials[special_index].arg;
6779 int start = specials[special_index].arg;
6780 int step = specials[special_index].width;
6782 if (!step || step < 0) {
6785 w = step - (cur_x - text_start_x - start) % step;
6791 /* TODO: add back in "+ window.border_inner_margin" to the end of
6793 int pos_x = text_start_x + text_width -
6794 get_string_width_special(s);
6796 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6797 "get_string_width(p) %i gap_x %i "
6798 "specials[special_index].arg %i window.border_inner_margin %i "
6799 "window.border_width %i\n", pos_x, text_start_x, text_width,
6800 cur_x, get_string_width_special(s), gap_x,
6801 specials[special_index].arg, window.border_inner_margin,
6802 window.border_width); */
6803 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6804 cur_x = pos_x - specials[special_index].arg;
6811 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6812 2 - (cur_x - text_start_x);
6813 /* int pos_x = text_start_x + text_width / 2 -
6814 get_string_width_special(s) / 2; */
6816 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6817 "get_string_width(p) %i gap_x %i "
6818 "specials[special_index].arg %i\n", pos_x, text_start_x,
6819 text_width, cur_x, get_string_width(s), gap_x,
6820 specials[special_index].arg); */
6821 if (pos_x > specials[special_index].arg) {
6822 w = pos_x - specials[special_index].arg;
6836 if (cur_y_add > 0) {
6840 cur_y += font_descent();
6845 static void draw_text(void)
6849 llua_draw_pre_hook();
6850 #endif /* HAVE_LUA */
6851 if (output_methods & TO_X) {
6852 cur_y = text_start_y;
6855 if (draw_borders && window.border_width > 0) {
6856 if (stippled_borders) {
6857 char ss[2] = { stippled_borders, stippled_borders };
6858 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6859 CapButt, JoinMiter);
6860 XSetDashes(display, window.gc, 0, ss, 2);
6862 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6863 CapButt, JoinMiter);
6866 XDrawRectangle(display, window.drawable, window.gc,
6867 text_start_x - window.border_inner_margin - window.border_width,
6868 text_start_y - window.border_inner_margin - window.border_width,
6869 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6870 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6878 for_each_line(text_buffer, draw_line);
6879 #if defined(HAVE_LUA) && defined(X11)
6880 llua_draw_post_hook();
6881 #endif /* HAVE_LUA */
6884 static void draw_stuff(void)
6886 if (overwrite_file) {
6887 overwrite_fpointer = fopen(overwrite_file, "w");
6888 if(!overwrite_fpointer)
6889 ERR("Can't overwrite '%s' anymore", overwrite_file);
6892 append_fpointer = fopen(append_file, "a");
6893 if(!append_fpointer)
6894 ERR("Can't append '%s' anymore", append_file);
6897 if (output_methods & TO_X) {
6899 if (draw_shades && !draw_outline) {
6902 set_foreground_color(default_bg_color);
6913 for (i = -1; i < 2; i++) {
6914 for (j = -1; j < 2; j++) {
6915 if (i == 0 && j == 0) {
6920 set_foreground_color(default_out_color);
6921 draw_mode = OUTLINE;
6929 set_foreground_color(default_fg_color);
6935 if (output_methods & TO_X) {
6940 swap.swap_window = window.window;
6941 swap.swap_action = XdbeBackground;
6942 XdbeSwapBuffers(display, &swap, 1);
6947 if(overwrite_fpointer) {
6948 fclose(overwrite_fpointer);
6949 overwrite_fpointer = 0;
6951 if(append_fpointer) {
6952 fclose(append_fpointer);
6953 append_fpointer = 0;
6958 static void clear_text(int exposures)
6962 /* The swap action is XdbeBackground, which clears */
6966 if (display && window.window) { // make sure these are !null
6967 /* there is some extra space for borders and outlines */
6968 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
6969 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
6970 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
6971 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
6976 static int need_to_update;
6978 /* update_text() generates new text and clears old text area */
6979 static void update_text(void)
6986 if (output_methods & TO_X)
6992 #ifdef HAVE_SYS_INOTIFY_H
6996 static void main_loop(void)
6999 #ifdef SIGNAL_BLOCKING
7000 sigset_t newmask, oldmask;
7003 #ifdef HAVE_SYS_INOTIFY_H
7004 int inotify_config_wd = -1;
7005 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
7006 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
7007 char inotify_buff[INOTIFY_BUF_LEN];
7008 #endif /* HAVE_SYS_INOTIFY_H */
7011 #ifdef SIGNAL_BLOCKING
7012 sigemptyset(&newmask);
7013 sigaddset(&newmask, SIGINT);
7014 sigaddset(&newmask, SIGTERM);
7015 sigaddset(&newmask, SIGUSR1);
7018 last_update_time = 0.0;
7019 next_update_time = get_time();
7021 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7024 #ifdef SIGNAL_BLOCKING
7025 /* block signals. we will inspect for pending signals later */
7026 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7027 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7032 if (output_methods & TO_X) {
7035 /* wait for X event or timeout */
7037 if (!XPending(display)) {
7041 t = next_update_time - get_time();
7045 } else if (t > update_interval) {
7046 t = update_interval;
7049 tv.tv_sec = (long) t;
7050 tv.tv_usec = (long) (t * 1000000) % 1000000;
7052 FD_SET(ConnectionNumber(display), &fdsr);
7054 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7056 if (errno != EINTR) {
7057 ERR("can't select(): %s", strerror(errno));
7067 if (need_to_update) {
7069 int wx = window.x, wy = window.y;
7079 /* resize window if it isn't right size */
7081 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7082 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7083 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7084 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7085 XResizeWindow(display, window.window, window.width,
7087 set_transparent_background(window.window);
7092 /* move window if it isn't in right position */
7093 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7094 XMoveWindow(display, window.window, window.x, window.y);
7099 if (changed && window.type == TYPE_PANEL) {
7102 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7105 switch (text_alignment) {
7132 set_struts(sidenum);
7143 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7144 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7145 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7146 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7147 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7152 /* handle X events */
7153 while (XPending(display)) {
7156 XNextEvent(display, &ev);
7163 r.width = ev.xexpose.width;
7164 r.height = ev.xexpose.height;
7165 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7169 case PropertyNotify:
7171 if ( ev.xproperty.state == PropertyNewValue ) {
7172 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7178 case ReparentNotify:
7179 /* set background to ParentRelative for all parents */
7181 set_transparent_background(window.window);
7185 case ConfigureNotify:
7187 /* if window size isn't what expected, set fixed size */
7188 if (ev.xconfigure.width != window.width
7189 || ev.xconfigure.height != window.height) {
7190 if (window.width != 0 && window.height != 0) {
7194 /* clear old stuff before screwing up
7199 XWindowAttributes attrs;
7200 if (XGetWindowAttributes(display,
7201 window.window, &attrs)) {
7202 window.width = attrs.width;
7203 window.height = attrs.height;
7207 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7208 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7209 if (text_width > maximum_width
7210 && maximum_width > 0) {
7211 text_width = maximum_width;
7215 /* if position isn't what expected, set fixed pos
7216 * total_updates avoids setting fixed_pos when window
7217 * is set to weird locations when started */
7218 /* // this is broken
7219 if (total_updates >= 2 && !fixed_pos
7220 && (window.x != ev.xconfigure.x
7221 || window.y != ev.xconfigure.y)
7222 && (ev.xconfigure.x != 0
7223 || ev.xconfigure.y != 0)) {
7231 /* if an ordinary window with decorations */
7232 if ((window.type == TYPE_NORMAL)
7233 && (!TEST_HINT(window.hints,
7234 HINT_UNDECORATED))) {
7235 /* allow conky to hold input focus. */
7238 /* forward the click to the desktop window */
7239 XUngrabPointer(display, ev.xbutton.time);
7240 ev.xbutton.window = window.desktop;
7241 ev.xbutton.x = ev.xbutton.x_root;
7242 ev.xbutton.y = ev.xbutton.y_root;
7243 XSendEvent(display, ev.xbutton.window, False,
7244 ButtonPressMask, &ev);
7245 XSetInputFocus(display, ev.xbutton.window,
7246 RevertToParent, ev.xbutton.time);
7253 /* if an ordinary window with decorations */
7254 if ((window.type == TYPE_NORMAL)
7255 && (!TEST_HINT(window.hints,
7256 HINT_UNDECORATED))) {
7257 /* allow conky to hold input focus. */
7260 /* forward the release to the desktop window */
7261 ev.xbutton.window = window.desktop;
7262 ev.xbutton.x = ev.xbutton.x_root;
7263 ev.xbutton.y = ev.xbutton.y_root;
7264 XSendEvent(display, ev.xbutton.window, False,
7265 ButtonReleaseMask, &ev);
7274 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7275 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7277 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7278 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7280 #endif /* HAVE_XDAMAGE */
7286 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7287 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7288 #endif /* HAVE_XDAMAGE */
7290 /* XDBE doesn't seem to provide a way to clear the back buffer without
7291 * interfering with the front buffer, other than passing XdbeBackground
7292 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7293 * redraw the text even if it wasn't part of the exposed area. OTOH,
7294 * if we're not going to call draw_stuff at all, then no swap happens
7295 * and we can safely do nothing. */
7297 if (!XEmptyRegion(x11_stuff.region)) {
7302 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7303 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7304 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7305 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7306 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7309 XSetRegion(display, window.gc, x11_stuff.region);
7312 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7316 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7319 XDestroyRegion(x11_stuff.region);
7320 x11_stuff.region = XCreateRegion();
7324 t = (next_update_time - get_time()) * 1000000;
7325 if(t > 0) usleep((useconds_t)t);
7332 #ifdef SIGNAL_BLOCKING
7333 /* unblock signals of interest and let handler fly */
7334 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7335 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7339 switch (g_signal_pending) {
7342 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7347 ERR("received SIGINT or SIGTERM to terminate. bye!");
7350 if (output_methods & TO_X) {
7351 XDestroyRegion(x11_stuff.region);
7352 x11_stuff.region = NULL;
7354 XDamageDestroy(display, x11_stuff.damage);
7355 XFixesDestroyRegion(display, x11_stuff.region2);
7356 XFixesDestroyRegion(display, x11_stuff.part);
7357 #endif /* HAVE_XDAMAGE */
7363 if(overwrite_file) {
7364 free(overwrite_file);
7373 /* Reaching here means someone set a signal
7374 * (SIGXXXX, signal_handler), but didn't write any code
7376 * If you don't want to handle a signal, don't set a handler on
7377 * it in the first place. */
7378 if (g_signal_pending) {
7379 ERR("ignoring signal (%d)", g_signal_pending);
7383 #ifdef HAVE_SYS_INOTIFY_H
7384 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7385 inotify_config_wd = inotify_add_watch(inotify_fd,
7389 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7390 int len = 0, idx = 0;
7392 struct timeval time_to_wait;
7394 FD_ZERO(&descriptors);
7395 FD_SET(inotify_fd, &descriptors);
7397 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7399 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7400 if (FD_ISSET(inotify_fd, &descriptors)) {
7401 /* process inotify events */
7402 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7403 while (len > 0 && idx < len) {
7404 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7405 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7406 /* current_config should be reloaded */
7407 ERR("'%s' modified, reloading...", current_config);
7409 if (ev->mask & IN_IGNORED) {
7410 /* for some reason we get IN_IGNORED here
7411 * sometimes, so we need to re-add the watch */
7412 inotify_config_wd = inotify_add_watch(inotify_fd,
7419 llua_inotify_query(ev->wd, ev->mask);
7421 #endif /* HAVE_LUA */
7422 idx += INOTIFY_EVENT_SIZE + ev->len;
7426 #endif /* HAVE_SYS_INOTIFY_H */
7428 g_signal_pending = 0;
7430 clean_up(NULL, NULL);
7432 #ifdef HAVE_SYS_INOTIFY_H
7433 if (inotify_fd != -1) {
7434 inotify_rm_watch(inotify_fd, inotify_config_wd);
7436 inotify_fd = inotify_config_wd = 0;
7438 #endif /* HAVE_SYS_INOTIFY_H */
7441 static void load_config_file(const char *);
7443 static void load_config_file_x11(const char *);
7445 void initialisation(int argc, char** argv);
7447 /* reload the config file */
7448 static void reload_config(void)
7450 char *current_config_copy = strdup(current_config);
7451 clean_up(NULL, NULL);
7452 current_config = current_config_copy;
7453 initialisation(argc_copy, argv_copy);
7456 void clean_up(void *memtofree1, void* memtofree2)
7465 timed_thread_destroy_registered_threads();
7467 if (info.cpu_usage) {
7468 free(info.cpu_usage);
7469 info.cpu_usage = NULL;
7472 if (x_initialised == YES) {
7475 if(x11_stuff.region) {
7476 XDestroyRegion(x11_stuff.region);
7477 x11_stuff.region = NULL;
7479 XClearWindow(display, RootWindow(display, screen));
7480 XCloseDisplay(display);
7482 if(info.x11.desktop.all_names) {
7483 free(info.x11.desktop.all_names);
7484 info.x11.desktop.all_names = NULL;
7486 if (info.x11.desktop.name) {
7487 free(info.x11.desktop.name);
7488 info.x11.desktop.name = NULL;
7492 free(fonts); //in set_default_configurations a font is set but not loaded
7498 for (i = 0; i < MAX_TEMPLATES; i++) {
7505 free_text_objects(&global_root_object, 0);
7524 free(current_config);
7527 #ifdef TCP_PORT_MONITOR
7528 tcp_portmon_clear();
7534 free_weather_info();
7538 #endif /* HAVE_LUA */
7541 for (i = 0; i < special_count; i++) {
7542 if (specials[i].type == GRAPH) {
7543 free(specials[i].graph);
7551 clear_diskio_stats();
7552 if(global_cpu != NULL) {
7558 static int string_to_bool(const char *s)
7561 // Assumes an option without a true/false means true
7563 } else if (strcasecmp(s, "yes") == EQUAL) {
7565 } else if (strcasecmp(s, "true") == EQUAL) {
7567 } else if (strcasecmp(s, "1") == EQUAL) {
7574 static enum alignment string_to_alignment(const char *s)
7576 if (strcasecmp(s, "top_left") == EQUAL) {
7578 } else if (strcasecmp(s, "top_right") == EQUAL) {
7580 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7582 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7584 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7585 return BOTTOM_RIGHT;
7586 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7587 return BOTTOM_MIDDLE;
7588 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7590 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7591 return MIDDLE_RIGHT;
7592 } else if (strcasecmp(s, "tl") == EQUAL) {
7594 } else if (strcasecmp(s, "tr") == EQUAL) {
7596 } else if (strcasecmp(s, "tm") == EQUAL) {
7598 } else if (strcasecmp(s, "bl") == EQUAL) {
7600 } else if (strcasecmp(s, "br") == EQUAL) {
7601 return BOTTOM_RIGHT;
7602 } else if (strcasecmp(s, "bm") == EQUAL) {
7603 return BOTTOM_MIDDLE;
7604 } else if (strcasecmp(s, "ml") == EQUAL) {
7606 } else if (strcasecmp(s, "mr") == EQUAL) {
7607 return MIDDLE_RIGHT;
7608 } else if (strcasecmp(s, "none") == EQUAL) {
7616 static void set_default_configurations_for_x(void)
7618 default_fg_color = WhitePixel(display, screen);
7619 default_bg_color = BlackPixel(display, screen);
7620 default_out_color = BlackPixel(display, screen);
7621 color0 = default_fg_color;
7622 color1 = default_fg_color;
7623 color2 = default_fg_color;
7624 color3 = default_fg_color;
7625 color4 = default_fg_color;
7626 color5 = default_fg_color;
7627 color6 = default_fg_color;
7628 color7 = default_fg_color;
7629 color8 = default_fg_color;
7630 color9 = default_fg_color;
7634 static void set_default_configurations(void)
7642 fork_to_background = 0;
7643 total_run_times = 0;
7644 info.cpu_avg_samples = 2;
7645 info.net_avg_samples = 2;
7646 info.diskio_avg_samples = 2;
7651 format_human_readable = 1;
7658 mpd_env_host = getenv("MPD_HOST");
7659 mpd_env_port = getenv("MPD_PORT");
7661 if (!mpd_env_host || !strlen(mpd_env_host)) {
7662 mpd_set_host("localhost");
7664 /* MPD_HOST environment variable is set */
7665 char *mpd_hostpart = strchr(mpd_env_host, '@');
7666 if (!mpd_hostpart) {
7667 mpd_set_host(mpd_env_host);
7669 /* MPD_HOST contains a password */
7670 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7671 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7673 if (!strlen(mpd_hostpart + 1)) {
7674 mpd_set_host("localhost");
7676 mpd_set_host(mpd_hostpart + 1);
7679 mpd_set_password(mpd_password, 1);
7684 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7685 /* failed to set port from environment variable */
7686 mpd_set_port("6600");
7690 info.xmms2.artist = NULL;
7691 info.xmms2.album = NULL;
7692 info.xmms2.title = NULL;
7693 info.xmms2.genre = NULL;
7694 info.xmms2.comment = NULL;
7695 info.xmms2.url = NULL;
7696 info.xmms2.status = NULL;
7697 info.xmms2.playlist = NULL;
7699 use_spacer = NO_SPACER;
7701 output_methods = TO_X;
7703 output_methods = TO_STDOUT;
7706 show_graph_scale = 0;
7707 show_graph_range = 0;
7710 draw_graph_borders = 1;
7712 set_first_font("6x10");
7720 window.type = TYPE_NORMAL;
7722 strcpy(window.class_name, PACKAGE_NAME);
7723 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7725 stippled_borders = 0;
7726 window.border_inner_margin = 3;
7727 window.border_outer_margin = 1;
7728 window.border_width = 1;
7729 text_alignment = BOTTOM_LEFT;
7730 info.x11.monitor.number = 1;
7731 info.x11.monitor.current = 0;
7732 info.x11.desktop.current = 1;
7733 info.x11.desktop.number = 1;
7734 info.x11.desktop.nitems = 0;
7735 info.x11.desktop.all_names = NULL;
7736 info.x11.desktop.name = NULL;
7739 for (i = 0; i < MAX_TEMPLATES; i++) {
7742 template[i] = strdup("");
7745 free(current_mail_spool);
7749 variable_substitute(MAIL_FILE, buf, 256);
7750 if (buf[0] != '\0') {
7751 current_mail_spool = strndup(buf, text_buffer_size);
7756 update_interval = 3.0;
7757 info.music_player_interval = 1.0;
7758 stuff_in_upper_case = 0;
7759 info.users.number = 1;
7761 #ifdef TCP_PORT_MONITOR
7762 /* set default connection limit */
7763 tcp_portmon_set_max_connections(0);
7767 /* returns 1 if you can overwrite or create the file at 'path' */
7768 static _Bool overwrite_works(const char *path)
7772 if (!(filepointer = fopen(path, "w")))
7774 fclose(filepointer);
7778 /* returns 1 if you can append or create the file at 'path' */
7779 static _Bool append_works(const char *path)
7783 if (!(filepointer = fopen(path, "a")))
7785 fclose(filepointer);
7791 /* WARNING, this type not in Xlib spec */
7792 int x11_error_handler(Display *d, XErrorEvent *err)
7793 __attribute__((noreturn));
7794 int x11_error_handler(Display *d, XErrorEvent *err)
7796 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7798 (long unsigned)err->display,
7799 (long)err->resourceid,
7809 int x11_ioerror_handler(Display *d)
7810 __attribute__((noreturn));
7811 int x11_ioerror_handler(Display *d)
7813 ERR("X Error: Display %lx\n",
7820 static void X11_initialisation(void)
7822 if (x_initialised == YES) return;
7823 output_methods |= TO_X;
7825 set_default_configurations_for_x();
7826 x_initialised = YES;
7829 /* WARNING, this type not in Xlib spec */
7830 XSetErrorHandler(&x11_error_handler);
7831 XSetIOErrorHandler(&x11_ioerror_handler);
7835 static char **xargv = 0;
7836 static int xargc = 0;
7838 static void X11_create_window(void)
7840 if (output_methods & TO_X) {
7842 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7843 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7845 #else /* OWN_WINDOW */
7846 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7847 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7849 #endif /* OWN_WINDOW */
7853 update_text_area(); /* to position text/window on screen */
7856 if (own_window && !fixed_pos) {
7857 XMoveWindow(display, window.window, window.x, window.y);
7860 set_transparent_background(window.window);
7868 x11_stuff.region = XCreateRegion();
7870 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7871 ERR("Xdamage extension unavailable");
7873 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7874 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7875 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7876 #endif /* HAVE_XDAMAGE */
7879 update_text_area(); /* to get initial size of the window */
7882 /* setup lua window globals */
7883 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
7884 #endif /* HAVE_LUA */
7888 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7889 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7890 #define CONF2(a) if (strcasecmp(name, a) == 0)
7891 #define CONF(a) else CONF2(a)
7892 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7893 || strcasecmp(name, b) == 0)
7894 #define CONF_CONTINUE 1
7895 #define CONF_BREAK 2
7896 #define CONF_BUFF_SIZE 512
7898 static FILE *open_config_file(const char *f)
7900 #ifdef CONFIG_OUTPUT
7901 if (!strcmp(f, "==builtin==")) {
7902 return conf_cookie_open();
7904 #endif /* CONFIG_OUTPUT */
7905 return fopen(f, "r");
7908 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7912 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7915 remove_comments(buf);
7920 while (*p && isspace((int) *p)) {
7924 return CONF_CONTINUE; /* empty line */
7931 while (*p2 && !isspace((int) *p2)) {
7935 *p2 = '\0'; /* break at name's end */
7942 while (*p && isspace((int) *p)) {
7948 p2 = *value + strlen(*value);
7949 while (isspace((int) *(p2 - 1))) {
7958 static void load_config_file(const char *f)
7963 set_default_configurations();
7964 fp = open_config_file(f);
7968 DBGP("reading contents from config file '%s'", f);
7971 char buff[CONF_BUFF_SIZE], *name, *value;
7972 int ret = do_config_step(&line, fp, buff, &name, &value);
7973 if (ret == CONF_BREAK) {
7975 } else if (ret == CONF_CONTINUE) {
7981 /* don't listen if X is already initialised or
7982 * if we already know we don't want it */
7983 if(x_initialised != YES) {
7984 if (string_to_bool(value)) {
7985 output_methods &= TO_X;
7987 output_methods &= ~TO_X;
7988 x_initialised = NEVER;
7993 if (!value || x_initialised == YES) {
7998 disp = strdup(value);
8003 if (window.type == TYPE_DOCK)
8006 #endif /*OWN_WINDOW */
8008 int a = string_to_alignment(value);
8019 CONF("background") {
8020 fork_to_background = string_to_bool(value);
8023 CONF2("background") {
8024 fork_to_background = string_to_bool(value);
8028 CONF("show_graph_scale") {
8029 show_graph_scale = string_to_bool(value);
8031 CONF("show_graph_range") {
8032 show_graph_range = string_to_bool(value);
8034 CONF("border_margin") {
8035 ERR("border_margin is deprecated, please use window.border_inner_margin instead");
8037 window.border_inner_margin = strtol(value, 0, 0);
8038 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8043 CONF("border_inner_margin") {
8045 window.border_inner_margin = strtol(value, 0, 0);
8046 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8051 CONF("border_outer_margin") {
8053 window.border_outer_margin = strtol(value, 0, 0);
8054 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8059 CONF("border_width") {
8061 window.border_width = strtol(value, 0, 0);
8062 if (window.border_width < 0) window.border_width = 0;
8068 #define TEMPLATE_CONF(n) \
8069 CONF("template"#n) { \
8071 free(template[n]); \
8072 template[n] = strdup(value); \
8089 info.mail = parse_mail_args(IMAP_TYPE, value);
8096 info.mail = parse_mail_args(POP3_TYPE, value);
8101 CONF("default_bar_size") {
8104 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8111 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8115 CONF("default_graph_size") {
8118 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8125 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8128 CONF("default_gauge_size") {
8131 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8138 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8145 mpd_set_host(value);
8151 if (value && mpd_set_port(value)) {
8155 CONF("mpd_password") {
8157 mpd_set_password(value, 0);
8163 CONF("music_player_interval") {
8165 info.music_player_interval = strtod(value, 0);
8171 CONF("sensor_device") {
8173 sensor_device = strtol(value, 0, 0);
8179 CONF("cpu_avg_samples") {
8181 cpu_avg_samples = strtol(value, 0, 0);
8182 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8185 info.cpu_avg_samples = cpu_avg_samples;
8191 CONF("net_avg_samples") {
8193 net_avg_samples = strtol(value, 0, 0);
8194 if (net_avg_samples < 1 || net_avg_samples > 14) {
8197 info.net_avg_samples = net_avg_samples;
8203 CONF("diskio_avg_samples") {
8205 diskio_avg_samples = strtol(value, 0, 0);
8206 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8209 info.diskio_avg_samples = diskio_avg_samples;
8217 CONF("double_buffer") {
8218 use_xdbe = string_to_bool(value);
8222 CONF("override_utf8_locale") {
8223 utf8_mode = string_to_bool(value);
8225 CONF("draw_borders") {
8226 draw_borders = string_to_bool(value);
8228 CONF("draw_graph_borders") {
8229 draw_graph_borders = string_to_bool(value);
8231 CONF("draw_shades") {
8232 draw_shades = string_to_bool(value);
8234 CONF("draw_outline") {
8235 draw_outline = string_to_bool(value);
8238 CONF("out_to_console") {
8239 if(string_to_bool(value))
8240 output_methods |= TO_STDOUT;
8242 CONF("out_to_stderr") {
8243 if(string_to_bool(value))
8244 output_methods |= TO_STDERR;
8246 CONF("overwrite_file") {
8247 if(overwrite_file) {
8248 free(overwrite_file);
8251 if(overwrite_works(value)) {
8252 overwrite_file = strdup(value);
8253 output_methods |= OVERWRITE_FILE;
8255 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8257 CONF("append_file") {
8262 if(append_works(value)) {
8263 append_file = strdup(value);
8264 output_methods |= APPEND_FILE;
8266 ERR("append_file won't be able to create/append '%s'", value);
8268 CONF("use_spacer") {
8270 if (strcasecmp(value, "left") == EQUAL) {
8271 use_spacer = LEFT_SPACER;
8272 } else if (strcasecmp(value, "right") == EQUAL) {
8273 use_spacer = RIGHT_SPACER;
8274 } else if (strcasecmp(value, "none") == EQUAL) {
8275 use_spacer = NO_SPACER;
8277 use_spacer = string_to_bool(value);
8278 ERR("use_spacer should have an argument of left, right, or"
8279 " none. '%s' seems to be some form of '%s', so"
8280 " defaulting to %s.", value,
8281 use_spacer ? "true" : "false",
8282 use_spacer ? "right" : "none");
8284 use_spacer = RIGHT_SPACER;
8286 use_spacer = NO_SPACER;
8290 ERR("use_spacer should have an argument. Defaulting to right.");
8291 use_spacer = RIGHT_SPACER;
8297 use_xft = string_to_bool(value);
8301 set_first_font(value);
8305 if (value && font_count >= 0) {
8306 fonts[0].font_alpha = atof(value) * 65535.0;
8313 if (string_to_bool(value)) {
8314 ERR("Xft not enabled at compile time");
8318 /* xftfont silently ignored when no Xft */
8321 /* xftalpha is silently ignored when no Xft */
8326 set_first_font(value);
8334 gap_x = atoi(value);
8341 gap_y = atoi(value);
8347 CONF("mail_spool") {
8351 variable_substitute(value, buffer, 256);
8353 if (buffer[0] != '\0') {
8354 if (current_mail_spool) {
8355 free(current_mail_spool);
8357 current_mail_spool = strndup(buffer, text_buffer_size);
8364 CONF("minimum_size") {
8366 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8368 if (sscanf(value, "%d", &minimum_width) != 1) {
8376 CONF("maximum_width") {
8378 if (sscanf(value, "%d", &maximum_width) != 1) {
8386 CONF("no_buffers") {
8387 no_buffers = string_to_bool(value);
8389 CONF("top_name_width") {
8391 if (sscanf(value, "%u", &top_name_width) != 1) {
8397 if (top_name_width >= max_user_text) {
8398 top_name_width = max_user_text - 1;
8401 CONF("top_cpu_separate") {
8402 cpu_separate = string_to_bool(value);
8404 CONF("short_units") {
8405 short_units = string_to_bool(value);
8407 CONF("format_human_readable") {
8408 format_human_readable = string_to_bool(value);
8410 CONF("pad_percents") {
8411 pad_percents = atoi(value);
8415 CONF("own_window") {
8417 own_window = string_to_bool(value);
8420 CONF("own_window_class") {
8422 memset(window.class_name, 0, sizeof(window.class_name));
8423 strncpy(window.class_name, value,
8424 sizeof(window.class_name) - 1);
8427 CONF("own_window_title") {
8429 memset(window.title, 0, sizeof(window.title));
8430 strncpy(window.title, value, sizeof(window.title) - 1);
8433 CONF("own_window_transparent") {
8435 set_transparent = string_to_bool(value);
8438 CONF("own_window_hints") {
8440 char *p_hint, *p_save;
8441 char delim[] = ", ";
8443 /* tokenize the value into individual hints */
8444 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8446 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8447 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8448 SET_HINT(window.hints, HINT_UNDECORATED);
8449 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8450 SET_HINT(window.hints, HINT_BELOW);
8451 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8452 SET_HINT(window.hints, HINT_ABOVE);
8453 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8454 SET_HINT(window.hints, HINT_STICKY);
8455 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8456 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8457 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8458 SET_HINT(window.hints, HINT_SKIP_PAGER);
8463 p_hint = strtok_r(NULL, delim, &p_save);
8464 } while (p_hint != NULL);
8470 CONF("own_window_type") {
8472 if (strncmp(value, "normal", 6) == EQUAL) {
8473 window.type = TYPE_NORMAL;
8474 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8475 window.type = TYPE_DESKTOP;
8476 } else if (strncmp(value, "dock", 4) == EQUAL) {
8477 window.type = TYPE_DOCK;
8478 text_alignment = TOP_LEFT;
8479 } else if (strncmp(value, "panel", 5) == EQUAL) {
8480 window.type = TYPE_PANEL;
8481 } else if (strncmp(value, "override", 8) == EQUAL) {
8482 window.type = TYPE_OVERRIDE;
8491 CONF("stippled_borders") {
8493 stippled_borders = strtol(value, 0, 0);
8495 stippled_borders = 4;
8499 CONF("imlib_cache_size") {
8501 cimlib_set_cache_size(atoi(value));
8504 CONF("imlib_cache_flush_interval") {
8506 cimlib_set_cache_flush_interval(atoi(value));
8511 CONF("update_interval") {
8513 update_interval = strtod(value, 0);
8517 if (info.music_player_interval == 0) {
8518 // default to update_interval
8519 info.music_player_interval = update_interval;
8522 CONF("total_run_times") {
8524 total_run_times = strtod(value, 0);
8530 stuff_in_upper_case = string_to_bool(value);
8532 CONF("max_specials") {
8534 max_specials = atoi(value);
8539 CONF("max_user_text") {
8541 max_user_text = atoi(value);
8546 CONF("text_buffer_size") {
8548 text_buffer_size = atoi(value);
8549 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8550 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8551 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8559 if (output_methods & TO_X) {
8560 X11_initialisation();
8569 global_text = (char *) malloc(1);
8570 global_text[0] = '\0';
8573 unsigned int l = strlen(global_text);
8575 char buf[CONF_BUFF_SIZE];
8577 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8583 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8591 /* Check for continuation of \\-\n. */
8592 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8593 global_text[l-1] = '\0';
8597 global_text = (char *) realloc(global_text, l + bl + 1);
8598 strcat(global_text, buf);
8600 if (strlen(global_text) > max_user_text) {
8605 if (strlen(global_text) < 1) {
8606 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
8608 global_text_lines = line + 1;
8611 #ifdef TCP_PORT_MONITOR
8612 CONF("max_port_monitor_connections") {
8614 if (!value || (sscanf(value, "%d", &max) != 1)) {
8615 /* an error. use default, warn and continue. */
8616 tcp_portmon_set_max_connections(0);
8618 } else if (tcp_portmon_set_max_connections(max)) {
8619 /* max is < 0, default has been set*/
8624 CONF("if_up_strictness") {
8626 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8627 ifup_strictness = IFUP_UP;
8628 } else if (strcasecmp(value, "up") == EQUAL) {
8629 ifup_strictness = IFUP_UP;
8630 } else if (strcasecmp(value, "link") == EQUAL) {
8631 ifup_strictness = IFUP_LINK;
8632 } else if (strcasecmp(value, "address") == EQUAL) {
8633 ifup_strictness = IFUP_ADDR;
8635 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8636 ifup_strictness = IFUP_UP;
8640 CONF("temperature_unit") {
8642 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8643 } else if (set_temp_output_unit(value)) {
8644 ERR("temperature_unit: incorrect argument");
8650 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8651 char *skey = malloc(maxlength);
8652 char *svalue = malloc(maxlength);
8654 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8655 oldvalue = getenv(skey);
8656 if (oldvalue == NULL) {
8657 setenv(skey, svalue, 0);
8659 //PS: Don't free oldvalue, it's the real envvar, not a copy
8672 char *ptr = strtok(value, " ");
8675 ptr = strtok(NULL, " ");
8682 CONF("lua_draw_hook_pre") {
8684 llua_set_draw_pre_hook(value);
8689 CONF("lua_draw_hook_post") {
8691 llua_set_draw_post_hook(value);
8697 #endif /* HAVE_LUA */
8709 CONF("default_color"){}
8710 CONF3("default_shade_color", "default_shadecolor"){}
8711 CONF3("default_outline_color", "default_outlinecolor") {}
8712 CONF("own_window_colour") {}
8715 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8721 if (info.music_player_interval == 0) {
8722 // default to update_interval
8723 info.music_player_interval = update_interval;
8725 if (!global_text) { // didn't supply any text
8726 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
8731 static void load_config_file_x11(const char *f)
8736 fp = open_config_file(f);
8740 DBGP("reading contents from config file '%s'", f);
8743 char buff[CONF_BUFF_SIZE], *name, *value;
8744 int ret = do_config_step(&line, fp, buff, &name, &value);
8745 if (ret == CONF_BREAK) {
8747 } else if (ret == CONF_CONTINUE) {
8752 X11_initialisation();
8753 if (x_initialised == YES) {
8755 color0 = get_x11_color(value);
8762 X11_initialisation();
8763 if (x_initialised == YES) {
8765 color1 = get_x11_color(value);
8772 X11_initialisation();
8773 if (x_initialised == YES) {
8775 color2 = get_x11_color(value);
8782 X11_initialisation();
8783 if (x_initialised == YES) {
8785 color3 = get_x11_color(value);
8792 X11_initialisation();
8793 if (x_initialised == YES) {
8795 color4 = get_x11_color(value);
8802 X11_initialisation();
8803 if (x_initialised == YES) {
8805 color5 = get_x11_color(value);
8812 X11_initialisation();
8813 if (x_initialised == YES) {
8815 color6 = get_x11_color(value);
8822 X11_initialisation();
8823 if (x_initialised == YES) {
8825 color7 = get_x11_color(value);
8832 X11_initialisation();
8833 if (x_initialised == YES) {
8835 color8 = get_x11_color(value);
8842 X11_initialisation();
8843 if (x_initialised == YES) {
8845 color9 = get_x11_color(value);
8851 CONF("default_color") {
8852 X11_initialisation();
8853 if (x_initialised == YES) {
8855 default_fg_color = get_x11_color(value);
8861 CONF3("default_shade_color", "default_shadecolor") {
8862 X11_initialisation();
8863 if (x_initialised == YES) {
8865 default_bg_color = get_x11_color(value);
8871 CONF3("default_outline_color", "default_outlinecolor") {
8872 X11_initialisation();
8873 if (x_initialised == YES) {
8875 default_out_color = get_x11_color(value);
8882 CONF("own_window_colour") {
8883 X11_initialisation();
8884 if (x_initialised == YES) {
8886 background_colour = get_x11_color(value);
8888 ERR("Invalid colour for own_window_colour (try omitting the "
8889 "'#' for hex colours");
8895 //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
8896 if(output_methods & TO_X) {
8897 X11_initialisation();
8906 #undef CONF_CONTINUE
8907 #undef CONF_BUFF_SIZE
8916 * TODO: make the xoap keys file readable from the config file
8917 * make the keys directly readable from the config file
8918 * make the xoap keys file giveable as a command line option
8920 static void load_xoap_keys(void) {
8922 char *par = (char *) malloc(11 * sizeof(char));
8923 char *key = (char *) malloc(17 * sizeof(char));
8925 xoap = (char *) malloc(64 * sizeof(char));
8926 to_real_path(xoap, XOAP_FILE);
8927 fp = fopen(xoap, "r");
8929 if( fscanf(fp, "%10s %16s", par, key) == 2 ) {
8930 strcpy(xoap, "?cc=*&link=xoap&prod=xoap&par=");
8932 strcat(xoap, "&key=");
8934 strcat(xoap, "&unit=m");
8948 static void print_help(const char *prog_name) {
8949 printf("Usage: %s [OPTION]...\n"
8950 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8951 "window. Command line options will override configurations defined in config\n"
8953 " -v, --version version\n"
8954 " -q, --quiet quiet mode\n"
8955 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
8956 " -c, --config=FILE config file to load\n"
8957 #ifdef CONFIG_OUTPUT
8958 " -C, --print-config print the builtin default config to stdout\n"
8959 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
8961 " -d, --daemonize daemonize, fork to background\n"
8962 " -h, --help help\n"
8964 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8965 " -f, --font=FONT font to use\n"
8966 " -X, --display=DISPLAY X11 display to use\n"
8968 " -o, --own-window create own window to draw\n"
8971 " -b, --double-buffer double buffer (prevents flickering)\n"
8973 " -w, --window-id=WIN_ID window id to draw\n"
8974 " -x X x position\n"
8975 " -y Y y position\n"
8977 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8978 " -u, --interval=SECS update interval\n"
8979 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8984 /* : means that character before that takes an argument */
8985 static const char *getopt_string = "vVqdDt:u:i:hc:"
8995 #ifdef CONFIG_OUTPUT
9000 static const struct option longopts[] = {
9001 { "help", 0, NULL, 'h' },
9002 { "version", 0, NULL, 'V' },
9003 { "debug", 0, NULL, 'D' },
9004 { "config", 1, NULL, 'c' },
9005 #ifdef CONFIG_OUTPUT
9006 { "print-config", 0, NULL, 'C' },
9008 { "daemonize", 0, NULL, 'd' },
9010 { "alignment", 1, NULL, 'a' },
9011 { "font", 1, NULL, 'f' },
9012 { "display", 1, NULL, 'X' },
9014 { "own-window", 0, NULL, 'o' },
9017 { "double-buffer", 0, NULL, 'b' },
9019 { "window-id", 1, NULL, 'w' },
9021 { "text", 1, NULL, 't' },
9022 { "interval", 0, NULL, 'u' },
9026 void initialisation(int argc, char **argv) {
9027 struct sigaction act, oact;
9029 load_config_file(current_config);
9031 /* init specials array */
9032 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9033 ERR("failed to create specials array");
9037 if (current_mail_spool == NULL) {
9040 variable_substitute(MAIL_FILE, buf, 256);
9042 if (buf[0] != '\0') {
9043 current_mail_spool = strndup(buf, text_buffer_size);
9048 /* handle other command line arguments */
9050 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9051 || defined(__NetBSD__)
9052 optind = optreset = 1;
9057 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9058 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9059 "kvm_open")) == NULL) {
9060 CRIT_ERR(NULL, NULL, "cannot read kvm");
9065 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9073 fork_to_background = 1;
9076 global_debug_level++;
9080 set_first_font(optarg);
9083 text_alignment = string_to_alignment(optarg);
9088 disp = strdup(optarg);
9107 global_text = strndup(optarg, max_user_text);
9108 convert_escapes(global_text);
9112 update_interval = strtod(optarg, 0);
9113 if (info.music_player_interval == 0) {
9114 // default to update_interval
9115 info.music_player_interval = update_interval;
9120 total_run_times = strtod(optarg, 0);
9124 gap_x = atoi(optarg);
9128 gap_y = atoi(optarg);
9139 if (output_methods & TO_X) {
9140 load_config_file_x11(current_config);
9144 /* generate text and get initial size */
9145 extract_variable_text(global_text);
9152 if (fork_to_background) {
9157 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9164 fprintf(stderr, "\n");
9169 /* parent process */
9170 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9177 text_buffer = malloc(max_user_text);
9178 memset(text_buffer, 0, max_user_text);
9179 tmpstring1 = malloc(text_buffer_size);
9180 memset(tmpstring1, 0, text_buffer_size);
9181 tmpstring2 = malloc(text_buffer_size);
9182 memset(tmpstring2, 0, text_buffer_size);
9187 X11_create_window();
9190 /* Set signal handlers */
9191 act.sa_handler = signal_handler;
9192 sigemptyset(&act.sa_mask);
9195 act.sa_flags |= SA_RESTART;
9198 if ( sigaction(SIGINT, &act, &oact) < 0
9199 || sigaction(SIGUSR1, &act, &oact) < 0
9200 || sigaction(SIGHUP, &act, &oact) < 0
9201 || sigaction(SIGTERM, &act, &oact) < 0) {
9202 ERR("error setting signal handler: %s", strerror(errno));
9207 int main(int argc, char **argv)
9216 g_signal_pending = 0;
9217 max_user_text = MAX_USER_TEXT_DEFAULT;
9219 memset(&info, 0, sizeof(info));
9220 memset(template, 0, sizeof(template));
9223 #ifdef TCP_PORT_MONITOR
9224 /* set default connection limit */
9225 tcp_portmon_set_max_connections(0);
9228 /* handle command line parameters that don't change configs */
9230 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9231 || ((s = getenv("LANG")) && *s)) {
9232 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9234 ERR("malloc failed");
9236 for (x = 0; x < strlen(s); x++) {
9237 temp[x] = tolower(s[x]);
9240 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9246 if (!setlocale(LC_CTYPE, "")) {
9247 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9251 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9262 if (current_config) {
9263 free(current_config);
9265 current_config = strndup(optarg, max_user_text);
9268 freopen("/dev/null", "w", stderr);
9271 print_help(argv[0]);
9273 #ifdef CONFIG_OUTPUT
9280 window.window = strtol(optarg, 0, 0);
9289 /* check if specified config file is valid */
9290 if (current_config) {
9292 if (stat(current_config, &sb) ||
9293 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9294 ERR("invalid configuration file '%s'\n", current_config);
9295 free(current_config);
9300 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9302 if (!current_config) {
9303 /* load default config file */
9304 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9307 /* Try to use personal config file first */
9308 to_real_path(buf, CONFIG_FILE);
9309 if (buf[0] && (fp = fopen(buf, "r"))) {
9310 current_config = strndup(buf, max_user_text);
9314 /* Try to use system config file if personal config not readable */
9315 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9316 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9320 /* No readable config found */
9321 if (!current_config) {
9322 #ifdef CONFIG_OUTPUT
9323 current_config = strdup("==builtin==");
9324 ERR("no readable personal or system-wide config file found,"
9325 " using builtin default");
9327 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9328 #endif /* ! CONF_OUTPUT */
9333 /* Load xoap keys, if existing */
9335 #endif /* WEATHER */
9337 #ifdef HAVE_SYS_INOTIFY_H
9338 inotify_fd = inotify_init();
9339 #endif /* HAVE_SYS_INOTIFY_H */
9341 initialisation(argc, argv);
9345 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9352 static void signal_handler(int sig)
9354 /* signal handler is light as a feather, as it should be.
9355 * we will poll g_signal_pending with each loop of conky
9356 * and do any signal processing there, NOT here. */
9357 g_signal_pending = sig;