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]");
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, "%127s %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
2845 CRIT_ERR("weather needs arguments: <uri> <icao> <data_type> [interval in minutes]");
2851 obj->data.s = strndup(arg, text_buffer_size);
2853 CRIT_ERR("lua needs arguments: <function name> [function parameters]");
2855 END OBJ(lua_parse, 0)
2857 obj->data.s = strndup(arg, text_buffer_size);
2859 CRIT_ERR("lua_parse needs arguments: <function name> [function parameters]");
2864 arg = scan_bar(arg, &obj->a, &obj->b);
2866 obj->data.s = strndup(arg, text_buffer_size);
2868 CRIT_ERR("lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2871 CRIT_ERR("lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2874 END OBJ(lua_graph, 0)
2875 SIZE_DEFAULTS(graph);
2878 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2879 &obj->e, &obj->char_a, &obj->char_b);
2883 CRIT_ERR("lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2886 CRIT_ERR("lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2888 END OBJ(lua_gauge, 0)
2889 SIZE_DEFAULTS(gauge);
2891 arg = scan_gauge(arg, &obj->a, &obj->b);
2893 obj->data.s = strndup(arg, text_buffer_size);
2895 CRIT_ERR("lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2898 CRIT_ERR("lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2901 #endif /* HAVE_LUA */
2904 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2905 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2906 ERR("hddtemp needs arguments");
2907 obj->type = OBJ_text;
2908 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2909 obj->data.hddtemp.update_time = 0;
2911 obj->data.hddtemp.temp = NULL;
2912 #endif /* HDDTEMP */
2913 #ifdef TCP_PORT_MONITOR
2914 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2915 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2916 #endif /* TCP_PORT_MONITOR */
2917 END OBJ(entropy_avail, INFO_ENTROPY)
2918 END OBJ(entropy_perc, INFO_ENTROPY)
2919 END OBJ(entropy_poolsize, INFO_ENTROPY)
2920 END OBJ(entropy_bar, INFO_ENTROPY)
2922 scan_bar(arg, &obj->a, &obj->b);
2925 obj->sub = malloc(sizeof(struct text_object));
2926 extract_variable_text_internal(obj->sub, arg, 0);
2928 CRIT_ERR("blink needs a argument");
2930 END OBJ(to_bytes, 0)
2932 obj->sub = malloc(sizeof(struct text_object));
2933 extract_variable_text_internal(obj->sub, arg, 0);
2935 CRIT_ERR("to_bytes needs a argument");
2940 obj->data.scroll.step = 1;
2941 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2942 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
2944 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2945 obj->data.scroll.start = 0;
2946 obj->sub = malloc(sizeof(struct text_object));
2947 extract_variable_text_internal(obj->sub,
2948 obj->data.scroll.text, 0);
2950 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
2955 unsigned int indenting = 0; //vars can be used as args for other vars
2958 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
2960 for (i=0; arg[i] != 0 && j < 2; i++) {
2961 if(startvar[j] == -1) {
2965 }else if(endvar[j] == -1) {
2968 }else if(arg[i] == '}') {
2971 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
2977 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
2978 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
2979 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
2980 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
2982 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
2983 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
2985 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
2986 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
2988 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
2989 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
2991 obj->sub = malloc(sizeof(struct text_object));
2992 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
2993 obj->sub->sub = malloc(sizeof(struct text_object));
2994 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
2996 CRIT_ERR("combine needs arguments: <text1> <text2>");
2999 CRIT_ERR("combine needs arguments: <text1> <text2>");
3004 CRIT_ERR("nvidia needs an argument\n");
3005 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3006 CRIT_ERR("nvidia: invalid argument"
3007 " specified: '%s'\n", arg);
3012 END OBJ(apcupsd, INFO_APCUPSD)
3016 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3017 CRIT_ERR("apcupsd needs arguments: <host> <port>");
3019 info.apcupsd.port = htons(port);
3020 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3023 CRIT_ERR("apcupsd needs arguments: <host> <port>");
3025 END OBJ(apcupsd_name, INFO_APCUPSD)
3026 END OBJ(apcupsd_model, INFO_APCUPSD)
3027 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3028 END OBJ(apcupsd_cable, INFO_APCUPSD)
3029 END OBJ(apcupsd_status, INFO_APCUPSD)
3030 END OBJ(apcupsd_linev, INFO_APCUPSD)
3031 END OBJ(apcupsd_load, INFO_APCUPSD)
3032 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3034 scan_bar(arg, &obj->a, &obj->b);
3036 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3038 SIZE_DEFAULTS(graph);
3039 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3040 &obj->e, &obj->char_a, &obj->char_b);
3042 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3043 SIZE_DEFAULTS(gauge);
3044 scan_gauge(arg, &obj->a, &obj->b);
3046 END OBJ(apcupsd_charge, INFO_APCUPSD)
3047 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3048 END OBJ(apcupsd_temp, INFO_APCUPSD)
3049 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3050 #endif /* APCUPSD */
3054 ERR("unknown variable %s", s);
3055 obj->type = OBJ_text;
3056 snprintf(buf, 256, "${%s}", s);
3057 obj->data.s = strndup(buf, text_buffer_size);
3064 static struct text_object *create_plain_text(const char *s)
3066 struct text_object *obj;
3068 if (s == NULL || *s == '\0') {
3072 obj = new_text_object_internal();
3074 obj->type = OBJ_text;
3075 obj->data.s = strndup(s, text_buffer_size);
3079 /* backslash_escape - do the actual substitution task for template objects
3081 * The field templates is used for substituting the \N occurences. Set it to
3082 * NULL to leave them as they are.
3084 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3088 unsigned int dup_idx = 0, dup_len;
3090 dup_len = strlen(src) + 1;
3091 src_dup = malloc(dup_len * sizeof(char));
3099 if (*(p + 1) == '\\') {
3100 src_dup[dup_idx++] = '\\';
3102 } else if (*(p + 1) == ' ') {
3103 src_dup[dup_idx++] = ' ';
3105 } else if (*(p + 1) == 'n') {
3106 src_dup[dup_idx++] = '\n';
3108 } else if (templates) {
3109 unsigned int tmpl_num;
3111 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3112 (tmpl_num > template_count))
3114 dup_len += strlen(templates[tmpl_num - 1]);
3115 src_dup = realloc(src_dup, dup_len * sizeof(char));
3116 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3117 dup_idx += strlen(templates[tmpl_num - 1]);
3122 src_dup[dup_idx++] = *p;
3127 src_dup[dup_idx] = '\0';
3128 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3132 /* handle_template_object - core logic of the template object
3134 * use config variables like this:
3135 * template1 = "$\1\2"
3136 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3138 * and use them like this:
3139 * ${template1 node name}
3140 * ${template2 root /}
3141 * ${template2 cdrom /mnt/cdrom}
3143 static char *handle_template(const char *tmpl, const char *args)
3145 char *args_dup = NULL;
3147 char **argsp = NULL;
3148 unsigned int argcnt = 0, template_idx, i;
3151 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3152 (template_idx >= MAX_TEMPLATES))
3156 args_dup = strdup(args);
3159 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3161 if (p > args_dup && *(p - 1) == '\\')
3164 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3170 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3171 argsp[argcnt - 1] = p_old;
3173 for (i = 0; i < argcnt; i++) {
3175 tmp = backslash_escape(argsp[i], NULL, 0);
3176 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3181 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3182 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3184 for (i = 0; i < argcnt; i++)
3190 static char *find_and_replace_templates(const char *inbuf)
3192 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3195 outlen = strlen(inbuf) + 1;
3196 o = outbuf = calloc(outlen, sizeof(char));
3197 memset(outbuf, 0, outlen * sizeof(char));
3199 p = indup = strdup(inbuf);
3201 while (*p && *p != '$')
3207 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3212 if (*(p + 1) == '{') {
3215 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3223 while (*p && stack > 0) {
3231 // stack is empty. that means the previous char was }, so we zero it
3234 // we ran into the end of string without finding a closing }, bark
3235 CRIT_ERR("cannot find a closing '}' in template expansion");
3239 while (*p && !isspace(*p))
3243 tmpl_out = handle_template(templ, args);
3245 outlen += strlen(tmpl_out);
3247 outbuf = realloc(outbuf, outlen * sizeof(char));
3248 strcat (outbuf, tmpl_out);
3250 o = outbuf + strlen(outbuf);
3252 ERR("failed to handle template '%s' with args '%s'", templ, args);
3256 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3261 static int text_contains_templates(const char *text)
3263 if (strcasestr(text, "${template") != NULL)
3265 if (strcasestr(text, "$template") != NULL)
3270 /* folds a string over top of itself, like so:
3272 * if start is "blah", and you call it with count = 1, the result will be "lah"
3274 static void strfold(char *start, int count)
3277 for (curplace = start + count; *curplace != 0; curplace++) {
3278 *(curplace - count) = *curplace;
3280 *(curplace - count) = 0;
3284 * - assumes that *string is '#'
3285 * - removes the part from '#' to the end of line ('\n' or '\0')
3286 * - it removes the '\n'
3287 * - copies the last char into 'char *last' argument, which should be a pointer
3288 * to a char rather than a string.
3290 static size_t remove_comment(char *string, char *last)
3293 while (*end != '\0' && *end != '\n') {
3296 if (last) *last = *end;
3297 if (*end == '\n') end++;
3298 strfold(string, end - string);
3299 return end - string;
3302 static size_t remove_comments(char *string)
3306 for (curplace = string; *curplace != 0; curplace++) {
3307 if (*curplace == '\\' && *(curplace + 1) == '#') {
3308 // strcpy can't be used for overlapping strings
3309 strfold(curplace, 1);
3311 } else if (*curplace == '#') {
3312 folded += remove_comment(curplace, 0);
3318 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3320 struct text_object *obj;
3321 char *p, *s, *orig_p;
3323 void *ifblock_opaque = NULL;
3328 p = strndup(const_p, max_user_text - 1);
3329 while (text_contains_templates(p)) {
3331 tmp = find_and_replace_templates(p);
3337 if (strcmp(p, const_p)) {
3338 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3340 DBGP("no templates to replace");
3343 memset(retval, 0, sizeof(struct text_object));
3345 line = global_text_lines;
3353 obj = create_plain_text(s);
3355 append_object(retval, obj);
3365 /* variable is either $foo or ${foo} */
3367 unsigned int brl = 1, brr = 0;
3371 while (*p && brl != brr) {
3386 while (*p && (isalnum((int) *p) || *p == '_')) {
3391 /* copy variable to buffer */
3392 len = (p - s > 255) ? 255 : (p - s);
3393 strncpy(buf, s, len);
3401 /* search for variable in environment */
3405 obj = create_plain_text(var);
3407 append_object(retval, obj);
3412 /* if variable wasn't found in environment, use some special */
3417 if (strchr(buf, ' ')) {
3418 arg = strchr(buf, ' ');
3421 while (isspace((int) *arg)) {
3429 /* lowercase variable name */
3432 *tmp_p = tolower(*tmp_p);
3436 obj = construct_text_object(buf, arg,
3437 line, allow_threaded,
3440 append_object(retval, obj);
3444 obj = create_plain_text("$");
3447 append_object(retval, obj);
3450 } else if (*p == '\\' && *(p+1) == '#') {
3452 } else if (*p == '#') {
3454 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3455 /* if remove_comment removed a newline, we need to 'back up' with p */
3461 obj = create_plain_text(s);
3463 append_object(retval, obj);
3466 if (!ifblock_stack_empty(&ifblock_opaque)) {
3467 ERR("one or more $endif's are missing");
3474 static void extract_variable_text(const char *p)
3476 free_text_objects(&global_root_object, 0);
3490 extract_variable_text_internal(&global_root_object, p, 1);
3493 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3495 extract_variable_text_internal(root, txt, 0);
3496 generate_text_internal(p, max_user_text, *root, cur);
3500 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3501 void *thread(void *), const char *text)
3503 if (obj->char_b && info.mail) {
3504 // this means we use info
3505 if (!info.mail->p_timed_thread) {
3506 info.mail->p_timed_thread =
3507 timed_thread_create(thread,
3508 (void *) info.mail, info.mail->interval * 1000000);
3509 if (!info.mail->p_timed_thread) {
3510 ERR("Error creating %s timed thread", text);
3512 timed_thread_register(info.mail->p_timed_thread,
3513 &info.mail->p_timed_thread);
3514 if (timed_thread_run(info.mail->p_timed_thread)) {
3515 ERR("Error running %s timed thread", text);
3519 } else if (obj->data.mail) {
3520 // this means we use obj
3521 if (!obj->data.mail->p_timed_thread) {
3522 obj->data.mail->p_timed_thread =
3523 timed_thread_create(thread,
3524 (void *) obj->data.mail,
3525 obj->data.mail->interval * 1000000);
3526 if (!obj->data.mail->p_timed_thread) {
3527 ERR("Error creating %s timed thread", text);
3529 timed_thread_register(obj->data.mail->p_timed_thread,
3530 &obj->data.mail->p_timed_thread);
3531 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3532 ERR("Error running %s timed thread", text);
3535 return obj->data.mail;
3536 } else if (!obj->a) {
3537 // something is wrong, warn once then stop
3538 ERR("There's a problem with your mail settings. "
3539 "Check that the global mail settings are properly defined"
3540 " (line %li).", obj->line);
3546 char *format_time(unsigned long timeval, const int width)
3549 unsigned long nt; // narrow time, for speed on 32-bit
3550 unsigned cc; // centiseconds
3551 unsigned nn; // multi-purpose whatever
3554 cc = nt % 100; // centiseconds past second
3555 nt /= 100; // total seconds
3556 nn = nt % 60; // seconds past the minute
3557 nt /= 60; // total minutes
3558 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3560 return strndup(buf, text_buffer_size);
3562 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3563 return strndup(buf, text_buffer_size);
3565 nn = nt % 60; // minutes past the hour
3566 nt /= 60; // total hours
3567 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3568 return strndup(buf, text_buffer_size);
3570 nn = nt; // now also hours
3571 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3572 return strndup(buf, text_buffer_size);
3574 nn /= 24; // now days
3575 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3576 return strndup(buf, text_buffer_size);
3578 nn /= 7; // now weeks
3579 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3580 return strndup(buf, text_buffer_size);
3582 // well shoot, this outta' fit...
3583 return strndup("<inf>", text_buffer_size);
3586 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3587 //string has to end with \0 and it's length should fit in a int
3589 void remove_deleted_chars(char *string){
3591 while(string[i] != 0){
3592 if(string[i] == BACKSPACE){
3594 strcpy( &(string[i-1]), &(string[i+1]) );
3596 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3601 static inline void format_media_player_time(char *buf, const int size,
3604 int days, hours, minutes;
3606 days = seconds / (24 * 60 * 60);
3607 seconds %= (24 * 60 * 60);
3608 hours = seconds / (60 * 60);
3609 seconds %= (60 * 60);
3610 minutes = seconds / 60;
3614 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3615 hours, minutes, seconds);
3616 } else if (hours > 0) {
3617 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3620 snprintf(buf, size, "%i:%02i", minutes, seconds);
3624 static inline double get_barnum(char *buf)
3636 if (sscanf(buf, "%lf", &barnum) == 0) {
3637 ERR("reading exec value failed (perhaps it's not the "
3638 "correct format?)");
3641 if (barnum > 100.0 || barnum < 0.0) {
3642 ERR("your exec value is not between 0 and 100, "
3643 "therefore it will be ignored");
3649 static void generate_text_internal(char *p, int p_max_size,
3650 struct text_object root, struct information *cur)
3652 struct text_object *obj;
3654 int need_to_load_fonts = 0;
3657 /* for the OBJ_top* handler */
3658 struct process **needed = 0;
3661 char buff_in[p_max_size];
3663 iconv_converting = 0;
3664 #endif /* HAVE_ICONV */
3668 while (obj && p_max_size > 0) {
3669 needed = 0; // reset for top stuff
3671 /* IFBLOCK jumping algorithm
3673 * This is easier as it looks like:
3674 * - each IF checks it's condition
3675 * - on FALSE: call DO_JUMP
3676 * - on TRUE: don't care
3677 * - each ELSE calls DO_JUMP unconditionally
3678 * - each ENDIF is silently being ignored
3681 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3682 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3683 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3684 * regularly, "obj" is being updated to point to obj->next, so object parsing
3685 * continues right after the corresponding ELSE or ENDIF. This means that if we
3686 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3687 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3688 * jumped, and there is nothing to do.
3692 obj = obj->data.ifblock.next; \
3695 #define OBJ(a) break; case OBJ_##a:
3697 switch (obj->type) {
3699 ERR("not implemented obj type %d", obj->type);
3702 struct sockaddr_in addr;
3703 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3705 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3707 memset(&addr, 0, sizeof(addr));
3708 addr.sin_family = AF_INET;
3709 addr.sin_port = obj->data.read_tcp.port;
3710 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3711 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3715 FD_SET(sock, &readfds);
3718 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3719 received = recv(sock, p, p_max_size, 0);
3724 ERR("read_tcp: Couldn't create a connection");
3727 ERR("read_tcp: Couldn't create a socket");
3730 ERR("read_tcp: Problem with resolving the hostname");
3735 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3737 #endif /* !__OpenBSD__ */
3740 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3741 obj->data.cpu_index);
3747 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3748 obj->data.cpu_index);
3750 /* OpenBSD has no such flag (SUSv2) */
3751 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3752 obj->data.cpu_index);
3753 #endif /* __OpenBSD */
3756 #if defined(__linux__)
3759 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3760 obj->data.cpu_index);
3765 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3766 obj->data.cpu_index);
3771 OBJ(wireless_essid) {
3772 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3774 OBJ(wireless_mode) {
3775 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3777 OBJ(wireless_bitrate) {
3778 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3781 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3783 OBJ(wireless_link_qual) {
3784 spaced_print(p, p_max_size, "%d", 4,
3785 obj->data.net->link_qual);
3787 OBJ(wireless_link_qual_max) {
3788 spaced_print(p, p_max_size, "%d", 4,
3789 obj->data.net->link_qual_max);
3791 OBJ(wireless_link_qual_perc) {
3792 if (obj->data.net->link_qual_max > 0) {
3793 spaced_print(p, p_max_size, "%.0f", 5,
3794 (double) obj->data.net->link_qual /
3795 obj->data.net->link_qual_max * 100);
3797 spaced_print(p, p_max_size, "unk", 5);
3800 OBJ(wireless_link_bar) {
3802 if(output_methods & TO_X) {
3803 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3804 obj->data.net->link_qual_max) * 255.0);
3807 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3808 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3809 obj->data.net->link_qual_max) * 100.0, obj->a);
3814 #endif /* HAVE_IWLIB */
3816 #endif /* __linux__ */
3820 get_adt746x_cpu(p, p_max_size);
3823 get_adt746x_fan(p, p_max_size);
3826 get_acpi_fan(p, p_max_size);
3828 OBJ(acpiacadapter) {
3829 get_acpi_ac_adapter(p, p_max_size);
3832 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3835 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3837 OBJ(battery_percent) {
3838 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3842 if(output_methods & TO_X) {
3843 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3846 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3847 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3852 OBJ(battery_short) {
3853 get_battery_short_status(p, p_max_size, obj->data.s);
3855 #endif /* __OpenBSD__ */
3858 human_readable(cur->buffers * 1024, p, 255);
3861 human_readable(cur->cached * 1024, p, 255);
3864 if (obj->data.cpu_index > info.cpu_count) {
3865 ERR("obj->data.cpu_index %i info.cpu_count %i",
3866 obj->data.cpu_index, info.cpu_count);
3867 CRIT_ERR("attempting to use more CPUs than you have!");
3869 percent_print(p, p_max_size,
3870 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3874 new_gauge(p, obj->a, obj->b,
3875 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3879 if(output_methods & TO_X) {
3880 new_bar(p, obj->a, obj->b,
3881 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3884 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3885 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3892 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3893 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3894 100, 1, obj->char_a, obj->char_b);
3897 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3898 obj->e, 1, obj->char_a, obj->char_b);
3901 new_fg(p, obj->data.l);
3934 OBJ(conky_version) {
3935 snprintf(p, p_max_size, "%s", VERSION);
3937 OBJ(conky_build_date) {
3938 snprintf(p, p_max_size, "%s", BUILD_DATE);
3940 OBJ(conky_build_arch) {
3941 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3943 #if defined(__linux__)
3945 snprintf(p, p_max_size, "%s",
3946 get_disk_protect_queue(obj->data.s));
3949 snprintf(p, p_max_size, "%s", i8k.version);
3952 snprintf(p, p_max_size, "%s", i8k.bios);
3955 snprintf(p, p_max_size, "%s", i8k.serial);
3960 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3961 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3963 OBJ(i8k_left_fan_status) {
3964 int left_fan_status;
3966 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3967 if (left_fan_status == 0) {
3968 snprintf(p, p_max_size, "off");
3970 if (left_fan_status == 1) {
3971 snprintf(p, p_max_size, "low");
3973 if (left_fan_status == 2) {
3974 snprintf(p, p_max_size, "high");
3977 OBJ(i8k_right_fan_status) {
3978 int right_fan_status;
3980 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3981 if (right_fan_status == 0) {
3982 snprintf(p, p_max_size, "off");
3984 if (right_fan_status == 1) {
3985 snprintf(p, p_max_size, "low");
3987 if (right_fan_status == 2) {
3988 snprintf(p, p_max_size, "high");
3991 OBJ(i8k_left_fan_rpm) {
3992 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3994 OBJ(i8k_right_fan_rpm) {
3995 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3997 OBJ(i8k_ac_status) {
4000 sscanf(i8k.ac_status, "%d", &ac_status);
4001 if (ac_status == -1) {
4002 snprintf(p, p_max_size, "disabled (read i8k docs)");
4004 if (ac_status == 0) {
4005 snprintf(p, p_max_size, "off");
4007 if (ac_status == 1) {
4008 snprintf(p, p_max_size, "on");
4011 OBJ(i8k_buttons_status) {
4012 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4016 get_ibm_acpi_fan(p, p_max_size);
4019 get_ibm_acpi_temps();
4020 temp_print(p, p_max_size,
4021 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4024 get_ibm_acpi_volume(p, p_max_size);
4026 OBJ(ibm_brightness) {
4027 get_ibm_acpi_brightness(p, p_max_size);
4030 /* information from sony_laptop kernel module
4031 * /sys/devices/platform/sony-laptop */
4032 OBJ(sony_fanspeed) {
4033 get_sony_fanspeed(p, p_max_size);
4036 if (!cur->gw_info.count) {
4041 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4044 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4047 snprintf(p, p_max_size, "%d", get_laptop_mode());
4050 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4052 #endif /* __linux__ */
4053 #if (defined(__FreeBSD__) || defined(__linux__))
4055 if ((obj->data.ifblock.s)
4056 && (!interface_up(obj->data.ifblock.s))) {
4062 OBJ(obsd_sensors_temp) {
4063 obsd_sensors.device = sensor_device;
4064 update_obsd_sensors();
4065 temp_print(p, p_max_size,
4066 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4069 OBJ(obsd_sensors_fan) {
4070 obsd_sensors.device = sensor_device;
4071 update_obsd_sensors();
4072 snprintf(p, p_max_size, "%d",
4073 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4075 OBJ(obsd_sensors_volt) {
4076 obsd_sensors.device = sensor_device;
4077 update_obsd_sensors();
4078 snprintf(p, p_max_size, "%.2f",
4079 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4082 get_obsd_vendor(p, p_max_size);
4085 get_obsd_product(p, p_max_size);
4087 #endif /* __OpenBSD__ */
4090 new_font(p, obj->data.s);
4091 need_to_load_fonts = 1;
4094 /* TODO: move this correction from kB to kB/s elsewhere
4095 * (or get rid of it??) */
4097 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4101 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4105 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4110 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4111 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4113 OBJ(diskiograph_read) {
4114 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4115 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4117 OBJ(diskiograph_write) {
4118 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4119 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4123 human_readable(obj->data.net->recv_speed, p, 255);
4126 spaced_print(p, p_max_size, "%.1f", 8,
4127 obj->data.net->recv_speed / 1024.0);
4130 OBJ(downspeedgraph) {
4131 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4132 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4136 /* Since we see you, you're if has not jumped.
4137 * Do Ninja jump here: without leaving traces.
4138 * This is to prevent us from stale jumped flags.
4140 obj = obj->data.ifblock.next;
4144 /* harmless object, just ignore */
4148 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4149 && (obj->data.net->addr.sa_data[3] & 255) == 0
4150 && (obj->data.net->addr.sa_data[4] & 255) == 0
4151 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4152 snprintf(p, p_max_size, "No Address");
4154 snprintf(p, p_max_size, "%u.%u.%u.%u",
4155 obj->data.net->addr.sa_data[2] & 255,
4156 obj->data.net->addr.sa_data[3] & 255,
4157 obj->data.net->addr.sa_data[4] & 255,
4158 obj->data.net->addr.sa_data[5] & 255);
4161 #if defined(__linux__)
4163 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4165 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4166 strcpy(p, obj->data.net->addrs);
4169 strcpy(p, "0.0.0.0");
4171 #endif /* __linux__ */
4172 #if defined(IMLIB2) && defined(X11)
4174 /* doesn't actually draw anything, just queues it omp. the
4175 * image will get drawn after the X event loop */
4176 cimlib_add_image(obj->data.s);
4180 evaluate(obj->data.s, p);
4183 read_exec(obj->data.s, p, text_buffer_size);
4184 remove_deleted_chars(p);
4187 struct information *tmp_info;
4188 struct text_object subroot;
4190 read_exec(obj->data.s, p, text_buffer_size);
4192 tmp_info = malloc(sizeof(struct information));
4193 memcpy(tmp_info, cur, sizeof(struct information));
4194 parse_conky_vars(&subroot, p, p, tmp_info);
4196 free_text_objects(&subroot, 1);
4203 read_exec(obj->data.s, p, text_buffer_size);
4204 barnum = get_barnum(p); /*using the same function*/
4206 if (barnum >= 0.0) {
4208 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4215 read_exec(obj->data.s, p, text_buffer_size);
4216 barnum = get_barnum(p);
4218 if (barnum >= 0.0) {
4220 if(output_methods & TO_X) {
4222 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4225 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4226 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4234 char showaslog = FALSE;
4235 char tempgrad = FALSE;
4237 char *cmd = obj->data.s;
4239 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4241 cmd += strlen(" "TEMPGRAD);
4243 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4245 cmd += strlen(" "LOGGRAPH);
4247 read_exec(cmd, p, text_buffer_size);
4248 barnum = get_barnum(p);
4251 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4252 100, 1, showaslog, tempgrad);
4257 if (current_update_time - obj->data.execi.last_update
4258 >= obj->data.execi.interval) {
4261 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4262 barnum = get_barnum(p);
4264 if (barnum >= 0.0) {
4267 obj->data.execi.last_update = current_update_time;
4270 if(output_methods & TO_X) {
4271 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4274 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4275 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4282 if (current_update_time - obj->data.execi.last_update
4283 >= obj->data.execi.interval) {
4285 char showaslog = FALSE;
4286 char tempgrad = FALSE;
4287 char *cmd = obj->data.execi.cmd;
4289 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4291 cmd += strlen(" "TEMPGRAD);
4293 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4295 cmd += strlen(" "LOGGRAPH);
4297 obj->char_a = showaslog;
4298 obj->char_b = tempgrad;
4299 read_exec(cmd, p, text_buffer_size);
4300 barnum = get_barnum(p);
4302 if (barnum >= 0.0) {
4305 obj->data.execi.last_update = current_update_time;
4307 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4310 if (current_update_time - obj->data.execi.last_update
4311 >= obj->data.execi.interval) {
4314 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4315 barnum = get_barnum(p);
4317 if (barnum >= 0.0) {
4318 obj->f = 255 * barnum / 100.0;
4320 obj->data.execi.last_update = current_update_time;
4322 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4326 if (current_update_time - obj->data.execi.last_update
4327 >= obj->data.execi.interval
4328 && obj->data.execi.interval != 0) {
4329 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4331 obj->data.execi.last_update = current_update_time;
4333 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4336 struct text_object subroot;
4337 struct information *tmp_info =
4338 malloc(sizeof(struct information));
4339 memcpy(tmp_info, cur, sizeof(struct information));
4341 if (current_update_time - obj->data.execi.last_update
4342 < obj->data.execi.interval
4343 || obj->data.execi.interval == 0) {
4344 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4346 char *output = obj->data.execi.buffer;
4347 FILE *fp = popen(obj->data.execi.cmd, "r");
4348 int length = fread(output, 1, text_buffer_size, fp);
4352 output[length] = '\0';
4353 if (length > 0 && output[length - 1] == '\n') {
4354 output[length - 1] = '\0';
4357 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4358 obj->data.execi.last_update = current_update_time;
4360 free_text_objects(&subroot, 1);
4364 if (!obj->data.texeci.p_timed_thread) {
4365 obj->data.texeci.p_timed_thread =
4366 timed_thread_create(&threaded_exec,
4367 (void *) obj, obj->data.texeci.interval * 1000000);
4368 if (!obj->data.texeci.p_timed_thread) {
4369 ERR("Error creating texeci timed thread");
4371 timed_thread_register(obj->data.texeci.p_timed_thread,
4372 &obj->data.texeci.p_timed_thread);
4373 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4374 ERR("Error running texeci timed thread");
4377 timed_thread_lock(obj->data.texeci.p_timed_thread);
4378 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4379 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4382 #endif /* HAVE_POPEN */
4384 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4386 if (mail && mail->p_timed_thread) {
4387 timed_thread_lock(mail->p_timed_thread);
4388 snprintf(p, p_max_size, "%lu", mail->unseen);
4389 timed_thread_unlock(mail->p_timed_thread);
4392 OBJ(imap_messages) {
4393 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4395 if (mail && mail->p_timed_thread) {
4396 timed_thread_lock(mail->p_timed_thread);
4397 snprintf(p, p_max_size, "%lu", mail->messages);
4398 timed_thread_unlock(mail->p_timed_thread);
4402 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4404 if (mail && mail->p_timed_thread) {
4405 timed_thread_lock(mail->p_timed_thread);
4406 snprintf(p, p_max_size, "%lu", mail->unseen);
4407 timed_thread_unlock(mail->p_timed_thread);
4411 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4413 if (mail && mail->p_timed_thread) {
4414 timed_thread_lock(mail->p_timed_thread);
4415 snprintf(p, p_max_size, "%.1f",
4416 mail->used / 1024.0 / 1024.0);
4417 timed_thread_unlock(mail->p_timed_thread);
4421 if (obj->data.fs != NULL) {
4422 if (obj->data.fs->size == 0) {
4424 if(output_methods & TO_X) {
4425 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4428 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4429 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4435 if(output_methods & TO_X) {
4436 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4437 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4438 obj->data.fs->size));
4441 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4442 new_bar_in_shell(p, p_max_size,
4443 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4451 if (obj->data.fs != NULL) {
4452 human_readable(obj->data.fs->avail, p, 255);
4456 if (obj->data.fs != NULL) {
4459 if (obj->data.fs->size) {
4460 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4463 percent_print(p, p_max_size, val);
4467 if (obj->data.fs != NULL) {
4468 human_readable(obj->data.fs->size, p, 255);
4472 if (obj->data.fs != NULL)
4473 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4476 if (obj->data.fs != NULL) {
4477 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4482 if (obj->data.fs != NULL) {
4483 if (obj->data.fs->size == 0) {
4485 if(output_methods & TO_X) {
4486 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4489 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4490 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4496 if(output_methods & TO_X) {
4497 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4498 (int) (obj->data.fsbar.fs->avail * 255 /
4499 obj->data.fs->size));
4502 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4503 new_bar_in_shell(p, p_max_size,
4504 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4512 if (obj->data.fs != NULL) {
4515 if (obj->data.fs->size) {
4516 val = obj->data.fs->free
4521 percent_print(p, p_max_size, 100 - val);
4525 float *v = info.loadavg;
4527 if (obj->data.loadavg[2]) {
4528 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4529 v[obj->data.loadavg[0] - 1],
4530 v[obj->data.loadavg[1] - 1],
4531 v[obj->data.loadavg[2] - 1]);
4532 } else if (obj->data.loadavg[1]) {
4533 snprintf(p, p_max_size, "%.2f %.2f",
4534 v[obj->data.loadavg[0] - 1],
4535 v[obj->data.loadavg[1] - 1]);
4536 } else if (obj->data.loadavg[0]) {
4537 snprintf(p, p_max_size, "%.2f",
4538 v[obj->data.loadavg[0] - 1]);
4542 new_goto(p, obj->data.i);
4545 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4549 new_hr(p, obj->data.i);
4553 if (cur->nameserver_info.nscount > obj->data.i)
4554 snprintf(p, p_max_size, "%s",
4555 cur->nameserver_info.ns_list[obj->data.i]);
4559 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4560 snprintf(p, p_max_size, "%s", skill);
4565 PRSS *data = get_rss_info(obj->data.rss.uri,
4566 obj->data.rss.delay);
4570 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4572 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4574 // remove trailing new line if one exists
4575 if (str[strlen(str) - 1] == '\n') {
4576 str[strlen(str) - 1] = 0;
4578 snprintf(p, p_max_size, "%s", str);
4579 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4580 if (obj->data.rss.act_par < data->item_count) {
4581 str = data->items[obj->data.rss.act_par].title;
4582 // remove trailing new line if one exists
4583 if (str[strlen(str) - 1] == '\n') {
4584 str[strlen(str) - 1] = 0;
4586 snprintf(p, p_max_size, "%s", str);
4588 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4589 if (obj->data.rss.act_par < data->item_count) {
4591 data->items[obj->data.rss.act_par].description;
4592 // remove trailing new line if one exists
4593 if (str[strlen(str) - 1] == '\n') {
4594 str[strlen(str) - 1] = 0;
4596 snprintf(p, p_max_size, "%s", str);
4598 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4599 if (data->item_count > 0) {
4602 //'tmpspaces' is a string with spaces too be placed in front of each title
4603 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4604 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4605 tmpspaces[obj->data.rss.nrspaces]=0;
4609 if (obj->data.rss.act_par > data->item_count) {
4610 show = data->item_count;
4612 show = obj->data.rss.act_par;
4614 for (itmp = 0; itmp < show; itmp++) {
4615 PRSS_Item *item = &data->items[itmp];
4619 // don't add new line before first item
4621 strncat(p, "\n", p_max_size);
4623 /* remove trailing new line if one exists,
4624 * we have our own */
4625 if (str[strlen(str) - 1] == '\n') {
4626 str[strlen(str) - 1] = 0;
4628 strncat(p, tmpspaces, p_max_size);
4629 strncat(p, str, p_max_size);
4640 process_weather_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4645 char *str = llua_getstring(obj->data.s);
4647 snprintf(p, p_max_size, "%s", str);
4652 char *str = llua_getstring(obj->data.s);
4659 if (llua_getnumber(obj->data.s, &per)) {
4661 if(output_methods & TO_X) {
4662 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4665 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4666 new_bar_in_shell(p, p_max_size, per, obj->a);
4675 if (llua_getnumber(obj->data.s, &per)) {
4676 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4677 per, obj->e, 1, obj->char_a, obj->char_b);
4682 if (llua_getnumber(obj->data.s, &per)) {
4683 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4687 #endif /* HAVE_LUA */
4692 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4693 if (obj->data.hddtemp.temp)
4694 free(obj->data.hddtemp.temp);
4695 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4696 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4697 obj->data.hddtemp.update_time = current_update_time;
4699 if (!obj->data.hddtemp.temp) {
4700 snprintf(p, p_max_size, "N/A");
4702 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4703 unit = obj->data.hddtemp.temp[0];
4705 if (*endptr != '\0')
4706 snprintf(p, p_max_size, "N/A");
4707 else if (unit == 'C')
4708 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4709 else if (unit == 'F')
4710 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4712 snprintf(p, p_max_size, "N/A");
4717 new_offset(p, obj->data.i);
4720 new_voffset(p, obj->data.i);
4726 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4727 obj->data.sysfs.devtype, obj->data.sysfs.type);
4729 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4731 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4732 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4733 } else if (r >= 100.0 || r == 0) {
4734 snprintf(p, p_max_size, "%d", (int) r);
4736 snprintf(p, p_max_size, "%.1f", r);
4742 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4743 obj->data.sysfs.devtype, obj->data.sysfs.type);
4745 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4747 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4748 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4749 } else if (r >= 100.0 || r == 0) {
4750 snprintf(p, p_max_size, "%d", (int) r);
4752 snprintf(p, p_max_size, "%.1f", r);
4758 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4759 obj->data.sysfs.devtype, obj->data.sysfs.type);
4761 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4763 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4764 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4765 } else if (r >= 100.0 || r == 0) {
4766 snprintf(p, p_max_size, "%d", (int) r);
4768 snprintf(p, p_max_size, "%.1f", r);
4771 #endif /* __linux__ */
4773 new_alignr(p, obj->data.i);
4776 new_alignc(p, obj->data.i);
4779 char buf[max_user_text];
4780 struct information *tmp_info =
4781 malloc(sizeof(struct information));
4782 memcpy(tmp_info, cur, sizeof(struct information));
4783 generate_text_internal(buf, max_user_text,
4784 *obj->sub, tmp_info);
4786 if (strlen(buf) != 0) {
4792 char expression[max_user_text];
4794 struct information *tmp_info;
4796 tmp_info = malloc(sizeof(struct information));
4797 memcpy(tmp_info, cur, sizeof(struct information));
4798 generate_text_internal(expression, max_user_text,
4799 *obj->sub, tmp_info);
4800 DBGP("parsed arg into '%s'", expression);
4802 val = compare(expression);
4804 ERR("compare failed for expression '%s'",
4812 if (obj->data.ifblock.str
4813 && !check_contains(obj->data.ifblock.s,
4814 obj->data.ifblock.str)) {
4816 } else if (obj->data.ifblock.s
4817 && access(obj->data.ifblock.s, F_OK)) {
4822 if ((obj->data.ifblock.s)
4823 && (!check_mount(obj->data.ifblock.s))) {
4829 if (!get_process_by_name(obj->data.ifblock.s)) {
4831 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4836 #if defined(__linux__)
4838 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4842 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4845 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4850 human_readable(cur->mem * 1024, p, 255);
4853 human_readable(cur->memeasyfree * 1024, p, 255);
4856 human_readable(cur->memfree * 1024, p, 255);
4859 human_readable(cur->memmax * 1024, p, 255);
4863 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4867 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4868 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4873 if(output_methods & TO_X) {
4874 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4875 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4878 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4879 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4886 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4887 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4888 100, 1, obj->char_a, obj->char_b);
4893 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4896 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4899 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4903 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4904 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4907 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4908 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4911 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4912 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4915 OBJ(if_mixer_mute) {
4916 if (!mixer_is_mute(obj->data.ifblock.i)) {
4922 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4924 OBJ(monitor_number) {
4925 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4928 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
4930 OBJ(desktop_number) {
4931 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
4934 if(cur->x11.desktop.name != NULL) {
4935 strncpy(p, cur->x11.desktop.name, p_max_size);
4942 update_mail_count(&obj->data.local_mail);
4943 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4946 update_mail_count(&obj->data.local_mail);
4947 snprintf(p, p_max_size, "%d",
4948 obj->data.local_mail.new_mail_count);
4951 update_mail_count(&obj->data.local_mail);
4952 snprintf(p, p_max_size, "%d",
4953 obj->data.local_mail.seen_mail_count);
4956 update_mail_count(&obj->data.local_mail);
4957 snprintf(p, p_max_size, "%d",
4958 obj->data.local_mail.unseen_mail_count);
4960 OBJ(flagged_mails) {
4961 update_mail_count(&obj->data.local_mail);
4962 snprintf(p, p_max_size, "%d",
4963 obj->data.local_mail.flagged_mail_count);
4965 OBJ(unflagged_mails) {
4966 update_mail_count(&obj->data.local_mail);
4967 snprintf(p, p_max_size, "%d",
4968 obj->data.local_mail.unflagged_mail_count);
4970 OBJ(forwarded_mails) {
4971 update_mail_count(&obj->data.local_mail);
4972 snprintf(p, p_max_size, "%d",
4973 obj->data.local_mail.forwarded_mail_count);
4975 OBJ(unforwarded_mails) {
4976 update_mail_count(&obj->data.local_mail);
4977 snprintf(p, p_max_size, "%d",
4978 obj->data.local_mail.unforwarded_mail_count);
4980 OBJ(replied_mails) {
4981 update_mail_count(&obj->data.local_mail);
4982 snprintf(p, p_max_size, "%d",
4983 obj->data.local_mail.replied_mail_count);
4985 OBJ(unreplied_mails) {
4986 update_mail_count(&obj->data.local_mail);
4987 snprintf(p, p_max_size, "%d",
4988 obj->data.local_mail.unreplied_mail_count);
4991 update_mail_count(&obj->data.local_mail);
4992 snprintf(p, p_max_size, "%d",
4993 obj->data.local_mail.draft_mail_count);
4995 OBJ(trashed_mails) {
4996 update_mail_count(&obj->data.local_mail);
4997 snprintf(p, p_max_size, "%d",
4998 obj->data.local_mail.trashed_mail_count);
5001 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5003 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5006 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5009 new_outline(p, obj->data.l);
5012 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5014 OBJ(running_processes) {
5015 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5018 snprintf(p, p_max_size, "%s", obj->data.s);
5022 new_bg(p, obj->data.l);
5025 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5029 human_readable(cur->swap * 1024, p, 255);
5032 human_readable(cur->swapfree * 1024, p, 255);
5035 human_readable(cur->swapmax * 1024, p, 255);
5038 if (cur->swapmax == 0) {
5039 strncpy(p, "No swap", p_max_size);
5041 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5046 if(output_methods & TO_X) {
5047 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5048 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5051 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5052 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5058 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5061 time_t t = time(NULL);
5062 struct tm *tm = localtime(&t);
5064 setlocale(LC_TIME, "");
5065 strftime(p, p_max_size, obj->data.s, tm);
5068 time_t t = time(NULL);
5069 struct tm *tm = gmtime(&t);
5071 strftime(p, p_max_size, obj->data.s, tm);
5078 if (obj->data.tztime.tz) {
5079 oldTZ = getenv("TZ");
5080 setenv("TZ", obj->data.tztime.tz, 1);
5086 setlocale(LC_TIME, "");
5087 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5089 setenv("TZ", oldTZ, 1);
5094 // Needless to free oldTZ since getenv gives ptr to static data
5097 human_readable(obj->data.net->recv, p, 255);
5100 human_readable(obj->data.net->trans, p, 255);
5103 snprintf(p, p_max_size, "%d", total_updates);
5106 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5111 human_readable(obj->data.net->trans_speed, p, 255);
5114 spaced_print(p, p_max_size, "%.1f", 8,
5115 obj->data.net->trans_speed / 1024.0);
5119 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5120 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5124 format_seconds_short(p, p_max_size, (int) cur->uptime);
5127 format_seconds(p, p_max_size, (int) cur->uptime);
5130 snprintf(p, p_max_size, "%s", cur->users.names);
5133 snprintf(p, p_max_size, "%s", cur->users.terms);
5136 snprintf(p, p_max_size, "%s", cur->users.times);
5139 snprintf(p, p_max_size, "%d", cur->users.number);
5141 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5142 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5146 msg = get_apm_adapter();
5147 snprintf(p, p_max_size, "%s", msg);
5150 OBJ(apm_battery_life) {
5153 msg = get_apm_battery_life();
5154 snprintf(p, p_max_size, "%s", msg);
5157 OBJ(apm_battery_time) {
5160 msg = get_apm_battery_time();
5161 snprintf(p, p_max_size, "%s", msg);
5164 #endif /* __FreeBSD__ __OpenBSD__ */
5167 #define mpd_printf(fmt, val) \
5168 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5169 #define mpd_sprintf(val) { \
5170 if (!obj->data.i || obj->data.i > p_max_size) \
5171 mpd_printf("%s", val); \
5173 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5178 mpd_sprintf(artist);
5182 mpd_printf("%s", random);
5184 mpd_printf("%s", repeat);
5192 mpd_printf("%d", volume);
5194 mpd_printf("%d", bitrate);
5196 mpd_printf("%s", status);
5198 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5201 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5204 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5208 if(output_methods & TO_X) {
5209 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5210 (int) (mpd_get_info()->progress * 255.0f));
5213 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5214 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5220 struct mpd_s *mpd = mpd_get_info();
5221 int len = obj->data.i;
5222 if (len == 0 || len > p_max_size)
5225 memset(p, 0, p_max_size);
5226 if (mpd->artist && *mpd->artist &&
5227 mpd->title && *mpd->title) {
5228 snprintf(p, len, "%s - %s", mpd->artist,
5230 } else if (mpd->title && *mpd->title) {
5231 snprintf(p, len, "%s", mpd->title);
5232 } else if (mpd->artist && *mpd->artist) {
5233 snprintf(p, len, "%s", mpd->artist);
5234 } else if (mpd->file && *mpd->file) {
5235 snprintf(p, len, "%s", mpd->file);
5240 OBJ(if_mpd_playing) {
5241 if (!mpd_get_info()->is_playing) {
5250 #define MOC_PRINT(t, a) \
5251 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5253 MOC_PRINT(state, "??");
5256 MOC_PRINT(file, "no file");
5259 MOC_PRINT(title, "no title");
5262 MOC_PRINT(artist, "no artist");
5265 MOC_PRINT(song, "no song");
5268 MOC_PRINT(album, "no album");
5270 OBJ(moc_totaltime) {
5271 MOC_PRINT(totaltime, "0:00");
5274 MOC_PRINT(timeleft, "0:00");
5277 MOC_PRINT(curtime, "0:00");
5280 MOC_PRINT(bitrate, "0Kbps");
5283 MOC_PRINT(rate, "0KHz");
5289 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5292 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5295 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5298 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5300 OBJ(xmms2_comment) {
5301 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5304 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5307 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5310 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5312 OBJ(xmms2_tracknr) {
5313 if (cur->xmms2.tracknr != -1) {
5314 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5317 OBJ(xmms2_bitrate) {
5318 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5321 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5324 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5326 OBJ(xmms2_elapsed) {
5327 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5328 (cur->xmms2.elapsed / 1000) % 60);
5330 OBJ(xmms2_duration) {
5331 snprintf(p, p_max_size, "%02d:%02d",
5332 cur->xmms2.duration / 60000,
5333 (cur->xmms2.duration / 1000) % 60);
5335 OBJ(xmms2_percent) {
5336 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5340 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5341 (int) (cur->xmms2.progress * 255.0f));
5344 OBJ(xmms2_playlist) {
5345 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5347 OBJ(xmms2_timesplayed) {
5348 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5351 if (strlen(cur->xmms2.title) < 2
5352 && strlen(cur->xmms2.title) < 2) {
5353 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5355 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5359 OBJ(if_xmms2_connected) {
5360 if (cur->xmms2.conn_state != 1) {
5366 OBJ(audacious_status) {
5367 snprintf(p, p_max_size, "%s",
5368 cur->audacious.items[AUDACIOUS_STATUS]);
5370 OBJ(audacious_title) {
5371 snprintf(p, cur->audacious.max_title_len > 0
5372 ? cur->audacious.max_title_len : p_max_size, "%s",
5373 cur->audacious.items[AUDACIOUS_TITLE]);
5375 OBJ(audacious_length) {
5376 snprintf(p, p_max_size, "%s",
5377 cur->audacious.items[AUDACIOUS_LENGTH]);
5379 OBJ(audacious_length_seconds) {
5380 snprintf(p, p_max_size, "%s",
5381 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5383 OBJ(audacious_position) {
5384 snprintf(p, p_max_size, "%s",
5385 cur->audacious.items[AUDACIOUS_POSITION]);
5387 OBJ(audacious_position_seconds) {
5388 snprintf(p, p_max_size, "%s",
5389 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5391 OBJ(audacious_bitrate) {
5392 snprintf(p, p_max_size, "%s",
5393 cur->audacious.items[AUDACIOUS_BITRATE]);
5395 OBJ(audacious_frequency) {
5396 snprintf(p, p_max_size, "%s",
5397 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5399 OBJ(audacious_channels) {
5400 snprintf(p, p_max_size, "%s",
5401 cur->audacious.items[AUDACIOUS_CHANNELS]);
5403 OBJ(audacious_filename) {
5404 snprintf(p, p_max_size, "%s",
5405 cur->audacious.items[AUDACIOUS_FILENAME]);
5407 OBJ(audacious_playlist_length) {
5408 snprintf(p, p_max_size, "%s",
5409 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5411 OBJ(audacious_playlist_position) {
5412 snprintf(p, p_max_size, "%s",
5413 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5415 OBJ(audacious_main_volume) {
5416 snprintf(p, p_max_size, "%s",
5417 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5420 OBJ(audacious_bar) {
5424 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5425 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5426 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5429 #endif /* AUDACIOUS */
5433 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5436 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5439 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5442 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5445 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5448 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5451 /* we have four different types of top (top, top_mem,
5452 * top_time and top_io). To avoid having almost-same code four
5453 * times, we have this special handler. */
5456 parse_top_args("top", obj->data.top.s, obj);
5457 if (!needed) needed = cur->cpu;
5459 parse_top_args("top_mem", obj->data.top.s, obj);
5460 if (!needed) needed = cur->memu;
5462 parse_top_args("top_time", obj->data.top.s, obj);
5463 if (!needed) needed = cur->time;
5466 parse_top_args("top_io", obj->data.top.s, obj);
5467 if (!needed) needed = cur->io;
5470 if (needed[obj->data.top.num]) {
5473 switch (obj->data.top.type) {
5475 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5476 needed[obj->data.top.num]->name);
5479 snprintf(p, 7, "%6.2f",
5480 needed[obj->data.top.num]->amount);
5483 snprintf(p, 6, "%5i",
5484 needed[obj->data.top.num]->pid);
5487 snprintf(p, 7, "%6.2f",
5488 needed[obj->data.top.num]->totalmem);
5491 timeval = format_time(
5492 needed[obj->data.top.num]->total_cpu_time, 9);
5493 snprintf(p, 10, "%9s", timeval);
5497 human_readable(needed[obj->data.top.num]->rss,
5501 human_readable(needed[obj->data.top.num]->vsize,
5505 case TOP_READ_BYTES:
5506 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5509 case TOP_WRITE_BYTES:
5510 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5514 snprintf(p, 7, "%6.2f",
5515 needed[obj->data.top.num]->io_perc);
5521 print_tail_object(obj, p, p_max_size);
5523 print_head_object(obj, p, p_max_size);
5525 FILE *fp = open_file(obj->data.s, &obj->a);
5528 /* FIXME: use something more general (see also tail.c, head.c */
5529 #define BUFSZ 0x1000
5534 while(fgets(buf, BUFSZ, fp) != NULL){
5535 for(j = 0; buf[j] != 0; j++) {
5536 if(buf[j] == '\n') {
5541 sprintf(p, "%d", lines);
5544 sprintf(p, "File Unreadable");
5549 FILE *fp = open_file(obj->data.s, &obj->a);
5554 char inword = FALSE;
5557 while(fgets(buf, BUFSZ, fp) != NULL){
5558 for(j = 0; buf[j] != 0; j++) {
5559 if(!isspace(buf[j])) {
5560 if(inword == FALSE) {
5569 sprintf(p, "%d", words);
5572 sprintf(p, "File Unreadable");
5575 #ifdef TCP_PORT_MONITOR
5577 tcp_portmon_action(p, p_max_size,
5578 &obj->data.tcp_port_monitor);
5580 #endif /* TCP_PORT_MONITOR */
5584 iconv_converting = 1;
5585 iconv_selected = obj->a;
5588 iconv_converting = 0;
5591 #endif /* HAVE_ICONV */
5593 OBJ(entropy_avail) {
5594 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5597 percent_print(p, p_max_size,
5598 cur->entropy.entropy_avail *
5599 100 / cur->entropy.poolsize);
5601 OBJ(entropy_poolsize) {
5602 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5605 double entropy_perc;
5607 entropy_perc = (double) cur->entropy.entropy_avail /
5608 (double) cur->entropy.poolsize;
5610 if(output_methods & TO_X) {
5611 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5614 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5615 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5624 s = smapi_get_val(obj->data.s);
5625 snprintf(p, p_max_size, "%s", s);
5629 OBJ(if_smapi_bat_installed) {
5631 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5632 if(!smapi_bat_installed(idx)) {
5636 ERR("argument to if_smapi_bat_installed must be an integer");
5638 OBJ(smapi_bat_perc) {
5640 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5641 val = smapi_bat_installed(idx) ?
5642 smapi_get_bat_int(idx, "remaining_percent") : 0;
5643 percent_print(p, p_max_size, val);
5645 ERR("argument to smapi_bat_perc must be an integer");
5647 OBJ(smapi_bat_temp) {
5649 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5650 val = smapi_bat_installed(idx) ?
5651 smapi_get_bat_int(idx, "temperature") : 0;
5652 /* temperature is in milli degree celsius */
5653 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5655 ERR("argument to smapi_bat_temp must be an integer");
5657 OBJ(smapi_bat_power) {
5659 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5660 val = smapi_bat_installed(idx) ?
5661 smapi_get_bat_int(idx, "power_now") : 0;
5662 /* power_now is in mW, set to W with one digit precision */
5663 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5665 ERR("argument to smapi_bat_power must be an integer");
5668 OBJ(smapi_bat_bar) {
5669 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5670 new_bar(p, obj->a, obj->b, (int)
5671 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5673 new_bar(p, obj->a, obj->b, 0);
5678 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5679 char buf[max_user_text];
5682 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5683 snprintf(p, p_max_size, "%s", buf);
5684 if(total_updates % 2) {
5685 for(j=0; p[j] != 0; j++) {
5691 char buf[max_user_text];
5693 char unit[16]; //16 because we can also have long names (like mega-bytes)
5695 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5696 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5697 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5698 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5699 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5700 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5701 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5703 snprintf(p, p_max_size, "%s", buf);
5707 char *tmp, buf[max_user_text];
5708 generate_text_internal(buf, max_user_text,
5711 if (strlen(buf) <= obj->data.scroll.show) {
5712 snprintf(p, p_max_size, "%s", buf);
5715 #define LINESEPARATOR '|'
5716 //place all the lines behind each other with LINESEPARATOR between them
5717 for(j = 0; buf[j] != 0; j++) {
5719 buf[j]=LINESEPARATOR;
5722 //scroll the output obj->data.scroll.start places by copying that many chars from
5723 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5724 //at the back of the string
5725 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5726 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5727 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5728 buf[j - obj->data.scroll.start] = buf[j];
5730 strcpy(&buf[j - obj->data.scroll.start], tmp);
5732 //only show the requested number of chars
5733 if(obj->data.scroll.show < j) {
5734 buf[obj->data.scroll.show] = 0;
5736 //next time, scroll a place more or reset scrolling if we are at the end
5737 obj->data.scroll.start += obj->data.scroll.step;
5738 if(obj->data.scroll.start >= j){
5739 obj->data.scroll.start = 0;
5741 snprintf(p, p_max_size, "%s", buf);
5744 char buf[2][max_user_text];
5751 struct llrows* next;
5753 struct llrows *ll_rows[2], *current[2];
5754 struct text_object * objsub = obj->sub;
5757 for(i=0; i<2; i++) {
5760 ll_rows[i] = malloc(sizeof(struct llrows));
5761 current[i] = ll_rows[i];
5762 for(j=0; j<i; j++) objsub = objsub->sub;
5763 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5764 for(j=0; buf[i][j] != 0; j++) {
5765 if(buf[i][j] == '\t') buf[i][j] = ' ';
5766 if(buf[i][j] == '\n') {
5768 current[i]->row = strdup(buf[i]+nextstart);
5769 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5770 current[i]->next = malloc(sizeof(struct llrows));
5771 current[i] = current[i]->next;
5776 current[i]->row = strdup(buf[i]+nextstart);
5777 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5778 current[i]->next = NULL;
5779 current[i] = ll_rows[i];
5781 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5783 strcat(p, current[0]->row);
5784 i=strlen(current[0]->row);
5786 while(i < longest) {
5791 strcat(p, obj->data.combine.seperation);
5792 strcat(p, current[1]->row);
5796 #pragma omp parallel for
5797 #endif /* HAVE_OPENMP */
5798 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5801 #pragma omp parallel for
5802 #endif /* HAVE_OPENMP */
5803 for(i=0; i<2; i++) {
5804 while(ll_rows[i] != NULL) {
5805 current[i]=ll_rows[i];
5806 free(current[i]->row);
5807 ll_rows[i]=current[i]->next;
5814 int value = get_nvidia_value(obj->data.nvidia.type, display);
5816 snprintf(p, p_max_size, "N/A");
5817 else if (obj->data.nvidia.type == NV_TEMP)
5818 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5819 else if (obj->data.nvidia.print_as_float &&
5820 value > 0 && value < 100)
5821 snprintf(p, p_max_size, "%.1f", (float)value);
5823 snprintf(p, p_max_size, "%d", value);
5828 /* This is just a meta-object to set host:port */
5831 snprintf(p, p_max_size, "%s",
5832 cur->apcupsd.items[APCUPSD_NAME]);
5834 OBJ(apcupsd_model) {
5835 snprintf(p, p_max_size, "%s",
5836 cur->apcupsd.items[APCUPSD_MODEL]);
5838 OBJ(apcupsd_upsmode) {
5839 snprintf(p, p_max_size, "%s",
5840 cur->apcupsd.items[APCUPSD_UPSMODE]);
5842 OBJ(apcupsd_cable) {
5843 snprintf(p, p_max_size, "%s",
5844 cur->apcupsd.items[APCUPSD_CABLE]);
5846 OBJ(apcupsd_status) {
5847 snprintf(p, p_max_size, "%s",
5848 cur->apcupsd.items[APCUPSD_STATUS]);
5850 OBJ(apcupsd_linev) {
5851 snprintf(p, p_max_size, "%s",
5852 cur->apcupsd.items[APCUPSD_LINEV]);
5855 snprintf(p, p_max_size, "%s",
5856 cur->apcupsd.items[APCUPSD_LOAD]);
5858 OBJ(apcupsd_loadbar) {
5861 if(output_methods & TO_X) {
5862 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5863 new_bar(p, obj->a, obj->b, (int) progress);
5866 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5867 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5868 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5874 OBJ(apcupsd_loadgraph) {
5876 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5877 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5878 (int)progress, 100, 1, obj->char_a, obj->char_b);
5880 OBJ(apcupsd_loadgauge) {
5882 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5883 new_gauge(p, obj->a, obj->b,
5887 OBJ(apcupsd_charge) {
5888 snprintf(p, p_max_size, "%s",
5889 cur->apcupsd.items[APCUPSD_CHARGE]);
5891 OBJ(apcupsd_timeleft) {
5892 snprintf(p, p_max_size, "%s",
5893 cur->apcupsd.items[APCUPSD_TIMELEFT]);
5896 snprintf(p, p_max_size, "%s",
5897 cur->apcupsd.items[APCUPSD_TEMP]);
5899 OBJ(apcupsd_lastxfer) {
5900 snprintf(p, p_max_size, "%s",
5901 cur->apcupsd.items[APCUPSD_LASTXFER]);
5903 #endif /* APCUPSD */
5910 unsigned int a = strlen(p);
5913 if (a > 0 && iconv_converting && iconv_selected > 0
5914 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5916 size_t dummy1, dummy2;
5918 const char *ptr = buff_in;
5920 char *ptr = buff_in;
5924 dummy1 = dummy2 = a;
5926 strncpy(buff_in, p, p_max_size);
5928 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5929 while (dummy1 > 0) {
5930 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5933 ERR("Iconv codeset conversion failed");
5938 /* It is nessecary when we are converting from multibyte to
5939 * singlebyte codepage */
5942 #endif /* HAVE_ICONV */
5949 /* load any new fonts we may have had */
5950 if (need_to_load_fonts) {
5956 void evaluate(char *text, char *buffer)
5958 struct information *tmp_info;
5959 struct text_object subroot;
5961 tmp_info = malloc(sizeof(struct information));
5962 memcpy(tmp_info, &info, sizeof(struct information));
5963 parse_conky_vars(&subroot, text, buffer, tmp_info);
5964 DBGP("evaluated '%s' to '%s'", text, buffer);
5966 free_text_objects(&subroot, 1);
5970 double current_update_time, next_update_time, last_update_time;
5972 static void generate_text(void)
5974 struct information *cur = &info;
5981 current_update_time = get_time();
5985 /* add things to the buffer */
5991 generate_text_internal(p, max_user_text, global_root_object, cur);
5993 if (stuff_in_upper_case) {
5996 tmp_p = text_buffer;
5998 *tmp_p = toupper(*tmp_p);
6003 next_update_time += update_interval;
6004 if (next_update_time < get_time()) {
6005 next_update_time = get_time() + update_interval;
6006 } else if (next_update_time > get_time() + update_interval) {
6007 next_update_time = get_time() + update_interval;
6009 last_update_time = current_update_time;
6014 static inline int get_string_width(const char *s)
6017 if (output_methods & TO_X) {
6018 return *s ? calc_text_width(s, strlen(s)) : 0;
6024 static inline int get_string_width_special(char *s)
6032 if ((output_methods & TO_X) == 0) {
6034 return (s) ? strlen(s) : 0;
6042 p = strndup(s, text_buffer_size);
6046 if (*p == SPECIAL_CHAR) {
6047 /* shift everything over by 1 so that the special char
6048 * doesn't mess up the size calculation */
6049 for (i = 0; i < (long)strlen(p); i++) {
6050 *(p + i) = *(p + i + 1);
6052 if (specials[special_index + idx].type == GRAPH
6053 || specials[special_index + idx].type == GAUGE
6054 || specials[special_index + idx].type == BAR) {
6055 width += specials[special_index + idx].width;
6062 if (strlen(final) > 1) {
6063 width += calc_text_width(final, strlen(final));
6071 static void text_size_updater(char *s);
6073 int last_font_height;
6074 static void update_text_area(void)
6078 if ((output_methods & TO_X) == 0)
6080 /* update text size if it isn't fixed */
6085 text_width = minimum_width;
6088 last_font_height = font_height();
6089 for_each_line(text_buffer, text_size_updater);
6091 if (text_height < minimum_height) {
6092 text_height = minimum_height;
6094 if (text_width > maximum_width && maximum_width > 0) {
6095 text_width = maximum_width;
6099 /* get text position on workarea */
6100 switch (text_alignment) {
6107 x = workarea[2] - text_width - gap_x;
6112 x = workarea[2] / 2 - text_width / 2 - gap_x;
6119 y = workarea[3] - text_height - gap_y;
6123 x = workarea[2] - text_width - gap_x;
6124 y = workarea[3] - text_height - gap_y;
6128 x = workarea[2] / 2 - text_width / 2 - gap_x;
6129 y = workarea[3] - text_height - gap_y;
6134 y = workarea[3] / 2 - text_height / 2 - gap_y;
6138 x = workarea[2] - text_width - gap_x;
6139 y = workarea[3] / 2 - text_height / 2 - gap_y;
6143 case NONE: // Let the WM manage the window
6154 if (own_window && !fixed_pos) {
6157 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6158 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6159 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6160 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6164 /* If window size doesn't match to workarea's size,
6165 * then window probably includes panels (gnome).
6166 * Blah, doesn't work on KDE. */
6167 if (workarea[2] != window.width || workarea[3] != window.height) {
6176 /* update lua window globals */
6177 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6178 #endif /* HAVE_LUA */
6183 static int cur_x, cur_y; /* current x and y for drawing */
6185 //draw_mode also without X11 because we only need to print to stdout with FG
6186 static int draw_mode; /* FG, BG or OUTLINE */
6188 static long current_color;
6190 static void text_size_updater(char *s)
6195 if ((output_methods & TO_X) == 0)
6197 /* get string widths and skip specials */
6200 if (*p == SPECIAL_CHAR) {
6202 w += get_string_width(s);
6205 if (specials[special_index].type == BAR
6206 || specials[special_index].type == GAUGE
6207 || specials[special_index].type == GRAPH) {
6208 w += specials[special_index].width;
6209 if (specials[special_index].height > last_font_height) {
6210 last_font_height = specials[special_index].height;
6211 last_font_height += font_height();
6213 } else if (specials[special_index].type == OFFSET) {
6214 if (specials[special_index].arg > 0) {
6215 w += specials[special_index].arg;
6217 } else if (specials[special_index].type == VOFFSET) {
6218 last_font_height += specials[special_index].arg;
6219 } else if (specials[special_index].type == GOTO) {
6220 if (specials[special_index].arg > cur_x) {
6221 w = (int) specials[special_index].arg;
6223 } else if (specials[special_index].type == TAB) {
6224 int start = specials[special_index].arg;
6225 int step = specials[special_index].width;
6227 if (!step || step < 0) {
6230 w += step - (cur_x - text_start_x - start) % step;
6231 } else if (specials[special_index].type == FONT) {
6232 selected_font = specials[special_index].font_added;
6233 if (font_height() > last_font_height) {
6234 last_font_height = font_height();
6243 w += get_string_width(s);
6244 if (w > text_width) {
6247 if (text_width > maximum_width && maximum_width) {
6248 text_width = maximum_width;
6251 text_height += last_font_height;
6252 last_font_height = font_height();
6255 static inline void set_foreground_color(long c)
6257 if ((output_methods & TO_X) == 0)
6260 XSetForeground(display, window.gc, c);
6264 static void draw_string(const char *s)
6266 int i, i2, pos, width_of_s;
6274 width_of_s = get_string_width(s);
6275 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6277 fflush(stdout); /* output immediately, don't buffer */
6279 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6280 fprintf(stderr, "%s\n", s);
6281 fflush(stderr); /* output immediately, don't buffer */
6283 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6284 fprintf(overwrite_fpointer, "%s\n", s);
6286 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6287 fprintf(append_fpointer, "%s\n", s);
6289 memset(tmpstring1, 0, text_buffer_size);
6290 memset(tmpstring2, 0, text_buffer_size);
6291 strncpy(tmpstring1, s, text_buffer_size - 1);
6296 if (output_methods & TO_X) {
6297 max = ((text_width - width_of_s) / get_string_width(" "));
6300 /* This code looks for tabs in the text and coverts them to spaces.
6301 * The trick is getting the correct number of spaces, and not going
6302 * over the window's size without forcing the window larger. */
6303 for (i = 0; i < (int) text_buffer_size; i++) {
6304 if (tmpstring1[i] == '\t') {
6306 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6307 /* guard against overrun */
6308 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6313 /* guard against overrun */
6314 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6319 if (output_methods & TO_X) {
6320 if (text_width == maximum_width) {
6321 /* this means the text is probably pushing the limit,
6322 * so we'll chop it */
6323 while (cur_x + get_string_width(tmpstring2) - text_start_x
6324 > maximum_width && strlen(tmpstring2) > 0) {
6325 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6332 if (output_methods & TO_X) {
6338 c.pixel = current_color;
6339 XQueryColor(display, DefaultColormap(display, screen), &c);
6342 c2.color.red = c.red;
6343 c2.color.green = c.green;
6344 c2.color.blue = c.blue;
6345 c2.color.alpha = fonts[selected_font].font_alpha;
6347 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6348 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6350 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6351 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6356 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6359 cur_x += width_of_s;
6362 memcpy(tmpstring1, s, text_buffer_size);
6365 static void draw_line(char *s)
6373 if ((output_methods & TO_X) == 0) {
6379 cur_x = text_start_x;
6380 cur_y += font_ascent();
6381 font_h = font_height();
6383 /* find specials and draw stuff */
6386 if (*p == SPECIAL_CHAR) {
6389 /* draw string before special */
6396 switch (specials[special_index].type) {
6397 case HORIZONTAL_LINE:
6399 int h = specials[special_index].height;
6400 int mid = font_ascent() / 2;
6402 w = text_start_x + text_width - cur_x;
6404 XSetLineAttributes(display, window.gc, h, LineSolid,
6405 CapButt, JoinMiter);
6406 XDrawLine(display, window.drawable, window.gc, cur_x,
6407 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6413 int h = specials[special_index].height;
6414 int tmp_s = specials[special_index].arg;
6415 int mid = font_ascent() / 2;
6416 char ss[2] = { tmp_s, tmp_s };
6418 w = text_start_x + text_width - cur_x - 1;
6419 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6420 CapButt, JoinMiter);
6421 XSetDashes(display, window.gc, 0, ss, 2);
6422 XDrawLine(display, window.drawable, window.gc, cur_x,
6423 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6429 int h, bar_usage, by;
6430 if (cur_x - text_start_x > maximum_width
6431 && maximum_width > 0) {
6434 h = specials[special_index].height;
6435 bar_usage = specials[special_index].arg;
6436 by = cur_y - (font_ascent() / 2) - 1;
6441 w = specials[special_index].width;
6443 w = text_start_x + text_width - cur_x - 1;
6449 XSetLineAttributes(display, window.gc, 1, LineSolid,
6450 CapButt, JoinMiter);
6452 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6454 XFillRectangle(display, window.drawable, window.gc, cur_x,
6455 by, w * bar_usage / 255, h);
6463 case GAUGE: /* new GAUGE */
6466 unsigned long last_colour = current_color;
6468 float angle, px, py;
6472 if (cur_x - text_start_x > maximum_width
6473 && maximum_width > 0) {
6477 h = specials[special_index].height;
6478 by = cur_y - (font_ascent() / 2) - 1;
6483 w = specials[special_index].width;
6485 w = text_start_x + text_width - cur_x - 1;
6491 XSetLineAttributes(display, window.gc, 1, LineSolid,
6492 CapButt, JoinMiter);
6494 XDrawArc(display, window.drawable, window.gc,
6495 cur_x, by, w, h * 2, 0, 180*64);
6498 usage = specials[special_index].arg;
6499 angle = (M_PI)*(float)(usage)/255.;
6500 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6501 py = (float)(by+(h))-(float)(h)*sin(angle);
6503 XDrawLine(display, window.drawable, window.gc,
6504 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6512 set_foreground_color(last_colour);
6520 int h, by, i = 0, j = 0;
6522 unsigned long last_colour = current_color;
6523 unsigned long *tmpcolour = 0;
6524 if (cur_x - text_start_x > maximum_width
6525 && maximum_width > 0) {
6528 h = specials[special_index].height;
6529 by = cur_y - (font_ascent() / 2) - 1;
6534 w = specials[special_index].width;
6536 w = text_start_x + text_width - cur_x - 1;
6541 if (draw_graph_borders) {
6542 XSetLineAttributes(display, window.gc, 1, LineSolid,
6543 CapButt, JoinMiter);
6544 XDrawRectangle(display, window.drawable, window.gc,
6547 XSetLineAttributes(display, window.gc, 1, LineSolid,
6548 CapButt, JoinMiter);
6550 if (specials[special_index].last_colour != 0
6551 || specials[special_index].first_colour != 0) {
6552 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6555 for (i = w - 2; i > -1; i--) {
6556 if (specials[special_index].last_colour != 0
6557 || specials[special_index].first_colour != 0) {
6558 if (specials[special_index].tempgrad) {
6561 (int)((float)(w - 2) - specials[special_index].graph[j] *
6562 (w - 2) / (float)specials[special_index].graph_scale)
6566 (int)((float)(w - 2) - specials[special_index].graph[j] *
6567 (w - 2) / (float)specials[special_index].graph_scale)
6570 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6572 (int)((float)(w - 2) - specials[special_index].graph[j] *
6573 (w - 2) / (float)specials[special_index].graph_scale)
6577 #endif /* DEBUG_lol */
6578 XSetForeground(display, window.gc, tmpcolour[
6579 (int)((float)(w - 2) - specials[special_index].graph[j] *
6580 (w - 2) / (float)specials[special_index].graph_scale)
6583 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6586 /* this is mugfugly, but it works */
6587 XDrawLine(display, window.drawable, window.gc,
6588 cur_x + i + 1, by + h, cur_x + i + 1,
6589 round_to_int((double)by + h - specials[special_index].graph[j] *
6590 (h - 1) / specials[special_index].graph_scale));
6591 if ((w - i) / ((float) (w - 2) /
6592 (specials[special_index].graph_width)) > j
6593 && j < MAX_GRAPH_DEPTH - 3) {
6597 if (tmpcolour) free(tmpcolour);
6602 /* if (draw_mode == BG) {
6603 set_foreground_color(default_bg_color);
6604 } else if (draw_mode == OUTLINE) {
6605 set_foreground_color(default_out_color);
6607 set_foreground_color(default_fg_color);
6609 if (show_graph_range) {
6612 unsigned short int seconds = update_interval * w;
6617 unsigned short int timeunits;
6619 timeunits = seconds / 86400; seconds %= 86400;
6620 if (timeunits > 0) {
6621 asprintf(&tmp_day_str, "%dd", timeunits);
6623 tmp_day_str = strdup("");
6625 timeunits = seconds / 3600; seconds %= 3600;
6626 if (timeunits > 0) {
6627 asprintf(&tmp_hour_str, "%dh", timeunits);
6629 tmp_hour_str = strdup("");
6631 timeunits = seconds / 60; seconds %= 60;
6632 if (timeunits > 0) {
6633 asprintf(&tmp_min_str, "%dm", timeunits);
6635 tmp_min_str = strdup("");
6638 asprintf(&tmp_sec_str, "%ds", seconds);
6640 tmp_sec_str = strdup("");
6642 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6643 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6645 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6647 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6648 cur_y += font_h / 2;
6649 draw_string(tmp_str);
6655 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6658 cur_x += font_ascent() / 2;
6659 cur_y += font_h / 2;
6661 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6663 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6664 draw_string(tmp_str);
6670 set_foreground_color(last_colour);
6676 int old = font_ascent();
6678 cur_y -= font_ascent();
6679 selected_font = specials[special_index].font_added;
6681 if (cur_y + font_ascent() < cur_y + old) {
6684 cur_y += font_ascent();
6686 font_h = font_height();
6690 if (draw_mode == FG) {
6691 set_foreground_color(specials[special_index].arg);
6696 if (draw_mode == BG) {
6697 set_foreground_color(specials[special_index].arg);
6702 if (draw_mode == OUTLINE) {
6703 set_foreground_color(specials[special_index].arg);
6708 w += specials[special_index].arg;
6712 cur_y += specials[special_index].arg;
6716 if (specials[special_index].arg >= 0) {
6717 cur_x = (int) specials[special_index].arg;
6723 int start = specials[special_index].arg;
6724 int step = specials[special_index].width;
6726 if (!step || step < 0) {
6729 w = step - (cur_x - text_start_x - start) % step;
6735 /* TODO: add back in "+ window.border_inner_margin" to the end of
6737 int pos_x = text_start_x + text_width -
6738 get_string_width_special(s);
6740 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6741 "get_string_width(p) %i gap_x %i "
6742 "specials[special_index].arg %i window.border_inner_margin %i "
6743 "window.border_width %i\n", pos_x, text_start_x, text_width,
6744 cur_x, get_string_width_special(s), gap_x,
6745 specials[special_index].arg, window.border_inner_margin,
6746 window.border_width); */
6747 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6748 cur_x = pos_x - specials[special_index].arg;
6755 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6756 2 - (cur_x - text_start_x);
6757 /* int pos_x = text_start_x + text_width / 2 -
6758 get_string_width_special(s) / 2; */
6760 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6761 "get_string_width(p) %i gap_x %i "
6762 "specials[special_index].arg %i\n", pos_x, text_start_x,
6763 text_width, cur_x, get_string_width(s), gap_x,
6764 specials[special_index].arg); */
6765 if (pos_x > specials[special_index].arg) {
6766 w = pos_x - specials[special_index].arg;
6780 if (cur_y_add > 0) {
6784 cur_y += font_descent();
6789 static void draw_text(void)
6793 llua_draw_pre_hook();
6794 #endif /* HAVE_LUA */
6795 if (output_methods & TO_X) {
6796 cur_y = text_start_y;
6799 if (draw_borders && window.border_width > 0) {
6800 if (stippled_borders) {
6801 char ss[2] = { stippled_borders, stippled_borders };
6802 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6803 CapButt, JoinMiter);
6804 XSetDashes(display, window.gc, 0, ss, 2);
6806 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6807 CapButt, JoinMiter);
6810 XDrawRectangle(display, window.drawable, window.gc,
6811 text_start_x - window.border_inner_margin - window.border_width,
6812 text_start_y - window.border_inner_margin - window.border_width,
6813 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6814 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6822 for_each_line(text_buffer, draw_line);
6823 #if defined(HAVE_LUA) && defined(X11)
6824 llua_draw_post_hook();
6825 #endif /* HAVE_LUA */
6828 static void draw_stuff(void)
6830 if (overwrite_file) {
6831 overwrite_fpointer = fopen(overwrite_file, "w");
6832 if(!overwrite_fpointer)
6833 ERR("Can't overwrite '%s' anymore", overwrite_file);
6836 append_fpointer = fopen(append_file, "a");
6837 if(!append_fpointer)
6838 ERR("Can't append '%s' anymore", append_file);
6841 if (output_methods & TO_X) {
6843 if (draw_shades && !draw_outline) {
6846 set_foreground_color(default_bg_color);
6857 for (i = -1; i < 2; i++) {
6858 for (j = -1; j < 2; j++) {
6859 if (i == 0 && j == 0) {
6864 set_foreground_color(default_out_color);
6865 draw_mode = OUTLINE;
6873 set_foreground_color(default_fg_color);
6879 if (output_methods & TO_X) {
6884 swap.swap_window = window.window;
6885 swap.swap_action = XdbeBackground;
6886 XdbeSwapBuffers(display, &swap, 1);
6891 if(overwrite_fpointer) {
6892 fclose(overwrite_fpointer);
6893 overwrite_fpointer = 0;
6895 if(append_fpointer) {
6896 fclose(append_fpointer);
6897 append_fpointer = 0;
6902 static void clear_text(int exposures)
6906 /* The swap action is XdbeBackground, which clears */
6910 if (display && window.window) { // make sure these are !null
6911 /* there is some extra space for borders and outlines */
6912 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
6913 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
6914 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
6915 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
6920 static int need_to_update;
6922 /* update_text() generates new text and clears old text area */
6923 static void update_text(void)
6930 if (output_methods & TO_X)
6936 #ifdef HAVE_SYS_INOTIFY_H
6940 static void main_loop(void)
6943 #ifdef SIGNAL_BLOCKING
6944 sigset_t newmask, oldmask;
6947 #ifdef HAVE_SYS_INOTIFY_H
6948 int inotify_config_wd = -1;
6949 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
6950 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
6951 char inotify_buff[INOTIFY_BUF_LEN];
6952 #endif /* HAVE_SYS_INOTIFY_H */
6955 #ifdef SIGNAL_BLOCKING
6956 sigemptyset(&newmask);
6957 sigaddset(&newmask, SIGINT);
6958 sigaddset(&newmask, SIGTERM);
6959 sigaddset(&newmask, SIGUSR1);
6962 last_update_time = 0.0;
6963 next_update_time = get_time();
6965 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
6968 #ifdef SIGNAL_BLOCKING
6969 /* block signals. we will inspect for pending signals later */
6970 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6971 CRIT_ERR("unable to sigprocmask()");
6976 if (output_methods & TO_X) {
6979 /* wait for X event or timeout */
6981 if (!XPending(display)) {
6985 t = next_update_time - get_time();
6989 } else if (t > update_interval) {
6990 t = update_interval;
6993 tv.tv_sec = (long) t;
6994 tv.tv_usec = (long) (t * 1000000) % 1000000;
6996 FD_SET(ConnectionNumber(display), &fdsr);
6998 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7000 if (errno != EINTR) {
7001 ERR("can't select(): %s", strerror(errno));
7011 if (need_to_update) {
7013 int wx = window.x, wy = window.y;
7023 /* resize window if it isn't right size */
7025 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7026 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7027 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7028 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7029 XResizeWindow(display, window.window, window.width,
7031 set_transparent_background(window.window);
7036 /* move window if it isn't in right position */
7037 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7038 XMoveWindow(display, window.window, window.x, window.y);
7043 if (changed && window.type == TYPE_PANEL) {
7046 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7049 switch (text_alignment) {
7076 set_struts(sidenum);
7087 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7088 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7089 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7090 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7091 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7096 /* handle X events */
7097 while (XPending(display)) {
7100 XNextEvent(display, &ev);
7107 r.width = ev.xexpose.width;
7108 r.height = ev.xexpose.height;
7109 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7114 case ReparentNotify:
7115 /* set background to ParentRelative for all parents */
7117 set_transparent_background(window.window);
7121 case ConfigureNotify:
7123 /* if window size isn't what expected, set fixed size */
7124 if (ev.xconfigure.width != window.width
7125 || ev.xconfigure.height != window.height) {
7126 if (window.width != 0 && window.height != 0) {
7130 /* clear old stuff before screwing up
7135 XWindowAttributes attrs;
7136 if (XGetWindowAttributes(display,
7137 window.window, &attrs)) {
7138 window.width = attrs.width;
7139 window.height = attrs.height;
7143 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7144 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7145 if (text_width > maximum_width
7146 && maximum_width > 0) {
7147 text_width = maximum_width;
7151 /* if position isn't what expected, set fixed pos
7152 * total_updates avoids setting fixed_pos when window
7153 * is set to weird locations when started */
7154 /* // this is broken
7155 if (total_updates >= 2 && !fixed_pos
7156 && (window.x != ev.xconfigure.x
7157 || window.y != ev.xconfigure.y)
7158 && (ev.xconfigure.x != 0
7159 || ev.xconfigure.y != 0)) {
7167 /* if an ordinary window with decorations */
7168 if ((window.type == TYPE_NORMAL)
7169 && (!TEST_HINT(window.hints,
7170 HINT_UNDECORATED))) {
7171 /* allow conky to hold input focus. */
7174 /* forward the click to the desktop window */
7175 XUngrabPointer(display, ev.xbutton.time);
7176 ev.xbutton.window = window.desktop;
7177 ev.xbutton.x = ev.xbutton.x_root;
7178 ev.xbutton.y = ev.xbutton.y_root;
7179 XSendEvent(display, ev.xbutton.window, False,
7180 ButtonPressMask, &ev);
7181 XSetInputFocus(display, ev.xbutton.window,
7182 RevertToParent, ev.xbutton.time);
7189 /* if an ordinary window with decorations */
7190 if ((window.type == TYPE_NORMAL)
7191 && (!TEST_HINT(window.hints,
7192 HINT_UNDECORATED))) {
7193 /* allow conky to hold input focus. */
7196 /* forward the release to the desktop window */
7197 ev.xbutton.window = window.desktop;
7198 ev.xbutton.x = ev.xbutton.x_root;
7199 ev.xbutton.y = ev.xbutton.y_root;
7200 XSendEvent(display, ev.xbutton.window, False,
7201 ButtonReleaseMask, &ev);
7210 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7211 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7213 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7214 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7216 #endif /* HAVE_XDAMAGE */
7222 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7223 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7224 #endif /* HAVE_XDAMAGE */
7226 /* XDBE doesn't seem to provide a way to clear the back buffer without
7227 * interfering with the front buffer, other than passing XdbeBackground
7228 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7229 * redraw the text even if it wasn't part of the exposed area. OTOH,
7230 * if we're not going to call draw_stuff at all, then no swap happens
7231 * and we can safely do nothing. */
7233 if (!XEmptyRegion(x11_stuff.region)) {
7238 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7239 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7240 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7241 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7242 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7245 XSetRegion(display, window.gc, x11_stuff.region);
7248 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7252 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7255 XDestroyRegion(x11_stuff.region);
7256 x11_stuff.region = XCreateRegion();
7260 t = (next_update_time - get_time()) * 1000000;
7261 if(t > 0) usleep((useconds_t)t);
7268 #ifdef SIGNAL_BLOCKING
7269 /* unblock signals of interest and let handler fly */
7270 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7271 CRIT_ERR("unable to sigprocmask()");
7275 switch (g_signal_pending) {
7278 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7283 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;
7368 #ifdef HAVE_SYS_INOTIFY_H
7369 if (inotify_fd != -1) {
7370 inotify_rm_watch(inotify_fd, inotify_config_wd);
7372 inotify_fd = inotify_config_wd = 0;
7374 #endif /* HAVE_SYS_INOTIFY_H */
7377 X11_destroy_window();
7381 static void load_config_file(const char *);
7382 static void load_config_file_x11(const char *);
7384 /* reload the config file */
7385 static void reload_config(void)
7387 timed_thread_destroy_registered_threads();
7389 if (info.cpu_usage) {
7390 free(info.cpu_usage);
7391 info.cpu_usage = NULL;
7402 #ifdef TCP_PORT_MONITOR
7403 tcp_portmon_clear();
7410 free_weather_info();
7414 #endif /* HAVE_LUA */
7417 X11_destroy_window();
7420 if (current_config) {
7422 load_config_file(current_config);
7423 load_config_file_x11(current_config);
7425 /* re-init specials array */
7426 if ((specials = realloc((void *) specials,
7427 sizeof(struct special_t) * max_specials)) == 0) {
7428 ERR("failed to realloc specials array");
7432 if (output_methods & TO_X) {
7433 X11_initialisation();
7436 extract_variable_text(global_text);
7442 tmpstring1 = malloc(text_buffer_size);
7443 memset(tmpstring1, 0, text_buffer_size);
7447 tmpstring2 = malloc(text_buffer_size);
7448 memset(tmpstring2, 0, text_buffer_size);
7452 text_buffer = malloc(max_user_text);
7453 memset(text_buffer, 0, max_user_text);
7455 X11_create_window();
7464 timed_thread_destroy_registered_threads();
7466 if (info.cpu_usage) {
7467 free(info.cpu_usage);
7468 info.cpu_usage = NULL;
7471 if (x_initialised == YES) {
7474 XdbeDeallocateBackBufferName(display, window.back_buffer);
7479 XDestroyWindow(display, window.window);
7480 XClearWindow(display, RootWindow(display, screen));
7485 XClearWindow(display, RootWindow(display, screen));
7492 free(fonts); //in set_default_configurations a font is set but not loaded
7497 for (i = 0; i < MAX_TEMPLATES; i++) {
7504 free_text_objects(&global_root_object, 0);
7523 free(current_config);
7526 #ifdef TCP_PORT_MONITOR
7527 tcp_portmon_clear();
7533 free_weather_info();
7537 #endif /* HAVE_LUA */
7540 for (i = 0; i < special_count; i++) {
7541 if (specials[i].type == GRAPH) {
7542 free(specials[i].graph);
7550 clear_diskio_stats();
7551 if(global_cpu != NULL) free(global_cpu);
7554 static int string_to_bool(const char *s)
7557 // Assumes an option without a true/false means true
7559 } else if (strcasecmp(s, "yes") == EQUAL) {
7561 } else if (strcasecmp(s, "true") == EQUAL) {
7563 } else if (strcasecmp(s, "1") == EQUAL) {
7570 static enum alignment string_to_alignment(const char *s)
7572 if (strcasecmp(s, "top_left") == EQUAL) {
7574 } else if (strcasecmp(s, "top_right") == EQUAL) {
7576 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7578 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7580 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7581 return BOTTOM_RIGHT;
7582 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7583 return BOTTOM_MIDDLE;
7584 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7586 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7587 return MIDDLE_RIGHT;
7588 } else if (strcasecmp(s, "tl") == EQUAL) {
7590 } else if (strcasecmp(s, "tr") == EQUAL) {
7592 } else if (strcasecmp(s, "tm") == EQUAL) {
7594 } else if (strcasecmp(s, "bl") == EQUAL) {
7596 } else if (strcasecmp(s, "br") == EQUAL) {
7597 return BOTTOM_RIGHT;
7598 } else if (strcasecmp(s, "bm") == EQUAL) {
7599 return BOTTOM_MIDDLE;
7600 } else if (strcasecmp(s, "ml") == EQUAL) {
7602 } else if (strcasecmp(s, "mr") == EQUAL) {
7603 return MIDDLE_RIGHT;
7604 } else if (strcasecmp(s, "none") == EQUAL) {
7612 static void set_default_configurations_for_x(void)
7614 default_fg_color = WhitePixel(display, screen);
7615 default_bg_color = BlackPixel(display, screen);
7616 default_out_color = BlackPixel(display, screen);
7617 color0 = default_fg_color;
7618 color1 = default_fg_color;
7619 color2 = default_fg_color;
7620 color3 = default_fg_color;
7621 color4 = default_fg_color;
7622 color5 = default_fg_color;
7623 color6 = default_fg_color;
7624 color7 = default_fg_color;
7625 color8 = default_fg_color;
7626 color9 = default_fg_color;
7630 static void set_default_configurations(void)
7638 fork_to_background = 0;
7639 total_run_times = 0;
7640 info.cpu_avg_samples = 2;
7641 info.net_avg_samples = 2;
7642 info.diskio_avg_samples = 2;
7647 format_human_readable = 1;
7654 mpd_env_host = getenv("MPD_HOST");
7655 mpd_env_port = getenv("MPD_PORT");
7657 if (!mpd_env_host || !strlen(mpd_env_host)) {
7658 mpd_set_host("localhost");
7660 /* MPD_HOST environment variable is set */
7661 char *mpd_hostpart = strchr(mpd_env_host, '@');
7662 if (!mpd_hostpart) {
7663 mpd_set_host(mpd_env_host);
7665 /* MPD_HOST contains a password */
7666 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7667 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7669 if (!strlen(mpd_hostpart + 1)) {
7670 mpd_set_host("localhost");
7672 mpd_set_host(mpd_hostpart + 1);
7675 mpd_set_password(mpd_password, 1);
7680 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7681 /* failed to set port from environment variable */
7682 mpd_set_port("6600");
7686 info.xmms2.artist = NULL;
7687 info.xmms2.album = NULL;
7688 info.xmms2.title = NULL;
7689 info.xmms2.genre = NULL;
7690 info.xmms2.comment = NULL;
7691 info.xmms2.url = NULL;
7692 info.xmms2.status = NULL;
7693 info.xmms2.playlist = NULL;
7695 use_spacer = NO_SPACER;
7697 output_methods = TO_X;
7699 output_methods = TO_STDOUT;
7702 show_graph_scale = 0;
7703 show_graph_range = 0;
7706 draw_graph_borders = 1;
7708 set_first_font("6x10");
7716 window.type = TYPE_NORMAL;
7718 strcpy(window.class_name, PACKAGE_NAME);
7719 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7721 stippled_borders = 0;
7722 window.border_inner_margin = 3;
7723 window.border_outer_margin = 1;
7724 window.border_width = 1;
7725 text_alignment = BOTTOM_LEFT;
7726 info.x11.monitor.number = 1;
7727 info.x11.monitor.current = 0;
7728 info.x11.desktop.current = 1;
7729 info.x11.desktop.number = 1;
7730 info.x11.desktop.name = NULL;
7733 for (i = 0; i < MAX_TEMPLATES; i++) {
7736 template[i] = strdup("");
7739 free(current_mail_spool);
7743 variable_substitute(MAIL_FILE, buf, 256);
7744 if (buf[0] != '\0') {
7745 current_mail_spool = strndup(buf, text_buffer_size);
7750 update_interval = 3.0;
7751 info.music_player_interval = 1.0;
7752 stuff_in_upper_case = 0;
7753 info.users.number = 1;
7755 #ifdef TCP_PORT_MONITOR
7756 /* set default connection limit */
7757 tcp_portmon_set_max_connections(0);
7761 /* returns 1 if you can overwrite or create the file at 'path' */
7762 static _Bool overwrite_works(const char *path)
7766 if (!(filepointer = fopen(path, "w")))
7768 fclose(filepointer);
7772 /* returns 1 if you can append or create the file at 'path' */
7773 static _Bool append_works(const char *path)
7777 if (!(filepointer = fopen(path, "a")))
7779 fclose(filepointer);
7785 /* WARNING, this type not in Xlib spec */
7786 int x11_error_handler(Display *d, XErrorEvent *err)
7787 __attribute__((noreturn));
7788 int x11_error_handler(Display *d, XErrorEvent *err)
7790 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7792 (long unsigned)err->display,
7793 (long)err->resourceid,
7803 int x11_ioerror_handler(Display *d)
7804 __attribute__((noreturn));
7805 int x11_ioerror_handler(Display *d)
7807 ERR("X Error: Display %lx\n",
7814 static void X11_initialisation(void)
7816 if (x_initialised == YES) return;
7817 output_methods |= TO_X;
7819 set_default_configurations_for_x();
7820 x_initialised = YES;
7823 /* WARNING, this type not in Xlib spec */
7824 XSetErrorHandler(&x11_error_handler);
7825 XSetIOErrorHandler(&x11_ioerror_handler);
7829 static void X11_destroy_window(void)
7831 /* this function only exists for the sake of consistency */
7832 if (output_methods & TO_X) {
7834 XDamageDestroy(display, x11_stuff.damage);
7835 XFixesDestroyRegion(display, x11_stuff.region2);
7836 XFixesDestroyRegion(display, x11_stuff.part);
7837 if (x11_stuff.region) {
7838 XDestroyRegion(x11_stuff.region);
7840 x11_stuff.region = NULL;
7841 #endif /* HAVE_XDAMAGE */
7847 static char **xargv = 0;
7848 static int xargc = 0;
7850 static void X11_create_window(void)
7852 if (output_methods & TO_X) {
7854 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7855 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7857 #else /* OWN_WINDOW */
7858 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7859 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7861 #endif /* OWN_WINDOW */
7865 update_text_area(); /* to position text/window on screen */
7868 if (own_window && !fixed_pos) {
7869 XMoveWindow(display, window.window, window.x, window.y);
7872 set_transparent_background(window.window);
7880 x11_stuff.region = XCreateRegion();
7882 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7883 ERR("Xdamage extension unavailable");
7885 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7886 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7887 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7888 #endif /* HAVE_XDAMAGE */
7891 update_text_area(); /* to get initial size of the window */
7894 /* setup lua window globals */
7895 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
7896 #endif /* HAVE_LUA */
7900 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7901 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7902 #define CONF2(a) if (strcasecmp(name, a) == 0)
7903 #define CONF(a) else CONF2(a)
7904 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7905 || strcasecmp(name, b) == 0)
7906 #define CONF_CONTINUE 1
7907 #define CONF_BREAK 2
7908 #define CONF_BUFF_SIZE 512
7910 static FILE *open_config_file(const char *f)
7912 #ifdef CONFIG_OUTPUT
7913 if (!strcmp(f, "==builtin==")) {
7914 return conf_cookie_open();
7916 #endif /* CONFIG_OUTPUT */
7917 return fopen(f, "r");
7920 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7924 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7927 remove_comments(buf);
7932 while (*p && isspace((int) *p)) {
7936 return CONF_CONTINUE; /* empty line */
7943 while (*p2 && !isspace((int) *p2)) {
7947 *p2 = '\0'; /* break at name's end */
7954 while (*p && isspace((int) *p)) {
7960 p2 = *value + strlen(*value);
7961 while (isspace((int) *(p2 - 1))) {
7970 static void load_config_file(const char *f)
7975 set_default_configurations();
7976 fp = open_config_file(f);
7980 DBGP("reading contents from config file '%s'", f);
7983 char buff[CONF_BUFF_SIZE], *name, *value;
7984 int ret = do_config_step(&line, fp, buff, &name, &value);
7985 if (ret == CONF_BREAK) {
7987 } else if (ret == CONF_CONTINUE) {
7993 /* don't listen if X is already initialised or
7994 * if we already know we don't want it */
7995 if(x_initialised == NO) {
7996 if (string_to_bool(value)) {
7997 output_methods &= TO_X;
7999 output_methods &= ~TO_X;
8000 x_initialised = NEVER;
8001 free(fonts); //in set_default_configurations a font is set but not loaded
8006 if (!value || x_initialised == YES) {
8011 disp = strdup(value);
8015 if (window.type == TYPE_DOCK)
8018 int a = string_to_alignment(value);
8029 CONF("background") {
8030 fork_to_background = string_to_bool(value);
8033 CONF2("background") {
8034 fork_to_background = string_to_bool(value);
8038 CONF("show_graph_scale") {
8039 show_graph_scale = string_to_bool(value);
8041 CONF("show_graph_range") {
8042 show_graph_range = string_to_bool(value);
8044 CONF("border_margin") {
8045 ERR("border_margin is deprecated, please use window.border_inner_margin instead");
8047 window.border_inner_margin = strtol(value, 0, 0);
8048 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8053 CONF("border_inner_margin") {
8055 window.border_inner_margin = strtol(value, 0, 0);
8056 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8061 CONF("border_outer_margin") {
8063 window.border_outer_margin = strtol(value, 0, 0);
8064 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8069 CONF("border_width") {
8071 window.border_width = strtol(value, 0, 0);
8072 if (window.border_width < 0) window.border_width = 0;
8078 #define TEMPLATE_CONF(n) \
8079 CONF("template"#n) { \
8081 free(template[n]); \
8082 template[n] = strdup(value); \
8099 info.mail = parse_mail_args(IMAP_TYPE, value);
8106 info.mail = parse_mail_args(POP3_TYPE, value);
8111 CONF("default_bar_size") {
8114 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8121 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8125 CONF("default_graph_size") {
8128 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8135 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8138 CONF("default_gauge_size") {
8141 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8148 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8155 mpd_set_host(value);
8161 if (value && mpd_set_port(value)) {
8165 CONF("mpd_password") {
8167 mpd_set_password(value, 0);
8173 CONF("music_player_interval") {
8175 info.music_player_interval = strtod(value, 0);
8181 CONF("sensor_device") {
8183 sensor_device = strtol(value, 0, 0);
8189 CONF("cpu_avg_samples") {
8191 cpu_avg_samples = strtol(value, 0, 0);
8192 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8195 info.cpu_avg_samples = cpu_avg_samples;
8201 CONF("net_avg_samples") {
8203 net_avg_samples = strtol(value, 0, 0);
8204 if (net_avg_samples < 1 || net_avg_samples > 14) {
8207 info.net_avg_samples = net_avg_samples;
8213 CONF("diskio_avg_samples") {
8215 diskio_avg_samples = strtol(value, 0, 0);
8216 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8219 info.diskio_avg_samples = diskio_avg_samples;
8227 CONF("double_buffer") {
8228 use_xdbe = string_to_bool(value);
8232 CONF("override_utf8_locale") {
8233 utf8_mode = string_to_bool(value);
8235 CONF("draw_borders") {
8236 draw_borders = string_to_bool(value);
8238 CONF("draw_graph_borders") {
8239 draw_graph_borders = string_to_bool(value);
8241 CONF("draw_shades") {
8242 draw_shades = string_to_bool(value);
8244 CONF("draw_outline") {
8245 draw_outline = string_to_bool(value);
8248 CONF("out_to_console") {
8249 if(string_to_bool(value))
8250 output_methods |= TO_STDOUT;
8252 CONF("out_to_stderr") {
8253 if(string_to_bool(value))
8254 output_methods |= TO_STDERR;
8256 CONF("overwrite_file") {
8257 if(overwrite_file) {
8258 free(overwrite_file);
8261 if(overwrite_works(value)) {
8262 overwrite_file = strdup(value);
8263 output_methods |= OVERWRITE_FILE;
8265 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8267 CONF("append_file") {
8272 if(append_works(value)) {
8273 append_file = strdup(value);
8274 output_methods |= APPEND_FILE;
8276 ERR("append_file won't be able to create/append '%s'", value);
8278 CONF("use_spacer") {
8280 if (strcasecmp(value, "left") == EQUAL) {
8281 use_spacer = LEFT_SPACER;
8282 } else if (strcasecmp(value, "right") == EQUAL) {
8283 use_spacer = RIGHT_SPACER;
8284 } else if (strcasecmp(value, "none") == EQUAL) {
8285 use_spacer = NO_SPACER;
8287 use_spacer = string_to_bool(value);
8288 ERR("use_spacer should have an argument of left, right, or"
8289 " none. '%s' seems to be some form of '%s', so"
8290 " defaulting to %s.", value,
8291 use_spacer ? "true" : "false",
8292 use_spacer ? "right" : "none");
8294 use_spacer = RIGHT_SPACER;
8296 use_spacer = NO_SPACER;
8300 ERR("use_spacer should have an argument. Defaulting to right.");
8301 use_spacer = RIGHT_SPACER;
8307 use_xft = string_to_bool(value);
8311 set_first_font(value);
8315 if (value && font_count >= 0) {
8316 fonts[0].font_alpha = atof(value) * 65535.0;
8323 if (string_to_bool(value)) {
8324 ERR("Xft not enabled at compile time");
8328 /* xftfont silently ignored when no Xft */
8331 /* xftalpha is silently ignored when no Xft */
8336 set_first_font(value);
8344 gap_x = atoi(value);
8351 gap_y = atoi(value);
8357 CONF("mail_spool") {
8361 variable_substitute(value, buffer, 256);
8363 if (buffer[0] != '\0') {
8364 if (current_mail_spool) {
8365 free(current_mail_spool);
8367 current_mail_spool = strndup(buffer, text_buffer_size);
8374 CONF("minimum_size") {
8376 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8378 if (sscanf(value, "%d", &minimum_width) != 1) {
8386 CONF("maximum_width") {
8388 if (sscanf(value, "%d", &maximum_width) != 1) {
8396 CONF("no_buffers") {
8397 no_buffers = string_to_bool(value);
8399 CONF("top_name_width") {
8401 if (sscanf(value, "%u", &top_name_width) != 1) {
8407 if (top_name_width >= max_user_text) {
8408 top_name_width = max_user_text - 1;
8411 CONF("top_cpu_separate") {
8412 cpu_separate = string_to_bool(value);
8414 CONF("short_units") {
8415 short_units = string_to_bool(value);
8417 CONF("format_human_readable") {
8418 format_human_readable = string_to_bool(value);
8420 CONF("pad_percents") {
8421 pad_percents = atoi(value);
8425 CONF("own_window") {
8427 own_window = string_to_bool(value);
8430 CONF("own_window_class") {
8432 memset(window.class_name, 0, sizeof(window.class_name));
8433 strncpy(window.class_name, value,
8434 sizeof(window.class_name) - 1);
8437 CONF("own_window_title") {
8439 memset(window.title, 0, sizeof(window.title));
8440 strncpy(window.title, value, sizeof(window.title) - 1);
8443 CONF("own_window_transparent") {
8445 set_transparent = string_to_bool(value);
8448 CONF("own_window_hints") {
8450 char *p_hint, *p_save;
8451 char delim[] = ", ";
8453 /* tokenize the value into individual hints */
8454 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8456 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8457 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8458 SET_HINT(window.hints, HINT_UNDECORATED);
8459 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8460 SET_HINT(window.hints, HINT_BELOW);
8461 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8462 SET_HINT(window.hints, HINT_ABOVE);
8463 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8464 SET_HINT(window.hints, HINT_STICKY);
8465 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8466 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8467 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8468 SET_HINT(window.hints, HINT_SKIP_PAGER);
8473 p_hint = strtok_r(NULL, delim, &p_save);
8474 } while (p_hint != NULL);
8480 CONF("own_window_type") {
8482 if (strncmp(value, "normal", 6) == EQUAL) {
8483 window.type = TYPE_NORMAL;
8484 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8485 window.type = TYPE_DESKTOP;
8486 } else if (strncmp(value, "dock", 4) == EQUAL) {
8487 window.type = TYPE_DOCK;
8488 text_alignment = TOP_LEFT;
8489 } else if (strncmp(value, "panel", 5) == EQUAL) {
8490 window.type = TYPE_PANEL;
8491 } else if (strncmp(value, "override", 8) == EQUAL) {
8492 window.type = TYPE_OVERRIDE;
8501 CONF("stippled_borders") {
8503 stippled_borders = strtol(value, 0, 0);
8505 stippled_borders = 4;
8509 CONF("imlib_cache_size") {
8511 cimlib_set_cache_size(atoi(value));
8514 CONF("imlib_cache_flush_interval") {
8516 cimlib_set_cache_flush_interval(atoi(value));
8521 CONF("update_interval") {
8523 update_interval = strtod(value, 0);
8527 if (info.music_player_interval == 0) {
8528 // default to update_interval
8529 info.music_player_interval = update_interval;
8532 CONF("total_run_times") {
8534 total_run_times = strtod(value, 0);
8540 stuff_in_upper_case = string_to_bool(value);
8542 CONF("max_specials") {
8544 max_specials = atoi(value);
8549 CONF("max_user_text") {
8551 max_user_text = atoi(value);
8556 CONF("text_buffer_size") {
8558 text_buffer_size = atoi(value);
8559 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8560 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8561 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8569 if (output_methods & TO_X) {
8570 X11_initialisation();
8579 global_text = (char *) malloc(1);
8580 global_text[0] = '\0';
8583 unsigned int l = strlen(global_text);
8585 char buf[CONF_BUFF_SIZE];
8587 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8593 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8601 /* Check for continuation of \\-\n. */
8602 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8603 global_text[l-1] = '\0';
8607 global_text = (char *) realloc(global_text, l + bl + 1);
8608 strcat(global_text, buf);
8610 if (strlen(global_text) > max_user_text) {
8615 if (strlen(global_text) < 1) {
8616 CRIT_ERR("no text supplied in configuration; exiting");
8618 global_text_lines = line + 1;
8621 #ifdef TCP_PORT_MONITOR
8622 CONF("max_port_monitor_connections") {
8624 if (!value || (sscanf(value, "%d", &max) != 1)) {
8625 /* an error. use default, warn and continue. */
8626 tcp_portmon_set_max_connections(0);
8628 } else if (tcp_portmon_set_max_connections(max)) {
8629 /* max is < 0, default has been set*/
8634 CONF("if_up_strictness") {
8636 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8637 ifup_strictness = IFUP_UP;
8638 } else if (strcasecmp(value, "up") == EQUAL) {
8639 ifup_strictness = IFUP_UP;
8640 } else if (strcasecmp(value, "link") == EQUAL) {
8641 ifup_strictness = IFUP_LINK;
8642 } else if (strcasecmp(value, "address") == EQUAL) {
8643 ifup_strictness = IFUP_ADDR;
8645 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8646 ifup_strictness = IFUP_UP;
8650 CONF("temperature_unit") {
8652 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8653 } else if (set_temp_output_unit(value)) {
8654 ERR("temperature_unit: incorrect argument");
8660 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8661 char *skey = malloc(maxlength);
8662 char *svalue = malloc(maxlength);
8664 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8665 oldvalue = getenv(skey);
8666 if (oldvalue == NULL) {
8667 setenv(skey, svalue, 0);
8669 //PS: Don't free oldvalue, it's the real envvar, not a copy
8682 char *ptr = strtok(value, " ");
8685 ptr = strtok(NULL, " ");
8692 CONF("lua_draw_hook_pre") {
8694 llua_set_draw_pre_hook(value);
8699 CONF("lua_draw_hook_post") {
8701 llua_set_draw_post_hook(value);
8707 #endif /* HAVE_LUA */
8719 CONF("default_color"){}
8720 CONF3("default_shade_color", "default_shadecolor"){}
8721 CONF3("default_outline_color", "default_outlinecolor") {}
8722 CONF("own_window_colour") {}
8725 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8731 if (info.music_player_interval == 0) {
8732 // default to update_interval
8733 info.music_player_interval = update_interval;
8735 if (!global_text) { // didn't supply any text
8736 CRIT_ERR("missing text block in configuration; exiting");
8740 static void load_config_file_x11(const char *f)
8745 fp = open_config_file(f);
8749 DBGP("reading contents from config file '%s'", f);
8752 char buff[CONF_BUFF_SIZE], *name, *value;
8753 int ret = do_config_step(&line, fp, buff, &name, &value);
8754 if (ret == CONF_BREAK) {
8756 } else if (ret == CONF_CONTINUE) {
8762 X11_initialisation();
8763 if (x_initialised == YES) {
8765 color0 = get_x11_color(value);
8772 X11_initialisation();
8773 if (x_initialised == YES) {
8775 color1 = get_x11_color(value);
8782 X11_initialisation();
8783 if (x_initialised == YES) {
8785 color2 = get_x11_color(value);
8792 X11_initialisation();
8793 if (x_initialised == YES) {
8795 color3 = get_x11_color(value);
8802 X11_initialisation();
8803 if (x_initialised == YES) {
8805 color4 = get_x11_color(value);
8812 X11_initialisation();
8813 if (x_initialised == YES) {
8815 color5 = get_x11_color(value);
8822 X11_initialisation();
8823 if (x_initialised == YES) {
8825 color6 = get_x11_color(value);
8832 X11_initialisation();
8833 if (x_initialised == YES) {
8835 color7 = get_x11_color(value);
8842 X11_initialisation();
8843 if (x_initialised == YES) {
8845 color8 = get_x11_color(value);
8852 X11_initialisation();
8853 if (x_initialised == YES) {
8855 color9 = get_x11_color(value);
8861 CONF("default_color") {
8862 X11_initialisation();
8863 if (x_initialised == YES) {
8865 default_fg_color = get_x11_color(value);
8871 CONF3("default_shade_color", "default_shadecolor") {
8872 X11_initialisation();
8873 if (x_initialised == YES) {
8875 default_bg_color = get_x11_color(value);
8881 CONF3("default_outline_color", "default_outlinecolor") {
8882 X11_initialisation();
8883 if (x_initialised == YES) {
8885 default_out_color = get_x11_color(value);
8892 CONF("own_window_colour") {
8893 X11_initialisation();
8894 if (x_initialised == YES) {
8896 background_colour = get_x11_color(value);
8898 ERR("Invalid colour for own_window_colour (try omitting the "
8899 "'#' for hex colours");
8905 //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
8906 if(output_methods & TO_X) {
8907 X11_initialisation();
8917 #undef CONF_CONTINUE
8918 #undef CONF_BUFF_SIZE
8925 static void print_help(const char *prog_name) {
8926 printf("Usage: %s [OPTION]...\n"
8927 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8928 "window. Command line options will override configurations defined in config\n"
8930 " -v, --version version\n"
8931 " -q, --quiet quiet mode\n"
8932 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
8933 " -c, --config=FILE config file to load\n"
8934 #ifdef CONFIG_OUTPUT
8935 " -C, --print-config print the builtin default config to stdout\n"
8936 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
8938 " -d, --daemonize daemonize, fork to background\n"
8939 " -h, --help help\n"
8941 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8942 " -f, --font=FONT font to use\n"
8943 " -X, --display=DISPLAY X11 display to use\n"
8945 " -o, --own-window create own window to draw\n"
8948 " -b, --double-buffer double buffer (prevents flickering)\n"
8950 " -w, --window-id=WIN_ID window id to draw\n"
8951 " -x X x position\n"
8952 " -y Y y position\n"
8954 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8955 " -u, --interval=SECS update interval\n"
8956 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
8961 /* : means that character before that takes an argument */
8962 static const char *getopt_string = "vVqdDt:u:i:hc:"
8972 #ifdef CONFIG_OUTPUT
8977 static const struct option longopts[] = {
8978 { "help", 0, NULL, 'h' },
8979 { "version", 0, NULL, 'V' },
8980 { "debug", 0, NULL, 'D' },
8981 { "config", 1, NULL, 'c' },
8982 #ifdef CONFIG_OUTPUT
8983 { "print-config", 0, NULL, 'C' },
8985 { "daemonize", 0, NULL, 'd' },
8987 { "alignment", 1, NULL, 'a' },
8988 { "font", 1, NULL, 'f' },
8989 { "display", 1, NULL, 'X' },
8991 { "own-window", 0, NULL, 'o' },
8994 { "double-buffer", 0, NULL, 'b' },
8996 { "window-id", 1, NULL, 'w' },
8998 { "text", 1, NULL, 't' },
8999 { "interval", 0, NULL, 'u' },
9003 int main(int argc, char **argv)
9009 struct sigaction act, oact;
9011 g_signal_pending = 0;
9012 max_user_text = MAX_USER_TEXT_DEFAULT;
9014 memset(&info, 0, sizeof(info));
9015 memset(template, 0, sizeof(template));
9018 #ifdef TCP_PORT_MONITOR
9019 /* set default connection limit */
9020 tcp_portmon_set_max_connections(0);
9023 /* handle command line parameters that don't change configs */
9025 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9026 || ((s = getenv("LANG")) && *s)) {
9027 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9029 ERR("malloc failed");
9031 for (x = 0; x < strlen(s); x++) {
9032 temp[x] = tolower(s[x]);
9035 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9041 if (!setlocale(LC_CTYPE, "")) {
9042 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9046 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9057 if (current_config) {
9058 free(current_config);
9060 current_config = strndup(optarg, max_user_text);
9063 freopen("/dev/null", "w", stderr);
9066 print_help(argv[0]);
9068 #ifdef CONFIG_OUTPUT
9075 window.window = strtol(optarg, 0, 0);
9084 /* check if specified config file is valid */
9085 if (current_config) {
9087 if (stat(current_config, &sb) ||
9088 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9089 ERR("invalid configuration file '%s'\n", current_config);
9090 free(current_config);
9095 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9097 if (!current_config) {
9098 /* load default config file */
9099 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9102 /* Try to use personal config file first */
9103 to_real_path(buf, CONFIG_FILE);
9104 if (buf[0] && (fp = fopen(buf, "r"))) {
9105 current_config = strndup(buf, max_user_text);
9109 /* Try to use system config file if personal config not readable */
9110 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9111 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9115 /* No readable config found */
9116 if (!current_config) {
9117 #ifdef CONFIG_OUTPUT
9118 current_config = strdup("==builtin==");
9119 ERR("no readable personal or system-wide config file found,"
9120 " using builtin default");
9122 CRIT_ERR("no readable personal or system-wide config file found");
9123 #endif /* ! CONF_OUTPUT */
9126 #ifdef HAVE_SYS_INOTIFY_H
9127 inotify_fd = inotify_init();
9128 #endif /* HAVE_SYS_INOTIFY_H */
9130 load_config_file(current_config);
9132 /* init specials array */
9133 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9134 ERR("failed to create specials array");
9138 if (current_mail_spool == NULL) {
9141 variable_substitute(MAIL_FILE, buf, 256);
9143 if (buf[0] != '\0') {
9144 current_mail_spool = strndup(buf, text_buffer_size);
9149 /* handle other command line arguments */
9151 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9152 || defined(__NetBSD__)
9153 optind = optreset = 1;
9158 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9159 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9160 "kvm_open")) == NULL) {
9161 CRIT_ERR("cannot read kvm");
9166 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9174 fork_to_background = 1;
9177 global_debug_level++;
9181 set_first_font(optarg);
9184 text_alignment = string_to_alignment(optarg);
9189 disp = strdup(optarg);
9208 global_text = strndup(optarg, max_user_text);
9209 convert_escapes(global_text);
9213 update_interval = strtod(optarg, 0);
9214 if (info.music_player_interval == 0) {
9215 // default to update_interval
9216 info.music_player_interval = update_interval;
9221 total_run_times = strtod(optarg, 0);
9225 gap_x = atoi(optarg);
9229 gap_y = atoi(optarg);
9240 if (output_methods & TO_X) {
9241 load_config_file_x11(current_config);
9245 /* generate text and get initial size */
9246 extract_variable_text(global_text);
9253 if (fork_to_background) {
9258 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9265 fprintf(stderr, "\n");
9270 /* parent process */
9271 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9278 text_buffer = malloc(max_user_text);
9279 memset(text_buffer, 0, max_user_text);
9280 tmpstring1 = malloc(text_buffer_size);
9281 memset(tmpstring1, 0, text_buffer_size);
9282 tmpstring2 = malloc(text_buffer_size);
9283 memset(tmpstring2, 0, text_buffer_size);
9288 X11_create_window();
9291 /* Set signal handlers */
9292 act.sa_handler = signal_handler;
9293 sigemptyset(&act.sa_mask);
9296 act.sa_flags |= SA_RESTART;
9299 if ( sigaction(SIGINT, &act, &oact) < 0
9300 || sigaction(SIGUSR1, &act, &oact) < 0
9301 || sigaction(SIGHUP, &act, &oact) < 0
9302 || sigaction(SIGTERM, &act, &oact) < 0) {
9303 ERR("error setting signal handler: %s", strerror(errno));
9308 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9315 static void signal_handler(int sig)
9317 /* signal handler is light as a feather, as it should be.
9318 * we will poll g_signal_pending with each loop of conky
9319 * and do any signal processing there, NOT here. */
9320 g_signal_pending = sig;