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;
152 /* prototypes for internally used functions */
153 static void signal_handler(int);
154 static void print_version(void) __attribute__((noreturn));
155 static void reload_config(void);
156 static void generate_text_internal(char *, int, struct text_object,
157 struct information *);
158 static int extract_variable_text_internal(struct text_object *,
161 static void print_version(void)
163 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
165 printf("\nCompiled in features:\n\n"
166 "System config file: "SYSTEM_CONFIG_FILE"\n"
167 "Package library path: "PACKAGE_LIBDIR"\n\n"
171 " * Xdamage extension\n"
172 # endif /* HAVE_XDAMAGE */
174 " * XDBE (double buffer extension)\n"
175 # endif /* HAVE_XDBE */
180 "\n Music detection:\n"
183 #endif /* AUDACIOUS */
199 #endif /* HAVE_OPENMP */
206 #ifdef TCP_PORT_MONITOR
208 #endif /* TCP_PORT_MONITOR */
213 " * Weather (METAR)\n"
217 #endif /* HAVE_IWLIB */
219 " * support for IBM/Lenovo notebooks\n"
229 #endif /* CONFIG_OUTPUT */
234 " * ALSA mixer support\n"
235 #endif /* MIXER_IS_ALSA */
245 #ifdef HAVE_LUA_CAIRO
247 #endif /* HAVE_LUA_CAIRO */
248 #ifdef HAVE_LUA_IMLIB2
251 #endif /* HAVE_LUA */
257 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
262 static void X11_destroy_window(void);
263 static void X11_create_window(void);
264 static void X11_initialisation(void);
266 struct _x11_stuff_s {
270 XserverRegion region2, part;
271 int event_base, error_base;
277 static int text_start_x, text_start_y; /* text start position in window */
278 static int text_width, text_height;
293 /* display to connect to */
294 static char *disp = NULL;
298 /* struct that has all info to be shared between
299 * instances of the same text object */
300 struct information info;
302 /* path to config file */
303 char *current_config;
305 /* set to 1 if you want all text to be in uppercase */
306 static unsigned int stuff_in_upper_case;
308 /* Run how many times? */
309 static unsigned long total_run_times;
312 static int fork_to_background;
314 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
316 /* filenames for output */
317 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
318 char *append_file = NULL; FILE *append_fpointer = NULL;
322 static int show_graph_scale;
323 static int show_graph_range;
325 /* Position on the screen */
326 static int text_alignment;
327 static int gap_x, gap_y;
330 static int draw_borders;
331 static int draw_graph_borders;
332 static int stippled_borders;
334 static int draw_shades, draw_outline;
336 static long default_fg_color, default_bg_color, default_out_color;
338 /* create own window or draw stuff to root? */
339 static int set_transparent = 0;
342 static int own_window = 0;
343 static int background_colour = 0;
345 /* fixed size/pos is set if wm/user changes them */
346 static int fixed_size = 0, fixed_pos = 0;
349 static int minimum_width, minimum_height;
350 static int maximum_width;
355 static int sensor_device;
358 static long color0, color1, color2, color3, color4, color5, color6, color7,
361 #define MAX_TEMPLATES 10
362 static char *template[MAX_TEMPLATES];
364 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
365 unsigned int max_user_text;
367 /* maximum size of individual text buffers, ie $exec buffer size */
368 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
371 #define CODEPAGE_LENGTH 20
373 long iconv_count = 0;
374 char iconv_converting;
375 static iconv_t **iconv_cd = 0;
377 int register_iconv(iconv_t *new_iconv)
379 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
381 CRIT_ERR("Out of memory");
383 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
384 if (!iconv_cd[iconv_count]) {
385 CRIT_ERR("Out of memory");
387 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
392 void free_iconv(void)
397 for (i = 0; i < iconv_count; i++) {
399 iconv_close(*iconv_cd[i]);
413 /* no buffers in used memory? */
416 /* pad percentages to decimals? */
417 static int pad_percents = 0;
419 static char *global_text = 0;
420 long global_text_lines;
422 static int total_updates;
423 static int updatereset;
425 int check_contains(char *f, char *s)
428 FILE *where = open_file(f, 0);
433 while (fgets(buf1, 256, where)) {
434 if (strstr(buf1, s)) {
441 ERR("Could not open the file");
447 static inline int calc_text_width(const char *s, int l)
449 if ((output_methods & TO_X) == 0)
456 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
457 (const FcChar8 *) s, l, &gi);
459 XftTextExtents8(display, fonts[selected_font].xftfont,
460 (const FcChar8 *) s, l, &gi);
466 return XTextWidth(fonts[selected_font].font, s, l);
471 /* formatted text to render on screen, generated in generate_text(),
472 * drawn in draw_stuff() */
474 static char *text_buffer;
477 static unsigned int special_index; /* used when drawing */
480 /* quite boring functions */
482 static inline void for_each_line(char *b, void f(char *))
486 for (ps = b, pe = b; *pe; pe++) {
500 static void convert_escapes(char *buf)
502 char *p = buf, *s = buf;
509 } else if (*s == '\\') {
520 /* Prints anything normally printed with snprintf according to the current value
521 * of use_spacer. Actually slightly more flexible than snprintf, as you can
522 * safely specify the destination buffer as one of your inputs. */
523 int spaced_print(char *buf, int size, const char *format, int width, ...)
532 tempbuf = malloc(size * sizeof(char));
534 // Passes the varargs along to vsnprintf
535 va_start(argp, width);
536 vsnprintf(tempbuf, size, format, argp);
539 switch (use_spacer) {
541 len = snprintf(buf, size, "%s", tempbuf);
544 len = snprintf(buf, size, "%*s", width, tempbuf);
547 len = snprintf(buf, size, "%-*s", width, tempbuf);
554 /* print percentage values
556 * - i.e., unsigned values between 0 and 100
557 * - respect the value of pad_percents */
558 static int percent_print(char *buf, int size, unsigned value)
560 return spaced_print(buf, size, "%u", pad_percents, value);
563 /* converts from bytes to human readable format (K, M, G, T)
565 * The algorithm always divides by 1024, as unit-conversion of byte
566 * counts suggests. But for output length determination we need to
567 * compare with 1000 here, as we print in decimal form. */
568 static void human_readable(long long num, char *buf, int size)
570 const char **suffix = suffixes;
576 /* Possibly just output as usual, for example for stdout usage */
577 if (!format_human_readable) {
578 spaced_print(buf, size, "%d", 6, round_to_int(num));
589 if (llabs(num) < 1000LL) {
590 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
594 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
602 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
604 * The goal is to always have a significance of 3, by
605 * adjusting the decimal part of the number. Sample output:
609 * so the point of alignment resides between number and unit. The
610 * upside of this is that there is minimal padding necessary, though
611 * there should be a way to make alignment take place at the decimal
612 * dot (then with fixed width decimal part).
614 * Note the repdigits below: when given a precision value, printf()
615 * rounds the float to it, not just cuts off the remaining digits. So
616 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
617 * printed with a precision of 0. Yay. */
619 precision = 0; /* print 100-999 without decimal part */
621 precision = 1; /* print 10-99 with one decimal place */
623 precision = 2; /* print 0-9 with two decimal places */
625 spaced_print(buf, size, format, width, precision, fnum, *suffix);
628 /* global object list root element */
629 static struct text_object global_root_object;
631 static inline void read_exec(const char *data, char *buf, const int size)
633 FILE *fp = popen(data, "r");
634 int length = fread(buf, 1, size, fp);
638 if (length > 0 && buf[length - 1] == '\n') {
639 buf[length - 1] = '\0';
643 void *threaded_exec(void *) __attribute__((noreturn));
645 void *threaded_exec(void *arg)
648 struct text_object *obj = (struct text_object *)arg;
651 buff = malloc(text_buffer_size);
652 read_exec(obj->data.texeci.cmd, buff,
661 timed_thread_lock(obj->data.texeci.p_timed_thread);
662 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
663 timed_thread_unlock(obj->data.texeci.p_timed_thread);
665 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
666 timed_thread_exit(obj->data.texeci.p_timed_thread);
672 static struct text_object *new_text_object_internal(void)
674 struct text_object *obj = malloc(sizeof(struct text_object));
675 memset(obj, 0, sizeof(struct text_object));
680 * Frees the list of text objects root points to. When internal = 1, it won't
681 * free global objects.
683 static void free_text_objects(struct text_object *root, int internal)
685 struct text_object *obj;
691 #define data obj->data
692 for (obj = root->prev; obj; obj = root->prev) {
693 root->prev = obj->prev;
699 #endif /* !__OpenBSD__ */
704 close(data.sysfs.fd);
706 #endif /* __linux__ */
708 free(data.read_tcp.host);
715 free(data.tztime.tz);
716 free(data.tztime.fmt);
719 free(data.mboxscan.args);
720 free(data.mboxscan.output);
725 case OBJ_unseen_mails:
726 case OBJ_flagged_mails:
727 case OBJ_unflagged_mails:
728 case OBJ_forwarded_mails:
729 case OBJ_unforwarded_mails:
730 case OBJ_replied_mails:
731 case OBJ_unreplied_mails:
732 case OBJ_draft_mails:
733 case OBJ_trashed_mails:
734 free(data.local_mail.box);
736 case OBJ_imap_unseen:
741 case OBJ_imap_messages:
746 case OBJ_pop3_unseen:
758 free_text_objects(obj->sub, 1);
761 case OBJ_if_existing:
764 free(data.ifblock.s);
765 free(data.ifblock.str);
768 free(data.tail.logfile);
769 free(data.tail.buffer);
785 case OBJ_iconv_start:
790 case OBJ_disk_protect:
794 free(data.ifblock.s);
795 free(data.ifblock.str);
798 if (info.gw_info.iface) {
799 free(info.gw_info.iface);
800 info.gw_info.iface = 0;
802 if (info.gw_info.ip) {
803 free(info.gw_info.ip);
807 case OBJ_ioscheduler:
812 #if (defined(__FreeBSD__) || defined(__linux__))
814 free(data.ifblock.s);
815 free(data.ifblock.str);
819 case OBJ_xmms2_artist:
820 if (info.xmms2.artist) {
821 free(info.xmms2.artist);
822 info.xmms2.artist = 0;
825 case OBJ_xmms2_album:
826 if (info.xmms2.album) {
827 free(info.xmms2.album);
828 info.xmms2.album = 0;
831 case OBJ_xmms2_title:
832 if (info.xmms2.title) {
833 free(info.xmms2.title);
834 info.xmms2.title = 0;
837 case OBJ_xmms2_genre:
838 if (info.xmms2.genre) {
839 free(info.xmms2.genre);
840 info.xmms2.genre = 0;
843 case OBJ_xmms2_comment:
844 if (info.xmms2.comment) {
845 free(info.xmms2.comment);
846 info.xmms2.comment = 0;
850 if (info.xmms2.url) {
851 free(info.xmms2.url);
856 if (info.xmms2.date) {
857 free(info.xmms2.date);
861 case OBJ_xmms2_status:
862 if (info.xmms2.status) {
863 free(info.xmms2.status);
864 info.xmms2.status = 0;
867 case OBJ_xmms2_playlist:
868 if (info.xmms2.playlist) {
869 free(info.xmms2.playlist);
870 info.xmms2.playlist = 0;
873 case OBJ_xmms2_smart:
874 if (info.xmms2.artist) {
875 free(info.xmms2.artist);
876 info.xmms2.artist = 0;
878 if (info.xmms2.title) {
879 free(info.xmms2.title);
880 info.xmms2.title = 0;
882 if (info.xmms2.url) {
883 free(info.xmms2.url);
890 case OBJ_bmpx_artist:
894 case OBJ_bmpx_bitrate:
904 free(data.rss.action);
909 free(data.weather.uri);
910 free(data.weather.data_type);
922 #endif /* HAVE_LUA */
929 case OBJ_battery_short:
932 case OBJ_battery_time:
935 #endif /* !__OpenBSD__ */
943 free(data.execi.cmd);
944 free(data.execi.buffer);
947 free(data.texeci.cmd);
948 free(data.texeci.buffer);
959 if (info.first_process && !internal) {
960 free_all_processes();
961 info.first_process = NULL;
963 if (data.top.s) free(data.top.s);
967 free(data.hddtemp.dev);
968 free(data.hddtemp.addr);
969 if (data.hddtemp.temp)
970 free(data.hddtemp.temp);
973 case OBJ_entropy_avail:
974 case OBJ_entropy_perc:
975 case OBJ_entropy_poolsize:
976 case OBJ_entropy_bar:
979 if (info.users.names) {
980 free(info.users.names);
981 info.users.names = 0;
985 if (info.users.terms) {
986 free(info.users.terms);
987 info.users.terms = 0;
991 if (info.users.times) {
992 free(info.users.times);
993 info.users.times = 0;
998 case OBJ_smapi_bat_perc:
999 case OBJ_smapi_bat_temp:
1000 case OBJ_smapi_bat_power:
1003 case OBJ_if_smapi_bat_installed:
1004 free(data.ifblock.s);
1005 free(data.ifblock.str);
1014 case OBJ_mpd_artist:
1016 case OBJ_mpd_random:
1017 case OBJ_mpd_repeat:
1019 case OBJ_mpd_bitrate:
1020 case OBJ_mpd_status:
1022 case OBJ_mpd_elapsed:
1023 case OBJ_mpd_length:
1027 case OBJ_mpd_percent:
1029 case OBJ_if_mpd_playing:
1037 case OBJ_moc_artist:
1040 case OBJ_moc_totaltime:
1041 case OBJ_moc_timeleft:
1042 case OBJ_moc_curtime:
1043 case OBJ_moc_bitrate:
1050 free_text_objects(obj->sub, 1);
1054 free(data.scroll.text);
1055 free_text_objects(obj->sub, 1);
1059 free(data.combine.left);
1060 free(data.combine.seperation);
1061 free(data.combine.right);
1062 free_text_objects(obj->sub, 1);
1067 case OBJ_apcupsd_name:
1068 case OBJ_apcupsd_model:
1069 case OBJ_apcupsd_upsmode:
1070 case OBJ_apcupsd_cable:
1071 case OBJ_apcupsd_status:
1072 case OBJ_apcupsd_linev:
1073 case OBJ_apcupsd_load:
1074 case OBJ_apcupsd_loadbar:
1076 case OBJ_apcupsd_loadgraph:
1077 case OBJ_apcupsd_loadgauge:
1079 case OBJ_apcupsd_charge:
1080 case OBJ_apcupsd_timeleft:
1081 case OBJ_apcupsd_temp:
1082 case OBJ_apcupsd_lastxfer:
1084 #endif /* APCUPSD */
1087 case OBJ_desktop_number:
1088 case OBJ_desktop_name:
1089 if(info.x11.desktop.name) {
1090 free(info.x11.desktop.name);
1091 info.x11.desktop.name = NULL;
1102 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1107 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1108 *a = mixer_init(buf1);
1109 scan_bar(arg + n, w, h);
1111 *a = mixer_init(NULL);
1112 scan_bar(arg, w, h);
1117 /* strip a leading /dev/ if any, following symlinks first
1119 * BEWARE: this function returns a pointer to static content
1120 * which gets overwritten in consecutive calls. I.e.:
1121 * this function is NOT reentrant.
1123 static const char *dev_name(const char *path)
1125 static char buf[255]; /* should be enough for pathnames */
1131 #define DEV_NAME(x) \
1132 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1133 if ((buflen = readlink(path, buf, 254)) == -1)
1134 return DEV_NAME(path);
1136 return DEV_NAME(buf);
1140 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1145 if (obj->data.top.was_parsed) {
1148 obj->data.top.was_parsed = 1;
1150 if (arg && !obj->data.top.s) {
1151 obj->data.top.s = strndup(arg, text_buffer_size);
1154 need_mask |= (1 << INFO_TOP);
1157 obj->type = OBJ_top;
1159 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1160 obj->type = OBJ_top_mem;
1162 } else if (strcmp(&s[3], "_time") == EQUAL) {
1163 obj->type = OBJ_top_time;
1166 } else if (strcmp(&s[3], "_io") == EQUAL) {
1167 obj->type = OBJ_top_io;
1172 ERR("Must be top, top_mem, top_time or top_io");
1174 ERR("Must be top, top_mem or top_time");
1180 ERR("top needs arguments");
1184 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1185 if (strcmp(buf, "name") == EQUAL) {
1186 obj->data.top.type = TOP_NAME;
1187 } else if (strcmp(buf, "cpu") == EQUAL) {
1188 obj->data.top.type = TOP_CPU;
1189 } else if (strcmp(buf, "pid") == EQUAL) {
1190 obj->data.top.type = TOP_PID;
1191 } else if (strcmp(buf, "mem") == EQUAL) {
1192 obj->data.top.type = TOP_MEM;
1193 } else if (strcmp(buf, "time") == EQUAL) {
1194 obj->data.top.type = TOP_TIME;
1195 } else if (strcmp(buf, "mem_res") == EQUAL) {
1196 obj->data.top.type = TOP_MEM_RES;
1197 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1198 obj->data.top.type = TOP_MEM_VSIZE;
1200 } else if (strcmp(buf, "io_read") == EQUAL) {
1201 obj->data.top.type = TOP_READ_BYTES;
1202 } else if (strcmp(buf, "io_write") == EQUAL) {
1203 obj->data.top.type = TOP_WRITE_BYTES;
1204 } else if (strcmp(buf, "io_perc") == EQUAL) {
1205 obj->data.top.type = TOP_IO_PERC;
1208 ERR("invalid type arg for top");
1210 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1211 "io_read, io_write, io_perc");
1213 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1217 if (n < 1 || n > 10) {
1218 ERR("invalid num arg for top. Must be between 1 and 10.");
1221 obj->data.top.num = n - 1;
1224 ERR("invalid argument count for top");
1230 /* construct_text_object() creates a new text_object */
1231 static struct text_object *construct_text_object(const char *s,
1232 const char *arg, long line, char allow_threaded, void **ifblock_opaque)
1234 // struct text_object *obj = new_text_object();
1235 struct text_object *obj = new_text_object_internal();
1239 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1240 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1241 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1242 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1243 obj_be_ifblock_if(ifblock_opaque, obj); {
1244 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1245 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1246 #define END } } else
1248 #define SIZE_DEFAULTS(arg) { \
1249 obj->a = default_##arg##_width; \
1250 obj->b = default_##arg##_height; \
1255 obj->type = OBJ_color;
1256 obj->data.l = get_x11_color(s);
1260 OBJ(freq, INFO_FREQ)
1263 obj->data.i = open_acpi_temperature(arg);
1264 END OBJ(acpiacadapter, 0)
1265 END OBJ(freq, INFO_FREQ)
1266 #endif /* !__OpenBSD__ */
1268 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1269 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1270 obj->data.cpu_index = 1;
1271 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1272 "Displaying the clock for CPU 1."); */
1274 obj->data.cpu_index = atoi(&arg[0]);
1277 END OBJ(freq_g, INFO_FREQ)
1279 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1280 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1281 obj->data.cpu_index = 1;
1282 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1283 "CPUs! Displaying the clock for CPU 1."); */
1285 obj->data.cpu_index = atoi(&arg[0]);
1288 END OBJ(read_tcp, 0)
1290 obj->data.read_tcp.host = malloc(text_buffer_size);
1291 sscanf(arg, "%s", obj->data.read_tcp.host);
1292 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1293 if(obj->data.read_tcp.port == 0) {
1294 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1295 strcpy(obj->data.read_tcp.host,"localhost");
1297 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1298 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1299 CRIT_ERR("read_tcp: Needs \"(host) port\" as argument(s)");
1302 CRIT_ERR("read_tcp: Needs \"(host) port\" as argument(s)");
1304 #if defined(__linux__)
1305 END OBJ(voltage_mv, 0)
1307 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1308 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1309 obj->data.cpu_index = 1;
1310 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1311 "CPUs! Displaying voltage for CPU 1."); */
1313 obj->data.cpu_index = atoi(&arg[0]);
1316 END OBJ(voltage_v, 0)
1318 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1319 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1320 obj->data.cpu_index = 1;
1321 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1322 "CPUs! Displaying voltage for CPU 1."); */
1324 obj->data.cpu_index = atoi(&arg[0]);
1329 END OBJ(wireless_essid, INFO_NET)
1331 obj->data.net = get_net_stat(arg);
1333 // default to DEFAULTNETDEV
1334 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1335 obj->data.net = get_net_stat(buf);
1338 END OBJ(wireless_mode, INFO_NET)
1340 obj->data.net = get_net_stat(arg);
1342 // default to DEFAULTNETDEV
1343 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1344 obj->data.net = get_net_stat(buf);
1347 END OBJ(wireless_bitrate, INFO_NET)
1349 obj->data.net = get_net_stat(arg);
1351 // default to DEFAULTNETDEV
1352 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1353 obj->data.net = get_net_stat(buf);
1356 END OBJ(wireless_ap, INFO_NET)
1358 obj->data.net = get_net_stat(arg);
1360 // default to DEFAULTNETDEV
1361 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1362 obj->data.net = get_net_stat(buf);
1365 END OBJ(wireless_link_qual, INFO_NET)
1367 obj->data.net = get_net_stat(arg);
1369 // default to DEFAULTNETDEV
1370 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1371 obj->data.net = get_net_stat(buf);
1374 END OBJ(wireless_link_qual_max, INFO_NET)
1376 obj->data.net = get_net_stat(arg);
1378 // default to DEFAULTNETDEV
1379 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1380 obj->data.net = get_net_stat(buf);
1383 END OBJ(wireless_link_qual_perc, INFO_NET)
1385 obj->data.net = get_net_stat(arg);
1387 // default to DEFAULTNETDEV
1388 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1389 obj->data.net = get_net_stat(buf);
1392 END OBJ(wireless_link_bar, INFO_NET)
1395 arg = scan_bar(arg, &obj->a, &obj->b);
1396 obj->data.net = get_net_stat(arg);
1398 // default to DEFAULTNETDEV
1399 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1400 obj->data.net = get_net_stat(buf);
1403 #endif /* HAVE_IWLIB */
1405 #endif /* __linux__ */
1413 sscanf(arg, "%63s", bat);
1415 strcpy(bat, "BAT0");
1417 obj->data.s = strndup(bat, text_buffer_size);
1418 END OBJ(battery_short, 0)
1422 sscanf(arg, "%63s", bat);
1424 strcpy(bat, "BAT0");
1426 obj->data.s = strndup(bat, text_buffer_size);
1427 END OBJ(battery_time, 0)
1431 sscanf(arg, "%63s", bat);
1433 strcpy(bat, "BAT0");
1435 obj->data.s = strndup(bat, text_buffer_size);
1436 END OBJ(battery_percent, 0)
1440 sscanf(arg, "%63s", bat);
1442 strcpy(bat, "BAT0");
1444 obj->data.s = strndup(bat, text_buffer_size);
1445 END OBJ(battery_bar, 0)
1450 arg = scan_bar(arg, &obj->a, &obj->b);
1451 sscanf(arg, "%63s", bat);
1453 strcpy(bat, "BAT0");
1455 obj->data.s = strndup(bat, text_buffer_size);
1456 #endif /* !__OpenBSD__ */
1458 #if defined(__linux__)
1459 END OBJ(disk_protect, 0)
1461 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1463 CRIT_ERR("disk_protect needs an argument");
1464 END OBJ(i8k_version, INFO_I8K)
1465 END OBJ(i8k_bios, INFO_I8K)
1466 END OBJ(i8k_serial, INFO_I8K)
1467 END OBJ(i8k_cpu_temp, INFO_I8K)
1468 END OBJ(i8k_left_fan_status, INFO_I8K)
1469 END OBJ(i8k_right_fan_status, INFO_I8K)
1470 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1471 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1472 END OBJ(i8k_ac_status, INFO_I8K)
1473 END OBJ(i8k_buttons_status, INFO_I8K)
1476 END OBJ(ibm_temps, 0)
1478 CRIT_ERR("ibm_temps: needs an argument");
1480 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1481 obj->data.sensor = 0;
1482 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1483 "Using 0 (CPU temp sensor).");
1485 obj->data.sensor = atoi(&arg[0]);
1486 END OBJ(ibm_volume, 0)
1487 END OBJ(ibm_brightness, 0)
1489 /* information from sony_laptop kernel module
1490 * /sys/devices/platform/sony-laptop */
1491 END OBJ(sony_fanspeed, 0)
1492 END OBJ_IF(if_gw, INFO_GW)
1493 END OBJ(ioscheduler, 0)
1495 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1498 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1499 END OBJ(laptop_mode, 0)
1500 END OBJ(pb_battery, 0)
1501 if (arg && strcmp(arg, "status") == EQUAL) {
1502 obj->data.i = PB_BATT_STATUS;
1503 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1504 obj->data.i = PB_BATT_PERCENT;
1505 } else if (arg && strcmp(arg, "time") == EQUAL) {
1506 obj->data.i = PB_BATT_TIME;
1508 ERR("pb_battery: needs one argument: status, percent or time");
1513 #endif /* __linux__ */
1514 #if (defined(__FreeBSD__) || defined(__linux__))
1515 END OBJ_IF(if_up, 0)
1517 ERR("if_up needs an argument");
1518 obj->data.ifblock.s = 0;
1520 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1523 #if defined(__OpenBSD__)
1524 END OBJ(obsd_sensors_temp, 0)
1526 CRIT_ERR("obsd_sensors_temp: needs an argument");
1528 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1529 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1530 obj->data.sensor = 0;
1531 ERR("Invalid temperature sensor number!");
1533 obj->data.sensor = atoi(&arg[0]);
1534 END OBJ(obsd_sensors_fan, 0)
1536 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1539 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1540 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1541 obj->data.sensor = 0;
1542 ERR("Invalid fan sensor number!");
1544 obj->data.sensor = atoi(&arg[0]);
1545 END OBJ(obsd_sensors_volt, 0)
1547 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1550 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1551 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1552 obj->data.sensor = 0;
1553 ERR("Invalid voltage sensor number!");
1555 obj->data.sensor = atoi(&arg[0]);
1556 END OBJ(obsd_vendor, 0)
1557 END OBJ(obsd_product, 0)
1558 #endif /* __OpenBSD__ */
1559 END OBJ(buffers, INFO_BUFFERS)
1560 END OBJ(cached, INFO_BUFFERS)
1561 #define SCAN_CPU(__arg, __var) { \
1563 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1564 __arg += __offset; \
1568 END OBJ(cpu, INFO_CPU)
1569 SCAN_CPU(arg, obj->data.cpu_index);
1570 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1572 END OBJ(cpugauge, INFO_CPU)
1573 SIZE_DEFAULTS(gauge);
1574 SCAN_CPU(arg, obj->data.cpu_index);
1575 scan_gauge(arg, &obj->a, &obj->b);
1576 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1578 END OBJ(cpubar, INFO_CPU)
1580 SCAN_CPU(arg, obj->data.cpu_index);
1581 scan_bar(arg, &obj->a, &obj->b);
1582 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1584 END OBJ(cpugraph, INFO_CPU)
1586 SIZE_DEFAULTS(graph);
1587 SCAN_CPU(arg, obj->data.cpu_index);
1588 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1589 &obj->e, &obj->char_a, &obj->char_b);
1590 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1592 END OBJ(loadgraph, INFO_LOADAVG)
1594 SIZE_DEFAULTS(graph);
1595 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1596 &obj->e, &obj->char_a, &obj->char_b);
1600 r = sscanf(arg, "%d", &a);
1602 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1606 END OBJ(diskio, INFO_DISKIO)
1607 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1608 END OBJ(diskio_read, INFO_DISKIO)
1609 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1610 END OBJ(diskio_write, INFO_DISKIO)
1611 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1613 END OBJ(diskiograph, INFO_DISKIO)
1615 SIZE_DEFAULTS(graph);
1616 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1617 &obj->e, &obj->char_a, &obj->char_b);
1619 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1621 END OBJ(diskiograph_read, INFO_DISKIO)
1623 SIZE_DEFAULTS(graph);
1624 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1625 &obj->e, &obj->char_a, &obj->char_b);
1627 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1629 END OBJ(diskiograph_write, INFO_DISKIO)
1631 SIZE_DEFAULTS(graph);
1632 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1633 &obj->e, &obj->char_a, &obj->char_b);
1635 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1640 if (output_methods & TO_X) {
1641 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1645 obj->data.l = color0;
1647 obj->data.l = color1;
1649 obj->data.l = color2;
1651 obj->data.l = color3;
1653 obj->data.l = color4;
1655 obj->data.l = color5;
1657 obj->data.l = color6;
1659 obj->data.l = color7;
1661 obj->data.l = color8;
1663 obj->data.l = color9;
1666 obj->data.s = scan_font(arg);
1668 END OBJ(conky_version, 0)
1669 END OBJ(conky_build_date, 0)
1670 END OBJ(conky_build_arch, 0)
1671 END OBJ(downspeed, INFO_NET)
1673 obj->data.net = get_net_stat(arg);
1675 // default to DEFAULTNETDEV
1676 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1677 obj->data.net = get_net_stat(buf);
1680 END OBJ(downspeedf, INFO_NET)
1682 obj->data.net = get_net_stat(arg);
1684 // default to DEFAULTNETDEV
1685 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1686 obj->data.net = get_net_stat(buf);
1690 END OBJ(downspeedgraph, INFO_NET)
1692 SIZE_DEFAULTS(graph);
1693 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1694 &obj->e, &obj->char_a, &obj->char_b);
1696 // default to DEFAULTNETDEV
1697 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1698 obj->data.net = get_net_stat(buf);
1702 obj_be_ifblock_else(ifblock_opaque, obj);
1704 obj_be_ifblock_endif(ifblock_opaque, obj);
1706 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1708 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1711 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1713 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1716 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1718 END OBJ(execgauge, 0)
1719 SIZE_DEFAULTS(gauge);
1720 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1721 END OBJ(execgraph, 0)
1722 SIZE_DEFAULTS(graph);
1723 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1725 END OBJ(execibar, 0)
1729 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1732 ERR("${execibar <interval> command}");
1733 obj->type = OBJ_text;
1734 snprintf(buf, 256, "${%s}", s);
1735 obj->data.s = strndup(buf, text_buffer_size);
1737 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1740 END OBJ(execigraph, 0)
1742 SIZE_DEFAULTS(graph);
1744 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1747 ERR("${execigraph <interval> command}");
1748 obj->type = OBJ_text;
1749 snprintf(buf, 256, "${%s}", s);
1750 obj->data.s = strndup(buf, text_buffer_size);
1752 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1754 END OBJ(execigauge, 0)
1756 SIZE_DEFAULTS(gauge);
1758 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1761 ERR("${execigauge <interval> command}");
1762 obj->type = OBJ_text;
1763 snprintf(buf, 256, "${%s}", s);
1764 obj->data.s = strndup(buf, text_buffer_size);
1766 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1772 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1775 ERR("${execi <interval> command}");
1776 obj->type = OBJ_text;
1777 snprintf(buf, 256, "${%s}", s);
1778 obj->data.s = strndup(buf, text_buffer_size);
1780 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1781 obj->data.execi.buffer = malloc(text_buffer_size);
1786 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1789 ERR("${execi <interval> command}");
1790 obj->type = OBJ_text;
1791 snprintf(buf, 256, "${%s}", s);
1792 obj->data.s = strndup(buf, text_buffer_size);
1794 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1795 obj->data.execi.buffer = malloc(text_buffer_size);
1797 END OBJ_THREAD(texeci, 0)
1800 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1803 ERR("${texeci <interval> command}");
1804 obj->type = OBJ_text;
1805 snprintf(buf, 256, "${%s}", s);
1806 obj->data.s = strndup(buf, text_buffer_size);
1808 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1809 obj->data.texeci.buffer = malloc(text_buffer_size);
1811 obj->data.texeci.p_timed_thread = NULL;
1812 END OBJ(pre_exec, 0)
1813 obj->type = OBJ_text;
1817 read_exec(arg, buf, sizeof(buf));
1818 obj->data.s = strndup(buf, text_buffer_size);
1820 obj->data.s = strndup("", text_buffer_size);
1823 END OBJ(fs_bar, INFO_FS)
1825 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1827 while (isspace(*arg)) {
1836 obj->data.fsbar.fs = prepare_fs_stat(arg);
1837 END OBJ(fs_bar_free, INFO_FS)
1839 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1841 while (isspace(*arg)) {
1851 obj->data.fsbar.fs = prepare_fs_stat(arg);
1852 END OBJ(fs_free, INFO_FS)
1856 obj->data.fs = prepare_fs_stat(arg);
1857 END OBJ(fs_used_perc, INFO_FS)
1861 obj->data.fs = prepare_fs_stat(arg);
1862 END OBJ(fs_free_perc, INFO_FS)
1866 obj->data.fs = prepare_fs_stat(arg);
1867 END OBJ(fs_size, INFO_FS)
1871 obj->data.fs = prepare_fs_stat(arg);
1872 END OBJ(fs_type, INFO_FS)
1876 obj->data.fs = prepare_fs_stat(arg);
1877 END OBJ(fs_used, INFO_FS)
1881 obj->data.fs = prepare_fs_stat(arg);
1883 obj->data.i = arg ? atoi(arg) : 1;
1884 END OBJ(nameserver, INFO_DNS)
1885 obj->data.i = arg ? atoi(arg) : 0;
1887 obj->data.i = arg ? atoi(arg) : 1;
1889 obj->data.i = arg ? atoi(arg) : 1;
1893 ERR("goto needs arguments");
1894 obj->type = OBJ_text;
1895 obj->data.s = strndup("${goto}", text_buffer_size);
1899 obj->data.i = atoi(arg);
1905 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1906 sscanf(arg, "%d", &b);
1912 obj->data.pair.a = a;
1913 obj->data.pair.b = b;
1916 END OBJ(i2c, INFO_SYSFS)
1917 char buf1[64], buf2[64];
1918 float factor, offset;
1922 ERR("i2c needs arguments");
1923 obj->type = OBJ_text;
1924 // obj->data.s = strndup("${i2c}", text_buffer_size);
1928 #define HWMON_RESET() {\
1933 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1934 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1935 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1936 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1939 ERR("i2c failed to parse arguments");
1940 obj->type = OBJ_text;
1943 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1944 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
1945 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1946 strncpy(obj->data.sysfs.type, buf2, 63);
1947 obj->data.sysfs.factor = factor;
1948 obj->data.sysfs.offset = offset;
1950 END OBJ(platform, INFO_SYSFS)
1951 char buf1[64], buf2[64];
1952 float factor, offset;
1956 ERR("platform needs arguments");
1957 obj->type = OBJ_text;
1961 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1962 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1963 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1964 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1967 ERR("platform failed to parse arguments");
1968 obj->type = OBJ_text;
1971 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1972 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
1973 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1974 strncpy(obj->data.sysfs.type, buf2, 63);
1975 obj->data.sysfs.factor = factor;
1976 obj->data.sysfs.offset = offset;
1978 END OBJ(hwmon, INFO_SYSFS)
1979 char buf1[64], buf2[64];
1980 float factor, offset;
1984 ERR("hwmon needs argumanets");
1985 obj->type = OBJ_text;
1989 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1990 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1991 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1992 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1997 ERR("hwmon failed to parse arguments");
1998 obj->type = OBJ_text;
2001 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2002 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2003 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2004 strncpy(obj->data.sysfs.type, buf2, 63);
2005 obj->data.sysfs.factor = factor;
2006 obj->data.sysfs.offset = offset;
2008 #endif /* !__OpenBSD__ */
2011 /* we have four different types of top (top, top_mem, top_time and top_io). To
2012 * avoid having almost-same code four times, we have this special
2014 if (strncmp(s, "top", 3) == EQUAL) {
2015 if (!parse_top_args(s, arg, obj)) {
2018 } else OBJ(addr, INFO_NET)
2020 obj->data.net = get_net_stat(arg);
2022 // default to DEFAULTNETDEV
2023 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2024 obj->data.net = get_net_stat(buf);
2027 #if defined(__linux__)
2028 END OBJ(addrs, INFO_NET)
2030 obj->data.net = get_net_stat(arg);
2032 // default to DEFAULTNETDEV
2033 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2034 obj->data.net = get_net_stat(buf);
2037 #endif /* __linux__ */
2039 if (init_tail_object(obj, arg)) {
2040 obj->type = OBJ_text;
2041 obj->data.s = strndup("${tail}", text_buffer_size);
2044 if (init_head_object(obj, arg)) {
2045 obj->type = OBJ_text;
2046 obj->data.s = strndup("${head}", text_buffer_size);
2050 obj->data.s = strndup(arg, text_buffer_size);
2052 CRIT_ERR("lines needs a argument");
2056 obj->data.s = strndup(arg, text_buffer_size);
2058 CRIT_ERR("words needs a argument");
2060 END OBJ(loadavg, INFO_LOADAVG)
2061 int a = 1, b = 2, c = 3, r = 3;
2064 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2065 if (r >= 3 && (c < 1 || c > 3)) {
2068 if (r >= 2 && (b < 1 || b > 3)) {
2071 if (r >= 1 && (a < 1 || a > 3)) {
2075 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2076 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2077 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2078 END OBJ_IF(if_empty, 0)
2080 ERR("if_empty needs an argument");
2081 obj->data.ifblock.s = 0;
2083 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2084 obj->sub = malloc(sizeof(struct text_object));
2085 extract_variable_text_internal(obj->sub,
2086 obj->data.ifblock.s, 0);
2088 END OBJ_IF(if_match, 0)
2090 ERR("if_match needs arguments");
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_existing, 0)
2100 ERR("if_existing needs an argument or two");
2101 obj->data.ifblock.s = NULL;
2102 obj->data.ifblock.str = NULL;
2104 char buf1[256], buf2[256];
2105 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2108 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2109 obj->data.ifblock.str = NULL;
2111 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2112 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2115 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2116 END OBJ_IF(if_mounted, 0)
2118 ERR("if_mounted needs an argument");
2119 obj->data.ifblock.s = 0;
2121 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2124 END OBJ_IF(if_running, INFO_TOP)
2126 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2128 END OBJ_IF(if_running, 0)
2132 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2133 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2136 ERR("if_running needs an argument");
2137 obj->data.ifblock.s = 0;
2143 char box[256], dst[256];
2147 /* Kapil: Changed from MAIL_FILE to
2148 current_mail_spool since the latter
2149 is a copy of the former if undefined
2150 but the latter should take precedence
2152 strncpy(box, current_mail_spool, sizeof(box));
2154 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2156 strncpy(box, arg, sizeof(box));
2160 variable_substitute(box, dst, sizeof(dst));
2161 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2162 obj->data.local_mail.interval = n1;
2163 END OBJ(new_mails, 0)
2165 char box[256], dst[256];
2169 strncpy(box, current_mail_spool, sizeof(box));
2171 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2173 strncpy(box, arg, sizeof(box));
2177 variable_substitute(box, dst, sizeof(dst));
2178 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2179 obj->data.local_mail.interval = n1;
2180 END OBJ(seen_mails, 0)
2182 char box[256], dst[256];
2186 strncpy(box, current_mail_spool, sizeof(box));
2188 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2190 strncpy(box, arg, sizeof(box));
2194 variable_substitute(box, dst, sizeof(dst));
2195 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2196 obj->data.local_mail.interval = n1;
2197 END OBJ(unseen_mails, 0)
2199 char box[256], dst[256];
2203 strncpy(box, current_mail_spool, sizeof(box));
2205 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2207 strncpy(box, arg, sizeof(box));
2211 variable_substitute(box, dst, sizeof(dst));
2212 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2213 obj->data.local_mail.interval = n1;
2214 END OBJ(flagged_mails, 0)
2216 char box[256], dst[256];
2220 strncpy(box, current_mail_spool, sizeof(box));
2222 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2224 strncpy(box, arg, sizeof(box));
2228 variable_substitute(box, dst, sizeof(dst));
2229 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2230 obj->data.local_mail.interval = n1;
2231 END OBJ(unflagged_mails, 0)
2233 char box[256], dst[256];
2237 strncpy(box, current_mail_spool, sizeof(box));
2239 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2241 strncpy(box, arg, sizeof(box));
2245 variable_substitute(box, dst, sizeof(dst));
2246 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2247 obj->data.local_mail.interval = n1;
2248 END OBJ(forwarded_mails, 0)
2250 char box[256], dst[256];
2254 strncpy(box, current_mail_spool, sizeof(box));
2256 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2258 strncpy(box, arg, sizeof(box));
2262 variable_substitute(box, dst, sizeof(dst));
2263 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2264 obj->data.local_mail.interval = n1;
2265 END OBJ(unforwarded_mails, 0)
2267 char box[256], dst[256];
2271 strncpy(box, current_mail_spool, sizeof(box));
2273 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2275 strncpy(box, arg, sizeof(box));
2279 variable_substitute(box, dst, sizeof(dst));
2280 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2281 obj->data.local_mail.interval = n1;
2282 END OBJ(replied_mails, 0)
2284 char box[256], dst[256];
2288 strncpy(box, current_mail_spool, sizeof(box));
2290 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2292 strncpy(box, arg, sizeof(box));
2296 variable_substitute(box, dst, sizeof(dst));
2297 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2298 obj->data.local_mail.interval = n1;
2299 END OBJ(unreplied_mails, 0)
2301 char box[256], dst[256];
2305 strncpy(box, current_mail_spool, sizeof(box));
2307 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2309 strncpy(box, arg, sizeof(box));
2313 variable_substitute(box, dst, sizeof(dst));
2314 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2315 obj->data.local_mail.interval = n1;
2316 END OBJ(draft_mails, 0)
2318 char box[256], dst[256];
2322 strncpy(box, current_mail_spool, sizeof(box));
2324 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2326 strncpy(box, arg, sizeof(box));
2330 variable_substitute(box, dst, sizeof(dst));
2331 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2332 obj->data.local_mail.interval = n1;
2333 END OBJ(trashed_mails, 0)
2335 char box[256], dst[256];
2339 strncpy(box, current_mail_spool, sizeof(box));
2341 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2343 strncpy(box, arg, sizeof(box));
2347 variable_substitute(box, dst, sizeof(dst));
2348 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2349 obj->data.local_mail.interval = n1;
2350 END OBJ(mboxscan, 0)
2351 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2352 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2353 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2354 obj->data.mboxscan.output[0] = 1;
2355 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2356 END OBJ(mem, INFO_MEM)
2357 END OBJ(memeasyfree, INFO_MEM)
2358 END OBJ(memfree, INFO_MEM)
2359 END OBJ(memmax, INFO_MEM)
2360 END OBJ(memperc, INFO_MEM)
2362 END OBJ(memgauge, INFO_MEM)
2363 SIZE_DEFAULTS(gauge);
2364 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2366 END OBJ(membar, INFO_MEM)
2368 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2370 END OBJ(memgraph, INFO_MEM)
2372 SIZE_DEFAULTS(graph);
2373 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2374 &obj->e, &obj->char_a, &obj->char_b);
2378 END OBJ(mixer, INFO_MIXER)
2379 obj->data.l = mixer_init(arg);
2380 END OBJ(mixerl, INFO_MIXER)
2381 obj->data.l = mixer_init(arg);
2382 END OBJ(mixerr, INFO_MIXER)
2383 obj->data.l = mixer_init(arg);
2385 END OBJ(mixerbar, INFO_MIXER)
2387 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2388 &obj->data.mixerbar.h);
2389 END OBJ(mixerlbar, INFO_MIXER)
2391 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2392 &obj->data.mixerbar.h);
2393 END OBJ(mixerrbar, INFO_MIXER)
2395 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2396 &obj->data.mixerbar.h);
2398 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2399 obj->data.ifblock.i = mixer_init(arg);
2401 END OBJ(monitor, INFO_X11)
2402 END OBJ(monitor_number, INFO_X11)
2403 END OBJ(desktop, INFO_X11)
2404 END OBJ(desktop_number, INFO_X11)
2405 END OBJ(desktop_name, INFO_X11)
2407 END OBJ(nodename, 0)
2408 END OBJ(processes, INFO_PROCS)
2409 END OBJ(running_processes, INFO_RUN_PROCS)
2410 END OBJ(shadecolor, 0)
2412 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2414 END OBJ(outlinecolor, 0)
2416 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2418 END OBJ(stippled_hr, 0)
2420 int a = stippled_borders, b = 1;
2423 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2424 sscanf(arg, "%d", &b);
2430 obj->data.pair.a = a;
2431 obj->data.pair.b = b;
2433 END OBJ(swap, INFO_MEM)
2434 END OBJ(swapfree, INFO_MEM)
2435 END OBJ(swapmax, INFO_MEM)
2436 END OBJ(swapperc, INFO_MEM)
2437 END OBJ(swapbar, INFO_MEM)
2439 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2442 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2444 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2446 char buf1[256], buf2[256], *fmt, *tz;
2450 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2460 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2461 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2463 END OBJ(iconv_start, 0)
2464 if (iconv_converting) {
2465 CRIT_ERR("You must stop your last iconv conversion before "
2466 "starting another");
2469 char iconv_from[CODEPAGE_LENGTH];
2470 char iconv_to[CODEPAGE_LENGTH];
2472 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2473 CRIT_ERR("Invalid arguments for iconv_start");
2477 new_iconv = iconv_open(iconv_to, iconv_from);
2478 if (new_iconv == (iconv_t) (-1)) {
2479 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2481 obj->a = register_iconv(&new_iconv);
2482 iconv_converting = 1;
2486 CRIT_ERR("Iconv requires arguments");
2488 END OBJ(iconv_stop, 0)
2489 iconv_converting = 0;
2492 END OBJ(totaldown, INFO_NET)
2494 obj->data.net = get_net_stat(arg);
2496 // default to DEFAULTNETDEV
2497 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2498 obj->data.net = get_net_stat(buf);
2501 END OBJ(totalup, INFO_NET)
2502 obj->data.net = get_net_stat(arg);
2504 obj->data.net = get_net_stat(arg);
2506 // default to DEFAULTNETDEV
2507 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2508 obj->data.net = get_net_stat(buf);
2512 END OBJ_IF(if_updatenr, 0)
2513 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2514 if(obj->data.ifblock.i == 0) CRIT_ERR("if_updatenr needs a number above 0 as argument");
2515 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2517 obj->data.i = arg ? atoi(arg) : 0;
2519 obj->data.i = arg ? atoi(arg) : 0;
2520 END OBJ(upspeed, INFO_NET)
2522 obj->data.net = get_net_stat(arg);
2524 // default to DEFAULTNETDEV
2525 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2526 obj->data.net = get_net_stat(buf);
2529 END OBJ(upspeedf, INFO_NET)
2531 obj->data.net = get_net_stat(arg);
2533 // default to DEFAULTNETDEV
2534 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2535 obj->data.net = get_net_stat(buf);
2540 END OBJ(upspeedgraph, INFO_NET)
2542 SIZE_DEFAULTS(graph);
2543 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2544 &obj->e, &obj->char_a, &obj->char_b);
2546 // default to DEFAULTNETDEV
2547 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2548 obj->data.net = get_net_stat(buf);
2551 END OBJ(uptime_short, INFO_UPTIME)
2552 END OBJ(uptime, INFO_UPTIME)
2553 END OBJ(user_names, INFO_USERS)
2554 END OBJ(user_times, INFO_USERS)
2555 END OBJ(user_terms, INFO_USERS)
2556 END OBJ(user_number, INFO_USERS)
2557 #if defined(__linux__)
2558 END OBJ(gw_iface, INFO_GW)
2559 END OBJ(gw_ip, INFO_GW)
2560 #endif /* !__linux__ */
2562 END OBJ(adt746xcpu, 0)
2563 END OBJ(adt746xfan, 0)
2564 #endif /* !__OpenBSD__ */
2565 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2566 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2567 END OBJ(apm_adapter, 0)
2568 END OBJ(apm_battery_life, 0)
2569 END OBJ(apm_battery_time, 0)
2570 #endif /* __FreeBSD__ */
2571 END OBJ_THREAD(imap_unseen, 0)
2574 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2579 END OBJ_THREAD(imap_messages, 0)
2582 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2587 END OBJ_THREAD(pop3_unseen, 0)
2590 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2595 END OBJ_THREAD(pop3_used, 0)
2598 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2606 obj->data.s = strndup(arg, text_buffer_size);
2608 ERR("smapi needs an argument");
2609 END OBJ_IF(if_smapi_bat_installed, 0)
2611 ERR("if_smapi_bat_installed needs an argument");
2612 obj->data.ifblock.s = 0;
2614 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2615 END OBJ(smapi_bat_perc, 0)
2617 obj->data.s = strndup(arg, text_buffer_size);
2619 ERR("smapi_bat_perc needs an argument");
2620 END OBJ(smapi_bat_temp, 0)
2622 obj->data.s = strndup(arg, text_buffer_size);
2624 ERR("smapi_bat_temp needs an argument");
2625 END OBJ(smapi_bat_power, 0)
2627 obj->data.s = strndup(arg, text_buffer_size);
2629 ERR("smapi_bat_power needs an argument");
2631 END OBJ(smapi_bat_bar, 0)
2635 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2636 ERR("first argument to smapi_bat_bar must be an integer value");
2640 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2643 ERR("smapi_bat_bar needs an argument");
2647 #define mpd_set_maxlen(name) \
2650 sscanf(arg, "%d", &i); \
2652 obj->data.i = i + 1; \
2654 ERR(#name ": invalid length argument"); \
2656 END OBJ(mpd_artist, INFO_MPD)
2657 mpd_set_maxlen(mpd_artist);
2659 END OBJ(mpd_title, INFO_MPD)
2660 mpd_set_maxlen(mpd_title);
2662 END OBJ(mpd_random, INFO_MPD) init_mpd();
2663 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2664 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2665 END OBJ(mpd_length, INFO_MPD) init_mpd();
2666 END OBJ(mpd_track, INFO_MPD)
2667 mpd_set_maxlen(mpd_track);
2669 END OBJ(mpd_name, INFO_MPD)
2670 mpd_set_maxlen(mpd_name);
2672 END OBJ(mpd_file, INFO_MPD)
2673 mpd_set_maxlen(mpd_file);
2675 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2676 END OBJ(mpd_album, INFO_MPD)
2677 mpd_set_maxlen(mpd_album);
2679 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2680 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2681 END OBJ(mpd_status, INFO_MPD) init_mpd();
2682 END OBJ(mpd_bar, INFO_MPD)
2684 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2686 END OBJ(mpd_smart, INFO_MPD)
2687 mpd_set_maxlen(mpd_smart);
2689 END OBJ_IF(if_mpd_playing, INFO_MPD)
2691 #undef mpd_set_maxlen
2694 END OBJ(moc_state, INFO_MOC)
2695 END OBJ(moc_file, INFO_MOC)
2696 END OBJ(moc_title, INFO_MOC)
2697 END OBJ(moc_artist, INFO_MOC)
2698 END OBJ(moc_song, INFO_MOC)
2699 END OBJ(moc_album, INFO_MOC)
2700 END OBJ(moc_totaltime, INFO_MOC)
2701 END OBJ(moc_timeleft, INFO_MOC)
2702 END OBJ(moc_curtime, INFO_MOC)
2703 END OBJ(moc_bitrate, INFO_MOC)
2704 END OBJ(moc_rate, INFO_MOC)
2707 END OBJ(xmms2_artist, INFO_XMMS2)
2708 END OBJ(xmms2_album, INFO_XMMS2)
2709 END OBJ(xmms2_title, INFO_XMMS2)
2710 END OBJ(xmms2_genre, INFO_XMMS2)
2711 END OBJ(xmms2_comment, INFO_XMMS2)
2712 END OBJ(xmms2_url, INFO_XMMS2)
2713 END OBJ(xmms2_tracknr, INFO_XMMS2)
2714 END OBJ(xmms2_bitrate, INFO_XMMS2)
2715 END OBJ(xmms2_date, INFO_XMMS2)
2716 END OBJ(xmms2_id, INFO_XMMS2)
2717 END OBJ(xmms2_duration, INFO_XMMS2)
2718 END OBJ(xmms2_elapsed, INFO_XMMS2)
2719 END OBJ(xmms2_size, INFO_XMMS2)
2720 END OBJ(xmms2_status, INFO_XMMS2)
2721 END OBJ(xmms2_percent, INFO_XMMS2)
2723 END OBJ(xmms2_bar, INFO_XMMS2)
2725 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2727 END OBJ(xmms2_smart, INFO_XMMS2)
2728 END OBJ(xmms2_playlist, INFO_XMMS2)
2729 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2730 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2733 END OBJ(audacious_status, INFO_AUDACIOUS)
2734 END OBJ(audacious_title, INFO_AUDACIOUS)
2736 sscanf(arg, "%d", &info.audacious.max_title_len);
2737 if (info.audacious.max_title_len > 0) {
2738 info.audacious.max_title_len++;
2740 CRIT_ERR("audacious_title: invalid length argument");
2743 END OBJ(audacious_length, INFO_AUDACIOUS)
2744 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2745 END OBJ(audacious_position, INFO_AUDACIOUS)
2746 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2747 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2748 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2749 END OBJ(audacious_channels, INFO_AUDACIOUS)
2750 END OBJ(audacious_filename, INFO_AUDACIOUS)
2751 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2752 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2753 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2755 END OBJ(audacious_bar, INFO_AUDACIOUS)
2757 scan_bar(arg, &obj->a, &obj->b);
2761 END OBJ(bmpx_title, INFO_BMPX)
2762 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2763 END OBJ(bmpx_artist, INFO_BMPX)
2764 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2765 END OBJ(bmpx_album, INFO_BMPX)
2766 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2767 END OBJ(bmpx_track, INFO_BMPX)
2768 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2769 END OBJ(bmpx_uri, INFO_BMPX)
2770 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2771 END OBJ(bmpx_bitrate, INFO_BMPX)
2772 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2778 char *userid = (char *) malloc(20 * sizeof(char));
2779 char *apikey = (char *) malloc(64 * sizeof(char));
2780 char *charid = (char *) malloc(20 * sizeof(char));
2782 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2783 obj->data.eve.charid = charid;
2784 obj->data.eve.userid = userid;
2785 obj->data.eve.apikey = apikey;
2789 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
2795 int argc, delay, act_par;
2796 unsigned int nrspaces = 0;
2797 char *uri = (char *) malloc(128 * sizeof(char));
2798 char *action = (char *) malloc(64 * sizeof(char));
2800 argc = sscanf(arg, "%127s %d %63s %d %u", uri, &delay, action,
2801 &act_par, &nrspaces);
2802 obj->data.rss.uri = uri;
2803 obj->data.rss.delay = delay;
2804 obj->data.rss.action = action;
2805 obj->data.rss.act_par = act_par;
2806 obj->data.rss.nrspaces = nrspaces;
2810 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
2811 "[act_par] [spaces in front]");
2815 END OBJ_THREAD(weather, 0)
2818 char *icao = (char *) malloc(5 * sizeof(char));
2819 char *uri = (char *) malloc(128 * sizeof(char));
2820 char *data_type = (char *) malloc(32 * sizeof(char));
2823 argc = sscanf(arg, "%119s %4s %31s %d", uri, icao, data_type, &interval);
2825 //icao MUST BE upper-case
2828 *tmp_p = toupper(*tmp_p);
2834 strcat(uri, ".TXT");
2835 obj->data.weather.uri = uri;
2837 obj->data.weather.data_type = data_type;
2839 // The data retrieval interval is limited to half an hour
2840 if (interval < 30) {
2843 obj->data.weather.interval = interval * 60; // convert to seconds
2846 CRIT_ERR("weather needs arguments: <uri> <icao> <data_type> [interval in minutes]");
2852 obj->data.s = strndup(arg, text_buffer_size);
2854 CRIT_ERR("lua needs arguments: <function name> [function parameters]");
2856 END OBJ(lua_parse, 0)
2858 obj->data.s = strndup(arg, text_buffer_size);
2860 CRIT_ERR("lua_parse needs arguments: <function name> [function parameters]");
2865 arg = scan_bar(arg, &obj->a, &obj->b);
2867 obj->data.s = strndup(arg, text_buffer_size);
2869 CRIT_ERR("lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2872 CRIT_ERR("lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2875 END OBJ(lua_graph, 0)
2876 SIZE_DEFAULTS(graph);
2879 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2880 &obj->e, &obj->char_a, &obj->char_b);
2884 CRIT_ERR("lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2887 CRIT_ERR("lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2889 END OBJ(lua_gauge, 0)
2890 SIZE_DEFAULTS(gauge);
2892 arg = scan_gauge(arg, &obj->a, &obj->b);
2894 obj->data.s = strndup(arg, text_buffer_size);
2896 CRIT_ERR("lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2899 CRIT_ERR("lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2902 #endif /* HAVE_LUA */
2905 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2906 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2907 ERR("hddtemp needs arguments");
2908 obj->type = OBJ_text;
2909 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2910 obj->data.hddtemp.update_time = 0;
2912 obj->data.hddtemp.temp = NULL;
2913 #endif /* HDDTEMP */
2914 #ifdef TCP_PORT_MONITOR
2915 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2916 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2917 #endif /* TCP_PORT_MONITOR */
2918 END OBJ(entropy_avail, INFO_ENTROPY)
2919 END OBJ(entropy_perc, INFO_ENTROPY)
2920 END OBJ(entropy_poolsize, INFO_ENTROPY)
2921 END OBJ(entropy_bar, INFO_ENTROPY)
2923 scan_bar(arg, &obj->a, &obj->b);
2926 obj->sub = malloc(sizeof(struct text_object));
2927 extract_variable_text_internal(obj->sub, arg, 0);
2929 CRIT_ERR("blink needs a argument");
2931 END OBJ(to_bytes, 0)
2933 obj->sub = malloc(sizeof(struct text_object));
2934 extract_variable_text_internal(obj->sub, arg, 0);
2936 CRIT_ERR("to_bytes needs a argument");
2941 obj->data.scroll.step = 1;
2942 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2943 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
2945 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2946 obj->data.scroll.start = 0;
2947 obj->sub = malloc(sizeof(struct text_object));
2948 extract_variable_text_internal(obj->sub,
2949 obj->data.scroll.text, 0);
2951 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
2956 unsigned int indenting = 0; //vars can be used as args for other vars
2959 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
2961 for (i=0; arg[i] != 0 && j < 2; i++) {
2962 if(startvar[j] == -1) {
2966 }else if(endvar[j] == -1) {
2969 }else if(arg[i] == '}') {
2972 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
2978 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
2979 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
2980 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
2981 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
2983 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
2984 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
2986 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
2987 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
2989 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
2990 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
2992 obj->sub = malloc(sizeof(struct text_object));
2993 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
2994 obj->sub->sub = malloc(sizeof(struct text_object));
2995 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
2997 CRIT_ERR("combine needs arguments: <text1> <text2>");
3000 CRIT_ERR("combine needs arguments: <text1> <text2>");
3005 CRIT_ERR("nvidia needs an argument\n");
3006 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3007 CRIT_ERR("nvidia: invalid argument"
3008 " specified: '%s'\n", arg);
3013 END OBJ(apcupsd, INFO_APCUPSD)
3017 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3018 CRIT_ERR("apcupsd needs arguments: <host> <port>");
3020 info.apcupsd.port = htons(port);
3021 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3024 CRIT_ERR("apcupsd needs arguments: <host> <port>");
3026 END OBJ(apcupsd_name, INFO_APCUPSD)
3027 END OBJ(apcupsd_model, INFO_APCUPSD)
3028 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3029 END OBJ(apcupsd_cable, INFO_APCUPSD)
3030 END OBJ(apcupsd_status, INFO_APCUPSD)
3031 END OBJ(apcupsd_linev, INFO_APCUPSD)
3032 END OBJ(apcupsd_load, INFO_APCUPSD)
3033 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3035 scan_bar(arg, &obj->a, &obj->b);
3037 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3039 SIZE_DEFAULTS(graph);
3040 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3041 &obj->e, &obj->char_a, &obj->char_b);
3043 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3044 SIZE_DEFAULTS(gauge);
3045 scan_gauge(arg, &obj->a, &obj->b);
3047 END OBJ(apcupsd_charge, INFO_APCUPSD)
3048 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3049 END OBJ(apcupsd_temp, INFO_APCUPSD)
3050 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3051 #endif /* APCUPSD */
3055 ERR("unknown variable %s", s);
3056 obj->type = OBJ_text;
3057 snprintf(buf, 256, "${%s}", s);
3058 obj->data.s = strndup(buf, text_buffer_size);
3065 static struct text_object *create_plain_text(const char *s)
3067 struct text_object *obj;
3069 if (s == NULL || *s == '\0') {
3073 obj = new_text_object_internal();
3075 obj->type = OBJ_text;
3076 obj->data.s = strndup(s, text_buffer_size);
3080 /* backslash_escape - do the actual substitution task for template objects
3082 * The field templates is used for substituting the \N occurences. Set it to
3083 * NULL to leave them as they are.
3085 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3089 unsigned int dup_idx = 0, dup_len;
3091 dup_len = strlen(src) + 1;
3092 src_dup = malloc(dup_len * sizeof(char));
3100 if (*(p + 1) == '\\') {
3101 src_dup[dup_idx++] = '\\';
3103 } else if (*(p + 1) == ' ') {
3104 src_dup[dup_idx++] = ' ';
3106 } else if (*(p + 1) == 'n') {
3107 src_dup[dup_idx++] = '\n';
3109 } else if (templates) {
3110 unsigned int tmpl_num;
3112 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3113 (tmpl_num > template_count))
3115 dup_len += strlen(templates[tmpl_num - 1]);
3116 src_dup = realloc(src_dup, dup_len * sizeof(char));
3117 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3118 dup_idx += strlen(templates[tmpl_num - 1]);
3123 src_dup[dup_idx++] = *p;
3128 src_dup[dup_idx] = '\0';
3129 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3133 /* handle_template_object - core logic of the template object
3135 * use config variables like this:
3136 * template1 = "$\1\2"
3137 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3139 * and use them like this:
3140 * ${template1 node name}
3141 * ${template2 root /}
3142 * ${template2 cdrom /mnt/cdrom}
3144 static char *handle_template(const char *tmpl, const char *args)
3146 char *args_dup = NULL;
3148 char **argsp = NULL;
3149 unsigned int argcnt = 0, template_idx, i;
3152 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3153 (template_idx >= MAX_TEMPLATES))
3157 args_dup = strdup(args);
3160 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3162 if (p > args_dup && *(p - 1) == '\\')
3165 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3171 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3172 argsp[argcnt - 1] = p_old;
3174 for (i = 0; i < argcnt; i++) {
3176 tmp = backslash_escape(argsp[i], NULL, 0);
3177 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3182 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3183 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3185 for (i = 0; i < argcnt; i++)
3191 static char *find_and_replace_templates(const char *inbuf)
3193 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3196 outlen = strlen(inbuf) + 1;
3197 o = outbuf = calloc(outlen, sizeof(char));
3198 memset(outbuf, 0, outlen * sizeof(char));
3200 p = indup = strdup(inbuf);
3202 while (*p && *p != '$')
3208 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3213 if (*(p + 1) == '{') {
3216 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3224 while (*p && stack > 0) {
3232 // stack is empty. that means the previous char was }, so we zero it
3235 // we ran into the end of string without finding a closing }, bark
3236 CRIT_ERR("cannot find a closing '}' in template expansion");
3240 while (*p && !isspace(*p))
3244 tmpl_out = handle_template(templ, args);
3246 outlen += strlen(tmpl_out);
3248 outbuf = realloc(outbuf, outlen * sizeof(char));
3249 strcat (outbuf, tmpl_out);
3251 o = outbuf + strlen(outbuf);
3253 ERR("failed to handle template '%s' with args '%s'", templ, args);
3257 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3262 static int text_contains_templates(const char *text)
3264 if (strcasestr(text, "${template") != NULL)
3266 if (strcasestr(text, "$template") != NULL)
3271 /* folds a string over top of itself, like so:
3273 * if start is "blah", and you call it with count = 1, the result will be "lah"
3275 static void strfold(char *start, int count)
3278 for (curplace = start + count; *curplace != 0; curplace++) {
3279 *(curplace - count) = *curplace;
3281 *(curplace - count) = 0;
3285 * - assumes that *string is '#'
3286 * - removes the part from '#' to the end of line ('\n' or '\0')
3287 * - it removes the '\n'
3288 * - copies the last char into 'char *last' argument, which should be a pointer
3289 * to a char rather than a string.
3291 static size_t remove_comment(char *string, char *last)
3294 while (*end != '\0' && *end != '\n') {
3297 if (last) *last = *end;
3298 if (*end == '\n') end++;
3299 strfold(string, end - string);
3300 return end - string;
3303 static size_t remove_comments(char *string)
3307 for (curplace = string; *curplace != 0; curplace++) {
3308 if (*curplace == '\\' && *(curplace + 1) == '#') {
3309 // strcpy can't be used for overlapping strings
3310 strfold(curplace, 1);
3312 } else if (*curplace == '#') {
3313 folded += remove_comment(curplace, 0);
3319 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3321 struct text_object *obj;
3322 char *p, *s, *orig_p;
3324 void *ifblock_opaque = NULL;
3329 p = strndup(const_p, max_user_text - 1);
3330 while (text_contains_templates(p)) {
3332 tmp = find_and_replace_templates(p);
3338 if (strcmp(p, const_p)) {
3339 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3341 DBGP("no templates to replace");
3344 memset(retval, 0, sizeof(struct text_object));
3346 line = global_text_lines;
3354 obj = create_plain_text(s);
3356 append_object(retval, obj);
3366 /* variable is either $foo or ${foo} */
3368 unsigned int brl = 1, brr = 0;
3372 while (*p && brl != brr) {
3387 while (*p && (isalnum((int) *p) || *p == '_')) {
3392 /* copy variable to buffer */
3393 len = (p - s > 255) ? 255 : (p - s);
3394 strncpy(buf, s, len);
3402 /* search for variable in environment */
3406 obj = create_plain_text(var);
3408 append_object(retval, obj);
3413 /* if variable wasn't found in environment, use some special */
3418 if (strchr(buf, ' ')) {
3419 arg = strchr(buf, ' ');
3422 while (isspace((int) *arg)) {
3430 /* lowercase variable name */
3433 *tmp_p = tolower(*tmp_p);
3437 obj = construct_text_object(buf, arg,
3438 line, allow_threaded,
3441 append_object(retval, obj);
3445 obj = create_plain_text("$");
3448 append_object(retval, obj);
3451 } else if (*p == '\\' && *(p+1) == '#') {
3453 } else if (*p == '#') {
3455 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3456 /* if remove_comment removed a newline, we need to 'back up' with p */
3462 obj = create_plain_text(s);
3464 append_object(retval, obj);
3467 if (!ifblock_stack_empty(&ifblock_opaque)) {
3468 ERR("one or more $endif's are missing");
3475 static void extract_variable_text(const char *p)
3477 free_text_objects(&global_root_object, 0);
3491 extract_variable_text_internal(&global_root_object, p, 1);
3494 void parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3496 extract_variable_text_internal(root, txt, 0);
3497 generate_text_internal(p, max_user_text, *root, cur);
3501 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3502 void *thread(void *), const char *text)
3504 if (obj->char_b && info.mail) {
3505 // this means we use info
3506 if (!info.mail->p_timed_thread) {
3507 info.mail->p_timed_thread =
3508 timed_thread_create(thread,
3509 (void *) info.mail, info.mail->interval * 1000000);
3510 if (!info.mail->p_timed_thread) {
3511 ERR("Error creating %s timed thread", text);
3513 timed_thread_register(info.mail->p_timed_thread,
3514 &info.mail->p_timed_thread);
3515 if (timed_thread_run(info.mail->p_timed_thread)) {
3516 ERR("Error running %s timed thread", text);
3520 } else if (obj->data.mail) {
3521 // this means we use obj
3522 if (!obj->data.mail->p_timed_thread) {
3523 obj->data.mail->p_timed_thread =
3524 timed_thread_create(thread,
3525 (void *) obj->data.mail,
3526 obj->data.mail->interval * 1000000);
3527 if (!obj->data.mail->p_timed_thread) {
3528 ERR("Error creating %s timed thread", text);
3530 timed_thread_register(obj->data.mail->p_timed_thread,
3531 &obj->data.mail->p_timed_thread);
3532 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3533 ERR("Error running %s timed thread", text);
3536 return obj->data.mail;
3537 } else if (!obj->a) {
3538 // something is wrong, warn once then stop
3539 ERR("There's a problem with your mail settings. "
3540 "Check that the global mail settings are properly defined"
3541 " (line %li).", obj->line);
3547 char *format_time(unsigned long timeval, const int width)
3550 unsigned long nt; // narrow time, for speed on 32-bit
3551 unsigned cc; // centiseconds
3552 unsigned nn; // multi-purpose whatever
3555 cc = nt % 100; // centiseconds past second
3556 nt /= 100; // total seconds
3557 nn = nt % 60; // seconds past the minute
3558 nt /= 60; // total minutes
3559 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3561 return strndup(buf, text_buffer_size);
3563 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3564 return strndup(buf, text_buffer_size);
3566 nn = nt % 60; // minutes past the hour
3567 nt /= 60; // total hours
3568 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3569 return strndup(buf, text_buffer_size);
3571 nn = nt; // now also hours
3572 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3573 return strndup(buf, text_buffer_size);
3575 nn /= 24; // now days
3576 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3577 return strndup(buf, text_buffer_size);
3579 nn /= 7; // now weeks
3580 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3581 return strndup(buf, text_buffer_size);
3583 // well shoot, this outta' fit...
3584 return strndup("<inf>", text_buffer_size);
3587 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3588 //string has to end with \0 and it's length should fit in a int
3590 void remove_deleted_chars(char *string){
3592 while(string[i] != 0){
3593 if(string[i] == BACKSPACE){
3595 strcpy( &(string[i-1]), &(string[i+1]) );
3597 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3602 static inline void format_media_player_time(char *buf, const int size,
3605 int days, hours, minutes;
3607 days = seconds / (24 * 60 * 60);
3608 seconds %= (24 * 60 * 60);
3609 hours = seconds / (60 * 60);
3610 seconds %= (60 * 60);
3611 minutes = seconds / 60;
3615 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3616 hours, minutes, seconds);
3617 } else if (hours > 0) {
3618 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3621 snprintf(buf, size, "%i:%02i", minutes, seconds);
3625 static inline double get_barnum(char *buf)
3637 if (sscanf(buf, "%lf", &barnum) == 0) {
3638 ERR("reading exec value failed (perhaps it's not the "
3639 "correct format?)");
3642 if (barnum > 100.0 || barnum < 0.0) {
3643 ERR("your exec value is not between 0 and 100, "
3644 "therefore it will be ignored");
3650 static void generate_text_internal(char *p, int p_max_size,
3651 struct text_object root, struct information *cur)
3653 struct text_object *obj;
3655 int need_to_load_fonts = 0;
3658 /* for the OBJ_top* handler */
3659 struct process **needed = 0;
3662 char buff_in[p_max_size];
3664 iconv_converting = 0;
3665 #endif /* HAVE_ICONV */
3669 while (obj && p_max_size > 0) {
3670 needed = 0; // reset for top stuff
3672 /* IFBLOCK jumping algorithm
3674 * This is easier as it looks like:
3675 * - each IF checks it's condition
3676 * - on FALSE: call DO_JUMP
3677 * - on TRUE: don't care
3678 * - each ELSE calls DO_JUMP unconditionally
3679 * - each ENDIF is silently being ignored
3682 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3683 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3684 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3685 * regularly, "obj" is being updated to point to obj->next, so object parsing
3686 * continues right after the corresponding ELSE or ENDIF. This means that if we
3687 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3688 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3689 * jumped, and there is nothing to do.
3693 obj = obj->data.ifblock.next; \
3696 #define OBJ(a) break; case OBJ_##a:
3698 switch (obj->type) {
3700 ERR("not implemented obj type %d", obj->type);
3703 struct sockaddr_in addr;
3704 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3706 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3708 memset(&addr, 0, sizeof(addr));
3709 addr.sin_family = AF_INET;
3710 addr.sin_port = obj->data.read_tcp.port;
3711 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3712 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3716 FD_SET(sock, &readfds);
3719 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3720 received = recv(sock, p, p_max_size, 0);
3725 ERR("read_tcp: Couldn't create a connection");
3728 ERR("read_tcp: Couldn't create a socket");
3731 ERR("read_tcp: Problem with resolving the hostname");
3736 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3738 #endif /* !__OpenBSD__ */
3741 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3742 obj->data.cpu_index);
3748 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3749 obj->data.cpu_index);
3751 /* OpenBSD has no such flag (SUSv2) */
3752 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3753 obj->data.cpu_index);
3754 #endif /* __OpenBSD */
3757 #if defined(__linux__)
3760 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3761 obj->data.cpu_index);
3766 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3767 obj->data.cpu_index);
3772 OBJ(wireless_essid) {
3773 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3775 OBJ(wireless_mode) {
3776 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3778 OBJ(wireless_bitrate) {
3779 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3782 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3784 OBJ(wireless_link_qual) {
3785 spaced_print(p, p_max_size, "%d", 4,
3786 obj->data.net->link_qual);
3788 OBJ(wireless_link_qual_max) {
3789 spaced_print(p, p_max_size, "%d", 4,
3790 obj->data.net->link_qual_max);
3792 OBJ(wireless_link_qual_perc) {
3793 if (obj->data.net->link_qual_max > 0) {
3794 spaced_print(p, p_max_size, "%.0f", 5,
3795 (double) obj->data.net->link_qual /
3796 obj->data.net->link_qual_max * 100);
3798 spaced_print(p, p_max_size, "unk", 5);
3801 OBJ(wireless_link_bar) {
3803 if(output_methods & TO_X) {
3804 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3805 obj->data.net->link_qual_max) * 255.0);
3808 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3809 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3810 obj->data.net->link_qual_max) * 100.0, obj->a);
3815 #endif /* HAVE_IWLIB */
3817 #endif /* __linux__ */
3821 get_adt746x_cpu(p, p_max_size);
3824 get_adt746x_fan(p, p_max_size);
3827 get_acpi_fan(p, p_max_size);
3829 OBJ(acpiacadapter) {
3830 get_acpi_ac_adapter(p, p_max_size);
3833 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3836 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3838 OBJ(battery_percent) {
3839 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3843 if(output_methods & TO_X) {
3844 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3847 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3848 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3853 OBJ(battery_short) {
3854 get_battery_short_status(p, p_max_size, obj->data.s);
3856 #endif /* __OpenBSD__ */
3859 human_readable(cur->buffers * 1024, p, 255);
3862 human_readable(cur->cached * 1024, p, 255);
3865 if (obj->data.cpu_index > info.cpu_count) {
3866 ERR("obj->data.cpu_index %i info.cpu_count %i",
3867 obj->data.cpu_index, info.cpu_count);
3868 CRIT_ERR("attempting to use more CPUs than you have!");
3870 percent_print(p, p_max_size,
3871 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3875 new_gauge(p, obj->a, obj->b,
3876 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3880 if(output_methods & TO_X) {
3881 new_bar(p, obj->a, obj->b,
3882 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3885 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3886 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3893 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3894 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3895 100, 1, obj->char_a, obj->char_b);
3898 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3899 obj->e, 1, obj->char_a, obj->char_b);
3902 new_fg(p, obj->data.l);
3935 OBJ(conky_version) {
3936 snprintf(p, p_max_size, "%s", VERSION);
3938 OBJ(conky_build_date) {
3939 snprintf(p, p_max_size, "%s", BUILD_DATE);
3941 OBJ(conky_build_arch) {
3942 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3944 #if defined(__linux__)
3946 snprintf(p, p_max_size, "%s",
3947 get_disk_protect_queue(obj->data.s));
3950 snprintf(p, p_max_size, "%s", i8k.version);
3953 snprintf(p, p_max_size, "%s", i8k.bios);
3956 snprintf(p, p_max_size, "%s", i8k.serial);
3961 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3962 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3964 OBJ(i8k_left_fan_status) {
3965 int left_fan_status;
3967 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3968 if (left_fan_status == 0) {
3969 snprintf(p, p_max_size, "off");
3971 if (left_fan_status == 1) {
3972 snprintf(p, p_max_size, "low");
3974 if (left_fan_status == 2) {
3975 snprintf(p, p_max_size, "high");
3978 OBJ(i8k_right_fan_status) {
3979 int right_fan_status;
3981 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3982 if (right_fan_status == 0) {
3983 snprintf(p, p_max_size, "off");
3985 if (right_fan_status == 1) {
3986 snprintf(p, p_max_size, "low");
3988 if (right_fan_status == 2) {
3989 snprintf(p, p_max_size, "high");
3992 OBJ(i8k_left_fan_rpm) {
3993 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3995 OBJ(i8k_right_fan_rpm) {
3996 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3998 OBJ(i8k_ac_status) {
4001 sscanf(i8k.ac_status, "%d", &ac_status);
4002 if (ac_status == -1) {
4003 snprintf(p, p_max_size, "disabled (read i8k docs)");
4005 if (ac_status == 0) {
4006 snprintf(p, p_max_size, "off");
4008 if (ac_status == 1) {
4009 snprintf(p, p_max_size, "on");
4012 OBJ(i8k_buttons_status) {
4013 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4017 get_ibm_acpi_fan(p, p_max_size);
4020 get_ibm_acpi_temps();
4021 temp_print(p, p_max_size,
4022 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4025 get_ibm_acpi_volume(p, p_max_size);
4027 OBJ(ibm_brightness) {
4028 get_ibm_acpi_brightness(p, p_max_size);
4031 /* information from sony_laptop kernel module
4032 * /sys/devices/platform/sony-laptop */
4033 OBJ(sony_fanspeed) {
4034 get_sony_fanspeed(p, p_max_size);
4037 if (!cur->gw_info.count) {
4042 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4045 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4048 snprintf(p, p_max_size, "%d", get_laptop_mode());
4051 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4053 #endif /* __linux__ */
4054 #if (defined(__FreeBSD__) || defined(__linux__))
4056 if ((obj->data.ifblock.s)
4057 && (!interface_up(obj->data.ifblock.s))) {
4063 OBJ(obsd_sensors_temp) {
4064 obsd_sensors.device = sensor_device;
4065 update_obsd_sensors();
4066 temp_print(p, p_max_size,
4067 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4070 OBJ(obsd_sensors_fan) {
4071 obsd_sensors.device = sensor_device;
4072 update_obsd_sensors();
4073 snprintf(p, p_max_size, "%d",
4074 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4076 OBJ(obsd_sensors_volt) {
4077 obsd_sensors.device = sensor_device;
4078 update_obsd_sensors();
4079 snprintf(p, p_max_size, "%.2f",
4080 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4083 get_obsd_vendor(p, p_max_size);
4086 get_obsd_product(p, p_max_size);
4088 #endif /* __OpenBSD__ */
4091 new_font(p, obj->data.s);
4092 need_to_load_fonts = 1;
4095 /* TODO: move this correction from kB to kB/s elsewhere
4096 * (or get rid of it??) */
4098 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4102 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4106 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4111 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4112 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4114 OBJ(diskiograph_read) {
4115 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4116 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4118 OBJ(diskiograph_write) {
4119 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4120 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4124 human_readable(obj->data.net->recv_speed, p, 255);
4127 spaced_print(p, p_max_size, "%.1f", 8,
4128 obj->data.net->recv_speed / 1024.0);
4131 OBJ(downspeedgraph) {
4132 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4133 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4137 /* Since we see you, you're if has not jumped.
4138 * Do Ninja jump here: without leaving traces.
4139 * This is to prevent us from stale jumped flags.
4141 obj = obj->data.ifblock.next;
4145 /* harmless object, just ignore */
4149 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4150 && (obj->data.net->addr.sa_data[3] & 255) == 0
4151 && (obj->data.net->addr.sa_data[4] & 255) == 0
4152 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4153 snprintf(p, p_max_size, "No Address");
4155 snprintf(p, p_max_size, "%u.%u.%u.%u",
4156 obj->data.net->addr.sa_data[2] & 255,
4157 obj->data.net->addr.sa_data[3] & 255,
4158 obj->data.net->addr.sa_data[4] & 255,
4159 obj->data.net->addr.sa_data[5] & 255);
4162 #if defined(__linux__)
4164 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4166 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4167 strcpy(p, obj->data.net->addrs);
4170 strcpy(p, "0.0.0.0");
4172 #endif /* __linux__ */
4173 #if defined(IMLIB2) && defined(X11)
4175 /* doesn't actually draw anything, just queues it omp. the
4176 * image will get drawn after the X event loop */
4177 cimlib_add_image(obj->data.s);
4181 evaluate(obj->data.s, p);
4184 read_exec(obj->data.s, p, text_buffer_size);
4185 remove_deleted_chars(p);
4188 struct information *tmp_info;
4189 struct text_object subroot;
4191 read_exec(obj->data.s, p, text_buffer_size);
4193 tmp_info = malloc(sizeof(struct information));
4194 memcpy(tmp_info, cur, sizeof(struct information));
4195 parse_conky_vars(&subroot, p, p, tmp_info);
4197 free_text_objects(&subroot, 1);
4204 read_exec(obj->data.s, p, text_buffer_size);
4205 barnum = get_barnum(p); /*using the same function*/
4207 if (barnum >= 0.0) {
4209 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4216 read_exec(obj->data.s, p, text_buffer_size);
4217 barnum = get_barnum(p);
4219 if (barnum >= 0.0) {
4221 if(output_methods & TO_X) {
4223 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4226 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4227 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4235 char showaslog = FALSE;
4236 char tempgrad = FALSE;
4238 char *cmd = obj->data.s;
4240 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4242 cmd += strlen(" "TEMPGRAD);
4244 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4246 cmd += strlen(" "LOGGRAPH);
4248 read_exec(cmd, p, text_buffer_size);
4249 barnum = get_barnum(p);
4252 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4253 100, 1, showaslog, tempgrad);
4258 if (current_update_time - obj->data.execi.last_update
4259 >= obj->data.execi.interval) {
4262 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4263 barnum = get_barnum(p);
4265 if (barnum >= 0.0) {
4268 obj->data.execi.last_update = current_update_time;
4271 if(output_methods & TO_X) {
4272 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4275 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4276 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4283 if (current_update_time - obj->data.execi.last_update
4284 >= obj->data.execi.interval) {
4286 char showaslog = FALSE;
4287 char tempgrad = FALSE;
4288 char *cmd = obj->data.execi.cmd;
4290 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4292 cmd += strlen(" "TEMPGRAD);
4294 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4296 cmd += strlen(" "LOGGRAPH);
4298 obj->char_a = showaslog;
4299 obj->char_b = tempgrad;
4300 read_exec(cmd, p, text_buffer_size);
4301 barnum = get_barnum(p);
4303 if (barnum >= 0.0) {
4306 obj->data.execi.last_update = current_update_time;
4308 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4311 if (current_update_time - obj->data.execi.last_update
4312 >= obj->data.execi.interval) {
4315 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4316 barnum = get_barnum(p);
4318 if (barnum >= 0.0) {
4319 obj->f = 255 * barnum / 100.0;
4321 obj->data.execi.last_update = current_update_time;
4323 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4327 if (current_update_time - obj->data.execi.last_update
4328 >= obj->data.execi.interval
4329 && obj->data.execi.interval != 0) {
4330 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4332 obj->data.execi.last_update = current_update_time;
4334 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4337 struct text_object subroot;
4338 struct information *tmp_info =
4339 malloc(sizeof(struct information));
4340 memcpy(tmp_info, cur, sizeof(struct information));
4342 if (current_update_time - obj->data.execi.last_update
4343 < obj->data.execi.interval
4344 || obj->data.execi.interval == 0) {
4345 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4347 char *output = obj->data.execi.buffer;
4348 FILE *fp = popen(obj->data.execi.cmd, "r");
4349 int length = fread(output, 1, text_buffer_size, fp);
4353 output[length] = '\0';
4354 if (length > 0 && output[length - 1] == '\n') {
4355 output[length - 1] = '\0';
4358 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4359 obj->data.execi.last_update = current_update_time;
4361 free_text_objects(&subroot, 1);
4365 if (!obj->data.texeci.p_timed_thread) {
4366 obj->data.texeci.p_timed_thread =
4367 timed_thread_create(&threaded_exec,
4368 (void *) obj, obj->data.texeci.interval * 1000000);
4369 if (!obj->data.texeci.p_timed_thread) {
4370 ERR("Error creating texeci timed thread");
4372 timed_thread_register(obj->data.texeci.p_timed_thread,
4373 &obj->data.texeci.p_timed_thread);
4374 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4375 ERR("Error running texeci timed thread");
4378 timed_thread_lock(obj->data.texeci.p_timed_thread);
4379 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4380 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4383 #endif /* HAVE_POPEN */
4385 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4387 if (mail && mail->p_timed_thread) {
4388 timed_thread_lock(mail->p_timed_thread);
4389 snprintf(p, p_max_size, "%lu", mail->unseen);
4390 timed_thread_unlock(mail->p_timed_thread);
4393 OBJ(imap_messages) {
4394 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4396 if (mail && mail->p_timed_thread) {
4397 timed_thread_lock(mail->p_timed_thread);
4398 snprintf(p, p_max_size, "%lu", mail->messages);
4399 timed_thread_unlock(mail->p_timed_thread);
4403 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4405 if (mail && mail->p_timed_thread) {
4406 timed_thread_lock(mail->p_timed_thread);
4407 snprintf(p, p_max_size, "%lu", mail->unseen);
4408 timed_thread_unlock(mail->p_timed_thread);
4412 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4414 if (mail && mail->p_timed_thread) {
4415 timed_thread_lock(mail->p_timed_thread);
4416 snprintf(p, p_max_size, "%.1f",
4417 mail->used / 1024.0 / 1024.0);
4418 timed_thread_unlock(mail->p_timed_thread);
4422 if (obj->data.fs != NULL) {
4423 if (obj->data.fs->size == 0) {
4425 if(output_methods & TO_X) {
4426 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4429 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4430 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4436 if(output_methods & TO_X) {
4437 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4438 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4439 obj->data.fs->size));
4442 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4443 new_bar_in_shell(p, p_max_size,
4444 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4452 if (obj->data.fs != NULL) {
4453 human_readable(obj->data.fs->avail, p, 255);
4457 if (obj->data.fs != NULL) {
4460 if (obj->data.fs->size) {
4461 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4464 percent_print(p, p_max_size, val);
4468 if (obj->data.fs != NULL) {
4469 human_readable(obj->data.fs->size, p, 255);
4473 if (obj->data.fs != NULL)
4474 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4477 if (obj->data.fs != NULL) {
4478 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4483 if (obj->data.fs != NULL) {
4484 if (obj->data.fs->size == 0) {
4486 if(output_methods & TO_X) {
4487 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4490 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4491 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4497 if(output_methods & TO_X) {
4498 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4499 (int) (obj->data.fsbar.fs->avail * 255 /
4500 obj->data.fs->size));
4503 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4504 new_bar_in_shell(p, p_max_size,
4505 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4513 if (obj->data.fs != NULL) {
4516 if (obj->data.fs->size) {
4517 val = obj->data.fs->free
4522 percent_print(p, p_max_size, 100 - val);
4526 float *v = info.loadavg;
4528 if (obj->data.loadavg[2]) {
4529 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4530 v[obj->data.loadavg[0] - 1],
4531 v[obj->data.loadavg[1] - 1],
4532 v[obj->data.loadavg[2] - 1]);
4533 } else if (obj->data.loadavg[1]) {
4534 snprintf(p, p_max_size, "%.2f %.2f",
4535 v[obj->data.loadavg[0] - 1],
4536 v[obj->data.loadavg[1] - 1]);
4537 } else if (obj->data.loadavg[0]) {
4538 snprintf(p, p_max_size, "%.2f",
4539 v[obj->data.loadavg[0] - 1]);
4543 new_goto(p, obj->data.i);
4546 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4550 new_hr(p, obj->data.i);
4554 if (cur->nameserver_info.nscount > obj->data.i)
4555 snprintf(p, p_max_size, "%s",
4556 cur->nameserver_info.ns_list[obj->data.i]);
4560 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4561 snprintf(p, p_max_size, "%s", skill);
4566 PRSS *data = get_rss_info(obj->data.rss.uri,
4567 obj->data.rss.delay);
4571 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4573 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4575 // remove trailing new line if one exists
4576 if (str[strlen(str) - 1] == '\n') {
4577 str[strlen(str) - 1] = 0;
4579 snprintf(p, p_max_size, "%s", str);
4580 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4581 if (obj->data.rss.act_par < data->item_count) {
4582 str = data->items[obj->data.rss.act_par].title;
4583 // remove trailing new line if one exists
4584 if (str[strlen(str) - 1] == '\n') {
4585 str[strlen(str) - 1] = 0;
4587 snprintf(p, p_max_size, "%s", str);
4589 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4590 if (obj->data.rss.act_par < data->item_count) {
4592 data->items[obj->data.rss.act_par].description;
4593 // remove trailing new line if one exists
4594 if (str[strlen(str) - 1] == '\n') {
4595 str[strlen(str) - 1] = 0;
4597 snprintf(p, p_max_size, "%s", str);
4599 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4600 if (data->item_count > 0) {
4603 //'tmpspaces' is a string with spaces too be placed in front of each title
4604 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4605 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4606 tmpspaces[obj->data.rss.nrspaces]=0;
4610 if (obj->data.rss.act_par > data->item_count) {
4611 show = data->item_count;
4613 show = obj->data.rss.act_par;
4615 for (itmp = 0; itmp < show; itmp++) {
4616 PRSS_Item *item = &data->items[itmp];
4620 // don't add new line before first item
4622 strncat(p, "\n", p_max_size);
4624 /* remove trailing new line if one exists,
4625 * we have our own */
4626 if (str[strlen(str) - 1] == '\n') {
4627 str[strlen(str) - 1] = 0;
4629 strncat(p, tmpspaces, p_max_size);
4630 strncat(p, str, p_max_size);
4641 process_weather_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4646 char *str = llua_getstring(obj->data.s);
4648 snprintf(p, p_max_size, "%s", str);
4653 char *str = llua_getstring(obj->data.s);
4660 if (llua_getnumber(obj->data.s, &per)) {
4662 if(output_methods & TO_X) {
4663 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4666 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4667 new_bar_in_shell(p, p_max_size, per, obj->a);
4676 if (llua_getnumber(obj->data.s, &per)) {
4677 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4678 per, obj->e, 1, obj->char_a, obj->char_b);
4683 if (llua_getnumber(obj->data.s, &per)) {
4684 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4688 #endif /* HAVE_LUA */
4693 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4694 if (obj->data.hddtemp.temp)
4695 free(obj->data.hddtemp.temp);
4696 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4697 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4698 obj->data.hddtemp.update_time = current_update_time;
4700 if (!obj->data.hddtemp.temp) {
4701 snprintf(p, p_max_size, "N/A");
4703 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4704 unit = obj->data.hddtemp.temp[0];
4706 if (*endptr != '\0')
4707 snprintf(p, p_max_size, "N/A");
4708 else if (unit == 'C')
4709 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4710 else if (unit == 'F')
4711 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4713 snprintf(p, p_max_size, "N/A");
4718 new_offset(p, obj->data.i);
4721 new_voffset(p, obj->data.i);
4727 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4728 obj->data.sysfs.devtype, obj->data.sysfs.type);
4730 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4732 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4733 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4734 } else if (r >= 100.0 || r == 0) {
4735 snprintf(p, p_max_size, "%d", (int) r);
4737 snprintf(p, p_max_size, "%.1f", r);
4743 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4744 obj->data.sysfs.devtype, obj->data.sysfs.type);
4746 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4748 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4749 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4750 } else if (r >= 100.0 || r == 0) {
4751 snprintf(p, p_max_size, "%d", (int) r);
4753 snprintf(p, p_max_size, "%.1f", r);
4759 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4760 obj->data.sysfs.devtype, obj->data.sysfs.type);
4762 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4764 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4765 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4766 } else if (r >= 100.0 || r == 0) {
4767 snprintf(p, p_max_size, "%d", (int) r);
4769 snprintf(p, p_max_size, "%.1f", r);
4772 #endif /* __linux__ */
4774 new_alignr(p, obj->data.i);
4777 new_alignc(p, obj->data.i);
4780 char buf[max_user_text];
4781 struct information *tmp_info =
4782 malloc(sizeof(struct information));
4783 memcpy(tmp_info, cur, sizeof(struct information));
4784 generate_text_internal(buf, max_user_text,
4785 *obj->sub, tmp_info);
4787 if (strlen(buf) != 0) {
4793 char expression[max_user_text];
4795 struct information *tmp_info;
4797 tmp_info = malloc(sizeof(struct information));
4798 memcpy(tmp_info, cur, sizeof(struct information));
4799 generate_text_internal(expression, max_user_text,
4800 *obj->sub, tmp_info);
4801 DBGP("parsed arg into '%s'", expression);
4803 val = compare(expression);
4805 ERR("compare failed for expression '%s'",
4813 if (obj->data.ifblock.str
4814 && !check_contains(obj->data.ifblock.s,
4815 obj->data.ifblock.str)) {
4817 } else if (obj->data.ifblock.s
4818 && access(obj->data.ifblock.s, F_OK)) {
4823 if ((obj->data.ifblock.s)
4824 && (!check_mount(obj->data.ifblock.s))) {
4830 if (!get_process_by_name(obj->data.ifblock.s)) {
4832 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4837 #if defined(__linux__)
4839 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4843 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4846 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4851 human_readable(cur->mem * 1024, p, 255);
4854 human_readable(cur->memeasyfree * 1024, p, 255);
4857 human_readable(cur->memfree * 1024, p, 255);
4860 human_readable(cur->memmax * 1024, p, 255);
4864 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4868 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4869 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4874 if(output_methods & TO_X) {
4875 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4876 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4879 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4880 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4887 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4888 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4889 100, 1, obj->char_a, obj->char_b);
4894 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4897 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4900 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4904 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4905 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4908 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4909 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4912 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4913 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4916 OBJ(if_mixer_mute) {
4917 if (!mixer_is_mute(obj->data.ifblock.i)) {
4923 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4925 OBJ(monitor_number) {
4926 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4929 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
4931 OBJ(desktop_number) {
4932 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
4935 if(cur->x11.desktop.name != NULL) {
4936 strncpy(p, cur->x11.desktop.name, p_max_size);
4943 update_mail_count(&obj->data.local_mail);
4944 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4947 update_mail_count(&obj->data.local_mail);
4948 snprintf(p, p_max_size, "%d",
4949 obj->data.local_mail.new_mail_count);
4952 update_mail_count(&obj->data.local_mail);
4953 snprintf(p, p_max_size, "%d",
4954 obj->data.local_mail.seen_mail_count);
4957 update_mail_count(&obj->data.local_mail);
4958 snprintf(p, p_max_size, "%d",
4959 obj->data.local_mail.unseen_mail_count);
4961 OBJ(flagged_mails) {
4962 update_mail_count(&obj->data.local_mail);
4963 snprintf(p, p_max_size, "%d",
4964 obj->data.local_mail.flagged_mail_count);
4966 OBJ(unflagged_mails) {
4967 update_mail_count(&obj->data.local_mail);
4968 snprintf(p, p_max_size, "%d",
4969 obj->data.local_mail.unflagged_mail_count);
4971 OBJ(forwarded_mails) {
4972 update_mail_count(&obj->data.local_mail);
4973 snprintf(p, p_max_size, "%d",
4974 obj->data.local_mail.forwarded_mail_count);
4976 OBJ(unforwarded_mails) {
4977 update_mail_count(&obj->data.local_mail);
4978 snprintf(p, p_max_size, "%d",
4979 obj->data.local_mail.unforwarded_mail_count);
4981 OBJ(replied_mails) {
4982 update_mail_count(&obj->data.local_mail);
4983 snprintf(p, p_max_size, "%d",
4984 obj->data.local_mail.replied_mail_count);
4986 OBJ(unreplied_mails) {
4987 update_mail_count(&obj->data.local_mail);
4988 snprintf(p, p_max_size, "%d",
4989 obj->data.local_mail.unreplied_mail_count);
4992 update_mail_count(&obj->data.local_mail);
4993 snprintf(p, p_max_size, "%d",
4994 obj->data.local_mail.draft_mail_count);
4996 OBJ(trashed_mails) {
4997 update_mail_count(&obj->data.local_mail);
4998 snprintf(p, p_max_size, "%d",
4999 obj->data.local_mail.trashed_mail_count);
5002 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5004 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5007 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5010 new_outline(p, obj->data.l);
5013 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5015 OBJ(running_processes) {
5016 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5019 snprintf(p, p_max_size, "%s", obj->data.s);
5023 new_bg(p, obj->data.l);
5026 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5030 human_readable(cur->swap * 1024, p, 255);
5033 human_readable(cur->swapfree * 1024, p, 255);
5036 human_readable(cur->swapmax * 1024, p, 255);
5039 if (cur->swapmax == 0) {
5040 strncpy(p, "No swap", p_max_size);
5042 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5047 if(output_methods & TO_X) {
5048 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5049 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5052 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5053 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5059 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5062 time_t t = time(NULL);
5063 struct tm *tm = localtime(&t);
5065 setlocale(LC_TIME, "");
5066 strftime(p, p_max_size, obj->data.s, tm);
5069 time_t t = time(NULL);
5070 struct tm *tm = gmtime(&t);
5072 strftime(p, p_max_size, obj->data.s, tm);
5079 if (obj->data.tztime.tz) {
5080 oldTZ = getenv("TZ");
5081 setenv("TZ", obj->data.tztime.tz, 1);
5087 setlocale(LC_TIME, "");
5088 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5090 setenv("TZ", oldTZ, 1);
5095 // Needless to free oldTZ since getenv gives ptr to static data
5098 human_readable(obj->data.net->recv, p, 255);
5101 human_readable(obj->data.net->trans, p, 255);
5104 snprintf(p, p_max_size, "%d", total_updates);
5107 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5112 human_readable(obj->data.net->trans_speed, p, 255);
5115 spaced_print(p, p_max_size, "%.1f", 8,
5116 obj->data.net->trans_speed / 1024.0);
5120 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5121 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5125 format_seconds_short(p, p_max_size, (int) cur->uptime);
5128 format_seconds(p, p_max_size, (int) cur->uptime);
5131 snprintf(p, p_max_size, "%s", cur->users.names);
5134 snprintf(p, p_max_size, "%s", cur->users.terms);
5137 snprintf(p, p_max_size, "%s", cur->users.times);
5140 snprintf(p, p_max_size, "%d", cur->users.number);
5142 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5143 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5147 msg = get_apm_adapter();
5148 snprintf(p, p_max_size, "%s", msg);
5151 OBJ(apm_battery_life) {
5154 msg = get_apm_battery_life();
5155 snprintf(p, p_max_size, "%s", msg);
5158 OBJ(apm_battery_time) {
5161 msg = get_apm_battery_time();
5162 snprintf(p, p_max_size, "%s", msg);
5165 #endif /* __FreeBSD__ __OpenBSD__ */
5168 #define mpd_printf(fmt, val) \
5169 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5170 #define mpd_sprintf(val) { \
5171 if (!obj->data.i || obj->data.i > p_max_size) \
5172 mpd_printf("%s", val); \
5174 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5179 mpd_sprintf(artist);
5183 mpd_printf("%s", random);
5185 mpd_printf("%s", repeat);
5193 mpd_printf("%d", volume);
5195 mpd_printf("%d", bitrate);
5197 mpd_printf("%s", status);
5199 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5202 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5205 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5209 if(output_methods & TO_X) {
5210 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5211 (int) (mpd_get_info()->progress * 255.0f));
5214 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5215 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5221 struct mpd_s *mpd = mpd_get_info();
5222 int len = obj->data.i;
5223 if (len == 0 || len > p_max_size)
5226 memset(p, 0, p_max_size);
5227 if (mpd->artist && *mpd->artist &&
5228 mpd->title && *mpd->title) {
5229 snprintf(p, len, "%s - %s", mpd->artist,
5231 } else if (mpd->title && *mpd->title) {
5232 snprintf(p, len, "%s", mpd->title);
5233 } else if (mpd->artist && *mpd->artist) {
5234 snprintf(p, len, "%s", mpd->artist);
5235 } else if (mpd->file && *mpd->file) {
5236 snprintf(p, len, "%s", mpd->file);
5241 OBJ(if_mpd_playing) {
5242 if (!mpd_get_info()->is_playing) {
5251 #define MOC_PRINT(t, a) \
5252 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5254 MOC_PRINT(state, "??");
5257 MOC_PRINT(file, "no file");
5260 MOC_PRINT(title, "no title");
5263 MOC_PRINT(artist, "no artist");
5266 MOC_PRINT(song, "no song");
5269 MOC_PRINT(album, "no album");
5271 OBJ(moc_totaltime) {
5272 MOC_PRINT(totaltime, "0:00");
5275 MOC_PRINT(timeleft, "0:00");
5278 MOC_PRINT(curtime, "0:00");
5281 MOC_PRINT(bitrate, "0Kbps");
5284 MOC_PRINT(rate, "0KHz");
5290 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5293 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5296 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5299 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5301 OBJ(xmms2_comment) {
5302 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5305 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5308 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5311 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5313 OBJ(xmms2_tracknr) {
5314 if (cur->xmms2.tracknr != -1) {
5315 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5318 OBJ(xmms2_bitrate) {
5319 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5322 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5325 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5327 OBJ(xmms2_elapsed) {
5328 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5329 (cur->xmms2.elapsed / 1000) % 60);
5331 OBJ(xmms2_duration) {
5332 snprintf(p, p_max_size, "%02d:%02d",
5333 cur->xmms2.duration / 60000,
5334 (cur->xmms2.duration / 1000) % 60);
5336 OBJ(xmms2_percent) {
5337 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5341 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5342 (int) (cur->xmms2.progress * 255.0f));
5345 OBJ(xmms2_playlist) {
5346 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5348 OBJ(xmms2_timesplayed) {
5349 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5352 if (strlen(cur->xmms2.title) < 2
5353 && strlen(cur->xmms2.title) < 2) {
5354 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5356 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5360 OBJ(if_xmms2_connected) {
5361 if (cur->xmms2.conn_state != 1) {
5367 OBJ(audacious_status) {
5368 snprintf(p, p_max_size, "%s",
5369 cur->audacious.items[AUDACIOUS_STATUS]);
5371 OBJ(audacious_title) {
5372 snprintf(p, cur->audacious.max_title_len > 0
5373 ? cur->audacious.max_title_len : p_max_size, "%s",
5374 cur->audacious.items[AUDACIOUS_TITLE]);
5376 OBJ(audacious_length) {
5377 snprintf(p, p_max_size, "%s",
5378 cur->audacious.items[AUDACIOUS_LENGTH]);
5380 OBJ(audacious_length_seconds) {
5381 snprintf(p, p_max_size, "%s",
5382 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5384 OBJ(audacious_position) {
5385 snprintf(p, p_max_size, "%s",
5386 cur->audacious.items[AUDACIOUS_POSITION]);
5388 OBJ(audacious_position_seconds) {
5389 snprintf(p, p_max_size, "%s",
5390 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5392 OBJ(audacious_bitrate) {
5393 snprintf(p, p_max_size, "%s",
5394 cur->audacious.items[AUDACIOUS_BITRATE]);
5396 OBJ(audacious_frequency) {
5397 snprintf(p, p_max_size, "%s",
5398 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5400 OBJ(audacious_channels) {
5401 snprintf(p, p_max_size, "%s",
5402 cur->audacious.items[AUDACIOUS_CHANNELS]);
5404 OBJ(audacious_filename) {
5405 snprintf(p, p_max_size, "%s",
5406 cur->audacious.items[AUDACIOUS_FILENAME]);
5408 OBJ(audacious_playlist_length) {
5409 snprintf(p, p_max_size, "%s",
5410 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5412 OBJ(audacious_playlist_position) {
5413 snprintf(p, p_max_size, "%s",
5414 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5416 OBJ(audacious_main_volume) {
5417 snprintf(p, p_max_size, "%s",
5418 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5421 OBJ(audacious_bar) {
5425 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5426 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5427 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5430 #endif /* AUDACIOUS */
5434 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5437 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5440 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5443 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5446 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5449 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5452 /* we have four different types of top (top, top_mem,
5453 * top_time and top_io). To avoid having almost-same code four
5454 * times, we have this special handler. */
5457 parse_top_args("top", obj->data.top.s, obj);
5458 if (!needed) needed = cur->cpu;
5460 parse_top_args("top_mem", obj->data.top.s, obj);
5461 if (!needed) needed = cur->memu;
5463 parse_top_args("top_time", obj->data.top.s, obj);
5464 if (!needed) needed = cur->time;
5467 parse_top_args("top_io", obj->data.top.s, obj);
5468 if (!needed) needed = cur->io;
5471 if (needed[obj->data.top.num]) {
5474 switch (obj->data.top.type) {
5476 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5477 needed[obj->data.top.num]->name);
5480 snprintf(p, 7, "%6.2f",
5481 needed[obj->data.top.num]->amount);
5484 snprintf(p, 6, "%5i",
5485 needed[obj->data.top.num]->pid);
5488 snprintf(p, 7, "%6.2f",
5489 needed[obj->data.top.num]->totalmem);
5492 timeval = format_time(
5493 needed[obj->data.top.num]->total_cpu_time, 9);
5494 snprintf(p, 10, "%9s", timeval);
5498 human_readable(needed[obj->data.top.num]->rss,
5502 human_readable(needed[obj->data.top.num]->vsize,
5506 case TOP_READ_BYTES:
5507 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5510 case TOP_WRITE_BYTES:
5511 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5515 snprintf(p, 7, "%6.2f",
5516 needed[obj->data.top.num]->io_perc);
5522 print_tail_object(obj, p, p_max_size);
5524 print_head_object(obj, p, p_max_size);
5526 FILE *fp = open_file(obj->data.s, &obj->a);
5529 /* FIXME: use something more general (see also tail.c, head.c */
5530 #define BUFSZ 0x1000
5535 while(fgets(buf, BUFSZ, fp) != NULL){
5536 for(j = 0; buf[j] != 0; j++) {
5537 if(buf[j] == '\n') {
5542 sprintf(p, "%d", lines);
5545 sprintf(p, "File Unreadable");
5550 FILE *fp = open_file(obj->data.s, &obj->a);
5555 char inword = FALSE;
5558 while(fgets(buf, BUFSZ, fp) != NULL){
5559 for(j = 0; buf[j] != 0; j++) {
5560 if(!isspace(buf[j])) {
5561 if(inword == FALSE) {
5570 sprintf(p, "%d", words);
5573 sprintf(p, "File Unreadable");
5576 #ifdef TCP_PORT_MONITOR
5578 tcp_portmon_action(p, p_max_size,
5579 &obj->data.tcp_port_monitor);
5581 #endif /* TCP_PORT_MONITOR */
5585 iconv_converting = 1;
5586 iconv_selected = obj->a;
5589 iconv_converting = 0;
5592 #endif /* HAVE_ICONV */
5594 OBJ(entropy_avail) {
5595 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5598 percent_print(p, p_max_size,
5599 cur->entropy.entropy_avail *
5600 100 / cur->entropy.poolsize);
5602 OBJ(entropy_poolsize) {
5603 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5606 double entropy_perc;
5608 entropy_perc = (double) cur->entropy.entropy_avail /
5609 (double) cur->entropy.poolsize;
5611 if(output_methods & TO_X) {
5612 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5615 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5616 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5625 s = smapi_get_val(obj->data.s);
5626 snprintf(p, p_max_size, "%s", s);
5630 OBJ(if_smapi_bat_installed) {
5632 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5633 if(!smapi_bat_installed(idx)) {
5637 ERR("argument to if_smapi_bat_installed must be an integer");
5639 OBJ(smapi_bat_perc) {
5641 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5642 val = smapi_bat_installed(idx) ?
5643 smapi_get_bat_int(idx, "remaining_percent") : 0;
5644 percent_print(p, p_max_size, val);
5646 ERR("argument to smapi_bat_perc must be an integer");
5648 OBJ(smapi_bat_temp) {
5650 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5651 val = smapi_bat_installed(idx) ?
5652 smapi_get_bat_int(idx, "temperature") : 0;
5653 /* temperature is in milli degree celsius */
5654 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5656 ERR("argument to smapi_bat_temp must be an integer");
5658 OBJ(smapi_bat_power) {
5660 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5661 val = smapi_bat_installed(idx) ?
5662 smapi_get_bat_int(idx, "power_now") : 0;
5663 /* power_now is in mW, set to W with one digit precision */
5664 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5666 ERR("argument to smapi_bat_power must be an integer");
5669 OBJ(smapi_bat_bar) {
5670 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5671 new_bar(p, obj->a, obj->b, (int)
5672 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5674 new_bar(p, obj->a, obj->b, 0);
5679 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5680 char buf[max_user_text];
5683 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5684 snprintf(p, p_max_size, "%s", buf);
5685 if(total_updates % 2) {
5686 for(j=0; p[j] != 0; j++) {
5692 char buf[max_user_text];
5694 char unit[16]; //16 because we can also have long names (like mega-bytes)
5696 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5697 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5698 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5699 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5700 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5701 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5702 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5704 snprintf(p, p_max_size, "%s", buf);
5708 char *tmp, buf[max_user_text];
5709 generate_text_internal(buf, max_user_text,
5712 if (strlen(buf) <= obj->data.scroll.show) {
5713 snprintf(p, p_max_size, "%s", buf);
5716 #define LINESEPARATOR '|'
5717 //place all the lines behind each other with LINESEPARATOR between them
5718 for(j = 0; buf[j] != 0; j++) {
5720 buf[j]=LINESEPARATOR;
5723 //scroll the output obj->data.scroll.start places by copying that many chars from
5724 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5725 //at the back of the string
5726 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5727 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5728 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5729 buf[j - obj->data.scroll.start] = buf[j];
5731 strcpy(&buf[j - obj->data.scroll.start], tmp);
5733 //only show the requested number of chars
5734 if(obj->data.scroll.show < j) {
5735 buf[obj->data.scroll.show] = 0;
5737 //next time, scroll a place more or reset scrolling if we are at the end
5738 obj->data.scroll.start += obj->data.scroll.step;
5739 if(obj->data.scroll.start >= j){
5740 obj->data.scroll.start = 0;
5742 snprintf(p, p_max_size, "%s", buf);
5745 char buf[2][max_user_text];
5752 struct llrows* next;
5754 struct llrows *ll_rows[2], *current[2];
5755 struct text_object * objsub = obj->sub;
5758 for(i=0; i<2; i++) {
5761 ll_rows[i] = malloc(sizeof(struct llrows));
5762 current[i] = ll_rows[i];
5763 for(j=0; j<i; j++) objsub = objsub->sub;
5764 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5765 for(j=0; buf[i][j] != 0; j++) {
5766 if(buf[i][j] == '\t') buf[i][j] = ' ';
5767 if(buf[i][j] == '\n') {
5769 current[i]->row = strdup(buf[i]+nextstart);
5770 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5771 current[i]->next = malloc(sizeof(struct llrows));
5772 current[i] = current[i]->next;
5777 current[i]->row = strdup(buf[i]+nextstart);
5778 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5779 current[i]->next = NULL;
5780 current[i] = ll_rows[i];
5782 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5784 strcat(p, current[0]->row);
5785 i=strlen(current[0]->row);
5787 while(i < longest) {
5792 strcat(p, obj->data.combine.seperation);
5793 strcat(p, current[1]->row);
5797 #pragma omp parallel for
5798 #endif /* HAVE_OPENMP */
5799 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5802 #pragma omp parallel for
5803 #endif /* HAVE_OPENMP */
5804 for(i=0; i<2; i++) {
5805 while(ll_rows[i] != NULL) {
5806 current[i]=ll_rows[i];
5807 free(current[i]->row);
5808 ll_rows[i]=current[i]->next;
5815 int value = get_nvidia_value(obj->data.nvidia.type, display);
5817 snprintf(p, p_max_size, "N/A");
5818 else if (obj->data.nvidia.type == NV_TEMP)
5819 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5820 else if (obj->data.nvidia.print_as_float &&
5821 value > 0 && value < 100)
5822 snprintf(p, p_max_size, "%.1f", (float)value);
5824 snprintf(p, p_max_size, "%d", value);
5829 /* This is just a meta-object to set host:port */
5832 snprintf(p, p_max_size, "%s",
5833 cur->apcupsd.items[APCUPSD_NAME]);
5835 OBJ(apcupsd_model) {
5836 snprintf(p, p_max_size, "%s",
5837 cur->apcupsd.items[APCUPSD_MODEL]);
5839 OBJ(apcupsd_upsmode) {
5840 snprintf(p, p_max_size, "%s",
5841 cur->apcupsd.items[APCUPSD_UPSMODE]);
5843 OBJ(apcupsd_cable) {
5844 snprintf(p, p_max_size, "%s",
5845 cur->apcupsd.items[APCUPSD_CABLE]);
5847 OBJ(apcupsd_status) {
5848 snprintf(p, p_max_size, "%s",
5849 cur->apcupsd.items[APCUPSD_STATUS]);
5851 OBJ(apcupsd_linev) {
5852 snprintf(p, p_max_size, "%s",
5853 cur->apcupsd.items[APCUPSD_LINEV]);
5856 snprintf(p, p_max_size, "%s",
5857 cur->apcupsd.items[APCUPSD_LOAD]);
5859 OBJ(apcupsd_loadbar) {
5862 if(output_methods & TO_X) {
5863 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5864 new_bar(p, obj->a, obj->b, (int) progress);
5867 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5868 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5869 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5875 OBJ(apcupsd_loadgraph) {
5877 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5878 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5879 (int)progress, 100, 1, obj->char_a, obj->char_b);
5881 OBJ(apcupsd_loadgauge) {
5883 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5884 new_gauge(p, obj->a, obj->b,
5888 OBJ(apcupsd_charge) {
5889 snprintf(p, p_max_size, "%s",
5890 cur->apcupsd.items[APCUPSD_CHARGE]);
5892 OBJ(apcupsd_timeleft) {
5893 snprintf(p, p_max_size, "%s",
5894 cur->apcupsd.items[APCUPSD_TIMELEFT]);
5897 snprintf(p, p_max_size, "%s",
5898 cur->apcupsd.items[APCUPSD_TEMP]);
5900 OBJ(apcupsd_lastxfer) {
5901 snprintf(p, p_max_size, "%s",
5902 cur->apcupsd.items[APCUPSD_LASTXFER]);
5904 #endif /* APCUPSD */
5911 unsigned int a = strlen(p);
5914 if (a > 0 && iconv_converting && iconv_selected > 0
5915 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5917 size_t dummy1, dummy2;
5919 const char *ptr = buff_in;
5921 char *ptr = buff_in;
5925 dummy1 = dummy2 = a;
5927 strncpy(buff_in, p, p_max_size);
5929 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5930 while (dummy1 > 0) {
5931 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5934 ERR("Iconv codeset conversion failed");
5939 /* It is nessecary when we are converting from multibyte to
5940 * singlebyte codepage */
5943 #endif /* HAVE_ICONV */
5950 /* load any new fonts we may have had */
5951 if (need_to_load_fonts) {
5957 void evaluate(char *text, char *buffer)
5959 struct information *tmp_info;
5960 struct text_object subroot;
5962 tmp_info = malloc(sizeof(struct information));
5963 memcpy(tmp_info, &info, sizeof(struct information));
5964 parse_conky_vars(&subroot, text, buffer, tmp_info);
5965 DBGP("evaluated '%s' to '%s'", text, buffer);
5967 free_text_objects(&subroot, 1);
5971 double current_update_time, next_update_time, last_update_time;
5973 static void generate_text(void)
5975 struct information *cur = &info;
5982 current_update_time = get_time();
5986 /* add things to the buffer */
5992 generate_text_internal(p, max_user_text, global_root_object, cur);
5994 if (stuff_in_upper_case) {
5997 tmp_p = text_buffer;
5999 *tmp_p = toupper(*tmp_p);
6004 next_update_time += update_interval;
6005 if (next_update_time < get_time()) {
6006 next_update_time = get_time() + update_interval;
6007 } else if (next_update_time > get_time() + update_interval) {
6008 next_update_time = get_time() + update_interval;
6010 last_update_time = current_update_time;
6015 static inline int get_string_width(const char *s)
6018 if (output_methods & TO_X) {
6019 return *s ? calc_text_width(s, strlen(s)) : 0;
6025 static inline int get_string_width_special(char *s)
6033 if ((output_methods & TO_X) == 0) {
6035 return (s) ? strlen(s) : 0;
6043 p = strndup(s, text_buffer_size);
6047 if (*p == SPECIAL_CHAR) {
6048 /* shift everything over by 1 so that the special char
6049 * doesn't mess up the size calculation */
6050 for (i = 0; i < (long)strlen(p); i++) {
6051 *(p + i) = *(p + i + 1);
6053 if (specials[special_index + idx].type == GRAPH
6054 || specials[special_index + idx].type == GAUGE
6055 || specials[special_index + idx].type == BAR) {
6056 width += specials[special_index + idx].width;
6063 if (strlen(final) > 1) {
6064 width += calc_text_width(final, strlen(final));
6072 static void text_size_updater(char *s);
6074 int last_font_height;
6075 static void update_text_area(void)
6079 if ((output_methods & TO_X) == 0)
6081 /* update text size if it isn't fixed */
6086 text_width = minimum_width;
6089 last_font_height = font_height();
6090 for_each_line(text_buffer, text_size_updater);
6092 if (text_height < minimum_height) {
6093 text_height = minimum_height;
6095 if (text_width > maximum_width && maximum_width > 0) {
6096 text_width = maximum_width;
6100 /* get text position on workarea */
6101 switch (text_alignment) {
6108 x = workarea[2] - text_width - gap_x;
6113 x = workarea[2] / 2 - text_width / 2 - gap_x;
6120 y = workarea[3] - text_height - gap_y;
6124 x = workarea[2] - text_width - gap_x;
6125 y = workarea[3] - text_height - gap_y;
6129 x = workarea[2] / 2 - text_width / 2 - gap_x;
6130 y = workarea[3] - text_height - gap_y;
6135 y = workarea[3] / 2 - text_height / 2 - gap_y;
6139 x = workarea[2] - text_width - gap_x;
6140 y = workarea[3] / 2 - text_height / 2 - gap_y;
6144 case NONE: // Let the WM manage the window
6155 if (own_window && !fixed_pos) {
6158 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6159 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6160 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6161 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6165 /* If window size doesn't match to workarea's size,
6166 * then window probably includes panels (gnome).
6167 * Blah, doesn't work on KDE. */
6168 if (workarea[2] != window.width || workarea[3] != window.height) {
6177 /* update lua window globals */
6178 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6179 #endif /* HAVE_LUA */
6184 static int cur_x, cur_y; /* current x and y for drawing */
6186 //draw_mode also without X11 because we only need to print to stdout with FG
6187 static int draw_mode; /* FG, BG or OUTLINE */
6189 static long current_color;
6191 static void text_size_updater(char *s)
6196 if ((output_methods & TO_X) == 0)
6198 /* get string widths and skip specials */
6201 if (*p == SPECIAL_CHAR) {
6203 w += get_string_width(s);
6206 if (specials[special_index].type == BAR
6207 || specials[special_index].type == GAUGE
6208 || specials[special_index].type == GRAPH) {
6209 w += specials[special_index].width;
6210 if (specials[special_index].height > last_font_height) {
6211 last_font_height = specials[special_index].height;
6212 last_font_height += font_height();
6214 } else if (specials[special_index].type == OFFSET) {
6215 if (specials[special_index].arg > 0) {
6216 w += specials[special_index].arg;
6218 } else if (specials[special_index].type == VOFFSET) {
6219 last_font_height += specials[special_index].arg;
6220 } else if (specials[special_index].type == GOTO) {
6221 if (specials[special_index].arg > cur_x) {
6222 w = (int) specials[special_index].arg;
6224 } else if (specials[special_index].type == TAB) {
6225 int start = specials[special_index].arg;
6226 int step = specials[special_index].width;
6228 if (!step || step < 0) {
6231 w += step - (cur_x - text_start_x - start) % step;
6232 } else if (specials[special_index].type == FONT) {
6233 selected_font = specials[special_index].font_added;
6234 if (font_height() > last_font_height) {
6235 last_font_height = font_height();
6244 w += get_string_width(s);
6245 if (w > text_width) {
6248 if (text_width > maximum_width && maximum_width) {
6249 text_width = maximum_width;
6252 text_height += last_font_height;
6253 last_font_height = font_height();
6256 static inline void set_foreground_color(long c)
6258 if ((output_methods & TO_X) == 0)
6261 XSetForeground(display, window.gc, c);
6265 static void draw_string(const char *s)
6267 int i, i2, pos, width_of_s;
6275 width_of_s = get_string_width(s);
6276 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6278 fflush(stdout); /* output immediately, don't buffer */
6280 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6281 fprintf(stderr, "%s\n", s);
6282 fflush(stderr); /* output immediately, don't buffer */
6284 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6285 fprintf(overwrite_fpointer, "%s\n", s);
6287 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6288 fprintf(append_fpointer, "%s\n", s);
6290 memset(tmpstring1, 0, text_buffer_size);
6291 memset(tmpstring2, 0, text_buffer_size);
6292 strncpy(tmpstring1, s, text_buffer_size - 1);
6297 if (output_methods & TO_X) {
6298 max = ((text_width - width_of_s) / get_string_width(" "));
6301 /* This code looks for tabs in the text and coverts them to spaces.
6302 * The trick is getting the correct number of spaces, and not going
6303 * over the window's size without forcing the window larger. */
6304 for (i = 0; i < (int) text_buffer_size; i++) {
6305 if (tmpstring1[i] == '\t') {
6307 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6308 /* guard against overrun */
6309 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6314 /* guard against overrun */
6315 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6320 if (output_methods & TO_X) {
6321 if (text_width == maximum_width) {
6322 /* this means the text is probably pushing the limit,
6323 * so we'll chop it */
6324 while (cur_x + get_string_width(tmpstring2) - text_start_x
6325 > maximum_width && strlen(tmpstring2) > 0) {
6326 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6333 if (output_methods & TO_X) {
6339 c.pixel = current_color;
6340 XQueryColor(display, DefaultColormap(display, screen), &c);
6343 c2.color.red = c.red;
6344 c2.color.green = c.green;
6345 c2.color.blue = c.blue;
6346 c2.color.alpha = fonts[selected_font].font_alpha;
6348 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6349 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6351 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6352 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6357 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6360 cur_x += width_of_s;
6363 memcpy(tmpstring1, s, text_buffer_size);
6366 static void draw_line(char *s)
6374 if ((output_methods & TO_X) == 0) {
6380 cur_x = text_start_x;
6381 cur_y += font_ascent();
6382 font_h = font_height();
6384 /* find specials and draw stuff */
6387 if (*p == SPECIAL_CHAR) {
6390 /* draw string before special */
6397 switch (specials[special_index].type) {
6398 case HORIZONTAL_LINE:
6400 int h = specials[special_index].height;
6401 int mid = font_ascent() / 2;
6403 w = text_start_x + text_width - cur_x;
6405 XSetLineAttributes(display, window.gc, h, LineSolid,
6406 CapButt, JoinMiter);
6407 XDrawLine(display, window.drawable, window.gc, cur_x,
6408 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6414 int h = specials[special_index].height;
6415 int tmp_s = specials[special_index].arg;
6416 int mid = font_ascent() / 2;
6417 char ss[2] = { tmp_s, tmp_s };
6419 w = text_start_x + text_width - cur_x - 1;
6420 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6421 CapButt, JoinMiter);
6422 XSetDashes(display, window.gc, 0, ss, 2);
6423 XDrawLine(display, window.drawable, window.gc, cur_x,
6424 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6430 int h, bar_usage, by;
6431 if (cur_x - text_start_x > maximum_width
6432 && maximum_width > 0) {
6435 h = specials[special_index].height;
6436 bar_usage = specials[special_index].arg;
6437 by = cur_y - (font_ascent() / 2) - 1;
6442 w = specials[special_index].width;
6444 w = text_start_x + text_width - cur_x - 1;
6450 XSetLineAttributes(display, window.gc, 1, LineSolid,
6451 CapButt, JoinMiter);
6453 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6455 XFillRectangle(display, window.drawable, window.gc, cur_x,
6456 by, w * bar_usage / 255, h);
6464 case GAUGE: /* new GAUGE */
6467 unsigned long last_colour = current_color;
6469 float angle, px, py;
6473 if (cur_x - text_start_x > maximum_width
6474 && maximum_width > 0) {
6478 h = specials[special_index].height;
6479 by = cur_y - (font_ascent() / 2) - 1;
6484 w = specials[special_index].width;
6486 w = text_start_x + text_width - cur_x - 1;
6492 XSetLineAttributes(display, window.gc, 1, LineSolid,
6493 CapButt, JoinMiter);
6495 XDrawArc(display, window.drawable, window.gc,
6496 cur_x, by, w, h * 2, 0, 180*64);
6499 usage = specials[special_index].arg;
6500 angle = (M_PI)*(float)(usage)/255.;
6501 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6502 py = (float)(by+(h))-(float)(h)*sin(angle);
6504 XDrawLine(display, window.drawable, window.gc,
6505 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6513 set_foreground_color(last_colour);
6521 int h, by, i = 0, j = 0;
6523 unsigned long last_colour = current_color;
6524 unsigned long *tmpcolour = 0;
6525 if (cur_x - text_start_x > maximum_width
6526 && maximum_width > 0) {
6529 h = specials[special_index].height;
6530 by = cur_y - (font_ascent() / 2) - 1;
6535 w = specials[special_index].width;
6537 w = text_start_x + text_width - cur_x - 1;
6542 if (draw_graph_borders) {
6543 XSetLineAttributes(display, window.gc, 1, LineSolid,
6544 CapButt, JoinMiter);
6545 XDrawRectangle(display, window.drawable, window.gc,
6548 XSetLineAttributes(display, window.gc, 1, LineSolid,
6549 CapButt, JoinMiter);
6551 if (specials[special_index].last_colour != 0
6552 || specials[special_index].first_colour != 0) {
6553 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6556 for (i = w - 2; i > -1; i--) {
6557 if (specials[special_index].last_colour != 0
6558 || specials[special_index].first_colour != 0) {
6559 if (specials[special_index].tempgrad) {
6562 (int)((float)(w - 2) - specials[special_index].graph[j] *
6563 (w - 2) / (float)specials[special_index].graph_scale)
6567 (int)((float)(w - 2) - specials[special_index].graph[j] *
6568 (w - 2) / (float)specials[special_index].graph_scale)
6571 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6573 (int)((float)(w - 2) - specials[special_index].graph[j] *
6574 (w - 2) / (float)specials[special_index].graph_scale)
6578 #endif /* DEBUG_lol */
6579 XSetForeground(display, window.gc, tmpcolour[
6580 (int)((float)(w - 2) - specials[special_index].graph[j] *
6581 (w - 2) / (float)specials[special_index].graph_scale)
6584 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6587 /* this is mugfugly, but it works */
6588 XDrawLine(display, window.drawable, window.gc,
6589 cur_x + i + 1, by + h, cur_x + i + 1,
6590 round_to_int((double)by + h - specials[special_index].graph[j] *
6591 (h - 1) / specials[special_index].graph_scale));
6592 if ((w - i) / ((float) (w - 2) /
6593 (specials[special_index].graph_width)) > j
6594 && j < MAX_GRAPH_DEPTH - 3) {
6598 if (tmpcolour) free(tmpcolour);
6603 /* if (draw_mode == BG) {
6604 set_foreground_color(default_bg_color);
6605 } else if (draw_mode == OUTLINE) {
6606 set_foreground_color(default_out_color);
6608 set_foreground_color(default_fg_color);
6610 if (show_graph_range) {
6613 unsigned short int seconds = update_interval * w;
6618 unsigned short int timeunits;
6620 timeunits = seconds / 86400; seconds %= 86400;
6621 if (timeunits > 0) {
6622 asprintf(&tmp_day_str, "%dd", timeunits);
6624 tmp_day_str = strdup("");
6626 timeunits = seconds / 3600; seconds %= 3600;
6627 if (timeunits > 0) {
6628 asprintf(&tmp_hour_str, "%dh", timeunits);
6630 tmp_hour_str = strdup("");
6632 timeunits = seconds / 60; seconds %= 60;
6633 if (timeunits > 0) {
6634 asprintf(&tmp_min_str, "%dm", timeunits);
6636 tmp_min_str = strdup("");
6639 asprintf(&tmp_sec_str, "%ds", seconds);
6641 tmp_sec_str = strdup("");
6643 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6644 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6646 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6648 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6649 cur_y += font_h / 2;
6650 draw_string(tmp_str);
6656 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6659 cur_x += font_ascent() / 2;
6660 cur_y += font_h / 2;
6662 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6664 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6665 draw_string(tmp_str);
6671 set_foreground_color(last_colour);
6677 int old = font_ascent();
6679 cur_y -= font_ascent();
6680 selected_font = specials[special_index].font_added;
6682 if (cur_y + font_ascent() < cur_y + old) {
6685 cur_y += font_ascent();
6687 font_h = font_height();
6691 if (draw_mode == FG) {
6692 set_foreground_color(specials[special_index].arg);
6697 if (draw_mode == BG) {
6698 set_foreground_color(specials[special_index].arg);
6703 if (draw_mode == OUTLINE) {
6704 set_foreground_color(specials[special_index].arg);
6709 w += specials[special_index].arg;
6713 cur_y += specials[special_index].arg;
6717 if (specials[special_index].arg >= 0) {
6718 cur_x = (int) specials[special_index].arg;
6724 int start = specials[special_index].arg;
6725 int step = specials[special_index].width;
6727 if (!step || step < 0) {
6730 w = step - (cur_x - text_start_x - start) % step;
6736 /* TODO: add back in "+ window.border_inner_margin" to the end of
6738 int pos_x = text_start_x + text_width -
6739 get_string_width_special(s);
6741 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6742 "get_string_width(p) %i gap_x %i "
6743 "specials[special_index].arg %i window.border_inner_margin %i "
6744 "window.border_width %i\n", pos_x, text_start_x, text_width,
6745 cur_x, get_string_width_special(s), gap_x,
6746 specials[special_index].arg, window.border_inner_margin,
6747 window.border_width); */
6748 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6749 cur_x = pos_x - specials[special_index].arg;
6756 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6757 2 - (cur_x - text_start_x);
6758 /* int pos_x = text_start_x + text_width / 2 -
6759 get_string_width_special(s) / 2; */
6761 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6762 "get_string_width(p) %i gap_x %i "
6763 "specials[special_index].arg %i\n", pos_x, text_start_x,
6764 text_width, cur_x, get_string_width(s), gap_x,
6765 specials[special_index].arg); */
6766 if (pos_x > specials[special_index].arg) {
6767 w = pos_x - specials[special_index].arg;
6781 if (cur_y_add > 0) {
6785 cur_y += font_descent();
6790 static void draw_text(void)
6794 llua_draw_pre_hook();
6795 #endif /* HAVE_LUA */
6796 if (output_methods & TO_X) {
6797 cur_y = text_start_y;
6800 if (draw_borders && window.border_width > 0) {
6801 if (stippled_borders) {
6802 char ss[2] = { stippled_borders, stippled_borders };
6803 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6804 CapButt, JoinMiter);
6805 XSetDashes(display, window.gc, 0, ss, 2);
6807 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6808 CapButt, JoinMiter);
6811 XDrawRectangle(display, window.drawable, window.gc,
6812 text_start_x - window.border_inner_margin - window.border_width,
6813 text_start_y - window.border_inner_margin - window.border_width,
6814 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6815 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6823 for_each_line(text_buffer, draw_line);
6824 #if defined(HAVE_LUA) && defined(X11)
6825 llua_draw_post_hook();
6826 #endif /* HAVE_LUA */
6829 static void draw_stuff(void)
6831 if (overwrite_file) {
6832 overwrite_fpointer = fopen(overwrite_file, "w");
6833 if(!overwrite_fpointer)
6834 ERR("Can't overwrite '%s' anymore", overwrite_file);
6837 append_fpointer = fopen(append_file, "a");
6838 if(!append_fpointer)
6839 ERR("Can't append '%s' anymore", append_file);
6842 if (output_methods & TO_X) {
6844 if (draw_shades && !draw_outline) {
6847 set_foreground_color(default_bg_color);
6858 for (i = -1; i < 2; i++) {
6859 for (j = -1; j < 2; j++) {
6860 if (i == 0 && j == 0) {
6865 set_foreground_color(default_out_color);
6866 draw_mode = OUTLINE;
6874 set_foreground_color(default_fg_color);
6880 if (output_methods & TO_X) {
6885 swap.swap_window = window.window;
6886 swap.swap_action = XdbeBackground;
6887 XdbeSwapBuffers(display, &swap, 1);
6892 if(overwrite_fpointer) {
6893 fclose(overwrite_fpointer);
6894 overwrite_fpointer = 0;
6896 if(append_fpointer) {
6897 fclose(append_fpointer);
6898 append_fpointer = 0;
6903 static void clear_text(int exposures)
6907 /* The swap action is XdbeBackground, which clears */
6911 if (display && window.window) { // make sure these are !null
6912 /* there is some extra space for borders and outlines */
6913 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
6914 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
6915 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
6916 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
6921 static int need_to_update;
6923 /* update_text() generates new text and clears old text area */
6924 static void update_text(void)
6931 if (output_methods & TO_X)
6937 #ifdef HAVE_SYS_INOTIFY_H
6941 static void main_loop(void)
6944 #ifdef SIGNAL_BLOCKING
6945 sigset_t newmask, oldmask;
6948 #ifdef HAVE_SYS_INOTIFY_H
6949 int inotify_config_wd = -1;
6950 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
6951 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
6952 char inotify_buff[INOTIFY_BUF_LEN];
6953 #endif /* HAVE_SYS_INOTIFY_H */
6956 #ifdef SIGNAL_BLOCKING
6957 sigemptyset(&newmask);
6958 sigaddset(&newmask, SIGINT);
6959 sigaddset(&newmask, SIGTERM);
6960 sigaddset(&newmask, SIGUSR1);
6963 last_update_time = 0.0;
6964 next_update_time = get_time();
6966 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
6969 #ifdef SIGNAL_BLOCKING
6970 /* block signals. we will inspect for pending signals later */
6971 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6972 CRIT_ERR("unable to sigprocmask()");
6977 if (output_methods & TO_X) {
6980 /* wait for X event or timeout */
6982 if (!XPending(display)) {
6986 t = next_update_time - get_time();
6990 } else if (t > update_interval) {
6991 t = update_interval;
6994 tv.tv_sec = (long) t;
6995 tv.tv_usec = (long) (t * 1000000) % 1000000;
6997 FD_SET(ConnectionNumber(display), &fdsr);
6999 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7001 if (errno != EINTR) {
7002 ERR("can't select(): %s", strerror(errno));
7012 if (need_to_update) {
7014 int wx = window.x, wy = window.y;
7024 /* resize window if it isn't right size */
7026 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7027 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7028 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7029 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7030 XResizeWindow(display, window.window, window.width,
7032 set_transparent_background(window.window);
7037 /* move window if it isn't in right position */
7038 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7039 XMoveWindow(display, window.window, window.x, window.y);
7044 if (changed && window.type == TYPE_PANEL) {
7047 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7050 switch (text_alignment) {
7077 set_struts(sidenum);
7088 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7089 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7090 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7091 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7092 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7097 /* handle X events */
7098 while (XPending(display)) {
7101 XNextEvent(display, &ev);
7108 r.width = ev.xexpose.width;
7109 r.height = ev.xexpose.height;
7110 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7115 case ReparentNotify:
7116 /* set background to ParentRelative for all parents */
7118 set_transparent_background(window.window);
7122 case ConfigureNotify:
7124 /* if window size isn't what expected, set fixed size */
7125 if (ev.xconfigure.width != window.width
7126 || ev.xconfigure.height != window.height) {
7127 if (window.width != 0 && window.height != 0) {
7131 /* clear old stuff before screwing up
7136 XWindowAttributes attrs;
7137 if (XGetWindowAttributes(display,
7138 window.window, &attrs)) {
7139 window.width = attrs.width;
7140 window.height = attrs.height;
7144 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7145 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7146 if (text_width > maximum_width
7147 && maximum_width > 0) {
7148 text_width = maximum_width;
7152 /* if position isn't what expected, set fixed pos
7153 * total_updates avoids setting fixed_pos when window
7154 * is set to weird locations when started */
7155 /* // this is broken
7156 if (total_updates >= 2 && !fixed_pos
7157 && (window.x != ev.xconfigure.x
7158 || window.y != ev.xconfigure.y)
7159 && (ev.xconfigure.x != 0
7160 || ev.xconfigure.y != 0)) {
7168 /* if an ordinary window with decorations */
7169 if ((window.type == TYPE_NORMAL)
7170 && (!TEST_HINT(window.hints,
7171 HINT_UNDECORATED))) {
7172 /* allow conky to hold input focus. */
7175 /* forward the click to the desktop window */
7176 XUngrabPointer(display, ev.xbutton.time);
7177 ev.xbutton.window = window.desktop;
7178 ev.xbutton.x = ev.xbutton.x_root;
7179 ev.xbutton.y = ev.xbutton.y_root;
7180 XSendEvent(display, ev.xbutton.window, False,
7181 ButtonPressMask, &ev);
7182 XSetInputFocus(display, ev.xbutton.window,
7183 RevertToParent, ev.xbutton.time);
7190 /* if an ordinary window with decorations */
7191 if ((window.type == TYPE_NORMAL)
7192 && (!TEST_HINT(window.hints,
7193 HINT_UNDECORATED))) {
7194 /* allow conky to hold input focus. */
7197 /* forward the release to the desktop window */
7198 ev.xbutton.window = window.desktop;
7199 ev.xbutton.x = ev.xbutton.x_root;
7200 ev.xbutton.y = ev.xbutton.y_root;
7201 XSendEvent(display, ev.xbutton.window, False,
7202 ButtonReleaseMask, &ev);
7211 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7212 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7214 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7215 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7217 #endif /* HAVE_XDAMAGE */
7223 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7224 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7225 #endif /* HAVE_XDAMAGE */
7227 /* XDBE doesn't seem to provide a way to clear the back buffer without
7228 * interfering with the front buffer, other than passing XdbeBackground
7229 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7230 * redraw the text even if it wasn't part of the exposed area. OTOH,
7231 * if we're not going to call draw_stuff at all, then no swap happens
7232 * and we can safely do nothing. */
7234 if (!XEmptyRegion(x11_stuff.region)) {
7239 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7240 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7241 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7242 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7243 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7246 XSetRegion(display, window.gc, x11_stuff.region);
7249 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7253 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7256 XDestroyRegion(x11_stuff.region);
7257 x11_stuff.region = XCreateRegion();
7261 t = (next_update_time - get_time()) * 1000000;
7262 if(t > 0) usleep((useconds_t)t);
7269 #ifdef SIGNAL_BLOCKING
7270 /* unblock signals of interest and let handler fly */
7271 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7272 CRIT_ERR("unable to sigprocmask()");
7276 switch (g_signal_pending) {
7279 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7284 ERR("received SIGINT or SIGTERM to terminate. bye!");
7287 if (output_methods & TO_X) {
7288 XDestroyRegion(x11_stuff.region);
7289 x11_stuff.region = NULL;
7291 XDamageDestroy(display, x11_stuff.damage);
7292 XFixesDestroyRegion(display, x11_stuff.region2);
7293 XFixesDestroyRegion(display, x11_stuff.part);
7294 #endif /* HAVE_XDAMAGE */
7300 if(overwrite_file) {
7301 free(overwrite_file);
7310 /* Reaching here means someone set a signal
7311 * (SIGXXXX, signal_handler), but didn't write any code
7313 * If you don't want to handle a signal, don't set a handler on
7314 * it in the first place. */
7315 if (g_signal_pending) {
7316 ERR("ignoring signal (%d)", g_signal_pending);
7320 #ifdef HAVE_SYS_INOTIFY_H
7321 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7322 inotify_config_wd = inotify_add_watch(inotify_fd,
7326 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7327 int len = 0, idx = 0;
7329 struct timeval time_to_wait;
7331 FD_ZERO(&descriptors);
7332 FD_SET(inotify_fd, &descriptors);
7334 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7336 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7337 if (FD_ISSET(inotify_fd, &descriptors)) {
7338 /* process inotify events */
7339 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7340 while (len > 0 && idx < len) {
7341 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7342 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7343 /* current_config should be reloaded */
7344 ERR("'%s' modified, reloading...", current_config);
7346 if (ev->mask & IN_IGNORED) {
7347 /* for some reason we get IN_IGNORED here
7348 * sometimes, so we need to re-add the watch */
7349 inotify_config_wd = inotify_add_watch(inotify_fd,
7356 llua_inotify_query(ev->wd, ev->mask);
7358 #endif /* HAVE_LUA */
7359 idx += INOTIFY_EVENT_SIZE + ev->len;
7363 #endif /* HAVE_SYS_INOTIFY_H */
7365 g_signal_pending = 0;
7369 #ifdef HAVE_SYS_INOTIFY_H
7370 if (inotify_fd != -1) {
7371 inotify_rm_watch(inotify_fd, inotify_config_wd);
7373 inotify_fd = inotify_config_wd = 0;
7375 #endif /* HAVE_SYS_INOTIFY_H */
7378 X11_destroy_window();
7382 static void load_config_file(const char *);
7383 static void load_config_file_x11(const char *);
7385 /* reload the config file */
7386 static void reload_config(void)
7388 timed_thread_destroy_registered_threads();
7390 if (info.cpu_usage) {
7391 free(info.cpu_usage);
7392 info.cpu_usage = NULL;
7403 #ifdef TCP_PORT_MONITOR
7404 tcp_portmon_clear();
7411 free_weather_info();
7415 #endif /* HAVE_LUA */
7418 X11_destroy_window();
7421 if (current_config) {
7423 load_config_file(current_config);
7424 load_config_file_x11(current_config);
7426 /* re-init specials array */
7427 if ((specials = realloc((void *) specials,
7428 sizeof(struct special_t) * max_specials)) == 0) {
7429 ERR("failed to realloc specials array");
7433 if (output_methods & TO_X) {
7434 X11_initialisation();
7437 extract_variable_text(global_text);
7443 tmpstring1 = malloc(text_buffer_size);
7444 memset(tmpstring1, 0, text_buffer_size);
7448 tmpstring2 = malloc(text_buffer_size);
7449 memset(tmpstring2, 0, text_buffer_size);
7453 text_buffer = malloc(max_user_text);
7454 memset(text_buffer, 0, max_user_text);
7456 X11_create_window();
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 XdbeDeallocateBackBufferName(display, window.back_buffer);
7480 XDestroyWindow(display, window.window);
7481 XClearWindow(display, RootWindow(display, screen));
7486 XClearWindow(display, RootWindow(display, screen));
7493 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) free(global_cpu);
7555 static int string_to_bool(const char *s)
7558 // Assumes an option without a true/false means true
7560 } else if (strcasecmp(s, "yes") == EQUAL) {
7562 } else if (strcasecmp(s, "true") == EQUAL) {
7564 } else if (strcasecmp(s, "1") == EQUAL) {
7571 static enum alignment string_to_alignment(const char *s)
7573 if (strcasecmp(s, "top_left") == EQUAL) {
7575 } else if (strcasecmp(s, "top_right") == EQUAL) {
7577 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7579 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7581 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7582 return BOTTOM_RIGHT;
7583 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7584 return BOTTOM_MIDDLE;
7585 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7587 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7588 return MIDDLE_RIGHT;
7589 } else if (strcasecmp(s, "tl") == EQUAL) {
7591 } else if (strcasecmp(s, "tr") == EQUAL) {
7593 } else if (strcasecmp(s, "tm") == EQUAL) {
7595 } else if (strcasecmp(s, "bl") == EQUAL) {
7597 } else if (strcasecmp(s, "br") == EQUAL) {
7598 return BOTTOM_RIGHT;
7599 } else if (strcasecmp(s, "bm") == EQUAL) {
7600 return BOTTOM_MIDDLE;
7601 } else if (strcasecmp(s, "ml") == EQUAL) {
7603 } else if (strcasecmp(s, "mr") == EQUAL) {
7604 return MIDDLE_RIGHT;
7605 } else if (strcasecmp(s, "none") == EQUAL) {
7613 static void set_default_configurations_for_x(void)
7615 default_fg_color = WhitePixel(display, screen);
7616 default_bg_color = BlackPixel(display, screen);
7617 default_out_color = BlackPixel(display, screen);
7618 color0 = default_fg_color;
7619 color1 = default_fg_color;
7620 color2 = default_fg_color;
7621 color3 = default_fg_color;
7622 color4 = default_fg_color;
7623 color5 = default_fg_color;
7624 color6 = default_fg_color;
7625 color7 = default_fg_color;
7626 color8 = default_fg_color;
7627 color9 = default_fg_color;
7631 static void set_default_configurations(void)
7639 fork_to_background = 0;
7640 total_run_times = 0;
7641 info.cpu_avg_samples = 2;
7642 info.net_avg_samples = 2;
7643 info.diskio_avg_samples = 2;
7648 format_human_readable = 1;
7655 mpd_env_host = getenv("MPD_HOST");
7656 mpd_env_port = getenv("MPD_PORT");
7658 if (!mpd_env_host || !strlen(mpd_env_host)) {
7659 mpd_set_host("localhost");
7661 /* MPD_HOST environment variable is set */
7662 char *mpd_hostpart = strchr(mpd_env_host, '@');
7663 if (!mpd_hostpart) {
7664 mpd_set_host(mpd_env_host);
7666 /* MPD_HOST contains a password */
7667 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7668 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7670 if (!strlen(mpd_hostpart + 1)) {
7671 mpd_set_host("localhost");
7673 mpd_set_host(mpd_hostpart + 1);
7676 mpd_set_password(mpd_password, 1);
7681 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7682 /* failed to set port from environment variable */
7683 mpd_set_port("6600");
7687 info.xmms2.artist = NULL;
7688 info.xmms2.album = NULL;
7689 info.xmms2.title = NULL;
7690 info.xmms2.genre = NULL;
7691 info.xmms2.comment = NULL;
7692 info.xmms2.url = NULL;
7693 info.xmms2.status = NULL;
7694 info.xmms2.playlist = NULL;
7696 use_spacer = NO_SPACER;
7698 output_methods = TO_X;
7700 output_methods = TO_STDOUT;
7703 show_graph_scale = 0;
7704 show_graph_range = 0;
7707 draw_graph_borders = 1;
7709 set_first_font("6x10");
7717 window.type = TYPE_NORMAL;
7719 strcpy(window.class_name, PACKAGE_NAME);
7720 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7722 stippled_borders = 0;
7723 window.border_inner_margin = 3;
7724 window.border_outer_margin = 1;
7725 window.border_width = 1;
7726 text_alignment = BOTTOM_LEFT;
7727 info.x11.monitor.number = 1;
7728 info.x11.monitor.current = 0;
7729 info.x11.desktop.current = 1;
7730 info.x11.desktop.number = 1;
7731 info.x11.desktop.name = NULL;
7734 for (i = 0; i < MAX_TEMPLATES; i++) {
7737 template[i] = strdup("");
7740 free(current_mail_spool);
7744 variable_substitute(MAIL_FILE, buf, 256);
7745 if (buf[0] != '\0') {
7746 current_mail_spool = strndup(buf, text_buffer_size);
7751 update_interval = 3.0;
7752 info.music_player_interval = 1.0;
7753 stuff_in_upper_case = 0;
7754 info.users.number = 1;
7756 #ifdef TCP_PORT_MONITOR
7757 /* set default connection limit */
7758 tcp_portmon_set_max_connections(0);
7762 /* returns 1 if you can overwrite or create the file at 'path' */
7763 static _Bool overwrite_works(const char *path)
7767 if (!(filepointer = fopen(path, "w")))
7769 fclose(filepointer);
7773 /* returns 1 if you can append or create the file at 'path' */
7774 static _Bool append_works(const char *path)
7778 if (!(filepointer = fopen(path, "a")))
7780 fclose(filepointer);
7786 /* WARNING, this type not in Xlib spec */
7787 int x11_error_handler(Display *d, XErrorEvent *err)
7788 __attribute__((noreturn));
7789 int x11_error_handler(Display *d, XErrorEvent *err)
7791 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7793 (long unsigned)err->display,
7794 (long)err->resourceid,
7804 int x11_ioerror_handler(Display *d)
7805 __attribute__((noreturn));
7806 int x11_ioerror_handler(Display *d)
7808 ERR("X Error: Display %lx\n",
7815 static void X11_initialisation(void)
7817 if (x_initialised == YES) return;
7818 output_methods |= TO_X;
7820 set_default_configurations_for_x();
7821 x_initialised = YES;
7824 /* WARNING, this type not in Xlib spec */
7825 XSetErrorHandler(&x11_error_handler);
7826 XSetIOErrorHandler(&x11_ioerror_handler);
7830 static void X11_destroy_window(void)
7832 /* this function only exists for the sake of consistency */
7833 if (output_methods & TO_X) {
7835 XDamageDestroy(display, x11_stuff.damage);
7836 XFixesDestroyRegion(display, x11_stuff.region2);
7837 XFixesDestroyRegion(display, x11_stuff.part);
7838 if (x11_stuff.region) {
7839 XDestroyRegion(x11_stuff.region);
7841 x11_stuff.region = NULL;
7842 #endif /* HAVE_XDAMAGE */
7848 static char **xargv = 0;
7849 static int xargc = 0;
7851 static void X11_create_window(void)
7853 if (output_methods & TO_X) {
7855 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7856 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7858 #else /* OWN_WINDOW */
7859 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7860 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7862 #endif /* OWN_WINDOW */
7866 update_text_area(); /* to position text/window on screen */
7869 if (own_window && !fixed_pos) {
7870 XMoveWindow(display, window.window, window.x, window.y);
7873 set_transparent_background(window.window);
7881 x11_stuff.region = XCreateRegion();
7883 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7884 ERR("Xdamage extension unavailable");
7886 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7887 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7888 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7889 #endif /* HAVE_XDAMAGE */
7892 update_text_area(); /* to get initial size of the window */
7895 /* setup lua window globals */
7896 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
7897 #endif /* HAVE_LUA */
7901 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7902 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7903 #define CONF2(a) if (strcasecmp(name, a) == 0)
7904 #define CONF(a) else CONF2(a)
7905 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7906 || strcasecmp(name, b) == 0)
7907 #define CONF_CONTINUE 1
7908 #define CONF_BREAK 2
7909 #define CONF_BUFF_SIZE 512
7911 static FILE *open_config_file(const char *f)
7913 #ifdef CONFIG_OUTPUT
7914 if (!strcmp(f, "==builtin==")) {
7915 return conf_cookie_open();
7917 #endif /* CONFIG_OUTPUT */
7918 return fopen(f, "r");
7921 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7925 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7928 remove_comments(buf);
7933 while (*p && isspace((int) *p)) {
7937 return CONF_CONTINUE; /* empty line */
7944 while (*p2 && !isspace((int) *p2)) {
7948 *p2 = '\0'; /* break at name's end */
7955 while (*p && isspace((int) *p)) {
7961 p2 = *value + strlen(*value);
7962 while (isspace((int) *(p2 - 1))) {
7971 static void load_config_file(const char *f)
7976 set_default_configurations();
7977 fp = open_config_file(f);
7981 DBGP("reading contents from config file '%s'", f);
7984 char buff[CONF_BUFF_SIZE], *name, *value;
7985 int ret = do_config_step(&line, fp, buff, &name, &value);
7986 if (ret == CONF_BREAK) {
7988 } else if (ret == CONF_CONTINUE) {
7994 /* don't listen if X is already initialised or
7995 * if we already know we don't want it */
7996 if(x_initialised == NO) {
7997 if (string_to_bool(value)) {
7998 output_methods &= TO_X;
8000 output_methods &= ~TO_X;
8001 x_initialised = NEVER;
8002 free(fonts); //in set_default_configurations a font is set but not loaded
8007 if (!value || x_initialised == YES) {
8012 disp = strdup(value);
8017 if (window.type == TYPE_DOCK)
8020 #endif /*OWN_WINDOW */
8022 int a = string_to_alignment(value);
8033 CONF("background") {
8034 fork_to_background = string_to_bool(value);
8037 CONF2("background") {
8038 fork_to_background = string_to_bool(value);
8042 CONF("show_graph_scale") {
8043 show_graph_scale = string_to_bool(value);
8045 CONF("show_graph_range") {
8046 show_graph_range = string_to_bool(value);
8048 CONF("border_margin") {
8049 ERR("border_margin is deprecated, please use window.border_inner_margin instead");
8051 window.border_inner_margin = strtol(value, 0, 0);
8052 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8057 CONF("border_inner_margin") {
8059 window.border_inner_margin = strtol(value, 0, 0);
8060 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8065 CONF("border_outer_margin") {
8067 window.border_outer_margin = strtol(value, 0, 0);
8068 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8073 CONF("border_width") {
8075 window.border_width = strtol(value, 0, 0);
8076 if (window.border_width < 0) window.border_width = 0;
8082 #define TEMPLATE_CONF(n) \
8083 CONF("template"#n) { \
8085 free(template[n]); \
8086 template[n] = strdup(value); \
8103 info.mail = parse_mail_args(IMAP_TYPE, value);
8110 info.mail = parse_mail_args(POP3_TYPE, value);
8115 CONF("default_bar_size") {
8118 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8125 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8129 CONF("default_graph_size") {
8132 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8139 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8142 CONF("default_gauge_size") {
8145 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8152 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8159 mpd_set_host(value);
8165 if (value && mpd_set_port(value)) {
8169 CONF("mpd_password") {
8171 mpd_set_password(value, 0);
8177 CONF("music_player_interval") {
8179 info.music_player_interval = strtod(value, 0);
8185 CONF("sensor_device") {
8187 sensor_device = strtol(value, 0, 0);
8193 CONF("cpu_avg_samples") {
8195 cpu_avg_samples = strtol(value, 0, 0);
8196 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8199 info.cpu_avg_samples = cpu_avg_samples;
8205 CONF("net_avg_samples") {
8207 net_avg_samples = strtol(value, 0, 0);
8208 if (net_avg_samples < 1 || net_avg_samples > 14) {
8211 info.net_avg_samples = net_avg_samples;
8217 CONF("diskio_avg_samples") {
8219 diskio_avg_samples = strtol(value, 0, 0);
8220 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8223 info.diskio_avg_samples = diskio_avg_samples;
8231 CONF("double_buffer") {
8232 use_xdbe = string_to_bool(value);
8236 CONF("override_utf8_locale") {
8237 utf8_mode = string_to_bool(value);
8239 CONF("draw_borders") {
8240 draw_borders = string_to_bool(value);
8242 CONF("draw_graph_borders") {
8243 draw_graph_borders = string_to_bool(value);
8245 CONF("draw_shades") {
8246 draw_shades = string_to_bool(value);
8248 CONF("draw_outline") {
8249 draw_outline = string_to_bool(value);
8252 CONF("out_to_console") {
8253 if(string_to_bool(value))
8254 output_methods |= TO_STDOUT;
8256 CONF("out_to_stderr") {
8257 if(string_to_bool(value))
8258 output_methods |= TO_STDERR;
8260 CONF("overwrite_file") {
8261 if(overwrite_file) {
8262 free(overwrite_file);
8265 if(overwrite_works(value)) {
8266 overwrite_file = strdup(value);
8267 output_methods |= OVERWRITE_FILE;
8269 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8271 CONF("append_file") {
8276 if(append_works(value)) {
8277 append_file = strdup(value);
8278 output_methods |= APPEND_FILE;
8280 ERR("append_file won't be able to create/append '%s'", value);
8282 CONF("use_spacer") {
8284 if (strcasecmp(value, "left") == EQUAL) {
8285 use_spacer = LEFT_SPACER;
8286 } else if (strcasecmp(value, "right") == EQUAL) {
8287 use_spacer = RIGHT_SPACER;
8288 } else if (strcasecmp(value, "none") == EQUAL) {
8289 use_spacer = NO_SPACER;
8291 use_spacer = string_to_bool(value);
8292 ERR("use_spacer should have an argument of left, right, or"
8293 " none. '%s' seems to be some form of '%s', so"
8294 " defaulting to %s.", value,
8295 use_spacer ? "true" : "false",
8296 use_spacer ? "right" : "none");
8298 use_spacer = RIGHT_SPACER;
8300 use_spacer = NO_SPACER;
8304 ERR("use_spacer should have an argument. Defaulting to right.");
8305 use_spacer = RIGHT_SPACER;
8311 use_xft = string_to_bool(value);
8315 set_first_font(value);
8319 if (value && font_count >= 0) {
8320 fonts[0].font_alpha = atof(value) * 65535.0;
8327 if (string_to_bool(value)) {
8328 ERR("Xft not enabled at compile time");
8332 /* xftfont silently ignored when no Xft */
8335 /* xftalpha is silently ignored when no Xft */
8340 set_first_font(value);
8348 gap_x = atoi(value);
8355 gap_y = atoi(value);
8361 CONF("mail_spool") {
8365 variable_substitute(value, buffer, 256);
8367 if (buffer[0] != '\0') {
8368 if (current_mail_spool) {
8369 free(current_mail_spool);
8371 current_mail_spool = strndup(buffer, text_buffer_size);
8378 CONF("minimum_size") {
8380 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8382 if (sscanf(value, "%d", &minimum_width) != 1) {
8390 CONF("maximum_width") {
8392 if (sscanf(value, "%d", &maximum_width) != 1) {
8400 CONF("no_buffers") {
8401 no_buffers = string_to_bool(value);
8403 CONF("top_name_width") {
8405 if (sscanf(value, "%u", &top_name_width) != 1) {
8411 if (top_name_width >= max_user_text) {
8412 top_name_width = max_user_text - 1;
8415 CONF("top_cpu_separate") {
8416 cpu_separate = string_to_bool(value);
8418 CONF("short_units") {
8419 short_units = string_to_bool(value);
8421 CONF("format_human_readable") {
8422 format_human_readable = string_to_bool(value);
8424 CONF("pad_percents") {
8425 pad_percents = atoi(value);
8429 CONF("own_window") {
8431 own_window = string_to_bool(value);
8434 CONF("own_window_class") {
8436 memset(window.class_name, 0, sizeof(window.class_name));
8437 strncpy(window.class_name, value,
8438 sizeof(window.class_name) - 1);
8441 CONF("own_window_title") {
8443 memset(window.title, 0, sizeof(window.title));
8444 strncpy(window.title, value, sizeof(window.title) - 1);
8447 CONF("own_window_transparent") {
8449 set_transparent = string_to_bool(value);
8452 CONF("own_window_hints") {
8454 char *p_hint, *p_save;
8455 char delim[] = ", ";
8457 /* tokenize the value into individual hints */
8458 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8460 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8461 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8462 SET_HINT(window.hints, HINT_UNDECORATED);
8463 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8464 SET_HINT(window.hints, HINT_BELOW);
8465 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8466 SET_HINT(window.hints, HINT_ABOVE);
8467 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8468 SET_HINT(window.hints, HINT_STICKY);
8469 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8470 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8471 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8472 SET_HINT(window.hints, HINT_SKIP_PAGER);
8477 p_hint = strtok_r(NULL, delim, &p_save);
8478 } while (p_hint != NULL);
8484 CONF("own_window_type") {
8486 if (strncmp(value, "normal", 6) == EQUAL) {
8487 window.type = TYPE_NORMAL;
8488 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8489 window.type = TYPE_DESKTOP;
8490 } else if (strncmp(value, "dock", 4) == EQUAL) {
8491 window.type = TYPE_DOCK;
8492 text_alignment = TOP_LEFT;
8493 } else if (strncmp(value, "panel", 5) == EQUAL) {
8494 window.type = TYPE_PANEL;
8495 } else if (strncmp(value, "override", 8) == EQUAL) {
8496 window.type = TYPE_OVERRIDE;
8505 CONF("stippled_borders") {
8507 stippled_borders = strtol(value, 0, 0);
8509 stippled_borders = 4;
8513 CONF("imlib_cache_size") {
8515 cimlib_set_cache_size(atoi(value));
8518 CONF("imlib_cache_flush_interval") {
8520 cimlib_set_cache_flush_interval(atoi(value));
8525 CONF("update_interval") {
8527 update_interval = strtod(value, 0);
8531 if (info.music_player_interval == 0) {
8532 // default to update_interval
8533 info.music_player_interval = update_interval;
8536 CONF("total_run_times") {
8538 total_run_times = strtod(value, 0);
8544 stuff_in_upper_case = string_to_bool(value);
8546 CONF("max_specials") {
8548 max_specials = atoi(value);
8553 CONF("max_user_text") {
8555 max_user_text = atoi(value);
8560 CONF("text_buffer_size") {
8562 text_buffer_size = atoi(value);
8563 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8564 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8565 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8573 if (output_methods & TO_X) {
8574 X11_initialisation();
8583 global_text = (char *) malloc(1);
8584 global_text[0] = '\0';
8587 unsigned int l = strlen(global_text);
8589 char buf[CONF_BUFF_SIZE];
8591 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8597 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8605 /* Check for continuation of \\-\n. */
8606 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8607 global_text[l-1] = '\0';
8611 global_text = (char *) realloc(global_text, l + bl + 1);
8612 strcat(global_text, buf);
8614 if (strlen(global_text) > max_user_text) {
8619 if (strlen(global_text) < 1) {
8620 CRIT_ERR("no text supplied in configuration; exiting");
8622 global_text_lines = line + 1;
8625 #ifdef TCP_PORT_MONITOR
8626 CONF("max_port_monitor_connections") {
8628 if (!value || (sscanf(value, "%d", &max) != 1)) {
8629 /* an error. use default, warn and continue. */
8630 tcp_portmon_set_max_connections(0);
8632 } else if (tcp_portmon_set_max_connections(max)) {
8633 /* max is < 0, default has been set*/
8638 CONF("if_up_strictness") {
8640 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8641 ifup_strictness = IFUP_UP;
8642 } else if (strcasecmp(value, "up") == EQUAL) {
8643 ifup_strictness = IFUP_UP;
8644 } else if (strcasecmp(value, "link") == EQUAL) {
8645 ifup_strictness = IFUP_LINK;
8646 } else if (strcasecmp(value, "address") == EQUAL) {
8647 ifup_strictness = IFUP_ADDR;
8649 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8650 ifup_strictness = IFUP_UP;
8654 CONF("temperature_unit") {
8656 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8657 } else if (set_temp_output_unit(value)) {
8658 ERR("temperature_unit: incorrect argument");
8664 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8665 char *skey = malloc(maxlength);
8666 char *svalue = malloc(maxlength);
8668 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8669 oldvalue = getenv(skey);
8670 if (oldvalue == NULL) {
8671 setenv(skey, svalue, 0);
8673 //PS: Don't free oldvalue, it's the real envvar, not a copy
8686 char *ptr = strtok(value, " ");
8689 ptr = strtok(NULL, " ");
8696 CONF("lua_draw_hook_pre") {
8698 llua_set_draw_pre_hook(value);
8703 CONF("lua_draw_hook_post") {
8705 llua_set_draw_post_hook(value);
8711 #endif /* HAVE_LUA */
8723 CONF("default_color"){}
8724 CONF3("default_shade_color", "default_shadecolor"){}
8725 CONF3("default_outline_color", "default_outlinecolor") {}
8726 CONF("own_window_colour") {}
8729 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8735 if (info.music_player_interval == 0) {
8736 // default to update_interval
8737 info.music_player_interval = update_interval;
8739 if (!global_text) { // didn't supply any text
8740 CRIT_ERR("missing text block in configuration; exiting");
8744 static void load_config_file_x11(const char *f)
8749 fp = open_config_file(f);
8753 DBGP("reading contents from config file '%s'", f);
8756 char buff[CONF_BUFF_SIZE], *name, *value;
8757 int ret = do_config_step(&line, fp, buff, &name, &value);
8758 if (ret == CONF_BREAK) {
8760 } else if (ret == CONF_CONTINUE) {
8766 X11_initialisation();
8767 if (x_initialised == YES) {
8769 color0 = get_x11_color(value);
8776 X11_initialisation();
8777 if (x_initialised == YES) {
8779 color1 = get_x11_color(value);
8786 X11_initialisation();
8787 if (x_initialised == YES) {
8789 color2 = get_x11_color(value);
8796 X11_initialisation();
8797 if (x_initialised == YES) {
8799 color3 = get_x11_color(value);
8806 X11_initialisation();
8807 if (x_initialised == YES) {
8809 color4 = get_x11_color(value);
8816 X11_initialisation();
8817 if (x_initialised == YES) {
8819 color5 = get_x11_color(value);
8826 X11_initialisation();
8827 if (x_initialised == YES) {
8829 color6 = get_x11_color(value);
8836 X11_initialisation();
8837 if (x_initialised == YES) {
8839 color7 = get_x11_color(value);
8846 X11_initialisation();
8847 if (x_initialised == YES) {
8849 color8 = get_x11_color(value);
8856 X11_initialisation();
8857 if (x_initialised == YES) {
8859 color9 = get_x11_color(value);
8865 CONF("default_color") {
8866 X11_initialisation();
8867 if (x_initialised == YES) {
8869 default_fg_color = get_x11_color(value);
8875 CONF3("default_shade_color", "default_shadecolor") {
8876 X11_initialisation();
8877 if (x_initialised == YES) {
8879 default_bg_color = get_x11_color(value);
8885 CONF3("default_outline_color", "default_outlinecolor") {
8886 X11_initialisation();
8887 if (x_initialised == YES) {
8889 default_out_color = get_x11_color(value);
8896 CONF("own_window_colour") {
8897 X11_initialisation();
8898 if (x_initialised == YES) {
8900 background_colour = get_x11_color(value);
8902 ERR("Invalid colour for own_window_colour (try omitting the "
8903 "'#' for hex colours");
8909 //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
8910 if(output_methods & TO_X) {
8911 X11_initialisation();
8921 #undef CONF_CONTINUE
8922 #undef CONF_BUFF_SIZE
8929 static void print_help(const char *prog_name) {
8930 printf("Usage: %s [OPTION]...\n"
8931 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8932 "window. Command line options will override configurations defined in config\n"
8934 " -v, --version version\n"
8935 " -q, --quiet quiet mode\n"
8936 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
8937 " -c, --config=FILE config file to load\n"
8938 #ifdef CONFIG_OUTPUT
8939 " -C, --print-config print the builtin default config to stdout\n"
8940 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
8942 " -d, --daemonize daemonize, fork to background\n"
8943 " -h, --help help\n"
8945 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8946 " -f, --font=FONT font to use\n"
8947 " -X, --display=DISPLAY X11 display to use\n"
8949 " -o, --own-window create own window to draw\n"
8952 " -b, --double-buffer double buffer (prevents flickering)\n"
8954 " -w, --window-id=WIN_ID window id to draw\n"
8955 " -x X x position\n"
8956 " -y Y y position\n"
8958 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8959 " -u, --interval=SECS update interval\n"
8960 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8965 /* : means that character before that takes an argument */
8966 static const char *getopt_string = "vVqdDt:u:i:hc:"
8976 #ifdef CONFIG_OUTPUT
8981 static const struct option longopts[] = {
8982 { "help", 0, NULL, 'h' },
8983 { "version", 0, NULL, 'V' },
8984 { "debug", 0, NULL, 'D' },
8985 { "config", 1, NULL, 'c' },
8986 #ifdef CONFIG_OUTPUT
8987 { "print-config", 0, NULL, 'C' },
8989 { "daemonize", 0, NULL, 'd' },
8991 { "alignment", 1, NULL, 'a' },
8992 { "font", 1, NULL, 'f' },
8993 { "display", 1, NULL, 'X' },
8995 { "own-window", 0, NULL, 'o' },
8998 { "double-buffer", 0, NULL, 'b' },
9000 { "window-id", 1, NULL, 'w' },
9002 { "text", 1, NULL, 't' },
9003 { "interval", 0, NULL, 'u' },
9007 int main(int argc, char **argv)
9013 struct sigaction act, oact;
9015 g_signal_pending = 0;
9016 max_user_text = MAX_USER_TEXT_DEFAULT;
9018 memset(&info, 0, sizeof(info));
9019 memset(template, 0, sizeof(template));
9022 #ifdef TCP_PORT_MONITOR
9023 /* set default connection limit */
9024 tcp_portmon_set_max_connections(0);
9027 /* handle command line parameters that don't change configs */
9029 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9030 || ((s = getenv("LANG")) && *s)) {
9031 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9033 ERR("malloc failed");
9035 for (x = 0; x < strlen(s); x++) {
9036 temp[x] = tolower(s[x]);
9039 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9045 if (!setlocale(LC_CTYPE, "")) {
9046 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9050 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9061 if (current_config) {
9062 free(current_config);
9064 current_config = strndup(optarg, max_user_text);
9067 freopen("/dev/null", "w", stderr);
9070 print_help(argv[0]);
9072 #ifdef CONFIG_OUTPUT
9079 window.window = strtol(optarg, 0, 0);
9088 /* check if specified config file is valid */
9089 if (current_config) {
9091 if (stat(current_config, &sb) ||
9092 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9093 ERR("invalid configuration file '%s'\n", current_config);
9094 free(current_config);
9099 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9101 if (!current_config) {
9102 /* load default config file */
9103 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9106 /* Try to use personal config file first */
9107 to_real_path(buf, CONFIG_FILE);
9108 if (buf[0] && (fp = fopen(buf, "r"))) {
9109 current_config = strndup(buf, max_user_text);
9113 /* Try to use system config file if personal config not readable */
9114 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9115 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9119 /* No readable config found */
9120 if (!current_config) {
9121 #ifdef CONFIG_OUTPUT
9122 current_config = strdup("==builtin==");
9123 ERR("no readable personal or system-wide config file found,"
9124 " using builtin default");
9126 CRIT_ERR("no readable personal or system-wide config file found");
9127 #endif /* ! CONF_OUTPUT */
9130 #ifdef HAVE_SYS_INOTIFY_H
9131 inotify_fd = inotify_init();
9132 #endif /* HAVE_SYS_INOTIFY_H */
9134 load_config_file(current_config);
9136 /* init specials array */
9137 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9138 ERR("failed to create specials array");
9142 if (current_mail_spool == NULL) {
9145 variable_substitute(MAIL_FILE, buf, 256);
9147 if (buf[0] != '\0') {
9148 current_mail_spool = strndup(buf, text_buffer_size);
9153 /* handle other command line arguments */
9155 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9156 || defined(__NetBSD__)
9157 optind = optreset = 1;
9162 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9163 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9164 "kvm_open")) == NULL) {
9165 CRIT_ERR("cannot read kvm");
9170 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9178 fork_to_background = 1;
9181 global_debug_level++;
9185 set_first_font(optarg);
9188 text_alignment = string_to_alignment(optarg);
9193 disp = strdup(optarg);
9212 global_text = strndup(optarg, max_user_text);
9213 convert_escapes(global_text);
9217 update_interval = strtod(optarg, 0);
9218 if (info.music_player_interval == 0) {
9219 // default to update_interval
9220 info.music_player_interval = update_interval;
9225 total_run_times = strtod(optarg, 0);
9229 gap_x = atoi(optarg);
9233 gap_y = atoi(optarg);
9244 if (output_methods & TO_X) {
9245 load_config_file_x11(current_config);
9249 /* generate text and get initial size */
9250 extract_variable_text(global_text);
9257 if (fork_to_background) {
9262 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9269 fprintf(stderr, "\n");
9274 /* parent process */
9275 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9282 text_buffer = malloc(max_user_text);
9283 memset(text_buffer, 0, max_user_text);
9284 tmpstring1 = malloc(text_buffer_size);
9285 memset(tmpstring1, 0, text_buffer_size);
9286 tmpstring2 = malloc(text_buffer_size);
9287 memset(tmpstring2, 0, text_buffer_size);
9292 X11_create_window();
9295 /* Set signal handlers */
9296 act.sa_handler = signal_handler;
9297 sigemptyset(&act.sa_mask);
9300 act.sa_flags |= SA_RESTART;
9303 if ( sigaction(SIGINT, &act, &oact) < 0
9304 || sigaction(SIGUSR1, &act, &oact) < 0
9305 || sigaction(SIGHUP, &act, &oact) < 0
9306 || sigaction(SIGTERM, &act, &oact) < 0) {
9307 ERR("error setting signal handler: %s", strerror(errno));
9312 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9319 static void signal_handler(int sig)
9321 /* signal handler is light as a feather, as it should be.
9322 * we will poll g_signal_pending with each loop of conky
9323 * and do any signal processing there, NOT here. */
9324 g_signal_pending = sig;